xref: /PHP-5.3/ext/sqlite3/libsqlite/sqlite3.c (revision 929e0f18)
1 #if defined(_MSC_VER) && _MSC_VER < 1300
2 #pragma optimize("", off)
3 #endif
4 
5 /******************************************************************************
6 ** This file is an amalgamation of many separate C source files from SQLite
7 ** version 3.7.7.1.  By combining all the individual C code files into this
8 ** single large file, the entire code can be compiled as a single translation
9 ** unit.  This allows many compilers to do optimizations that would not be
10 ** possible if the files were compiled separately.  Performance improvements
11 ** of 5% or more are commonly seen when SQLite is compiled as a single
12 ** translation unit.
13 **
14 ** This file is all you need to compile SQLite.  To use SQLite in other
15 ** programs, you need this file and the "sqlite3.h" header file that defines
16 ** the programming interface to the SQLite library.  (If you do not have
17 ** the "sqlite3.h" header file at hand, you will find a copy embedded within
18 ** the text of this file.  Search for "Begin file sqlite3.h" to find the start
19 ** of the embedded sqlite3.h header file.) Additional code files may be needed
20 ** if you want a wrapper to interface SQLite with your choice of programming
21 ** language. The code for the "sqlite3" command-line shell is also in a
22 ** separate file. This file contains only code for the core SQLite library.
23 */
24 #define SQLITE_CORE 1
25 #define SQLITE_AMALGAMATION 1
26 #ifndef SQLITE_PRIVATE
27 # define SQLITE_PRIVATE static
28 #endif
29 #ifndef SQLITE_API
30 # define SQLITE_API
31 #endif
32 /************** Begin file sqliteInt.h ***************************************/
33 /*
34 ** 2001 September 15
35 **
36 ** The author disclaims copyright to this source code.  In place of
37 ** a legal notice, here is a blessing:
38 **
39 **    May you do good and not evil.
40 **    May you find forgiveness for yourself and forgive others.
41 **    May you share freely, never taking more than you give.
42 **
43 *************************************************************************
44 ** Internal interface definitions for SQLite.
45 **
46 */
47 #ifndef _SQLITEINT_H_
48 #define _SQLITEINT_H_
49 
50 /*
51 ** These #defines should enable >2GB file support on POSIX if the
52 ** underlying operating system supports it.  If the OS lacks
53 ** large file support, or if the OS is windows, these should be no-ops.
54 **
55 ** Ticket #2739:  The _LARGEFILE_SOURCE macro must appear before any
56 ** system #includes.  Hence, this block of code must be the very first
57 ** code in all source files.
58 **
59 ** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch
60 ** on the compiler command line.  This is necessary if you are compiling
61 ** on a recent machine (ex: Red Hat 7.2) but you want your code to work
62 ** on an older machine (ex: Red Hat 6.0).  If you compile on Red Hat 7.2
63 ** without this option, LFS is enable.  But LFS does not exist in the kernel
64 ** in Red Hat 6.0, so the code won't work.  Hence, for maximum binary
65 ** portability you should omit LFS.
66 **
67 ** Similar is true for Mac OS X.  LFS is only supported on Mac OS X 9 and later.
68 */
69 #ifndef SQLITE_DISABLE_LFS
70 # define _LARGE_FILE       1
71 # ifndef _FILE_OFFSET_BITS
72 #   define _FILE_OFFSET_BITS 64
73 # endif
74 # define _LARGEFILE_SOURCE 1
75 #endif
76 
77 /*
78 ** Include the configuration header output by 'configure' if we're using the
79 ** autoconf-based build
80 */
81 #ifdef _HAVE_SQLITE_CONFIG_H
82 #include "config.h"
83 #endif
84 
85 /************** Include sqliteLimit.h in the middle of sqliteInt.h ***********/
86 /************** Begin file sqliteLimit.h *************************************/
87 /*
88 ** 2007 May 7
89 **
90 ** The author disclaims copyright to this source code.  In place of
91 ** a legal notice, here is a blessing:
92 **
93 **    May you do good and not evil.
94 **    May you find forgiveness for yourself and forgive others.
95 **    May you share freely, never taking more than you give.
96 **
97 *************************************************************************
98 **
99 ** This file defines various limits of what SQLite can process.
100 */
101 
102 /*
103 ** The maximum length of a TEXT or BLOB in bytes.   This also
104 ** limits the size of a row in a table or index.
105 **
106 ** The hard limit is the ability of a 32-bit signed integer
107 ** to count the size: 2^31-1 or 2147483647.
108 */
109 #ifndef SQLITE_MAX_LENGTH
110 # define SQLITE_MAX_LENGTH 1000000000
111 #endif
112 
113 /*
114 ** This is the maximum number of
115 **
116 **    * Columns in a table
117 **    * Columns in an index
118 **    * Columns in a view
119 **    * Terms in the SET clause of an UPDATE statement
120 **    * Terms in the result set of a SELECT statement
121 **    * Terms in the GROUP BY or ORDER BY clauses of a SELECT statement.
122 **    * Terms in the VALUES clause of an INSERT statement
123 **
124 ** The hard upper limit here is 32676.  Most database people will
125 ** tell you that in a well-normalized database, you usually should
126 ** not have more than a dozen or so columns in any table.  And if
127 ** that is the case, there is no point in having more than a few
128 ** dozen values in any of the other situations described above.
129 */
130 #ifndef SQLITE_MAX_COLUMN
131 # define SQLITE_MAX_COLUMN 2000
132 #endif
133 
134 /*
135 ** The maximum length of a single SQL statement in bytes.
136 **
137 ** It used to be the case that setting this value to zero would
138 ** turn the limit off.  That is no longer true.  It is not possible
139 ** to turn this limit off.
140 */
141 #ifndef SQLITE_MAX_SQL_LENGTH
142 # define SQLITE_MAX_SQL_LENGTH 1000000000
143 #endif
144 
145 /*
146 ** The maximum depth of an expression tree. This is limited to
147 ** some extent by SQLITE_MAX_SQL_LENGTH. But sometime you might
148 ** want to place more severe limits on the complexity of an
149 ** expression.
150 **
151 ** A value of 0 used to mean that the limit was not enforced.
152 ** But that is no longer true.  The limit is now strictly enforced
153 ** at all times.
154 */
155 #ifndef SQLITE_MAX_EXPR_DEPTH
156 # define SQLITE_MAX_EXPR_DEPTH 1000
157 #endif
158 
159 /*
160 ** The maximum number of terms in a compound SELECT statement.
161 ** The code generator for compound SELECT statements does one
162 ** level of recursion for each term.  A stack overflow can result
163 ** if the number of terms is too large.  In practice, most SQL
164 ** never has more than 3 or 4 terms.  Use a value of 0 to disable
165 ** any limit on the number of terms in a compount SELECT.
166 */
167 #ifndef SQLITE_MAX_COMPOUND_SELECT
168 # define SQLITE_MAX_COMPOUND_SELECT 500
169 #endif
170 
171 /*
172 ** The maximum number of opcodes in a VDBE program.
173 ** Not currently enforced.
174 */
175 #ifndef SQLITE_MAX_VDBE_OP
176 # define SQLITE_MAX_VDBE_OP 25000
177 #endif
178 
179 /*
180 ** The maximum number of arguments to an SQL function.
181 */
182 #ifndef SQLITE_MAX_FUNCTION_ARG
183 # define SQLITE_MAX_FUNCTION_ARG 127
184 #endif
185 
186 /*
187 ** The maximum number of in-memory pages to use for the main database
188 ** table and for temporary tables.  The SQLITE_DEFAULT_CACHE_SIZE
189 */
190 #ifndef SQLITE_DEFAULT_CACHE_SIZE
191 # define SQLITE_DEFAULT_CACHE_SIZE  2000
192 #endif
193 #ifndef SQLITE_DEFAULT_TEMP_CACHE_SIZE
194 # define SQLITE_DEFAULT_TEMP_CACHE_SIZE  500
195 #endif
196 
197 /*
198 ** The default number of frames to accumulate in the log file before
199 ** checkpointing the database in WAL mode.
200 */
201 #ifndef SQLITE_DEFAULT_WAL_AUTOCHECKPOINT
202 # define SQLITE_DEFAULT_WAL_AUTOCHECKPOINT  1000
203 #endif
204 
205 /*
206 ** The maximum number of attached databases.  This must be between 0
207 ** and 62.  The upper bound on 62 is because a 64-bit integer bitmap
208 ** is used internally to track attached databases.
209 */
210 #ifndef SQLITE_MAX_ATTACHED
211 # define SQLITE_MAX_ATTACHED 10
212 #endif
213 
214 
215 /*
216 ** The maximum value of a ?nnn wildcard that the parser will accept.
217 */
218 #ifndef SQLITE_MAX_VARIABLE_NUMBER
219 # define SQLITE_MAX_VARIABLE_NUMBER 999
220 #endif
221 
222 /* Maximum page size.  The upper bound on this value is 65536.  This a limit
223 ** imposed by the use of 16-bit offsets within each page.
224 **
225 ** Earlier versions of SQLite allowed the user to change this value at
226 ** compile time. This is no longer permitted, on the grounds that it creates
227 ** a library that is technically incompatible with an SQLite library
228 ** compiled with a different limit. If a process operating on a database
229 ** with a page-size of 65536 bytes crashes, then an instance of SQLite
230 ** compiled with the default page-size limit will not be able to rollback
231 ** the aborted transaction. This could lead to database corruption.
232 */
233 #ifdef SQLITE_MAX_PAGE_SIZE
234 # undef SQLITE_MAX_PAGE_SIZE
235 #endif
236 #define SQLITE_MAX_PAGE_SIZE 65536
237 
238 
239 /*
240 ** The default size of a database page.
241 */
242 #ifndef SQLITE_DEFAULT_PAGE_SIZE
243 # define SQLITE_DEFAULT_PAGE_SIZE 1024
244 #endif
245 #if SQLITE_DEFAULT_PAGE_SIZE>SQLITE_MAX_PAGE_SIZE
246 # undef SQLITE_DEFAULT_PAGE_SIZE
247 # define SQLITE_DEFAULT_PAGE_SIZE SQLITE_MAX_PAGE_SIZE
248 #endif
249 
250 /*
251 ** Ordinarily, if no value is explicitly provided, SQLite creates databases
252 ** with page size SQLITE_DEFAULT_PAGE_SIZE. However, based on certain
253 ** device characteristics (sector-size and atomic write() support),
254 ** SQLite may choose a larger value. This constant is the maximum value
255 ** SQLite will choose on its own.
256 */
257 #ifndef SQLITE_MAX_DEFAULT_PAGE_SIZE
258 # define SQLITE_MAX_DEFAULT_PAGE_SIZE 8192
259 #endif
260 #if SQLITE_MAX_DEFAULT_PAGE_SIZE>SQLITE_MAX_PAGE_SIZE
261 # undef SQLITE_MAX_DEFAULT_PAGE_SIZE
262 # define SQLITE_MAX_DEFAULT_PAGE_SIZE SQLITE_MAX_PAGE_SIZE
263 #endif
264 
265 
266 /*
267 ** Maximum number of pages in one database file.
268 **
269 ** This is really just the default value for the max_page_count pragma.
270 ** This value can be lowered (or raised) at run-time using that the
271 ** max_page_count macro.
272 */
273 #ifndef SQLITE_MAX_PAGE_COUNT
274 # define SQLITE_MAX_PAGE_COUNT 1073741823
275 #endif
276 
277 /*
278 ** Maximum length (in bytes) of the pattern in a LIKE or GLOB
279 ** operator.
280 */
281 #ifndef SQLITE_MAX_LIKE_PATTERN_LENGTH
282 # define SQLITE_MAX_LIKE_PATTERN_LENGTH 50000
283 #endif
284 
285 /*
286 ** Maximum depth of recursion for triggers.
287 **
288 ** A value of 1 means that a trigger program will not be able to itself
289 ** fire any triggers. A value of 0 means that no trigger programs at all
290 ** may be executed.
291 */
292 #ifndef SQLITE_MAX_TRIGGER_DEPTH
293 # define SQLITE_MAX_TRIGGER_DEPTH 1000
294 #endif
295 
296 /************** End of sqliteLimit.h *****************************************/
297 /************** Continuing where we left off in sqliteInt.h ******************/
298 
299 /* Disable nuisance warnings on Borland compilers */
300 #if defined(__BORLANDC__)
301 #pragma warn -rch /* unreachable code */
302 #pragma warn -ccc /* Condition is always true or false */
303 #pragma warn -aus /* Assigned value is never used */
304 #pragma warn -csu /* Comparing signed and unsigned */
305 #pragma warn -spa /* Suspicious pointer arithmetic */
306 #endif
307 
308 /* Needed for various definitions... */
309 #ifndef _GNU_SOURCE
310 # define _GNU_SOURCE
311 #endif
312 
313 /*
314 ** Include standard header files as necessary
315 */
316 #ifdef HAVE_STDINT_H
317 #include <stdint.h>
318 #endif
319 #ifdef HAVE_INTTYPES_H
320 #include <inttypes.h>
321 #endif
322 
323 /*
324 ** The number of samples of an index that SQLite takes in order to
325 ** construct a histogram of the table content when running ANALYZE
326 ** and with SQLITE_ENABLE_STAT2
327 */
328 #define SQLITE_INDEX_SAMPLES 10
329 
330 /*
331 ** The following macros are used to cast pointers to integers and
332 ** integers to pointers.  The way you do this varies from one compiler
333 ** to the next, so we have developed the following set of #if statements
334 ** to generate appropriate macros for a wide range of compilers.
335 **
336 ** The correct "ANSI" way to do this is to use the intptr_t type.
337 ** Unfortunately, that typedef is not available on all compilers, or
338 ** if it is available, it requires an #include of specific headers
339 ** that vary from one machine to the next.
340 **
341 ** Ticket #3860:  The llvm-gcc-4.2 compiler from Apple chokes on
342 ** the ((void*)&((char*)0)[X]) construct.  But MSVC chokes on ((void*)(X)).
343 ** So we have to define the macros in different ways depending on the
344 ** compiler.
345 */
346 #if defined(__PTRDIFF_TYPE__)  /* This case should work for GCC */
347 # define SQLITE_INT_TO_PTR(X)  ((void*)(__PTRDIFF_TYPE__)(X))
348 # define SQLITE_PTR_TO_INT(X)  ((int)(__PTRDIFF_TYPE__)(X))
349 #elif !defined(__GNUC__)       /* Works for compilers other than LLVM */
350 # define SQLITE_INT_TO_PTR(X)  ((void*)&((char*)0)[X])
351 # define SQLITE_PTR_TO_INT(X)  ((int)(((char*)X)-(char*)0))
352 #elif defined(HAVE_STDINT_H)   /* Use this case if we have ANSI headers */
353 # define SQLITE_INT_TO_PTR(X)  ((void*)(intptr_t)(X))
354 # define SQLITE_PTR_TO_INT(X)  ((int)(intptr_t)(X))
355 #else                          /* Generates a warning - but it always works */
356 # define SQLITE_INT_TO_PTR(X)  ((void*)(X))
357 # define SQLITE_PTR_TO_INT(X)  ((int)(X))
358 #endif
359 
360 /*
361 ** The SQLITE_THREADSAFE macro must be defined as 0, 1, or 2.
362 ** 0 means mutexes are permanently disable and the library is never
363 ** threadsafe.  1 means the library is serialized which is the highest
364 ** level of threadsafety.  2 means the libary is multithreaded - multiple
365 ** threads can use SQLite as long as no two threads try to use the same
366 ** database connection at the same time.
367 **
368 ** Older versions of SQLite used an optional THREADSAFE macro.
369 ** We support that for legacy.
370 */
371 #if !defined(SQLITE_THREADSAFE)
372 #if defined(THREADSAFE)
373 # define SQLITE_THREADSAFE THREADSAFE
374 #else
375 # define SQLITE_THREADSAFE 1 /* IMP: R-07272-22309 */
376 #endif
377 #endif
378 
379 /*
380 ** The SQLITE_DEFAULT_MEMSTATUS macro must be defined as either 0 or 1.
381 ** It determines whether or not the features related to
382 ** SQLITE_CONFIG_MEMSTATUS are available by default or not. This value can
383 ** be overridden at runtime using the sqlite3_config() API.
384 */
385 #if !defined(SQLITE_DEFAULT_MEMSTATUS)
386 # define SQLITE_DEFAULT_MEMSTATUS 1
387 #endif
388 
389 /*
390 ** Exactly one of the following macros must be defined in order to
391 ** specify which memory allocation subsystem to use.
392 **
393 **     SQLITE_SYSTEM_MALLOC          // Use normal system malloc()
394 **     SQLITE_MEMDEBUG               // Debugging version of system malloc()
395 **
396 ** (Historical note:  There used to be several other options, but we've
397 ** pared it down to just these two.)
398 **
399 ** If none of the above are defined, then set SQLITE_SYSTEM_MALLOC as
400 ** the default.
401 */
402 #if defined(SQLITE_SYSTEM_MALLOC)+defined(SQLITE_MEMDEBUG)>1
403 # error "At most one of the following compile-time configuration options\
404  is allows: SQLITE_SYSTEM_MALLOC, SQLITE_MEMDEBUG"
405 #endif
406 #if defined(SQLITE_SYSTEM_MALLOC)+defined(SQLITE_MEMDEBUG)==0
407 # define SQLITE_SYSTEM_MALLOC 1
408 #endif
409 
410 /*
411 ** If SQLITE_MALLOC_SOFT_LIMIT is not zero, then try to keep the
412 ** sizes of memory allocations below this value where possible.
413 */
414 #if !defined(SQLITE_MALLOC_SOFT_LIMIT)
415 # define SQLITE_MALLOC_SOFT_LIMIT 1024
416 #endif
417 
418 /*
419 ** We need to define _XOPEN_SOURCE as follows in order to enable
420 ** recursive mutexes on most Unix systems.  But Mac OS X is different.
421 ** The _XOPEN_SOURCE define causes problems for Mac OS X we are told,
422 ** so it is omitted there.  See ticket #2673.
423 **
424 ** Later we learn that _XOPEN_SOURCE is poorly or incorrectly
425 ** implemented on some systems.  So we avoid defining it at all
426 ** if it is already defined or if it is unneeded because we are
427 ** not doing a threadsafe build.  Ticket #2681.
428 **
429 ** See also ticket #2741.
430 */
431 #if !defined(_XOPEN_SOURCE) && !defined(__DARWIN__) && !defined(__APPLE__) && SQLITE_THREADSAFE
432 #  define _XOPEN_SOURCE 500  /* Needed to enable pthread recursive mutexes */
433 #endif
434 
435 /*
436 ** The TCL headers are only needed when compiling the TCL bindings.
437 */
438 #if defined(SQLITE_TCL) || defined(TCLSH)
439 # include <tcl.h>
440 #endif
441 
442 /*
443 ** Many people are failing to set -DNDEBUG=1 when compiling SQLite.
444 ** Setting NDEBUG makes the code smaller and run faster.  So the following
445 ** lines are added to automatically set NDEBUG unless the -DSQLITE_DEBUG=1
446 ** option is set.  Thus NDEBUG becomes an opt-in rather than an opt-out
447 ** feature.
448 */
449 #if !defined(NDEBUG) && !defined(SQLITE_DEBUG)
450 # define NDEBUG 1
451 #endif
452 
453 /*
454 ** The testcase() macro is used to aid in coverage testing.  When
455 ** doing coverage testing, the condition inside the argument to
456 ** testcase() must be evaluated both true and false in order to
457 ** get full branch coverage.  The testcase() macro is inserted
458 ** to help ensure adequate test coverage in places where simple
459 ** condition/decision coverage is inadequate.  For example, testcase()
460 ** can be used to make sure boundary values are tested.  For
461 ** bitmask tests, testcase() can be used to make sure each bit
462 ** is significant and used at least once.  On switch statements
463 ** where multiple cases go to the same block of code, testcase()
464 ** can insure that all cases are evaluated.
465 **
466 */
467 #ifdef SQLITE_COVERAGE_TEST
468 SQLITE_PRIVATE   void sqlite3Coverage(int);
469 # define testcase(X)  if( X ){ sqlite3Coverage(__LINE__); }
470 #else
471 # define testcase(X)
472 #endif
473 
474 /*
475 ** The TESTONLY macro is used to enclose variable declarations or
476 ** other bits of code that are needed to support the arguments
477 ** within testcase() and assert() macros.
478 */
479 #if !defined(NDEBUG) || defined(SQLITE_COVERAGE_TEST)
480 # define TESTONLY(X)  X
481 #else
482 # define TESTONLY(X)
483 #endif
484 
485 /*
486 ** Sometimes we need a small amount of code such as a variable initialization
487 ** to setup for a later assert() statement.  We do not want this code to
488 ** appear when assert() is disabled.  The following macro is therefore
489 ** used to contain that setup code.  The "VVA" acronym stands for
490 ** "Verification, Validation, and Accreditation".  In other words, the
491 ** code within VVA_ONLY() will only run during verification processes.
492 */
493 #ifndef NDEBUG
494 # define VVA_ONLY(X)  X
495 #else
496 # define VVA_ONLY(X)
497 #endif
498 
499 /*
500 ** The ALWAYS and NEVER macros surround boolean expressions which
501 ** are intended to always be true or false, respectively.  Such
502 ** expressions could be omitted from the code completely.  But they
503 ** are included in a few cases in order to enhance the resilience
504 ** of SQLite to unexpected behavior - to make the code "self-healing"
505 ** or "ductile" rather than being "brittle" and crashing at the first
506 ** hint of unplanned behavior.
507 **
508 ** In other words, ALWAYS and NEVER are added for defensive code.
509 **
510 ** When doing coverage testing ALWAYS and NEVER are hard-coded to
511 ** be true and false so that the unreachable code then specify will
512 ** not be counted as untested code.
513 */
514 #if defined(SQLITE_COVERAGE_TEST)
515 # define ALWAYS(X)      (1)
516 # define NEVER(X)       (0)
517 #elif !defined(NDEBUG)
518 # define ALWAYS(X)      ((X)?1:(assert(0),0))
519 # define NEVER(X)       ((X)?(assert(0),1):0)
520 #else
521 # define ALWAYS(X)      (X)
522 # define NEVER(X)       (X)
523 #endif
524 
525 /*
526 ** Return true (non-zero) if the input is a integer that is too large
527 ** to fit in 32-bits.  This macro is used inside of various testcase()
528 ** macros to verify that we have tested SQLite for large-file support.
529 */
530 #define IS_BIG_INT(X)  (((X)&~(i64)0xffffffff)!=0)
531 
532 /*
533 ** The macro unlikely() is a hint that surrounds a boolean
534 ** expression that is usually false.  Macro likely() surrounds
535 ** a boolean expression that is usually true.  GCC is able to
536 ** use these hints to generate better code, sometimes.
537 */
538 #if defined(__GNUC__) && 0
539 # define likely(X)    __builtin_expect((X),1)
540 # define unlikely(X)  __builtin_expect((X),0)
541 #else
542 # define likely(X)    !!(X)
543 # define unlikely(X)  !!(X)
544 #endif
545 
546 /************** Include sqlite3.h in the middle of sqliteInt.h ***************/
547 /************** Begin file sqlite3.h *****************************************/
548 /*
549 ** 2001 September 15
550 **
551 ** The author disclaims copyright to this source code.  In place of
552 ** a legal notice, here is a blessing:
553 **
554 **    May you do good and not evil.
555 **    May you find forgiveness for yourself and forgive others.
556 **    May you share freely, never taking more than you give.
557 **
558 *************************************************************************
559 ** This header file defines the interface that the SQLite library
560 ** presents to client programs.  If a C-function, structure, datatype,
561 ** or constant definition does not appear in this file, then it is
562 ** not a published API of SQLite, is subject to change without
563 ** notice, and should not be referenced by programs that use SQLite.
564 **
565 ** Some of the definitions that are in this file are marked as
566 ** "experimental".  Experimental interfaces are normally new
567 ** features recently added to SQLite.  We do not anticipate changes
568 ** to experimental interfaces but reserve the right to make minor changes
569 ** if experience from use "in the wild" suggest such changes are prudent.
570 **
571 ** The official C-language API documentation for SQLite is derived
572 ** from comments in this file.  This file is the authoritative source
573 ** on how SQLite interfaces are suppose to operate.
574 **
575 ** The name of this file under configuration management is "sqlite.h.in".
576 ** The makefile makes some minor changes to this file (such as inserting
577 ** the version number) and changes its name to "sqlite3.h" as
578 ** part of the build process.
579 */
580 #ifndef _SQLITE3_H_
581 #define _SQLITE3_H_
582 #include <stdarg.h>     /* Needed for the definition of va_list */
583 
584 /*
585 ** Make sure we can call this stuff from C++.
586 */
587 #if 0
588 extern "C" {
589 #endif
590 
591 
592 /*
593 ** Add the ability to override 'extern'
594 */
595 #ifndef SQLITE_EXTERN
596 # define SQLITE_EXTERN extern
597 #endif
598 
599 #ifndef SQLITE_API
600 # define SQLITE_API
601 #endif
602 
603 
604 /*
605 ** These no-op macros are used in front of interfaces to mark those
606 ** interfaces as either deprecated or experimental.  New applications
607 ** should not use deprecated interfaces - they are support for backwards
608 ** compatibility only.  Application writers should be aware that
609 ** experimental interfaces are subject to change in point releases.
610 **
611 ** These macros used to resolve to various kinds of compiler magic that
612 ** would generate warning messages when they were used.  But that
613 ** compiler magic ended up generating such a flurry of bug reports
614 ** that we have taken it all out and gone back to using simple
615 ** noop macros.
616 */
617 #define SQLITE_DEPRECATED
618 #define SQLITE_EXPERIMENTAL
619 
620 /*
621 ** Ensure these symbols were not defined by some previous header file.
622 */
623 #ifdef SQLITE_VERSION
624 # undef SQLITE_VERSION
625 #endif
626 #ifdef SQLITE_VERSION_NUMBER
627 # undef SQLITE_VERSION_NUMBER
628 #endif
629 
630 /*
631 ** CAPI3REF: Compile-Time Library Version Numbers
632 **
633 ** ^(The [SQLITE_VERSION] C preprocessor macro in the sqlite3.h header
634 ** evaluates to a string literal that is the SQLite version in the
635 ** format "X.Y.Z" where X is the major version number (always 3 for
636 ** SQLite3) and Y is the minor version number and Z is the release number.)^
637 ** ^(The [SQLITE_VERSION_NUMBER] C preprocessor macro resolves to an integer
638 ** with the value (X*1000000 + Y*1000 + Z) where X, Y, and Z are the same
639 ** numbers used in [SQLITE_VERSION].)^
640 ** The SQLITE_VERSION_NUMBER for any given release of SQLite will also
641 ** be larger than the release from which it is derived.  Either Y will
642 ** be held constant and Z will be incremented or else Y will be incremented
643 ** and Z will be reset to zero.
644 **
645 ** Since version 3.6.18, SQLite source code has been stored in the
646 ** <a href="http://www.fossil-scm.org/">Fossil configuration management
647 ** system</a>.  ^The SQLITE_SOURCE_ID macro evaluates to
648 ** a string which identifies a particular check-in of SQLite
649 ** within its configuration management system.  ^The SQLITE_SOURCE_ID
650 ** string contains the date and time of the check-in (UTC) and an SHA1
651 ** hash of the entire source tree.
652 **
653 ** See also: [sqlite3_libversion()],
654 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
655 ** [sqlite_version()] and [sqlite_source_id()].
656 */
657 #define SQLITE_VERSION        "3.7.7.1"
658 #define SQLITE_VERSION_NUMBER 3007007
659 #define SQLITE_SOURCE_ID      "2011-06-28 17:39:05 af0d91adf497f5f36ec3813f04235a6e195a605f"
660 
661 /*
662 ** CAPI3REF: Run-Time Library Version Numbers
663 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
664 **
665 ** These interfaces provide the same information as the [SQLITE_VERSION],
666 ** [SQLITE_VERSION_NUMBER], and [SQLITE_SOURCE_ID] C preprocessor macros
667 ** but are associated with the library instead of the header file.  ^(Cautious
668 ** programmers might include assert() statements in their application to
669 ** verify that values returned by these interfaces match the macros in
670 ** the header, and thus insure that the application is
671 ** compiled with matching library and header files.
672 **
673 ** <blockquote><pre>
674 ** assert( sqlite3_libversion_number()==SQLITE_VERSION_NUMBER );
675 ** assert( strcmp(sqlite3_sourceid(),SQLITE_SOURCE_ID)==0 );
676 ** assert( strcmp(sqlite3_libversion(),SQLITE_VERSION)==0 );
677 ** </pre></blockquote>)^
678 **
679 ** ^The sqlite3_version[] string constant contains the text of [SQLITE_VERSION]
680 ** macro.  ^The sqlite3_libversion() function returns a pointer to the
681 ** to the sqlite3_version[] string constant.  The sqlite3_libversion()
682 ** function is provided for use in DLLs since DLL users usually do not have
683 ** direct access to string constants within the DLL.  ^The
684 ** sqlite3_libversion_number() function returns an integer equal to
685 ** [SQLITE_VERSION_NUMBER].  ^The sqlite3_sourceid() function returns
686 ** a pointer to a string constant whose value is the same as the
687 ** [SQLITE_SOURCE_ID] C preprocessor macro.
688 **
689 ** See also: [sqlite_version()] and [sqlite_source_id()].
690 */
691 SQLITE_API const char sqlite3_version[] = SQLITE_VERSION;
692 SQLITE_API const char *sqlite3_libversion(void);
693 SQLITE_API const char *sqlite3_sourceid(void);
694 SQLITE_API int sqlite3_libversion_number(void);
695 
696 /*
697 ** CAPI3REF: Run-Time Library Compilation Options Diagnostics
698 **
699 ** ^The sqlite3_compileoption_used() function returns 0 or 1
700 ** indicating whether the specified option was defined at
701 ** compile time.  ^The SQLITE_ prefix may be omitted from the
702 ** option name passed to sqlite3_compileoption_used().
703 **
704 ** ^The sqlite3_compileoption_get() function allows iterating
705 ** over the list of options that were defined at compile time by
706 ** returning the N-th compile time option string.  ^If N is out of range,
707 ** sqlite3_compileoption_get() returns a NULL pointer.  ^The SQLITE_
708 ** prefix is omitted from any strings returned by
709 ** sqlite3_compileoption_get().
710 **
711 ** ^Support for the diagnostic functions sqlite3_compileoption_used()
712 ** and sqlite3_compileoption_get() may be omitted by specifying the
713 ** [SQLITE_OMIT_COMPILEOPTION_DIAGS] option at compile time.
714 **
715 ** See also: SQL functions [sqlite_compileoption_used()] and
716 ** [sqlite_compileoption_get()] and the [compile_options pragma].
717 */
718 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
719 SQLITE_API int sqlite3_compileoption_used(const char *zOptName);
720 SQLITE_API const char *sqlite3_compileoption_get(int N);
721 #endif
722 
723 /*
724 ** CAPI3REF: Test To See If The Library Is Threadsafe
725 **
726 ** ^The sqlite3_threadsafe() function returns zero if and only if
727 ** SQLite was compiled mutexing code omitted due to the
728 ** [SQLITE_THREADSAFE] compile-time option being set to 0.
729 **
730 ** SQLite can be compiled with or without mutexes.  When
731 ** the [SQLITE_THREADSAFE] C preprocessor macro is 1 or 2, mutexes
732 ** are enabled and SQLite is threadsafe.  When the
733 ** [SQLITE_THREADSAFE] macro is 0,
734 ** the mutexes are omitted.  Without the mutexes, it is not safe
735 ** to use SQLite concurrently from more than one thread.
736 **
737 ** Enabling mutexes incurs a measurable performance penalty.
738 ** So if speed is of utmost importance, it makes sense to disable
739 ** the mutexes.  But for maximum safety, mutexes should be enabled.
740 ** ^The default behavior is for mutexes to be enabled.
741 **
742 ** This interface can be used by an application to make sure that the
743 ** version of SQLite that it is linking against was compiled with
744 ** the desired setting of the [SQLITE_THREADSAFE] macro.
745 **
746 ** This interface only reports on the compile-time mutex setting
747 ** of the [SQLITE_THREADSAFE] flag.  If SQLite is compiled with
748 ** SQLITE_THREADSAFE=1 or =2 then mutexes are enabled by default but
749 ** can be fully or partially disabled using a call to [sqlite3_config()]
750 ** with the verbs [SQLITE_CONFIG_SINGLETHREAD], [SQLITE_CONFIG_MULTITHREAD],
751 ** or [SQLITE_CONFIG_MUTEX].  ^(The return value of the
752 ** sqlite3_threadsafe() function shows only the compile-time setting of
753 ** thread safety, not any run-time changes to that setting made by
754 ** sqlite3_config(). In other words, the return value from sqlite3_threadsafe()
755 ** is unchanged by calls to sqlite3_config().)^
756 **
757 ** See the [threading mode] documentation for additional information.
758 */
759 SQLITE_API int sqlite3_threadsafe(void);
760 
761 /*
762 ** CAPI3REF: Database Connection Handle
763 ** KEYWORDS: {database connection} {database connections}
764 **
765 ** Each open SQLite database is represented by a pointer to an instance of
766 ** the opaque structure named "sqlite3".  It is useful to think of an sqlite3
767 ** pointer as an object.  The [sqlite3_open()], [sqlite3_open16()], and
768 ** [sqlite3_open_v2()] interfaces are its constructors, and [sqlite3_close()]
769 ** is its destructor.  There are many other interfaces (such as
770 ** [sqlite3_prepare_v2()], [sqlite3_create_function()], and
771 ** [sqlite3_busy_timeout()] to name but three) that are methods on an
772 ** sqlite3 object.
773 */
774 typedef struct sqlite3 sqlite3;
775 
776 /*
777 ** CAPI3REF: 64-Bit Integer Types
778 ** KEYWORDS: sqlite_int64 sqlite_uint64
779 **
780 ** Because there is no cross-platform way to specify 64-bit integer types
781 ** SQLite includes typedefs for 64-bit signed and unsigned integers.
782 **
783 ** The sqlite3_int64 and sqlite3_uint64 are the preferred type definitions.
784 ** The sqlite_int64 and sqlite_uint64 types are supported for backwards
785 ** compatibility only.
786 **
787 ** ^The sqlite3_int64 and sqlite_int64 types can store integer values
788 ** between -9223372036854775808 and +9223372036854775807 inclusive.  ^The
789 ** sqlite3_uint64 and sqlite_uint64 types can store integer values
790 ** between 0 and +18446744073709551615 inclusive.
791 */
792 #ifdef SQLITE_INT64_TYPE
793   typedef SQLITE_INT64_TYPE sqlite_int64;
794   typedef unsigned SQLITE_INT64_TYPE sqlite_uint64;
795 #elif defined(_MSC_VER) || defined(__BORLANDC__)
796   typedef __int64 sqlite_int64;
797   typedef unsigned __int64 sqlite_uint64;
798 #else
799   typedef long long int sqlite_int64;
800   typedef unsigned long long int sqlite_uint64;
801 #endif
802 typedef sqlite_int64 sqlite3_int64;
803 typedef sqlite_uint64 sqlite3_uint64;
804 
805 /*
806 ** If compiling for a processor that lacks floating point support,
807 ** substitute integer for floating-point.
808 */
809 #ifdef SQLITE_OMIT_FLOATING_POINT
810 # define double sqlite3_int64
811 #endif
812 
813 /*
814 ** CAPI3REF: Closing A Database Connection
815 **
816 ** ^The sqlite3_close() routine is the destructor for the [sqlite3] object.
817 ** ^Calls to sqlite3_close() return SQLITE_OK if the [sqlite3] object is
818 ** successfully destroyed and all associated resources are deallocated.
819 **
820 ** Applications must [sqlite3_finalize | finalize] all [prepared statements]
821 ** and [sqlite3_blob_close | close] all [BLOB handles] associated with
822 ** the [sqlite3] object prior to attempting to close the object.  ^If
823 ** sqlite3_close() is called on a [database connection] that still has
824 ** outstanding [prepared statements] or [BLOB handles], then it returns
825 ** SQLITE_BUSY.
826 **
827 ** ^If [sqlite3_close()] is invoked while a transaction is open,
828 ** the transaction is automatically rolled back.
829 **
830 ** The C parameter to [sqlite3_close(C)] must be either a NULL
831 ** pointer or an [sqlite3] object pointer obtained
832 ** from [sqlite3_open()], [sqlite3_open16()], or
833 ** [sqlite3_open_v2()], and not previously closed.
834 ** ^Calling sqlite3_close() with a NULL pointer argument is a
835 ** harmless no-op.
836 */
837 SQLITE_API int sqlite3_close(sqlite3 *);
838 
839 /*
840 ** The type for a callback function.
841 ** This is legacy and deprecated.  It is included for historical
842 ** compatibility and is not documented.
843 */
844 typedef int (*sqlite3_callback)(void*,int,char**, char**);
845 
846 /*
847 ** CAPI3REF: One-Step Query Execution Interface
848 **
849 ** The sqlite3_exec() interface is a convenience wrapper around
850 ** [sqlite3_prepare_v2()], [sqlite3_step()], and [sqlite3_finalize()],
851 ** that allows an application to run multiple statements of SQL
852 ** without having to use a lot of C code.
853 **
854 ** ^The sqlite3_exec() interface runs zero or more UTF-8 encoded,
855 ** semicolon-separate SQL statements passed into its 2nd argument,
856 ** in the context of the [database connection] passed in as its 1st
857 ** argument.  ^If the callback function of the 3rd argument to
858 ** sqlite3_exec() is not NULL, then it is invoked for each result row
859 ** coming out of the evaluated SQL statements.  ^The 4th argument to
860 ** sqlite3_exec() is relayed through to the 1st argument of each
861 ** callback invocation.  ^If the callback pointer to sqlite3_exec()
862 ** is NULL, then no callback is ever invoked and result rows are
863 ** ignored.
864 **
865 ** ^If an error occurs while evaluating the SQL statements passed into
866 ** sqlite3_exec(), then execution of the current statement stops and
867 ** subsequent statements are skipped.  ^If the 5th parameter to sqlite3_exec()
868 ** is not NULL then any error message is written into memory obtained
869 ** from [sqlite3_malloc()] and passed back through the 5th parameter.
870 ** To avoid memory leaks, the application should invoke [sqlite3_free()]
871 ** on error message strings returned through the 5th parameter of
872 ** of sqlite3_exec() after the error message string is no longer needed.
873 ** ^If the 5th parameter to sqlite3_exec() is not NULL and no errors
874 ** occur, then sqlite3_exec() sets the pointer in its 5th parameter to
875 ** NULL before returning.
876 **
877 ** ^If an sqlite3_exec() callback returns non-zero, the sqlite3_exec()
878 ** routine returns SQLITE_ABORT without invoking the callback again and
879 ** without running any subsequent SQL statements.
880 **
881 ** ^The 2nd argument to the sqlite3_exec() callback function is the
882 ** number of columns in the result.  ^The 3rd argument to the sqlite3_exec()
883 ** callback is an array of pointers to strings obtained as if from
884 ** [sqlite3_column_text()], one for each column.  ^If an element of a
885 ** result row is NULL then the corresponding string pointer for the
886 ** sqlite3_exec() callback is a NULL pointer.  ^The 4th argument to the
887 ** sqlite3_exec() callback is an array of pointers to strings where each
888 ** entry represents the name of corresponding result column as obtained
889 ** from [sqlite3_column_name()].
890 **
891 ** ^If the 2nd parameter to sqlite3_exec() is a NULL pointer, a pointer
892 ** to an empty string, or a pointer that contains only whitespace and/or
893 ** SQL comments, then no SQL statements are evaluated and the database
894 ** is not changed.
895 **
896 ** Restrictions:
897 **
898 ** <ul>
899 ** <li> The application must insure that the 1st parameter to sqlite3_exec()
900 **      is a valid and open [database connection].
901 ** <li> The application must not close [database connection] specified by
902 **      the 1st parameter to sqlite3_exec() while sqlite3_exec() is running.
903 ** <li> The application must not modify the SQL statement text passed into
904 **      the 2nd parameter of sqlite3_exec() while sqlite3_exec() is running.
905 ** </ul>
906 */
907 SQLITE_API int sqlite3_exec(
908   sqlite3*,                                  /* An open database */
909   const char *sql,                           /* SQL to be evaluated */
910   int (*callback)(void*,int,char**,char**),  /* Callback function */
911   void *,                                    /* 1st argument to callback */
912   char **errmsg                              /* Error msg written here */
913 );
914 
915 /*
916 ** CAPI3REF: Result Codes
917 ** KEYWORDS: SQLITE_OK {error code} {error codes}
918 ** KEYWORDS: {result code} {result codes}
919 **
920 ** Many SQLite functions return an integer result code from the set shown
921 ** here in order to indicates success or failure.
922 **
923 ** New error codes may be added in future versions of SQLite.
924 **
925 ** See also: [SQLITE_IOERR_READ | extended result codes],
926 ** [sqlite3_vtab_on_conflict()] [SQLITE_ROLLBACK | result codes].
927 */
928 #define SQLITE_OK           0   /* Successful result */
929 /* beginning-of-error-codes */
930 #define SQLITE_ERROR        1   /* SQL error or missing database */
931 #define SQLITE_INTERNAL     2   /* Internal logic error in SQLite */
932 #define SQLITE_PERM         3   /* Access permission denied */
933 #define SQLITE_ABORT        4   /* Callback routine requested an abort */
934 #define SQLITE_BUSY         5   /* The database file is locked */
935 #define SQLITE_LOCKED       6   /* A table in the database is locked */
936 #define SQLITE_NOMEM        7   /* A malloc() failed */
937 #define SQLITE_READONLY     8   /* Attempt to write a readonly database */
938 #define SQLITE_INTERRUPT    9   /* Operation terminated by sqlite3_interrupt()*/
939 #define SQLITE_IOERR       10   /* Some kind of disk I/O error occurred */
940 #define SQLITE_CORRUPT     11   /* The database disk image is malformed */
941 #define SQLITE_NOTFOUND    12   /* Unknown opcode in sqlite3_file_control() */
942 #define SQLITE_FULL        13   /* Insertion failed because database is full */
943 #define SQLITE_CANTOPEN    14   /* Unable to open the database file */
944 #define SQLITE_PROTOCOL    15   /* Database lock protocol error */
945 #define SQLITE_EMPTY       16   /* Database is empty */
946 #define SQLITE_SCHEMA      17   /* The database schema changed */
947 #define SQLITE_TOOBIG      18   /* String or BLOB exceeds size limit */
948 #define SQLITE_CONSTRAINT  19   /* Abort due to constraint violation */
949 #define SQLITE_MISMATCH    20   /* Data type mismatch */
950 #define SQLITE_MISUSE      21   /* Library used incorrectly */
951 #define SQLITE_NOLFS       22   /* Uses OS features not supported on host */
952 #define SQLITE_AUTH        23   /* Authorization denied */
953 #define SQLITE_FORMAT      24   /* Auxiliary database format error */
954 #define SQLITE_RANGE       25   /* 2nd parameter to sqlite3_bind out of range */
955 #define SQLITE_NOTADB      26   /* File opened that is not a database file */
956 #define SQLITE_ROW         100  /* sqlite3_step() has another row ready */
957 #define SQLITE_DONE        101  /* sqlite3_step() has finished executing */
958 /* end-of-error-codes */
959 
960 /*
961 ** CAPI3REF: Extended Result Codes
962 ** KEYWORDS: {extended error code} {extended error codes}
963 ** KEYWORDS: {extended result code} {extended result codes}
964 **
965 ** In its default configuration, SQLite API routines return one of 26 integer
966 ** [SQLITE_OK | result codes].  However, experience has shown that many of
967 ** these result codes are too coarse-grained.  They do not provide as
968 ** much information about problems as programmers might like.  In an effort to
969 ** address this, newer versions of SQLite (version 3.3.8 and later) include
970 ** support for additional result codes that provide more detailed information
971 ** about errors. The extended result codes are enabled or disabled
972 ** on a per database connection basis using the
973 ** [sqlite3_extended_result_codes()] API.
974 **
975 ** Some of the available extended result codes are listed here.
976 ** One may expect the number of extended result codes will be expand
977 ** over time.  Software that uses extended result codes should expect
978 ** to see new result codes in future releases of SQLite.
979 **
980 ** The SQLITE_OK result code will never be extended.  It will always
981 ** be exactly zero.
982 */
983 #define SQLITE_IOERR_READ              (SQLITE_IOERR | (1<<8))
984 #define SQLITE_IOERR_SHORT_READ        (SQLITE_IOERR | (2<<8))
985 #define SQLITE_IOERR_WRITE             (SQLITE_IOERR | (3<<8))
986 #define SQLITE_IOERR_FSYNC             (SQLITE_IOERR | (4<<8))
987 #define SQLITE_IOERR_DIR_FSYNC         (SQLITE_IOERR | (5<<8))
988 #define SQLITE_IOERR_TRUNCATE          (SQLITE_IOERR | (6<<8))
989 #define SQLITE_IOERR_FSTAT             (SQLITE_IOERR | (7<<8))
990 #define SQLITE_IOERR_UNLOCK            (SQLITE_IOERR | (8<<8))
991 #define SQLITE_IOERR_RDLOCK            (SQLITE_IOERR | (9<<8))
992 #define SQLITE_IOERR_DELETE            (SQLITE_IOERR | (10<<8))
993 #define SQLITE_IOERR_BLOCKED           (SQLITE_IOERR | (11<<8))
994 #define SQLITE_IOERR_NOMEM             (SQLITE_IOERR | (12<<8))
995 #define SQLITE_IOERR_ACCESS            (SQLITE_IOERR | (13<<8))
996 #define SQLITE_IOERR_CHECKRESERVEDLOCK (SQLITE_IOERR | (14<<8))
997 #define SQLITE_IOERR_LOCK              (SQLITE_IOERR | (15<<8))
998 #define SQLITE_IOERR_CLOSE             (SQLITE_IOERR | (16<<8))
999 #define SQLITE_IOERR_DIR_CLOSE         (SQLITE_IOERR | (17<<8))
1000 #define SQLITE_IOERR_SHMOPEN           (SQLITE_IOERR | (18<<8))
1001 #define SQLITE_IOERR_SHMSIZE           (SQLITE_IOERR | (19<<8))
1002 #define SQLITE_IOERR_SHMLOCK           (SQLITE_IOERR | (20<<8))
1003 #define SQLITE_IOERR_SHMMAP            (SQLITE_IOERR | (21<<8))
1004 #define SQLITE_IOERR_SEEK              (SQLITE_IOERR | (22<<8))
1005 #define SQLITE_LOCKED_SHAREDCACHE      (SQLITE_LOCKED |  (1<<8))
1006 #define SQLITE_BUSY_RECOVERY           (SQLITE_BUSY   |  (1<<8))
1007 #define SQLITE_CANTOPEN_NOTEMPDIR      (SQLITE_CANTOPEN | (1<<8))
1008 #define SQLITE_CORRUPT_VTAB            (SQLITE_CORRUPT | (1<<8))
1009 #define SQLITE_READONLY_RECOVERY       (SQLITE_READONLY | (1<<8))
1010 #define SQLITE_READONLY_CANTLOCK       (SQLITE_READONLY | (2<<8))
1011 
1012 /*
1013 ** CAPI3REF: Flags For File Open Operations
1014 **
1015 ** These bit values are intended for use in the
1016 ** 3rd parameter to the [sqlite3_open_v2()] interface and
1017 ** in the 4th parameter to the [sqlite3_vfs.xOpen] method.
1018 */
1019 #define SQLITE_OPEN_READONLY         0x00000001  /* Ok for sqlite3_open_v2() */
1020 #define SQLITE_OPEN_READWRITE        0x00000002  /* Ok for sqlite3_open_v2() */
1021 #define SQLITE_OPEN_CREATE           0x00000004  /* Ok for sqlite3_open_v2() */
1022 #define SQLITE_OPEN_DELETEONCLOSE    0x00000008  /* VFS only */
1023 #define SQLITE_OPEN_EXCLUSIVE        0x00000010  /* VFS only */
1024 #define SQLITE_OPEN_AUTOPROXY        0x00000020  /* VFS only */
1025 #define SQLITE_OPEN_URI              0x00000040  /* Ok for sqlite3_open_v2() */
1026 #define SQLITE_OPEN_MAIN_DB          0x00000100  /* VFS only */
1027 #define SQLITE_OPEN_TEMP_DB          0x00000200  /* VFS only */
1028 #define SQLITE_OPEN_TRANSIENT_DB     0x00000400  /* VFS only */
1029 #define SQLITE_OPEN_MAIN_JOURNAL     0x00000800  /* VFS only */
1030 #define SQLITE_OPEN_TEMP_JOURNAL     0x00001000  /* VFS only */
1031 #define SQLITE_OPEN_SUBJOURNAL       0x00002000  /* VFS only */
1032 #define SQLITE_OPEN_MASTER_JOURNAL   0x00004000  /* VFS only */
1033 #define SQLITE_OPEN_NOMUTEX          0x00008000  /* Ok for sqlite3_open_v2() */
1034 #define SQLITE_OPEN_FULLMUTEX        0x00010000  /* Ok for sqlite3_open_v2() */
1035 #define SQLITE_OPEN_SHAREDCACHE      0x00020000  /* Ok for sqlite3_open_v2() */
1036 #define SQLITE_OPEN_PRIVATECACHE     0x00040000  /* Ok for sqlite3_open_v2() */
1037 #define SQLITE_OPEN_WAL              0x00080000  /* VFS only */
1038 
1039 /* Reserved:                         0x00F00000 */
1040 
1041 /*
1042 ** CAPI3REF: Device Characteristics
1043 **
1044 ** The xDeviceCharacteristics method of the [sqlite3_io_methods]
1045 ** object returns an integer which is a vector of the these
1046 ** bit values expressing I/O characteristics of the mass storage
1047 ** device that holds the file that the [sqlite3_io_methods]
1048 ** refers to.
1049 **
1050 ** The SQLITE_IOCAP_ATOMIC property means that all writes of
1051 ** any size are atomic.  The SQLITE_IOCAP_ATOMICnnn values
1052 ** mean that writes of blocks that are nnn bytes in size and
1053 ** are aligned to an address which is an integer multiple of
1054 ** nnn are atomic.  The SQLITE_IOCAP_SAFE_APPEND value means
1055 ** that when data is appended to a file, the data is appended
1056 ** first then the size of the file is extended, never the other
1057 ** way around.  The SQLITE_IOCAP_SEQUENTIAL property means that
1058 ** information is written to disk in the same order as calls
1059 ** to xWrite().
1060 */
1061 #define SQLITE_IOCAP_ATOMIC                 0x00000001
1062 #define SQLITE_IOCAP_ATOMIC512              0x00000002
1063 #define SQLITE_IOCAP_ATOMIC1K               0x00000004
1064 #define SQLITE_IOCAP_ATOMIC2K               0x00000008
1065 #define SQLITE_IOCAP_ATOMIC4K               0x00000010
1066 #define SQLITE_IOCAP_ATOMIC8K               0x00000020
1067 #define SQLITE_IOCAP_ATOMIC16K              0x00000040
1068 #define SQLITE_IOCAP_ATOMIC32K              0x00000080
1069 #define SQLITE_IOCAP_ATOMIC64K              0x00000100
1070 #define SQLITE_IOCAP_SAFE_APPEND            0x00000200
1071 #define SQLITE_IOCAP_SEQUENTIAL             0x00000400
1072 #define SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN  0x00000800
1073 
1074 /*
1075 ** CAPI3REF: File Locking Levels
1076 **
1077 ** SQLite uses one of these integer values as the second
1078 ** argument to calls it makes to the xLock() and xUnlock() methods
1079 ** of an [sqlite3_io_methods] object.
1080 */
1081 #define SQLITE_LOCK_NONE          0
1082 #define SQLITE_LOCK_SHARED        1
1083 #define SQLITE_LOCK_RESERVED      2
1084 #define SQLITE_LOCK_PENDING       3
1085 #define SQLITE_LOCK_EXCLUSIVE     4
1086 
1087 /*
1088 ** CAPI3REF: Synchronization Type Flags
1089 **
1090 ** When SQLite invokes the xSync() method of an
1091 ** [sqlite3_io_methods] object it uses a combination of
1092 ** these integer values as the second argument.
1093 **
1094 ** When the SQLITE_SYNC_DATAONLY flag is used, it means that the
1095 ** sync operation only needs to flush data to mass storage.  Inode
1096 ** information need not be flushed. If the lower four bits of the flag
1097 ** equal SQLITE_SYNC_NORMAL, that means to use normal fsync() semantics.
1098 ** If the lower four bits equal SQLITE_SYNC_FULL, that means
1099 ** to use Mac OS X style fullsync instead of fsync().
1100 **
1101 ** Do not confuse the SQLITE_SYNC_NORMAL and SQLITE_SYNC_FULL flags
1102 ** with the [PRAGMA synchronous]=NORMAL and [PRAGMA synchronous]=FULL
1103 ** settings.  The [synchronous pragma] determines when calls to the
1104 ** xSync VFS method occur and applies uniformly across all platforms.
1105 ** The SQLITE_SYNC_NORMAL and SQLITE_SYNC_FULL flags determine how
1106 ** energetic or rigorous or forceful the sync operations are and
1107 ** only make a difference on Mac OSX for the default SQLite code.
1108 ** (Third-party VFS implementations might also make the distinction
1109 ** between SQLITE_SYNC_NORMAL and SQLITE_SYNC_FULL, but among the
1110 ** operating systems natively supported by SQLite, only Mac OSX
1111 ** cares about the difference.)
1112 */
1113 #define SQLITE_SYNC_NORMAL        0x00002
1114 #define SQLITE_SYNC_FULL          0x00003
1115 #define SQLITE_SYNC_DATAONLY      0x00010
1116 
1117 /*
1118 ** CAPI3REF: OS Interface Open File Handle
1119 **
1120 ** An [sqlite3_file] object represents an open file in the
1121 ** [sqlite3_vfs | OS interface layer].  Individual OS interface
1122 ** implementations will
1123 ** want to subclass this object by appending additional fields
1124 ** for their own use.  The pMethods entry is a pointer to an
1125 ** [sqlite3_io_methods] object that defines methods for performing
1126 ** I/O operations on the open file.
1127 */
1128 typedef struct sqlite3_file sqlite3_file;
1129 struct sqlite3_file {
1130   const struct sqlite3_io_methods *pMethods;  /* Methods for an open file */
1131 };
1132 
1133 /*
1134 ** CAPI3REF: OS Interface File Virtual Methods Object
1135 **
1136 ** Every file opened by the [sqlite3_vfs.xOpen] method populates an
1137 ** [sqlite3_file] object (or, more commonly, a subclass of the
1138 ** [sqlite3_file] object) with a pointer to an instance of this object.
1139 ** This object defines the methods used to perform various operations
1140 ** against the open file represented by the [sqlite3_file] object.
1141 **
1142 ** If the [sqlite3_vfs.xOpen] method sets the sqlite3_file.pMethods element
1143 ** to a non-NULL pointer, then the sqlite3_io_methods.xClose method
1144 ** may be invoked even if the [sqlite3_vfs.xOpen] reported that it failed.  The
1145 ** only way to prevent a call to xClose following a failed [sqlite3_vfs.xOpen]
1146 ** is for the [sqlite3_vfs.xOpen] to set the sqlite3_file.pMethods element
1147 ** to NULL.
1148 **
1149 ** The flags argument to xSync may be one of [SQLITE_SYNC_NORMAL] or
1150 ** [SQLITE_SYNC_FULL].  The first choice is the normal fsync().
1151 ** The second choice is a Mac OS X style fullsync.  The [SQLITE_SYNC_DATAONLY]
1152 ** flag may be ORed in to indicate that only the data of the file
1153 ** and not its inode needs to be synced.
1154 **
1155 ** The integer values to xLock() and xUnlock() are one of
1156 ** <ul>
1157 ** <li> [SQLITE_LOCK_NONE],
1158 ** <li> [SQLITE_LOCK_SHARED],
1159 ** <li> [SQLITE_LOCK_RESERVED],
1160 ** <li> [SQLITE_LOCK_PENDING], or
1161 ** <li> [SQLITE_LOCK_EXCLUSIVE].
1162 ** </ul>
1163 ** xLock() increases the lock. xUnlock() decreases the lock.
1164 ** The xCheckReservedLock() method checks whether any database connection,
1165 ** either in this process or in some other process, is holding a RESERVED,
1166 ** PENDING, or EXCLUSIVE lock on the file.  It returns true
1167 ** if such a lock exists and false otherwise.
1168 **
1169 ** The xFileControl() method is a generic interface that allows custom
1170 ** VFS implementations to directly control an open file using the
1171 ** [sqlite3_file_control()] interface.  The second "op" argument is an
1172 ** integer opcode.  The third argument is a generic pointer intended to
1173 ** point to a structure that may contain arguments or space in which to
1174 ** write return values.  Potential uses for xFileControl() might be
1175 ** functions to enable blocking locks with timeouts, to change the
1176 ** locking strategy (for example to use dot-file locks), to inquire
1177 ** about the status of a lock, or to break stale locks.  The SQLite
1178 ** core reserves all opcodes less than 100 for its own use.
1179 ** A [SQLITE_FCNTL_LOCKSTATE | list of opcodes] less than 100 is available.
1180 ** Applications that define a custom xFileControl method should use opcodes
1181 ** greater than 100 to avoid conflicts.  VFS implementations should
1182 ** return [SQLITE_NOTFOUND] for file control opcodes that they do not
1183 ** recognize.
1184 **
1185 ** The xSectorSize() method returns the sector size of the
1186 ** device that underlies the file.  The sector size is the
1187 ** minimum write that can be performed without disturbing
1188 ** other bytes in the file.  The xDeviceCharacteristics()
1189 ** method returns a bit vector describing behaviors of the
1190 ** underlying device:
1191 **
1192 ** <ul>
1193 ** <li> [SQLITE_IOCAP_ATOMIC]
1194 ** <li> [SQLITE_IOCAP_ATOMIC512]
1195 ** <li> [SQLITE_IOCAP_ATOMIC1K]
1196 ** <li> [SQLITE_IOCAP_ATOMIC2K]
1197 ** <li> [SQLITE_IOCAP_ATOMIC4K]
1198 ** <li> [SQLITE_IOCAP_ATOMIC8K]
1199 ** <li> [SQLITE_IOCAP_ATOMIC16K]
1200 ** <li> [SQLITE_IOCAP_ATOMIC32K]
1201 ** <li> [SQLITE_IOCAP_ATOMIC64K]
1202 ** <li> [SQLITE_IOCAP_SAFE_APPEND]
1203 ** <li> [SQLITE_IOCAP_SEQUENTIAL]
1204 ** </ul>
1205 **
1206 ** The SQLITE_IOCAP_ATOMIC property means that all writes of
1207 ** any size are atomic.  The SQLITE_IOCAP_ATOMICnnn values
1208 ** mean that writes of blocks that are nnn bytes in size and
1209 ** are aligned to an address which is an integer multiple of
1210 ** nnn are atomic.  The SQLITE_IOCAP_SAFE_APPEND value means
1211 ** that when data is appended to a file, the data is appended
1212 ** first then the size of the file is extended, never the other
1213 ** way around.  The SQLITE_IOCAP_SEQUENTIAL property means that
1214 ** information is written to disk in the same order as calls
1215 ** to xWrite().
1216 **
1217 ** If xRead() returns SQLITE_IOERR_SHORT_READ it must also fill
1218 ** in the unread portions of the buffer with zeros.  A VFS that
1219 ** fails to zero-fill short reads might seem to work.  However,
1220 ** failure to zero-fill short reads will eventually lead to
1221 ** database corruption.
1222 */
1223 typedef struct sqlite3_io_methods sqlite3_io_methods;
1224 struct sqlite3_io_methods {
1225   int iVersion;
1226   int (*xClose)(sqlite3_file*);
1227   int (*xRead)(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
1228   int (*xWrite)(sqlite3_file*, const void*, int iAmt, sqlite3_int64 iOfst);
1229   int (*xTruncate)(sqlite3_file*, sqlite3_int64 size);
1230   int (*xSync)(sqlite3_file*, int flags);
1231   int (*xFileSize)(sqlite3_file*, sqlite3_int64 *pSize);
1232   int (*xLock)(sqlite3_file*, int);
1233   int (*xUnlock)(sqlite3_file*, int);
1234   int (*xCheckReservedLock)(sqlite3_file*, int *pResOut);
1235   int (*xFileControl)(sqlite3_file*, int op, void *pArg);
1236   int (*xSectorSize)(sqlite3_file*);
1237   int (*xDeviceCharacteristics)(sqlite3_file*);
1238   /* Methods above are valid for version 1 */
1239   int (*xShmMap)(sqlite3_file*, int iPg, int pgsz, int, void volatile**);
1240   int (*xShmLock)(sqlite3_file*, int offset, int n, int flags);
1241   void (*xShmBarrier)(sqlite3_file*);
1242   int (*xShmUnmap)(sqlite3_file*, int deleteFlag);
1243   /* Methods above are valid for version 2 */
1244   /* Additional methods may be added in future releases */
1245 };
1246 
1247 /*
1248 ** CAPI3REF: Standard File Control Opcodes
1249 **
1250 ** These integer constants are opcodes for the xFileControl method
1251 ** of the [sqlite3_io_methods] object and for the [sqlite3_file_control()]
1252 ** interface.
1253 **
1254 ** The [SQLITE_FCNTL_LOCKSTATE] opcode is used for debugging.  This
1255 ** opcode causes the xFileControl method to write the current state of
1256 ** the lock (one of [SQLITE_LOCK_NONE], [SQLITE_LOCK_SHARED],
1257 ** [SQLITE_LOCK_RESERVED], [SQLITE_LOCK_PENDING], or [SQLITE_LOCK_EXCLUSIVE])
1258 ** into an integer that the pArg argument points to. This capability
1259 ** is used during testing and only needs to be supported when SQLITE_TEST
1260 ** is defined.
1261 **
1262 ** The [SQLITE_FCNTL_SIZE_HINT] opcode is used by SQLite to give the VFS
1263 ** layer a hint of how large the database file will grow to be during the
1264 ** current transaction.  This hint is not guaranteed to be accurate but it
1265 ** is often close.  The underlying VFS might choose to preallocate database
1266 ** file space based on this hint in order to help writes to the database
1267 ** file run faster.
1268 **
1269 ** The [SQLITE_FCNTL_CHUNK_SIZE] opcode is used to request that the VFS
1270 ** extends and truncates the database file in chunks of a size specified
1271 ** by the user. The fourth argument to [sqlite3_file_control()] should
1272 ** point to an integer (type int) containing the new chunk-size to use
1273 ** for the nominated database. Allocating database file space in large
1274 ** chunks (say 1MB at a time), may reduce file-system fragmentation and
1275 ** improve performance on some systems.
1276 **
1277 ** The [SQLITE_FCNTL_FILE_POINTER] opcode is used to obtain a pointer
1278 ** to the [sqlite3_file] object associated with a particular database
1279 ** connection.  See the [sqlite3_file_control()] documentation for
1280 ** additional information.
1281 **
1282 ** ^(The [SQLITE_FCNTL_SYNC_OMITTED] opcode is generated internally by
1283 ** SQLite and sent to all VFSes in place of a call to the xSync method
1284 ** when the database connection has [PRAGMA synchronous] set to OFF.)^
1285 ** Some specialized VFSes need this signal in order to operate correctly
1286 ** when [PRAGMA synchronous | PRAGMA synchronous=OFF] is set, but most
1287 ** VFSes do not need this signal and should silently ignore this opcode.
1288 ** Applications should not call [sqlite3_file_control()] with this
1289 ** opcode as doing so may disrupt the operation of the specialized VFSes
1290 ** that do require it.
1291 */
1292 #define SQLITE_FCNTL_LOCKSTATE        1
1293 #define SQLITE_GET_LOCKPROXYFILE      2
1294 #define SQLITE_SET_LOCKPROXYFILE      3
1295 #define SQLITE_LAST_ERRNO             4
1296 #define SQLITE_FCNTL_SIZE_HINT        5
1297 #define SQLITE_FCNTL_CHUNK_SIZE       6
1298 #define SQLITE_FCNTL_FILE_POINTER     7
1299 #define SQLITE_FCNTL_SYNC_OMITTED     8
1300 
1301 
1302 /*
1303 ** CAPI3REF: Mutex Handle
1304 **
1305 ** The mutex module within SQLite defines [sqlite3_mutex] to be an
1306 ** abstract type for a mutex object.  The SQLite core never looks
1307 ** at the internal representation of an [sqlite3_mutex].  It only
1308 ** deals with pointers to the [sqlite3_mutex] object.
1309 **
1310 ** Mutexes are created using [sqlite3_mutex_alloc()].
1311 */
1312 typedef struct sqlite3_mutex sqlite3_mutex;
1313 
1314 /*
1315 ** CAPI3REF: OS Interface Object
1316 **
1317 ** An instance of the sqlite3_vfs object defines the interface between
1318 ** the SQLite core and the underlying operating system.  The "vfs"
1319 ** in the name of the object stands for "virtual file system".  See
1320 ** the [VFS | VFS documentation] for further information.
1321 **
1322 ** The value of the iVersion field is initially 1 but may be larger in
1323 ** future versions of SQLite.  Additional fields may be appended to this
1324 ** object when the iVersion value is increased.  Note that the structure
1325 ** of the sqlite3_vfs object changes in the transaction between
1326 ** SQLite version 3.5.9 and 3.6.0 and yet the iVersion field was not
1327 ** modified.
1328 **
1329 ** The szOsFile field is the size of the subclassed [sqlite3_file]
1330 ** structure used by this VFS.  mxPathname is the maximum length of
1331 ** a pathname in this VFS.
1332 **
1333 ** Registered sqlite3_vfs objects are kept on a linked list formed by
1334 ** the pNext pointer.  The [sqlite3_vfs_register()]
1335 ** and [sqlite3_vfs_unregister()] interfaces manage this list
1336 ** in a thread-safe way.  The [sqlite3_vfs_find()] interface
1337 ** searches the list.  Neither the application code nor the VFS
1338 ** implementation should use the pNext pointer.
1339 **
1340 ** The pNext field is the only field in the sqlite3_vfs
1341 ** structure that SQLite will ever modify.  SQLite will only access
1342 ** or modify this field while holding a particular static mutex.
1343 ** The application should never modify anything within the sqlite3_vfs
1344 ** object once the object has been registered.
1345 **
1346 ** The zName field holds the name of the VFS module.  The name must
1347 ** be unique across all VFS modules.
1348 **
1349 ** [[sqlite3_vfs.xOpen]]
1350 ** ^SQLite guarantees that the zFilename parameter to xOpen
1351 ** is either a NULL pointer or string obtained
1352 ** from xFullPathname() with an optional suffix added.
1353 ** ^If a suffix is added to the zFilename parameter, it will
1354 ** consist of a single "-" character followed by no more than
1355 ** 10 alphanumeric and/or "-" characters.
1356 ** ^SQLite further guarantees that
1357 ** the string will be valid and unchanged until xClose() is
1358 ** called. Because of the previous sentence,
1359 ** the [sqlite3_file] can safely store a pointer to the
1360 ** filename if it needs to remember the filename for some reason.
1361 ** If the zFilename parameter to xOpen is a NULL pointer then xOpen
1362 ** must invent its own temporary name for the file.  ^Whenever the
1363 ** xFilename parameter is NULL it will also be the case that the
1364 ** flags parameter will include [SQLITE_OPEN_DELETEONCLOSE].
1365 **
1366 ** The flags argument to xOpen() includes all bits set in
1367 ** the flags argument to [sqlite3_open_v2()].  Or if [sqlite3_open()]
1368 ** or [sqlite3_open16()] is used, then flags includes at least
1369 ** [SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE].
1370 ** If xOpen() opens a file read-only then it sets *pOutFlags to
1371 ** include [SQLITE_OPEN_READONLY].  Other bits in *pOutFlags may be set.
1372 **
1373 ** ^(SQLite will also add one of the following flags to the xOpen()
1374 ** call, depending on the object being opened:
1375 **
1376 ** <ul>
1377 ** <li>  [SQLITE_OPEN_MAIN_DB]
1378 ** <li>  [SQLITE_OPEN_MAIN_JOURNAL]
1379 ** <li>  [SQLITE_OPEN_TEMP_DB]
1380 ** <li>  [SQLITE_OPEN_TEMP_JOURNAL]
1381 ** <li>  [SQLITE_OPEN_TRANSIENT_DB]
1382 ** <li>  [SQLITE_OPEN_SUBJOURNAL]
1383 ** <li>  [SQLITE_OPEN_MASTER_JOURNAL]
1384 ** <li>  [SQLITE_OPEN_WAL]
1385 ** </ul>)^
1386 **
1387 ** The file I/O implementation can use the object type flags to
1388 ** change the way it deals with files.  For example, an application
1389 ** that does not care about crash recovery or rollback might make
1390 ** the open of a journal file a no-op.  Writes to this journal would
1391 ** also be no-ops, and any attempt to read the journal would return
1392 ** SQLITE_IOERR.  Or the implementation might recognize that a database
1393 ** file will be doing page-aligned sector reads and writes in a random
1394 ** order and set up its I/O subsystem accordingly.
1395 **
1396 ** SQLite might also add one of the following flags to the xOpen method:
1397 **
1398 ** <ul>
1399 ** <li> [SQLITE_OPEN_DELETEONCLOSE]
1400 ** <li> [SQLITE_OPEN_EXCLUSIVE]
1401 ** </ul>
1402 **
1403 ** The [SQLITE_OPEN_DELETEONCLOSE] flag means the file should be
1404 ** deleted when it is closed.  ^The [SQLITE_OPEN_DELETEONCLOSE]
1405 ** will be set for TEMP databases and their journals, transient
1406 ** databases, and subjournals.
1407 **
1408 ** ^The [SQLITE_OPEN_EXCLUSIVE] flag is always used in conjunction
1409 ** with the [SQLITE_OPEN_CREATE] flag, which are both directly
1410 ** analogous to the O_EXCL and O_CREAT flags of the POSIX open()
1411 ** API.  The SQLITE_OPEN_EXCLUSIVE flag, when paired with the
1412 ** SQLITE_OPEN_CREATE, is used to indicate that file should always
1413 ** be created, and that it is an error if it already exists.
1414 ** It is <i>not</i> used to indicate the file should be opened
1415 ** for exclusive access.
1416 **
1417 ** ^At least szOsFile bytes of memory are allocated by SQLite
1418 ** to hold the  [sqlite3_file] structure passed as the third
1419 ** argument to xOpen.  The xOpen method does not have to
1420 ** allocate the structure; it should just fill it in.  Note that
1421 ** the xOpen method must set the sqlite3_file.pMethods to either
1422 ** a valid [sqlite3_io_methods] object or to NULL.  xOpen must do
1423 ** this even if the open fails.  SQLite expects that the sqlite3_file.pMethods
1424 ** element will be valid after xOpen returns regardless of the success
1425 ** or failure of the xOpen call.
1426 **
1427 ** [[sqlite3_vfs.xAccess]]
1428 ** ^The flags argument to xAccess() may be [SQLITE_ACCESS_EXISTS]
1429 ** to test for the existence of a file, or [SQLITE_ACCESS_READWRITE] to
1430 ** test whether a file is readable and writable, or [SQLITE_ACCESS_READ]
1431 ** to test whether a file is at least readable.   The file can be a
1432 ** directory.
1433 **
1434 ** ^SQLite will always allocate at least mxPathname+1 bytes for the
1435 ** output buffer xFullPathname.  The exact size of the output buffer
1436 ** is also passed as a parameter to both  methods. If the output buffer
1437 ** is not large enough, [SQLITE_CANTOPEN] should be returned. Since this is
1438 ** handled as a fatal error by SQLite, vfs implementations should endeavor
1439 ** to prevent this by setting mxPathname to a sufficiently large value.
1440 **
1441 ** The xRandomness(), xSleep(), xCurrentTime(), and xCurrentTimeInt64()
1442 ** interfaces are not strictly a part of the filesystem, but they are
1443 ** included in the VFS structure for completeness.
1444 ** The xRandomness() function attempts to return nBytes bytes
1445 ** of good-quality randomness into zOut.  The return value is
1446 ** the actual number of bytes of randomness obtained.
1447 ** The xSleep() method causes the calling thread to sleep for at
1448 ** least the number of microseconds given.  ^The xCurrentTime()
1449 ** method returns a Julian Day Number for the current date and time as
1450 ** a floating point value.
1451 ** ^The xCurrentTimeInt64() method returns, as an integer, the Julian
1452 ** Day Number multiplied by 86400000 (the number of milliseconds in
1453 ** a 24-hour day).
1454 ** ^SQLite will use the xCurrentTimeInt64() method to get the current
1455 ** date and time if that method is available (if iVersion is 2 or
1456 ** greater and the function pointer is not NULL) and will fall back
1457 ** to xCurrentTime() if xCurrentTimeInt64() is unavailable.
1458 **
1459 ** ^The xSetSystemCall(), xGetSystemCall(), and xNestSystemCall() interfaces
1460 ** are not used by the SQLite core.  These optional interfaces are provided
1461 ** by some VFSes to facilitate testing of the VFS code. By overriding
1462 ** system calls with functions under its control, a test program can
1463 ** simulate faults and error conditions that would otherwise be difficult
1464 ** or impossible to induce.  The set of system calls that can be overridden
1465 ** varies from one VFS to another, and from one version of the same VFS to the
1466 ** next.  Applications that use these interfaces must be prepared for any
1467 ** or all of these interfaces to be NULL or for their behavior to change
1468 ** from one release to the next.  Applications must not attempt to access
1469 ** any of these methods if the iVersion of the VFS is less than 3.
1470 */
1471 typedef struct sqlite3_vfs sqlite3_vfs;
1472 typedef void (*sqlite3_syscall_ptr)(void);
1473 struct sqlite3_vfs {
1474   int iVersion;            /* Structure version number (currently 3) */
1475   int szOsFile;            /* Size of subclassed sqlite3_file */
1476   int mxPathname;          /* Maximum file pathname length */
1477   sqlite3_vfs *pNext;      /* Next registered VFS */
1478   const char *zName;       /* Name of this virtual file system */
1479   void *pAppData;          /* Pointer to application-specific data */
1480   int (*xOpen)(sqlite3_vfs*, const char *zName, sqlite3_file*,
1481                int flags, int *pOutFlags);
1482   int (*xDelete)(sqlite3_vfs*, const char *zName, int syncDir);
1483   int (*xAccess)(sqlite3_vfs*, const char *zName, int flags, int *pResOut);
1484   int (*xFullPathname)(sqlite3_vfs*, const char *zName, int nOut, char *zOut);
1485   void *(*xDlOpen)(sqlite3_vfs*, const char *zFilename);
1486   void (*xDlError)(sqlite3_vfs*, int nByte, char *zErrMsg);
1487   void (*(*xDlSym)(sqlite3_vfs*,void*, const char *zSymbol))(void);
1488   void (*xDlClose)(sqlite3_vfs*, void*);
1489   int (*xRandomness)(sqlite3_vfs*, int nByte, char *zOut);
1490   int (*xSleep)(sqlite3_vfs*, int microseconds);
1491   int (*xCurrentTime)(sqlite3_vfs*, double*);
1492   int (*xGetLastError)(sqlite3_vfs*, int, char *);
1493   /*
1494   ** The methods above are in version 1 of the sqlite_vfs object
1495   ** definition.  Those that follow are added in version 2 or later
1496   */
1497   int (*xCurrentTimeInt64)(sqlite3_vfs*, sqlite3_int64*);
1498   /*
1499   ** The methods above are in versions 1 and 2 of the sqlite_vfs object.
1500   ** Those below are for version 3 and greater.
1501   */
1502   int (*xSetSystemCall)(sqlite3_vfs*, const char *zName, sqlite3_syscall_ptr);
1503   sqlite3_syscall_ptr (*xGetSystemCall)(sqlite3_vfs*, const char *zName);
1504   const char *(*xNextSystemCall)(sqlite3_vfs*, const char *zName);
1505   /*
1506   ** The methods above are in versions 1 through 3 of the sqlite_vfs object.
1507   ** New fields may be appended in figure versions.  The iVersion
1508   ** value will increment whenever this happens.
1509   */
1510 };
1511 
1512 /*
1513 ** CAPI3REF: Flags for the xAccess VFS method
1514 **
1515 ** These integer constants can be used as the third parameter to
1516 ** the xAccess method of an [sqlite3_vfs] object.  They determine
1517 ** what kind of permissions the xAccess method is looking for.
1518 ** With SQLITE_ACCESS_EXISTS, the xAccess method
1519 ** simply checks whether the file exists.
1520 ** With SQLITE_ACCESS_READWRITE, the xAccess method
1521 ** checks whether the named directory is both readable and writable
1522 ** (in other words, if files can be added, removed, and renamed within
1523 ** the directory).
1524 ** The SQLITE_ACCESS_READWRITE constant is currently used only by the
1525 ** [temp_store_directory pragma], though this could change in a future
1526 ** release of SQLite.
1527 ** With SQLITE_ACCESS_READ, the xAccess method
1528 ** checks whether the file is readable.  The SQLITE_ACCESS_READ constant is
1529 ** currently unused, though it might be used in a future release of
1530 ** SQLite.
1531 */
1532 #define SQLITE_ACCESS_EXISTS    0
1533 #define SQLITE_ACCESS_READWRITE 1   /* Used by PRAGMA temp_store_directory */
1534 #define SQLITE_ACCESS_READ      2   /* Unused */
1535 
1536 /*
1537 ** CAPI3REF: Flags for the xShmLock VFS method
1538 **
1539 ** These integer constants define the various locking operations
1540 ** allowed by the xShmLock method of [sqlite3_io_methods].  The
1541 ** following are the only legal combinations of flags to the
1542 ** xShmLock method:
1543 **
1544 ** <ul>
1545 ** <li>  SQLITE_SHM_LOCK | SQLITE_SHM_SHARED
1546 ** <li>  SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE
1547 ** <li>  SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED
1548 ** <li>  SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE
1549 ** </ul>
1550 **
1551 ** When unlocking, the same SHARED or EXCLUSIVE flag must be supplied as
1552 ** was given no the corresponding lock.
1553 **
1554 ** The xShmLock method can transition between unlocked and SHARED or
1555 ** between unlocked and EXCLUSIVE.  It cannot transition between SHARED
1556 ** and EXCLUSIVE.
1557 */
1558 #define SQLITE_SHM_UNLOCK       1
1559 #define SQLITE_SHM_LOCK         2
1560 #define SQLITE_SHM_SHARED       4
1561 #define SQLITE_SHM_EXCLUSIVE    8
1562 
1563 /*
1564 ** CAPI3REF: Maximum xShmLock index
1565 **
1566 ** The xShmLock method on [sqlite3_io_methods] may use values
1567 ** between 0 and this upper bound as its "offset" argument.
1568 ** The SQLite core will never attempt to acquire or release a
1569 ** lock outside of this range
1570 */
1571 #define SQLITE_SHM_NLOCK        8
1572 
1573 
1574 /*
1575 ** CAPI3REF: Initialize The SQLite Library
1576 **
1577 ** ^The sqlite3_initialize() routine initializes the
1578 ** SQLite library.  ^The sqlite3_shutdown() routine
1579 ** deallocates any resources that were allocated by sqlite3_initialize().
1580 ** These routines are designed to aid in process initialization and
1581 ** shutdown on embedded systems.  Workstation applications using
1582 ** SQLite normally do not need to invoke either of these routines.
1583 **
1584 ** A call to sqlite3_initialize() is an "effective" call if it is
1585 ** the first time sqlite3_initialize() is invoked during the lifetime of
1586 ** the process, or if it is the first time sqlite3_initialize() is invoked
1587 ** following a call to sqlite3_shutdown().  ^(Only an effective call
1588 ** of sqlite3_initialize() does any initialization.  All other calls
1589 ** are harmless no-ops.)^
1590 **
1591 ** A call to sqlite3_shutdown() is an "effective" call if it is the first
1592 ** call to sqlite3_shutdown() since the last sqlite3_initialize().  ^(Only
1593 ** an effective call to sqlite3_shutdown() does any deinitialization.
1594 ** All other valid calls to sqlite3_shutdown() are harmless no-ops.)^
1595 **
1596 ** The sqlite3_initialize() interface is threadsafe, but sqlite3_shutdown()
1597 ** is not.  The sqlite3_shutdown() interface must only be called from a
1598 ** single thread.  All open [database connections] must be closed and all
1599 ** other SQLite resources must be deallocated prior to invoking
1600 ** sqlite3_shutdown().
1601 **
1602 ** Among other things, ^sqlite3_initialize() will invoke
1603 ** sqlite3_os_init().  Similarly, ^sqlite3_shutdown()
1604 ** will invoke sqlite3_os_end().
1605 **
1606 ** ^The sqlite3_initialize() routine returns [SQLITE_OK] on success.
1607 ** ^If for some reason, sqlite3_initialize() is unable to initialize
1608 ** the library (perhaps it is unable to allocate a needed resource such
1609 ** as a mutex) it returns an [error code] other than [SQLITE_OK].
1610 **
1611 ** ^The sqlite3_initialize() routine is called internally by many other
1612 ** SQLite interfaces so that an application usually does not need to
1613 ** invoke sqlite3_initialize() directly.  For example, [sqlite3_open()]
1614 ** calls sqlite3_initialize() so the SQLite library will be automatically
1615 ** initialized when [sqlite3_open()] is called if it has not be initialized
1616 ** already.  ^However, if SQLite is compiled with the [SQLITE_OMIT_AUTOINIT]
1617 ** compile-time option, then the automatic calls to sqlite3_initialize()
1618 ** are omitted and the application must call sqlite3_initialize() directly
1619 ** prior to using any other SQLite interface.  For maximum portability,
1620 ** it is recommended that applications always invoke sqlite3_initialize()
1621 ** directly prior to using any other SQLite interface.  Future releases
1622 ** of SQLite may require this.  In other words, the behavior exhibited
1623 ** when SQLite is compiled with [SQLITE_OMIT_AUTOINIT] might become the
1624 ** default behavior in some future release of SQLite.
1625 **
1626 ** The sqlite3_os_init() routine does operating-system specific
1627 ** initialization of the SQLite library.  The sqlite3_os_end()
1628 ** routine undoes the effect of sqlite3_os_init().  Typical tasks
1629 ** performed by these routines include allocation or deallocation
1630 ** of static resources, initialization of global variables,
1631 ** setting up a default [sqlite3_vfs] module, or setting up
1632 ** a default configuration using [sqlite3_config()].
1633 **
1634 ** The application should never invoke either sqlite3_os_init()
1635 ** or sqlite3_os_end() directly.  The application should only invoke
1636 ** sqlite3_initialize() and sqlite3_shutdown().  The sqlite3_os_init()
1637 ** interface is called automatically by sqlite3_initialize() and
1638 ** sqlite3_os_end() is called by sqlite3_shutdown().  Appropriate
1639 ** implementations for sqlite3_os_init() and sqlite3_os_end()
1640 ** are built into SQLite when it is compiled for Unix, Windows, or OS/2.
1641 ** When [custom builds | built for other platforms]
1642 ** (using the [SQLITE_OS_OTHER=1] compile-time
1643 ** option) the application must supply a suitable implementation for
1644 ** sqlite3_os_init() and sqlite3_os_end().  An application-supplied
1645 ** implementation of sqlite3_os_init() or sqlite3_os_end()
1646 ** must return [SQLITE_OK] on success and some other [error code] upon
1647 ** failure.
1648 */
1649 SQLITE_API int sqlite3_initialize(void);
1650 SQLITE_API int sqlite3_shutdown(void);
1651 SQLITE_API int sqlite3_os_init(void);
1652 SQLITE_API int sqlite3_os_end(void);
1653 
1654 /*
1655 ** CAPI3REF: Configuring The SQLite Library
1656 **
1657 ** The sqlite3_config() interface is used to make global configuration
1658 ** changes to SQLite in order to tune SQLite to the specific needs of
1659 ** the application.  The default configuration is recommended for most
1660 ** applications and so this routine is usually not necessary.  It is
1661 ** provided to support rare applications with unusual needs.
1662 **
1663 ** The sqlite3_config() interface is not threadsafe.  The application
1664 ** must insure that no other SQLite interfaces are invoked by other
1665 ** threads while sqlite3_config() is running.  Furthermore, sqlite3_config()
1666 ** may only be invoked prior to library initialization using
1667 ** [sqlite3_initialize()] or after shutdown by [sqlite3_shutdown()].
1668 ** ^If sqlite3_config() is called after [sqlite3_initialize()] and before
1669 ** [sqlite3_shutdown()] then it will return SQLITE_MISUSE.
1670 ** Note, however, that ^sqlite3_config() can be called as part of the
1671 ** implementation of an application-defined [sqlite3_os_init()].
1672 **
1673 ** The first argument to sqlite3_config() is an integer
1674 ** [configuration option] that determines
1675 ** what property of SQLite is to be configured.  Subsequent arguments
1676 ** vary depending on the [configuration option]
1677 ** in the first argument.
1678 **
1679 ** ^When a configuration option is set, sqlite3_config() returns [SQLITE_OK].
1680 ** ^If the option is unknown or SQLite is unable to set the option
1681 ** then this routine returns a non-zero [error code].
1682 */
1683 SQLITE_API int sqlite3_config(int, ...);
1684 
1685 /*
1686 ** CAPI3REF: Configure database connections
1687 **
1688 ** The sqlite3_db_config() interface is used to make configuration
1689 ** changes to a [database connection].  The interface is similar to
1690 ** [sqlite3_config()] except that the changes apply to a single
1691 ** [database connection] (specified in the first argument).
1692 **
1693 ** The second argument to sqlite3_db_config(D,V,...)  is the
1694 ** [SQLITE_DBCONFIG_LOOKASIDE | configuration verb] - an integer code
1695 ** that indicates what aspect of the [database connection] is being configured.
1696 ** Subsequent arguments vary depending on the configuration verb.
1697 **
1698 ** ^Calls to sqlite3_db_config() return SQLITE_OK if and only if
1699 ** the call is considered successful.
1700 */
1701 SQLITE_API int sqlite3_db_config(sqlite3*, int op, ...);
1702 
1703 /*
1704 ** CAPI3REF: Memory Allocation Routines
1705 **
1706 ** An instance of this object defines the interface between SQLite
1707 ** and low-level memory allocation routines.
1708 **
1709 ** This object is used in only one place in the SQLite interface.
1710 ** A pointer to an instance of this object is the argument to
1711 ** [sqlite3_config()] when the configuration option is
1712 ** [SQLITE_CONFIG_MALLOC] or [SQLITE_CONFIG_GETMALLOC].
1713 ** By creating an instance of this object
1714 ** and passing it to [sqlite3_config]([SQLITE_CONFIG_MALLOC])
1715 ** during configuration, an application can specify an alternative
1716 ** memory allocation subsystem for SQLite to use for all of its
1717 ** dynamic memory needs.
1718 **
1719 ** Note that SQLite comes with several [built-in memory allocators]
1720 ** that are perfectly adequate for the overwhelming majority of applications
1721 ** and that this object is only useful to a tiny minority of applications
1722 ** with specialized memory allocation requirements.  This object is
1723 ** also used during testing of SQLite in order to specify an alternative
1724 ** memory allocator that simulates memory out-of-memory conditions in
1725 ** order to verify that SQLite recovers gracefully from such
1726 ** conditions.
1727 **
1728 ** The xMalloc and xFree methods must work like the
1729 ** malloc() and free() functions from the standard C library.
1730 ** The xRealloc method must work like realloc() from the standard C library
1731 ** with the exception that if the second argument to xRealloc is zero,
1732 ** xRealloc must be a no-op - it must not perform any allocation or
1733 ** deallocation.  ^SQLite guarantees that the second argument to
1734 ** xRealloc is always a value returned by a prior call to xRoundup.
1735 ** And so in cases where xRoundup always returns a positive number,
1736 ** xRealloc can perform exactly as the standard library realloc() and
1737 ** still be in compliance with this specification.
1738 **
1739 ** xSize should return the allocated size of a memory allocation
1740 ** previously obtained from xMalloc or xRealloc.  The allocated size
1741 ** is always at least as big as the requested size but may be larger.
1742 **
1743 ** The xRoundup method returns what would be the allocated size of
1744 ** a memory allocation given a particular requested size.  Most memory
1745 ** allocators round up memory allocations at least to the next multiple
1746 ** of 8.  Some allocators round up to a larger multiple or to a power of 2.
1747 ** Every memory allocation request coming in through [sqlite3_malloc()]
1748 ** or [sqlite3_realloc()] first calls xRoundup.  If xRoundup returns 0,
1749 ** that causes the corresponding memory allocation to fail.
1750 **
1751 ** The xInit method initializes the memory allocator.  (For example,
1752 ** it might allocate any require mutexes or initialize internal data
1753 ** structures.  The xShutdown method is invoked (indirectly) by
1754 ** [sqlite3_shutdown()] and should deallocate any resources acquired
1755 ** by xInit.  The pAppData pointer is used as the only parameter to
1756 ** xInit and xShutdown.
1757 **
1758 ** SQLite holds the [SQLITE_MUTEX_STATIC_MASTER] mutex when it invokes
1759 ** the xInit method, so the xInit method need not be threadsafe.  The
1760 ** xShutdown method is only called from [sqlite3_shutdown()] so it does
1761 ** not need to be threadsafe either.  For all other methods, SQLite
1762 ** holds the [SQLITE_MUTEX_STATIC_MEM] mutex as long as the
1763 ** [SQLITE_CONFIG_MEMSTATUS] configuration option is turned on (which
1764 ** it is by default) and so the methods are automatically serialized.
1765 ** However, if [SQLITE_CONFIG_MEMSTATUS] is disabled, then the other
1766 ** methods must be threadsafe or else make their own arrangements for
1767 ** serialization.
1768 **
1769 ** SQLite will never invoke xInit() more than once without an intervening
1770 ** call to xShutdown().
1771 */
1772 typedef struct sqlite3_mem_methods sqlite3_mem_methods;
1773 struct sqlite3_mem_methods {
1774   void *(*xMalloc)(int);         /* Memory allocation function */
1775   void (*xFree)(void*);          /* Free a prior allocation */
1776   void *(*xRealloc)(void*,int);  /* Resize an allocation */
1777   int (*xSize)(void*);           /* Return the size of an allocation */
1778   int (*xRoundup)(int);          /* Round up request size to allocation size */
1779   int (*xInit)(void*);           /* Initialize the memory allocator */
1780   void (*xShutdown)(void*);      /* Deinitialize the memory allocator */
1781   void *pAppData;                /* Argument to xInit() and xShutdown() */
1782 };
1783 
1784 /*
1785 ** CAPI3REF: Configuration Options
1786 ** KEYWORDS: {configuration option}
1787 **
1788 ** These constants are the available integer configuration options that
1789 ** can be passed as the first argument to the [sqlite3_config()] interface.
1790 **
1791 ** New configuration options may be added in future releases of SQLite.
1792 ** Existing configuration options might be discontinued.  Applications
1793 ** should check the return code from [sqlite3_config()] to make sure that
1794 ** the call worked.  The [sqlite3_config()] interface will return a
1795 ** non-zero [error code] if a discontinued or unsupported configuration option
1796 ** is invoked.
1797 **
1798 ** <dl>
1799 ** [[SQLITE_CONFIG_SINGLETHREAD]] <dt>SQLITE_CONFIG_SINGLETHREAD</dt>
1800 ** <dd>There are no arguments to this option.  ^This option sets the
1801 ** [threading mode] to Single-thread.  In other words, it disables
1802 ** all mutexing and puts SQLite into a mode where it can only be used
1803 ** by a single thread.   ^If SQLite is compiled with
1804 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
1805 ** it is not possible to change the [threading mode] from its default
1806 ** value of Single-thread and so [sqlite3_config()] will return
1807 ** [SQLITE_ERROR] if called with the SQLITE_CONFIG_SINGLETHREAD
1808 ** configuration option.</dd>
1809 **
1810 ** [[SQLITE_CONFIG_MULTITHREAD]] <dt>SQLITE_CONFIG_MULTITHREAD</dt>
1811 ** <dd>There are no arguments to this option.  ^This option sets the
1812 ** [threading mode] to Multi-thread.  In other words, it disables
1813 ** mutexing on [database connection] and [prepared statement] objects.
1814 ** The application is responsible for serializing access to
1815 ** [database connections] and [prepared statements].  But other mutexes
1816 ** are enabled so that SQLite will be safe to use in a multi-threaded
1817 ** environment as long as no two threads attempt to use the same
1818 ** [database connection] at the same time.  ^If SQLite is compiled with
1819 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
1820 ** it is not possible to set the Multi-thread [threading mode] and
1821 ** [sqlite3_config()] will return [SQLITE_ERROR] if called with the
1822 ** SQLITE_CONFIG_MULTITHREAD configuration option.</dd>
1823 **
1824 ** [[SQLITE_CONFIG_SERIALIZED]] <dt>SQLITE_CONFIG_SERIALIZED</dt>
1825 ** <dd>There are no arguments to this option.  ^This option sets the
1826 ** [threading mode] to Serialized. In other words, this option enables
1827 ** all mutexes including the recursive
1828 ** mutexes on [database connection] and [prepared statement] objects.
1829 ** In this mode (which is the default when SQLite is compiled with
1830 ** [SQLITE_THREADSAFE=1]) the SQLite library will itself serialize access
1831 ** to [database connections] and [prepared statements] so that the
1832 ** application is free to use the same [database connection] or the
1833 ** same [prepared statement] in different threads at the same time.
1834 ** ^If SQLite is compiled with
1835 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
1836 ** it is not possible to set the Serialized [threading mode] and
1837 ** [sqlite3_config()] will return [SQLITE_ERROR] if called with the
1838 ** SQLITE_CONFIG_SERIALIZED configuration option.</dd>
1839 **
1840 ** [[SQLITE_CONFIG_MALLOC]] <dt>SQLITE_CONFIG_MALLOC</dt>
1841 ** <dd> ^(This option takes a single argument which is a pointer to an
1842 ** instance of the [sqlite3_mem_methods] structure.  The argument specifies
1843 ** alternative low-level memory allocation routines to be used in place of
1844 ** the memory allocation routines built into SQLite.)^ ^SQLite makes
1845 ** its own private copy of the content of the [sqlite3_mem_methods] structure
1846 ** before the [sqlite3_config()] call returns.</dd>
1847 **
1848 ** [[SQLITE_CONFIG_GETMALLOC]] <dt>SQLITE_CONFIG_GETMALLOC</dt>
1849 ** <dd> ^(This option takes a single argument which is a pointer to an
1850 ** instance of the [sqlite3_mem_methods] structure.  The [sqlite3_mem_methods]
1851 ** structure is filled with the currently defined memory allocation routines.)^
1852 ** This option can be used to overload the default memory allocation
1853 ** routines with a wrapper that simulations memory allocation failure or
1854 ** tracks memory usage, for example. </dd>
1855 **
1856 ** [[SQLITE_CONFIG_MEMSTATUS]] <dt>SQLITE_CONFIG_MEMSTATUS</dt>
1857 ** <dd> ^This option takes single argument of type int, interpreted as a
1858 ** boolean, which enables or disables the collection of memory allocation
1859 ** statistics. ^(When memory allocation statistics are disabled, the
1860 ** following SQLite interfaces become non-operational:
1861 **   <ul>
1862 **   <li> [sqlite3_memory_used()]
1863 **   <li> [sqlite3_memory_highwater()]
1864 **   <li> [sqlite3_soft_heap_limit64()]
1865 **   <li> [sqlite3_status()]
1866 **   </ul>)^
1867 ** ^Memory allocation statistics are enabled by default unless SQLite is
1868 ** compiled with [SQLITE_DEFAULT_MEMSTATUS]=0 in which case memory
1869 ** allocation statistics are disabled by default.
1870 ** </dd>
1871 **
1872 ** [[SQLITE_CONFIG_SCRATCH]] <dt>SQLITE_CONFIG_SCRATCH</dt>
1873 ** <dd> ^This option specifies a static memory buffer that SQLite can use for
1874 ** scratch memory.  There are three arguments:  A pointer an 8-byte
1875 ** aligned memory buffer from which the scratch allocations will be
1876 ** drawn, the size of each scratch allocation (sz),
1877 ** and the maximum number of scratch allocations (N).  The sz
1878 ** argument must be a multiple of 16.
1879 ** The first argument must be a pointer to an 8-byte aligned buffer
1880 ** of at least sz*N bytes of memory.
1881 ** ^SQLite will use no more than two scratch buffers per thread.  So
1882 ** N should be set to twice the expected maximum number of threads.
1883 ** ^SQLite will never require a scratch buffer that is more than 6
1884 ** times the database page size. ^If SQLite needs needs additional
1885 ** scratch memory beyond what is provided by this configuration option, then
1886 ** [sqlite3_malloc()] will be used to obtain the memory needed.</dd>
1887 **
1888 ** [[SQLITE_CONFIG_PAGECACHE]] <dt>SQLITE_CONFIG_PAGECACHE</dt>
1889 ** <dd> ^This option specifies a static memory buffer that SQLite can use for
1890 ** the database page cache with the default page cache implementation.
1891 ** This configuration should not be used if an application-define page
1892 ** cache implementation is loaded using the SQLITE_CONFIG_PCACHE option.
1893 ** There are three arguments to this option: A pointer to 8-byte aligned
1894 ** memory, the size of each page buffer (sz), and the number of pages (N).
1895 ** The sz argument should be the size of the largest database page
1896 ** (a power of two between 512 and 32768) plus a little extra for each
1897 ** page header.  ^The page header size is 20 to 40 bytes depending on
1898 ** the host architecture.  ^It is harmless, apart from the wasted memory,
1899 ** to make sz a little too large.  The first
1900 ** argument should point to an allocation of at least sz*N bytes of memory.
1901 ** ^SQLite will use the memory provided by the first argument to satisfy its
1902 ** memory needs for the first N pages that it adds to cache.  ^If additional
1903 ** page cache memory is needed beyond what is provided by this option, then
1904 ** SQLite goes to [sqlite3_malloc()] for the additional storage space.
1905 ** The pointer in the first argument must
1906 ** be aligned to an 8-byte boundary or subsequent behavior of SQLite
1907 ** will be undefined.</dd>
1908 **
1909 ** [[SQLITE_CONFIG_HEAP]] <dt>SQLITE_CONFIG_HEAP</dt>
1910 ** <dd> ^This option specifies a static memory buffer that SQLite will use
1911 ** for all of its dynamic memory allocation needs beyond those provided
1912 ** for by [SQLITE_CONFIG_SCRATCH] and [SQLITE_CONFIG_PAGECACHE].
1913 ** There are three arguments: An 8-byte aligned pointer to the memory,
1914 ** the number of bytes in the memory buffer, and the minimum allocation size.
1915 ** ^If the first pointer (the memory pointer) is NULL, then SQLite reverts
1916 ** to using its default memory allocator (the system malloc() implementation),
1917 ** undoing any prior invocation of [SQLITE_CONFIG_MALLOC].  ^If the
1918 ** memory pointer is not NULL and either [SQLITE_ENABLE_MEMSYS3] or
1919 ** [SQLITE_ENABLE_MEMSYS5] are defined, then the alternative memory
1920 ** allocator is engaged to handle all of SQLites memory allocation needs.
1921 ** The first pointer (the memory pointer) must be aligned to an 8-byte
1922 ** boundary or subsequent behavior of SQLite will be undefined.
1923 ** The minimum allocation size is capped at 2^12. Reasonable values
1924 ** for the minimum allocation size are 2^5 through 2^8.</dd>
1925 **
1926 ** [[SQLITE_CONFIG_MUTEX]] <dt>SQLITE_CONFIG_MUTEX</dt>
1927 ** <dd> ^(This option takes a single argument which is a pointer to an
1928 ** instance of the [sqlite3_mutex_methods] structure.  The argument specifies
1929 ** alternative low-level mutex routines to be used in place
1930 ** the mutex routines built into SQLite.)^  ^SQLite makes a copy of the
1931 ** content of the [sqlite3_mutex_methods] structure before the call to
1932 ** [sqlite3_config()] returns. ^If SQLite is compiled with
1933 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
1934 ** the entire mutexing subsystem is omitted from the build and hence calls to
1935 ** [sqlite3_config()] with the SQLITE_CONFIG_MUTEX configuration option will
1936 ** return [SQLITE_ERROR].</dd>
1937 **
1938 ** [[SQLITE_CONFIG_GETMUTEX]] <dt>SQLITE_CONFIG_GETMUTEX</dt>
1939 ** <dd> ^(This option takes a single argument which is a pointer to an
1940 ** instance of the [sqlite3_mutex_methods] structure.  The
1941 ** [sqlite3_mutex_methods]
1942 ** structure is filled with the currently defined mutex routines.)^
1943 ** This option can be used to overload the default mutex allocation
1944 ** routines with a wrapper used to track mutex usage for performance
1945 ** profiling or testing, for example.   ^If SQLite is compiled with
1946 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
1947 ** the entire mutexing subsystem is omitted from the build and hence calls to
1948 ** [sqlite3_config()] with the SQLITE_CONFIG_GETMUTEX configuration option will
1949 ** return [SQLITE_ERROR].</dd>
1950 **
1951 ** [[SQLITE_CONFIG_LOOKASIDE]] <dt>SQLITE_CONFIG_LOOKASIDE</dt>
1952 ** <dd> ^(This option takes two arguments that determine the default
1953 ** memory allocation for the lookaside memory allocator on each
1954 ** [database connection].  The first argument is the
1955 ** size of each lookaside buffer slot and the second is the number of
1956 ** slots allocated to each database connection.)^  ^(This option sets the
1957 ** <i>default</i> lookaside size. The [SQLITE_DBCONFIG_LOOKASIDE]
1958 ** verb to [sqlite3_db_config()] can be used to change the lookaside
1959 ** configuration on individual connections.)^ </dd>
1960 **
1961 ** [[SQLITE_CONFIG_PCACHE]] <dt>SQLITE_CONFIG_PCACHE</dt>
1962 ** <dd> ^(This option takes a single argument which is a pointer to
1963 ** an [sqlite3_pcache_methods] object.  This object specifies the interface
1964 ** to a custom page cache implementation.)^  ^SQLite makes a copy of the
1965 ** object and uses it for page cache memory allocations.</dd>
1966 **
1967 ** [[SQLITE_CONFIG_GETPCACHE]] <dt>SQLITE_CONFIG_GETPCACHE</dt>
1968 ** <dd> ^(This option takes a single argument which is a pointer to an
1969 ** [sqlite3_pcache_methods] object.  SQLite copies of the current
1970 ** page cache implementation into that object.)^ </dd>
1971 **
1972 ** [[SQLITE_CONFIG_LOG]] <dt>SQLITE_CONFIG_LOG</dt>
1973 ** <dd> ^The SQLITE_CONFIG_LOG option takes two arguments: a pointer to a
1974 ** function with a call signature of void(*)(void*,int,const char*),
1975 ** and a pointer to void. ^If the function pointer is not NULL, it is
1976 ** invoked by [sqlite3_log()] to process each logging event.  ^If the
1977 ** function pointer is NULL, the [sqlite3_log()] interface becomes a no-op.
1978 ** ^The void pointer that is the second argument to SQLITE_CONFIG_LOG is
1979 ** passed through as the first parameter to the application-defined logger
1980 ** function whenever that function is invoked.  ^The second parameter to
1981 ** the logger function is a copy of the first parameter to the corresponding
1982 ** [sqlite3_log()] call and is intended to be a [result code] or an
1983 ** [extended result code].  ^The third parameter passed to the logger is
1984 ** log message after formatting via [sqlite3_snprintf()].
1985 ** The SQLite logging interface is not reentrant; the logger function
1986 ** supplied by the application must not invoke any SQLite interface.
1987 ** In a multi-threaded application, the application-defined logger
1988 ** function must be threadsafe. </dd>
1989 **
1990 ** [[SQLITE_CONFIG_URI]] <dt>SQLITE_CONFIG_URI
1991 ** <dd> This option takes a single argument of type int. If non-zero, then
1992 ** URI handling is globally enabled. If the parameter is zero, then URI handling
1993 ** is globally disabled. If URI handling is globally enabled, all filenames
1994 ** passed to [sqlite3_open()], [sqlite3_open_v2()], [sqlite3_open16()] or
1995 ** specified as part of [ATTACH] commands are interpreted as URIs, regardless
1996 ** of whether or not the [SQLITE_OPEN_URI] flag is set when the database
1997 ** connection is opened. If it is globally disabled, filenames are
1998 ** only interpreted as URIs if the SQLITE_OPEN_URI flag is set when the
1999 ** database connection is opened. By default, URI handling is globally
2000 ** disabled. The default value may be changed by compiling with the
2001 ** [SQLITE_USE_URI] symbol defined.
2002 ** </dl>
2003 */
2004 #define SQLITE_CONFIG_SINGLETHREAD  1  /* nil */
2005 #define SQLITE_CONFIG_MULTITHREAD   2  /* nil */
2006 #define SQLITE_CONFIG_SERIALIZED    3  /* nil */
2007 #define SQLITE_CONFIG_MALLOC        4  /* sqlite3_mem_methods* */
2008 #define SQLITE_CONFIG_GETMALLOC     5  /* sqlite3_mem_methods* */
2009 #define SQLITE_CONFIG_SCRATCH       6  /* void*, int sz, int N */
2010 #define SQLITE_CONFIG_PAGECACHE     7  /* void*, int sz, int N */
2011 #define SQLITE_CONFIG_HEAP          8  /* void*, int nByte, int min */
2012 #define SQLITE_CONFIG_MEMSTATUS     9  /* boolean */
2013 #define SQLITE_CONFIG_MUTEX        10  /* sqlite3_mutex_methods* */
2014 #define SQLITE_CONFIG_GETMUTEX     11  /* sqlite3_mutex_methods* */
2015 /* previously SQLITE_CONFIG_CHUNKALLOC 12 which is now unused. */
2016 #define SQLITE_CONFIG_LOOKASIDE    13  /* int int */
2017 #define SQLITE_CONFIG_PCACHE       14  /* sqlite3_pcache_methods* */
2018 #define SQLITE_CONFIG_GETPCACHE    15  /* sqlite3_pcache_methods* */
2019 #define SQLITE_CONFIG_LOG          16  /* xFunc, void* */
2020 #define SQLITE_CONFIG_URI          17  /* int */
2021 
2022 /*
2023 ** CAPI3REF: Database Connection Configuration Options
2024 **
2025 ** These constants are the available integer configuration options that
2026 ** can be passed as the second argument to the [sqlite3_db_config()] interface.
2027 **
2028 ** New configuration options may be added in future releases of SQLite.
2029 ** Existing configuration options might be discontinued.  Applications
2030 ** should check the return code from [sqlite3_db_config()] to make sure that
2031 ** the call worked.  ^The [sqlite3_db_config()] interface will return a
2032 ** non-zero [error code] if a discontinued or unsupported configuration option
2033 ** is invoked.
2034 **
2035 ** <dl>
2036 ** <dt>SQLITE_DBCONFIG_LOOKASIDE</dt>
2037 ** <dd> ^This option takes three additional arguments that determine the
2038 ** [lookaside memory allocator] configuration for the [database connection].
2039 ** ^The first argument (the third parameter to [sqlite3_db_config()] is a
2040 ** pointer to a memory buffer to use for lookaside memory.
2041 ** ^The first argument after the SQLITE_DBCONFIG_LOOKASIDE verb
2042 ** may be NULL in which case SQLite will allocate the
2043 ** lookaside buffer itself using [sqlite3_malloc()]. ^The second argument is the
2044 ** size of each lookaside buffer slot.  ^The third argument is the number of
2045 ** slots.  The size of the buffer in the first argument must be greater than
2046 ** or equal to the product of the second and third arguments.  The buffer
2047 ** must be aligned to an 8-byte boundary.  ^If the second argument to
2048 ** SQLITE_DBCONFIG_LOOKASIDE is not a multiple of 8, it is internally
2049 ** rounded down to the next smaller multiple of 8.  ^(The lookaside memory
2050 ** configuration for a database connection can only be changed when that
2051 ** connection is not currently using lookaside memory, or in other words
2052 ** when the "current value" returned by
2053 ** [sqlite3_db_status](D,[SQLITE_CONFIG_LOOKASIDE],...) is zero.
2054 ** Any attempt to change the lookaside memory configuration when lookaside
2055 ** memory is in use leaves the configuration unchanged and returns
2056 ** [SQLITE_BUSY].)^</dd>
2057 **
2058 ** <dt>SQLITE_DBCONFIG_ENABLE_FKEY</dt>
2059 ** <dd> ^This option is used to enable or disable the enforcement of
2060 ** [foreign key constraints].  There should be two additional arguments.
2061 ** The first argument is an integer which is 0 to disable FK enforcement,
2062 ** positive to enable FK enforcement or negative to leave FK enforcement
2063 ** unchanged.  The second parameter is a pointer to an integer into which
2064 ** is written 0 or 1 to indicate whether FK enforcement is off or on
2065 ** following this call.  The second parameter may be a NULL pointer, in
2066 ** which case the FK enforcement setting is not reported back. </dd>
2067 **
2068 ** <dt>SQLITE_DBCONFIG_ENABLE_TRIGGER</dt>
2069 ** <dd> ^This option is used to enable or disable [CREATE TRIGGER | triggers].
2070 ** There should be two additional arguments.
2071 ** The first argument is an integer which is 0 to disable triggers,
2072 ** positive to enable triggers or negative to leave the setting unchanged.
2073 ** The second parameter is a pointer to an integer into which
2074 ** is written 0 or 1 to indicate whether triggers are disabled or enabled
2075 ** following this call.  The second parameter may be a NULL pointer, in
2076 ** which case the trigger setting is not reported back. </dd>
2077 **
2078 ** </dl>
2079 */
2080 #define SQLITE_DBCONFIG_LOOKASIDE       1001  /* void* int int */
2081 #define SQLITE_DBCONFIG_ENABLE_FKEY     1002  /* int int* */
2082 #define SQLITE_DBCONFIG_ENABLE_TRIGGER  1003  /* int int* */
2083 
2084 
2085 /*
2086 ** CAPI3REF: Enable Or Disable Extended Result Codes
2087 **
2088 ** ^The sqlite3_extended_result_codes() routine enables or disables the
2089 ** [extended result codes] feature of SQLite. ^The extended result
2090 ** codes are disabled by default for historical compatibility.
2091 */
2092 SQLITE_API int sqlite3_extended_result_codes(sqlite3*, int onoff);
2093 
2094 /*
2095 ** CAPI3REF: Last Insert Rowid
2096 **
2097 ** ^Each entry in an SQLite table has a unique 64-bit signed
2098 ** integer key called the [ROWID | "rowid"]. ^The rowid is always available
2099 ** as an undeclared column named ROWID, OID, or _ROWID_ as long as those
2100 ** names are not also used by explicitly declared columns. ^If
2101 ** the table has a column of type [INTEGER PRIMARY KEY] then that column
2102 ** is another alias for the rowid.
2103 **
2104 ** ^This routine returns the [rowid] of the most recent
2105 ** successful [INSERT] into the database from the [database connection]
2106 ** in the first argument.  ^As of SQLite version 3.7.7, this routines
2107 ** records the last insert rowid of both ordinary tables and [virtual tables].
2108 ** ^If no successful [INSERT]s
2109 ** have ever occurred on that database connection, zero is returned.
2110 **
2111 ** ^(If an [INSERT] occurs within a trigger or within a [virtual table]
2112 ** method, then this routine will return the [rowid] of the inserted
2113 ** row as long as the trigger or virtual table method is running.
2114 ** But once the trigger or virtual table method ends, the value returned
2115 ** by this routine reverts to what it was before the trigger or virtual
2116 ** table method began.)^
2117 **
2118 ** ^An [INSERT] that fails due to a constraint violation is not a
2119 ** successful [INSERT] and does not change the value returned by this
2120 ** routine.  ^Thus INSERT OR FAIL, INSERT OR IGNORE, INSERT OR ROLLBACK,
2121 ** and INSERT OR ABORT make no changes to the return value of this
2122 ** routine when their insertion fails.  ^(When INSERT OR REPLACE
2123 ** encounters a constraint violation, it does not fail.  The
2124 ** INSERT continues to completion after deleting rows that caused
2125 ** the constraint problem so INSERT OR REPLACE will always change
2126 ** the return value of this interface.)^
2127 **
2128 ** ^For the purposes of this routine, an [INSERT] is considered to
2129 ** be successful even if it is subsequently rolled back.
2130 **
2131 ** This function is accessible to SQL statements via the
2132 ** [last_insert_rowid() SQL function].
2133 **
2134 ** If a separate thread performs a new [INSERT] on the same
2135 ** database connection while the [sqlite3_last_insert_rowid()]
2136 ** function is running and thus changes the last insert [rowid],
2137 ** then the value returned by [sqlite3_last_insert_rowid()] is
2138 ** unpredictable and might not equal either the old or the new
2139 ** last insert [rowid].
2140 */
2141 SQLITE_API sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*);
2142 
2143 /*
2144 ** CAPI3REF: Count The Number Of Rows Modified
2145 **
2146 ** ^This function returns the number of database rows that were changed
2147 ** or inserted or deleted by the most recently completed SQL statement
2148 ** on the [database connection] specified by the first parameter.
2149 ** ^(Only changes that are directly specified by the [INSERT], [UPDATE],
2150 ** or [DELETE] statement are counted.  Auxiliary changes caused by
2151 ** triggers or [foreign key actions] are not counted.)^ Use the
2152 ** [sqlite3_total_changes()] function to find the total number of changes
2153 ** including changes caused by triggers and foreign key actions.
2154 **
2155 ** ^Changes to a view that are simulated by an [INSTEAD OF trigger]
2156 ** are not counted.  Only real table changes are counted.
2157 **
2158 ** ^(A "row change" is a change to a single row of a single table
2159 ** caused by an INSERT, DELETE, or UPDATE statement.  Rows that
2160 ** are changed as side effects of [REPLACE] constraint resolution,
2161 ** rollback, ABORT processing, [DROP TABLE], or by any other
2162 ** mechanisms do not count as direct row changes.)^
2163 **
2164 ** A "trigger context" is a scope of execution that begins and
2165 ** ends with the script of a [CREATE TRIGGER | trigger].
2166 ** Most SQL statements are
2167 ** evaluated outside of any trigger.  This is the "top level"
2168 ** trigger context.  If a trigger fires from the top level, a
2169 ** new trigger context is entered for the duration of that one
2170 ** trigger.  Subtriggers create subcontexts for their duration.
2171 **
2172 ** ^Calling [sqlite3_exec()] or [sqlite3_step()] recursively does
2173 ** not create a new trigger context.
2174 **
2175 ** ^This function returns the number of direct row changes in the
2176 ** most recent INSERT, UPDATE, or DELETE statement within the same
2177 ** trigger context.
2178 **
2179 ** ^Thus, when called from the top level, this function returns the
2180 ** number of changes in the most recent INSERT, UPDATE, or DELETE
2181 ** that also occurred at the top level.  ^(Within the body of a trigger,
2182 ** the sqlite3_changes() interface can be called to find the number of
2183 ** changes in the most recently completed INSERT, UPDATE, or DELETE
2184 ** statement within the body of the same trigger.
2185 ** However, the number returned does not include changes
2186 ** caused by subtriggers since those have their own context.)^
2187 **
2188 ** See also the [sqlite3_total_changes()] interface, the
2189 ** [count_changes pragma], and the [changes() SQL function].
2190 **
2191 ** If a separate thread makes changes on the same database connection
2192 ** while [sqlite3_changes()] is running then the value returned
2193 ** is unpredictable and not meaningful.
2194 */
2195 SQLITE_API int sqlite3_changes(sqlite3*);
2196 
2197 /*
2198 ** CAPI3REF: Total Number Of Rows Modified
2199 **
2200 ** ^This function returns the number of row changes caused by [INSERT],
2201 ** [UPDATE] or [DELETE] statements since the [database connection] was opened.
2202 ** ^(The count returned by sqlite3_total_changes() includes all changes
2203 ** from all [CREATE TRIGGER | trigger] contexts and changes made by
2204 ** [foreign key actions]. However,
2205 ** the count does not include changes used to implement [REPLACE] constraints,
2206 ** do rollbacks or ABORT processing, or [DROP TABLE] processing.  The
2207 ** count does not include rows of views that fire an [INSTEAD OF trigger],
2208 ** though if the INSTEAD OF trigger makes changes of its own, those changes
2209 ** are counted.)^
2210 ** ^The sqlite3_total_changes() function counts the changes as soon as
2211 ** the statement that makes them is completed (when the statement handle
2212 ** is passed to [sqlite3_reset()] or [sqlite3_finalize()]).
2213 **
2214 ** See also the [sqlite3_changes()] interface, the
2215 ** [count_changes pragma], and the [total_changes() SQL function].
2216 **
2217 ** If a separate thread makes changes on the same database connection
2218 ** while [sqlite3_total_changes()] is running then the value
2219 ** returned is unpredictable and not meaningful.
2220 */
2221 SQLITE_API int sqlite3_total_changes(sqlite3*);
2222 
2223 /*
2224 ** CAPI3REF: Interrupt A Long-Running Query
2225 **
2226 ** ^This function causes any pending database operation to abort and
2227 ** return at its earliest opportunity. This routine is typically
2228 ** called in response to a user action such as pressing "Cancel"
2229 ** or Ctrl-C where the user wants a long query operation to halt
2230 ** immediately.
2231 **
2232 ** ^It is safe to call this routine from a thread different from the
2233 ** thread that is currently running the database operation.  But it
2234 ** is not safe to call this routine with a [database connection] that
2235 ** is closed or might close before sqlite3_interrupt() returns.
2236 **
2237 ** ^If an SQL operation is very nearly finished at the time when
2238 ** sqlite3_interrupt() is called, then it might not have an opportunity
2239 ** to be interrupted and might continue to completion.
2240 **
2241 ** ^An SQL operation that is interrupted will return [SQLITE_INTERRUPT].
2242 ** ^If the interrupted SQL operation is an INSERT, UPDATE, or DELETE
2243 ** that is inside an explicit transaction, then the entire transaction
2244 ** will be rolled back automatically.
2245 **
2246 ** ^The sqlite3_interrupt(D) call is in effect until all currently running
2247 ** SQL statements on [database connection] D complete.  ^Any new SQL statements
2248 ** that are started after the sqlite3_interrupt() call and before the
2249 ** running statements reaches zero are interrupted as if they had been
2250 ** running prior to the sqlite3_interrupt() call.  ^New SQL statements
2251 ** that are started after the running statement count reaches zero are
2252 ** not effected by the sqlite3_interrupt().
2253 ** ^A call to sqlite3_interrupt(D) that occurs when there are no running
2254 ** SQL statements is a no-op and has no effect on SQL statements
2255 ** that are started after the sqlite3_interrupt() call returns.
2256 **
2257 ** If the database connection closes while [sqlite3_interrupt()]
2258 ** is running then bad things will likely happen.
2259 */
2260 SQLITE_API void sqlite3_interrupt(sqlite3*);
2261 
2262 /*
2263 ** CAPI3REF: Determine If An SQL Statement Is Complete
2264 **
2265 ** These routines are useful during command-line input to determine if the
2266 ** currently entered text seems to form a complete SQL statement or
2267 ** if additional input is needed before sending the text into
2268 ** SQLite for parsing.  ^These routines return 1 if the input string
2269 ** appears to be a complete SQL statement.  ^A statement is judged to be
2270 ** complete if it ends with a semicolon token and is not a prefix of a
2271 ** well-formed CREATE TRIGGER statement.  ^Semicolons that are embedded within
2272 ** string literals or quoted identifier names or comments are not
2273 ** independent tokens (they are part of the token in which they are
2274 ** embedded) and thus do not count as a statement terminator.  ^Whitespace
2275 ** and comments that follow the final semicolon are ignored.
2276 **
2277 ** ^These routines return 0 if the statement is incomplete.  ^If a
2278 ** memory allocation fails, then SQLITE_NOMEM is returned.
2279 **
2280 ** ^These routines do not parse the SQL statements thus
2281 ** will not detect syntactically incorrect SQL.
2282 **
2283 ** ^(If SQLite has not been initialized using [sqlite3_initialize()] prior
2284 ** to invoking sqlite3_complete16() then sqlite3_initialize() is invoked
2285 ** automatically by sqlite3_complete16().  If that initialization fails,
2286 ** then the return value from sqlite3_complete16() will be non-zero
2287 ** regardless of whether or not the input SQL is complete.)^
2288 **
2289 ** The input to [sqlite3_complete()] must be a zero-terminated
2290 ** UTF-8 string.
2291 **
2292 ** The input to [sqlite3_complete16()] must be a zero-terminated
2293 ** UTF-16 string in native byte order.
2294 */
2295 SQLITE_API int sqlite3_complete(const char *sql);
2296 SQLITE_API int sqlite3_complete16(const void *sql);
2297 
2298 /*
2299 ** CAPI3REF: Register A Callback To Handle SQLITE_BUSY Errors
2300 **
2301 ** ^This routine sets a callback function that might be invoked whenever
2302 ** an attempt is made to open a database table that another thread
2303 ** or process has locked.
2304 **
2305 ** ^If the busy callback is NULL, then [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED]
2306 ** is returned immediately upon encountering the lock.  ^If the busy callback
2307 ** is not NULL, then the callback might be invoked with two arguments.
2308 **
2309 ** ^The first argument to the busy handler is a copy of the void* pointer which
2310 ** is the third argument to sqlite3_busy_handler().  ^The second argument to
2311 ** the busy handler callback is the number of times that the busy handler has
2312 ** been invoked for this locking event.  ^If the
2313 ** busy callback returns 0, then no additional attempts are made to
2314 ** access the database and [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED] is returned.
2315 ** ^If the callback returns non-zero, then another attempt
2316 ** is made to open the database for reading and the cycle repeats.
2317 **
2318 ** The presence of a busy handler does not guarantee that it will be invoked
2319 ** when there is lock contention. ^If SQLite determines that invoking the busy
2320 ** handler could result in a deadlock, it will go ahead and return [SQLITE_BUSY]
2321 ** or [SQLITE_IOERR_BLOCKED] instead of invoking the busy handler.
2322 ** Consider a scenario where one process is holding a read lock that
2323 ** it is trying to promote to a reserved lock and
2324 ** a second process is holding a reserved lock that it is trying
2325 ** to promote to an exclusive lock.  The first process cannot proceed
2326 ** because it is blocked by the second and the second process cannot
2327 ** proceed because it is blocked by the first.  If both processes
2328 ** invoke the busy handlers, neither will make any progress.  Therefore,
2329 ** SQLite returns [SQLITE_BUSY] for the first process, hoping that this
2330 ** will induce the first process to release its read lock and allow
2331 ** the second process to proceed.
2332 **
2333 ** ^The default busy callback is NULL.
2334 **
2335 ** ^The [SQLITE_BUSY] error is converted to [SQLITE_IOERR_BLOCKED]
2336 ** when SQLite is in the middle of a large transaction where all the
2337 ** changes will not fit into the in-memory cache.  SQLite will
2338 ** already hold a RESERVED lock on the database file, but it needs
2339 ** to promote this lock to EXCLUSIVE so that it can spill cache
2340 ** pages into the database file without harm to concurrent
2341 ** readers.  ^If it is unable to promote the lock, then the in-memory
2342 ** cache will be left in an inconsistent state and so the error
2343 ** code is promoted from the relatively benign [SQLITE_BUSY] to
2344 ** the more severe [SQLITE_IOERR_BLOCKED].  ^This error code promotion
2345 ** forces an automatic rollback of the changes.  See the
2346 ** <a href="/cvstrac/wiki?p=CorruptionFollowingBusyError">
2347 ** CorruptionFollowingBusyError</a> wiki page for a discussion of why
2348 ** this is important.
2349 **
2350 ** ^(There can only be a single busy handler defined for each
2351 ** [database connection].  Setting a new busy handler clears any
2352 ** previously set handler.)^  ^Note that calling [sqlite3_busy_timeout()]
2353 ** will also set or clear the busy handler.
2354 **
2355 ** The busy callback should not take any actions which modify the
2356 ** database connection that invoked the busy handler.  Any such actions
2357 ** result in undefined behavior.
2358 **
2359 ** A busy handler must not close the database connection
2360 ** or [prepared statement] that invoked the busy handler.
2361 */
2362 SQLITE_API int sqlite3_busy_handler(sqlite3*, int(*)(void*,int), void*);
2363 
2364 /*
2365 ** CAPI3REF: Set A Busy Timeout
2366 **
2367 ** ^This routine sets a [sqlite3_busy_handler | busy handler] that sleeps
2368 ** for a specified amount of time when a table is locked.  ^The handler
2369 ** will sleep multiple times until at least "ms" milliseconds of sleeping
2370 ** have accumulated.  ^After at least "ms" milliseconds of sleeping,
2371 ** the handler returns 0 which causes [sqlite3_step()] to return
2372 ** [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED].
2373 **
2374 ** ^Calling this routine with an argument less than or equal to zero
2375 ** turns off all busy handlers.
2376 **
2377 ** ^(There can only be a single busy handler for a particular
2378 ** [database connection] any any given moment.  If another busy handler
2379 ** was defined  (using [sqlite3_busy_handler()]) prior to calling
2380 ** this routine, that other busy handler is cleared.)^
2381 */
2382 SQLITE_API int sqlite3_busy_timeout(sqlite3*, int ms);
2383 
2384 /*
2385 ** CAPI3REF: Convenience Routines For Running Queries
2386 **
2387 ** This is a legacy interface that is preserved for backwards compatibility.
2388 ** Use of this interface is not recommended.
2389 **
2390 ** Definition: A <b>result table</b> is memory data structure created by the
2391 ** [sqlite3_get_table()] interface.  A result table records the
2392 ** complete query results from one or more queries.
2393 **
2394 ** The table conceptually has a number of rows and columns.  But
2395 ** these numbers are not part of the result table itself.  These
2396 ** numbers are obtained separately.  Let N be the number of rows
2397 ** and M be the number of columns.
2398 **
2399 ** A result table is an array of pointers to zero-terminated UTF-8 strings.
2400 ** There are (N+1)*M elements in the array.  The first M pointers point
2401 ** to zero-terminated strings that  contain the names of the columns.
2402 ** The remaining entries all point to query results.  NULL values result
2403 ** in NULL pointers.  All other values are in their UTF-8 zero-terminated
2404 ** string representation as returned by [sqlite3_column_text()].
2405 **
2406 ** A result table might consist of one or more memory allocations.
2407 ** It is not safe to pass a result table directly to [sqlite3_free()].
2408 ** A result table should be deallocated using [sqlite3_free_table()].
2409 **
2410 ** ^(As an example of the result table format, suppose a query result
2411 ** is as follows:
2412 **
2413 ** <blockquote><pre>
2414 **        Name        | Age
2415 **        -----------------------
2416 **        Alice       | 43
2417 **        Bob         | 28
2418 **        Cindy       | 21
2419 ** </pre></blockquote>
2420 **
2421 ** There are two column (M==2) and three rows (N==3).  Thus the
2422 ** result table has 8 entries.  Suppose the result table is stored
2423 ** in an array names azResult.  Then azResult holds this content:
2424 **
2425 ** <blockquote><pre>
2426 **        azResult&#91;0] = "Name";
2427 **        azResult&#91;1] = "Age";
2428 **        azResult&#91;2] = "Alice";
2429 **        azResult&#91;3] = "43";
2430 **        azResult&#91;4] = "Bob";
2431 **        azResult&#91;5] = "28";
2432 **        azResult&#91;6] = "Cindy";
2433 **        azResult&#91;7] = "21";
2434 ** </pre></blockquote>)^
2435 **
2436 ** ^The sqlite3_get_table() function evaluates one or more
2437 ** semicolon-separated SQL statements in the zero-terminated UTF-8
2438 ** string of its 2nd parameter and returns a result table to the
2439 ** pointer given in its 3rd parameter.
2440 **
2441 ** After the application has finished with the result from sqlite3_get_table(),
2442 ** it must pass the result table pointer to sqlite3_free_table() in order to
2443 ** release the memory that was malloced.  Because of the way the
2444 ** [sqlite3_malloc()] happens within sqlite3_get_table(), the calling
2445 ** function must not try to call [sqlite3_free()] directly.  Only
2446 ** [sqlite3_free_table()] is able to release the memory properly and safely.
2447 **
2448 ** The sqlite3_get_table() interface is implemented as a wrapper around
2449 ** [sqlite3_exec()].  The sqlite3_get_table() routine does not have access
2450 ** to any internal data structures of SQLite.  It uses only the public
2451 ** interface defined here.  As a consequence, errors that occur in the
2452 ** wrapper layer outside of the internal [sqlite3_exec()] call are not
2453 ** reflected in subsequent calls to [sqlite3_errcode()] or
2454 ** [sqlite3_errmsg()].
2455 */
2456 SQLITE_API int sqlite3_get_table(
2457   sqlite3 *db,          /* An open database */
2458   const char *zSql,     /* SQL to be evaluated */
2459   char ***pazResult,    /* Results of the query */
2460   int *pnRow,           /* Number of result rows written here */
2461   int *pnColumn,        /* Number of result columns written here */
2462   char **pzErrmsg       /* Error msg written here */
2463 );
2464 SQLITE_API void sqlite3_free_table(char **result);
2465 
2466 /*
2467 ** CAPI3REF: Formatted String Printing Functions
2468 **
2469 ** These routines are work-alikes of the "printf()" family of functions
2470 ** from the standard C library.
2471 **
2472 ** ^The sqlite3_mprintf() and sqlite3_vmprintf() routines write their
2473 ** results into memory obtained from [sqlite3_malloc()].
2474 ** The strings returned by these two routines should be
2475 ** released by [sqlite3_free()].  ^Both routines return a
2476 ** NULL pointer if [sqlite3_malloc()] is unable to allocate enough
2477 ** memory to hold the resulting string.
2478 **
2479 ** ^(The sqlite3_snprintf() routine is similar to "snprintf()" from
2480 ** the standard C library.  The result is written into the
2481 ** buffer supplied as the second parameter whose size is given by
2482 ** the first parameter. Note that the order of the
2483 ** first two parameters is reversed from snprintf().)^  This is an
2484 ** historical accident that cannot be fixed without breaking
2485 ** backwards compatibility.  ^(Note also that sqlite3_snprintf()
2486 ** returns a pointer to its buffer instead of the number of
2487 ** characters actually written into the buffer.)^  We admit that
2488 ** the number of characters written would be a more useful return
2489 ** value but we cannot change the implementation of sqlite3_snprintf()
2490 ** now without breaking compatibility.
2491 **
2492 ** ^As long as the buffer size is greater than zero, sqlite3_snprintf()
2493 ** guarantees that the buffer is always zero-terminated.  ^The first
2494 ** parameter "n" is the total size of the buffer, including space for
2495 ** the zero terminator.  So the longest string that can be completely
2496 ** written will be n-1 characters.
2497 **
2498 ** ^The sqlite3_vsnprintf() routine is a varargs version of sqlite3_snprintf().
2499 **
2500 ** These routines all implement some additional formatting
2501 ** options that are useful for constructing SQL statements.
2502 ** All of the usual printf() formatting options apply.  In addition, there
2503 ** is are "%q", "%Q", and "%z" options.
2504 **
2505 ** ^(The %q option works like %s in that it substitutes a null-terminated
2506 ** string from the argument list.  But %q also doubles every '\'' character.
2507 ** %q is designed for use inside a string literal.)^  By doubling each '\''
2508 ** character it escapes that character and allows it to be inserted into
2509 ** the string.
2510 **
2511 ** For example, assume the string variable zText contains text as follows:
2512 **
2513 ** <blockquote><pre>
2514 **  char *zText = "It's a happy day!";
2515 ** </pre></blockquote>
2516 **
2517 ** One can use this text in an SQL statement as follows:
2518 **
2519 ** <blockquote><pre>
2520 **  char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES('%q')", zText);
2521 **  sqlite3_exec(db, zSQL, 0, 0, 0);
2522 **  sqlite3_free(zSQL);
2523 ** </pre></blockquote>
2524 **
2525 ** Because the %q format string is used, the '\'' character in zText
2526 ** is escaped and the SQL generated is as follows:
2527 **
2528 ** <blockquote><pre>
2529 **  INSERT INTO table1 VALUES('It''s a happy day!')
2530 ** </pre></blockquote>
2531 **
2532 ** This is correct.  Had we used %s instead of %q, the generated SQL
2533 ** would have looked like this:
2534 **
2535 ** <blockquote><pre>
2536 **  INSERT INTO table1 VALUES('It's a happy day!');
2537 ** </pre></blockquote>
2538 **
2539 ** This second example is an SQL syntax error.  As a general rule you should
2540 ** always use %q instead of %s when inserting text into a string literal.
2541 **
2542 ** ^(The %Q option works like %q except it also adds single quotes around
2543 ** the outside of the total string.  Additionally, if the parameter in the
2544 ** argument list is a NULL pointer, %Q substitutes the text "NULL" (without
2545 ** single quotes).)^  So, for example, one could say:
2546 **
2547 ** <blockquote><pre>
2548 **  char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES(%Q)", zText);
2549 **  sqlite3_exec(db, zSQL, 0, 0, 0);
2550 **  sqlite3_free(zSQL);
2551 ** </pre></blockquote>
2552 **
2553 ** The code above will render a correct SQL statement in the zSQL
2554 ** variable even if the zText variable is a NULL pointer.
2555 **
2556 ** ^(The "%z" formatting option works like "%s" but with the
2557 ** addition that after the string has been read and copied into
2558 ** the result, [sqlite3_free()] is called on the input string.)^
2559 */
2560 SQLITE_API char *sqlite3_mprintf(const char*,...);
2561 SQLITE_API char *sqlite3_vmprintf(const char*, va_list);
2562 SQLITE_API char *sqlite3_snprintf(int,char*,const char*, ...);
2563 SQLITE_API char *sqlite3_vsnprintf(int,char*,const char*, va_list);
2564 
2565 /*
2566 ** CAPI3REF: Memory Allocation Subsystem
2567 **
2568 ** The SQLite core uses these three routines for all of its own
2569 ** internal memory allocation needs. "Core" in the previous sentence
2570 ** does not include operating-system specific VFS implementation.  The
2571 ** Windows VFS uses native malloc() and free() for some operations.
2572 **
2573 ** ^The sqlite3_malloc() routine returns a pointer to a block
2574 ** of memory at least N bytes in length, where N is the parameter.
2575 ** ^If sqlite3_malloc() is unable to obtain sufficient free
2576 ** memory, it returns a NULL pointer.  ^If the parameter N to
2577 ** sqlite3_malloc() is zero or negative then sqlite3_malloc() returns
2578 ** a NULL pointer.
2579 **
2580 ** ^Calling sqlite3_free() with a pointer previously returned
2581 ** by sqlite3_malloc() or sqlite3_realloc() releases that memory so
2582 ** that it might be reused.  ^The sqlite3_free() routine is
2583 ** a no-op if is called with a NULL pointer.  Passing a NULL pointer
2584 ** to sqlite3_free() is harmless.  After being freed, memory
2585 ** should neither be read nor written.  Even reading previously freed
2586 ** memory might result in a segmentation fault or other severe error.
2587 ** Memory corruption, a segmentation fault, or other severe error
2588 ** might result if sqlite3_free() is called with a non-NULL pointer that
2589 ** was not obtained from sqlite3_malloc() or sqlite3_realloc().
2590 **
2591 ** ^(The sqlite3_realloc() interface attempts to resize a
2592 ** prior memory allocation to be at least N bytes, where N is the
2593 ** second parameter.  The memory allocation to be resized is the first
2594 ** parameter.)^ ^ If the first parameter to sqlite3_realloc()
2595 ** is a NULL pointer then its behavior is identical to calling
2596 ** sqlite3_malloc(N) where N is the second parameter to sqlite3_realloc().
2597 ** ^If the second parameter to sqlite3_realloc() is zero or
2598 ** negative then the behavior is exactly the same as calling
2599 ** sqlite3_free(P) where P is the first parameter to sqlite3_realloc().
2600 ** ^sqlite3_realloc() returns a pointer to a memory allocation
2601 ** of at least N bytes in size or NULL if sufficient memory is unavailable.
2602 ** ^If M is the size of the prior allocation, then min(N,M) bytes
2603 ** of the prior allocation are copied into the beginning of buffer returned
2604 ** by sqlite3_realloc() and the prior allocation is freed.
2605 ** ^If sqlite3_realloc() returns NULL, then the prior allocation
2606 ** is not freed.
2607 **
2608 ** ^The memory returned by sqlite3_malloc() and sqlite3_realloc()
2609 ** is always aligned to at least an 8 byte boundary, or to a
2610 ** 4 byte boundary if the [SQLITE_4_BYTE_ALIGNED_MALLOC] compile-time
2611 ** option is used.
2612 **
2613 ** In SQLite version 3.5.0 and 3.5.1, it was possible to define
2614 ** the SQLITE_OMIT_MEMORY_ALLOCATION which would cause the built-in
2615 ** implementation of these routines to be omitted.  That capability
2616 ** is no longer provided.  Only built-in memory allocators can be used.
2617 **
2618 ** The Windows OS interface layer calls
2619 ** the system malloc() and free() directly when converting
2620 ** filenames between the UTF-8 encoding used by SQLite
2621 ** and whatever filename encoding is used by the particular Windows
2622 ** installation.  Memory allocation errors are detected, but
2623 ** they are reported back as [SQLITE_CANTOPEN] or
2624 ** [SQLITE_IOERR] rather than [SQLITE_NOMEM].
2625 **
2626 ** The pointer arguments to [sqlite3_free()] and [sqlite3_realloc()]
2627 ** must be either NULL or else pointers obtained from a prior
2628 ** invocation of [sqlite3_malloc()] or [sqlite3_realloc()] that have
2629 ** not yet been released.
2630 **
2631 ** The application must not read or write any part of
2632 ** a block of memory after it has been released using
2633 ** [sqlite3_free()] or [sqlite3_realloc()].
2634 */
2635 SQLITE_API void *sqlite3_malloc(int);
2636 SQLITE_API void *sqlite3_realloc(void*, int);
2637 SQLITE_API void sqlite3_free(void*);
2638 
2639 /*
2640 ** CAPI3REF: Memory Allocator Statistics
2641 **
2642 ** SQLite provides these two interfaces for reporting on the status
2643 ** of the [sqlite3_malloc()], [sqlite3_free()], and [sqlite3_realloc()]
2644 ** routines, which form the built-in memory allocation subsystem.
2645 **
2646 ** ^The [sqlite3_memory_used()] routine returns the number of bytes
2647 ** of memory currently outstanding (malloced but not freed).
2648 ** ^The [sqlite3_memory_highwater()] routine returns the maximum
2649 ** value of [sqlite3_memory_used()] since the high-water mark
2650 ** was last reset.  ^The values returned by [sqlite3_memory_used()] and
2651 ** [sqlite3_memory_highwater()] include any overhead
2652 ** added by SQLite in its implementation of [sqlite3_malloc()],
2653 ** but not overhead added by the any underlying system library
2654 ** routines that [sqlite3_malloc()] may call.
2655 **
2656 ** ^The memory high-water mark is reset to the current value of
2657 ** [sqlite3_memory_used()] if and only if the parameter to
2658 ** [sqlite3_memory_highwater()] is true.  ^The value returned
2659 ** by [sqlite3_memory_highwater(1)] is the high-water mark
2660 ** prior to the reset.
2661 */
2662 SQLITE_API sqlite3_int64 sqlite3_memory_used(void);
2663 SQLITE_API sqlite3_int64 sqlite3_memory_highwater(int resetFlag);
2664 
2665 /*
2666 ** CAPI3REF: Pseudo-Random Number Generator
2667 **
2668 ** SQLite contains a high-quality pseudo-random number generator (PRNG) used to
2669 ** select random [ROWID | ROWIDs] when inserting new records into a table that
2670 ** already uses the largest possible [ROWID].  The PRNG is also used for
2671 ** the build-in random() and randomblob() SQL functions.  This interface allows
2672 ** applications to access the same PRNG for other purposes.
2673 **
2674 ** ^A call to this routine stores N bytes of randomness into buffer P.
2675 **
2676 ** ^The first time this routine is invoked (either internally or by
2677 ** the application) the PRNG is seeded using randomness obtained
2678 ** from the xRandomness method of the default [sqlite3_vfs] object.
2679 ** ^On all subsequent invocations, the pseudo-randomness is generated
2680 ** internally and without recourse to the [sqlite3_vfs] xRandomness
2681 ** method.
2682 */
2683 SQLITE_API void sqlite3_randomness(int N, void *P);
2684 
2685 /*
2686 ** CAPI3REF: Compile-Time Authorization Callbacks
2687 **
2688 ** ^This routine registers an authorizer callback with a particular
2689 ** [database connection], supplied in the first argument.
2690 ** ^The authorizer callback is invoked as SQL statements are being compiled
2691 ** by [sqlite3_prepare()] or its variants [sqlite3_prepare_v2()],
2692 ** [sqlite3_prepare16()] and [sqlite3_prepare16_v2()].  ^At various
2693 ** points during the compilation process, as logic is being created
2694 ** to perform various actions, the authorizer callback is invoked to
2695 ** see if those actions are allowed.  ^The authorizer callback should
2696 ** return [SQLITE_OK] to allow the action, [SQLITE_IGNORE] to disallow the
2697 ** specific action but allow the SQL statement to continue to be
2698 ** compiled, or [SQLITE_DENY] to cause the entire SQL statement to be
2699 ** rejected with an error.  ^If the authorizer callback returns
2700 ** any value other than [SQLITE_IGNORE], [SQLITE_OK], or [SQLITE_DENY]
2701 ** then the [sqlite3_prepare_v2()] or equivalent call that triggered
2702 ** the authorizer will fail with an error message.
2703 **
2704 ** When the callback returns [SQLITE_OK], that means the operation
2705 ** requested is ok.  ^When the callback returns [SQLITE_DENY], the
2706 ** [sqlite3_prepare_v2()] or equivalent call that triggered the
2707 ** authorizer will fail with an error message explaining that
2708 ** access is denied.
2709 **
2710 ** ^The first parameter to the authorizer callback is a copy of the third
2711 ** parameter to the sqlite3_set_authorizer() interface. ^The second parameter
2712 ** to the callback is an integer [SQLITE_COPY | action code] that specifies
2713 ** the particular action to be authorized. ^The third through sixth parameters
2714 ** to the callback are zero-terminated strings that contain additional
2715 ** details about the action to be authorized.
2716 **
2717 ** ^If the action code is [SQLITE_READ]
2718 ** and the callback returns [SQLITE_IGNORE] then the
2719 ** [prepared statement] statement is constructed to substitute
2720 ** a NULL value in place of the table column that would have
2721 ** been read if [SQLITE_OK] had been returned.  The [SQLITE_IGNORE]
2722 ** return can be used to deny an untrusted user access to individual
2723 ** columns of a table.
2724 ** ^If the action code is [SQLITE_DELETE] and the callback returns
2725 ** [SQLITE_IGNORE] then the [DELETE] operation proceeds but the
2726 ** [truncate optimization] is disabled and all rows are deleted individually.
2727 **
2728 ** An authorizer is used when [sqlite3_prepare | preparing]
2729 ** SQL statements from an untrusted source, to ensure that the SQL statements
2730 ** do not try to access data they are not allowed to see, or that they do not
2731 ** try to execute malicious statements that damage the database.  For
2732 ** example, an application may allow a user to enter arbitrary
2733 ** SQL queries for evaluation by a database.  But the application does
2734 ** not want the user to be able to make arbitrary changes to the
2735 ** database.  An authorizer could then be put in place while the
2736 ** user-entered SQL is being [sqlite3_prepare | prepared] that
2737 ** disallows everything except [SELECT] statements.
2738 **
2739 ** Applications that need to process SQL from untrusted sources
2740 ** might also consider lowering resource limits using [sqlite3_limit()]
2741 ** and limiting database size using the [max_page_count] [PRAGMA]
2742 ** in addition to using an authorizer.
2743 **
2744 ** ^(Only a single authorizer can be in place on a database connection
2745 ** at a time.  Each call to sqlite3_set_authorizer overrides the
2746 ** previous call.)^  ^Disable the authorizer by installing a NULL callback.
2747 ** The authorizer is disabled by default.
2748 **
2749 ** The authorizer callback must not do anything that will modify
2750 ** the database connection that invoked the authorizer callback.
2751 ** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their
2752 ** database connections for the meaning of "modify" in this paragraph.
2753 **
2754 ** ^When [sqlite3_prepare_v2()] is used to prepare a statement, the
2755 ** statement might be re-prepared during [sqlite3_step()] due to a
2756 ** schema change.  Hence, the application should ensure that the
2757 ** correct authorizer callback remains in place during the [sqlite3_step()].
2758 **
2759 ** ^Note that the authorizer callback is invoked only during
2760 ** [sqlite3_prepare()] or its variants.  Authorization is not
2761 ** performed during statement evaluation in [sqlite3_step()], unless
2762 ** as stated in the previous paragraph, sqlite3_step() invokes
2763 ** sqlite3_prepare_v2() to reprepare a statement after a schema change.
2764 */
2765 SQLITE_API int sqlite3_set_authorizer(
2766   sqlite3*,
2767   int (*xAuth)(void*,int,const char*,const char*,const char*,const char*),
2768   void *pUserData
2769 );
2770 
2771 /*
2772 ** CAPI3REF: Authorizer Return Codes
2773 **
2774 ** The [sqlite3_set_authorizer | authorizer callback function] must
2775 ** return either [SQLITE_OK] or one of these two constants in order
2776 ** to signal SQLite whether or not the action is permitted.  See the
2777 ** [sqlite3_set_authorizer | authorizer documentation] for additional
2778 ** information.
2779 **
2780 ** Note that SQLITE_IGNORE is also used as a [SQLITE_ROLLBACK | return code]
2781 ** from the [sqlite3_vtab_on_conflict()] interface.
2782 */
2783 #define SQLITE_DENY   1   /* Abort the SQL statement with an error */
2784 #define SQLITE_IGNORE 2   /* Don't allow access, but don't generate an error */
2785 
2786 /*
2787 ** CAPI3REF: Authorizer Action Codes
2788 **
2789 ** The [sqlite3_set_authorizer()] interface registers a callback function
2790 ** that is invoked to authorize certain SQL statement actions.  The
2791 ** second parameter to the callback is an integer code that specifies
2792 ** what action is being authorized.  These are the integer action codes that
2793 ** the authorizer callback may be passed.
2794 **
2795 ** These action code values signify what kind of operation is to be
2796 ** authorized.  The 3rd and 4th parameters to the authorization
2797 ** callback function will be parameters or NULL depending on which of these
2798 ** codes is used as the second parameter.  ^(The 5th parameter to the
2799 ** authorizer callback is the name of the database ("main", "temp",
2800 ** etc.) if applicable.)^  ^The 6th parameter to the authorizer callback
2801 ** is the name of the inner-most trigger or view that is responsible for
2802 ** the access attempt or NULL if this access attempt is directly from
2803 ** top-level SQL code.
2804 */
2805 /******************************************* 3rd ************ 4th ***********/
2806 #define SQLITE_CREATE_INDEX          1   /* Index Name      Table Name      */
2807 #define SQLITE_CREATE_TABLE          2   /* Table Name      NULL            */
2808 #define SQLITE_CREATE_TEMP_INDEX     3   /* Index Name      Table Name      */
2809 #define SQLITE_CREATE_TEMP_TABLE     4   /* Table Name      NULL            */
2810 #define SQLITE_CREATE_TEMP_TRIGGER   5   /* Trigger Name    Table Name      */
2811 #define SQLITE_CREATE_TEMP_VIEW      6   /* View Name       NULL            */
2812 #define SQLITE_CREATE_TRIGGER        7   /* Trigger Name    Table Name      */
2813 #define SQLITE_CREATE_VIEW           8   /* View Name       NULL            */
2814 #define SQLITE_DELETE                9   /* Table Name      NULL            */
2815 #define SQLITE_DROP_INDEX           10   /* Index Name      Table Name      */
2816 #define SQLITE_DROP_TABLE           11   /* Table Name      NULL            */
2817 #define SQLITE_DROP_TEMP_INDEX      12   /* Index Name      Table Name      */
2818 #define SQLITE_DROP_TEMP_TABLE      13   /* Table Name      NULL            */
2819 #define SQLITE_DROP_TEMP_TRIGGER    14   /* Trigger Name    Table Name      */
2820 #define SQLITE_DROP_TEMP_VIEW       15   /* View Name       NULL            */
2821 #define SQLITE_DROP_TRIGGER         16   /* Trigger Name    Table Name      */
2822 #define SQLITE_DROP_VIEW            17   /* View Name       NULL            */
2823 #define SQLITE_INSERT               18   /* Table Name      NULL            */
2824 #define SQLITE_PRAGMA               19   /* Pragma Name     1st arg or NULL */
2825 #define SQLITE_READ                 20   /* Table Name      Column Name     */
2826 #define SQLITE_SELECT               21   /* NULL            NULL            */
2827 #define SQLITE_TRANSACTION          22   /* Operation       NULL            */
2828 #define SQLITE_UPDATE               23   /* Table Name      Column Name     */
2829 #define SQLITE_ATTACH               24   /* Filename        NULL            */
2830 #define SQLITE_DETACH               25   /* Database Name   NULL            */
2831 #define SQLITE_ALTER_TABLE          26   /* Database Name   Table Name      */
2832 #define SQLITE_REINDEX              27   /* Index Name      NULL            */
2833 #define SQLITE_ANALYZE              28   /* Table Name      NULL            */
2834 #define SQLITE_CREATE_VTABLE        29   /* Table Name      Module Name     */
2835 #define SQLITE_DROP_VTABLE          30   /* Table Name      Module Name     */
2836 #define SQLITE_FUNCTION             31   /* NULL            Function Name   */
2837 #define SQLITE_SAVEPOINT            32   /* Operation       Savepoint Name  */
2838 #define SQLITE_COPY                  0   /* No longer used */
2839 
2840 /*
2841 ** CAPI3REF: Tracing And Profiling Functions
2842 **
2843 ** These routines register callback functions that can be used for
2844 ** tracing and profiling the execution of SQL statements.
2845 **
2846 ** ^The callback function registered by sqlite3_trace() is invoked at
2847 ** various times when an SQL statement is being run by [sqlite3_step()].
2848 ** ^The sqlite3_trace() callback is invoked with a UTF-8 rendering of the
2849 ** SQL statement text as the statement first begins executing.
2850 ** ^(Additional sqlite3_trace() callbacks might occur
2851 ** as each triggered subprogram is entered.  The callbacks for triggers
2852 ** contain a UTF-8 SQL comment that identifies the trigger.)^
2853 **
2854 ** ^The callback function registered by sqlite3_profile() is invoked
2855 ** as each SQL statement finishes.  ^The profile callback contains
2856 ** the original statement text and an estimate of wall-clock time
2857 ** of how long that statement took to run.  ^The profile callback
2858 ** time is in units of nanoseconds, however the current implementation
2859 ** is only capable of millisecond resolution so the six least significant
2860 ** digits in the time are meaningless.  Future versions of SQLite
2861 ** might provide greater resolution on the profiler callback.  The
2862 ** sqlite3_profile() function is considered experimental and is
2863 ** subject to change in future versions of SQLite.
2864 */
2865 SQLITE_API void *sqlite3_trace(sqlite3*, void(*xTrace)(void*,const char*), void*);
2866 SQLITE_API SQLITE_EXPERIMENTAL void *sqlite3_profile(sqlite3*,
2867    void(*xProfile)(void*,const char*,sqlite3_uint64), void*);
2868 
2869 /*
2870 ** CAPI3REF: Query Progress Callbacks
2871 **
2872 ** ^The sqlite3_progress_handler(D,N,X,P) interface causes the callback
2873 ** function X to be invoked periodically during long running calls to
2874 ** [sqlite3_exec()], [sqlite3_step()] and [sqlite3_get_table()] for
2875 ** database connection D.  An example use for this
2876 ** interface is to keep a GUI updated during a large query.
2877 **
2878 ** ^The parameter P is passed through as the only parameter to the
2879 ** callback function X.  ^The parameter N is the number of
2880 ** [virtual machine instructions] that are evaluated between successive
2881 ** invocations of the callback X.
2882 **
2883 ** ^Only a single progress handler may be defined at one time per
2884 ** [database connection]; setting a new progress handler cancels the
2885 ** old one.  ^Setting parameter X to NULL disables the progress handler.
2886 ** ^The progress handler is also disabled by setting N to a value less
2887 ** than 1.
2888 **
2889 ** ^If the progress callback returns non-zero, the operation is
2890 ** interrupted.  This feature can be used to implement a
2891 ** "Cancel" button on a GUI progress dialog box.
2892 **
2893 ** The progress handler callback must not do anything that will modify
2894 ** the database connection that invoked the progress handler.
2895 ** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their
2896 ** database connections for the meaning of "modify" in this paragraph.
2897 **
2898 */
2899 SQLITE_API void sqlite3_progress_handler(sqlite3*, int, int(*)(void*), void*);
2900 
2901 /*
2902 ** CAPI3REF: Opening A New Database Connection
2903 **
2904 ** ^These routines open an SQLite database file as specified by the
2905 ** filename argument. ^The filename argument is interpreted as UTF-8 for
2906 ** sqlite3_open() and sqlite3_open_v2() and as UTF-16 in the native byte
2907 ** order for sqlite3_open16(). ^(A [database connection] handle is usually
2908 ** returned in *ppDb, even if an error occurs.  The only exception is that
2909 ** if SQLite is unable to allocate memory to hold the [sqlite3] object,
2910 ** a NULL will be written into *ppDb instead of a pointer to the [sqlite3]
2911 ** object.)^ ^(If the database is opened (and/or created) successfully, then
2912 ** [SQLITE_OK] is returned.  Otherwise an [error code] is returned.)^ ^The
2913 ** [sqlite3_errmsg()] or [sqlite3_errmsg16()] routines can be used to obtain
2914 ** an English language description of the error following a failure of any
2915 ** of the sqlite3_open() routines.
2916 **
2917 ** ^The default encoding for the database will be UTF-8 if
2918 ** sqlite3_open() or sqlite3_open_v2() is called and
2919 ** UTF-16 in the native byte order if sqlite3_open16() is used.
2920 **
2921 ** Whether or not an error occurs when it is opened, resources
2922 ** associated with the [database connection] handle should be released by
2923 ** passing it to [sqlite3_close()] when it is no longer required.
2924 **
2925 ** The sqlite3_open_v2() interface works like sqlite3_open()
2926 ** except that it accepts two additional parameters for additional control
2927 ** over the new database connection.  ^(The flags parameter to
2928 ** sqlite3_open_v2() can take one of
2929 ** the following three values, optionally combined with the
2930 ** [SQLITE_OPEN_NOMUTEX], [SQLITE_OPEN_FULLMUTEX], [SQLITE_OPEN_SHAREDCACHE],
2931 ** [SQLITE_OPEN_PRIVATECACHE], and/or [SQLITE_OPEN_URI] flags:)^
2932 **
2933 ** <dl>
2934 ** ^(<dt>[SQLITE_OPEN_READONLY]</dt>
2935 ** <dd>The database is opened in read-only mode.  If the database does not
2936 ** already exist, an error is returned.</dd>)^
2937 **
2938 ** ^(<dt>[SQLITE_OPEN_READWRITE]</dt>
2939 ** <dd>The database is opened for reading and writing if possible, or reading
2940 ** only if the file is write protected by the operating system.  In either
2941 ** case the database must already exist, otherwise an error is returned.</dd>)^
2942 **
2943 ** ^(<dt>[SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE]</dt>
2944 ** <dd>The database is opened for reading and writing, and is created if
2945 ** it does not already exist. This is the behavior that is always used for
2946 ** sqlite3_open() and sqlite3_open16().</dd>)^
2947 ** </dl>
2948 **
2949 ** If the 3rd parameter to sqlite3_open_v2() is not one of the
2950 ** combinations shown above optionally combined with other
2951 ** [SQLITE_OPEN_READONLY | SQLITE_OPEN_* bits]
2952 ** then the behavior is undefined.
2953 **
2954 ** ^If the [SQLITE_OPEN_NOMUTEX] flag is set, then the database connection
2955 ** opens in the multi-thread [threading mode] as long as the single-thread
2956 ** mode has not been set at compile-time or start-time.  ^If the
2957 ** [SQLITE_OPEN_FULLMUTEX] flag is set then the database connection opens
2958 ** in the serialized [threading mode] unless single-thread was
2959 ** previously selected at compile-time or start-time.
2960 ** ^The [SQLITE_OPEN_SHAREDCACHE] flag causes the database connection to be
2961 ** eligible to use [shared cache mode], regardless of whether or not shared
2962 ** cache is enabled using [sqlite3_enable_shared_cache()].  ^The
2963 ** [SQLITE_OPEN_PRIVATECACHE] flag causes the database connection to not
2964 ** participate in [shared cache mode] even if it is enabled.
2965 **
2966 ** ^The fourth parameter to sqlite3_open_v2() is the name of the
2967 ** [sqlite3_vfs] object that defines the operating system interface that
2968 ** the new database connection should use.  ^If the fourth parameter is
2969 ** a NULL pointer then the default [sqlite3_vfs] object is used.
2970 **
2971 ** ^If the filename is ":memory:", then a private, temporary in-memory database
2972 ** is created for the connection.  ^This in-memory database will vanish when
2973 ** the database connection is closed.  Future versions of SQLite might
2974 ** make use of additional special filenames that begin with the ":" character.
2975 ** It is recommended that when a database filename actually does begin with
2976 ** a ":" character you should prefix the filename with a pathname such as
2977 ** "./" to avoid ambiguity.
2978 **
2979 ** ^If the filename is an empty string, then a private, temporary
2980 ** on-disk database will be created.  ^This private database will be
2981 ** automatically deleted as soon as the database connection is closed.
2982 **
2983 ** [[URI filenames in sqlite3_open()]] <h3>URI Filenames</h3>
2984 **
2985 ** ^If [URI filename] interpretation is enabled, and the filename argument
2986 ** begins with "file:", then the filename is interpreted as a URI. ^URI
2987 ** filename interpretation is enabled if the [SQLITE_OPEN_URI] flag is
2988 ** set in the fourth argument to sqlite3_open_v2(), or if it has
2989 ** been enabled globally using the [SQLITE_CONFIG_URI] option with the
2990 ** [sqlite3_config()] method or by the [SQLITE_USE_URI] compile-time option.
2991 ** As of SQLite version 3.7.7, URI filename interpretation is turned off
2992 ** by default, but future releases of SQLite might enable URI filename
2993 ** interpretation by default.  See "[URI filenames]" for additional
2994 ** information.
2995 **
2996 ** URI filenames are parsed according to RFC 3986. ^If the URI contains an
2997 ** authority, then it must be either an empty string or the string
2998 ** "localhost". ^If the authority is not an empty string or "localhost", an
2999 ** error is returned to the caller. ^The fragment component of a URI, if
3000 ** present, is ignored.
3001 **
3002 ** ^SQLite uses the path component of the URI as the name of the disk file
3003 ** which contains the database. ^If the path begins with a '/' character,
3004 ** then it is interpreted as an absolute path. ^If the path does not begin
3005 ** with a '/' (meaning that the authority section is omitted from the URI)
3006 ** then the path is interpreted as a relative path.
3007 ** ^On windows, the first component of an absolute path
3008 ** is a drive specification (e.g. "C:").
3009 **
3010 ** [[core URI query parameters]]
3011 ** The query component of a URI may contain parameters that are interpreted
3012 ** either by SQLite itself, or by a [VFS | custom VFS implementation].
3013 ** SQLite interprets the following three query parameters:
3014 **
3015 ** <ul>
3016 **   <li> <b>vfs</b>: ^The "vfs" parameter may be used to specify the name of
3017 **     a VFS object that provides the operating system interface that should
3018 **     be used to access the database file on disk. ^If this option is set to
3019 **     an empty string the default VFS object is used. ^Specifying an unknown
3020 **     VFS is an error. ^If sqlite3_open_v2() is used and the vfs option is
3021 **     present, then the VFS specified by the option takes precedence over
3022 **     the value passed as the fourth parameter to sqlite3_open_v2().
3023 **
3024 **   <li> <b>mode</b>: ^(The mode parameter may be set to either "ro", "rw" or
3025 **     "rwc". Attempting to set it to any other value is an error)^.
3026 **     ^If "ro" is specified, then the database is opened for read-only
3027 **     access, just as if the [SQLITE_OPEN_READONLY] flag had been set in the
3028 **     third argument to sqlite3_prepare_v2(). ^If the mode option is set to
3029 **     "rw", then the database is opened for read-write (but not create)
3030 **     access, as if SQLITE_OPEN_READWRITE (but not SQLITE_OPEN_CREATE) had
3031 **     been set. ^Value "rwc" is equivalent to setting both
3032 **     SQLITE_OPEN_READWRITE and SQLITE_OPEN_CREATE. ^If sqlite3_open_v2() is
3033 **     used, it is an error to specify a value for the mode parameter that is
3034 **     less restrictive than that specified by the flags passed as the third
3035 **     parameter.
3036 **
3037 **   <li> <b>cache</b>: ^The cache parameter may be set to either "shared" or
3038 **     "private". ^Setting it to "shared" is equivalent to setting the
3039 **     SQLITE_OPEN_SHAREDCACHE bit in the flags argument passed to
3040 **     sqlite3_open_v2(). ^Setting the cache parameter to "private" is
3041 **     equivalent to setting the SQLITE_OPEN_PRIVATECACHE bit.
3042 **     ^If sqlite3_open_v2() is used and the "cache" parameter is present in
3043 **     a URI filename, its value overrides any behaviour requested by setting
3044 **     SQLITE_OPEN_PRIVATECACHE or SQLITE_OPEN_SHAREDCACHE flag.
3045 ** </ul>
3046 **
3047 ** ^Specifying an unknown parameter in the query component of a URI is not an
3048 ** error.  Future versions of SQLite might understand additional query
3049 ** parameters.  See "[query parameters with special meaning to SQLite]" for
3050 ** additional information.
3051 **
3052 ** [[URI filename examples]] <h3>URI filename examples</h3>
3053 **
3054 ** <table border="1" align=center cellpadding=5>
3055 ** <tr><th> URI filenames <th> Results
3056 ** <tr><td> file:data.db <td>
3057 **          Open the file "data.db" in the current directory.
3058 ** <tr><td> file:/home/fred/data.db<br>
3059 **          file:///home/fred/data.db <br>
3060 **          file://localhost/home/fred/data.db <br> <td>
3061 **          Open the database file "/home/fred/data.db".
3062 ** <tr><td> file://darkstar/home/fred/data.db <td>
3063 **          An error. "darkstar" is not a recognized authority.
3064 ** <tr><td style="white-space:nowrap">
3065 **          file:///C:/Documents%20and%20Settings/fred/Desktop/data.db
3066 **     <td> Windows only: Open the file "data.db" on fred's desktop on drive
3067 **          C:. Note that the %20 escaping in this example is not strictly
3068 **          necessary - space characters can be used literally
3069 **          in URI filenames.
3070 ** <tr><td> file:data.db?mode=ro&cache=private <td>
3071 **          Open file "data.db" in the current directory for read-only access.
3072 **          Regardless of whether or not shared-cache mode is enabled by
3073 **          default, use a private cache.
3074 ** <tr><td> file:/home/fred/data.db?vfs=unix-nolock <td>
3075 **          Open file "/home/fred/data.db". Use the special VFS "unix-nolock".
3076 ** <tr><td> file:data.db?mode=readonly <td>
3077 **          An error. "readonly" is not a valid option for the "mode" parameter.
3078 ** </table>
3079 **
3080 ** ^URI hexadecimal escape sequences (%HH) are supported within the path and
3081 ** query components of a URI. A hexadecimal escape sequence consists of a
3082 ** percent sign - "%" - followed by exactly two hexadecimal digits
3083 ** specifying an octet value. ^Before the path or query components of a
3084 ** URI filename are interpreted, they are encoded using UTF-8 and all
3085 ** hexadecimal escape sequences replaced by a single byte containing the
3086 ** corresponding octet. If this process generates an invalid UTF-8 encoding,
3087 ** the results are undefined.
3088 **
3089 ** <b>Note to Windows users:</b>  The encoding used for the filename argument
3090 ** of sqlite3_open() and sqlite3_open_v2() must be UTF-8, not whatever
3091 ** codepage is currently defined.  Filenames containing international
3092 ** characters must be converted to UTF-8 prior to passing them into
3093 ** sqlite3_open() or sqlite3_open_v2().
3094 */
3095 SQLITE_API int sqlite3_open(
3096   const char *filename,   /* Database filename (UTF-8) */
3097   sqlite3 **ppDb          /* OUT: SQLite db handle */
3098 );
3099 SQLITE_API int sqlite3_open16(
3100   const void *filename,   /* Database filename (UTF-16) */
3101   sqlite3 **ppDb          /* OUT: SQLite db handle */
3102 );
3103 SQLITE_API int sqlite3_open_v2(
3104   const char *filename,   /* Database filename (UTF-8) */
3105   sqlite3 **ppDb,         /* OUT: SQLite db handle */
3106   int flags,              /* Flags */
3107   const char *zVfs        /* Name of VFS module to use */
3108 );
3109 
3110 /*
3111 ** CAPI3REF: Obtain Values For URI Parameters
3112 **
3113 ** This is a utility routine, useful to VFS implementations, that checks
3114 ** to see if a database file was a URI that contained a specific query
3115 ** parameter, and if so obtains the value of the query parameter.
3116 **
3117 ** The zFilename argument is the filename pointer passed into the xOpen()
3118 ** method of a VFS implementation.  The zParam argument is the name of the
3119 ** query parameter we seek.  This routine returns the value of the zParam
3120 ** parameter if it exists.  If the parameter does not exist, this routine
3121 ** returns a NULL pointer.
3122 **
3123 ** If the zFilename argument to this function is not a pointer that SQLite
3124 ** passed into the xOpen VFS method, then the behavior of this routine
3125 ** is undefined and probably undesirable.
3126 */
3127 SQLITE_API const char *sqlite3_uri_parameter(const char *zFilename, const char *zParam);
3128 
3129 
3130 /*
3131 ** CAPI3REF: Error Codes And Messages
3132 **
3133 ** ^The sqlite3_errcode() interface returns the numeric [result code] or
3134 ** [extended result code] for the most recent failed sqlite3_* API call
3135 ** associated with a [database connection]. If a prior API call failed
3136 ** but the most recent API call succeeded, the return value from
3137 ** sqlite3_errcode() is undefined.  ^The sqlite3_extended_errcode()
3138 ** interface is the same except that it always returns the
3139 ** [extended result code] even when extended result codes are
3140 ** disabled.
3141 **
3142 ** ^The sqlite3_errmsg() and sqlite3_errmsg16() return English-language
3143 ** text that describes the error, as either UTF-8 or UTF-16 respectively.
3144 ** ^(Memory to hold the error message string is managed internally.
3145 ** The application does not need to worry about freeing the result.
3146 ** However, the error string might be overwritten or deallocated by
3147 ** subsequent calls to other SQLite interface functions.)^
3148 **
3149 ** When the serialized [threading mode] is in use, it might be the
3150 ** case that a second error occurs on a separate thread in between
3151 ** the time of the first error and the call to these interfaces.
3152 ** When that happens, the second error will be reported since these
3153 ** interfaces always report the most recent result.  To avoid
3154 ** this, each thread can obtain exclusive use of the [database connection] D
3155 ** by invoking [sqlite3_mutex_enter]([sqlite3_db_mutex](D)) before beginning
3156 ** to use D and invoking [sqlite3_mutex_leave]([sqlite3_db_mutex](D)) after
3157 ** all calls to the interfaces listed here are completed.
3158 **
3159 ** If an interface fails with SQLITE_MISUSE, that means the interface
3160 ** was invoked incorrectly by the application.  In that case, the
3161 ** error code and message may or may not be set.
3162 */
3163 SQLITE_API int sqlite3_errcode(sqlite3 *db);
3164 SQLITE_API int sqlite3_extended_errcode(sqlite3 *db);
3165 SQLITE_API const char *sqlite3_errmsg(sqlite3*);
3166 SQLITE_API const void *sqlite3_errmsg16(sqlite3*);
3167 
3168 /*
3169 ** CAPI3REF: SQL Statement Object
3170 ** KEYWORDS: {prepared statement} {prepared statements}
3171 **
3172 ** An instance of this object represents a single SQL statement.
3173 ** This object is variously known as a "prepared statement" or a
3174 ** "compiled SQL statement" or simply as a "statement".
3175 **
3176 ** The life of a statement object goes something like this:
3177 **
3178 ** <ol>
3179 ** <li> Create the object using [sqlite3_prepare_v2()] or a related
3180 **      function.
3181 ** <li> Bind values to [host parameters] using the sqlite3_bind_*()
3182 **      interfaces.
3183 ** <li> Run the SQL by calling [sqlite3_step()] one or more times.
3184 ** <li> Reset the statement using [sqlite3_reset()] then go back
3185 **      to step 2.  Do this zero or more times.
3186 ** <li> Destroy the object using [sqlite3_finalize()].
3187 ** </ol>
3188 **
3189 ** Refer to documentation on individual methods above for additional
3190 ** information.
3191 */
3192 typedef struct sqlite3_stmt sqlite3_stmt;
3193 
3194 /*
3195 ** CAPI3REF: Run-time Limits
3196 **
3197 ** ^(This interface allows the size of various constructs to be limited
3198 ** on a connection by connection basis.  The first parameter is the
3199 ** [database connection] whose limit is to be set or queried.  The
3200 ** second parameter is one of the [limit categories] that define a
3201 ** class of constructs to be size limited.  The third parameter is the
3202 ** new limit for that construct.)^
3203 **
3204 ** ^If the new limit is a negative number, the limit is unchanged.
3205 ** ^(For each limit category SQLITE_LIMIT_<i>NAME</i> there is a
3206 ** [limits | hard upper bound]
3207 ** set at compile-time by a C preprocessor macro called
3208 ** [limits | SQLITE_MAX_<i>NAME</i>].
3209 ** (The "_LIMIT_" in the name is changed to "_MAX_".))^
3210 ** ^Attempts to increase a limit above its hard upper bound are
3211 ** silently truncated to the hard upper bound.
3212 **
3213 ** ^Regardless of whether or not the limit was changed, the
3214 ** [sqlite3_limit()] interface returns the prior value of the limit.
3215 ** ^Hence, to find the current value of a limit without changing it,
3216 ** simply invoke this interface with the third parameter set to -1.
3217 **
3218 ** Run-time limits are intended for use in applications that manage
3219 ** both their own internal database and also databases that are controlled
3220 ** by untrusted external sources.  An example application might be a
3221 ** web browser that has its own databases for storing history and
3222 ** separate databases controlled by JavaScript applications downloaded
3223 ** off the Internet.  The internal databases can be given the
3224 ** large, default limits.  Databases managed by external sources can
3225 ** be given much smaller limits designed to prevent a denial of service
3226 ** attack.  Developers might also want to use the [sqlite3_set_authorizer()]
3227 ** interface to further control untrusted SQL.  The size of the database
3228 ** created by an untrusted script can be contained using the
3229 ** [max_page_count] [PRAGMA].
3230 **
3231 ** New run-time limit categories may be added in future releases.
3232 */
3233 SQLITE_API int sqlite3_limit(sqlite3*, int id, int newVal);
3234 
3235 /*
3236 ** CAPI3REF: Run-Time Limit Categories
3237 ** KEYWORDS: {limit category} {*limit categories}
3238 **
3239 ** These constants define various performance limits
3240 ** that can be lowered at run-time using [sqlite3_limit()].
3241 ** The synopsis of the meanings of the various limits is shown below.
3242 ** Additional information is available at [limits | Limits in SQLite].
3243 **
3244 ** <dl>
3245 ** [[SQLITE_LIMIT_LENGTH]] ^(<dt>SQLITE_LIMIT_LENGTH</dt>
3246 ** <dd>The maximum size of any string or BLOB or table row, in bytes.<dd>)^
3247 **
3248 ** [[SQLITE_LIMIT_SQL_LENGTH]] ^(<dt>SQLITE_LIMIT_SQL_LENGTH</dt>
3249 ** <dd>The maximum length of an SQL statement, in bytes.</dd>)^
3250 **
3251 ** [[SQLITE_LIMIT_COLUMN]] ^(<dt>SQLITE_LIMIT_COLUMN</dt>
3252 ** <dd>The maximum number of columns in a table definition or in the
3253 ** result set of a [SELECT] or the maximum number of columns in an index
3254 ** or in an ORDER BY or GROUP BY clause.</dd>)^
3255 **
3256 ** [[SQLITE_LIMIT_EXPR_DEPTH]] ^(<dt>SQLITE_LIMIT_EXPR_DEPTH</dt>
3257 ** <dd>The maximum depth of the parse tree on any expression.</dd>)^
3258 **
3259 ** [[SQLITE_LIMIT_COMPOUND_SELECT]] ^(<dt>SQLITE_LIMIT_COMPOUND_SELECT</dt>
3260 ** <dd>The maximum number of terms in a compound SELECT statement.</dd>)^
3261 **
3262 ** [[SQLITE_LIMIT_VDBE_OP]] ^(<dt>SQLITE_LIMIT_VDBE_OP</dt>
3263 ** <dd>The maximum number of instructions in a virtual machine program
3264 ** used to implement an SQL statement.  This limit is not currently
3265 ** enforced, though that might be added in some future release of
3266 ** SQLite.</dd>)^
3267 **
3268 ** [[SQLITE_LIMIT_FUNCTION_ARG]] ^(<dt>SQLITE_LIMIT_FUNCTION_ARG</dt>
3269 ** <dd>The maximum number of arguments on a function.</dd>)^
3270 **
3271 ** [[SQLITE_LIMIT_ATTACHED]] ^(<dt>SQLITE_LIMIT_ATTACHED</dt>
3272 ** <dd>The maximum number of [ATTACH | attached databases].)^</dd>
3273 **
3274 ** [[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]]
3275 ** ^(<dt>SQLITE_LIMIT_LIKE_PATTERN_LENGTH</dt>
3276 ** <dd>The maximum length of the pattern argument to the [LIKE] or
3277 ** [GLOB] operators.</dd>)^
3278 **
3279 ** [[SQLITE_LIMIT_VARIABLE_NUMBER]]
3280 ** ^(<dt>SQLITE_LIMIT_VARIABLE_NUMBER</dt>
3281 ** <dd>The maximum index number of any [parameter] in an SQL statement.)^
3282 **
3283 ** [[SQLITE_LIMIT_TRIGGER_DEPTH]] ^(<dt>SQLITE_LIMIT_TRIGGER_DEPTH</dt>
3284 ** <dd>The maximum depth of recursion for triggers.</dd>)^
3285 ** </dl>
3286 */
3287 #define SQLITE_LIMIT_LENGTH                    0
3288 #define SQLITE_LIMIT_SQL_LENGTH                1
3289 #define SQLITE_LIMIT_COLUMN                    2
3290 #define SQLITE_LIMIT_EXPR_DEPTH                3
3291 #define SQLITE_LIMIT_COMPOUND_SELECT           4
3292 #define SQLITE_LIMIT_VDBE_OP                   5
3293 #define SQLITE_LIMIT_FUNCTION_ARG              6
3294 #define SQLITE_LIMIT_ATTACHED                  7
3295 #define SQLITE_LIMIT_LIKE_PATTERN_LENGTH       8
3296 #define SQLITE_LIMIT_VARIABLE_NUMBER           9
3297 #define SQLITE_LIMIT_TRIGGER_DEPTH            10
3298 
3299 /*
3300 ** CAPI3REF: Compiling An SQL Statement
3301 ** KEYWORDS: {SQL statement compiler}
3302 **
3303 ** To execute an SQL query, it must first be compiled into a byte-code
3304 ** program using one of these routines.
3305 **
3306 ** The first argument, "db", is a [database connection] obtained from a
3307 ** prior successful call to [sqlite3_open()], [sqlite3_open_v2()] or
3308 ** [sqlite3_open16()].  The database connection must not have been closed.
3309 **
3310 ** The second argument, "zSql", is the statement to be compiled, encoded
3311 ** as either UTF-8 or UTF-16.  The sqlite3_prepare() and sqlite3_prepare_v2()
3312 ** interfaces use UTF-8, and sqlite3_prepare16() and sqlite3_prepare16_v2()
3313 ** use UTF-16.
3314 **
3315 ** ^If the nByte argument is less than zero, then zSql is read up to the
3316 ** first zero terminator. ^If nByte is non-negative, then it is the maximum
3317 ** number of  bytes read from zSql.  ^When nByte is non-negative, the
3318 ** zSql string ends at either the first '\000' or '\u0000' character or
3319 ** the nByte-th byte, whichever comes first. If the caller knows
3320 ** that the supplied string is nul-terminated, then there is a small
3321 ** performance advantage to be gained by passing an nByte parameter that
3322 ** is equal to the number of bytes in the input string <i>including</i>
3323 ** the nul-terminator bytes.
3324 **
3325 ** ^If pzTail is not NULL then *pzTail is made to point to the first byte
3326 ** past the end of the first SQL statement in zSql.  These routines only
3327 ** compile the first statement in zSql, so *pzTail is left pointing to
3328 ** what remains uncompiled.
3329 **
3330 ** ^*ppStmt is left pointing to a compiled [prepared statement] that can be
3331 ** executed using [sqlite3_step()].  ^If there is an error, *ppStmt is set
3332 ** to NULL.  ^If the input text contains no SQL (if the input is an empty
3333 ** string or a comment) then *ppStmt is set to NULL.
3334 ** The calling procedure is responsible for deleting the compiled
3335 ** SQL statement using [sqlite3_finalize()] after it has finished with it.
3336 ** ppStmt may not be NULL.
3337 **
3338 ** ^On success, the sqlite3_prepare() family of routines return [SQLITE_OK];
3339 ** otherwise an [error code] is returned.
3340 **
3341 ** The sqlite3_prepare_v2() and sqlite3_prepare16_v2() interfaces are
3342 ** recommended for all new programs. The two older interfaces are retained
3343 ** for backwards compatibility, but their use is discouraged.
3344 ** ^In the "v2" interfaces, the prepared statement
3345 ** that is returned (the [sqlite3_stmt] object) contains a copy of the
3346 ** original SQL text. This causes the [sqlite3_step()] interface to
3347 ** behave differently in three ways:
3348 **
3349 ** <ol>
3350 ** <li>
3351 ** ^If the database schema changes, instead of returning [SQLITE_SCHEMA] as it
3352 ** always used to do, [sqlite3_step()] will automatically recompile the SQL
3353 ** statement and try to run it again.
3354 ** </li>
3355 **
3356 ** <li>
3357 ** ^When an error occurs, [sqlite3_step()] will return one of the detailed
3358 ** [error codes] or [extended error codes].  ^The legacy behavior was that
3359 ** [sqlite3_step()] would only return a generic [SQLITE_ERROR] result code
3360 ** and the application would have to make a second call to [sqlite3_reset()]
3361 ** in order to find the underlying cause of the problem. With the "v2" prepare
3362 ** interfaces, the underlying reason for the error is returned immediately.
3363 ** </li>
3364 **
3365 ** <li>
3366 ** ^If the specific value bound to [parameter | host parameter] in the
3367 ** WHERE clause might influence the choice of query plan for a statement,
3368 ** then the statement will be automatically recompiled, as if there had been
3369 ** a schema change, on the first  [sqlite3_step()] call following any change
3370 ** to the [sqlite3_bind_text | bindings] of that [parameter].
3371 ** ^The specific value of WHERE-clause [parameter] might influence the
3372 ** choice of query plan if the parameter is the left-hand side of a [LIKE]
3373 ** or [GLOB] operator or if the parameter is compared to an indexed column
3374 ** and the [SQLITE_ENABLE_STAT2] compile-time option is enabled.
3375 ** the
3376 ** </li>
3377 ** </ol>
3378 */
3379 SQLITE_API int sqlite3_prepare(
3380   sqlite3 *db,            /* Database handle */
3381   const char *zSql,       /* SQL statement, UTF-8 encoded */
3382   int nByte,              /* Maximum length of zSql in bytes. */
3383   sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
3384   const char **pzTail     /* OUT: Pointer to unused portion of zSql */
3385 );
3386 SQLITE_API int sqlite3_prepare_v2(
3387   sqlite3 *db,            /* Database handle */
3388   const char *zSql,       /* SQL statement, UTF-8 encoded */
3389   int nByte,              /* Maximum length of zSql in bytes. */
3390   sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
3391   const char **pzTail     /* OUT: Pointer to unused portion of zSql */
3392 );
3393 SQLITE_API int sqlite3_prepare16(
3394   sqlite3 *db,            /* Database handle */
3395   const void *zSql,       /* SQL statement, UTF-16 encoded */
3396   int nByte,              /* Maximum length of zSql in bytes. */
3397   sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
3398   const void **pzTail     /* OUT: Pointer to unused portion of zSql */
3399 );
3400 SQLITE_API int sqlite3_prepare16_v2(
3401   sqlite3 *db,            /* Database handle */
3402   const void *zSql,       /* SQL statement, UTF-16 encoded */
3403   int nByte,              /* Maximum length of zSql in bytes. */
3404   sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
3405   const void **pzTail     /* OUT: Pointer to unused portion of zSql */
3406 );
3407 
3408 /*
3409 ** CAPI3REF: Retrieving Statement SQL
3410 **
3411 ** ^This interface can be used to retrieve a saved copy of the original
3412 ** SQL text used to create a [prepared statement] if that statement was
3413 ** compiled using either [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()].
3414 */
3415 SQLITE_API const char *sqlite3_sql(sqlite3_stmt *pStmt);
3416 
3417 /*
3418 ** CAPI3REF: Determine If An SQL Statement Writes The Database
3419 **
3420 ** ^The sqlite3_stmt_readonly(X) interface returns true (non-zero) if
3421 ** and only if the [prepared statement] X makes no direct changes to
3422 ** the content of the database file.
3423 **
3424 ** Note that [application-defined SQL functions] or
3425 ** [virtual tables] might change the database indirectly as a side effect.
3426 ** ^(For example, if an application defines a function "eval()" that
3427 ** calls [sqlite3_exec()], then the following SQL statement would
3428 ** change the database file through side-effects:
3429 **
3430 ** <blockquote><pre>
3431 **    SELECT eval('DELETE FROM t1') FROM t2;
3432 ** </pre></blockquote>
3433 **
3434 ** But because the [SELECT] statement does not change the database file
3435 ** directly, sqlite3_stmt_readonly() would still return true.)^
3436 **
3437 ** ^Transaction control statements such as [BEGIN], [COMMIT], [ROLLBACK],
3438 ** [SAVEPOINT], and [RELEASE] cause sqlite3_stmt_readonly() to return true,
3439 ** since the statements themselves do not actually modify the database but
3440 ** rather they control the timing of when other statements modify the
3441 ** database.  ^The [ATTACH] and [DETACH] statements also cause
3442 ** sqlite3_stmt_readonly() to return true since, while those statements
3443 ** change the configuration of a database connection, they do not make
3444 ** changes to the content of the database files on disk.
3445 */
3446 SQLITE_API int sqlite3_stmt_readonly(sqlite3_stmt *pStmt);
3447 
3448 /*
3449 ** CAPI3REF: Dynamically Typed Value Object
3450 ** KEYWORDS: {protected sqlite3_value} {unprotected sqlite3_value}
3451 **
3452 ** SQLite uses the sqlite3_value object to represent all values
3453 ** that can be stored in a database table. SQLite uses dynamic typing
3454 ** for the values it stores.  ^Values stored in sqlite3_value objects
3455 ** can be integers, floating point values, strings, BLOBs, or NULL.
3456 **
3457 ** An sqlite3_value object may be either "protected" or "unprotected".
3458 ** Some interfaces require a protected sqlite3_value.  Other interfaces
3459 ** will accept either a protected or an unprotected sqlite3_value.
3460 ** Every interface that accepts sqlite3_value arguments specifies
3461 ** whether or not it requires a protected sqlite3_value.
3462 **
3463 ** The terms "protected" and "unprotected" refer to whether or not
3464 ** a mutex is held.  An internal mutex is held for a protected
3465 ** sqlite3_value object but no mutex is held for an unprotected
3466 ** sqlite3_value object.  If SQLite is compiled to be single-threaded
3467 ** (with [SQLITE_THREADSAFE=0] and with [sqlite3_threadsafe()] returning 0)
3468 ** or if SQLite is run in one of reduced mutex modes
3469 ** [SQLITE_CONFIG_SINGLETHREAD] or [SQLITE_CONFIG_MULTITHREAD]
3470 ** then there is no distinction between protected and unprotected
3471 ** sqlite3_value objects and they can be used interchangeably.  However,
3472 ** for maximum code portability it is recommended that applications
3473 ** still make the distinction between protected and unprotected
3474 ** sqlite3_value objects even when not strictly required.
3475 **
3476 ** ^The sqlite3_value objects that are passed as parameters into the
3477 ** implementation of [application-defined SQL functions] are protected.
3478 ** ^The sqlite3_value object returned by
3479 ** [sqlite3_column_value()] is unprotected.
3480 ** Unprotected sqlite3_value objects may only be used with
3481 ** [sqlite3_result_value()] and [sqlite3_bind_value()].
3482 ** The [sqlite3_value_blob | sqlite3_value_type()] family of
3483 ** interfaces require protected sqlite3_value objects.
3484 */
3485 typedef struct Mem sqlite3_value;
3486 
3487 /*
3488 ** CAPI3REF: SQL Function Context Object
3489 **
3490 ** The context in which an SQL function executes is stored in an
3491 ** sqlite3_context object.  ^A pointer to an sqlite3_context object
3492 ** is always first parameter to [application-defined SQL functions].
3493 ** The application-defined SQL function implementation will pass this
3494 ** pointer through into calls to [sqlite3_result_int | sqlite3_result()],
3495 ** [sqlite3_aggregate_context()], [sqlite3_user_data()],
3496 ** [sqlite3_context_db_handle()], [sqlite3_get_auxdata()],
3497 ** and/or [sqlite3_set_auxdata()].
3498 */
3499 typedef struct sqlite3_context sqlite3_context;
3500 
3501 /*
3502 ** CAPI3REF: Binding Values To Prepared Statements
3503 ** KEYWORDS: {host parameter} {host parameters} {host parameter name}
3504 ** KEYWORDS: {SQL parameter} {SQL parameters} {parameter binding}
3505 **
3506 ** ^(In the SQL statement text input to [sqlite3_prepare_v2()] and its variants,
3507 ** literals may be replaced by a [parameter] that matches one of following
3508 ** templates:
3509 **
3510 ** <ul>
3511 ** <li>  ?
3512 ** <li>  ?NNN
3513 ** <li>  :VVV
3514 ** <li>  @VVV
3515 ** <li>  $VVV
3516 ** </ul>
3517 **
3518 ** In the templates above, NNN represents an integer literal,
3519 ** and VVV represents an alphanumeric identifier.)^  ^The values of these
3520 ** parameters (also called "host parameter names" or "SQL parameters")
3521 ** can be set using the sqlite3_bind_*() routines defined here.
3522 **
3523 ** ^The first argument to the sqlite3_bind_*() routines is always
3524 ** a pointer to the [sqlite3_stmt] object returned from
3525 ** [sqlite3_prepare_v2()] or its variants.
3526 **
3527 ** ^The second argument is the index of the SQL parameter to be set.
3528 ** ^The leftmost SQL parameter has an index of 1.  ^When the same named
3529 ** SQL parameter is used more than once, second and subsequent
3530 ** occurrences have the same index as the first occurrence.
3531 ** ^The index for named parameters can be looked up using the
3532 ** [sqlite3_bind_parameter_index()] API if desired.  ^The index
3533 ** for "?NNN" parameters is the value of NNN.
3534 ** ^The NNN value must be between 1 and the [sqlite3_limit()]
3535 ** parameter [SQLITE_LIMIT_VARIABLE_NUMBER] (default value: 999).
3536 **
3537 ** ^The third argument is the value to bind to the parameter.
3538 **
3539 ** ^(In those routines that have a fourth argument, its value is the
3540 ** number of bytes in the parameter.  To be clear: the value is the
3541 ** number of <u>bytes</u> in the value, not the number of characters.)^
3542 ** ^If the fourth parameter is negative, the length of the string is
3543 ** the number of bytes up to the first zero terminator.
3544 **
3545 ** ^The fifth argument to sqlite3_bind_blob(), sqlite3_bind_text(), and
3546 ** sqlite3_bind_text16() is a destructor used to dispose of the BLOB or
3547 ** string after SQLite has finished with it.  ^The destructor is called
3548 ** to dispose of the BLOB or string even if the call to sqlite3_bind_blob(),
3549 ** sqlite3_bind_text(), or sqlite3_bind_text16() fails.
3550 ** ^If the fifth argument is
3551 ** the special value [SQLITE_STATIC], then SQLite assumes that the
3552 ** information is in static, unmanaged space and does not need to be freed.
3553 ** ^If the fifth argument has the value [SQLITE_TRANSIENT], then
3554 ** SQLite makes its own private copy of the data immediately, before
3555 ** the sqlite3_bind_*() routine returns.
3556 **
3557 ** ^The sqlite3_bind_zeroblob() routine binds a BLOB of length N that
3558 ** is filled with zeroes.  ^A zeroblob uses a fixed amount of memory
3559 ** (just an integer to hold its size) while it is being processed.
3560 ** Zeroblobs are intended to serve as placeholders for BLOBs whose
3561 ** content is later written using
3562 ** [sqlite3_blob_open | incremental BLOB I/O] routines.
3563 ** ^A negative value for the zeroblob results in a zero-length BLOB.
3564 **
3565 ** ^If any of the sqlite3_bind_*() routines are called with a NULL pointer
3566 ** for the [prepared statement] or with a prepared statement for which
3567 ** [sqlite3_step()] has been called more recently than [sqlite3_reset()],
3568 ** then the call will return [SQLITE_MISUSE].  If any sqlite3_bind_()
3569 ** routine is passed a [prepared statement] that has been finalized, the
3570 ** result is undefined and probably harmful.
3571 **
3572 ** ^Bindings are not cleared by the [sqlite3_reset()] routine.
3573 ** ^Unbound parameters are interpreted as NULL.
3574 **
3575 ** ^The sqlite3_bind_* routines return [SQLITE_OK] on success or an
3576 ** [error code] if anything goes wrong.
3577 ** ^[SQLITE_RANGE] is returned if the parameter
3578 ** index is out of range.  ^[SQLITE_NOMEM] is returned if malloc() fails.
3579 **
3580 ** See also: [sqlite3_bind_parameter_count()],
3581 ** [sqlite3_bind_parameter_name()], and [sqlite3_bind_parameter_index()].
3582 */
3583 SQLITE_API int sqlite3_bind_blob(sqlite3_stmt*, int, const void*, int n, void(*)(void*));
3584 SQLITE_API int sqlite3_bind_double(sqlite3_stmt*, int, double);
3585 SQLITE_API int sqlite3_bind_int(sqlite3_stmt*, int, int);
3586 SQLITE_API int sqlite3_bind_int64(sqlite3_stmt*, int, sqlite3_int64);
3587 SQLITE_API int sqlite3_bind_null(sqlite3_stmt*, int);
3588 SQLITE_API int sqlite3_bind_text(sqlite3_stmt*, int, const char*, int n, void(*)(void*));
3589 SQLITE_API int sqlite3_bind_text16(sqlite3_stmt*, int, const void*, int, void(*)(void*));
3590 SQLITE_API int sqlite3_bind_value(sqlite3_stmt*, int, const sqlite3_value*);
3591 SQLITE_API int sqlite3_bind_zeroblob(sqlite3_stmt*, int, int n);
3592 
3593 /*
3594 ** CAPI3REF: Number Of SQL Parameters
3595 **
3596 ** ^This routine can be used to find the number of [SQL parameters]
3597 ** in a [prepared statement].  SQL parameters are tokens of the
3598 ** form "?", "?NNN", ":AAA", "$AAA", or "@AAA" that serve as
3599 ** placeholders for values that are [sqlite3_bind_blob | bound]
3600 ** to the parameters at a later time.
3601 **
3602 ** ^(This routine actually returns the index of the largest (rightmost)
3603 ** parameter. For all forms except ?NNN, this will correspond to the
3604 ** number of unique parameters.  If parameters of the ?NNN form are used,
3605 ** there may be gaps in the list.)^
3606 **
3607 ** See also: [sqlite3_bind_blob|sqlite3_bind()],
3608 ** [sqlite3_bind_parameter_name()], and
3609 ** [sqlite3_bind_parameter_index()].
3610 */
3611 SQLITE_API int sqlite3_bind_parameter_count(sqlite3_stmt*);
3612 
3613 /*
3614 ** CAPI3REF: Name Of A Host Parameter
3615 **
3616 ** ^The sqlite3_bind_parameter_name(P,N) interface returns
3617 ** the name of the N-th [SQL parameter] in the [prepared statement] P.
3618 ** ^(SQL parameters of the form "?NNN" or ":AAA" or "@AAA" or "$AAA"
3619 ** have a name which is the string "?NNN" or ":AAA" or "@AAA" or "$AAA"
3620 ** respectively.
3621 ** In other words, the initial ":" or "$" or "@" or "?"
3622 ** is included as part of the name.)^
3623 ** ^Parameters of the form "?" without a following integer have no name
3624 ** and are referred to as "nameless" or "anonymous parameters".
3625 **
3626 ** ^The first host parameter has an index of 1, not 0.
3627 **
3628 ** ^If the value N is out of range or if the N-th parameter is
3629 ** nameless, then NULL is returned.  ^The returned string is
3630 ** always in UTF-8 encoding even if the named parameter was
3631 ** originally specified as UTF-16 in [sqlite3_prepare16()] or
3632 ** [sqlite3_prepare16_v2()].
3633 **
3634 ** See also: [sqlite3_bind_blob|sqlite3_bind()],
3635 ** [sqlite3_bind_parameter_count()], and
3636 ** [sqlite3_bind_parameter_index()].
3637 */
3638 SQLITE_API const char *sqlite3_bind_parameter_name(sqlite3_stmt*, int);
3639 
3640 /*
3641 ** CAPI3REF: Index Of A Parameter With A Given Name
3642 **
3643 ** ^Return the index of an SQL parameter given its name.  ^The
3644 ** index value returned is suitable for use as the second
3645 ** parameter to [sqlite3_bind_blob|sqlite3_bind()].  ^A zero
3646 ** is returned if no matching parameter is found.  ^The parameter
3647 ** name must be given in UTF-8 even if the original statement
3648 ** was prepared from UTF-16 text using [sqlite3_prepare16_v2()].
3649 **
3650 ** See also: [sqlite3_bind_blob|sqlite3_bind()],
3651 ** [sqlite3_bind_parameter_count()], and
3652 ** [sqlite3_bind_parameter_index()].
3653 */
3654 SQLITE_API int sqlite3_bind_parameter_index(sqlite3_stmt*, const char *zName);
3655 
3656 /*
3657 ** CAPI3REF: Reset All Bindings On A Prepared Statement
3658 **
3659 ** ^Contrary to the intuition of many, [sqlite3_reset()] does not reset
3660 ** the [sqlite3_bind_blob | bindings] on a [prepared statement].
3661 ** ^Use this routine to reset all host parameters to NULL.
3662 */
3663 SQLITE_API int sqlite3_clear_bindings(sqlite3_stmt*);
3664 
3665 /*
3666 ** CAPI3REF: Number Of Columns In A Result Set
3667 **
3668 ** ^Return the number of columns in the result set returned by the
3669 ** [prepared statement]. ^This routine returns 0 if pStmt is an SQL
3670 ** statement that does not return data (for example an [UPDATE]).
3671 **
3672 ** See also: [sqlite3_data_count()]
3673 */
3674 SQLITE_API int sqlite3_column_count(sqlite3_stmt *pStmt);
3675 
3676 /*
3677 ** CAPI3REF: Column Names In A Result Set
3678 **
3679 ** ^These routines return the name assigned to a particular column
3680 ** in the result set of a [SELECT] statement.  ^The sqlite3_column_name()
3681 ** interface returns a pointer to a zero-terminated UTF-8 string
3682 ** and sqlite3_column_name16() returns a pointer to a zero-terminated
3683 ** UTF-16 string.  ^The first parameter is the [prepared statement]
3684 ** that implements the [SELECT] statement. ^The second parameter is the
3685 ** column number.  ^The leftmost column is number 0.
3686 **
3687 ** ^The returned string pointer is valid until either the [prepared statement]
3688 ** is destroyed by [sqlite3_finalize()] or until the statement is automatically
3689 ** reprepared by the first call to [sqlite3_step()] for a particular run
3690 ** or until the next call to
3691 ** sqlite3_column_name() or sqlite3_column_name16() on the same column.
3692 **
3693 ** ^If sqlite3_malloc() fails during the processing of either routine
3694 ** (for example during a conversion from UTF-8 to UTF-16) then a
3695 ** NULL pointer is returned.
3696 **
3697 ** ^The name of a result column is the value of the "AS" clause for
3698 ** that column, if there is an AS clause.  If there is no AS clause
3699 ** then the name of the column is unspecified and may change from
3700 ** one release of SQLite to the next.
3701 */
3702 SQLITE_API const char *sqlite3_column_name(sqlite3_stmt*, int N);
3703 SQLITE_API const void *sqlite3_column_name16(sqlite3_stmt*, int N);
3704 
3705 /*
3706 ** CAPI3REF: Source Of Data In A Query Result
3707 **
3708 ** ^These routines provide a means to determine the database, table, and
3709 ** table column that is the origin of a particular result column in
3710 ** [SELECT] statement.
3711 ** ^The name of the database or table or column can be returned as
3712 ** either a UTF-8 or UTF-16 string.  ^The _database_ routines return
3713 ** the database name, the _table_ routines return the table name, and
3714 ** the origin_ routines return the column name.
3715 ** ^The returned string is valid until the [prepared statement] is destroyed
3716 ** using [sqlite3_finalize()] or until the statement is automatically
3717 ** reprepared by the first call to [sqlite3_step()] for a particular run
3718 ** or until the same information is requested
3719 ** again in a different encoding.
3720 **
3721 ** ^The names returned are the original un-aliased names of the
3722 ** database, table, and column.
3723 **
3724 ** ^The first argument to these interfaces is a [prepared statement].
3725 ** ^These functions return information about the Nth result column returned by
3726 ** the statement, where N is the second function argument.
3727 ** ^The left-most column is column 0 for these routines.
3728 **
3729 ** ^If the Nth column returned by the statement is an expression or
3730 ** subquery and is not a column value, then all of these functions return
3731 ** NULL.  ^These routine might also return NULL if a memory allocation error
3732 ** occurs.  ^Otherwise, they return the name of the attached database, table,
3733 ** or column that query result column was extracted from.
3734 **
3735 ** ^As with all other SQLite APIs, those whose names end with "16" return
3736 ** UTF-16 encoded strings and the other functions return UTF-8.
3737 **
3738 ** ^These APIs are only available if the library was compiled with the
3739 ** [SQLITE_ENABLE_COLUMN_METADATA] C-preprocessor symbol.
3740 **
3741 ** If two or more threads call one or more of these routines against the same
3742 ** prepared statement and column at the same time then the results are
3743 ** undefined.
3744 **
3745 ** If two or more threads call one or more
3746 ** [sqlite3_column_database_name | column metadata interfaces]
3747 ** for the same [prepared statement] and result column
3748 ** at the same time then the results are undefined.
3749 */
3750 SQLITE_API const char *sqlite3_column_database_name(sqlite3_stmt*,int);
3751 SQLITE_API const void *sqlite3_column_database_name16(sqlite3_stmt*,int);
3752 SQLITE_API const char *sqlite3_column_table_name(sqlite3_stmt*,int);
3753 SQLITE_API const void *sqlite3_column_table_name16(sqlite3_stmt*,int);
3754 SQLITE_API const char *sqlite3_column_origin_name(sqlite3_stmt*,int);
3755 SQLITE_API const void *sqlite3_column_origin_name16(sqlite3_stmt*,int);
3756 
3757 /*
3758 ** CAPI3REF: Declared Datatype Of A Query Result
3759 **
3760 ** ^(The first parameter is a [prepared statement].
3761 ** If this statement is a [SELECT] statement and the Nth column of the
3762 ** returned result set of that [SELECT] is a table column (not an
3763 ** expression or subquery) then the declared type of the table
3764 ** column is returned.)^  ^If the Nth column of the result set is an
3765 ** expression or subquery, then a NULL pointer is returned.
3766 ** ^The returned string is always UTF-8 encoded.
3767 **
3768 ** ^(For example, given the database schema:
3769 **
3770 ** CREATE TABLE t1(c1 VARIANT);
3771 **
3772 ** and the following statement to be compiled:
3773 **
3774 ** SELECT c1 + 1, c1 FROM t1;
3775 **
3776 ** this routine would return the string "VARIANT" for the second result
3777 ** column (i==1), and a NULL pointer for the first result column (i==0).)^
3778 **
3779 ** ^SQLite uses dynamic run-time typing.  ^So just because a column
3780 ** is declared to contain a particular type does not mean that the
3781 ** data stored in that column is of the declared type.  SQLite is
3782 ** strongly typed, but the typing is dynamic not static.  ^Type
3783 ** is associated with individual values, not with the containers
3784 ** used to hold those values.
3785 */
3786 SQLITE_API const char *sqlite3_column_decltype(sqlite3_stmt*,int);
3787 SQLITE_API const void *sqlite3_column_decltype16(sqlite3_stmt*,int);
3788 
3789 /*
3790 ** CAPI3REF: Evaluate An SQL Statement
3791 **
3792 ** After a [prepared statement] has been prepared using either
3793 ** [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()] or one of the legacy
3794 ** interfaces [sqlite3_prepare()] or [sqlite3_prepare16()], this function
3795 ** must be called one or more times to evaluate the statement.
3796 **
3797 ** The details of the behavior of the sqlite3_step() interface depend
3798 ** on whether the statement was prepared using the newer "v2" interface
3799 ** [sqlite3_prepare_v2()] and [sqlite3_prepare16_v2()] or the older legacy
3800 ** interface [sqlite3_prepare()] and [sqlite3_prepare16()].  The use of the
3801 ** new "v2" interface is recommended for new applications but the legacy
3802 ** interface will continue to be supported.
3803 **
3804 ** ^In the legacy interface, the return value will be either [SQLITE_BUSY],
3805 ** [SQLITE_DONE], [SQLITE_ROW], [SQLITE_ERROR], or [SQLITE_MISUSE].
3806 ** ^With the "v2" interface, any of the other [result codes] or
3807 ** [extended result codes] might be returned as well.
3808 **
3809 ** ^[SQLITE_BUSY] means that the database engine was unable to acquire the
3810 ** database locks it needs to do its job.  ^If the statement is a [COMMIT]
3811 ** or occurs outside of an explicit transaction, then you can retry the
3812 ** statement.  If the statement is not a [COMMIT] and occurs within an
3813 ** explicit transaction then you should rollback the transaction before
3814 ** continuing.
3815 **
3816 ** ^[SQLITE_DONE] means that the statement has finished executing
3817 ** successfully.  sqlite3_step() should not be called again on this virtual
3818 ** machine without first calling [sqlite3_reset()] to reset the virtual
3819 ** machine back to its initial state.
3820 **
3821 ** ^If the SQL statement being executed returns any data, then [SQLITE_ROW]
3822 ** is returned each time a new row of data is ready for processing by the
3823 ** caller. The values may be accessed using the [column access functions].
3824 ** sqlite3_step() is called again to retrieve the next row of data.
3825 **
3826 ** ^[SQLITE_ERROR] means that a run-time error (such as a constraint
3827 ** violation) has occurred.  sqlite3_step() should not be called again on
3828 ** the VM. More information may be found by calling [sqlite3_errmsg()].
3829 ** ^With the legacy interface, a more specific error code (for example,
3830 ** [SQLITE_INTERRUPT], [SQLITE_SCHEMA], [SQLITE_CORRUPT], and so forth)
3831 ** can be obtained by calling [sqlite3_reset()] on the
3832 ** [prepared statement].  ^In the "v2" interface,
3833 ** the more specific error code is returned directly by sqlite3_step().
3834 **
3835 ** [SQLITE_MISUSE] means that the this routine was called inappropriately.
3836 ** Perhaps it was called on a [prepared statement] that has
3837 ** already been [sqlite3_finalize | finalized] or on one that had
3838 ** previously returned [SQLITE_ERROR] or [SQLITE_DONE].  Or it could
3839 ** be the case that the same database connection is being used by two or
3840 ** more threads at the same moment in time.
3841 **
3842 ** For all versions of SQLite up to and including 3.6.23.1, a call to
3843 ** [sqlite3_reset()] was required after sqlite3_step() returned anything
3844 ** other than [SQLITE_ROW] before any subsequent invocation of
3845 ** sqlite3_step().  Failure to reset the prepared statement using
3846 ** [sqlite3_reset()] would result in an [SQLITE_MISUSE] return from
3847 ** sqlite3_step().  But after version 3.6.23.1, sqlite3_step() began
3848 ** calling [sqlite3_reset()] automatically in this circumstance rather
3849 ** than returning [SQLITE_MISUSE].  This is not considered a compatibility
3850 ** break because any application that ever receives an SQLITE_MISUSE error
3851 ** is broken by definition.  The [SQLITE_OMIT_AUTORESET] compile-time option
3852 ** can be used to restore the legacy behavior.
3853 **
3854 ** <b>Goofy Interface Alert:</b> In the legacy interface, the sqlite3_step()
3855 ** API always returns a generic error code, [SQLITE_ERROR], following any
3856 ** error other than [SQLITE_BUSY] and [SQLITE_MISUSE].  You must call
3857 ** [sqlite3_reset()] or [sqlite3_finalize()] in order to find one of the
3858 ** specific [error codes] that better describes the error.
3859 ** We admit that this is a goofy design.  The problem has been fixed
3860 ** with the "v2" interface.  If you prepare all of your SQL statements
3861 ** using either [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()] instead
3862 ** of the legacy [sqlite3_prepare()] and [sqlite3_prepare16()] interfaces,
3863 ** then the more specific [error codes] are returned directly
3864 ** by sqlite3_step().  The use of the "v2" interface is recommended.
3865 */
3866 SQLITE_API int sqlite3_step(sqlite3_stmt*);
3867 
3868 /*
3869 ** CAPI3REF: Number of columns in a result set
3870 **
3871 ** ^The sqlite3_data_count(P) interface returns the number of columns in the
3872 ** current row of the result set of [prepared statement] P.
3873 ** ^If prepared statement P does not have results ready to return
3874 ** (via calls to the [sqlite3_column_int | sqlite3_column_*()] of
3875 ** interfaces) then sqlite3_data_count(P) returns 0.
3876 ** ^The sqlite3_data_count(P) routine also returns 0 if P is a NULL pointer.
3877 **
3878 ** See also: [sqlite3_column_count()]
3879 */
3880 SQLITE_API int sqlite3_data_count(sqlite3_stmt *pStmt);
3881 
3882 /*
3883 ** CAPI3REF: Fundamental Datatypes
3884 ** KEYWORDS: SQLITE_TEXT
3885 **
3886 ** ^(Every value in SQLite has one of five fundamental datatypes:
3887 **
3888 ** <ul>
3889 ** <li> 64-bit signed integer
3890 ** <li> 64-bit IEEE floating point number
3891 ** <li> string
3892 ** <li> BLOB
3893 ** <li> NULL
3894 ** </ul>)^
3895 **
3896 ** These constants are codes for each of those types.
3897 **
3898 ** Note that the SQLITE_TEXT constant was also used in SQLite version 2
3899 ** for a completely different meaning.  Software that links against both
3900 ** SQLite version 2 and SQLite version 3 should use SQLITE3_TEXT, not
3901 ** SQLITE_TEXT.
3902 */
3903 #define SQLITE_INTEGER  1
3904 #define SQLITE_FLOAT    2
3905 #define SQLITE_BLOB     4
3906 #define SQLITE_NULL     5
3907 #ifdef SQLITE_TEXT
3908 # undef SQLITE_TEXT
3909 #else
3910 # define SQLITE_TEXT     3
3911 #endif
3912 #define SQLITE3_TEXT     3
3913 
3914 /*
3915 ** CAPI3REF: Result Values From A Query
3916 ** KEYWORDS: {column access functions}
3917 **
3918 ** These routines form the "result set" interface.
3919 **
3920 ** ^These routines return information about a single column of the current
3921 ** result row of a query.  ^In every case the first argument is a pointer
3922 ** to the [prepared statement] that is being evaluated (the [sqlite3_stmt*]
3923 ** that was returned from [sqlite3_prepare_v2()] or one of its variants)
3924 ** and the second argument is the index of the column for which information
3925 ** should be returned. ^The leftmost column of the result set has the index 0.
3926 ** ^The number of columns in the result can be determined using
3927 ** [sqlite3_column_count()].
3928 **
3929 ** If the SQL statement does not currently point to a valid row, or if the
3930 ** column index is out of range, the result is undefined.
3931 ** These routines may only be called when the most recent call to
3932 ** [sqlite3_step()] has returned [SQLITE_ROW] and neither
3933 ** [sqlite3_reset()] nor [sqlite3_finalize()] have been called subsequently.
3934 ** If any of these routines are called after [sqlite3_reset()] or
3935 ** [sqlite3_finalize()] or after [sqlite3_step()] has returned
3936 ** something other than [SQLITE_ROW], the results are undefined.
3937 ** If [sqlite3_step()] or [sqlite3_reset()] or [sqlite3_finalize()]
3938 ** are called from a different thread while any of these routines
3939 ** are pending, then the results are undefined.
3940 **
3941 ** ^The sqlite3_column_type() routine returns the
3942 ** [SQLITE_INTEGER | datatype code] for the initial data type
3943 ** of the result column.  ^The returned value is one of [SQLITE_INTEGER],
3944 ** [SQLITE_FLOAT], [SQLITE_TEXT], [SQLITE_BLOB], or [SQLITE_NULL].  The value
3945 ** returned by sqlite3_column_type() is only meaningful if no type
3946 ** conversions have occurred as described below.  After a type conversion,
3947 ** the value returned by sqlite3_column_type() is undefined.  Future
3948 ** versions of SQLite may change the behavior of sqlite3_column_type()
3949 ** following a type conversion.
3950 **
3951 ** ^If the result is a BLOB or UTF-8 string then the sqlite3_column_bytes()
3952 ** routine returns the number of bytes in that BLOB or string.
3953 ** ^If the result is a UTF-16 string, then sqlite3_column_bytes() converts
3954 ** the string to UTF-8 and then returns the number of bytes.
3955 ** ^If the result is a numeric value then sqlite3_column_bytes() uses
3956 ** [sqlite3_snprintf()] to convert that value to a UTF-8 string and returns
3957 ** the number of bytes in that string.
3958 ** ^If the result is NULL, then sqlite3_column_bytes() returns zero.
3959 **
3960 ** ^If the result is a BLOB or UTF-16 string then the sqlite3_column_bytes16()
3961 ** routine returns the number of bytes in that BLOB or string.
3962 ** ^If the result is a UTF-8 string, then sqlite3_column_bytes16() converts
3963 ** the string to UTF-16 and then returns the number of bytes.
3964 ** ^If the result is a numeric value then sqlite3_column_bytes16() uses
3965 ** [sqlite3_snprintf()] to convert that value to a UTF-16 string and returns
3966 ** the number of bytes in that string.
3967 ** ^If the result is NULL, then sqlite3_column_bytes16() returns zero.
3968 **
3969 ** ^The values returned by [sqlite3_column_bytes()] and
3970 ** [sqlite3_column_bytes16()] do not include the zero terminators at the end
3971 ** of the string.  ^For clarity: the values returned by
3972 ** [sqlite3_column_bytes()] and [sqlite3_column_bytes16()] are the number of
3973 ** bytes in the string, not the number of characters.
3974 **
3975 ** ^Strings returned by sqlite3_column_text() and sqlite3_column_text16(),
3976 ** even empty strings, are always zero terminated.  ^The return
3977 ** value from sqlite3_column_blob() for a zero-length BLOB is a NULL pointer.
3978 **
3979 ** ^The object returned by [sqlite3_column_value()] is an
3980 ** [unprotected sqlite3_value] object.  An unprotected sqlite3_value object
3981 ** may only be used with [sqlite3_bind_value()] and [sqlite3_result_value()].
3982 ** If the [unprotected sqlite3_value] object returned by
3983 ** [sqlite3_column_value()] is used in any other way, including calls
3984 ** to routines like [sqlite3_value_int()], [sqlite3_value_text()],
3985 ** or [sqlite3_value_bytes()], then the behavior is undefined.
3986 **
3987 ** These routines attempt to convert the value where appropriate.  ^For
3988 ** example, if the internal representation is FLOAT and a text result
3989 ** is requested, [sqlite3_snprintf()] is used internally to perform the
3990 ** conversion automatically.  ^(The following table details the conversions
3991 ** that are applied:
3992 **
3993 ** <blockquote>
3994 ** <table border="1">
3995 ** <tr><th> Internal<br>Type <th> Requested<br>Type <th>  Conversion
3996 **
3997 ** <tr><td>  NULL    <td> INTEGER   <td> Result is 0
3998 ** <tr><td>  NULL    <td>  FLOAT    <td> Result is 0.0
3999 ** <tr><td>  NULL    <td>   TEXT    <td> Result is NULL pointer
4000 ** <tr><td>  NULL    <td>   BLOB    <td> Result is NULL pointer
4001 ** <tr><td> INTEGER  <td>  FLOAT    <td> Convert from integer to float
4002 ** <tr><td> INTEGER  <td>   TEXT    <td> ASCII rendering of the integer
4003 ** <tr><td> INTEGER  <td>   BLOB    <td> Same as INTEGER->TEXT
4004 ** <tr><td>  FLOAT   <td> INTEGER   <td> Convert from float to integer
4005 ** <tr><td>  FLOAT   <td>   TEXT    <td> ASCII rendering of the float
4006 ** <tr><td>  FLOAT   <td>   BLOB    <td> Same as FLOAT->TEXT
4007 ** <tr><td>  TEXT    <td> INTEGER   <td> Use atoi()
4008 ** <tr><td>  TEXT    <td>  FLOAT    <td> Use atof()
4009 ** <tr><td>  TEXT    <td>   BLOB    <td> No change
4010 ** <tr><td>  BLOB    <td> INTEGER   <td> Convert to TEXT then use atoi()
4011 ** <tr><td>  BLOB    <td>  FLOAT    <td> Convert to TEXT then use atof()
4012 ** <tr><td>  BLOB    <td>   TEXT    <td> Add a zero terminator if needed
4013 ** </table>
4014 ** </blockquote>)^
4015 **
4016 ** The table above makes reference to standard C library functions atoi()
4017 ** and atof().  SQLite does not really use these functions.  It has its
4018 ** own equivalent internal routines.  The atoi() and atof() names are
4019 ** used in the table for brevity and because they are familiar to most
4020 ** C programmers.
4021 **
4022 ** Note that when type conversions occur, pointers returned by prior
4023 ** calls to sqlite3_column_blob(), sqlite3_column_text(), and/or
4024 ** sqlite3_column_text16() may be invalidated.
4025 ** Type conversions and pointer invalidations might occur
4026 ** in the following cases:
4027 **
4028 ** <ul>
4029 ** <li> The initial content is a BLOB and sqlite3_column_text() or
4030 **      sqlite3_column_text16() is called.  A zero-terminator might
4031 **      need to be added to the string.</li>
4032 ** <li> The initial content is UTF-8 text and sqlite3_column_bytes16() or
4033 **      sqlite3_column_text16() is called.  The content must be converted
4034 **      to UTF-16.</li>
4035 ** <li> The initial content is UTF-16 text and sqlite3_column_bytes() or
4036 **      sqlite3_column_text() is called.  The content must be converted
4037 **      to UTF-8.</li>
4038 ** </ul>
4039 **
4040 ** ^Conversions between UTF-16be and UTF-16le are always done in place and do
4041 ** not invalidate a prior pointer, though of course the content of the buffer
4042 ** that the prior pointer references will have been modified.  Other kinds
4043 ** of conversion are done in place when it is possible, but sometimes they
4044 ** are not possible and in those cases prior pointers are invalidated.
4045 **
4046 ** The safest and easiest to remember policy is to invoke these routines
4047 ** in one of the following ways:
4048 **
4049 ** <ul>
4050 **  <li>sqlite3_column_text() followed by sqlite3_column_bytes()</li>
4051 **  <li>sqlite3_column_blob() followed by sqlite3_column_bytes()</li>
4052 **  <li>sqlite3_column_text16() followed by sqlite3_column_bytes16()</li>
4053 ** </ul>
4054 **
4055 ** In other words, you should call sqlite3_column_text(),
4056 ** sqlite3_column_blob(), or sqlite3_column_text16() first to force the result
4057 ** into the desired format, then invoke sqlite3_column_bytes() or
4058 ** sqlite3_column_bytes16() to find the size of the result.  Do not mix calls
4059 ** to sqlite3_column_text() or sqlite3_column_blob() with calls to
4060 ** sqlite3_column_bytes16(), and do not mix calls to sqlite3_column_text16()
4061 ** with calls to sqlite3_column_bytes().
4062 **
4063 ** ^The pointers returned are valid until a type conversion occurs as
4064 ** described above, or until [sqlite3_step()] or [sqlite3_reset()] or
4065 ** [sqlite3_finalize()] is called.  ^The memory space used to hold strings
4066 ** and BLOBs is freed automatically.  Do <b>not</b> pass the pointers returned
4067 ** [sqlite3_column_blob()], [sqlite3_column_text()], etc. into
4068 ** [sqlite3_free()].
4069 **
4070 ** ^(If a memory allocation error occurs during the evaluation of any
4071 ** of these routines, a default value is returned.  The default value
4072 ** is either the integer 0, the floating point number 0.0, or a NULL
4073 ** pointer.  Subsequent calls to [sqlite3_errcode()] will return
4074 ** [SQLITE_NOMEM].)^
4075 */
4076 SQLITE_API const void *sqlite3_column_blob(sqlite3_stmt*, int iCol);
4077 SQLITE_API int sqlite3_column_bytes(sqlite3_stmt*, int iCol);
4078 SQLITE_API int sqlite3_column_bytes16(sqlite3_stmt*, int iCol);
4079 SQLITE_API double sqlite3_column_double(sqlite3_stmt*, int iCol);
4080 SQLITE_API int sqlite3_column_int(sqlite3_stmt*, int iCol);
4081 SQLITE_API sqlite3_int64 sqlite3_column_int64(sqlite3_stmt*, int iCol);
4082 SQLITE_API const unsigned char *sqlite3_column_text(sqlite3_stmt*, int iCol);
4083 SQLITE_API const void *sqlite3_column_text16(sqlite3_stmt*, int iCol);
4084 SQLITE_API int sqlite3_column_type(sqlite3_stmt*, int iCol);
4085 SQLITE_API sqlite3_value *sqlite3_column_value(sqlite3_stmt*, int iCol);
4086 
4087 /*
4088 ** CAPI3REF: Destroy A Prepared Statement Object
4089 **
4090 ** ^The sqlite3_finalize() function is called to delete a [prepared statement].
4091 ** ^If the most recent evaluation of the statement encountered no errors
4092 ** or if the statement is never been evaluated, then sqlite3_finalize() returns
4093 ** SQLITE_OK.  ^If the most recent evaluation of statement S failed, then
4094 ** sqlite3_finalize(S) returns the appropriate [error code] or
4095 ** [extended error code].
4096 **
4097 ** ^The sqlite3_finalize(S) routine can be called at any point during
4098 ** the life cycle of [prepared statement] S:
4099 ** before statement S is ever evaluated, after
4100 ** one or more calls to [sqlite3_reset()], or after any call
4101 ** to [sqlite3_step()] regardless of whether or not the statement has
4102 ** completed execution.
4103 **
4104 ** ^Invoking sqlite3_finalize() on a NULL pointer is a harmless no-op.
4105 **
4106 ** The application must finalize every [prepared statement] in order to avoid
4107 ** resource leaks.  It is a grievous error for the application to try to use
4108 ** a prepared statement after it has been finalized.  Any use of a prepared
4109 ** statement after it has been finalized can result in undefined and
4110 ** undesirable behavior such as segfaults and heap corruption.
4111 */
4112 SQLITE_API int sqlite3_finalize(sqlite3_stmt *pStmt);
4113 
4114 /*
4115 ** CAPI3REF: Reset A Prepared Statement Object
4116 **
4117 ** The sqlite3_reset() function is called to reset a [prepared statement]
4118 ** object back to its initial state, ready to be re-executed.
4119 ** ^Any SQL statement variables that had values bound to them using
4120 ** the [sqlite3_bind_blob | sqlite3_bind_*() API] retain their values.
4121 ** Use [sqlite3_clear_bindings()] to reset the bindings.
4122 **
4123 ** ^The [sqlite3_reset(S)] interface resets the [prepared statement] S
4124 ** back to the beginning of its program.
4125 **
4126 ** ^If the most recent call to [sqlite3_step(S)] for the
4127 ** [prepared statement] S returned [SQLITE_ROW] or [SQLITE_DONE],
4128 ** or if [sqlite3_step(S)] has never before been called on S,
4129 ** then [sqlite3_reset(S)] returns [SQLITE_OK].
4130 **
4131 ** ^If the most recent call to [sqlite3_step(S)] for the
4132 ** [prepared statement] S indicated an error, then
4133 ** [sqlite3_reset(S)] returns an appropriate [error code].
4134 **
4135 ** ^The [sqlite3_reset(S)] interface does not change the values
4136 ** of any [sqlite3_bind_blob|bindings] on the [prepared statement] S.
4137 */
4138 SQLITE_API int sqlite3_reset(sqlite3_stmt *pStmt);
4139 
4140 /*
4141 ** CAPI3REF: Create Or Redefine SQL Functions
4142 ** KEYWORDS: {function creation routines}
4143 ** KEYWORDS: {application-defined SQL function}
4144 ** KEYWORDS: {application-defined SQL functions}
4145 **
4146 ** ^These functions (collectively known as "function creation routines")
4147 ** are used to add SQL functions or aggregates or to redefine the behavior
4148 ** of existing SQL functions or aggregates.  The only differences between
4149 ** these routines are the text encoding expected for
4150 ** the second parameter (the name of the function being created)
4151 ** and the presence or absence of a destructor callback for
4152 ** the application data pointer.
4153 **
4154 ** ^The first parameter is the [database connection] to which the SQL
4155 ** function is to be added.  ^If an application uses more than one database
4156 ** connection then application-defined SQL functions must be added
4157 ** to each database connection separately.
4158 **
4159 ** ^The second parameter is the name of the SQL function to be created or
4160 ** redefined.  ^The length of the name is limited to 255 bytes in a UTF-8
4161 ** representation, exclusive of the zero-terminator.  ^Note that the name
4162 ** length limit is in UTF-8 bytes, not characters nor UTF-16 bytes.
4163 ** ^Any attempt to create a function with a longer name
4164 ** will result in [SQLITE_MISUSE] being returned.
4165 **
4166 ** ^The third parameter (nArg)
4167 ** is the number of arguments that the SQL function or
4168 ** aggregate takes. ^If this parameter is -1, then the SQL function or
4169 ** aggregate may take any number of arguments between 0 and the limit
4170 ** set by [sqlite3_limit]([SQLITE_LIMIT_FUNCTION_ARG]).  If the third
4171 ** parameter is less than -1 or greater than 127 then the behavior is
4172 ** undefined.
4173 **
4174 ** ^The fourth parameter, eTextRep, specifies what
4175 ** [SQLITE_UTF8 | text encoding] this SQL function prefers for
4176 ** its parameters.  Every SQL function implementation must be able to work
4177 ** with UTF-8, UTF-16le, or UTF-16be.  But some implementations may be
4178 ** more efficient with one encoding than another.  ^An application may
4179 ** invoke sqlite3_create_function() or sqlite3_create_function16() multiple
4180 ** times with the same function but with different values of eTextRep.
4181 ** ^When multiple implementations of the same function are available, SQLite
4182 ** will pick the one that involves the least amount of data conversion.
4183 ** If there is only a single implementation which does not care what text
4184 ** encoding is used, then the fourth argument should be [SQLITE_ANY].
4185 **
4186 ** ^(The fifth parameter is an arbitrary pointer.  The implementation of the
4187 ** function can gain access to this pointer using [sqlite3_user_data()].)^
4188 **
4189 ** ^The sixth, seventh and eighth parameters, xFunc, xStep and xFinal, are
4190 ** pointers to C-language functions that implement the SQL function or
4191 ** aggregate. ^A scalar SQL function requires an implementation of the xFunc
4192 ** callback only; NULL pointers must be passed as the xStep and xFinal
4193 ** parameters. ^An aggregate SQL function requires an implementation of xStep
4194 ** and xFinal and NULL pointer must be passed for xFunc. ^To delete an existing
4195 ** SQL function or aggregate, pass NULL pointers for all three function
4196 ** callbacks.
4197 **
4198 ** ^(If the ninth parameter to sqlite3_create_function_v2() is not NULL,
4199 ** then it is destructor for the application data pointer.
4200 ** The destructor is invoked when the function is deleted, either by being
4201 ** overloaded or when the database connection closes.)^
4202 ** ^The destructor is also invoked if the call to
4203 ** sqlite3_create_function_v2() fails.
4204 ** ^When the destructor callback of the tenth parameter is invoked, it
4205 ** is passed a single argument which is a copy of the application data
4206 ** pointer which was the fifth parameter to sqlite3_create_function_v2().
4207 **
4208 ** ^It is permitted to register multiple implementations of the same
4209 ** functions with the same name but with either differing numbers of
4210 ** arguments or differing preferred text encodings.  ^SQLite will use
4211 ** the implementation that most closely matches the way in which the
4212 ** SQL function is used.  ^A function implementation with a non-negative
4213 ** nArg parameter is a better match than a function implementation with
4214 ** a negative nArg.  ^A function where the preferred text encoding
4215 ** matches the database encoding is a better
4216 ** match than a function where the encoding is different.
4217 ** ^A function where the encoding difference is between UTF16le and UTF16be
4218 ** is a closer match than a function where the encoding difference is
4219 ** between UTF8 and UTF16.
4220 **
4221 ** ^Built-in functions may be overloaded by new application-defined functions.
4222 **
4223 ** ^An application-defined function is permitted to call other
4224 ** SQLite interfaces.  However, such calls must not
4225 ** close the database connection nor finalize or reset the prepared
4226 ** statement in which the function is running.
4227 */
4228 SQLITE_API int sqlite3_create_function(
4229   sqlite3 *db,
4230   const char *zFunctionName,
4231   int nArg,
4232   int eTextRep,
4233   void *pApp,
4234   void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
4235   void (*xStep)(sqlite3_context*,int,sqlite3_value**),
4236   void (*xFinal)(sqlite3_context*)
4237 );
4238 SQLITE_API int sqlite3_create_function16(
4239   sqlite3 *db,
4240   const void *zFunctionName,
4241   int nArg,
4242   int eTextRep,
4243   void *pApp,
4244   void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
4245   void (*xStep)(sqlite3_context*,int,sqlite3_value**),
4246   void (*xFinal)(sqlite3_context*)
4247 );
4248 SQLITE_API int sqlite3_create_function_v2(
4249   sqlite3 *db,
4250   const char *zFunctionName,
4251   int nArg,
4252   int eTextRep,
4253   void *pApp,
4254   void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
4255   void (*xStep)(sqlite3_context*,int,sqlite3_value**),
4256   void (*xFinal)(sqlite3_context*),
4257   void(*xDestroy)(void*)
4258 );
4259 
4260 /*
4261 ** CAPI3REF: Text Encodings
4262 **
4263 ** These constant define integer codes that represent the various
4264 ** text encodings supported by SQLite.
4265 */
4266 #define SQLITE_UTF8           1
4267 #define SQLITE_UTF16LE        2
4268 #define SQLITE_UTF16BE        3
4269 #define SQLITE_UTF16          4    /* Use native byte order */
4270 #define SQLITE_ANY            5    /* sqlite3_create_function only */
4271 #define SQLITE_UTF16_ALIGNED  8    /* sqlite3_create_collation only */
4272 
4273 /*
4274 ** CAPI3REF: Deprecated Functions
4275 ** DEPRECATED
4276 **
4277 ** These functions are [deprecated].  In order to maintain
4278 ** backwards compatibility with older code, these functions continue
4279 ** to be supported.  However, new applications should avoid
4280 ** the use of these functions.  To help encourage people to avoid
4281 ** using these functions, we are not going to tell you what they do.
4282 */
4283 #ifndef SQLITE_OMIT_DEPRECATED
4284 SQLITE_API SQLITE_DEPRECATED int sqlite3_aggregate_count(sqlite3_context*);
4285 SQLITE_API SQLITE_DEPRECATED int sqlite3_expired(sqlite3_stmt*);
4286 SQLITE_API SQLITE_DEPRECATED int sqlite3_transfer_bindings(sqlite3_stmt*, sqlite3_stmt*);
4287 SQLITE_API SQLITE_DEPRECATED int sqlite3_global_recover(void);
4288 SQLITE_API SQLITE_DEPRECATED void sqlite3_thread_cleanup(void);
4289 SQLITE_API SQLITE_DEPRECATED int sqlite3_memory_alarm(void(*)(void*,sqlite3_int64,int),void*,sqlite3_int64);
4290 #endif
4291 
4292 /*
4293 ** CAPI3REF: Obtaining SQL Function Parameter Values
4294 **
4295 ** The C-language implementation of SQL functions and aggregates uses
4296 ** this set of interface routines to access the parameter values on
4297 ** the function or aggregate.
4298 **
4299 ** The xFunc (for scalar functions) or xStep (for aggregates) parameters
4300 ** to [sqlite3_create_function()] and [sqlite3_create_function16()]
4301 ** define callbacks that implement the SQL functions and aggregates.
4302 ** The 3rd parameter to these callbacks is an array of pointers to
4303 ** [protected sqlite3_value] objects.  There is one [sqlite3_value] object for
4304 ** each parameter to the SQL function.  These routines are used to
4305 ** extract values from the [sqlite3_value] objects.
4306 **
4307 ** These routines work only with [protected sqlite3_value] objects.
4308 ** Any attempt to use these routines on an [unprotected sqlite3_value]
4309 ** object results in undefined behavior.
4310 **
4311 ** ^These routines work just like the corresponding [column access functions]
4312 ** except that  these routines take a single [protected sqlite3_value] object
4313 ** pointer instead of a [sqlite3_stmt*] pointer and an integer column number.
4314 **
4315 ** ^The sqlite3_value_text16() interface extracts a UTF-16 string
4316 ** in the native byte-order of the host machine.  ^The
4317 ** sqlite3_value_text16be() and sqlite3_value_text16le() interfaces
4318 ** extract UTF-16 strings as big-endian and little-endian respectively.
4319 **
4320 ** ^(The sqlite3_value_numeric_type() interface attempts to apply
4321 ** numeric affinity to the value.  This means that an attempt is
4322 ** made to convert the value to an integer or floating point.  If
4323 ** such a conversion is possible without loss of information (in other
4324 ** words, if the value is a string that looks like a number)
4325 ** then the conversion is performed.  Otherwise no conversion occurs.
4326 ** The [SQLITE_INTEGER | datatype] after conversion is returned.)^
4327 **
4328 ** Please pay particular attention to the fact that the pointer returned
4329 ** from [sqlite3_value_blob()], [sqlite3_value_text()], or
4330 ** [sqlite3_value_text16()] can be invalidated by a subsequent call to
4331 ** [sqlite3_value_bytes()], [sqlite3_value_bytes16()], [sqlite3_value_text()],
4332 ** or [sqlite3_value_text16()].
4333 **
4334 ** These routines must be called from the same thread as
4335 ** the SQL function that supplied the [sqlite3_value*] parameters.
4336 */
4337 SQLITE_API const void *sqlite3_value_blob(sqlite3_value*);
4338 SQLITE_API int sqlite3_value_bytes(sqlite3_value*);
4339 SQLITE_API int sqlite3_value_bytes16(sqlite3_value*);
4340 SQLITE_API double sqlite3_value_double(sqlite3_value*);
4341 SQLITE_API int sqlite3_value_int(sqlite3_value*);
4342 SQLITE_API sqlite3_int64 sqlite3_value_int64(sqlite3_value*);
4343 SQLITE_API const unsigned char *sqlite3_value_text(sqlite3_value*);
4344 SQLITE_API const void *sqlite3_value_text16(sqlite3_value*);
4345 SQLITE_API const void *sqlite3_value_text16le(sqlite3_value*);
4346 SQLITE_API const void *sqlite3_value_text16be(sqlite3_value*);
4347 SQLITE_API int sqlite3_value_type(sqlite3_value*);
4348 SQLITE_API int sqlite3_value_numeric_type(sqlite3_value*);
4349 
4350 /*
4351 ** CAPI3REF: Obtain Aggregate Function Context
4352 **
4353 ** Implementations of aggregate SQL functions use this
4354 ** routine to allocate memory for storing their state.
4355 **
4356 ** ^The first time the sqlite3_aggregate_context(C,N) routine is called
4357 ** for a particular aggregate function, SQLite
4358 ** allocates N of memory, zeroes out that memory, and returns a pointer
4359 ** to the new memory. ^On second and subsequent calls to
4360 ** sqlite3_aggregate_context() for the same aggregate function instance,
4361 ** the same buffer is returned.  Sqlite3_aggregate_context() is normally
4362 ** called once for each invocation of the xStep callback and then one
4363 ** last time when the xFinal callback is invoked.  ^(When no rows match
4364 ** an aggregate query, the xStep() callback of the aggregate function
4365 ** implementation is never called and xFinal() is called exactly once.
4366 ** In those cases, sqlite3_aggregate_context() might be called for the
4367 ** first time from within xFinal().)^
4368 **
4369 ** ^The sqlite3_aggregate_context(C,N) routine returns a NULL pointer if N is
4370 ** less than or equal to zero or if a memory allocate error occurs.
4371 **
4372 ** ^(The amount of space allocated by sqlite3_aggregate_context(C,N) is
4373 ** determined by the N parameter on first successful call.  Changing the
4374 ** value of N in subsequent call to sqlite3_aggregate_context() within
4375 ** the same aggregate function instance will not resize the memory
4376 ** allocation.)^
4377 **
4378 ** ^SQLite automatically frees the memory allocated by
4379 ** sqlite3_aggregate_context() when the aggregate query concludes.
4380 **
4381 ** The first parameter must be a copy of the
4382 ** [sqlite3_context | SQL function context] that is the first parameter
4383 ** to the xStep or xFinal callback routine that implements the aggregate
4384 ** function.
4385 **
4386 ** This routine must be called from the same thread in which
4387 ** the aggregate SQL function is running.
4388 */
4389 SQLITE_API void *sqlite3_aggregate_context(sqlite3_context*, int nBytes);
4390 
4391 /*
4392 ** CAPI3REF: User Data For Functions
4393 **
4394 ** ^The sqlite3_user_data() interface returns a copy of
4395 ** the pointer that was the pUserData parameter (the 5th parameter)
4396 ** of the [sqlite3_create_function()]
4397 ** and [sqlite3_create_function16()] routines that originally
4398 ** registered the application defined function.
4399 **
4400 ** This routine must be called from the same thread in which
4401 ** the application-defined function is running.
4402 */
4403 SQLITE_API void *sqlite3_user_data(sqlite3_context*);
4404 
4405 /*
4406 ** CAPI3REF: Database Connection For Functions
4407 **
4408 ** ^The sqlite3_context_db_handle() interface returns a copy of
4409 ** the pointer to the [database connection] (the 1st parameter)
4410 ** of the [sqlite3_create_function()]
4411 ** and [sqlite3_create_function16()] routines that originally
4412 ** registered the application defined function.
4413 */
4414 SQLITE_API sqlite3 *sqlite3_context_db_handle(sqlite3_context*);
4415 
4416 /*
4417 ** CAPI3REF: Function Auxiliary Data
4418 **
4419 ** The following two functions may be used by scalar SQL functions to
4420 ** associate metadata with argument values. If the same value is passed to
4421 ** multiple invocations of the same SQL function during query execution, under
4422 ** some circumstances the associated metadata may be preserved. This may
4423 ** be used, for example, to add a regular-expression matching scalar
4424 ** function. The compiled version of the regular expression is stored as
4425 ** metadata associated with the SQL value passed as the regular expression
4426 ** pattern.  The compiled regular expression can be reused on multiple
4427 ** invocations of the same function so that the original pattern string
4428 ** does not need to be recompiled on each invocation.
4429 **
4430 ** ^The sqlite3_get_auxdata() interface returns a pointer to the metadata
4431 ** associated by the sqlite3_set_auxdata() function with the Nth argument
4432 ** value to the application-defined function. ^If no metadata has been ever
4433 ** been set for the Nth argument of the function, or if the corresponding
4434 ** function parameter has changed since the meta-data was set,
4435 ** then sqlite3_get_auxdata() returns a NULL pointer.
4436 **
4437 ** ^The sqlite3_set_auxdata() interface saves the metadata
4438 ** pointed to by its 3rd parameter as the metadata for the N-th
4439 ** argument of the application-defined function.  Subsequent
4440 ** calls to sqlite3_get_auxdata() might return this data, if it has
4441 ** not been destroyed.
4442 ** ^If it is not NULL, SQLite will invoke the destructor
4443 ** function given by the 4th parameter to sqlite3_set_auxdata() on
4444 ** the metadata when the corresponding function parameter changes
4445 ** or when the SQL statement completes, whichever comes first.
4446 **
4447 ** SQLite is free to call the destructor and drop metadata on any
4448 ** parameter of any function at any time.  ^The only guarantee is that
4449 ** the destructor will be called before the metadata is dropped.
4450 **
4451 ** ^(In practice, metadata is preserved between function calls for
4452 ** expressions that are constant at compile time. This includes literal
4453 ** values and [parameters].)^
4454 **
4455 ** These routines must be called from the same thread in which
4456 ** the SQL function is running.
4457 */
4458 SQLITE_API void *sqlite3_get_auxdata(sqlite3_context*, int N);
4459 SQLITE_API void sqlite3_set_auxdata(sqlite3_context*, int N, void*, void (*)(void*));
4460 
4461 
4462 /*
4463 ** CAPI3REF: Constants Defining Special Destructor Behavior
4464 **
4465 ** These are special values for the destructor that is passed in as the
4466 ** final argument to routines like [sqlite3_result_blob()].  ^If the destructor
4467 ** argument is SQLITE_STATIC, it means that the content pointer is constant
4468 ** and will never change.  It does not need to be destroyed.  ^The
4469 ** SQLITE_TRANSIENT value means that the content will likely change in
4470 ** the near future and that SQLite should make its own private copy of
4471 ** the content before returning.
4472 **
4473 ** The typedef is necessary to work around problems in certain
4474 ** C++ compilers.  See ticket #2191.
4475 */
4476 typedef void (*sqlite3_destructor_type)(void*);
4477 #define SQLITE_STATIC      ((sqlite3_destructor_type)0)
4478 #define SQLITE_TRANSIENT   ((sqlite3_destructor_type)-1)
4479 
4480 /*
4481 ** CAPI3REF: Setting The Result Of An SQL Function
4482 **
4483 ** These routines are used by the xFunc or xFinal callbacks that
4484 ** implement SQL functions and aggregates.  See
4485 ** [sqlite3_create_function()] and [sqlite3_create_function16()]
4486 ** for additional information.
4487 **
4488 ** These functions work very much like the [parameter binding] family of
4489 ** functions used to bind values to host parameters in prepared statements.
4490 ** Refer to the [SQL parameter] documentation for additional information.
4491 **
4492 ** ^The sqlite3_result_blob() interface sets the result from
4493 ** an application-defined function to be the BLOB whose content is pointed
4494 ** to by the second parameter and which is N bytes long where N is the
4495 ** third parameter.
4496 **
4497 ** ^The sqlite3_result_zeroblob() interfaces set the result of
4498 ** the application-defined function to be a BLOB containing all zero
4499 ** bytes and N bytes in size, where N is the value of the 2nd parameter.
4500 **
4501 ** ^The sqlite3_result_double() interface sets the result from
4502 ** an application-defined function to be a floating point value specified
4503 ** by its 2nd argument.
4504 **
4505 ** ^The sqlite3_result_error() and sqlite3_result_error16() functions
4506 ** cause the implemented SQL function to throw an exception.
4507 ** ^SQLite uses the string pointed to by the
4508 ** 2nd parameter of sqlite3_result_error() or sqlite3_result_error16()
4509 ** as the text of an error message.  ^SQLite interprets the error
4510 ** message string from sqlite3_result_error() as UTF-8. ^SQLite
4511 ** interprets the string from sqlite3_result_error16() as UTF-16 in native
4512 ** byte order.  ^If the third parameter to sqlite3_result_error()
4513 ** or sqlite3_result_error16() is negative then SQLite takes as the error
4514 ** message all text up through the first zero character.
4515 ** ^If the third parameter to sqlite3_result_error() or
4516 ** sqlite3_result_error16() is non-negative then SQLite takes that many
4517 ** bytes (not characters) from the 2nd parameter as the error message.
4518 ** ^The sqlite3_result_error() and sqlite3_result_error16()
4519 ** routines make a private copy of the error message text before
4520 ** they return.  Hence, the calling function can deallocate or
4521 ** modify the text after they return without harm.
4522 ** ^The sqlite3_result_error_code() function changes the error code
4523 ** returned by SQLite as a result of an error in a function.  ^By default,
4524 ** the error code is SQLITE_ERROR.  ^A subsequent call to sqlite3_result_error()
4525 ** or sqlite3_result_error16() resets the error code to SQLITE_ERROR.
4526 **
4527 ** ^The sqlite3_result_toobig() interface causes SQLite to throw an error
4528 ** indicating that a string or BLOB is too long to represent.
4529 **
4530 ** ^The sqlite3_result_nomem() interface causes SQLite to throw an error
4531 ** indicating that a memory allocation failed.
4532 **
4533 ** ^The sqlite3_result_int() interface sets the return value
4534 ** of the application-defined function to be the 32-bit signed integer
4535 ** value given in the 2nd argument.
4536 ** ^The sqlite3_result_int64() interface sets the return value
4537 ** of the application-defined function to be the 64-bit signed integer
4538 ** value given in the 2nd argument.
4539 **
4540 ** ^The sqlite3_result_null() interface sets the return value
4541 ** of the application-defined function to be NULL.
4542 **
4543 ** ^The sqlite3_result_text(), sqlite3_result_text16(),
4544 ** sqlite3_result_text16le(), and sqlite3_result_text16be() interfaces
4545 ** set the return value of the application-defined function to be
4546 ** a text string which is represented as UTF-8, UTF-16 native byte order,
4547 ** UTF-16 little endian, or UTF-16 big endian, respectively.
4548 ** ^SQLite takes the text result from the application from
4549 ** the 2nd parameter of the sqlite3_result_text* interfaces.
4550 ** ^If the 3rd parameter to the sqlite3_result_text* interfaces
4551 ** is negative, then SQLite takes result text from the 2nd parameter
4552 ** through the first zero character.
4553 ** ^If the 3rd parameter to the sqlite3_result_text* interfaces
4554 ** is non-negative, then as many bytes (not characters) of the text
4555 ** pointed to by the 2nd parameter are taken as the application-defined
4556 ** function result.
4557 ** ^If the 4th parameter to the sqlite3_result_text* interfaces
4558 ** or sqlite3_result_blob is a non-NULL pointer, then SQLite calls that
4559 ** function as the destructor on the text or BLOB result when it has
4560 ** finished using that result.
4561 ** ^If the 4th parameter to the sqlite3_result_text* interfaces or to
4562 ** sqlite3_result_blob is the special constant SQLITE_STATIC, then SQLite
4563 ** assumes that the text or BLOB result is in constant space and does not
4564 ** copy the content of the parameter nor call a destructor on the content
4565 ** when it has finished using that result.
4566 ** ^If the 4th parameter to the sqlite3_result_text* interfaces
4567 ** or sqlite3_result_blob is the special constant SQLITE_TRANSIENT
4568 ** then SQLite makes a copy of the result into space obtained from
4569 ** from [sqlite3_malloc()] before it returns.
4570 **
4571 ** ^The sqlite3_result_value() interface sets the result of
4572 ** the application-defined function to be a copy the
4573 ** [unprotected sqlite3_value] object specified by the 2nd parameter.  ^The
4574 ** sqlite3_result_value() interface makes a copy of the [sqlite3_value]
4575 ** so that the [sqlite3_value] specified in the parameter may change or
4576 ** be deallocated after sqlite3_result_value() returns without harm.
4577 ** ^A [protected sqlite3_value] object may always be used where an
4578 ** [unprotected sqlite3_value] object is required, so either
4579 ** kind of [sqlite3_value] object can be used with this interface.
4580 **
4581 ** If these routines are called from within the different thread
4582 ** than the one containing the application-defined function that received
4583 ** the [sqlite3_context] pointer, the results are undefined.
4584 */
4585 SQLITE_API void sqlite3_result_blob(sqlite3_context*, const void*, int, void(*)(void*));
4586 SQLITE_API void sqlite3_result_double(sqlite3_context*, double);
4587 SQLITE_API void sqlite3_result_error(sqlite3_context*, const char*, int);
4588 SQLITE_API void sqlite3_result_error16(sqlite3_context*, const void*, int);
4589 SQLITE_API void sqlite3_result_error_toobig(sqlite3_context*);
4590 SQLITE_API void sqlite3_result_error_nomem(sqlite3_context*);
4591 SQLITE_API void sqlite3_result_error_code(sqlite3_context*, int);
4592 SQLITE_API void sqlite3_result_int(sqlite3_context*, int);
4593 SQLITE_API void sqlite3_result_int64(sqlite3_context*, sqlite3_int64);
4594 SQLITE_API void sqlite3_result_null(sqlite3_context*);
4595 SQLITE_API void sqlite3_result_text(sqlite3_context*, const char*, int, void(*)(void*));
4596 SQLITE_API void sqlite3_result_text16(sqlite3_context*, const void*, int, void(*)(void*));
4597 SQLITE_API void sqlite3_result_text16le(sqlite3_context*, const void*, int,void(*)(void*));
4598 SQLITE_API void sqlite3_result_text16be(sqlite3_context*, const void*, int,void(*)(void*));
4599 SQLITE_API void sqlite3_result_value(sqlite3_context*, sqlite3_value*);
4600 SQLITE_API void sqlite3_result_zeroblob(sqlite3_context*, int n);
4601 
4602 /*
4603 ** CAPI3REF: Define New Collating Sequences
4604 **
4605 ** ^These functions add, remove, or modify a [collation] associated
4606 ** with the [database connection] specified as the first argument.
4607 **
4608 ** ^The name of the collation is a UTF-8 string
4609 ** for sqlite3_create_collation() and sqlite3_create_collation_v2()
4610 ** and a UTF-16 string in native byte order for sqlite3_create_collation16().
4611 ** ^Collation names that compare equal according to [sqlite3_strnicmp()] are
4612 ** considered to be the same name.
4613 **
4614 ** ^(The third argument (eTextRep) must be one of the constants:
4615 ** <ul>
4616 ** <li> [SQLITE_UTF8],
4617 ** <li> [SQLITE_UTF16LE],
4618 ** <li> [SQLITE_UTF16BE],
4619 ** <li> [SQLITE_UTF16], or
4620 ** <li> [SQLITE_UTF16_ALIGNED].
4621 ** </ul>)^
4622 ** ^The eTextRep argument determines the encoding of strings passed
4623 ** to the collating function callback, xCallback.
4624 ** ^The [SQLITE_UTF16] and [SQLITE_UTF16_ALIGNED] values for eTextRep
4625 ** force strings to be UTF16 with native byte order.
4626 ** ^The [SQLITE_UTF16_ALIGNED] value for eTextRep forces strings to begin
4627 ** on an even byte address.
4628 **
4629 ** ^The fourth argument, pArg, is an application data pointer that is passed
4630 ** through as the first argument to the collating function callback.
4631 **
4632 ** ^The fifth argument, xCallback, is a pointer to the collating function.
4633 ** ^Multiple collating functions can be registered using the same name but
4634 ** with different eTextRep parameters and SQLite will use whichever
4635 ** function requires the least amount of data transformation.
4636 ** ^If the xCallback argument is NULL then the collating function is
4637 ** deleted.  ^When all collating functions having the same name are deleted,
4638 ** that collation is no longer usable.
4639 **
4640 ** ^The collating function callback is invoked with a copy of the pArg
4641 ** application data pointer and with two strings in the encoding specified
4642 ** by the eTextRep argument.  The collating function must return an
4643 ** integer that is negative, zero, or positive
4644 ** if the first string is less than, equal to, or greater than the second,
4645 ** respectively.  A collating function must always return the same answer
4646 ** given the same inputs.  If two or more collating functions are registered
4647 ** to the same collation name (using different eTextRep values) then all
4648 ** must give an equivalent answer when invoked with equivalent strings.
4649 ** The collating function must obey the following properties for all
4650 ** strings A, B, and C:
4651 **
4652 ** <ol>
4653 ** <li> If A==B then B==A.
4654 ** <li> If A==B and B==C then A==C.
4655 ** <li> If A&lt;B THEN B&gt;A.
4656 ** <li> If A&lt;B and B&lt;C then A&lt;C.
4657 ** </ol>
4658 **
4659 ** If a collating function fails any of the above constraints and that
4660 ** collating function is  registered and used, then the behavior of SQLite
4661 ** is undefined.
4662 **
4663 ** ^The sqlite3_create_collation_v2() works like sqlite3_create_collation()
4664 ** with the addition that the xDestroy callback is invoked on pArg when
4665 ** the collating function is deleted.
4666 ** ^Collating functions are deleted when they are overridden by later
4667 ** calls to the collation creation functions or when the
4668 ** [database connection] is closed using [sqlite3_close()].
4669 **
4670 ** ^The xDestroy callback is <u>not</u> called if the
4671 ** sqlite3_create_collation_v2() function fails.  Applications that invoke
4672 ** sqlite3_create_collation_v2() with a non-NULL xDestroy argument should
4673 ** check the return code and dispose of the application data pointer
4674 ** themselves rather than expecting SQLite to deal with it for them.
4675 ** This is different from every other SQLite interface.  The inconsistency
4676 ** is unfortunate but cannot be changed without breaking backwards
4677 ** compatibility.
4678 **
4679 ** See also:  [sqlite3_collation_needed()] and [sqlite3_collation_needed16()].
4680 */
4681 SQLITE_API int sqlite3_create_collation(
4682   sqlite3*,
4683   const char *zName,
4684   int eTextRep,
4685   void *pArg,
4686   int(*xCompare)(void*,int,const void*,int,const void*)
4687 );
4688 SQLITE_API int sqlite3_create_collation_v2(
4689   sqlite3*,
4690   const char *zName,
4691   int eTextRep,
4692   void *pArg,
4693   int(*xCompare)(void*,int,const void*,int,const void*),
4694   void(*xDestroy)(void*)
4695 );
4696 SQLITE_API int sqlite3_create_collation16(
4697   sqlite3*,
4698   const void *zName,
4699   int eTextRep,
4700   void *pArg,
4701   int(*xCompare)(void*,int,const void*,int,const void*)
4702 );
4703 
4704 /*
4705 ** CAPI3REF: Collation Needed Callbacks
4706 **
4707 ** ^To avoid having to register all collation sequences before a database
4708 ** can be used, a single callback function may be registered with the
4709 ** [database connection] to be invoked whenever an undefined collation
4710 ** sequence is required.
4711 **
4712 ** ^If the function is registered using the sqlite3_collation_needed() API,
4713 ** then it is passed the names of undefined collation sequences as strings
4714 ** encoded in UTF-8. ^If sqlite3_collation_needed16() is used,
4715 ** the names are passed as UTF-16 in machine native byte order.
4716 ** ^A call to either function replaces the existing collation-needed callback.
4717 **
4718 ** ^(When the callback is invoked, the first argument passed is a copy
4719 ** of the second argument to sqlite3_collation_needed() or
4720 ** sqlite3_collation_needed16().  The second argument is the database
4721 ** connection.  The third argument is one of [SQLITE_UTF8], [SQLITE_UTF16BE],
4722 ** or [SQLITE_UTF16LE], indicating the most desirable form of the collation
4723 ** sequence function required.  The fourth parameter is the name of the
4724 ** required collation sequence.)^
4725 **
4726 ** The callback function should register the desired collation using
4727 ** [sqlite3_create_collation()], [sqlite3_create_collation16()], or
4728 ** [sqlite3_create_collation_v2()].
4729 */
4730 SQLITE_API int sqlite3_collation_needed(
4731   sqlite3*,
4732   void*,
4733   void(*)(void*,sqlite3*,int eTextRep,const char*)
4734 );
4735 SQLITE_API int sqlite3_collation_needed16(
4736   sqlite3*,
4737   void*,
4738   void(*)(void*,sqlite3*,int eTextRep,const void*)
4739 );
4740 
4741 #ifdef SQLITE_HAS_CODEC
4742 /*
4743 ** Specify the key for an encrypted database.  This routine should be
4744 ** called right after sqlite3_open().
4745 **
4746 ** The code to implement this API is not available in the public release
4747 ** of SQLite.
4748 */
4749 SQLITE_API int sqlite3_key(
4750   sqlite3 *db,                   /* Database to be rekeyed */
4751   const void *pKey, int nKey     /* The key */
4752 );
4753 
4754 /*
4755 ** Change the key on an open database.  If the current database is not
4756 ** encrypted, this routine will encrypt it.  If pNew==0 or nNew==0, the
4757 ** database is decrypted.
4758 **
4759 ** The code to implement this API is not available in the public release
4760 ** of SQLite.
4761 */
4762 SQLITE_API int sqlite3_rekey(
4763   sqlite3 *db,                   /* Database to be rekeyed */
4764   const void *pKey, int nKey     /* The new key */
4765 );
4766 
4767 /*
4768 ** Specify the activation key for a SEE database.  Unless
4769 ** activated, none of the SEE routines will work.
4770 */
4771 SQLITE_API void sqlite3_activate_see(
4772   const char *zPassPhrase        /* Activation phrase */
4773 );
4774 #endif
4775 
4776 #ifdef SQLITE_ENABLE_CEROD
4777 /*
4778 ** Specify the activation key for a CEROD database.  Unless
4779 ** activated, none of the CEROD routines will work.
4780 */
4781 SQLITE_API void sqlite3_activate_cerod(
4782   const char *zPassPhrase        /* Activation phrase */
4783 );
4784 #endif
4785 
4786 /*
4787 ** CAPI3REF: Suspend Execution For A Short Time
4788 **
4789 ** The sqlite3_sleep() function causes the current thread to suspend execution
4790 ** for at least a number of milliseconds specified in its parameter.
4791 **
4792 ** If the operating system does not support sleep requests with
4793 ** millisecond time resolution, then the time will be rounded up to
4794 ** the nearest second. The number of milliseconds of sleep actually
4795 ** requested from the operating system is returned.
4796 **
4797 ** ^SQLite implements this interface by calling the xSleep()
4798 ** method of the default [sqlite3_vfs] object.  If the xSleep() method
4799 ** of the default VFS is not implemented correctly, or not implemented at
4800 ** all, then the behavior of sqlite3_sleep() may deviate from the description
4801 ** in the previous paragraphs.
4802 */
4803 SQLITE_API int sqlite3_sleep(int);
4804 
4805 /*
4806 ** CAPI3REF: Name Of The Folder Holding Temporary Files
4807 **
4808 ** ^(If this global variable is made to point to a string which is
4809 ** the name of a folder (a.k.a. directory), then all temporary files
4810 ** created by SQLite when using a built-in [sqlite3_vfs | VFS]
4811 ** will be placed in that directory.)^  ^If this variable
4812 ** is a NULL pointer, then SQLite performs a search for an appropriate
4813 ** temporary file directory.
4814 **
4815 ** It is not safe to read or modify this variable in more than one
4816 ** thread at a time.  It is not safe to read or modify this variable
4817 ** if a [database connection] is being used at the same time in a separate
4818 ** thread.
4819 ** It is intended that this variable be set once
4820 ** as part of process initialization and before any SQLite interface
4821 ** routines have been called and that this variable remain unchanged
4822 ** thereafter.
4823 **
4824 ** ^The [temp_store_directory pragma] may modify this variable and cause
4825 ** it to point to memory obtained from [sqlite3_malloc].  ^Furthermore,
4826 ** the [temp_store_directory pragma] always assumes that any string
4827 ** that this variable points to is held in memory obtained from
4828 ** [sqlite3_malloc] and the pragma may attempt to free that memory
4829 ** using [sqlite3_free].
4830 ** Hence, if this variable is modified directly, either it should be
4831 ** made NULL or made to point to memory obtained from [sqlite3_malloc]
4832 ** or else the use of the [temp_store_directory pragma] should be avoided.
4833 */
4834 SQLITE_API char *sqlite3_temp_directory;
4835 
4836 /*
4837 ** CAPI3REF: Test For Auto-Commit Mode
4838 ** KEYWORDS: {autocommit mode}
4839 **
4840 ** ^The sqlite3_get_autocommit() interface returns non-zero or
4841 ** zero if the given database connection is or is not in autocommit mode,
4842 ** respectively.  ^Autocommit mode is on by default.
4843 ** ^Autocommit mode is disabled by a [BEGIN] statement.
4844 ** ^Autocommit mode is re-enabled by a [COMMIT] or [ROLLBACK].
4845 **
4846 ** If certain kinds of errors occur on a statement within a multi-statement
4847 ** transaction (errors including [SQLITE_FULL], [SQLITE_IOERR],
4848 ** [SQLITE_NOMEM], [SQLITE_BUSY], and [SQLITE_INTERRUPT]) then the
4849 ** transaction might be rolled back automatically.  The only way to
4850 ** find out whether SQLite automatically rolled back the transaction after
4851 ** an error is to use this function.
4852 **
4853 ** If another thread changes the autocommit status of the database
4854 ** connection while this routine is running, then the return value
4855 ** is undefined.
4856 */
4857 SQLITE_API int sqlite3_get_autocommit(sqlite3*);
4858 
4859 /*
4860 ** CAPI3REF: Find The Database Handle Of A Prepared Statement
4861 **
4862 ** ^The sqlite3_db_handle interface returns the [database connection] handle
4863 ** to which a [prepared statement] belongs.  ^The [database connection]
4864 ** returned by sqlite3_db_handle is the same [database connection]
4865 ** that was the first argument
4866 ** to the [sqlite3_prepare_v2()] call (or its variants) that was used to
4867 ** create the statement in the first place.
4868 */
4869 SQLITE_API sqlite3 *sqlite3_db_handle(sqlite3_stmt*);
4870 
4871 /*
4872 ** CAPI3REF: Find the next prepared statement
4873 **
4874 ** ^This interface returns a pointer to the next [prepared statement] after
4875 ** pStmt associated with the [database connection] pDb.  ^If pStmt is NULL
4876 ** then this interface returns a pointer to the first prepared statement
4877 ** associated with the database connection pDb.  ^If no prepared statement
4878 ** satisfies the conditions of this routine, it returns NULL.
4879 **
4880 ** The [database connection] pointer D in a call to
4881 ** [sqlite3_next_stmt(D,S)] must refer to an open database
4882 ** connection and in particular must not be a NULL pointer.
4883 */
4884 SQLITE_API sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt);
4885 
4886 /*
4887 ** CAPI3REF: Commit And Rollback Notification Callbacks
4888 **
4889 ** ^The sqlite3_commit_hook() interface registers a callback
4890 ** function to be invoked whenever a transaction is [COMMIT | committed].
4891 ** ^Any callback set by a previous call to sqlite3_commit_hook()
4892 ** for the same database connection is overridden.
4893 ** ^The sqlite3_rollback_hook() interface registers a callback
4894 ** function to be invoked whenever a transaction is [ROLLBACK | rolled back].
4895 ** ^Any callback set by a previous call to sqlite3_rollback_hook()
4896 ** for the same database connection is overridden.
4897 ** ^The pArg argument is passed through to the callback.
4898 ** ^If the callback on a commit hook function returns non-zero,
4899 ** then the commit is converted into a rollback.
4900 **
4901 ** ^The sqlite3_commit_hook(D,C,P) and sqlite3_rollback_hook(D,C,P) functions
4902 ** return the P argument from the previous call of the same function
4903 ** on the same [database connection] D, or NULL for
4904 ** the first call for each function on D.
4905 **
4906 ** The callback implementation must not do anything that will modify
4907 ** the database connection that invoked the callback.  Any actions
4908 ** to modify the database connection must be deferred until after the
4909 ** completion of the [sqlite3_step()] call that triggered the commit
4910 ** or rollback hook in the first place.
4911 ** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their
4912 ** database connections for the meaning of "modify" in this paragraph.
4913 **
4914 ** ^Registering a NULL function disables the callback.
4915 **
4916 ** ^When the commit hook callback routine returns zero, the [COMMIT]
4917 ** operation is allowed to continue normally.  ^If the commit hook
4918 ** returns non-zero, then the [COMMIT] is converted into a [ROLLBACK].
4919 ** ^The rollback hook is invoked on a rollback that results from a commit
4920 ** hook returning non-zero, just as it would be with any other rollback.
4921 **
4922 ** ^For the purposes of this API, a transaction is said to have been
4923 ** rolled back if an explicit "ROLLBACK" statement is executed, or
4924 ** an error or constraint causes an implicit rollback to occur.
4925 ** ^The rollback callback is not invoked if a transaction is
4926 ** automatically rolled back because the database connection is closed.
4927 **
4928 ** See also the [sqlite3_update_hook()] interface.
4929 */
4930 SQLITE_API void *sqlite3_commit_hook(sqlite3*, int(*)(void*), void*);
4931 SQLITE_API void *sqlite3_rollback_hook(sqlite3*, void(*)(void *), void*);
4932 
4933 /*
4934 ** CAPI3REF: Data Change Notification Callbacks
4935 **
4936 ** ^The sqlite3_update_hook() interface registers a callback function
4937 ** with the [database connection] identified by the first argument
4938 ** to be invoked whenever a row is updated, inserted or deleted.
4939 ** ^Any callback set by a previous call to this function
4940 ** for the same database connection is overridden.
4941 **
4942 ** ^The second argument is a pointer to the function to invoke when a
4943 ** row is updated, inserted or deleted.
4944 ** ^The first argument to the callback is a copy of the third argument
4945 ** to sqlite3_update_hook().
4946 ** ^The second callback argument is one of [SQLITE_INSERT], [SQLITE_DELETE],
4947 ** or [SQLITE_UPDATE], depending on the operation that caused the callback
4948 ** to be invoked.
4949 ** ^The third and fourth arguments to the callback contain pointers to the
4950 ** database and table name containing the affected row.
4951 ** ^The final callback parameter is the [rowid] of the row.
4952 ** ^In the case of an update, this is the [rowid] after the update takes place.
4953 **
4954 ** ^(The update hook is not invoked when internal system tables are
4955 ** modified (i.e. sqlite_master and sqlite_sequence).)^
4956 **
4957 ** ^In the current implementation, the update hook
4958 ** is not invoked when duplication rows are deleted because of an
4959 ** [ON CONFLICT | ON CONFLICT REPLACE] clause.  ^Nor is the update hook
4960 ** invoked when rows are deleted using the [truncate optimization].
4961 ** The exceptions defined in this paragraph might change in a future
4962 ** release of SQLite.
4963 **
4964 ** The update hook implementation must not do anything that will modify
4965 ** the database connection that invoked the update hook.  Any actions
4966 ** to modify the database connection must be deferred until after the
4967 ** completion of the [sqlite3_step()] call that triggered the update hook.
4968 ** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their
4969 ** database connections for the meaning of "modify" in this paragraph.
4970 **
4971 ** ^The sqlite3_update_hook(D,C,P) function
4972 ** returns the P argument from the previous call
4973 ** on the same [database connection] D, or NULL for
4974 ** the first call on D.
4975 **
4976 ** See also the [sqlite3_commit_hook()] and [sqlite3_rollback_hook()]
4977 ** interfaces.
4978 */
4979 SQLITE_API void *sqlite3_update_hook(
4980   sqlite3*,
4981   void(*)(void *,int ,char const *,char const *,sqlite3_int64),
4982   void*
4983 );
4984 
4985 /*
4986 ** CAPI3REF: Enable Or Disable Shared Pager Cache
4987 ** KEYWORDS: {shared cache}
4988 **
4989 ** ^(This routine enables or disables the sharing of the database cache
4990 ** and schema data structures between [database connection | connections]
4991 ** to the same database. Sharing is enabled if the argument is true
4992 ** and disabled if the argument is false.)^
4993 **
4994 ** ^Cache sharing is enabled and disabled for an entire process.
4995 ** This is a change as of SQLite version 3.5.0. In prior versions of SQLite,
4996 ** sharing was enabled or disabled for each thread separately.
4997 **
4998 ** ^(The cache sharing mode set by this interface effects all subsequent
4999 ** calls to [sqlite3_open()], [sqlite3_open_v2()], and [sqlite3_open16()].
5000 ** Existing database connections continue use the sharing mode
5001 ** that was in effect at the time they were opened.)^
5002 **
5003 ** ^(This routine returns [SQLITE_OK] if shared cache was enabled or disabled
5004 ** successfully.  An [error code] is returned otherwise.)^
5005 **
5006 ** ^Shared cache is disabled by default. But this might change in
5007 ** future releases of SQLite.  Applications that care about shared
5008 ** cache setting should set it explicitly.
5009 **
5010 ** See Also:  [SQLite Shared-Cache Mode]
5011 */
5012 SQLITE_API int sqlite3_enable_shared_cache(int);
5013 
5014 /*
5015 ** CAPI3REF: Attempt To Free Heap Memory
5016 **
5017 ** ^The sqlite3_release_memory() interface attempts to free N bytes
5018 ** of heap memory by deallocating non-essential memory allocations
5019 ** held by the database library.   Memory used to cache database
5020 ** pages to improve performance is an example of non-essential memory.
5021 ** ^sqlite3_release_memory() returns the number of bytes actually freed,
5022 ** which might be more or less than the amount requested.
5023 ** ^The sqlite3_release_memory() routine is a no-op returning zero
5024 ** if SQLite is not compiled with [SQLITE_ENABLE_MEMORY_MANAGEMENT].
5025 */
5026 SQLITE_API int sqlite3_release_memory(int);
5027 
5028 /*
5029 ** CAPI3REF: Impose A Limit On Heap Size
5030 **
5031 ** ^The sqlite3_soft_heap_limit64() interface sets and/or queries the
5032 ** soft limit on the amount of heap memory that may be allocated by SQLite.
5033 ** ^SQLite strives to keep heap memory utilization below the soft heap
5034 ** limit by reducing the number of pages held in the page cache
5035 ** as heap memory usages approaches the limit.
5036 ** ^The soft heap limit is "soft" because even though SQLite strives to stay
5037 ** below the limit, it will exceed the limit rather than generate
5038 ** an [SQLITE_NOMEM] error.  In other words, the soft heap limit
5039 ** is advisory only.
5040 **
5041 ** ^The return value from sqlite3_soft_heap_limit64() is the size of
5042 ** the soft heap limit prior to the call.  ^If the argument N is negative
5043 ** then no change is made to the soft heap limit.  Hence, the current
5044 ** size of the soft heap limit can be determined by invoking
5045 ** sqlite3_soft_heap_limit64() with a negative argument.
5046 **
5047 ** ^If the argument N is zero then the soft heap limit is disabled.
5048 **
5049 ** ^(The soft heap limit is not enforced in the current implementation
5050 ** if one or more of following conditions are true:
5051 **
5052 ** <ul>
5053 ** <li> The soft heap limit is set to zero.
5054 ** <li> Memory accounting is disabled using a combination of the
5055 **      [sqlite3_config]([SQLITE_CONFIG_MEMSTATUS],...) start-time option and
5056 **      the [SQLITE_DEFAULT_MEMSTATUS] compile-time option.
5057 ** <li> An alternative page cache implementation is specified using
5058 **      [sqlite3_config]([SQLITE_CONFIG_PCACHE],...).
5059 ** <li> The page cache allocates from its own memory pool supplied
5060 **      by [sqlite3_config]([SQLITE_CONFIG_PAGECACHE],...) rather than
5061 **      from the heap.
5062 ** </ul>)^
5063 **
5064 ** Beginning with SQLite version 3.7.3, the soft heap limit is enforced
5065 ** regardless of whether or not the [SQLITE_ENABLE_MEMORY_MANAGEMENT]
5066 ** compile-time option is invoked.  With [SQLITE_ENABLE_MEMORY_MANAGEMENT],
5067 ** the soft heap limit is enforced on every memory allocation.  Without
5068 ** [SQLITE_ENABLE_MEMORY_MANAGEMENT], the soft heap limit is only enforced
5069 ** when memory is allocated by the page cache.  Testing suggests that because
5070 ** the page cache is the predominate memory user in SQLite, most
5071 ** applications will achieve adequate soft heap limit enforcement without
5072 ** the use of [SQLITE_ENABLE_MEMORY_MANAGEMENT].
5073 **
5074 ** The circumstances under which SQLite will enforce the soft heap limit may
5075 ** changes in future releases of SQLite.
5076 */
5077 SQLITE_API sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 N);
5078 
5079 /*
5080 ** CAPI3REF: Deprecated Soft Heap Limit Interface
5081 ** DEPRECATED
5082 **
5083 ** This is a deprecated version of the [sqlite3_soft_heap_limit64()]
5084 ** interface.  This routine is provided for historical compatibility
5085 ** only.  All new applications should use the
5086 ** [sqlite3_soft_heap_limit64()] interface rather than this one.
5087 */
5088 SQLITE_API SQLITE_DEPRECATED void sqlite3_soft_heap_limit(int N);
5089 
5090 
5091 /*
5092 ** CAPI3REF: Extract Metadata About A Column Of A Table
5093 **
5094 ** ^This routine returns metadata about a specific column of a specific
5095 ** database table accessible using the [database connection] handle
5096 ** passed as the first function argument.
5097 **
5098 ** ^The column is identified by the second, third and fourth parameters to
5099 ** this function. ^The second parameter is either the name of the database
5100 ** (i.e. "main", "temp", or an attached database) containing the specified
5101 ** table or NULL. ^If it is NULL, then all attached databases are searched
5102 ** for the table using the same algorithm used by the database engine to
5103 ** resolve unqualified table references.
5104 **
5105 ** ^The third and fourth parameters to this function are the table and column
5106 ** name of the desired column, respectively. Neither of these parameters
5107 ** may be NULL.
5108 **
5109 ** ^Metadata is returned by writing to the memory locations passed as the 5th
5110 ** and subsequent parameters to this function. ^Any of these arguments may be
5111 ** NULL, in which case the corresponding element of metadata is omitted.
5112 **
5113 ** ^(<blockquote>
5114 ** <table border="1">
5115 ** <tr><th> Parameter <th> Output<br>Type <th>  Description
5116 **
5117 ** <tr><td> 5th <td> const char* <td> Data type
5118 ** <tr><td> 6th <td> const char* <td> Name of default collation sequence
5119 ** <tr><td> 7th <td> int         <td> True if column has a NOT NULL constraint
5120 ** <tr><td> 8th <td> int         <td> True if column is part of the PRIMARY KEY
5121 ** <tr><td> 9th <td> int         <td> True if column is [AUTOINCREMENT]
5122 ** </table>
5123 ** </blockquote>)^
5124 **
5125 ** ^The memory pointed to by the character pointers returned for the
5126 ** declaration type and collation sequence is valid only until the next
5127 ** call to any SQLite API function.
5128 **
5129 ** ^If the specified table is actually a view, an [error code] is returned.
5130 **
5131 ** ^If the specified column is "rowid", "oid" or "_rowid_" and an
5132 ** [INTEGER PRIMARY KEY] column has been explicitly declared, then the output
5133 ** parameters are set for the explicitly declared column. ^(If there is no
5134 ** explicitly declared [INTEGER PRIMARY KEY] column, then the output
5135 ** parameters are set as follows:
5136 **
5137 ** <pre>
5138 **     data type: "INTEGER"
5139 **     collation sequence: "BINARY"
5140 **     not null: 0
5141 **     primary key: 1
5142 **     auto increment: 0
5143 ** </pre>)^
5144 **
5145 ** ^(This function may load one or more schemas from database files. If an
5146 ** error occurs during this process, or if the requested table or column
5147 ** cannot be found, an [error code] is returned and an error message left
5148 ** in the [database connection] (to be retrieved using sqlite3_errmsg()).)^
5149 **
5150 ** ^This API is only available if the library was compiled with the
5151 ** [SQLITE_ENABLE_COLUMN_METADATA] C-preprocessor symbol defined.
5152 */
5153 SQLITE_API int sqlite3_table_column_metadata(
5154   sqlite3 *db,                /* Connection handle */
5155   const char *zDbName,        /* Database name or NULL */
5156   const char *zTableName,     /* Table name */
5157   const char *zColumnName,    /* Column name */
5158   char const **pzDataType,    /* OUTPUT: Declared data type */
5159   char const **pzCollSeq,     /* OUTPUT: Collation sequence name */
5160   int *pNotNull,              /* OUTPUT: True if NOT NULL constraint exists */
5161   int *pPrimaryKey,           /* OUTPUT: True if column part of PK */
5162   int *pAutoinc               /* OUTPUT: True if column is auto-increment */
5163 );
5164 
5165 /*
5166 ** CAPI3REF: Load An Extension
5167 **
5168 ** ^This interface loads an SQLite extension library from the named file.
5169 **
5170 ** ^The sqlite3_load_extension() interface attempts to load an
5171 ** SQLite extension library contained in the file zFile.
5172 **
5173 ** ^The entry point is zProc.
5174 ** ^zProc may be 0, in which case the name of the entry point
5175 ** defaults to "sqlite3_extension_init".
5176 ** ^The sqlite3_load_extension() interface returns
5177 ** [SQLITE_OK] on success and [SQLITE_ERROR] if something goes wrong.
5178 ** ^If an error occurs and pzErrMsg is not 0, then the
5179 ** [sqlite3_load_extension()] interface shall attempt to
5180 ** fill *pzErrMsg with error message text stored in memory
5181 ** obtained from [sqlite3_malloc()]. The calling function
5182 ** should free this memory by calling [sqlite3_free()].
5183 **
5184 ** ^Extension loading must be enabled using
5185 ** [sqlite3_enable_load_extension()] prior to calling this API,
5186 ** otherwise an error will be returned.
5187 **
5188 ** See also the [load_extension() SQL function].
5189 */
5190 SQLITE_API int sqlite3_load_extension(
5191   sqlite3 *db,          /* Load the extension into this database connection */
5192   const char *zFile,    /* Name of the shared library containing extension */
5193   const char *zProc,    /* Entry point.  Derived from zFile if 0 */
5194   char **pzErrMsg       /* Put error message here if not 0 */
5195 );
5196 
5197 /*
5198 ** CAPI3REF: Enable Or Disable Extension Loading
5199 **
5200 ** ^So as not to open security holes in older applications that are
5201 ** unprepared to deal with extension loading, and as a means of disabling
5202 ** extension loading while evaluating user-entered SQL, the following API
5203 ** is provided to turn the [sqlite3_load_extension()] mechanism on and off.
5204 **
5205 ** ^Extension loading is off by default. See ticket #1863.
5206 ** ^Call the sqlite3_enable_load_extension() routine with onoff==1
5207 ** to turn extension loading on and call it with onoff==0 to turn
5208 ** it back off again.
5209 */
5210 SQLITE_API int sqlite3_enable_load_extension(sqlite3 *db, int onoff);
5211 
5212 /*
5213 ** CAPI3REF: Automatically Load Statically Linked Extensions
5214 **
5215 ** ^This interface causes the xEntryPoint() function to be invoked for
5216 ** each new [database connection] that is created.  The idea here is that
5217 ** xEntryPoint() is the entry point for a statically linked SQLite extension
5218 ** that is to be automatically loaded into all new database connections.
5219 **
5220 ** ^(Even though the function prototype shows that xEntryPoint() takes
5221 ** no arguments and returns void, SQLite invokes xEntryPoint() with three
5222 ** arguments and expects and integer result as if the signature of the
5223 ** entry point where as follows:
5224 **
5225 ** <blockquote><pre>
5226 ** &nbsp;  int xEntryPoint(
5227 ** &nbsp;    sqlite3 *db,
5228 ** &nbsp;    const char **pzErrMsg,
5229 ** &nbsp;    const struct sqlite3_api_routines *pThunk
5230 ** &nbsp;  );
5231 ** </pre></blockquote>)^
5232 **
5233 ** If the xEntryPoint routine encounters an error, it should make *pzErrMsg
5234 ** point to an appropriate error message (obtained from [sqlite3_mprintf()])
5235 ** and return an appropriate [error code].  ^SQLite ensures that *pzErrMsg
5236 ** is NULL before calling the xEntryPoint().  ^SQLite will invoke
5237 ** [sqlite3_free()] on *pzErrMsg after xEntryPoint() returns.  ^If any
5238 ** xEntryPoint() returns an error, the [sqlite3_open()], [sqlite3_open16()],
5239 ** or [sqlite3_open_v2()] call that provoked the xEntryPoint() will fail.
5240 **
5241 ** ^Calling sqlite3_auto_extension(X) with an entry point X that is already
5242 ** on the list of automatic extensions is a harmless no-op. ^No entry point
5243 ** will be called more than once for each database connection that is opened.
5244 **
5245 ** See also: [sqlite3_reset_auto_extension()].
5246 */
5247 SQLITE_API int sqlite3_auto_extension(void (*xEntryPoint)(void));
5248 
5249 /*
5250 ** CAPI3REF: Reset Automatic Extension Loading
5251 **
5252 ** ^This interface disables all automatic extensions previously
5253 ** registered using [sqlite3_auto_extension()].
5254 */
5255 SQLITE_API void sqlite3_reset_auto_extension(void);
5256 
5257 /*
5258 ** The interface to the virtual-table mechanism is currently considered
5259 ** to be experimental.  The interface might change in incompatible ways.
5260 ** If this is a problem for you, do not use the interface at this time.
5261 **
5262 ** When the virtual-table mechanism stabilizes, we will declare the
5263 ** interface fixed, support it indefinitely, and remove this comment.
5264 */
5265 
5266 /*
5267 ** Structures used by the virtual table interface
5268 */
5269 typedef struct sqlite3_vtab sqlite3_vtab;
5270 typedef struct sqlite3_index_info sqlite3_index_info;
5271 typedef struct sqlite3_vtab_cursor sqlite3_vtab_cursor;
5272 typedef struct sqlite3_module sqlite3_module;
5273 
5274 /*
5275 ** CAPI3REF: Virtual Table Object
5276 ** KEYWORDS: sqlite3_module {virtual table module}
5277 **
5278 ** This structure, sometimes called a "virtual table module",
5279 ** defines the implementation of a [virtual tables].
5280 ** This structure consists mostly of methods for the module.
5281 **
5282 ** ^A virtual table module is created by filling in a persistent
5283 ** instance of this structure and passing a pointer to that instance
5284 ** to [sqlite3_create_module()] or [sqlite3_create_module_v2()].
5285 ** ^The registration remains valid until it is replaced by a different
5286 ** module or until the [database connection] closes.  The content
5287 ** of this structure must not change while it is registered with
5288 ** any database connection.
5289 */
5290 struct sqlite3_module {
5291   int iVersion;
5292   int (*xCreate)(sqlite3*, void *pAux,
5293                int argc, const char *const*argv,
5294                sqlite3_vtab **ppVTab, char**);
5295   int (*xConnect)(sqlite3*, void *pAux,
5296                int argc, const char *const*argv,
5297                sqlite3_vtab **ppVTab, char**);
5298   int (*xBestIndex)(sqlite3_vtab *pVTab, sqlite3_index_info*);
5299   int (*xDisconnect)(sqlite3_vtab *pVTab);
5300   int (*xDestroy)(sqlite3_vtab *pVTab);
5301   int (*xOpen)(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor);
5302   int (*xClose)(sqlite3_vtab_cursor*);
5303   int (*xFilter)(sqlite3_vtab_cursor*, int idxNum, const char *idxStr,
5304                 int argc, sqlite3_value **argv);
5305   int (*xNext)(sqlite3_vtab_cursor*);
5306   int (*xEof)(sqlite3_vtab_cursor*);
5307   int (*xColumn)(sqlite3_vtab_cursor*, sqlite3_context*, int);
5308   int (*xRowid)(sqlite3_vtab_cursor*, sqlite3_int64 *pRowid);
5309   int (*xUpdate)(sqlite3_vtab *, int, sqlite3_value **, sqlite3_int64 *);
5310   int (*xBegin)(sqlite3_vtab *pVTab);
5311   int (*xSync)(sqlite3_vtab *pVTab);
5312   int (*xCommit)(sqlite3_vtab *pVTab);
5313   int (*xRollback)(sqlite3_vtab *pVTab);
5314   int (*xFindFunction)(sqlite3_vtab *pVtab, int nArg, const char *zName,
5315                        void (**pxFunc)(sqlite3_context*,int,sqlite3_value**),
5316                        void **ppArg);
5317   int (*xRename)(sqlite3_vtab *pVtab, const char *zNew);
5318   /* The methods above are in version 1 of the sqlite_module object. Those
5319   ** below are for version 2 and greater. */
5320   int (*xSavepoint)(sqlite3_vtab *pVTab, int);
5321   int (*xRelease)(sqlite3_vtab *pVTab, int);
5322   int (*xRollbackTo)(sqlite3_vtab *pVTab, int);
5323 };
5324 
5325 /*
5326 ** CAPI3REF: Virtual Table Indexing Information
5327 ** KEYWORDS: sqlite3_index_info
5328 **
5329 ** The sqlite3_index_info structure and its substructures is used as part
5330 ** of the [virtual table] interface to
5331 ** pass information into and receive the reply from the [xBestIndex]
5332 ** method of a [virtual table module].  The fields under **Inputs** are the
5333 ** inputs to xBestIndex and are read-only.  xBestIndex inserts its
5334 ** results into the **Outputs** fields.
5335 **
5336 ** ^(The aConstraint[] array records WHERE clause constraints of the form:
5337 **
5338 ** <blockquote>column OP expr</blockquote>
5339 **
5340 ** where OP is =, &lt;, &lt;=, &gt;, or &gt;=.)^  ^(The particular operator is
5341 ** stored in aConstraint[].op using one of the
5342 ** [SQLITE_INDEX_CONSTRAINT_EQ | SQLITE_INDEX_CONSTRAINT_ values].)^
5343 ** ^(The index of the column is stored in
5344 ** aConstraint[].iColumn.)^  ^(aConstraint[].usable is TRUE if the
5345 ** expr on the right-hand side can be evaluated (and thus the constraint
5346 ** is usable) and false if it cannot.)^
5347 **
5348 ** ^The optimizer automatically inverts terms of the form "expr OP column"
5349 ** and makes other simplifications to the WHERE clause in an attempt to
5350 ** get as many WHERE clause terms into the form shown above as possible.
5351 ** ^The aConstraint[] array only reports WHERE clause terms that are
5352 ** relevant to the particular virtual table being queried.
5353 **
5354 ** ^Information about the ORDER BY clause is stored in aOrderBy[].
5355 ** ^Each term of aOrderBy records a column of the ORDER BY clause.
5356 **
5357 ** The [xBestIndex] method must fill aConstraintUsage[] with information
5358 ** about what parameters to pass to xFilter.  ^If argvIndex>0 then
5359 ** the right-hand side of the corresponding aConstraint[] is evaluated
5360 ** and becomes the argvIndex-th entry in argv.  ^(If aConstraintUsage[].omit
5361 ** is true, then the constraint is assumed to be fully handled by the
5362 ** virtual table and is not checked again by SQLite.)^
5363 **
5364 ** ^The idxNum and idxPtr values are recorded and passed into the
5365 ** [xFilter] method.
5366 ** ^[sqlite3_free()] is used to free idxPtr if and only if
5367 ** needToFreeIdxPtr is true.
5368 **
5369 ** ^The orderByConsumed means that output from [xFilter]/[xNext] will occur in
5370 ** the correct order to satisfy the ORDER BY clause so that no separate
5371 ** sorting step is required.
5372 **
5373 ** ^The estimatedCost value is an estimate of the cost of doing the
5374 ** particular lookup.  A full scan of a table with N entries should have
5375 ** a cost of N.  A binary search of a table of N entries should have a
5376 ** cost of approximately log(N).
5377 */
5378 struct sqlite3_index_info {
5379   /* Inputs */
5380   int nConstraint;           /* Number of entries in aConstraint */
5381   struct sqlite3_index_constraint {
5382      int iColumn;              /* Column on left-hand side of constraint */
5383      unsigned char op;         /* Constraint operator */
5384      unsigned char usable;     /* True if this constraint is usable */
5385      int iTermOffset;          /* Used internally - xBestIndex should ignore */
5386   } *aConstraint;            /* Table of WHERE clause constraints */
5387   int nOrderBy;              /* Number of terms in the ORDER BY clause */
5388   struct sqlite3_index_orderby {
5389      int iColumn;              /* Column number */
5390      unsigned char desc;       /* True for DESC.  False for ASC. */
5391   } *aOrderBy;               /* The ORDER BY clause */
5392   /* Outputs */
5393   struct sqlite3_index_constraint_usage {
5394     int argvIndex;           /* if >0, constraint is part of argv to xFilter */
5395     unsigned char omit;      /* Do not code a test for this constraint */
5396   } *aConstraintUsage;
5397   int idxNum;                /* Number used to identify the index */
5398   char *idxStr;              /* String, possibly obtained from sqlite3_malloc */
5399   int needToFreeIdxStr;      /* Free idxStr using sqlite3_free() if true */
5400   int orderByConsumed;       /* True if output is already ordered */
5401   double estimatedCost;      /* Estimated cost of using this index */
5402 };
5403 
5404 /*
5405 ** CAPI3REF: Virtual Table Constraint Operator Codes
5406 **
5407 ** These macros defined the allowed values for the
5408 ** [sqlite3_index_info].aConstraint[].op field.  Each value represents
5409 ** an operator that is part of a constraint term in the wHERE clause of
5410 ** a query that uses a [virtual table].
5411 */
5412 #define SQLITE_INDEX_CONSTRAINT_EQ    2
5413 #define SQLITE_INDEX_CONSTRAINT_GT    4
5414 #define SQLITE_INDEX_CONSTRAINT_LE    8
5415 #define SQLITE_INDEX_CONSTRAINT_LT    16
5416 #define SQLITE_INDEX_CONSTRAINT_GE    32
5417 #define SQLITE_INDEX_CONSTRAINT_MATCH 64
5418 
5419 /*
5420 ** CAPI3REF: Register A Virtual Table Implementation
5421 **
5422 ** ^These routines are used to register a new [virtual table module] name.
5423 ** ^Module names must be registered before
5424 ** creating a new [virtual table] using the module and before using a
5425 ** preexisting [virtual table] for the module.
5426 **
5427 ** ^The module name is registered on the [database connection] specified
5428 ** by the first parameter.  ^The name of the module is given by the
5429 ** second parameter.  ^The third parameter is a pointer to
5430 ** the implementation of the [virtual table module].   ^The fourth
5431 ** parameter is an arbitrary client data pointer that is passed through
5432 ** into the [xCreate] and [xConnect] methods of the virtual table module
5433 ** when a new virtual table is be being created or reinitialized.
5434 **
5435 ** ^The sqlite3_create_module_v2() interface has a fifth parameter which
5436 ** is a pointer to a destructor for the pClientData.  ^SQLite will
5437 ** invoke the destructor function (if it is not NULL) when SQLite
5438 ** no longer needs the pClientData pointer.  ^The destructor will also
5439 ** be invoked if the call to sqlite3_create_module_v2() fails.
5440 ** ^The sqlite3_create_module()
5441 ** interface is equivalent to sqlite3_create_module_v2() with a NULL
5442 ** destructor.
5443 */
5444 SQLITE_API int sqlite3_create_module(
5445   sqlite3 *db,               /* SQLite connection to register module with */
5446   const char *zName,         /* Name of the module */
5447   const sqlite3_module *p,   /* Methods for the module */
5448   void *pClientData          /* Client data for xCreate/xConnect */
5449 );
5450 SQLITE_API int sqlite3_create_module_v2(
5451   sqlite3 *db,               /* SQLite connection to register module with */
5452   const char *zName,         /* Name of the module */
5453   const sqlite3_module *p,   /* Methods for the module */
5454   void *pClientData,         /* Client data for xCreate/xConnect */
5455   void(*xDestroy)(void*)     /* Module destructor function */
5456 );
5457 
5458 /*
5459 ** CAPI3REF: Virtual Table Instance Object
5460 ** KEYWORDS: sqlite3_vtab
5461 **
5462 ** Every [virtual table module] implementation uses a subclass
5463 ** of this object to describe a particular instance
5464 ** of the [virtual table].  Each subclass will
5465 ** be tailored to the specific needs of the module implementation.
5466 ** The purpose of this superclass is to define certain fields that are
5467 ** common to all module implementations.
5468 **
5469 ** ^Virtual tables methods can set an error message by assigning a
5470 ** string obtained from [sqlite3_mprintf()] to zErrMsg.  The method should
5471 ** take care that any prior string is freed by a call to [sqlite3_free()]
5472 ** prior to assigning a new string to zErrMsg.  ^After the error message
5473 ** is delivered up to the client application, the string will be automatically
5474 ** freed by sqlite3_free() and the zErrMsg field will be zeroed.
5475 */
5476 struct sqlite3_vtab {
5477   const sqlite3_module *pModule;  /* The module for this virtual table */
5478   int nRef;                       /* NO LONGER USED */
5479   char *zErrMsg;                  /* Error message from sqlite3_mprintf() */
5480   /* Virtual table implementations will typically add additional fields */
5481 };
5482 
5483 /*
5484 ** CAPI3REF: Virtual Table Cursor Object
5485 ** KEYWORDS: sqlite3_vtab_cursor {virtual table cursor}
5486 **
5487 ** Every [virtual table module] implementation uses a subclass of the
5488 ** following structure to describe cursors that point into the
5489 ** [virtual table] and are used
5490 ** to loop through the virtual table.  Cursors are created using the
5491 ** [sqlite3_module.xOpen | xOpen] method of the module and are destroyed
5492 ** by the [sqlite3_module.xClose | xClose] method.  Cursors are used
5493 ** by the [xFilter], [xNext], [xEof], [xColumn], and [xRowid] methods
5494 ** of the module.  Each module implementation will define
5495 ** the content of a cursor structure to suit its own needs.
5496 **
5497 ** This superclass exists in order to define fields of the cursor that
5498 ** are common to all implementations.
5499 */
5500 struct sqlite3_vtab_cursor {
5501   sqlite3_vtab *pVtab;      /* Virtual table of this cursor */
5502   /* Virtual table implementations will typically add additional fields */
5503 };
5504 
5505 /*
5506 ** CAPI3REF: Declare The Schema Of A Virtual Table
5507 **
5508 ** ^The [xCreate] and [xConnect] methods of a
5509 ** [virtual table module] call this interface
5510 ** to declare the format (the names and datatypes of the columns) of
5511 ** the virtual tables they implement.
5512 */
5513 SQLITE_API int sqlite3_declare_vtab(sqlite3*, const char *zSQL);
5514 
5515 /*
5516 ** CAPI3REF: Overload A Function For A Virtual Table
5517 **
5518 ** ^(Virtual tables can provide alternative implementations of functions
5519 ** using the [xFindFunction] method of the [virtual table module].
5520 ** But global versions of those functions
5521 ** must exist in order to be overloaded.)^
5522 **
5523 ** ^(This API makes sure a global version of a function with a particular
5524 ** name and number of parameters exists.  If no such function exists
5525 ** before this API is called, a new function is created.)^  ^The implementation
5526 ** of the new function always causes an exception to be thrown.  So
5527 ** the new function is not good for anything by itself.  Its only
5528 ** purpose is to be a placeholder function that can be overloaded
5529 ** by a [virtual table].
5530 */
5531 SQLITE_API int sqlite3_overload_function(sqlite3*, const char *zFuncName, int nArg);
5532 
5533 /*
5534 ** The interface to the virtual-table mechanism defined above (back up
5535 ** to a comment remarkably similar to this one) is currently considered
5536 ** to be experimental.  The interface might change in incompatible ways.
5537 ** If this is a problem for you, do not use the interface at this time.
5538 **
5539 ** When the virtual-table mechanism stabilizes, we will declare the
5540 ** interface fixed, support it indefinitely, and remove this comment.
5541 */
5542 
5543 /*
5544 ** CAPI3REF: A Handle To An Open BLOB
5545 ** KEYWORDS: {BLOB handle} {BLOB handles}
5546 **
5547 ** An instance of this object represents an open BLOB on which
5548 ** [sqlite3_blob_open | incremental BLOB I/O] can be performed.
5549 ** ^Objects of this type are created by [sqlite3_blob_open()]
5550 ** and destroyed by [sqlite3_blob_close()].
5551 ** ^The [sqlite3_blob_read()] and [sqlite3_blob_write()] interfaces
5552 ** can be used to read or write small subsections of the BLOB.
5553 ** ^The [sqlite3_blob_bytes()] interface returns the size of the BLOB in bytes.
5554 */
5555 typedef struct sqlite3_blob sqlite3_blob;
5556 
5557 /*
5558 ** CAPI3REF: Open A BLOB For Incremental I/O
5559 **
5560 ** ^(This interfaces opens a [BLOB handle | handle] to the BLOB located
5561 ** in row iRow, column zColumn, table zTable in database zDb;
5562 ** in other words, the same BLOB that would be selected by:
5563 **
5564 ** <pre>
5565 **     SELECT zColumn FROM zDb.zTable WHERE [rowid] = iRow;
5566 ** </pre>)^
5567 **
5568 ** ^If the flags parameter is non-zero, then the BLOB is opened for read
5569 ** and write access. ^If it is zero, the BLOB is opened for read access.
5570 ** ^It is not possible to open a column that is part of an index or primary
5571 ** key for writing. ^If [foreign key constraints] are enabled, it is
5572 ** not possible to open a column that is part of a [child key] for writing.
5573 **
5574 ** ^Note that the database name is not the filename that contains
5575 ** the database but rather the symbolic name of the database that
5576 ** appears after the AS keyword when the database is connected using [ATTACH].
5577 ** ^For the main database file, the database name is "main".
5578 ** ^For TEMP tables, the database name is "temp".
5579 **
5580 ** ^(On success, [SQLITE_OK] is returned and the new [BLOB handle] is written
5581 ** to *ppBlob. Otherwise an [error code] is returned and *ppBlob is set
5582 ** to be a null pointer.)^
5583 ** ^This function sets the [database connection] error code and message
5584 ** accessible via [sqlite3_errcode()] and [sqlite3_errmsg()] and related
5585 ** functions. ^Note that the *ppBlob variable is always initialized in a
5586 ** way that makes it safe to invoke [sqlite3_blob_close()] on *ppBlob
5587 ** regardless of the success or failure of this routine.
5588 **
5589 ** ^(If the row that a BLOB handle points to is modified by an
5590 ** [UPDATE], [DELETE], or by [ON CONFLICT] side-effects
5591 ** then the BLOB handle is marked as "expired".
5592 ** This is true if any column of the row is changed, even a column
5593 ** other than the one the BLOB handle is open on.)^
5594 ** ^Calls to [sqlite3_blob_read()] and [sqlite3_blob_write()] for
5595 ** an expired BLOB handle fail with a return code of [SQLITE_ABORT].
5596 ** ^(Changes written into a BLOB prior to the BLOB expiring are not
5597 ** rolled back by the expiration of the BLOB.  Such changes will eventually
5598 ** commit if the transaction continues to completion.)^
5599 **
5600 ** ^Use the [sqlite3_blob_bytes()] interface to determine the size of
5601 ** the opened blob.  ^The size of a blob may not be changed by this
5602 ** interface.  Use the [UPDATE] SQL command to change the size of a
5603 ** blob.
5604 **
5605 ** ^The [sqlite3_bind_zeroblob()] and [sqlite3_result_zeroblob()] interfaces
5606 ** and the built-in [zeroblob] SQL function can be used, if desired,
5607 ** to create an empty, zero-filled blob in which to read or write using
5608 ** this interface.
5609 **
5610 ** To avoid a resource leak, every open [BLOB handle] should eventually
5611 ** be released by a call to [sqlite3_blob_close()].
5612 */
5613 SQLITE_API int sqlite3_blob_open(
5614   sqlite3*,
5615   const char *zDb,
5616   const char *zTable,
5617   const char *zColumn,
5618   sqlite3_int64 iRow,
5619   int flags,
5620   sqlite3_blob **ppBlob
5621 );
5622 
5623 /*
5624 ** CAPI3REF: Move a BLOB Handle to a New Row
5625 **
5626 ** ^This function is used to move an existing blob handle so that it points
5627 ** to a different row of the same database table. ^The new row is identified
5628 ** by the rowid value passed as the second argument. Only the row can be
5629 ** changed. ^The database, table and column on which the blob handle is open
5630 ** remain the same. Moving an existing blob handle to a new row can be
5631 ** faster than closing the existing handle and opening a new one.
5632 **
5633 ** ^(The new row must meet the same criteria as for [sqlite3_blob_open()] -
5634 ** it must exist and there must be either a blob or text value stored in
5635 ** the nominated column.)^ ^If the new row is not present in the table, or if
5636 ** it does not contain a blob or text value, or if another error occurs, an
5637 ** SQLite error code is returned and the blob handle is considered aborted.
5638 ** ^All subsequent calls to [sqlite3_blob_read()], [sqlite3_blob_write()] or
5639 ** [sqlite3_blob_reopen()] on an aborted blob handle immediately return
5640 ** SQLITE_ABORT. ^Calling [sqlite3_blob_bytes()] on an aborted blob handle
5641 ** always returns zero.
5642 **
5643 ** ^This function sets the database handle error code and message.
5644 */
5645 SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_blob_reopen(sqlite3_blob *, sqlite3_int64);
5646 
5647 /*
5648 ** CAPI3REF: Close A BLOB Handle
5649 **
5650 ** ^Closes an open [BLOB handle].
5651 **
5652 ** ^Closing a BLOB shall cause the current transaction to commit
5653 ** if there are no other BLOBs, no pending prepared statements, and the
5654 ** database connection is in [autocommit mode].
5655 ** ^If any writes were made to the BLOB, they might be held in cache
5656 ** until the close operation if they will fit.
5657 **
5658 ** ^(Closing the BLOB often forces the changes
5659 ** out to disk and so if any I/O errors occur, they will likely occur
5660 ** at the time when the BLOB is closed.  Any errors that occur during
5661 ** closing are reported as a non-zero return value.)^
5662 **
5663 ** ^(The BLOB is closed unconditionally.  Even if this routine returns
5664 ** an error code, the BLOB is still closed.)^
5665 **
5666 ** ^Calling this routine with a null pointer (such as would be returned
5667 ** by a failed call to [sqlite3_blob_open()]) is a harmless no-op.
5668 */
5669 SQLITE_API int sqlite3_blob_close(sqlite3_blob *);
5670 
5671 /*
5672 ** CAPI3REF: Return The Size Of An Open BLOB
5673 **
5674 ** ^Returns the size in bytes of the BLOB accessible via the
5675 ** successfully opened [BLOB handle] in its only argument.  ^The
5676 ** incremental blob I/O routines can only read or overwriting existing
5677 ** blob content; they cannot change the size of a blob.
5678 **
5679 ** This routine only works on a [BLOB handle] which has been created
5680 ** by a prior successful call to [sqlite3_blob_open()] and which has not
5681 ** been closed by [sqlite3_blob_close()].  Passing any other pointer in
5682 ** to this routine results in undefined and probably undesirable behavior.
5683 */
5684 SQLITE_API int sqlite3_blob_bytes(sqlite3_blob *);
5685 
5686 /*
5687 ** CAPI3REF: Read Data From A BLOB Incrementally
5688 **
5689 ** ^(This function is used to read data from an open [BLOB handle] into a
5690 ** caller-supplied buffer. N bytes of data are copied into buffer Z
5691 ** from the open BLOB, starting at offset iOffset.)^
5692 **
5693 ** ^If offset iOffset is less than N bytes from the end of the BLOB,
5694 ** [SQLITE_ERROR] is returned and no data is read.  ^If N or iOffset is
5695 ** less than zero, [SQLITE_ERROR] is returned and no data is read.
5696 ** ^The size of the blob (and hence the maximum value of N+iOffset)
5697 ** can be determined using the [sqlite3_blob_bytes()] interface.
5698 **
5699 ** ^An attempt to read from an expired [BLOB handle] fails with an
5700 ** error code of [SQLITE_ABORT].
5701 **
5702 ** ^(On success, sqlite3_blob_read() returns SQLITE_OK.
5703 ** Otherwise, an [error code] or an [extended error code] is returned.)^
5704 **
5705 ** This routine only works on a [BLOB handle] which has been created
5706 ** by a prior successful call to [sqlite3_blob_open()] and which has not
5707 ** been closed by [sqlite3_blob_close()].  Passing any other pointer in
5708 ** to this routine results in undefined and probably undesirable behavior.
5709 **
5710 ** See also: [sqlite3_blob_write()].
5711 */
5712 SQLITE_API int sqlite3_blob_read(sqlite3_blob *, void *Z, int N, int iOffset);
5713 
5714 /*
5715 ** CAPI3REF: Write Data Into A BLOB Incrementally
5716 **
5717 ** ^This function is used to write data into an open [BLOB handle] from a
5718 ** caller-supplied buffer. ^N bytes of data are copied from the buffer Z
5719 ** into the open BLOB, starting at offset iOffset.
5720 **
5721 ** ^If the [BLOB handle] passed as the first argument was not opened for
5722 ** writing (the flags parameter to [sqlite3_blob_open()] was zero),
5723 ** this function returns [SQLITE_READONLY].
5724 **
5725 ** ^This function may only modify the contents of the BLOB; it is
5726 ** not possible to increase the size of a BLOB using this API.
5727 ** ^If offset iOffset is less than N bytes from the end of the BLOB,
5728 ** [SQLITE_ERROR] is returned and no data is written.  ^If N is
5729 ** less than zero [SQLITE_ERROR] is returned and no data is written.
5730 ** The size of the BLOB (and hence the maximum value of N+iOffset)
5731 ** can be determined using the [sqlite3_blob_bytes()] interface.
5732 **
5733 ** ^An attempt to write to an expired [BLOB handle] fails with an
5734 ** error code of [SQLITE_ABORT].  ^Writes to the BLOB that occurred
5735 ** before the [BLOB handle] expired are not rolled back by the
5736 ** expiration of the handle, though of course those changes might
5737 ** have been overwritten by the statement that expired the BLOB handle
5738 ** or by other independent statements.
5739 **
5740 ** ^(On success, sqlite3_blob_write() returns SQLITE_OK.
5741 ** Otherwise, an  [error code] or an [extended error code] is returned.)^
5742 **
5743 ** This routine only works on a [BLOB handle] which has been created
5744 ** by a prior successful call to [sqlite3_blob_open()] and which has not
5745 ** been closed by [sqlite3_blob_close()].  Passing any other pointer in
5746 ** to this routine results in undefined and probably undesirable behavior.
5747 **
5748 ** See also: [sqlite3_blob_read()].
5749 */
5750 SQLITE_API int sqlite3_blob_write(sqlite3_blob *, const void *z, int n, int iOffset);
5751 
5752 /*
5753 ** CAPI3REF: Virtual File System Objects
5754 **
5755 ** A virtual filesystem (VFS) is an [sqlite3_vfs] object
5756 ** that SQLite uses to interact
5757 ** with the underlying operating system.  Most SQLite builds come with a
5758 ** single default VFS that is appropriate for the host computer.
5759 ** New VFSes can be registered and existing VFSes can be unregistered.
5760 ** The following interfaces are provided.
5761 **
5762 ** ^The sqlite3_vfs_find() interface returns a pointer to a VFS given its name.
5763 ** ^Names are case sensitive.
5764 ** ^Names are zero-terminated UTF-8 strings.
5765 ** ^If there is no match, a NULL pointer is returned.
5766 ** ^If zVfsName is NULL then the default VFS is returned.
5767 **
5768 ** ^New VFSes are registered with sqlite3_vfs_register().
5769 ** ^Each new VFS becomes the default VFS if the makeDflt flag is set.
5770 ** ^The same VFS can be registered multiple times without injury.
5771 ** ^To make an existing VFS into the default VFS, register it again
5772 ** with the makeDflt flag set.  If two different VFSes with the
5773 ** same name are registered, the behavior is undefined.  If a
5774 ** VFS is registered with a name that is NULL or an empty string,
5775 ** then the behavior is undefined.
5776 **
5777 ** ^Unregister a VFS with the sqlite3_vfs_unregister() interface.
5778 ** ^(If the default VFS is unregistered, another VFS is chosen as
5779 ** the default.  The choice for the new VFS is arbitrary.)^
5780 */
5781 SQLITE_API sqlite3_vfs *sqlite3_vfs_find(const char *zVfsName);
5782 SQLITE_API int sqlite3_vfs_register(sqlite3_vfs*, int makeDflt);
5783 SQLITE_API int sqlite3_vfs_unregister(sqlite3_vfs*);
5784 
5785 /*
5786 ** CAPI3REF: Mutexes
5787 **
5788 ** The SQLite core uses these routines for thread
5789 ** synchronization. Though they are intended for internal
5790 ** use by SQLite, code that links against SQLite is
5791 ** permitted to use any of these routines.
5792 **
5793 ** The SQLite source code contains multiple implementations
5794 ** of these mutex routines.  An appropriate implementation
5795 ** is selected automatically at compile-time.  ^(The following
5796 ** implementations are available in the SQLite core:
5797 **
5798 ** <ul>
5799 ** <li>   SQLITE_MUTEX_OS2
5800 ** <li>   SQLITE_MUTEX_PTHREAD
5801 ** <li>   SQLITE_MUTEX_W32
5802 ** <li>   SQLITE_MUTEX_NOOP
5803 ** </ul>)^
5804 **
5805 ** ^The SQLITE_MUTEX_NOOP implementation is a set of routines
5806 ** that does no real locking and is appropriate for use in
5807 ** a single-threaded application.  ^The SQLITE_MUTEX_OS2,
5808 ** SQLITE_MUTEX_PTHREAD, and SQLITE_MUTEX_W32 implementations
5809 ** are appropriate for use on OS/2, Unix, and Windows.
5810 **
5811 ** ^(If SQLite is compiled with the SQLITE_MUTEX_APPDEF preprocessor
5812 ** macro defined (with "-DSQLITE_MUTEX_APPDEF=1"), then no mutex
5813 ** implementation is included with the library. In this case the
5814 ** application must supply a custom mutex implementation using the
5815 ** [SQLITE_CONFIG_MUTEX] option of the sqlite3_config() function
5816 ** before calling sqlite3_initialize() or any other public sqlite3_
5817 ** function that calls sqlite3_initialize().)^
5818 **
5819 ** ^The sqlite3_mutex_alloc() routine allocates a new
5820 ** mutex and returns a pointer to it. ^If it returns NULL
5821 ** that means that a mutex could not be allocated.  ^SQLite
5822 ** will unwind its stack and return an error.  ^(The argument
5823 ** to sqlite3_mutex_alloc() is one of these integer constants:
5824 **
5825 ** <ul>
5826 ** <li>  SQLITE_MUTEX_FAST
5827 ** <li>  SQLITE_MUTEX_RECURSIVE
5828 ** <li>  SQLITE_MUTEX_STATIC_MASTER
5829 ** <li>  SQLITE_MUTEX_STATIC_MEM
5830 ** <li>  SQLITE_MUTEX_STATIC_MEM2
5831 ** <li>  SQLITE_MUTEX_STATIC_PRNG
5832 ** <li>  SQLITE_MUTEX_STATIC_LRU
5833 ** <li>  SQLITE_MUTEX_STATIC_LRU2
5834 ** </ul>)^
5835 **
5836 ** ^The first two constants (SQLITE_MUTEX_FAST and SQLITE_MUTEX_RECURSIVE)
5837 ** cause sqlite3_mutex_alloc() to create
5838 ** a new mutex.  ^The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
5839 ** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
5840 ** The mutex implementation does not need to make a distinction
5841 ** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
5842 ** not want to.  ^SQLite will only request a recursive mutex in
5843 ** cases where it really needs one.  ^If a faster non-recursive mutex
5844 ** implementation is available on the host platform, the mutex subsystem
5845 ** might return such a mutex in response to SQLITE_MUTEX_FAST.
5846 **
5847 ** ^The other allowed parameters to sqlite3_mutex_alloc() (anything other
5848 ** than SQLITE_MUTEX_FAST and SQLITE_MUTEX_RECURSIVE) each return
5849 ** a pointer to a static preexisting mutex.  ^Six static mutexes are
5850 ** used by the current version of SQLite.  Future versions of SQLite
5851 ** may add additional static mutexes.  Static mutexes are for internal
5852 ** use by SQLite only.  Applications that use SQLite mutexes should
5853 ** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
5854 ** SQLITE_MUTEX_RECURSIVE.
5855 **
5856 ** ^Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
5857 ** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
5858 ** returns a different mutex on every call.  ^But for the static
5859 ** mutex types, the same mutex is returned on every call that has
5860 ** the same type number.
5861 **
5862 ** ^The sqlite3_mutex_free() routine deallocates a previously
5863 ** allocated dynamic mutex.  ^SQLite is careful to deallocate every
5864 ** dynamic mutex that it allocates.  The dynamic mutexes must not be in
5865 ** use when they are deallocated.  Attempting to deallocate a static
5866 ** mutex results in undefined behavior.  ^SQLite never deallocates
5867 ** a static mutex.
5868 **
5869 ** ^The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
5870 ** to enter a mutex.  ^If another thread is already within the mutex,
5871 ** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
5872 ** SQLITE_BUSY.  ^The sqlite3_mutex_try() interface returns [SQLITE_OK]
5873 ** upon successful entry.  ^(Mutexes created using
5874 ** SQLITE_MUTEX_RECURSIVE can be entered multiple times by the same thread.
5875 ** In such cases the,
5876 ** mutex must be exited an equal number of times before another thread
5877 ** can enter.)^  ^(If the same thread tries to enter any other
5878 ** kind of mutex more than once, the behavior is undefined.
5879 ** SQLite will never exhibit
5880 ** such behavior in its own use of mutexes.)^
5881 **
5882 ** ^(Some systems (for example, Windows 95) do not support the operation
5883 ** implemented by sqlite3_mutex_try().  On those systems, sqlite3_mutex_try()
5884 ** will always return SQLITE_BUSY.  The SQLite core only ever uses
5885 ** sqlite3_mutex_try() as an optimization so this is acceptable behavior.)^
5886 **
5887 ** ^The sqlite3_mutex_leave() routine exits a mutex that was
5888 ** previously entered by the same thread.   ^(The behavior
5889 ** is undefined if the mutex is not currently entered by the
5890 ** calling thread or is not currently allocated.  SQLite will
5891 ** never do either.)^
5892 **
5893 ** ^If the argument to sqlite3_mutex_enter(), sqlite3_mutex_try(), or
5894 ** sqlite3_mutex_leave() is a NULL pointer, then all three routines
5895 ** behave as no-ops.
5896 **
5897 ** See also: [sqlite3_mutex_held()] and [sqlite3_mutex_notheld()].
5898 */
5899 SQLITE_API sqlite3_mutex *sqlite3_mutex_alloc(int);
5900 SQLITE_API void sqlite3_mutex_free(sqlite3_mutex*);
5901 SQLITE_API void sqlite3_mutex_enter(sqlite3_mutex*);
5902 SQLITE_API int sqlite3_mutex_try(sqlite3_mutex*);
5903 SQLITE_API void sqlite3_mutex_leave(sqlite3_mutex*);
5904 
5905 /*
5906 ** CAPI3REF: Mutex Methods Object
5907 **
5908 ** An instance of this structure defines the low-level routines
5909 ** used to allocate and use mutexes.
5910 **
5911 ** Usually, the default mutex implementations provided by SQLite are
5912 ** sufficient, however the user has the option of substituting a custom
5913 ** implementation for specialized deployments or systems for which SQLite
5914 ** does not provide a suitable implementation. In this case, the user
5915 ** creates and populates an instance of this structure to pass
5916 ** to sqlite3_config() along with the [SQLITE_CONFIG_MUTEX] option.
5917 ** Additionally, an instance of this structure can be used as an
5918 ** output variable when querying the system for the current mutex
5919 ** implementation, using the [SQLITE_CONFIG_GETMUTEX] option.
5920 **
5921 ** ^The xMutexInit method defined by this structure is invoked as
5922 ** part of system initialization by the sqlite3_initialize() function.
5923 ** ^The xMutexInit routine is called by SQLite exactly once for each
5924 ** effective call to [sqlite3_initialize()].
5925 **
5926 ** ^The xMutexEnd method defined by this structure is invoked as
5927 ** part of system shutdown by the sqlite3_shutdown() function. The
5928 ** implementation of this method is expected to release all outstanding
5929 ** resources obtained by the mutex methods implementation, especially
5930 ** those obtained by the xMutexInit method.  ^The xMutexEnd()
5931 ** interface is invoked exactly once for each call to [sqlite3_shutdown()].
5932 **
5933 ** ^(The remaining seven methods defined by this structure (xMutexAlloc,
5934 ** xMutexFree, xMutexEnter, xMutexTry, xMutexLeave, xMutexHeld and
5935 ** xMutexNotheld) implement the following interfaces (respectively):
5936 **
5937 ** <ul>
5938 **   <li>  [sqlite3_mutex_alloc()] </li>
5939 **   <li>  [sqlite3_mutex_free()] </li>
5940 **   <li>  [sqlite3_mutex_enter()] </li>
5941 **   <li>  [sqlite3_mutex_try()] </li>
5942 **   <li>  [sqlite3_mutex_leave()] </li>
5943 **   <li>  [sqlite3_mutex_held()] </li>
5944 **   <li>  [sqlite3_mutex_notheld()] </li>
5945 ** </ul>)^
5946 **
5947 ** The only difference is that the public sqlite3_XXX functions enumerated
5948 ** above silently ignore any invocations that pass a NULL pointer instead
5949 ** of a valid mutex handle. The implementations of the methods defined
5950 ** by this structure are not required to handle this case, the results
5951 ** of passing a NULL pointer instead of a valid mutex handle are undefined
5952 ** (i.e. it is acceptable to provide an implementation that segfaults if
5953 ** it is passed a NULL pointer).
5954 **
5955 ** The xMutexInit() method must be threadsafe.  ^It must be harmless to
5956 ** invoke xMutexInit() multiple times within the same process and without
5957 ** intervening calls to xMutexEnd().  Second and subsequent calls to
5958 ** xMutexInit() must be no-ops.
5959 **
5960 ** ^xMutexInit() must not use SQLite memory allocation ([sqlite3_malloc()]
5961 ** and its associates).  ^Similarly, xMutexAlloc() must not use SQLite memory
5962 ** allocation for a static mutex.  ^However xMutexAlloc() may use SQLite
5963 ** memory allocation for a fast or recursive mutex.
5964 **
5965 ** ^SQLite will invoke the xMutexEnd() method when [sqlite3_shutdown()] is
5966 ** called, but only if the prior call to xMutexInit returned SQLITE_OK.
5967 ** If xMutexInit fails in any way, it is expected to clean up after itself
5968 ** prior to returning.
5969 */
5970 typedef struct sqlite3_mutex_methods sqlite3_mutex_methods;
5971 struct sqlite3_mutex_methods {
5972   int (*xMutexInit)(void);
5973   int (*xMutexEnd)(void);
5974   sqlite3_mutex *(*xMutexAlloc)(int);
5975   void (*xMutexFree)(sqlite3_mutex *);
5976   void (*xMutexEnter)(sqlite3_mutex *);
5977   int (*xMutexTry)(sqlite3_mutex *);
5978   void (*xMutexLeave)(sqlite3_mutex *);
5979   int (*xMutexHeld)(sqlite3_mutex *);
5980   int (*xMutexNotheld)(sqlite3_mutex *);
5981 };
5982 
5983 /*
5984 ** CAPI3REF: Mutex Verification Routines
5985 **
5986 ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routines
5987 ** are intended for use inside assert() statements.  ^The SQLite core
5988 ** never uses these routines except inside an assert() and applications
5989 ** are advised to follow the lead of the core.  ^The SQLite core only
5990 ** provides implementations for these routines when it is compiled
5991 ** with the SQLITE_DEBUG flag.  ^External mutex implementations
5992 ** are only required to provide these routines if SQLITE_DEBUG is
5993 ** defined and if NDEBUG is not defined.
5994 **
5995 ** ^These routines should return true if the mutex in their argument
5996 ** is held or not held, respectively, by the calling thread.
5997 **
5998 ** ^The implementation is not required to provided versions of these
5999 ** routines that actually work. If the implementation does not provide working
6000 ** versions of these routines, it should at least provide stubs that always
6001 ** return true so that one does not get spurious assertion failures.
6002 **
6003 ** ^If the argument to sqlite3_mutex_held() is a NULL pointer then
6004 ** the routine should return 1.   This seems counter-intuitive since
6005 ** clearly the mutex cannot be held if it does not exist.  But
6006 ** the reason the mutex does not exist is because the build is not
6007 ** using mutexes.  And we do not want the assert() containing the
6008 ** call to sqlite3_mutex_held() to fail, so a non-zero return is
6009 ** the appropriate thing to do.  ^The sqlite3_mutex_notheld()
6010 ** interface should also return 1 when given a NULL pointer.
6011 */
6012 #ifndef NDEBUG
6013 SQLITE_API int sqlite3_mutex_held(sqlite3_mutex*);
6014 SQLITE_API int sqlite3_mutex_notheld(sqlite3_mutex*);
6015 #endif
6016 
6017 /*
6018 ** CAPI3REF: Mutex Types
6019 **
6020 ** The [sqlite3_mutex_alloc()] interface takes a single argument
6021 ** which is one of these integer constants.
6022 **
6023 ** The set of static mutexes may change from one SQLite release to the
6024 ** next.  Applications that override the built-in mutex logic must be
6025 ** prepared to accommodate additional static mutexes.
6026 */
6027 #define SQLITE_MUTEX_FAST             0
6028 #define SQLITE_MUTEX_RECURSIVE        1
6029 #define SQLITE_MUTEX_STATIC_MASTER    2
6030 #define SQLITE_MUTEX_STATIC_MEM       3  /* sqlite3_malloc() */
6031 #define SQLITE_MUTEX_STATIC_MEM2      4  /* NOT USED */
6032 #define SQLITE_MUTEX_STATIC_OPEN      4  /* sqlite3BtreeOpen() */
6033 #define SQLITE_MUTEX_STATIC_PRNG      5  /* sqlite3_random() */
6034 #define SQLITE_MUTEX_STATIC_LRU       6  /* lru page list */
6035 #define SQLITE_MUTEX_STATIC_LRU2      7  /* NOT USED */
6036 #define SQLITE_MUTEX_STATIC_PMEM      7  /* sqlite3PageMalloc() */
6037 
6038 /*
6039 ** CAPI3REF: Retrieve the mutex for a database connection
6040 **
6041 ** ^This interface returns a pointer the [sqlite3_mutex] object that
6042 ** serializes access to the [database connection] given in the argument
6043 ** when the [threading mode] is Serialized.
6044 ** ^If the [threading mode] is Single-thread or Multi-thread then this
6045 ** routine returns a NULL pointer.
6046 */
6047 SQLITE_API sqlite3_mutex *sqlite3_db_mutex(sqlite3*);
6048 
6049 /*
6050 ** CAPI3REF: Low-Level Control Of Database Files
6051 **
6052 ** ^The [sqlite3_file_control()] interface makes a direct call to the
6053 ** xFileControl method for the [sqlite3_io_methods] object associated
6054 ** with a particular database identified by the second argument. ^The
6055 ** name of the database is "main" for the main database or "temp" for the
6056 ** TEMP database, or the name that appears after the AS keyword for
6057 ** databases that are added using the [ATTACH] SQL command.
6058 ** ^A NULL pointer can be used in place of "main" to refer to the
6059 ** main database file.
6060 ** ^The third and fourth parameters to this routine
6061 ** are passed directly through to the second and third parameters of
6062 ** the xFileControl method.  ^The return value of the xFileControl
6063 ** method becomes the return value of this routine.
6064 **
6065 ** ^The SQLITE_FCNTL_FILE_POINTER value for the op parameter causes
6066 ** a pointer to the underlying [sqlite3_file] object to be written into
6067 ** the space pointed to by the 4th parameter.  ^The SQLITE_FCNTL_FILE_POINTER
6068 ** case is a short-circuit path which does not actually invoke the
6069 ** underlying sqlite3_io_methods.xFileControl method.
6070 **
6071 ** ^If the second parameter (zDbName) does not match the name of any
6072 ** open database file, then SQLITE_ERROR is returned.  ^This error
6073 ** code is not remembered and will not be recalled by [sqlite3_errcode()]
6074 ** or [sqlite3_errmsg()].  The underlying xFileControl method might
6075 ** also return SQLITE_ERROR.  There is no way to distinguish between
6076 ** an incorrect zDbName and an SQLITE_ERROR return from the underlying
6077 ** xFileControl method.
6078 **
6079 ** See also: [SQLITE_FCNTL_LOCKSTATE]
6080 */
6081 SQLITE_API int sqlite3_file_control(sqlite3*, const char *zDbName, int op, void*);
6082 
6083 /*
6084 ** CAPI3REF: Testing Interface
6085 **
6086 ** ^The sqlite3_test_control() interface is used to read out internal
6087 ** state of SQLite and to inject faults into SQLite for testing
6088 ** purposes.  ^The first parameter is an operation code that determines
6089 ** the number, meaning, and operation of all subsequent parameters.
6090 **
6091 ** This interface is not for use by applications.  It exists solely
6092 ** for verifying the correct operation of the SQLite library.  Depending
6093 ** on how the SQLite library is compiled, this interface might not exist.
6094 **
6095 ** The details of the operation codes, their meanings, the parameters
6096 ** they take, and what they do are all subject to change without notice.
6097 ** Unlike most of the SQLite API, this function is not guaranteed to
6098 ** operate consistently from one release to the next.
6099 */
6100 SQLITE_API int sqlite3_test_control(int op, ...);
6101 
6102 /*
6103 ** CAPI3REF: Testing Interface Operation Codes
6104 **
6105 ** These constants are the valid operation code parameters used
6106 ** as the first argument to [sqlite3_test_control()].
6107 **
6108 ** These parameters and their meanings are subject to change
6109 ** without notice.  These values are for testing purposes only.
6110 ** Applications should not use any of these parameters or the
6111 ** [sqlite3_test_control()] interface.
6112 */
6113 #define SQLITE_TESTCTRL_FIRST                    5
6114 #define SQLITE_TESTCTRL_PRNG_SAVE                5
6115 #define SQLITE_TESTCTRL_PRNG_RESTORE             6
6116 #define SQLITE_TESTCTRL_PRNG_RESET               7
6117 #define SQLITE_TESTCTRL_BITVEC_TEST              8
6118 #define SQLITE_TESTCTRL_FAULT_INSTALL            9
6119 #define SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS     10
6120 #define SQLITE_TESTCTRL_PENDING_BYTE            11
6121 #define SQLITE_TESTCTRL_ASSERT                  12
6122 #define SQLITE_TESTCTRL_ALWAYS                  13
6123 #define SQLITE_TESTCTRL_RESERVE                 14
6124 #define SQLITE_TESTCTRL_OPTIMIZATIONS           15
6125 #define SQLITE_TESTCTRL_ISKEYWORD               16
6126 #define SQLITE_TESTCTRL_PGHDRSZ                 17
6127 #define SQLITE_TESTCTRL_SCRATCHMALLOC           18
6128 #define SQLITE_TESTCTRL_LOCALTIME_FAULT         19
6129 #define SQLITE_TESTCTRL_LAST                    19
6130 
6131 /*
6132 ** CAPI3REF: SQLite Runtime Status
6133 **
6134 ** ^This interface is used to retrieve runtime status information
6135 ** about the performance of SQLite, and optionally to reset various
6136 ** highwater marks.  ^The first argument is an integer code for
6137 ** the specific parameter to measure.  ^(Recognized integer codes
6138 ** are of the form [status parameters | SQLITE_STATUS_...].)^
6139 ** ^The current value of the parameter is returned into *pCurrent.
6140 ** ^The highest recorded value is returned in *pHighwater.  ^If the
6141 ** resetFlag is true, then the highest record value is reset after
6142 ** *pHighwater is written.  ^(Some parameters do not record the highest
6143 ** value.  For those parameters
6144 ** nothing is written into *pHighwater and the resetFlag is ignored.)^
6145 ** ^(Other parameters record only the highwater mark and not the current
6146 ** value.  For these latter parameters nothing is written into *pCurrent.)^
6147 **
6148 ** ^The sqlite3_status() routine returns SQLITE_OK on success and a
6149 ** non-zero [error code] on failure.
6150 **
6151 ** This routine is threadsafe but is not atomic.  This routine can be
6152 ** called while other threads are running the same or different SQLite
6153 ** interfaces.  However the values returned in *pCurrent and
6154 ** *pHighwater reflect the status of SQLite at different points in time
6155 ** and it is possible that another thread might change the parameter
6156 ** in between the times when *pCurrent and *pHighwater are written.
6157 **
6158 ** See also: [sqlite3_db_status()]
6159 */
6160 SQLITE_API int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag);
6161 
6162 
6163 /*
6164 ** CAPI3REF: Status Parameters
6165 ** KEYWORDS: {status parameters}
6166 **
6167 ** These integer constants designate various run-time status parameters
6168 ** that can be returned by [sqlite3_status()].
6169 **
6170 ** <dl>
6171 ** [[SQLITE_STATUS_MEMORY_USED]] ^(<dt>SQLITE_STATUS_MEMORY_USED</dt>
6172 ** <dd>This parameter is the current amount of memory checked out
6173 ** using [sqlite3_malloc()], either directly or indirectly.  The
6174 ** figure includes calls made to [sqlite3_malloc()] by the application
6175 ** and internal memory usage by the SQLite library.  Scratch memory
6176 ** controlled by [SQLITE_CONFIG_SCRATCH] and auxiliary page-cache
6177 ** memory controlled by [SQLITE_CONFIG_PAGECACHE] is not included in
6178 ** this parameter.  The amount returned is the sum of the allocation
6179 ** sizes as reported by the xSize method in [sqlite3_mem_methods].</dd>)^
6180 **
6181 ** [[SQLITE_STATUS_MALLOC_SIZE]] ^(<dt>SQLITE_STATUS_MALLOC_SIZE</dt>
6182 ** <dd>This parameter records the largest memory allocation request
6183 ** handed to [sqlite3_malloc()] or [sqlite3_realloc()] (or their
6184 ** internal equivalents).  Only the value returned in the
6185 ** *pHighwater parameter to [sqlite3_status()] is of interest.
6186 ** The value written into the *pCurrent parameter is undefined.</dd>)^
6187 **
6188 ** [[SQLITE_STATUS_MALLOC_COUNT]] ^(<dt>SQLITE_STATUS_MALLOC_COUNT</dt>
6189 ** <dd>This parameter records the number of separate memory allocations
6190 ** currently checked out.</dd>)^
6191 **
6192 ** [[SQLITE_STATUS_PAGECACHE_USED]] ^(<dt>SQLITE_STATUS_PAGECACHE_USED</dt>
6193 ** <dd>This parameter returns the number of pages used out of the
6194 ** [pagecache memory allocator] that was configured using
6195 ** [SQLITE_CONFIG_PAGECACHE].  The
6196 ** value returned is in pages, not in bytes.</dd>)^
6197 **
6198 ** [[SQLITE_STATUS_PAGECACHE_OVERFLOW]]
6199 ** ^(<dt>SQLITE_STATUS_PAGECACHE_OVERFLOW</dt>
6200 ** <dd>This parameter returns the number of bytes of page cache
6201 ** allocation which could not be satisfied by the [SQLITE_CONFIG_PAGECACHE]
6202 ** buffer and where forced to overflow to [sqlite3_malloc()].  The
6203 ** returned value includes allocations that overflowed because they
6204 ** where too large (they were larger than the "sz" parameter to
6205 ** [SQLITE_CONFIG_PAGECACHE]) and allocations that overflowed because
6206 ** no space was left in the page cache.</dd>)^
6207 **
6208 ** [[SQLITE_STATUS_PAGECACHE_SIZE]] ^(<dt>SQLITE_STATUS_PAGECACHE_SIZE</dt>
6209 ** <dd>This parameter records the largest memory allocation request
6210 ** handed to [pagecache memory allocator].  Only the value returned in the
6211 ** *pHighwater parameter to [sqlite3_status()] is of interest.
6212 ** The value written into the *pCurrent parameter is undefined.</dd>)^
6213 **
6214 ** [[SQLITE_STATUS_SCRATCH_USED]] ^(<dt>SQLITE_STATUS_SCRATCH_USED</dt>
6215 ** <dd>This parameter returns the number of allocations used out of the
6216 ** [scratch memory allocator] configured using
6217 ** [SQLITE_CONFIG_SCRATCH].  The value returned is in allocations, not
6218 ** in bytes.  Since a single thread may only have one scratch allocation
6219 ** outstanding at time, this parameter also reports the number of threads
6220 ** using scratch memory at the same time.</dd>)^
6221 **
6222 ** [[SQLITE_STATUS_SCRATCH_OVERFLOW]] ^(<dt>SQLITE_STATUS_SCRATCH_OVERFLOW</dt>
6223 ** <dd>This parameter returns the number of bytes of scratch memory
6224 ** allocation which could not be satisfied by the [SQLITE_CONFIG_SCRATCH]
6225 ** buffer and where forced to overflow to [sqlite3_malloc()].  The values
6226 ** returned include overflows because the requested allocation was too
6227 ** larger (that is, because the requested allocation was larger than the
6228 ** "sz" parameter to [SQLITE_CONFIG_SCRATCH]) and because no scratch buffer
6229 ** slots were available.
6230 ** </dd>)^
6231 **
6232 ** [[SQLITE_STATUS_SCRATCH_SIZE]] ^(<dt>SQLITE_STATUS_SCRATCH_SIZE</dt>
6233 ** <dd>This parameter records the largest memory allocation request
6234 ** handed to [scratch 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_PARSER_STACK]] ^(<dt>SQLITE_STATUS_PARSER_STACK</dt>
6239 ** <dd>This parameter records the deepest parser stack.  It is only
6240 ** meaningful if SQLite is compiled with [YYTRACKMAXSTACKDEPTH].</dd>)^
6241 ** </dl>
6242 **
6243 ** New status parameters may be added from time to time.
6244 */
6245 #define SQLITE_STATUS_MEMORY_USED          0
6246 #define SQLITE_STATUS_PAGECACHE_USED       1
6247 #define SQLITE_STATUS_PAGECACHE_OVERFLOW   2
6248 #define SQLITE_STATUS_SCRATCH_USED         3
6249 #define SQLITE_STATUS_SCRATCH_OVERFLOW     4
6250 #define SQLITE_STATUS_MALLOC_SIZE          5
6251 #define SQLITE_STATUS_PARSER_STACK         6
6252 #define SQLITE_STATUS_PAGECACHE_SIZE       7
6253 #define SQLITE_STATUS_SCRATCH_SIZE         8
6254 #define SQLITE_STATUS_MALLOC_COUNT         9
6255 
6256 /*
6257 ** CAPI3REF: Database Connection Status
6258 **
6259 ** ^This interface is used to retrieve runtime status information
6260 ** about a single [database connection].  ^The first argument is the
6261 ** database connection object to be interrogated.  ^The second argument
6262 ** is an integer constant, taken from the set of
6263 ** [SQLITE_DBSTATUS options], that
6264 ** determines the parameter to interrogate.  The set of
6265 ** [SQLITE_DBSTATUS options] is likely
6266 ** to grow in future releases of SQLite.
6267 **
6268 ** ^The current value of the requested parameter is written into *pCur
6269 ** and the highest instantaneous value is written into *pHiwtr.  ^If
6270 ** the resetFlg is true, then the highest instantaneous value is
6271 ** reset back down to the current value.
6272 **
6273 ** ^The sqlite3_db_status() routine returns SQLITE_OK on success and a
6274 ** non-zero [error code] on failure.
6275 **
6276 ** See also: [sqlite3_status()] and [sqlite3_stmt_status()].
6277 */
6278 SQLITE_API int sqlite3_db_status(sqlite3*, int op, int *pCur, int *pHiwtr, int resetFlg);
6279 
6280 /*
6281 ** CAPI3REF: Status Parameters for database connections
6282 ** KEYWORDS: {SQLITE_DBSTATUS options}
6283 **
6284 ** These constants are the available integer "verbs" that can be passed as
6285 ** the second argument to the [sqlite3_db_status()] interface.
6286 **
6287 ** New verbs may be added in future releases of SQLite. Existing verbs
6288 ** might be discontinued. Applications should check the return code from
6289 ** [sqlite3_db_status()] to make sure that the call worked.
6290 ** The [sqlite3_db_status()] interface will return a non-zero error code
6291 ** if a discontinued or unsupported verb is invoked.
6292 **
6293 ** <dl>
6294 ** [[SQLITE_DBSTATUS_LOOKASIDE_USED]] ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_USED</dt>
6295 ** <dd>This parameter returns the number of lookaside memory slots currently
6296 ** checked out.</dd>)^
6297 **
6298 ** [[SQLITE_DBSTATUS_LOOKASIDE_HIT]] ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_HIT</dt>
6299 ** <dd>This parameter returns the number malloc attempts that were
6300 ** satisfied using lookaside memory. Only the high-water value is meaningful;
6301 ** the current value is always zero.)^
6302 **
6303 ** [[SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE]]
6304 ** ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE</dt>
6305 ** <dd>This parameter returns the number malloc attempts that might have
6306 ** been satisfied using lookaside memory but failed due to the amount of
6307 ** memory requested being larger than the lookaside slot size.
6308 ** Only the high-water value is meaningful;
6309 ** the current value is always zero.)^
6310 **
6311 ** [[SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL]]
6312 ** ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL</dt>
6313 ** <dd>This parameter returns the number malloc attempts that might have
6314 ** been satisfied using lookaside memory but failed due to all lookaside
6315 ** memory already being in use.
6316 ** Only the high-water value is meaningful;
6317 ** the current value is always zero.)^
6318 **
6319 ** [[SQLITE_DBSTATUS_CACHE_USED]] ^(<dt>SQLITE_DBSTATUS_CACHE_USED</dt>
6320 ** <dd>This parameter returns the approximate number of of bytes of heap
6321 ** memory used by all pager caches associated with the database connection.)^
6322 ** ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_USED is always 0.
6323 **
6324 ** [[SQLITE_DBSTATUS_SCHEMA_USED]] ^(<dt>SQLITE_DBSTATUS_SCHEMA_USED</dt>
6325 ** <dd>This parameter returns the approximate number of of bytes of heap
6326 ** memory used to store the schema for all databases associated
6327 ** with the connection - main, temp, and any [ATTACH]-ed databases.)^
6328 ** ^The full amount of memory used by the schemas is reported, even if the
6329 ** schema memory is shared with other database connections due to
6330 ** [shared cache mode] being enabled.
6331 ** ^The highwater mark associated with SQLITE_DBSTATUS_SCHEMA_USED is always 0.
6332 **
6333 ** [[SQLITE_DBSTATUS_STMT_USED]] ^(<dt>SQLITE_DBSTATUS_STMT_USED</dt>
6334 ** <dd>This parameter returns the approximate number of of bytes of heap
6335 ** and lookaside memory used by all prepared statements associated with
6336 ** the database connection.)^
6337 ** ^The highwater mark associated with SQLITE_DBSTATUS_STMT_USED is always 0.
6338 ** </dd>
6339 ** </dl>
6340 */
6341 #define SQLITE_DBSTATUS_LOOKASIDE_USED       0
6342 #define SQLITE_DBSTATUS_CACHE_USED           1
6343 #define SQLITE_DBSTATUS_SCHEMA_USED          2
6344 #define SQLITE_DBSTATUS_STMT_USED            3
6345 #define SQLITE_DBSTATUS_LOOKASIDE_HIT        4
6346 #define SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE  5
6347 #define SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL  6
6348 #define SQLITE_DBSTATUS_MAX                  6   /* Largest defined DBSTATUS */
6349 
6350 
6351 /*
6352 ** CAPI3REF: Prepared Statement Status
6353 **
6354 ** ^(Each prepared statement maintains various
6355 ** [SQLITE_STMTSTATUS counters] that measure the number
6356 ** of times it has performed specific operations.)^  These counters can
6357 ** be used to monitor the performance characteristics of the prepared
6358 ** statements.  For example, if the number of table steps greatly exceeds
6359 ** the number of table searches or result rows, that would tend to indicate
6360 ** that the prepared statement is using a full table scan rather than
6361 ** an index.
6362 **
6363 ** ^(This interface is used to retrieve and reset counter values from
6364 ** a [prepared statement].  The first argument is the prepared statement
6365 ** object to be interrogated.  The second argument
6366 ** is an integer code for a specific [SQLITE_STMTSTATUS counter]
6367 ** to be interrogated.)^
6368 ** ^The current value of the requested counter is returned.
6369 ** ^If the resetFlg is true, then the counter is reset to zero after this
6370 ** interface call returns.
6371 **
6372 ** See also: [sqlite3_status()] and [sqlite3_db_status()].
6373 */
6374 SQLITE_API int sqlite3_stmt_status(sqlite3_stmt*, int op,int resetFlg);
6375 
6376 /*
6377 ** CAPI3REF: Status Parameters for prepared statements
6378 ** KEYWORDS: {SQLITE_STMTSTATUS counter} {SQLITE_STMTSTATUS counters}
6379 **
6380 ** These preprocessor macros define integer codes that name counter
6381 ** values associated with the [sqlite3_stmt_status()] interface.
6382 ** The meanings of the various counters are as follows:
6383 **
6384 ** <dl>
6385 ** [[SQLITE_STMTSTATUS_FULLSCAN_STEP]] <dt>SQLITE_STMTSTATUS_FULLSCAN_STEP</dt>
6386 ** <dd>^This is the number of times that SQLite has stepped forward in
6387 ** a table as part of a full table scan.  Large numbers for this counter
6388 ** may indicate opportunities for performance improvement through
6389 ** careful use of indices.</dd>
6390 **
6391 ** [[SQLITE_STMTSTATUS_SORT]] <dt>SQLITE_STMTSTATUS_SORT</dt>
6392 ** <dd>^This is the number of sort operations that have occurred.
6393 ** A non-zero value in this counter may indicate an opportunity to
6394 ** improvement performance through careful use of indices.</dd>
6395 **
6396 ** [[SQLITE_STMTSTATUS_AUTOINDEX]] <dt>SQLITE_STMTSTATUS_AUTOINDEX</dt>
6397 ** <dd>^This is the number of rows inserted into transient indices that
6398 ** were created automatically in order to help joins run faster.
6399 ** A non-zero value in this counter may indicate an opportunity to
6400 ** improvement performance by adding permanent indices that do not
6401 ** need to be reinitialized each time the statement is run.</dd>
6402 **
6403 ** </dl>
6404 */
6405 #define SQLITE_STMTSTATUS_FULLSCAN_STEP     1
6406 #define SQLITE_STMTSTATUS_SORT              2
6407 #define SQLITE_STMTSTATUS_AUTOINDEX         3
6408 
6409 /*
6410 ** CAPI3REF: Custom Page Cache Object
6411 **
6412 ** The sqlite3_pcache type is opaque.  It is implemented by
6413 ** the pluggable module.  The SQLite core has no knowledge of
6414 ** its size or internal structure and never deals with the
6415 ** sqlite3_pcache object except by holding and passing pointers
6416 ** to the object.
6417 **
6418 ** See [sqlite3_pcache_methods] for additional information.
6419 */
6420 typedef struct sqlite3_pcache sqlite3_pcache;
6421 
6422 /*
6423 ** CAPI3REF: Application Defined Page Cache.
6424 ** KEYWORDS: {page cache}
6425 **
6426 ** ^(The [sqlite3_config]([SQLITE_CONFIG_PCACHE], ...) interface can
6427 ** register an alternative page cache implementation by passing in an
6428 ** instance of the sqlite3_pcache_methods structure.)^
6429 ** In many applications, most of the heap memory allocated by
6430 ** SQLite is used for the page cache.
6431 ** By implementing a
6432 ** custom page cache using this API, an application can better control
6433 ** the amount of memory consumed by SQLite, the way in which
6434 ** that memory is allocated and released, and the policies used to
6435 ** determine exactly which parts of a database file are cached and for
6436 ** how long.
6437 **
6438 ** The alternative page cache mechanism is an
6439 ** extreme measure that is only needed by the most demanding applications.
6440 ** The built-in page cache is recommended for most uses.
6441 **
6442 ** ^(The contents of the sqlite3_pcache_methods structure are copied to an
6443 ** internal buffer by SQLite within the call to [sqlite3_config].  Hence
6444 ** the application may discard the parameter after the call to
6445 ** [sqlite3_config()] returns.)^
6446 **
6447 ** [[the xInit() page cache method]]
6448 ** ^(The xInit() method is called once for each effective
6449 ** call to [sqlite3_initialize()])^
6450 ** (usually only once during the lifetime of the process). ^(The xInit()
6451 ** method is passed a copy of the sqlite3_pcache_methods.pArg value.)^
6452 ** The intent of the xInit() method is to set up global data structures
6453 ** required by the custom page cache implementation.
6454 ** ^(If the xInit() method is NULL, then the
6455 ** built-in default page cache is used instead of the application defined
6456 ** page cache.)^
6457 **
6458 ** [[the xShutdown() page cache method]]
6459 ** ^The xShutdown() method is called by [sqlite3_shutdown()].
6460 ** It can be used to clean up
6461 ** any outstanding resources before process shutdown, if required.
6462 ** ^The xShutdown() method may be NULL.
6463 **
6464 ** ^SQLite automatically serializes calls to the xInit method,
6465 ** so the xInit method need not be threadsafe.  ^The
6466 ** xShutdown method is only called from [sqlite3_shutdown()] so it does
6467 ** not need to be threadsafe either.  All other methods must be threadsafe
6468 ** in multithreaded applications.
6469 **
6470 ** ^SQLite will never invoke xInit() more than once without an intervening
6471 ** call to xShutdown().
6472 **
6473 ** [[the xCreate() page cache methods]]
6474 ** ^SQLite invokes the xCreate() method to construct a new cache instance.
6475 ** SQLite will typically create one cache instance for each open database file,
6476 ** though this is not guaranteed. ^The
6477 ** first parameter, szPage, is the size in bytes of the pages that must
6478 ** be allocated by the cache.  ^szPage will not be a power of two.  ^szPage
6479 ** will the page size of the database file that is to be cached plus an
6480 ** increment (here called "R") of less than 250.  SQLite will use the
6481 ** extra R bytes on each page to store metadata about the underlying
6482 ** database page on disk.  The value of R depends
6483 ** on the SQLite version, the target platform, and how SQLite was compiled.
6484 ** ^(R is constant for a particular build of SQLite. Except, there are two
6485 ** distinct values of R when SQLite is compiled with the proprietary
6486 ** ZIPVFS extension.)^  ^The second argument to
6487 ** xCreate(), bPurgeable, is true if the cache being created will
6488 ** be used to cache database pages of a file stored on disk, or
6489 ** false if it is used for an in-memory database. The cache implementation
6490 ** does not have to do anything special based with the value of bPurgeable;
6491 ** it is purely advisory.  ^On a cache where bPurgeable is false, SQLite will
6492 ** never invoke xUnpin() except to deliberately delete a page.
6493 ** ^In other words, calls to xUnpin() on a cache with bPurgeable set to
6494 ** false will always have the "discard" flag set to true.
6495 ** ^Hence, a cache created with bPurgeable false will
6496 ** never contain any unpinned pages.
6497 **
6498 ** [[the xCachesize() page cache method]]
6499 ** ^(The xCachesize() method may be called at any time by SQLite to set the
6500 ** suggested maximum cache-size (number of pages stored by) the cache
6501 ** instance passed as the first argument. This is the value configured using
6502 ** the SQLite "[PRAGMA cache_size]" command.)^  As with the bPurgeable
6503 ** parameter, the implementation is not required to do anything with this
6504 ** value; it is advisory only.
6505 **
6506 ** [[the xPagecount() page cache methods]]
6507 ** The xPagecount() method must return the number of pages currently
6508 ** stored in the cache, both pinned and unpinned.
6509 **
6510 ** [[the xFetch() page cache methods]]
6511 ** The xFetch() method locates a page in the cache and returns a pointer to
6512 ** the page, or a NULL pointer.
6513 ** A "page", in this context, means a buffer of szPage bytes aligned at an
6514 ** 8-byte boundary. The page to be fetched is determined by the key. ^The
6515 ** minimum key value is 1.  After it has been retrieved using xFetch, the page
6516 ** is considered to be "pinned".
6517 **
6518 ** If the requested page is already in the page cache, then the page cache
6519 ** implementation must return a pointer to the page buffer with its content
6520 ** intact.  If the requested page is not already in the cache, then the
6521 ** cache implementation should use the value of the createFlag
6522 ** parameter to help it determined what action to take:
6523 **
6524 ** <table border=1 width=85% align=center>
6525 ** <tr><th> createFlag <th> Behaviour when page is not already in cache
6526 ** <tr><td> 0 <td> Do not allocate a new page.  Return NULL.
6527 ** <tr><td> 1 <td> Allocate a new page if it easy and convenient to do so.
6528 **                 Otherwise return NULL.
6529 ** <tr><td> 2 <td> Make every effort to allocate a new page.  Only return
6530 **                 NULL if allocating a new page is effectively impossible.
6531 ** </table>
6532 **
6533 ** ^(SQLite will normally invoke xFetch() with a createFlag of 0 or 1.  SQLite
6534 ** will only use a createFlag of 2 after a prior call with a createFlag of 1
6535 ** failed.)^  In between the to xFetch() calls, SQLite may
6536 ** attempt to unpin one or more cache pages by spilling the content of
6537 ** pinned pages to disk and synching the operating system disk cache.
6538 **
6539 ** [[the xUnpin() page cache method]]
6540 ** ^xUnpin() is called by SQLite with a pointer to a currently pinned page
6541 ** as its second argument.  If the third parameter, discard, is non-zero,
6542 ** then the page must be evicted from the cache.
6543 ** ^If the discard parameter is
6544 ** zero, then the page may be discarded or retained at the discretion of
6545 ** page cache implementation. ^The page cache implementation
6546 ** may choose to evict unpinned pages at any time.
6547 **
6548 ** The cache must not perform any reference counting. A single
6549 ** call to xUnpin() unpins the page regardless of the number of prior calls
6550 ** to xFetch().
6551 **
6552 ** [[the xRekey() page cache methods]]
6553 ** The xRekey() method is used to change the key value associated with the
6554 ** page passed as the second argument. If the cache
6555 ** previously contains an entry associated with newKey, it must be
6556 ** discarded. ^Any prior cache entry associated with newKey is guaranteed not
6557 ** to be pinned.
6558 **
6559 ** When SQLite calls the xTruncate() method, the cache must discard all
6560 ** existing cache entries with page numbers (keys) greater than or equal
6561 ** to the value of the iLimit parameter passed to xTruncate(). If any
6562 ** of these pages are pinned, they are implicitly unpinned, meaning that
6563 ** they can be safely discarded.
6564 **
6565 ** [[the xDestroy() page cache method]]
6566 ** ^The xDestroy() method is used to delete a cache allocated by xCreate().
6567 ** All resources associated with the specified cache should be freed. ^After
6568 ** calling the xDestroy() method, SQLite considers the [sqlite3_pcache*]
6569 ** handle invalid, and will not use it with any other sqlite3_pcache_methods
6570 ** functions.
6571 */
6572 typedef struct sqlite3_pcache_methods sqlite3_pcache_methods;
6573 struct sqlite3_pcache_methods {
6574   void *pArg;
6575   int (*xInit)(void*);
6576   void (*xShutdown)(void*);
6577   sqlite3_pcache *(*xCreate)(int szPage, int bPurgeable);
6578   void (*xCachesize)(sqlite3_pcache*, int nCachesize);
6579   int (*xPagecount)(sqlite3_pcache*);
6580   void *(*xFetch)(sqlite3_pcache*, unsigned key, int createFlag);
6581   void (*xUnpin)(sqlite3_pcache*, void*, int discard);
6582   void (*xRekey)(sqlite3_pcache*, void*, unsigned oldKey, unsigned newKey);
6583   void (*xTruncate)(sqlite3_pcache*, unsigned iLimit);
6584   void (*xDestroy)(sqlite3_pcache*);
6585 };
6586 
6587 /*
6588 ** CAPI3REF: Online Backup Object
6589 **
6590 ** The sqlite3_backup object records state information about an ongoing
6591 ** online backup operation.  ^The sqlite3_backup object is created by
6592 ** a call to [sqlite3_backup_init()] and is destroyed by a call to
6593 ** [sqlite3_backup_finish()].
6594 **
6595 ** See Also: [Using the SQLite Online Backup API]
6596 */
6597 typedef struct sqlite3_backup sqlite3_backup;
6598 
6599 /*
6600 ** CAPI3REF: Online Backup API.
6601 **
6602 ** The backup API copies the content of one database into another.
6603 ** It is useful either for creating backups of databases or
6604 ** for copying in-memory databases to or from persistent files.
6605 **
6606 ** See Also: [Using the SQLite Online Backup API]
6607 **
6608 ** ^SQLite holds a write transaction open on the destination database file
6609 ** for the duration of the backup operation.
6610 ** ^The source database is read-locked only while it is being read;
6611 ** it is not locked continuously for the entire backup operation.
6612 ** ^Thus, the backup may be performed on a live source database without
6613 ** preventing other database connections from
6614 ** reading or writing to the source database while the backup is underway.
6615 **
6616 ** ^(To perform a backup operation:
6617 **   <ol>
6618 **     <li><b>sqlite3_backup_init()</b> is called once to initialize the
6619 **         backup,
6620 **     <li><b>sqlite3_backup_step()</b> is called one or more times to transfer
6621 **         the data between the two databases, and finally
6622 **     <li><b>sqlite3_backup_finish()</b> is called to release all resources
6623 **         associated with the backup operation.
6624 **   </ol>)^
6625 ** There should be exactly one call to sqlite3_backup_finish() for each
6626 ** successful call to sqlite3_backup_init().
6627 **
6628 ** [[sqlite3_backup_init()]] <b>sqlite3_backup_init()</b>
6629 **
6630 ** ^The D and N arguments to sqlite3_backup_init(D,N,S,M) are the
6631 ** [database connection] associated with the destination database
6632 ** and the database name, respectively.
6633 ** ^The database name is "main" for the main database, "temp" for the
6634 ** temporary database, or the name specified after the AS keyword in
6635 ** an [ATTACH] statement for an attached database.
6636 ** ^The S and M arguments passed to
6637 ** sqlite3_backup_init(D,N,S,M) identify the [database connection]
6638 ** and database name of the source database, respectively.
6639 ** ^The source and destination [database connections] (parameters S and D)
6640 ** must be different or else sqlite3_backup_init(D,N,S,M) will fail with
6641 ** an error.
6642 **
6643 ** ^If an error occurs within sqlite3_backup_init(D,N,S,M), then NULL is
6644 ** returned and an error code and error message are stored in the
6645 ** destination [database connection] D.
6646 ** ^The error code and message for the failed call to sqlite3_backup_init()
6647 ** can be retrieved using the [sqlite3_errcode()], [sqlite3_errmsg()], and/or
6648 ** [sqlite3_errmsg16()] functions.
6649 ** ^A successful call to sqlite3_backup_init() returns a pointer to an
6650 ** [sqlite3_backup] object.
6651 ** ^The [sqlite3_backup] object may be used with the sqlite3_backup_step() and
6652 ** sqlite3_backup_finish() functions to perform the specified backup
6653 ** operation.
6654 **
6655 ** [[sqlite3_backup_step()]] <b>sqlite3_backup_step()</b>
6656 **
6657 ** ^Function sqlite3_backup_step(B,N) will copy up to N pages between
6658 ** the source and destination databases specified by [sqlite3_backup] object B.
6659 ** ^If N is negative, all remaining source pages are copied.
6660 ** ^If sqlite3_backup_step(B,N) successfully copies N pages and there
6661 ** are still more pages to be copied, then the function returns [SQLITE_OK].
6662 ** ^If sqlite3_backup_step(B,N) successfully finishes copying all pages
6663 ** from source to destination, then it returns [SQLITE_DONE].
6664 ** ^If an error occurs while running sqlite3_backup_step(B,N),
6665 ** then an [error code] is returned. ^As well as [SQLITE_OK] and
6666 ** [SQLITE_DONE], a call to sqlite3_backup_step() may return [SQLITE_READONLY],
6667 ** [SQLITE_NOMEM], [SQLITE_BUSY], [SQLITE_LOCKED], or an
6668 ** [SQLITE_IOERR_ACCESS | SQLITE_IOERR_XXX] extended error code.
6669 **
6670 ** ^(The sqlite3_backup_step() might return [SQLITE_READONLY] if
6671 ** <ol>
6672 ** <li> the destination database was opened read-only, or
6673 ** <li> the destination database is using write-ahead-log journaling
6674 ** and the destination and source page sizes differ, or
6675 ** <li> the destination database is an in-memory database and the
6676 ** destination and source page sizes differ.
6677 ** </ol>)^
6678 **
6679 ** ^If sqlite3_backup_step() cannot obtain a required file-system lock, then
6680 ** the [sqlite3_busy_handler | busy-handler function]
6681 ** is invoked (if one is specified). ^If the
6682 ** busy-handler returns non-zero before the lock is available, then
6683 ** [SQLITE_BUSY] is returned to the caller. ^In this case the call to
6684 ** sqlite3_backup_step() can be retried later. ^If the source
6685 ** [database connection]
6686 ** is being used to write to the source database when sqlite3_backup_step()
6687 ** is called, then [SQLITE_LOCKED] is returned immediately. ^Again, in this
6688 ** case the call to sqlite3_backup_step() can be retried later on. ^(If
6689 ** [SQLITE_IOERR_ACCESS | SQLITE_IOERR_XXX], [SQLITE_NOMEM], or
6690 ** [SQLITE_READONLY] is returned, then
6691 ** there is no point in retrying the call to sqlite3_backup_step(). These
6692 ** errors are considered fatal.)^  The application must accept
6693 ** that the backup operation has failed and pass the backup operation handle
6694 ** to the sqlite3_backup_finish() to release associated resources.
6695 **
6696 ** ^The first call to sqlite3_backup_step() obtains an exclusive lock
6697 ** on the destination file. ^The exclusive lock is not released until either
6698 ** sqlite3_backup_finish() is called or the backup operation is complete
6699 ** and sqlite3_backup_step() returns [SQLITE_DONE].  ^Every call to
6700 ** sqlite3_backup_step() obtains a [shared lock] on the source database that
6701 ** lasts for the duration of the sqlite3_backup_step() call.
6702 ** ^Because the source database is not locked between calls to
6703 ** sqlite3_backup_step(), the source database may be modified mid-way
6704 ** through the backup process.  ^If the source database is modified by an
6705 ** external process or via a database connection other than the one being
6706 ** used by the backup operation, then the backup will be automatically
6707 ** restarted by the next call to sqlite3_backup_step(). ^If the source
6708 ** database is modified by the using the same database connection as is used
6709 ** by the backup operation, then the backup database is automatically
6710 ** updated at the same time.
6711 **
6712 ** [[sqlite3_backup_finish()]] <b>sqlite3_backup_finish()</b>
6713 **
6714 ** When sqlite3_backup_step() has returned [SQLITE_DONE], or when the
6715 ** application wishes to abandon the backup operation, the application
6716 ** should destroy the [sqlite3_backup] by passing it to sqlite3_backup_finish().
6717 ** ^The sqlite3_backup_finish() interfaces releases all
6718 ** resources associated with the [sqlite3_backup] object.
6719 ** ^If sqlite3_backup_step() has not yet returned [SQLITE_DONE], then any
6720 ** active write-transaction on the destination database is rolled back.
6721 ** The [sqlite3_backup] object is invalid
6722 ** and may not be used following a call to sqlite3_backup_finish().
6723 **
6724 ** ^The value returned by sqlite3_backup_finish is [SQLITE_OK] if no
6725 ** sqlite3_backup_step() errors occurred, regardless or whether or not
6726 ** sqlite3_backup_step() completed.
6727 ** ^If an out-of-memory condition or IO error occurred during any prior
6728 ** sqlite3_backup_step() call on the same [sqlite3_backup] object, then
6729 ** sqlite3_backup_finish() returns the corresponding [error code].
6730 **
6731 ** ^A return of [SQLITE_BUSY] or [SQLITE_LOCKED] from sqlite3_backup_step()
6732 ** is not a permanent error and does not affect the return value of
6733 ** sqlite3_backup_finish().
6734 **
6735 ** [[sqlite3_backup__remaining()]] [[sqlite3_backup_pagecount()]]
6736 ** <b>sqlite3_backup_remaining() and sqlite3_backup_pagecount()</b>
6737 **
6738 ** ^Each call to sqlite3_backup_step() sets two values inside
6739 ** the [sqlite3_backup] object: the number of pages still to be backed
6740 ** up and the total number of pages in the source database file.
6741 ** The sqlite3_backup_remaining() and sqlite3_backup_pagecount() interfaces
6742 ** retrieve these two values, respectively.
6743 **
6744 ** ^The values returned by these functions are only updated by
6745 ** sqlite3_backup_step(). ^If the source database is modified during a backup
6746 ** operation, then the values are not updated to account for any extra
6747 ** pages that need to be updated or the size of the source database file
6748 ** changing.
6749 **
6750 ** <b>Concurrent Usage of Database Handles</b>
6751 **
6752 ** ^The source [database connection] may be used by the application for other
6753 ** purposes while a backup operation is underway or being initialized.
6754 ** ^If SQLite is compiled and configured to support threadsafe database
6755 ** connections, then the source database connection may be used concurrently
6756 ** from within other threads.
6757 **
6758 ** However, the application must guarantee that the destination
6759 ** [database connection] is not passed to any other API (by any thread) after
6760 ** sqlite3_backup_init() is called and before the corresponding call to
6761 ** sqlite3_backup_finish().  SQLite does not currently check to see
6762 ** if the application incorrectly accesses the destination [database connection]
6763 ** and so no error code is reported, but the operations may malfunction
6764 ** nevertheless.  Use of the destination database connection while a
6765 ** backup is in progress might also also cause a mutex deadlock.
6766 **
6767 ** If running in [shared cache mode], the application must
6768 ** guarantee that the shared cache used by the destination database
6769 ** is not accessed while the backup is running. In practice this means
6770 ** that the application must guarantee that the disk file being
6771 ** backed up to is not accessed by any connection within the process,
6772 ** not just the specific connection that was passed to sqlite3_backup_init().
6773 **
6774 ** The [sqlite3_backup] object itself is partially threadsafe. Multiple
6775 ** threads may safely make multiple concurrent calls to sqlite3_backup_step().
6776 ** However, the sqlite3_backup_remaining() and sqlite3_backup_pagecount()
6777 ** APIs are not strictly speaking threadsafe. If they are invoked at the
6778 ** same time as another thread is invoking sqlite3_backup_step() it is
6779 ** possible that they return invalid values.
6780 */
6781 SQLITE_API sqlite3_backup *sqlite3_backup_init(
6782   sqlite3 *pDest,                        /* Destination database handle */
6783   const char *zDestName,                 /* Destination database name */
6784   sqlite3 *pSource,                      /* Source database handle */
6785   const char *zSourceName                /* Source database name */
6786 );
6787 SQLITE_API int sqlite3_backup_step(sqlite3_backup *p, int nPage);
6788 SQLITE_API int sqlite3_backup_finish(sqlite3_backup *p);
6789 SQLITE_API int sqlite3_backup_remaining(sqlite3_backup *p);
6790 SQLITE_API int sqlite3_backup_pagecount(sqlite3_backup *p);
6791 
6792 /*
6793 ** CAPI3REF: Unlock Notification
6794 **
6795 ** ^When running in shared-cache mode, a database operation may fail with
6796 ** an [SQLITE_LOCKED] error if the required locks on the shared-cache or
6797 ** individual tables within the shared-cache cannot be obtained. See
6798 ** [SQLite Shared-Cache Mode] for a description of shared-cache locking.
6799 ** ^This API may be used to register a callback that SQLite will invoke
6800 ** when the connection currently holding the required lock relinquishes it.
6801 ** ^This API is only available if the library was compiled with the
6802 ** [SQLITE_ENABLE_UNLOCK_NOTIFY] C-preprocessor symbol defined.
6803 **
6804 ** See Also: [Using the SQLite Unlock Notification Feature].
6805 **
6806 ** ^Shared-cache locks are released when a database connection concludes
6807 ** its current transaction, either by committing it or rolling it back.
6808 **
6809 ** ^When a connection (known as the blocked connection) fails to obtain a
6810 ** shared-cache lock and SQLITE_LOCKED is returned to the caller, the
6811 ** identity of the database connection (the blocking connection) that
6812 ** has locked the required resource is stored internally. ^After an
6813 ** application receives an SQLITE_LOCKED error, it may call the
6814 ** sqlite3_unlock_notify() method with the blocked connection handle as
6815 ** the first argument to register for a callback that will be invoked
6816 ** when the blocking connections current transaction is concluded. ^The
6817 ** callback is invoked from within the [sqlite3_step] or [sqlite3_close]
6818 ** call that concludes the blocking connections transaction.
6819 **
6820 ** ^(If sqlite3_unlock_notify() is called in a multi-threaded application,
6821 ** there is a chance that the blocking connection will have already
6822 ** concluded its transaction by the time sqlite3_unlock_notify() is invoked.
6823 ** If this happens, then the specified callback is invoked immediately,
6824 ** from within the call to sqlite3_unlock_notify().)^
6825 **
6826 ** ^If the blocked connection is attempting to obtain a write-lock on a
6827 ** shared-cache table, and more than one other connection currently holds
6828 ** a read-lock on the same table, then SQLite arbitrarily selects one of
6829 ** the other connections to use as the blocking connection.
6830 **
6831 ** ^(There may be at most one unlock-notify callback registered by a
6832 ** blocked connection. If sqlite3_unlock_notify() is called when the
6833 ** blocked connection already has a registered unlock-notify callback,
6834 ** then the new callback replaces the old.)^ ^If sqlite3_unlock_notify() is
6835 ** called with a NULL pointer as its second argument, then any existing
6836 ** unlock-notify callback is canceled. ^The blocked connections
6837 ** unlock-notify callback may also be canceled by closing the blocked
6838 ** connection using [sqlite3_close()].
6839 **
6840 ** The unlock-notify callback is not reentrant. If an application invokes
6841 ** any sqlite3_xxx API functions from within an unlock-notify callback, a
6842 ** crash or deadlock may be the result.
6843 **
6844 ** ^Unless deadlock is detected (see below), sqlite3_unlock_notify() always
6845 ** returns SQLITE_OK.
6846 **
6847 ** <b>Callback Invocation Details</b>
6848 **
6849 ** When an unlock-notify callback is registered, the application provides a
6850 ** single void* pointer that is passed to the callback when it is invoked.
6851 ** However, the signature of the callback function allows SQLite to pass
6852 ** it an array of void* context pointers. The first argument passed to
6853 ** an unlock-notify callback is a pointer to an array of void* pointers,
6854 ** and the second is the number of entries in the array.
6855 **
6856 ** When a blocking connections transaction is concluded, there may be
6857 ** more than one blocked connection that has registered for an unlock-notify
6858 ** callback. ^If two or more such blocked connections have specified the
6859 ** same callback function, then instead of invoking the callback function
6860 ** multiple times, it is invoked once with the set of void* context pointers
6861 ** specified by the blocked connections bundled together into an array.
6862 ** This gives the application an opportunity to prioritize any actions
6863 ** related to the set of unblocked database connections.
6864 **
6865 ** <b>Deadlock Detection</b>
6866 **
6867 ** Assuming that after registering for an unlock-notify callback a
6868 ** database waits for the callback to be issued before taking any further
6869 ** action (a reasonable assumption), then using this API may cause the
6870 ** application to deadlock. For example, if connection X is waiting for
6871 ** connection Y's transaction to be concluded, and similarly connection
6872 ** Y is waiting on connection X's transaction, then neither connection
6873 ** will proceed and the system may remain deadlocked indefinitely.
6874 **
6875 ** To avoid this scenario, the sqlite3_unlock_notify() performs deadlock
6876 ** detection. ^If a given call to sqlite3_unlock_notify() would put the
6877 ** system in a deadlocked state, then SQLITE_LOCKED is returned and no
6878 ** unlock-notify callback is registered. The system is said to be in
6879 ** a deadlocked state if connection A has registered for an unlock-notify
6880 ** callback on the conclusion of connection B's transaction, and connection
6881 ** B has itself registered for an unlock-notify callback when connection
6882 ** A's transaction is concluded. ^Indirect deadlock is also detected, so
6883 ** the system is also considered to be deadlocked if connection B has
6884 ** registered for an unlock-notify callback on the conclusion of connection
6885 ** C's transaction, where connection C is waiting on connection A. ^Any
6886 ** number of levels of indirection are allowed.
6887 **
6888 ** <b>The "DROP TABLE" Exception</b>
6889 **
6890 ** When a call to [sqlite3_step()] returns SQLITE_LOCKED, it is almost
6891 ** always appropriate to call sqlite3_unlock_notify(). There is however,
6892 ** one exception. When executing a "DROP TABLE" or "DROP INDEX" statement,
6893 ** SQLite checks if there are any currently executing SELECT statements
6894 ** that belong to the same connection. If there are, SQLITE_LOCKED is
6895 ** returned. In this case there is no "blocking connection", so invoking
6896 ** sqlite3_unlock_notify() results in the unlock-notify callback being
6897 ** invoked immediately. If the application then re-attempts the "DROP TABLE"
6898 ** or "DROP INDEX" query, an infinite loop might be the result.
6899 **
6900 ** One way around this problem is to check the extended error code returned
6901 ** by an sqlite3_step() call. ^(If there is a blocking connection, then the
6902 ** extended error code is set to SQLITE_LOCKED_SHAREDCACHE. Otherwise, in
6903 ** the special "DROP TABLE/INDEX" case, the extended error code is just
6904 ** SQLITE_LOCKED.)^
6905 */
6906 SQLITE_API int sqlite3_unlock_notify(
6907   sqlite3 *pBlocked,                          /* Waiting connection */
6908   void (*xNotify)(void **apArg, int nArg),    /* Callback function to invoke */
6909   void *pNotifyArg                            /* Argument to pass to xNotify */
6910 );
6911 
6912 
6913 /*
6914 ** CAPI3REF: String Comparison
6915 **
6916 ** ^The [sqlite3_strnicmp()] API allows applications and extensions to
6917 ** compare the contents of two buffers containing UTF-8 strings in a
6918 ** case-independent fashion, using the same definition of case independence
6919 ** that SQLite uses internally when comparing identifiers.
6920 */
6921 SQLITE_API int sqlite3_strnicmp(const char *, const char *, int);
6922 
6923 /*
6924 ** CAPI3REF: Error Logging Interface
6925 **
6926 ** ^The [sqlite3_log()] interface writes a message into the error log
6927 ** established by the [SQLITE_CONFIG_LOG] option to [sqlite3_config()].
6928 ** ^If logging is enabled, the zFormat string and subsequent arguments are
6929 ** used with [sqlite3_snprintf()] to generate the final output string.
6930 **
6931 ** The sqlite3_log() interface is intended for use by extensions such as
6932 ** virtual tables, collating functions, and SQL functions.  While there is
6933 ** nothing to prevent an application from calling sqlite3_log(), doing so
6934 ** is considered bad form.
6935 **
6936 ** The zFormat string must not be NULL.
6937 **
6938 ** To avoid deadlocks and other threading problems, the sqlite3_log() routine
6939 ** will not use dynamically allocated memory.  The log message is stored in
6940 ** a fixed-length buffer on the stack.  If the log message is longer than
6941 ** a few hundred characters, it will be truncated to the length of the
6942 ** buffer.
6943 */
6944 SQLITE_API void sqlite3_log(int iErrCode, const char *zFormat, ...);
6945 
6946 /*
6947 ** CAPI3REF: Write-Ahead Log Commit Hook
6948 **
6949 ** ^The [sqlite3_wal_hook()] function is used to register a callback that
6950 ** will be invoked each time a database connection commits data to a
6951 ** [write-ahead log] (i.e. whenever a transaction is committed in
6952 ** [journal_mode | journal_mode=WAL mode]).
6953 **
6954 ** ^The callback is invoked by SQLite after the commit has taken place and
6955 ** the associated write-lock on the database released, so the implementation
6956 ** may read, write or [checkpoint] the database as required.
6957 **
6958 ** ^The first parameter passed to the callback function when it is invoked
6959 ** is a copy of the third parameter passed to sqlite3_wal_hook() when
6960 ** registering the callback. ^The second is a copy of the database handle.
6961 ** ^The third parameter is the name of the database that was written to -
6962 ** either "main" or the name of an [ATTACH]-ed database. ^The fourth parameter
6963 ** is the number of pages currently in the write-ahead log file,
6964 ** including those that were just committed.
6965 **
6966 ** The callback function should normally return [SQLITE_OK].  ^If an error
6967 ** code is returned, that error will propagate back up through the
6968 ** SQLite code base to cause the statement that provoked the callback
6969 ** to report an error, though the commit will have still occurred. If the
6970 ** callback returns [SQLITE_ROW] or [SQLITE_DONE], or if it returns a value
6971 ** that does not correspond to any valid SQLite error code, the results
6972 ** are undefined.
6973 **
6974 ** A single database handle may have at most a single write-ahead log callback
6975 ** registered at one time. ^Calling [sqlite3_wal_hook()] replaces any
6976 ** previously registered write-ahead log callback. ^Note that the
6977 ** [sqlite3_wal_autocheckpoint()] interface and the
6978 ** [wal_autocheckpoint pragma] both invoke [sqlite3_wal_hook()] and will
6979 ** those overwrite any prior [sqlite3_wal_hook()] settings.
6980 */
6981 SQLITE_API void *sqlite3_wal_hook(
6982   sqlite3*,
6983   int(*)(void *,sqlite3*,const char*,int),
6984   void*
6985 );
6986 
6987 /*
6988 ** CAPI3REF: Configure an auto-checkpoint
6989 **
6990 ** ^The [sqlite3_wal_autocheckpoint(D,N)] is a wrapper around
6991 ** [sqlite3_wal_hook()] that causes any database on [database connection] D
6992 ** to automatically [checkpoint]
6993 ** after committing a transaction if there are N or
6994 ** more frames in the [write-ahead log] file.  ^Passing zero or
6995 ** a negative value as the nFrame parameter disables automatic
6996 ** checkpoints entirely.
6997 **
6998 ** ^The callback registered by this function replaces any existing callback
6999 ** registered using [sqlite3_wal_hook()].  ^Likewise, registering a callback
7000 ** using [sqlite3_wal_hook()] disables the automatic checkpoint mechanism
7001 ** configured by this function.
7002 **
7003 ** ^The [wal_autocheckpoint pragma] can be used to invoke this interface
7004 ** from SQL.
7005 **
7006 ** ^Every new [database connection] defaults to having the auto-checkpoint
7007 ** enabled with a threshold of 1000 or [SQLITE_DEFAULT_WAL_AUTOCHECKPOINT]
7008 ** pages.  The use of this interface
7009 ** is only necessary if the default setting is found to be suboptimal
7010 ** for a particular application.
7011 */
7012 SQLITE_API int sqlite3_wal_autocheckpoint(sqlite3 *db, int N);
7013 
7014 /*
7015 ** CAPI3REF: Checkpoint a database
7016 **
7017 ** ^The [sqlite3_wal_checkpoint(D,X)] interface causes database named X
7018 ** on [database connection] D to be [checkpointed].  ^If X is NULL or an
7019 ** empty string, then a checkpoint is run on all databases of
7020 ** connection D.  ^If the database connection D is not in
7021 ** [WAL | write-ahead log mode] then this interface is a harmless no-op.
7022 **
7023 ** ^The [wal_checkpoint pragma] can be used to invoke this interface
7024 ** from SQL.  ^The [sqlite3_wal_autocheckpoint()] interface and the
7025 ** [wal_autocheckpoint pragma] can be used to cause this interface to be
7026 ** run whenever the WAL reaches a certain size threshold.
7027 **
7028 ** See also: [sqlite3_wal_checkpoint_v2()]
7029 */
7030 SQLITE_API int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb);
7031 
7032 /*
7033 ** CAPI3REF: Checkpoint a database
7034 **
7035 ** Run a checkpoint operation on WAL database zDb attached to database
7036 ** handle db. The specific operation is determined by the value of the
7037 ** eMode parameter:
7038 **
7039 ** <dl>
7040 ** <dt>SQLITE_CHECKPOINT_PASSIVE<dd>
7041 **   Checkpoint as many frames as possible without waiting for any database
7042 **   readers or writers to finish. Sync the db file if all frames in the log
7043 **   are checkpointed. This mode is the same as calling
7044 **   sqlite3_wal_checkpoint(). The busy-handler callback is never invoked.
7045 **
7046 ** <dt>SQLITE_CHECKPOINT_FULL<dd>
7047 **   This mode blocks (calls the busy-handler callback) until there is no
7048 **   database writer and all readers are reading from the most recent database
7049 **   snapshot. It then checkpoints all frames in the log file and syncs the
7050 **   database file. This call blocks database writers while it is running,
7051 **   but not database readers.
7052 **
7053 ** <dt>SQLITE_CHECKPOINT_RESTART<dd>
7054 **   This mode works the same way as SQLITE_CHECKPOINT_FULL, except after
7055 **   checkpointing the log file it blocks (calls the busy-handler callback)
7056 **   until all readers are reading from the database file only. This ensures
7057 **   that the next client to write to the database file restarts the log file
7058 **   from the beginning. This call blocks database writers while it is running,
7059 **   but not database readers.
7060 ** </dl>
7061 **
7062 ** If pnLog is not NULL, then *pnLog is set to the total number of frames in
7063 ** the log file before returning. If pnCkpt is not NULL, then *pnCkpt is set to
7064 ** the total number of checkpointed frames (including any that were already
7065 ** checkpointed when this function is called). *pnLog and *pnCkpt may be
7066 ** populated even if sqlite3_wal_checkpoint_v2() returns other than SQLITE_OK.
7067 ** If no values are available because of an error, they are both set to -1
7068 ** before returning to communicate this to the caller.
7069 **
7070 ** All calls obtain an exclusive "checkpoint" lock on the database file. If
7071 ** any other process is running a checkpoint operation at the same time, the
7072 ** lock cannot be obtained and SQLITE_BUSY is returned. Even if there is a
7073 ** busy-handler configured, it will not be invoked in this case.
7074 **
7075 ** The SQLITE_CHECKPOINT_FULL and RESTART modes also obtain the exclusive
7076 ** "writer" lock on the database file. If the writer lock cannot be obtained
7077 ** immediately, and a busy-handler is configured, it is invoked and the writer
7078 ** lock retried until either the busy-handler returns 0 or the lock is
7079 ** successfully obtained. The busy-handler is also invoked while waiting for
7080 ** database readers as described above. If the busy-handler returns 0 before
7081 ** the writer lock is obtained or while waiting for database readers, the
7082 ** checkpoint operation proceeds from that point in the same way as
7083 ** SQLITE_CHECKPOINT_PASSIVE - checkpointing as many frames as possible
7084 ** without blocking any further. SQLITE_BUSY is returned in this case.
7085 **
7086 ** If parameter zDb is NULL or points to a zero length string, then the
7087 ** specified operation is attempted on all WAL databases. In this case the
7088 ** values written to output parameters *pnLog and *pnCkpt are undefined. If
7089 ** an SQLITE_BUSY error is encountered when processing one or more of the
7090 ** attached WAL databases, the operation is still attempted on any remaining
7091 ** attached databases and SQLITE_BUSY is returned to the caller. If any other
7092 ** error occurs while processing an attached database, processing is abandoned
7093 ** and the error code returned to the caller immediately. If no error
7094 ** (SQLITE_BUSY or otherwise) is encountered while processing the attached
7095 ** databases, SQLITE_OK is returned.
7096 **
7097 ** If database zDb is the name of an attached database that is not in WAL
7098 ** mode, SQLITE_OK is returned and both *pnLog and *pnCkpt set to -1. If
7099 ** zDb is not NULL (or a zero length string) and is not the name of any
7100 ** attached database, SQLITE_ERROR is returned to the caller.
7101 */
7102 SQLITE_API int sqlite3_wal_checkpoint_v2(
7103   sqlite3 *db,                    /* Database handle */
7104   const char *zDb,                /* Name of attached database (or NULL) */
7105   int eMode,                      /* SQLITE_CHECKPOINT_* value */
7106   int *pnLog,                     /* OUT: Size of WAL log in frames */
7107   int *pnCkpt                     /* OUT: Total number of frames checkpointed */
7108 );
7109 
7110 /*
7111 ** CAPI3REF: Checkpoint operation parameters
7112 **
7113 ** These constants can be used as the 3rd parameter to
7114 ** [sqlite3_wal_checkpoint_v2()].  See the [sqlite3_wal_checkpoint_v2()]
7115 ** documentation for additional information about the meaning and use of
7116 ** each of these values.
7117 */
7118 #define SQLITE_CHECKPOINT_PASSIVE 0
7119 #define SQLITE_CHECKPOINT_FULL    1
7120 #define SQLITE_CHECKPOINT_RESTART 2
7121 
7122 /*
7123 ** CAPI3REF: Virtual Table Interface Configuration
7124 **
7125 ** This function may be called by either the [xConnect] or [xCreate] method
7126 ** of a [virtual table] implementation to configure
7127 ** various facets of the virtual table interface.
7128 **
7129 ** If this interface is invoked outside the context of an xConnect or
7130 ** xCreate virtual table method then the behavior is undefined.
7131 **
7132 ** At present, there is only one option that may be configured using
7133 ** this function. (See [SQLITE_VTAB_CONSTRAINT_SUPPORT].)  Further options
7134 ** may be added in the future.
7135 */
7136 SQLITE_API int sqlite3_vtab_config(sqlite3*, int op, ...);
7137 
7138 /*
7139 ** CAPI3REF: Virtual Table Configuration Options
7140 **
7141 ** These macros define the various options to the
7142 ** [sqlite3_vtab_config()] interface that [virtual table] implementations
7143 ** can use to customize and optimize their behavior.
7144 **
7145 ** <dl>
7146 ** <dt>SQLITE_VTAB_CONSTRAINT_SUPPORT
7147 ** <dd>Calls of the form
7148 ** [sqlite3_vtab_config](db,SQLITE_VTAB_CONSTRAINT_SUPPORT,X) are supported,
7149 ** where X is an integer.  If X is zero, then the [virtual table] whose
7150 ** [xCreate] or [xConnect] method invoked [sqlite3_vtab_config()] does not
7151 ** support constraints.  In this configuration (which is the default) if
7152 ** a call to the [xUpdate] method returns [SQLITE_CONSTRAINT], then the entire
7153 ** statement is rolled back as if [ON CONFLICT | OR ABORT] had been
7154 ** specified as part of the users SQL statement, regardless of the actual
7155 ** ON CONFLICT mode specified.
7156 **
7157 ** If X is non-zero, then the virtual table implementation guarantees
7158 ** that if [xUpdate] returns [SQLITE_CONSTRAINT], it will do so before
7159 ** any modifications to internal or persistent data structures have been made.
7160 ** If the [ON CONFLICT] mode is ABORT, FAIL, IGNORE or ROLLBACK, SQLite
7161 ** is able to roll back a statement or database transaction, and abandon
7162 ** or continue processing the current SQL statement as appropriate.
7163 ** If the ON CONFLICT mode is REPLACE and the [xUpdate] method returns
7164 ** [SQLITE_CONSTRAINT], SQLite handles this as if the ON CONFLICT mode
7165 ** had been ABORT.
7166 **
7167 ** Virtual table implementations that are required to handle OR REPLACE
7168 ** must do so within the [xUpdate] method. If a call to the
7169 ** [sqlite3_vtab_on_conflict()] function indicates that the current ON
7170 ** CONFLICT policy is REPLACE, the virtual table implementation should
7171 ** silently replace the appropriate rows within the xUpdate callback and
7172 ** return SQLITE_OK. Or, if this is not possible, it may return
7173 ** SQLITE_CONSTRAINT, in which case SQLite falls back to OR ABORT
7174 ** constraint handling.
7175 ** </dl>
7176 */
7177 #define SQLITE_VTAB_CONSTRAINT_SUPPORT 1
7178 
7179 /*
7180 ** CAPI3REF: Determine The Virtual Table Conflict Policy
7181 **
7182 ** This function may only be called from within a call to the [xUpdate] method
7183 ** of a [virtual table] implementation for an INSERT or UPDATE operation. ^The
7184 ** value returned is one of [SQLITE_ROLLBACK], [SQLITE_IGNORE], [SQLITE_FAIL],
7185 ** [SQLITE_ABORT], or [SQLITE_REPLACE], according to the [ON CONFLICT] mode
7186 ** of the SQL statement that triggered the call to the [xUpdate] method of the
7187 ** [virtual table].
7188 */
7189 SQLITE_API int sqlite3_vtab_on_conflict(sqlite3 *);
7190 
7191 /*
7192 ** CAPI3REF: Conflict resolution modes
7193 **
7194 ** These constants are returned by [sqlite3_vtab_on_conflict()] to
7195 ** inform a [virtual table] implementation what the [ON CONFLICT] mode
7196 ** is for the SQL statement being evaluated.
7197 **
7198 ** Note that the [SQLITE_IGNORE] constant is also used as a potential
7199 ** return value from the [sqlite3_set_authorizer()] callback and that
7200 ** [SQLITE_ABORT] is also a [result code].
7201 */
7202 #define SQLITE_ROLLBACK 1
7203 /* #define SQLITE_IGNORE 2 // Also used by sqlite3_authorizer() callback */
7204 #define SQLITE_FAIL     3
7205 /* #define SQLITE_ABORT 4  // Also an error code */
7206 #define SQLITE_REPLACE  5
7207 
7208 
7209 
7210 /*
7211 ** Undo the hack that converts floating point types to integer for
7212 ** builds on processors without floating point support.
7213 */
7214 #ifdef SQLITE_OMIT_FLOATING_POINT
7215 # undef double
7216 #endif
7217 
7218 #if 0
7219 }  /* End of the 'extern "C"' block */
7220 #endif
7221 #endif
7222 
7223 /*
7224 ** 2010 August 30
7225 **
7226 ** The author disclaims copyright to this source code.  In place of
7227 ** a legal notice, here is a blessing:
7228 **
7229 **    May you do good and not evil.
7230 **    May you find forgiveness for yourself and forgive others.
7231 **    May you share freely, never taking more than you give.
7232 **
7233 *************************************************************************
7234 */
7235 
7236 #ifndef _SQLITE3RTREE_H_
7237 #define _SQLITE3RTREE_H_
7238 
7239 
7240 #if 0
7241 extern "C" {
7242 #endif
7243 
7244 typedef struct sqlite3_rtree_geometry sqlite3_rtree_geometry;
7245 
7246 /*
7247 ** Register a geometry callback named zGeom that can be used as part of an
7248 ** R-Tree geometry query as follows:
7249 **
7250 **   SELECT ... FROM <rtree> WHERE <rtree col> MATCH $zGeom(... params ...)
7251 */
7252 SQLITE_API int sqlite3_rtree_geometry_callback(
7253   sqlite3 *db,
7254   const char *zGeom,
7255   int (*xGeom)(sqlite3_rtree_geometry *, int nCoord, double *aCoord, int *pRes),
7256   void *pContext
7257 );
7258 
7259 
7260 /*
7261 ** A pointer to a structure of the following type is passed as the first
7262 ** argument to callbacks registered using rtree_geometry_callback().
7263 */
7264 struct sqlite3_rtree_geometry {
7265   void *pContext;                 /* Copy of pContext passed to s_r_g_c() */
7266   int nParam;                     /* Size of array aParam[] */
7267   double *aParam;                 /* Parameters passed to SQL geom function */
7268   void *pUser;                    /* Callback implementation user data */
7269   void (*xDelUser)(void *);       /* Called by SQLite to clean up pUser */
7270 };
7271 
7272 
7273 #if 0
7274 }  /* end of the 'extern "C"' block */
7275 #endif
7276 
7277 #endif  /* ifndef _SQLITE3RTREE_H_ */
7278 
7279 
7280 /************** End of sqlite3.h *********************************************/
7281 /************** Continuing where we left off in sqliteInt.h ******************/
7282 /************** Include hash.h in the middle of sqliteInt.h ******************/
7283 /************** Begin file hash.h ********************************************/
7284 /*
7285 ** 2001 September 22
7286 **
7287 ** The author disclaims copyright to this source code.  In place of
7288 ** a legal notice, here is a blessing:
7289 **
7290 **    May you do good and not evil.
7291 **    May you find forgiveness for yourself and forgive others.
7292 **    May you share freely, never taking more than you give.
7293 **
7294 *************************************************************************
7295 ** This is the header file for the generic hash-table implemenation
7296 ** used in SQLite.
7297 */
7298 #ifndef _SQLITE_HASH_H_
7299 #define _SQLITE_HASH_H_
7300 
7301 /* Forward declarations of structures. */
7302 typedef struct Hash Hash;
7303 typedef struct HashElem HashElem;
7304 
7305 /* A complete hash table is an instance of the following structure.
7306 ** The internals of this structure are intended to be opaque -- client
7307 ** code should not attempt to access or modify the fields of this structure
7308 ** directly.  Change this structure only by using the routines below.
7309 ** However, some of the "procedures" and "functions" for modifying and
7310 ** accessing this structure are really macros, so we can't really make
7311 ** this structure opaque.
7312 **
7313 ** All elements of the hash table are on a single doubly-linked list.
7314 ** Hash.first points to the head of this list.
7315 **
7316 ** There are Hash.htsize buckets.  Each bucket points to a spot in
7317 ** the global doubly-linked list.  The contents of the bucket are the
7318 ** element pointed to plus the next _ht.count-1 elements in the list.
7319 **
7320 ** Hash.htsize and Hash.ht may be zero.  In that case lookup is done
7321 ** by a linear search of the global list.  For small tables, the
7322 ** Hash.ht table is never allocated because if there are few elements
7323 ** in the table, it is faster to do a linear search than to manage
7324 ** the hash table.
7325 */
7326 struct Hash {
7327   unsigned int htsize;      /* Number of buckets in the hash table */
7328   unsigned int count;       /* Number of entries in this table */
7329   HashElem *first;          /* The first element of the array */
7330   struct _ht {              /* the hash table */
7331     int count;                 /* Number of entries with this hash */
7332     HashElem *chain;           /* Pointer to first entry with this hash */
7333   } *ht;
7334 };
7335 
7336 /* Each element in the hash table is an instance of the following
7337 ** structure.  All elements are stored on a single doubly-linked list.
7338 **
7339 ** Again, this structure is intended to be opaque, but it can't really
7340 ** be opaque because it is used by macros.
7341 */
7342 struct HashElem {
7343   HashElem *next, *prev;       /* Next and previous elements in the table */
7344   void *data;                  /* Data associated with this element */
7345   const char *pKey; int nKey;  /* Key associated with this element */
7346 };
7347 
7348 /*
7349 ** Access routines.  To delete, insert a NULL pointer.
7350 */
7351 SQLITE_PRIVATE void sqlite3HashInit(Hash*);
7352 SQLITE_PRIVATE void *sqlite3HashInsert(Hash*, const char *pKey, int nKey, void *pData);
7353 SQLITE_PRIVATE void *sqlite3HashFind(const Hash*, const char *pKey, int nKey);
7354 SQLITE_PRIVATE void sqlite3HashClear(Hash*);
7355 
7356 /*
7357 ** Macros for looping over all elements of a hash table.  The idiom is
7358 ** like this:
7359 **
7360 **   Hash h;
7361 **   HashElem *p;
7362 **   ...
7363 **   for(p=sqliteHashFirst(&h); p; p=sqliteHashNext(p)){
7364 **     SomeStructure *pData = sqliteHashData(p);
7365 **     // do something with pData
7366 **   }
7367 */
7368 #define sqliteHashFirst(H)  ((H)->first)
7369 #define sqliteHashNext(E)   ((E)->next)
7370 #define sqliteHashData(E)   ((E)->data)
7371 /* #define sqliteHashKey(E)    ((E)->pKey) // NOT USED */
7372 /* #define sqliteHashKeysize(E) ((E)->nKey)  // NOT USED */
7373 
7374 /*
7375 ** Number of entries in a hash table
7376 */
7377 /* #define sqliteHashCount(H)  ((H)->count) // NOT USED */
7378 
7379 #endif /* _SQLITE_HASH_H_ */
7380 
7381 /************** End of hash.h ************************************************/
7382 /************** Continuing where we left off in sqliteInt.h ******************/
7383 /************** Include parse.h in the middle of sqliteInt.h *****************/
7384 /************** Begin file parse.h *******************************************/
7385 #define TK_SEMI                            1
7386 #define TK_EXPLAIN                         2
7387 #define TK_QUERY                           3
7388 #define TK_PLAN                            4
7389 #define TK_BEGIN                           5
7390 #define TK_TRANSACTION                     6
7391 #define TK_DEFERRED                        7
7392 #define TK_IMMEDIATE                       8
7393 #define TK_EXCLUSIVE                       9
7394 #define TK_COMMIT                         10
7395 #define TK_END                            11
7396 #define TK_ROLLBACK                       12
7397 #define TK_SAVEPOINT                      13
7398 #define TK_RELEASE                        14
7399 #define TK_TO                             15
7400 #define TK_TABLE                          16
7401 #define TK_CREATE                         17
7402 #define TK_IF                             18
7403 #define TK_NOT                            19
7404 #define TK_EXISTS                         20
7405 #define TK_TEMP                           21
7406 #define TK_LP                             22
7407 #define TK_RP                             23
7408 #define TK_AS                             24
7409 #define TK_COMMA                          25
7410 #define TK_ID                             26
7411 #define TK_INDEXED                        27
7412 #define TK_ABORT                          28
7413 #define TK_ACTION                         29
7414 #define TK_AFTER                          30
7415 #define TK_ANALYZE                        31
7416 #define TK_ASC                            32
7417 #define TK_ATTACH                         33
7418 #define TK_BEFORE                         34
7419 #define TK_BY                             35
7420 #define TK_CASCADE                        36
7421 #define TK_CAST                           37
7422 #define TK_COLUMNKW                       38
7423 #define TK_CONFLICT                       39
7424 #define TK_DATABASE                       40
7425 #define TK_DESC                           41
7426 #define TK_DETACH                         42
7427 #define TK_EACH                           43
7428 #define TK_FAIL                           44
7429 #define TK_FOR                            45
7430 #define TK_IGNORE                         46
7431 #define TK_INITIALLY                      47
7432 #define TK_INSTEAD                        48
7433 #define TK_LIKE_KW                        49
7434 #define TK_MATCH                          50
7435 #define TK_NO                             51
7436 #define TK_KEY                            52
7437 #define TK_OF                             53
7438 #define TK_OFFSET                         54
7439 #define TK_PRAGMA                         55
7440 #define TK_RAISE                          56
7441 #define TK_REPLACE                        57
7442 #define TK_RESTRICT                       58
7443 #define TK_ROW                            59
7444 #define TK_TRIGGER                        60
7445 #define TK_VACUUM                         61
7446 #define TK_VIEW                           62
7447 #define TK_VIRTUAL                        63
7448 #define TK_REINDEX                        64
7449 #define TK_RENAME                         65
7450 #define TK_CTIME_KW                       66
7451 #define TK_ANY                            67
7452 #define TK_OR                             68
7453 #define TK_AND                            69
7454 #define TK_IS                             70
7455 #define TK_BETWEEN                        71
7456 #define TK_IN                             72
7457 #define TK_ISNULL                         73
7458 #define TK_NOTNULL                        74
7459 #define TK_NE                             75
7460 #define TK_EQ                             76
7461 #define TK_GT                             77
7462 #define TK_LE                             78
7463 #define TK_LT                             79
7464 #define TK_GE                             80
7465 #define TK_ESCAPE                         81
7466 #define TK_BITAND                         82
7467 #define TK_BITOR                          83
7468 #define TK_LSHIFT                         84
7469 #define TK_RSHIFT                         85
7470 #define TK_PLUS                           86
7471 #define TK_MINUS                          87
7472 #define TK_STAR                           88
7473 #define TK_SLASH                          89
7474 #define TK_REM                            90
7475 #define TK_CONCAT                         91
7476 #define TK_COLLATE                        92
7477 #define TK_BITNOT                         93
7478 #define TK_STRING                         94
7479 #define TK_JOIN_KW                        95
7480 #define TK_CONSTRAINT                     96
7481 #define TK_DEFAULT                        97
7482 #define TK_NULL                           98
7483 #define TK_PRIMARY                        99
7484 #define TK_UNIQUE                         100
7485 #define TK_CHECK                          101
7486 #define TK_REFERENCES                     102
7487 #define TK_AUTOINCR                       103
7488 #define TK_ON                             104
7489 #define TK_INSERT                         105
7490 #define TK_DELETE                         106
7491 #define TK_UPDATE                         107
7492 #define TK_SET                            108
7493 #define TK_DEFERRABLE                     109
7494 #define TK_FOREIGN                        110
7495 #define TK_DROP                           111
7496 #define TK_UNION                          112
7497 #define TK_ALL                            113
7498 #define TK_EXCEPT                         114
7499 #define TK_INTERSECT                      115
7500 #define TK_SELECT                         116
7501 #define TK_DISTINCT                       117
7502 #define TK_DOT                            118
7503 #define TK_FROM                           119
7504 #define TK_JOIN                           120
7505 #define TK_USING                          121
7506 #define TK_ORDER                          122
7507 #define TK_GROUP                          123
7508 #define TK_HAVING                         124
7509 #define TK_LIMIT                          125
7510 #define TK_WHERE                          126
7511 #define TK_INTO                           127
7512 #define TK_VALUES                         128
7513 #define TK_INTEGER                        129
7514 #define TK_FLOAT                          130
7515 #define TK_BLOB                           131
7516 #define TK_REGISTER                       132
7517 #define TK_VARIABLE                       133
7518 #define TK_CASE                           134
7519 #define TK_WHEN                           135
7520 #define TK_THEN                           136
7521 #define TK_ELSE                           137
7522 #define TK_INDEX                          138
7523 #define TK_ALTER                          139
7524 #define TK_ADD                            140
7525 #define TK_TO_TEXT                        141
7526 #define TK_TO_BLOB                        142
7527 #define TK_TO_NUMERIC                     143
7528 #define TK_TO_INT                         144
7529 #define TK_TO_REAL                        145
7530 #define TK_ISNOT                          146
7531 #define TK_END_OF_FILE                    147
7532 #define TK_ILLEGAL                        148
7533 #define TK_SPACE                          149
7534 #define TK_UNCLOSED_STRING                150
7535 #define TK_FUNCTION                       151
7536 #define TK_COLUMN                         152
7537 #define TK_AGG_FUNCTION                   153
7538 #define TK_AGG_COLUMN                     154
7539 #define TK_CONST_FUNC                     155
7540 #define TK_UMINUS                         156
7541 #define TK_UPLUS                          157
7542 
7543 /************** End of parse.h ***********************************************/
7544 /************** Continuing where we left off in sqliteInt.h ******************/
7545 #include <stdio.h>
7546 #include <stdlib.h>
7547 #include <string.h>
7548 #include <assert.h>
7549 #include <stddef.h>
7550 
7551 /*
7552 ** If compiling for a processor that lacks floating point support,
7553 ** substitute integer for floating-point
7554 */
7555 #ifdef SQLITE_OMIT_FLOATING_POINT
7556 # define double sqlite_int64
7557 # define float sqlite_int64
7558 # define LONGDOUBLE_TYPE sqlite_int64
7559 # ifndef SQLITE_BIG_DBL
7560 #   define SQLITE_BIG_DBL (((sqlite3_int64)1)<<50)
7561 # endif
7562 # define SQLITE_OMIT_DATETIME_FUNCS 1
7563 # define SQLITE_OMIT_TRACE 1
7564 # undef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
7565 # undef SQLITE_HAVE_ISNAN
7566 #endif
7567 #ifndef SQLITE_BIG_DBL
7568 # define SQLITE_BIG_DBL (1e99)
7569 #endif
7570 
7571 /*
7572 ** OMIT_TEMPDB is set to 1 if SQLITE_OMIT_TEMPDB is defined, or 0
7573 ** afterward. Having this macro allows us to cause the C compiler
7574 ** to omit code used by TEMP tables without messy #ifndef statements.
7575 */
7576 #ifdef SQLITE_OMIT_TEMPDB
7577 #define OMIT_TEMPDB 1
7578 #else
7579 #define OMIT_TEMPDB 0
7580 #endif
7581 
7582 /*
7583 ** The "file format" number is an integer that is incremented whenever
7584 ** the VDBE-level file format changes.  The following macros define the
7585 ** the default file format for new databases and the maximum file format
7586 ** that the library can read.
7587 */
7588 #define SQLITE_MAX_FILE_FORMAT 4
7589 #ifndef SQLITE_DEFAULT_FILE_FORMAT
7590 # define SQLITE_DEFAULT_FILE_FORMAT 1
7591 #endif
7592 
7593 /*
7594 ** Determine whether triggers are recursive by default.  This can be
7595 ** changed at run-time using a pragma.
7596 */
7597 #ifndef SQLITE_DEFAULT_RECURSIVE_TRIGGERS
7598 # define SQLITE_DEFAULT_RECURSIVE_TRIGGERS 0
7599 #endif
7600 
7601 /*
7602 ** Provide a default value for SQLITE_TEMP_STORE in case it is not specified
7603 ** on the command-line
7604 */
7605 #ifndef SQLITE_TEMP_STORE
7606 # define SQLITE_TEMP_STORE 1
7607 #endif
7608 
7609 /*
7610 ** GCC does not define the offsetof() macro so we'll have to do it
7611 ** ourselves.
7612 */
7613 #ifndef offsetof
7614 #define offsetof(STRUCTURE,FIELD) ((int)((char*)&((STRUCTURE*)0)->FIELD))
7615 #endif
7616 
7617 /*
7618 ** Check to see if this machine uses EBCDIC.  (Yes, believe it or
7619 ** not, there are still machines out there that use EBCDIC.)
7620 */
7621 #if 'A' == '\301'
7622 # define SQLITE_EBCDIC 1
7623 #else
7624 # define SQLITE_ASCII 1
7625 #endif
7626 
7627 /*
7628 ** Integers of known sizes.  These typedefs might change for architectures
7629 ** where the sizes very.  Preprocessor macros are available so that the
7630 ** types can be conveniently redefined at compile-type.  Like this:
7631 **
7632 **         cc '-DUINTPTR_TYPE=long long int' ...
7633 */
7634 #ifndef UINT32_TYPE
7635 # ifdef HAVE_UINT32_T
7636 #  define UINT32_TYPE uint32_t
7637 # else
7638 #  define UINT32_TYPE unsigned int
7639 # endif
7640 #endif
7641 #ifndef UINT16_TYPE
7642 # ifdef HAVE_UINT16_T
7643 #  define UINT16_TYPE uint16_t
7644 # else
7645 #  define UINT16_TYPE unsigned short int
7646 # endif
7647 #endif
7648 #ifndef INT16_TYPE
7649 # ifdef HAVE_INT16_T
7650 #  define INT16_TYPE int16_t
7651 # else
7652 #  define INT16_TYPE short int
7653 # endif
7654 #endif
7655 #ifndef UINT8_TYPE
7656 # ifdef HAVE_UINT8_T
7657 #  define UINT8_TYPE uint8_t
7658 # else
7659 #  define UINT8_TYPE unsigned char
7660 # endif
7661 #endif
7662 #ifndef INT8_TYPE
7663 # ifdef HAVE_INT8_T
7664 #  define INT8_TYPE int8_t
7665 # else
7666 #  define INT8_TYPE signed char
7667 # endif
7668 #endif
7669 #ifndef LONGDOUBLE_TYPE
7670 # define LONGDOUBLE_TYPE long double
7671 #endif
7672 typedef sqlite_int64 i64;          /* 8-byte signed integer */
7673 typedef sqlite_uint64 u64;         /* 8-byte unsigned integer */
7674 typedef UINT32_TYPE u32;           /* 4-byte unsigned integer */
7675 typedef UINT16_TYPE u16;           /* 2-byte unsigned integer */
7676 typedef INT16_TYPE i16;            /* 2-byte signed integer */
7677 typedef UINT8_TYPE u8;             /* 1-byte unsigned integer */
7678 typedef INT8_TYPE i8;              /* 1-byte signed integer */
7679 
7680 /*
7681 ** SQLITE_MAX_U32 is a u64 constant that is the maximum u64 value
7682 ** that can be stored in a u32 without loss of data.  The value
7683 ** is 0x00000000ffffffff.  But because of quirks of some compilers, we
7684 ** have to specify the value in the less intuitive manner shown:
7685 */
7686 #define SQLITE_MAX_U32  ((((u64)1)<<32)-1)
7687 
7688 /*
7689 ** Macros to determine whether the machine is big or little endian,
7690 ** evaluated at runtime.
7691 */
7692 #ifdef SQLITE_AMALGAMATION
7693 SQLITE_PRIVATE const int sqlite3one = 1;
7694 #else
7695 SQLITE_PRIVATE const int sqlite3one;
7696 #endif
7697 #if defined(i386) || defined(__i386__) || defined(_M_IX86)\
7698                              || defined(__x86_64) || defined(__x86_64__)
7699 # define SQLITE_BIGENDIAN    0
7700 # define SQLITE_LITTLEENDIAN 1
7701 # define SQLITE_UTF16NATIVE  SQLITE_UTF16LE
7702 #else
7703 # define SQLITE_BIGENDIAN    (*(char *)(&sqlite3one)==0)
7704 # define SQLITE_LITTLEENDIAN (*(char *)(&sqlite3one)==1)
7705 # define SQLITE_UTF16NATIVE (SQLITE_BIGENDIAN?SQLITE_UTF16BE:SQLITE_UTF16LE)
7706 #endif
7707 
7708 /*
7709 ** Constants for the largest and smallest possible 64-bit signed integers.
7710 ** These macros are designed to work correctly on both 32-bit and 64-bit
7711 ** compilers.
7712 */
7713 #define LARGEST_INT64  (0xffffffff|(((i64)0x7fffffff)<<32))
7714 #define SMALLEST_INT64 (((i64)-1) - LARGEST_INT64)
7715 
7716 /*
7717 ** Round up a number to the next larger multiple of 8.  This is used
7718 ** to force 8-byte alignment on 64-bit architectures.
7719 */
7720 #define ROUND8(x)     (((x)+7)&~7)
7721 
7722 /*
7723 ** Round down to the nearest multiple of 8
7724 */
7725 #define ROUNDDOWN8(x) ((x)&~7)
7726 
7727 /*
7728 ** Assert that the pointer X is aligned to an 8-byte boundary.  This
7729 ** macro is used only within assert() to verify that the code gets
7730 ** all alignment restrictions correct.
7731 **
7732 ** Except, if SQLITE_4_BYTE_ALIGNED_MALLOC is defined, then the
7733 ** underlying malloc() implemention might return us 4-byte aligned
7734 ** pointers.  In that case, only verify 4-byte alignment.
7735 */
7736 #ifdef SQLITE_4_BYTE_ALIGNED_MALLOC
7737 # define EIGHT_BYTE_ALIGNMENT(X)   ((((char*)(X) - (char*)0)&3)==0)
7738 #else
7739 # define EIGHT_BYTE_ALIGNMENT(X)   ((((char*)(X) - (char*)0)&7)==0)
7740 #endif
7741 
7742 
7743 /*
7744 ** An instance of the following structure is used to store the busy-handler
7745 ** callback for a given sqlite handle.
7746 **
7747 ** The sqlite.busyHandler member of the sqlite struct contains the busy
7748 ** callback for the database handle. Each pager opened via the sqlite
7749 ** handle is passed a pointer to sqlite.busyHandler. The busy-handler
7750 ** callback is currently invoked only from within pager.c.
7751 */
7752 typedef struct BusyHandler BusyHandler;
7753 struct BusyHandler {
7754   int (*xFunc)(void *,int);  /* The busy callback */
7755   void *pArg;                /* First arg to busy callback */
7756   int nBusy;                 /* Incremented with each busy call */
7757 };
7758 
7759 /*
7760 ** Name of the master database table.  The master database table
7761 ** is a special table that holds the names and attributes of all
7762 ** user tables and indices.
7763 */
7764 #define MASTER_NAME       "sqlite_master"
7765 #define TEMP_MASTER_NAME  "sqlite_temp_master"
7766 
7767 /*
7768 ** The root-page of the master database table.
7769 */
7770 #define MASTER_ROOT       1
7771 
7772 /*
7773 ** The name of the schema table.
7774 */
7775 #define SCHEMA_TABLE(x)  ((!OMIT_TEMPDB)&&(x==1)?TEMP_MASTER_NAME:MASTER_NAME)
7776 
7777 /*
7778 ** A convenience macro that returns the number of elements in
7779 ** an array.
7780 */
7781 #define ArraySize(X)    ((int)(sizeof(X)/sizeof(X[0])))
7782 
7783 /*
7784 ** The following value as a destructor means to use sqlite3DbFree().
7785 ** This is an internal extension to SQLITE_STATIC and SQLITE_TRANSIENT.
7786 */
7787 #define SQLITE_DYNAMIC   ((sqlite3_destructor_type)sqlite3DbFree)
7788 
7789 /*
7790 ** When SQLITE_OMIT_WSD is defined, it means that the target platform does
7791 ** not support Writable Static Data (WSD) such as global and static variables.
7792 ** All variables must either be on the stack or dynamically allocated from
7793 ** the heap.  When WSD is unsupported, the variable declarations scattered
7794 ** throughout the SQLite code must become constants instead.  The SQLITE_WSD
7795 ** macro is used for this purpose.  And instead of referencing the variable
7796 ** directly, we use its constant as a key to lookup the run-time allocated
7797 ** buffer that holds real variable.  The constant is also the initializer
7798 ** for the run-time allocated buffer.
7799 **
7800 ** In the usual case where WSD is supported, the SQLITE_WSD and GLOBAL
7801 ** macros become no-ops and have zero performance impact.
7802 */
7803 #ifdef SQLITE_OMIT_WSD
7804   #define SQLITE_WSD const
7805   #define GLOBAL(t,v) (*(t*)sqlite3_wsd_find((void*)&(v), sizeof(v)))
7806   #define sqlite3GlobalConfig GLOBAL(struct Sqlite3Config, sqlite3Config)
7807 SQLITE_API   int sqlite3_wsd_init(int N, int J);
7808 SQLITE_API   void *sqlite3_wsd_find(void *K, int L);
7809 #else
7810   #define SQLITE_WSD
7811   #define GLOBAL(t,v) v
7812   #define sqlite3GlobalConfig sqlite3Config
7813 #endif
7814 
7815 /*
7816 ** The following macros are used to suppress compiler warnings and to
7817 ** make it clear to human readers when a function parameter is deliberately
7818 ** left unused within the body of a function. This usually happens when
7819 ** a function is called via a function pointer. For example the
7820 ** implementation of an SQL aggregate step callback may not use the
7821 ** parameter indicating the number of arguments passed to the aggregate,
7822 ** if it knows that this is enforced elsewhere.
7823 **
7824 ** When a function parameter is not used at all within the body of a function,
7825 ** it is generally named "NotUsed" or "NotUsed2" to make things even clearer.
7826 ** However, these macros may also be used to suppress warnings related to
7827 ** parameters that may or may not be used depending on compilation options.
7828 ** For example those parameters only used in assert() statements. In these
7829 ** cases the parameters are named as per the usual conventions.
7830 */
7831 #define UNUSED_PARAMETER(x) (void)(x)
7832 #define UNUSED_PARAMETER2(x,y) UNUSED_PARAMETER(x),UNUSED_PARAMETER(y)
7833 
7834 /*
7835 ** Forward references to structures
7836 */
7837 typedef struct AggInfo AggInfo;
7838 typedef struct AuthContext AuthContext;
7839 typedef struct AutoincInfo AutoincInfo;
7840 typedef struct Bitvec Bitvec;
7841 typedef struct CollSeq CollSeq;
7842 typedef struct Column Column;
7843 typedef struct Db Db;
7844 typedef struct Schema Schema;
7845 typedef struct Expr Expr;
7846 typedef struct ExprList ExprList;
7847 typedef struct ExprSpan ExprSpan;
7848 typedef struct FKey FKey;
7849 typedef struct FuncDestructor FuncDestructor;
7850 typedef struct FuncDef FuncDef;
7851 typedef struct FuncDefHash FuncDefHash;
7852 typedef struct IdList IdList;
7853 typedef struct Index Index;
7854 typedef struct IndexSample IndexSample;
7855 typedef struct KeyClass KeyClass;
7856 typedef struct KeyInfo KeyInfo;
7857 typedef struct Lookaside Lookaside;
7858 typedef struct LookasideSlot LookasideSlot;
7859 typedef struct Module Module;
7860 typedef struct NameContext NameContext;
7861 typedef struct Parse Parse;
7862 typedef struct RowSet RowSet;
7863 typedef struct Savepoint Savepoint;
7864 typedef struct Select Select;
7865 typedef struct SrcList SrcList;
7866 typedef struct StrAccum StrAccum;
7867 typedef struct Table Table;
7868 typedef struct TableLock TableLock;
7869 typedef struct Token Token;
7870 typedef struct Trigger Trigger;
7871 typedef struct TriggerPrg TriggerPrg;
7872 typedef struct TriggerStep TriggerStep;
7873 typedef struct UnpackedRecord UnpackedRecord;
7874 typedef struct VTable VTable;
7875 typedef struct VtabCtx VtabCtx;
7876 typedef struct Walker Walker;
7877 typedef struct WherePlan WherePlan;
7878 typedef struct WhereInfo WhereInfo;
7879 typedef struct WhereLevel WhereLevel;
7880 
7881 /*
7882 ** Defer sourcing vdbe.h and btree.h until after the "u8" and
7883 ** "BusyHandler" typedefs. vdbe.h also requires a few of the opaque
7884 ** pointer types (i.e. FuncDef) defined above.
7885 */
7886 /************** Include btree.h in the middle of sqliteInt.h *****************/
7887 /************** Begin file btree.h *******************************************/
7888 /*
7889 ** 2001 September 15
7890 **
7891 ** The author disclaims copyright to this source code.  In place of
7892 ** a legal notice, here is a blessing:
7893 **
7894 **    May you do good and not evil.
7895 **    May you find forgiveness for yourself and forgive others.
7896 **    May you share freely, never taking more than you give.
7897 **
7898 *************************************************************************
7899 ** This header file defines the interface that the sqlite B-Tree file
7900 ** subsystem.  See comments in the source code for a detailed description
7901 ** of what each interface routine does.
7902 */
7903 #ifndef _BTREE_H_
7904 #define _BTREE_H_
7905 
7906 /* TODO: This definition is just included so other modules compile. It
7907 ** needs to be revisited.
7908 */
7909 #define SQLITE_N_BTREE_META 10
7910 
7911 /*
7912 ** If defined as non-zero, auto-vacuum is enabled by default. Otherwise
7913 ** it must be turned on for each database using "PRAGMA auto_vacuum = 1".
7914 */
7915 #ifndef SQLITE_DEFAULT_AUTOVACUUM
7916   #define SQLITE_DEFAULT_AUTOVACUUM 0
7917 #endif
7918 
7919 #define BTREE_AUTOVACUUM_NONE 0        /* Do not do auto-vacuum */
7920 #define BTREE_AUTOVACUUM_FULL 1        /* Do full auto-vacuum */
7921 #define BTREE_AUTOVACUUM_INCR 2        /* Incremental vacuum */
7922 
7923 /*
7924 ** Forward declarations of structure
7925 */
7926 typedef struct Btree Btree;
7927 typedef struct BtCursor BtCursor;
7928 typedef struct BtShared BtShared;
7929 
7930 
7931 SQLITE_PRIVATE int sqlite3BtreeOpen(
7932   sqlite3_vfs *pVfs,       /* VFS to use with this b-tree */
7933   const char *zFilename,   /* Name of database file to open */
7934   sqlite3 *db,             /* Associated database connection */
7935   Btree **ppBtree,         /* Return open Btree* here */
7936   int flags,               /* Flags */
7937   int vfsFlags             /* Flags passed through to VFS open */
7938 );
7939 
7940 /* The flags parameter to sqlite3BtreeOpen can be the bitwise or of the
7941 ** following values.
7942 **
7943 ** NOTE:  These values must match the corresponding PAGER_ values in
7944 ** pager.h.
7945 */
7946 #define BTREE_OMIT_JOURNAL  1  /* Do not create or use a rollback journal */
7947 #define BTREE_NO_READLOCK   2  /* Omit readlocks on readonly files */
7948 #define BTREE_MEMORY        4  /* This is an in-memory DB */
7949 #define BTREE_SINGLE        8  /* The file contains at most 1 b-tree */
7950 #define BTREE_UNORDERED    16  /* Use of a hash implementation is OK */
7951 
7952 SQLITE_PRIVATE int sqlite3BtreeClose(Btree*);
7953 SQLITE_PRIVATE int sqlite3BtreeSetCacheSize(Btree*,int);
7954 SQLITE_PRIVATE int sqlite3BtreeSetSafetyLevel(Btree*,int,int,int);
7955 SQLITE_PRIVATE int sqlite3BtreeSyncDisabled(Btree*);
7956 SQLITE_PRIVATE int sqlite3BtreeSetPageSize(Btree *p, int nPagesize, int nReserve, int eFix);
7957 SQLITE_PRIVATE int sqlite3BtreeGetPageSize(Btree*);
7958 SQLITE_PRIVATE int sqlite3BtreeMaxPageCount(Btree*,int);
7959 SQLITE_PRIVATE u32 sqlite3BtreeLastPage(Btree*);
7960 SQLITE_PRIVATE int sqlite3BtreeSecureDelete(Btree*,int);
7961 SQLITE_PRIVATE int sqlite3BtreeGetReserve(Btree*);
7962 SQLITE_PRIVATE int sqlite3BtreeSetAutoVacuum(Btree *, int);
7963 SQLITE_PRIVATE int sqlite3BtreeGetAutoVacuum(Btree *);
7964 SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree*,int);
7965 SQLITE_PRIVATE int sqlite3BtreeCommitPhaseOne(Btree*, const char *zMaster);
7966 SQLITE_PRIVATE int sqlite3BtreeCommitPhaseTwo(Btree*, int);
7967 SQLITE_PRIVATE int sqlite3BtreeCommit(Btree*);
7968 SQLITE_PRIVATE int sqlite3BtreeRollback(Btree*);
7969 SQLITE_PRIVATE int sqlite3BtreeBeginStmt(Btree*,int);
7970 SQLITE_PRIVATE int sqlite3BtreeCreateTable(Btree*, int*, int flags);
7971 SQLITE_PRIVATE int sqlite3BtreeIsInTrans(Btree*);
7972 SQLITE_PRIVATE int sqlite3BtreeIsInReadTrans(Btree*);
7973 SQLITE_PRIVATE int sqlite3BtreeIsInBackup(Btree*);
7974 SQLITE_PRIVATE void *sqlite3BtreeSchema(Btree *, int, void(*)(void *));
7975 SQLITE_PRIVATE int sqlite3BtreeSchemaLocked(Btree *pBtree);
7976 SQLITE_PRIVATE int sqlite3BtreeLockTable(Btree *pBtree, int iTab, u8 isWriteLock);
7977 SQLITE_PRIVATE int sqlite3BtreeSavepoint(Btree *, int, int);
7978 
7979 SQLITE_PRIVATE const char *sqlite3BtreeGetFilename(Btree *);
7980 SQLITE_PRIVATE const char *sqlite3BtreeGetJournalname(Btree *);
7981 SQLITE_PRIVATE int sqlite3BtreeCopyFile(Btree *, Btree *);
7982 
7983 SQLITE_PRIVATE int sqlite3BtreeIncrVacuum(Btree *);
7984 
7985 /* The flags parameter to sqlite3BtreeCreateTable can be the bitwise OR
7986 ** of the flags shown below.
7987 **
7988 ** Every SQLite table must have either BTREE_INTKEY or BTREE_BLOBKEY set.
7989 ** With BTREE_INTKEY, the table key is a 64-bit integer and arbitrary data
7990 ** is stored in the leaves.  (BTREE_INTKEY is used for SQL tables.)  With
7991 ** BTREE_BLOBKEY, the key is an arbitrary BLOB and no content is stored
7992 ** anywhere - the key is the content.  (BTREE_BLOBKEY is used for SQL
7993 ** indices.)
7994 */
7995 #define BTREE_INTKEY     1    /* Table has only 64-bit signed integer keys */
7996 #define BTREE_BLOBKEY    2    /* Table has keys only - no data */
7997 
7998 SQLITE_PRIVATE int sqlite3BtreeDropTable(Btree*, int, int*);
7999 SQLITE_PRIVATE int sqlite3BtreeClearTable(Btree*, int, int*);
8000 SQLITE_PRIVATE void sqlite3BtreeTripAllCursors(Btree*, int);
8001 
8002 SQLITE_PRIVATE void sqlite3BtreeGetMeta(Btree *pBtree, int idx, u32 *pValue);
8003 SQLITE_PRIVATE int sqlite3BtreeUpdateMeta(Btree*, int idx, u32 value);
8004 
8005 /*
8006 ** The second parameter to sqlite3BtreeGetMeta or sqlite3BtreeUpdateMeta
8007 ** should be one of the following values. The integer values are assigned
8008 ** to constants so that the offset of the corresponding field in an
8009 ** SQLite database header may be found using the following formula:
8010 **
8011 **   offset = 36 + (idx * 4)
8012 **
8013 ** For example, the free-page-count field is located at byte offset 36 of
8014 ** the database file header. The incr-vacuum-flag field is located at
8015 ** byte offset 64 (== 36+4*7).
8016 */
8017 #define BTREE_FREE_PAGE_COUNT     0
8018 #define BTREE_SCHEMA_VERSION      1
8019 #define BTREE_FILE_FORMAT         2
8020 #define BTREE_DEFAULT_CACHE_SIZE  3
8021 #define BTREE_LARGEST_ROOT_PAGE   4
8022 #define BTREE_TEXT_ENCODING       5
8023 #define BTREE_USER_VERSION        6
8024 #define BTREE_INCR_VACUUM         7
8025 
8026 SQLITE_PRIVATE int sqlite3BtreeCursor(
8027   Btree*,                              /* BTree containing table to open */
8028   int iTable,                          /* Index of root page */
8029   int wrFlag,                          /* 1 for writing.  0 for read-only */
8030   struct KeyInfo*,                     /* First argument to compare function */
8031   BtCursor *pCursor                    /* Space to write cursor structure */
8032 );
8033 SQLITE_PRIVATE int sqlite3BtreeCursorSize(void);
8034 SQLITE_PRIVATE void sqlite3BtreeCursorZero(BtCursor*);
8035 
8036 SQLITE_PRIVATE int sqlite3BtreeCloseCursor(BtCursor*);
8037 SQLITE_PRIVATE int sqlite3BtreeMovetoUnpacked(
8038   BtCursor*,
8039   UnpackedRecord *pUnKey,
8040   i64 intKey,
8041   int bias,
8042   int *pRes
8043 );
8044 SQLITE_PRIVATE int sqlite3BtreeCursorHasMoved(BtCursor*, int*);
8045 SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor*);
8046 SQLITE_PRIVATE int sqlite3BtreeInsert(BtCursor*, const void *pKey, i64 nKey,
8047                                   const void *pData, int nData,
8048                                   int nZero, int bias, int seekResult);
8049 SQLITE_PRIVATE int sqlite3BtreeFirst(BtCursor*, int *pRes);
8050 SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor*, int *pRes);
8051 SQLITE_PRIVATE int sqlite3BtreeNext(BtCursor*, int *pRes);
8052 SQLITE_PRIVATE int sqlite3BtreeEof(BtCursor*);
8053 SQLITE_PRIVATE int sqlite3BtreePrevious(BtCursor*, int *pRes);
8054 SQLITE_PRIVATE int sqlite3BtreeKeySize(BtCursor*, i64 *pSize);
8055 SQLITE_PRIVATE int sqlite3BtreeKey(BtCursor*, u32 offset, u32 amt, void*);
8056 SQLITE_PRIVATE const void *sqlite3BtreeKeyFetch(BtCursor*, int *pAmt);
8057 SQLITE_PRIVATE const void *sqlite3BtreeDataFetch(BtCursor*, int *pAmt);
8058 SQLITE_PRIVATE int sqlite3BtreeDataSize(BtCursor*, u32 *pSize);
8059 SQLITE_PRIVATE int sqlite3BtreeData(BtCursor*, u32 offset, u32 amt, void*);
8060 SQLITE_PRIVATE void sqlite3BtreeSetCachedRowid(BtCursor*, sqlite3_int64);
8061 SQLITE_PRIVATE sqlite3_int64 sqlite3BtreeGetCachedRowid(BtCursor*);
8062 
8063 SQLITE_PRIVATE char *sqlite3BtreeIntegrityCheck(Btree*, int *aRoot, int nRoot, int, int*);
8064 SQLITE_PRIVATE struct Pager *sqlite3BtreePager(Btree*);
8065 
8066 SQLITE_PRIVATE int sqlite3BtreePutData(BtCursor*, u32 offset, u32 amt, void*);
8067 SQLITE_PRIVATE void sqlite3BtreeCacheOverflow(BtCursor *);
8068 SQLITE_PRIVATE void sqlite3BtreeClearCursor(BtCursor *);
8069 
8070 SQLITE_PRIVATE int sqlite3BtreeSetVersion(Btree *pBt, int iVersion);
8071 
8072 #ifndef NDEBUG
8073 SQLITE_PRIVATE int sqlite3BtreeCursorIsValid(BtCursor*);
8074 #endif
8075 
8076 #ifndef SQLITE_OMIT_BTREECOUNT
8077 SQLITE_PRIVATE int sqlite3BtreeCount(BtCursor *, i64 *);
8078 #endif
8079 
8080 #ifdef SQLITE_TEST
8081 SQLITE_PRIVATE int sqlite3BtreeCursorInfo(BtCursor*, int*, int);
8082 SQLITE_PRIVATE void sqlite3BtreeCursorList(Btree*);
8083 #endif
8084 
8085 #ifndef SQLITE_OMIT_WAL
8086 SQLITE_PRIVATE   int sqlite3BtreeCheckpoint(Btree*, int, int *, int *);
8087 #endif
8088 
8089 /*
8090 ** If we are not using shared cache, then there is no need to
8091 ** use mutexes to access the BtShared structures.  So make the
8092 ** Enter and Leave procedures no-ops.
8093 */
8094 #ifndef SQLITE_OMIT_SHARED_CACHE
8095 SQLITE_PRIVATE   void sqlite3BtreeEnter(Btree*);
8096 SQLITE_PRIVATE   void sqlite3BtreeEnterAll(sqlite3*);
8097 #else
8098 # define sqlite3BtreeEnter(X)
8099 # define sqlite3BtreeEnterAll(X)
8100 #endif
8101 
8102 #if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE
8103 SQLITE_PRIVATE   int sqlite3BtreeSharable(Btree*);
8104 SQLITE_PRIVATE   void sqlite3BtreeLeave(Btree*);
8105 SQLITE_PRIVATE   void sqlite3BtreeEnterCursor(BtCursor*);
8106 SQLITE_PRIVATE   void sqlite3BtreeLeaveCursor(BtCursor*);
8107 SQLITE_PRIVATE   void sqlite3BtreeLeaveAll(sqlite3*);
8108 #ifndef NDEBUG
8109   /* These routines are used inside assert() statements only. */
8110 SQLITE_PRIVATE   int sqlite3BtreeHoldsMutex(Btree*);
8111 SQLITE_PRIVATE   int sqlite3BtreeHoldsAllMutexes(sqlite3*);
8112 SQLITE_PRIVATE   int sqlite3SchemaMutexHeld(sqlite3*,int,Schema*);
8113 #endif
8114 #else
8115 
8116 # define sqlite3BtreeSharable(X) 0
8117 # define sqlite3BtreeLeave(X)
8118 # define sqlite3BtreeEnterCursor(X)
8119 # define sqlite3BtreeLeaveCursor(X)
8120 # define sqlite3BtreeLeaveAll(X)
8121 
8122 # define sqlite3BtreeHoldsMutex(X) 1
8123 # define sqlite3BtreeHoldsAllMutexes(X) 1
8124 # define sqlite3SchemaMutexHeld(X,Y,Z) 1
8125 #endif
8126 
8127 
8128 #endif /* _BTREE_H_ */
8129 
8130 /************** End of btree.h ***********************************************/
8131 /************** Continuing where we left off in sqliteInt.h ******************/
8132 /************** Include vdbe.h in the middle of sqliteInt.h ******************/
8133 /************** Begin file vdbe.h ********************************************/
8134 /*
8135 ** 2001 September 15
8136 **
8137 ** The author disclaims copyright to this source code.  In place of
8138 ** a legal notice, here is a blessing:
8139 **
8140 **    May you do good and not evil.
8141 **    May you find forgiveness for yourself and forgive others.
8142 **    May you share freely, never taking more than you give.
8143 **
8144 *************************************************************************
8145 ** Header file for the Virtual DataBase Engine (VDBE)
8146 **
8147 ** This header defines the interface to the virtual database engine
8148 ** or VDBE.  The VDBE implements an abstract machine that runs a
8149 ** simple program to access and modify the underlying database.
8150 */
8151 #ifndef _SQLITE_VDBE_H_
8152 #define _SQLITE_VDBE_H_
8153 
8154 /*
8155 ** A single VDBE is an opaque structure named "Vdbe".  Only routines
8156 ** in the source file sqliteVdbe.c are allowed to see the insides
8157 ** of this structure.
8158 */
8159 typedef struct Vdbe Vdbe;
8160 
8161 /*
8162 ** The names of the following types declared in vdbeInt.h are required
8163 ** for the VdbeOp definition.
8164 */
8165 typedef struct VdbeFunc VdbeFunc;
8166 typedef struct Mem Mem;
8167 typedef struct SubProgram SubProgram;
8168 
8169 /*
8170 ** A single instruction of the virtual machine has an opcode
8171 ** and as many as three operands.  The instruction is recorded
8172 ** as an instance of the following structure:
8173 */
8174 struct VdbeOp {
8175   u8 opcode;          /* What operation to perform */
8176   signed char p4type; /* One of the P4_xxx constants for p4 */
8177   u8 opflags;         /* Mask of the OPFLG_* flags in opcodes.h */
8178   u8 p5;              /* Fifth parameter is an unsigned character */
8179   int p1;             /* First operand */
8180   int p2;             /* Second parameter (often the jump destination) */
8181   int p3;             /* The third parameter */
8182   union {             /* fourth parameter */
8183     int i;                 /* Integer value if p4type==P4_INT32 */
8184     void *p;               /* Generic pointer */
8185     char *z;               /* Pointer to data for string (char array) types */
8186     i64 *pI64;             /* Used when p4type is P4_INT64 */
8187     double *pReal;         /* Used when p4type is P4_REAL */
8188     FuncDef *pFunc;        /* Used when p4type is P4_FUNCDEF */
8189     VdbeFunc *pVdbeFunc;   /* Used when p4type is P4_VDBEFUNC */
8190     CollSeq *pColl;        /* Used when p4type is P4_COLLSEQ */
8191     Mem *pMem;             /* Used when p4type is P4_MEM */
8192     VTable *pVtab;         /* Used when p4type is P4_VTAB */
8193     KeyInfo *pKeyInfo;     /* Used when p4type is P4_KEYINFO */
8194     int *ai;               /* Used when p4type is P4_INTARRAY */
8195     SubProgram *pProgram;  /* Used when p4type is P4_SUBPROGRAM */
8196   } p4;
8197 #ifdef SQLITE_DEBUG
8198   char *zComment;          /* Comment to improve readability */
8199 #endif
8200 #ifdef VDBE_PROFILE
8201   int cnt;                 /* Number of times this instruction was executed */
8202   u64 cycles;              /* Total time spent executing this instruction */
8203 #endif
8204 };
8205 typedef struct VdbeOp VdbeOp;
8206 
8207 
8208 /*
8209 ** A sub-routine used to implement a trigger program.
8210 */
8211 struct SubProgram {
8212   VdbeOp *aOp;                  /* Array of opcodes for sub-program */
8213   int nOp;                      /* Elements in aOp[] */
8214   int nMem;                     /* Number of memory cells required */
8215   int nCsr;                     /* Number of cursors required */
8216   void *token;                  /* id that may be used to recursive triggers */
8217   SubProgram *pNext;            /* Next sub-program already visited */
8218 };
8219 
8220 /*
8221 ** A smaller version of VdbeOp used for the VdbeAddOpList() function because
8222 ** it takes up less space.
8223 */
8224 struct VdbeOpList {
8225   u8 opcode;          /* What operation to perform */
8226   signed char p1;     /* First operand */
8227   signed char p2;     /* Second parameter (often the jump destination) */
8228   signed char p3;     /* Third parameter */
8229 };
8230 typedef struct VdbeOpList VdbeOpList;
8231 
8232 /*
8233 ** Allowed values of VdbeOp.p4type
8234 */
8235 #define P4_NOTUSED    0   /* The P4 parameter is not used */
8236 #define P4_DYNAMIC  (-1)  /* Pointer to a string obtained from sqliteMalloc() */
8237 #define P4_STATIC   (-2)  /* Pointer to a static string */
8238 #define P4_COLLSEQ  (-4)  /* P4 is a pointer to a CollSeq structure */
8239 #define P4_FUNCDEF  (-5)  /* P4 is a pointer to a FuncDef structure */
8240 #define P4_KEYINFO  (-6)  /* P4 is a pointer to a KeyInfo structure */
8241 #define P4_VDBEFUNC (-7)  /* P4 is a pointer to a VdbeFunc structure */
8242 #define P4_MEM      (-8)  /* P4 is a pointer to a Mem*    structure */
8243 #define P4_TRANSIENT  0   /* P4 is a pointer to a transient string */
8244 #define P4_VTAB     (-10) /* P4 is a pointer to an sqlite3_vtab structure */
8245 #define P4_MPRINTF  (-11) /* P4 is a string obtained from sqlite3_mprintf() */
8246 #define P4_REAL     (-12) /* P4 is a 64-bit floating point value */
8247 #define P4_INT64    (-13) /* P4 is a 64-bit signed integer */
8248 #define P4_INT32    (-14) /* P4 is a 32-bit signed integer */
8249 #define P4_INTARRAY (-15) /* P4 is a vector of 32-bit integers */
8250 #define P4_SUBPROGRAM  (-18) /* P4 is a pointer to a SubProgram structure */
8251 
8252 /* When adding a P4 argument using P4_KEYINFO, a copy of the KeyInfo structure
8253 ** is made.  That copy is freed when the Vdbe is finalized.  But if the
8254 ** argument is P4_KEYINFO_HANDOFF, the passed in pointer is used.  It still
8255 ** gets freed when the Vdbe is finalized so it still should be obtained
8256 ** from a single sqliteMalloc().  But no copy is made and the calling
8257 ** function should *not* try to free the KeyInfo.
8258 */
8259 #define P4_KEYINFO_HANDOFF (-16)
8260 #define P4_KEYINFO_STATIC  (-17)
8261 
8262 /*
8263 ** The Vdbe.aColName array contains 5n Mem structures, where n is the
8264 ** number of columns of data returned by the statement.
8265 */
8266 #define COLNAME_NAME     0
8267 #define COLNAME_DECLTYPE 1
8268 #define COLNAME_DATABASE 2
8269 #define COLNAME_TABLE    3
8270 #define COLNAME_COLUMN   4
8271 #ifdef SQLITE_ENABLE_COLUMN_METADATA
8272 # define COLNAME_N        5      /* Number of COLNAME_xxx symbols */
8273 #else
8274 # ifdef SQLITE_OMIT_DECLTYPE
8275 #   define COLNAME_N      1      /* Store only the name */
8276 # else
8277 #   define COLNAME_N      2      /* Store the name and decltype */
8278 # endif
8279 #endif
8280 
8281 /*
8282 ** The following macro converts a relative address in the p2 field
8283 ** of a VdbeOp structure into a negative number so that
8284 ** sqlite3VdbeAddOpList() knows that the address is relative.  Calling
8285 ** the macro again restores the address.
8286 */
8287 #define ADDR(X)  (-1-(X))
8288 
8289 /*
8290 ** The makefile scans the vdbe.c source file and creates the "opcodes.h"
8291 ** header file that defines a number for each opcode used by the VDBE.
8292 */
8293 /************** Include opcodes.h in the middle of vdbe.h ********************/
8294 /************** Begin file opcodes.h *****************************************/
8295 /* Automatically generated.  Do not edit */
8296 /* See the mkopcodeh.awk script for details */
8297 #define OP_Goto                                 1
8298 #define OP_Gosub                                2
8299 #define OP_Return                               3
8300 #define OP_Yield                                4
8301 #define OP_HaltIfNull                           5
8302 #define OP_Halt                                 6
8303 #define OP_Integer                              7
8304 #define OP_Int64                                8
8305 #define OP_Real                               130   /* same as TK_FLOAT    */
8306 #define OP_String8                             94   /* same as TK_STRING   */
8307 #define OP_String                               9
8308 #define OP_Null                                10
8309 #define OP_Blob                                11
8310 #define OP_Variable                            12
8311 #define OP_Move                                13
8312 #define OP_Copy                                14
8313 #define OP_SCopy                               15
8314 #define OP_ResultRow                           16
8315 #define OP_Concat                              91   /* same as TK_CONCAT   */
8316 #define OP_Add                                 86   /* same as TK_PLUS     */
8317 #define OP_Subtract                            87   /* same as TK_MINUS    */
8318 #define OP_Multiply                            88   /* same as TK_STAR     */
8319 #define OP_Divide                              89   /* same as TK_SLASH    */
8320 #define OP_Remainder                           90   /* same as TK_REM      */
8321 #define OP_CollSeq                             17
8322 #define OP_Function                            18
8323 #define OP_BitAnd                              82   /* same as TK_BITAND   */
8324 #define OP_BitOr                               83   /* same as TK_BITOR    */
8325 #define OP_ShiftLeft                           84   /* same as TK_LSHIFT   */
8326 #define OP_ShiftRight                          85   /* same as TK_RSHIFT   */
8327 #define OP_AddImm                              20
8328 #define OP_MustBeInt                           21
8329 #define OP_RealAffinity                        22
8330 #define OP_ToText                             141   /* same as TK_TO_TEXT  */
8331 #define OP_ToBlob                             142   /* same as TK_TO_BLOB  */
8332 #define OP_ToNumeric                          143   /* same as TK_TO_NUMERIC*/
8333 #define OP_ToInt                              144   /* same as TK_TO_INT   */
8334 #define OP_ToReal                             145   /* same as TK_TO_REAL  */
8335 #define OP_Eq                                  76   /* same as TK_EQ       */
8336 #define OP_Ne                                  75   /* same as TK_NE       */
8337 #define OP_Lt                                  79   /* same as TK_LT       */
8338 #define OP_Le                                  78   /* same as TK_LE       */
8339 #define OP_Gt                                  77   /* same as TK_GT       */
8340 #define OP_Ge                                  80   /* same as TK_GE       */
8341 #define OP_Permutation                         23
8342 #define OP_Compare                             24
8343 #define OP_Jump                                25
8344 #define OP_And                                 69   /* same as TK_AND      */
8345 #define OP_Or                                  68   /* same as TK_OR       */
8346 #define OP_Not                                 19   /* same as TK_NOT      */
8347 #define OP_BitNot                              93   /* same as TK_BITNOT   */
8348 #define OP_If                                  26
8349 #define OP_IfNot                               27
8350 #define OP_IsNull                              73   /* same as TK_ISNULL   */
8351 #define OP_NotNull                             74   /* same as TK_NOTNULL  */
8352 #define OP_Column                              28
8353 #define OP_Affinity                            29
8354 #define OP_MakeRecord                          30
8355 #define OP_Count                               31
8356 #define OP_Savepoint                           32
8357 #define OP_AutoCommit                          33
8358 #define OP_Transaction                         34
8359 #define OP_ReadCookie                          35
8360 #define OP_SetCookie                           36
8361 #define OP_VerifyCookie                        37
8362 #define OP_OpenRead                            38
8363 #define OP_OpenWrite                           39
8364 #define OP_OpenAutoindex                       40
8365 #define OP_OpenEphemeral                       41
8366 #define OP_OpenPseudo                          42
8367 #define OP_Close                               43
8368 #define OP_SeekLt                              44
8369 #define OP_SeekLe                              45
8370 #define OP_SeekGe                              46
8371 #define OP_SeekGt                              47
8372 #define OP_Seek                                48
8373 #define OP_NotFound                            49
8374 #define OP_Found                               50
8375 #define OP_IsUnique                            51
8376 #define OP_NotExists                           52
8377 #define OP_Sequence                            53
8378 #define OP_NewRowid                            54
8379 #define OP_Insert                              55
8380 #define OP_InsertInt                           56
8381 #define OP_Delete                              57
8382 #define OP_ResetCount                          58
8383 #define OP_RowKey                              59
8384 #define OP_RowData                             60
8385 #define OP_Rowid                               61
8386 #define OP_NullRow                             62
8387 #define OP_Last                                63
8388 #define OP_Sort                                64
8389 #define OP_Rewind                              65
8390 #define OP_Prev                                66
8391 #define OP_Next                                67
8392 #define OP_IdxInsert                           70
8393 #define OP_IdxDelete                           71
8394 #define OP_IdxRowid                            72
8395 #define OP_IdxLT                               81
8396 #define OP_IdxGE                               92
8397 #define OP_Destroy                             95
8398 #define OP_Clear                               96
8399 #define OP_CreateIndex                         97
8400 #define OP_CreateTable                         98
8401 #define OP_ParseSchema                         99
8402 #define OP_LoadAnalysis                       100
8403 #define OP_DropTable                          101
8404 #define OP_DropIndex                          102
8405 #define OP_DropTrigger                        103
8406 #define OP_IntegrityCk                        104
8407 #define OP_RowSetAdd                          105
8408 #define OP_RowSetRead                         106
8409 #define OP_RowSetTest                         107
8410 #define OP_Program                            108
8411 #define OP_Param                              109
8412 #define OP_FkCounter                          110
8413 #define OP_FkIfZero                           111
8414 #define OP_MemMax                             112
8415 #define OP_IfPos                              113
8416 #define OP_IfNeg                              114
8417 #define OP_IfZero                             115
8418 #define OP_AggStep                            116
8419 #define OP_AggFinal                           117
8420 #define OP_Checkpoint                         118
8421 #define OP_JournalMode                        119
8422 #define OP_Vacuum                             120
8423 #define OP_IncrVacuum                         121
8424 #define OP_Expire                             122
8425 #define OP_TableLock                          123
8426 #define OP_VBegin                             124
8427 #define OP_VCreate                            125
8428 #define OP_VDestroy                           126
8429 #define OP_VOpen                              127
8430 #define OP_VFilter                            128
8431 #define OP_VColumn                            129
8432 #define OP_VNext                              131
8433 #define OP_VRename                            132
8434 #define OP_VUpdate                            133
8435 #define OP_Pagecount                          134
8436 #define OP_MaxPgcnt                           135
8437 #define OP_Trace                              136
8438 #define OP_Noop                               137
8439 #define OP_Explain                            138
8440 
8441 /* The following opcode values are never used */
8442 #define OP_NotUsed_139                        139
8443 #define OP_NotUsed_140                        140
8444 
8445 
8446 /* Properties such as "out2" or "jump" that are specified in
8447 ** comments following the "case" for each opcode in the vdbe.c
8448 ** are encoded into bitvectors as follows:
8449 */
8450 #define OPFLG_JUMP            0x0001  /* jump:  P2 holds jmp target */
8451 #define OPFLG_OUT2_PRERELEASE 0x0002  /* out2-prerelease: */
8452 #define OPFLG_IN1             0x0004  /* in1:   P1 is an input */
8453 #define OPFLG_IN2             0x0008  /* in2:   P2 is an input */
8454 #define OPFLG_IN3             0x0010  /* in3:   P3 is an input */
8455 #define OPFLG_OUT2            0x0020  /* out2:  P2 is an output */
8456 #define OPFLG_OUT3            0x0040  /* out3:  P3 is an output */
8457 #define OPFLG_INITIALIZER {\
8458 /*   0 */ 0x00, 0x01, 0x05, 0x04, 0x04, 0x10, 0x00, 0x02,\
8459 /*   8 */ 0x02, 0x02, 0x02, 0x02, 0x02, 0x00, 0x24, 0x24,\
8460 /*  16 */ 0x00, 0x00, 0x00, 0x24, 0x04, 0x05, 0x04, 0x00,\
8461 /*  24 */ 0x00, 0x01, 0x05, 0x05, 0x00, 0x00, 0x00, 0x02,\
8462 /*  32 */ 0x00, 0x00, 0x00, 0x02, 0x10, 0x00, 0x00, 0x00,\
8463 /*  40 */ 0x00, 0x00, 0x00, 0x00, 0x11, 0x11, 0x11, 0x11,\
8464 /*  48 */ 0x08, 0x11, 0x11, 0x11, 0x11, 0x02, 0x02, 0x00,\
8465 /*  56 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x01,\
8466 /*  64 */ 0x01, 0x01, 0x01, 0x01, 0x4c, 0x4c, 0x08, 0x00,\
8467 /*  72 */ 0x02, 0x05, 0x05, 0x15, 0x15, 0x15, 0x15, 0x15,\
8468 /*  80 */ 0x15, 0x01, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c,\
8469 /*  88 */ 0x4c, 0x4c, 0x4c, 0x4c, 0x01, 0x24, 0x02, 0x02,\
8470 /*  96 */ 0x00, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,\
8471 /* 104 */ 0x00, 0x0c, 0x45, 0x15, 0x01, 0x02, 0x00, 0x01,\
8472 /* 112 */ 0x08, 0x05, 0x05, 0x05, 0x00, 0x00, 0x00, 0x02,\
8473 /* 120 */ 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
8474 /* 128 */ 0x01, 0x00, 0x02, 0x01, 0x00, 0x00, 0x02, 0x02,\
8475 /* 136 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x04, 0x04,\
8476 /* 144 */ 0x04, 0x04,}
8477 
8478 /************** End of opcodes.h *********************************************/
8479 /************** Continuing where we left off in vdbe.h ***********************/
8480 
8481 /*
8482 ** Prototypes for the VDBE interface.  See comments on the implementation
8483 ** for a description of what each of these routines does.
8484 */
8485 SQLITE_PRIVATE Vdbe *sqlite3VdbeCreate(sqlite3*);
8486 SQLITE_PRIVATE int sqlite3VdbeAddOp0(Vdbe*,int);
8487 SQLITE_PRIVATE int sqlite3VdbeAddOp1(Vdbe*,int,int);
8488 SQLITE_PRIVATE int sqlite3VdbeAddOp2(Vdbe*,int,int,int);
8489 SQLITE_PRIVATE int sqlite3VdbeAddOp3(Vdbe*,int,int,int,int);
8490 SQLITE_PRIVATE int sqlite3VdbeAddOp4(Vdbe*,int,int,int,int,const char *zP4,int);
8491 SQLITE_PRIVATE int sqlite3VdbeAddOp4Int(Vdbe*,int,int,int,int,int);
8492 SQLITE_PRIVATE int sqlite3VdbeAddOpList(Vdbe*, int nOp, VdbeOpList const *aOp);
8493 SQLITE_PRIVATE void sqlite3VdbeAddParseSchemaOp(Vdbe*,int,char*);
8494 SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe*, int addr, int P1);
8495 SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe*, int addr, int P2);
8496 SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe*, int addr, int P3);
8497 SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe*, u8 P5);
8498 SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe*, int addr);
8499 SQLITE_PRIVATE void sqlite3VdbeChangeToNoop(Vdbe*, int addr, int N);
8500 SQLITE_PRIVATE void sqlite3VdbeChangeP4(Vdbe*, int addr, const char *zP4, int N);
8501 SQLITE_PRIVATE void sqlite3VdbeUsesBtree(Vdbe*, int);
8502 SQLITE_PRIVATE VdbeOp *sqlite3VdbeGetOp(Vdbe*, int);
8503 SQLITE_PRIVATE int sqlite3VdbeMakeLabel(Vdbe*);
8504 SQLITE_PRIVATE void sqlite3VdbeRunOnlyOnce(Vdbe*);
8505 SQLITE_PRIVATE void sqlite3VdbeDelete(Vdbe*);
8506 SQLITE_PRIVATE void sqlite3VdbeDeleteObject(sqlite3*,Vdbe*);
8507 SQLITE_PRIVATE void sqlite3VdbeMakeReady(Vdbe*,Parse*);
8508 SQLITE_PRIVATE int sqlite3VdbeFinalize(Vdbe*);
8509 SQLITE_PRIVATE void sqlite3VdbeResolveLabel(Vdbe*, int);
8510 SQLITE_PRIVATE int sqlite3VdbeCurrentAddr(Vdbe*);
8511 #ifdef SQLITE_DEBUG
8512 SQLITE_PRIVATE   int sqlite3VdbeAssertMayAbort(Vdbe *, int);
8513 SQLITE_PRIVATE   void sqlite3VdbeTrace(Vdbe*,FILE*);
8514 #endif
8515 SQLITE_PRIVATE void sqlite3VdbeResetStepResult(Vdbe*);
8516 SQLITE_PRIVATE void sqlite3VdbeRewind(Vdbe*);
8517 SQLITE_PRIVATE int sqlite3VdbeReset(Vdbe*);
8518 SQLITE_PRIVATE void sqlite3VdbeSetNumCols(Vdbe*,int);
8519 SQLITE_PRIVATE int sqlite3VdbeSetColName(Vdbe*, int, int, const char *, void(*)(void*));
8520 SQLITE_PRIVATE void sqlite3VdbeCountChanges(Vdbe*);
8521 SQLITE_PRIVATE sqlite3 *sqlite3VdbeDb(Vdbe*);
8522 SQLITE_PRIVATE void sqlite3VdbeSetSql(Vdbe*, const char *z, int n, int);
8523 SQLITE_PRIVATE void sqlite3VdbeSwap(Vdbe*,Vdbe*);
8524 SQLITE_PRIVATE VdbeOp *sqlite3VdbeTakeOpArray(Vdbe*, int*, int*);
8525 SQLITE_PRIVATE sqlite3_value *sqlite3VdbeGetValue(Vdbe*, int, u8);
8526 SQLITE_PRIVATE void sqlite3VdbeSetVarmask(Vdbe*, int);
8527 #ifndef SQLITE_OMIT_TRACE
8528 SQLITE_PRIVATE   char *sqlite3VdbeExpandSql(Vdbe*, const char*);
8529 #endif
8530 
8531 SQLITE_PRIVATE UnpackedRecord *sqlite3VdbeRecordUnpack(KeyInfo*,int,const void*,char*,int);
8532 SQLITE_PRIVATE void sqlite3VdbeDeleteUnpackedRecord(UnpackedRecord*);
8533 SQLITE_PRIVATE int sqlite3VdbeRecordCompare(int,const void*,UnpackedRecord*);
8534 
8535 #ifndef SQLITE_OMIT_TRIGGER
8536 SQLITE_PRIVATE void sqlite3VdbeLinkSubProgram(Vdbe *, SubProgram *);
8537 #endif
8538 
8539 
8540 #ifndef NDEBUG
8541 SQLITE_PRIVATE   void sqlite3VdbeComment(Vdbe*, const char*, ...);
8542 # define VdbeComment(X)  sqlite3VdbeComment X
8543 SQLITE_PRIVATE   void sqlite3VdbeNoopComment(Vdbe*, const char*, ...);
8544 # define VdbeNoopComment(X)  sqlite3VdbeNoopComment X
8545 #else
8546 # define VdbeComment(X)
8547 # define VdbeNoopComment(X)
8548 #endif
8549 
8550 #endif
8551 
8552 /************** End of vdbe.h ************************************************/
8553 /************** Continuing where we left off in sqliteInt.h ******************/
8554 /************** Include pager.h in the middle of sqliteInt.h *****************/
8555 /************** Begin file pager.h *******************************************/
8556 /*
8557 ** 2001 September 15
8558 **
8559 ** The author disclaims copyright to this source code.  In place of
8560 ** a legal notice, here is a blessing:
8561 **
8562 **    May you do good and not evil.
8563 **    May you find forgiveness for yourself and forgive others.
8564 **    May you share freely, never taking more than you give.
8565 **
8566 *************************************************************************
8567 ** This header file defines the interface that the sqlite page cache
8568 ** subsystem.  The page cache subsystem reads and writes a file a page
8569 ** at a time and provides a journal for rollback.
8570 */
8571 
8572 #ifndef _PAGER_H_
8573 #define _PAGER_H_
8574 
8575 /*
8576 ** Default maximum size for persistent journal files. A negative
8577 ** value means no limit. This value may be overridden using the
8578 ** sqlite3PagerJournalSizeLimit() API. See also "PRAGMA journal_size_limit".
8579 */
8580 #ifndef SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT
8581   #define SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT -1
8582 #endif
8583 
8584 /*
8585 ** The type used to represent a page number.  The first page in a file
8586 ** is called page 1.  0 is used to represent "not a page".
8587 */
8588 typedef u32 Pgno;
8589 
8590 /*
8591 ** Each open file is managed by a separate instance of the "Pager" structure.
8592 */
8593 typedef struct Pager Pager;
8594 
8595 /*
8596 ** Handle type for pages.
8597 */
8598 typedef struct PgHdr DbPage;
8599 
8600 /*
8601 ** Page number PAGER_MJ_PGNO is never used in an SQLite database (it is
8602 ** reserved for working around a windows/posix incompatibility). It is
8603 ** used in the journal to signify that the remainder of the journal file
8604 ** is devoted to storing a master journal name - there are no more pages to
8605 ** roll back. See comments for function writeMasterJournal() in pager.c
8606 ** for details.
8607 */
8608 #define PAGER_MJ_PGNO(x) ((Pgno)((PENDING_BYTE/((x)->pageSize))+1))
8609 
8610 /*
8611 ** Allowed values for the flags parameter to sqlite3PagerOpen().
8612 **
8613 ** NOTE: These values must match the corresponding BTREE_ values in btree.h.
8614 */
8615 #define PAGER_OMIT_JOURNAL  0x0001    /* Do not use a rollback journal */
8616 #define PAGER_NO_READLOCK   0x0002    /* Omit readlocks on readonly files */
8617 #define PAGER_MEMORY        0x0004    /* In-memory database */
8618 
8619 /*
8620 ** Valid values for the second argument to sqlite3PagerLockingMode().
8621 */
8622 #define PAGER_LOCKINGMODE_QUERY      -1
8623 #define PAGER_LOCKINGMODE_NORMAL      0
8624 #define PAGER_LOCKINGMODE_EXCLUSIVE   1
8625 
8626 /*
8627 ** Numeric constants that encode the journalmode.
8628 */
8629 #define PAGER_JOURNALMODE_QUERY     (-1)  /* Query the value of journalmode */
8630 #define PAGER_JOURNALMODE_DELETE      0   /* Commit by deleting journal file */
8631 #define PAGER_JOURNALMODE_PERSIST     1   /* Commit by zeroing journal header */
8632 #define PAGER_JOURNALMODE_OFF         2   /* Journal omitted.  */
8633 #define PAGER_JOURNALMODE_TRUNCATE    3   /* Commit by truncating journal */
8634 #define PAGER_JOURNALMODE_MEMORY      4   /* In-memory journal file */
8635 #define PAGER_JOURNALMODE_WAL         5   /* Use write-ahead logging */
8636 
8637 /*
8638 ** The remainder of this file contains the declarations of the functions
8639 ** that make up the Pager sub-system API. See source code comments for
8640 ** a detailed description of each routine.
8641 */
8642 
8643 /* Open and close a Pager connection. */
8644 SQLITE_PRIVATE int sqlite3PagerOpen(
8645   sqlite3_vfs*,
8646   Pager **ppPager,
8647   const char*,
8648   int,
8649   int,
8650   int,
8651   void(*)(DbPage*)
8652 );
8653 SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager);
8654 SQLITE_PRIVATE int sqlite3PagerReadFileheader(Pager*, int, unsigned char*);
8655 
8656 /* Functions used to configure a Pager object. */
8657 SQLITE_PRIVATE void sqlite3PagerSetBusyhandler(Pager*, int(*)(void *), void *);
8658 SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager*, u32*, int);
8659 SQLITE_PRIVATE int sqlite3PagerMaxPageCount(Pager*, int);
8660 SQLITE_PRIVATE void sqlite3PagerSetCachesize(Pager*, int);
8661 SQLITE_PRIVATE void sqlite3PagerSetSafetyLevel(Pager*,int,int,int);
8662 SQLITE_PRIVATE int sqlite3PagerLockingMode(Pager *, int);
8663 SQLITE_PRIVATE int sqlite3PagerSetJournalMode(Pager *, int);
8664 SQLITE_PRIVATE int sqlite3PagerGetJournalMode(Pager*);
8665 SQLITE_PRIVATE int sqlite3PagerOkToChangeJournalMode(Pager*);
8666 SQLITE_PRIVATE i64 sqlite3PagerJournalSizeLimit(Pager *, i64);
8667 SQLITE_PRIVATE sqlite3_backup **sqlite3PagerBackupPtr(Pager*);
8668 
8669 /* Functions used to obtain and release page references. */
8670 SQLITE_PRIVATE int sqlite3PagerAcquire(Pager *pPager, Pgno pgno, DbPage **ppPage, int clrFlag);
8671 #define sqlite3PagerGet(A,B,C) sqlite3PagerAcquire(A,B,C,0)
8672 SQLITE_PRIVATE DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno);
8673 SQLITE_PRIVATE void sqlite3PagerRef(DbPage*);
8674 SQLITE_PRIVATE void sqlite3PagerUnref(DbPage*);
8675 
8676 /* Operations on page references. */
8677 SQLITE_PRIVATE int sqlite3PagerWrite(DbPage*);
8678 SQLITE_PRIVATE void sqlite3PagerDontWrite(DbPage*);
8679 SQLITE_PRIVATE int sqlite3PagerMovepage(Pager*,DbPage*,Pgno,int);
8680 SQLITE_PRIVATE int sqlite3PagerPageRefcount(DbPage*);
8681 SQLITE_PRIVATE void *sqlite3PagerGetData(DbPage *);
8682 SQLITE_PRIVATE void *sqlite3PagerGetExtra(DbPage *);
8683 
8684 /* Functions used to manage pager transactions and savepoints. */
8685 SQLITE_PRIVATE void sqlite3PagerPagecount(Pager*, int*);
8686 SQLITE_PRIVATE int sqlite3PagerBegin(Pager*, int exFlag, int);
8687 SQLITE_PRIVATE int sqlite3PagerCommitPhaseOne(Pager*,const char *zMaster, int);
8688 SQLITE_PRIVATE int sqlite3PagerExclusiveLock(Pager*);
8689 SQLITE_PRIVATE int sqlite3PagerSync(Pager *pPager);
8690 SQLITE_PRIVATE int sqlite3PagerCommitPhaseTwo(Pager*);
8691 SQLITE_PRIVATE int sqlite3PagerRollback(Pager*);
8692 SQLITE_PRIVATE int sqlite3PagerOpenSavepoint(Pager *pPager, int n);
8693 SQLITE_PRIVATE int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint);
8694 SQLITE_PRIVATE int sqlite3PagerSharedLock(Pager *pPager);
8695 
8696 SQLITE_PRIVATE int sqlite3PagerCheckpoint(Pager *pPager, int, int*, int*);
8697 SQLITE_PRIVATE int sqlite3PagerWalSupported(Pager *pPager);
8698 SQLITE_PRIVATE int sqlite3PagerWalCallback(Pager *pPager);
8699 SQLITE_PRIVATE int sqlite3PagerOpenWal(Pager *pPager, int *pisOpen);
8700 SQLITE_PRIVATE int sqlite3PagerCloseWal(Pager *pPager);
8701 
8702 /* Functions used to query pager state and configuration. */
8703 SQLITE_PRIVATE u8 sqlite3PagerIsreadonly(Pager*);
8704 SQLITE_PRIVATE int sqlite3PagerRefcount(Pager*);
8705 SQLITE_PRIVATE int sqlite3PagerMemUsed(Pager*);
8706 SQLITE_PRIVATE const char *sqlite3PagerFilename(Pager*);
8707 SQLITE_PRIVATE const sqlite3_vfs *sqlite3PagerVfs(Pager*);
8708 SQLITE_PRIVATE sqlite3_file *sqlite3PagerFile(Pager*);
8709 SQLITE_PRIVATE const char *sqlite3PagerJournalname(Pager*);
8710 SQLITE_PRIVATE int sqlite3PagerNosync(Pager*);
8711 SQLITE_PRIVATE void *sqlite3PagerTempSpace(Pager*);
8712 SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager*);
8713 
8714 /* Functions used to truncate the database file. */
8715 SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager*,Pgno);
8716 
8717 #if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_WAL)
8718 SQLITE_PRIVATE void *sqlite3PagerCodec(DbPage *);
8719 #endif
8720 
8721 /* Functions to support testing and debugging. */
8722 #if !defined(NDEBUG) || defined(SQLITE_TEST)
8723 SQLITE_PRIVATE   Pgno sqlite3PagerPagenumber(DbPage*);
8724 SQLITE_PRIVATE   int sqlite3PagerIswriteable(DbPage*);
8725 #endif
8726 #ifdef SQLITE_TEST
8727 SQLITE_PRIVATE   int *sqlite3PagerStats(Pager*);
8728 SQLITE_PRIVATE   void sqlite3PagerRefdump(Pager*);
8729   void disable_simulated_io_errors(void);
8730   void enable_simulated_io_errors(void);
8731 #else
8732 # define disable_simulated_io_errors()
8733 # define enable_simulated_io_errors()
8734 #endif
8735 
8736 #endif /* _PAGER_H_ */
8737 
8738 /************** End of pager.h ***********************************************/
8739 /************** Continuing where we left off in sqliteInt.h ******************/
8740 /************** Include pcache.h in the middle of sqliteInt.h ****************/
8741 /************** Begin file pcache.h ******************************************/
8742 /*
8743 ** 2008 August 05
8744 **
8745 ** The author disclaims copyright to this source code.  In place of
8746 ** a legal notice, here is a blessing:
8747 **
8748 **    May you do good and not evil.
8749 **    May you find forgiveness for yourself and forgive others.
8750 **    May you share freely, never taking more than you give.
8751 **
8752 *************************************************************************
8753 ** This header file defines the interface that the sqlite page cache
8754 ** subsystem.
8755 */
8756 
8757 #ifndef _PCACHE_H_
8758 
8759 typedef struct PgHdr PgHdr;
8760 typedef struct PCache PCache;
8761 
8762 /*
8763 ** Every page in the cache is controlled by an instance of the following
8764 ** structure.
8765 */
8766 struct PgHdr {
8767   void *pData;                   /* Content of this page */
8768   void *pExtra;                  /* Extra content */
8769   PgHdr *pDirty;                 /* Transient list of dirty pages */
8770   Pgno pgno;                     /* Page number for this page */
8771   Pager *pPager;                 /* The pager this page is part of */
8772 #ifdef SQLITE_CHECK_PAGES
8773   u32 pageHash;                  /* Hash of page content */
8774 #endif
8775   u16 flags;                     /* PGHDR flags defined below */
8776 
8777   /**********************************************************************
8778   ** Elements above are public.  All that follows is private to pcache.c
8779   ** and should not be accessed by other modules.
8780   */
8781   i16 nRef;                      /* Number of users of this page */
8782   PCache *pCache;                /* Cache that owns this page */
8783 
8784   PgHdr *pDirtyNext;             /* Next element in list of dirty pages */
8785   PgHdr *pDirtyPrev;             /* Previous element in list of dirty pages */
8786 };
8787 
8788 /* Bit values for PgHdr.flags */
8789 #define PGHDR_DIRTY             0x002  /* Page has changed */
8790 #define PGHDR_NEED_SYNC         0x004  /* Fsync the rollback journal before
8791                                        ** writing this page to the database */
8792 #define PGHDR_NEED_READ         0x008  /* Content is unread */
8793 #define PGHDR_REUSE_UNLIKELY    0x010  /* A hint that reuse is unlikely */
8794 #define PGHDR_DONT_WRITE        0x020  /* Do not write content to disk */
8795 
8796 /* Initialize and shutdown the page cache subsystem */
8797 SQLITE_PRIVATE int sqlite3PcacheInitialize(void);
8798 SQLITE_PRIVATE void sqlite3PcacheShutdown(void);
8799 
8800 /* Page cache buffer management:
8801 ** These routines implement SQLITE_CONFIG_PAGECACHE.
8802 */
8803 SQLITE_PRIVATE void sqlite3PCacheBufferSetup(void *, int sz, int n);
8804 
8805 /* Create a new pager cache.
8806 ** Under memory stress, invoke xStress to try to make pages clean.
8807 ** Only clean and unpinned pages can be reclaimed.
8808 */
8809 SQLITE_PRIVATE void sqlite3PcacheOpen(
8810   int szPage,                    /* Size of every page */
8811   int szExtra,                   /* Extra space associated with each page */
8812   int bPurgeable,                /* True if pages are on backing store */
8813   int (*xStress)(void*, PgHdr*), /* Call to try to make pages clean */
8814   void *pStress,                 /* Argument to xStress */
8815   PCache *pToInit                /* Preallocated space for the PCache */
8816 );
8817 
8818 /* Modify the page-size after the cache has been created. */
8819 SQLITE_PRIVATE void sqlite3PcacheSetPageSize(PCache *, int);
8820 
8821 /* Return the size in bytes of a PCache object.  Used to preallocate
8822 ** storage space.
8823 */
8824 SQLITE_PRIVATE int sqlite3PcacheSize(void);
8825 
8826 /* One release per successful fetch.  Page is pinned until released.
8827 ** Reference counted.
8828 */
8829 SQLITE_PRIVATE int sqlite3PcacheFetch(PCache*, Pgno, int createFlag, PgHdr**);
8830 SQLITE_PRIVATE void sqlite3PcacheRelease(PgHdr*);
8831 
8832 SQLITE_PRIVATE void sqlite3PcacheDrop(PgHdr*);         /* Remove page from cache */
8833 SQLITE_PRIVATE void sqlite3PcacheMakeDirty(PgHdr*);    /* Make sure page is marked dirty */
8834 SQLITE_PRIVATE void sqlite3PcacheMakeClean(PgHdr*);    /* Mark a single page as clean */
8835 SQLITE_PRIVATE void sqlite3PcacheCleanAll(PCache*);    /* Mark all dirty list pages as clean */
8836 
8837 /* Change a page number.  Used by incr-vacuum. */
8838 SQLITE_PRIVATE void sqlite3PcacheMove(PgHdr*, Pgno);
8839 
8840 /* Remove all pages with pgno>x.  Reset the cache if x==0 */
8841 SQLITE_PRIVATE void sqlite3PcacheTruncate(PCache*, Pgno x);
8842 
8843 /* Get a list of all dirty pages in the cache, sorted by page number */
8844 SQLITE_PRIVATE PgHdr *sqlite3PcacheDirtyList(PCache*);
8845 
8846 /* Reset and close the cache object */
8847 SQLITE_PRIVATE void sqlite3PcacheClose(PCache*);
8848 
8849 /* Clear flags from pages of the page cache */
8850 SQLITE_PRIVATE void sqlite3PcacheClearSyncFlags(PCache *);
8851 
8852 /* Discard the contents of the cache */
8853 SQLITE_PRIVATE void sqlite3PcacheClear(PCache*);
8854 
8855 /* Return the total number of outstanding page references */
8856 SQLITE_PRIVATE int sqlite3PcacheRefCount(PCache*);
8857 
8858 /* Increment the reference count of an existing page */
8859 SQLITE_PRIVATE void sqlite3PcacheRef(PgHdr*);
8860 
8861 SQLITE_PRIVATE int sqlite3PcachePageRefcount(PgHdr*);
8862 
8863 /* Return the total number of pages stored in the cache */
8864 SQLITE_PRIVATE int sqlite3PcachePagecount(PCache*);
8865 
8866 #if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG)
8867 /* Iterate through all dirty pages currently stored in the cache. This
8868 ** interface is only available if SQLITE_CHECK_PAGES is defined when the
8869 ** library is built.
8870 */
8871 SQLITE_PRIVATE void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *));
8872 #endif
8873 
8874 /* Set and get the suggested cache-size for the specified pager-cache.
8875 **
8876 ** If no global maximum is configured, then the system attempts to limit
8877 ** the total number of pages cached by purgeable pager-caches to the sum
8878 ** of the suggested cache-sizes.
8879 */
8880 SQLITE_PRIVATE void sqlite3PcacheSetCachesize(PCache *, int);
8881 #ifdef SQLITE_TEST
8882 SQLITE_PRIVATE int sqlite3PcacheGetCachesize(PCache *);
8883 #endif
8884 
8885 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
8886 /* Try to return memory used by the pcache module to the main memory heap */
8887 SQLITE_PRIVATE int sqlite3PcacheReleaseMemory(int);
8888 #endif
8889 
8890 #ifdef SQLITE_TEST
8891 SQLITE_PRIVATE void sqlite3PcacheStats(int*,int*,int*,int*);
8892 #endif
8893 
8894 SQLITE_PRIVATE void sqlite3PCacheSetDefault(void);
8895 
8896 #endif /* _PCACHE_H_ */
8897 
8898 /************** End of pcache.h **********************************************/
8899 /************** Continuing where we left off in sqliteInt.h ******************/
8900 
8901 /************** Include os.h in the middle of sqliteInt.h ********************/
8902 /************** Begin file os.h **********************************************/
8903 /*
8904 ** 2001 September 16
8905 **
8906 ** The author disclaims copyright to this source code.  In place of
8907 ** a legal notice, here is a blessing:
8908 **
8909 **    May you do good and not evil.
8910 **    May you find forgiveness for yourself and forgive others.
8911 **    May you share freely, never taking more than you give.
8912 **
8913 ******************************************************************************
8914 **
8915 ** This header file (together with is companion C source-code file
8916 ** "os.c") attempt to abstract the underlying operating system so that
8917 ** the SQLite library will work on both POSIX and windows systems.
8918 **
8919 ** This header file is #include-ed by sqliteInt.h and thus ends up
8920 ** being included by every source file.
8921 */
8922 #ifndef _SQLITE_OS_H_
8923 #define _SQLITE_OS_H_
8924 
8925 /*
8926 ** Figure out if we are dealing with Unix, Windows, or some other
8927 ** operating system.  After the following block of preprocess macros,
8928 ** all of SQLITE_OS_UNIX, SQLITE_OS_WIN, SQLITE_OS_OS2, and SQLITE_OS_OTHER
8929 ** will defined to either 1 or 0.  One of the four will be 1.  The other
8930 ** three will be 0.
8931 */
8932 #if defined(SQLITE_OS_OTHER)
8933 # if SQLITE_OS_OTHER==1
8934 #   undef SQLITE_OS_UNIX
8935 #   define SQLITE_OS_UNIX 0
8936 #   undef SQLITE_OS_WIN
8937 #   define SQLITE_OS_WIN 0
8938 #   undef SQLITE_OS_OS2
8939 #   define SQLITE_OS_OS2 0
8940 # else
8941 #   undef SQLITE_OS_OTHER
8942 # endif
8943 #endif
8944 #if !defined(SQLITE_OS_UNIX) && !defined(SQLITE_OS_OTHER)
8945 # define SQLITE_OS_OTHER 0
8946 # ifndef SQLITE_OS_WIN
8947 #   if defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__BORLANDC__)
8948 #     define SQLITE_OS_WIN 1
8949 #     define SQLITE_OS_UNIX 0
8950 #     define SQLITE_OS_OS2 0
8951 #   elif defined(__EMX__) || defined(_OS2) || defined(OS2) || defined(_OS2_) || defined(__OS2__)
8952 #     define SQLITE_OS_WIN 0
8953 #     define SQLITE_OS_UNIX 0
8954 #     define SQLITE_OS_OS2 1
8955 #   else
8956 #     define SQLITE_OS_WIN 0
8957 #     define SQLITE_OS_UNIX 1
8958 #     define SQLITE_OS_OS2 0
8959 #  endif
8960 # else
8961 #  define SQLITE_OS_UNIX 0
8962 #  define SQLITE_OS_OS2 0
8963 # endif
8964 #else
8965 # ifndef SQLITE_OS_WIN
8966 #  define SQLITE_OS_WIN 0
8967 # endif
8968 #endif
8969 
8970 /*
8971 ** Determine if we are dealing with WindowsCE - which has a much
8972 ** reduced API.
8973 */
8974 #if defined(_WIN32_WCE)
8975 # define SQLITE_OS_WINCE 1
8976 #else
8977 # define SQLITE_OS_WINCE 0
8978 #endif
8979 
8980 
8981 /*
8982 ** Define the maximum size of a temporary filename
8983 */
8984 #if SQLITE_OS_WIN
8985 # include <windows.h>
8986 # define SQLITE_TEMPNAME_SIZE (MAX_PATH+50)
8987 #elif SQLITE_OS_OS2
8988 # if (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ >= 3) && defined(OS2_HIGH_MEMORY)
8989 #  include <os2safe.h> /* has to be included before os2.h for linking to work */
8990 # endif
8991 # define INCL_DOSDATETIME
8992 # define INCL_DOSFILEMGR
8993 # define INCL_DOSERRORS
8994 # define INCL_DOSMISC
8995 # define INCL_DOSPROCESS
8996 # define INCL_DOSMODULEMGR
8997 # define INCL_DOSSEMAPHORES
8998 # include <os2.h>
8999 # include <uconv.h>
9000 # define SQLITE_TEMPNAME_SIZE (CCHMAXPATHCOMP)
9001 #else
9002 # define SQLITE_TEMPNAME_SIZE 200
9003 #endif
9004 
9005 /* If the SET_FULLSYNC macro is not defined above, then make it
9006 ** a no-op
9007 */
9008 #ifndef SET_FULLSYNC
9009 # define SET_FULLSYNC(x,y)
9010 #endif
9011 
9012 /*
9013 ** The default size of a disk sector
9014 */
9015 #ifndef SQLITE_DEFAULT_SECTOR_SIZE
9016 # define SQLITE_DEFAULT_SECTOR_SIZE 512
9017 #endif
9018 
9019 /*
9020 ** Temporary files are named starting with this prefix followed by 16 random
9021 ** alphanumeric characters, and no file extension. They are stored in the
9022 ** OS's standard temporary file directory, and are deleted prior to exit.
9023 ** If sqlite is being embedded in another program, you may wish to change the
9024 ** prefix to reflect your program's name, so that if your program exits
9025 ** prematurely, old temporary files can be easily identified. This can be done
9026 ** using -DSQLITE_TEMP_FILE_PREFIX=myprefix_ on the compiler command line.
9027 **
9028 ** 2006-10-31:  The default prefix used to be "sqlite_".  But then
9029 ** Mcafee started using SQLite in their anti-virus product and it
9030 ** started putting files with the "sqlite" name in the c:/temp folder.
9031 ** This annoyed many windows users.  Those users would then do a
9032 ** Google search for "sqlite", find the telephone numbers of the
9033 ** developers and call to wake them up at night and complain.
9034 ** For this reason, the default name prefix is changed to be "sqlite"
9035 ** spelled backwards.  So the temp files are still identified, but
9036 ** anybody smart enough to figure out the code is also likely smart
9037 ** enough to know that calling the developer will not help get rid
9038 ** of the file.
9039 */
9040 #ifndef SQLITE_TEMP_FILE_PREFIX
9041 # define SQLITE_TEMP_FILE_PREFIX "etilqs_"
9042 #endif
9043 
9044 /*
9045 ** The following values may be passed as the second argument to
9046 ** sqlite3OsLock(). The various locks exhibit the following semantics:
9047 **
9048 ** SHARED:    Any number of processes may hold a SHARED lock simultaneously.
9049 ** RESERVED:  A single process may hold a RESERVED lock on a file at
9050 **            any time. Other processes may hold and obtain new SHARED locks.
9051 ** PENDING:   A single process may hold a PENDING lock on a file at
9052 **            any one time. Existing SHARED locks may persist, but no new
9053 **            SHARED locks may be obtained by other processes.
9054 ** EXCLUSIVE: An EXCLUSIVE lock precludes all other locks.
9055 **
9056 ** PENDING_LOCK may not be passed directly to sqlite3OsLock(). Instead, a
9057 ** process that requests an EXCLUSIVE lock may actually obtain a PENDING
9058 ** lock. This can be upgraded to an EXCLUSIVE lock by a subsequent call to
9059 ** sqlite3OsLock().
9060 */
9061 #define NO_LOCK         0
9062 #define SHARED_LOCK     1
9063 #define RESERVED_LOCK   2
9064 #define PENDING_LOCK    3
9065 #define EXCLUSIVE_LOCK  4
9066 
9067 /*
9068 ** File Locking Notes:  (Mostly about windows but also some info for Unix)
9069 **
9070 ** We cannot use LockFileEx() or UnlockFileEx() on Win95/98/ME because
9071 ** those functions are not available.  So we use only LockFile() and
9072 ** UnlockFile().
9073 **
9074 ** LockFile() prevents not just writing but also reading by other processes.
9075 ** A SHARED_LOCK is obtained by locking a single randomly-chosen
9076 ** byte out of a specific range of bytes. The lock byte is obtained at
9077 ** random so two separate readers can probably access the file at the
9078 ** same time, unless they are unlucky and choose the same lock byte.
9079 ** An EXCLUSIVE_LOCK is obtained by locking all bytes in the range.
9080 ** There can only be one writer.  A RESERVED_LOCK is obtained by locking
9081 ** a single byte of the file that is designated as the reserved lock byte.
9082 ** A PENDING_LOCK is obtained by locking a designated byte different from
9083 ** the RESERVED_LOCK byte.
9084 **
9085 ** On WinNT/2K/XP systems, LockFileEx() and UnlockFileEx() are available,
9086 ** which means we can use reader/writer locks.  When reader/writer locks
9087 ** are used, the lock is placed on the same range of bytes that is used
9088 ** for probabilistic locking in Win95/98/ME.  Hence, the locking scheme
9089 ** will support two or more Win95 readers or two or more WinNT readers.
9090 ** But a single Win95 reader will lock out all WinNT readers and a single
9091 ** WinNT reader will lock out all other Win95 readers.
9092 **
9093 ** The following #defines specify the range of bytes used for locking.
9094 ** SHARED_SIZE is the number of bytes available in the pool from which
9095 ** a random byte is selected for a shared lock.  The pool of bytes for
9096 ** shared locks begins at SHARED_FIRST.
9097 **
9098 ** The same locking strategy and
9099 ** byte ranges are used for Unix.  This leaves open the possiblity of having
9100 ** clients on win95, winNT, and unix all talking to the same shared file
9101 ** and all locking correctly.  To do so would require that samba (or whatever
9102 ** tool is being used for file sharing) implements locks correctly between
9103 ** windows and unix.  I'm guessing that isn't likely to happen, but by
9104 ** using the same locking range we are at least open to the possibility.
9105 **
9106 ** Locking in windows is manditory.  For this reason, we cannot store
9107 ** actual data in the bytes used for locking.  The pager never allocates
9108 ** the pages involved in locking therefore.  SHARED_SIZE is selected so
9109 ** that all locks will fit on a single page even at the minimum page size.
9110 ** PENDING_BYTE defines the beginning of the locks.  By default PENDING_BYTE
9111 ** is set high so that we don't have to allocate an unused page except
9112 ** for very large databases.  But one should test the page skipping logic
9113 ** by setting PENDING_BYTE low and running the entire regression suite.
9114 **
9115 ** Changing the value of PENDING_BYTE results in a subtly incompatible
9116 ** file format.  Depending on how it is changed, you might not notice
9117 ** the incompatibility right away, even running a full regression test.
9118 ** The default location of PENDING_BYTE is the first byte past the
9119 ** 1GB boundary.
9120 **
9121 */
9122 #ifdef SQLITE_OMIT_WSD
9123 # define PENDING_BYTE     (0x40000000)
9124 #else
9125 # define PENDING_BYTE      sqlite3PendingByte
9126 #endif
9127 #define RESERVED_BYTE     (PENDING_BYTE+1)
9128 #define SHARED_FIRST      (PENDING_BYTE+2)
9129 #define SHARED_SIZE       510
9130 
9131 /*
9132 ** Wrapper around OS specific sqlite3_os_init() function.
9133 */
9134 SQLITE_PRIVATE int sqlite3OsInit(void);
9135 
9136 /*
9137 ** Functions for accessing sqlite3_file methods
9138 */
9139 SQLITE_PRIVATE int sqlite3OsClose(sqlite3_file*);
9140 SQLITE_PRIVATE int sqlite3OsRead(sqlite3_file*, void*, int amt, i64 offset);
9141 SQLITE_PRIVATE int sqlite3OsWrite(sqlite3_file*, const void*, int amt, i64 offset);
9142 SQLITE_PRIVATE int sqlite3OsTruncate(sqlite3_file*, i64 size);
9143 SQLITE_PRIVATE int sqlite3OsSync(sqlite3_file*, int);
9144 SQLITE_PRIVATE int sqlite3OsFileSize(sqlite3_file*, i64 *pSize);
9145 SQLITE_PRIVATE int sqlite3OsLock(sqlite3_file*, int);
9146 SQLITE_PRIVATE int sqlite3OsUnlock(sqlite3_file*, int);
9147 SQLITE_PRIVATE int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut);
9148 SQLITE_PRIVATE int sqlite3OsFileControl(sqlite3_file*,int,void*);
9149 #define SQLITE_FCNTL_DB_UNCHANGED 0xca093fa0
9150 SQLITE_PRIVATE int sqlite3OsSectorSize(sqlite3_file *id);
9151 SQLITE_PRIVATE int sqlite3OsDeviceCharacteristics(sqlite3_file *id);
9152 SQLITE_PRIVATE int sqlite3OsShmMap(sqlite3_file *,int,int,int,void volatile **);
9153 SQLITE_PRIVATE int sqlite3OsShmLock(sqlite3_file *id, int, int, int);
9154 SQLITE_PRIVATE void sqlite3OsShmBarrier(sqlite3_file *id);
9155 SQLITE_PRIVATE int sqlite3OsShmUnmap(sqlite3_file *id, int);
9156 
9157 /*
9158 ** Functions for accessing sqlite3_vfs methods
9159 */
9160 SQLITE_PRIVATE int sqlite3OsOpen(sqlite3_vfs *, const char *, sqlite3_file*, int, int *);
9161 SQLITE_PRIVATE int sqlite3OsDelete(sqlite3_vfs *, const char *, int);
9162 SQLITE_PRIVATE int sqlite3OsAccess(sqlite3_vfs *, const char *, int, int *pResOut);
9163 SQLITE_PRIVATE int sqlite3OsFullPathname(sqlite3_vfs *, const char *, int, char *);
9164 #ifndef SQLITE_OMIT_LOAD_EXTENSION
9165 SQLITE_PRIVATE void *sqlite3OsDlOpen(sqlite3_vfs *, const char *);
9166 SQLITE_PRIVATE void sqlite3OsDlError(sqlite3_vfs *, int, char *);
9167 SQLITE_PRIVATE void (*sqlite3OsDlSym(sqlite3_vfs *, void *, const char *))(void);
9168 SQLITE_PRIVATE void sqlite3OsDlClose(sqlite3_vfs *, void *);
9169 #endif /* SQLITE_OMIT_LOAD_EXTENSION */
9170 SQLITE_PRIVATE int sqlite3OsRandomness(sqlite3_vfs *, int, char *);
9171 SQLITE_PRIVATE int sqlite3OsSleep(sqlite3_vfs *, int);
9172 SQLITE_PRIVATE int sqlite3OsCurrentTimeInt64(sqlite3_vfs *, sqlite3_int64*);
9173 
9174 /*
9175 ** Convenience functions for opening and closing files using
9176 ** sqlite3_malloc() to obtain space for the file-handle structure.
9177 */
9178 SQLITE_PRIVATE int sqlite3OsOpenMalloc(sqlite3_vfs *, const char *, sqlite3_file **, int,int*);
9179 SQLITE_PRIVATE int sqlite3OsCloseFree(sqlite3_file *);
9180 
9181 #endif /* _SQLITE_OS_H_ */
9182 
9183 /************** End of os.h **************************************************/
9184 /************** Continuing where we left off in sqliteInt.h ******************/
9185 /************** Include mutex.h in the middle of sqliteInt.h *****************/
9186 /************** Begin file mutex.h *******************************************/
9187 /*
9188 ** 2007 August 28
9189 **
9190 ** The author disclaims copyright to this source code.  In place of
9191 ** a legal notice, here is a blessing:
9192 **
9193 **    May you do good and not evil.
9194 **    May you find forgiveness for yourself and forgive others.
9195 **    May you share freely, never taking more than you give.
9196 **
9197 *************************************************************************
9198 **
9199 ** This file contains the common header for all mutex implementations.
9200 ** The sqliteInt.h header #includes this file so that it is available
9201 ** to all source files.  We break it out in an effort to keep the code
9202 ** better organized.
9203 **
9204 ** NOTE:  source files should *not* #include this header file directly.
9205 ** Source files should #include the sqliteInt.h file and let that file
9206 ** include this one indirectly.
9207 */
9208 
9209 
9210 /*
9211 ** Figure out what version of the code to use.  The choices are
9212 **
9213 **   SQLITE_MUTEX_OMIT         No mutex logic.  Not even stubs.  The
9214 **                             mutexes implemention cannot be overridden
9215 **                             at start-time.
9216 **
9217 **   SQLITE_MUTEX_NOOP         For single-threaded applications.  No
9218 **                             mutual exclusion is provided.  But this
9219 **                             implementation can be overridden at
9220 **                             start-time.
9221 **
9222 **   SQLITE_MUTEX_PTHREADS     For multi-threaded applications on Unix.
9223 **
9224 **   SQLITE_MUTEX_W32          For multi-threaded applications on Win32.
9225 **
9226 **   SQLITE_MUTEX_OS2          For multi-threaded applications on OS/2.
9227 */
9228 #if !SQLITE_THREADSAFE
9229 # define SQLITE_MUTEX_OMIT
9230 #endif
9231 #if SQLITE_THREADSAFE && !defined(SQLITE_MUTEX_NOOP)
9232 #  if SQLITE_OS_UNIX
9233 #    define SQLITE_MUTEX_PTHREADS
9234 #  elif SQLITE_OS_WIN
9235 #    define SQLITE_MUTEX_W32
9236 #  elif SQLITE_OS_OS2
9237 #    define SQLITE_MUTEX_OS2
9238 #  else
9239 #    define SQLITE_MUTEX_NOOP
9240 #  endif
9241 #endif
9242 
9243 #ifdef SQLITE_MUTEX_OMIT
9244 /*
9245 ** If this is a no-op implementation, implement everything as macros.
9246 */
9247 #define sqlite3_mutex_alloc(X)    ((sqlite3_mutex*)8)
9248 #define sqlite3_mutex_free(X)
9249 #define sqlite3_mutex_enter(X)
9250 #define sqlite3_mutex_try(X)      SQLITE_OK
9251 #define sqlite3_mutex_leave(X)
9252 #define sqlite3_mutex_held(X)     ((void)(X),1)
9253 #define sqlite3_mutex_notheld(X)  ((void)(X),1)
9254 #define sqlite3MutexAlloc(X)      ((sqlite3_mutex*)8)
9255 #define sqlite3MutexInit()        SQLITE_OK
9256 #define sqlite3MutexEnd()
9257 #endif /* defined(SQLITE_MUTEX_OMIT) */
9258 
9259 /************** End of mutex.h ***********************************************/
9260 /************** Continuing where we left off in sqliteInt.h ******************/
9261 
9262 
9263 /*
9264 ** Each database file to be accessed by the system is an instance
9265 ** of the following structure.  There are normally two of these structures
9266 ** in the sqlite.aDb[] array.  aDb[0] is the main database file and
9267 ** aDb[1] is the database file used to hold temporary tables.  Additional
9268 ** databases may be attached.
9269 */
9270 struct Db {
9271   char *zName;         /* Name of this database */
9272   Btree *pBt;          /* The B*Tree structure for this database file */
9273   u8 inTrans;          /* 0: not writable.  1: Transaction.  2: Checkpoint */
9274   u8 safety_level;     /* How aggressive at syncing data to disk */
9275   Schema *pSchema;     /* Pointer to database schema (possibly shared) */
9276 };
9277 
9278 /*
9279 ** An instance of the following structure stores a database schema.
9280 **
9281 ** Most Schema objects are associated with a Btree.  The exception is
9282 ** the Schema for the TEMP databaes (sqlite3.aDb[1]) which is free-standing.
9283 ** In shared cache mode, a single Schema object can be shared by multiple
9284 ** Btrees that refer to the same underlying BtShared object.
9285 **
9286 ** Schema objects are automatically deallocated when the last Btree that
9287 ** references them is destroyed.   The TEMP Schema is manually freed by
9288 ** sqlite3_close().
9289 *
9290 ** A thread must be holding a mutex on the corresponding Btree in order
9291 ** to access Schema content.  This implies that the thread must also be
9292 ** holding a mutex on the sqlite3 connection pointer that owns the Btree.
9293 ** For a TEMP Schema, only the connection mutex is required.
9294 */
9295 struct Schema {
9296   int schema_cookie;   /* Database schema version number for this file */
9297   int iGeneration;     /* Generation counter.  Incremented with each change */
9298   Hash tblHash;        /* All tables indexed by name */
9299   Hash idxHash;        /* All (named) indices indexed by name */
9300   Hash trigHash;       /* All triggers indexed by name */
9301   Hash fkeyHash;       /* All foreign keys by referenced table name */
9302   Table *pSeqTab;      /* The sqlite_sequence table used by AUTOINCREMENT */
9303   u8 file_format;      /* Schema format version for this file */
9304   u8 enc;              /* Text encoding used by this database */
9305   u16 flags;           /* Flags associated with this schema */
9306   int cache_size;      /* Number of pages to use in the cache */
9307 };
9308 
9309 /*
9310 ** These macros can be used to test, set, or clear bits in the
9311 ** Db.pSchema->flags field.
9312 */
9313 #define DbHasProperty(D,I,P)     (((D)->aDb[I].pSchema->flags&(P))==(P))
9314 #define DbHasAnyProperty(D,I,P)  (((D)->aDb[I].pSchema->flags&(P))!=0)
9315 #define DbSetProperty(D,I,P)     (D)->aDb[I].pSchema->flags|=(P)
9316 #define DbClearProperty(D,I,P)   (D)->aDb[I].pSchema->flags&=~(P)
9317 
9318 /*
9319 ** Allowed values for the DB.pSchema->flags field.
9320 **
9321 ** The DB_SchemaLoaded flag is set after the database schema has been
9322 ** read into internal hash tables.
9323 **
9324 ** DB_UnresetViews means that one or more views have column names that
9325 ** have been filled out.  If the schema changes, these column names might
9326 ** changes and so the view will need to be reset.
9327 */
9328 #define DB_SchemaLoaded    0x0001  /* The schema has been loaded */
9329 #define DB_UnresetViews    0x0002  /* Some views have defined column names */
9330 #define DB_Empty           0x0004  /* The file is empty (length 0 bytes) */
9331 
9332 /*
9333 ** The number of different kinds of things that can be limited
9334 ** using the sqlite3_limit() interface.
9335 */
9336 #define SQLITE_N_LIMIT (SQLITE_LIMIT_TRIGGER_DEPTH+1)
9337 
9338 /*
9339 ** Lookaside malloc is a set of fixed-size buffers that can be used
9340 ** to satisfy small transient memory allocation requests for objects
9341 ** associated with a particular database connection.  The use of
9342 ** lookaside malloc provides a significant performance enhancement
9343 ** (approx 10%) by avoiding numerous malloc/free requests while parsing
9344 ** SQL statements.
9345 **
9346 ** The Lookaside structure holds configuration information about the
9347 ** lookaside malloc subsystem.  Each available memory allocation in
9348 ** the lookaside subsystem is stored on a linked list of LookasideSlot
9349 ** objects.
9350 **
9351 ** Lookaside allocations are only allowed for objects that are associated
9352 ** with a particular database connection.  Hence, schema information cannot
9353 ** be stored in lookaside because in shared cache mode the schema information
9354 ** is shared by multiple database connections.  Therefore, while parsing
9355 ** schema information, the Lookaside.bEnabled flag is cleared so that
9356 ** lookaside allocations are not used to construct the schema objects.
9357 */
9358 struct Lookaside {
9359   u16 sz;                 /* Size of each buffer in bytes */
9360   u8 bEnabled;            /* False to disable new lookaside allocations */
9361   u8 bMalloced;           /* True if pStart obtained from sqlite3_malloc() */
9362   int nOut;               /* Number of buffers currently checked out */
9363   int mxOut;              /* Highwater mark for nOut */
9364   int anStat[3];          /* 0: hits.  1: size misses.  2: full misses */
9365   LookasideSlot *pFree;   /* List of available buffers */
9366   void *pStart;           /* First byte of available memory space */
9367   void *pEnd;             /* First byte past end of available space */
9368 };
9369 struct LookasideSlot {
9370   LookasideSlot *pNext;    /* Next buffer in the list of free buffers */
9371 };
9372 
9373 /*
9374 ** A hash table for function definitions.
9375 **
9376 ** Hash each FuncDef structure into one of the FuncDefHash.a[] slots.
9377 ** Collisions are on the FuncDef.pHash chain.
9378 */
9379 struct FuncDefHash {
9380   FuncDef *a[23];       /* Hash table for functions */
9381 };
9382 
9383 /*
9384 ** Each database connection is an instance of the following structure.
9385 **
9386 ** The sqlite.lastRowid records the last insert rowid generated by an
9387 ** insert statement.  Inserts on views do not affect its value.  Each
9388 ** trigger has its own context, so that lastRowid can be updated inside
9389 ** triggers as usual.  The previous value will be restored once the trigger
9390 ** exits.  Upon entering a before or instead of trigger, lastRowid is no
9391 ** longer (since after version 2.8.12) reset to -1.
9392 **
9393 ** The sqlite.nChange does not count changes within triggers and keeps no
9394 ** context.  It is reset at start of sqlite3_exec.
9395 ** The sqlite.lsChange represents the number of changes made by the last
9396 ** insert, update, or delete statement.  It remains constant throughout the
9397 ** length of a statement and is then updated by OP_SetCounts.  It keeps a
9398 ** context stack just like lastRowid so that the count of changes
9399 ** within a trigger is not seen outside the trigger.  Changes to views do not
9400 ** affect the value of lsChange.
9401 ** The sqlite.csChange keeps track of the number of current changes (since
9402 ** the last statement) and is used to update sqlite_lsChange.
9403 **
9404 ** The member variables sqlite.errCode, sqlite.zErrMsg and sqlite.zErrMsg16
9405 ** store the most recent error code and, if applicable, string. The
9406 ** internal function sqlite3Error() is used to set these variables
9407 ** consistently.
9408 */
9409 struct sqlite3 {
9410   sqlite3_vfs *pVfs;            /* OS Interface */
9411   int nDb;                      /* Number of backends currently in use */
9412   Db *aDb;                      /* All backends */
9413   int flags;                    /* Miscellaneous flags. See below */
9414   unsigned int openFlags;       /* Flags passed to sqlite3_vfs.xOpen() */
9415   int errCode;                  /* Most recent error code (SQLITE_*) */
9416   int errMask;                  /* & result codes with this before returning */
9417   u8 autoCommit;                /* The auto-commit flag. */
9418   u8 temp_store;                /* 1: file 2: memory 0: default */
9419   u8 mallocFailed;              /* True if we have seen a malloc failure */
9420   u8 dfltLockMode;              /* Default locking-mode for attached dbs */
9421   signed char nextAutovac;      /* Autovac setting after VACUUM if >=0 */
9422   u8 suppressErr;               /* Do not issue error messages if true */
9423   u8 vtabOnConflict;            /* Value to return for s3_vtab_on_conflict() */
9424   int nextPagesize;             /* Pagesize after VACUUM if >0 */
9425   int nTable;                   /* Number of tables in the database */
9426   CollSeq *pDfltColl;           /* The default collating sequence (BINARY) */
9427   i64 lastRowid;                /* ROWID of most recent insert (see above) */
9428   u32 magic;                    /* Magic number for detect library misuse */
9429   int nChange;                  /* Value returned by sqlite3_changes() */
9430   int nTotalChange;             /* Value returned by sqlite3_total_changes() */
9431   sqlite3_mutex *mutex;         /* Connection mutex */
9432   int aLimit[SQLITE_N_LIMIT];   /* Limits */
9433   struct sqlite3InitInfo {      /* Information used during initialization */
9434     int iDb;                    /* When back is being initialized */
9435     int newTnum;                /* Rootpage of table being initialized */
9436     u8 busy;                    /* TRUE if currently initializing */
9437     u8 orphanTrigger;           /* Last statement is orphaned TEMP trigger */
9438   } init;
9439   int nExtension;               /* Number of loaded extensions */
9440   void **aExtension;            /* Array of shared library handles */
9441   struct Vdbe *pVdbe;           /* List of active virtual machines */
9442   int activeVdbeCnt;            /* Number of VDBEs currently executing */
9443   int writeVdbeCnt;             /* Number of active VDBEs that are writing */
9444   int vdbeExecCnt;              /* Number of nested calls to VdbeExec() */
9445   void (*xTrace)(void*,const char*);        /* Trace function */
9446   void *pTraceArg;                          /* Argument to the trace function */
9447   void (*xProfile)(void*,const char*,u64);  /* Profiling function */
9448   void *pProfileArg;                        /* Argument to profile function */
9449   void *pCommitArg;                 /* Argument to xCommitCallback() */
9450   int (*xCommitCallback)(void*);    /* Invoked at every commit. */
9451   void *pRollbackArg;               /* Argument to xRollbackCallback() */
9452   void (*xRollbackCallback)(void*); /* Invoked at every commit. */
9453   void *pUpdateArg;
9454   void (*xUpdateCallback)(void*,int, const char*,const char*,sqlite_int64);
9455 #ifndef SQLITE_OMIT_WAL
9456   int (*xWalCallback)(void *, sqlite3 *, const char *, int);
9457   void *pWalArg;
9458 #endif
9459   void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*);
9460   void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*);
9461   void *pCollNeededArg;
9462   sqlite3_value *pErr;          /* Most recent error message */
9463   char *zErrMsg;                /* Most recent error message (UTF-8 encoded) */
9464   char *zErrMsg16;              /* Most recent error message (UTF-16 encoded) */
9465   union {
9466     volatile int isInterrupted; /* True if sqlite3_interrupt has been called */
9467     double notUsed1;            /* Spacer */
9468   } u1;
9469   Lookaside lookaside;          /* Lookaside malloc configuration */
9470 #ifndef SQLITE_OMIT_AUTHORIZATION
9471   int (*xAuth)(void*,int,const char*,const char*,const char*,const char*);
9472                                 /* Access authorization function */
9473   void *pAuthArg;               /* 1st argument to the access auth function */
9474 #endif
9475 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
9476   int (*xProgress)(void *);     /* The progress callback */
9477   void *pProgressArg;           /* Argument to the progress callback */
9478   int nProgressOps;             /* Number of opcodes for progress callback */
9479 #endif
9480 #ifndef SQLITE_OMIT_VIRTUALTABLE
9481   Hash aModule;                 /* populated by sqlite3_create_module() */
9482   VtabCtx *pVtabCtx;            /* Context for active vtab connect/create */
9483   VTable **aVTrans;             /* Virtual tables with open transactions */
9484   int nVTrans;                  /* Allocated size of aVTrans */
9485   VTable *pDisconnect;    /* Disconnect these in next sqlite3_prepare() */
9486 #endif
9487   FuncDefHash aFunc;            /* Hash table of connection functions */
9488   Hash aCollSeq;                /* All collating sequences */
9489   BusyHandler busyHandler;      /* Busy callback */
9490   int busyTimeout;              /* Busy handler timeout, in msec */
9491   Db aDbStatic[2];              /* Static space for the 2 default backends */
9492   Savepoint *pSavepoint;        /* List of active savepoints */
9493   int nSavepoint;               /* Number of non-transaction savepoints */
9494   int nStatement;               /* Number of nested statement-transactions  */
9495   u8 isTransactionSavepoint;    /* True if the outermost savepoint is a TS */
9496   i64 nDeferredCons;            /* Net deferred constraints this transaction. */
9497   int *pnBytesFreed;            /* If not NULL, increment this in DbFree() */
9498 
9499 #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
9500   /* The following variables are all protected by the STATIC_MASTER
9501   ** mutex, not by sqlite3.mutex. They are used by code in notify.c.
9502   **
9503   ** When X.pUnlockConnection==Y, that means that X is waiting for Y to
9504   ** unlock so that it can proceed.
9505   **
9506   ** When X.pBlockingConnection==Y, that means that something that X tried
9507   ** tried to do recently failed with an SQLITE_LOCKED error due to locks
9508   ** held by Y.
9509   */
9510   sqlite3 *pBlockingConnection; /* Connection that caused SQLITE_LOCKED */
9511   sqlite3 *pUnlockConnection;           /* Connection to watch for unlock */
9512   void *pUnlockArg;                     /* Argument to xUnlockNotify */
9513   void (*xUnlockNotify)(void **, int);  /* Unlock notify callback */
9514   sqlite3 *pNextBlocked;        /* Next in list of all blocked connections */
9515 #endif
9516 };
9517 
9518 /*
9519 ** A macro to discover the encoding of a database.
9520 */
9521 #define ENC(db) ((db)->aDb[0].pSchema->enc)
9522 
9523 /*
9524 ** Possible values for the sqlite3.flags.
9525 */
9526 #define SQLITE_VdbeTrace      0x00000100  /* True to trace VDBE execution */
9527 #define SQLITE_InternChanges  0x00000200  /* Uncommitted Hash table changes */
9528 #define SQLITE_FullColNames   0x00000400  /* Show full column names on SELECT */
9529 #define SQLITE_ShortColNames  0x00000800  /* Show short columns names */
9530 #define SQLITE_CountRows      0x00001000  /* Count rows changed by INSERT, */
9531                                           /*   DELETE, or UPDATE and return */
9532                                           /*   the count using a callback. */
9533 #define SQLITE_NullCallback   0x00002000  /* Invoke the callback once if the */
9534                                           /*   result set is empty */
9535 #define SQLITE_SqlTrace       0x00004000  /* Debug print SQL as it executes */
9536 #define SQLITE_VdbeListing    0x00008000  /* Debug listings of VDBE programs */
9537 #define SQLITE_WriteSchema    0x00010000  /* OK to update SQLITE_MASTER */
9538 #define SQLITE_NoReadlock     0x00020000  /* Readlocks are omitted when
9539                                           ** accessing read-only databases */
9540 #define SQLITE_IgnoreChecks   0x00040000  /* Do not enforce check constraints */
9541 #define SQLITE_ReadUncommitted 0x0080000  /* For shared-cache mode */
9542 #define SQLITE_LegacyFileFmt  0x00100000  /* Create new databases in format 1 */
9543 #define SQLITE_FullFSync      0x00200000  /* Use full fsync on the backend */
9544 #define SQLITE_CkptFullFSync  0x00400000  /* Use full fsync for checkpoint */
9545 #define SQLITE_RecoveryMode   0x00800000  /* Ignore schema errors */
9546 #define SQLITE_ReverseOrder   0x01000000  /* Reverse unordered SELECTs */
9547 #define SQLITE_RecTriggers    0x02000000  /* Enable recursive triggers */
9548 #define SQLITE_ForeignKeys    0x04000000  /* Enforce foreign key constraints  */
9549 #define SQLITE_AutoIndex      0x08000000  /* Enable automatic indexes */
9550 #define SQLITE_PreferBuiltin  0x10000000  /* Preference to built-in funcs */
9551 #define SQLITE_LoadExtension  0x20000000  /* Enable load_extension */
9552 #define SQLITE_EnableTrigger  0x40000000  /* True to enable triggers */
9553 
9554 /*
9555 ** Bits of the sqlite3.flags field that are used by the
9556 ** sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,...) interface.
9557 ** These must be the low-order bits of the flags field.
9558 */
9559 #define SQLITE_QueryFlattener 0x01        /* Disable query flattening */
9560 #define SQLITE_ColumnCache    0x02        /* Disable the column cache */
9561 #define SQLITE_IndexSort      0x04        /* Disable indexes for sorting */
9562 #define SQLITE_IndexSearch    0x08        /* Disable indexes for searching */
9563 #define SQLITE_IndexCover     0x10        /* Disable index covering table */
9564 #define SQLITE_GroupByOrder   0x20        /* Disable GROUPBY cover of ORDERBY */
9565 #define SQLITE_FactorOutConst 0x40        /* Disable factoring out constants */
9566 #define SQLITE_IdxRealAsInt   0x80        /* Store REAL as INT in indices */
9567 #define SQLITE_OptMask        0xff        /* Mask of all disablable opts */
9568 
9569 /*
9570 ** Possible values for the sqlite.magic field.
9571 ** The numbers are obtained at random and have no special meaning, other
9572 ** than being distinct from one another.
9573 */
9574 #define SQLITE_MAGIC_OPEN     0xa029a697  /* Database is open */
9575 #define SQLITE_MAGIC_CLOSED   0x9f3c2d33  /* Database is closed */
9576 #define SQLITE_MAGIC_SICK     0x4b771290  /* Error and awaiting close */
9577 #define SQLITE_MAGIC_BUSY     0xf03b7906  /* Database currently in use */
9578 #define SQLITE_MAGIC_ERROR    0xb5357930  /* An SQLITE_MISUSE error occurred */
9579 
9580 /*
9581 ** Each SQL function is defined by an instance of the following
9582 ** structure.  A pointer to this structure is stored in the sqlite.aFunc
9583 ** hash table.  When multiple functions have the same name, the hash table
9584 ** points to a linked list of these structures.
9585 */
9586 struct FuncDef {
9587   i16 nArg;            /* Number of arguments.  -1 means unlimited */
9588   u8 iPrefEnc;         /* Preferred text encoding (SQLITE_UTF8, 16LE, 16BE) */
9589   u8 flags;            /* Some combination of SQLITE_FUNC_* */
9590   void *pUserData;     /* User data parameter */
9591   FuncDef *pNext;      /* Next function with same name */
9592   void (*xFunc)(sqlite3_context*,int,sqlite3_value**); /* Regular function */
9593   void (*xStep)(sqlite3_context*,int,sqlite3_value**); /* Aggregate step */
9594   void (*xFinalize)(sqlite3_context*);                /* Aggregate finalizer */
9595   char *zName;         /* SQL name of the function. */
9596   FuncDef *pHash;      /* Next with a different name but the same hash */
9597   FuncDestructor *pDestructor;   /* Reference counted destructor function */
9598 };
9599 
9600 /*
9601 ** This structure encapsulates a user-function destructor callback (as
9602 ** configured using create_function_v2()) and a reference counter. When
9603 ** create_function_v2() is called to create a function with a destructor,
9604 ** a single object of this type is allocated. FuncDestructor.nRef is set to
9605 ** the number of FuncDef objects created (either 1 or 3, depending on whether
9606 ** or not the specified encoding is SQLITE_ANY). The FuncDef.pDestructor
9607 ** member of each of the new FuncDef objects is set to point to the allocated
9608 ** FuncDestructor.
9609 **
9610 ** Thereafter, when one of the FuncDef objects is deleted, the reference
9611 ** count on this object is decremented. When it reaches 0, the destructor
9612 ** is invoked and the FuncDestructor structure freed.
9613 */
9614 struct FuncDestructor {
9615   int nRef;
9616   void (*xDestroy)(void *);
9617   void *pUserData;
9618 };
9619 
9620 /*
9621 ** Possible values for FuncDef.flags
9622 */
9623 #define SQLITE_FUNC_LIKE     0x01 /* Candidate for the LIKE optimization */
9624 #define SQLITE_FUNC_CASE     0x02 /* Case-sensitive LIKE-type function */
9625 #define SQLITE_FUNC_EPHEM    0x04 /* Ephemeral.  Delete with VDBE */
9626 #define SQLITE_FUNC_NEEDCOLL 0x08 /* sqlite3GetFuncCollSeq() might be called */
9627 #define SQLITE_FUNC_PRIVATE  0x10 /* Allowed for internal use only */
9628 #define SQLITE_FUNC_COUNT    0x20 /* Built-in count(*) aggregate */
9629 #define SQLITE_FUNC_COALESCE 0x40 /* Built-in coalesce() or ifnull() function */
9630 
9631 /*
9632 ** The following three macros, FUNCTION(), LIKEFUNC() and AGGREGATE() are
9633 ** used to create the initializers for the FuncDef structures.
9634 **
9635 **   FUNCTION(zName, nArg, iArg, bNC, xFunc)
9636 **     Used to create a scalar function definition of a function zName
9637 **     implemented by C function xFunc that accepts nArg arguments. The
9638 **     value passed as iArg is cast to a (void*) and made available
9639 **     as the user-data (sqlite3_user_data()) for the function. If
9640 **     argument bNC is true, then the SQLITE_FUNC_NEEDCOLL flag is set.
9641 **
9642 **   AGGREGATE(zName, nArg, iArg, bNC, xStep, xFinal)
9643 **     Used to create an aggregate function definition implemented by
9644 **     the C functions xStep and xFinal. The first four parameters
9645 **     are interpreted in the same way as the first 4 parameters to
9646 **     FUNCTION().
9647 **
9648 **   LIKEFUNC(zName, nArg, pArg, flags)
9649 **     Used to create a scalar function definition of a function zName
9650 **     that accepts nArg arguments and is implemented by a call to C
9651 **     function likeFunc. Argument pArg is cast to a (void *) and made
9652 **     available as the function user-data (sqlite3_user_data()). The
9653 **     FuncDef.flags variable is set to the value passed as the flags
9654 **     parameter.
9655 */
9656 #define FUNCTION(zName, nArg, iArg, bNC, xFunc) \
9657   {nArg, SQLITE_UTF8, bNC*SQLITE_FUNC_NEEDCOLL, \
9658    SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, #zName, 0, 0}
9659 #define STR_FUNCTION(zName, nArg, pArg, bNC, xFunc) \
9660   {nArg, SQLITE_UTF8, bNC*SQLITE_FUNC_NEEDCOLL, \
9661    pArg, 0, xFunc, 0, 0, #zName, 0, 0}
9662 #define LIKEFUNC(zName, nArg, arg, flags) \
9663   {nArg, SQLITE_UTF8, flags, (void *)arg, 0, likeFunc, 0, 0, #zName, 0, 0}
9664 #define AGGREGATE(zName, nArg, arg, nc, xStep, xFinal) \
9665   {nArg, SQLITE_UTF8, nc*SQLITE_FUNC_NEEDCOLL, \
9666    SQLITE_INT_TO_PTR(arg), 0, 0, xStep,xFinal,#zName,0,0}
9667 
9668 /*
9669 ** All current savepoints are stored in a linked list starting at
9670 ** sqlite3.pSavepoint. The first element in the list is the most recently
9671 ** opened savepoint. Savepoints are added to the list by the vdbe
9672 ** OP_Savepoint instruction.
9673 */
9674 struct Savepoint {
9675   char *zName;                        /* Savepoint name (nul-terminated) */
9676   i64 nDeferredCons;                  /* Number of deferred fk violations */
9677   Savepoint *pNext;                   /* Parent savepoint (if any) */
9678 };
9679 
9680 /*
9681 ** The following are used as the second parameter to sqlite3Savepoint(),
9682 ** and as the P1 argument to the OP_Savepoint instruction.
9683 */
9684 #define SAVEPOINT_BEGIN      0
9685 #define SAVEPOINT_RELEASE    1
9686 #define SAVEPOINT_ROLLBACK   2
9687 
9688 
9689 /*
9690 ** Each SQLite module (virtual table definition) is defined by an
9691 ** instance of the following structure, stored in the sqlite3.aModule
9692 ** hash table.
9693 */
9694 struct Module {
9695   const sqlite3_module *pModule;       /* Callback pointers */
9696   const char *zName;                   /* Name passed to create_module() */
9697   void *pAux;                          /* pAux passed to create_module() */
9698   void (*xDestroy)(void *);            /* Module destructor function */
9699 };
9700 
9701 /*
9702 ** information about each column of an SQL table is held in an instance
9703 ** of this structure.
9704 */
9705 struct Column {
9706   char *zName;     /* Name of this column */
9707   Expr *pDflt;     /* Default value of this column */
9708   char *zDflt;     /* Original text of the default value */
9709   char *zType;     /* Data type for this column */
9710   char *zColl;     /* Collating sequence.  If NULL, use the default */
9711   u8 notNull;      /* True if there is a NOT NULL constraint */
9712   u8 isPrimKey;    /* True if this column is part of the PRIMARY KEY */
9713   char affinity;   /* One of the SQLITE_AFF_... values */
9714 #ifndef SQLITE_OMIT_VIRTUALTABLE
9715   u8 isHidden;     /* True if this column is 'hidden' */
9716 #endif
9717 };
9718 
9719 /*
9720 ** A "Collating Sequence" is defined by an instance of the following
9721 ** structure. Conceptually, a collating sequence consists of a name and
9722 ** a comparison routine that defines the order of that sequence.
9723 **
9724 ** There may two separate implementations of the collation function, one
9725 ** that processes text in UTF-8 encoding (CollSeq.xCmp) and another that
9726 ** processes text encoded in UTF-16 (CollSeq.xCmp16), using the machine
9727 ** native byte order. When a collation sequence is invoked, SQLite selects
9728 ** the version that will require the least expensive encoding
9729 ** translations, if any.
9730 **
9731 ** The CollSeq.pUser member variable is an extra parameter that passed in
9732 ** as the first argument to the UTF-8 comparison function, xCmp.
9733 ** CollSeq.pUser16 is the equivalent for the UTF-16 comparison function,
9734 ** xCmp16.
9735 **
9736 ** If both CollSeq.xCmp and CollSeq.xCmp16 are NULL, it means that the
9737 ** collating sequence is undefined.  Indices built on an undefined
9738 ** collating sequence may not be read or written.
9739 */
9740 struct CollSeq {
9741   char *zName;          /* Name of the collating sequence, UTF-8 encoded */
9742   u8 enc;               /* Text encoding handled by xCmp() */
9743   u8 type;              /* One of the SQLITE_COLL_... values below */
9744   void *pUser;          /* First argument to xCmp() */
9745   int (*xCmp)(void*,int, const void*, int, const void*);
9746   void (*xDel)(void*);  /* Destructor for pUser */
9747 };
9748 
9749 /*
9750 ** Allowed values of CollSeq.type:
9751 */
9752 #define SQLITE_COLL_BINARY  1  /* The default memcmp() collating sequence */
9753 #define SQLITE_COLL_NOCASE  2  /* The built-in NOCASE collating sequence */
9754 #define SQLITE_COLL_REVERSE 3  /* The built-in REVERSE collating sequence */
9755 #define SQLITE_COLL_USER    0  /* Any other user-defined collating sequence */
9756 
9757 /*
9758 ** A sort order can be either ASC or DESC.
9759 */
9760 #define SQLITE_SO_ASC       0  /* Sort in ascending order */
9761 #define SQLITE_SO_DESC      1  /* Sort in ascending order */
9762 
9763 /*
9764 ** Column affinity types.
9765 **
9766 ** These used to have mnemonic name like 'i' for SQLITE_AFF_INTEGER and
9767 ** 't' for SQLITE_AFF_TEXT.  But we can save a little space and improve
9768 ** the speed a little by numbering the values consecutively.
9769 **
9770 ** But rather than start with 0 or 1, we begin with 'a'.  That way,
9771 ** when multiple affinity types are concatenated into a string and
9772 ** used as the P4 operand, they will be more readable.
9773 **
9774 ** Note also that the numeric types are grouped together so that testing
9775 ** for a numeric type is a single comparison.
9776 */
9777 #define SQLITE_AFF_TEXT     'a'
9778 #define SQLITE_AFF_NONE     'b'
9779 #define SQLITE_AFF_NUMERIC  'c'
9780 #define SQLITE_AFF_INTEGER  'd'
9781 #define SQLITE_AFF_REAL     'e'
9782 
9783 #define sqlite3IsNumericAffinity(X)  ((X)>=SQLITE_AFF_NUMERIC)
9784 
9785 /*
9786 ** The SQLITE_AFF_MASK values masks off the significant bits of an
9787 ** affinity value.
9788 */
9789 #define SQLITE_AFF_MASK     0x67
9790 
9791 /*
9792 ** Additional bit values that can be ORed with an affinity without
9793 ** changing the affinity.
9794 */
9795 #define SQLITE_JUMPIFNULL   0x08  /* jumps if either operand is NULL */
9796 #define SQLITE_STOREP2      0x10  /* Store result in reg[P2] rather than jump */
9797 #define SQLITE_NULLEQ       0x80  /* NULL=NULL */
9798 
9799 /*
9800 ** An object of this type is created for each virtual table present in
9801 ** the database schema.
9802 **
9803 ** If the database schema is shared, then there is one instance of this
9804 ** structure for each database connection (sqlite3*) that uses the shared
9805 ** schema. This is because each database connection requires its own unique
9806 ** instance of the sqlite3_vtab* handle used to access the virtual table
9807 ** implementation. sqlite3_vtab* handles can not be shared between
9808 ** database connections, even when the rest of the in-memory database
9809 ** schema is shared, as the implementation often stores the database
9810 ** connection handle passed to it via the xConnect() or xCreate() method
9811 ** during initialization internally. This database connection handle may
9812 ** then be used by the virtual table implementation to access real tables
9813 ** within the database. So that they appear as part of the callers
9814 ** transaction, these accesses need to be made via the same database
9815 ** connection as that used to execute SQL operations on the virtual table.
9816 **
9817 ** All VTable objects that correspond to a single table in a shared
9818 ** database schema are initially stored in a linked-list pointed to by
9819 ** the Table.pVTable member variable of the corresponding Table object.
9820 ** When an sqlite3_prepare() operation is required to access the virtual
9821 ** table, it searches the list for the VTable that corresponds to the
9822 ** database connection doing the preparing so as to use the correct
9823 ** sqlite3_vtab* handle in the compiled query.
9824 **
9825 ** When an in-memory Table object is deleted (for example when the
9826 ** schema is being reloaded for some reason), the VTable objects are not
9827 ** deleted and the sqlite3_vtab* handles are not xDisconnect()ed
9828 ** immediately. Instead, they are moved from the Table.pVTable list to
9829 ** another linked list headed by the sqlite3.pDisconnect member of the
9830 ** corresponding sqlite3 structure. They are then deleted/xDisconnected
9831 ** next time a statement is prepared using said sqlite3*. This is done
9832 ** to avoid deadlock issues involving multiple sqlite3.mutex mutexes.
9833 ** Refer to comments above function sqlite3VtabUnlockList() for an
9834 ** explanation as to why it is safe to add an entry to an sqlite3.pDisconnect
9835 ** list without holding the corresponding sqlite3.mutex mutex.
9836 **
9837 ** The memory for objects of this type is always allocated by
9838 ** sqlite3DbMalloc(), using the connection handle stored in VTable.db as
9839 ** the first argument.
9840 */
9841 struct VTable {
9842   sqlite3 *db;              /* Database connection associated with this table */
9843   Module *pMod;             /* Pointer to module implementation */
9844   sqlite3_vtab *pVtab;      /* Pointer to vtab instance */
9845   int nRef;                 /* Number of pointers to this structure */
9846   u8 bConstraint;           /* True if constraints are supported */
9847   int iSavepoint;           /* Depth of the SAVEPOINT stack */
9848   VTable *pNext;            /* Next in linked list (see above) */
9849 };
9850 
9851 /*
9852 ** Each SQL table is represented in memory by an instance of the
9853 ** following structure.
9854 **
9855 ** Table.zName is the name of the table.  The case of the original
9856 ** CREATE TABLE statement is stored, but case is not significant for
9857 ** comparisons.
9858 **
9859 ** Table.nCol is the number of columns in this table.  Table.aCol is a
9860 ** pointer to an array of Column structures, one for each column.
9861 **
9862 ** If the table has an INTEGER PRIMARY KEY, then Table.iPKey is the index of
9863 ** the column that is that key.   Otherwise Table.iPKey is negative.  Note
9864 ** that the datatype of the PRIMARY KEY must be INTEGER for this field to
9865 ** be set.  An INTEGER PRIMARY KEY is used as the rowid for each row of
9866 ** the table.  If a table has no INTEGER PRIMARY KEY, then a random rowid
9867 ** is generated for each row of the table.  TF_HasPrimaryKey is set if
9868 ** the table has any PRIMARY KEY, INTEGER or otherwise.
9869 **
9870 ** Table.tnum is the page number for the root BTree page of the table in the
9871 ** database file.  If Table.iDb is the index of the database table backend
9872 ** in sqlite.aDb[].  0 is for the main database and 1 is for the file that
9873 ** holds temporary tables and indices.  If TF_Ephemeral is set
9874 ** then the table is stored in a file that is automatically deleted
9875 ** when the VDBE cursor to the table is closed.  In this case Table.tnum
9876 ** refers VDBE cursor number that holds the table open, not to the root
9877 ** page number.  Transient tables are used to hold the results of a
9878 ** sub-query that appears instead of a real table name in the FROM clause
9879 ** of a SELECT statement.
9880 */
9881 struct Table {
9882   char *zName;         /* Name of the table or view */
9883   int iPKey;           /* If not negative, use aCol[iPKey] as the primary key */
9884   int nCol;            /* Number of columns in this table */
9885   Column *aCol;        /* Information about each column */
9886   Index *pIndex;       /* List of SQL indexes on this table. */
9887   int tnum;            /* Root BTree node for this table (see note above) */
9888   unsigned nRowEst;    /* Estimated rows in table - from sqlite_stat1 table */
9889   Select *pSelect;     /* NULL for tables.  Points to definition if a view. */
9890   u16 nRef;            /* Number of pointers to this Table */
9891   u8 tabFlags;         /* Mask of TF_* values */
9892   u8 keyConf;          /* What to do in case of uniqueness conflict on iPKey */
9893   FKey *pFKey;         /* Linked list of all foreign keys in this table */
9894   char *zColAff;       /* String defining the affinity of each column */
9895 #ifndef SQLITE_OMIT_CHECK
9896   Expr *pCheck;        /* The AND of all CHECK constraints */
9897 #endif
9898 #ifndef SQLITE_OMIT_ALTERTABLE
9899   int addColOffset;    /* Offset in CREATE TABLE stmt to add a new column */
9900 #endif
9901 #ifndef SQLITE_OMIT_VIRTUALTABLE
9902   VTable *pVTable;     /* List of VTable objects. */
9903   int nModuleArg;      /* Number of arguments to the module */
9904   char **azModuleArg;  /* Text of all module args. [0] is module name */
9905 #endif
9906   Trigger *pTrigger;   /* List of triggers stored in pSchema */
9907   Schema *pSchema;     /* Schema that contains this table */
9908   Table *pNextZombie;  /* Next on the Parse.pZombieTab list */
9909 };
9910 
9911 /*
9912 ** Allowed values for Tabe.tabFlags.
9913 */
9914 #define TF_Readonly        0x01    /* Read-only system table */
9915 #define TF_Ephemeral       0x02    /* An ephemeral table */
9916 #define TF_HasPrimaryKey   0x04    /* Table has a primary key */
9917 #define TF_Autoincrement   0x08    /* Integer primary key is autoincrement */
9918 #define TF_Virtual         0x10    /* Is a virtual table */
9919 #define TF_NeedMetadata    0x20    /* aCol[].zType and aCol[].pColl missing */
9920 
9921 
9922 
9923 /*
9924 ** Test to see whether or not a table is a virtual table.  This is
9925 ** done as a macro so that it will be optimized out when virtual
9926 ** table support is omitted from the build.
9927 */
9928 #ifndef SQLITE_OMIT_VIRTUALTABLE
9929 #  define IsVirtual(X)      (((X)->tabFlags & TF_Virtual)!=0)
9930 #  define IsHiddenColumn(X) ((X)->isHidden)
9931 #else
9932 #  define IsVirtual(X)      0
9933 #  define IsHiddenColumn(X) 0
9934 #endif
9935 
9936 /*
9937 ** Each foreign key constraint is an instance of the following structure.
9938 **
9939 ** A foreign key is associated with two tables.  The "from" table is
9940 ** the table that contains the REFERENCES clause that creates the foreign
9941 ** key.  The "to" table is the table that is named in the REFERENCES clause.
9942 ** Consider this example:
9943 **
9944 **     CREATE TABLE ex1(
9945 **       a INTEGER PRIMARY KEY,
9946 **       b INTEGER CONSTRAINT fk1 REFERENCES ex2(x)
9947 **     );
9948 **
9949 ** For foreign key "fk1", the from-table is "ex1" and the to-table is "ex2".
9950 **
9951 ** Each REFERENCES clause generates an instance of the following structure
9952 ** which is attached to the from-table.  The to-table need not exist when
9953 ** the from-table is created.  The existence of the to-table is not checked.
9954 */
9955 struct FKey {
9956   Table *pFrom;     /* Table containing the REFERENCES clause (aka: Child) */
9957   FKey *pNextFrom;  /* Next foreign key in pFrom */
9958   char *zTo;        /* Name of table that the key points to (aka: Parent) */
9959   FKey *pNextTo;    /* Next foreign key on table named zTo */
9960   FKey *pPrevTo;    /* Previous foreign key on table named zTo */
9961   int nCol;         /* Number of columns in this key */
9962   /* EV: R-30323-21917 */
9963   u8 isDeferred;    /* True if constraint checking is deferred till COMMIT */
9964   u8 aAction[2];          /* ON DELETE and ON UPDATE actions, respectively */
9965   Trigger *apTrigger[2];  /* Triggers for aAction[] actions */
9966   struct sColMap {  /* Mapping of columns in pFrom to columns in zTo */
9967     int iFrom;         /* Index of column in pFrom */
9968     char *zCol;        /* Name of column in zTo.  If 0 use PRIMARY KEY */
9969   } aCol[1];        /* One entry for each of nCol column s */
9970 };
9971 
9972 /*
9973 ** SQLite supports many different ways to resolve a constraint
9974 ** error.  ROLLBACK processing means that a constraint violation
9975 ** causes the operation in process to fail and for the current transaction
9976 ** to be rolled back.  ABORT processing means the operation in process
9977 ** fails and any prior changes from that one operation are backed out,
9978 ** but the transaction is not rolled back.  FAIL processing means that
9979 ** the operation in progress stops and returns an error code.  But prior
9980 ** changes due to the same operation are not backed out and no rollback
9981 ** occurs.  IGNORE means that the particular row that caused the constraint
9982 ** error is not inserted or updated.  Processing continues and no error
9983 ** is returned.  REPLACE means that preexisting database rows that caused
9984 ** a UNIQUE constraint violation are removed so that the new insert or
9985 ** update can proceed.  Processing continues and no error is reported.
9986 **
9987 ** RESTRICT, SETNULL, and CASCADE actions apply only to foreign keys.
9988 ** RESTRICT is the same as ABORT for IMMEDIATE foreign keys and the
9989 ** same as ROLLBACK for DEFERRED keys.  SETNULL means that the foreign
9990 ** key is set to NULL.  CASCADE means that a DELETE or UPDATE of the
9991 ** referenced table row is propagated into the row that holds the
9992 ** foreign key.
9993 **
9994 ** The following symbolic values are used to record which type
9995 ** of action to take.
9996 */
9997 #define OE_None     0   /* There is no constraint to check */
9998 #define OE_Rollback 1   /* Fail the operation and rollback the transaction */
9999 #define OE_Abort    2   /* Back out changes but do no rollback transaction */
10000 #define OE_Fail     3   /* Stop the operation but leave all prior changes */
10001 #define OE_Ignore   4   /* Ignore the error. Do not do the INSERT or UPDATE */
10002 #define OE_Replace  5   /* Delete existing record, then do INSERT or UPDATE */
10003 
10004 #define OE_Restrict 6   /* OE_Abort for IMMEDIATE, OE_Rollback for DEFERRED */
10005 #define OE_SetNull  7   /* Set the foreign key value to NULL */
10006 #define OE_SetDflt  8   /* Set the foreign key value to its default */
10007 #define OE_Cascade  9   /* Cascade the changes */
10008 
10009 #define OE_Default  99  /* Do whatever the default action is */
10010 
10011 
10012 /*
10013 ** An instance of the following structure is passed as the first
10014 ** argument to sqlite3VdbeKeyCompare and is used to control the
10015 ** comparison of the two index keys.
10016 */
10017 struct KeyInfo {
10018   sqlite3 *db;        /* The database connection */
10019   u8 enc;             /* Text encoding - one of the SQLITE_UTF* values */
10020   u16 nField;         /* Number of entries in aColl[] */
10021   u8 *aSortOrder;     /* Sort order for each column.  May be NULL */
10022   CollSeq *aColl[1];  /* Collating sequence for each term of the key */
10023 };
10024 
10025 /*
10026 ** An instance of the following structure holds information about a
10027 ** single index record that has already been parsed out into individual
10028 ** values.
10029 **
10030 ** A record is an object that contains one or more fields of data.
10031 ** Records are used to store the content of a table row and to store
10032 ** the key of an index.  A blob encoding of a record is created by
10033 ** the OP_MakeRecord opcode of the VDBE and is disassembled by the
10034 ** OP_Column opcode.
10035 **
10036 ** This structure holds a record that has already been disassembled
10037 ** into its constituent fields.
10038 */
10039 struct UnpackedRecord {
10040   KeyInfo *pKeyInfo;  /* Collation and sort-order information */
10041   u16 nField;         /* Number of entries in apMem[] */
10042   u16 flags;          /* Boolean settings.  UNPACKED_... below */
10043   i64 rowid;          /* Used by UNPACKED_PREFIX_SEARCH */
10044   Mem *aMem;          /* Values */
10045 };
10046 
10047 /*
10048 ** Allowed values of UnpackedRecord.flags
10049 */
10050 #define UNPACKED_NEED_FREE     0x0001  /* Memory is from sqlite3Malloc() */
10051 #define UNPACKED_NEED_DESTROY  0x0002  /* apMem[]s should all be destroyed */
10052 #define UNPACKED_IGNORE_ROWID  0x0004  /* Ignore trailing rowid on key1 */
10053 #define UNPACKED_INCRKEY       0x0008  /* Make this key an epsilon larger */
10054 #define UNPACKED_PREFIX_MATCH  0x0010  /* A prefix match is considered OK */
10055 #define UNPACKED_PREFIX_SEARCH 0x0020  /* A prefix match is considered OK */
10056 
10057 /*
10058 ** Each SQL index is represented in memory by an
10059 ** instance of the following structure.
10060 **
10061 ** The columns of the table that are to be indexed are described
10062 ** by the aiColumn[] field of this structure.  For example, suppose
10063 ** we have the following table and index:
10064 **
10065 **     CREATE TABLE Ex1(c1 int, c2 int, c3 text);
10066 **     CREATE INDEX Ex2 ON Ex1(c3,c1);
10067 **
10068 ** In the Table structure describing Ex1, nCol==3 because there are
10069 ** three columns in the table.  In the Index structure describing
10070 ** Ex2, nColumn==2 since 2 of the 3 columns of Ex1 are indexed.
10071 ** The value of aiColumn is {2, 0}.  aiColumn[0]==2 because the
10072 ** first column to be indexed (c3) has an index of 2 in Ex1.aCol[].
10073 ** The second column to be indexed (c1) has an index of 0 in
10074 ** Ex1.aCol[], hence Ex2.aiColumn[1]==0.
10075 **
10076 ** The Index.onError field determines whether or not the indexed columns
10077 ** must be unique and what to do if they are not.  When Index.onError=OE_None,
10078 ** it means this is not a unique index.  Otherwise it is a unique index
10079 ** and the value of Index.onError indicate the which conflict resolution
10080 ** algorithm to employ whenever an attempt is made to insert a non-unique
10081 ** element.
10082 */
10083 struct Index {
10084   char *zName;     /* Name of this index */
10085   int nColumn;     /* Number of columns in the table used by this index */
10086   int *aiColumn;   /* Which columns are used by this index.  1st is 0 */
10087   unsigned *aiRowEst; /* Result of ANALYZE: Est. rows selected by each column */
10088   Table *pTable;   /* The SQL table being indexed */
10089   int tnum;        /* Page containing root of this index in database file */
10090   u8 onError;      /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
10091   u8 autoIndex;    /* True if is automatically created (ex: by UNIQUE) */
10092   u8 bUnordered;   /* Use this index for == or IN queries only */
10093   char *zColAff;   /* String defining the affinity of each column */
10094   Index *pNext;    /* The next index associated with the same table */
10095   Schema *pSchema; /* Schema containing this index */
10096   u8 *aSortOrder;  /* Array of size Index.nColumn. True==DESC, False==ASC */
10097   char **azColl;   /* Array of collation sequence names for index */
10098   IndexSample *aSample;    /* Array of SQLITE_INDEX_SAMPLES samples */
10099 };
10100 
10101 /*
10102 ** Each sample stored in the sqlite_stat2 table is represented in memory
10103 ** using a structure of this type.
10104 */
10105 struct IndexSample {
10106   union {
10107     char *z;        /* Value if eType is SQLITE_TEXT or SQLITE_BLOB */
10108     double r;       /* Value if eType is SQLITE_FLOAT or SQLITE_INTEGER */
10109   } u;
10110   u8 eType;         /* SQLITE_NULL, SQLITE_INTEGER ... etc. */
10111   u8 nByte;         /* Size in byte of text or blob. */
10112 };
10113 
10114 /*
10115 ** Each token coming out of the lexer is an instance of
10116 ** this structure.  Tokens are also used as part of an expression.
10117 **
10118 ** Note if Token.z==0 then Token.dyn and Token.n are undefined and
10119 ** may contain random values.  Do not make any assumptions about Token.dyn
10120 ** and Token.n when Token.z==0.
10121 */
10122 struct Token {
10123   const char *z;     /* Text of the token.  Not NULL-terminated! */
10124   unsigned int n;    /* Number of characters in this token */
10125 };
10126 
10127 /*
10128 ** An instance of this structure contains information needed to generate
10129 ** code for a SELECT that contains aggregate functions.
10130 **
10131 ** If Expr.op==TK_AGG_COLUMN or TK_AGG_FUNCTION then Expr.pAggInfo is a
10132 ** pointer to this structure.  The Expr.iColumn field is the index in
10133 ** AggInfo.aCol[] or AggInfo.aFunc[] of information needed to generate
10134 ** code for that node.
10135 **
10136 ** AggInfo.pGroupBy and AggInfo.aFunc.pExpr point to fields within the
10137 ** original Select structure that describes the SELECT statement.  These
10138 ** fields do not need to be freed when deallocating the AggInfo structure.
10139 */
10140 struct AggInfo {
10141   u8 directMode;          /* Direct rendering mode means take data directly
10142                           ** from source tables rather than from accumulators */
10143   u8 useSortingIdx;       /* In direct mode, reference the sorting index rather
10144                           ** than the source table */
10145   int sortingIdx;         /* Cursor number of the sorting index */
10146   ExprList *pGroupBy;     /* The group by clause */
10147   int nSortingColumn;     /* Number of columns in the sorting index */
10148   struct AggInfo_col {    /* For each column used in source tables */
10149     Table *pTab;             /* Source table */
10150     int iTable;              /* Cursor number of the source table */
10151     int iColumn;             /* Column number within the source table */
10152     int iSorterColumn;       /* Column number in the sorting index */
10153     int iMem;                /* Memory location that acts as accumulator */
10154     Expr *pExpr;             /* The original expression */
10155   } *aCol;
10156   int nColumn;            /* Number of used entries in aCol[] */
10157   int nColumnAlloc;       /* Number of slots allocated for aCol[] */
10158   int nAccumulator;       /* Number of columns that show through to the output.
10159                           ** Additional columns are used only as parameters to
10160                           ** aggregate functions */
10161   struct AggInfo_func {   /* For each aggregate function */
10162     Expr *pExpr;             /* Expression encoding the function */
10163     FuncDef *pFunc;          /* The aggregate function implementation */
10164     int iMem;                /* Memory location that acts as accumulator */
10165     int iDistinct;           /* Ephemeral table used to enforce DISTINCT */
10166   } *aFunc;
10167   int nFunc;              /* Number of entries in aFunc[] */
10168   int nFuncAlloc;         /* Number of slots allocated for aFunc[] */
10169 };
10170 
10171 /*
10172 ** The datatype ynVar is a signed integer, either 16-bit or 32-bit.
10173 ** Usually it is 16-bits.  But if SQLITE_MAX_VARIABLE_NUMBER is greater
10174 ** than 32767 we have to make it 32-bit.  16-bit is preferred because
10175 ** it uses less memory in the Expr object, which is a big memory user
10176 ** in systems with lots of prepared statements.  And few applications
10177 ** need more than about 10 or 20 variables.  But some extreme users want
10178 ** to have prepared statements with over 32767 variables, and for them
10179 ** the option is available (at compile-time).
10180 */
10181 #if SQLITE_MAX_VARIABLE_NUMBER<=32767
10182 typedef i16 ynVar;
10183 #else
10184 typedef int ynVar;
10185 #endif
10186 
10187 /*
10188 ** Each node of an expression in the parse tree is an instance
10189 ** of this structure.
10190 **
10191 ** Expr.op is the opcode. The integer parser token codes are reused
10192 ** as opcodes here. For example, the parser defines TK_GE to be an integer
10193 ** code representing the ">=" operator. This same integer code is reused
10194 ** to represent the greater-than-or-equal-to operator in the expression
10195 ** tree.
10196 **
10197 ** If the expression is an SQL literal (TK_INTEGER, TK_FLOAT, TK_BLOB,
10198 ** or TK_STRING), then Expr.token contains the text of the SQL literal. If
10199 ** the expression is a variable (TK_VARIABLE), then Expr.token contains the
10200 ** variable name. Finally, if the expression is an SQL function (TK_FUNCTION),
10201 ** then Expr.token contains the name of the function.
10202 **
10203 ** Expr.pRight and Expr.pLeft are the left and right subexpressions of a
10204 ** binary operator. Either or both may be NULL.
10205 **
10206 ** Expr.x.pList is a list of arguments if the expression is an SQL function,
10207 ** a CASE expression or an IN expression of the form "<lhs> IN (<y>, <z>...)".
10208 ** Expr.x.pSelect is used if the expression is a sub-select or an expression of
10209 ** the form "<lhs> IN (SELECT ...)". If the EP_xIsSelect bit is set in the
10210 ** Expr.flags mask, then Expr.x.pSelect is valid. Otherwise, Expr.x.pList is
10211 ** valid.
10212 **
10213 ** An expression of the form ID or ID.ID refers to a column in a table.
10214 ** For such expressions, Expr.op is set to TK_COLUMN and Expr.iTable is
10215 ** the integer cursor number of a VDBE cursor pointing to that table and
10216 ** Expr.iColumn is the column number for the specific column.  If the
10217 ** expression is used as a result in an aggregate SELECT, then the
10218 ** value is also stored in the Expr.iAgg column in the aggregate so that
10219 ** it can be accessed after all aggregates are computed.
10220 **
10221 ** If the expression is an unbound variable marker (a question mark
10222 ** character '?' in the original SQL) then the Expr.iTable holds the index
10223 ** number for that variable.
10224 **
10225 ** If the expression is a subquery then Expr.iColumn holds an integer
10226 ** register number containing the result of the subquery.  If the
10227 ** subquery gives a constant result, then iTable is -1.  If the subquery
10228 ** gives a different answer at different times during statement processing
10229 ** then iTable is the address of a subroutine that computes the subquery.
10230 **
10231 ** If the Expr is of type OP_Column, and the table it is selecting from
10232 ** is a disk table or the "old.*" pseudo-table, then pTab points to the
10233 ** corresponding table definition.
10234 **
10235 ** ALLOCATION NOTES:
10236 **
10237 ** Expr objects can use a lot of memory space in database schema.  To
10238 ** help reduce memory requirements, sometimes an Expr object will be
10239 ** truncated.  And to reduce the number of memory allocations, sometimes
10240 ** two or more Expr objects will be stored in a single memory allocation,
10241 ** together with Expr.zToken strings.
10242 **
10243 ** If the EP_Reduced and EP_TokenOnly flags are set when
10244 ** an Expr object is truncated.  When EP_Reduced is set, then all
10245 ** the child Expr objects in the Expr.pLeft and Expr.pRight subtrees
10246 ** are contained within the same memory allocation.  Note, however, that
10247 ** the subtrees in Expr.x.pList or Expr.x.pSelect are always separately
10248 ** allocated, regardless of whether or not EP_Reduced is set.
10249 */
10250 struct Expr {
10251   u8 op;                 /* Operation performed by this node */
10252   char affinity;         /* The affinity of the column or 0 if not a column */
10253   u16 flags;             /* Various flags.  EP_* See below */
10254   union {
10255     char *zToken;          /* Token value. Zero terminated and dequoted */
10256     int iValue;            /* Non-negative integer value if EP_IntValue */
10257   } u;
10258 
10259   /* If the EP_TokenOnly flag is set in the Expr.flags mask, then no
10260   ** space is allocated for the fields below this point. An attempt to
10261   ** access them will result in a segfault or malfunction.
10262   *********************************************************************/
10263 
10264   Expr *pLeft;           /* Left subnode */
10265   Expr *pRight;          /* Right subnode */
10266   union {
10267     ExprList *pList;     /* Function arguments or in "<expr> IN (<expr-list)" */
10268     Select *pSelect;     /* Used for sub-selects and "<expr> IN (<select>)" */
10269   } x;
10270   CollSeq *pColl;        /* The collation type of the column or 0 */
10271 
10272   /* If the EP_Reduced flag is set in the Expr.flags mask, then no
10273   ** space is allocated for the fields below this point. An attempt to
10274   ** access them will result in a segfault or malfunction.
10275   *********************************************************************/
10276 
10277   int iTable;            /* TK_COLUMN: cursor number of table holding column
10278                          ** TK_REGISTER: register number
10279                          ** TK_TRIGGER: 1 -> new, 0 -> old */
10280   ynVar iColumn;         /* TK_COLUMN: column index.  -1 for rowid.
10281                          ** TK_VARIABLE: variable number (always >= 1). */
10282   i16 iAgg;              /* Which entry in pAggInfo->aCol[] or ->aFunc[] */
10283   i16 iRightJoinTable;   /* If EP_FromJoin, the right table of the join */
10284   u8 flags2;             /* Second set of flags.  EP2_... */
10285   u8 op2;                /* If a TK_REGISTER, the original value of Expr.op */
10286   AggInfo *pAggInfo;     /* Used by TK_AGG_COLUMN and TK_AGG_FUNCTION */
10287   Table *pTab;           /* Table for TK_COLUMN expressions. */
10288 #if SQLITE_MAX_EXPR_DEPTH>0
10289   int nHeight;           /* Height of the tree headed by this node */
10290 #endif
10291 };
10292 
10293 /*
10294 ** The following are the meanings of bits in the Expr.flags field.
10295 */
10296 #define EP_FromJoin   0x0001  /* Originated in ON or USING clause of a join */
10297 #define EP_Agg        0x0002  /* Contains one or more aggregate functions */
10298 #define EP_Resolved   0x0004  /* IDs have been resolved to COLUMNs */
10299 #define EP_Error      0x0008  /* Expression contains one or more errors */
10300 #define EP_Distinct   0x0010  /* Aggregate function with DISTINCT keyword */
10301 #define EP_VarSelect  0x0020  /* pSelect is correlated, not constant */
10302 #define EP_DblQuoted  0x0040  /* token.z was originally in "..." */
10303 #define EP_InfixFunc  0x0080  /* True for an infix function: LIKE, GLOB, etc */
10304 #define EP_ExpCollate 0x0100  /* Collating sequence specified explicitly */
10305 #define EP_FixedDest  0x0200  /* Result needed in a specific register */
10306 #define EP_IntValue   0x0400  /* Integer value contained in u.iValue */
10307 #define EP_xIsSelect  0x0800  /* x.pSelect is valid (otherwise x.pList is) */
10308 
10309 #define EP_Reduced    0x1000  /* Expr struct is EXPR_REDUCEDSIZE bytes only */
10310 #define EP_TokenOnly  0x2000  /* Expr struct is EXPR_TOKENONLYSIZE bytes only */
10311 #define EP_Static     0x4000  /* Held in memory not obtained from malloc() */
10312 
10313 /*
10314 ** The following are the meanings of bits in the Expr.flags2 field.
10315 */
10316 #define EP2_MallocedToken  0x0001  /* Need to sqlite3DbFree() Expr.zToken */
10317 #define EP2_Irreducible    0x0002  /* Cannot EXPRDUP_REDUCE this Expr */
10318 
10319 /*
10320 ** The pseudo-routine sqlite3ExprSetIrreducible sets the EP2_Irreducible
10321 ** flag on an expression structure.  This flag is used for VV&A only.  The
10322 ** routine is implemented as a macro that only works when in debugging mode,
10323 ** so as not to burden production code.
10324 */
10325 #ifdef SQLITE_DEBUG
10326 # define ExprSetIrreducible(X)  (X)->flags2 |= EP2_Irreducible
10327 #else
10328 # define ExprSetIrreducible(X)
10329 #endif
10330 
10331 /*
10332 ** These macros can be used to test, set, or clear bits in the
10333 ** Expr.flags field.
10334 */
10335 #define ExprHasProperty(E,P)     (((E)->flags&(P))==(P))
10336 #define ExprHasAnyProperty(E,P)  (((E)->flags&(P))!=0)
10337 #define ExprSetProperty(E,P)     (E)->flags|=(P)
10338 #define ExprClearProperty(E,P)   (E)->flags&=~(P)
10339 
10340 /*
10341 ** Macros to determine the number of bytes required by a normal Expr
10342 ** struct, an Expr struct with the EP_Reduced flag set in Expr.flags
10343 ** and an Expr struct with the EP_TokenOnly flag set.
10344 */
10345 #define EXPR_FULLSIZE           sizeof(Expr)           /* Full size */
10346 #define EXPR_REDUCEDSIZE        offsetof(Expr,iTable)  /* Common features */
10347 #define EXPR_TOKENONLYSIZE      offsetof(Expr,pLeft)   /* Fewer features */
10348 
10349 /*
10350 ** Flags passed to the sqlite3ExprDup() function. See the header comment
10351 ** above sqlite3ExprDup() for details.
10352 */
10353 #define EXPRDUP_REDUCE         0x0001  /* Used reduced-size Expr nodes */
10354 
10355 /*
10356 ** A list of expressions.  Each expression may optionally have a
10357 ** name.  An expr/name combination can be used in several ways, such
10358 ** as the list of "expr AS ID" fields following a "SELECT" or in the
10359 ** list of "ID = expr" items in an UPDATE.  A list of expressions can
10360 ** also be used as the argument to a function, in which case the a.zName
10361 ** field is not used.
10362 */
10363 struct ExprList {
10364   int nExpr;             /* Number of expressions on the list */
10365   int nAlloc;            /* Number of entries allocated below */
10366   int iECursor;          /* VDBE Cursor associated with this ExprList */
10367   struct ExprList_item {
10368     Expr *pExpr;           /* The list of expressions */
10369     char *zName;           /* Token associated with this expression */
10370     char *zSpan;           /* Original text of the expression */
10371     u8 sortOrder;          /* 1 for DESC or 0 for ASC */
10372     u8 done;               /* A flag to indicate when processing is finished */
10373     u16 iCol;              /* For ORDER BY, column number in result set */
10374     u16 iAlias;            /* Index into Parse.aAlias[] for zName */
10375   } *a;                  /* One entry for each expression */
10376 };
10377 
10378 /*
10379 ** An instance of this structure is used by the parser to record both
10380 ** the parse tree for an expression and the span of input text for an
10381 ** expression.
10382 */
10383 struct ExprSpan {
10384   Expr *pExpr;          /* The expression parse tree */
10385   const char *zStart;   /* First character of input text */
10386   const char *zEnd;     /* One character past the end of input text */
10387 };
10388 
10389 /*
10390 ** An instance of this structure can hold a simple list of identifiers,
10391 ** such as the list "a,b,c" in the following statements:
10392 **
10393 **      INSERT INTO t(a,b,c) VALUES ...;
10394 **      CREATE INDEX idx ON t(a,b,c);
10395 **      CREATE TRIGGER trig BEFORE UPDATE ON t(a,b,c) ...;
10396 **
10397 ** The IdList.a.idx field is used when the IdList represents the list of
10398 ** column names after a table name in an INSERT statement.  In the statement
10399 **
10400 **     INSERT INTO t(a,b,c) ...
10401 **
10402 ** If "a" is the k-th column of table "t", then IdList.a[0].idx==k.
10403 */
10404 struct IdList {
10405   struct IdList_item {
10406     char *zName;      /* Name of the identifier */
10407     int idx;          /* Index in some Table.aCol[] of a column named zName */
10408   } *a;
10409   int nId;         /* Number of identifiers on the list */
10410   int nAlloc;      /* Number of entries allocated for a[] below */
10411 };
10412 
10413 /*
10414 ** The bitmask datatype defined below is used for various optimizations.
10415 **
10416 ** Changing this from a 64-bit to a 32-bit type limits the number of
10417 ** tables in a join to 32 instead of 64.  But it also reduces the size
10418 ** of the library by 738 bytes on ix86.
10419 */
10420 typedef u64 Bitmask;
10421 
10422 /*
10423 ** The number of bits in a Bitmask.  "BMS" means "BitMask Size".
10424 */
10425 #define BMS  ((int)(sizeof(Bitmask)*8))
10426 
10427 /*
10428 ** The following structure describes the FROM clause of a SELECT statement.
10429 ** Each table or subquery in the FROM clause is a separate element of
10430 ** the SrcList.a[] array.
10431 **
10432 ** With the addition of multiple database support, the following structure
10433 ** can also be used to describe a particular table such as the table that
10434 ** is modified by an INSERT, DELETE, or UPDATE statement.  In standard SQL,
10435 ** such a table must be a simple name: ID.  But in SQLite, the table can
10436 ** now be identified by a database name, a dot, then the table name: ID.ID.
10437 **
10438 ** The jointype starts out showing the join type between the current table
10439 ** and the next table on the list.  The parser builds the list this way.
10440 ** But sqlite3SrcListShiftJoinType() later shifts the jointypes so that each
10441 ** jointype expresses the join between the table and the previous table.
10442 **
10443 ** In the colUsed field, the high-order bit (bit 63) is set if the table
10444 ** contains more than 63 columns and the 64-th or later column is used.
10445 */
10446 struct SrcList {
10447   i16 nSrc;        /* Number of tables or subqueries in the FROM clause */
10448   i16 nAlloc;      /* Number of entries allocated in a[] below */
10449   struct SrcList_item {
10450     char *zDatabase;  /* Name of database holding this table */
10451     char *zName;      /* Name of the table */
10452     char *zAlias;     /* The "B" part of a "A AS B" phrase.  zName is the "A" */
10453     Table *pTab;      /* An SQL table corresponding to zName */
10454     Select *pSelect;  /* A SELECT statement used in place of a table name */
10455     u8 isPopulated;   /* Temporary table associated with SELECT is populated */
10456     u8 jointype;      /* Type of join between this able and the previous */
10457     u8 notIndexed;    /* True if there is a NOT INDEXED clause */
10458 #ifndef SQLITE_OMIT_EXPLAIN
10459     u8 iSelectId;     /* If pSelect!=0, the id of the sub-select in EQP */
10460 #endif
10461     int iCursor;      /* The VDBE cursor number used to access this table */
10462     Expr *pOn;        /* The ON clause of a join */
10463     IdList *pUsing;   /* The USING clause of a join */
10464     Bitmask colUsed;  /* Bit N (1<<N) set if column N of pTab is used */
10465     char *zIndex;     /* Identifier from "INDEXED BY <zIndex>" clause */
10466     Index *pIndex;    /* Index structure corresponding to zIndex, if any */
10467   } a[1];             /* One entry for each identifier on the list */
10468 };
10469 
10470 /*
10471 ** Permitted values of the SrcList.a.jointype field
10472 */
10473 #define JT_INNER     0x0001    /* Any kind of inner or cross join */
10474 #define JT_CROSS     0x0002    /* Explicit use of the CROSS keyword */
10475 #define JT_NATURAL   0x0004    /* True for a "natural" join */
10476 #define JT_LEFT      0x0008    /* Left outer join */
10477 #define JT_RIGHT     0x0010    /* Right outer join */
10478 #define JT_OUTER     0x0020    /* The "OUTER" keyword is present */
10479 #define JT_ERROR     0x0040    /* unknown or unsupported join type */
10480 
10481 
10482 /*
10483 ** A WherePlan object holds information that describes a lookup
10484 ** strategy.
10485 **
10486 ** This object is intended to be opaque outside of the where.c module.
10487 ** It is included here only so that that compiler will know how big it
10488 ** is.  None of the fields in this object should be used outside of
10489 ** the where.c module.
10490 **
10491 ** Within the union, pIdx is only used when wsFlags&WHERE_INDEXED is true.
10492 ** pTerm is only used when wsFlags&WHERE_MULTI_OR is true.  And pVtabIdx
10493 ** is only used when wsFlags&WHERE_VIRTUALTABLE is true.  It is never the
10494 ** case that more than one of these conditions is true.
10495 */
10496 struct WherePlan {
10497   u32 wsFlags;                   /* WHERE_* flags that describe the strategy */
10498   u32 nEq;                       /* Number of == constraints */
10499   double nRow;                   /* Estimated number of rows (for EQP) */
10500   union {
10501     Index *pIdx;                   /* Index when WHERE_INDEXED is true */
10502     struct WhereTerm *pTerm;       /* WHERE clause term for OR-search */
10503     sqlite3_index_info *pVtabIdx;  /* Virtual table index to use */
10504   } u;
10505 };
10506 
10507 /*
10508 ** For each nested loop in a WHERE clause implementation, the WhereInfo
10509 ** structure contains a single instance of this structure.  This structure
10510 ** is intended to be private the the where.c module and should not be
10511 ** access or modified by other modules.
10512 **
10513 ** The pIdxInfo field is used to help pick the best index on a
10514 ** virtual table.  The pIdxInfo pointer contains indexing
10515 ** information for the i-th table in the FROM clause before reordering.
10516 ** All the pIdxInfo pointers are freed by whereInfoFree() in where.c.
10517 ** All other information in the i-th WhereLevel object for the i-th table
10518 ** after FROM clause ordering.
10519 */
10520 struct WhereLevel {
10521   WherePlan plan;       /* query plan for this element of the FROM clause */
10522   int iLeftJoin;        /* Memory cell used to implement LEFT OUTER JOIN */
10523   int iTabCur;          /* The VDBE cursor used to access the table */
10524   int iIdxCur;          /* The VDBE cursor used to access pIdx */
10525   int addrBrk;          /* Jump here to break out of the loop */
10526   int addrNxt;          /* Jump here to start the next IN combination */
10527   int addrCont;         /* Jump here to continue with the next loop cycle */
10528   int addrFirst;        /* First instruction of interior of the loop */
10529   u8 iFrom;             /* Which entry in the FROM clause */
10530   u8 op, p5;            /* Opcode and P5 of the opcode that ends the loop */
10531   int p1, p2;           /* Operands of the opcode used to ends the loop */
10532   union {               /* Information that depends on plan.wsFlags */
10533     struct {
10534       int nIn;              /* Number of entries in aInLoop[] */
10535       struct InLoop {
10536         int iCur;              /* The VDBE cursor used by this IN operator */
10537         int addrInTop;         /* Top of the IN loop */
10538       } *aInLoop;           /* Information about each nested IN operator */
10539     } in;                 /* Used when plan.wsFlags&WHERE_IN_ABLE */
10540   } u;
10541 
10542   /* The following field is really not part of the current level.  But
10543   ** we need a place to cache virtual table index information for each
10544   ** virtual table in the FROM clause and the WhereLevel structure is
10545   ** a convenient place since there is one WhereLevel for each FROM clause
10546   ** element.
10547   */
10548   sqlite3_index_info *pIdxInfo;  /* Index info for n-th source table */
10549 };
10550 
10551 /*
10552 ** Flags appropriate for the wctrlFlags parameter of sqlite3WhereBegin()
10553 ** and the WhereInfo.wctrlFlags member.
10554 */
10555 #define WHERE_ORDERBY_NORMAL   0x0000 /* No-op */
10556 #define WHERE_ORDERBY_MIN      0x0001 /* ORDER BY processing for min() func */
10557 #define WHERE_ORDERBY_MAX      0x0002 /* ORDER BY processing for max() func */
10558 #define WHERE_ONEPASS_DESIRED  0x0004 /* Want to do one-pass UPDATE/DELETE */
10559 #define WHERE_DUPLICATES_OK    0x0008 /* Ok to return a row more than once */
10560 #define WHERE_OMIT_OPEN        0x0010 /* Table cursors are already open */
10561 #define WHERE_OMIT_CLOSE       0x0020 /* Omit close of table & index cursors */
10562 #define WHERE_FORCE_TABLE      0x0040 /* Do not use an index-only search */
10563 #define WHERE_ONETABLE_ONLY    0x0080 /* Only code the 1st table in pTabList */
10564 
10565 /*
10566 ** The WHERE clause processing routine has two halves.  The
10567 ** first part does the start of the WHERE loop and the second
10568 ** half does the tail of the WHERE loop.  An instance of
10569 ** this structure is returned by the first half and passed
10570 ** into the second half to give some continuity.
10571 */
10572 struct WhereInfo {
10573   Parse *pParse;       /* Parsing and code generating context */
10574   u16 wctrlFlags;      /* Flags originally passed to sqlite3WhereBegin() */
10575   u8 okOnePass;        /* Ok to use one-pass algorithm for UPDATE or DELETE */
10576   u8 untestedTerms;    /* Not all WHERE terms resolved by outer loop */
10577   SrcList *pTabList;             /* List of tables in the join */
10578   int iTop;                      /* The very beginning of the WHERE loop */
10579   int iContinue;                 /* Jump here to continue with next record */
10580   int iBreak;                    /* Jump here to break out of the loop */
10581   int nLevel;                    /* Number of nested loop */
10582   struct WhereClause *pWC;       /* Decomposition of the WHERE clause */
10583   double savedNQueryLoop;        /* pParse->nQueryLoop outside the WHERE loop */
10584   double nRowOut;                /* Estimated number of output rows */
10585   WhereLevel a[1];               /* Information about each nest loop in WHERE */
10586 };
10587 
10588 /*
10589 ** A NameContext defines a context in which to resolve table and column
10590 ** names.  The context consists of a list of tables (the pSrcList) field and
10591 ** a list of named expression (pEList).  The named expression list may
10592 ** be NULL.  The pSrc corresponds to the FROM clause of a SELECT or
10593 ** to the table being operated on by INSERT, UPDATE, or DELETE.  The
10594 ** pEList corresponds to the result set of a SELECT and is NULL for
10595 ** other statements.
10596 **
10597 ** NameContexts can be nested.  When resolving names, the inner-most
10598 ** context is searched first.  If no match is found, the next outer
10599 ** context is checked.  If there is still no match, the next context
10600 ** is checked.  This process continues until either a match is found
10601 ** or all contexts are check.  When a match is found, the nRef member of
10602 ** the context containing the match is incremented.
10603 **
10604 ** Each subquery gets a new NameContext.  The pNext field points to the
10605 ** NameContext in the parent query.  Thus the process of scanning the
10606 ** NameContext list corresponds to searching through successively outer
10607 ** subqueries looking for a match.
10608 */
10609 struct NameContext {
10610   Parse *pParse;       /* The parser */
10611   SrcList *pSrcList;   /* One or more tables used to resolve names */
10612   ExprList *pEList;    /* Optional list of named expressions */
10613   int nRef;            /* Number of names resolved by this context */
10614   int nErr;            /* Number of errors encountered while resolving names */
10615   u8 allowAgg;         /* Aggregate functions allowed here */
10616   u8 hasAgg;           /* True if aggregates are seen */
10617   u8 isCheck;          /* True if resolving names in a CHECK constraint */
10618   int nDepth;          /* Depth of subquery recursion. 1 for no recursion */
10619   AggInfo *pAggInfo;   /* Information about aggregates at this level */
10620   NameContext *pNext;  /* Next outer name context.  NULL for outermost */
10621 };
10622 
10623 /*
10624 ** An instance of the following structure contains all information
10625 ** needed to generate code for a single SELECT statement.
10626 **
10627 ** nLimit is set to -1 if there is no LIMIT clause.  nOffset is set to 0.
10628 ** If there is a LIMIT clause, the parser sets nLimit to the value of the
10629 ** limit and nOffset to the value of the offset (or 0 if there is not
10630 ** offset).  But later on, nLimit and nOffset become the memory locations
10631 ** in the VDBE that record the limit and offset counters.
10632 **
10633 ** addrOpenEphm[] entries contain the address of OP_OpenEphemeral opcodes.
10634 ** These addresses must be stored so that we can go back and fill in
10635 ** the P4_KEYINFO and P2 parameters later.  Neither the KeyInfo nor
10636 ** the number of columns in P2 can be computed at the same time
10637 ** as the OP_OpenEphm instruction is coded because not
10638 ** enough information about the compound query is known at that point.
10639 ** The KeyInfo for addrOpenTran[0] and [1] contains collating sequences
10640 ** for the result set.  The KeyInfo for addrOpenTran[2] contains collating
10641 ** sequences for the ORDER BY clause.
10642 */
10643 struct Select {
10644   ExprList *pEList;      /* The fields of the result */
10645   u8 op;                 /* One of: TK_UNION TK_ALL TK_INTERSECT TK_EXCEPT */
10646   char affinity;         /* MakeRecord with this affinity for SRT_Set */
10647   u16 selFlags;          /* Various SF_* values */
10648   SrcList *pSrc;         /* The FROM clause */
10649   Expr *pWhere;          /* The WHERE clause */
10650   ExprList *pGroupBy;    /* The GROUP BY clause */
10651   Expr *pHaving;         /* The HAVING clause */
10652   ExprList *pOrderBy;    /* The ORDER BY clause */
10653   Select *pPrior;        /* Prior select in a compound select statement */
10654   Select *pNext;         /* Next select to the left in a compound */
10655   Select *pRightmost;    /* Right-most select in a compound select statement */
10656   Expr *pLimit;          /* LIMIT expression. NULL means not used. */
10657   Expr *pOffset;         /* OFFSET expression. NULL means not used. */
10658   int iLimit, iOffset;   /* Memory registers holding LIMIT & OFFSET counters */
10659   int addrOpenEphm[3];   /* OP_OpenEphem opcodes related to this select */
10660   double nSelectRow;     /* Estimated number of result rows */
10661 };
10662 
10663 /*
10664 ** Allowed values for Select.selFlags.  The "SF" prefix stands for
10665 ** "Select Flag".
10666 */
10667 #define SF_Distinct        0x0001  /* Output should be DISTINCT */
10668 #define SF_Resolved        0x0002  /* Identifiers have been resolved */
10669 #define SF_Aggregate       0x0004  /* Contains aggregate functions */
10670 #define SF_UsesEphemeral   0x0008  /* Uses the OpenEphemeral opcode */
10671 #define SF_Expanded        0x0010  /* sqlite3SelectExpand() called on this */
10672 #define SF_HasTypeInfo     0x0020  /* FROM subqueries have Table metadata */
10673 
10674 
10675 /*
10676 ** The results of a select can be distributed in several ways.  The
10677 ** "SRT" prefix means "SELECT Result Type".
10678 */
10679 #define SRT_Union        1  /* Store result as keys in an index */
10680 #define SRT_Except       2  /* Remove result from a UNION index */
10681 #define SRT_Exists       3  /* Store 1 if the result is not empty */
10682 #define SRT_Discard      4  /* Do not save the results anywhere */
10683 
10684 /* The ORDER BY clause is ignored for all of the above */
10685 #define IgnorableOrderby(X) ((X->eDest)<=SRT_Discard)
10686 
10687 #define SRT_Output       5  /* Output each row of result */
10688 #define SRT_Mem          6  /* Store result in a memory cell */
10689 #define SRT_Set          7  /* Store results as keys in an index */
10690 #define SRT_Table        8  /* Store result as data with an automatic rowid */
10691 #define SRT_EphemTab     9  /* Create transient tab and store like SRT_Table */
10692 #define SRT_Coroutine   10  /* Generate a single row of result */
10693 
10694 /*
10695 ** A structure used to customize the behavior of sqlite3Select(). See
10696 ** comments above sqlite3Select() for details.
10697 */
10698 typedef struct SelectDest SelectDest;
10699 struct SelectDest {
10700   u8 eDest;         /* How to dispose of the results */
10701   u8 affinity;      /* Affinity used when eDest==SRT_Set */
10702   int iParm;        /* A parameter used by the eDest disposal method */
10703   int iMem;         /* Base register where results are written */
10704   int nMem;         /* Number of registers allocated */
10705 };
10706 
10707 /*
10708 ** During code generation of statements that do inserts into AUTOINCREMENT
10709 ** tables, the following information is attached to the Table.u.autoInc.p
10710 ** pointer of each autoincrement table to record some side information that
10711 ** the code generator needs.  We have to keep per-table autoincrement
10712 ** information in case inserts are down within triggers.  Triggers do not
10713 ** normally coordinate their activities, but we do need to coordinate the
10714 ** loading and saving of autoincrement information.
10715 */
10716 struct AutoincInfo {
10717   AutoincInfo *pNext;   /* Next info block in a list of them all */
10718   Table *pTab;          /* Table this info block refers to */
10719   int iDb;              /* Index in sqlite3.aDb[] of database holding pTab */
10720   int regCtr;           /* Memory register holding the rowid counter */
10721 };
10722 
10723 /*
10724 ** Size of the column cache
10725 */
10726 #ifndef SQLITE_N_COLCACHE
10727 # define SQLITE_N_COLCACHE 10
10728 #endif
10729 
10730 /*
10731 ** At least one instance of the following structure is created for each
10732 ** trigger that may be fired while parsing an INSERT, UPDATE or DELETE
10733 ** statement. All such objects are stored in the linked list headed at
10734 ** Parse.pTriggerPrg and deleted once statement compilation has been
10735 ** completed.
10736 **
10737 ** A Vdbe sub-program that implements the body and WHEN clause of trigger
10738 ** TriggerPrg.pTrigger, assuming a default ON CONFLICT clause of
10739 ** TriggerPrg.orconf, is stored in the TriggerPrg.pProgram variable.
10740 ** The Parse.pTriggerPrg list never contains two entries with the same
10741 ** values for both pTrigger and orconf.
10742 **
10743 ** The TriggerPrg.aColmask[0] variable is set to a mask of old.* columns
10744 ** accessed (or set to 0 for triggers fired as a result of INSERT
10745 ** statements). Similarly, the TriggerPrg.aColmask[1] variable is set to
10746 ** a mask of new.* columns used by the program.
10747 */
10748 struct TriggerPrg {
10749   Trigger *pTrigger;      /* Trigger this program was coded from */
10750   int orconf;             /* Default ON CONFLICT policy */
10751   SubProgram *pProgram;   /* Program implementing pTrigger/orconf */
10752   u32 aColmask[2];        /* Masks of old.*, new.* columns accessed */
10753   TriggerPrg *pNext;      /* Next entry in Parse.pTriggerPrg list */
10754 };
10755 
10756 /*
10757 ** The yDbMask datatype for the bitmask of all attached databases.
10758 */
10759 #if SQLITE_MAX_ATTACHED>30
10760   typedef sqlite3_uint64 yDbMask;
10761 #else
10762   typedef unsigned int yDbMask;
10763 #endif
10764 
10765 /*
10766 ** An SQL parser context.  A copy of this structure is passed through
10767 ** the parser and down into all the parser action routine in order to
10768 ** carry around information that is global to the entire parse.
10769 **
10770 ** The structure is divided into two parts.  When the parser and code
10771 ** generate call themselves recursively, the first part of the structure
10772 ** is constant but the second part is reset at the beginning and end of
10773 ** each recursion.
10774 **
10775 ** The nTableLock and aTableLock variables are only used if the shared-cache
10776 ** feature is enabled (if sqlite3Tsd()->useSharedData is true). They are
10777 ** used to store the set of table-locks required by the statement being
10778 ** compiled. Function sqlite3TableLock() is used to add entries to the
10779 ** list.
10780 */
10781 struct Parse {
10782   sqlite3 *db;         /* The main database structure */
10783   int rc;              /* Return code from execution */
10784   char *zErrMsg;       /* An error message */
10785   Vdbe *pVdbe;         /* An engine for executing database bytecode */
10786   u8 colNamesSet;      /* TRUE after OP_ColumnName has been issued to pVdbe */
10787   u8 nameClash;        /* A permanent table name clashes with temp table name */
10788   u8 checkSchema;      /* Causes schema cookie check after an error */
10789   u8 nested;           /* Number of nested calls to the parser/code generator */
10790   u8 parseError;       /* True after a parsing error.  Ticket #1794 */
10791   u8 nTempReg;         /* Number of temporary registers in aTempReg[] */
10792   u8 nTempInUse;       /* Number of aTempReg[] currently checked out */
10793   int aTempReg[8];     /* Holding area for temporary registers */
10794   int nRangeReg;       /* Size of the temporary register block */
10795   int iRangeReg;       /* First register in temporary register block */
10796   int nErr;            /* Number of errors seen */
10797   int nTab;            /* Number of previously allocated VDBE cursors */
10798   int nMem;            /* Number of memory cells used so far */
10799   int nSet;            /* Number of sets used so far */
10800   int ckBase;          /* Base register of data during check constraints */
10801   int iCacheLevel;     /* ColCache valid when aColCache[].iLevel<=iCacheLevel */
10802   int iCacheCnt;       /* Counter used to generate aColCache[].lru values */
10803   u8 nColCache;        /* Number of entries in the column cache */
10804   u8 iColCache;        /* Next entry of the cache to replace */
10805   struct yColCache {
10806     int iTable;           /* Table cursor number */
10807     int iColumn;          /* Table column number */
10808     u8 tempReg;           /* iReg is a temp register that needs to be freed */
10809     int iLevel;           /* Nesting level */
10810     int iReg;             /* Reg with value of this column. 0 means none. */
10811     int lru;              /* Least recently used entry has the smallest value */
10812   } aColCache[SQLITE_N_COLCACHE];  /* One for each column cache entry */
10813   yDbMask writeMask;   /* Start a write transaction on these databases */
10814   yDbMask cookieMask;  /* Bitmask of schema verified databases */
10815   u8 isMultiWrite;     /* True if statement may affect/insert multiple rows */
10816   u8 mayAbort;         /* True if statement may throw an ABORT exception */
10817   int cookieGoto;      /* Address of OP_Goto to cookie verifier subroutine */
10818   int cookieValue[SQLITE_MAX_ATTACHED+2];  /* Values of cookies to verify */
10819 #ifndef SQLITE_OMIT_SHARED_CACHE
10820   int nTableLock;        /* Number of locks in aTableLock */
10821   TableLock *aTableLock; /* Required table locks for shared-cache mode */
10822 #endif
10823   int regRowid;        /* Register holding rowid of CREATE TABLE entry */
10824   int regRoot;         /* Register holding root page number for new objects */
10825   AutoincInfo *pAinc;  /* Information about AUTOINCREMENT counters */
10826   int nMaxArg;         /* Max args passed to user function by sub-program */
10827 
10828   /* Information used while coding trigger programs. */
10829   Parse *pToplevel;    /* Parse structure for main program (or NULL) */
10830   Table *pTriggerTab;  /* Table triggers are being coded for */
10831   u32 oldmask;         /* Mask of old.* columns referenced */
10832   u32 newmask;         /* Mask of new.* columns referenced */
10833   u8 eTriggerOp;       /* TK_UPDATE, TK_INSERT or TK_DELETE */
10834   u8 eOrconf;          /* Default ON CONFLICT policy for trigger steps */
10835   u8 disableTriggers;  /* True to disable triggers */
10836   double nQueryLoop;   /* Estimated number of iterations of a query */
10837 
10838   /* Above is constant between recursions.  Below is reset before and after
10839   ** each recursion */
10840 
10841   int nVar;            /* Number of '?' variables seen in the SQL so far */
10842   int nzVar;           /* Number of available slots in azVar[] */
10843   char **azVar;        /* Pointers to names of parameters */
10844   Vdbe *pReprepare;    /* VM being reprepared (sqlite3Reprepare()) */
10845   int nAlias;          /* Number of aliased result set columns */
10846   int nAliasAlloc;     /* Number of allocated slots for aAlias[] */
10847   int *aAlias;         /* Register used to hold aliased result */
10848   u8 explain;          /* True if the EXPLAIN flag is found on the query */
10849   Token sNameToken;    /* Token with unqualified schema object name */
10850   Token sLastToken;    /* The last token parsed */
10851   const char *zTail;   /* All SQL text past the last semicolon parsed */
10852   Table *pNewTable;    /* A table being constructed by CREATE TABLE */
10853   Trigger *pNewTrigger;     /* Trigger under construct by a CREATE TRIGGER */
10854   const char *zAuthContext; /* The 6th parameter to db->xAuth callbacks */
10855 #ifndef SQLITE_OMIT_VIRTUALTABLE
10856   Token sArg;                /* Complete text of a module argument */
10857   u8 declareVtab;            /* True if inside sqlite3_declare_vtab() */
10858   int nVtabLock;             /* Number of virtual tables to lock */
10859   Table **apVtabLock;        /* Pointer to virtual tables needing locking */
10860 #endif
10861   int nHeight;            /* Expression tree height of current sub-select */
10862   Table *pZombieTab;      /* List of Table objects to delete after code gen */
10863   TriggerPrg *pTriggerPrg;    /* Linked list of coded triggers */
10864 
10865 #ifndef SQLITE_OMIT_EXPLAIN
10866   int iSelectId;
10867   int iNextSelectId;
10868 #endif
10869 };
10870 
10871 #ifdef SQLITE_OMIT_VIRTUALTABLE
10872   #define IN_DECLARE_VTAB 0
10873 #else
10874   #define IN_DECLARE_VTAB (pParse->declareVtab)
10875 #endif
10876 
10877 /*
10878 ** An instance of the following structure can be declared on a stack and used
10879 ** to save the Parse.zAuthContext value so that it can be restored later.
10880 */
10881 struct AuthContext {
10882   const char *zAuthContext;   /* Put saved Parse.zAuthContext here */
10883   Parse *pParse;              /* The Parse structure */
10884 };
10885 
10886 /*
10887 ** Bitfield flags for P5 value in OP_Insert and OP_Delete
10888 */
10889 #define OPFLAG_NCHANGE       0x01    /* Set to update db->nChange */
10890 #define OPFLAG_LASTROWID     0x02    /* Set to update db->lastRowid */
10891 #define OPFLAG_ISUPDATE      0x04    /* This OP_Insert is an sql UPDATE */
10892 #define OPFLAG_APPEND        0x08    /* This is likely to be an append */
10893 #define OPFLAG_USESEEKRESULT 0x10    /* Try to avoid a seek in BtreeInsert() */
10894 #define OPFLAG_CLEARCACHE    0x20    /* Clear pseudo-table cache in OP_Column */
10895 
10896 /*
10897  * Each trigger present in the database schema is stored as an instance of
10898  * struct Trigger.
10899  *
10900  * Pointers to instances of struct Trigger are stored in two ways.
10901  * 1. In the "trigHash" hash table (part of the sqlite3* that represents the
10902  *    database). This allows Trigger structures to be retrieved by name.
10903  * 2. All triggers associated with a single table form a linked list, using the
10904  *    pNext member of struct Trigger. A pointer to the first element of the
10905  *    linked list is stored as the "pTrigger" member of the associated
10906  *    struct Table.
10907  *
10908  * The "step_list" member points to the first element of a linked list
10909  * containing the SQL statements specified as the trigger program.
10910  */
10911 struct Trigger {
10912   char *zName;            /* The name of the trigger                        */
10913   char *table;            /* The table or view to which the trigger applies */
10914   u8 op;                  /* One of TK_DELETE, TK_UPDATE, TK_INSERT         */
10915   u8 tr_tm;               /* One of TRIGGER_BEFORE, TRIGGER_AFTER */
10916   Expr *pWhen;            /* The WHEN clause of the expression (may be NULL) */
10917   IdList *pColumns;       /* If this is an UPDATE OF <column-list> trigger,
10918                              the <column-list> is stored here */
10919   Schema *pSchema;        /* Schema containing the trigger */
10920   Schema *pTabSchema;     /* Schema containing the table */
10921   TriggerStep *step_list; /* Link list of trigger program steps             */
10922   Trigger *pNext;         /* Next trigger associated with the table */
10923 };
10924 
10925 /*
10926 ** A trigger is either a BEFORE or an AFTER trigger.  The following constants
10927 ** determine which.
10928 **
10929 ** If there are multiple triggers, you might of some BEFORE and some AFTER.
10930 ** In that cases, the constants below can be ORed together.
10931 */
10932 #define TRIGGER_BEFORE  1
10933 #define TRIGGER_AFTER   2
10934 
10935 /*
10936  * An instance of struct TriggerStep is used to store a single SQL statement
10937  * that is a part of a trigger-program.
10938  *
10939  * Instances of struct TriggerStep are stored in a singly linked list (linked
10940  * using the "pNext" member) referenced by the "step_list" member of the
10941  * associated struct Trigger instance. The first element of the linked list is
10942  * the first step of the trigger-program.
10943  *
10944  * The "op" member indicates whether this is a "DELETE", "INSERT", "UPDATE" or
10945  * "SELECT" statement. The meanings of the other members is determined by the
10946  * value of "op" as follows:
10947  *
10948  * (op == TK_INSERT)
10949  * orconf    -> stores the ON CONFLICT algorithm
10950  * pSelect   -> If this is an INSERT INTO ... SELECT ... statement, then
10951  *              this stores a pointer to the SELECT statement. Otherwise NULL.
10952  * target    -> A token holding the quoted name of the table to insert into.
10953  * pExprList -> If this is an INSERT INTO ... VALUES ... statement, then
10954  *              this stores values to be inserted. Otherwise NULL.
10955  * pIdList   -> If this is an INSERT INTO ... (<column-names>) VALUES ...
10956  *              statement, then this stores the column-names to be
10957  *              inserted into.
10958  *
10959  * (op == TK_DELETE)
10960  * target    -> A token holding the quoted name of the table to delete from.
10961  * pWhere    -> The WHERE clause of the DELETE statement if one is specified.
10962  *              Otherwise NULL.
10963  *
10964  * (op == TK_UPDATE)
10965  * target    -> A token holding the quoted name of the table to update rows of.
10966  * pWhere    -> The WHERE clause of the UPDATE statement if one is specified.
10967  *              Otherwise NULL.
10968  * pExprList -> A list of the columns to update and the expressions to update
10969  *              them to. See sqlite3Update() documentation of "pChanges"
10970  *              argument.
10971  *
10972  */
10973 struct TriggerStep {
10974   u8 op;               /* One of TK_DELETE, TK_UPDATE, TK_INSERT, TK_SELECT */
10975   u8 orconf;           /* OE_Rollback etc. */
10976   Trigger *pTrig;      /* The trigger that this step is a part of */
10977   Select *pSelect;     /* SELECT statment or RHS of INSERT INTO .. SELECT ... */
10978   Token target;        /* Target table for DELETE, UPDATE, INSERT */
10979   Expr *pWhere;        /* The WHERE clause for DELETE or UPDATE steps */
10980   ExprList *pExprList; /* SET clause for UPDATE.  VALUES clause for INSERT */
10981   IdList *pIdList;     /* Column names for INSERT */
10982   TriggerStep *pNext;  /* Next in the link-list */
10983   TriggerStep *pLast;  /* Last element in link-list. Valid for 1st elem only */
10984 };
10985 
10986 /*
10987 ** The following structure contains information used by the sqliteFix...
10988 ** routines as they walk the parse tree to make database references
10989 ** explicit.
10990 */
10991 typedef struct DbFixer DbFixer;
10992 struct DbFixer {
10993   Parse *pParse;      /* The parsing context.  Error messages written here */
10994   const char *zDb;    /* Make sure all objects are contained in this database */
10995   const char *zType;  /* Type of the container - used for error messages */
10996   const Token *pName; /* Name of the container - used for error messages */
10997 };
10998 
10999 /*
11000 ** An objected used to accumulate the text of a string where we
11001 ** do not necessarily know how big the string will be in the end.
11002 */
11003 struct StrAccum {
11004   sqlite3 *db;         /* Optional database for lookaside.  Can be NULL */
11005   char *zBase;         /* A base allocation.  Not from malloc. */
11006   char *zText;         /* The string collected so far */
11007   int  nChar;          /* Length of the string so far */
11008   int  nAlloc;         /* Amount of space allocated in zText */
11009   int  mxAlloc;        /* Maximum allowed string length */
11010   u8   mallocFailed;   /* Becomes true if any memory allocation fails */
11011   u8   useMalloc;      /* 0: none,  1: sqlite3DbMalloc,  2: sqlite3_malloc */
11012   u8   tooBig;         /* Becomes true if string size exceeds limits */
11013 };
11014 
11015 /*
11016 ** A pointer to this structure is used to communicate information
11017 ** from sqlite3Init and OP_ParseSchema into the sqlite3InitCallback.
11018 */
11019 typedef struct {
11020   sqlite3 *db;        /* The database being initialized */
11021   int iDb;            /* 0 for main database.  1 for TEMP, 2.. for ATTACHed */
11022   char **pzErrMsg;    /* Error message stored here */
11023   int rc;             /* Result code stored here */
11024 } InitData;
11025 
11026 /*
11027 ** Structure containing global configuration data for the SQLite library.
11028 **
11029 ** This structure also contains some state information.
11030 */
11031 struct Sqlite3Config {
11032   int bMemstat;                     /* True to enable memory status */
11033   int bCoreMutex;                   /* True to enable core mutexing */
11034   int bFullMutex;                   /* True to enable full mutexing */
11035   int bOpenUri;                     /* True to interpret filenames as URIs */
11036   int mxStrlen;                     /* Maximum string length */
11037   int szLookaside;                  /* Default lookaside buffer size */
11038   int nLookaside;                   /* Default lookaside buffer count */
11039   sqlite3_mem_methods m;            /* Low-level memory allocation interface */
11040   sqlite3_mutex_methods mutex;      /* Low-level mutex interface */
11041   sqlite3_pcache_methods pcache;    /* Low-level page-cache interface */
11042   void *pHeap;                      /* Heap storage space */
11043   int nHeap;                        /* Size of pHeap[] */
11044   int mnReq, mxReq;                 /* Min and max heap requests sizes */
11045   void *pScratch;                   /* Scratch memory */
11046   int szScratch;                    /* Size of each scratch buffer */
11047   int nScratch;                     /* Number of scratch buffers */
11048   void *pPage;                      /* Page cache memory */
11049   int szPage;                       /* Size of each page in pPage[] */
11050   int nPage;                        /* Number of pages in pPage[] */
11051   int mxParserStack;                /* maximum depth of the parser stack */
11052   int sharedCacheEnabled;           /* true if shared-cache mode enabled */
11053   /* The above might be initialized to non-zero.  The following need to always
11054   ** initially be zero, however. */
11055   int isInit;                       /* True after initialization has finished */
11056   int inProgress;                   /* True while initialization in progress */
11057   int isMutexInit;                  /* True after mutexes are initialized */
11058   int isMallocInit;                 /* True after malloc is initialized */
11059   int isPCacheInit;                 /* True after malloc is initialized */
11060   sqlite3_mutex *pInitMutex;        /* Mutex used by sqlite3_initialize() */
11061   int nRefInitMutex;                /* Number of users of pInitMutex */
11062   void (*xLog)(void*,int,const char*); /* Function for logging */
11063   void *pLogArg;                       /* First argument to xLog() */
11064   int bLocaltimeFault;              /* True to fail localtime() calls */
11065 };
11066 
11067 /*
11068 ** Context pointer passed down through the tree-walk.
11069 */
11070 struct Walker {
11071   int (*xExprCallback)(Walker*, Expr*);     /* Callback for expressions */
11072   int (*xSelectCallback)(Walker*,Select*);  /* Callback for SELECTs */
11073   Parse *pParse;                            /* Parser context.  */
11074   union {                                   /* Extra data for callback */
11075     NameContext *pNC;                          /* Naming context */
11076     int i;                                     /* Integer value */
11077   } u;
11078 };
11079 
11080 /* Forward declarations */
11081 SQLITE_PRIVATE int sqlite3WalkExpr(Walker*, Expr*);
11082 SQLITE_PRIVATE int sqlite3WalkExprList(Walker*, ExprList*);
11083 SQLITE_PRIVATE int sqlite3WalkSelect(Walker*, Select*);
11084 SQLITE_PRIVATE int sqlite3WalkSelectExpr(Walker*, Select*);
11085 SQLITE_PRIVATE int sqlite3WalkSelectFrom(Walker*, Select*);
11086 
11087 /*
11088 ** Return code from the parse-tree walking primitives and their
11089 ** callbacks.
11090 */
11091 #define WRC_Continue    0   /* Continue down into children */
11092 #define WRC_Prune       1   /* Omit children but continue walking siblings */
11093 #define WRC_Abort       2   /* Abandon the tree walk */
11094 
11095 /*
11096 ** Assuming zIn points to the first byte of a UTF-8 character,
11097 ** advance zIn to point to the first byte of the next UTF-8 character.
11098 */
11099 #define SQLITE_SKIP_UTF8(zIn) {                        \
11100   if( (*(zIn++))>=0xc0 ){                              \
11101     while( (*zIn & 0xc0)==0x80 ){ zIn++; }             \
11102   }                                                    \
11103 }
11104 
11105 /*
11106 ** The SQLITE_*_BKPT macros are substitutes for the error codes with
11107 ** the same name but without the _BKPT suffix.  These macros invoke
11108 ** routines that report the line-number on which the error originated
11109 ** using sqlite3_log().  The routines also provide a convenient place
11110 ** to set a debugger breakpoint.
11111 */
11112 SQLITE_PRIVATE int sqlite3CorruptError(int);
11113 SQLITE_PRIVATE int sqlite3MisuseError(int);
11114 SQLITE_PRIVATE int sqlite3CantopenError(int);
11115 #define SQLITE_CORRUPT_BKPT sqlite3CorruptError(__LINE__)
11116 #define SQLITE_MISUSE_BKPT sqlite3MisuseError(__LINE__)
11117 #define SQLITE_CANTOPEN_BKPT sqlite3CantopenError(__LINE__)
11118 
11119 
11120 /*
11121 ** FTS4 is really an extension for FTS3.  It is enabled using the
11122 ** SQLITE_ENABLE_FTS3 macro.  But to avoid confusion we also all
11123 ** the SQLITE_ENABLE_FTS4 macro to serve as an alisse for SQLITE_ENABLE_FTS3.
11124 */
11125 #if defined(SQLITE_ENABLE_FTS4) && !defined(SQLITE_ENABLE_FTS3)
11126 # define SQLITE_ENABLE_FTS3
11127 #endif
11128 
11129 /*
11130 ** The ctype.h header is needed for non-ASCII systems.  It is also
11131 ** needed by FTS3 when FTS3 is included in the amalgamation.
11132 */
11133 #if !defined(SQLITE_ASCII) || \
11134     (defined(SQLITE_ENABLE_FTS3) && defined(SQLITE_AMALGAMATION))
11135 # include <ctype.h>
11136 #endif
11137 
11138 /*
11139 ** The following macros mimic the standard library functions toupper(),
11140 ** isspace(), isalnum(), isdigit() and isxdigit(), respectively. The
11141 ** sqlite versions only work for ASCII characters, regardless of locale.
11142 */
11143 #ifdef SQLITE_ASCII
11144 # define sqlite3Toupper(x)  ((x)&~(sqlite3CtypeMap[(unsigned char)(x)]&0x20))
11145 # define sqlite3Isspace(x)   (sqlite3CtypeMap[(unsigned char)(x)]&0x01)
11146 # define sqlite3Isalnum(x)   (sqlite3CtypeMap[(unsigned char)(x)]&0x06)
11147 # define sqlite3Isalpha(x)   (sqlite3CtypeMap[(unsigned char)(x)]&0x02)
11148 # define sqlite3Isdigit(x)   (sqlite3CtypeMap[(unsigned char)(x)]&0x04)
11149 # define sqlite3Isxdigit(x)  (sqlite3CtypeMap[(unsigned char)(x)]&0x08)
11150 # define sqlite3Tolower(x)   (sqlite3UpperToLower[(unsigned char)(x)])
11151 #else
11152 # define sqlite3Toupper(x)   toupper((unsigned char)(x))
11153 # define sqlite3Isspace(x)   isspace((unsigned char)(x))
11154 # define sqlite3Isalnum(x)   isalnum((unsigned char)(x))
11155 # define sqlite3Isalpha(x)   isalpha((unsigned char)(x))
11156 # define sqlite3Isdigit(x)   isdigit((unsigned char)(x))
11157 # define sqlite3Isxdigit(x)  isxdigit((unsigned char)(x))
11158 # define sqlite3Tolower(x)   tolower((unsigned char)(x))
11159 #endif
11160 
11161 /*
11162 ** Internal function prototypes
11163 */
11164 SQLITE_PRIVATE int sqlite3StrICmp(const char *, const char *);
11165 SQLITE_PRIVATE int sqlite3Strlen30(const char*);
11166 #define sqlite3StrNICmp sqlite3_strnicmp
11167 
11168 SQLITE_PRIVATE int sqlite3MallocInit(void);
11169 SQLITE_PRIVATE void sqlite3MallocEnd(void);
11170 SQLITE_PRIVATE void *sqlite3Malloc(int);
11171 SQLITE_PRIVATE void *sqlite3MallocZero(int);
11172 SQLITE_PRIVATE void *sqlite3DbMallocZero(sqlite3*, int);
11173 SQLITE_PRIVATE void *sqlite3DbMallocRaw(sqlite3*, int);
11174 SQLITE_PRIVATE char *sqlite3DbStrDup(sqlite3*,const char*);
11175 SQLITE_PRIVATE char *sqlite3DbStrNDup(sqlite3*,const char*, int);
11176 SQLITE_PRIVATE void *sqlite3Realloc(void*, int);
11177 SQLITE_PRIVATE void *sqlite3DbReallocOrFree(sqlite3 *, void *, int);
11178 SQLITE_PRIVATE void *sqlite3DbRealloc(sqlite3 *, void *, int);
11179 SQLITE_PRIVATE void sqlite3DbFree(sqlite3*, void*);
11180 SQLITE_PRIVATE int sqlite3MallocSize(void*);
11181 SQLITE_PRIVATE int sqlite3DbMallocSize(sqlite3*, void*);
11182 SQLITE_PRIVATE void *sqlite3ScratchMalloc(int);
11183 SQLITE_PRIVATE void sqlite3ScratchFree(void*);
11184 SQLITE_PRIVATE void *sqlite3PageMalloc(int);
11185 SQLITE_PRIVATE void sqlite3PageFree(void*);
11186 SQLITE_PRIVATE void sqlite3MemSetDefault(void);
11187 SQLITE_PRIVATE void sqlite3BenignMallocHooks(void (*)(void), void (*)(void));
11188 SQLITE_PRIVATE int sqlite3HeapNearlyFull(void);
11189 
11190 /*
11191 ** On systems with ample stack space and that support alloca(), make
11192 ** use of alloca() to obtain space for large automatic objects.  By default,
11193 ** obtain space from malloc().
11194 **
11195 ** The alloca() routine never returns NULL.  This will cause code paths
11196 ** that deal with sqlite3StackAlloc() failures to be unreachable.
11197 */
11198 #ifdef SQLITE_USE_ALLOCA
11199 # define sqlite3StackAllocRaw(D,N)   alloca(N)
11200 # define sqlite3StackAllocZero(D,N)  memset(alloca(N), 0, N)
11201 # define sqlite3StackFree(D,P)
11202 #else
11203 # define sqlite3StackAllocRaw(D,N)   sqlite3DbMallocRaw(D,N)
11204 # define sqlite3StackAllocZero(D,N)  sqlite3DbMallocZero(D,N)
11205 # define sqlite3StackFree(D,P)       sqlite3DbFree(D,P)
11206 #endif
11207 
11208 #ifdef SQLITE_ENABLE_MEMSYS3
11209 SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys3(void);
11210 #endif
11211 #ifdef SQLITE_ENABLE_MEMSYS5
11212 SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys5(void);
11213 #endif
11214 
11215 
11216 #ifndef SQLITE_MUTEX_OMIT
11217 SQLITE_PRIVATE   sqlite3_mutex_methods const *sqlite3DefaultMutex(void);
11218 SQLITE_PRIVATE   sqlite3_mutex_methods const *sqlite3NoopMutex(void);
11219 SQLITE_PRIVATE   sqlite3_mutex *sqlite3MutexAlloc(int);
11220 SQLITE_PRIVATE   int sqlite3MutexInit(void);
11221 SQLITE_PRIVATE   int sqlite3MutexEnd(void);
11222 #endif
11223 
11224 SQLITE_PRIVATE int sqlite3StatusValue(int);
11225 SQLITE_PRIVATE void sqlite3StatusAdd(int, int);
11226 SQLITE_PRIVATE void sqlite3StatusSet(int, int);
11227 
11228 #ifndef SQLITE_OMIT_FLOATING_POINT
11229 SQLITE_PRIVATE   int sqlite3IsNaN(double);
11230 #else
11231 # define sqlite3IsNaN(X)  0
11232 #endif
11233 
11234 SQLITE_PRIVATE void sqlite3VXPrintf(StrAccum*, int, const char*, va_list);
11235 #ifndef SQLITE_OMIT_TRACE
11236 SQLITE_PRIVATE void sqlite3XPrintf(StrAccum*, const char*, ...);
11237 #endif
11238 SQLITE_PRIVATE char *sqlite3MPrintf(sqlite3*,const char*, ...);
11239 SQLITE_PRIVATE char *sqlite3VMPrintf(sqlite3*,const char*, va_list);
11240 SQLITE_PRIVATE char *sqlite3MAppendf(sqlite3*,char*,const char*,...);
11241 #if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
11242 SQLITE_PRIVATE   void sqlite3DebugPrintf(const char*, ...);
11243 #endif
11244 #if defined(SQLITE_TEST)
11245 SQLITE_PRIVATE   void *sqlite3TestTextToPtr(const char*);
11246 #endif
11247 SQLITE_PRIVATE void sqlite3SetString(char **, sqlite3*, const char*, ...);
11248 SQLITE_PRIVATE void sqlite3ErrorMsg(Parse*, const char*, ...);
11249 SQLITE_PRIVATE int sqlite3Dequote(char*);
11250 SQLITE_PRIVATE int sqlite3KeywordCode(const unsigned char*, int);
11251 SQLITE_PRIVATE int sqlite3RunParser(Parse*, const char*, char **);
11252 SQLITE_PRIVATE void sqlite3FinishCoding(Parse*);
11253 SQLITE_PRIVATE int sqlite3GetTempReg(Parse*);
11254 SQLITE_PRIVATE void sqlite3ReleaseTempReg(Parse*,int);
11255 SQLITE_PRIVATE int sqlite3GetTempRange(Parse*,int);
11256 SQLITE_PRIVATE void sqlite3ReleaseTempRange(Parse*,int,int);
11257 SQLITE_PRIVATE Expr *sqlite3ExprAlloc(sqlite3*,int,const Token*,int);
11258 SQLITE_PRIVATE Expr *sqlite3Expr(sqlite3*,int,const char*);
11259 SQLITE_PRIVATE void sqlite3ExprAttachSubtrees(sqlite3*,Expr*,Expr*,Expr*);
11260 SQLITE_PRIVATE Expr *sqlite3PExpr(Parse*, int, Expr*, Expr*, const Token*);
11261 SQLITE_PRIVATE Expr *sqlite3ExprAnd(sqlite3*,Expr*, Expr*);
11262 SQLITE_PRIVATE Expr *sqlite3ExprFunction(Parse*,ExprList*, Token*);
11263 SQLITE_PRIVATE void sqlite3ExprAssignVarNumber(Parse*, Expr*);
11264 SQLITE_PRIVATE void sqlite3ExprDelete(sqlite3*, Expr*);
11265 SQLITE_PRIVATE ExprList *sqlite3ExprListAppend(Parse*,ExprList*,Expr*);
11266 SQLITE_PRIVATE void sqlite3ExprListSetName(Parse*,ExprList*,Token*,int);
11267 SQLITE_PRIVATE void sqlite3ExprListSetSpan(Parse*,ExprList*,ExprSpan*);
11268 SQLITE_PRIVATE void sqlite3ExprListDelete(sqlite3*, ExprList*);
11269 SQLITE_PRIVATE int sqlite3Init(sqlite3*, char**);
11270 SQLITE_PRIVATE int sqlite3InitCallback(void*, int, char**, char**);
11271 SQLITE_PRIVATE void sqlite3Pragma(Parse*,Token*,Token*,Token*,int);
11272 SQLITE_PRIVATE void sqlite3ResetInternalSchema(sqlite3*, int);
11273 SQLITE_PRIVATE void sqlite3BeginParse(Parse*,int);
11274 SQLITE_PRIVATE void sqlite3CommitInternalChanges(sqlite3*);
11275 SQLITE_PRIVATE Table *sqlite3ResultSetOfSelect(Parse*,Select*);
11276 SQLITE_PRIVATE void sqlite3OpenMasterTable(Parse *, int);
11277 SQLITE_PRIVATE void sqlite3StartTable(Parse*,Token*,Token*,int,int,int,int);
11278 SQLITE_PRIVATE void sqlite3AddColumn(Parse*,Token*);
11279 SQLITE_PRIVATE void sqlite3AddNotNull(Parse*, int);
11280 SQLITE_PRIVATE void sqlite3AddPrimaryKey(Parse*, ExprList*, int, int, int);
11281 SQLITE_PRIVATE void sqlite3AddCheckConstraint(Parse*, Expr*);
11282 SQLITE_PRIVATE void sqlite3AddColumnType(Parse*,Token*);
11283 SQLITE_PRIVATE void sqlite3AddDefaultValue(Parse*,ExprSpan*);
11284 SQLITE_PRIVATE void sqlite3AddCollateType(Parse*, Token*);
11285 SQLITE_PRIVATE void sqlite3EndTable(Parse*,Token*,Token*,Select*);
11286 SQLITE_PRIVATE int sqlite3ParseUri(const char*,const char*,unsigned int*,
11287                     sqlite3_vfs**,char**,char **);
11288 
11289 SQLITE_PRIVATE Bitvec *sqlite3BitvecCreate(u32);
11290 SQLITE_PRIVATE int sqlite3BitvecTest(Bitvec*, u32);
11291 SQLITE_PRIVATE int sqlite3BitvecSet(Bitvec*, u32);
11292 SQLITE_PRIVATE void sqlite3BitvecClear(Bitvec*, u32, void*);
11293 SQLITE_PRIVATE void sqlite3BitvecDestroy(Bitvec*);
11294 SQLITE_PRIVATE u32 sqlite3BitvecSize(Bitvec*);
11295 SQLITE_PRIVATE int sqlite3BitvecBuiltinTest(int,int*);
11296 
11297 SQLITE_PRIVATE RowSet *sqlite3RowSetInit(sqlite3*, void*, unsigned int);
11298 SQLITE_PRIVATE void sqlite3RowSetClear(RowSet*);
11299 SQLITE_PRIVATE void sqlite3RowSetInsert(RowSet*, i64);
11300 SQLITE_PRIVATE int sqlite3RowSetTest(RowSet*, u8 iBatch, i64);
11301 SQLITE_PRIVATE int sqlite3RowSetNext(RowSet*, i64*);
11302 
11303 SQLITE_PRIVATE void sqlite3CreateView(Parse*,Token*,Token*,Token*,Select*,int,int);
11304 
11305 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
11306 SQLITE_PRIVATE   int sqlite3ViewGetColumnNames(Parse*,Table*);
11307 #else
11308 # define sqlite3ViewGetColumnNames(A,B) 0
11309 #endif
11310 
11311 SQLITE_PRIVATE void sqlite3DropTable(Parse*, SrcList*, int, int);
11312 SQLITE_PRIVATE void sqlite3DeleteTable(sqlite3*, Table*);
11313 #ifndef SQLITE_OMIT_AUTOINCREMENT
11314 SQLITE_PRIVATE   void sqlite3AutoincrementBegin(Parse *pParse);
11315 SQLITE_PRIVATE   void sqlite3AutoincrementEnd(Parse *pParse);
11316 #else
11317 # define sqlite3AutoincrementBegin(X)
11318 # define sqlite3AutoincrementEnd(X)
11319 #endif
11320 SQLITE_PRIVATE void sqlite3Insert(Parse*, SrcList*, ExprList*, Select*, IdList*, int);
11321 SQLITE_PRIVATE void *sqlite3ArrayAllocate(sqlite3*,void*,int,int,int*,int*,int*);
11322 SQLITE_PRIVATE IdList *sqlite3IdListAppend(sqlite3*, IdList*, Token*);
11323 SQLITE_PRIVATE int sqlite3IdListIndex(IdList*,const char*);
11324 SQLITE_PRIVATE SrcList *sqlite3SrcListEnlarge(sqlite3*, SrcList*, int, int);
11325 SQLITE_PRIVATE SrcList *sqlite3SrcListAppend(sqlite3*, SrcList*, Token*, Token*);
11326 SQLITE_PRIVATE SrcList *sqlite3SrcListAppendFromTerm(Parse*, SrcList*, Token*, Token*,
11327                                       Token*, Select*, Expr*, IdList*);
11328 SQLITE_PRIVATE void sqlite3SrcListIndexedBy(Parse *, SrcList *, Token *);
11329 SQLITE_PRIVATE int sqlite3IndexedByLookup(Parse *, struct SrcList_item *);
11330 SQLITE_PRIVATE void sqlite3SrcListShiftJoinType(SrcList*);
11331 SQLITE_PRIVATE void sqlite3SrcListAssignCursors(Parse*, SrcList*);
11332 SQLITE_PRIVATE void sqlite3IdListDelete(sqlite3*, IdList*);
11333 SQLITE_PRIVATE void sqlite3SrcListDelete(sqlite3*, SrcList*);
11334 SQLITE_PRIVATE Index *sqlite3CreateIndex(Parse*,Token*,Token*,SrcList*,ExprList*,int,Token*,
11335                         Token*, int, int);
11336 SQLITE_PRIVATE void sqlite3DropIndex(Parse*, SrcList*, int);
11337 SQLITE_PRIVATE int sqlite3Select(Parse*, Select*, SelectDest*);
11338 SQLITE_PRIVATE Select *sqlite3SelectNew(Parse*,ExprList*,SrcList*,Expr*,ExprList*,
11339                          Expr*,ExprList*,int,Expr*,Expr*);
11340 SQLITE_PRIVATE void sqlite3SelectDelete(sqlite3*, Select*);
11341 SQLITE_PRIVATE Table *sqlite3SrcListLookup(Parse*, SrcList*);
11342 SQLITE_PRIVATE int sqlite3IsReadOnly(Parse*, Table*, int);
11343 SQLITE_PRIVATE void sqlite3OpenTable(Parse*, int iCur, int iDb, Table*, int);
11344 #if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
11345 SQLITE_PRIVATE Expr *sqlite3LimitWhere(Parse *, SrcList *, Expr *, ExprList *, Expr *, Expr *, char *);
11346 #endif
11347 SQLITE_PRIVATE void sqlite3DeleteFrom(Parse*, SrcList*, Expr*);
11348 SQLITE_PRIVATE void sqlite3Update(Parse*, SrcList*, ExprList*, Expr*, int);
11349 SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(Parse*, SrcList*, Expr*, ExprList**, u16);
11350 SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo*);
11351 SQLITE_PRIVATE int sqlite3ExprCodeGetColumn(Parse*, Table*, int, int, int);
11352 SQLITE_PRIVATE void sqlite3ExprCodeGetColumnOfTable(Vdbe*, Table*, int, int, int);
11353 SQLITE_PRIVATE void sqlite3ExprCodeMove(Parse*, int, int, int);
11354 SQLITE_PRIVATE void sqlite3ExprCodeCopy(Parse*, int, int, int);
11355 SQLITE_PRIVATE void sqlite3ExprCacheStore(Parse*, int, int, int);
11356 SQLITE_PRIVATE void sqlite3ExprCachePush(Parse*);
11357 SQLITE_PRIVATE void sqlite3ExprCachePop(Parse*, int);
11358 SQLITE_PRIVATE void sqlite3ExprCacheRemove(Parse*, int, int);
11359 SQLITE_PRIVATE void sqlite3ExprCacheClear(Parse*);
11360 SQLITE_PRIVATE void sqlite3ExprCacheAffinityChange(Parse*, int, int);
11361 SQLITE_PRIVATE int sqlite3ExprCode(Parse*, Expr*, int);
11362 SQLITE_PRIVATE int sqlite3ExprCodeTemp(Parse*, Expr*, int*);
11363 SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse*, Expr*, int);
11364 SQLITE_PRIVATE int sqlite3ExprCodeAndCache(Parse*, Expr*, int);
11365 SQLITE_PRIVATE void sqlite3ExprCodeConstants(Parse*, Expr*);
11366 SQLITE_PRIVATE int sqlite3ExprCodeExprList(Parse*, ExprList*, int, int);
11367 SQLITE_PRIVATE void sqlite3ExprIfTrue(Parse*, Expr*, int, int);
11368 SQLITE_PRIVATE void sqlite3ExprIfFalse(Parse*, Expr*, int, int);
11369 SQLITE_PRIVATE Table *sqlite3FindTable(sqlite3*,const char*, const char*);
11370 SQLITE_PRIVATE Table *sqlite3LocateTable(Parse*,int isView,const char*, const char*);
11371 SQLITE_PRIVATE Index *sqlite3FindIndex(sqlite3*,const char*, const char*);
11372 SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTable(sqlite3*,int,const char*);
11373 SQLITE_PRIVATE void sqlite3UnlinkAndDeleteIndex(sqlite3*,int,const char*);
11374 SQLITE_PRIVATE void sqlite3Vacuum(Parse*);
11375 SQLITE_PRIVATE int sqlite3RunVacuum(char**, sqlite3*);
11376 SQLITE_PRIVATE char *sqlite3NameFromToken(sqlite3*, Token*);
11377 SQLITE_PRIVATE int sqlite3ExprCompare(Expr*, Expr*);
11378 SQLITE_PRIVATE int sqlite3ExprListCompare(ExprList*, ExprList*);
11379 SQLITE_PRIVATE void sqlite3ExprAnalyzeAggregates(NameContext*, Expr*);
11380 SQLITE_PRIVATE void sqlite3ExprAnalyzeAggList(NameContext*,ExprList*);
11381 SQLITE_PRIVATE Vdbe *sqlite3GetVdbe(Parse*);
11382 SQLITE_PRIVATE void sqlite3PrngSaveState(void);
11383 SQLITE_PRIVATE void sqlite3PrngRestoreState(void);
11384 SQLITE_PRIVATE void sqlite3PrngResetState(void);
11385 SQLITE_PRIVATE void sqlite3RollbackAll(sqlite3*);
11386 SQLITE_PRIVATE void sqlite3CodeVerifySchema(Parse*, int);
11387 SQLITE_PRIVATE void sqlite3CodeVerifyNamedSchema(Parse*, const char *zDb);
11388 SQLITE_PRIVATE void sqlite3BeginTransaction(Parse*, int);
11389 SQLITE_PRIVATE void sqlite3CommitTransaction(Parse*);
11390 SQLITE_PRIVATE void sqlite3RollbackTransaction(Parse*);
11391 SQLITE_PRIVATE void sqlite3Savepoint(Parse*, int, Token*);
11392 SQLITE_PRIVATE void sqlite3CloseSavepoints(sqlite3 *);
11393 SQLITE_PRIVATE int sqlite3ExprIsConstant(Expr*);
11394 SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr*);
11395 SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr*);
11396 SQLITE_PRIVATE int sqlite3ExprIsInteger(Expr*, int*);
11397 SQLITE_PRIVATE int sqlite3ExprCanBeNull(const Expr*);
11398 SQLITE_PRIVATE void sqlite3ExprCodeIsNullJump(Vdbe*, const Expr*, int, int);
11399 SQLITE_PRIVATE int sqlite3ExprNeedsNoAffinityChange(const Expr*, char);
11400 SQLITE_PRIVATE int sqlite3IsRowid(const char*);
11401 SQLITE_PRIVATE void sqlite3GenerateRowDelete(Parse*, Table*, int, int, int, Trigger *, int);
11402 SQLITE_PRIVATE void sqlite3GenerateRowIndexDelete(Parse*, Table*, int, int*);
11403 SQLITE_PRIVATE int sqlite3GenerateIndexKey(Parse*, Index*, int, int, int);
11404 SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(Parse*,Table*,int,int,
11405                                      int*,int,int,int,int,int*);
11406 SQLITE_PRIVATE void sqlite3CompleteInsertion(Parse*, Table*, int, int, int*, int, int, int);
11407 SQLITE_PRIVATE int sqlite3OpenTableAndIndices(Parse*, Table*, int, int);
11408 SQLITE_PRIVATE void sqlite3BeginWriteOperation(Parse*, int, int);
11409 SQLITE_PRIVATE void sqlite3MultiWrite(Parse*);
11410 SQLITE_PRIVATE void sqlite3MayAbort(Parse*);
11411 SQLITE_PRIVATE void sqlite3HaltConstraint(Parse*, int, char*, int);
11412 SQLITE_PRIVATE Expr *sqlite3ExprDup(sqlite3*,Expr*,int);
11413 SQLITE_PRIVATE ExprList *sqlite3ExprListDup(sqlite3*,ExprList*,int);
11414 SQLITE_PRIVATE SrcList *sqlite3SrcListDup(sqlite3*,SrcList*,int);
11415 SQLITE_PRIVATE IdList *sqlite3IdListDup(sqlite3*,IdList*);
11416 SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3*,Select*,int);
11417 SQLITE_PRIVATE void sqlite3FuncDefInsert(FuncDefHash*, FuncDef*);
11418 SQLITE_PRIVATE FuncDef *sqlite3FindFunction(sqlite3*,const char*,int,int,u8,int);
11419 SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(sqlite3*);
11420 SQLITE_PRIVATE void sqlite3RegisterDateTimeFunctions(void);
11421 SQLITE_PRIVATE void sqlite3RegisterGlobalFunctions(void);
11422 SQLITE_PRIVATE int sqlite3SafetyCheckOk(sqlite3*);
11423 SQLITE_PRIVATE int sqlite3SafetyCheckSickOrOk(sqlite3*);
11424 SQLITE_PRIVATE void sqlite3ChangeCookie(Parse*, int);
11425 
11426 #if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
11427 SQLITE_PRIVATE void sqlite3MaterializeView(Parse*, Table*, Expr*, int);
11428 #endif
11429 
11430 #ifndef SQLITE_OMIT_TRIGGER
11431 SQLITE_PRIVATE   void sqlite3BeginTrigger(Parse*, Token*,Token*,int,int,IdList*,SrcList*,
11432                            Expr*,int, int);
11433 SQLITE_PRIVATE   void sqlite3FinishTrigger(Parse*, TriggerStep*, Token*);
11434 SQLITE_PRIVATE   void sqlite3DropTrigger(Parse*, SrcList*, int);
11435 SQLITE_PRIVATE   void sqlite3DropTriggerPtr(Parse*, Trigger*);
11436 SQLITE_PRIVATE   Trigger *sqlite3TriggersExist(Parse *, Table*, int, ExprList*, int *pMask);
11437 SQLITE_PRIVATE   Trigger *sqlite3TriggerList(Parse *, Table *);
11438 SQLITE_PRIVATE   void sqlite3CodeRowTrigger(Parse*, Trigger *, int, ExprList*, int, Table *,
11439                             int, int, int);
11440 SQLITE_PRIVATE   void sqlite3CodeRowTriggerDirect(Parse *, Trigger *, Table *, int, int, int);
11441   void sqliteViewTriggers(Parse*, Table*, Expr*, int, ExprList*);
11442 SQLITE_PRIVATE   void sqlite3DeleteTriggerStep(sqlite3*, TriggerStep*);
11443 SQLITE_PRIVATE   TriggerStep *sqlite3TriggerSelectStep(sqlite3*,Select*);
11444 SQLITE_PRIVATE   TriggerStep *sqlite3TriggerInsertStep(sqlite3*,Token*, IdList*,
11445                                         ExprList*,Select*,u8);
11446 SQLITE_PRIVATE   TriggerStep *sqlite3TriggerUpdateStep(sqlite3*,Token*,ExprList*, Expr*, u8);
11447 SQLITE_PRIVATE   TriggerStep *sqlite3TriggerDeleteStep(sqlite3*,Token*, Expr*);
11448 SQLITE_PRIVATE   void sqlite3DeleteTrigger(sqlite3*, Trigger*);
11449 SQLITE_PRIVATE   void sqlite3UnlinkAndDeleteTrigger(sqlite3*,int,const char*);
11450 SQLITE_PRIVATE   u32 sqlite3TriggerColmask(Parse*,Trigger*,ExprList*,int,int,Table*,int);
11451 # define sqlite3ParseToplevel(p) ((p)->pToplevel ? (p)->pToplevel : (p))
11452 #else
11453 # define sqlite3TriggersExist(B,C,D,E,F) 0
11454 # define sqlite3DeleteTrigger(A,B)
11455 # define sqlite3DropTriggerPtr(A,B)
11456 # define sqlite3UnlinkAndDeleteTrigger(A,B,C)
11457 # define sqlite3CodeRowTrigger(A,B,C,D,E,F,G,H,I)
11458 # define sqlite3CodeRowTriggerDirect(A,B,C,D,E,F)
11459 # define sqlite3TriggerList(X, Y) 0
11460 # define sqlite3ParseToplevel(p) p
11461 # define sqlite3TriggerColmask(A,B,C,D,E,F,G) 0
11462 #endif
11463 
11464 SQLITE_PRIVATE int sqlite3JoinType(Parse*, Token*, Token*, Token*);
11465 SQLITE_PRIVATE void sqlite3CreateForeignKey(Parse*, ExprList*, Token*, ExprList*, int);
11466 SQLITE_PRIVATE void sqlite3DeferForeignKey(Parse*, int);
11467 #ifndef SQLITE_OMIT_AUTHORIZATION
11468 SQLITE_PRIVATE   void sqlite3AuthRead(Parse*,Expr*,Schema*,SrcList*);
11469 SQLITE_PRIVATE   int sqlite3AuthCheck(Parse*,int, const char*, const char*, const char*);
11470 SQLITE_PRIVATE   void sqlite3AuthContextPush(Parse*, AuthContext*, const char*);
11471 SQLITE_PRIVATE   void sqlite3AuthContextPop(AuthContext*);
11472 SQLITE_PRIVATE   int sqlite3AuthReadCol(Parse*, const char *, const char *, int);
11473 #else
11474 # define sqlite3AuthRead(a,b,c,d)
11475 # define sqlite3AuthCheck(a,b,c,d,e)    SQLITE_OK
11476 # define sqlite3AuthContextPush(a,b,c)
11477 # define sqlite3AuthContextPop(a)  ((void)(a))
11478 #endif
11479 SQLITE_PRIVATE void sqlite3Attach(Parse*, Expr*, Expr*, Expr*);
11480 SQLITE_PRIVATE void sqlite3Detach(Parse*, Expr*);
11481 SQLITE_PRIVATE int sqlite3FixInit(DbFixer*, Parse*, int, const char*, const Token*);
11482 SQLITE_PRIVATE int sqlite3FixSrcList(DbFixer*, SrcList*);
11483 SQLITE_PRIVATE int sqlite3FixSelect(DbFixer*, Select*);
11484 SQLITE_PRIVATE int sqlite3FixExpr(DbFixer*, Expr*);
11485 SQLITE_PRIVATE int sqlite3FixExprList(DbFixer*, ExprList*);
11486 SQLITE_PRIVATE int sqlite3FixTriggerStep(DbFixer*, TriggerStep*);
11487 SQLITE_PRIVATE int sqlite3AtoF(const char *z, double*, int, u8);
11488 SQLITE_PRIVATE int sqlite3GetInt32(const char *, int*);
11489 SQLITE_PRIVATE int sqlite3Atoi(const char*);
11490 SQLITE_PRIVATE int sqlite3Utf16ByteLen(const void *pData, int nChar);
11491 SQLITE_PRIVATE int sqlite3Utf8CharLen(const char *pData, int nByte);
11492 SQLITE_PRIVATE u32 sqlite3Utf8Read(const u8*, const u8**);
11493 
11494 /*
11495 ** Routines to read and write variable-length integers.  These used to
11496 ** be defined locally, but now we use the varint routines in the util.c
11497 ** file.  Code should use the MACRO forms below, as the Varint32 versions
11498 ** are coded to assume the single byte case is already handled (which
11499 ** the MACRO form does).
11500 */
11501 SQLITE_PRIVATE int sqlite3PutVarint(unsigned char*, u64);
11502 SQLITE_PRIVATE int sqlite3PutVarint32(unsigned char*, u32);
11503 SQLITE_PRIVATE u8 sqlite3GetVarint(const unsigned char *, u64 *);
11504 SQLITE_PRIVATE u8 sqlite3GetVarint32(const unsigned char *, u32 *);
11505 SQLITE_PRIVATE int sqlite3VarintLen(u64 v);
11506 
11507 /*
11508 ** The header of a record consists of a sequence variable-length integers.
11509 ** These integers are almost always small and are encoded as a single byte.
11510 ** The following macros take advantage this fact to provide a fast encode
11511 ** and decode of the integers in a record header.  It is faster for the common
11512 ** case where the integer is a single byte.  It is a little slower when the
11513 ** integer is two or more bytes.  But overall it is faster.
11514 **
11515 ** The following expressions are equivalent:
11516 **
11517 **     x = sqlite3GetVarint32( A, &B );
11518 **     x = sqlite3PutVarint32( A, B );
11519 **
11520 **     x = getVarint32( A, B );
11521 **     x = putVarint32( A, B );
11522 **
11523 */
11524 #define getVarint32(A,B)  (u8)((*(A)<(u8)0x80) ? ((B) = (u32)*(A)),1 : sqlite3GetVarint32((A), (u32 *)&(B)))
11525 #define putVarint32(A,B)  (u8)(((u32)(B)<(u32)0x80) ? (*(A) = (unsigned char)(B)),1 : sqlite3PutVarint32((A), (B)))
11526 #define getVarint    sqlite3GetVarint
11527 #define putVarint    sqlite3PutVarint
11528 
11529 
11530 SQLITE_PRIVATE const char *sqlite3IndexAffinityStr(Vdbe *, Index *);
11531 SQLITE_PRIVATE void sqlite3TableAffinityStr(Vdbe *, Table *);
11532 SQLITE_PRIVATE char sqlite3CompareAffinity(Expr *pExpr, char aff2);
11533 SQLITE_PRIVATE int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity);
11534 SQLITE_PRIVATE char sqlite3ExprAffinity(Expr *pExpr);
11535 SQLITE_PRIVATE int sqlite3Atoi64(const char*, i64*, int, u8);
11536 SQLITE_PRIVATE void sqlite3Error(sqlite3*, int, const char*,...);
11537 SQLITE_PRIVATE void *sqlite3HexToBlob(sqlite3*, const char *z, int n);
11538 SQLITE_PRIVATE u8 sqlite3HexToInt(int h);
11539 SQLITE_PRIVATE int sqlite3TwoPartName(Parse *, Token *, Token *, Token **);
11540 SQLITE_PRIVATE const char *sqlite3ErrStr(int);
11541 SQLITE_PRIVATE int sqlite3ReadSchema(Parse *pParse);
11542 SQLITE_PRIVATE CollSeq *sqlite3FindCollSeq(sqlite3*,u8 enc, const char*,int);
11543 SQLITE_PRIVATE CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char*zName);
11544 SQLITE_PRIVATE CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr);
11545 SQLITE_PRIVATE Expr *sqlite3ExprSetColl(Expr*, CollSeq*);
11546 SQLITE_PRIVATE Expr *sqlite3ExprSetCollByToken(Parse *pParse, Expr*, Token*);
11547 SQLITE_PRIVATE int sqlite3CheckCollSeq(Parse *, CollSeq *);
11548 SQLITE_PRIVATE int sqlite3CheckObjectName(Parse *, const char *);
11549 SQLITE_PRIVATE void sqlite3VdbeSetChanges(sqlite3 *, int);
11550 SQLITE_PRIVATE int sqlite3AddInt64(i64*,i64);
11551 SQLITE_PRIVATE int sqlite3SubInt64(i64*,i64);
11552 SQLITE_PRIVATE int sqlite3MulInt64(i64*,i64);
11553 SQLITE_PRIVATE int sqlite3AbsInt32(int);
11554 #ifdef SQLITE_ENABLE_8_3_NAMES
11555 SQLITE_PRIVATE void sqlite3FileSuffix3(const char*, char*);
11556 #else
11557 # define sqlite3FileSuffix3(X,Y)
11558 #endif
11559 SQLITE_PRIVATE u8 sqlite3GetBoolean(const char *z);
11560 
11561 SQLITE_PRIVATE const void *sqlite3ValueText(sqlite3_value*, u8);
11562 SQLITE_PRIVATE int sqlite3ValueBytes(sqlite3_value*, u8);
11563 SQLITE_PRIVATE void sqlite3ValueSetStr(sqlite3_value*, int, const void *,u8,
11564                         void(*)(void*));
11565 SQLITE_PRIVATE void sqlite3ValueFree(sqlite3_value*);
11566 SQLITE_PRIVATE sqlite3_value *sqlite3ValueNew(sqlite3 *);
11567 SQLITE_PRIVATE char *sqlite3Utf16to8(sqlite3 *, const void*, int, u8);
11568 #ifdef SQLITE_ENABLE_STAT2
11569 SQLITE_PRIVATE char *sqlite3Utf8to16(sqlite3 *, u8, char *, int, int *);
11570 #endif
11571 SQLITE_PRIVATE int sqlite3ValueFromExpr(sqlite3 *, Expr *, u8, u8, sqlite3_value **);
11572 SQLITE_PRIVATE void sqlite3ValueApplyAffinity(sqlite3_value *, u8, u8);
11573 #ifndef SQLITE_AMALGAMATION
11574 SQLITE_PRIVATE const unsigned char sqlite3OpcodeProperty[];
11575 SQLITE_PRIVATE const unsigned char sqlite3UpperToLower[];
11576 SQLITE_PRIVATE const unsigned char sqlite3CtypeMap[];
11577 SQLITE_PRIVATE const Token sqlite3IntTokens[];
11578 SQLITE_PRIVATE SQLITE_WSD struct Sqlite3Config sqlite3Config;
11579 SQLITE_PRIVATE SQLITE_WSD FuncDefHash sqlite3GlobalFunctions;
11580 #ifndef SQLITE_OMIT_WSD
11581 SQLITE_PRIVATE int sqlite3PendingByte;
11582 #endif
11583 #endif
11584 SQLITE_PRIVATE void sqlite3RootPageMoved(sqlite3*, int, int, int);
11585 SQLITE_PRIVATE void sqlite3Reindex(Parse*, Token*, Token*);
11586 SQLITE_PRIVATE void sqlite3AlterFunctions(void);
11587 SQLITE_PRIVATE void sqlite3AlterRenameTable(Parse*, SrcList*, Token*);
11588 SQLITE_PRIVATE int sqlite3GetToken(const unsigned char *, int *);
11589 SQLITE_PRIVATE void sqlite3NestedParse(Parse*, const char*, ...);
11590 SQLITE_PRIVATE void sqlite3ExpirePreparedStatements(sqlite3*);
11591 SQLITE_PRIVATE int sqlite3CodeSubselect(Parse *, Expr *, int, int);
11592 SQLITE_PRIVATE void sqlite3SelectPrep(Parse*, Select*, NameContext*);
11593 SQLITE_PRIVATE int sqlite3ResolveExprNames(NameContext*, Expr*);
11594 SQLITE_PRIVATE void sqlite3ResolveSelectNames(Parse*, Select*, NameContext*);
11595 SQLITE_PRIVATE int sqlite3ResolveOrderGroupBy(Parse*, Select*, ExprList*, const char*);
11596 SQLITE_PRIVATE void sqlite3ColumnDefault(Vdbe *, Table *, int, int);
11597 SQLITE_PRIVATE void sqlite3AlterFinishAddColumn(Parse *, Token *);
11598 SQLITE_PRIVATE void sqlite3AlterBeginAddColumn(Parse *, SrcList *);
11599 SQLITE_PRIVATE CollSeq *sqlite3GetCollSeq(sqlite3*, u8, CollSeq *, const char*);
11600 SQLITE_PRIVATE char sqlite3AffinityType(const char*);
11601 SQLITE_PRIVATE void sqlite3Analyze(Parse*, Token*, Token*);
11602 SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler*);
11603 SQLITE_PRIVATE int sqlite3FindDb(sqlite3*, Token*);
11604 SQLITE_PRIVATE int sqlite3FindDbName(sqlite3 *, const char *);
11605 SQLITE_PRIVATE int sqlite3AnalysisLoad(sqlite3*,int iDB);
11606 SQLITE_PRIVATE void sqlite3DeleteIndexSamples(sqlite3*,Index*);
11607 SQLITE_PRIVATE void sqlite3DefaultRowEst(Index*);
11608 SQLITE_PRIVATE void sqlite3RegisterLikeFunctions(sqlite3*, int);
11609 SQLITE_PRIVATE int sqlite3IsLikeFunction(sqlite3*,Expr*,int*,char*);
11610 SQLITE_PRIVATE void sqlite3MinimumFileFormat(Parse*, int, int);
11611 SQLITE_PRIVATE void sqlite3SchemaClear(void *);
11612 SQLITE_PRIVATE Schema *sqlite3SchemaGet(sqlite3 *, Btree *);
11613 SQLITE_PRIVATE int sqlite3SchemaToIndex(sqlite3 *db, Schema *);
11614 SQLITE_PRIVATE KeyInfo *sqlite3IndexKeyinfo(Parse *, Index *);
11615 SQLITE_PRIVATE int sqlite3CreateFunc(sqlite3 *, const char *, int, int, void *,
11616   void (*)(sqlite3_context*,int,sqlite3_value **),
11617   void (*)(sqlite3_context*,int,sqlite3_value **), void (*)(sqlite3_context*),
11618   FuncDestructor *pDestructor
11619 );
11620 SQLITE_PRIVATE int sqlite3ApiExit(sqlite3 *db, int);
11621 SQLITE_PRIVATE int sqlite3OpenTempDatabase(Parse *);
11622 
11623 SQLITE_PRIVATE void sqlite3StrAccumInit(StrAccum*, char*, int, int);
11624 SQLITE_PRIVATE void sqlite3StrAccumAppend(StrAccum*,const char*,int);
11625 SQLITE_PRIVATE char *sqlite3StrAccumFinish(StrAccum*);
11626 SQLITE_PRIVATE void sqlite3StrAccumReset(StrAccum*);
11627 SQLITE_PRIVATE void sqlite3SelectDestInit(SelectDest*,int,int);
11628 SQLITE_PRIVATE Expr *sqlite3CreateColumnExpr(sqlite3 *, SrcList *, int, int);
11629 
11630 SQLITE_PRIVATE void sqlite3BackupRestart(sqlite3_backup *);
11631 SQLITE_PRIVATE void sqlite3BackupUpdate(sqlite3_backup *, Pgno, const u8 *);
11632 
11633 /*
11634 ** The interface to the LEMON-generated parser
11635 */
11636 SQLITE_PRIVATE void *sqlite3ParserAlloc(void*(*)(size_t));
11637 SQLITE_PRIVATE void sqlite3ParserFree(void*, void(*)(void*));
11638 SQLITE_PRIVATE void sqlite3Parser(void*, int, Token, Parse*);
11639 #ifdef YYTRACKMAXSTACKDEPTH
11640 SQLITE_PRIVATE   int sqlite3ParserStackPeak(void*);
11641 #endif
11642 
11643 SQLITE_PRIVATE void sqlite3AutoLoadExtensions(sqlite3*);
11644 #ifndef SQLITE_OMIT_LOAD_EXTENSION
11645 SQLITE_PRIVATE   void sqlite3CloseExtensions(sqlite3*);
11646 #else
11647 # define sqlite3CloseExtensions(X)
11648 #endif
11649 
11650 #ifndef SQLITE_OMIT_SHARED_CACHE
11651 SQLITE_PRIVATE   void sqlite3TableLock(Parse *, int, int, u8, const char *);
11652 #else
11653   #define sqlite3TableLock(v,w,x,y,z)
11654 #endif
11655 
11656 #ifdef SQLITE_TEST
11657 SQLITE_PRIVATE   int sqlite3Utf8To8(unsigned char*);
11658 #endif
11659 
11660 #ifdef SQLITE_OMIT_VIRTUALTABLE
11661 #  define sqlite3VtabClear(Y)
11662 #  define sqlite3VtabSync(X,Y) SQLITE_OK
11663 #  define sqlite3VtabRollback(X)
11664 #  define sqlite3VtabCommit(X)
11665 #  define sqlite3VtabInSync(db) 0
11666 #  define sqlite3VtabLock(X)
11667 #  define sqlite3VtabUnlock(X)
11668 #  define sqlite3VtabUnlockList(X)
11669 #  define sqlite3VtabSavepoint(X, Y, Z) SQLITE_OK
11670 #else
11671 SQLITE_PRIVATE    void sqlite3VtabClear(sqlite3 *db, Table*);
11672 SQLITE_PRIVATE    int sqlite3VtabSync(sqlite3 *db, char **);
11673 SQLITE_PRIVATE    int sqlite3VtabRollback(sqlite3 *db);
11674 SQLITE_PRIVATE    int sqlite3VtabCommit(sqlite3 *db);
11675 SQLITE_PRIVATE    void sqlite3VtabLock(VTable *);
11676 SQLITE_PRIVATE    void sqlite3VtabUnlock(VTable *);
11677 SQLITE_PRIVATE    void sqlite3VtabUnlockList(sqlite3*);
11678 SQLITE_PRIVATE    int sqlite3VtabSavepoint(sqlite3 *, int, int);
11679 #  define sqlite3VtabInSync(db) ((db)->nVTrans>0 && (db)->aVTrans==0)
11680 #endif
11681 SQLITE_PRIVATE void sqlite3VtabMakeWritable(Parse*,Table*);
11682 SQLITE_PRIVATE void sqlite3VtabBeginParse(Parse*, Token*, Token*, Token*);
11683 SQLITE_PRIVATE void sqlite3VtabFinishParse(Parse*, Token*);
11684 SQLITE_PRIVATE void sqlite3VtabArgInit(Parse*);
11685 SQLITE_PRIVATE void sqlite3VtabArgExtend(Parse*, Token*);
11686 SQLITE_PRIVATE int sqlite3VtabCallCreate(sqlite3*, int, const char *, char **);
11687 SQLITE_PRIVATE int sqlite3VtabCallConnect(Parse*, Table*);
11688 SQLITE_PRIVATE int sqlite3VtabCallDestroy(sqlite3*, int, const char *);
11689 SQLITE_PRIVATE int sqlite3VtabBegin(sqlite3 *, VTable *);
11690 SQLITE_PRIVATE FuncDef *sqlite3VtabOverloadFunction(sqlite3 *,FuncDef*, int nArg, Expr*);
11691 SQLITE_PRIVATE void sqlite3InvalidFunction(sqlite3_context*,int,sqlite3_value**);
11692 SQLITE_PRIVATE int sqlite3VdbeParameterIndex(Vdbe*, const char*, int);
11693 SQLITE_PRIVATE int sqlite3TransferBindings(sqlite3_stmt *, sqlite3_stmt *);
11694 SQLITE_PRIVATE int sqlite3Reprepare(Vdbe*);
11695 SQLITE_PRIVATE void sqlite3ExprListCheckLength(Parse*, ExprList*, const char*);
11696 SQLITE_PRIVATE CollSeq *sqlite3BinaryCompareCollSeq(Parse *, Expr *, Expr *);
11697 SQLITE_PRIVATE int sqlite3TempInMemory(const sqlite3*);
11698 SQLITE_PRIVATE VTable *sqlite3GetVTable(sqlite3*, Table*);
11699 SQLITE_PRIVATE const char *sqlite3JournalModename(int);
11700 SQLITE_PRIVATE int sqlite3Checkpoint(sqlite3*, int, int, int*, int*);
11701 SQLITE_PRIVATE int sqlite3WalDefaultHook(void*,sqlite3*,const char*,int);
11702 
11703 /* Declarations for functions in fkey.c. All of these are replaced by
11704 ** no-op macros if OMIT_FOREIGN_KEY is defined. In this case no foreign
11705 ** key functionality is available. If OMIT_TRIGGER is defined but
11706 ** OMIT_FOREIGN_KEY is not, only some of the functions are no-oped. In
11707 ** this case foreign keys are parsed, but no other functionality is
11708 ** provided (enforcement of FK constraints requires the triggers sub-system).
11709 */
11710 #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
11711 SQLITE_PRIVATE   void sqlite3FkCheck(Parse*, Table*, int, int);
11712 SQLITE_PRIVATE   void sqlite3FkDropTable(Parse*, SrcList *, Table*);
11713 SQLITE_PRIVATE   void sqlite3FkActions(Parse*, Table*, ExprList*, int);
11714 SQLITE_PRIVATE   int sqlite3FkRequired(Parse*, Table*, int*, int);
11715 SQLITE_PRIVATE   u32 sqlite3FkOldmask(Parse*, Table*);
11716 SQLITE_PRIVATE   FKey *sqlite3FkReferences(Table *);
11717 #else
11718   #define sqlite3FkActions(a,b,c,d)
11719   #define sqlite3FkCheck(a,b,c,d)
11720   #define sqlite3FkDropTable(a,b,c)
11721   #define sqlite3FkOldmask(a,b)      0
11722   #define sqlite3FkRequired(a,b,c,d) 0
11723 #endif
11724 #ifndef SQLITE_OMIT_FOREIGN_KEY
11725 SQLITE_PRIVATE   void sqlite3FkDelete(sqlite3 *, Table*);
11726 #else
11727   #define sqlite3FkDelete(a,b)
11728 #endif
11729 
11730 
11731 /*
11732 ** Available fault injectors.  Should be numbered beginning with 0.
11733 */
11734 #define SQLITE_FAULTINJECTOR_MALLOC     0
11735 #define SQLITE_FAULTINJECTOR_COUNT      1
11736 
11737 /*
11738 ** The interface to the code in fault.c used for identifying "benign"
11739 ** malloc failures. This is only present if SQLITE_OMIT_BUILTIN_TEST
11740 ** is not defined.
11741 */
11742 #ifndef SQLITE_OMIT_BUILTIN_TEST
11743 SQLITE_PRIVATE   void sqlite3BeginBenignMalloc(void);
11744 SQLITE_PRIVATE   void sqlite3EndBenignMalloc(void);
11745 #else
11746   #define sqlite3BeginBenignMalloc()
11747   #define sqlite3EndBenignMalloc()
11748 #endif
11749 
11750 #define IN_INDEX_ROWID           1
11751 #define IN_INDEX_EPH             2
11752 #define IN_INDEX_INDEX           3
11753 SQLITE_PRIVATE int sqlite3FindInIndex(Parse *, Expr *, int*);
11754 
11755 #ifdef SQLITE_ENABLE_ATOMIC_WRITE
11756 SQLITE_PRIVATE   int sqlite3JournalOpen(sqlite3_vfs *, const char *, sqlite3_file *, int, int);
11757 SQLITE_PRIVATE   int sqlite3JournalSize(sqlite3_vfs *);
11758 SQLITE_PRIVATE   int sqlite3JournalCreate(sqlite3_file *);
11759 #else
11760   #define sqlite3JournalSize(pVfs) ((pVfs)->szOsFile)
11761 #endif
11762 
11763 SQLITE_PRIVATE void sqlite3MemJournalOpen(sqlite3_file *);
11764 SQLITE_PRIVATE int sqlite3MemJournalSize(void);
11765 SQLITE_PRIVATE int sqlite3IsMemJournal(sqlite3_file *);
11766 
11767 #if SQLITE_MAX_EXPR_DEPTH>0
11768 SQLITE_PRIVATE   void sqlite3ExprSetHeight(Parse *pParse, Expr *p);
11769 SQLITE_PRIVATE   int sqlite3SelectExprHeight(Select *);
11770 SQLITE_PRIVATE   int sqlite3ExprCheckHeight(Parse*, int);
11771 #else
11772   #define sqlite3ExprSetHeight(x,y)
11773   #define sqlite3SelectExprHeight(x) 0
11774   #define sqlite3ExprCheckHeight(x,y)
11775 #endif
11776 
11777 SQLITE_PRIVATE u32 sqlite3Get4byte(const u8*);
11778 SQLITE_PRIVATE void sqlite3Put4byte(u8*, u32);
11779 
11780 #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
11781 SQLITE_PRIVATE   void sqlite3ConnectionBlocked(sqlite3 *, sqlite3 *);
11782 SQLITE_PRIVATE   void sqlite3ConnectionUnlocked(sqlite3 *db);
11783 SQLITE_PRIVATE   void sqlite3ConnectionClosed(sqlite3 *db);
11784 #else
11785   #define sqlite3ConnectionBlocked(x,y)
11786   #define sqlite3ConnectionUnlocked(x)
11787   #define sqlite3ConnectionClosed(x)
11788 #endif
11789 
11790 #ifdef SQLITE_DEBUG
11791 SQLITE_PRIVATE   void sqlite3ParserTrace(FILE*, char *);
11792 #endif
11793 
11794 /*
11795 ** If the SQLITE_ENABLE IOTRACE exists then the global variable
11796 ** sqlite3IoTrace is a pointer to a printf-like routine used to
11797 ** print I/O tracing messages.
11798 */
11799 #ifdef SQLITE_ENABLE_IOTRACE
11800 # define IOTRACE(A)  if( sqlite3IoTrace ){ sqlite3IoTrace A; }
11801 SQLITE_PRIVATE   void sqlite3VdbeIOTraceSql(Vdbe*);
11802 SQLITE_PRIVATE void (*sqlite3IoTrace)(const char*,...);
11803 #else
11804 # define IOTRACE(A)
11805 # define sqlite3VdbeIOTraceSql(X)
11806 #endif
11807 
11808 /*
11809 ** These routines are available for the mem2.c debugging memory allocator
11810 ** only.  They are used to verify that different "types" of memory
11811 ** allocations are properly tracked by the system.
11812 **
11813 ** sqlite3MemdebugSetType() sets the "type" of an allocation to one of
11814 ** the MEMTYPE_* macros defined below.  The type must be a bitmask with
11815 ** a single bit set.
11816 **
11817 ** sqlite3MemdebugHasType() returns true if any of the bits in its second
11818 ** argument match the type set by the previous sqlite3MemdebugSetType().
11819 ** sqlite3MemdebugHasType() is intended for use inside assert() statements.
11820 **
11821 ** sqlite3MemdebugNoType() returns true if none of the bits in its second
11822 ** argument match the type set by the previous sqlite3MemdebugSetType().
11823 **
11824 ** Perhaps the most important point is the difference between MEMTYPE_HEAP
11825 ** and MEMTYPE_LOOKASIDE.  If an allocation is MEMTYPE_LOOKASIDE, that means
11826 ** it might have been allocated by lookaside, except the allocation was
11827 ** too large or lookaside was already full.  It is important to verify
11828 ** that allocations that might have been satisfied by lookaside are not
11829 ** passed back to non-lookaside free() routines.  Asserts such as the
11830 ** example above are placed on the non-lookaside free() routines to verify
11831 ** this constraint.
11832 **
11833 ** All of this is no-op for a production build.  It only comes into
11834 ** play when the SQLITE_MEMDEBUG compile-time option is used.
11835 */
11836 #ifdef SQLITE_MEMDEBUG
11837 SQLITE_PRIVATE   void sqlite3MemdebugSetType(void*,u8);
11838 SQLITE_PRIVATE   int sqlite3MemdebugHasType(void*,u8);
11839 SQLITE_PRIVATE   int sqlite3MemdebugNoType(void*,u8);
11840 #else
11841 # define sqlite3MemdebugSetType(X,Y)  /* no-op */
11842 # define sqlite3MemdebugHasType(X,Y)  1
11843 # define sqlite3MemdebugNoType(X,Y)   1
11844 #endif
11845 #define MEMTYPE_HEAP       0x01  /* General heap allocations */
11846 #define MEMTYPE_LOOKASIDE  0x02  /* Might have been lookaside memory */
11847 #define MEMTYPE_SCRATCH    0x04  /* Scratch allocations */
11848 #define MEMTYPE_PCACHE     0x08  /* Page cache allocations */
11849 #define MEMTYPE_DB         0x10  /* Uses sqlite3DbMalloc, not sqlite_malloc */
11850 
11851 #endif /* _SQLITEINT_H_ */
11852 
11853 /************** End of sqliteInt.h *******************************************/
11854 /************** Begin file global.c ******************************************/
11855 /*
11856 ** 2008 June 13
11857 **
11858 ** The author disclaims copyright to this source code.  In place of
11859 ** a legal notice, here is a blessing:
11860 **
11861 **    May you do good and not evil.
11862 **    May you find forgiveness for yourself and forgive others.
11863 **    May you share freely, never taking more than you give.
11864 **
11865 *************************************************************************
11866 **
11867 ** This file contains definitions of global variables and contants.
11868 */
11869 
11870 /* An array to map all upper-case characters into their corresponding
11871 ** lower-case character.
11872 **
11873 ** SQLite only considers US-ASCII (or EBCDIC) characters.  We do not
11874 ** handle case conversions for the UTF character set since the tables
11875 ** involved are nearly as big or bigger than SQLite itself.
11876 */
11877 SQLITE_PRIVATE const unsigned char sqlite3UpperToLower[] = {
11878 #ifdef SQLITE_ASCII
11879       0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17,
11880      18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
11881      36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
11882      54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 97, 98, 99,100,101,102,103,
11883     104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,
11884     122, 91, 92, 93, 94, 95, 96, 97, 98, 99,100,101,102,103,104,105,106,107,
11885     108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,
11886     126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
11887     144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,
11888     162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,
11889     180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,
11890     198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,
11891     216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,
11892     234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,
11893     252,253,254,255
11894 #endif
11895 #ifdef SQLITE_EBCDIC
11896       0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, /* 0x */
11897      16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, /* 1x */
11898      32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, /* 2x */
11899      48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, /* 3x */
11900      64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, /* 4x */
11901      80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, /* 5x */
11902      96, 97, 66, 67, 68, 69, 70, 71, 72, 73,106,107,108,109,110,111, /* 6x */
11903     112, 81, 82, 83, 84, 85, 86, 87, 88, 89,122,123,124,125,126,127, /* 7x */
11904     128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143, /* 8x */
11905     144,145,146,147,148,149,150,151,152,153,154,155,156,157,156,159, /* 9x */
11906     160,161,162,163,164,165,166,167,168,169,170,171,140,141,142,175, /* Ax */
11907     176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191, /* Bx */
11908     192,129,130,131,132,133,134,135,136,137,202,203,204,205,206,207, /* Cx */
11909     208,145,146,147,148,149,150,151,152,153,218,219,220,221,222,223, /* Dx */
11910     224,225,162,163,164,165,166,167,168,169,232,203,204,205,206,207, /* Ex */
11911     239,240,241,242,243,244,245,246,247,248,249,219,220,221,222,255, /* Fx */
11912 #endif
11913 };
11914 
11915 /*
11916 ** The following 256 byte lookup table is used to support SQLites built-in
11917 ** equivalents to the following standard library functions:
11918 **
11919 **   isspace()                        0x01
11920 **   isalpha()                        0x02
11921 **   isdigit()                        0x04
11922 **   isalnum()                        0x06
11923 **   isxdigit()                       0x08
11924 **   toupper()                        0x20
11925 **   SQLite identifier character      0x40
11926 **
11927 ** Bit 0x20 is set if the mapped character requires translation to upper
11928 ** case. i.e. if the character is a lower-case ASCII character.
11929 ** If x is a lower-case ASCII character, then its upper-case equivalent
11930 ** is (x - 0x20). Therefore toupper() can be implemented as:
11931 **
11932 **   (x & ~(map[x]&0x20))
11933 **
11934 ** Standard function tolower() is implemented using the sqlite3UpperToLower[]
11935 ** array. tolower() is used more often than toupper() by SQLite.
11936 **
11937 ** Bit 0x40 is set if the character non-alphanumeric and can be used in an
11938 ** SQLite identifier.  Identifiers are alphanumerics, "_", "$", and any
11939 ** non-ASCII UTF character. Hence the test for whether or not a character is
11940 ** part of an identifier is 0x46.
11941 **
11942 ** SQLite's versions are identical to the standard versions assuming a
11943 ** locale of "C". They are implemented as macros in sqliteInt.h.
11944 */
11945 #ifdef SQLITE_ASCII
11946 SQLITE_PRIVATE const unsigned char sqlite3CtypeMap[256] = {
11947   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 00..07    ........ */
11948   0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00,  /* 08..0f    ........ */
11949   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 10..17    ........ */
11950   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 18..1f    ........ */
11951   0x01, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,  /* 20..27     !"#$%&' */
11952   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 28..2f    ()*+,-./ */
11953   0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,  /* 30..37    01234567 */
11954   0x0c, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 38..3f    89:;<=>? */
11955 
11956   0x00, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x02,  /* 40..47    @ABCDEFG */
11957   0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,  /* 48..4f    HIJKLMNO */
11958   0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,  /* 50..57    PQRSTUVW */
11959   0x02, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x40,  /* 58..5f    XYZ[\]^_ */
11960   0x00, 0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x22,  /* 60..67    `abcdefg */
11961   0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,  /* 68..6f    hijklmno */
11962   0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,  /* 70..77    pqrstuvw */
11963   0x22, 0x22, 0x22, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 78..7f    xyz{|}~. */
11964 
11965   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* 80..87    ........ */
11966   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* 88..8f    ........ */
11967   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* 90..97    ........ */
11968   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* 98..9f    ........ */
11969   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* a0..a7    ........ */
11970   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* a8..af    ........ */
11971   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* b0..b7    ........ */
11972   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* b8..bf    ........ */
11973 
11974   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* c0..c7    ........ */
11975   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* c8..cf    ........ */
11976   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* d0..d7    ........ */
11977   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* d8..df    ........ */
11978   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* e0..e7    ........ */
11979   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* e8..ef    ........ */
11980   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* f0..f7    ........ */
11981   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40   /* f8..ff    ........ */
11982 };
11983 #endif
11984 
11985 #ifndef SQLITE_USE_URI
11986 # define  SQLITE_USE_URI 0
11987 #endif
11988 
11989 /*
11990 ** The following singleton contains the global configuration for
11991 ** the SQLite library.
11992 */
11993 SQLITE_PRIVATE SQLITE_WSD struct Sqlite3Config sqlite3Config = {
11994    SQLITE_DEFAULT_MEMSTATUS,  /* bMemstat */
11995    1,                         /* bCoreMutex */
11996    SQLITE_THREADSAFE==1,      /* bFullMutex */
11997    SQLITE_USE_URI,            /* bOpenUri */
11998    0x7ffffffe,                /* mxStrlen */
11999    100,                       /* szLookaside */
12000    500,                       /* nLookaside */
12001    {0,0,0,0,0,0,0,0},         /* m */
12002    {0,0,0,0,0,0,0,0,0},       /* mutex */
12003    {0,0,0,0,0,0,0,0,0,0,0},   /* pcache */
12004    (void*)0,                  /* pHeap */
12005    0,                         /* nHeap */
12006    0, 0,                      /* mnHeap, mxHeap */
12007    (void*)0,                  /* pScratch */
12008    0,                         /* szScratch */
12009    0,                         /* nScratch */
12010    (void*)0,                  /* pPage */
12011    0,                         /* szPage */
12012    0,                         /* nPage */
12013    0,                         /* mxParserStack */
12014    0,                         /* sharedCacheEnabled */
12015    /* All the rest should always be initialized to zero */
12016    0,                         /* isInit */
12017    0,                         /* inProgress */
12018    0,                         /* isMutexInit */
12019    0,                         /* isMallocInit */
12020    0,                         /* isPCacheInit */
12021    0,                         /* pInitMutex */
12022    0,                         /* nRefInitMutex */
12023    0,                         /* xLog */
12024    0,                         /* pLogArg */
12025    0,                         /* bLocaltimeFault */
12026 };
12027 
12028 
12029 /*
12030 ** Hash table for global functions - functions common to all
12031 ** database connections.  After initialization, this table is
12032 ** read-only.
12033 */
12034 SQLITE_PRIVATE SQLITE_WSD FuncDefHash sqlite3GlobalFunctions;
12035 
12036 /*
12037 ** Constant tokens for values 0 and 1.
12038 */
12039 SQLITE_PRIVATE const Token sqlite3IntTokens[] = {
12040    { "0", 1 },
12041    { "1", 1 }
12042 };
12043 
12044 
12045 /*
12046 ** The value of the "pending" byte must be 0x40000000 (1 byte past the
12047 ** 1-gibabyte boundary) in a compatible database.  SQLite never uses
12048 ** the database page that contains the pending byte.  It never attempts
12049 ** to read or write that page.  The pending byte page is set assign
12050 ** for use by the VFS layers as space for managing file locks.
12051 **
12052 ** During testing, it is often desirable to move the pending byte to
12053 ** a different position in the file.  This allows code that has to
12054 ** deal with the pending byte to run on files that are much smaller
12055 ** than 1 GiB.  The sqlite3_test_control() interface can be used to
12056 ** move the pending byte.
12057 **
12058 ** IMPORTANT:  Changing the pending byte to any value other than
12059 ** 0x40000000 results in an incompatible database file format!
12060 ** Changing the pending byte during operating results in undefined
12061 ** and dileterious behavior.
12062 */
12063 #ifndef SQLITE_OMIT_WSD
12064 SQLITE_PRIVATE int sqlite3PendingByte = 0x40000000;
12065 #endif
12066 
12067 /*
12068 ** Properties of opcodes.  The OPFLG_INITIALIZER macro is
12069 ** created by mkopcodeh.awk during compilation.  Data is obtained
12070 ** from the comments following the "case OP_xxxx:" statements in
12071 ** the vdbe.c file.
12072 */
12073 SQLITE_PRIVATE const unsigned char sqlite3OpcodeProperty[] = OPFLG_INITIALIZER;
12074 
12075 /************** End of global.c **********************************************/
12076 /************** Begin file ctime.c *******************************************/
12077 /*
12078 ** 2010 February 23
12079 **
12080 ** The author disclaims copyright to this source code.  In place of
12081 ** a legal notice, here is a blessing:
12082 **
12083 **    May you do good and not evil.
12084 **    May you find forgiveness for yourself and forgive others.
12085 **    May you share freely, never taking more than you give.
12086 **
12087 *************************************************************************
12088 **
12089 ** This file implements routines used to report what compile-time options
12090 ** SQLite was built with.
12091 */
12092 
12093 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
12094 
12095 
12096 /*
12097 ** An array of names of all compile-time options.  This array should
12098 ** be sorted A-Z.
12099 **
12100 ** This array looks large, but in a typical installation actually uses
12101 ** only a handful of compile-time options, so most times this array is usually
12102 ** rather short and uses little memory space.
12103 */
12104 static const char * const azCompileOpt[] = {
12105 
12106 /* These macros are provided to "stringify" the value of the define
12107 ** for those options in which the value is meaningful. */
12108 #define CTIMEOPT_VAL_(opt) #opt
12109 #define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt)
12110 
12111 #ifdef SQLITE_32BIT_ROWID
12112   "32BIT_ROWID",
12113 #endif
12114 #ifdef SQLITE_4_BYTE_ALIGNED_MALLOC
12115   "4_BYTE_ALIGNED_MALLOC",
12116 #endif
12117 #ifdef SQLITE_CASE_SENSITIVE_LIKE
12118   "CASE_SENSITIVE_LIKE",
12119 #endif
12120 #ifdef SQLITE_CHECK_PAGES
12121   "CHECK_PAGES",
12122 #endif
12123 #ifdef SQLITE_COVERAGE_TEST
12124   "COVERAGE_TEST",
12125 #endif
12126 #ifdef SQLITE_DEBUG
12127   "DEBUG",
12128 #endif
12129 #ifdef SQLITE_DEFAULT_LOCKING_MODE
12130   "DEFAULT_LOCKING_MODE=" CTIMEOPT_VAL(SQLITE_DEFAULT_LOCKING_MODE),
12131 #endif
12132 #ifdef SQLITE_DISABLE_DIRSYNC
12133   "DISABLE_DIRSYNC",
12134 #endif
12135 #ifdef SQLITE_DISABLE_LFS
12136   "DISABLE_LFS",
12137 #endif
12138 #ifdef SQLITE_ENABLE_ATOMIC_WRITE
12139   "ENABLE_ATOMIC_WRITE",
12140 #endif
12141 #ifdef SQLITE_ENABLE_CEROD
12142   "ENABLE_CEROD",
12143 #endif
12144 #ifdef SQLITE_ENABLE_COLUMN_METADATA
12145   "ENABLE_COLUMN_METADATA",
12146 #endif
12147 #ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
12148   "ENABLE_EXPENSIVE_ASSERT",
12149 #endif
12150 #ifdef SQLITE_ENABLE_FTS1
12151   "ENABLE_FTS1",
12152 #endif
12153 #ifdef SQLITE_ENABLE_FTS2
12154   "ENABLE_FTS2",
12155 #endif
12156 #ifdef SQLITE_ENABLE_FTS3
12157   "ENABLE_FTS3",
12158 #endif
12159 #ifdef SQLITE_ENABLE_FTS3_PARENTHESIS
12160   "ENABLE_FTS3_PARENTHESIS",
12161 #endif
12162 #ifdef SQLITE_ENABLE_FTS4
12163   "ENABLE_FTS4",
12164 #endif
12165 #ifdef SQLITE_ENABLE_ICU
12166   "ENABLE_ICU",
12167 #endif
12168 #ifdef SQLITE_ENABLE_IOTRACE
12169   "ENABLE_IOTRACE",
12170 #endif
12171 #ifdef SQLITE_ENABLE_LOAD_EXTENSION
12172   "ENABLE_LOAD_EXTENSION",
12173 #endif
12174 #ifdef SQLITE_ENABLE_LOCKING_STYLE
12175   "ENABLE_LOCKING_STYLE=" CTIMEOPT_VAL(SQLITE_ENABLE_LOCKING_STYLE),
12176 #endif
12177 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
12178   "ENABLE_MEMORY_MANAGEMENT",
12179 #endif
12180 #ifdef SQLITE_ENABLE_MEMSYS3
12181   "ENABLE_MEMSYS3",
12182 #endif
12183 #ifdef SQLITE_ENABLE_MEMSYS5
12184   "ENABLE_MEMSYS5",
12185 #endif
12186 #ifdef SQLITE_ENABLE_OVERSIZE_CELL_CHECK
12187   "ENABLE_OVERSIZE_CELL_CHECK",
12188 #endif
12189 #ifdef SQLITE_ENABLE_RTREE
12190   "ENABLE_RTREE",
12191 #endif
12192 #ifdef SQLITE_ENABLE_STAT2
12193   "ENABLE_STAT2",
12194 #endif
12195 #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
12196   "ENABLE_UNLOCK_NOTIFY",
12197 #endif
12198 #ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
12199   "ENABLE_UPDATE_DELETE_LIMIT",
12200 #endif
12201 #ifdef SQLITE_HAS_CODEC
12202   "HAS_CODEC",
12203 #endif
12204 #ifdef SQLITE_HAVE_ISNAN
12205   "HAVE_ISNAN",
12206 #endif
12207 #ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
12208   "HOMEGROWN_RECURSIVE_MUTEX",
12209 #endif
12210 #ifdef SQLITE_IGNORE_AFP_LOCK_ERRORS
12211   "IGNORE_AFP_LOCK_ERRORS",
12212 #endif
12213 #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
12214   "IGNORE_FLOCK_LOCK_ERRORS",
12215 #endif
12216 #ifdef SQLITE_INT64_TYPE
12217   "INT64_TYPE",
12218 #endif
12219 #ifdef SQLITE_LOCK_TRACE
12220   "LOCK_TRACE",
12221 #endif
12222 #ifdef SQLITE_MEMDEBUG
12223   "MEMDEBUG",
12224 #endif
12225 #ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
12226   "MIXED_ENDIAN_64BIT_FLOAT",
12227 #endif
12228 #ifdef SQLITE_NO_SYNC
12229   "NO_SYNC",
12230 #endif
12231 #ifdef SQLITE_OMIT_ALTERTABLE
12232   "OMIT_ALTERTABLE",
12233 #endif
12234 #ifdef SQLITE_OMIT_ANALYZE
12235   "OMIT_ANALYZE",
12236 #endif
12237 #ifdef SQLITE_OMIT_ATTACH
12238   "OMIT_ATTACH",
12239 #endif
12240 #ifdef SQLITE_OMIT_AUTHORIZATION
12241   "OMIT_AUTHORIZATION",
12242 #endif
12243 #ifdef SQLITE_OMIT_AUTOINCREMENT
12244   "OMIT_AUTOINCREMENT",
12245 #endif
12246 #ifdef SQLITE_OMIT_AUTOINIT
12247   "OMIT_AUTOINIT",
12248 #endif
12249 #ifdef SQLITE_OMIT_AUTOMATIC_INDEX
12250   "OMIT_AUTOMATIC_INDEX",
12251 #endif
12252 #ifdef SQLITE_OMIT_AUTORESET
12253   "OMIT_AUTORESET",
12254 #endif
12255 #ifdef SQLITE_OMIT_AUTOVACUUM
12256   "OMIT_AUTOVACUUM",
12257 #endif
12258 #ifdef SQLITE_OMIT_BETWEEN_OPTIMIZATION
12259   "OMIT_BETWEEN_OPTIMIZATION",
12260 #endif
12261 #ifdef SQLITE_OMIT_BLOB_LITERAL
12262   "OMIT_BLOB_LITERAL",
12263 #endif
12264 #ifdef SQLITE_OMIT_BTREECOUNT
12265   "OMIT_BTREECOUNT",
12266 #endif
12267 #ifdef SQLITE_OMIT_BUILTIN_TEST
12268   "OMIT_BUILTIN_TEST",
12269 #endif
12270 #ifdef SQLITE_OMIT_CAST
12271   "OMIT_CAST",
12272 #endif
12273 #ifdef SQLITE_OMIT_CHECK
12274   "OMIT_CHECK",
12275 #endif
12276 /* // redundant
12277 ** #ifdef SQLITE_OMIT_COMPILEOPTION_DIAGS
12278 **   "OMIT_COMPILEOPTION_DIAGS",
12279 ** #endif
12280 */
12281 #ifdef SQLITE_OMIT_COMPLETE
12282   "OMIT_COMPLETE",
12283 #endif
12284 #ifdef SQLITE_OMIT_COMPOUND_SELECT
12285   "OMIT_COMPOUND_SELECT",
12286 #endif
12287 #ifdef SQLITE_OMIT_DATETIME_FUNCS
12288   "OMIT_DATETIME_FUNCS",
12289 #endif
12290 #ifdef SQLITE_OMIT_DECLTYPE
12291   "OMIT_DECLTYPE",
12292 #endif
12293 #ifdef SQLITE_OMIT_DEPRECATED
12294   "OMIT_DEPRECATED",
12295 #endif
12296 #ifdef SQLITE_OMIT_DISKIO
12297   "OMIT_DISKIO",
12298 #endif
12299 #ifdef SQLITE_OMIT_EXPLAIN
12300   "OMIT_EXPLAIN",
12301 #endif
12302 #ifdef SQLITE_OMIT_FLAG_PRAGMAS
12303   "OMIT_FLAG_PRAGMAS",
12304 #endif
12305 #ifdef SQLITE_OMIT_FLOATING_POINT
12306   "OMIT_FLOATING_POINT",
12307 #endif
12308 #ifdef SQLITE_OMIT_FOREIGN_KEY
12309   "OMIT_FOREIGN_KEY",
12310 #endif
12311 #ifdef SQLITE_OMIT_GET_TABLE
12312   "OMIT_GET_TABLE",
12313 #endif
12314 #ifdef SQLITE_OMIT_INCRBLOB
12315   "OMIT_INCRBLOB",
12316 #endif
12317 #ifdef SQLITE_OMIT_INTEGRITY_CHECK
12318   "OMIT_INTEGRITY_CHECK",
12319 #endif
12320 #ifdef SQLITE_OMIT_LIKE_OPTIMIZATION
12321   "OMIT_LIKE_OPTIMIZATION",
12322 #endif
12323 #ifdef SQLITE_OMIT_LOAD_EXTENSION
12324   "OMIT_LOAD_EXTENSION",
12325 #endif
12326 #ifdef SQLITE_OMIT_LOCALTIME
12327   "OMIT_LOCALTIME",
12328 #endif
12329 #ifdef SQLITE_OMIT_LOOKASIDE
12330   "OMIT_LOOKASIDE",
12331 #endif
12332 #ifdef SQLITE_OMIT_MEMORYDB
12333   "OMIT_MEMORYDB",
12334 #endif
12335 #ifdef SQLITE_OMIT_OR_OPTIMIZATION
12336   "OMIT_OR_OPTIMIZATION",
12337 #endif
12338 #ifdef SQLITE_OMIT_PAGER_PRAGMAS
12339   "OMIT_PAGER_PRAGMAS",
12340 #endif
12341 #ifdef SQLITE_OMIT_PRAGMA
12342   "OMIT_PRAGMA",
12343 #endif
12344 #ifdef SQLITE_OMIT_PROGRESS_CALLBACK
12345   "OMIT_PROGRESS_CALLBACK",
12346 #endif
12347 #ifdef SQLITE_OMIT_QUICKBALANCE
12348   "OMIT_QUICKBALANCE",
12349 #endif
12350 #ifdef SQLITE_OMIT_REINDEX
12351   "OMIT_REINDEX",
12352 #endif
12353 #ifdef SQLITE_OMIT_SCHEMA_PRAGMAS
12354   "OMIT_SCHEMA_PRAGMAS",
12355 #endif
12356 #ifdef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
12357   "OMIT_SCHEMA_VERSION_PRAGMAS",
12358 #endif
12359 #ifdef SQLITE_OMIT_SHARED_CACHE
12360   "OMIT_SHARED_CACHE",
12361 #endif
12362 #ifdef SQLITE_OMIT_SUBQUERY
12363   "OMIT_SUBQUERY",
12364 #endif
12365 #ifdef SQLITE_OMIT_TCL_VARIABLE
12366   "OMIT_TCL_VARIABLE",
12367 #endif
12368 #ifdef SQLITE_OMIT_TEMPDB
12369   "OMIT_TEMPDB",
12370 #endif
12371 #ifdef SQLITE_OMIT_TRACE
12372   "OMIT_TRACE",
12373 #endif
12374 #ifdef SQLITE_OMIT_TRIGGER
12375   "OMIT_TRIGGER",
12376 #endif
12377 #ifdef SQLITE_OMIT_TRUNCATE_OPTIMIZATION
12378   "OMIT_TRUNCATE_OPTIMIZATION",
12379 #endif
12380 #ifdef SQLITE_OMIT_UTF16
12381   "OMIT_UTF16",
12382 #endif
12383 #ifdef SQLITE_OMIT_VACUUM
12384   "OMIT_VACUUM",
12385 #endif
12386 #ifdef SQLITE_OMIT_VIEW
12387   "OMIT_VIEW",
12388 #endif
12389 #ifdef SQLITE_OMIT_VIRTUALTABLE
12390   "OMIT_VIRTUALTABLE",
12391 #endif
12392 #ifdef SQLITE_OMIT_WAL
12393   "OMIT_WAL",
12394 #endif
12395 #ifdef SQLITE_OMIT_WSD
12396   "OMIT_WSD",
12397 #endif
12398 #ifdef SQLITE_OMIT_XFER_OPT
12399   "OMIT_XFER_OPT",
12400 #endif
12401 #ifdef SQLITE_PERFORMANCE_TRACE
12402   "PERFORMANCE_TRACE",
12403 #endif
12404 #ifdef SQLITE_PROXY_DEBUG
12405   "PROXY_DEBUG",
12406 #endif
12407 #ifdef SQLITE_SECURE_DELETE
12408   "SECURE_DELETE",
12409 #endif
12410 #ifdef SQLITE_SMALL_STACK
12411   "SMALL_STACK",
12412 #endif
12413 #ifdef SQLITE_SOUNDEX
12414   "SOUNDEX",
12415 #endif
12416 #ifdef SQLITE_TCL
12417   "TCL",
12418 #endif
12419 #ifdef SQLITE_TEMP_STORE
12420   "TEMP_STORE=" CTIMEOPT_VAL(SQLITE_TEMP_STORE),
12421 #endif
12422 #ifdef SQLITE_TEST
12423   "TEST",
12424 #endif
12425 #ifdef SQLITE_THREADSAFE
12426   "THREADSAFE=" CTIMEOPT_VAL(SQLITE_THREADSAFE),
12427 #endif
12428 #ifdef SQLITE_USE_ALLOCA
12429   "USE_ALLOCA",
12430 #endif
12431 #ifdef SQLITE_ZERO_MALLOC
12432   "ZERO_MALLOC"
12433 #endif
12434 };
12435 
12436 /*
12437 ** Given the name of a compile-time option, return true if that option
12438 ** was used and false if not.
12439 **
12440 ** The name can optionally begin with "SQLITE_" but the "SQLITE_" prefix
12441 ** is not required for a match.
12442 */
sqlite3_compileoption_used(const char * zOptName)12443 SQLITE_API int sqlite3_compileoption_used(const char *zOptName){
12444   int i, n;
12445   if( sqlite3StrNICmp(zOptName, "SQLITE_", 7)==0 ) zOptName += 7;
12446   n = sqlite3Strlen30(zOptName);
12447 
12448   /* Since ArraySize(azCompileOpt) is normally in single digits, a
12449   ** linear search is adequate.  No need for a binary search. */
12450   for(i=0; i<ArraySize(azCompileOpt); i++){
12451     if(   (sqlite3StrNICmp(zOptName, azCompileOpt[i], n)==0)
12452        && ( (azCompileOpt[i][n]==0) || (azCompileOpt[i][n]=='=') ) ) return 1;
12453   }
12454   return 0;
12455 }
12456 
12457 /*
12458 ** Return the N-th compile-time option string.  If N is out of range,
12459 ** return a NULL pointer.
12460 */
sqlite3_compileoption_get(int N)12461 SQLITE_API const char *sqlite3_compileoption_get(int N){
12462   if( N>=0 && N<ArraySize(azCompileOpt) ){
12463     return azCompileOpt[N];
12464   }
12465   return 0;
12466 }
12467 
12468 #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
12469 
12470 /************** End of ctime.c ***********************************************/
12471 /************** Begin file status.c ******************************************/
12472 /*
12473 ** 2008 June 18
12474 **
12475 ** The author disclaims copyright to this source code.  In place of
12476 ** a legal notice, here is a blessing:
12477 **
12478 **    May you do good and not evil.
12479 **    May you find forgiveness for yourself and forgive others.
12480 **    May you share freely, never taking more than you give.
12481 **
12482 *************************************************************************
12483 **
12484 ** This module implements the sqlite3_status() interface and related
12485 ** functionality.
12486 */
12487 /************** Include vdbeInt.h in the middle of status.c ******************/
12488 /************** Begin file vdbeInt.h *****************************************/
12489 /*
12490 ** 2003 September 6
12491 **
12492 ** The author disclaims copyright to this source code.  In place of
12493 ** a legal notice, here is a blessing:
12494 **
12495 **    May you do good and not evil.
12496 **    May you find forgiveness for yourself and forgive others.
12497 **    May you share freely, never taking more than you give.
12498 **
12499 *************************************************************************
12500 ** This is the header file for information that is private to the
12501 ** VDBE.  This information used to all be at the top of the single
12502 ** source code file "vdbe.c".  When that file became too big (over
12503 ** 6000 lines long) it was split up into several smaller files and
12504 ** this header information was factored out.
12505 */
12506 #ifndef _VDBEINT_H_
12507 #define _VDBEINT_H_
12508 
12509 /*
12510 ** SQL is translated into a sequence of instructions to be
12511 ** executed by a virtual machine.  Each instruction is an instance
12512 ** of the following structure.
12513 */
12514 typedef struct VdbeOp Op;
12515 
12516 /*
12517 ** Boolean values
12518 */
12519 typedef unsigned char Bool;
12520 
12521 /*
12522 ** A cursor is a pointer into a single BTree within a database file.
12523 ** The cursor can seek to a BTree entry with a particular key, or
12524 ** loop over all entries of the Btree.  You can also insert new BTree
12525 ** entries or retrieve the key or data from the entry that the cursor
12526 ** is currently pointing to.
12527 **
12528 ** Every cursor that the virtual machine has open is represented by an
12529 ** instance of the following structure.
12530 */
12531 struct VdbeCursor {
12532   BtCursor *pCursor;    /* The cursor structure of the backend */
12533   Btree *pBt;           /* Separate file holding temporary table */
12534   KeyInfo *pKeyInfo;    /* Info about index keys needed by index cursors */
12535   int iDb;              /* Index of cursor database in db->aDb[] (or -1) */
12536   int pseudoTableReg;   /* Register holding pseudotable content. */
12537   int nField;           /* Number of fields in the header */
12538   Bool zeroed;          /* True if zeroed out and ready for reuse */
12539   Bool rowidIsValid;    /* True if lastRowid is valid */
12540   Bool atFirst;         /* True if pointing to first entry */
12541   Bool useRandomRowid;  /* Generate new record numbers semi-randomly */
12542   Bool nullRow;         /* True if pointing to a row with no data */
12543   Bool deferredMoveto;  /* A call to sqlite3BtreeMoveto() is needed */
12544   Bool isTable;         /* True if a table requiring integer keys */
12545   Bool isIndex;         /* True if an index containing keys only - no data */
12546   Bool isOrdered;       /* True if the underlying table is BTREE_UNORDERED */
12547   sqlite3_vtab_cursor *pVtabCursor;  /* The cursor for a virtual table */
12548   const sqlite3_module *pModule;     /* Module for cursor pVtabCursor */
12549   i64 seqCount;         /* Sequence counter */
12550   i64 movetoTarget;     /* Argument to the deferred sqlite3BtreeMoveto() */
12551   i64 lastRowid;        /* Last rowid from a Next or NextIdx operation */
12552 
12553   /* Result of last sqlite3BtreeMoveto() done by an OP_NotExists or
12554   ** OP_IsUnique opcode on this cursor. */
12555   int seekResult;
12556 
12557   /* Cached information about the header for the data record that the
12558   ** cursor is currently pointing to.  Only valid if cacheStatus matches
12559   ** Vdbe.cacheCtr.  Vdbe.cacheCtr will never take on the value of
12560   ** CACHE_STALE and so setting cacheStatus=CACHE_STALE guarantees that
12561   ** the cache is out of date.
12562   **
12563   ** aRow might point to (ephemeral) data for the current row, or it might
12564   ** be NULL.
12565   */
12566   u32 cacheStatus;      /* Cache is valid if this matches Vdbe.cacheCtr */
12567   int payloadSize;      /* Total number of bytes in the record */
12568   u32 *aType;           /* Type values for all entries in the record */
12569   u32 *aOffset;         /* Cached offsets to the start of each columns data */
12570   u8 *aRow;             /* Data for the current row, if all on one page */
12571 };
12572 typedef struct VdbeCursor VdbeCursor;
12573 
12574 /*
12575 ** When a sub-program is executed (OP_Program), a structure of this type
12576 ** is allocated to store the current value of the program counter, as
12577 ** well as the current memory cell array and various other frame specific
12578 ** values stored in the Vdbe struct. When the sub-program is finished,
12579 ** these values are copied back to the Vdbe from the VdbeFrame structure,
12580 ** restoring the state of the VM to as it was before the sub-program
12581 ** began executing.
12582 **
12583 ** The memory for a VdbeFrame object is allocated and managed by a memory
12584 ** cell in the parent (calling) frame. When the memory cell is deleted or
12585 ** overwritten, the VdbeFrame object is not freed immediately. Instead, it
12586 ** is linked into the Vdbe.pDelFrame list. The contents of the Vdbe.pDelFrame
12587 ** list is deleted when the VM is reset in VdbeHalt(). The reason for doing
12588 ** this instead of deleting the VdbeFrame immediately is to avoid recursive
12589 ** calls to sqlite3VdbeMemRelease() when the memory cells belonging to the
12590 ** child frame are released.
12591 **
12592 ** The currently executing frame is stored in Vdbe.pFrame. Vdbe.pFrame is
12593 ** set to NULL if the currently executing frame is the main program.
12594 */
12595 typedef struct VdbeFrame VdbeFrame;
12596 struct VdbeFrame {
12597   Vdbe *v;                /* VM this frame belongs to */
12598   int pc;                 /* Program Counter in parent (calling) frame */
12599   Op *aOp;                /* Program instructions for parent frame */
12600   int nOp;                /* Size of aOp array */
12601   Mem *aMem;              /* Array of memory cells for parent frame */
12602   int nMem;               /* Number of entries in aMem */
12603   VdbeCursor **apCsr;     /* Array of Vdbe cursors for parent frame */
12604   u16 nCursor;            /* Number of entries in apCsr */
12605   void *token;            /* Copy of SubProgram.token */
12606   int nChildMem;          /* Number of memory cells for child frame */
12607   int nChildCsr;          /* Number of cursors for child frame */
12608   i64 lastRowid;          /* Last insert rowid (sqlite3.lastRowid) */
12609   int nChange;            /* Statement changes (Vdbe.nChanges)     */
12610   VdbeFrame *pParent;     /* Parent of this frame, or NULL if parent is main */
12611 };
12612 
12613 #define VdbeFrameMem(p) ((Mem *)&((u8 *)p)[ROUND8(sizeof(VdbeFrame))])
12614 
12615 /*
12616 ** A value for VdbeCursor.cacheValid that means the cache is always invalid.
12617 */
12618 #define CACHE_STALE 0
12619 
12620 /*
12621 ** Internally, the vdbe manipulates nearly all SQL values as Mem
12622 ** structures. Each Mem struct may cache multiple representations (string,
12623 ** integer etc.) of the same value.
12624 */
12625 struct Mem {
12626   sqlite3 *db;        /* The associated database connection */
12627   char *z;            /* String or BLOB value */
12628   double r;           /* Real value */
12629   union {
12630     i64 i;              /* Integer value used when MEM_Int is set in flags */
12631     int nZero;          /* Used when bit MEM_Zero is set in flags */
12632     FuncDef *pDef;      /* Used only when flags==MEM_Agg */
12633     RowSet *pRowSet;    /* Used only when flags==MEM_RowSet */
12634     VdbeFrame *pFrame;  /* Used when flags==MEM_Frame */
12635   } u;
12636   int n;              /* Number of characters in string value, excluding '\0' */
12637   u16 flags;          /* Some combination of MEM_Null, MEM_Str, MEM_Dyn, etc. */
12638   u8  type;           /* One of SQLITE_NULL, SQLITE_TEXT, SQLITE_INTEGER, etc */
12639   u8  enc;            /* SQLITE_UTF8, SQLITE_UTF16BE, SQLITE_UTF16LE */
12640 #ifdef SQLITE_DEBUG
12641   Mem *pScopyFrom;    /* This Mem is a shallow copy of pScopyFrom */
12642   void *pFiller;      /* So that sizeof(Mem) is a multiple of 8 */
12643 #endif
12644   void (*xDel)(void *);  /* If not null, call this function to delete Mem.z */
12645   char *zMalloc;      /* Dynamic buffer allocated by sqlite3_malloc() */
12646 };
12647 
12648 /* One or more of the following flags are set to indicate the validOK
12649 ** representations of the value stored in the Mem struct.
12650 **
12651 ** If the MEM_Null flag is set, then the value is an SQL NULL value.
12652 ** No other flags may be set in this case.
12653 **
12654 ** If the MEM_Str flag is set then Mem.z points at a string representation.
12655 ** Usually this is encoded in the same unicode encoding as the main
12656 ** database (see below for exceptions). If the MEM_Term flag is also
12657 ** set, then the string is nul terminated. The MEM_Int and MEM_Real
12658 ** flags may coexist with the MEM_Str flag.
12659 */
12660 #define MEM_Null      0x0001   /* Value is NULL */
12661 #define MEM_Str       0x0002   /* Value is a string */
12662 #define MEM_Int       0x0004   /* Value is an integer */
12663 #define MEM_Real      0x0008   /* Value is a real number */
12664 #define MEM_Blob      0x0010   /* Value is a BLOB */
12665 #define MEM_RowSet    0x0020   /* Value is a RowSet object */
12666 #define MEM_Frame     0x0040   /* Value is a VdbeFrame object */
12667 #define MEM_Invalid   0x0080   /* Value is undefined */
12668 #define MEM_TypeMask  0x00ff   /* Mask of type bits */
12669 
12670 /* Whenever Mem contains a valid string or blob representation, one of
12671 ** the following flags must be set to determine the memory management
12672 ** policy for Mem.z.  The MEM_Term flag tells us whether or not the
12673 ** string is \000 or \u0000 terminated
12674 */
12675 #define MEM_Term      0x0200   /* String rep is nul terminated */
12676 #define MEM_Dyn       0x0400   /* Need to call sqliteFree() on Mem.z */
12677 #define MEM_Static    0x0800   /* Mem.z points to a static string */
12678 #define MEM_Ephem     0x1000   /* Mem.z points to an ephemeral string */
12679 #define MEM_Agg       0x2000   /* Mem.z points to an agg function context */
12680 #define MEM_Zero      0x4000   /* Mem.i contains count of 0s appended to blob */
12681 #ifdef SQLITE_OMIT_INCRBLOB
12682   #undef MEM_Zero
12683   #define MEM_Zero 0x0000
12684 #endif
12685 
12686 /*
12687 ** Clear any existing type flags from a Mem and replace them with f
12688 */
12689 #define MemSetTypeFlag(p, f) \
12690    ((p)->flags = ((p)->flags&~(MEM_TypeMask|MEM_Zero))|f)
12691 
12692 /*
12693 ** Return true if a memory cell is not marked as invalid.  This macro
12694 ** is for use inside assert() statements only.
12695 */
12696 #ifdef SQLITE_DEBUG
12697 #define memIsValid(M)  ((M)->flags & MEM_Invalid)==0
12698 #endif
12699 
12700 
12701 /* A VdbeFunc is just a FuncDef (defined in sqliteInt.h) that contains
12702 ** additional information about auxiliary information bound to arguments
12703 ** of the function.  This is used to implement the sqlite3_get_auxdata()
12704 ** and sqlite3_set_auxdata() APIs.  The "auxdata" is some auxiliary data
12705 ** that can be associated with a constant argument to a function.  This
12706 ** allows functions such as "regexp" to compile their constant regular
12707 ** expression argument once and reused the compiled code for multiple
12708 ** invocations.
12709 */
12710 struct VdbeFunc {
12711   FuncDef *pFunc;               /* The definition of the function */
12712   int nAux;                     /* Number of entries allocated for apAux[] */
12713   struct AuxData {
12714     void *pAux;                   /* Aux data for the i-th argument */
12715     void (*xDelete)(void *);      /* Destructor for the aux data */
12716   } apAux[1];                   /* One slot for each function argument */
12717 };
12718 
12719 /*
12720 ** The "context" argument for a installable function.  A pointer to an
12721 ** instance of this structure is the first argument to the routines used
12722 ** implement the SQL functions.
12723 **
12724 ** There is a typedef for this structure in sqlite.h.  So all routines,
12725 ** even the public interface to SQLite, can use a pointer to this structure.
12726 ** But this file is the only place where the internal details of this
12727 ** structure are known.
12728 **
12729 ** This structure is defined inside of vdbeInt.h because it uses substructures
12730 ** (Mem) which are only defined there.
12731 */
12732 struct sqlite3_context {
12733   FuncDef *pFunc;       /* Pointer to function information.  MUST BE FIRST */
12734   VdbeFunc *pVdbeFunc;  /* Auxilary data, if created. */
12735   Mem s;                /* The return value is stored here */
12736   Mem *pMem;            /* Memory cell used to store aggregate context */
12737   int isError;          /* Error code returned by the function. */
12738   CollSeq *pColl;       /* Collating sequence */
12739 };
12740 
12741 /*
12742 ** An instance of the virtual machine.  This structure contains the complete
12743 ** state of the virtual machine.
12744 **
12745 ** The "sqlite3_stmt" structure pointer that is returned by sqlite3_prepare()
12746 ** is really a pointer to an instance of this structure.
12747 **
12748 ** The Vdbe.inVtabMethod variable is set to non-zero for the duration of
12749 ** any virtual table method invocations made by the vdbe program. It is
12750 ** set to 2 for xDestroy method calls and 1 for all other methods. This
12751 ** variable is used for two purposes: to allow xDestroy methods to execute
12752 ** "DROP TABLE" statements and to prevent some nasty side effects of
12753 ** malloc failure when SQLite is invoked recursively by a virtual table
12754 ** method function.
12755 */
12756 struct Vdbe {
12757   sqlite3 *db;            /* The database connection that owns this statement */
12758   Op *aOp;                /* Space to hold the virtual machine's program */
12759   Mem *aMem;              /* The memory locations */
12760   Mem **apArg;            /* Arguments to currently executing user function */
12761   Mem *aColName;          /* Column names to return */
12762   Mem *pResultSet;        /* Pointer to an array of results */
12763   int nMem;               /* Number of memory locations currently allocated */
12764   int nOp;                /* Number of instructions in the program */
12765   int nOpAlloc;           /* Number of slots allocated for aOp[] */
12766   int nLabel;             /* Number of labels used */
12767   int nLabelAlloc;        /* Number of slots allocated in aLabel[] */
12768   int *aLabel;            /* Space to hold the labels */
12769   u16 nResColumn;         /* Number of columns in one row of the result set */
12770   u16 nCursor;            /* Number of slots in apCsr[] */
12771   u32 magic;              /* Magic number for sanity checking */
12772   char *zErrMsg;          /* Error message written here */
12773   Vdbe *pPrev,*pNext;     /* Linked list of VDBEs with the same Vdbe.db */
12774   VdbeCursor **apCsr;     /* One element of this array for each open cursor */
12775   Mem *aVar;              /* Values for the OP_Variable opcode. */
12776   char **azVar;           /* Name of variables */
12777   ynVar nVar;             /* Number of entries in aVar[] */
12778   ynVar nzVar;            /* Number of entries in azVar[] */
12779   u32 cacheCtr;           /* VdbeCursor row cache generation counter */
12780   int pc;                 /* The program counter */
12781   int rc;                 /* Value to return */
12782   u8 errorAction;         /* Recovery action to do in case of an error */
12783   u8 explain;             /* True if EXPLAIN present on SQL command */
12784   u8 changeCntOn;         /* True to update the change-counter */
12785   u8 expired;             /* True if the VM needs to be recompiled */
12786   u8 runOnlyOnce;         /* Automatically expire on reset */
12787   u8 minWriteFileFormat;  /* Minimum file format for writable database files */
12788   u8 inVtabMethod;        /* See comments above */
12789   u8 usesStmtJournal;     /* True if uses a statement journal */
12790   u8 readOnly;            /* True for read-only statements */
12791   u8 isPrepareV2;         /* True if prepared with prepare_v2() */
12792   int nChange;            /* Number of db changes made since last reset */
12793   yDbMask btreeMask;      /* Bitmask of db->aDb[] entries referenced */
12794   yDbMask lockMask;       /* Subset of btreeMask that requires a lock */
12795   int iStatement;         /* Statement number (or 0 if has not opened stmt) */
12796   int aCounter[3];        /* Counters used by sqlite3_stmt_status() */
12797 #ifndef SQLITE_OMIT_TRACE
12798   i64 startTime;          /* Time when query started - used for profiling */
12799 #endif
12800   i64 nFkConstraint;      /* Number of imm. FK constraints this VM */
12801   i64 nStmtDefCons;       /* Number of def. constraints when stmt started */
12802   char *zSql;             /* Text of the SQL statement that generated this */
12803   void *pFree;            /* Free this when deleting the vdbe */
12804 #ifdef SQLITE_DEBUG
12805   FILE *trace;            /* Write an execution trace here, if not NULL */
12806 #endif
12807   VdbeFrame *pFrame;      /* Parent frame */
12808   VdbeFrame *pDelFrame;   /* List of frame objects to free on VM reset */
12809   int nFrame;             /* Number of frames in pFrame list */
12810   u32 expmask;            /* Binding to these vars invalidates VM */
12811   SubProgram *pProgram;   /* Linked list of all sub-programs used by VM */
12812 };
12813 
12814 /*
12815 ** The following are allowed values for Vdbe.magic
12816 */
12817 #define VDBE_MAGIC_INIT     0x26bceaa5    /* Building a VDBE program */
12818 #define VDBE_MAGIC_RUN      0xbdf20da3    /* VDBE is ready to execute */
12819 #define VDBE_MAGIC_HALT     0x519c2973    /* VDBE has completed execution */
12820 #define VDBE_MAGIC_DEAD     0xb606c3c8    /* The VDBE has been deallocated */
12821 
12822 /*
12823 ** Function prototypes
12824 */
12825 SQLITE_PRIVATE void sqlite3VdbeFreeCursor(Vdbe *, VdbeCursor*);
12826 void sqliteVdbePopStack(Vdbe*,int);
12827 SQLITE_PRIVATE int sqlite3VdbeCursorMoveto(VdbeCursor*);
12828 #if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
12829 SQLITE_PRIVATE void sqlite3VdbePrintOp(FILE*, int, Op*);
12830 #endif
12831 SQLITE_PRIVATE u32 sqlite3VdbeSerialTypeLen(u32);
12832 SQLITE_PRIVATE u32 sqlite3VdbeSerialType(Mem*, int);
12833 SQLITE_PRIVATE u32 sqlite3VdbeSerialPut(unsigned char*, int, Mem*, int);
12834 SQLITE_PRIVATE u32 sqlite3VdbeSerialGet(const unsigned char*, u32, Mem*);
12835 SQLITE_PRIVATE void sqlite3VdbeDeleteAuxData(VdbeFunc*, int);
12836 
12837 int sqlite2BtreeKeyCompare(BtCursor *, const void *, int, int, int *);
12838 SQLITE_PRIVATE int sqlite3VdbeIdxKeyCompare(VdbeCursor*,UnpackedRecord*,int*);
12839 SQLITE_PRIVATE int sqlite3VdbeIdxRowid(sqlite3*, BtCursor *, i64 *);
12840 SQLITE_PRIVATE int sqlite3MemCompare(const Mem*, const Mem*, const CollSeq*);
12841 SQLITE_PRIVATE int sqlite3VdbeExec(Vdbe*);
12842 SQLITE_PRIVATE int sqlite3VdbeList(Vdbe*);
12843 SQLITE_PRIVATE int sqlite3VdbeHalt(Vdbe*);
12844 SQLITE_PRIVATE int sqlite3VdbeChangeEncoding(Mem *, int);
12845 SQLITE_PRIVATE int sqlite3VdbeMemTooBig(Mem*);
12846 SQLITE_PRIVATE int sqlite3VdbeMemCopy(Mem*, const Mem*);
12847 SQLITE_PRIVATE void sqlite3VdbeMemShallowCopy(Mem*, const Mem*, int);
12848 SQLITE_PRIVATE void sqlite3VdbeMemMove(Mem*, Mem*);
12849 SQLITE_PRIVATE int sqlite3VdbeMemNulTerminate(Mem*);
12850 SQLITE_PRIVATE int sqlite3VdbeMemSetStr(Mem*, const char*, int, u8, void(*)(void*));
12851 SQLITE_PRIVATE void sqlite3VdbeMemSetInt64(Mem*, i64);
12852 #ifdef SQLITE_OMIT_FLOATING_POINT
12853 # define sqlite3VdbeMemSetDouble sqlite3VdbeMemSetInt64
12854 #else
12855 SQLITE_PRIVATE   void sqlite3VdbeMemSetDouble(Mem*, double);
12856 #endif
12857 SQLITE_PRIVATE void sqlite3VdbeMemSetNull(Mem*);
12858 SQLITE_PRIVATE void sqlite3VdbeMemSetZeroBlob(Mem*,int);
12859 SQLITE_PRIVATE void sqlite3VdbeMemSetRowSet(Mem*);
12860 SQLITE_PRIVATE int sqlite3VdbeMemMakeWriteable(Mem*);
12861 SQLITE_PRIVATE int sqlite3VdbeMemStringify(Mem*, int);
12862 SQLITE_PRIVATE i64 sqlite3VdbeIntValue(Mem*);
12863 SQLITE_PRIVATE int sqlite3VdbeMemIntegerify(Mem*);
12864 SQLITE_PRIVATE double sqlite3VdbeRealValue(Mem*);
12865 SQLITE_PRIVATE void sqlite3VdbeIntegerAffinity(Mem*);
12866 SQLITE_PRIVATE int sqlite3VdbeMemRealify(Mem*);
12867 SQLITE_PRIVATE int sqlite3VdbeMemNumerify(Mem*);
12868 SQLITE_PRIVATE int sqlite3VdbeMemFromBtree(BtCursor*,int,int,int,Mem*);
12869 SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p);
12870 SQLITE_PRIVATE void sqlite3VdbeMemReleaseExternal(Mem *p);
12871 SQLITE_PRIVATE int sqlite3VdbeMemFinalize(Mem*, FuncDef*);
12872 SQLITE_PRIVATE const char *sqlite3OpcodeName(int);
12873 SQLITE_PRIVATE int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve);
12874 SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *, int);
12875 SQLITE_PRIVATE void sqlite3VdbeFrameDelete(VdbeFrame*);
12876 SQLITE_PRIVATE int sqlite3VdbeFrameRestore(VdbeFrame *);
12877 SQLITE_PRIVATE void sqlite3VdbeMemStoreType(Mem *pMem);
12878 
12879 #if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
12880 SQLITE_PRIVATE   void sqlite3VdbeEnter(Vdbe*);
12881 SQLITE_PRIVATE   void sqlite3VdbeLeave(Vdbe*);
12882 #else
12883 # define sqlite3VdbeEnter(X)
12884 # define sqlite3VdbeLeave(X)
12885 #endif
12886 
12887 #ifdef SQLITE_DEBUG
12888 SQLITE_PRIVATE void sqlite3VdbeMemPrepareToChange(Vdbe*,Mem*);
12889 #endif
12890 
12891 #ifndef SQLITE_OMIT_FOREIGN_KEY
12892 SQLITE_PRIVATE int sqlite3VdbeCheckFk(Vdbe *, int);
12893 #else
12894 # define sqlite3VdbeCheckFk(p,i) 0
12895 #endif
12896 
12897 SQLITE_PRIVATE int sqlite3VdbeMemTranslate(Mem*, u8);
12898 #ifdef SQLITE_DEBUG
12899 SQLITE_PRIVATE   void sqlite3VdbePrintSql(Vdbe*);
12900 SQLITE_PRIVATE   void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf);
12901 #endif
12902 SQLITE_PRIVATE int sqlite3VdbeMemHandleBom(Mem *pMem);
12903 
12904 #ifndef SQLITE_OMIT_INCRBLOB
12905 SQLITE_PRIVATE   int sqlite3VdbeMemExpandBlob(Mem *);
12906 #else
12907   #define sqlite3VdbeMemExpandBlob(x) SQLITE_OK
12908 #endif
12909 
12910 #endif /* !defined(_VDBEINT_H_) */
12911 
12912 /************** End of vdbeInt.h *********************************************/
12913 /************** Continuing where we left off in status.c *********************/
12914 
12915 /*
12916 ** Variables in which to record status information.
12917 */
12918 typedef struct sqlite3StatType sqlite3StatType;
12919 static SQLITE_WSD struct sqlite3StatType {
12920   int nowValue[10];         /* Current value */
12921   int mxValue[10];          /* Maximum value */
12922 } sqlite3Stat = { {0,}, {0,} };
12923 
12924 
12925 /* The "wsdStat" macro will resolve to the status information
12926 ** state vector.  If writable static data is unsupported on the target,
12927 ** we have to locate the state vector at run-time.  In the more common
12928 ** case where writable static data is supported, wsdStat can refer directly
12929 ** to the "sqlite3Stat" state vector declared above.
12930 */
12931 #ifdef SQLITE_OMIT_WSD
12932 # define wsdStatInit  sqlite3StatType *x = &GLOBAL(sqlite3StatType,sqlite3Stat)
12933 # define wsdStat x[0]
12934 #else
12935 # define wsdStatInit
12936 # define wsdStat sqlite3Stat
12937 #endif
12938 
12939 /*
12940 ** Return the current value of a status parameter.
12941 */
sqlite3StatusValue(int op)12942 SQLITE_PRIVATE int sqlite3StatusValue(int op){
12943   wsdStatInit;
12944   assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
12945   return wsdStat.nowValue[op];
12946 }
12947 
12948 /*
12949 ** Add N to the value of a status record.  It is assumed that the
12950 ** caller holds appropriate locks.
12951 */
sqlite3StatusAdd(int op,int N)12952 SQLITE_PRIVATE void sqlite3StatusAdd(int op, int N){
12953   wsdStatInit;
12954   assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
12955   wsdStat.nowValue[op] += N;
12956   if( wsdStat.nowValue[op]>wsdStat.mxValue[op] ){
12957     wsdStat.mxValue[op] = wsdStat.nowValue[op];
12958   }
12959 }
12960 
12961 /*
12962 ** Set the value of a status to X.
12963 */
sqlite3StatusSet(int op,int X)12964 SQLITE_PRIVATE void sqlite3StatusSet(int op, int X){
12965   wsdStatInit;
12966   assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
12967   wsdStat.nowValue[op] = X;
12968   if( wsdStat.nowValue[op]>wsdStat.mxValue[op] ){
12969     wsdStat.mxValue[op] = wsdStat.nowValue[op];
12970   }
12971 }
12972 
12973 /*
12974 ** Query status information.
12975 **
12976 ** This implementation assumes that reading or writing an aligned
12977 ** 32-bit integer is an atomic operation.  If that assumption is not true,
12978 ** then this routine is not threadsafe.
12979 */
sqlite3_status(int op,int * pCurrent,int * pHighwater,int resetFlag)12980 SQLITE_API int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag){
12981   wsdStatInit;
12982   if( op<0 || op>=ArraySize(wsdStat.nowValue) ){
12983     return SQLITE_MISUSE_BKPT;
12984   }
12985   *pCurrent = wsdStat.nowValue[op];
12986   *pHighwater = wsdStat.mxValue[op];
12987   if( resetFlag ){
12988     wsdStat.mxValue[op] = wsdStat.nowValue[op];
12989   }
12990   return SQLITE_OK;
12991 }
12992 
12993 /*
12994 ** Query status information for a single database connection
12995 */
sqlite3_db_status(sqlite3 * db,int op,int * pCurrent,int * pHighwater,int resetFlag)12996 SQLITE_API int sqlite3_db_status(
12997   sqlite3 *db,          /* The database connection whose status is desired */
12998   int op,               /* Status verb */
12999   int *pCurrent,        /* Write current value here */
13000   int *pHighwater,      /* Write high-water mark here */
13001   int resetFlag         /* Reset high-water mark if true */
13002 ){
13003   int rc = SQLITE_OK;   /* Return code */
13004   sqlite3_mutex_enter(db->mutex);
13005   switch( op ){
13006     case SQLITE_DBSTATUS_LOOKASIDE_USED: {
13007       *pCurrent = db->lookaside.nOut;
13008       *pHighwater = db->lookaside.mxOut;
13009       if( resetFlag ){
13010         db->lookaside.mxOut = db->lookaside.nOut;
13011       }
13012       break;
13013     }
13014 
13015     case SQLITE_DBSTATUS_LOOKASIDE_HIT:
13016     case SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE:
13017     case SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL: {
13018       testcase( op==SQLITE_DBSTATUS_LOOKASIDE_HIT );
13019       testcase( op==SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE );
13020       testcase( op==SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL );
13021       assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)>=0 );
13022       assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)<3 );
13023       *pCurrent = 0;
13024       *pHighwater = db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT];
13025       if( resetFlag ){
13026         db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT] = 0;
13027       }
13028       break;
13029     }
13030 
13031     /*
13032     ** Return an approximation for the amount of memory currently used
13033     ** by all pagers associated with the given database connection.  The
13034     ** highwater mark is meaningless and is returned as zero.
13035     */
13036     case SQLITE_DBSTATUS_CACHE_USED: {
13037       int totalUsed = 0;
13038       int i;
13039       sqlite3BtreeEnterAll(db);
13040       for(i=0; i<db->nDb; i++){
13041         Btree *pBt = db->aDb[i].pBt;
13042         if( pBt ){
13043           Pager *pPager = sqlite3BtreePager(pBt);
13044           totalUsed += sqlite3PagerMemUsed(pPager);
13045         }
13046       }
13047       sqlite3BtreeLeaveAll(db);
13048       *pCurrent = totalUsed;
13049       *pHighwater = 0;
13050       break;
13051     }
13052 
13053     /*
13054     ** *pCurrent gets an accurate estimate of the amount of memory used
13055     ** to store the schema for all databases (main, temp, and any ATTACHed
13056     ** databases.  *pHighwater is set to zero.
13057     */
13058     case SQLITE_DBSTATUS_SCHEMA_USED: {
13059       int i;                      /* Used to iterate through schemas */
13060       int nByte = 0;              /* Used to accumulate return value */
13061 
13062       sqlite3BtreeEnterAll(db);
13063       db->pnBytesFreed = &nByte;
13064       for(i=0; i<db->nDb; i++){
13065         Schema *pSchema = db->aDb[i].pSchema;
13066         if( ALWAYS(pSchema!=0) ){
13067           HashElem *p;
13068 
13069           nByte += sqlite3GlobalConfig.m.xRoundup(sizeof(HashElem)) * (
13070               pSchema->tblHash.count
13071             + pSchema->trigHash.count
13072             + pSchema->idxHash.count
13073             + pSchema->fkeyHash.count
13074           );
13075           nByte += sqlite3MallocSize(pSchema->tblHash.ht);
13076           nByte += sqlite3MallocSize(pSchema->trigHash.ht);
13077           nByte += sqlite3MallocSize(pSchema->idxHash.ht);
13078           nByte += sqlite3MallocSize(pSchema->fkeyHash.ht);
13079 
13080           for(p=sqliteHashFirst(&pSchema->trigHash); p; p=sqliteHashNext(p)){
13081             sqlite3DeleteTrigger(db, (Trigger*)sqliteHashData(p));
13082           }
13083           for(p=sqliteHashFirst(&pSchema->tblHash); p; p=sqliteHashNext(p)){
13084             sqlite3DeleteTable(db, (Table *)sqliteHashData(p));
13085           }
13086         }
13087       }
13088       db->pnBytesFreed = 0;
13089       sqlite3BtreeLeaveAll(db);
13090 
13091       *pHighwater = 0;
13092       *pCurrent = nByte;
13093       break;
13094     }
13095 
13096     /*
13097     ** *pCurrent gets an accurate estimate of the amount of memory used
13098     ** to store all prepared statements.
13099     ** *pHighwater is set to zero.
13100     */
13101     case SQLITE_DBSTATUS_STMT_USED: {
13102       struct Vdbe *pVdbe;         /* Used to iterate through VMs */
13103       int nByte = 0;              /* Used to accumulate return value */
13104 
13105       db->pnBytesFreed = &nByte;
13106       for(pVdbe=db->pVdbe; pVdbe; pVdbe=pVdbe->pNext){
13107         sqlite3VdbeDeleteObject(db, pVdbe);
13108       }
13109       db->pnBytesFreed = 0;
13110 
13111       *pHighwater = 0;
13112       *pCurrent = nByte;
13113 
13114       break;
13115     }
13116 
13117     default: {
13118       rc = SQLITE_ERROR;
13119     }
13120   }
13121   sqlite3_mutex_leave(db->mutex);
13122   return rc;
13123 }
13124 
13125 /************** End of status.c **********************************************/
13126 /************** Begin file date.c ********************************************/
13127 /*
13128 ** 2003 October 31
13129 **
13130 ** The author disclaims copyright to this source code.  In place of
13131 ** a legal notice, here is a blessing:
13132 **
13133 **    May you do good and not evil.
13134 **    May you find forgiveness for yourself and forgive others.
13135 **    May you share freely, never taking more than you give.
13136 **
13137 *************************************************************************
13138 ** This file contains the C functions that implement date and time
13139 ** functions for SQLite.
13140 **
13141 ** There is only one exported symbol in this file - the function
13142 ** sqlite3RegisterDateTimeFunctions() found at the bottom of the file.
13143 ** All other code has file scope.
13144 **
13145 ** SQLite processes all times and dates as Julian Day numbers.  The
13146 ** dates and times are stored as the number of days since noon
13147 ** in Greenwich on November 24, 4714 B.C. according to the Gregorian
13148 ** calendar system.
13149 **
13150 ** 1970-01-01 00:00:00 is JD 2440587.5
13151 ** 2000-01-01 00:00:00 is JD 2451544.5
13152 **
13153 ** This implemention requires years to be expressed as a 4-digit number
13154 ** which means that only dates between 0000-01-01 and 9999-12-31 can
13155 ** be represented, even though julian day numbers allow a much wider
13156 ** range of dates.
13157 **
13158 ** The Gregorian calendar system is used for all dates and times,
13159 ** even those that predate the Gregorian calendar.  Historians usually
13160 ** use the Julian calendar for dates prior to 1582-10-15 and for some
13161 ** dates afterwards, depending on locale.  Beware of this difference.
13162 **
13163 ** The conversion algorithms are implemented based on descriptions
13164 ** in the following text:
13165 **
13166 **      Jean Meeus
13167 **      Astronomical Algorithms, 2nd Edition, 1998
13168 **      ISBM 0-943396-61-1
13169 **      Willmann-Bell, Inc
13170 **      Richmond, Virginia (USA)
13171 */
13172 #include <time.h>
13173 
13174 #ifndef SQLITE_OMIT_DATETIME_FUNCS
13175 
13176 
13177 /*
13178 ** A structure for holding a single date and time.
13179 */
13180 typedef struct DateTime DateTime;
13181 struct DateTime {
13182   sqlite3_int64 iJD; /* The julian day number times 86400000 */
13183   int Y, M, D;       /* Year, month, and day */
13184   int h, m;          /* Hour and minutes */
13185   int tz;            /* Timezone offset in minutes */
13186   double s;          /* Seconds */
13187   char validYMD;     /* True (1) if Y,M,D are valid */
13188   char validHMS;     /* True (1) if h,m,s are valid */
13189   char validJD;      /* True (1) if iJD is valid */
13190   char validTZ;      /* True (1) if tz is valid */
13191 };
13192 
13193 
13194 /*
13195 ** Convert zDate into one or more integers.  Additional arguments
13196 ** come in groups of 5 as follows:
13197 **
13198 **       N       number of digits in the integer
13199 **       min     minimum allowed value of the integer
13200 **       max     maximum allowed value of the integer
13201 **       nextC   first character after the integer
13202 **       pVal    where to write the integers value.
13203 **
13204 ** Conversions continue until one with nextC==0 is encountered.
13205 ** The function returns the number of successful conversions.
13206 */
getDigits(const char * zDate,...)13207 static int getDigits(const char *zDate, ...){
13208   va_list ap;
13209   int val;
13210   int N;
13211   int min;
13212   int max;
13213   int nextC;
13214   int *pVal;
13215   int cnt = 0;
13216   va_start(ap, zDate);
13217   do{
13218     N = va_arg(ap, int);
13219     min = va_arg(ap, int);
13220     max = va_arg(ap, int);
13221     nextC = va_arg(ap, int);
13222     pVal = va_arg(ap, int*);
13223     val = 0;
13224     while( N-- ){
13225       if( !sqlite3Isdigit(*zDate) ){
13226         goto end_getDigits;
13227       }
13228       val = val*10 + *zDate - '0';
13229       zDate++;
13230     }
13231     if( val<min || val>max || (nextC!=0 && nextC!=*zDate) ){
13232       goto end_getDigits;
13233     }
13234     *pVal = val;
13235     zDate++;
13236     cnt++;
13237   }while( nextC );
13238 end_getDigits:
13239   va_end(ap);
13240   return cnt;
13241 }
13242 
13243 /*
13244 ** Parse a timezone extension on the end of a date-time.
13245 ** The extension is of the form:
13246 **
13247 **        (+/-)HH:MM
13248 **
13249 ** Or the "zulu" notation:
13250 **
13251 **        Z
13252 **
13253 ** If the parse is successful, write the number of minutes
13254 ** of change in p->tz and return 0.  If a parser error occurs,
13255 ** return non-zero.
13256 **
13257 ** A missing specifier is not considered an error.
13258 */
parseTimezone(const char * zDate,DateTime * p)13259 static int parseTimezone(const char *zDate, DateTime *p){
13260   int sgn = 0;
13261   int nHr, nMn;
13262   int c;
13263   while( sqlite3Isspace(*zDate) ){ zDate++; }
13264   p->tz = 0;
13265   c = *zDate;
13266   if( c=='-' ){
13267     sgn = -1;
13268   }else if( c=='+' ){
13269     sgn = +1;
13270   }else if( c=='Z' || c=='z' ){
13271     zDate++;
13272     goto zulu_time;
13273   }else{
13274     return c!=0;
13275   }
13276   zDate++;
13277   if( getDigits(zDate, 2, 0, 14, ':', &nHr, 2, 0, 59, 0, &nMn)!=2 ){
13278     return 1;
13279   }
13280   zDate += 5;
13281   p->tz = sgn*(nMn + nHr*60);
13282 zulu_time:
13283   while( sqlite3Isspace(*zDate) ){ zDate++; }
13284   return *zDate!=0;
13285 }
13286 
13287 /*
13288 ** Parse times of the form HH:MM or HH:MM:SS or HH:MM:SS.FFFF.
13289 ** The HH, MM, and SS must each be exactly 2 digits.  The
13290 ** fractional seconds FFFF can be one or more digits.
13291 **
13292 ** Return 1 if there is a parsing error and 0 on success.
13293 */
parseHhMmSs(const char * zDate,DateTime * p)13294 static int parseHhMmSs(const char *zDate, DateTime *p){
13295   int h, m, s;
13296   double ms = 0.0;
13297   if( getDigits(zDate, 2, 0, 24, ':', &h, 2, 0, 59, 0, &m)!=2 ){
13298     return 1;
13299   }
13300   zDate += 5;
13301   if( *zDate==':' ){
13302     zDate++;
13303     if( getDigits(zDate, 2, 0, 59, 0, &s)!=1 ){
13304       return 1;
13305     }
13306     zDate += 2;
13307     if( *zDate=='.' && sqlite3Isdigit(zDate[1]) ){
13308       double rScale = 1.0;
13309       zDate++;
13310       while( sqlite3Isdigit(*zDate) ){
13311         ms = ms*10.0 + *zDate - '0';
13312         rScale *= 10.0;
13313         zDate++;
13314       }
13315       ms /= rScale;
13316     }
13317   }else{
13318     s = 0;
13319   }
13320   p->validJD = 0;
13321   p->validHMS = 1;
13322   p->h = h;
13323   p->m = m;
13324   p->s = s + ms;
13325   if( parseTimezone(zDate, p) ) return 1;
13326   p->validTZ = (p->tz!=0)?1:0;
13327   return 0;
13328 }
13329 
13330 /*
13331 ** Convert from YYYY-MM-DD HH:MM:SS to julian day.  We always assume
13332 ** that the YYYY-MM-DD is according to the Gregorian calendar.
13333 **
13334 ** Reference:  Meeus page 61
13335 */
computeJD(DateTime * p)13336 static void computeJD(DateTime *p){
13337   int Y, M, D, A, B, X1, X2;
13338 
13339   if( p->validJD ) return;
13340   if( p->validYMD ){
13341     Y = p->Y;
13342     M = p->M;
13343     D = p->D;
13344   }else{
13345     Y = 2000;  /* If no YMD specified, assume 2000-Jan-01 */
13346     M = 1;
13347     D = 1;
13348   }
13349   if( M<=2 ){
13350     Y--;
13351     M += 12;
13352   }
13353   A = Y/100;
13354   B = 2 - A + (A/4);
13355   X1 = 36525*(Y+4716)/100;
13356   X2 = 306001*(M+1)/10000;
13357   p->iJD = (sqlite3_int64)((X1 + X2 + D + B - 1524.5 ) * 86400000);
13358   p->validJD = 1;
13359   if( p->validHMS ){
13360     p->iJD += p->h*3600000 + p->m*60000 + (sqlite3_int64)(p->s*1000);
13361     if( p->validTZ ){
13362       p->iJD -= p->tz*60000;
13363       p->validYMD = 0;
13364       p->validHMS = 0;
13365       p->validTZ = 0;
13366     }
13367   }
13368 }
13369 
13370 /*
13371 ** Parse dates of the form
13372 **
13373 **     YYYY-MM-DD HH:MM:SS.FFF
13374 **     YYYY-MM-DD HH:MM:SS
13375 **     YYYY-MM-DD HH:MM
13376 **     YYYY-MM-DD
13377 **
13378 ** Write the result into the DateTime structure and return 0
13379 ** on success and 1 if the input string is not a well-formed
13380 ** date.
13381 */
parseYyyyMmDd(const char * zDate,DateTime * p)13382 static int parseYyyyMmDd(const char *zDate, DateTime *p){
13383   int Y, M, D, neg;
13384 
13385   if( zDate[0]=='-' ){
13386     zDate++;
13387     neg = 1;
13388   }else{
13389     neg = 0;
13390   }
13391   if( getDigits(zDate,4,0,9999,'-',&Y,2,1,12,'-',&M,2,1,31,0,&D)!=3 ){
13392     return 1;
13393   }
13394   zDate += 10;
13395   while( sqlite3Isspace(*zDate) || 'T'==*(u8*)zDate ){ zDate++; }
13396   if( parseHhMmSs(zDate, p)==0 ){
13397     /* We got the time */
13398   }else if( *zDate==0 ){
13399     p->validHMS = 0;
13400   }else{
13401     return 1;
13402   }
13403   p->validJD = 0;
13404   p->validYMD = 1;
13405   p->Y = neg ? -Y : Y;
13406   p->M = M;
13407   p->D = D;
13408   if( p->validTZ ){
13409     computeJD(p);
13410   }
13411   return 0;
13412 }
13413 
13414 /*
13415 ** Set the time to the current time reported by the VFS
13416 */
setDateTimeToCurrent(sqlite3_context * context,DateTime * p)13417 static void setDateTimeToCurrent(sqlite3_context *context, DateTime *p){
13418   sqlite3 *db = sqlite3_context_db_handle(context);
13419   sqlite3OsCurrentTimeInt64(db->pVfs, &p->iJD);
13420   p->validJD = 1;
13421 }
13422 
13423 /*
13424 ** Attempt to parse the given string into a Julian Day Number.  Return
13425 ** the number of errors.
13426 **
13427 ** The following are acceptable forms for the input string:
13428 **
13429 **      YYYY-MM-DD HH:MM:SS.FFF  +/-HH:MM
13430 **      DDDD.DD
13431 **      now
13432 **
13433 ** In the first form, the +/-HH:MM is always optional.  The fractional
13434 ** seconds extension (the ".FFF") is optional.  The seconds portion
13435 ** (":SS.FFF") is option.  The year and date can be omitted as long
13436 ** as there is a time string.  The time string can be omitted as long
13437 ** as there is a year and date.
13438 */
parseDateOrTime(sqlite3_context * context,const char * zDate,DateTime * p)13439 static int parseDateOrTime(
13440   sqlite3_context *context,
13441   const char *zDate,
13442   DateTime *p
13443 ){
13444   double r;
13445   if( parseYyyyMmDd(zDate,p)==0 ){
13446     return 0;
13447   }else if( parseHhMmSs(zDate, p)==0 ){
13448     return 0;
13449   }else if( sqlite3StrICmp(zDate,"now")==0){
13450     setDateTimeToCurrent(context, p);
13451     return 0;
13452   }else if( sqlite3AtoF(zDate, &r, sqlite3Strlen30(zDate), SQLITE_UTF8) ){
13453     p->iJD = (sqlite3_int64)(r*86400000.0 + 0.5);
13454     p->validJD = 1;
13455     return 0;
13456   }
13457   return 1;
13458 }
13459 
13460 /*
13461 ** Compute the Year, Month, and Day from the julian day number.
13462 */
computeYMD(DateTime * p)13463 static void computeYMD(DateTime *p){
13464   int Z, A, B, C, D, E, X1;
13465   if( p->validYMD ) return;
13466   if( !p->validJD ){
13467     p->Y = 2000;
13468     p->M = 1;
13469     p->D = 1;
13470   }else{
13471     Z = (int)((p->iJD + 43200000)/86400000);
13472     A = (int)((Z - 1867216.25)/36524.25);
13473     A = Z + 1 + A - (A/4);
13474     B = A + 1524;
13475     C = (int)((B - 122.1)/365.25);
13476     D = (36525*C)/100;
13477     E = (int)((B-D)/30.6001);
13478     X1 = (int)(30.6001*E);
13479     p->D = B - D - X1;
13480     p->M = E<14 ? E-1 : E-13;
13481     p->Y = p->M>2 ? C - 4716 : C - 4715;
13482   }
13483   p->validYMD = 1;
13484 }
13485 
13486 /*
13487 ** Compute the Hour, Minute, and Seconds from the julian day number.
13488 */
computeHMS(DateTime * p)13489 static void computeHMS(DateTime *p){
13490   int s;
13491   if( p->validHMS ) return;
13492   computeJD(p);
13493   s = (int)((p->iJD + 43200000) % 86400000);
13494   p->s = s/1000.0;
13495   s = (int)p->s;
13496   p->s -= s;
13497   p->h = s/3600;
13498   s -= p->h*3600;
13499   p->m = s/60;
13500   p->s += s - p->m*60;
13501   p->validHMS = 1;
13502 }
13503 
13504 /*
13505 ** Compute both YMD and HMS
13506 */
computeYMD_HMS(DateTime * p)13507 static void computeYMD_HMS(DateTime *p){
13508   computeYMD(p);
13509   computeHMS(p);
13510 }
13511 
13512 /*
13513 ** Clear the YMD and HMS and the TZ
13514 */
clearYMD_HMS_TZ(DateTime * p)13515 static void clearYMD_HMS_TZ(DateTime *p){
13516   p->validYMD = 0;
13517   p->validHMS = 0;
13518   p->validTZ = 0;
13519 }
13520 
13521 /*
13522 ** On recent Windows platforms, the localtime_s() function is available
13523 ** as part of the "Secure CRT". It is essentially equivalent to
13524 ** localtime_r() available under most POSIX platforms, except that the
13525 ** order of the parameters is reversed.
13526 **
13527 ** See http://msdn.microsoft.com/en-us/library/a442x3ye(VS.80).aspx.
13528 **
13529 ** If the user has not indicated to use localtime_r() or localtime_s()
13530 ** already, check for an MSVC build environment that provides
13531 ** localtime_s().
13532 */
13533 #if !defined(HAVE_LOCALTIME_R) && !defined(HAVE_LOCALTIME_S) && \
13534      defined(_MSC_VER) && defined(_CRT_INSECURE_DEPRECATE)
13535 #define HAVE_LOCALTIME_S 1
13536 #endif
13537 
13538 #ifndef SQLITE_OMIT_LOCALTIME
13539 /*
13540 ** The following routine implements the rough equivalent of localtime_r()
13541 ** using whatever operating-system specific localtime facility that
13542 ** is available.  This routine returns 0 on success and
13543 ** non-zero on any kind of error.
13544 **
13545 ** If the sqlite3GlobalConfig.bLocaltimeFault variable is true then this
13546 ** routine will always fail.
13547 */
osLocaltime(time_t * t,struct tm * pTm)13548 static int osLocaltime(time_t *t, struct tm *pTm){
13549   int rc;
13550 #if (!defined(HAVE_LOCALTIME_R) || !HAVE_LOCALTIME_R) \
13551       && (!defined(HAVE_LOCALTIME_S) || !HAVE_LOCALTIME_S)
13552   struct tm *pX;
13553   sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
13554   sqlite3_mutex_enter(mutex);
13555   pX = localtime(t);
13556 #ifndef SQLITE_OMIT_BUILTIN_TEST
13557   if( sqlite3GlobalConfig.bLocaltimeFault ) pX = 0;
13558 #endif
13559   if( pX ) *pTm = *pX;
13560   sqlite3_mutex_leave(mutex);
13561   rc = pX==0;
13562 #else
13563 #ifndef SQLITE_OMIT_BUILTIN_TEST
13564   if( sqlite3GlobalConfig.bLocaltimeFault ) return 1;
13565 #endif
13566 #if defined(HAVE_LOCALTIME_R) && HAVE_LOCALTIME_R
13567   rc = localtime_r(t, pTm)==0;
13568 #else
13569   rc = localtime_s(pTm, t);
13570 #endif /* HAVE_LOCALTIME_R */
13571 #endif /* HAVE_LOCALTIME_R || HAVE_LOCALTIME_S */
13572   return rc;
13573 }
13574 #endif /* SQLITE_OMIT_LOCALTIME */
13575 
13576 
13577 #ifndef SQLITE_OMIT_LOCALTIME
13578 /*
13579 ** Compute the difference (in milliseconds) between localtime and UTC
13580 ** (a.k.a. GMT) for the time value p where p is in UTC. If no error occurs,
13581 ** return this value and set *pRc to SQLITE_OK.
13582 **
13583 ** Or, if an error does occur, set *pRc to SQLITE_ERROR. The returned value
13584 ** is undefined in this case.
13585 */
localtimeOffset(DateTime * p,sqlite3_context * pCtx,int * pRc)13586 static sqlite3_int64 localtimeOffset(
13587   DateTime *p,                    /* Date at which to calculate offset */
13588   sqlite3_context *pCtx,          /* Write error here if one occurs */
13589   int *pRc                        /* OUT: Error code. SQLITE_OK or ERROR */
13590 ){
13591   DateTime x, y;
13592   time_t t;
13593   struct tm sLocal;
13594 
13595   /* Initialize the contents of sLocal to avoid a compiler warning. */
13596   memset(&sLocal, 0, sizeof(sLocal));
13597 
13598   x = *p;
13599   computeYMD_HMS(&x);
13600   if( x.Y<1971 || x.Y>=2038 ){
13601     x.Y = 2000;
13602     x.M = 1;
13603     x.D = 1;
13604     x.h = 0;
13605     x.m = 0;
13606     x.s = 0.0;
13607   } else {
13608     int s = (int)(x.s + 0.5);
13609     x.s = s;
13610   }
13611   x.tz = 0;
13612   x.validJD = 0;
13613   computeJD(&x);
13614   t = (time_t)(x.iJD/1000 - 21086676*(i64)10000);
13615   if( osLocaltime(&t, &sLocal) ){
13616     sqlite3_result_error(pCtx, "local time unavailable", -1);
13617     *pRc = SQLITE_ERROR;
13618     return 0;
13619   }
13620   y.Y = sLocal.tm_year + 1900;
13621   y.M = sLocal.tm_mon + 1;
13622   y.D = sLocal.tm_mday;
13623   y.h = sLocal.tm_hour;
13624   y.m = sLocal.tm_min;
13625   y.s = sLocal.tm_sec;
13626   y.validYMD = 1;
13627   y.validHMS = 1;
13628   y.validJD = 0;
13629   y.validTZ = 0;
13630   computeJD(&y);
13631   *pRc = SQLITE_OK;
13632   return y.iJD - x.iJD;
13633 }
13634 #endif /* SQLITE_OMIT_LOCALTIME */
13635 
13636 /*
13637 ** Process a modifier to a date-time stamp.  The modifiers are
13638 ** as follows:
13639 **
13640 **     NNN days
13641 **     NNN hours
13642 **     NNN minutes
13643 **     NNN.NNNN seconds
13644 **     NNN months
13645 **     NNN years
13646 **     start of month
13647 **     start of year
13648 **     start of week
13649 **     start of day
13650 **     weekday N
13651 **     unixepoch
13652 **     localtime
13653 **     utc
13654 **
13655 ** Return 0 on success and 1 if there is any kind of error. If the error
13656 ** is in a system call (i.e. localtime()), then an error message is written
13657 ** to context pCtx. If the error is an unrecognized modifier, no error is
13658 ** written to pCtx.
13659 */
parseModifier(sqlite3_context * pCtx,const char * zMod,DateTime * p)13660 static int parseModifier(sqlite3_context *pCtx, const char *zMod, DateTime *p){
13661   int rc = 1;
13662   int n;
13663   double r;
13664   char *z, zBuf[30];
13665   z = zBuf;
13666   for(n=0; n<ArraySize(zBuf)-1 && zMod[n]; n++){
13667     z[n] = (char)sqlite3UpperToLower[(u8)zMod[n]];
13668   }
13669   z[n] = 0;
13670   switch( z[0] ){
13671 #ifndef SQLITE_OMIT_LOCALTIME
13672     case 'l': {
13673       /*    localtime
13674       **
13675       ** Assuming the current time value is UTC (a.k.a. GMT), shift it to
13676       ** show local time.
13677       */
13678       if( strcmp(z, "localtime")==0 ){
13679         computeJD(p);
13680         p->iJD += localtimeOffset(p, pCtx, &rc);
13681         clearYMD_HMS_TZ(p);
13682       }
13683       break;
13684     }
13685 #endif
13686     case 'u': {
13687       /*
13688       **    unixepoch
13689       **
13690       ** Treat the current value of p->iJD as the number of
13691       ** seconds since 1970.  Convert to a real julian day number.
13692       */
13693       if( strcmp(z, "unixepoch")==0 && p->validJD ){
13694         p->iJD = (p->iJD + 43200)/86400 + 21086676*(i64)10000000;
13695         clearYMD_HMS_TZ(p);
13696         rc = 0;
13697       }
13698 #ifndef SQLITE_OMIT_LOCALTIME
13699       else if( strcmp(z, "utc")==0 ){
13700         sqlite3_int64 c1;
13701         computeJD(p);
13702         c1 = localtimeOffset(p, pCtx, &rc);
13703         if( rc==SQLITE_OK ){
13704           p->iJD -= c1;
13705           clearYMD_HMS_TZ(p);
13706           p->iJD += c1 - localtimeOffset(p, pCtx, &rc);
13707         }
13708       }
13709 #endif
13710       break;
13711     }
13712     case 'w': {
13713       /*
13714       **    weekday N
13715       **
13716       ** Move the date to the same time on the next occurrence of
13717       ** weekday N where 0==Sunday, 1==Monday, and so forth.  If the
13718       ** date is already on the appropriate weekday, this is a no-op.
13719       */
13720       if( strncmp(z, "weekday ", 8)==0
13721                && sqlite3AtoF(&z[8], &r, sqlite3Strlen30(&z[8]), SQLITE_UTF8)
13722                && (n=(int)r)==r && n>=0 && r<7 ){
13723         sqlite3_int64 Z;
13724         computeYMD_HMS(p);
13725         p->validTZ = 0;
13726         p->validJD = 0;
13727         computeJD(p);
13728         Z = ((p->iJD + 129600000)/86400000) % 7;
13729         if( Z>n ) Z -= 7;
13730         p->iJD += (n - Z)*86400000;
13731         clearYMD_HMS_TZ(p);
13732         rc = 0;
13733       }
13734       break;
13735     }
13736     case 's': {
13737       /*
13738       **    start of TTTTT
13739       **
13740       ** Move the date backwards to the beginning of the current day,
13741       ** or month or year.
13742       */
13743       if( strncmp(z, "start of ", 9)!=0 ) break;
13744       z += 9;
13745       computeYMD(p);
13746       p->validHMS = 1;
13747       p->h = p->m = 0;
13748       p->s = 0.0;
13749       p->validTZ = 0;
13750       p->validJD = 0;
13751       if( strcmp(z,"month")==0 ){
13752         p->D = 1;
13753         rc = 0;
13754       }else if( strcmp(z,"year")==0 ){
13755         computeYMD(p);
13756         p->M = 1;
13757         p->D = 1;
13758         rc = 0;
13759       }else if( strcmp(z,"day")==0 ){
13760         rc = 0;
13761       }
13762       break;
13763     }
13764     case '+':
13765     case '-':
13766     case '0':
13767     case '1':
13768     case '2':
13769     case '3':
13770     case '4':
13771     case '5':
13772     case '6':
13773     case '7':
13774     case '8':
13775     case '9': {
13776       double rRounder;
13777       for(n=1; z[n] && z[n]!=':' && !sqlite3Isspace(z[n]); n++){}
13778       if( !sqlite3AtoF(z, &r, n, SQLITE_UTF8) ){
13779         rc = 1;
13780         break;
13781       }
13782       if( z[n]==':' ){
13783         /* A modifier of the form (+|-)HH:MM:SS.FFF adds (or subtracts) the
13784         ** specified number of hours, minutes, seconds, and fractional seconds
13785         ** to the time.  The ".FFF" may be omitted.  The ":SS.FFF" may be
13786         ** omitted.
13787         */
13788         const char *z2 = z;
13789         DateTime tx;
13790         sqlite3_int64 day;
13791         if( !sqlite3Isdigit(*z2) ) z2++;
13792         memset(&tx, 0, sizeof(tx));
13793         if( parseHhMmSs(z2, &tx) ) break;
13794         computeJD(&tx);
13795         tx.iJD -= 43200000;
13796         day = tx.iJD/86400000;
13797         tx.iJD -= day*86400000;
13798         if( z[0]=='-' ) tx.iJD = -tx.iJD;
13799         computeJD(p);
13800         clearYMD_HMS_TZ(p);
13801         p->iJD += tx.iJD;
13802         rc = 0;
13803         break;
13804       }
13805       z += n;
13806       while( sqlite3Isspace(*z) ) z++;
13807       n = sqlite3Strlen30(z);
13808       if( n>10 || n<3 ) break;
13809       if( z[n-1]=='s' ){ z[n-1] = 0; n--; }
13810       computeJD(p);
13811       rc = 0;
13812       rRounder = r<0 ? -0.5 : +0.5;
13813       if( n==3 && strcmp(z,"day")==0 ){
13814         p->iJD += (sqlite3_int64)(r*86400000.0 + rRounder);
13815       }else if( n==4 && strcmp(z,"hour")==0 ){
13816         p->iJD += (sqlite3_int64)(r*(86400000.0/24.0) + rRounder);
13817       }else if( n==6 && strcmp(z,"minute")==0 ){
13818         p->iJD += (sqlite3_int64)(r*(86400000.0/(24.0*60.0)) + rRounder);
13819       }else if( n==6 && strcmp(z,"second")==0 ){
13820         p->iJD += (sqlite3_int64)(r*(86400000.0/(24.0*60.0*60.0)) + rRounder);
13821       }else if( n==5 && strcmp(z,"month")==0 ){
13822         int x, y;
13823         computeYMD_HMS(p);
13824         p->M += (int)r;
13825         x = p->M>0 ? (p->M-1)/12 : (p->M-12)/12;
13826         p->Y += x;
13827         p->M -= x*12;
13828         p->validJD = 0;
13829         computeJD(p);
13830         y = (int)r;
13831         if( y!=r ){
13832           p->iJD += (sqlite3_int64)((r - y)*30.0*86400000.0 + rRounder);
13833         }
13834       }else if( n==4 && strcmp(z,"year")==0 ){
13835         int y = (int)r;
13836         computeYMD_HMS(p);
13837         p->Y += y;
13838         p->validJD = 0;
13839         computeJD(p);
13840         if( y!=r ){
13841           p->iJD += (sqlite3_int64)((r - y)*365.0*86400000.0 + rRounder);
13842         }
13843       }else{
13844         rc = 1;
13845       }
13846       clearYMD_HMS_TZ(p);
13847       break;
13848     }
13849     default: {
13850       break;
13851     }
13852   }
13853   return rc;
13854 }
13855 
13856 /*
13857 ** Process time function arguments.  argv[0] is a date-time stamp.
13858 ** argv[1] and following are modifiers.  Parse them all and write
13859 ** the resulting time into the DateTime structure p.  Return 0
13860 ** on success and 1 if there are any errors.
13861 **
13862 ** If there are zero parameters (if even argv[0] is undefined)
13863 ** then assume a default value of "now" for argv[0].
13864 */
isDate(sqlite3_context * context,int argc,sqlite3_value ** argv,DateTime * p)13865 static int isDate(
13866   sqlite3_context *context,
13867   int argc,
13868   sqlite3_value **argv,
13869   DateTime *p
13870 ){
13871   int i;
13872   const unsigned char *z;
13873   int eType;
13874   memset(p, 0, sizeof(*p));
13875   if( argc==0 ){
13876     setDateTimeToCurrent(context, p);
13877   }else if( (eType = sqlite3_value_type(argv[0]))==SQLITE_FLOAT
13878                    || eType==SQLITE_INTEGER ){
13879     p->iJD = (sqlite3_int64)(sqlite3_value_double(argv[0])*86400000.0 + 0.5);
13880     p->validJD = 1;
13881   }else{
13882     z = sqlite3_value_text(argv[0]);
13883     if( !z || parseDateOrTime(context, (char*)z, p) ){
13884       return 1;
13885     }
13886   }
13887   for(i=1; i<argc; i++){
13888     z = sqlite3_value_text(argv[i]);
13889     if( z==0 || parseModifier(context, (char*)z, p) ) return 1;
13890   }
13891   return 0;
13892 }
13893 
13894 
13895 /*
13896 ** The following routines implement the various date and time functions
13897 ** of SQLite.
13898 */
13899 
13900 /*
13901 **    julianday( TIMESTRING, MOD, MOD, ...)
13902 **
13903 ** Return the julian day number of the date specified in the arguments
13904 */
juliandayFunc(sqlite3_context * context,int argc,sqlite3_value ** argv)13905 static void juliandayFunc(
13906   sqlite3_context *context,
13907   int argc,
13908   sqlite3_value **argv
13909 ){
13910   DateTime x;
13911   if( isDate(context, argc, argv, &x)==0 ){
13912     computeJD(&x);
13913     sqlite3_result_double(context, x.iJD/86400000.0);
13914   }
13915 }
13916 
13917 /*
13918 **    datetime( TIMESTRING, MOD, MOD, ...)
13919 **
13920 ** Return YYYY-MM-DD HH:MM:SS
13921 */
datetimeFunc(sqlite3_context * context,int argc,sqlite3_value ** argv)13922 static void datetimeFunc(
13923   sqlite3_context *context,
13924   int argc,
13925   sqlite3_value **argv
13926 ){
13927   DateTime x;
13928   if( isDate(context, argc, argv, &x)==0 ){
13929     char zBuf[100];
13930     computeYMD_HMS(&x);
13931     sqlite3_snprintf(sizeof(zBuf), zBuf, "%04d-%02d-%02d %02d:%02d:%02d",
13932                      x.Y, x.M, x.D, x.h, x.m, (int)(x.s));
13933     sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
13934   }
13935 }
13936 
13937 /*
13938 **    time( TIMESTRING, MOD, MOD, ...)
13939 **
13940 ** Return HH:MM:SS
13941 */
timeFunc(sqlite3_context * context,int argc,sqlite3_value ** argv)13942 static void timeFunc(
13943   sqlite3_context *context,
13944   int argc,
13945   sqlite3_value **argv
13946 ){
13947   DateTime x;
13948   if( isDate(context, argc, argv, &x)==0 ){
13949     char zBuf[100];
13950     computeHMS(&x);
13951     sqlite3_snprintf(sizeof(zBuf), zBuf, "%02d:%02d:%02d", x.h, x.m, (int)x.s);
13952     sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
13953   }
13954 }
13955 
13956 /*
13957 **    date( TIMESTRING, MOD, MOD, ...)
13958 **
13959 ** Return YYYY-MM-DD
13960 */
dateFunc(sqlite3_context * context,int argc,sqlite3_value ** argv)13961 static void dateFunc(
13962   sqlite3_context *context,
13963   int argc,
13964   sqlite3_value **argv
13965 ){
13966   DateTime x;
13967   if( isDate(context, argc, argv, &x)==0 ){
13968     char zBuf[100];
13969     computeYMD(&x);
13970     sqlite3_snprintf(sizeof(zBuf), zBuf, "%04d-%02d-%02d", x.Y, x.M, x.D);
13971     sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
13972   }
13973 }
13974 
13975 /*
13976 **    strftime( FORMAT, TIMESTRING, MOD, MOD, ...)
13977 **
13978 ** Return a string described by FORMAT.  Conversions as follows:
13979 **
13980 **   %d  day of month
13981 **   %f  ** fractional seconds  SS.SSS
13982 **   %H  hour 00-24
13983 **   %j  day of year 000-366
13984 **   %J  ** Julian day number
13985 **   %m  month 01-12
13986 **   %M  minute 00-59
13987 **   %s  seconds since 1970-01-01
13988 **   %S  seconds 00-59
13989 **   %w  day of week 0-6  sunday==0
13990 **   %W  week of year 00-53
13991 **   %Y  year 0000-9999
13992 **   %%  %
13993 */
strftimeFunc(sqlite3_context * context,int argc,sqlite3_value ** argv)13994 static void strftimeFunc(
13995   sqlite3_context *context,
13996   int argc,
13997   sqlite3_value **argv
13998 ){
13999   DateTime x;
14000   u64 n;
14001   size_t i,j;
14002   char *z;
14003   sqlite3 *db;
14004   const char *zFmt = (const char*)sqlite3_value_text(argv[0]);
14005   char zBuf[100];
14006   if( zFmt==0 || isDate(context, argc-1, argv+1, &x) ) return;
14007   db = sqlite3_context_db_handle(context);
14008   for(i=0, n=1; zFmt[i]; i++, n++){
14009     if( zFmt[i]=='%' ){
14010       switch( zFmt[i+1] ){
14011         case 'd':
14012         case 'H':
14013         case 'm':
14014         case 'M':
14015         case 'S':
14016         case 'W':
14017           n++;
14018           /* fall thru */
14019         case 'w':
14020         case '%':
14021           break;
14022         case 'f':
14023           n += 8;
14024           break;
14025         case 'j':
14026           n += 3;
14027           break;
14028         case 'Y':
14029           n += 8;
14030           break;
14031         case 's':
14032         case 'J':
14033           n += 50;
14034           break;
14035         default:
14036           return;  /* ERROR.  return a NULL */
14037       }
14038       i++;
14039     }
14040   }
14041   testcase( n==sizeof(zBuf)-1 );
14042   testcase( n==sizeof(zBuf) );
14043   testcase( n==(u64)db->aLimit[SQLITE_LIMIT_LENGTH]+1 );
14044   testcase( n==(u64)db->aLimit[SQLITE_LIMIT_LENGTH] );
14045   if( n<sizeof(zBuf) ){
14046     z = zBuf;
14047   }else if( n>(u64)db->aLimit[SQLITE_LIMIT_LENGTH] ){
14048     sqlite3_result_error_toobig(context);
14049     return;
14050   }else{
14051     z = sqlite3DbMallocRaw(db, (int)n);
14052     if( z==0 ){
14053       sqlite3_result_error_nomem(context);
14054       return;
14055     }
14056   }
14057   computeJD(&x);
14058   computeYMD_HMS(&x);
14059   for(i=j=0; zFmt[i]; i++){
14060     if( zFmt[i]!='%' ){
14061       z[j++] = zFmt[i];
14062     }else{
14063       i++;
14064       switch( zFmt[i] ){
14065         case 'd':  sqlite3_snprintf(3, &z[j],"%02d",x.D); j+=2; break;
14066         case 'f': {
14067           double s = x.s;
14068           if( s>59.999 ) s = 59.999;
14069           sqlite3_snprintf(7, &z[j],"%06.3f", s);
14070           j += sqlite3Strlen30(&z[j]);
14071           break;
14072         }
14073         case 'H':  sqlite3_snprintf(3, &z[j],"%02d",x.h); j+=2; break;
14074         case 'W': /* Fall thru */
14075         case 'j': {
14076           int nDay;             /* Number of days since 1st day of year */
14077           DateTime y = x;
14078           y.validJD = 0;
14079           y.M = 1;
14080           y.D = 1;
14081           computeJD(&y);
14082           nDay = (int)((x.iJD-y.iJD+43200000)/86400000);
14083           if( zFmt[i]=='W' ){
14084             int wd;   /* 0=Monday, 1=Tuesday, ... 6=Sunday */
14085             wd = (int)(((x.iJD+43200000)/86400000)%7);
14086             sqlite3_snprintf(3, &z[j],"%02d",(nDay+7-wd)/7);
14087             j += 2;
14088           }else{
14089             sqlite3_snprintf(4, &z[j],"%03d",nDay+1);
14090             j += 3;
14091           }
14092           break;
14093         }
14094         case 'J': {
14095           sqlite3_snprintf(20, &z[j],"%.16g",x.iJD/86400000.0);
14096           j+=sqlite3Strlen30(&z[j]);
14097           break;
14098         }
14099         case 'm':  sqlite3_snprintf(3, &z[j],"%02d",x.M); j+=2; break;
14100         case 'M':  sqlite3_snprintf(3, &z[j],"%02d",x.m); j+=2; break;
14101         case 's': {
14102           sqlite3_snprintf(30,&z[j],"%lld",
14103                            (i64)(x.iJD/1000 - 21086676*(i64)10000));
14104           j += sqlite3Strlen30(&z[j]);
14105           break;
14106         }
14107         case 'S':  sqlite3_snprintf(3,&z[j],"%02d",(int)x.s); j+=2; break;
14108         case 'w': {
14109           z[j++] = (char)(((x.iJD+129600000)/86400000) % 7) + '0';
14110           break;
14111         }
14112         case 'Y': {
14113           sqlite3_snprintf(5,&z[j],"%04d",x.Y); j+=sqlite3Strlen30(&z[j]);
14114           break;
14115         }
14116         default:   z[j++] = '%'; break;
14117       }
14118     }
14119   }
14120   z[j] = 0;
14121   sqlite3_result_text(context, z, -1,
14122                       z==zBuf ? SQLITE_TRANSIENT : SQLITE_DYNAMIC);
14123 }
14124 
14125 /*
14126 ** current_time()
14127 **
14128 ** This function returns the same value as time('now').
14129 */
ctimeFunc(sqlite3_context * context,int NotUsed,sqlite3_value ** NotUsed2)14130 static void ctimeFunc(
14131   sqlite3_context *context,
14132   int NotUsed,
14133   sqlite3_value **NotUsed2
14134 ){
14135   UNUSED_PARAMETER2(NotUsed, NotUsed2);
14136   timeFunc(context, 0, 0);
14137 }
14138 
14139 /*
14140 ** current_date()
14141 **
14142 ** This function returns the same value as date('now').
14143 */
cdateFunc(sqlite3_context * context,int NotUsed,sqlite3_value ** NotUsed2)14144 static void cdateFunc(
14145   sqlite3_context *context,
14146   int NotUsed,
14147   sqlite3_value **NotUsed2
14148 ){
14149   UNUSED_PARAMETER2(NotUsed, NotUsed2);
14150   dateFunc(context, 0, 0);
14151 }
14152 
14153 /*
14154 ** current_timestamp()
14155 **
14156 ** This function returns the same value as datetime('now').
14157 */
ctimestampFunc(sqlite3_context * context,int NotUsed,sqlite3_value ** NotUsed2)14158 static void ctimestampFunc(
14159   sqlite3_context *context,
14160   int NotUsed,
14161   sqlite3_value **NotUsed2
14162 ){
14163   UNUSED_PARAMETER2(NotUsed, NotUsed2);
14164   datetimeFunc(context, 0, 0);
14165 }
14166 #endif /* !defined(SQLITE_OMIT_DATETIME_FUNCS) */
14167 
14168 #ifdef SQLITE_OMIT_DATETIME_FUNCS
14169 /*
14170 ** If the library is compiled to omit the full-scale date and time
14171 ** handling (to get a smaller binary), the following minimal version
14172 ** of the functions current_time(), current_date() and current_timestamp()
14173 ** are included instead. This is to support column declarations that
14174 ** include "DEFAULT CURRENT_TIME" etc.
14175 **
14176 ** This function uses the C-library functions time(), gmtime()
14177 ** and strftime(). The format string to pass to strftime() is supplied
14178 ** as the user-data for the function.
14179 */
currentTimeFunc(sqlite3_context * context,int argc,sqlite3_value ** argv)14180 static void currentTimeFunc(
14181   sqlite3_context *context,
14182   int argc,
14183   sqlite3_value **argv
14184 ){
14185   time_t t;
14186   char *zFormat = (char *)sqlite3_user_data(context);
14187   sqlite3 *db;
14188   sqlite3_int64 iT;
14189   char zBuf[20];
14190 
14191   UNUSED_PARAMETER(argc);
14192   UNUSED_PARAMETER(argv);
14193 
14194   db = sqlite3_context_db_handle(context);
14195   sqlite3OsCurrentTimeInt64(db->pVfs, &iT);
14196   t = iT/1000 - 10000*(sqlite3_int64)21086676;
14197 #ifdef HAVE_GMTIME_R
14198   {
14199     struct tm sNow;
14200     gmtime_r(&t, &sNow);
14201     strftime(zBuf, 20, zFormat, &sNow);
14202   }
14203 #else
14204   {
14205     struct tm *pTm;
14206     sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
14207     pTm = gmtime(&t);
14208     strftime(zBuf, 20, zFormat, pTm);
14209     sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
14210   }
14211 #endif
14212 
14213   sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
14214 }
14215 #endif
14216 
14217 /*
14218 ** This function registered all of the above C functions as SQL
14219 ** functions.  This should be the only routine in this file with
14220 ** external linkage.
14221 */
sqlite3RegisterDateTimeFunctions(void)14222 SQLITE_PRIVATE void sqlite3RegisterDateTimeFunctions(void){
14223   static SQLITE_WSD FuncDef aDateTimeFuncs[] = {
14224 #ifndef SQLITE_OMIT_DATETIME_FUNCS
14225     FUNCTION(julianday,        -1, 0, 0, juliandayFunc ),
14226     FUNCTION(date,             -1, 0, 0, dateFunc      ),
14227     FUNCTION(time,             -1, 0, 0, timeFunc      ),
14228     FUNCTION(datetime,         -1, 0, 0, datetimeFunc  ),
14229     FUNCTION(strftime,         -1, 0, 0, strftimeFunc  ),
14230     FUNCTION(current_time,      0, 0, 0, ctimeFunc     ),
14231     FUNCTION(current_timestamp, 0, 0, 0, ctimestampFunc),
14232     FUNCTION(current_date,      0, 0, 0, cdateFunc     ),
14233 #else
14234     STR_FUNCTION(current_time,      0, "%H:%M:%S",          0, currentTimeFunc),
14235     STR_FUNCTION(current_date,      0, "%Y-%m-%d",          0, currentTimeFunc),
14236     STR_FUNCTION(current_timestamp, 0, "%Y-%m-%d %H:%M:%S", 0, currentTimeFunc),
14237 #endif
14238   };
14239   int i;
14240   FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
14241   FuncDef *aFunc = (FuncDef*)&GLOBAL(FuncDef, aDateTimeFuncs);
14242 
14243   for(i=0; i<ArraySize(aDateTimeFuncs); i++){
14244     sqlite3FuncDefInsert(pHash, &aFunc[i]);
14245   }
14246 }
14247 
14248 /************** End of date.c ************************************************/
14249 /************** Begin file os.c **********************************************/
14250 /*
14251 ** 2005 November 29
14252 **
14253 ** The author disclaims copyright to this source code.  In place of
14254 ** a legal notice, here is a blessing:
14255 **
14256 **    May you do good and not evil.
14257 **    May you find forgiveness for yourself and forgive others.
14258 **    May you share freely, never taking more than you give.
14259 **
14260 ******************************************************************************
14261 **
14262 ** This file contains OS interface code that is common to all
14263 ** architectures.
14264 */
14265 #define _SQLITE_OS_C_ 1
14266 #undef _SQLITE_OS_C_
14267 
14268 /*
14269 ** The default SQLite sqlite3_vfs implementations do not allocate
14270 ** memory (actually, os_unix.c allocates a small amount of memory
14271 ** from within OsOpen()), but some third-party implementations may.
14272 ** So we test the effects of a malloc() failing and the sqlite3OsXXX()
14273 ** function returning SQLITE_IOERR_NOMEM using the DO_OS_MALLOC_TEST macro.
14274 **
14275 ** The following functions are instrumented for malloc() failure
14276 ** testing:
14277 **
14278 **     sqlite3OsOpen()
14279 **     sqlite3OsRead()
14280 **     sqlite3OsWrite()
14281 **     sqlite3OsSync()
14282 **     sqlite3OsLock()
14283 **
14284 */
14285 #if defined(SQLITE_TEST)
14286 SQLITE_API int sqlite3_memdebug_vfs_oom_test = 1;
14287   #define DO_OS_MALLOC_TEST(x)                                       \
14288   if (sqlite3_memdebug_vfs_oom_test && (!x || !sqlite3IsMemJournal(x))) {  \
14289     void *pTstAlloc = sqlite3Malloc(10);                             \
14290     if (!pTstAlloc) return SQLITE_IOERR_NOMEM;                       \
14291     sqlite3_free(pTstAlloc);                                         \
14292   }
14293 #else
14294   #define DO_OS_MALLOC_TEST(x)
14295 #endif
14296 
14297 /*
14298 ** The following routines are convenience wrappers around methods
14299 ** of the sqlite3_file object.  This is mostly just syntactic sugar. All
14300 ** of this would be completely automatic if SQLite were coded using
14301 ** C++ instead of plain old C.
14302 */
sqlite3OsClose(sqlite3_file * pId)14303 SQLITE_PRIVATE int sqlite3OsClose(sqlite3_file *pId){
14304   int rc = SQLITE_OK;
14305   if( pId->pMethods ){
14306     rc = pId->pMethods->xClose(pId);
14307     pId->pMethods = 0;
14308   }
14309   return rc;
14310 }
sqlite3OsRead(sqlite3_file * id,void * pBuf,int amt,i64 offset)14311 SQLITE_PRIVATE int sqlite3OsRead(sqlite3_file *id, void *pBuf, int amt, i64 offset){
14312   DO_OS_MALLOC_TEST(id);
14313   return id->pMethods->xRead(id, pBuf, amt, offset);
14314 }
sqlite3OsWrite(sqlite3_file * id,const void * pBuf,int amt,i64 offset)14315 SQLITE_PRIVATE int sqlite3OsWrite(sqlite3_file *id, const void *pBuf, int amt, i64 offset){
14316   DO_OS_MALLOC_TEST(id);
14317   return id->pMethods->xWrite(id, pBuf, amt, offset);
14318 }
sqlite3OsTruncate(sqlite3_file * id,i64 size)14319 SQLITE_PRIVATE int sqlite3OsTruncate(sqlite3_file *id, i64 size){
14320   return id->pMethods->xTruncate(id, size);
14321 }
sqlite3OsSync(sqlite3_file * id,int flags)14322 SQLITE_PRIVATE int sqlite3OsSync(sqlite3_file *id, int flags){
14323   DO_OS_MALLOC_TEST(id);
14324   return id->pMethods->xSync(id, flags);
14325 }
sqlite3OsFileSize(sqlite3_file * id,i64 * pSize)14326 SQLITE_PRIVATE int sqlite3OsFileSize(sqlite3_file *id, i64 *pSize){
14327   DO_OS_MALLOC_TEST(id);
14328   return id->pMethods->xFileSize(id, pSize);
14329 }
sqlite3OsLock(sqlite3_file * id,int lockType)14330 SQLITE_PRIVATE int sqlite3OsLock(sqlite3_file *id, int lockType){
14331   DO_OS_MALLOC_TEST(id);
14332   return id->pMethods->xLock(id, lockType);
14333 }
sqlite3OsUnlock(sqlite3_file * id,int lockType)14334 SQLITE_PRIVATE int sqlite3OsUnlock(sqlite3_file *id, int lockType){
14335   return id->pMethods->xUnlock(id, lockType);
14336 }
sqlite3OsCheckReservedLock(sqlite3_file * id,int * pResOut)14337 SQLITE_PRIVATE int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut){
14338   DO_OS_MALLOC_TEST(id);
14339   return id->pMethods->xCheckReservedLock(id, pResOut);
14340 }
sqlite3OsFileControl(sqlite3_file * id,int op,void * pArg)14341 SQLITE_PRIVATE int sqlite3OsFileControl(sqlite3_file *id, int op, void *pArg){
14342   return id->pMethods->xFileControl(id, op, pArg);
14343 }
sqlite3OsSectorSize(sqlite3_file * id)14344 SQLITE_PRIVATE int sqlite3OsSectorSize(sqlite3_file *id){
14345   int (*xSectorSize)(sqlite3_file*) = id->pMethods->xSectorSize;
14346   return (xSectorSize ? xSectorSize(id) : SQLITE_DEFAULT_SECTOR_SIZE);
14347 }
sqlite3OsDeviceCharacteristics(sqlite3_file * id)14348 SQLITE_PRIVATE int sqlite3OsDeviceCharacteristics(sqlite3_file *id){
14349   return id->pMethods->xDeviceCharacteristics(id);
14350 }
sqlite3OsShmLock(sqlite3_file * id,int offset,int n,int flags)14351 SQLITE_PRIVATE int sqlite3OsShmLock(sqlite3_file *id, int offset, int n, int flags){
14352   return id->pMethods->xShmLock(id, offset, n, flags);
14353 }
sqlite3OsShmBarrier(sqlite3_file * id)14354 SQLITE_PRIVATE void sqlite3OsShmBarrier(sqlite3_file *id){
14355   id->pMethods->xShmBarrier(id);
14356 }
sqlite3OsShmUnmap(sqlite3_file * id,int deleteFlag)14357 SQLITE_PRIVATE int sqlite3OsShmUnmap(sqlite3_file *id, int deleteFlag){
14358   return id->pMethods->xShmUnmap(id, deleteFlag);
14359 }
sqlite3OsShmMap(sqlite3_file * id,int iPage,int pgsz,int bExtend,void volatile ** pp)14360 SQLITE_PRIVATE int sqlite3OsShmMap(
14361   sqlite3_file *id,               /* Database file handle */
14362   int iPage,
14363   int pgsz,
14364   int bExtend,                    /* True to extend file if necessary */
14365   void volatile **pp              /* OUT: Pointer to mapping */
14366 ){
14367   return id->pMethods->xShmMap(id, iPage, pgsz, bExtend, pp);
14368 }
14369 
14370 /*
14371 ** The next group of routines are convenience wrappers around the
14372 ** VFS methods.
14373 */
sqlite3OsOpen(sqlite3_vfs * pVfs,const char * zPath,sqlite3_file * pFile,int flags,int * pFlagsOut)14374 SQLITE_PRIVATE int sqlite3OsOpen(
14375   sqlite3_vfs *pVfs,
14376   const char *zPath,
14377   sqlite3_file *pFile,
14378   int flags,
14379   int *pFlagsOut
14380 ){
14381   int rc;
14382   DO_OS_MALLOC_TEST(0);
14383   /* 0x87f3f is a mask of SQLITE_OPEN_ flags that are valid to be passed
14384   ** down into the VFS layer.  Some SQLITE_OPEN_ flags (for example,
14385   ** SQLITE_OPEN_FULLMUTEX or SQLITE_OPEN_SHAREDCACHE) are blocked before
14386   ** reaching the VFS. */
14387   rc = pVfs->xOpen(pVfs, zPath, pFile, flags & 0x87f3f, pFlagsOut);
14388   assert( rc==SQLITE_OK || pFile->pMethods==0 );
14389   return rc;
14390 }
sqlite3OsDelete(sqlite3_vfs * pVfs,const char * zPath,int dirSync)14391 SQLITE_PRIVATE int sqlite3OsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
14392   return pVfs->xDelete(pVfs, zPath, dirSync);
14393 }
sqlite3OsAccess(sqlite3_vfs * pVfs,const char * zPath,int flags,int * pResOut)14394 SQLITE_PRIVATE int sqlite3OsAccess(
14395   sqlite3_vfs *pVfs,
14396   const char *zPath,
14397   int flags,
14398   int *pResOut
14399 ){
14400   DO_OS_MALLOC_TEST(0);
14401   return pVfs->xAccess(pVfs, zPath, flags, pResOut);
14402 }
sqlite3OsFullPathname(sqlite3_vfs * pVfs,const char * zPath,int nPathOut,char * zPathOut)14403 SQLITE_PRIVATE int sqlite3OsFullPathname(
14404   sqlite3_vfs *pVfs,
14405   const char *zPath,
14406   int nPathOut,
14407   char *zPathOut
14408 ){
14409   zPathOut[0] = 0;
14410   return pVfs->xFullPathname(pVfs, zPath, nPathOut, zPathOut);
14411 }
14412 #ifndef SQLITE_OMIT_LOAD_EXTENSION
sqlite3OsDlOpen(sqlite3_vfs * pVfs,const char * zPath)14413 SQLITE_PRIVATE void *sqlite3OsDlOpen(sqlite3_vfs *pVfs, const char *zPath){
14414   return pVfs->xDlOpen(pVfs, zPath);
14415 }
sqlite3OsDlError(sqlite3_vfs * pVfs,int nByte,char * zBufOut)14416 SQLITE_PRIVATE void sqlite3OsDlError(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
14417   pVfs->xDlError(pVfs, nByte, zBufOut);
14418 }
sqlite3OsDlSym(sqlite3_vfs * pVfs,void * pHdle,const char * zSym)14419 SQLITE_PRIVATE void (*sqlite3OsDlSym(sqlite3_vfs *pVfs, void *pHdle, const char *zSym))(void){
14420   return pVfs->xDlSym(pVfs, pHdle, zSym);
14421 }
sqlite3OsDlClose(sqlite3_vfs * pVfs,void * pHandle)14422 SQLITE_PRIVATE void sqlite3OsDlClose(sqlite3_vfs *pVfs, void *pHandle){
14423   pVfs->xDlClose(pVfs, pHandle);
14424 }
14425 #endif /* SQLITE_OMIT_LOAD_EXTENSION */
sqlite3OsRandomness(sqlite3_vfs * pVfs,int nByte,char * zBufOut)14426 SQLITE_PRIVATE int sqlite3OsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
14427   return pVfs->xRandomness(pVfs, nByte, zBufOut);
14428 }
sqlite3OsSleep(sqlite3_vfs * pVfs,int nMicro)14429 SQLITE_PRIVATE int sqlite3OsSleep(sqlite3_vfs *pVfs, int nMicro){
14430   return pVfs->xSleep(pVfs, nMicro);
14431 }
sqlite3OsCurrentTimeInt64(sqlite3_vfs * pVfs,sqlite3_int64 * pTimeOut)14432 SQLITE_PRIVATE int sqlite3OsCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *pTimeOut){
14433   int rc;
14434   /* IMPLEMENTATION-OF: R-49045-42493 SQLite will use the xCurrentTimeInt64()
14435   ** method to get the current date and time if that method is available
14436   ** (if iVersion is 2 or greater and the function pointer is not NULL) and
14437   ** will fall back to xCurrentTime() if xCurrentTimeInt64() is
14438   ** unavailable.
14439   */
14440   if( pVfs->iVersion>=2 && pVfs->xCurrentTimeInt64 ){
14441     rc = pVfs->xCurrentTimeInt64(pVfs, pTimeOut);
14442   }else{
14443     double r;
14444     rc = pVfs->xCurrentTime(pVfs, &r);
14445     *pTimeOut = (sqlite3_int64)(r*86400000.0);
14446   }
14447   return rc;
14448 }
14449 
sqlite3OsOpenMalloc(sqlite3_vfs * pVfs,const char * zFile,sqlite3_file ** ppFile,int flags,int * pOutFlags)14450 SQLITE_PRIVATE int sqlite3OsOpenMalloc(
14451   sqlite3_vfs *pVfs,
14452   const char *zFile,
14453   sqlite3_file **ppFile,
14454   int flags,
14455   int *pOutFlags
14456 ){
14457   int rc = SQLITE_NOMEM;
14458   sqlite3_file *pFile;
14459   pFile = (sqlite3_file *)sqlite3Malloc(pVfs->szOsFile);
14460   if( pFile ){
14461     rc = sqlite3OsOpen(pVfs, zFile, pFile, flags, pOutFlags);
14462     if( rc!=SQLITE_OK ){
14463       sqlite3_free(pFile);
14464     }else{
14465       *ppFile = pFile;
14466     }
14467   }
14468   return rc;
14469 }
sqlite3OsCloseFree(sqlite3_file * pFile)14470 SQLITE_PRIVATE int sqlite3OsCloseFree(sqlite3_file *pFile){
14471   int rc = SQLITE_OK;
14472   assert( pFile );
14473   rc = sqlite3OsClose(pFile);
14474   sqlite3_free(pFile);
14475   return rc;
14476 }
14477 
14478 /*
14479 ** This function is a wrapper around the OS specific implementation of
14480 ** sqlite3_os_init(). The purpose of the wrapper is to provide the
14481 ** ability to simulate a malloc failure, so that the handling of an
14482 ** error in sqlite3_os_init() by the upper layers can be tested.
14483 */
sqlite3OsInit(void)14484 SQLITE_PRIVATE int sqlite3OsInit(void){
14485   void *p = sqlite3_malloc(10);
14486   if( p==0 ) return SQLITE_NOMEM;
14487   sqlite3_free(p);
14488   return sqlite3_os_init();
14489 }
14490 
14491 /*
14492 ** The list of all registered VFS implementations.
14493 */
14494 static sqlite3_vfs * SQLITE_WSD vfsList = 0;
14495 #define vfsList GLOBAL(sqlite3_vfs *, vfsList)
14496 
14497 /*
14498 ** Locate a VFS by name.  If no name is given, simply return the
14499 ** first VFS on the list.
14500 */
sqlite3_vfs_find(const char * zVfs)14501 SQLITE_API sqlite3_vfs *sqlite3_vfs_find(const char *zVfs){
14502   sqlite3_vfs *pVfs = 0;
14503 #if SQLITE_THREADSAFE
14504   sqlite3_mutex *mutex;
14505 #endif
14506 #ifndef SQLITE_OMIT_AUTOINIT
14507   int rc = sqlite3_initialize();
14508   if( rc ) return 0;
14509 #endif
14510 #if SQLITE_THREADSAFE
14511   mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
14512 #endif
14513   sqlite3_mutex_enter(mutex);
14514   for(pVfs = vfsList; pVfs; pVfs=pVfs->pNext){
14515     if( zVfs==0 ) break;
14516     if( strcmp(zVfs, pVfs->zName)==0 ) break;
14517   }
14518   sqlite3_mutex_leave(mutex);
14519   return pVfs;
14520 }
14521 
14522 /*
14523 ** Unlink a VFS from the linked list
14524 */
vfsUnlink(sqlite3_vfs * pVfs)14525 static void vfsUnlink(sqlite3_vfs *pVfs){
14526   assert( sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)) );
14527   if( pVfs==0 ){
14528     /* No-op */
14529   }else if( vfsList==pVfs ){
14530     vfsList = pVfs->pNext;
14531   }else if( vfsList ){
14532     sqlite3_vfs *p = vfsList;
14533     while( p->pNext && p->pNext!=pVfs ){
14534       p = p->pNext;
14535     }
14536     if( p->pNext==pVfs ){
14537       p->pNext = pVfs->pNext;
14538     }
14539   }
14540 }
14541 
14542 /*
14543 ** Register a VFS with the system.  It is harmless to register the same
14544 ** VFS multiple times.  The new VFS becomes the default if makeDflt is
14545 ** true.
14546 */
sqlite3_vfs_register(sqlite3_vfs * pVfs,int makeDflt)14547 SQLITE_API int sqlite3_vfs_register(sqlite3_vfs *pVfs, int makeDflt){
14548   sqlite3_mutex *mutex = 0;
14549 #ifndef SQLITE_OMIT_AUTOINIT
14550   int rc = sqlite3_initialize();
14551   if( rc ) return rc;
14552 #endif
14553   mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
14554   sqlite3_mutex_enter(mutex);
14555   vfsUnlink(pVfs);
14556   if( makeDflt || vfsList==0 ){
14557     pVfs->pNext = vfsList;
14558     vfsList = pVfs;
14559   }else{
14560     pVfs->pNext = vfsList->pNext;
14561     vfsList->pNext = pVfs;
14562   }
14563   assert(vfsList);
14564   sqlite3_mutex_leave(mutex);
14565   return SQLITE_OK;
14566 }
14567 
14568 /*
14569 ** Unregister a VFS so that it is no longer accessible.
14570 */
sqlite3_vfs_unregister(sqlite3_vfs * pVfs)14571 SQLITE_API int sqlite3_vfs_unregister(sqlite3_vfs *pVfs){
14572 #if SQLITE_THREADSAFE
14573   sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
14574 #endif
14575   sqlite3_mutex_enter(mutex);
14576   vfsUnlink(pVfs);
14577   sqlite3_mutex_leave(mutex);
14578   return SQLITE_OK;
14579 }
14580 
14581 /************** End of os.c **************************************************/
14582 /************** Begin file fault.c *******************************************/
14583 /*
14584 ** 2008 Jan 22
14585 **
14586 ** The author disclaims copyright to this source code.  In place of
14587 ** a legal notice, here is a blessing:
14588 **
14589 **    May you do good and not evil.
14590 **    May you find forgiveness for yourself and forgive others.
14591 **    May you share freely, never taking more than you give.
14592 **
14593 *************************************************************************
14594 **
14595 ** This file contains code to support the concept of "benign"
14596 ** malloc failures (when the xMalloc() or xRealloc() method of the
14597 ** sqlite3_mem_methods structure fails to allocate a block of memory
14598 ** and returns 0).
14599 **
14600 ** Most malloc failures are non-benign. After they occur, SQLite
14601 ** abandons the current operation and returns an error code (usually
14602 ** SQLITE_NOMEM) to the user. However, sometimes a fault is not necessarily
14603 ** fatal. For example, if a malloc fails while resizing a hash table, this
14604 ** is completely recoverable simply by not carrying out the resize. The
14605 ** hash table will continue to function normally.  So a malloc failure
14606 ** during a hash table resize is a benign fault.
14607 */
14608 
14609 
14610 #ifndef SQLITE_OMIT_BUILTIN_TEST
14611 
14612 /*
14613 ** Global variables.
14614 */
14615 typedef struct BenignMallocHooks BenignMallocHooks;
14616 static SQLITE_WSD struct BenignMallocHooks {
14617   void (*xBenignBegin)(void);
14618   void (*xBenignEnd)(void);
14619 } sqlite3Hooks = { 0, 0 };
14620 
14621 /* The "wsdHooks" macro will resolve to the appropriate BenignMallocHooks
14622 ** structure.  If writable static data is unsupported on the target,
14623 ** we have to locate the state vector at run-time.  In the more common
14624 ** case where writable static data is supported, wsdHooks can refer directly
14625 ** to the "sqlite3Hooks" state vector declared above.
14626 */
14627 #ifdef SQLITE_OMIT_WSD
14628 # define wsdHooksInit \
14629   BenignMallocHooks *x = &GLOBAL(BenignMallocHooks,sqlite3Hooks)
14630 # define wsdHooks x[0]
14631 #else
14632 # define wsdHooksInit
14633 # define wsdHooks sqlite3Hooks
14634 #endif
14635 
14636 
14637 /*
14638 ** Register hooks to call when sqlite3BeginBenignMalloc() and
14639 ** sqlite3EndBenignMalloc() are called, respectively.
14640 */
sqlite3BenignMallocHooks(void (* xBenignBegin)(void),void (* xBenignEnd)(void))14641 SQLITE_PRIVATE void sqlite3BenignMallocHooks(
14642   void (*xBenignBegin)(void),
14643   void (*xBenignEnd)(void)
14644 ){
14645   wsdHooksInit;
14646   wsdHooks.xBenignBegin = xBenignBegin;
14647   wsdHooks.xBenignEnd = xBenignEnd;
14648 }
14649 
14650 /*
14651 ** This (sqlite3EndBenignMalloc()) is called by SQLite code to indicate that
14652 ** subsequent malloc failures are benign. A call to sqlite3EndBenignMalloc()
14653 ** indicates that subsequent malloc failures are non-benign.
14654 */
sqlite3BeginBenignMalloc(void)14655 SQLITE_PRIVATE void sqlite3BeginBenignMalloc(void){
14656   wsdHooksInit;
14657   if( wsdHooks.xBenignBegin ){
14658     wsdHooks.xBenignBegin();
14659   }
14660 }
sqlite3EndBenignMalloc(void)14661 SQLITE_PRIVATE void sqlite3EndBenignMalloc(void){
14662   wsdHooksInit;
14663   if( wsdHooks.xBenignEnd ){
14664     wsdHooks.xBenignEnd();
14665   }
14666 }
14667 
14668 #endif   /* #ifndef SQLITE_OMIT_BUILTIN_TEST */
14669 
14670 /************** End of fault.c ***********************************************/
14671 /************** Begin file mem0.c ********************************************/
14672 /*
14673 ** 2008 October 28
14674 **
14675 ** The author disclaims copyright to this source code.  In place of
14676 ** a legal notice, here is a blessing:
14677 **
14678 **    May you do good and not evil.
14679 **    May you find forgiveness for yourself and forgive others.
14680 **    May you share freely, never taking more than you give.
14681 **
14682 *************************************************************************
14683 **
14684 ** This file contains a no-op memory allocation drivers for use when
14685 ** SQLITE_ZERO_MALLOC is defined.  The allocation drivers implemented
14686 ** here always fail.  SQLite will not operate with these drivers.  These
14687 ** are merely placeholders.  Real drivers must be substituted using
14688 ** sqlite3_config() before SQLite will operate.
14689 */
14690 
14691 /*
14692 ** This version of the memory allocator is the default.  It is
14693 ** used when no other memory allocator is specified using compile-time
14694 ** macros.
14695 */
14696 #ifdef SQLITE_ZERO_MALLOC
14697 
14698 /*
14699 ** No-op versions of all memory allocation routines
14700 */
sqlite3MemMalloc(int nByte)14701 static void *sqlite3MemMalloc(int nByte){ return 0; }
sqlite3MemFree(void * pPrior)14702 static void sqlite3MemFree(void *pPrior){ return; }
sqlite3MemRealloc(void * pPrior,int nByte)14703 static void *sqlite3MemRealloc(void *pPrior, int nByte){ return 0; }
sqlite3MemSize(void * pPrior)14704 static int sqlite3MemSize(void *pPrior){ return 0; }
sqlite3MemRoundup(int n)14705 static int sqlite3MemRoundup(int n){ return n; }
sqlite3MemInit(void * NotUsed)14706 static int sqlite3MemInit(void *NotUsed){ return SQLITE_OK; }
sqlite3MemShutdown(void * NotUsed)14707 static void sqlite3MemShutdown(void *NotUsed){ return; }
14708 
14709 /*
14710 ** This routine is the only routine in this file with external linkage.
14711 **
14712 ** Populate the low-level memory allocation function pointers in
14713 ** sqlite3GlobalConfig.m with pointers to the routines in this file.
14714 */
sqlite3MemSetDefault(void)14715 SQLITE_PRIVATE void sqlite3MemSetDefault(void){
14716   static const sqlite3_mem_methods defaultMethods = {
14717      sqlite3MemMalloc,
14718      sqlite3MemFree,
14719      sqlite3MemRealloc,
14720      sqlite3MemSize,
14721      sqlite3MemRoundup,
14722      sqlite3MemInit,
14723      sqlite3MemShutdown,
14724      0
14725   };
14726   sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods);
14727 }
14728 
14729 #endif /* SQLITE_ZERO_MALLOC */
14730 
14731 /************** End of mem0.c ************************************************/
14732 /************** Begin file mem1.c ********************************************/
14733 /*
14734 ** 2007 August 14
14735 **
14736 ** The author disclaims copyright to this source code.  In place of
14737 ** a legal notice, here is a blessing:
14738 **
14739 **    May you do good and not evil.
14740 **    May you find forgiveness for yourself and forgive others.
14741 **    May you share freely, never taking more than you give.
14742 **
14743 *************************************************************************
14744 **
14745 ** This file contains low-level memory allocation drivers for when
14746 ** SQLite will use the standard C-library malloc/realloc/free interface
14747 ** to obtain the memory it needs.
14748 **
14749 ** This file contains implementations of the low-level memory allocation
14750 ** routines specified in the sqlite3_mem_methods object.
14751 */
14752 
14753 /*
14754 ** This version of the memory allocator is the default.  It is
14755 ** used when no other memory allocator is specified using compile-time
14756 ** macros.
14757 */
14758 #ifdef SQLITE_SYSTEM_MALLOC
14759 
14760 /*
14761 ** Like malloc(), but remember the size of the allocation
14762 ** so that we can find it later using sqlite3MemSize().
14763 **
14764 ** For this low-level routine, we are guaranteed that nByte>0 because
14765 ** cases of nByte<=0 will be intercepted and dealt with by higher level
14766 ** routines.
14767 */
sqlite3MemMalloc(int nByte)14768 static void *sqlite3MemMalloc(int nByte){
14769   sqlite3_int64 *p;
14770   assert( nByte>0 );
14771   nByte = ROUND8(nByte);
14772   p = malloc( nByte+8 );
14773   if( p ){
14774     p[0] = nByte;
14775     p++;
14776   }else{
14777     testcase( sqlite3GlobalConfig.xLog!=0 );
14778     sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes of memory", nByte);
14779   }
14780   return (void *)p;
14781 }
14782 
14783 /*
14784 ** Like free() but works for allocations obtained from sqlite3MemMalloc()
14785 ** or sqlite3MemRealloc().
14786 **
14787 ** For this low-level routine, we already know that pPrior!=0 since
14788 ** cases where pPrior==0 will have been intecepted and dealt with
14789 ** by higher-level routines.
14790 */
sqlite3MemFree(void * pPrior)14791 static void sqlite3MemFree(void *pPrior){
14792   sqlite3_int64 *p = (sqlite3_int64*)pPrior;
14793   assert( pPrior!=0 );
14794   p--;
14795   free(p);
14796 }
14797 
14798 /*
14799 ** Report the allocated size of a prior return from xMalloc()
14800 ** or xRealloc().
14801 */
sqlite3MemSize(void * pPrior)14802 static int sqlite3MemSize(void *pPrior){
14803   sqlite3_int64 *p;
14804   if( pPrior==0 ) return 0;
14805   p = (sqlite3_int64*)pPrior;
14806   p--;
14807   return (int)p[0];
14808 }
14809 
14810 /*
14811 ** Like realloc().  Resize an allocation previously obtained from
14812 ** sqlite3MemMalloc().
14813 **
14814 ** For this low-level interface, we know that pPrior!=0.  Cases where
14815 ** pPrior==0 while have been intercepted by higher-level routine and
14816 ** redirected to xMalloc.  Similarly, we know that nByte>0 becauses
14817 ** cases where nByte<=0 will have been intercepted by higher-level
14818 ** routines and redirected to xFree.
14819 */
sqlite3MemRealloc(void * pPrior,int nByte)14820 static void *sqlite3MemRealloc(void *pPrior, int nByte){
14821   sqlite3_int64 *p = (sqlite3_int64*)pPrior;
14822   assert( pPrior!=0 && nByte>0 );
14823   assert( nByte==ROUND8(nByte) ); /* EV: R-46199-30249 */
14824   p--;
14825   p = realloc(p, nByte+8 );
14826   if( p ){
14827     p[0] = nByte;
14828     p++;
14829   }else{
14830     testcase( sqlite3GlobalConfig.xLog!=0 );
14831     sqlite3_log(SQLITE_NOMEM,
14832       "failed memory resize %u to %u bytes",
14833       sqlite3MemSize(pPrior), nByte);
14834   }
14835   return (void*)p;
14836 }
14837 
14838 /*
14839 ** Round up a request size to the next valid allocation size.
14840 */
sqlite3MemRoundup(int n)14841 static int sqlite3MemRoundup(int n){
14842   return ROUND8(n);
14843 }
14844 
14845 /*
14846 ** Initialize this module.
14847 */
sqlite3MemInit(void * NotUsed)14848 static int sqlite3MemInit(void *NotUsed){
14849   UNUSED_PARAMETER(NotUsed);
14850   return SQLITE_OK;
14851 }
14852 
14853 /*
14854 ** Deinitialize this module.
14855 */
sqlite3MemShutdown(void * NotUsed)14856 static void sqlite3MemShutdown(void *NotUsed){
14857   UNUSED_PARAMETER(NotUsed);
14858   return;
14859 }
14860 
14861 /*
14862 ** This routine is the only routine in this file with external linkage.
14863 **
14864 ** Populate the low-level memory allocation function pointers in
14865 ** sqlite3GlobalConfig.m with pointers to the routines in this file.
14866 */
sqlite3MemSetDefault(void)14867 SQLITE_PRIVATE void sqlite3MemSetDefault(void){
14868   static const sqlite3_mem_methods defaultMethods = {
14869      sqlite3MemMalloc,
14870      sqlite3MemFree,
14871      sqlite3MemRealloc,
14872      sqlite3MemSize,
14873      sqlite3MemRoundup,
14874      sqlite3MemInit,
14875      sqlite3MemShutdown,
14876      0
14877   };
14878   sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods);
14879 }
14880 
14881 #endif /* SQLITE_SYSTEM_MALLOC */
14882 
14883 /************** End of mem1.c ************************************************/
14884 /************** Begin file mem2.c ********************************************/
14885 /*
14886 ** 2007 August 15
14887 **
14888 ** The author disclaims copyright to this source code.  In place of
14889 ** a legal notice, here is a blessing:
14890 **
14891 **    May you do good and not evil.
14892 **    May you find forgiveness for yourself and forgive others.
14893 **    May you share freely, never taking more than you give.
14894 **
14895 *************************************************************************
14896 **
14897 ** This file contains low-level memory allocation drivers for when
14898 ** SQLite will use the standard C-library malloc/realloc/free interface
14899 ** to obtain the memory it needs while adding lots of additional debugging
14900 ** information to each allocation in order to help detect and fix memory
14901 ** leaks and memory usage errors.
14902 **
14903 ** This file contains implementations of the low-level memory allocation
14904 ** routines specified in the sqlite3_mem_methods object.
14905 */
14906 
14907 /*
14908 ** This version of the memory allocator is used only if the
14909 ** SQLITE_MEMDEBUG macro is defined
14910 */
14911 #ifdef SQLITE_MEMDEBUG
14912 
14913 /*
14914 ** The backtrace functionality is only available with GLIBC
14915 */
14916 #ifdef __GLIBC__
14917   extern int backtrace(void**,int);
14918   extern void backtrace_symbols_fd(void*const*,int,int);
14919 #else
14920 # define backtrace(A,B) 1
14921 # define backtrace_symbols_fd(A,B,C)
14922 #endif
14923 
14924 /*
14925 ** Each memory allocation looks like this:
14926 **
14927 **  ------------------------------------------------------------------------
14928 **  | Title |  backtrace pointers |  MemBlockHdr |  allocation |  EndGuard |
14929 **  ------------------------------------------------------------------------
14930 **
14931 ** The application code sees only a pointer to the allocation.  We have
14932 ** to back up from the allocation pointer to find the MemBlockHdr.  The
14933 ** MemBlockHdr tells us the size of the allocation and the number of
14934 ** backtrace pointers.  There is also a guard word at the end of the
14935 ** MemBlockHdr.
14936 */
14937 struct MemBlockHdr {
14938   i64 iSize;                          /* Size of this allocation */
14939   struct MemBlockHdr *pNext, *pPrev;  /* Linked list of all unfreed memory */
14940   char nBacktrace;                    /* Number of backtraces on this alloc */
14941   char nBacktraceSlots;               /* Available backtrace slots */
14942   u8 nTitle;                          /* Bytes of title; includes '\0' */
14943   u8 eType;                           /* Allocation type code */
14944   int iForeGuard;                     /* Guard word for sanity */
14945 };
14946 
14947 /*
14948 ** Guard words
14949 */
14950 #define FOREGUARD 0x80F5E153
14951 #define REARGUARD 0xE4676B53
14952 
14953 /*
14954 ** Number of malloc size increments to track.
14955 */
14956 #define NCSIZE  1000
14957 
14958 /*
14959 ** All of the static variables used by this module are collected
14960 ** into a single structure named "mem".  This is to keep the
14961 ** static variables organized and to reduce namespace pollution
14962 ** when this module is combined with other in the amalgamation.
14963 */
14964 static struct {
14965 
14966   /*
14967   ** Mutex to control access to the memory allocation subsystem.
14968   */
14969   sqlite3_mutex *mutex;
14970 
14971   /*
14972   ** Head and tail of a linked list of all outstanding allocations
14973   */
14974   struct MemBlockHdr *pFirst;
14975   struct MemBlockHdr *pLast;
14976 
14977   /*
14978   ** The number of levels of backtrace to save in new allocations.
14979   */
14980   int nBacktrace;
14981   void (*xBacktrace)(int, int, void **);
14982 
14983   /*
14984   ** Title text to insert in front of each block
14985   */
14986   int nTitle;        /* Bytes of zTitle to save.  Includes '\0' and padding */
14987   char zTitle[100];  /* The title text */
14988 
14989   /*
14990   ** sqlite3MallocDisallow() increments the following counter.
14991   ** sqlite3MallocAllow() decrements it.
14992   */
14993   int disallow; /* Do not allow memory allocation */
14994 
14995   /*
14996   ** Gather statistics on the sizes of memory allocations.
14997   ** nAlloc[i] is the number of allocation attempts of i*8
14998   ** bytes.  i==NCSIZE is the number of allocation attempts for
14999   ** sizes more than NCSIZE*8 bytes.
15000   */
15001   int nAlloc[NCSIZE];      /* Total number of allocations */
15002   int nCurrent[NCSIZE];    /* Current number of allocations */
15003   int mxCurrent[NCSIZE];   /* Highwater mark for nCurrent */
15004 
15005 } mem;
15006 
15007 
15008 /*
15009 ** Adjust memory usage statistics
15010 */
adjustStats(int iSize,int increment)15011 static void adjustStats(int iSize, int increment){
15012   int i = ROUND8(iSize)/8;
15013   if( i>NCSIZE-1 ){
15014     i = NCSIZE - 1;
15015   }
15016   if( increment>0 ){
15017     mem.nAlloc[i]++;
15018     mem.nCurrent[i]++;
15019     if( mem.nCurrent[i]>mem.mxCurrent[i] ){
15020       mem.mxCurrent[i] = mem.nCurrent[i];
15021     }
15022   }else{
15023     mem.nCurrent[i]--;
15024     assert( mem.nCurrent[i]>=0 );
15025   }
15026 }
15027 
15028 /*
15029 ** Given an allocation, find the MemBlockHdr for that allocation.
15030 **
15031 ** This routine checks the guards at either end of the allocation and
15032 ** if they are incorrect it asserts.
15033 */
sqlite3MemsysGetHeader(void * pAllocation)15034 static struct MemBlockHdr *sqlite3MemsysGetHeader(void *pAllocation){
15035   struct MemBlockHdr *p;
15036   int *pInt;
15037   u8 *pU8;
15038   int nReserve;
15039 
15040   p = (struct MemBlockHdr*)pAllocation;
15041   p--;
15042   assert( p->iForeGuard==(int)FOREGUARD );
15043   nReserve = ROUND8(p->iSize);
15044   pInt = (int*)pAllocation;
15045   pU8 = (u8*)pAllocation;
15046   assert( pInt[nReserve/sizeof(int)]==(int)REARGUARD );
15047   /* This checks any of the "extra" bytes allocated due
15048   ** to rounding up to an 8 byte boundary to ensure
15049   ** they haven't been overwritten.
15050   */
15051   while( nReserve-- > p->iSize ) assert( pU8[nReserve]==0x65 );
15052   return p;
15053 }
15054 
15055 /*
15056 ** Return the number of bytes currently allocated at address p.
15057 */
sqlite3MemSize(void * p)15058 static int sqlite3MemSize(void *p){
15059   struct MemBlockHdr *pHdr;
15060   if( !p ){
15061     return 0;
15062   }
15063   pHdr = sqlite3MemsysGetHeader(p);
15064   return pHdr->iSize;
15065 }
15066 
15067 /*
15068 ** Initialize the memory allocation subsystem.
15069 */
sqlite3MemInit(void * NotUsed)15070 static int sqlite3MemInit(void *NotUsed){
15071   UNUSED_PARAMETER(NotUsed);
15072   assert( (sizeof(struct MemBlockHdr)&7) == 0 );
15073   if( !sqlite3GlobalConfig.bMemstat ){
15074     /* If memory status is enabled, then the malloc.c wrapper will already
15075     ** hold the STATIC_MEM mutex when the routines here are invoked. */
15076     mem.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
15077   }
15078   return SQLITE_OK;
15079 }
15080 
15081 /*
15082 ** Deinitialize the memory allocation subsystem.
15083 */
sqlite3MemShutdown(void * NotUsed)15084 static void sqlite3MemShutdown(void *NotUsed){
15085   UNUSED_PARAMETER(NotUsed);
15086   mem.mutex = 0;
15087 }
15088 
15089 /*
15090 ** Round up a request size to the next valid allocation size.
15091 */
sqlite3MemRoundup(int n)15092 static int sqlite3MemRoundup(int n){
15093   return ROUND8(n);
15094 }
15095 
15096 /*
15097 ** Fill a buffer with pseudo-random bytes.  This is used to preset
15098 ** the content of a new memory allocation to unpredictable values and
15099 ** to clear the content of a freed allocation to unpredictable values.
15100 */
randomFill(char * pBuf,int nByte)15101 static void randomFill(char *pBuf, int nByte){
15102   unsigned int x, y, r;
15103   x = SQLITE_PTR_TO_INT(pBuf);
15104   y = nByte | 1;
15105   while( nByte >= 4 ){
15106     x = (x>>1) ^ (-(x&1) & 0xd0000001);
15107     y = y*1103515245 + 12345;
15108     r = x ^ y;
15109     *(int*)pBuf = r;
15110     pBuf += 4;
15111     nByte -= 4;
15112   }
15113   while( nByte-- > 0 ){
15114     x = (x>>1) ^ (-(x&1) & 0xd0000001);
15115     y = y*1103515245 + 12345;
15116     r = x ^ y;
15117     *(pBuf++) = r & 0xff;
15118   }
15119 }
15120 
15121 /*
15122 ** Allocate nByte bytes of memory.
15123 */
sqlite3MemMalloc(int nByte)15124 static void *sqlite3MemMalloc(int nByte){
15125   struct MemBlockHdr *pHdr;
15126   void **pBt;
15127   char *z;
15128   int *pInt;
15129   void *p = 0;
15130   int totalSize;
15131   int nReserve;
15132   sqlite3_mutex_enter(mem.mutex);
15133   assert( mem.disallow==0 );
15134   nReserve = ROUND8(nByte);
15135   totalSize = nReserve + sizeof(*pHdr) + sizeof(int) +
15136                mem.nBacktrace*sizeof(void*) + mem.nTitle;
15137   p = malloc(totalSize);
15138   if( p ){
15139     z = p;
15140     pBt = (void**)&z[mem.nTitle];
15141     pHdr = (struct MemBlockHdr*)&pBt[mem.nBacktrace];
15142     pHdr->pNext = 0;
15143     pHdr->pPrev = mem.pLast;
15144     if( mem.pLast ){
15145       mem.pLast->pNext = pHdr;
15146     }else{
15147       mem.pFirst = pHdr;
15148     }
15149     mem.pLast = pHdr;
15150     pHdr->iForeGuard = FOREGUARD;
15151     pHdr->eType = MEMTYPE_HEAP;
15152     pHdr->nBacktraceSlots = mem.nBacktrace;
15153     pHdr->nTitle = mem.nTitle;
15154     if( mem.nBacktrace ){
15155       void *aAddr[40];
15156       pHdr->nBacktrace = backtrace(aAddr, mem.nBacktrace+1)-1;
15157       memcpy(pBt, &aAddr[1], pHdr->nBacktrace*sizeof(void*));
15158       assert(pBt[0]);
15159       if( mem.xBacktrace ){
15160         mem.xBacktrace(nByte, pHdr->nBacktrace-1, &aAddr[1]);
15161       }
15162     }else{
15163       pHdr->nBacktrace = 0;
15164     }
15165     if( mem.nTitle ){
15166       memcpy(z, mem.zTitle, mem.nTitle);
15167     }
15168     pHdr->iSize = nByte;
15169     adjustStats(nByte, +1);
15170     pInt = (int*)&pHdr[1];
15171     pInt[nReserve/sizeof(int)] = REARGUARD;
15172     randomFill((char*)pInt, nByte);
15173     memset(((char*)pInt)+nByte, 0x65, nReserve-nByte);
15174     p = (void*)pInt;
15175   }
15176   sqlite3_mutex_leave(mem.mutex);
15177   return p;
15178 }
15179 
15180 /*
15181 ** Free memory.
15182 */
sqlite3MemFree(void * pPrior)15183 static void sqlite3MemFree(void *pPrior){
15184   struct MemBlockHdr *pHdr;
15185   void **pBt;
15186   char *z;
15187   assert( sqlite3GlobalConfig.bMemstat || sqlite3GlobalConfig.bCoreMutex==0
15188        || mem.mutex!=0 );
15189   pHdr = sqlite3MemsysGetHeader(pPrior);
15190   pBt = (void**)pHdr;
15191   pBt -= pHdr->nBacktraceSlots;
15192   sqlite3_mutex_enter(mem.mutex);
15193   if( pHdr->pPrev ){
15194     assert( pHdr->pPrev->pNext==pHdr );
15195     pHdr->pPrev->pNext = pHdr->pNext;
15196   }else{
15197     assert( mem.pFirst==pHdr );
15198     mem.pFirst = pHdr->pNext;
15199   }
15200   if( pHdr->pNext ){
15201     assert( pHdr->pNext->pPrev==pHdr );
15202     pHdr->pNext->pPrev = pHdr->pPrev;
15203   }else{
15204     assert( mem.pLast==pHdr );
15205     mem.pLast = pHdr->pPrev;
15206   }
15207   z = (char*)pBt;
15208   z -= pHdr->nTitle;
15209   adjustStats(pHdr->iSize, -1);
15210   randomFill(z, sizeof(void*)*pHdr->nBacktraceSlots + sizeof(*pHdr) +
15211                 pHdr->iSize + sizeof(int) + pHdr->nTitle);
15212   free(z);
15213   sqlite3_mutex_leave(mem.mutex);
15214 }
15215 
15216 /*
15217 ** Change the size of an existing memory allocation.
15218 **
15219 ** For this debugging implementation, we *always* make a copy of the
15220 ** allocation into a new place in memory.  In this way, if the
15221 ** higher level code is using pointer to the old allocation, it is
15222 ** much more likely to break and we are much more liking to find
15223 ** the error.
15224 */
sqlite3MemRealloc(void * pPrior,int nByte)15225 static void *sqlite3MemRealloc(void *pPrior, int nByte){
15226   struct MemBlockHdr *pOldHdr;
15227   void *pNew;
15228   assert( mem.disallow==0 );
15229   assert( (nByte & 7)==0 );     /* EV: R-46199-30249 */
15230   pOldHdr = sqlite3MemsysGetHeader(pPrior);
15231   pNew = sqlite3MemMalloc(nByte);
15232   if( pNew ){
15233     memcpy(pNew, pPrior, nByte<pOldHdr->iSize ? nByte : pOldHdr->iSize);
15234     if( nByte>pOldHdr->iSize ){
15235       randomFill(&((char*)pNew)[pOldHdr->iSize], nByte - pOldHdr->iSize);
15236     }
15237     sqlite3MemFree(pPrior);
15238   }
15239   return pNew;
15240 }
15241 
15242 /*
15243 ** Populate the low-level memory allocation function pointers in
15244 ** sqlite3GlobalConfig.m with pointers to the routines in this file.
15245 */
sqlite3MemSetDefault(void)15246 SQLITE_PRIVATE void sqlite3MemSetDefault(void){
15247   static const sqlite3_mem_methods defaultMethods = {
15248      sqlite3MemMalloc,
15249      sqlite3MemFree,
15250      sqlite3MemRealloc,
15251      sqlite3MemSize,
15252      sqlite3MemRoundup,
15253      sqlite3MemInit,
15254      sqlite3MemShutdown,
15255      0
15256   };
15257   sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods);
15258 }
15259 
15260 /*
15261 ** Set the "type" of an allocation.
15262 */
sqlite3MemdebugSetType(void * p,u8 eType)15263 SQLITE_PRIVATE void sqlite3MemdebugSetType(void *p, u8 eType){
15264   if( p && sqlite3GlobalConfig.m.xMalloc==sqlite3MemMalloc ){
15265     struct MemBlockHdr *pHdr;
15266     pHdr = sqlite3MemsysGetHeader(p);
15267     assert( pHdr->iForeGuard==FOREGUARD );
15268     pHdr->eType = eType;
15269   }
15270 }
15271 
15272 /*
15273 ** Return TRUE if the mask of type in eType matches the type of the
15274 ** allocation p.  Also return true if p==NULL.
15275 **
15276 ** This routine is designed for use within an assert() statement, to
15277 ** verify the type of an allocation.  For example:
15278 **
15279 **     assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) );
15280 */
sqlite3MemdebugHasType(void * p,u8 eType)15281 SQLITE_PRIVATE int sqlite3MemdebugHasType(void *p, u8 eType){
15282   int rc = 1;
15283   if( p && sqlite3GlobalConfig.m.xMalloc==sqlite3MemMalloc ){
15284     struct MemBlockHdr *pHdr;
15285     pHdr = sqlite3MemsysGetHeader(p);
15286     assert( pHdr->iForeGuard==FOREGUARD );         /* Allocation is valid */
15287     if( (pHdr->eType&eType)==0 ){
15288       rc = 0;
15289     }
15290   }
15291   return rc;
15292 }
15293 
15294 /*
15295 ** Return TRUE if the mask of type in eType matches no bits of the type of the
15296 ** allocation p.  Also return true if p==NULL.
15297 **
15298 ** This routine is designed for use within an assert() statement, to
15299 ** verify the type of an allocation.  For example:
15300 **
15301 **     assert( sqlite3MemdebugNoType(p, MEMTYPE_DB) );
15302 */
sqlite3MemdebugNoType(void * p,u8 eType)15303 SQLITE_PRIVATE int sqlite3MemdebugNoType(void *p, u8 eType){
15304   int rc = 1;
15305   if( p && sqlite3GlobalConfig.m.xMalloc==sqlite3MemMalloc ){
15306     struct MemBlockHdr *pHdr;
15307     pHdr = sqlite3MemsysGetHeader(p);
15308     assert( pHdr->iForeGuard==FOREGUARD );         /* Allocation is valid */
15309     if( (pHdr->eType&eType)!=0 ){
15310       rc = 0;
15311     }
15312   }
15313   return rc;
15314 }
15315 
15316 /*
15317 ** Set the number of backtrace levels kept for each allocation.
15318 ** A value of zero turns off backtracing.  The number is always rounded
15319 ** up to a multiple of 2.
15320 */
sqlite3MemdebugBacktrace(int depth)15321 SQLITE_PRIVATE void sqlite3MemdebugBacktrace(int depth){
15322   if( depth<0 ){ depth = 0; }
15323   if( depth>20 ){ depth = 20; }
15324   depth = (depth+1)&0xfe;
15325   mem.nBacktrace = depth;
15326 }
15327 
sqlite3MemdebugBacktraceCallback(void (* xBacktrace)(int,int,void **))15328 SQLITE_PRIVATE void sqlite3MemdebugBacktraceCallback(void (*xBacktrace)(int, int, void **)){
15329   mem.xBacktrace = xBacktrace;
15330 }
15331 
15332 /*
15333 ** Set the title string for subsequent allocations.
15334 */
sqlite3MemdebugSettitle(const char * zTitle)15335 SQLITE_PRIVATE void sqlite3MemdebugSettitle(const char *zTitle){
15336   unsigned int n = sqlite3Strlen30(zTitle) + 1;
15337   sqlite3_mutex_enter(mem.mutex);
15338   if( n>=sizeof(mem.zTitle) ) n = sizeof(mem.zTitle)-1;
15339   memcpy(mem.zTitle, zTitle, n);
15340   mem.zTitle[n] = 0;
15341   mem.nTitle = ROUND8(n);
15342   sqlite3_mutex_leave(mem.mutex);
15343 }
15344 
sqlite3MemdebugSync()15345 SQLITE_PRIVATE void sqlite3MemdebugSync(){
15346   struct MemBlockHdr *pHdr;
15347   for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){
15348     void **pBt = (void**)pHdr;
15349     pBt -= pHdr->nBacktraceSlots;
15350     mem.xBacktrace(pHdr->iSize, pHdr->nBacktrace-1, &pBt[1]);
15351   }
15352 }
15353 
15354 /*
15355 ** Open the file indicated and write a log of all unfreed memory
15356 ** allocations into that log.
15357 */
sqlite3MemdebugDump(const char * zFilename)15358 SQLITE_PRIVATE void sqlite3MemdebugDump(const char *zFilename){
15359   FILE *out;
15360   struct MemBlockHdr *pHdr;
15361   void **pBt;
15362   int i;
15363   out = fopen(zFilename, "w");
15364   if( out==0 ){
15365     fprintf(stderr, "** Unable to output memory debug output log: %s **\n",
15366                     zFilename);
15367     return;
15368   }
15369   for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){
15370     char *z = (char*)pHdr;
15371     z -= pHdr->nBacktraceSlots*sizeof(void*) + pHdr->nTitle;
15372     fprintf(out, "**** %lld bytes at %p from %s ****\n",
15373             pHdr->iSize, &pHdr[1], pHdr->nTitle ? z : "???");
15374     if( pHdr->nBacktrace ){
15375       fflush(out);
15376       pBt = (void**)pHdr;
15377       pBt -= pHdr->nBacktraceSlots;
15378       backtrace_symbols_fd(pBt, pHdr->nBacktrace, fileno(out));
15379       fprintf(out, "\n");
15380     }
15381   }
15382   fprintf(out, "COUNTS:\n");
15383   for(i=0; i<NCSIZE-1; i++){
15384     if( mem.nAlloc[i] ){
15385       fprintf(out, "   %5d: %10d %10d %10d\n",
15386             i*8, mem.nAlloc[i], mem.nCurrent[i], mem.mxCurrent[i]);
15387     }
15388   }
15389   if( mem.nAlloc[NCSIZE-1] ){
15390     fprintf(out, "   %5d: %10d %10d %10d\n",
15391              NCSIZE*8-8, mem.nAlloc[NCSIZE-1],
15392              mem.nCurrent[NCSIZE-1], mem.mxCurrent[NCSIZE-1]);
15393   }
15394   fclose(out);
15395 }
15396 
15397 /*
15398 ** Return the number of times sqlite3MemMalloc() has been called.
15399 */
sqlite3MemdebugMallocCount()15400 SQLITE_PRIVATE int sqlite3MemdebugMallocCount(){
15401   int i;
15402   int nTotal = 0;
15403   for(i=0; i<NCSIZE; i++){
15404     nTotal += mem.nAlloc[i];
15405   }
15406   return nTotal;
15407 }
15408 
15409 
15410 #endif /* SQLITE_MEMDEBUG */
15411 
15412 /************** End of mem2.c ************************************************/
15413 /************** Begin file mem3.c ********************************************/
15414 /*
15415 ** 2007 October 14
15416 **
15417 ** The author disclaims copyright to this source code.  In place of
15418 ** a legal notice, here is a blessing:
15419 **
15420 **    May you do good and not evil.
15421 **    May you find forgiveness for yourself and forgive others.
15422 **    May you share freely, never taking more than you give.
15423 **
15424 *************************************************************************
15425 ** This file contains the C functions that implement a memory
15426 ** allocation subsystem for use by SQLite.
15427 **
15428 ** This version of the memory allocation subsystem omits all
15429 ** use of malloc(). The SQLite user supplies a block of memory
15430 ** before calling sqlite3_initialize() from which allocations
15431 ** are made and returned by the xMalloc() and xRealloc()
15432 ** implementations. Once sqlite3_initialize() has been called,
15433 ** the amount of memory available to SQLite is fixed and cannot
15434 ** be changed.
15435 **
15436 ** This version of the memory allocation subsystem is included
15437 ** in the build only if SQLITE_ENABLE_MEMSYS3 is defined.
15438 */
15439 
15440 /*
15441 ** This version of the memory allocator is only built into the library
15442 ** SQLITE_ENABLE_MEMSYS3 is defined. Defining this symbol does not
15443 ** mean that the library will use a memory-pool by default, just that
15444 ** it is available. The mempool allocator is activated by calling
15445 ** sqlite3_config().
15446 */
15447 #ifdef SQLITE_ENABLE_MEMSYS3
15448 
15449 /*
15450 ** Maximum size (in Mem3Blocks) of a "small" chunk.
15451 */
15452 #define MX_SMALL 10
15453 
15454 
15455 /*
15456 ** Number of freelist hash slots
15457 */
15458 #define N_HASH  61
15459 
15460 /*
15461 ** A memory allocation (also called a "chunk") consists of two or
15462 ** more blocks where each block is 8 bytes.  The first 8 bytes are
15463 ** a header that is not returned to the user.
15464 **
15465 ** A chunk is two or more blocks that is either checked out or
15466 ** free.  The first block has format u.hdr.  u.hdr.size4x is 4 times the
15467 ** size of the allocation in blocks if the allocation is free.
15468 ** The u.hdr.size4x&1 bit is true if the chunk is checked out and
15469 ** false if the chunk is on the freelist.  The u.hdr.size4x&2 bit
15470 ** is true if the previous chunk is checked out and false if the
15471 ** previous chunk is free.  The u.hdr.prevSize field is the size of
15472 ** the previous chunk in blocks if the previous chunk is on the
15473 ** freelist. If the previous chunk is checked out, then
15474 ** u.hdr.prevSize can be part of the data for that chunk and should
15475 ** not be read or written.
15476 **
15477 ** We often identify a chunk by its index in mem3.aPool[].  When
15478 ** this is done, the chunk index refers to the second block of
15479 ** the chunk.  In this way, the first chunk has an index of 1.
15480 ** A chunk index of 0 means "no such chunk" and is the equivalent
15481 ** of a NULL pointer.
15482 **
15483 ** The second block of free chunks is of the form u.list.  The
15484 ** two fields form a double-linked list of chunks of related sizes.
15485 ** Pointers to the head of the list are stored in mem3.aiSmall[]
15486 ** for smaller chunks and mem3.aiHash[] for larger chunks.
15487 **
15488 ** The second block of a chunk is user data if the chunk is checked
15489 ** out.  If a chunk is checked out, the user data may extend into
15490 ** the u.hdr.prevSize value of the following chunk.
15491 */
15492 typedef struct Mem3Block Mem3Block;
15493 struct Mem3Block {
15494   union {
15495     struct {
15496       u32 prevSize;   /* Size of previous chunk in Mem3Block elements */
15497       u32 size4x;     /* 4x the size of current chunk in Mem3Block elements */
15498     } hdr;
15499     struct {
15500       u32 next;       /* Index in mem3.aPool[] of next free chunk */
15501       u32 prev;       /* Index in mem3.aPool[] of previous free chunk */
15502     } list;
15503   } u;
15504 };
15505 
15506 /*
15507 ** All of the static variables used by this module are collected
15508 ** into a single structure named "mem3".  This is to keep the
15509 ** static variables organized and to reduce namespace pollution
15510 ** when this module is combined with other in the amalgamation.
15511 */
15512 static SQLITE_WSD struct Mem3Global {
15513   /*
15514   ** Memory available for allocation. nPool is the size of the array
15515   ** (in Mem3Blocks) pointed to by aPool less 2.
15516   */
15517   u32 nPool;
15518   Mem3Block *aPool;
15519 
15520   /*
15521   ** True if we are evaluating an out-of-memory callback.
15522   */
15523   int alarmBusy;
15524 
15525   /*
15526   ** Mutex to control access to the memory allocation subsystem.
15527   */
15528   sqlite3_mutex *mutex;
15529 
15530   /*
15531   ** The minimum amount of free space that we have seen.
15532   */
15533   u32 mnMaster;
15534 
15535   /*
15536   ** iMaster is the index of the master chunk.  Most new allocations
15537   ** occur off of this chunk.  szMaster is the size (in Mem3Blocks)
15538   ** of the current master.  iMaster is 0 if there is not master chunk.
15539   ** The master chunk is not in either the aiHash[] or aiSmall[].
15540   */
15541   u32 iMaster;
15542   u32 szMaster;
15543 
15544   /*
15545   ** Array of lists of free blocks according to the block size
15546   ** for smaller chunks, or a hash on the block size for larger
15547   ** chunks.
15548   */
15549   u32 aiSmall[MX_SMALL-1];   /* For sizes 2 through MX_SMALL, inclusive */
15550   u32 aiHash[N_HASH];        /* For sizes MX_SMALL+1 and larger */
15551 } mem3 = { 97535575 };
15552 
15553 #define mem3 GLOBAL(struct Mem3Global, mem3)
15554 
15555 /*
15556 ** Unlink the chunk at mem3.aPool[i] from list it is currently
15557 ** on.  *pRoot is the list that i is a member of.
15558 */
memsys3UnlinkFromList(u32 i,u32 * pRoot)15559 static void memsys3UnlinkFromList(u32 i, u32 *pRoot){
15560   u32 next = mem3.aPool[i].u.list.next;
15561   u32 prev = mem3.aPool[i].u.list.prev;
15562   assert( sqlite3_mutex_held(mem3.mutex) );
15563   if( prev==0 ){
15564     *pRoot = next;
15565   }else{
15566     mem3.aPool[prev].u.list.next = next;
15567   }
15568   if( next ){
15569     mem3.aPool[next].u.list.prev = prev;
15570   }
15571   mem3.aPool[i].u.list.next = 0;
15572   mem3.aPool[i].u.list.prev = 0;
15573 }
15574 
15575 /*
15576 ** Unlink the chunk at index i from
15577 ** whatever list is currently a member of.
15578 */
memsys3Unlink(u32 i)15579 static void memsys3Unlink(u32 i){
15580   u32 size, hash;
15581   assert( sqlite3_mutex_held(mem3.mutex) );
15582   assert( (mem3.aPool[i-1].u.hdr.size4x & 1)==0 );
15583   assert( i>=1 );
15584   size = mem3.aPool[i-1].u.hdr.size4x/4;
15585   assert( size==mem3.aPool[i+size-1].u.hdr.prevSize );
15586   assert( size>=2 );
15587   if( size <= MX_SMALL ){
15588     memsys3UnlinkFromList(i, &mem3.aiSmall[size-2]);
15589   }else{
15590     hash = size % N_HASH;
15591     memsys3UnlinkFromList(i, &mem3.aiHash[hash]);
15592   }
15593 }
15594 
15595 /*
15596 ** Link the chunk at mem3.aPool[i] so that is on the list rooted
15597 ** at *pRoot.
15598 */
memsys3LinkIntoList(u32 i,u32 * pRoot)15599 static void memsys3LinkIntoList(u32 i, u32 *pRoot){
15600   assert( sqlite3_mutex_held(mem3.mutex) );
15601   mem3.aPool[i].u.list.next = *pRoot;
15602   mem3.aPool[i].u.list.prev = 0;
15603   if( *pRoot ){
15604     mem3.aPool[*pRoot].u.list.prev = i;
15605   }
15606   *pRoot = i;
15607 }
15608 
15609 /*
15610 ** Link the chunk at index i into either the appropriate
15611 ** small chunk list, or into the large chunk hash table.
15612 */
memsys3Link(u32 i)15613 static void memsys3Link(u32 i){
15614   u32 size, hash;
15615   assert( sqlite3_mutex_held(mem3.mutex) );
15616   assert( i>=1 );
15617   assert( (mem3.aPool[i-1].u.hdr.size4x & 1)==0 );
15618   size = mem3.aPool[i-1].u.hdr.size4x/4;
15619   assert( size==mem3.aPool[i+size-1].u.hdr.prevSize );
15620   assert( size>=2 );
15621   if( size <= MX_SMALL ){
15622     memsys3LinkIntoList(i, &mem3.aiSmall[size-2]);
15623   }else{
15624     hash = size % N_HASH;
15625     memsys3LinkIntoList(i, &mem3.aiHash[hash]);
15626   }
15627 }
15628 
15629 /*
15630 ** If the STATIC_MEM mutex is not already held, obtain it now. The mutex
15631 ** will already be held (obtained by code in malloc.c) if
15632 ** sqlite3GlobalConfig.bMemStat is true.
15633 */
memsys3Enter(void)15634 static void memsys3Enter(void){
15635   if( sqlite3GlobalConfig.bMemstat==0 && mem3.mutex==0 ){
15636     mem3.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
15637   }
15638   sqlite3_mutex_enter(mem3.mutex);
15639 }
memsys3Leave(void)15640 static void memsys3Leave(void){
15641   sqlite3_mutex_leave(mem3.mutex);
15642 }
15643 
15644 /*
15645 ** Called when we are unable to satisfy an allocation of nBytes.
15646 */
memsys3OutOfMemory(int nByte)15647 static void memsys3OutOfMemory(int nByte){
15648   if( !mem3.alarmBusy ){
15649     mem3.alarmBusy = 1;
15650     assert( sqlite3_mutex_held(mem3.mutex) );
15651     sqlite3_mutex_leave(mem3.mutex);
15652     sqlite3_release_memory(nByte);
15653     sqlite3_mutex_enter(mem3.mutex);
15654     mem3.alarmBusy = 0;
15655   }
15656 }
15657 
15658 
15659 /*
15660 ** Chunk i is a free chunk that has been unlinked.  Adjust its
15661 ** size parameters for check-out and return a pointer to the
15662 ** user portion of the chunk.
15663 */
memsys3Checkout(u32 i,u32 nBlock)15664 static void *memsys3Checkout(u32 i, u32 nBlock){
15665   u32 x;
15666   assert( sqlite3_mutex_held(mem3.mutex) );
15667   assert( i>=1 );
15668   assert( mem3.aPool[i-1].u.hdr.size4x/4==nBlock );
15669   assert( mem3.aPool[i+nBlock-1].u.hdr.prevSize==nBlock );
15670   x = mem3.aPool[i-1].u.hdr.size4x;
15671   mem3.aPool[i-1].u.hdr.size4x = nBlock*4 | 1 | (x&2);
15672   mem3.aPool[i+nBlock-1].u.hdr.prevSize = nBlock;
15673   mem3.aPool[i+nBlock-1].u.hdr.size4x |= 2;
15674   return &mem3.aPool[i];
15675 }
15676 
15677 /*
15678 ** Carve a piece off of the end of the mem3.iMaster free chunk.
15679 ** Return a pointer to the new allocation.  Or, if the master chunk
15680 ** is not large enough, return 0.
15681 */
memsys3FromMaster(u32 nBlock)15682 static void *memsys3FromMaster(u32 nBlock){
15683   assert( sqlite3_mutex_held(mem3.mutex) );
15684   assert( mem3.szMaster>=nBlock );
15685   if( nBlock>=mem3.szMaster-1 ){
15686     /* Use the entire master */
15687     void *p = memsys3Checkout(mem3.iMaster, mem3.szMaster);
15688     mem3.iMaster = 0;
15689     mem3.szMaster = 0;
15690     mem3.mnMaster = 0;
15691     return p;
15692   }else{
15693     /* Split the master block.  Return the tail. */
15694     u32 newi, x;
15695     newi = mem3.iMaster + mem3.szMaster - nBlock;
15696     assert( newi > mem3.iMaster+1 );
15697     mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.prevSize = nBlock;
15698     mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.size4x |= 2;
15699     mem3.aPool[newi-1].u.hdr.size4x = nBlock*4 + 1;
15700     mem3.szMaster -= nBlock;
15701     mem3.aPool[newi-1].u.hdr.prevSize = mem3.szMaster;
15702     x = mem3.aPool[mem3.iMaster-1].u.hdr.size4x & 2;
15703     mem3.aPool[mem3.iMaster-1].u.hdr.size4x = mem3.szMaster*4 | x;
15704     if( mem3.szMaster < mem3.mnMaster ){
15705       mem3.mnMaster = mem3.szMaster;
15706     }
15707     return (void*)&mem3.aPool[newi];
15708   }
15709 }
15710 
15711 /*
15712 ** *pRoot is the head of a list of free chunks of the same size
15713 ** or same size hash.  In other words, *pRoot is an entry in either
15714 ** mem3.aiSmall[] or mem3.aiHash[].
15715 **
15716 ** This routine examines all entries on the given list and tries
15717 ** to coalesce each entries with adjacent free chunks.
15718 **
15719 ** If it sees a chunk that is larger than mem3.iMaster, it replaces
15720 ** the current mem3.iMaster with the new larger chunk.  In order for
15721 ** this mem3.iMaster replacement to work, the master chunk must be
15722 ** linked into the hash tables.  That is not the normal state of
15723 ** affairs, of course.  The calling routine must link the master
15724 ** chunk before invoking this routine, then must unlink the (possibly
15725 ** changed) master chunk once this routine has finished.
15726 */
memsys3Merge(u32 * pRoot)15727 static void memsys3Merge(u32 *pRoot){
15728   u32 iNext, prev, size, i, x;
15729 
15730   assert( sqlite3_mutex_held(mem3.mutex) );
15731   for(i=*pRoot; i>0; i=iNext){
15732     iNext = mem3.aPool[i].u.list.next;
15733     size = mem3.aPool[i-1].u.hdr.size4x;
15734     assert( (size&1)==0 );
15735     if( (size&2)==0 ){
15736       memsys3UnlinkFromList(i, pRoot);
15737       assert( i > mem3.aPool[i-1].u.hdr.prevSize );
15738       prev = i - mem3.aPool[i-1].u.hdr.prevSize;
15739       if( prev==iNext ){
15740         iNext = mem3.aPool[prev].u.list.next;
15741       }
15742       memsys3Unlink(prev);
15743       size = i + size/4 - prev;
15744       x = mem3.aPool[prev-1].u.hdr.size4x & 2;
15745       mem3.aPool[prev-1].u.hdr.size4x = size*4 | x;
15746       mem3.aPool[prev+size-1].u.hdr.prevSize = size;
15747       memsys3Link(prev);
15748       i = prev;
15749     }else{
15750       size /= 4;
15751     }
15752     if( size>mem3.szMaster ){
15753       mem3.iMaster = i;
15754       mem3.szMaster = size;
15755     }
15756   }
15757 }
15758 
15759 /*
15760 ** Return a block of memory of at least nBytes in size.
15761 ** Return NULL if unable.
15762 **
15763 ** This function assumes that the necessary mutexes, if any, are
15764 ** already held by the caller. Hence "Unsafe".
15765 */
memsys3MallocUnsafe(int nByte)15766 static void *memsys3MallocUnsafe(int nByte){
15767   u32 i;
15768   u32 nBlock;
15769   u32 toFree;
15770 
15771   assert( sqlite3_mutex_held(mem3.mutex) );
15772   assert( sizeof(Mem3Block)==8 );
15773   if( nByte<=12 ){
15774     nBlock = 2;
15775   }else{
15776     nBlock = (nByte + 11)/8;
15777   }
15778   assert( nBlock>=2 );
15779 
15780   /* STEP 1:
15781   ** Look for an entry of the correct size in either the small
15782   ** chunk table or in the large chunk hash table.  This is
15783   ** successful most of the time (about 9 times out of 10).
15784   */
15785   if( nBlock <= MX_SMALL ){
15786     i = mem3.aiSmall[nBlock-2];
15787     if( i>0 ){
15788       memsys3UnlinkFromList(i, &mem3.aiSmall[nBlock-2]);
15789       return memsys3Checkout(i, nBlock);
15790     }
15791   }else{
15792     int hash = nBlock % N_HASH;
15793     for(i=mem3.aiHash[hash]; i>0; i=mem3.aPool[i].u.list.next){
15794       if( mem3.aPool[i-1].u.hdr.size4x/4==nBlock ){
15795         memsys3UnlinkFromList(i, &mem3.aiHash[hash]);
15796         return memsys3Checkout(i, nBlock);
15797       }
15798     }
15799   }
15800 
15801   /* STEP 2:
15802   ** Try to satisfy the allocation by carving a piece off of the end
15803   ** of the master chunk.  This step usually works if step 1 fails.
15804   */
15805   if( mem3.szMaster>=nBlock ){
15806     return memsys3FromMaster(nBlock);
15807   }
15808 
15809 
15810   /* STEP 3:
15811   ** Loop through the entire memory pool.  Coalesce adjacent free
15812   ** chunks.  Recompute the master chunk as the largest free chunk.
15813   ** Then try again to satisfy the allocation by carving a piece off
15814   ** of the end of the master chunk.  This step happens very
15815   ** rarely (we hope!)
15816   */
15817   for(toFree=nBlock*16; toFree<(mem3.nPool*16); toFree *= 2){
15818     memsys3OutOfMemory(toFree);
15819     if( mem3.iMaster ){
15820       memsys3Link(mem3.iMaster);
15821       mem3.iMaster = 0;
15822       mem3.szMaster = 0;
15823     }
15824     for(i=0; i<N_HASH; i++){
15825       memsys3Merge(&mem3.aiHash[i]);
15826     }
15827     for(i=0; i<MX_SMALL-1; i++){
15828       memsys3Merge(&mem3.aiSmall[i]);
15829     }
15830     if( mem3.szMaster ){
15831       memsys3Unlink(mem3.iMaster);
15832       if( mem3.szMaster>=nBlock ){
15833         return memsys3FromMaster(nBlock);
15834       }
15835     }
15836   }
15837 
15838   /* If none of the above worked, then we fail. */
15839   return 0;
15840 }
15841 
15842 /*
15843 ** Free an outstanding memory allocation.
15844 **
15845 ** This function assumes that the necessary mutexes, if any, are
15846 ** already held by the caller. Hence "Unsafe".
15847 */
memsys3FreeUnsafe(void * pOld)15848 void memsys3FreeUnsafe(void *pOld){
15849   Mem3Block *p = (Mem3Block*)pOld;
15850   int i;
15851   u32 size, x;
15852   assert( sqlite3_mutex_held(mem3.mutex) );
15853   assert( p>mem3.aPool && p<&mem3.aPool[mem3.nPool] );
15854   i = p - mem3.aPool;
15855   assert( (mem3.aPool[i-1].u.hdr.size4x&1)==1 );
15856   size = mem3.aPool[i-1].u.hdr.size4x/4;
15857   assert( i+size<=mem3.nPool+1 );
15858   mem3.aPool[i-1].u.hdr.size4x &= ~1;
15859   mem3.aPool[i+size-1].u.hdr.prevSize = size;
15860   mem3.aPool[i+size-1].u.hdr.size4x &= ~2;
15861   memsys3Link(i);
15862 
15863   /* Try to expand the master using the newly freed chunk */
15864   if( mem3.iMaster ){
15865     while( (mem3.aPool[mem3.iMaster-1].u.hdr.size4x&2)==0 ){
15866       size = mem3.aPool[mem3.iMaster-1].u.hdr.prevSize;
15867       mem3.iMaster -= size;
15868       mem3.szMaster += size;
15869       memsys3Unlink(mem3.iMaster);
15870       x = mem3.aPool[mem3.iMaster-1].u.hdr.size4x & 2;
15871       mem3.aPool[mem3.iMaster-1].u.hdr.size4x = mem3.szMaster*4 | x;
15872       mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.prevSize = mem3.szMaster;
15873     }
15874     x = mem3.aPool[mem3.iMaster-1].u.hdr.size4x & 2;
15875     while( (mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.size4x&1)==0 ){
15876       memsys3Unlink(mem3.iMaster+mem3.szMaster);
15877       mem3.szMaster += mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.size4x/4;
15878       mem3.aPool[mem3.iMaster-1].u.hdr.size4x = mem3.szMaster*4 | x;
15879       mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.prevSize = mem3.szMaster;
15880     }
15881   }
15882 }
15883 
15884 /*
15885 ** Return the size of an outstanding allocation, in bytes.  The
15886 ** size returned omits the 8-byte header overhead.  This only
15887 ** works for chunks that are currently checked out.
15888 */
memsys3Size(void * p)15889 static int memsys3Size(void *p){
15890   Mem3Block *pBlock;
15891   if( p==0 ) return 0;
15892   pBlock = (Mem3Block*)p;
15893   assert( (pBlock[-1].u.hdr.size4x&1)!=0 );
15894   return (pBlock[-1].u.hdr.size4x&~3)*2 - 4;
15895 }
15896 
15897 /*
15898 ** Round up a request size to the next valid allocation size.
15899 */
memsys3Roundup(int n)15900 static int memsys3Roundup(int n){
15901   if( n<=12 ){
15902     return 12;
15903   }else{
15904     return ((n+11)&~7) - 4;
15905   }
15906 }
15907 
15908 /*
15909 ** Allocate nBytes of memory.
15910 */
memsys3Malloc(int nBytes)15911 static void *memsys3Malloc(int nBytes){
15912   sqlite3_int64 *p;
15913   assert( nBytes>0 );          /* malloc.c filters out 0 byte requests */
15914   memsys3Enter();
15915   p = memsys3MallocUnsafe(nBytes);
15916   memsys3Leave();
15917   return (void*)p;
15918 }
15919 
15920 /*
15921 ** Free memory.
15922 */
memsys3Free(void * pPrior)15923 void memsys3Free(void *pPrior){
15924   assert( pPrior );
15925   memsys3Enter();
15926   memsys3FreeUnsafe(pPrior);
15927   memsys3Leave();
15928 }
15929 
15930 /*
15931 ** Change the size of an existing memory allocation
15932 */
memsys3Realloc(void * pPrior,int nBytes)15933 void *memsys3Realloc(void *pPrior, int nBytes){
15934   int nOld;
15935   void *p;
15936   if( pPrior==0 ){
15937     return sqlite3_malloc(nBytes);
15938   }
15939   if( nBytes<=0 ){
15940     sqlite3_free(pPrior);
15941     return 0;
15942   }
15943   nOld = memsys3Size(pPrior);
15944   if( nBytes<=nOld && nBytes>=nOld-128 ){
15945     return pPrior;
15946   }
15947   memsys3Enter();
15948   p = memsys3MallocUnsafe(nBytes);
15949   if( p ){
15950     if( nOld<nBytes ){
15951       memcpy(p, pPrior, nOld);
15952     }else{
15953       memcpy(p, pPrior, nBytes);
15954     }
15955     memsys3FreeUnsafe(pPrior);
15956   }
15957   memsys3Leave();
15958   return p;
15959 }
15960 
15961 /*
15962 ** Initialize this module.
15963 */
memsys3Init(void * NotUsed)15964 static int memsys3Init(void *NotUsed){
15965   UNUSED_PARAMETER(NotUsed);
15966   if( !sqlite3GlobalConfig.pHeap ){
15967     return SQLITE_ERROR;
15968   }
15969 
15970   /* Store a pointer to the memory block in global structure mem3. */
15971   assert( sizeof(Mem3Block)==8 );
15972   mem3.aPool = (Mem3Block *)sqlite3GlobalConfig.pHeap;
15973   mem3.nPool = (sqlite3GlobalConfig.nHeap / sizeof(Mem3Block)) - 2;
15974 
15975   /* Initialize the master block. */
15976   mem3.szMaster = mem3.nPool;
15977   mem3.mnMaster = mem3.szMaster;
15978   mem3.iMaster = 1;
15979   mem3.aPool[0].u.hdr.size4x = (mem3.szMaster<<2) + 2;
15980   mem3.aPool[mem3.nPool].u.hdr.prevSize = mem3.nPool;
15981   mem3.aPool[mem3.nPool].u.hdr.size4x = 1;
15982 
15983   return SQLITE_OK;
15984 }
15985 
15986 /*
15987 ** Deinitialize this module.
15988 */
memsys3Shutdown(void * NotUsed)15989 static void memsys3Shutdown(void *NotUsed){
15990   UNUSED_PARAMETER(NotUsed);
15991   mem3.mutex = 0;
15992   return;
15993 }
15994 
15995 
15996 
15997 /*
15998 ** Open the file indicated and write a log of all unfreed memory
15999 ** allocations into that log.
16000 */
sqlite3Memsys3Dump(const char * zFilename)16001 SQLITE_PRIVATE void sqlite3Memsys3Dump(const char *zFilename){
16002 #ifdef SQLITE_DEBUG
16003   FILE *out;
16004   u32 i, j;
16005   u32 size;
16006   if( zFilename==0 || zFilename[0]==0 ){
16007     out = stdout;
16008   }else{
16009     out = fopen(zFilename, "w");
16010     if( out==0 ){
16011       fprintf(stderr, "** Unable to output memory debug output log: %s **\n",
16012                       zFilename);
16013       return;
16014     }
16015   }
16016   memsys3Enter();
16017   fprintf(out, "CHUNKS:\n");
16018   for(i=1; i<=mem3.nPool; i+=size/4){
16019     size = mem3.aPool[i-1].u.hdr.size4x;
16020     if( size/4<=1 ){
16021       fprintf(out, "%p size error\n", &mem3.aPool[i]);
16022       assert( 0 );
16023       break;
16024     }
16025     if( (size&1)==0 && mem3.aPool[i+size/4-1].u.hdr.prevSize!=size/4 ){
16026       fprintf(out, "%p tail size does not match\n", &mem3.aPool[i]);
16027       assert( 0 );
16028       break;
16029     }
16030     if( ((mem3.aPool[i+size/4-1].u.hdr.size4x&2)>>1)!=(size&1) ){
16031       fprintf(out, "%p tail checkout bit is incorrect\n", &mem3.aPool[i]);
16032       assert( 0 );
16033       break;
16034     }
16035     if( size&1 ){
16036       fprintf(out, "%p %6d bytes checked out\n", &mem3.aPool[i], (size/4)*8-8);
16037     }else{
16038       fprintf(out, "%p %6d bytes free%s\n", &mem3.aPool[i], (size/4)*8-8,
16039                   i==mem3.iMaster ? " **master**" : "");
16040     }
16041   }
16042   for(i=0; i<MX_SMALL-1; i++){
16043     if( mem3.aiSmall[i]==0 ) continue;
16044     fprintf(out, "small(%2d):", i);
16045     for(j = mem3.aiSmall[i]; j>0; j=mem3.aPool[j].u.list.next){
16046       fprintf(out, " %p(%d)", &mem3.aPool[j],
16047               (mem3.aPool[j-1].u.hdr.size4x/4)*8-8);
16048     }
16049     fprintf(out, "\n");
16050   }
16051   for(i=0; i<N_HASH; i++){
16052     if( mem3.aiHash[i]==0 ) continue;
16053     fprintf(out, "hash(%2d):", i);
16054     for(j = mem3.aiHash[i]; j>0; j=mem3.aPool[j].u.list.next){
16055       fprintf(out, " %p(%d)", &mem3.aPool[j],
16056               (mem3.aPool[j-1].u.hdr.size4x/4)*8-8);
16057     }
16058     fprintf(out, "\n");
16059   }
16060   fprintf(out, "master=%d\n", mem3.iMaster);
16061   fprintf(out, "nowUsed=%d\n", mem3.nPool*8 - mem3.szMaster*8);
16062   fprintf(out, "mxUsed=%d\n", mem3.nPool*8 - mem3.mnMaster*8);
16063   sqlite3_mutex_leave(mem3.mutex);
16064   if( out==stdout ){
16065     fflush(stdout);
16066   }else{
16067     fclose(out);
16068   }
16069 #else
16070   UNUSED_PARAMETER(zFilename);
16071 #endif
16072 }
16073 
16074 /*
16075 ** This routine is the only routine in this file with external
16076 ** linkage.
16077 **
16078 ** Populate the low-level memory allocation function pointers in
16079 ** sqlite3GlobalConfig.m with pointers to the routines in this file. The
16080 ** arguments specify the block of memory to manage.
16081 **
16082 ** This routine is only called by sqlite3_config(), and therefore
16083 ** is not required to be threadsafe (it is not).
16084 */
sqlite3MemGetMemsys3(void)16085 SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys3(void){
16086   static const sqlite3_mem_methods mempoolMethods = {
16087      memsys3Malloc,
16088      memsys3Free,
16089      memsys3Realloc,
16090      memsys3Size,
16091      memsys3Roundup,
16092      memsys3Init,
16093      memsys3Shutdown,
16094      0
16095   };
16096   return &mempoolMethods;
16097 }
16098 
16099 #endif /* SQLITE_ENABLE_MEMSYS3 */
16100 
16101 /************** End of mem3.c ************************************************/
16102 /************** Begin file mem5.c ********************************************/
16103 /*
16104 ** 2007 October 14
16105 **
16106 ** The author disclaims copyright to this source code.  In place of
16107 ** a legal notice, here is a blessing:
16108 **
16109 **    May you do good and not evil.
16110 **    May you find forgiveness for yourself and forgive others.
16111 **    May you share freely, never taking more than you give.
16112 **
16113 *************************************************************************
16114 ** This file contains the C functions that implement a memory
16115 ** allocation subsystem for use by SQLite.
16116 **
16117 ** This version of the memory allocation subsystem omits all
16118 ** use of malloc(). The application gives SQLite a block of memory
16119 ** before calling sqlite3_initialize() from which allocations
16120 ** are made and returned by the xMalloc() and xRealloc()
16121 ** implementations. Once sqlite3_initialize() has been called,
16122 ** the amount of memory available to SQLite is fixed and cannot
16123 ** be changed.
16124 **
16125 ** This version of the memory allocation subsystem is included
16126 ** in the build only if SQLITE_ENABLE_MEMSYS5 is defined.
16127 **
16128 ** This memory allocator uses the following algorithm:
16129 **
16130 **   1.  All memory allocations sizes are rounded up to a power of 2.
16131 **
16132 **   2.  If two adjacent free blocks are the halves of a larger block,
16133 **       then the two blocks are coalesed into the single larger block.
16134 **
16135 **   3.  New memory is allocated from the first available free block.
16136 **
16137 ** This algorithm is described in: J. M. Robson. "Bounds for Some Functions
16138 ** Concerning Dynamic Storage Allocation". Journal of the Association for
16139 ** Computing Machinery, Volume 21, Number 8, July 1974, pages 491-499.
16140 **
16141 ** Let n be the size of the largest allocation divided by the minimum
16142 ** allocation size (after rounding all sizes up to a power of 2.)  Let M
16143 ** be the maximum amount of memory ever outstanding at one time.  Let
16144 ** N be the total amount of memory available for allocation.  Robson
16145 ** proved that this memory allocator will never breakdown due to
16146 ** fragmentation as long as the following constraint holds:
16147 **
16148 **      N >=  M*(1 + log2(n)/2) - n + 1
16149 **
16150 ** The sqlite3_status() logic tracks the maximum values of n and M so
16151 ** that an application can, at any time, verify this constraint.
16152 */
16153 
16154 /*
16155 ** This version of the memory allocator is used only when
16156 ** SQLITE_ENABLE_MEMSYS5 is defined.
16157 */
16158 #ifdef SQLITE_ENABLE_MEMSYS5
16159 
16160 /*
16161 ** A minimum allocation is an instance of the following structure.
16162 ** Larger allocations are an array of these structures where the
16163 ** size of the array is a power of 2.
16164 **
16165 ** The size of this object must be a power of two.  That fact is
16166 ** verified in memsys5Init().
16167 */
16168 typedef struct Mem5Link Mem5Link;
16169 struct Mem5Link {
16170   int next;       /* Index of next free chunk */
16171   int prev;       /* Index of previous free chunk */
16172 };
16173 
16174 /*
16175 ** Maximum size of any allocation is ((1<<LOGMAX)*mem5.szAtom). Since
16176 ** mem5.szAtom is always at least 8 and 32-bit integers are used,
16177 ** it is not actually possible to reach this limit.
16178 */
16179 #define LOGMAX 30
16180 
16181 /*
16182 ** Masks used for mem5.aCtrl[] elements.
16183 */
16184 #define CTRL_LOGSIZE  0x1f    /* Log2 Size of this block */
16185 #define CTRL_FREE     0x20    /* True if not checked out */
16186 
16187 /*
16188 ** All of the static variables used by this module are collected
16189 ** into a single structure named "mem5".  This is to keep the
16190 ** static variables organized and to reduce namespace pollution
16191 ** when this module is combined with other in the amalgamation.
16192 */
16193 static SQLITE_WSD struct Mem5Global {
16194   /*
16195   ** Memory available for allocation
16196   */
16197   int szAtom;      /* Smallest possible allocation in bytes */
16198   int nBlock;      /* Number of szAtom sized blocks in zPool */
16199   u8 *zPool;       /* Memory available to be allocated */
16200 
16201   /*
16202   ** Mutex to control access to the memory allocation subsystem.
16203   */
16204   sqlite3_mutex *mutex;
16205 
16206   /*
16207   ** Performance statistics
16208   */
16209   u64 nAlloc;         /* Total number of calls to malloc */
16210   u64 totalAlloc;     /* Total of all malloc calls - includes internal frag */
16211   u64 totalExcess;    /* Total internal fragmentation */
16212   u32 currentOut;     /* Current checkout, including internal fragmentation */
16213   u32 currentCount;   /* Current number of distinct checkouts */
16214   u32 maxOut;         /* Maximum instantaneous currentOut */
16215   u32 maxCount;       /* Maximum instantaneous currentCount */
16216   u32 maxRequest;     /* Largest allocation (exclusive of internal frag) */
16217 
16218   /*
16219   ** Lists of free blocks.  aiFreelist[0] is a list of free blocks of
16220   ** size mem5.szAtom.  aiFreelist[1] holds blocks of size szAtom*2.
16221   ** and so forth.
16222   */
16223   int aiFreelist[LOGMAX+1];
16224 
16225   /*
16226   ** Space for tracking which blocks are checked out and the size
16227   ** of each block.  One byte per block.
16228   */
16229   u8 *aCtrl;
16230 
16231 } mem5;
16232 
16233 /*
16234 ** Access the static variable through a macro for SQLITE_OMIT_WSD
16235 */
16236 #define mem5 GLOBAL(struct Mem5Global, mem5)
16237 
16238 /*
16239 ** Assuming mem5.zPool is divided up into an array of Mem5Link
16240 ** structures, return a pointer to the idx-th such lik.
16241 */
16242 #define MEM5LINK(idx) ((Mem5Link *)(&mem5.zPool[(idx)*mem5.szAtom]))
16243 
16244 /*
16245 ** Unlink the chunk at mem5.aPool[i] from list it is currently
16246 ** on.  It should be found on mem5.aiFreelist[iLogsize].
16247 */
memsys5Unlink(int i,int iLogsize)16248 static void memsys5Unlink(int i, int iLogsize){
16249   int next, prev;
16250   assert( i>=0 && i<mem5.nBlock );
16251   assert( iLogsize>=0 && iLogsize<=LOGMAX );
16252   assert( (mem5.aCtrl[i] & CTRL_LOGSIZE)==iLogsize );
16253 
16254   next = MEM5LINK(i)->next;
16255   prev = MEM5LINK(i)->prev;
16256   if( prev<0 ){
16257     mem5.aiFreelist[iLogsize] = next;
16258   }else{
16259     MEM5LINK(prev)->next = next;
16260   }
16261   if( next>=0 ){
16262     MEM5LINK(next)->prev = prev;
16263   }
16264 }
16265 
16266 /*
16267 ** Link the chunk at mem5.aPool[i] so that is on the iLogsize
16268 ** free list.
16269 */
memsys5Link(int i,int iLogsize)16270 static void memsys5Link(int i, int iLogsize){
16271   int x;
16272   assert( sqlite3_mutex_held(mem5.mutex) );
16273   assert( i>=0 && i<mem5.nBlock );
16274   assert( iLogsize>=0 && iLogsize<=LOGMAX );
16275   assert( (mem5.aCtrl[i] & CTRL_LOGSIZE)==iLogsize );
16276 
16277   x = MEM5LINK(i)->next = mem5.aiFreelist[iLogsize];
16278   MEM5LINK(i)->prev = -1;
16279   if( x>=0 ){
16280     assert( x<mem5.nBlock );
16281     MEM5LINK(x)->prev = i;
16282   }
16283   mem5.aiFreelist[iLogsize] = i;
16284 }
16285 
16286 /*
16287 ** If the STATIC_MEM mutex is not already held, obtain it now. The mutex
16288 ** will already be held (obtained by code in malloc.c) if
16289 ** sqlite3GlobalConfig.bMemStat is true.
16290 */
memsys5Enter(void)16291 static void memsys5Enter(void){
16292   sqlite3_mutex_enter(mem5.mutex);
16293 }
memsys5Leave(void)16294 static void memsys5Leave(void){
16295   sqlite3_mutex_leave(mem5.mutex);
16296 }
16297 
16298 /*
16299 ** Return the size of an outstanding allocation, in bytes.  The
16300 ** size returned omits the 8-byte header overhead.  This only
16301 ** works for chunks that are currently checked out.
16302 */
memsys5Size(void * p)16303 static int memsys5Size(void *p){
16304   int iSize = 0;
16305   if( p ){
16306     int i = ((u8 *)p-mem5.zPool)/mem5.szAtom;
16307     assert( i>=0 && i<mem5.nBlock );
16308     iSize = mem5.szAtom * (1 << (mem5.aCtrl[i]&CTRL_LOGSIZE));
16309   }
16310   return iSize;
16311 }
16312 
16313 /*
16314 ** Find the first entry on the freelist iLogsize.  Unlink that
16315 ** entry and return its index.
16316 */
memsys5UnlinkFirst(int iLogsize)16317 static int memsys5UnlinkFirst(int iLogsize){
16318   int i;
16319   int iFirst;
16320 
16321   assert( iLogsize>=0 && iLogsize<=LOGMAX );
16322   i = iFirst = mem5.aiFreelist[iLogsize];
16323   assert( iFirst>=0 );
16324   while( i>0 ){
16325     if( i<iFirst ) iFirst = i;
16326     i = MEM5LINK(i)->next;
16327   }
16328   memsys5Unlink(iFirst, iLogsize);
16329   return iFirst;
16330 }
16331 
16332 /*
16333 ** Return a block of memory of at least nBytes in size.
16334 ** Return NULL if unable.  Return NULL if nBytes==0.
16335 **
16336 ** The caller guarantees that nByte positive.
16337 **
16338 ** The caller has obtained a mutex prior to invoking this
16339 ** routine so there is never any chance that two or more
16340 ** threads can be in this routine at the same time.
16341 */
memsys5MallocUnsafe(int nByte)16342 static void *memsys5MallocUnsafe(int nByte){
16343   int i;           /* Index of a mem5.aPool[] slot */
16344   int iBin;        /* Index into mem5.aiFreelist[] */
16345   int iFullSz;     /* Size of allocation rounded up to power of 2 */
16346   int iLogsize;    /* Log2 of iFullSz/POW2_MIN */
16347 
16348   /* nByte must be a positive */
16349   assert( nByte>0 );
16350 
16351   /* Keep track of the maximum allocation request.  Even unfulfilled
16352   ** requests are counted */
16353   if( (u32)nByte>mem5.maxRequest ){
16354     mem5.maxRequest = nByte;
16355   }
16356 
16357   /* Abort if the requested allocation size is larger than the largest
16358   ** power of two that we can represent using 32-bit signed integers.
16359   */
16360   if( nByte > 0x40000000 ){
16361     return 0;
16362   }
16363 
16364   /* Round nByte up to the next valid power of two */
16365   for(iFullSz=mem5.szAtom, iLogsize=0; iFullSz<nByte; iFullSz *= 2, iLogsize++){}
16366 
16367   /* Make sure mem5.aiFreelist[iLogsize] contains at least one free
16368   ** block.  If not, then split a block of the next larger power of
16369   ** two in order to create a new free block of size iLogsize.
16370   */
16371   for(iBin=iLogsize; mem5.aiFreelist[iBin]<0 && iBin<=LOGMAX; iBin++){}
16372   if( iBin>LOGMAX ){
16373     testcase( sqlite3GlobalConfig.xLog!=0 );
16374     sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes", nByte);
16375     return 0;
16376   }
16377   i = memsys5UnlinkFirst(iBin);
16378   while( iBin>iLogsize ){
16379     int newSize;
16380 
16381     iBin--;
16382     newSize = 1 << iBin;
16383     mem5.aCtrl[i+newSize] = CTRL_FREE | iBin;
16384     memsys5Link(i+newSize, iBin);
16385   }
16386   mem5.aCtrl[i] = iLogsize;
16387 
16388   /* Update allocator performance statistics. */
16389   mem5.nAlloc++;
16390   mem5.totalAlloc += iFullSz;
16391   mem5.totalExcess += iFullSz - nByte;
16392   mem5.currentCount++;
16393   mem5.currentOut += iFullSz;
16394   if( mem5.maxCount<mem5.currentCount ) mem5.maxCount = mem5.currentCount;
16395   if( mem5.maxOut<mem5.currentOut ) mem5.maxOut = mem5.currentOut;
16396 
16397   /* Return a pointer to the allocated memory. */
16398   return (void*)&mem5.zPool[i*mem5.szAtom];
16399 }
16400 
16401 /*
16402 ** Free an outstanding memory allocation.
16403 */
memsys5FreeUnsafe(void * pOld)16404 static void memsys5FreeUnsafe(void *pOld){
16405   u32 size, iLogsize;
16406   int iBlock;
16407 
16408   /* Set iBlock to the index of the block pointed to by pOld in
16409   ** the array of mem5.szAtom byte blocks pointed to by mem5.zPool.
16410   */
16411   iBlock = ((u8 *)pOld-mem5.zPool)/mem5.szAtom;
16412 
16413   /* Check that the pointer pOld points to a valid, non-free block. */
16414   assert( iBlock>=0 && iBlock<mem5.nBlock );
16415   assert( ((u8 *)pOld-mem5.zPool)%mem5.szAtom==0 );
16416   assert( (mem5.aCtrl[iBlock] & CTRL_FREE)==0 );
16417 
16418   iLogsize = mem5.aCtrl[iBlock] & CTRL_LOGSIZE;
16419   size = 1<<iLogsize;
16420   assert( iBlock+size-1<(u32)mem5.nBlock );
16421 
16422   mem5.aCtrl[iBlock] |= CTRL_FREE;
16423   mem5.aCtrl[iBlock+size-1] |= CTRL_FREE;
16424   assert( mem5.currentCount>0 );
16425   assert( mem5.currentOut>=(size*mem5.szAtom) );
16426   mem5.currentCount--;
16427   mem5.currentOut -= size*mem5.szAtom;
16428   assert( mem5.currentOut>0 || mem5.currentCount==0 );
16429   assert( mem5.currentCount>0 || mem5.currentOut==0 );
16430 
16431   mem5.aCtrl[iBlock] = CTRL_FREE | iLogsize;
16432   while( ALWAYS(iLogsize<LOGMAX) ){
16433     int iBuddy;
16434     if( (iBlock>>iLogsize) & 1 ){
16435       iBuddy = iBlock - size;
16436     }else{
16437       iBuddy = iBlock + size;
16438     }
16439     assert( iBuddy>=0 );
16440     if( (iBuddy+(1<<iLogsize))>mem5.nBlock ) break;
16441     if( mem5.aCtrl[iBuddy]!=(CTRL_FREE | iLogsize) ) break;
16442     memsys5Unlink(iBuddy, iLogsize);
16443     iLogsize++;
16444     if( iBuddy<iBlock ){
16445       mem5.aCtrl[iBuddy] = CTRL_FREE | iLogsize;
16446       mem5.aCtrl[iBlock] = 0;
16447       iBlock = iBuddy;
16448     }else{
16449       mem5.aCtrl[iBlock] = CTRL_FREE | iLogsize;
16450       mem5.aCtrl[iBuddy] = 0;
16451     }
16452     size *= 2;
16453   }
16454   memsys5Link(iBlock, iLogsize);
16455 }
16456 
16457 /*
16458 ** Allocate nBytes of memory
16459 */
memsys5Malloc(int nBytes)16460 static void *memsys5Malloc(int nBytes){
16461   sqlite3_int64 *p = 0;
16462   if( nBytes>0 ){
16463     memsys5Enter();
16464     p = memsys5MallocUnsafe(nBytes);
16465     memsys5Leave();
16466   }
16467   return (void*)p;
16468 }
16469 
16470 /*
16471 ** Free memory.
16472 **
16473 ** The outer layer memory allocator prevents this routine from
16474 ** being called with pPrior==0.
16475 */
memsys5Free(void * pPrior)16476 static void memsys5Free(void *pPrior){
16477   assert( pPrior!=0 );
16478   memsys5Enter();
16479   memsys5FreeUnsafe(pPrior);
16480   memsys5Leave();
16481 }
16482 
16483 /*
16484 ** Change the size of an existing memory allocation.
16485 **
16486 ** The outer layer memory allocator prevents this routine from
16487 ** being called with pPrior==0.
16488 **
16489 ** nBytes is always a value obtained from a prior call to
16490 ** memsys5Round().  Hence nBytes is always a non-negative power
16491 ** of two.  If nBytes==0 that means that an oversize allocation
16492 ** (an allocation larger than 0x40000000) was requested and this
16493 ** routine should return 0 without freeing pPrior.
16494 */
memsys5Realloc(void * pPrior,int nBytes)16495 static void *memsys5Realloc(void *pPrior, int nBytes){
16496   int nOld;
16497   void *p;
16498   assert( pPrior!=0 );
16499   assert( (nBytes&(nBytes-1))==0 );  /* EV: R-46199-30249 */
16500   assert( nBytes>=0 );
16501   if( nBytes==0 ){
16502     return 0;
16503   }
16504   nOld = memsys5Size(pPrior);
16505   if( nBytes<=nOld ){
16506     return pPrior;
16507   }
16508   memsys5Enter();
16509   p = memsys5MallocUnsafe(nBytes);
16510   if( p ){
16511     memcpy(p, pPrior, nOld);
16512     memsys5FreeUnsafe(pPrior);
16513   }
16514   memsys5Leave();
16515   return p;
16516 }
16517 
16518 /*
16519 ** Round up a request size to the next valid allocation size.  If
16520 ** the allocation is too large to be handled by this allocation system,
16521 ** return 0.
16522 **
16523 ** All allocations must be a power of two and must be expressed by a
16524 ** 32-bit signed integer.  Hence the largest allocation is 0x40000000
16525 ** or 1073741824 bytes.
16526 */
memsys5Roundup(int n)16527 static int memsys5Roundup(int n){
16528   int iFullSz;
16529   if( n > 0x40000000 ) return 0;
16530   for(iFullSz=mem5.szAtom; iFullSz<n; iFullSz *= 2);
16531   return iFullSz;
16532 }
16533 
16534 /*
16535 ** Return the ceiling of the logarithm base 2 of iValue.
16536 **
16537 ** Examples:   memsys5Log(1) -> 0
16538 **             memsys5Log(2) -> 1
16539 **             memsys5Log(4) -> 2
16540 **             memsys5Log(5) -> 3
16541 **             memsys5Log(8) -> 3
16542 **             memsys5Log(9) -> 4
16543 */
memsys5Log(int iValue)16544 static int memsys5Log(int iValue){
16545   int iLog;
16546   for(iLog=0; (iLog<(int)((sizeof(int)*8)-1)) && (1<<iLog)<iValue; iLog++);
16547   return iLog;
16548 }
16549 
16550 /*
16551 ** Initialize the memory allocator.
16552 **
16553 ** This routine is not threadsafe.  The caller must be holding a mutex
16554 ** to prevent multiple threads from entering at the same time.
16555 */
memsys5Init(void * NotUsed)16556 static int memsys5Init(void *NotUsed){
16557   int ii;            /* Loop counter */
16558   int nByte;         /* Number of bytes of memory available to this allocator */
16559   u8 *zByte;         /* Memory usable by this allocator */
16560   int nMinLog;       /* Log base 2 of minimum allocation size in bytes */
16561   int iOffset;       /* An offset into mem5.aCtrl[] */
16562 
16563   UNUSED_PARAMETER(NotUsed);
16564 
16565   /* For the purposes of this routine, disable the mutex */
16566   mem5.mutex = 0;
16567 
16568   /* The size of a Mem5Link object must be a power of two.  Verify that
16569   ** this is case.
16570   */
16571   assert( (sizeof(Mem5Link)&(sizeof(Mem5Link)-1))==0 );
16572 
16573   nByte = sqlite3GlobalConfig.nHeap;
16574   zByte = (u8*)sqlite3GlobalConfig.pHeap;
16575   assert( zByte!=0 );  /* sqlite3_config() does not allow otherwise */
16576 
16577   /* boundaries on sqlite3GlobalConfig.mnReq are enforced in sqlite3_config() */
16578   nMinLog = memsys5Log(sqlite3GlobalConfig.mnReq);
16579   mem5.szAtom = (1<<nMinLog);
16580   while( (int)sizeof(Mem5Link)>mem5.szAtom ){
16581     mem5.szAtom = mem5.szAtom << 1;
16582   }
16583 
16584   mem5.nBlock = (nByte / (mem5.szAtom+sizeof(u8)));
16585   mem5.zPool = zByte;
16586   mem5.aCtrl = (u8 *)&mem5.zPool[mem5.nBlock*mem5.szAtom];
16587 
16588   for(ii=0; ii<=LOGMAX; ii++){
16589     mem5.aiFreelist[ii] = -1;
16590   }
16591 
16592   iOffset = 0;
16593   for(ii=LOGMAX; ii>=0; ii--){
16594     int nAlloc = (1<<ii);
16595     if( (iOffset+nAlloc)<=mem5.nBlock ){
16596       mem5.aCtrl[iOffset] = ii | CTRL_FREE;
16597       memsys5Link(iOffset, ii);
16598       iOffset += nAlloc;
16599     }
16600     assert((iOffset+nAlloc)>mem5.nBlock);
16601   }
16602 
16603   /* If a mutex is required for normal operation, allocate one */
16604   if( sqlite3GlobalConfig.bMemstat==0 ){
16605     mem5.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
16606   }
16607 
16608   return SQLITE_OK;
16609 }
16610 
16611 /*
16612 ** Deinitialize this module.
16613 */
memsys5Shutdown(void * NotUsed)16614 static void memsys5Shutdown(void *NotUsed){
16615   UNUSED_PARAMETER(NotUsed);
16616   mem5.mutex = 0;
16617   return;
16618 }
16619 
16620 #ifdef SQLITE_TEST
16621 /*
16622 ** Open the file indicated and write a log of all unfreed memory
16623 ** allocations into that log.
16624 */
sqlite3Memsys5Dump(const char * zFilename)16625 SQLITE_PRIVATE void sqlite3Memsys5Dump(const char *zFilename){
16626   FILE *out;
16627   int i, j, n;
16628   int nMinLog;
16629 
16630   if( zFilename==0 || zFilename[0]==0 ){
16631     out = stdout;
16632   }else{
16633     out = fopen(zFilename, "w");
16634     if( out==0 ){
16635       fprintf(stderr, "** Unable to output memory debug output log: %s **\n",
16636                       zFilename);
16637       return;
16638     }
16639   }
16640   memsys5Enter();
16641   nMinLog = memsys5Log(mem5.szAtom);
16642   for(i=0; i<=LOGMAX && i+nMinLog<32; i++){
16643     for(n=0, j=mem5.aiFreelist[i]; j>=0; j = MEM5LINK(j)->next, n++){}
16644     fprintf(out, "freelist items of size %d: %d\n", mem5.szAtom << i, n);
16645   }
16646   fprintf(out, "mem5.nAlloc       = %llu\n", mem5.nAlloc);
16647   fprintf(out, "mem5.totalAlloc   = %llu\n", mem5.totalAlloc);
16648   fprintf(out, "mem5.totalExcess  = %llu\n", mem5.totalExcess);
16649   fprintf(out, "mem5.currentOut   = %u\n", mem5.currentOut);
16650   fprintf(out, "mem5.currentCount = %u\n", mem5.currentCount);
16651   fprintf(out, "mem5.maxOut       = %u\n", mem5.maxOut);
16652   fprintf(out, "mem5.maxCount     = %u\n", mem5.maxCount);
16653   fprintf(out, "mem5.maxRequest   = %u\n", mem5.maxRequest);
16654   memsys5Leave();
16655   if( out==stdout ){
16656     fflush(stdout);
16657   }else{
16658     fclose(out);
16659   }
16660 }
16661 #endif
16662 
16663 /*
16664 ** This routine is the only routine in this file with external
16665 ** linkage. It returns a pointer to a static sqlite3_mem_methods
16666 ** struct populated with the memsys5 methods.
16667 */
sqlite3MemGetMemsys5(void)16668 SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys5(void){
16669   static const sqlite3_mem_methods memsys5Methods = {
16670      memsys5Malloc,
16671      memsys5Free,
16672      memsys5Realloc,
16673      memsys5Size,
16674      memsys5Roundup,
16675      memsys5Init,
16676      memsys5Shutdown,
16677      0
16678   };
16679   return &memsys5Methods;
16680 }
16681 
16682 #endif /* SQLITE_ENABLE_MEMSYS5 */
16683 
16684 /************** End of mem5.c ************************************************/
16685 /************** Begin file mutex.c *******************************************/
16686 /*
16687 ** 2007 August 14
16688 **
16689 ** The author disclaims copyright to this source code.  In place of
16690 ** a legal notice, here is a blessing:
16691 **
16692 **    May you do good and not evil.
16693 **    May you find forgiveness for yourself and forgive others.
16694 **    May you share freely, never taking more than you give.
16695 **
16696 *************************************************************************
16697 ** This file contains the C functions that implement mutexes.
16698 **
16699 ** This file contains code that is common across all mutex implementations.
16700 */
16701 
16702 #if defined(SQLITE_DEBUG) && !defined(SQLITE_MUTEX_OMIT)
16703 /*
16704 ** For debugging purposes, record when the mutex subsystem is initialized
16705 ** and uninitialized so that we can assert() if there is an attempt to
16706 ** allocate a mutex while the system is uninitialized.
16707 */
16708 static SQLITE_WSD int mutexIsInit = 0;
16709 #endif /* SQLITE_DEBUG */
16710 
16711 
16712 #ifndef SQLITE_MUTEX_OMIT
16713 /*
16714 ** Initialize the mutex system.
16715 */
sqlite3MutexInit(void)16716 SQLITE_PRIVATE int sqlite3MutexInit(void){
16717   int rc = SQLITE_OK;
16718   if( !sqlite3GlobalConfig.mutex.xMutexAlloc ){
16719     /* If the xMutexAlloc method has not been set, then the user did not
16720     ** install a mutex implementation via sqlite3_config() prior to
16721     ** sqlite3_initialize() being called. This block copies pointers to
16722     ** the default implementation into the sqlite3GlobalConfig structure.
16723     */
16724     sqlite3_mutex_methods const *pFrom;
16725     sqlite3_mutex_methods *pTo = &sqlite3GlobalConfig.mutex;
16726 
16727     if( sqlite3GlobalConfig.bCoreMutex ){
16728       pFrom = sqlite3DefaultMutex();
16729     }else{
16730       pFrom = sqlite3NoopMutex();
16731     }
16732     memcpy(pTo, pFrom, offsetof(sqlite3_mutex_methods, xMutexAlloc));
16733     memcpy(&pTo->xMutexFree, &pFrom->xMutexFree,
16734            sizeof(*pTo) - offsetof(sqlite3_mutex_methods, xMutexFree));
16735     pTo->xMutexAlloc = pFrom->xMutexAlloc;
16736   }
16737   rc = sqlite3GlobalConfig.mutex.xMutexInit();
16738 
16739 #ifdef SQLITE_DEBUG
16740   GLOBAL(int, mutexIsInit) = 1;
16741 #endif
16742 
16743   return rc;
16744 }
16745 
16746 /*
16747 ** Shutdown the mutex system. This call frees resources allocated by
16748 ** sqlite3MutexInit().
16749 */
sqlite3MutexEnd(void)16750 SQLITE_PRIVATE int sqlite3MutexEnd(void){
16751   int rc = SQLITE_OK;
16752   if( sqlite3GlobalConfig.mutex.xMutexEnd ){
16753     rc = sqlite3GlobalConfig.mutex.xMutexEnd();
16754   }
16755 
16756 #ifdef SQLITE_DEBUG
16757   GLOBAL(int, mutexIsInit) = 0;
16758 #endif
16759 
16760   return rc;
16761 }
16762 
16763 /*
16764 ** Retrieve a pointer to a static mutex or allocate a new dynamic one.
16765 */
sqlite3_mutex_alloc(int id)16766 SQLITE_API sqlite3_mutex *sqlite3_mutex_alloc(int id){
16767 #ifndef SQLITE_OMIT_AUTOINIT
16768   if( sqlite3_initialize() ) return 0;
16769 #endif
16770   return sqlite3GlobalConfig.mutex.xMutexAlloc(id);
16771 }
16772 
sqlite3MutexAlloc(int id)16773 SQLITE_PRIVATE sqlite3_mutex *sqlite3MutexAlloc(int id){
16774   if( !sqlite3GlobalConfig.bCoreMutex ){
16775     return 0;
16776   }
16777   assert( GLOBAL(int, mutexIsInit) );
16778   return sqlite3GlobalConfig.mutex.xMutexAlloc(id);
16779 }
16780 
16781 /*
16782 ** Free a dynamic mutex.
16783 */
sqlite3_mutex_free(sqlite3_mutex * p)16784 SQLITE_API void sqlite3_mutex_free(sqlite3_mutex *p){
16785   if( p ){
16786     sqlite3GlobalConfig.mutex.xMutexFree(p);
16787   }
16788 }
16789 
16790 /*
16791 ** Obtain the mutex p. If some other thread already has the mutex, block
16792 ** until it can be obtained.
16793 */
sqlite3_mutex_enter(sqlite3_mutex * p)16794 SQLITE_API void sqlite3_mutex_enter(sqlite3_mutex *p){
16795   if( p ){
16796     sqlite3GlobalConfig.mutex.xMutexEnter(p);
16797   }
16798 }
16799 
16800 /*
16801 ** Obtain the mutex p. If successful, return SQLITE_OK. Otherwise, if another
16802 ** thread holds the mutex and it cannot be obtained, return SQLITE_BUSY.
16803 */
sqlite3_mutex_try(sqlite3_mutex * p)16804 SQLITE_API int sqlite3_mutex_try(sqlite3_mutex *p){
16805   int rc = SQLITE_OK;
16806   if( p ){
16807     return sqlite3GlobalConfig.mutex.xMutexTry(p);
16808   }
16809   return rc;
16810 }
16811 
16812 /*
16813 ** The sqlite3_mutex_leave() routine exits a mutex that was previously
16814 ** entered by the same thread.  The behavior is undefined if the mutex
16815 ** is not currently entered. If a NULL pointer is passed as an argument
16816 ** this function is a no-op.
16817 */
sqlite3_mutex_leave(sqlite3_mutex * p)16818 SQLITE_API void sqlite3_mutex_leave(sqlite3_mutex *p){
16819   if( p ){
16820     sqlite3GlobalConfig.mutex.xMutexLeave(p);
16821   }
16822 }
16823 
16824 #ifndef NDEBUG
16825 /*
16826 ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
16827 ** intended for use inside assert() statements.
16828 */
sqlite3_mutex_held(sqlite3_mutex * p)16829 SQLITE_API int sqlite3_mutex_held(sqlite3_mutex *p){
16830   return p==0 || sqlite3GlobalConfig.mutex.xMutexHeld(p);
16831 }
sqlite3_mutex_notheld(sqlite3_mutex * p)16832 SQLITE_API int sqlite3_mutex_notheld(sqlite3_mutex *p){
16833   return p==0 || sqlite3GlobalConfig.mutex.xMutexNotheld(p);
16834 }
16835 #endif
16836 
16837 #endif /* SQLITE_MUTEX_OMIT */
16838 
16839 /************** End of mutex.c ***********************************************/
16840 /************** Begin file mutex_noop.c **************************************/
16841 /*
16842 ** 2008 October 07
16843 **
16844 ** The author disclaims copyright to this source code.  In place of
16845 ** a legal notice, here is a blessing:
16846 **
16847 **    May you do good and not evil.
16848 **    May you find forgiveness for yourself and forgive others.
16849 **    May you share freely, never taking more than you give.
16850 **
16851 *************************************************************************
16852 ** This file contains the C functions that implement mutexes.
16853 **
16854 ** This implementation in this file does not provide any mutual
16855 ** exclusion and is thus suitable for use only in applications
16856 ** that use SQLite in a single thread.  The routines defined
16857 ** here are place-holders.  Applications can substitute working
16858 ** mutex routines at start-time using the
16859 **
16860 **     sqlite3_config(SQLITE_CONFIG_MUTEX,...)
16861 **
16862 ** interface.
16863 **
16864 ** If compiled with SQLITE_DEBUG, then additional logic is inserted
16865 ** that does error checking on mutexes to make sure they are being
16866 ** called correctly.
16867 */
16868 
16869 #ifndef SQLITE_MUTEX_OMIT
16870 
16871 #ifndef SQLITE_DEBUG
16872 /*
16873 ** Stub routines for all mutex methods.
16874 **
16875 ** This routines provide no mutual exclusion or error checking.
16876 */
noopMutexInit(void)16877 static int noopMutexInit(void){ return SQLITE_OK; }
noopMutexEnd(void)16878 static int noopMutexEnd(void){ return SQLITE_OK; }
noopMutexAlloc(int id)16879 static sqlite3_mutex *noopMutexAlloc(int id){
16880   UNUSED_PARAMETER(id);
16881   return (sqlite3_mutex*)8;
16882 }
noopMutexFree(sqlite3_mutex * p)16883 static void noopMutexFree(sqlite3_mutex *p){ UNUSED_PARAMETER(p); return; }
noopMutexEnter(sqlite3_mutex * p)16884 static void noopMutexEnter(sqlite3_mutex *p){ UNUSED_PARAMETER(p); return; }
noopMutexTry(sqlite3_mutex * p)16885 static int noopMutexTry(sqlite3_mutex *p){
16886   UNUSED_PARAMETER(p);
16887   return SQLITE_OK;
16888 }
noopMutexLeave(sqlite3_mutex * p)16889 static void noopMutexLeave(sqlite3_mutex *p){ UNUSED_PARAMETER(p); return; }
16890 
sqlite3NoopMutex(void)16891 SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3NoopMutex(void){
16892   static const sqlite3_mutex_methods sMutex = {
16893     noopMutexInit,
16894     noopMutexEnd,
16895     noopMutexAlloc,
16896     noopMutexFree,
16897     noopMutexEnter,
16898     noopMutexTry,
16899     noopMutexLeave,
16900 
16901     0,
16902     0,
16903   };
16904 
16905   return &sMutex;
16906 }
16907 #endif /* !SQLITE_DEBUG */
16908 
16909 #ifdef SQLITE_DEBUG
16910 /*
16911 ** In this implementation, error checking is provided for testing
16912 ** and debugging purposes.  The mutexes still do not provide any
16913 ** mutual exclusion.
16914 */
16915 
16916 /*
16917 ** The mutex object
16918 */
16919 typedef struct sqlite3_debug_mutex {
16920   int id;     /* The mutex type */
16921   int cnt;    /* Number of entries without a matching leave */
16922 } sqlite3_debug_mutex;
16923 
16924 /*
16925 ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
16926 ** intended for use inside assert() statements.
16927 */
debugMutexHeld(sqlite3_mutex * pX)16928 static int debugMutexHeld(sqlite3_mutex *pX){
16929   sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
16930   return p==0 || p->cnt>0;
16931 }
debugMutexNotheld(sqlite3_mutex * pX)16932 static int debugMutexNotheld(sqlite3_mutex *pX){
16933   sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
16934   return p==0 || p->cnt==0;
16935 }
16936 
16937 /*
16938 ** Initialize and deinitialize the mutex subsystem.
16939 */
debugMutexInit(void)16940 static int debugMutexInit(void){ return SQLITE_OK; }
debugMutexEnd(void)16941 static int debugMutexEnd(void){ return SQLITE_OK; }
16942 
16943 /*
16944 ** The sqlite3_mutex_alloc() routine allocates a new
16945 ** mutex and returns a pointer to it.  If it returns NULL
16946 ** that means that a mutex could not be allocated.
16947 */
debugMutexAlloc(int id)16948 static sqlite3_mutex *debugMutexAlloc(int id){
16949   static sqlite3_debug_mutex aStatic[6];
16950   sqlite3_debug_mutex *pNew = 0;
16951   switch( id ){
16952     case SQLITE_MUTEX_FAST:
16953     case SQLITE_MUTEX_RECURSIVE: {
16954       pNew = sqlite3Malloc(sizeof(*pNew));
16955       if( pNew ){
16956         pNew->id = id;
16957         pNew->cnt = 0;
16958       }
16959       break;
16960     }
16961     default: {
16962       assert( id-2 >= 0 );
16963       assert( id-2 < (int)(sizeof(aStatic)/sizeof(aStatic[0])) );
16964       pNew = &aStatic[id-2];
16965       pNew->id = id;
16966       break;
16967     }
16968   }
16969   return (sqlite3_mutex*)pNew;
16970 }
16971 
16972 /*
16973 ** This routine deallocates a previously allocated mutex.
16974 */
debugMutexFree(sqlite3_mutex * pX)16975 static void debugMutexFree(sqlite3_mutex *pX){
16976   sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
16977   assert( p->cnt==0 );
16978   assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
16979   sqlite3_free(p);
16980 }
16981 
16982 /*
16983 ** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
16984 ** to enter a mutex.  If another thread is already within the mutex,
16985 ** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
16986 ** SQLITE_BUSY.  The sqlite3_mutex_try() interface returns SQLITE_OK
16987 ** upon successful entry.  Mutexes created using SQLITE_MUTEX_RECURSIVE can
16988 ** be entered multiple times by the same thread.  In such cases the,
16989 ** mutex must be exited an equal number of times before another thread
16990 ** can enter.  If the same thread tries to enter any other kind of mutex
16991 ** more than once, the behavior is undefined.
16992 */
debugMutexEnter(sqlite3_mutex * pX)16993 static void debugMutexEnter(sqlite3_mutex *pX){
16994   sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
16995   assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) );
16996   p->cnt++;
16997 }
debugMutexTry(sqlite3_mutex * pX)16998 static int debugMutexTry(sqlite3_mutex *pX){
16999   sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
17000   assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) );
17001   p->cnt++;
17002   return SQLITE_OK;
17003 }
17004 
17005 /*
17006 ** The sqlite3_mutex_leave() routine exits a mutex that was
17007 ** previously entered by the same thread.  The behavior
17008 ** is undefined if the mutex is not currently entered or
17009 ** is not currently allocated.  SQLite will never do either.
17010 */
debugMutexLeave(sqlite3_mutex * pX)17011 static void debugMutexLeave(sqlite3_mutex *pX){
17012   sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
17013   assert( debugMutexHeld(pX) );
17014   p->cnt--;
17015   assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) );
17016 }
17017 
sqlite3NoopMutex(void)17018 SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3NoopMutex(void){
17019   static const sqlite3_mutex_methods sMutex = {
17020     debugMutexInit,
17021     debugMutexEnd,
17022     debugMutexAlloc,
17023     debugMutexFree,
17024     debugMutexEnter,
17025     debugMutexTry,
17026     debugMutexLeave,
17027 
17028     debugMutexHeld,
17029     debugMutexNotheld
17030   };
17031 
17032   return &sMutex;
17033 }
17034 #endif /* SQLITE_DEBUG */
17035 
17036 /*
17037 ** If compiled with SQLITE_MUTEX_NOOP, then the no-op mutex implementation
17038 ** is used regardless of the run-time threadsafety setting.
17039 */
17040 #ifdef SQLITE_MUTEX_NOOP
sqlite3DefaultMutex(void)17041 SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
17042   return sqlite3NoopMutex();
17043 }
17044 #endif /* SQLITE_MUTEX_NOOP */
17045 #endif /* SQLITE_MUTEX_OMIT */
17046 
17047 /************** End of mutex_noop.c ******************************************/
17048 /************** Begin file mutex_os2.c ***************************************/
17049 /*
17050 ** 2007 August 28
17051 **
17052 ** The author disclaims copyright to this source code.  In place of
17053 ** a legal notice, here is a blessing:
17054 **
17055 **    May you do good and not evil.
17056 **    May you find forgiveness for yourself and forgive others.
17057 **    May you share freely, never taking more than you give.
17058 **
17059 *************************************************************************
17060 ** This file contains the C functions that implement mutexes for OS/2
17061 */
17062 
17063 /*
17064 ** The code in this file is only used if SQLITE_MUTEX_OS2 is defined.
17065 ** See the mutex.h file for details.
17066 */
17067 #ifdef SQLITE_MUTEX_OS2
17068 
17069 /********************** OS/2 Mutex Implementation **********************
17070 **
17071 ** This implementation of mutexes is built using the OS/2 API.
17072 */
17073 
17074 /*
17075 ** The mutex object
17076 ** Each recursive mutex is an instance of the following structure.
17077 */
17078 struct sqlite3_mutex {
17079   HMTX mutex;       /* Mutex controlling the lock */
17080   int  id;          /* Mutex type */
17081 #ifdef SQLITE_DEBUG
17082  int   trace;       /* True to trace changes */
17083 #endif
17084 };
17085 
17086 #ifdef SQLITE_DEBUG
17087 #define SQLITE3_MUTEX_INITIALIZER { 0, 0, 0 }
17088 #else
17089 #define SQLITE3_MUTEX_INITIALIZER { 0, 0 }
17090 #endif
17091 
17092 /*
17093 ** Initialize and deinitialize the mutex subsystem.
17094 */
os2MutexInit(void)17095 static int os2MutexInit(void){ return SQLITE_OK; }
os2MutexEnd(void)17096 static int os2MutexEnd(void){ return SQLITE_OK; }
17097 
17098 /*
17099 ** The sqlite3_mutex_alloc() routine allocates a new
17100 ** mutex and returns a pointer to it.  If it returns NULL
17101 ** that means that a mutex could not be allocated.
17102 ** SQLite will unwind its stack and return an error.  The argument
17103 ** to sqlite3_mutex_alloc() is one of these integer constants:
17104 **
17105 ** <ul>
17106 ** <li>  SQLITE_MUTEX_FAST
17107 ** <li>  SQLITE_MUTEX_RECURSIVE
17108 ** <li>  SQLITE_MUTEX_STATIC_MASTER
17109 ** <li>  SQLITE_MUTEX_STATIC_MEM
17110 ** <li>  SQLITE_MUTEX_STATIC_MEM2
17111 ** <li>  SQLITE_MUTEX_STATIC_PRNG
17112 ** <li>  SQLITE_MUTEX_STATIC_LRU
17113 ** <li>  SQLITE_MUTEX_STATIC_LRU2
17114 ** </ul>
17115 **
17116 ** The first two constants cause sqlite3_mutex_alloc() to create
17117 ** a new mutex.  The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
17118 ** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
17119 ** The mutex implementation does not need to make a distinction
17120 ** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
17121 ** not want to.  But SQLite will only request a recursive mutex in
17122 ** cases where it really needs one.  If a faster non-recursive mutex
17123 ** implementation is available on the host platform, the mutex subsystem
17124 ** might return such a mutex in response to SQLITE_MUTEX_FAST.
17125 **
17126 ** The other allowed parameters to sqlite3_mutex_alloc() each return
17127 ** a pointer to a static preexisting mutex.  Six static mutexes are
17128 ** used by the current version of SQLite.  Future versions of SQLite
17129 ** may add additional static mutexes.  Static mutexes are for internal
17130 ** use by SQLite only.  Applications that use SQLite mutexes should
17131 ** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
17132 ** SQLITE_MUTEX_RECURSIVE.
17133 **
17134 ** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
17135 ** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
17136 ** returns a different mutex on every call.  But for the static
17137 ** mutex types, the same mutex is returned on every call that has
17138 ** the same type number.
17139 */
os2MutexAlloc(int iType)17140 static sqlite3_mutex *os2MutexAlloc(int iType){
17141   sqlite3_mutex *p = NULL;
17142   switch( iType ){
17143     case SQLITE_MUTEX_FAST:
17144     case SQLITE_MUTEX_RECURSIVE: {
17145       p = sqlite3MallocZero( sizeof(*p) );
17146       if( p ){
17147         p->id = iType;
17148         if( DosCreateMutexSem( 0, &p->mutex, 0, FALSE ) != NO_ERROR ){
17149           sqlite3_free( p );
17150           p = NULL;
17151         }
17152       }
17153       break;
17154     }
17155     default: {
17156       static volatile int isInit = 0;
17157       static sqlite3_mutex staticMutexes[6] = {
17158         SQLITE3_MUTEX_INITIALIZER,
17159         SQLITE3_MUTEX_INITIALIZER,
17160         SQLITE3_MUTEX_INITIALIZER,
17161         SQLITE3_MUTEX_INITIALIZER,
17162         SQLITE3_MUTEX_INITIALIZER,
17163         SQLITE3_MUTEX_INITIALIZER,
17164       };
17165       if ( !isInit ){
17166         APIRET rc;
17167         PTIB ptib;
17168         PPIB ppib;
17169         HMTX mutex;
17170         char name[32];
17171         DosGetInfoBlocks( &ptib, &ppib );
17172         sqlite3_snprintf( sizeof(name), name, "\\SEM32\\SQLITE%04x",
17173                           ppib->pib_ulpid );
17174         while( !isInit ){
17175           mutex = 0;
17176           rc = DosCreateMutexSem( name, &mutex, 0, FALSE);
17177           if( rc == NO_ERROR ){
17178             unsigned int i;
17179             if( !isInit ){
17180               for( i = 0; i < sizeof(staticMutexes)/sizeof(staticMutexes[0]); i++ ){
17181                 DosCreateMutexSem( 0, &staticMutexes[i].mutex, 0, FALSE );
17182               }
17183               isInit = 1;
17184             }
17185             DosCloseMutexSem( mutex );
17186           }else if( rc == ERROR_DUPLICATE_NAME ){
17187             DosSleep( 1 );
17188           }else{
17189             return p;
17190           }
17191         }
17192       }
17193       assert( iType-2 >= 0 );
17194       assert( iType-2 < sizeof(staticMutexes)/sizeof(staticMutexes[0]) );
17195       p = &staticMutexes[iType-2];
17196       p->id = iType;
17197       break;
17198     }
17199   }
17200   return p;
17201 }
17202 
17203 
17204 /*
17205 ** This routine deallocates a previously allocated mutex.
17206 ** SQLite is careful to deallocate every mutex that it allocates.
17207 */
os2MutexFree(sqlite3_mutex * p)17208 static void os2MutexFree(sqlite3_mutex *p){
17209 #ifdef SQLITE_DEBUG
17210   TID tid;
17211   PID pid;
17212   ULONG ulCount;
17213   DosQueryMutexSem(p->mutex, &pid, &tid, &ulCount);
17214   assert( ulCount==0 );
17215   assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
17216 #endif
17217   DosCloseMutexSem( p->mutex );
17218   sqlite3_free( p );
17219 }
17220 
17221 #ifdef SQLITE_DEBUG
17222 /*
17223 ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
17224 ** intended for use inside assert() statements.
17225 */
os2MutexHeld(sqlite3_mutex * p)17226 static int os2MutexHeld(sqlite3_mutex *p){
17227   TID tid;
17228   PID pid;
17229   ULONG ulCount;
17230   PTIB ptib;
17231   DosQueryMutexSem(p->mutex, &pid, &tid, &ulCount);
17232   if( ulCount==0 || ( ulCount>1 && p->id!=SQLITE_MUTEX_RECURSIVE ) )
17233     return 0;
17234   DosGetInfoBlocks(&ptib, NULL);
17235   return tid==ptib->tib_ptib2->tib2_ultid;
17236 }
os2MutexNotheld(sqlite3_mutex * p)17237 static int os2MutexNotheld(sqlite3_mutex *p){
17238   TID tid;
17239   PID pid;
17240   ULONG ulCount;
17241   PTIB ptib;
17242   DosQueryMutexSem(p->mutex, &pid, &tid, &ulCount);
17243   if( ulCount==0 )
17244     return 1;
17245   DosGetInfoBlocks(&ptib, NULL);
17246   return tid!=ptib->tib_ptib2->tib2_ultid;
17247 }
os2MutexTrace(sqlite3_mutex * p,char * pAction)17248 static void os2MutexTrace(sqlite3_mutex *p, char *pAction){
17249   TID   tid;
17250   PID   pid;
17251   ULONG ulCount;
17252   DosQueryMutexSem(p->mutex, &pid, &tid, &ulCount);
17253   printf("%s mutex %p (%d) with nRef=%ld\n", pAction, (void*)p, p->trace, ulCount);
17254 }
17255 #endif
17256 
17257 /*
17258 ** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
17259 ** to enter a mutex.  If another thread is already within the mutex,
17260 ** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
17261 ** SQLITE_BUSY.  The sqlite3_mutex_try() interface returns SQLITE_OK
17262 ** upon successful entry.  Mutexes created using SQLITE_MUTEX_RECURSIVE can
17263 ** be entered multiple times by the same thread.  In such cases the,
17264 ** mutex must be exited an equal number of times before another thread
17265 ** can enter.  If the same thread tries to enter any other kind of mutex
17266 ** more than once, the behavior is undefined.
17267 */
os2MutexEnter(sqlite3_mutex * p)17268 static void os2MutexEnter(sqlite3_mutex *p){
17269   assert( p->id==SQLITE_MUTEX_RECURSIVE || os2MutexNotheld(p) );
17270   DosRequestMutexSem(p->mutex, SEM_INDEFINITE_WAIT);
17271 #ifdef SQLITE_DEBUG
17272   if( p->trace ) os2MutexTrace(p, "enter");
17273 #endif
17274 }
os2MutexTry(sqlite3_mutex * p)17275 static int os2MutexTry(sqlite3_mutex *p){
17276   int rc = SQLITE_BUSY;
17277   assert( p->id==SQLITE_MUTEX_RECURSIVE || os2MutexNotheld(p) );
17278   if( DosRequestMutexSem(p->mutex, SEM_IMMEDIATE_RETURN) == NO_ERROR ) {
17279     rc = SQLITE_OK;
17280 #ifdef SQLITE_DEBUG
17281     if( p->trace ) os2MutexTrace(p, "try");
17282 #endif
17283   }
17284   return rc;
17285 }
17286 
17287 /*
17288 ** The sqlite3_mutex_leave() routine exits a mutex that was
17289 ** previously entered by the same thread.  The behavior
17290 ** is undefined if the mutex is not currently entered or
17291 ** is not currently allocated.  SQLite will never do either.
17292 */
os2MutexLeave(sqlite3_mutex * p)17293 static void os2MutexLeave(sqlite3_mutex *p){
17294   assert( os2MutexHeld(p) );
17295   DosReleaseMutexSem(p->mutex);
17296 #ifdef SQLITE_DEBUG
17297   if( p->trace ) os2MutexTrace(p, "leave");
17298 #endif
17299 }
17300 
sqlite3DefaultMutex(void)17301 SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
17302   static const sqlite3_mutex_methods sMutex = {
17303     os2MutexInit,
17304     os2MutexEnd,
17305     os2MutexAlloc,
17306     os2MutexFree,
17307     os2MutexEnter,
17308     os2MutexTry,
17309     os2MutexLeave,
17310 #ifdef SQLITE_DEBUG
17311     os2MutexHeld,
17312     os2MutexNotheld
17313 #else
17314     0,
17315     0
17316 #endif
17317   };
17318 
17319   return &sMutex;
17320 }
17321 #endif /* SQLITE_MUTEX_OS2 */
17322 
17323 /************** End of mutex_os2.c *******************************************/
17324 /************** Begin file mutex_unix.c **************************************/
17325 /*
17326 ** 2007 August 28
17327 **
17328 ** The author disclaims copyright to this source code.  In place of
17329 ** a legal notice, here is a blessing:
17330 **
17331 **    May you do good and not evil.
17332 **    May you find forgiveness for yourself and forgive others.
17333 **    May you share freely, never taking more than you give.
17334 **
17335 *************************************************************************
17336 ** This file contains the C functions that implement mutexes for pthreads
17337 */
17338 
17339 /*
17340 ** The code in this file is only used if we are compiling threadsafe
17341 ** under unix with pthreads.
17342 **
17343 ** Note that this implementation requires a version of pthreads that
17344 ** supports recursive mutexes.
17345 */
17346 #ifdef SQLITE_MUTEX_PTHREADS
17347 
17348 #include <pthread.h>
17349 
17350 /*
17351 ** The sqlite3_mutex.id, sqlite3_mutex.nRef, and sqlite3_mutex.owner fields
17352 ** are necessary under two condidtions:  (1) Debug builds and (2) using
17353 ** home-grown mutexes.  Encapsulate these conditions into a single #define.
17354 */
17355 #if defined(SQLITE_DEBUG) || defined(SQLITE_HOMEGROWN_RECURSIVE_MUTEX)
17356 # define SQLITE_MUTEX_NREF 1
17357 #else
17358 # define SQLITE_MUTEX_NREF 0
17359 #endif
17360 
17361 /*
17362 ** Each recursive mutex is an instance of the following structure.
17363 */
17364 struct sqlite3_mutex {
17365   pthread_mutex_t mutex;     /* Mutex controlling the lock */
17366 #if SQLITE_MUTEX_NREF
17367   int id;                    /* Mutex type */
17368   volatile int nRef;         /* Number of entrances */
17369   volatile pthread_t owner;  /* Thread that is within this mutex */
17370   int trace;                 /* True to trace changes */
17371 #endif
17372 };
17373 #if SQLITE_MUTEX_NREF
17374 #define SQLITE3_MUTEX_INITIALIZER { PTHREAD_MUTEX_INITIALIZER, 0, 0, (pthread_t)0, 0 }
17375 #else
17376 #define SQLITE3_MUTEX_INITIALIZER { PTHREAD_MUTEX_INITIALIZER }
17377 #endif
17378 
17379 /*
17380 ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
17381 ** intended for use only inside assert() statements.  On some platforms,
17382 ** there might be race conditions that can cause these routines to
17383 ** deliver incorrect results.  In particular, if pthread_equal() is
17384 ** not an atomic operation, then these routines might delivery
17385 ** incorrect results.  On most platforms, pthread_equal() is a
17386 ** comparison of two integers and is therefore atomic.  But we are
17387 ** told that HPUX is not such a platform.  If so, then these routines
17388 ** will not always work correctly on HPUX.
17389 **
17390 ** On those platforms where pthread_equal() is not atomic, SQLite
17391 ** should be compiled without -DSQLITE_DEBUG and with -DNDEBUG to
17392 ** make sure no assert() statements are evaluated and hence these
17393 ** routines are never called.
17394 */
17395 #if !defined(NDEBUG) || defined(SQLITE_DEBUG)
pthreadMutexHeld(sqlite3_mutex * p)17396 static int pthreadMutexHeld(sqlite3_mutex *p){
17397   return (p->nRef!=0 && pthread_equal(p->owner, pthread_self()));
17398 }
pthreadMutexNotheld(sqlite3_mutex * p)17399 static int pthreadMutexNotheld(sqlite3_mutex *p){
17400   return p->nRef==0 || pthread_equal(p->owner, pthread_self())==0;
17401 }
17402 #endif
17403 
17404 /*
17405 ** Initialize and deinitialize the mutex subsystem.
17406 */
pthreadMutexInit(void)17407 static int pthreadMutexInit(void){ return SQLITE_OK; }
pthreadMutexEnd(void)17408 static int pthreadMutexEnd(void){ return SQLITE_OK; }
17409 
17410 /*
17411 ** The sqlite3_mutex_alloc() routine allocates a new
17412 ** mutex and returns a pointer to it.  If it returns NULL
17413 ** that means that a mutex could not be allocated.  SQLite
17414 ** will unwind its stack and return an error.  The argument
17415 ** to sqlite3_mutex_alloc() is one of these integer constants:
17416 **
17417 ** <ul>
17418 ** <li>  SQLITE_MUTEX_FAST
17419 ** <li>  SQLITE_MUTEX_RECURSIVE
17420 ** <li>  SQLITE_MUTEX_STATIC_MASTER
17421 ** <li>  SQLITE_MUTEX_STATIC_MEM
17422 ** <li>  SQLITE_MUTEX_STATIC_MEM2
17423 ** <li>  SQLITE_MUTEX_STATIC_PRNG
17424 ** <li>  SQLITE_MUTEX_STATIC_LRU
17425 ** <li>  SQLITE_MUTEX_STATIC_PMEM
17426 ** </ul>
17427 **
17428 ** The first two constants cause sqlite3_mutex_alloc() to create
17429 ** a new mutex.  The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
17430 ** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
17431 ** The mutex implementation does not need to make a distinction
17432 ** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
17433 ** not want to.  But SQLite will only request a recursive mutex in
17434 ** cases where it really needs one.  If a faster non-recursive mutex
17435 ** implementation is available on the host platform, the mutex subsystem
17436 ** might return such a mutex in response to SQLITE_MUTEX_FAST.
17437 **
17438 ** The other allowed parameters to sqlite3_mutex_alloc() each return
17439 ** a pointer to a static preexisting mutex.  Six static mutexes are
17440 ** used by the current version of SQLite.  Future versions of SQLite
17441 ** may add additional static mutexes.  Static mutexes are for internal
17442 ** use by SQLite only.  Applications that use SQLite mutexes should
17443 ** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
17444 ** SQLITE_MUTEX_RECURSIVE.
17445 **
17446 ** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
17447 ** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
17448 ** returns a different mutex on every call.  But for the static
17449 ** mutex types, the same mutex is returned on every call that has
17450 ** the same type number.
17451 */
pthreadMutexAlloc(int iType)17452 static sqlite3_mutex *pthreadMutexAlloc(int iType){
17453   static sqlite3_mutex staticMutexes[] = {
17454     SQLITE3_MUTEX_INITIALIZER,
17455     SQLITE3_MUTEX_INITIALIZER,
17456     SQLITE3_MUTEX_INITIALIZER,
17457     SQLITE3_MUTEX_INITIALIZER,
17458     SQLITE3_MUTEX_INITIALIZER,
17459     SQLITE3_MUTEX_INITIALIZER
17460   };
17461   sqlite3_mutex *p;
17462   switch( iType ){
17463     case SQLITE_MUTEX_RECURSIVE: {
17464       p = sqlite3MallocZero( sizeof(*p) );
17465       if( p ){
17466 #ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
17467         /* If recursive mutexes are not available, we will have to
17468         ** build our own.  See below. */
17469         pthread_mutex_init(&p->mutex, 0);
17470 #else
17471         /* Use a recursive mutex if it is available */
17472         pthread_mutexattr_t recursiveAttr;
17473         pthread_mutexattr_init(&recursiveAttr);
17474         pthread_mutexattr_settype(&recursiveAttr, PTHREAD_MUTEX_RECURSIVE);
17475         pthread_mutex_init(&p->mutex, &recursiveAttr);
17476         pthread_mutexattr_destroy(&recursiveAttr);
17477 #endif
17478 #if SQLITE_MUTEX_NREF
17479         p->id = iType;
17480 #endif
17481       }
17482       break;
17483     }
17484     case SQLITE_MUTEX_FAST: {
17485       p = sqlite3MallocZero( sizeof(*p) );
17486       if( p ){
17487 #if SQLITE_MUTEX_NREF
17488         p->id = iType;
17489 #endif
17490         pthread_mutex_init(&p->mutex, 0);
17491       }
17492       break;
17493     }
17494     default: {
17495       assert( iType-2 >= 0 );
17496       assert( iType-2 < ArraySize(staticMutexes) );
17497       p = &staticMutexes[iType-2];
17498 #if SQLITE_MUTEX_NREF
17499       p->id = iType;
17500 #endif
17501       break;
17502     }
17503   }
17504   return p;
17505 }
17506 
17507 
17508 /*
17509 ** This routine deallocates a previously
17510 ** allocated mutex.  SQLite is careful to deallocate every
17511 ** mutex that it allocates.
17512 */
pthreadMutexFree(sqlite3_mutex * p)17513 static void pthreadMutexFree(sqlite3_mutex *p){
17514   assert( p->nRef==0 );
17515   assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
17516   pthread_mutex_destroy(&p->mutex);
17517   sqlite3_free(p);
17518 }
17519 
17520 /*
17521 ** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
17522 ** to enter a mutex.  If another thread is already within the mutex,
17523 ** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
17524 ** SQLITE_BUSY.  The sqlite3_mutex_try() interface returns SQLITE_OK
17525 ** upon successful entry.  Mutexes created using SQLITE_MUTEX_RECURSIVE can
17526 ** be entered multiple times by the same thread.  In such cases the,
17527 ** mutex must be exited an equal number of times before another thread
17528 ** can enter.  If the same thread tries to enter any other kind of mutex
17529 ** more than once, the behavior is undefined.
17530 */
pthreadMutexEnter(sqlite3_mutex * p)17531 static void pthreadMutexEnter(sqlite3_mutex *p){
17532   assert( p->id==SQLITE_MUTEX_RECURSIVE || pthreadMutexNotheld(p) );
17533 
17534 #ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
17535   /* If recursive mutexes are not available, then we have to grow
17536   ** our own.  This implementation assumes that pthread_equal()
17537   ** is atomic - that it cannot be deceived into thinking self
17538   ** and p->owner are equal if p->owner changes between two values
17539   ** that are not equal to self while the comparison is taking place.
17540   ** This implementation also assumes a coherent cache - that
17541   ** separate processes cannot read different values from the same
17542   ** address at the same time.  If either of these two conditions
17543   ** are not met, then the mutexes will fail and problems will result.
17544   */
17545   {
17546     pthread_t self = pthread_self();
17547     if( p->nRef>0 && pthread_equal(p->owner, self) ){
17548       p->nRef++;
17549     }else{
17550       pthread_mutex_lock(&p->mutex);
17551       assert( p->nRef==0 );
17552       p->owner = self;
17553       p->nRef = 1;
17554     }
17555   }
17556 #else
17557   /* Use the built-in recursive mutexes if they are available.
17558   */
17559   pthread_mutex_lock(&p->mutex);
17560 #if SQLITE_MUTEX_NREF
17561   assert( p->nRef>0 || p->owner==0 );
17562   p->owner = pthread_self();
17563   p->nRef++;
17564 #endif
17565 #endif
17566 
17567 #ifdef SQLITE_DEBUG
17568   if( p->trace ){
17569     printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
17570   }
17571 #endif
17572 }
pthreadMutexTry(sqlite3_mutex * p)17573 static int pthreadMutexTry(sqlite3_mutex *p){
17574   int rc;
17575   assert( p->id==SQLITE_MUTEX_RECURSIVE || pthreadMutexNotheld(p) );
17576 
17577 #ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
17578   /* If recursive mutexes are not available, then we have to grow
17579   ** our own.  This implementation assumes that pthread_equal()
17580   ** is atomic - that it cannot be deceived into thinking self
17581   ** and p->owner are equal if p->owner changes between two values
17582   ** that are not equal to self while the comparison is taking place.
17583   ** This implementation also assumes a coherent cache - that
17584   ** separate processes cannot read different values from the same
17585   ** address at the same time.  If either of these two conditions
17586   ** are not met, then the mutexes will fail and problems will result.
17587   */
17588   {
17589     pthread_t self = pthread_self();
17590     if( p->nRef>0 && pthread_equal(p->owner, self) ){
17591       p->nRef++;
17592       rc = SQLITE_OK;
17593     }else if( pthread_mutex_trylock(&p->mutex)==0 ){
17594       assert( p->nRef==0 );
17595       p->owner = self;
17596       p->nRef = 1;
17597       rc = SQLITE_OK;
17598     }else{
17599       rc = SQLITE_BUSY;
17600     }
17601   }
17602 #else
17603   /* Use the built-in recursive mutexes if they are available.
17604   */
17605   if( pthread_mutex_trylock(&p->mutex)==0 ){
17606 #if SQLITE_MUTEX_NREF
17607     p->owner = pthread_self();
17608     p->nRef++;
17609 #endif
17610     rc = SQLITE_OK;
17611   }else{
17612     rc = SQLITE_BUSY;
17613   }
17614 #endif
17615 
17616 #ifdef SQLITE_DEBUG
17617   if( rc==SQLITE_OK && p->trace ){
17618     printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
17619   }
17620 #endif
17621   return rc;
17622 }
17623 
17624 /*
17625 ** The sqlite3_mutex_leave() routine exits a mutex that was
17626 ** previously entered by the same thread.  The behavior
17627 ** is undefined if the mutex is not currently entered or
17628 ** is not currently allocated.  SQLite will never do either.
17629 */
pthreadMutexLeave(sqlite3_mutex * p)17630 static void pthreadMutexLeave(sqlite3_mutex *p){
17631   assert( pthreadMutexHeld(p) );
17632 #if SQLITE_MUTEX_NREF
17633   p->nRef--;
17634   if( p->nRef==0 ) p->owner = 0;
17635 #endif
17636   assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
17637 
17638 #ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
17639   if( p->nRef==0 ){
17640     pthread_mutex_unlock(&p->mutex);
17641   }
17642 #else
17643   pthread_mutex_unlock(&p->mutex);
17644 #endif
17645 
17646 #ifdef SQLITE_DEBUG
17647   if( p->trace ){
17648     printf("leave mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
17649   }
17650 #endif
17651 }
17652 
sqlite3DefaultMutex(void)17653 SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
17654   static const sqlite3_mutex_methods sMutex = {
17655     pthreadMutexInit,
17656     pthreadMutexEnd,
17657     pthreadMutexAlloc,
17658     pthreadMutexFree,
17659     pthreadMutexEnter,
17660     pthreadMutexTry,
17661     pthreadMutexLeave,
17662 #ifdef SQLITE_DEBUG
17663     pthreadMutexHeld,
17664     pthreadMutexNotheld
17665 #else
17666     0,
17667     0
17668 #endif
17669   };
17670 
17671   return &sMutex;
17672 }
17673 
17674 #endif /* SQLITE_MUTEX_PTHREAD */
17675 
17676 /************** End of mutex_unix.c ******************************************/
17677 /************** Begin file mutex_w32.c ***************************************/
17678 /*
17679 ** 2007 August 14
17680 **
17681 ** The author disclaims copyright to this source code.  In place of
17682 ** a legal notice, here is a blessing:
17683 **
17684 **    May you do good and not evil.
17685 **    May you find forgiveness for yourself and forgive others.
17686 **    May you share freely, never taking more than you give.
17687 **
17688 *************************************************************************
17689 ** This file contains the C functions that implement mutexes for win32
17690 */
17691 
17692 /*
17693 ** The code in this file is only used if we are compiling multithreaded
17694 ** on a win32 system.
17695 */
17696 #ifdef SQLITE_MUTEX_W32
17697 
17698 /*
17699 ** Each recursive mutex is an instance of the following structure.
17700 */
17701 struct sqlite3_mutex {
17702   CRITICAL_SECTION mutex;    /* Mutex controlling the lock */
17703   int id;                    /* Mutex type */
17704 #ifdef SQLITE_DEBUG
17705   volatile int nRef;         /* Number of enterances */
17706   volatile DWORD owner;      /* Thread holding this mutex */
17707   int trace;                 /* True to trace changes */
17708 #endif
17709 };
17710 #define SQLITE_W32_MUTEX_INITIALIZER { 0 }
17711 #ifdef SQLITE_DEBUG
17712 #define SQLITE3_MUTEX_INITIALIZER { SQLITE_W32_MUTEX_INITIALIZER, 0, 0L, (DWORD)0, 0 }
17713 #else
17714 #define SQLITE3_MUTEX_INITIALIZER { SQLITE_W32_MUTEX_INITIALIZER, 0 }
17715 #endif
17716 
17717 /*
17718 ** Return true (non-zero) if we are running under WinNT, Win2K, WinXP,
17719 ** or WinCE.  Return false (zero) for Win95, Win98, or WinME.
17720 **
17721 ** Here is an interesting observation:  Win95, Win98, and WinME lack
17722 ** the LockFileEx() API.  But we can still statically link against that
17723 ** API as long as we don't call it win running Win95/98/ME.  A call to
17724 ** this routine is used to determine if the host is Win95/98/ME or
17725 ** WinNT/2K/XP so that we will know whether or not we can safely call
17726 ** the LockFileEx() API.
17727 **
17728 ** mutexIsNT() is only used for the TryEnterCriticalSection() API call,
17729 ** which is only available if your application was compiled with
17730 ** _WIN32_WINNT defined to a value >= 0x0400.  Currently, the only
17731 ** call to TryEnterCriticalSection() is #ifdef'ed out, so #ifdef
17732 ** this out as well.
17733 */
17734 #if 0
17735 #if SQLITE_OS_WINCE
17736 # define mutexIsNT()  (1)
17737 #else
mutexIsNT(void)17738   static int mutexIsNT(void){
17739     static int osType = 0;
17740     if( osType==0 ){
17741       OSVERSIONINFO sInfo;
17742       sInfo.dwOSVersionInfoSize = sizeof(sInfo);
17743       GetVersionEx(&sInfo);
17744       osType = sInfo.dwPlatformId==VER_PLATFORM_WIN32_NT ? 2 : 1;
17745     }
17746     return osType==2;
17747   }
17748 #endif /* SQLITE_OS_WINCE */
17749 #endif
17750 
17751 #ifdef SQLITE_DEBUG
17752 /*
17753 ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
17754 ** intended for use only inside assert() statements.
17755 */
winMutexHeld(sqlite3_mutex * p)17756 static int winMutexHeld(sqlite3_mutex *p){
17757   return p->nRef!=0 && p->owner==GetCurrentThreadId();
17758 }
winMutexNotheld2(sqlite3_mutex * p,DWORD tid)17759 static int winMutexNotheld2(sqlite3_mutex *p, DWORD tid){
17760   return p->nRef==0 || p->owner!=tid;
17761 }
winMutexNotheld(sqlite3_mutex * p)17762 static int winMutexNotheld(sqlite3_mutex *p){
17763   DWORD tid = GetCurrentThreadId();
17764   return winMutexNotheld2(p, tid);
17765 }
17766 #endif
17767 
17768 
17769 /*
17770 ** Initialize and deinitialize the mutex subsystem.
17771 */
17772 static sqlite3_mutex winMutex_staticMutexes[6] = {
17773   SQLITE3_MUTEX_INITIALIZER,
17774   SQLITE3_MUTEX_INITIALIZER,
17775   SQLITE3_MUTEX_INITIALIZER,
17776   SQLITE3_MUTEX_INITIALIZER,
17777   SQLITE3_MUTEX_INITIALIZER,
17778   SQLITE3_MUTEX_INITIALIZER
17779 };
17780 static int winMutex_isInit = 0;
17781 /* As winMutexInit() and winMutexEnd() are called as part
17782 ** of the sqlite3_initialize and sqlite3_shutdown()
17783 ** processing, the "interlocked" magic is probably not
17784 ** strictly necessary.
17785 */
17786 static long winMutex_lock = 0;
17787 
winMutexInit(void)17788 static int winMutexInit(void){
17789   /* The first to increment to 1 does actual initialization */
17790   if( InterlockedCompareExchange(&winMutex_lock, 1, 0)==0 ){
17791     int i;
17792     for(i=0; i<ArraySize(winMutex_staticMutexes); i++){
17793       InitializeCriticalSection(&winMutex_staticMutexes[i].mutex);
17794     }
17795     winMutex_isInit = 1;
17796   }else{
17797     /* Someone else is in the process of initing the static mutexes */
17798     while( !winMutex_isInit ){
17799       Sleep(1);
17800     }
17801   }
17802   return SQLITE_OK;
17803 }
17804 
winMutexEnd(void)17805 static int winMutexEnd(void){
17806   /* The first to decrement to 0 does actual shutdown
17807   ** (which should be the last to shutdown.) */
17808   if( InterlockedCompareExchange(&winMutex_lock, 0, 1)==1 ){
17809     if( winMutex_isInit==1 ){
17810       int i;
17811       for(i=0; i<ArraySize(winMutex_staticMutexes); i++){
17812         DeleteCriticalSection(&winMutex_staticMutexes[i].mutex);
17813       }
17814       winMutex_isInit = 0;
17815     }
17816   }
17817   return SQLITE_OK;
17818 }
17819 
17820 /*
17821 ** The sqlite3_mutex_alloc() routine allocates a new
17822 ** mutex and returns a pointer to it.  If it returns NULL
17823 ** that means that a mutex could not be allocated.  SQLite
17824 ** will unwind its stack and return an error.  The argument
17825 ** to sqlite3_mutex_alloc() is one of these integer constants:
17826 **
17827 ** <ul>
17828 ** <li>  SQLITE_MUTEX_FAST
17829 ** <li>  SQLITE_MUTEX_RECURSIVE
17830 ** <li>  SQLITE_MUTEX_STATIC_MASTER
17831 ** <li>  SQLITE_MUTEX_STATIC_MEM
17832 ** <li>  SQLITE_MUTEX_STATIC_MEM2
17833 ** <li>  SQLITE_MUTEX_STATIC_PRNG
17834 ** <li>  SQLITE_MUTEX_STATIC_LRU
17835 ** <li>  SQLITE_MUTEX_STATIC_PMEM
17836 ** </ul>
17837 **
17838 ** The first two constants cause sqlite3_mutex_alloc() to create
17839 ** a new mutex.  The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
17840 ** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
17841 ** The mutex implementation does not need to make a distinction
17842 ** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
17843 ** not want to.  But SQLite will only request a recursive mutex in
17844 ** cases where it really needs one.  If a faster non-recursive mutex
17845 ** implementation is available on the host platform, the mutex subsystem
17846 ** might return such a mutex in response to SQLITE_MUTEX_FAST.
17847 **
17848 ** The other allowed parameters to sqlite3_mutex_alloc() each return
17849 ** a pointer to a static preexisting mutex.  Six static mutexes are
17850 ** used by the current version of SQLite.  Future versions of SQLite
17851 ** may add additional static mutexes.  Static mutexes are for internal
17852 ** use by SQLite only.  Applications that use SQLite mutexes should
17853 ** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
17854 ** SQLITE_MUTEX_RECURSIVE.
17855 **
17856 ** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
17857 ** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
17858 ** returns a different mutex on every call.  But for the static
17859 ** mutex types, the same mutex is returned on every call that has
17860 ** the same type number.
17861 */
winMutexAlloc(int iType)17862 static sqlite3_mutex *winMutexAlloc(int iType){
17863   sqlite3_mutex *p;
17864 
17865   switch( iType ){
17866     case SQLITE_MUTEX_FAST:
17867     case SQLITE_MUTEX_RECURSIVE: {
17868       p = sqlite3MallocZero( sizeof(*p) );
17869       if( p ){
17870 #ifdef SQLITE_DEBUG
17871         p->id = iType;
17872 #endif
17873         InitializeCriticalSection(&p->mutex);
17874       }
17875       break;
17876     }
17877     default: {
17878       assert( winMutex_isInit==1 );
17879       assert( iType-2 >= 0 );
17880       assert( iType-2 < ArraySize(winMutex_staticMutexes) );
17881       p = &winMutex_staticMutexes[iType-2];
17882 #ifdef SQLITE_DEBUG
17883       p->id = iType;
17884 #endif
17885       break;
17886     }
17887   }
17888   return p;
17889 }
17890 
17891 
17892 /*
17893 ** This routine deallocates a previously
17894 ** allocated mutex.  SQLite is careful to deallocate every
17895 ** mutex that it allocates.
17896 */
winMutexFree(sqlite3_mutex * p)17897 static void winMutexFree(sqlite3_mutex *p){
17898   assert( p );
17899   assert( p->nRef==0 && p->owner==0 );
17900   assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
17901   DeleteCriticalSection(&p->mutex);
17902   sqlite3_free(p);
17903 }
17904 
17905 /*
17906 ** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
17907 ** to enter a mutex.  If another thread is already within the mutex,
17908 ** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
17909 ** SQLITE_BUSY.  The sqlite3_mutex_try() interface returns SQLITE_OK
17910 ** upon successful entry.  Mutexes created using SQLITE_MUTEX_RECURSIVE can
17911 ** be entered multiple times by the same thread.  In such cases the,
17912 ** mutex must be exited an equal number of times before another thread
17913 ** can enter.  If the same thread tries to enter any other kind of mutex
17914 ** more than once, the behavior is undefined.
17915 */
winMutexEnter(sqlite3_mutex * p)17916 static void winMutexEnter(sqlite3_mutex *p){
17917 #ifdef SQLITE_DEBUG
17918   DWORD tid = GetCurrentThreadId();
17919   assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) );
17920 #endif
17921   EnterCriticalSection(&p->mutex);
17922 #ifdef SQLITE_DEBUG
17923   assert( p->nRef>0 || p->owner==0 );
17924   p->owner = tid;
17925   p->nRef++;
17926   if( p->trace ){
17927     printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
17928   }
17929 #endif
17930 }
winMutexTry(sqlite3_mutex * p)17931 static int winMutexTry(sqlite3_mutex *p){
17932 #ifndef NDEBUG
17933   DWORD tid = GetCurrentThreadId();
17934 #endif
17935   int rc = SQLITE_BUSY;
17936   assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) );
17937   /*
17938   ** The sqlite3_mutex_try() routine is very rarely used, and when it
17939   ** is used it is merely an optimization.  So it is OK for it to always
17940   ** fail.
17941   **
17942   ** The TryEnterCriticalSection() interface is only available on WinNT.
17943   ** And some windows compilers complain if you try to use it without
17944   ** first doing some #defines that prevent SQLite from building on Win98.
17945   ** For that reason, we will omit this optimization for now.  See
17946   ** ticket #2685.
17947   */
17948 #if 0
17949   if( mutexIsNT() && TryEnterCriticalSection(&p->mutex) ){
17950     p->owner = tid;
17951     p->nRef++;
17952     rc = SQLITE_OK;
17953   }
17954 #else
17955   UNUSED_PARAMETER(p);
17956 #endif
17957 #ifdef SQLITE_DEBUG
17958   if( rc==SQLITE_OK && p->trace ){
17959     printf("try mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
17960   }
17961 #endif
17962   return rc;
17963 }
17964 
17965 /*
17966 ** The sqlite3_mutex_leave() routine exits a mutex that was
17967 ** previously entered by the same thread.  The behavior
17968 ** is undefined if the mutex is not currently entered or
17969 ** is not currently allocated.  SQLite will never do either.
17970 */
winMutexLeave(sqlite3_mutex * p)17971 static void winMutexLeave(sqlite3_mutex *p){
17972 #ifndef NDEBUG
17973   DWORD tid = GetCurrentThreadId();
17974   assert( p->nRef>0 );
17975   assert( p->owner==tid );
17976   p->nRef--;
17977   if( p->nRef==0 ) p->owner = 0;
17978   assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
17979 #endif
17980   LeaveCriticalSection(&p->mutex);
17981 #ifdef SQLITE_DEBUG
17982   if( p->trace ){
17983     printf("leave mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
17984   }
17985 #endif
17986 }
17987 
sqlite3DefaultMutex(void)17988 SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
17989   static const sqlite3_mutex_methods sMutex = {
17990     winMutexInit,
17991     winMutexEnd,
17992     winMutexAlloc,
17993     winMutexFree,
17994     winMutexEnter,
17995     winMutexTry,
17996     winMutexLeave,
17997 #ifdef SQLITE_DEBUG
17998     winMutexHeld,
17999     winMutexNotheld
18000 #else
18001     0,
18002     0
18003 #endif
18004   };
18005 
18006   return &sMutex;
18007 }
18008 #endif /* SQLITE_MUTEX_W32 */
18009 
18010 /************** End of mutex_w32.c *******************************************/
18011 /************** Begin file malloc.c ******************************************/
18012 /*
18013 ** 2001 September 15
18014 **
18015 ** The author disclaims copyright to this source code.  In place of
18016 ** a legal notice, here is a blessing:
18017 **
18018 **    May you do good and not evil.
18019 **    May you find forgiveness for yourself and forgive others.
18020 **    May you share freely, never taking more than you give.
18021 **
18022 *************************************************************************
18023 **
18024 ** Memory allocation functions used throughout sqlite.
18025 */
18026 
18027 /*
18028 ** Attempt to release up to n bytes of non-essential memory currently
18029 ** held by SQLite. An example of non-essential memory is memory used to
18030 ** cache database pages that are not currently in use.
18031 */
sqlite3_release_memory(int n)18032 SQLITE_API int sqlite3_release_memory(int n){
18033 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
18034   return sqlite3PcacheReleaseMemory(n);
18035 #else
18036   /* IMPLEMENTATION-OF: R-34391-24921 The sqlite3_release_memory() routine
18037   ** is a no-op returning zero if SQLite is not compiled with
18038   ** SQLITE_ENABLE_MEMORY_MANAGEMENT. */
18039   UNUSED_PARAMETER(n);
18040   return 0;
18041 #endif
18042 }
18043 
18044 /*
18045 ** An instance of the following object records the location of
18046 ** each unused scratch buffer.
18047 */
18048 typedef struct ScratchFreeslot {
18049   struct ScratchFreeslot *pNext;   /* Next unused scratch buffer */
18050 } ScratchFreeslot;
18051 
18052 /*
18053 ** State information local to the memory allocation subsystem.
18054 */
18055 static SQLITE_WSD struct Mem0Global {
18056   sqlite3_mutex *mutex;         /* Mutex to serialize access */
18057 
18058   /*
18059   ** The alarm callback and its arguments.  The mem0.mutex lock will
18060   ** be held while the callback is running.  Recursive calls into
18061   ** the memory subsystem are allowed, but no new callbacks will be
18062   ** issued.
18063   */
18064   sqlite3_int64 alarmThreshold;
18065   void (*alarmCallback)(void*, sqlite3_int64,int);
18066   void *alarmArg;
18067 
18068   /*
18069   ** Pointers to the end of sqlite3GlobalConfig.pScratch memory
18070   ** (so that a range test can be used to determine if an allocation
18071   ** being freed came from pScratch) and a pointer to the list of
18072   ** unused scratch allocations.
18073   */
18074   void *pScratchEnd;
18075   ScratchFreeslot *pScratchFree;
18076   u32 nScratchFree;
18077 
18078   /*
18079   ** True if heap is nearly "full" where "full" is defined by the
18080   ** sqlite3_soft_heap_limit() setting.
18081   */
18082   int nearlyFull;
18083 } mem0 = { 0, 0, 0, 0, 0, 0, 0, 0 };
18084 
18085 #define mem0 GLOBAL(struct Mem0Global, mem0)
18086 
18087 /*
18088 ** This routine runs when the memory allocator sees that the
18089 ** total memory allocation is about to exceed the soft heap
18090 ** limit.
18091 */
softHeapLimitEnforcer(void * NotUsed,sqlite3_int64 NotUsed2,int allocSize)18092 static void softHeapLimitEnforcer(
18093   void *NotUsed,
18094   sqlite3_int64 NotUsed2,
18095   int allocSize
18096 ){
18097   UNUSED_PARAMETER2(NotUsed, NotUsed2);
18098   sqlite3_release_memory(allocSize);
18099 }
18100 
18101 /*
18102 ** Change the alarm callback
18103 */
sqlite3MemoryAlarm(void (* xCallback)(void * pArg,sqlite3_int64 used,int N),void * pArg,sqlite3_int64 iThreshold)18104 static int sqlite3MemoryAlarm(
18105   void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
18106   void *pArg,
18107   sqlite3_int64 iThreshold
18108 ){
18109   int nUsed;
18110   sqlite3_mutex_enter(mem0.mutex);
18111   mem0.alarmCallback = xCallback;
18112   mem0.alarmArg = pArg;
18113   mem0.alarmThreshold = iThreshold;
18114   nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
18115   mem0.nearlyFull = (iThreshold>0 && iThreshold<=nUsed);
18116   sqlite3_mutex_leave(mem0.mutex);
18117   return SQLITE_OK;
18118 }
18119 
18120 #ifndef SQLITE_OMIT_DEPRECATED
18121 /*
18122 ** Deprecated external interface.  Internal/core SQLite code
18123 ** should call sqlite3MemoryAlarm.
18124 */
sqlite3_memory_alarm(void (* xCallback)(void * pArg,sqlite3_int64 used,int N),void * pArg,sqlite3_int64 iThreshold)18125 SQLITE_API int sqlite3_memory_alarm(
18126   void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
18127   void *pArg,
18128   sqlite3_int64 iThreshold
18129 ){
18130   return sqlite3MemoryAlarm(xCallback, pArg, iThreshold);
18131 }
18132 #endif
18133 
18134 /*
18135 ** Set the soft heap-size limit for the library. Passing a zero or
18136 ** negative value indicates no limit.
18137 */
sqlite3_soft_heap_limit64(sqlite3_int64 n)18138 SQLITE_API sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 n){
18139   sqlite3_int64 priorLimit;
18140   sqlite3_int64 excess;
18141 #ifndef SQLITE_OMIT_AUTOINIT
18142   sqlite3_initialize();
18143 #endif
18144   sqlite3_mutex_enter(mem0.mutex);
18145   priorLimit = mem0.alarmThreshold;
18146   sqlite3_mutex_leave(mem0.mutex);
18147   if( n<0 ) return priorLimit;
18148   if( n>0 ){
18149     sqlite3MemoryAlarm(softHeapLimitEnforcer, 0, n);
18150   }else{
18151     sqlite3MemoryAlarm(0, 0, 0);
18152   }
18153   excess = sqlite3_memory_used() - n;
18154   if( excess>0 ) sqlite3_release_memory((int)(excess & 0x7fffffff));
18155   return priorLimit;
18156 }
sqlite3_soft_heap_limit(int n)18157 SQLITE_API void sqlite3_soft_heap_limit(int n){
18158   if( n<0 ) n = 0;
18159   sqlite3_soft_heap_limit64(n);
18160 }
18161 
18162 /*
18163 ** Initialize the memory allocation subsystem.
18164 */
sqlite3MallocInit(void)18165 SQLITE_PRIVATE int sqlite3MallocInit(void){
18166   if( sqlite3GlobalConfig.m.xMalloc==0 ){
18167     sqlite3MemSetDefault();
18168   }
18169   memset(&mem0, 0, sizeof(mem0));
18170   if( sqlite3GlobalConfig.bCoreMutex ){
18171     mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
18172   }
18173   if( sqlite3GlobalConfig.pScratch && sqlite3GlobalConfig.szScratch>=100
18174       && sqlite3GlobalConfig.nScratch>0 ){
18175     int i, n, sz;
18176     ScratchFreeslot *pSlot;
18177     sz = ROUNDDOWN8(sqlite3GlobalConfig.szScratch);
18178     sqlite3GlobalConfig.szScratch = sz;
18179     pSlot = (ScratchFreeslot*)sqlite3GlobalConfig.pScratch;
18180     n = sqlite3GlobalConfig.nScratch;
18181     mem0.pScratchFree = pSlot;
18182     mem0.nScratchFree = n;
18183     for(i=0; i<n-1; i++){
18184       pSlot->pNext = (ScratchFreeslot*)(sz+(char*)pSlot);
18185       pSlot = pSlot->pNext;
18186     }
18187     pSlot->pNext = 0;
18188     mem0.pScratchEnd = (void*)&pSlot[1];
18189   }else{
18190     mem0.pScratchEnd = 0;
18191     sqlite3GlobalConfig.pScratch = 0;
18192     sqlite3GlobalConfig.szScratch = 0;
18193     sqlite3GlobalConfig.nScratch = 0;
18194   }
18195   if( sqlite3GlobalConfig.pPage==0 || sqlite3GlobalConfig.szPage<512
18196       || sqlite3GlobalConfig.nPage<1 ){
18197     sqlite3GlobalConfig.pPage = 0;
18198     sqlite3GlobalConfig.szPage = 0;
18199     sqlite3GlobalConfig.nPage = 0;
18200   }
18201   return sqlite3GlobalConfig.m.xInit(sqlite3GlobalConfig.m.pAppData);
18202 }
18203 
18204 /*
18205 ** Return true if the heap is currently under memory pressure - in other
18206 ** words if the amount of heap used is close to the limit set by
18207 ** sqlite3_soft_heap_limit().
18208 */
sqlite3HeapNearlyFull(void)18209 SQLITE_PRIVATE int sqlite3HeapNearlyFull(void){
18210   return mem0.nearlyFull;
18211 }
18212 
18213 /*
18214 ** Deinitialize the memory allocation subsystem.
18215 */
sqlite3MallocEnd(void)18216 SQLITE_PRIVATE void sqlite3MallocEnd(void){
18217   if( sqlite3GlobalConfig.m.xShutdown ){
18218     sqlite3GlobalConfig.m.xShutdown(sqlite3GlobalConfig.m.pAppData);
18219   }
18220   memset(&mem0, 0, sizeof(mem0));
18221 }
18222 
18223 /*
18224 ** Return the amount of memory currently checked out.
18225 */
sqlite3_memory_used(void)18226 SQLITE_API sqlite3_int64 sqlite3_memory_used(void){
18227   int n, mx;
18228   sqlite3_int64 res;
18229   sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, 0);
18230   res = (sqlite3_int64)n;  /* Work around bug in Borland C. Ticket #3216 */
18231   return res;
18232 }
18233 
18234 /*
18235 ** Return the maximum amount of memory that has ever been
18236 ** checked out since either the beginning of this process
18237 ** or since the most recent reset.
18238 */
sqlite3_memory_highwater(int resetFlag)18239 SQLITE_API sqlite3_int64 sqlite3_memory_highwater(int resetFlag){
18240   int n, mx;
18241   sqlite3_int64 res;
18242   sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, resetFlag);
18243   res = (sqlite3_int64)mx;  /* Work around bug in Borland C. Ticket #3216 */
18244   return res;
18245 }
18246 
18247 /*
18248 ** Trigger the alarm
18249 */
sqlite3MallocAlarm(int nByte)18250 static void sqlite3MallocAlarm(int nByte){
18251   void (*xCallback)(void*,sqlite3_int64,int);
18252   sqlite3_int64 nowUsed;
18253   void *pArg;
18254   if( mem0.alarmCallback==0 ) return;
18255   xCallback = mem0.alarmCallback;
18256   nowUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
18257   pArg = mem0.alarmArg;
18258   mem0.alarmCallback = 0;
18259   sqlite3_mutex_leave(mem0.mutex);
18260   xCallback(pArg, nowUsed, nByte);
18261   sqlite3_mutex_enter(mem0.mutex);
18262   mem0.alarmCallback = xCallback;
18263   mem0.alarmArg = pArg;
18264 }
18265 
18266 /*
18267 ** Do a memory allocation with statistics and alarms.  Assume the
18268 ** lock is already held.
18269 */
mallocWithAlarm(int n,void ** pp)18270 static int mallocWithAlarm(int n, void **pp){
18271   int nFull;
18272   void *p;
18273   assert( sqlite3_mutex_held(mem0.mutex) );
18274   nFull = sqlite3GlobalConfig.m.xRoundup(n);
18275   sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, n);
18276   if( mem0.alarmCallback!=0 ){
18277     int nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
18278     if( nUsed >= mem0.alarmThreshold - nFull ){
18279       mem0.nearlyFull = 1;
18280       sqlite3MallocAlarm(nFull);
18281     }else{
18282       mem0.nearlyFull = 0;
18283     }
18284   }
18285   p = sqlite3GlobalConfig.m.xMalloc(nFull);
18286 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
18287   if( p==0 && mem0.alarmCallback ){
18288     sqlite3MallocAlarm(nFull);
18289     p = sqlite3GlobalConfig.m.xMalloc(nFull);
18290   }
18291 #endif
18292   if( p ){
18293     nFull = sqlite3MallocSize(p);
18294     sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nFull);
18295     sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, 1);
18296   }
18297   *pp = p;
18298   return nFull;
18299 }
18300 
18301 /*
18302 ** Allocate memory.  This routine is like sqlite3_malloc() except that it
18303 ** assumes the memory subsystem has already been initialized.
18304 */
sqlite3Malloc(int n)18305 SQLITE_PRIVATE void *sqlite3Malloc(int n){
18306   void *p;
18307   if( n<=0               /* IMP: R-65312-04917 */
18308    || n>=0x7fffff00
18309   ){
18310     /* A memory allocation of a number of bytes which is near the maximum
18311     ** signed integer value might cause an integer overflow inside of the
18312     ** xMalloc().  Hence we limit the maximum size to 0x7fffff00, giving
18313     ** 255 bytes of overhead.  SQLite itself will never use anything near
18314     ** this amount.  The only way to reach the limit is with sqlite3_malloc() */
18315     p = 0;
18316   }else if( sqlite3GlobalConfig.bMemstat ){
18317     sqlite3_mutex_enter(mem0.mutex);
18318     mallocWithAlarm(n, &p);
18319     sqlite3_mutex_leave(mem0.mutex);
18320   }else{
18321     p = sqlite3GlobalConfig.m.xMalloc(n);
18322   }
18323   assert( EIGHT_BYTE_ALIGNMENT(p) );  /* IMP: R-04675-44850 */
18324   return p;
18325 }
18326 
18327 /*
18328 ** This version of the memory allocation is for use by the application.
18329 ** First make sure the memory subsystem is initialized, then do the
18330 ** allocation.
18331 */
sqlite3_malloc(int n)18332 SQLITE_API void *sqlite3_malloc(int n){
18333 #ifndef SQLITE_OMIT_AUTOINIT
18334   if( sqlite3_initialize() ) return 0;
18335 #endif
18336   return sqlite3Malloc(n);
18337 }
18338 
18339 /*
18340 ** Each thread may only have a single outstanding allocation from
18341 ** xScratchMalloc().  We verify this constraint in the single-threaded
18342 ** case by setting scratchAllocOut to 1 when an allocation
18343 ** is outstanding clearing it when the allocation is freed.
18344 */
18345 #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
18346 static int scratchAllocOut = 0;
18347 #endif
18348 
18349 
18350 /*
18351 ** Allocate memory that is to be used and released right away.
18352 ** This routine is similar to alloca() in that it is not intended
18353 ** for situations where the memory might be held long-term.  This
18354 ** routine is intended to get memory to old large transient data
18355 ** structures that would not normally fit on the stack of an
18356 ** embedded processor.
18357 */
sqlite3ScratchMalloc(int n)18358 SQLITE_PRIVATE void *sqlite3ScratchMalloc(int n){
18359   void *p;
18360   assert( n>0 );
18361 
18362   sqlite3_mutex_enter(mem0.mutex);
18363   if( mem0.nScratchFree && sqlite3GlobalConfig.szScratch>=n ){
18364     p = mem0.pScratchFree;
18365     mem0.pScratchFree = mem0.pScratchFree->pNext;
18366     mem0.nScratchFree--;
18367     sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, 1);
18368     sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
18369     sqlite3_mutex_leave(mem0.mutex);
18370   }else{
18371     if( sqlite3GlobalConfig.bMemstat ){
18372       sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
18373       n = mallocWithAlarm(n, &p);
18374       if( p ) sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, n);
18375       sqlite3_mutex_leave(mem0.mutex);
18376     }else{
18377       sqlite3_mutex_leave(mem0.mutex);
18378       p = sqlite3GlobalConfig.m.xMalloc(n);
18379     }
18380     sqlite3MemdebugSetType(p, MEMTYPE_SCRATCH);
18381   }
18382   assert( sqlite3_mutex_notheld(mem0.mutex) );
18383 
18384 
18385 #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
18386   /* Verify that no more than two scratch allocations per thread
18387   ** are outstanding at one time.  (This is only checked in the
18388   ** single-threaded case since checking in the multi-threaded case
18389   ** would be much more complicated.) */
18390   assert( scratchAllocOut<=1 );
18391   if( p ) scratchAllocOut++;
18392 #endif
18393 
18394   return p;
18395 }
sqlite3ScratchFree(void * p)18396 SQLITE_PRIVATE void sqlite3ScratchFree(void *p){
18397   if( p ){
18398 
18399 #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
18400     /* Verify that no more than two scratch allocation per thread
18401     ** is outstanding at one time.  (This is only checked in the
18402     ** single-threaded case since checking in the multi-threaded case
18403     ** would be much more complicated.) */
18404     assert( scratchAllocOut>=1 && scratchAllocOut<=2 );
18405     scratchAllocOut--;
18406 #endif
18407 
18408     if( p>=sqlite3GlobalConfig.pScratch && p<mem0.pScratchEnd ){
18409       /* Release memory from the SQLITE_CONFIG_SCRATCH allocation */
18410       ScratchFreeslot *pSlot;
18411       pSlot = (ScratchFreeslot*)p;
18412       sqlite3_mutex_enter(mem0.mutex);
18413       pSlot->pNext = mem0.pScratchFree;
18414       mem0.pScratchFree = pSlot;
18415       mem0.nScratchFree++;
18416       assert( mem0.nScratchFree <= (u32)sqlite3GlobalConfig.nScratch );
18417       sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, -1);
18418       sqlite3_mutex_leave(mem0.mutex);
18419     }else{
18420       /* Release memory back to the heap */
18421       assert( sqlite3MemdebugHasType(p, MEMTYPE_SCRATCH) );
18422       assert( sqlite3MemdebugNoType(p, ~MEMTYPE_SCRATCH) );
18423       sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
18424       if( sqlite3GlobalConfig.bMemstat ){
18425         int iSize = sqlite3MallocSize(p);
18426         sqlite3_mutex_enter(mem0.mutex);
18427         sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, -iSize);
18428         sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize);
18429         sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, -1);
18430         sqlite3GlobalConfig.m.xFree(p);
18431         sqlite3_mutex_leave(mem0.mutex);
18432       }else{
18433         sqlite3GlobalConfig.m.xFree(p);
18434       }
18435     }
18436   }
18437 }
18438 
18439 /*
18440 ** TRUE if p is a lookaside memory allocation from db
18441 */
18442 #ifndef SQLITE_OMIT_LOOKASIDE
isLookaside(sqlite3 * db,void * p)18443 static int isLookaside(sqlite3 *db, void *p){
18444   return p && p>=db->lookaside.pStart && p<db->lookaside.pEnd;
18445 }
18446 #else
18447 #define isLookaside(A,B) 0
18448 #endif
18449 
18450 /*
18451 ** Return the size of a memory allocation previously obtained from
18452 ** sqlite3Malloc() or sqlite3_malloc().
18453 */
sqlite3MallocSize(void * p)18454 SQLITE_PRIVATE int sqlite3MallocSize(void *p){
18455   assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
18456   assert( sqlite3MemdebugNoType(p, MEMTYPE_DB) );
18457   return sqlite3GlobalConfig.m.xSize(p);
18458 }
sqlite3DbMallocSize(sqlite3 * db,void * p)18459 SQLITE_PRIVATE int sqlite3DbMallocSize(sqlite3 *db, void *p){
18460   assert( db==0 || sqlite3_mutex_held(db->mutex) );
18461   if( db && isLookaside(db, p) ){
18462     return db->lookaside.sz;
18463   }else{
18464     assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) );
18465     assert( sqlite3MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) );
18466     assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
18467     return sqlite3GlobalConfig.m.xSize(p);
18468   }
18469 }
18470 
18471 /*
18472 ** Free memory previously obtained from sqlite3Malloc().
18473 */
sqlite3_free(void * p)18474 SQLITE_API void sqlite3_free(void *p){
18475   if( p==0 ) return;  /* IMP: R-49053-54554 */
18476   assert( sqlite3MemdebugNoType(p, MEMTYPE_DB) );
18477   assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
18478   if( sqlite3GlobalConfig.bMemstat ){
18479     sqlite3_mutex_enter(mem0.mutex);
18480     sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -sqlite3MallocSize(p));
18481     sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, -1);
18482     sqlite3GlobalConfig.m.xFree(p);
18483     sqlite3_mutex_leave(mem0.mutex);
18484   }else{
18485     sqlite3GlobalConfig.m.xFree(p);
18486   }
18487 }
18488 
18489 /*
18490 ** Free memory that might be associated with a particular database
18491 ** connection.
18492 */
sqlite3DbFree(sqlite3 * db,void * p)18493 SQLITE_PRIVATE void sqlite3DbFree(sqlite3 *db, void *p){
18494   assert( db==0 || sqlite3_mutex_held(db->mutex) );
18495   if( db ){
18496     if( db->pnBytesFreed ){
18497       *db->pnBytesFreed += sqlite3DbMallocSize(db, p);
18498       return;
18499     }
18500     if( isLookaside(db, p) ){
18501       LookasideSlot *pBuf = (LookasideSlot*)p;
18502       pBuf->pNext = db->lookaside.pFree;
18503       db->lookaside.pFree = pBuf;
18504       db->lookaside.nOut--;
18505       return;
18506     }
18507   }
18508   assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) );
18509   assert( sqlite3MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) );
18510   assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
18511   sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
18512   sqlite3_free(p);
18513 }
18514 
18515 /*
18516 ** Change the size of an existing memory allocation
18517 */
sqlite3Realloc(void * pOld,int nBytes)18518 SQLITE_PRIVATE void *sqlite3Realloc(void *pOld, int nBytes){
18519   int nOld, nNew, nDiff;
18520   void *pNew;
18521   if( pOld==0 ){
18522     return sqlite3Malloc(nBytes); /* IMP: R-28354-25769 */
18523   }
18524   if( nBytes<=0 ){
18525     sqlite3_free(pOld); /* IMP: R-31593-10574 */
18526     return 0;
18527   }
18528   if( nBytes>=0x7fffff00 ){
18529     /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */
18530     return 0;
18531   }
18532   nOld = sqlite3MallocSize(pOld);
18533   /* IMPLEMENTATION-OF: R-46199-30249 SQLite guarantees that the second
18534   ** argument to xRealloc is always a value returned by a prior call to
18535   ** xRoundup. */
18536   nNew = sqlite3GlobalConfig.m.xRoundup(nBytes);
18537   if( nOld==nNew ){
18538     pNew = pOld;
18539   }else if( sqlite3GlobalConfig.bMemstat ){
18540     sqlite3_mutex_enter(mem0.mutex);
18541     sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, nBytes);
18542     nDiff = nNew - nOld;
18543     if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED) >=
18544           mem0.alarmThreshold-nDiff ){
18545       sqlite3MallocAlarm(nDiff);
18546     }
18547     assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) );
18548     assert( sqlite3MemdebugNoType(pOld, ~MEMTYPE_HEAP) );
18549     pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
18550     if( pNew==0 && mem0.alarmCallback ){
18551       sqlite3MallocAlarm(nBytes);
18552       pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
18553     }
18554     if( pNew ){
18555       nNew = sqlite3MallocSize(pNew);
18556       sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
18557     }
18558     sqlite3_mutex_leave(mem0.mutex);
18559   }else{
18560     pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
18561   }
18562   assert( EIGHT_BYTE_ALIGNMENT(pNew) ); /* IMP: R-04675-44850 */
18563   return pNew;
18564 }
18565 
18566 /*
18567 ** The public interface to sqlite3Realloc.  Make sure that the memory
18568 ** subsystem is initialized prior to invoking sqliteRealloc.
18569 */
sqlite3_realloc(void * pOld,int n)18570 SQLITE_API void *sqlite3_realloc(void *pOld, int n){
18571 #ifndef SQLITE_OMIT_AUTOINIT
18572   if( sqlite3_initialize() ) return 0;
18573 #endif
18574   return sqlite3Realloc(pOld, n);
18575 }
18576 
18577 
18578 /*
18579 ** Allocate and zero memory.
18580 */
sqlite3MallocZero(int n)18581 SQLITE_PRIVATE void *sqlite3MallocZero(int n){
18582   void *p = sqlite3Malloc(n);
18583   if( p ){
18584     memset(p, 0, n);
18585   }
18586   return p;
18587 }
18588 
18589 /*
18590 ** Allocate and zero memory.  If the allocation fails, make
18591 ** the mallocFailed flag in the connection pointer.
18592 */
sqlite3DbMallocZero(sqlite3 * db,int n)18593 SQLITE_PRIVATE void *sqlite3DbMallocZero(sqlite3 *db, int n){
18594   void *p = sqlite3DbMallocRaw(db, n);
18595   if( p ){
18596     memset(p, 0, n);
18597   }
18598   return p;
18599 }
18600 
18601 /*
18602 ** Allocate and zero memory.  If the allocation fails, make
18603 ** the mallocFailed flag in the connection pointer.
18604 **
18605 ** If db!=0 and db->mallocFailed is true (indicating a prior malloc
18606 ** failure on the same database connection) then always return 0.
18607 ** Hence for a particular database connection, once malloc starts
18608 ** failing, it fails consistently until mallocFailed is reset.
18609 ** This is an important assumption.  There are many places in the
18610 ** code that do things like this:
18611 **
18612 **         int *a = (int*)sqlite3DbMallocRaw(db, 100);
18613 **         int *b = (int*)sqlite3DbMallocRaw(db, 200);
18614 **         if( b ) a[10] = 9;
18615 **
18616 ** In other words, if a subsequent malloc (ex: "b") worked, it is assumed
18617 ** that all prior mallocs (ex: "a") worked too.
18618 */
sqlite3DbMallocRaw(sqlite3 * db,int n)18619 SQLITE_PRIVATE void *sqlite3DbMallocRaw(sqlite3 *db, int n){
18620   void *p;
18621   assert( db==0 || sqlite3_mutex_held(db->mutex) );
18622   assert( db==0 || db->pnBytesFreed==0 );
18623 #ifndef SQLITE_OMIT_LOOKASIDE
18624   if( db ){
18625     LookasideSlot *pBuf;
18626     if( db->mallocFailed ){
18627       return 0;
18628     }
18629     if( db->lookaside.bEnabled ){
18630       if( n>db->lookaside.sz ){
18631         db->lookaside.anStat[1]++;
18632       }else if( (pBuf = db->lookaside.pFree)==0 ){
18633         db->lookaside.anStat[2]++;
18634       }else{
18635         db->lookaside.pFree = pBuf->pNext;
18636         db->lookaside.nOut++;
18637         db->lookaside.anStat[0]++;
18638         if( db->lookaside.nOut>db->lookaside.mxOut ){
18639           db->lookaside.mxOut = db->lookaside.nOut;
18640         }
18641         return (void*)pBuf;
18642       }
18643     }
18644   }
18645 #else
18646   if( db && db->mallocFailed ){
18647     return 0;
18648   }
18649 #endif
18650   p = sqlite3Malloc(n);
18651   if( !p && db ){
18652     db->mallocFailed = 1;
18653   }
18654   sqlite3MemdebugSetType(p, MEMTYPE_DB |
18655          ((db && db->lookaside.bEnabled) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
18656   return p;
18657 }
18658 
18659 /*
18660 ** Resize the block of memory pointed to by p to n bytes. If the
18661 ** resize fails, set the mallocFailed flag in the connection object.
18662 */
sqlite3DbRealloc(sqlite3 * db,void * p,int n)18663 SQLITE_PRIVATE void *sqlite3DbRealloc(sqlite3 *db, void *p, int n){
18664   void *pNew = 0;
18665   assert( db!=0 );
18666   assert( sqlite3_mutex_held(db->mutex) );
18667   if( db->mallocFailed==0 ){
18668     if( p==0 ){
18669       return sqlite3DbMallocRaw(db, n);
18670     }
18671     if( isLookaside(db, p) ){
18672       if( n<=db->lookaside.sz ){
18673         return p;
18674       }
18675       pNew = sqlite3DbMallocRaw(db, n);
18676       if( pNew ){
18677         memcpy(pNew, p, db->lookaside.sz);
18678         sqlite3DbFree(db, p);
18679       }
18680     }else{
18681       assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) );
18682       assert( sqlite3MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) );
18683       sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
18684       pNew = sqlite3_realloc(p, n);
18685       if( !pNew ){
18686         sqlite3MemdebugSetType(p, MEMTYPE_DB|MEMTYPE_HEAP);
18687         db->mallocFailed = 1;
18688       }
18689       sqlite3MemdebugSetType(pNew, MEMTYPE_DB |
18690             (db->lookaside.bEnabled ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
18691     }
18692   }
18693   return pNew;
18694 }
18695 
18696 /*
18697 ** Attempt to reallocate p.  If the reallocation fails, then free p
18698 ** and set the mallocFailed flag in the database connection.
18699 */
sqlite3DbReallocOrFree(sqlite3 * db,void * p,int n)18700 SQLITE_PRIVATE void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, int n){
18701   void *pNew;
18702   pNew = sqlite3DbRealloc(db, p, n);
18703   if( !pNew ){
18704     sqlite3DbFree(db, p);
18705   }
18706   return pNew;
18707 }
18708 
18709 /*
18710 ** Make a copy of a string in memory obtained from sqliteMalloc(). These
18711 ** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
18712 ** is because when memory debugging is turned on, these two functions are
18713 ** called via macros that record the current file and line number in the
18714 ** ThreadData structure.
18715 */
sqlite3DbStrDup(sqlite3 * db,const char * z)18716 SQLITE_PRIVATE char *sqlite3DbStrDup(sqlite3 *db, const char *z){
18717   char *zNew;
18718   size_t n;
18719   if( z==0 ){
18720     return 0;
18721   }
18722   n = sqlite3Strlen30(z) + 1;
18723   assert( (n&0x7fffffff)==n );
18724   zNew = sqlite3DbMallocRaw(db, (int)n);
18725   if( zNew ){
18726     memcpy(zNew, z, n);
18727   }
18728   return zNew;
18729 }
sqlite3DbStrNDup(sqlite3 * db,const char * z,int n)18730 SQLITE_PRIVATE char *sqlite3DbStrNDup(sqlite3 *db, const char *z, int n){
18731   char *zNew;
18732   if( z==0 ){
18733     return 0;
18734   }
18735   assert( (n&0x7fffffff)==n );
18736   zNew = sqlite3DbMallocRaw(db, n+1);
18737   if( zNew ){
18738     memcpy(zNew, z, n);
18739     zNew[n] = 0;
18740   }
18741   return zNew;
18742 }
18743 
18744 /*
18745 ** Create a string from the zFromat argument and the va_list that follows.
18746 ** Store the string in memory obtained from sqliteMalloc() and make *pz
18747 ** point to that string.
18748 */
sqlite3SetString(char ** pz,sqlite3 * db,const char * zFormat,...)18749 SQLITE_PRIVATE void sqlite3SetString(char **pz, sqlite3 *db, const char *zFormat, ...){
18750   va_list ap;
18751   char *z;
18752 
18753   va_start(ap, zFormat);
18754   z = sqlite3VMPrintf(db, zFormat, ap);
18755   va_end(ap);
18756   sqlite3DbFree(db, *pz);
18757   *pz = z;
18758 }
18759 
18760 
18761 /*
18762 ** This function must be called before exiting any API function (i.e.
18763 ** returning control to the user) that has called sqlite3_malloc or
18764 ** sqlite3_realloc.
18765 **
18766 ** The returned value is normally a copy of the second argument to this
18767 ** function. However, if a malloc() failure has occurred since the previous
18768 ** invocation SQLITE_NOMEM is returned instead.
18769 **
18770 ** If the first argument, db, is not NULL and a malloc() error has occurred,
18771 ** then the connection error-code (the value returned by sqlite3_errcode())
18772 ** is set to SQLITE_NOMEM.
18773 */
sqlite3ApiExit(sqlite3 * db,int rc)18774 SQLITE_PRIVATE int sqlite3ApiExit(sqlite3* db, int rc){
18775   /* If the db handle is not NULL, then we must hold the connection handle
18776   ** mutex here. Otherwise the read (and possible write) of db->mallocFailed
18777   ** is unsafe, as is the call to sqlite3Error().
18778   */
18779   assert( !db || sqlite3_mutex_held(db->mutex) );
18780   if( db && (db->mallocFailed || rc==SQLITE_IOERR_NOMEM) ){
18781     sqlite3Error(db, SQLITE_NOMEM, 0);
18782     db->mallocFailed = 0;
18783     rc = SQLITE_NOMEM;
18784   }
18785   return rc & (db ? db->errMask : 0xff);
18786 }
18787 
18788 /************** End of malloc.c **********************************************/
18789 /************** Begin file printf.c ******************************************/
18790 /*
18791 ** The "printf" code that follows dates from the 1980's.  It is in
18792 ** the public domain.  The original comments are included here for
18793 ** completeness.  They are very out-of-date but might be useful as
18794 ** an historical reference.  Most of the "enhancements" have been backed
18795 ** out so that the functionality is now the same as standard printf().
18796 **
18797 **************************************************************************
18798 **
18799 ** The following modules is an enhanced replacement for the "printf" subroutines
18800 ** found in the standard C library.  The following enhancements are
18801 ** supported:
18802 **
18803 **      +  Additional functions.  The standard set of "printf" functions
18804 **         includes printf, fprintf, sprintf, vprintf, vfprintf, and
18805 **         vsprintf.  This module adds the following:
18806 **
18807 **           *  snprintf -- Works like sprintf, but has an extra argument
18808 **                          which is the size of the buffer written to.
18809 **
18810 **           *  mprintf --  Similar to sprintf.  Writes output to memory
18811 **                          obtained from malloc.
18812 **
18813 **           *  xprintf --  Calls a function to dispose of output.
18814 **
18815 **           *  nprintf --  No output, but returns the number of characters
18816 **                          that would have been output by printf.
18817 **
18818 **           *  A v- version (ex: vsnprintf) of every function is also
18819 **              supplied.
18820 **
18821 **      +  A few extensions to the formatting notation are supported:
18822 **
18823 **           *  The "=" flag (similar to "-") causes the output to be
18824 **              be centered in the appropriately sized field.
18825 **
18826 **           *  The %b field outputs an integer in binary notation.
18827 **
18828 **           *  The %c field now accepts a precision.  The character output
18829 **              is repeated by the number of times the precision specifies.
18830 **
18831 **           *  The %' field works like %c, but takes as its character the
18832 **              next character of the format string, instead of the next
18833 **              argument.  For example,  printf("%.78'-")  prints 78 minus
18834 **              signs, the same as  printf("%.78c",'-').
18835 **
18836 **      +  When compiled using GCC on a SPARC, this version of printf is
18837 **         faster than the library printf for SUN OS 4.1.
18838 **
18839 **      +  All functions are fully reentrant.
18840 **
18841 */
18842 
18843 /*
18844 ** Conversion types fall into various categories as defined by the
18845 ** following enumeration.
18846 */
18847 #define etRADIX       1 /* Integer types.  %d, %x, %o, and so forth */
18848 #define etFLOAT       2 /* Floating point.  %f */
18849 #define etEXP         3 /* Exponentional notation. %e and %E */
18850 #define etGENERIC     4 /* Floating or exponential, depending on exponent. %g */
18851 #define etSIZE        5 /* Return number of characters processed so far. %n */
18852 #define etSTRING      6 /* Strings. %s */
18853 #define etDYNSTRING   7 /* Dynamically allocated strings. %z */
18854 #define etPERCENT     8 /* Percent symbol. %% */
18855 #define etCHARX       9 /* Characters. %c */
18856 /* The rest are extensions, not normally found in printf() */
18857 #define etSQLESCAPE  10 /* Strings with '\'' doubled.  %q */
18858 #define etSQLESCAPE2 11 /* Strings with '\'' doubled and enclosed in '',
18859                           NULL pointers replaced by SQL NULL.  %Q */
18860 #define etTOKEN      12 /* a pointer to a Token structure */
18861 #define etSRCLIST    13 /* a pointer to a SrcList */
18862 #define etPOINTER    14 /* The %p conversion */
18863 #define etSQLESCAPE3 15 /* %w -> Strings with '\"' doubled */
18864 #define etORDINAL    16 /* %r -> 1st, 2nd, 3rd, 4th, etc.  English only */
18865 
18866 #define etINVALID     0 /* Any unrecognized conversion type */
18867 
18868 
18869 /*
18870 ** An "etByte" is an 8-bit unsigned value.
18871 */
18872 typedef unsigned char etByte;
18873 
18874 /*
18875 ** Each builtin conversion character (ex: the 'd' in "%d") is described
18876 ** by an instance of the following structure
18877 */
18878 typedef struct et_info {   /* Information about each format field */
18879   char fmttype;            /* The format field code letter */
18880   etByte base;             /* The base for radix conversion */
18881   etByte flags;            /* One or more of FLAG_ constants below */
18882   etByte type;             /* Conversion paradigm */
18883   etByte charset;          /* Offset into aDigits[] of the digits string */
18884   etByte prefix;           /* Offset into aPrefix[] of the prefix string */
18885 } et_info;
18886 
18887 /*
18888 ** Allowed values for et_info.flags
18889 */
18890 #define FLAG_SIGNED  1     /* True if the value to convert is signed */
18891 #define FLAG_INTERN  2     /* True if for internal use only */
18892 #define FLAG_STRING  4     /* Allow infinity precision */
18893 
18894 
18895 /*
18896 ** The following table is searched linearly, so it is good to put the
18897 ** most frequently used conversion types first.
18898 */
18899 static const char aDigits[] = "0123456789ABCDEF0123456789abcdef";
18900 static const char aPrefix[] = "-x0\000X0";
18901 static const et_info fmtinfo[] = {
18902   {  'd', 10, 1, etRADIX,      0,  0 },
18903   {  's',  0, 4, etSTRING,     0,  0 },
18904   {  'g',  0, 1, etGENERIC,    30, 0 },
18905   {  'z',  0, 4, etDYNSTRING,  0,  0 },
18906   {  'q',  0, 4, etSQLESCAPE,  0,  0 },
18907   {  'Q',  0, 4, etSQLESCAPE2, 0,  0 },
18908   {  'w',  0, 4, etSQLESCAPE3, 0,  0 },
18909   {  'c',  0, 0, etCHARX,      0,  0 },
18910   {  'o',  8, 0, etRADIX,      0,  2 },
18911   {  'u', 10, 0, etRADIX,      0,  0 },
18912   {  'x', 16, 0, etRADIX,      16, 1 },
18913   {  'X', 16, 0, etRADIX,      0,  4 },
18914 #ifndef SQLITE_OMIT_FLOATING_POINT
18915   {  'f',  0, 1, etFLOAT,      0,  0 },
18916   {  'e',  0, 1, etEXP,        30, 0 },
18917   {  'E',  0, 1, etEXP,        14, 0 },
18918   {  'G',  0, 1, etGENERIC,    14, 0 },
18919 #endif
18920   {  'i', 10, 1, etRADIX,      0,  0 },
18921   {  'n',  0, 0, etSIZE,       0,  0 },
18922   {  '%',  0, 0, etPERCENT,    0,  0 },
18923   {  'p', 16, 0, etPOINTER,    0,  1 },
18924 
18925 /* All the rest have the FLAG_INTERN bit set and are thus for internal
18926 ** use only */
18927   {  'T',  0, 2, etTOKEN,      0,  0 },
18928   {  'S',  0, 2, etSRCLIST,    0,  0 },
18929   {  'r', 10, 3, etORDINAL,    0,  0 },
18930 };
18931 
18932 /*
18933 ** If SQLITE_OMIT_FLOATING_POINT is defined, then none of the floating point
18934 ** conversions will work.
18935 */
18936 #ifndef SQLITE_OMIT_FLOATING_POINT
18937 /*
18938 ** "*val" is a double such that 0.1 <= *val < 10.0
18939 ** Return the ascii code for the leading digit of *val, then
18940 ** multiply "*val" by 10.0 to renormalize.
18941 **
18942 ** Example:
18943 **     input:     *val = 3.14159
18944 **     output:    *val = 1.4159    function return = '3'
18945 **
18946 ** The counter *cnt is incremented each time.  After counter exceeds
18947 ** 16 (the number of significant digits in a 64-bit float) '0' is
18948 ** always returned.
18949 */
et_getdigit(LONGDOUBLE_TYPE * val,int * cnt)18950 static char et_getdigit(LONGDOUBLE_TYPE *val, int *cnt){
18951   int digit;
18952   LONGDOUBLE_TYPE d;
18953   if( (*cnt)++ >= 16 ) return '0';
18954   digit = (int)*val;
18955   d = digit;
18956   digit += '0';
18957   *val = (*val - d)*10.0;
18958   return (char)digit;
18959 }
18960 #endif /* SQLITE_OMIT_FLOATING_POINT */
18961 
18962 /*
18963 ** Append N space characters to the given string buffer.
18964 */
appendSpace(StrAccum * pAccum,int N)18965 static void appendSpace(StrAccum *pAccum, int N){
18966   static const char zSpaces[] = "                             ";
18967   while( N>=(int)sizeof(zSpaces)-1 ){
18968     sqlite3StrAccumAppend(pAccum, zSpaces, sizeof(zSpaces)-1);
18969     N -= sizeof(zSpaces)-1;
18970   }
18971   if( N>0 ){
18972     sqlite3StrAccumAppend(pAccum, zSpaces, N);
18973   }
18974 }
18975 
18976 /*
18977 ** On machines with a small stack size, you can redefine the
18978 ** SQLITE_PRINT_BUF_SIZE to be less than 350.
18979 */
18980 #ifndef SQLITE_PRINT_BUF_SIZE
18981 # if defined(SQLITE_SMALL_STACK)
18982 #   define SQLITE_PRINT_BUF_SIZE 50
18983 # else
18984 #   define SQLITE_PRINT_BUF_SIZE 350
18985 # endif
18986 #endif
18987 #define etBUFSIZE SQLITE_PRINT_BUF_SIZE  /* Size of the output buffer */
18988 
18989 /*
18990 ** The root program.  All variations call this core.
18991 **
18992 ** INPUTS:
18993 **   func   This is a pointer to a function taking three arguments
18994 **            1. A pointer to anything.  Same as the "arg" parameter.
18995 **            2. A pointer to the list of characters to be output
18996 **               (Note, this list is NOT null terminated.)
18997 **            3. An integer number of characters to be output.
18998 **               (Note: This number might be zero.)
18999 **
19000 **   arg    This is the pointer to anything which will be passed as the
19001 **          first argument to "func".  Use it for whatever you like.
19002 **
19003 **   fmt    This is the format string, as in the usual print.
19004 **
19005 **   ap     This is a pointer to a list of arguments.  Same as in
19006 **          vfprint.
19007 **
19008 ** OUTPUTS:
19009 **          The return value is the total number of characters sent to
19010 **          the function "func".  Returns -1 on a error.
19011 **
19012 ** Note that the order in which automatic variables are declared below
19013 ** seems to make a big difference in determining how fast this beast
19014 ** will run.
19015 */
sqlite3VXPrintf(StrAccum * pAccum,int useExtended,const char * fmt,va_list ap)19016 SQLITE_PRIVATE void sqlite3VXPrintf(
19017   StrAccum *pAccum,                  /* Accumulate results here */
19018   int useExtended,                   /* Allow extended %-conversions */
19019   const char *fmt,                   /* Format string */
19020   va_list ap                         /* arguments */
19021 ){
19022   int c;                     /* Next character in the format string */
19023   char *bufpt;               /* Pointer to the conversion buffer */
19024   int precision;             /* Precision of the current field */
19025   int length;                /* Length of the field */
19026   int idx;                   /* A general purpose loop counter */
19027   int width;                 /* Width of the current field */
19028   etByte flag_leftjustify;   /* True if "-" flag is present */
19029   etByte flag_plussign;      /* True if "+" flag is present */
19030   etByte flag_blanksign;     /* True if " " flag is present */
19031   etByte flag_alternateform; /* True if "#" flag is present */
19032   etByte flag_altform2;      /* True if "!" flag is present */
19033   etByte flag_zeropad;       /* True if field width constant starts with zero */
19034   etByte flag_long;          /* True if "l" flag is present */
19035   etByte flag_longlong;      /* True if the "ll" flag is present */
19036   etByte done;               /* Loop termination flag */
19037   sqlite_uint64 longvalue;   /* Value for integer types */
19038   LONGDOUBLE_TYPE realvalue; /* Value for real types */
19039   const et_info *infop;      /* Pointer to the appropriate info structure */
19040   char buf[etBUFSIZE];       /* Conversion buffer */
19041   char prefix;               /* Prefix character.  "+" or "-" or " " or '\0'. */
19042   etByte xtype = 0;          /* Conversion paradigm */
19043   char *zExtra;              /* Extra memory used for etTCLESCAPE conversions */
19044 #ifndef SQLITE_OMIT_FLOATING_POINT
19045   int  exp, e2;              /* exponent of real numbers */
19046   double rounder;            /* Used for rounding floating point values */
19047   etByte flag_dp;            /* True if decimal point should be shown */
19048   etByte flag_rtz;           /* True if trailing zeros should be removed */
19049   etByte flag_exp;           /* True to force display of the exponent */
19050   int nsd;                   /* Number of significant digits returned */
19051 #endif
19052 
19053   length = 0;
19054   bufpt = 0;
19055   for(; (c=(*fmt))!=0; ++fmt){
19056     if( c!='%' ){
19057       int amt;
19058       bufpt = (char *)fmt;
19059       amt = 1;
19060       while( (c=(*++fmt))!='%' && c!=0 ) amt++;
19061       sqlite3StrAccumAppend(pAccum, bufpt, amt);
19062       if( c==0 ) break;
19063     }
19064     if( (c=(*++fmt))==0 ){
19065       sqlite3StrAccumAppend(pAccum, "%", 1);
19066       break;
19067     }
19068     /* Find out what flags are present */
19069     flag_leftjustify = flag_plussign = flag_blanksign =
19070      flag_alternateform = flag_altform2 = flag_zeropad = 0;
19071     done = 0;
19072     do{
19073       switch( c ){
19074         case '-':   flag_leftjustify = 1;     break;
19075         case '+':   flag_plussign = 1;        break;
19076         case ' ':   flag_blanksign = 1;       break;
19077         case '#':   flag_alternateform = 1;   break;
19078         case '!':   flag_altform2 = 1;        break;
19079         case '0':   flag_zeropad = 1;         break;
19080         default:    done = 1;                 break;
19081       }
19082     }while( !done && (c=(*++fmt))!=0 );
19083     /* Get the field width */
19084     width = 0;
19085     if( c=='*' ){
19086       width = va_arg(ap,int);
19087       if( width<0 ){
19088         flag_leftjustify = 1;
19089         width = -width;
19090       }
19091       c = *++fmt;
19092     }else{
19093       while( c>='0' && c<='9' ){
19094         width = width*10 + c - '0';
19095         c = *++fmt;
19096       }
19097     }
19098     if( width > etBUFSIZE-10 ){
19099       width = etBUFSIZE-10;
19100     }
19101     /* Get the precision */
19102     if( c=='.' ){
19103       precision = 0;
19104       c = *++fmt;
19105       if( c=='*' ){
19106         precision = va_arg(ap,int);
19107         if( precision<0 ) precision = -precision;
19108         c = *++fmt;
19109       }else{
19110         while( c>='0' && c<='9' ){
19111           precision = precision*10 + c - '0';
19112           c = *++fmt;
19113         }
19114       }
19115     }else{
19116       precision = -1;
19117     }
19118     /* Get the conversion type modifier */
19119     if( c=='l' ){
19120       flag_long = 1;
19121       c = *++fmt;
19122       if( c=='l' ){
19123         flag_longlong = 1;
19124         c = *++fmt;
19125       }else{
19126         flag_longlong = 0;
19127       }
19128     }else{
19129       flag_long = flag_longlong = 0;
19130     }
19131     /* Fetch the info entry for the field */
19132     infop = &fmtinfo[0];
19133     xtype = etINVALID;
19134     for(idx=0; idx<ArraySize(fmtinfo); idx++){
19135       if( c==fmtinfo[idx].fmttype ){
19136         infop = &fmtinfo[idx];
19137         if( useExtended || (infop->flags & FLAG_INTERN)==0 ){
19138           xtype = infop->type;
19139         }else{
19140           return;
19141         }
19142         break;
19143       }
19144     }
19145     zExtra = 0;
19146 
19147 
19148     /* Limit the precision to prevent overflowing buf[] during conversion */
19149     if( precision>etBUFSIZE-40 && (infop->flags & FLAG_STRING)==0 ){
19150       precision = etBUFSIZE-40;
19151     }
19152 
19153     /*
19154     ** At this point, variables are initialized as follows:
19155     **
19156     **   flag_alternateform          TRUE if a '#' is present.
19157     **   flag_altform2               TRUE if a '!' is present.
19158     **   flag_plussign               TRUE if a '+' is present.
19159     **   flag_leftjustify            TRUE if a '-' is present or if the
19160     **                               field width was negative.
19161     **   flag_zeropad                TRUE if the width began with 0.
19162     **   flag_long                   TRUE if the letter 'l' (ell) prefixed
19163     **                               the conversion character.
19164     **   flag_longlong               TRUE if the letter 'll' (ell ell) prefixed
19165     **                               the conversion character.
19166     **   flag_blanksign              TRUE if a ' ' is present.
19167     **   width                       The specified field width.  This is
19168     **                               always non-negative.  Zero is the default.
19169     **   precision                   The specified precision.  The default
19170     **                               is -1.
19171     **   xtype                       The class of the conversion.
19172     **   infop                       Pointer to the appropriate info struct.
19173     */
19174     switch( xtype ){
19175       case etPOINTER:
19176         flag_longlong = sizeof(char*)==sizeof(i64);
19177         flag_long = sizeof(char*)==sizeof(long int);
19178         /* Fall through into the next case */
19179       case etORDINAL:
19180       case etRADIX:
19181         if( infop->flags & FLAG_SIGNED ){
19182           i64 v;
19183           if( flag_longlong ){
19184             v = va_arg(ap,i64);
19185           }else if( flag_long ){
19186             v = va_arg(ap,long int);
19187           }else{
19188             v = va_arg(ap,int);
19189           }
19190           if( v<0 ){
19191             if( v==SMALLEST_INT64 ){
19192               longvalue = ((u64)1)<<63;
19193             }else{
19194               longvalue = -v;
19195             }
19196             prefix = '-';
19197           }else{
19198             longvalue = v;
19199             if( flag_plussign )        prefix = '+';
19200             else if( flag_blanksign )  prefix = ' ';
19201             else                       prefix = 0;
19202           }
19203         }else{
19204           if( flag_longlong ){
19205             longvalue = va_arg(ap,u64);
19206           }else if( flag_long ){
19207             longvalue = va_arg(ap,unsigned long int);
19208           }else{
19209             longvalue = va_arg(ap,unsigned int);
19210           }
19211           prefix = 0;
19212         }
19213         if( longvalue==0 ) flag_alternateform = 0;
19214         if( flag_zeropad && precision<width-(prefix!=0) ){
19215           precision = width-(prefix!=0);
19216         }
19217         bufpt = &buf[etBUFSIZE-1];
19218         if( xtype==etORDINAL ){
19219           static const char zOrd[] = "thstndrd";
19220           int x = (int)(longvalue % 10);
19221           if( x>=4 || (longvalue/10)%10==1 ){
19222             x = 0;
19223           }
19224           buf[etBUFSIZE-3] = zOrd[x*2];
19225           buf[etBUFSIZE-2] = zOrd[x*2+1];
19226           bufpt -= 2;
19227         }
19228         {
19229           register const char *cset;      /* Use registers for speed */
19230           register int base;
19231           cset = &aDigits[infop->charset];
19232           base = infop->base;
19233           do{                                           /* Convert to ascii */
19234             *(--bufpt) = cset[longvalue%base];
19235             longvalue = longvalue/base;
19236           }while( longvalue>0 );
19237         }
19238         length = (int)(&buf[etBUFSIZE-1]-bufpt);
19239         for(idx=precision-length; idx>0; idx--){
19240           *(--bufpt) = '0';                             /* Zero pad */
19241         }
19242         if( prefix ) *(--bufpt) = prefix;               /* Add sign */
19243         if( flag_alternateform && infop->prefix ){      /* Add "0" or "0x" */
19244           const char *pre;
19245           char x;
19246           pre = &aPrefix[infop->prefix];
19247           for(; (x=(*pre))!=0; pre++) *(--bufpt) = x;
19248         }
19249         length = (int)(&buf[etBUFSIZE-1]-bufpt);
19250         break;
19251       case etFLOAT:
19252       case etEXP:
19253       case etGENERIC:
19254         realvalue = va_arg(ap,double);
19255 #ifdef SQLITE_OMIT_FLOATING_POINT
19256         length = 0;
19257 #else
19258         if( precision<0 ) precision = 6;         /* Set default precision */
19259         if( precision>etBUFSIZE/2-10 ) precision = etBUFSIZE/2-10;
19260         if( realvalue<0.0 ){
19261           realvalue = -realvalue;
19262           prefix = '-';
19263         }else{
19264           if( flag_plussign )          prefix = '+';
19265           else if( flag_blanksign )    prefix = ' ';
19266           else                         prefix = 0;
19267         }
19268         if( xtype==etGENERIC && precision>0 ) precision--;
19269 #if 0
19270         /* Rounding works like BSD when the constant 0.4999 is used.  Wierd! */
19271         for(idx=precision, rounder=0.4999; idx>0; idx--, rounder*=0.1);
19272 #else
19273         /* It makes more sense to use 0.5 */
19274         for(idx=precision, rounder=0.5; idx>0; idx--, rounder*=0.1){}
19275 #endif
19276         if( xtype==etFLOAT ) realvalue += rounder;
19277         /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */
19278         exp = 0;
19279         if( sqlite3IsNaN((double)realvalue) ){
19280           bufpt = "NaN";
19281           length = 3;
19282           break;
19283         }
19284         if( realvalue>0.0 ){
19285           while( realvalue>=1e32 && exp<=350 ){ realvalue *= 1e-32; exp+=32; }
19286           while( realvalue>=1e8 && exp<=350 ){ realvalue *= 1e-8; exp+=8; }
19287           while( realvalue>=10.0 && exp<=350 ){ realvalue *= 0.1; exp++; }
19288           while( realvalue<1e-8 ){ realvalue *= 1e8; exp-=8; }
19289           while( realvalue<1.0 ){ realvalue *= 10.0; exp--; }
19290           if( exp>350 ){
19291             if( prefix=='-' ){
19292               bufpt = "-Inf";
19293             }else if( prefix=='+' ){
19294               bufpt = "+Inf";
19295             }else{
19296               bufpt = "Inf";
19297             }
19298             length = sqlite3Strlen30(bufpt);
19299             break;
19300           }
19301         }
19302         bufpt = buf;
19303         /*
19304         ** If the field type is etGENERIC, then convert to either etEXP
19305         ** or etFLOAT, as appropriate.
19306         */
19307         flag_exp = xtype==etEXP;
19308         if( xtype!=etFLOAT ){
19309           realvalue += rounder;
19310           if( realvalue>=10.0 ){ realvalue *= 0.1; exp++; }
19311         }
19312         if( xtype==etGENERIC ){
19313           flag_rtz = !flag_alternateform;
19314           if( exp<-4 || exp>precision ){
19315             xtype = etEXP;
19316           }else{
19317             precision = precision - exp;
19318             xtype = etFLOAT;
19319           }
19320         }else{
19321           flag_rtz = 0;
19322         }
19323         if( xtype==etEXP ){
19324           e2 = 0;
19325         }else{
19326           e2 = exp;
19327         }
19328         nsd = 0;
19329         flag_dp = (precision>0 ?1:0) | flag_alternateform | flag_altform2;
19330         /* The sign in front of the number */
19331         if( prefix ){
19332           *(bufpt++) = prefix;
19333         }
19334         /* Digits prior to the decimal point */
19335         if( e2<0 ){
19336           *(bufpt++) = '0';
19337         }else{
19338           for(; e2>=0; e2--){
19339             *(bufpt++) = et_getdigit(&realvalue,&nsd);
19340           }
19341         }
19342         /* The decimal point */
19343         if( flag_dp ){
19344           *(bufpt++) = '.';
19345         }
19346         /* "0" digits after the decimal point but before the first
19347         ** significant digit of the number */
19348         for(e2++; e2<0; precision--, e2++){
19349           assert( precision>0 );
19350           *(bufpt++) = '0';
19351         }
19352         /* Significant digits after the decimal point */
19353         while( (precision--)>0 ){
19354           *(bufpt++) = et_getdigit(&realvalue,&nsd);
19355         }
19356         /* Remove trailing zeros and the "." if no digits follow the "." */
19357         if( flag_rtz && flag_dp ){
19358           while( bufpt[-1]=='0' ) *(--bufpt) = 0;
19359           assert( bufpt>buf );
19360           if( bufpt[-1]=='.' ){
19361             if( flag_altform2 ){
19362               *(bufpt++) = '0';
19363             }else{
19364               *(--bufpt) = 0;
19365             }
19366           }
19367         }
19368         /* Add the "eNNN" suffix */
19369         if( flag_exp || xtype==etEXP ){
19370           *(bufpt++) = aDigits[infop->charset];
19371           if( exp<0 ){
19372             *(bufpt++) = '-'; exp = -exp;
19373           }else{
19374             *(bufpt++) = '+';
19375           }
19376           if( exp>=100 ){
19377             *(bufpt++) = (char)((exp/100)+'0');        /* 100's digit */
19378             exp %= 100;
19379           }
19380           *(bufpt++) = (char)(exp/10+'0');             /* 10's digit */
19381           *(bufpt++) = (char)(exp%10+'0');             /* 1's digit */
19382         }
19383         *bufpt = 0;
19384 
19385         /* The converted number is in buf[] and zero terminated. Output it.
19386         ** Note that the number is in the usual order, not reversed as with
19387         ** integer conversions. */
19388         length = (int)(bufpt-buf);
19389         bufpt = buf;
19390 
19391         /* Special case:  Add leading zeros if the flag_zeropad flag is
19392         ** set and we are not left justified */
19393         if( flag_zeropad && !flag_leftjustify && length < width){
19394           int i;
19395           int nPad = width - length;
19396           for(i=width; i>=nPad; i--){
19397             bufpt[i] = bufpt[i-nPad];
19398           }
19399           i = prefix!=0;
19400           while( nPad-- ) bufpt[i++] = '0';
19401           length = width;
19402         }
19403 #endif /* !defined(SQLITE_OMIT_FLOATING_POINT) */
19404         break;
19405       case etSIZE:
19406         *(va_arg(ap,int*)) = pAccum->nChar;
19407         length = width = 0;
19408         break;
19409       case etPERCENT:
19410         buf[0] = '%';
19411         bufpt = buf;
19412         length = 1;
19413         break;
19414       case etCHARX:
19415         c = va_arg(ap,int);
19416         buf[0] = (char)c;
19417         if( precision>=0 ){
19418           for(idx=1; idx<precision; idx++) buf[idx] = (char)c;
19419           length = precision;
19420         }else{
19421           length =1;
19422         }
19423         bufpt = buf;
19424         break;
19425       case etSTRING:
19426       case etDYNSTRING:
19427         bufpt = va_arg(ap,char*);
19428         if( bufpt==0 ){
19429           bufpt = "";
19430         }else if( xtype==etDYNSTRING ){
19431           zExtra = bufpt;
19432         }
19433         if( precision>=0 ){
19434           for(length=0; length<precision && bufpt[length]; length++){}
19435         }else{
19436           length = sqlite3Strlen30(bufpt);
19437         }
19438         break;
19439       case etSQLESCAPE:
19440       case etSQLESCAPE2:
19441       case etSQLESCAPE3: {
19442         int i, j, k, n, isnull;
19443         int needQuote;
19444         char ch;
19445         char q = ((xtype==etSQLESCAPE3)?'"':'\'');   /* Quote character */
19446         char *escarg = va_arg(ap,char*);
19447         isnull = escarg==0;
19448         if( isnull ) escarg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)");
19449         k = precision;
19450         for(i=n=0; k!=0 && (ch=escarg[i])!=0; i++, k--){
19451           if( ch==q )  n++;
19452         }
19453         needQuote = !isnull && xtype==etSQLESCAPE2;
19454         n += i + 1 + needQuote*2;
19455         if( n>etBUFSIZE ){
19456           bufpt = zExtra = sqlite3Malloc( n );
19457           if( bufpt==0 ){
19458             pAccum->mallocFailed = 1;
19459             return;
19460           }
19461         }else{
19462           bufpt = buf;
19463         }
19464         j = 0;
19465         if( needQuote ) bufpt[j++] = q;
19466         k = i;
19467         for(i=0; i<k; i++){
19468           bufpt[j++] = ch = escarg[i];
19469           if( ch==q ) bufpt[j++] = ch;
19470         }
19471         if( needQuote ) bufpt[j++] = q;
19472         bufpt[j] = 0;
19473         length = j;
19474         /* The precision in %q and %Q means how many input characters to
19475         ** consume, not the length of the output...
19476         ** if( precision>=0 && precision<length ) length = precision; */
19477         break;
19478       }
19479       case etTOKEN: {
19480         Token *pToken = va_arg(ap, Token*);
19481         if( pToken ){
19482           sqlite3StrAccumAppend(pAccum, (const char*)pToken->z, pToken->n);
19483         }
19484         length = width = 0;
19485         break;
19486       }
19487       case etSRCLIST: {
19488         SrcList *pSrc = va_arg(ap, SrcList*);
19489         int k = va_arg(ap, int);
19490         struct SrcList_item *pItem = &pSrc->a[k];
19491         assert( k>=0 && k<pSrc->nSrc );
19492         if( pItem->zDatabase ){
19493           sqlite3StrAccumAppend(pAccum, pItem->zDatabase, -1);
19494           sqlite3StrAccumAppend(pAccum, ".", 1);
19495         }
19496         sqlite3StrAccumAppend(pAccum, pItem->zName, -1);
19497         length = width = 0;
19498         break;
19499       }
19500       default: {
19501         assert( xtype==etINVALID );
19502         return;
19503       }
19504     }/* End switch over the format type */
19505     /*
19506     ** The text of the conversion is pointed to by "bufpt" and is
19507     ** "length" characters long.  The field width is "width".  Do
19508     ** the output.
19509     */
19510     if( !flag_leftjustify ){
19511       register int nspace;
19512       nspace = width-length;
19513       if( nspace>0 ){
19514         appendSpace(pAccum, nspace);
19515       }
19516     }
19517     if( length>0 ){
19518       sqlite3StrAccumAppend(pAccum, bufpt, length);
19519     }
19520     if( flag_leftjustify ){
19521       register int nspace;
19522       nspace = width-length;
19523       if( nspace>0 ){
19524         appendSpace(pAccum, nspace);
19525       }
19526     }
19527     if( zExtra ){
19528       sqlite3_free(zExtra);
19529     }
19530   }/* End for loop over the format string */
19531 } /* End of function */
19532 
19533 /*
19534 ** Append N bytes of text from z to the StrAccum object.
19535 */
sqlite3StrAccumAppend(StrAccum * p,const char * z,int N)19536 SQLITE_PRIVATE void sqlite3StrAccumAppend(StrAccum *p, const char *z, int N){
19537   assert( z!=0 || N==0 );
19538   if( p->tooBig | p->mallocFailed ){
19539     testcase(p->tooBig);
19540     testcase(p->mallocFailed);
19541     return;
19542   }
19543   if( N<0 ){
19544     N = sqlite3Strlen30(z);
19545   }
19546   if( N==0 || NEVER(z==0) ){
19547     return;
19548   }
19549   if( p->nChar+N >= p->nAlloc ){
19550     char *zNew;
19551     if( !p->useMalloc ){
19552       p->tooBig = 1;
19553       N = p->nAlloc - p->nChar - 1;
19554       if( N<=0 ){
19555         return;
19556       }
19557     }else{
19558       char *zOld = (p->zText==p->zBase ? 0 : p->zText);
19559       i64 szNew = p->nChar;
19560       szNew += N + 1;
19561       if( szNew > p->mxAlloc ){
19562         sqlite3StrAccumReset(p);
19563         p->tooBig = 1;
19564         return;
19565       }else{
19566         p->nAlloc = (int)szNew;
19567       }
19568       if( p->useMalloc==1 ){
19569         zNew = sqlite3DbRealloc(p->db, zOld, p->nAlloc);
19570       }else{
19571         zNew = sqlite3_realloc(zOld, p->nAlloc);
19572       }
19573       if( zNew ){
19574         if( zOld==0 ) memcpy(zNew, p->zText, p->nChar);
19575         p->zText = zNew;
19576       }else{
19577         p->mallocFailed = 1;
19578         sqlite3StrAccumReset(p);
19579         return;
19580       }
19581     }
19582   }
19583   memcpy(&p->zText[p->nChar], z, N);
19584   p->nChar += N;
19585 }
19586 
19587 /*
19588 ** Finish off a string by making sure it is zero-terminated.
19589 ** Return a pointer to the resulting string.  Return a NULL
19590 ** pointer if any kind of error was encountered.
19591 */
sqlite3StrAccumFinish(StrAccum * p)19592 SQLITE_PRIVATE char *sqlite3StrAccumFinish(StrAccum *p){
19593   if( p->zText ){
19594     p->zText[p->nChar] = 0;
19595     if( p->useMalloc && p->zText==p->zBase ){
19596       if( p->useMalloc==1 ){
19597         p->zText = sqlite3DbMallocRaw(p->db, p->nChar+1 );
19598       }else{
19599         p->zText = sqlite3_malloc(p->nChar+1);
19600       }
19601       if( p->zText ){
19602         memcpy(p->zText, p->zBase, p->nChar+1);
19603       }else{
19604         p->mallocFailed = 1;
19605       }
19606     }
19607   }
19608   return p->zText;
19609 }
19610 
19611 /*
19612 ** Reset an StrAccum string.  Reclaim all malloced memory.
19613 */
sqlite3StrAccumReset(StrAccum * p)19614 SQLITE_PRIVATE void sqlite3StrAccumReset(StrAccum *p){
19615   if( p->zText!=p->zBase ){
19616     if( p->useMalloc==1 ){
19617       sqlite3DbFree(p->db, p->zText);
19618     }else{
19619       sqlite3_free(p->zText);
19620     }
19621   }
19622   p->zText = 0;
19623 }
19624 
19625 /*
19626 ** Initialize a string accumulator
19627 */
sqlite3StrAccumInit(StrAccum * p,char * zBase,int n,int mx)19628 SQLITE_PRIVATE void sqlite3StrAccumInit(StrAccum *p, char *zBase, int n, int mx){
19629   p->zText = p->zBase = zBase;
19630   p->db = 0;
19631   p->nChar = 0;
19632   p->nAlloc = n;
19633   p->mxAlloc = mx;
19634   p->useMalloc = 1;
19635   p->tooBig = 0;
19636   p->mallocFailed = 0;
19637 }
19638 
19639 /*
19640 ** Print into memory obtained from sqliteMalloc().  Use the internal
19641 ** %-conversion extensions.
19642 */
sqlite3VMPrintf(sqlite3 * db,const char * zFormat,va_list ap)19643 SQLITE_PRIVATE char *sqlite3VMPrintf(sqlite3 *db, const char *zFormat, va_list ap){
19644   char *z;
19645   char zBase[SQLITE_PRINT_BUF_SIZE];
19646   StrAccum acc;
19647   assert( db!=0 );
19648   sqlite3StrAccumInit(&acc, zBase, sizeof(zBase),
19649                       db->aLimit[SQLITE_LIMIT_LENGTH]);
19650   acc.db = db;
19651   sqlite3VXPrintf(&acc, 1, zFormat, ap);
19652   z = sqlite3StrAccumFinish(&acc);
19653   if( acc.mallocFailed ){
19654     db->mallocFailed = 1;
19655   }
19656   return z;
19657 }
19658 
19659 /*
19660 ** Print into memory obtained from sqliteMalloc().  Use the internal
19661 ** %-conversion extensions.
19662 */
sqlite3MPrintf(sqlite3 * db,const char * zFormat,...)19663 SQLITE_PRIVATE char *sqlite3MPrintf(sqlite3 *db, const char *zFormat, ...){
19664   va_list ap;
19665   char *z;
19666   va_start(ap, zFormat);
19667   z = sqlite3VMPrintf(db, zFormat, ap);
19668   va_end(ap);
19669   return z;
19670 }
19671 
19672 /*
19673 ** Like sqlite3MPrintf(), but call sqlite3DbFree() on zStr after formatting
19674 ** the string and before returnning.  This routine is intended to be used
19675 ** to modify an existing string.  For example:
19676 **
19677 **       x = sqlite3MPrintf(db, x, "prefix %s suffix", x);
19678 **
19679 */
sqlite3MAppendf(sqlite3 * db,char * zStr,const char * zFormat,...)19680 SQLITE_PRIVATE char *sqlite3MAppendf(sqlite3 *db, char *zStr, const char *zFormat, ...){
19681   va_list ap;
19682   char *z;
19683   va_start(ap, zFormat);
19684   z = sqlite3VMPrintf(db, zFormat, ap);
19685   va_end(ap);
19686   sqlite3DbFree(db, zStr);
19687   return z;
19688 }
19689 
19690 /*
19691 ** Print into memory obtained from sqlite3_malloc().  Omit the internal
19692 ** %-conversion extensions.
19693 */
sqlite3_vmprintf(const char * zFormat,va_list ap)19694 SQLITE_API char *sqlite3_vmprintf(const char *zFormat, va_list ap){
19695   char *z;
19696   char zBase[SQLITE_PRINT_BUF_SIZE];
19697   StrAccum acc;
19698 #ifndef SQLITE_OMIT_AUTOINIT
19699   if( sqlite3_initialize() ) return 0;
19700 #endif
19701   sqlite3StrAccumInit(&acc, zBase, sizeof(zBase), SQLITE_MAX_LENGTH);
19702   acc.useMalloc = 2;
19703   sqlite3VXPrintf(&acc, 0, zFormat, ap);
19704   z = sqlite3StrAccumFinish(&acc);
19705   return z;
19706 }
19707 
19708 /*
19709 ** Print into memory obtained from sqlite3_malloc()().  Omit the internal
19710 ** %-conversion extensions.
19711 */
sqlite3_mprintf(const char * zFormat,...)19712 SQLITE_API char *sqlite3_mprintf(const char *zFormat, ...){
19713   va_list ap;
19714   char *z;
19715 #ifndef SQLITE_OMIT_AUTOINIT
19716   if( sqlite3_initialize() ) return 0;
19717 #endif
19718   va_start(ap, zFormat);
19719   z = sqlite3_vmprintf(zFormat, ap);
19720   va_end(ap);
19721   return z;
19722 }
19723 
19724 /*
19725 ** sqlite3_snprintf() works like snprintf() except that it ignores the
19726 ** current locale settings.  This is important for SQLite because we
19727 ** are not able to use a "," as the decimal point in place of "." as
19728 ** specified by some locales.
19729 **
19730 ** Oops:  The first two arguments of sqlite3_snprintf() are backwards
19731 ** from the snprintf() standard.  Unfortunately, it is too late to change
19732 ** this without breaking compatibility, so we just have to live with the
19733 ** mistake.
19734 **
19735 ** sqlite3_vsnprintf() is the varargs version.
19736 */
sqlite3_vsnprintf(int n,char * zBuf,const char * zFormat,va_list ap)19737 SQLITE_API char *sqlite3_vsnprintf(int n, char *zBuf, const char *zFormat, va_list ap){
19738   StrAccum acc;
19739   if( n<=0 ) return zBuf;
19740   sqlite3StrAccumInit(&acc, zBuf, n, 0);
19741   acc.useMalloc = 0;
19742   sqlite3VXPrintf(&acc, 0, zFormat, ap);
19743   return sqlite3StrAccumFinish(&acc);
19744 }
sqlite3_snprintf(int n,char * zBuf,const char * zFormat,...)19745 SQLITE_API char *sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){
19746   char *z;
19747   va_list ap;
19748   va_start(ap,zFormat);
19749   z = sqlite3_vsnprintf(n, zBuf, zFormat, ap);
19750   va_end(ap);
19751   return z;
19752 }
19753 
19754 /*
19755 ** This is the routine that actually formats the sqlite3_log() message.
19756 ** We house it in a separate routine from sqlite3_log() to avoid using
19757 ** stack space on small-stack systems when logging is disabled.
19758 **
19759 ** sqlite3_log() must render into a static buffer.  It cannot dynamically
19760 ** allocate memory because it might be called while the memory allocator
19761 ** mutex is held.
19762 */
renderLogMsg(int iErrCode,const char * zFormat,va_list ap)19763 static void renderLogMsg(int iErrCode, const char *zFormat, va_list ap){
19764   StrAccum acc;                          /* String accumulator */
19765   char zMsg[SQLITE_PRINT_BUF_SIZE*3];    /* Complete log message */
19766 
19767   sqlite3StrAccumInit(&acc, zMsg, sizeof(zMsg), 0);
19768   acc.useMalloc = 0;
19769   sqlite3VXPrintf(&acc, 0, zFormat, ap);
19770   sqlite3GlobalConfig.xLog(sqlite3GlobalConfig.pLogArg, iErrCode,
19771                            sqlite3StrAccumFinish(&acc));
19772 }
19773 
19774 /*
19775 ** Format and write a message to the log if logging is enabled.
19776 */
sqlite3_log(int iErrCode,const char * zFormat,...)19777 SQLITE_API void sqlite3_log(int iErrCode, const char *zFormat, ...){
19778   va_list ap;                             /* Vararg list */
19779   if( sqlite3GlobalConfig.xLog ){
19780     va_start(ap, zFormat);
19781     renderLogMsg(iErrCode, zFormat, ap);
19782     va_end(ap);
19783   }
19784 }
19785 
19786 #if defined(SQLITE_DEBUG)
19787 /*
19788 ** A version of printf() that understands %lld.  Used for debugging.
19789 ** The printf() built into some versions of windows does not understand %lld
19790 ** and segfaults if you give it a long long int.
19791 */
sqlite3DebugPrintf(const char * zFormat,...)19792 SQLITE_PRIVATE void sqlite3DebugPrintf(const char *zFormat, ...){
19793   va_list ap;
19794   StrAccum acc;
19795   char zBuf[500];
19796   sqlite3StrAccumInit(&acc, zBuf, sizeof(zBuf), 0);
19797   acc.useMalloc = 0;
19798   va_start(ap,zFormat);
19799   sqlite3VXPrintf(&acc, 0, zFormat, ap);
19800   va_end(ap);
19801   sqlite3StrAccumFinish(&acc);
19802   fprintf(stdout,"%s", zBuf);
19803   fflush(stdout);
19804 }
19805 #endif
19806 
19807 #ifndef SQLITE_OMIT_TRACE
19808 /*
19809 ** variable-argument wrapper around sqlite3VXPrintf().
19810 */
sqlite3XPrintf(StrAccum * p,const char * zFormat,...)19811 SQLITE_PRIVATE void sqlite3XPrintf(StrAccum *p, const char *zFormat, ...){
19812   va_list ap;
19813   va_start(ap,zFormat);
19814   sqlite3VXPrintf(p, 1, zFormat, ap);
19815   va_end(ap);
19816 }
19817 #endif
19818 
19819 /************** End of printf.c **********************************************/
19820 /************** Begin file random.c ******************************************/
19821 /*
19822 ** 2001 September 15
19823 **
19824 ** The author disclaims copyright to this source code.  In place of
19825 ** a legal notice, here is a blessing:
19826 **
19827 **    May you do good and not evil.
19828 **    May you find forgiveness for yourself and forgive others.
19829 **    May you share freely, never taking more than you give.
19830 **
19831 *************************************************************************
19832 ** This file contains code to implement a pseudo-random number
19833 ** generator (PRNG) for SQLite.
19834 **
19835 ** Random numbers are used by some of the database backends in order
19836 ** to generate random integer keys for tables or random filenames.
19837 */
19838 
19839 
19840 /* All threads share a single random number generator.
19841 ** This structure is the current state of the generator.
19842 */
19843 static SQLITE_WSD struct sqlite3PrngType {
19844   unsigned char isInit;          /* True if initialized */
19845   unsigned char i, j;            /* State variables */
19846   unsigned char s[256];          /* State variables */
19847 } sqlite3Prng;
19848 
19849 /*
19850 ** Get a single 8-bit random value from the RC4 PRNG.  The Mutex
19851 ** must be held while executing this routine.
19852 **
19853 ** Why not just use a library random generator like lrand48() for this?
19854 ** Because the OP_NewRowid opcode in the VDBE depends on having a very
19855 ** good source of random numbers.  The lrand48() library function may
19856 ** well be good enough.  But maybe not.  Or maybe lrand48() has some
19857 ** subtle problems on some systems that could cause problems.  It is hard
19858 ** to know.  To minimize the risk of problems due to bad lrand48()
19859 ** implementations, SQLite uses this random number generator based
19860 ** on RC4, which we know works very well.
19861 **
19862 ** (Later):  Actually, OP_NewRowid does not depend on a good source of
19863 ** randomness any more.  But we will leave this code in all the same.
19864 */
randomByte(void)19865 static u8 randomByte(void){
19866   unsigned char t;
19867 
19868 
19869   /* The "wsdPrng" macro will resolve to the pseudo-random number generator
19870   ** state vector.  If writable static data is unsupported on the target,
19871   ** we have to locate the state vector at run-time.  In the more common
19872   ** case where writable static data is supported, wsdPrng can refer directly
19873   ** to the "sqlite3Prng" state vector declared above.
19874   */
19875 #ifdef SQLITE_OMIT_WSD
19876   struct sqlite3PrngType *p = &GLOBAL(struct sqlite3PrngType, sqlite3Prng);
19877 # define wsdPrng p[0]
19878 #else
19879 # define wsdPrng sqlite3Prng
19880 #endif
19881 
19882 
19883   /* Initialize the state of the random number generator once,
19884   ** the first time this routine is called.  The seed value does
19885   ** not need to contain a lot of randomness since we are not
19886   ** trying to do secure encryption or anything like that...
19887   **
19888   ** Nothing in this file or anywhere else in SQLite does any kind of
19889   ** encryption.  The RC4 algorithm is being used as a PRNG (pseudo-random
19890   ** number generator) not as an encryption device.
19891   */
19892   if( !wsdPrng.isInit ){
19893     int i;
19894     char k[256];
19895     wsdPrng.j = 0;
19896     wsdPrng.i = 0;
19897     sqlite3OsRandomness(sqlite3_vfs_find(0), 256, k);
19898     for(i=0; i<256; i++){
19899       wsdPrng.s[i] = (u8)i;
19900     }
19901     for(i=0; i<256; i++){
19902       wsdPrng.j += wsdPrng.s[i] + k[i];
19903       t = wsdPrng.s[wsdPrng.j];
19904       wsdPrng.s[wsdPrng.j] = wsdPrng.s[i];
19905       wsdPrng.s[i] = t;
19906     }
19907     wsdPrng.isInit = 1;
19908   }
19909 
19910   /* Generate and return single random byte
19911   */
19912   wsdPrng.i++;
19913   t = wsdPrng.s[wsdPrng.i];
19914   wsdPrng.j += t;
19915   wsdPrng.s[wsdPrng.i] = wsdPrng.s[wsdPrng.j];
19916   wsdPrng.s[wsdPrng.j] = t;
19917   t += wsdPrng.s[wsdPrng.i];
19918   return wsdPrng.s[t];
19919 }
19920 
19921 /*
19922 ** Return N random bytes.
19923 */
sqlite3_randomness(int N,void * pBuf)19924 SQLITE_API void sqlite3_randomness(int N, void *pBuf){
19925   unsigned char *zBuf = pBuf;
19926 #if SQLITE_THREADSAFE
19927   sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_PRNG);
19928 #endif
19929   sqlite3_mutex_enter(mutex);
19930   while( N-- ){
19931     *(zBuf++) = randomByte();
19932   }
19933   sqlite3_mutex_leave(mutex);
19934 }
19935 
19936 #ifndef SQLITE_OMIT_BUILTIN_TEST
19937 /*
19938 ** For testing purposes, we sometimes want to preserve the state of
19939 ** PRNG and restore the PRNG to its saved state at a later time, or
19940 ** to reset the PRNG to its initial state.  These routines accomplish
19941 ** those tasks.
19942 **
19943 ** The sqlite3_test_control() interface calls these routines to
19944 ** control the PRNG.
19945 */
19946 static SQLITE_WSD struct sqlite3PrngType sqlite3SavedPrng;
sqlite3PrngSaveState(void)19947 SQLITE_PRIVATE void sqlite3PrngSaveState(void){
19948   memcpy(
19949     &GLOBAL(struct sqlite3PrngType, sqlite3SavedPrng),
19950     &GLOBAL(struct sqlite3PrngType, sqlite3Prng),
19951     sizeof(sqlite3Prng)
19952   );
19953 }
sqlite3PrngRestoreState(void)19954 SQLITE_PRIVATE void sqlite3PrngRestoreState(void){
19955   memcpy(
19956     &GLOBAL(struct sqlite3PrngType, sqlite3Prng),
19957     &GLOBAL(struct sqlite3PrngType, sqlite3SavedPrng),
19958     sizeof(sqlite3Prng)
19959   );
19960 }
sqlite3PrngResetState(void)19961 SQLITE_PRIVATE void sqlite3PrngResetState(void){
19962   GLOBAL(struct sqlite3PrngType, sqlite3Prng).isInit = 0;
19963 }
19964 #endif /* SQLITE_OMIT_BUILTIN_TEST */
19965 
19966 /************** End of random.c **********************************************/
19967 /************** Begin file utf.c *********************************************/
19968 /*
19969 ** 2004 April 13
19970 **
19971 ** The author disclaims copyright to this source code.  In place of
19972 ** a legal notice, here is a blessing:
19973 **
19974 **    May you do good and not evil.
19975 **    May you find forgiveness for yourself and forgive others.
19976 **    May you share freely, never taking more than you give.
19977 **
19978 *************************************************************************
19979 ** This file contains routines used to translate between UTF-8,
19980 ** UTF-16, UTF-16BE, and UTF-16LE.
19981 **
19982 ** Notes on UTF-8:
19983 **
19984 **   Byte-0    Byte-1    Byte-2    Byte-3    Value
19985 **  0xxxxxxx                                 00000000 00000000 0xxxxxxx
19986 **  110yyyyy  10xxxxxx                       00000000 00000yyy yyxxxxxx
19987 **  1110zzzz  10yyyyyy  10xxxxxx             00000000 zzzzyyyy yyxxxxxx
19988 **  11110uuu  10uuzzzz  10yyyyyy  10xxxxxx   000uuuuu zzzzyyyy yyxxxxxx
19989 **
19990 **
19991 ** Notes on UTF-16:  (with wwww+1==uuuuu)
19992 **
19993 **      Word-0               Word-1          Value
19994 **  110110ww wwzzzzyy   110111yy yyxxxxxx    000uuuuu zzzzyyyy yyxxxxxx
19995 **  zzzzyyyy yyxxxxxx                        00000000 zzzzyyyy yyxxxxxx
19996 **
19997 **
19998 ** BOM or Byte Order Mark:
19999 **     0xff 0xfe   little-endian utf-16 follows
20000 **     0xfe 0xff   big-endian utf-16 follows
20001 **
20002 */
20003 
20004 #ifndef SQLITE_AMALGAMATION
20005 /*
20006 ** The following constant value is used by the SQLITE_BIGENDIAN and
20007 ** SQLITE_LITTLEENDIAN macros.
20008 */
20009 SQLITE_PRIVATE const int sqlite3one = 1;
20010 #endif /* SQLITE_AMALGAMATION */
20011 
20012 /*
20013 ** This lookup table is used to help decode the first byte of
20014 ** a multi-byte UTF8 character.
20015 */
20016 static const unsigned char sqlite3Utf8Trans1[] = {
20017   0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
20018   0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
20019   0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
20020   0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
20021   0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
20022   0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
20023   0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
20024   0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x00, 0x00,
20025 };
20026 
20027 
20028 #define WRITE_UTF8(zOut, c) {                          \
20029   if( c<0x00080 ){                                     \
20030     *zOut++ = (u8)(c&0xFF);                            \
20031   }                                                    \
20032   else if( c<0x00800 ){                                \
20033     *zOut++ = 0xC0 + (u8)((c>>6)&0x1F);                \
20034     *zOut++ = 0x80 + (u8)(c & 0x3F);                   \
20035   }                                                    \
20036   else if( c<0x10000 ){                                \
20037     *zOut++ = 0xE0 + (u8)((c>>12)&0x0F);               \
20038     *zOut++ = 0x80 + (u8)((c>>6) & 0x3F);              \
20039     *zOut++ = 0x80 + (u8)(c & 0x3F);                   \
20040   }else{                                               \
20041     *zOut++ = 0xF0 + (u8)((c>>18) & 0x07);             \
20042     *zOut++ = 0x80 + (u8)((c>>12) & 0x3F);             \
20043     *zOut++ = 0x80 + (u8)((c>>6) & 0x3F);              \
20044     *zOut++ = 0x80 + (u8)(c & 0x3F);                   \
20045   }                                                    \
20046 }
20047 
20048 #define WRITE_UTF16LE(zOut, c) {                                    \
20049   if( c<=0xFFFF ){                                                  \
20050     *zOut++ = (u8)(c&0x00FF);                                       \
20051     *zOut++ = (u8)((c>>8)&0x00FF);                                  \
20052   }else{                                                            \
20053     *zOut++ = (u8)(((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0));  \
20054     *zOut++ = (u8)(0x00D8 + (((c-0x10000)>>18)&0x03));              \
20055     *zOut++ = (u8)(c&0x00FF);                                       \
20056     *zOut++ = (u8)(0x00DC + ((c>>8)&0x03));                         \
20057   }                                                                 \
20058 }
20059 
20060 #define WRITE_UTF16BE(zOut, c) {                                    \
20061   if( c<=0xFFFF ){                                                  \
20062     *zOut++ = (u8)((c>>8)&0x00FF);                                  \
20063     *zOut++ = (u8)(c&0x00FF);                                       \
20064   }else{                                                            \
20065     *zOut++ = (u8)(0x00D8 + (((c-0x10000)>>18)&0x03));              \
20066     *zOut++ = (u8)(((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0));  \
20067     *zOut++ = (u8)(0x00DC + ((c>>8)&0x03));                         \
20068     *zOut++ = (u8)(c&0x00FF);                                       \
20069   }                                                                 \
20070 }
20071 
20072 #define READ_UTF16LE(zIn, TERM, c){                                   \
20073   c = (*zIn++);                                                       \
20074   c += ((*zIn++)<<8);                                                 \
20075   if( c>=0xD800 && c<0xE000 && TERM ){                                \
20076     int c2 = (*zIn++);                                                \
20077     c2 += ((*zIn++)<<8);                                              \
20078     c = (c2&0x03FF) + ((c&0x003F)<<10) + (((c&0x03C0)+0x0040)<<10);   \
20079   }                                                                   \
20080 }
20081 
20082 #define READ_UTF16BE(zIn, TERM, c){                                   \
20083   c = ((*zIn++)<<8);                                                  \
20084   c += (*zIn++);                                                      \
20085   if( c>=0xD800 && c<0xE000 && TERM ){                                \
20086     int c2 = ((*zIn++)<<8);                                           \
20087     c2 += (*zIn++);                                                   \
20088     c = (c2&0x03FF) + ((c&0x003F)<<10) + (((c&0x03C0)+0x0040)<<10);   \
20089   }                                                                   \
20090 }
20091 
20092 /*
20093 ** Translate a single UTF-8 character.  Return the unicode value.
20094 **
20095 ** During translation, assume that the byte that zTerm points
20096 ** is a 0x00.
20097 **
20098 ** Write a pointer to the next unread byte back into *pzNext.
20099 **
20100 ** Notes On Invalid UTF-8:
20101 **
20102 **  *  This routine never allows a 7-bit character (0x00 through 0x7f) to
20103 **     be encoded as a multi-byte character.  Any multi-byte character that
20104 **     attempts to encode a value between 0x00 and 0x7f is rendered as 0xfffd.
20105 **
20106 **  *  This routine never allows a UTF16 surrogate value to be encoded.
20107 **     If a multi-byte character attempts to encode a value between
20108 **     0xd800 and 0xe000 then it is rendered as 0xfffd.
20109 **
20110 **  *  Bytes in the range of 0x80 through 0xbf which occur as the first
20111 **     byte of a character are interpreted as single-byte characters
20112 **     and rendered as themselves even though they are technically
20113 **     invalid characters.
20114 **
20115 **  *  This routine accepts an infinite number of different UTF8 encodings
20116 **     for unicode values 0x80 and greater.  It do not change over-length
20117 **     encodings to 0xfffd as some systems recommend.
20118 */
20119 #define READ_UTF8(zIn, zTerm, c)                           \
20120   c = *(zIn++);                                            \
20121   if( c>=0xc0 ){                                           \
20122     c = sqlite3Utf8Trans1[c-0xc0];                         \
20123     while( zIn!=zTerm && (*zIn & 0xc0)==0x80 ){            \
20124       c = (c<<6) + (0x3f & *(zIn++));                      \
20125     }                                                      \
20126     if( c<0x80                                             \
20127         || (c&0xFFFFF800)==0xD800                          \
20128         || (c&0xFFFFFFFE)==0xFFFE ){  c = 0xFFFD; }        \
20129   }
sqlite3Utf8Read(const unsigned char * zIn,const unsigned char ** pzNext)20130 SQLITE_PRIVATE u32 sqlite3Utf8Read(
20131   const unsigned char *zIn,       /* First byte of UTF-8 character */
20132   const unsigned char **pzNext    /* Write first byte past UTF-8 char here */
20133 ){
20134   unsigned int c;
20135 
20136   /* Same as READ_UTF8() above but without the zTerm parameter.
20137   ** For this routine, we assume the UTF8 string is always zero-terminated.
20138   */
20139   c = *(zIn++);
20140   if( c>=0xc0 ){
20141     c = sqlite3Utf8Trans1[c-0xc0];
20142     while( (*zIn & 0xc0)==0x80 ){
20143       c = (c<<6) + (0x3f & *(zIn++));
20144     }
20145     if( c<0x80
20146         || (c&0xFFFFF800)==0xD800
20147         || (c&0xFFFFFFFE)==0xFFFE ){  c = 0xFFFD; }
20148   }
20149   *pzNext = zIn;
20150   return c;
20151 }
20152 
20153 
20154 
20155 
20156 /*
20157 ** If the TRANSLATE_TRACE macro is defined, the value of each Mem is
20158 ** printed on stderr on the way into and out of sqlite3VdbeMemTranslate().
20159 */
20160 /* #define TRANSLATE_TRACE 1 */
20161 
20162 #ifndef SQLITE_OMIT_UTF16
20163 /*
20164 ** This routine transforms the internal text encoding used by pMem to
20165 ** desiredEnc. It is an error if the string is already of the desired
20166 ** encoding, or if *pMem does not contain a string value.
20167 */
sqlite3VdbeMemTranslate(Mem * pMem,u8 desiredEnc)20168 SQLITE_PRIVATE int sqlite3VdbeMemTranslate(Mem *pMem, u8 desiredEnc){
20169   int len;                    /* Maximum length of output string in bytes */
20170   unsigned char *zOut;                  /* Output buffer */
20171   unsigned char *zIn;                   /* Input iterator */
20172   unsigned char *zTerm;                 /* End of input */
20173   unsigned char *z;                     /* Output iterator */
20174   unsigned int c;
20175 
20176   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
20177   assert( pMem->flags&MEM_Str );
20178   assert( pMem->enc!=desiredEnc );
20179   assert( pMem->enc!=0 );
20180   assert( pMem->n>=0 );
20181 
20182 #if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG)
20183   {
20184     char zBuf[100];
20185     sqlite3VdbeMemPrettyPrint(pMem, zBuf);
20186     fprintf(stderr, "INPUT:  %s\n", zBuf);
20187   }
20188 #endif
20189 
20190   /* If the translation is between UTF-16 little and big endian, then
20191   ** all that is required is to swap the byte order. This case is handled
20192   ** differently from the others.
20193   */
20194   if( pMem->enc!=SQLITE_UTF8 && desiredEnc!=SQLITE_UTF8 ){
20195     u8 temp;
20196     int rc;
20197     rc = sqlite3VdbeMemMakeWriteable(pMem);
20198     if( rc!=SQLITE_OK ){
20199       assert( rc==SQLITE_NOMEM );
20200       return SQLITE_NOMEM;
20201     }
20202     zIn = (u8*)pMem->z;
20203     zTerm = &zIn[pMem->n&~1];
20204     while( zIn<zTerm ){
20205       temp = *zIn;
20206       *zIn = *(zIn+1);
20207       zIn++;
20208       *zIn++ = temp;
20209     }
20210     pMem->enc = desiredEnc;
20211     goto translate_out;
20212   }
20213 
20214   /* Set len to the maximum number of bytes required in the output buffer. */
20215   if( desiredEnc==SQLITE_UTF8 ){
20216     /* When converting from UTF-16, the maximum growth results from
20217     ** translating a 2-byte character to a 4-byte UTF-8 character.
20218     ** A single byte is required for the output string
20219     ** nul-terminator.
20220     */
20221     pMem->n &= ~1;
20222     len = pMem->n * 2 + 1;
20223   }else{
20224     /* When converting from UTF-8 to UTF-16 the maximum growth is caused
20225     ** when a 1-byte UTF-8 character is translated into a 2-byte UTF-16
20226     ** character. Two bytes are required in the output buffer for the
20227     ** nul-terminator.
20228     */
20229     len = pMem->n * 2 + 2;
20230   }
20231 
20232   /* Set zIn to point at the start of the input buffer and zTerm to point 1
20233   ** byte past the end.
20234   **
20235   ** Variable zOut is set to point at the output buffer, space obtained
20236   ** from sqlite3_malloc().
20237   */
20238   zIn = (u8*)pMem->z;
20239   zTerm = &zIn[pMem->n];
20240   zOut = sqlite3DbMallocRaw(pMem->db, len);
20241   if( !zOut ){
20242     return SQLITE_NOMEM;
20243   }
20244   z = zOut;
20245 
20246   if( pMem->enc==SQLITE_UTF8 ){
20247     if( desiredEnc==SQLITE_UTF16LE ){
20248       /* UTF-8 -> UTF-16 Little-endian */
20249       while( zIn<zTerm ){
20250         /* c = sqlite3Utf8Read(zIn, zTerm, (const u8**)&zIn); */
20251         READ_UTF8(zIn, zTerm, c);
20252         WRITE_UTF16LE(z, c);
20253       }
20254     }else{
20255       assert( desiredEnc==SQLITE_UTF16BE );
20256       /* UTF-8 -> UTF-16 Big-endian */
20257       while( zIn<zTerm ){
20258         /* c = sqlite3Utf8Read(zIn, zTerm, (const u8**)&zIn); */
20259         READ_UTF8(zIn, zTerm, c);
20260         WRITE_UTF16BE(z, c);
20261       }
20262     }
20263     pMem->n = (int)(z - zOut);
20264     *z++ = 0;
20265   }else{
20266     assert( desiredEnc==SQLITE_UTF8 );
20267     if( pMem->enc==SQLITE_UTF16LE ){
20268       /* UTF-16 Little-endian -> UTF-8 */
20269       while( zIn<zTerm ){
20270         READ_UTF16LE(zIn, zIn<zTerm, c);
20271         WRITE_UTF8(z, c);
20272       }
20273     }else{
20274       /* UTF-16 Big-endian -> UTF-8 */
20275       while( zIn<zTerm ){
20276         READ_UTF16BE(zIn, zIn<zTerm, c);
20277         WRITE_UTF8(z, c);
20278       }
20279     }
20280     pMem->n = (int)(z - zOut);
20281   }
20282   *z = 0;
20283   assert( (pMem->n+(desiredEnc==SQLITE_UTF8?1:2))<=len );
20284 
20285   sqlite3VdbeMemRelease(pMem);
20286   pMem->flags &= ~(MEM_Static|MEM_Dyn|MEM_Ephem);
20287   pMem->enc = desiredEnc;
20288   pMem->flags |= (MEM_Term|MEM_Dyn);
20289   pMem->z = (char*)zOut;
20290   pMem->zMalloc = pMem->z;
20291 
20292 translate_out:
20293 #if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG)
20294   {
20295     char zBuf[100];
20296     sqlite3VdbeMemPrettyPrint(pMem, zBuf);
20297     fprintf(stderr, "OUTPUT: %s\n", zBuf);
20298   }
20299 #endif
20300   return SQLITE_OK;
20301 }
20302 
20303 /*
20304 ** This routine checks for a byte-order mark at the beginning of the
20305 ** UTF-16 string stored in *pMem. If one is present, it is removed and
20306 ** the encoding of the Mem adjusted. This routine does not do any
20307 ** byte-swapping, it just sets Mem.enc appropriately.
20308 **
20309 ** The allocation (static, dynamic etc.) and encoding of the Mem may be
20310 ** changed by this function.
20311 */
sqlite3VdbeMemHandleBom(Mem * pMem)20312 SQLITE_PRIVATE int sqlite3VdbeMemHandleBom(Mem *pMem){
20313   int rc = SQLITE_OK;
20314   u8 bom = 0;
20315 
20316   assert( pMem->n>=0 );
20317   if( pMem->n>1 ){
20318     u8 b1 = *(u8 *)pMem->z;
20319     u8 b2 = *(((u8 *)pMem->z) + 1);
20320     if( b1==0xFE && b2==0xFF ){
20321       bom = SQLITE_UTF16BE;
20322     }
20323     if( b1==0xFF && b2==0xFE ){
20324       bom = SQLITE_UTF16LE;
20325     }
20326   }
20327 
20328   if( bom ){
20329     rc = sqlite3VdbeMemMakeWriteable(pMem);
20330     if( rc==SQLITE_OK ){
20331       pMem->n -= 2;
20332       memmove(pMem->z, &pMem->z[2], pMem->n);
20333       pMem->z[pMem->n] = '\0';
20334       pMem->z[pMem->n+1] = '\0';
20335       pMem->flags |= MEM_Term;
20336       pMem->enc = bom;
20337     }
20338   }
20339   return rc;
20340 }
20341 #endif /* SQLITE_OMIT_UTF16 */
20342 
20343 /*
20344 ** pZ is a UTF-8 encoded unicode string. If nByte is less than zero,
20345 ** return the number of unicode characters in pZ up to (but not including)
20346 ** the first 0x00 byte. If nByte is not less than zero, return the
20347 ** number of unicode characters in the first nByte of pZ (or up to
20348 ** the first 0x00, whichever comes first).
20349 */
sqlite3Utf8CharLen(const char * zIn,int nByte)20350 SQLITE_PRIVATE int sqlite3Utf8CharLen(const char *zIn, int nByte){
20351   int r = 0;
20352   const u8 *z = (const u8*)zIn;
20353   const u8 *zTerm;
20354   if( nByte>=0 ){
20355     zTerm = &z[nByte];
20356   }else{
20357     zTerm = (const u8*)(-1);
20358   }
20359   assert( z<=zTerm );
20360   while( *z!=0 && z<zTerm ){
20361     SQLITE_SKIP_UTF8(z);
20362     r++;
20363   }
20364   return r;
20365 }
20366 
20367 /* This test function is not currently used by the automated test-suite.
20368 ** Hence it is only available in debug builds.
20369 */
20370 #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
20371 /*
20372 ** Translate UTF-8 to UTF-8.
20373 **
20374 ** This has the effect of making sure that the string is well-formed
20375 ** UTF-8.  Miscoded characters are removed.
20376 **
20377 ** The translation is done in-place and aborted if the output
20378 ** overruns the input.
20379 */
sqlite3Utf8To8(unsigned char * zIn)20380 SQLITE_PRIVATE int sqlite3Utf8To8(unsigned char *zIn){
20381   unsigned char *zOut = zIn;
20382   unsigned char *zStart = zIn;
20383   u32 c;
20384 
20385   while( zIn[0] && zOut<=zIn ){
20386     c = sqlite3Utf8Read(zIn, (const u8**)&zIn);
20387     if( c!=0xfffd ){
20388       WRITE_UTF8(zOut, c);
20389     }
20390   }
20391   *zOut = 0;
20392   return (int)(zOut - zStart);
20393 }
20394 #endif
20395 
20396 #ifndef SQLITE_OMIT_UTF16
20397 /*
20398 ** Convert a UTF-16 string in the native encoding into a UTF-8 string.
20399 ** Memory to hold the UTF-8 string is obtained from sqlite3_malloc and must
20400 ** be freed by the calling function.
20401 **
20402 ** NULL is returned if there is an allocation error.
20403 */
sqlite3Utf16to8(sqlite3 * db,const void * z,int nByte,u8 enc)20404 SQLITE_PRIVATE char *sqlite3Utf16to8(sqlite3 *db, const void *z, int nByte, u8 enc){
20405   Mem m;
20406   memset(&m, 0, sizeof(m));
20407   m.db = db;
20408   sqlite3VdbeMemSetStr(&m, z, nByte, enc, SQLITE_STATIC);
20409   sqlite3VdbeChangeEncoding(&m, SQLITE_UTF8);
20410   if( db->mallocFailed ){
20411     sqlite3VdbeMemRelease(&m);
20412     m.z = 0;
20413   }
20414   assert( (m.flags & MEM_Term)!=0 || db->mallocFailed );
20415   assert( (m.flags & MEM_Str)!=0 || db->mallocFailed );
20416   assert( (m.flags & MEM_Dyn)!=0 || db->mallocFailed );
20417   assert( m.z || db->mallocFailed );
20418   return m.z;
20419 }
20420 
20421 /*
20422 ** Convert a UTF-8 string to the UTF-16 encoding specified by parameter
20423 ** enc. A pointer to the new string is returned, and the value of *pnOut
20424 ** is set to the length of the returned string in bytes. The call should
20425 ** arrange to call sqlite3DbFree() on the returned pointer when it is
20426 ** no longer required.
20427 **
20428 ** If a malloc failure occurs, NULL is returned and the db.mallocFailed
20429 ** flag set.
20430 */
20431 #ifdef SQLITE_ENABLE_STAT2
sqlite3Utf8to16(sqlite3 * db,u8 enc,char * z,int n,int * pnOut)20432 SQLITE_PRIVATE char *sqlite3Utf8to16(sqlite3 *db, u8 enc, char *z, int n, int *pnOut){
20433   Mem m;
20434   memset(&m, 0, sizeof(m));
20435   m.db = db;
20436   sqlite3VdbeMemSetStr(&m, z, n, SQLITE_UTF8, SQLITE_STATIC);
20437   if( sqlite3VdbeMemTranslate(&m, enc) ){
20438     assert( db->mallocFailed );
20439     return 0;
20440   }
20441   assert( m.z==m.zMalloc );
20442   *pnOut = m.n;
20443   return m.z;
20444 }
20445 #endif
20446 
20447 /*
20448 ** zIn is a UTF-16 encoded unicode string at least nChar characters long.
20449 ** Return the number of bytes in the first nChar unicode characters
20450 ** in pZ.  nChar must be non-negative.
20451 */
sqlite3Utf16ByteLen(const void * zIn,int nChar)20452 SQLITE_PRIVATE int sqlite3Utf16ByteLen(const void *zIn, int nChar){
20453   int c;
20454   unsigned char const *z = zIn;
20455   int n = 0;
20456 
20457   if( SQLITE_UTF16NATIVE==SQLITE_UTF16BE ){
20458     while( n<nChar ){
20459       READ_UTF16BE(z, 1, c);
20460       n++;
20461     }
20462   }else{
20463     while( n<nChar ){
20464       READ_UTF16LE(z, 1, c);
20465       n++;
20466     }
20467   }
20468   return (int)(z-(unsigned char const *)zIn);
20469 }
20470 
20471 #if defined(SQLITE_TEST)
20472 /*
20473 ** This routine is called from the TCL test function "translate_selftest".
20474 ** It checks that the primitives for serializing and deserializing
20475 ** characters in each encoding are inverses of each other.
20476 */
sqlite3UtfSelfTest(void)20477 SQLITE_PRIVATE void sqlite3UtfSelfTest(void){
20478   unsigned int i, t;
20479   unsigned char zBuf[20];
20480   unsigned char *z;
20481   int n;
20482   unsigned int c;
20483 
20484   for(i=0; i<0x00110000; i++){
20485     z = zBuf;
20486     WRITE_UTF8(z, i);
20487     n = (int)(z-zBuf);
20488     assert( n>0 && n<=4 );
20489     z[0] = 0;
20490     z = zBuf;
20491     c = sqlite3Utf8Read(z, (const u8**)&z);
20492     t = i;
20493     if( i>=0xD800 && i<=0xDFFF ) t = 0xFFFD;
20494     if( (i&0xFFFFFFFE)==0xFFFE ) t = 0xFFFD;
20495     assert( c==t );
20496     assert( (z-zBuf)==n );
20497   }
20498   for(i=0; i<0x00110000; i++){
20499     if( i>=0xD800 && i<0xE000 ) continue;
20500     z = zBuf;
20501     WRITE_UTF16LE(z, i);
20502     n = (int)(z-zBuf);
20503     assert( n>0 && n<=4 );
20504     z[0] = 0;
20505     z = zBuf;
20506     READ_UTF16LE(z, 1, c);
20507     assert( c==i );
20508     assert( (z-zBuf)==n );
20509   }
20510   for(i=0; i<0x00110000; i++){
20511     if( i>=0xD800 && i<0xE000 ) continue;
20512     z = zBuf;
20513     WRITE_UTF16BE(z, i);
20514     n = (int)(z-zBuf);
20515     assert( n>0 && n<=4 );
20516     z[0] = 0;
20517     z = zBuf;
20518     READ_UTF16BE(z, 1, c);
20519     assert( c==i );
20520     assert( (z-zBuf)==n );
20521   }
20522 }
20523 #endif /* SQLITE_TEST */
20524 #endif /* SQLITE_OMIT_UTF16 */
20525 
20526 /************** End of utf.c *************************************************/
20527 /************** Begin file util.c ********************************************/
20528 /*
20529 ** 2001 September 15
20530 **
20531 ** The author disclaims copyright to this source code.  In place of
20532 ** a legal notice, here is a blessing:
20533 **
20534 **    May you do good and not evil.
20535 **    May you find forgiveness for yourself and forgive others.
20536 **    May you share freely, never taking more than you give.
20537 **
20538 *************************************************************************
20539 ** Utility functions used throughout sqlite.
20540 **
20541 ** This file contains functions for allocating memory, comparing
20542 ** strings, and stuff like that.
20543 **
20544 */
20545 #ifdef SQLITE_HAVE_ISNAN
20546 # include <math.h>
20547 #endif
20548 
20549 /*
20550 ** Routine needed to support the testcase() macro.
20551 */
20552 #ifdef SQLITE_COVERAGE_TEST
sqlite3Coverage(int x)20553 SQLITE_PRIVATE void sqlite3Coverage(int x){
20554   static unsigned dummy = 0;
20555   dummy += (unsigned)x;
20556 }
20557 #endif
20558 
20559 #ifndef SQLITE_OMIT_FLOATING_POINT
20560 /*
20561 ** Return true if the floating point value is Not a Number (NaN).
20562 **
20563 ** Use the math library isnan() function if compiled with SQLITE_HAVE_ISNAN.
20564 ** Otherwise, we have our own implementation that works on most systems.
20565 */
sqlite3IsNaN(double x)20566 SQLITE_PRIVATE int sqlite3IsNaN(double x){
20567   int rc;   /* The value return */
20568 #if !defined(SQLITE_HAVE_ISNAN)
20569   /*
20570   ** Systems that support the isnan() library function should probably
20571   ** make use of it by compiling with -DSQLITE_HAVE_ISNAN.  But we have
20572   ** found that many systems do not have a working isnan() function so
20573   ** this implementation is provided as an alternative.
20574   **
20575   ** This NaN test sometimes fails if compiled on GCC with -ffast-math.
20576   ** On the other hand, the use of -ffast-math comes with the following
20577   ** warning:
20578   **
20579   **      This option [-ffast-math] should never be turned on by any
20580   **      -O option since it can result in incorrect output for programs
20581   **      which depend on an exact implementation of IEEE or ISO
20582   **      rules/specifications for math functions.
20583   **
20584   ** Under MSVC, this NaN test may fail if compiled with a floating-
20585   ** point precision mode other than /fp:precise.  From the MSDN
20586   ** documentation:
20587   **
20588   **      The compiler [with /fp:precise] will properly handle comparisons
20589   **      involving NaN. For example, x != x evaluates to true if x is NaN
20590   **      ...
20591   */
20592 #ifdef __FAST_MATH__
20593 # error SQLite will not work correctly with the -ffast-math option of GCC.
20594 #endif
20595   volatile double y = x;
20596   volatile double z = y;
20597   rc = (y!=z);
20598 #else  /* if defined(SQLITE_HAVE_ISNAN) */
20599   rc = isnan(x);
20600 #endif /* SQLITE_HAVE_ISNAN */
20601   testcase( rc );
20602   return rc;
20603 }
20604 #endif /* SQLITE_OMIT_FLOATING_POINT */
20605 
20606 /*
20607 ** Compute a string length that is limited to what can be stored in
20608 ** lower 30 bits of a 32-bit signed integer.
20609 **
20610 ** The value returned will never be negative.  Nor will it ever be greater
20611 ** than the actual length of the string.  For very long strings (greater
20612 ** than 1GiB) the value returned might be less than the true string length.
20613 */
sqlite3Strlen30(const char * z)20614 SQLITE_PRIVATE int sqlite3Strlen30(const char *z){
20615   const char *z2 = z;
20616   if( z==0 ) return 0;
20617   while( *z2 ){ z2++; }
20618   return 0x3fffffff & (int)(z2 - z);
20619 }
20620 
20621 /*
20622 ** Set the most recent error code and error string for the sqlite
20623 ** handle "db". The error code is set to "err_code".
20624 **
20625 ** If it is not NULL, string zFormat specifies the format of the
20626 ** error string in the style of the printf functions: The following
20627 ** format characters are allowed:
20628 **
20629 **      %s      Insert a string
20630 **      %z      A string that should be freed after use
20631 **      %d      Insert an integer
20632 **      %T      Insert a token
20633 **      %S      Insert the first element of a SrcList
20634 **
20635 ** zFormat and any string tokens that follow it are assumed to be
20636 ** encoded in UTF-8.
20637 **
20638 ** To clear the most recent error for sqlite handle "db", sqlite3Error
20639 ** should be called with err_code set to SQLITE_OK and zFormat set
20640 ** to NULL.
20641 */
sqlite3Error(sqlite3 * db,int err_code,const char * zFormat,...)20642 SQLITE_PRIVATE void sqlite3Error(sqlite3 *db, int err_code, const char *zFormat, ...){
20643   if( db && (db->pErr || (db->pErr = sqlite3ValueNew(db))!=0) ){
20644     db->errCode = err_code;
20645     if( zFormat ){
20646       char *z;
20647       va_list ap;
20648       va_start(ap, zFormat);
20649       z = sqlite3VMPrintf(db, zFormat, ap);
20650       va_end(ap);
20651       sqlite3ValueSetStr(db->pErr, -1, z, SQLITE_UTF8, SQLITE_DYNAMIC);
20652     }else{
20653       sqlite3ValueSetStr(db->pErr, 0, 0, SQLITE_UTF8, SQLITE_STATIC);
20654     }
20655   }
20656 }
20657 
20658 /*
20659 ** Add an error message to pParse->zErrMsg and increment pParse->nErr.
20660 ** The following formatting characters are allowed:
20661 **
20662 **      %s      Insert a string
20663 **      %z      A string that should be freed after use
20664 **      %d      Insert an integer
20665 **      %T      Insert a token
20666 **      %S      Insert the first element of a SrcList
20667 **
20668 ** This function should be used to report any error that occurs whilst
20669 ** compiling an SQL statement (i.e. within sqlite3_prepare()). The
20670 ** last thing the sqlite3_prepare() function does is copy the error
20671 ** stored by this function into the database handle using sqlite3Error().
20672 ** Function sqlite3Error() should be used during statement execution
20673 ** (sqlite3_step() etc.).
20674 */
sqlite3ErrorMsg(Parse * pParse,const char * zFormat,...)20675 SQLITE_PRIVATE void sqlite3ErrorMsg(Parse *pParse, const char *zFormat, ...){
20676   char *zMsg;
20677   va_list ap;
20678   sqlite3 *db = pParse->db;
20679   va_start(ap, zFormat);
20680   zMsg = sqlite3VMPrintf(db, zFormat, ap);
20681   va_end(ap);
20682   if( db->suppressErr ){
20683     sqlite3DbFree(db, zMsg);
20684   }else{
20685     pParse->nErr++;
20686     sqlite3DbFree(db, pParse->zErrMsg);
20687     pParse->zErrMsg = zMsg;
20688     pParse->rc = SQLITE_ERROR;
20689   }
20690 }
20691 
20692 /*
20693 ** Convert an SQL-style quoted string into a normal string by removing
20694 ** the quote characters.  The conversion is done in-place.  If the
20695 ** input does not begin with a quote character, then this routine
20696 ** is a no-op.
20697 **
20698 ** The input string must be zero-terminated.  A new zero-terminator
20699 ** is added to the dequoted string.
20700 **
20701 ** The return value is -1 if no dequoting occurs or the length of the
20702 ** dequoted string, exclusive of the zero terminator, if dequoting does
20703 ** occur.
20704 **
20705 ** 2002-Feb-14: This routine is extended to remove MS-Access style
20706 ** brackets from around identifers.  For example:  "[a-b-c]" becomes
20707 ** "a-b-c".
20708 */
sqlite3Dequote(char * z)20709 SQLITE_PRIVATE int sqlite3Dequote(char *z){
20710   char quote;
20711   int i, j;
20712   if( z==0 ) return -1;
20713   quote = z[0];
20714   switch( quote ){
20715     case '\'':  break;
20716     case '"':   break;
20717     case '`':   break;                /* For MySQL compatibility */
20718     case '[':   quote = ']';  break;  /* For MS SqlServer compatibility */
20719     default:    return -1;
20720   }
20721   for(i=1, j=0; ALWAYS(z[i]); i++){
20722     if( z[i]==quote ){
20723       if( z[i+1]==quote ){
20724         z[j++] = quote;
20725         i++;
20726       }else{
20727         break;
20728       }
20729     }else{
20730       z[j++] = z[i];
20731     }
20732   }
20733   z[j] = 0;
20734   return j;
20735 }
20736 
20737 /* Convenient short-hand */
20738 #define UpperToLower sqlite3UpperToLower
20739 
20740 /*
20741 ** Some systems have stricmp().  Others have strcasecmp().  Because
20742 ** there is no consistency, we will define our own.
20743 **
20744 ** IMPLEMENTATION-OF: R-20522-24639 The sqlite3_strnicmp() API allows
20745 ** applications and extensions to compare the contents of two buffers
20746 ** containing UTF-8 strings in a case-independent fashion, using the same
20747 ** definition of case independence that SQLite uses internally when
20748 ** comparing identifiers.
20749 */
sqlite3StrICmp(const char * zLeft,const char * zRight)20750 SQLITE_PRIVATE int sqlite3StrICmp(const char *zLeft, const char *zRight){
20751   register unsigned char *a, *b;
20752   a = (unsigned char *)zLeft;
20753   b = (unsigned char *)zRight;
20754   while( *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
20755   return UpperToLower[*a] - UpperToLower[*b];
20756 }
sqlite3_strnicmp(const char * zLeft,const char * zRight,int N)20757 SQLITE_API int sqlite3_strnicmp(const char *zLeft, const char *zRight, int N){
20758   register unsigned char *a, *b;
20759   a = (unsigned char *)zLeft;
20760   b = (unsigned char *)zRight;
20761   while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
20762   return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b];
20763 }
20764 
20765 /*
20766 ** The string z[] is an text representation of a real number.
20767 ** Convert this string to a double and write it into *pResult.
20768 **
20769 ** The string z[] is length bytes in length (bytes, not characters) and
20770 ** uses the encoding enc.  The string is not necessarily zero-terminated.
20771 **
20772 ** Return TRUE if the result is a valid real number (or integer) and FALSE
20773 ** if the string is empty or contains extraneous text.  Valid numbers
20774 ** are in one of these formats:
20775 **
20776 **    [+-]digits[E[+-]digits]
20777 **    [+-]digits.[digits][E[+-]digits]
20778 **    [+-].digits[E[+-]digits]
20779 **
20780 ** Leading and trailing whitespace is ignored for the purpose of determining
20781 ** validity.
20782 **
20783 ** If some prefix of the input string is a valid number, this routine
20784 ** returns FALSE but it still converts the prefix and writes the result
20785 ** into *pResult.
20786 */
sqlite3AtoF(const char * z,double * pResult,int length,u8 enc)20787 SQLITE_PRIVATE int sqlite3AtoF(const char *z, double *pResult, int length, u8 enc){
20788 #ifndef SQLITE_OMIT_FLOATING_POINT
20789   int incr = (enc==SQLITE_UTF8?1:2);
20790   const char *zEnd = z + length;
20791   /* sign * significand * (10 ^ (esign * exponent)) */
20792   int sign = 1;    /* sign of significand */
20793   i64 s = 0;       /* significand */
20794   int d = 0;       /* adjust exponent for shifting decimal point */
20795   int esign = 1;   /* sign of exponent */
20796   int e = 0;       /* exponent */
20797   int eValid = 1;  /* True exponent is either not used or is well-formed */
20798   double result;
20799   int nDigits = 0;
20800 
20801   *pResult = 0.0;   /* Default return value, in case of an error */
20802 
20803   if( enc==SQLITE_UTF16BE ) z++;
20804 
20805   /* skip leading spaces */
20806   while( z<zEnd && sqlite3Isspace(*z) ) z+=incr;
20807   if( z>=zEnd ) return 0;
20808 
20809   /* get sign of significand */
20810   if( *z=='-' ){
20811     sign = -1;
20812     z+=incr;
20813   }else if( *z=='+' ){
20814     z+=incr;
20815   }
20816 
20817   /* skip leading zeroes */
20818   while( z<zEnd && z[0]=='0' ) z+=incr, nDigits++;
20819 
20820   /* copy max significant digits to significand */
20821   while( z<zEnd && sqlite3Isdigit(*z) && s<((LARGEST_INT64-9)/10) ){
20822     s = s*10 + (*z - '0');
20823     z+=incr, nDigits++;
20824   }
20825 
20826   /* skip non-significant significand digits
20827   ** (increase exponent by d to shift decimal left) */
20828   while( z<zEnd && sqlite3Isdigit(*z) ) z+=incr, nDigits++, d++;
20829   if( z>=zEnd ) goto do_atof_calc;
20830 
20831   /* if decimal point is present */
20832   if( *z=='.' ){
20833     z+=incr;
20834     /* copy digits from after decimal to significand
20835     ** (decrease exponent by d to shift decimal right) */
20836     while( z<zEnd && sqlite3Isdigit(*z) && s<((LARGEST_INT64-9)/10) ){
20837       s = s*10 + (*z - '0');
20838       z+=incr, nDigits++, d--;
20839     }
20840     /* skip non-significant digits */
20841     while( z<zEnd && sqlite3Isdigit(*z) ) z+=incr, nDigits++;
20842   }
20843   if( z>=zEnd ) goto do_atof_calc;
20844 
20845   /* if exponent is present */
20846   if( *z=='e' || *z=='E' ){
20847     z+=incr;
20848     eValid = 0;
20849     if( z>=zEnd ) goto do_atof_calc;
20850     /* get sign of exponent */
20851     if( *z=='-' ){
20852       esign = -1;
20853       z+=incr;
20854     }else if( *z=='+' ){
20855       z+=incr;
20856     }
20857     /* copy digits to exponent */
20858     while( z<zEnd && sqlite3Isdigit(*z) ){
20859       e = e*10 + (*z - '0');
20860       z+=incr;
20861       eValid = 1;
20862     }
20863   }
20864 
20865   /* skip trailing spaces */
20866   if( nDigits && eValid ){
20867     while( z<zEnd && sqlite3Isspace(*z) ) z+=incr;
20868   }
20869 
20870 do_atof_calc:
20871   /* adjust exponent by d, and update sign */
20872   e = (e*esign) + d;
20873   if( e<0 ) {
20874     esign = -1;
20875     e *= -1;
20876   } else {
20877     esign = 1;
20878   }
20879 
20880   /* if 0 significand */
20881   if( !s ) {
20882     /* In the IEEE 754 standard, zero is signed.
20883     ** Add the sign if we've seen at least one digit */
20884     result = (sign<0 && nDigits) ? -(double)0 : (double)0;
20885   } else {
20886     /* attempt to reduce exponent */
20887     if( esign>0 ){
20888       while( s<(LARGEST_INT64/10) && e>0 ) e--,s*=10;
20889     }else{
20890       while( !(s%10) && e>0 ) e--,s/=10;
20891     }
20892 
20893     /* adjust the sign of significand */
20894     s = sign<0 ? -s : s;
20895 
20896     /* if exponent, scale significand as appropriate
20897     ** and store in result. */
20898     if( e ){
20899       double scale = 1.0;
20900       /* attempt to handle extremely small/large numbers better */
20901       if( e>307 && e<342 ){
20902         while( e%308 ) { scale *= 1.0e+1; e -= 1; }
20903         if( esign<0 ){
20904           result = s / scale;
20905           result /= 1.0e+308;
20906         }else{
20907           result = s * scale;
20908           result *= 1.0e+308;
20909         }
20910       }else{
20911         /* 1.0e+22 is the largest power of 10 than can be
20912         ** represented exactly. */
20913         while( e%22 ) { scale *= 1.0e+1; e -= 1; }
20914         while( e>0 ) { scale *= 1.0e+22; e -= 22; }
20915         if( esign<0 ){
20916           result = s / scale;
20917         }else{
20918           result = s * scale;
20919         }
20920       }
20921     } else {
20922       result = (double)s;
20923     }
20924   }
20925 
20926   /* store the result */
20927   *pResult = result;
20928 
20929   /* return true if number and no extra non-whitespace chracters after */
20930   return z>=zEnd && nDigits>0 && eValid;
20931 #else
20932   return !sqlite3Atoi64(z, pResult, length, enc);
20933 #endif /* SQLITE_OMIT_FLOATING_POINT */
20934 }
20935 
20936 /*
20937 ** Compare the 19-character string zNum against the text representation
20938 ** value 2^63:  9223372036854775808.  Return negative, zero, or positive
20939 ** if zNum is less than, equal to, or greater than the string.
20940 ** Note that zNum must contain exactly 19 characters.
20941 **
20942 ** Unlike memcmp() this routine is guaranteed to return the difference
20943 ** in the values of the last digit if the only difference is in the
20944 ** last digit.  So, for example,
20945 **
20946 **      compare2pow63("9223372036854775800", 1)
20947 **
20948 ** will return -8.
20949 */
compare2pow63(const char * zNum,int incr)20950 static int compare2pow63(const char *zNum, int incr){
20951   int c = 0;
20952   int i;
20953                     /* 012345678901234567 */
20954   const char *pow63 = "922337203685477580";
20955   for(i=0; c==0 && i<18; i++){
20956     c = (zNum[i*incr]-pow63[i])*10;
20957   }
20958   if( c==0 ){
20959     c = zNum[18*incr] - '8';
20960     testcase( c==(-1) );
20961     testcase( c==0 );
20962     testcase( c==(+1) );
20963   }
20964   return c;
20965 }
20966 
20967 
20968 /*
20969 ** Convert zNum to a 64-bit signed integer.
20970 **
20971 ** If the zNum value is representable as a 64-bit twos-complement
20972 ** integer, then write that value into *pNum and return 0.
20973 **
20974 ** If zNum is exactly 9223372036854665808, return 2.  This special
20975 ** case is broken out because while 9223372036854665808 cannot be a
20976 ** signed 64-bit integer, its negative -9223372036854665808 can be.
20977 **
20978 ** If zNum is too big for a 64-bit integer and is not
20979 ** 9223372036854665808 then return 1.
20980 **
20981 ** length is the number of bytes in the string (bytes, not characters).
20982 ** The string is not necessarily zero-terminated.  The encoding is
20983 ** given by enc.
20984 */
sqlite3Atoi64(const char * zNum,i64 * pNum,int length,u8 enc)20985 SQLITE_PRIVATE int sqlite3Atoi64(const char *zNum, i64 *pNum, int length, u8 enc){
20986   int incr = (enc==SQLITE_UTF8?1:2);
20987   u64 u = 0;
20988   int neg = 0; /* assume positive */
20989   int i;
20990   int c = 0;
20991   const char *zStart;
20992   const char *zEnd = zNum + length;
20993   if( enc==SQLITE_UTF16BE ) zNum++;
20994   while( zNum<zEnd && sqlite3Isspace(*zNum) ) zNum+=incr;
20995   if( zNum<zEnd ){
20996     if( *zNum=='-' ){
20997       neg = 1;
20998       zNum+=incr;
20999     }else if( *zNum=='+' ){
21000       zNum+=incr;
21001     }
21002   }
21003   zStart = zNum;
21004   while( zNum<zEnd && zNum[0]=='0' ){ zNum+=incr; } /* Skip leading zeros. */
21005   for(i=0; &zNum[i]<zEnd && (c=zNum[i])>='0' && c<='9'; i+=incr){
21006     u = u*10 + c - '0';
21007   }
21008   if( u>LARGEST_INT64 ){
21009     *pNum = SMALLEST_INT64;
21010   }else if( neg ){
21011     *pNum = -(i64)u;
21012   }else{
21013     *pNum = (i64)u;
21014   }
21015   testcase( i==18 );
21016   testcase( i==19 );
21017   testcase( i==20 );
21018   if( (c!=0 && &zNum[i]<zEnd) || (i==0 && zStart==zNum) || i>19*incr ){
21019     /* zNum is empty or contains non-numeric text or is longer
21020     ** than 19 digits (thus guaranteeing that it is too large) */
21021     return 1;
21022   }else if( i<19*incr ){
21023     /* Less than 19 digits, so we know that it fits in 64 bits */
21024     assert( u<=LARGEST_INT64 );
21025     return 0;
21026   }else{
21027     /* zNum is a 19-digit numbers.  Compare it against 9223372036854775808. */
21028     c = compare2pow63(zNum, incr);
21029     if( c<0 ){
21030       /* zNum is less than 9223372036854775808 so it fits */
21031       assert( u<=LARGEST_INT64 );
21032       return 0;
21033     }else if( c>0 ){
21034       /* zNum is greater than 9223372036854775808 so it overflows */
21035       return 1;
21036     }else{
21037       /* zNum is exactly 9223372036854775808.  Fits if negative.  The
21038       ** special case 2 overflow if positive */
21039       assert( u-1==LARGEST_INT64 );
21040       assert( (*pNum)==SMALLEST_INT64 );
21041       return neg ? 0 : 2;
21042     }
21043   }
21044 }
21045 
21046 /*
21047 ** If zNum represents an integer that will fit in 32-bits, then set
21048 ** *pValue to that integer and return true.  Otherwise return false.
21049 **
21050 ** Any non-numeric characters that following zNum are ignored.
21051 ** This is different from sqlite3Atoi64() which requires the
21052 ** input number to be zero-terminated.
21053 */
sqlite3GetInt32(const char * zNum,int * pValue)21054 SQLITE_PRIVATE int sqlite3GetInt32(const char *zNum, int *pValue){
21055   sqlite_int64 v = 0;
21056   int i, c;
21057   int neg = 0;
21058   if( zNum[0]=='-' ){
21059     neg = 1;
21060     zNum++;
21061   }else if( zNum[0]=='+' ){
21062     zNum++;
21063   }
21064   while( zNum[0]=='0' ) zNum++;
21065   for(i=0; i<11 && (c = zNum[i] - '0')>=0 && c<=9; i++){
21066     v = v*10 + c;
21067   }
21068 
21069   /* The longest decimal representation of a 32 bit integer is 10 digits:
21070   **
21071   **             1234567890
21072   **     2^31 -> 2147483648
21073   */
21074   testcase( i==10 );
21075   if( i>10 ){
21076     return 0;
21077   }
21078   testcase( v-neg==2147483647 );
21079   if( v-neg>2147483647 ){
21080     return 0;
21081   }
21082   if( neg ){
21083     v = -v;
21084   }
21085   *pValue = (int)v;
21086   return 1;
21087 }
21088 
21089 /*
21090 ** Return a 32-bit integer value extracted from a string.  If the
21091 ** string is not an integer, just return 0.
21092 */
sqlite3Atoi(const char * z)21093 SQLITE_PRIVATE int sqlite3Atoi(const char *z){
21094   int x = 0;
21095   if( z ) sqlite3GetInt32(z, &x);
21096   return x;
21097 }
21098 
21099 /*
21100 ** The variable-length integer encoding is as follows:
21101 **
21102 ** KEY:
21103 **         A = 0xxxxxxx    7 bits of data and one flag bit
21104 **         B = 1xxxxxxx    7 bits of data and one flag bit
21105 **         C = xxxxxxxx    8 bits of data
21106 **
21107 **  7 bits - A
21108 ** 14 bits - BA
21109 ** 21 bits - BBA
21110 ** 28 bits - BBBA
21111 ** 35 bits - BBBBA
21112 ** 42 bits - BBBBBA
21113 ** 49 bits - BBBBBBA
21114 ** 56 bits - BBBBBBBA
21115 ** 64 bits - BBBBBBBBC
21116 */
21117 
21118 /*
21119 ** Write a 64-bit variable-length integer to memory starting at p[0].
21120 ** The length of data write will be between 1 and 9 bytes.  The number
21121 ** of bytes written is returned.
21122 **
21123 ** A variable-length integer consists of the lower 7 bits of each byte
21124 ** for all bytes that have the 8th bit set and one byte with the 8th
21125 ** bit clear.  Except, if we get to the 9th byte, it stores the full
21126 ** 8 bits and is the last byte.
21127 */
sqlite3PutVarint(unsigned char * p,u64 v)21128 SQLITE_PRIVATE int sqlite3PutVarint(unsigned char *p, u64 v){
21129   int i, j, n;
21130   u8 buf[10];
21131   if( v & (((u64)0xff000000)<<32) ){
21132     p[8] = (u8)v;
21133     v >>= 8;
21134     for(i=7; i>=0; i--){
21135       p[i] = (u8)((v & 0x7f) | 0x80);
21136       v >>= 7;
21137     }
21138     return 9;
21139   }
21140   n = 0;
21141   do{
21142     buf[n++] = (u8)((v & 0x7f) | 0x80);
21143     v >>= 7;
21144   }while( v!=0 );
21145   buf[0] &= 0x7f;
21146   assert( n<=9 );
21147   for(i=0, j=n-1; j>=0; j--, i++){
21148     p[i] = buf[j];
21149   }
21150   return n;
21151 }
21152 
21153 /*
21154 ** This routine is a faster version of sqlite3PutVarint() that only
21155 ** works for 32-bit positive integers and which is optimized for
21156 ** the common case of small integers.  A MACRO version, putVarint32,
21157 ** is provided which inlines the single-byte case.  All code should use
21158 ** the MACRO version as this function assumes the single-byte case has
21159 ** already been handled.
21160 */
sqlite3PutVarint32(unsigned char * p,u32 v)21161 SQLITE_PRIVATE int sqlite3PutVarint32(unsigned char *p, u32 v){
21162 #ifndef putVarint32
21163   if( (v & ~0x7f)==0 ){
21164     p[0] = v;
21165     return 1;
21166   }
21167 #endif
21168   if( (v & ~0x3fff)==0 ){
21169     p[0] = (u8)((v>>7) | 0x80);
21170     p[1] = (u8)(v & 0x7f);
21171     return 2;
21172   }
21173   return sqlite3PutVarint(p, v);
21174 }
21175 
21176 /*
21177 ** Bitmasks used by sqlite3GetVarint().  These precomputed constants
21178 ** are defined here rather than simply putting the constant expressions
21179 ** inline in order to work around bugs in the RVT compiler.
21180 **
21181 ** SLOT_2_0     A mask for  (0x7f<<14) | 0x7f
21182 **
21183 ** SLOT_4_2_0   A mask for  (0x7f<<28) | SLOT_2_0
21184 */
21185 #define SLOT_2_0     0x001fc07f
21186 #define SLOT_4_2_0   0xf01fc07f
21187 
21188 
21189 /*
21190 ** Read a 64-bit variable-length integer from memory starting at p[0].
21191 ** Return the number of bytes read.  The value is stored in *v.
21192 */
sqlite3GetVarint(const unsigned char * p,u64 * v)21193 SQLITE_PRIVATE u8 sqlite3GetVarint(const unsigned char *p, u64 *v){
21194   u32 a,b,s;
21195 
21196   a = *p;
21197   /* a: p0 (unmasked) */
21198   if (!(a&0x80))
21199   {
21200     *v = a;
21201     return 1;
21202   }
21203 
21204   p++;
21205   b = *p;
21206   /* b: p1 (unmasked) */
21207   if (!(b&0x80))
21208   {
21209     a &= 0x7f;
21210     a = a<<7;
21211     a |= b;
21212     *v = a;
21213     return 2;
21214   }
21215 
21216   /* Verify that constants are precomputed correctly */
21217   assert( SLOT_2_0 == ((0x7f<<14) | (0x7f)) );
21218   assert( SLOT_4_2_0 == ((0xfU<<28) | (0x7f<<14) | (0x7f)) );
21219 
21220   p++;
21221   a = a<<14;
21222   a |= *p;
21223   /* a: p0<<14 | p2 (unmasked) */
21224   if (!(a&0x80))
21225   {
21226     a &= SLOT_2_0;
21227     b &= 0x7f;
21228     b = b<<7;
21229     a |= b;
21230     *v = a;
21231     return 3;
21232   }
21233 
21234   /* CSE1 from below */
21235   a &= SLOT_2_0;
21236   p++;
21237   b = b<<14;
21238   b |= *p;
21239   /* b: p1<<14 | p3 (unmasked) */
21240   if (!(b&0x80))
21241   {
21242     b &= SLOT_2_0;
21243     /* moved CSE1 up */
21244     /* a &= (0x7f<<14)|(0x7f); */
21245     a = a<<7;
21246     a |= b;
21247     *v = a;
21248     return 4;
21249   }
21250 
21251   /* a: p0<<14 | p2 (masked) */
21252   /* b: p1<<14 | p3 (unmasked) */
21253   /* 1:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
21254   /* moved CSE1 up */
21255   /* a &= (0x7f<<14)|(0x7f); */
21256   b &= SLOT_2_0;
21257   s = a;
21258   /* s: p0<<14 | p2 (masked) */
21259 
21260   p++;
21261   a = a<<14;
21262   a |= *p;
21263   /* a: p0<<28 | p2<<14 | p4 (unmasked) */
21264   if (!(a&0x80))
21265   {
21266     /* we can skip these cause they were (effectively) done above in calc'ing s */
21267     /* a &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
21268     /* b &= (0x7f<<14)|(0x7f); */
21269     b = b<<7;
21270     a |= b;
21271     s = s>>18;
21272     *v = ((u64)s)<<32 | a;
21273     return 5;
21274   }
21275 
21276   /* 2:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
21277   s = s<<7;
21278   s |= b;
21279   /* s: p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
21280 
21281   p++;
21282   b = b<<14;
21283   b |= *p;
21284   /* b: p1<<28 | p3<<14 | p5 (unmasked) */
21285   if (!(b&0x80))
21286   {
21287     /* we can skip this cause it was (effectively) done above in calc'ing s */
21288     /* b &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
21289     a &= SLOT_2_0;
21290     a = a<<7;
21291     a |= b;
21292     s = s>>18;
21293     *v = ((u64)s)<<32 | a;
21294     return 6;
21295   }
21296 
21297   p++;
21298   a = a<<14;
21299   a |= *p;
21300   /* a: p2<<28 | p4<<14 | p6 (unmasked) */
21301   if (!(a&0x80))
21302   {
21303     a &= SLOT_4_2_0;
21304     b &= SLOT_2_0;
21305     b = b<<7;
21306     a |= b;
21307     s = s>>11;
21308     *v = ((u64)s)<<32 | a;
21309     return 7;
21310   }
21311 
21312   /* CSE2 from below */
21313   a &= SLOT_2_0;
21314   p++;
21315   b = b<<14;
21316   b |= *p;
21317   /* b: p3<<28 | p5<<14 | p7 (unmasked) */
21318   if (!(b&0x80))
21319   {
21320     b &= SLOT_4_2_0;
21321     /* moved CSE2 up */
21322     /* a &= (0x7f<<14)|(0x7f); */
21323     a = a<<7;
21324     a |= b;
21325     s = s>>4;
21326     *v = ((u64)s)<<32 | a;
21327     return 8;
21328   }
21329 
21330   p++;
21331   a = a<<15;
21332   a |= *p;
21333   /* a: p4<<29 | p6<<15 | p8 (unmasked) */
21334 
21335   /* moved CSE2 up */
21336   /* a &= (0x7f<<29)|(0x7f<<15)|(0xff); */
21337   b &= SLOT_2_0;
21338   b = b<<8;
21339   a |= b;
21340 
21341   s = s<<4;
21342   b = p[-4];
21343   b &= 0x7f;
21344   b = b>>3;
21345   s |= b;
21346 
21347   *v = ((u64)s)<<32 | a;
21348 
21349   return 9;
21350 }
21351 
21352 /*
21353 ** Read a 32-bit variable-length integer from memory starting at p[0].
21354 ** Return the number of bytes read.  The value is stored in *v.
21355 **
21356 ** If the varint stored in p[0] is larger than can fit in a 32-bit unsigned
21357 ** integer, then set *v to 0xffffffff.
21358 **
21359 ** A MACRO version, getVarint32, is provided which inlines the
21360 ** single-byte case.  All code should use the MACRO version as
21361 ** this function assumes the single-byte case has already been handled.
21362 */
sqlite3GetVarint32(const unsigned char * p,u32 * v)21363 SQLITE_PRIVATE u8 sqlite3GetVarint32(const unsigned char *p, u32 *v){
21364   u32 a,b;
21365 
21366   /* The 1-byte case.  Overwhelmingly the most common.  Handled inline
21367   ** by the getVarin32() macro */
21368   a = *p;
21369   /* a: p0 (unmasked) */
21370 #ifndef getVarint32
21371   if (!(a&0x80))
21372   {
21373     /* Values between 0 and 127 */
21374     *v = a;
21375     return 1;
21376   }
21377 #endif
21378 
21379   /* The 2-byte case */
21380   p++;
21381   b = *p;
21382   /* b: p1 (unmasked) */
21383   if (!(b&0x80))
21384   {
21385     /* Values between 128 and 16383 */
21386     a &= 0x7f;
21387     a = a<<7;
21388     *v = a | b;
21389     return 2;
21390   }
21391 
21392   /* The 3-byte case */
21393   p++;
21394   a = a<<14;
21395   a |= *p;
21396   /* a: p0<<14 | p2 (unmasked) */
21397   if (!(a&0x80))
21398   {
21399     /* Values between 16384 and 2097151 */
21400     a &= (0x7f<<14)|(0x7f);
21401     b &= 0x7f;
21402     b = b<<7;
21403     *v = a | b;
21404     return 3;
21405   }
21406 
21407   /* A 32-bit varint is used to store size information in btrees.
21408   ** Objects are rarely larger than 2MiB limit of a 3-byte varint.
21409   ** A 3-byte varint is sufficient, for example, to record the size
21410   ** of a 1048569-byte BLOB or string.
21411   **
21412   ** We only unroll the first 1-, 2-, and 3- byte cases.  The very
21413   ** rare larger cases can be handled by the slower 64-bit varint
21414   ** routine.
21415   */
21416 #if 1
21417   {
21418     u64 v64;
21419     u8 n;
21420 
21421     p -= 2;
21422     n = sqlite3GetVarint(p, &v64);
21423     assert( n>3 && n<=9 );
21424     if( (v64 & SQLITE_MAX_U32)!=v64 ){
21425       *v = 0xffffffff;
21426     }else{
21427       *v = (u32)v64;
21428     }
21429     return n;
21430   }
21431 
21432 #else
21433   /* For following code (kept for historical record only) shows an
21434   ** unrolling for the 3- and 4-byte varint cases.  This code is
21435   ** slightly faster, but it is also larger and much harder to test.
21436   */
21437   p++;
21438   b = b<<14;
21439   b |= *p;
21440   /* b: p1<<14 | p3 (unmasked) */
21441   if (!(b&0x80))
21442   {
21443     /* Values between 2097152 and 268435455 */
21444     b &= (0x7f<<14)|(0x7f);
21445     a &= (0x7f<<14)|(0x7f);
21446     a = a<<7;
21447     *v = a | b;
21448     return 4;
21449   }
21450 
21451   p++;
21452   a = a<<14;
21453   a |= *p;
21454   /* a: p0<<28 | p2<<14 | p4 (unmasked) */
21455   if (!(a&0x80))
21456   {
21457     /* Values  between 268435456 and 34359738367 */
21458     a &= SLOT_4_2_0;
21459     b &= SLOT_4_2_0;
21460     b = b<<7;
21461     *v = a | b;
21462     return 5;
21463   }
21464 
21465   /* We can only reach this point when reading a corrupt database
21466   ** file.  In that case we are not in any hurry.  Use the (relatively
21467   ** slow) general-purpose sqlite3GetVarint() routine to extract the
21468   ** value. */
21469   {
21470     u64 v64;
21471     u8 n;
21472 
21473     p -= 4;
21474     n = sqlite3GetVarint(p, &v64);
21475     assert( n>5 && n<=9 );
21476     *v = (u32)v64;
21477     return n;
21478   }
21479 #endif
21480 }
21481 
21482 /*
21483 ** Return the number of bytes that will be needed to store the given
21484 ** 64-bit integer.
21485 */
sqlite3VarintLen(u64 v)21486 SQLITE_PRIVATE int sqlite3VarintLen(u64 v){
21487   int i = 0;
21488   do{
21489     i++;
21490     v >>= 7;
21491   }while( v!=0 && ALWAYS(i<9) );
21492   return i;
21493 }
21494 
21495 
21496 /*
21497 ** Read or write a four-byte big-endian integer value.
21498 */
sqlite3Get4byte(const u8 * p)21499 SQLITE_PRIVATE u32 sqlite3Get4byte(const u8 *p){
21500   return (p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
21501 }
sqlite3Put4byte(unsigned char * p,u32 v)21502 SQLITE_PRIVATE void sqlite3Put4byte(unsigned char *p, u32 v){
21503   p[0] = (u8)(v>>24);
21504   p[1] = (u8)(v>>16);
21505   p[2] = (u8)(v>>8);
21506   p[3] = (u8)v;
21507 }
21508 
21509 
21510 
21511 /*
21512 ** Translate a single byte of Hex into an integer.
21513 ** This routine only works if h really is a valid hexadecimal
21514 ** character:  0..9a..fA..F
21515 */
sqlite3HexToInt(int h)21516 SQLITE_PRIVATE u8 sqlite3HexToInt(int h){
21517   assert( (h>='0' && h<='9') ||  (h>='a' && h<='f') ||  (h>='A' && h<='F') );
21518 #ifdef SQLITE_ASCII
21519   h += 9*(1&(h>>6));
21520 #endif
21521 #ifdef SQLITE_EBCDIC
21522   h += 9*(1&~(h>>4));
21523 #endif
21524   return (u8)(h & 0xf);
21525 }
21526 
21527 #if !defined(SQLITE_OMIT_BLOB_LITERAL) || defined(SQLITE_HAS_CODEC)
21528 /*
21529 ** Convert a BLOB literal of the form "x'hhhhhh'" into its binary
21530 ** value.  Return a pointer to its binary value.  Space to hold the
21531 ** binary value has been obtained from malloc and must be freed by
21532 ** the calling routine.
21533 */
sqlite3HexToBlob(sqlite3 * db,const char * z,int n)21534 SQLITE_PRIVATE void *sqlite3HexToBlob(sqlite3 *db, const char *z, int n){
21535   char *zBlob;
21536   int i;
21537 
21538   zBlob = (char *)sqlite3DbMallocRaw(db, n/2 + 1);
21539   n--;
21540   if( zBlob ){
21541     for(i=0; i<n; i+=2){
21542       zBlob[i/2] = (sqlite3HexToInt(z[i])<<4) | sqlite3HexToInt(z[i+1]);
21543     }
21544     zBlob[i/2] = 0;
21545   }
21546   return zBlob;
21547 }
21548 #endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC */
21549 
21550 /*
21551 ** Log an error that is an API call on a connection pointer that should
21552 ** not have been used.  The "type" of connection pointer is given as the
21553 ** argument.  The zType is a word like "NULL" or "closed" or "invalid".
21554 */
logBadConnection(const char * zType)21555 static void logBadConnection(const char *zType){
21556   sqlite3_log(SQLITE_MISUSE,
21557      "API call with %s database connection pointer",
21558      zType
21559   );
21560 }
21561 
21562 /*
21563 ** Check to make sure we have a valid db pointer.  This test is not
21564 ** foolproof but it does provide some measure of protection against
21565 ** misuse of the interface such as passing in db pointers that are
21566 ** NULL or which have been previously closed.  If this routine returns
21567 ** 1 it means that the db pointer is valid and 0 if it should not be
21568 ** dereferenced for any reason.  The calling function should invoke
21569 ** SQLITE_MISUSE immediately.
21570 **
21571 ** sqlite3SafetyCheckOk() requires that the db pointer be valid for
21572 ** use.  sqlite3SafetyCheckSickOrOk() allows a db pointer that failed to
21573 ** open properly and is not fit for general use but which can be
21574 ** used as an argument to sqlite3_errmsg() or sqlite3_close().
21575 */
sqlite3SafetyCheckOk(sqlite3 * db)21576 SQLITE_PRIVATE int sqlite3SafetyCheckOk(sqlite3 *db){
21577   u32 magic;
21578   if( db==0 ){
21579     logBadConnection("NULL");
21580     return 0;
21581   }
21582   magic = db->magic;
21583   if( magic!=SQLITE_MAGIC_OPEN ){
21584     if( sqlite3SafetyCheckSickOrOk(db) ){
21585       testcase( sqlite3GlobalConfig.xLog!=0 );
21586       logBadConnection("unopened");
21587     }
21588     return 0;
21589   }else{
21590     return 1;
21591   }
21592 }
sqlite3SafetyCheckSickOrOk(sqlite3 * db)21593 SQLITE_PRIVATE int sqlite3SafetyCheckSickOrOk(sqlite3 *db){
21594   u32 magic;
21595   magic = db->magic;
21596   if( magic!=SQLITE_MAGIC_SICK &&
21597       magic!=SQLITE_MAGIC_OPEN &&
21598       magic!=SQLITE_MAGIC_BUSY ){
21599     testcase( sqlite3GlobalConfig.xLog!=0 );
21600     logBadConnection("invalid");
21601     return 0;
21602   }else{
21603     return 1;
21604   }
21605 }
21606 
21607 /*
21608 ** Attempt to add, substract, or multiply the 64-bit signed value iB against
21609 ** the other 64-bit signed integer at *pA and store the result in *pA.
21610 ** Return 0 on success.  Or if the operation would have resulted in an
21611 ** overflow, leave *pA unchanged and return 1.
21612 */
sqlite3AddInt64(i64 * pA,i64 iB)21613 SQLITE_PRIVATE int sqlite3AddInt64(i64 *pA, i64 iB){
21614   i64 iA = *pA;
21615   testcase( iA==0 ); testcase( iA==1 );
21616   testcase( iB==-1 ); testcase( iB==0 );
21617   if( iB>=0 ){
21618     testcase( iA>0 && LARGEST_INT64 - iA == iB );
21619     testcase( iA>0 && LARGEST_INT64 - iA == iB - 1 );
21620     if( iA>0 && LARGEST_INT64 - iA < iB ) return 1;
21621     *pA += iB;
21622   }else{
21623     testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 1 );
21624     testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 2 );
21625     if( iA<0 && -(iA + LARGEST_INT64) > iB + 1 ) return 1;
21626     *pA += iB;
21627   }
21628   return 0;
21629 }
sqlite3SubInt64(i64 * pA,i64 iB)21630 SQLITE_PRIVATE int sqlite3SubInt64(i64 *pA, i64 iB){
21631   testcase( iB==SMALLEST_INT64+1 );
21632   if( iB==SMALLEST_INT64 ){
21633     testcase( (*pA)==(-1) ); testcase( (*pA)==0 );
21634     if( (*pA)>=0 ) return 1;
21635     *pA -= iB;
21636     return 0;
21637   }else{
21638     return sqlite3AddInt64(pA, -iB);
21639   }
21640 }
21641 #define TWOPOWER32 (((i64)1)<<32)
21642 #define TWOPOWER31 (((i64)1)<<31)
sqlite3MulInt64(i64 * pA,i64 iB)21643 SQLITE_PRIVATE int sqlite3MulInt64(i64 *pA, i64 iB){
21644   i64 iA = *pA;
21645   i64 iA1, iA0, iB1, iB0, r;
21646 
21647   iA1 = iA/TWOPOWER32;
21648   iA0 = iA % TWOPOWER32;
21649   iB1 = iB/TWOPOWER32;
21650   iB0 = iB % TWOPOWER32;
21651   if( iA1*iB1 != 0 ) return 1;
21652   assert( iA1*iB0==0 || iA0*iB1==0 );
21653   r = iA1*iB0 + iA0*iB1;
21654   testcase( r==(-TWOPOWER31)-1 );
21655   testcase( r==(-TWOPOWER31) );
21656   testcase( r==TWOPOWER31 );
21657   testcase( r==TWOPOWER31-1 );
21658   if( r<(-TWOPOWER31) || r>=TWOPOWER31 ) return 1;
21659   r *= TWOPOWER32;
21660   if( sqlite3AddInt64(&r, iA0*iB0) ) return 1;
21661   *pA = r;
21662   return 0;
21663 }
21664 
21665 /*
21666 ** Compute the absolute value of a 32-bit signed integer, of possible.  Or
21667 ** if the integer has a value of -2147483648, return +2147483647
21668 */
sqlite3AbsInt32(int x)21669 SQLITE_PRIVATE int sqlite3AbsInt32(int x){
21670   if( x>=0 ) return x;
21671   if( x==(int)0x80000000 ) return 0x7fffffff;
21672   return -x;
21673 }
21674 
21675 #ifdef SQLITE_ENABLE_8_3_NAMES
21676 /*
21677 ** If SQLITE_ENABLE_8_3_NAME is set at compile-time and if the database
21678 ** filename in zBaseFilename is a URI with the "8_3_names=1" parameter and
21679 ** if filename in z[] has a suffix (a.k.a. "extension") that is longer than
21680 ** three characters, then shorten the suffix on z[] to be the last three
21681 ** characters of the original suffix.
21682 **
21683 ** Examples:
21684 **
21685 **     test.db-journal    =>   test.nal
21686 **     test.db-wal        =>   test.wal
21687 **     test.db-shm        =>   test.shm
21688 */
sqlite3FileSuffix3(const char * zBaseFilename,char * z)21689 SQLITE_PRIVATE void sqlite3FileSuffix3(const char *zBaseFilename, char *z){
21690   const char *zOk;
21691   zOk = sqlite3_uri_parameter(zBaseFilename, "8_3_names");
21692   if( zOk && sqlite3GetBoolean(zOk) ){
21693     int i, sz;
21694     sz = sqlite3Strlen30(z);
21695     for(i=sz-1; i>0 && z[i]!='/' && z[i]!='.'; i--){}
21696     if( z[i]=='.' && ALWAYS(sz>i+4) ) memcpy(&z[i+1], &z[sz-3], 4);
21697   }
21698 }
21699 #endif
21700 
21701 /************** End of util.c ************************************************/
21702 /************** Begin file hash.c ********************************************/
21703 /*
21704 ** 2001 September 22
21705 **
21706 ** The author disclaims copyright to this source code.  In place of
21707 ** a legal notice, here is a blessing:
21708 **
21709 **    May you do good and not evil.
21710 **    May you find forgiveness for yourself and forgive others.
21711 **    May you share freely, never taking more than you give.
21712 **
21713 *************************************************************************
21714 ** This is the implementation of generic hash-tables
21715 ** used in SQLite.
21716 */
21717 
21718 /* Turn bulk memory into a hash table object by initializing the
21719 ** fields of the Hash structure.
21720 **
21721 ** "pNew" is a pointer to the hash table that is to be initialized.
21722 */
sqlite3HashInit(Hash * pNew)21723 SQLITE_PRIVATE void sqlite3HashInit(Hash *pNew){
21724   assert( pNew!=0 );
21725   pNew->first = 0;
21726   pNew->count = 0;
21727   pNew->htsize = 0;
21728   pNew->ht = 0;
21729 }
21730 
21731 /* Remove all entries from a hash table.  Reclaim all memory.
21732 ** Call this routine to delete a hash table or to reset a hash table
21733 ** to the empty state.
21734 */
sqlite3HashClear(Hash * pH)21735 SQLITE_PRIVATE void sqlite3HashClear(Hash *pH){
21736   HashElem *elem;         /* For looping over all elements of the table */
21737 
21738   assert( pH!=0 );
21739   elem = pH->first;
21740   pH->first = 0;
21741   sqlite3_free(pH->ht);
21742   pH->ht = 0;
21743   pH->htsize = 0;
21744   while( elem ){
21745     HashElem *next_elem = elem->next;
21746     sqlite3_free(elem);
21747     elem = next_elem;
21748   }
21749   pH->count = 0;
21750 }
21751 
21752 /*
21753 ** The hashing function.
21754 */
strHash(const char * z,int nKey)21755 static unsigned int strHash(const char *z, int nKey){
21756   int h = 0;
21757   assert( nKey>=0 );
21758   while( nKey > 0  ){
21759     h = (h<<3) ^ h ^ sqlite3UpperToLower[(unsigned char)*z++];
21760     nKey--;
21761   }
21762   return h;
21763 }
21764 
21765 
21766 /* Link pNew element into the hash table pH.  If pEntry!=0 then also
21767 ** insert pNew into the pEntry hash bucket.
21768 */
insertElement(Hash * pH,struct _ht * pEntry,HashElem * pNew)21769 static void insertElement(
21770   Hash *pH,              /* The complete hash table */
21771   struct _ht *pEntry,    /* The entry into which pNew is inserted */
21772   HashElem *pNew         /* The element to be inserted */
21773 ){
21774   HashElem *pHead;       /* First element already in pEntry */
21775   if( pEntry ){
21776     pHead = pEntry->count ? pEntry->chain : 0;
21777     pEntry->count++;
21778     pEntry->chain = pNew;
21779   }else{
21780     pHead = 0;
21781   }
21782   if( pHead ){
21783     pNew->next = pHead;
21784     pNew->prev = pHead->prev;
21785     if( pHead->prev ){ pHead->prev->next = pNew; }
21786     else             { pH->first = pNew; }
21787     pHead->prev = pNew;
21788   }else{
21789     pNew->next = pH->first;
21790     if( pH->first ){ pH->first->prev = pNew; }
21791     pNew->prev = 0;
21792     pH->first = pNew;
21793   }
21794 }
21795 
21796 
21797 /* Resize the hash table so that it cantains "new_size" buckets.
21798 **
21799 ** The hash table might fail to resize if sqlite3_malloc() fails or
21800 ** if the new size is the same as the prior size.
21801 ** Return TRUE if the resize occurs and false if not.
21802 */
rehash(Hash * pH,unsigned int new_size)21803 static int rehash(Hash *pH, unsigned int new_size){
21804   struct _ht *new_ht;            /* The new hash table */
21805   HashElem *elem, *next_elem;    /* For looping over existing elements */
21806 
21807 #if SQLITE_MALLOC_SOFT_LIMIT>0
21808   if( new_size*sizeof(struct _ht)>SQLITE_MALLOC_SOFT_LIMIT ){
21809     new_size = SQLITE_MALLOC_SOFT_LIMIT/sizeof(struct _ht);
21810   }
21811   if( new_size==pH->htsize ) return 0;
21812 #endif
21813 
21814   /* The inability to allocates space for a larger hash table is
21815   ** a performance hit but it is not a fatal error.  So mark the
21816   ** allocation as a benign.
21817   */
21818   sqlite3BeginBenignMalloc();
21819   new_ht = (struct _ht *)sqlite3Malloc( new_size*sizeof(struct _ht) );
21820   sqlite3EndBenignMalloc();
21821 
21822   if( new_ht==0 ) return 0;
21823   sqlite3_free(pH->ht);
21824   pH->ht = new_ht;
21825   pH->htsize = new_size = sqlite3MallocSize(new_ht)/sizeof(struct _ht);
21826   memset(new_ht, 0, new_size*sizeof(struct _ht));
21827   for(elem=pH->first, pH->first=0; elem; elem = next_elem){
21828     unsigned int h = strHash(elem->pKey, elem->nKey) % new_size;
21829     next_elem = elem->next;
21830     insertElement(pH, &new_ht[h], elem);
21831   }
21832   return 1;
21833 }
21834 
21835 /* This function (for internal use only) locates an element in an
21836 ** hash table that matches the given key.  The hash for this key has
21837 ** already been computed and is passed as the 4th parameter.
21838 */
findElementGivenHash(const Hash * pH,const char * pKey,int nKey,unsigned int h)21839 static HashElem *findElementGivenHash(
21840   const Hash *pH,     /* The pH to be searched */
21841   const char *pKey,   /* The key we are searching for */
21842   int nKey,           /* Bytes in key (not counting zero terminator) */
21843   unsigned int h      /* The hash for this key. */
21844 ){
21845   HashElem *elem;                /* Used to loop thru the element list */
21846   int count;                     /* Number of elements left to test */
21847 
21848   if( pH->ht ){
21849     struct _ht *pEntry = &pH->ht[h];
21850     elem = pEntry->chain;
21851     count = pEntry->count;
21852   }else{
21853     elem = pH->first;
21854     count = pH->count;
21855   }
21856   while( count-- && ALWAYS(elem) ){
21857     if( elem->nKey==nKey && sqlite3StrNICmp(elem->pKey,pKey,nKey)==0 ){
21858       return elem;
21859     }
21860     elem = elem->next;
21861   }
21862   return 0;
21863 }
21864 
21865 /* Remove a single entry from the hash table given a pointer to that
21866 ** element and a hash on the element's key.
21867 */
removeElementGivenHash(Hash * pH,HashElem * elem,unsigned int h)21868 static void removeElementGivenHash(
21869   Hash *pH,         /* The pH containing "elem" */
21870   HashElem* elem,   /* The element to be removed from the pH */
21871   unsigned int h    /* Hash value for the element */
21872 ){
21873   struct _ht *pEntry;
21874   if( elem->prev ){
21875     elem->prev->next = elem->next;
21876   }else{
21877     pH->first = elem->next;
21878   }
21879   if( elem->next ){
21880     elem->next->prev = elem->prev;
21881   }
21882   if( pH->ht ){
21883     pEntry = &pH->ht[h];
21884     if( pEntry->chain==elem ){
21885       pEntry->chain = elem->next;
21886     }
21887     pEntry->count--;
21888     assert( pEntry->count>=0 );
21889   }
21890   sqlite3_free( elem );
21891   pH->count--;
21892   if( pH->count<=0 ){
21893     assert( pH->first==0 );
21894     assert( pH->count==0 );
21895     sqlite3HashClear(pH);
21896   }
21897 }
21898 
21899 /* Attempt to locate an element of the hash table pH with a key
21900 ** that matches pKey,nKey.  Return the data for this element if it is
21901 ** found, or NULL if there is no match.
21902 */
sqlite3HashFind(const Hash * pH,const char * pKey,int nKey)21903 SQLITE_PRIVATE void *sqlite3HashFind(const Hash *pH, const char *pKey, int nKey){
21904   HashElem *elem;    /* The element that matches key */
21905   unsigned int h;    /* A hash on key */
21906 
21907   assert( pH!=0 );
21908   assert( pKey!=0 );
21909   assert( nKey>=0 );
21910   if( pH->ht ){
21911     h = strHash(pKey, nKey) % pH->htsize;
21912   }else{
21913     h = 0;
21914   }
21915   elem = findElementGivenHash(pH, pKey, nKey, h);
21916   return elem ? elem->data : 0;
21917 }
21918 
21919 /* Insert an element into the hash table pH.  The key is pKey,nKey
21920 ** and the data is "data".
21921 **
21922 ** If no element exists with a matching key, then a new
21923 ** element is created and NULL is returned.
21924 **
21925 ** If another element already exists with the same key, then the
21926 ** new data replaces the old data and the old data is returned.
21927 ** The key is not copied in this instance.  If a malloc fails, then
21928 ** the new data is returned and the hash table is unchanged.
21929 **
21930 ** If the "data" parameter to this function is NULL, then the
21931 ** element corresponding to "key" is removed from the hash table.
21932 */
sqlite3HashInsert(Hash * pH,const char * pKey,int nKey,void * data)21933 SQLITE_PRIVATE void *sqlite3HashInsert(Hash *pH, const char *pKey, int nKey, void *data){
21934   unsigned int h;       /* the hash of the key modulo hash table size */
21935   HashElem *elem;       /* Used to loop thru the element list */
21936   HashElem *new_elem;   /* New element added to the pH */
21937 
21938   assert( pH!=0 );
21939   assert( pKey!=0 );
21940   assert( nKey>=0 );
21941   if( pH->htsize ){
21942     h = strHash(pKey, nKey) % pH->htsize;
21943   }else{
21944     h = 0;
21945   }
21946   elem = findElementGivenHash(pH,pKey,nKey,h);
21947   if( elem ){
21948     void *old_data = elem->data;
21949     if( data==0 ){
21950       removeElementGivenHash(pH,elem,h);
21951     }else{
21952       elem->data = data;
21953       elem->pKey = pKey;
21954       assert(nKey==elem->nKey);
21955     }
21956     return old_data;
21957   }
21958   if( data==0 ) return 0;
21959   new_elem = (HashElem*)sqlite3Malloc( sizeof(HashElem) );
21960   if( new_elem==0 ) return data;
21961   new_elem->pKey = pKey;
21962   new_elem->nKey = nKey;
21963   new_elem->data = data;
21964   pH->count++;
21965   if( pH->count>=10 && pH->count > 2*pH->htsize ){
21966     if( rehash(pH, pH->count*2) ){
21967       assert( pH->htsize>0 );
21968       h = strHash(pKey, nKey) % pH->htsize;
21969     }
21970   }
21971   if( pH->ht ){
21972     insertElement(pH, &pH->ht[h], new_elem);
21973   }else{
21974     insertElement(pH, 0, new_elem);
21975   }
21976   return 0;
21977 }
21978 
21979 /************** End of hash.c ************************************************/
21980 /************** Begin file opcodes.c *****************************************/
21981 /* Automatically generated.  Do not edit */
21982 /* See the mkopcodec.awk script for details. */
21983 #if !defined(SQLITE_OMIT_EXPLAIN) || !defined(NDEBUG) || defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
sqlite3OpcodeName(int i)21984 SQLITE_PRIVATE const char *sqlite3OpcodeName(int i){
21985  static const char *const azName[] = { "?",
21986      /*   1 */ "Goto",
21987      /*   2 */ "Gosub",
21988      /*   3 */ "Return",
21989      /*   4 */ "Yield",
21990      /*   5 */ "HaltIfNull",
21991      /*   6 */ "Halt",
21992      /*   7 */ "Integer",
21993      /*   8 */ "Int64",
21994      /*   9 */ "String",
21995      /*  10 */ "Null",
21996      /*  11 */ "Blob",
21997      /*  12 */ "Variable",
21998      /*  13 */ "Move",
21999      /*  14 */ "Copy",
22000      /*  15 */ "SCopy",
22001      /*  16 */ "ResultRow",
22002      /*  17 */ "CollSeq",
22003      /*  18 */ "Function",
22004      /*  19 */ "Not",
22005      /*  20 */ "AddImm",
22006      /*  21 */ "MustBeInt",
22007      /*  22 */ "RealAffinity",
22008      /*  23 */ "Permutation",
22009      /*  24 */ "Compare",
22010      /*  25 */ "Jump",
22011      /*  26 */ "If",
22012      /*  27 */ "IfNot",
22013      /*  28 */ "Column",
22014      /*  29 */ "Affinity",
22015      /*  30 */ "MakeRecord",
22016      /*  31 */ "Count",
22017      /*  32 */ "Savepoint",
22018      /*  33 */ "AutoCommit",
22019      /*  34 */ "Transaction",
22020      /*  35 */ "ReadCookie",
22021      /*  36 */ "SetCookie",
22022      /*  37 */ "VerifyCookie",
22023      /*  38 */ "OpenRead",
22024      /*  39 */ "OpenWrite",
22025      /*  40 */ "OpenAutoindex",
22026      /*  41 */ "OpenEphemeral",
22027      /*  42 */ "OpenPseudo",
22028      /*  43 */ "Close",
22029      /*  44 */ "SeekLt",
22030      /*  45 */ "SeekLe",
22031      /*  46 */ "SeekGe",
22032      /*  47 */ "SeekGt",
22033      /*  48 */ "Seek",
22034      /*  49 */ "NotFound",
22035      /*  50 */ "Found",
22036      /*  51 */ "IsUnique",
22037      /*  52 */ "NotExists",
22038      /*  53 */ "Sequence",
22039      /*  54 */ "NewRowid",
22040      /*  55 */ "Insert",
22041      /*  56 */ "InsertInt",
22042      /*  57 */ "Delete",
22043      /*  58 */ "ResetCount",
22044      /*  59 */ "RowKey",
22045      /*  60 */ "RowData",
22046      /*  61 */ "Rowid",
22047      /*  62 */ "NullRow",
22048      /*  63 */ "Last",
22049      /*  64 */ "Sort",
22050      /*  65 */ "Rewind",
22051      /*  66 */ "Prev",
22052      /*  67 */ "Next",
22053      /*  68 */ "Or",
22054      /*  69 */ "And",
22055      /*  70 */ "IdxInsert",
22056      /*  71 */ "IdxDelete",
22057      /*  72 */ "IdxRowid",
22058      /*  73 */ "IsNull",
22059      /*  74 */ "NotNull",
22060      /*  75 */ "Ne",
22061      /*  76 */ "Eq",
22062      /*  77 */ "Gt",
22063      /*  78 */ "Le",
22064      /*  79 */ "Lt",
22065      /*  80 */ "Ge",
22066      /*  81 */ "IdxLT",
22067      /*  82 */ "BitAnd",
22068      /*  83 */ "BitOr",
22069      /*  84 */ "ShiftLeft",
22070      /*  85 */ "ShiftRight",
22071      /*  86 */ "Add",
22072      /*  87 */ "Subtract",
22073      /*  88 */ "Multiply",
22074      /*  89 */ "Divide",
22075      /*  90 */ "Remainder",
22076      /*  91 */ "Concat",
22077      /*  92 */ "IdxGE",
22078      /*  93 */ "BitNot",
22079      /*  94 */ "String8",
22080      /*  95 */ "Destroy",
22081      /*  96 */ "Clear",
22082      /*  97 */ "CreateIndex",
22083      /*  98 */ "CreateTable",
22084      /*  99 */ "ParseSchema",
22085      /* 100 */ "LoadAnalysis",
22086      /* 101 */ "DropTable",
22087      /* 102 */ "DropIndex",
22088      /* 103 */ "DropTrigger",
22089      /* 104 */ "IntegrityCk",
22090      /* 105 */ "RowSetAdd",
22091      /* 106 */ "RowSetRead",
22092      /* 107 */ "RowSetTest",
22093      /* 108 */ "Program",
22094      /* 109 */ "Param",
22095      /* 110 */ "FkCounter",
22096      /* 111 */ "FkIfZero",
22097      /* 112 */ "MemMax",
22098      /* 113 */ "IfPos",
22099      /* 114 */ "IfNeg",
22100      /* 115 */ "IfZero",
22101      /* 116 */ "AggStep",
22102      /* 117 */ "AggFinal",
22103      /* 118 */ "Checkpoint",
22104      /* 119 */ "JournalMode",
22105      /* 120 */ "Vacuum",
22106      /* 121 */ "IncrVacuum",
22107      /* 122 */ "Expire",
22108      /* 123 */ "TableLock",
22109      /* 124 */ "VBegin",
22110      /* 125 */ "VCreate",
22111      /* 126 */ "VDestroy",
22112      /* 127 */ "VOpen",
22113      /* 128 */ "VFilter",
22114      /* 129 */ "VColumn",
22115      /* 130 */ "Real",
22116      /* 131 */ "VNext",
22117      /* 132 */ "VRename",
22118      /* 133 */ "VUpdate",
22119      /* 134 */ "Pagecount",
22120      /* 135 */ "MaxPgcnt",
22121      /* 136 */ "Trace",
22122      /* 137 */ "Noop",
22123      /* 138 */ "Explain",
22124      /* 139 */ "NotUsed_139",
22125      /* 140 */ "NotUsed_140",
22126      /* 141 */ "ToText",
22127      /* 142 */ "ToBlob",
22128      /* 143 */ "ToNumeric",
22129      /* 144 */ "ToInt",
22130      /* 145 */ "ToReal",
22131   };
22132   return azName[i];
22133 }
22134 #endif
22135 
22136 /************** End of opcodes.c *********************************************/
22137 /************** Begin file os_os2.c ******************************************/
22138 /*
22139 ** 2006 Feb 14
22140 **
22141 ** The author disclaims copyright to this source code.  In place of
22142 ** a legal notice, here is a blessing:
22143 **
22144 **    May you do good and not evil.
22145 **    May you find forgiveness for yourself and forgive others.
22146 **    May you share freely, never taking more than you give.
22147 **
22148 ******************************************************************************
22149 **
22150 ** This file contains code that is specific to OS/2.
22151 */
22152 
22153 
22154 #if SQLITE_OS_OS2
22155 
22156 /*
22157 ** A Note About Memory Allocation:
22158 **
22159 ** This driver uses malloc()/free() directly rather than going through
22160 ** the SQLite-wrappers sqlite3_malloc()/sqlite3_free().  Those wrappers
22161 ** are designed for use on embedded systems where memory is scarce and
22162 ** malloc failures happen frequently.  OS/2 does not typically run on
22163 ** embedded systems, and when it does the developers normally have bigger
22164 ** problems to worry about than running out of memory.  So there is not
22165 ** a compelling need to use the wrappers.
22166 **
22167 ** But there is a good reason to not use the wrappers.  If we use the
22168 ** wrappers then we will get simulated malloc() failures within this
22169 ** driver.  And that causes all kinds of problems for our tests.  We
22170 ** could enhance SQLite to deal with simulated malloc failures within
22171 ** the OS driver, but the code to deal with those failure would not
22172 ** be exercised on Linux (which does not need to malloc() in the driver)
22173 ** and so we would have difficulty writing coverage tests for that
22174 ** code.  Better to leave the code out, we think.
22175 **
22176 ** The point of this discussion is as follows:  When creating a new
22177 ** OS layer for an embedded system, if you use this file as an example,
22178 ** avoid the use of malloc()/free().  Those routines work ok on OS/2
22179 ** desktops but not so well in embedded systems.
22180 */
22181 
22182 /*
22183 ** Macros used to determine whether or not to use threads.
22184 */
22185 #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE
22186 # define SQLITE_OS2_THREADS 1
22187 #endif
22188 
22189 /*
22190 ** Include code that is common to all os_*.c files
22191 */
22192 /************** Include os_common.h in the middle of os_os2.c ****************/
22193 /************** Begin file os_common.h ***************************************/
22194 /*
22195 ** 2004 May 22
22196 **
22197 ** The author disclaims copyright to this source code.  In place of
22198 ** a legal notice, here is a blessing:
22199 **
22200 **    May you do good and not evil.
22201 **    May you find forgiveness for yourself and forgive others.
22202 **    May you share freely, never taking more than you give.
22203 **
22204 ******************************************************************************
22205 **
22206 ** This file contains macros and a little bit of code that is common to
22207 ** all of the platform-specific files (os_*.c) and is #included into those
22208 ** files.
22209 **
22210 ** This file should be #included by the os_*.c files only.  It is not a
22211 ** general purpose header file.
22212 */
22213 #ifndef _OS_COMMON_H_
22214 #define _OS_COMMON_H_
22215 
22216 /*
22217 ** At least two bugs have slipped in because we changed the MEMORY_DEBUG
22218 ** macro to SQLITE_DEBUG and some older makefiles have not yet made the
22219 ** switch.  The following code should catch this problem at compile-time.
22220 */
22221 #ifdef MEMORY_DEBUG
22222 # error "The MEMORY_DEBUG macro is obsolete.  Use SQLITE_DEBUG instead."
22223 #endif
22224 
22225 #ifdef SQLITE_DEBUG
22226 SQLITE_PRIVATE int sqlite3OSTrace = 0;
22227 #define OSTRACE(X)          if( sqlite3OSTrace ) sqlite3DebugPrintf X
22228 #else
22229 #define OSTRACE(X)
22230 #endif
22231 
22232 /*
22233 ** Macros for performance tracing.  Normally turned off.  Only works
22234 ** on i486 hardware.
22235 */
22236 #ifdef SQLITE_PERFORMANCE_TRACE
22237 
22238 /*
22239 ** hwtime.h contains inline assembler code for implementing
22240 ** high-performance timing routines.
22241 */
22242 /************** Include hwtime.h in the middle of os_common.h ****************/
22243 /************** Begin file hwtime.h ******************************************/
22244 /*
22245 ** 2008 May 27
22246 **
22247 ** The author disclaims copyright to this source code.  In place of
22248 ** a legal notice, here is a blessing:
22249 **
22250 **    May you do good and not evil.
22251 **    May you find forgiveness for yourself and forgive others.
22252 **    May you share freely, never taking more than you give.
22253 **
22254 ******************************************************************************
22255 **
22256 ** This file contains inline asm code for retrieving "high-performance"
22257 ** counters for x86 class CPUs.
22258 */
22259 #ifndef _HWTIME_H_
22260 #define _HWTIME_H_
22261 
22262 /*
22263 ** The following routine only works on pentium-class (or newer) processors.
22264 ** It uses the RDTSC opcode to read the cycle count value out of the
22265 ** processor and returns that value.  This can be used for high-res
22266 ** profiling.
22267 */
22268 #if (defined(__GNUC__) || defined(_MSC_VER)) && \
22269       (defined(i386) || defined(__i386__) || defined(_M_IX86))
22270 
22271   #if defined(__GNUC__)
22272 
sqlite3Hwtime(void)22273   __inline__ sqlite_uint64 sqlite3Hwtime(void){
22274      unsigned int lo, hi;
22275      __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
22276      return (sqlite_uint64)hi << 32 | lo;
22277   }
22278 
22279   #elif defined(_MSC_VER)
22280 
sqlite3Hwtime(void)22281   __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
22282      __asm {
22283         rdtsc
22284         ret       ; return value at EDX:EAX
22285      }
22286   }
22287 
22288   #endif
22289 
22290 #elif (defined(__GNUC__) && defined(__x86_64__))
22291 
22292   __inline__ sqlite_uint64 sqlite3Hwtime(void){
22293       unsigned long val;
22294       __asm__ __volatile__ ("rdtsc" : "=A" (val));
22295       return val;
22296   }
22297 
22298 #elif (defined(__GNUC__) && defined(__ppc__))
22299 
22300   __inline__ sqlite_uint64 sqlite3Hwtime(void){
22301       unsigned long long retval;
22302       unsigned long junk;
22303       __asm__ __volatile__ ("\n\
22304           1:      mftbu   %1\n\
22305                   mftb    %L0\n\
22306                   mftbu   %0\n\
22307                   cmpw    %0,%1\n\
22308                   bne     1b"
22309                   : "=r" (retval), "=r" (junk));
22310       return retval;
22311   }
22312 
22313 #else
22314 
22315   #error Need implementation of sqlite3Hwtime() for your platform.
22316 
22317   /*
22318   ** To compile without implementing sqlite3Hwtime() for your platform,
22319   ** you can remove the above #error and use the following
22320   ** stub function.  You will lose timing support for many
22321   ** of the debugging and testing utilities, but it should at
22322   ** least compile and run.
22323   */
22324 SQLITE_PRIVATE   sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
22325 
22326 #endif
22327 
22328 #endif /* !defined(_HWTIME_H_) */
22329 
22330 /************** End of hwtime.h **********************************************/
22331 /************** Continuing where we left off in os_common.h ******************/
22332 
22333 static sqlite_uint64 g_start;
22334 static sqlite_uint64 g_elapsed;
22335 #define TIMER_START       g_start=sqlite3Hwtime()
22336 #define TIMER_END         g_elapsed=sqlite3Hwtime()-g_start
22337 #define TIMER_ELAPSED     g_elapsed
22338 #else
22339 #define TIMER_START
22340 #define TIMER_END
22341 #define TIMER_ELAPSED     ((sqlite_uint64)0)
22342 #endif
22343 
22344 /*
22345 ** If we compile with the SQLITE_TEST macro set, then the following block
22346 ** of code will give us the ability to simulate a disk I/O error.  This
22347 ** is used for testing the I/O recovery logic.
22348 */
22349 #ifdef SQLITE_TEST
22350 SQLITE_API int sqlite3_io_error_hit = 0;            /* Total number of I/O Errors */
22351 SQLITE_API int sqlite3_io_error_hardhit = 0;        /* Number of non-benign errors */
22352 SQLITE_API int sqlite3_io_error_pending = 0;        /* Count down to first I/O error */
22353 SQLITE_API int sqlite3_io_error_persist = 0;        /* True if I/O errors persist */
22354 SQLITE_API int sqlite3_io_error_benign = 0;         /* True if errors are benign */
22355 SQLITE_API int sqlite3_diskfull_pending = 0;
22356 SQLITE_API int sqlite3_diskfull = 0;
22357 #define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X)
22358 #define SimulateIOError(CODE)  \
22359   if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \
22360        || sqlite3_io_error_pending-- == 1 )  \
22361               { local_ioerr(); CODE; }
22362 static void local_ioerr(){
22363   IOTRACE(("IOERR\n"));
22364   sqlite3_io_error_hit++;
22365   if( !sqlite3_io_error_benign ) sqlite3_io_error_hardhit++;
22366 }
22367 #define SimulateDiskfullError(CODE) \
22368    if( sqlite3_diskfull_pending ){ \
22369      if( sqlite3_diskfull_pending == 1 ){ \
22370        local_ioerr(); \
22371        sqlite3_diskfull = 1; \
22372        sqlite3_io_error_hit = 1; \
22373        CODE; \
22374      }else{ \
22375        sqlite3_diskfull_pending--; \
22376      } \
22377    }
22378 #else
22379 #define SimulateIOErrorBenign(X)
22380 #define SimulateIOError(A)
22381 #define SimulateDiskfullError(A)
22382 #endif
22383 
22384 /*
22385 ** When testing, keep a count of the number of open files.
22386 */
22387 #ifdef SQLITE_TEST
22388 SQLITE_API int sqlite3_open_file_count = 0;
22389 #define OpenCounter(X)  sqlite3_open_file_count+=(X)
22390 #else
22391 #define OpenCounter(X)
22392 #endif
22393 
22394 #endif /* !defined(_OS_COMMON_H_) */
22395 
22396 /************** End of os_common.h *******************************************/
22397 /************** Continuing where we left off in os_os2.c *********************/
22398 
22399 /* Forward references */
22400 typedef struct os2File os2File;         /* The file structure */
22401 typedef struct os2ShmNode os2ShmNode;   /* A shared descritive memory node */
22402 typedef struct os2ShmLink os2ShmLink;   /* A connection to shared-memory */
22403 
22404 /*
22405 ** The os2File structure is subclass of sqlite3_file specific for the OS/2
22406 ** protability layer.
22407 */
22408 struct os2File {
22409   const sqlite3_io_methods *pMethod;  /* Always the first entry */
22410   HFILE h;                  /* Handle for accessing the file */
22411   int flags;                /* Flags provided to os2Open() */
22412   int locktype;             /* Type of lock currently held on this file */
22413   int szChunk;              /* Chunk size configured by FCNTL_CHUNK_SIZE */
22414   char *zFullPathCp;        /* Full path name of this file */
22415   os2ShmLink *pShmLink;     /* Instance of shared memory on this file */
22416 };
22417 
22418 #define LOCK_TIMEOUT 10L /* the default locking timeout */
22419 
22420 /*
22421 ** Missing from some versions of the OS/2 toolkit -
22422 ** used to allocate from high memory if possible
22423 */
22424 #ifndef OBJ_ANY
22425 # define OBJ_ANY 0x00000400
22426 #endif
22427 
22428 /*****************************************************************************
22429 ** The next group of routines implement the I/O methods specified
22430 ** by the sqlite3_io_methods object.
22431 ******************************************************************************/
22432 
22433 /*
22434 ** Close a file.
22435 */
22436 static int os2Close( sqlite3_file *id ){
22437   APIRET rc;
22438   os2File *pFile = (os2File*)id;
22439 
22440   assert( id!=0 );
22441   OSTRACE(( "CLOSE %d (%s)\n", pFile->h, pFile->zFullPathCp ));
22442 
22443   rc = DosClose( pFile->h );
22444 
22445   if( pFile->flags & SQLITE_OPEN_DELETEONCLOSE )
22446     DosForceDelete( (PSZ)pFile->zFullPathCp );
22447 
22448   free( pFile->zFullPathCp );
22449   pFile->zFullPathCp = NULL;
22450   pFile->locktype = NO_LOCK;
22451   pFile->h = (HFILE)-1;
22452   pFile->flags = 0;
22453 
22454   OpenCounter( -1 );
22455   return rc == NO_ERROR ? SQLITE_OK : SQLITE_IOERR;
22456 }
22457 
22458 /*
22459 ** Read data from a file into a buffer.  Return SQLITE_OK if all
22460 ** bytes were read successfully and SQLITE_IOERR if anything goes
22461 ** wrong.
22462 */
22463 static int os2Read(
22464   sqlite3_file *id,               /* File to read from */
22465   void *pBuf,                     /* Write content into this buffer */
22466   int amt,                        /* Number of bytes to read */
22467   sqlite3_int64 offset            /* Begin reading at this offset */
22468 ){
22469   ULONG fileLocation = 0L;
22470   ULONG got;
22471   os2File *pFile = (os2File*)id;
22472   assert( id!=0 );
22473   SimulateIOError( return SQLITE_IOERR_READ );
22474   OSTRACE(( "READ %d lock=%d\n", pFile->h, pFile->locktype ));
22475   if( DosSetFilePtr(pFile->h, offset, FILE_BEGIN, &fileLocation) != NO_ERROR ){
22476     return SQLITE_IOERR;
22477   }
22478   if( DosRead( pFile->h, pBuf, amt, &got ) != NO_ERROR ){
22479     return SQLITE_IOERR_READ;
22480   }
22481   if( got == (ULONG)amt )
22482     return SQLITE_OK;
22483   else {
22484     /* Unread portions of the input buffer must be zero-filled */
22485     memset(&((char*)pBuf)[got], 0, amt-got);
22486     return SQLITE_IOERR_SHORT_READ;
22487   }
22488 }
22489 
22490 /*
22491 ** Write data from a buffer into a file.  Return SQLITE_OK on success
22492 ** or some other error code on failure.
22493 */
22494 static int os2Write(
22495   sqlite3_file *id,               /* File to write into */
22496   const void *pBuf,               /* The bytes to be written */
22497   int amt,                        /* Number of bytes to write */
22498   sqlite3_int64 offset            /* Offset into the file to begin writing at */
22499 ){
22500   ULONG fileLocation = 0L;
22501   APIRET rc = NO_ERROR;
22502   ULONG wrote;
22503   os2File *pFile = (os2File*)id;
22504   assert( id!=0 );
22505   SimulateIOError( return SQLITE_IOERR_WRITE );
22506   SimulateDiskfullError( return SQLITE_FULL );
22507   OSTRACE(( "WRITE %d lock=%d\n", pFile->h, pFile->locktype ));
22508   if( DosSetFilePtr(pFile->h, offset, FILE_BEGIN, &fileLocation) != NO_ERROR ){
22509     return SQLITE_IOERR;
22510   }
22511   assert( amt>0 );
22512   while( amt > 0 &&
22513          ( rc = DosWrite( pFile->h, (PVOID)pBuf, amt, &wrote ) ) == NO_ERROR &&
22514          wrote > 0
22515   ){
22516     amt -= wrote;
22517     pBuf = &((char*)pBuf)[wrote];
22518   }
22519 
22520   return ( rc != NO_ERROR || amt > (int)wrote ) ? SQLITE_FULL : SQLITE_OK;
22521 }
22522 
22523 /*
22524 ** Truncate an open file to a specified size
22525 */
22526 static int os2Truncate( sqlite3_file *id, i64 nByte ){
22527   APIRET rc;
22528   os2File *pFile = (os2File*)id;
22529   assert( id!=0 );
22530   OSTRACE(( "TRUNCATE %d %lld\n", pFile->h, nByte ));
22531   SimulateIOError( return SQLITE_IOERR_TRUNCATE );
22532 
22533   /* If the user has configured a chunk-size for this file, truncate the
22534   ** file so that it consists of an integer number of chunks (i.e. the
22535   ** actual file size after the operation may be larger than the requested
22536   ** size).
22537   */
22538   if( pFile->szChunk ){
22539     nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
22540   }
22541 
22542   rc = DosSetFileSize( pFile->h, nByte );
22543   return rc == NO_ERROR ? SQLITE_OK : SQLITE_IOERR_TRUNCATE;
22544 }
22545 
22546 #ifdef SQLITE_TEST
22547 /*
22548 ** Count the number of fullsyncs and normal syncs.  This is used to test
22549 ** that syncs and fullsyncs are occuring at the right times.
22550 */
22551 SQLITE_API int sqlite3_sync_count = 0;
22552 SQLITE_API int sqlite3_fullsync_count = 0;
22553 #endif
22554 
22555 /*
22556 ** Make sure all writes to a particular file are committed to disk.
22557 */
22558 static int os2Sync( sqlite3_file *id, int flags ){
22559   os2File *pFile = (os2File*)id;
22560   OSTRACE(( "SYNC %d lock=%d\n", pFile->h, pFile->locktype ));
22561 #ifdef SQLITE_TEST
22562   if( flags & SQLITE_SYNC_FULL){
22563     sqlite3_fullsync_count++;
22564   }
22565   sqlite3_sync_count++;
22566 #endif
22567   /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
22568   ** no-op
22569   */
22570 #ifdef SQLITE_NO_SYNC
22571   UNUSED_PARAMETER(pFile);
22572   return SQLITE_OK;
22573 #else
22574   return DosResetBuffer( pFile->h ) == NO_ERROR ? SQLITE_OK : SQLITE_IOERR;
22575 #endif
22576 }
22577 
22578 /*
22579 ** Determine the current size of a file in bytes
22580 */
22581 static int os2FileSize( sqlite3_file *id, sqlite3_int64 *pSize ){
22582   APIRET rc = NO_ERROR;
22583   FILESTATUS3 fsts3FileInfo;
22584   memset(&fsts3FileInfo, 0, sizeof(fsts3FileInfo));
22585   assert( id!=0 );
22586   SimulateIOError( return SQLITE_IOERR_FSTAT );
22587   rc = DosQueryFileInfo( ((os2File*)id)->h, FIL_STANDARD, &fsts3FileInfo, sizeof(FILESTATUS3) );
22588   if( rc == NO_ERROR ){
22589     *pSize = fsts3FileInfo.cbFile;
22590     return SQLITE_OK;
22591   }else{
22592     return SQLITE_IOERR_FSTAT;
22593   }
22594 }
22595 
22596 /*
22597 ** Acquire a reader lock.
22598 */
22599 static int getReadLock( os2File *pFile ){
22600   FILELOCK  LockArea,
22601             UnlockArea;
22602   APIRET res;
22603   memset(&LockArea, 0, sizeof(LockArea));
22604   memset(&UnlockArea, 0, sizeof(UnlockArea));
22605   LockArea.lOffset = SHARED_FIRST;
22606   LockArea.lRange = SHARED_SIZE;
22607   UnlockArea.lOffset = 0L;
22608   UnlockArea.lRange = 0L;
22609   res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 1L );
22610   OSTRACE(( "GETREADLOCK %d res=%d\n", pFile->h, res ));
22611   return res;
22612 }
22613 
22614 /*
22615 ** Undo a readlock
22616 */
22617 static int unlockReadLock( os2File *id ){
22618   FILELOCK  LockArea,
22619             UnlockArea;
22620   APIRET res;
22621   memset(&LockArea, 0, sizeof(LockArea));
22622   memset(&UnlockArea, 0, sizeof(UnlockArea));
22623   LockArea.lOffset = 0L;
22624   LockArea.lRange = 0L;
22625   UnlockArea.lOffset = SHARED_FIRST;
22626   UnlockArea.lRange = SHARED_SIZE;
22627   res = DosSetFileLocks( id->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 1L );
22628   OSTRACE(( "UNLOCK-READLOCK file handle=%d res=%d?\n", id->h, res ));
22629   return res;
22630 }
22631 
22632 /*
22633 ** Lock the file with the lock specified by parameter locktype - one
22634 ** of the following:
22635 **
22636 **     (1) SHARED_LOCK
22637 **     (2) RESERVED_LOCK
22638 **     (3) PENDING_LOCK
22639 **     (4) EXCLUSIVE_LOCK
22640 **
22641 ** Sometimes when requesting one lock state, additional lock states
22642 ** are inserted in between.  The locking might fail on one of the later
22643 ** transitions leaving the lock state different from what it started but
22644 ** still short of its goal.  The following chart shows the allowed
22645 ** transitions and the inserted intermediate states:
22646 **
22647 **    UNLOCKED -> SHARED
22648 **    SHARED -> RESERVED
22649 **    SHARED -> (PENDING) -> EXCLUSIVE
22650 **    RESERVED -> (PENDING) -> EXCLUSIVE
22651 **    PENDING -> EXCLUSIVE
22652 **
22653 ** This routine will only increase a lock.  The os2Unlock() routine
22654 ** erases all locks at once and returns us immediately to locking level 0.
22655 ** It is not possible to lower the locking level one step at a time.  You
22656 ** must go straight to locking level 0.
22657 */
22658 static int os2Lock( sqlite3_file *id, int locktype ){
22659   int rc = SQLITE_OK;       /* Return code from subroutines */
22660   APIRET res = NO_ERROR;    /* Result of an OS/2 lock call */
22661   int newLocktype;       /* Set pFile->locktype to this value before exiting */
22662   int gotPendingLock = 0;/* True if we acquired a PENDING lock this time */
22663   FILELOCK  LockArea,
22664             UnlockArea;
22665   os2File *pFile = (os2File*)id;
22666   memset(&LockArea, 0, sizeof(LockArea));
22667   memset(&UnlockArea, 0, sizeof(UnlockArea));
22668   assert( pFile!=0 );
22669   OSTRACE(( "LOCK %d %d was %d\n", pFile->h, locktype, pFile->locktype ));
22670 
22671   /* If there is already a lock of this type or more restrictive on the
22672   ** os2File, do nothing. Don't use the end_lock: exit path, as
22673   ** sqlite3_mutex_enter() hasn't been called yet.
22674   */
22675   if( pFile->locktype>=locktype ){
22676     OSTRACE(( "LOCK %d %d ok (already held)\n", pFile->h, locktype ));
22677     return SQLITE_OK;
22678   }
22679 
22680   /* Make sure the locking sequence is correct
22681   */
22682   assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
22683   assert( locktype!=PENDING_LOCK );
22684   assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
22685 
22686   /* Lock the PENDING_LOCK byte if we need to acquire a PENDING lock or
22687   ** a SHARED lock.  If we are acquiring a SHARED lock, the acquisition of
22688   ** the PENDING_LOCK byte is temporary.
22689   */
22690   newLocktype = pFile->locktype;
22691   if( pFile->locktype==NO_LOCK
22692       || (locktype==EXCLUSIVE_LOCK && pFile->locktype==RESERVED_LOCK)
22693   ){
22694     LockArea.lOffset = PENDING_BYTE;
22695     LockArea.lRange = 1L;
22696     UnlockArea.lOffset = 0L;
22697     UnlockArea.lRange = 0L;
22698 
22699     /* wait longer than LOCK_TIMEOUT here not to have to try multiple times */
22700     res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, 100L, 0L );
22701     if( res == NO_ERROR ){
22702       gotPendingLock = 1;
22703       OSTRACE(( "LOCK %d pending lock boolean set.  res=%d\n", pFile->h, res ));
22704     }
22705   }
22706 
22707   /* Acquire a shared lock
22708   */
22709   if( locktype==SHARED_LOCK && res == NO_ERROR ){
22710     assert( pFile->locktype==NO_LOCK );
22711     res = getReadLock(pFile);
22712     if( res == NO_ERROR ){
22713       newLocktype = SHARED_LOCK;
22714     }
22715     OSTRACE(( "LOCK %d acquire shared lock. res=%d\n", pFile->h, res ));
22716   }
22717 
22718   /* Acquire a RESERVED lock
22719   */
22720   if( locktype==RESERVED_LOCK && res == NO_ERROR ){
22721     assert( pFile->locktype==SHARED_LOCK );
22722     LockArea.lOffset = RESERVED_BYTE;
22723     LockArea.lRange = 1L;
22724     UnlockArea.lOffset = 0L;
22725     UnlockArea.lRange = 0L;
22726     res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
22727     if( res == NO_ERROR ){
22728       newLocktype = RESERVED_LOCK;
22729     }
22730     OSTRACE(( "LOCK %d acquire reserved lock. res=%d\n", pFile->h, res ));
22731   }
22732 
22733   /* Acquire a PENDING lock
22734   */
22735   if( locktype==EXCLUSIVE_LOCK && res == NO_ERROR ){
22736     newLocktype = PENDING_LOCK;
22737     gotPendingLock = 0;
22738     OSTRACE(( "LOCK %d acquire pending lock. pending lock boolean unset.\n",
22739                pFile->h ));
22740   }
22741 
22742   /* Acquire an EXCLUSIVE lock
22743   */
22744   if( locktype==EXCLUSIVE_LOCK && res == NO_ERROR ){
22745     assert( pFile->locktype>=SHARED_LOCK );
22746     res = unlockReadLock(pFile);
22747     OSTRACE(( "unreadlock = %d\n", res ));
22748     LockArea.lOffset = SHARED_FIRST;
22749     LockArea.lRange = SHARED_SIZE;
22750     UnlockArea.lOffset = 0L;
22751     UnlockArea.lRange = 0L;
22752     res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
22753     if( res == NO_ERROR ){
22754       newLocktype = EXCLUSIVE_LOCK;
22755     }else{
22756       OSTRACE(( "OS/2 error-code = %d\n", res ));
22757       getReadLock(pFile);
22758     }
22759     OSTRACE(( "LOCK %d acquire exclusive lock.  res=%d\n", pFile->h, res ));
22760   }
22761 
22762   /* If we are holding a PENDING lock that ought to be released, then
22763   ** release it now.
22764   */
22765   if( gotPendingLock && locktype==SHARED_LOCK ){
22766     int r;
22767     LockArea.lOffset = 0L;
22768     LockArea.lRange = 0L;
22769     UnlockArea.lOffset = PENDING_BYTE;
22770     UnlockArea.lRange = 1L;
22771     r = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
22772     OSTRACE(( "LOCK %d unlocking pending/is shared. r=%d\n", pFile->h, r ));
22773   }
22774 
22775   /* Update the state of the lock has held in the file descriptor then
22776   ** return the appropriate result code.
22777   */
22778   if( res == NO_ERROR ){
22779     rc = SQLITE_OK;
22780   }else{
22781     OSTRACE(( "LOCK FAILED %d trying for %d but got %d\n", pFile->h,
22782               locktype, newLocktype ));
22783     rc = SQLITE_BUSY;
22784   }
22785   pFile->locktype = newLocktype;
22786   OSTRACE(( "LOCK %d now %d\n", pFile->h, pFile->locktype ));
22787   return rc;
22788 }
22789 
22790 /*
22791 ** This routine checks if there is a RESERVED lock held on the specified
22792 ** file by this or any other process. If such a lock is held, return
22793 ** non-zero, otherwise zero.
22794 */
22795 static int os2CheckReservedLock( sqlite3_file *id, int *pOut ){
22796   int r = 0;
22797   os2File *pFile = (os2File*)id;
22798   assert( pFile!=0 );
22799   if( pFile->locktype>=RESERVED_LOCK ){
22800     r = 1;
22801     OSTRACE(( "TEST WR-LOCK %d %d (local)\n", pFile->h, r ));
22802   }else{
22803     FILELOCK  LockArea,
22804               UnlockArea;
22805     APIRET rc = NO_ERROR;
22806     memset(&LockArea, 0, sizeof(LockArea));
22807     memset(&UnlockArea, 0, sizeof(UnlockArea));
22808     LockArea.lOffset = RESERVED_BYTE;
22809     LockArea.lRange = 1L;
22810     UnlockArea.lOffset = 0L;
22811     UnlockArea.lRange = 0L;
22812     rc = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
22813     OSTRACE(( "TEST WR-LOCK %d lock reserved byte rc=%d\n", pFile->h, rc ));
22814     if( rc == NO_ERROR ){
22815       APIRET rcu = NO_ERROR; /* return code for unlocking */
22816       LockArea.lOffset = 0L;
22817       LockArea.lRange = 0L;
22818       UnlockArea.lOffset = RESERVED_BYTE;
22819       UnlockArea.lRange = 1L;
22820       rcu = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
22821       OSTRACE(( "TEST WR-LOCK %d unlock reserved byte r=%d\n", pFile->h, rcu ));
22822     }
22823     r = !(rc == NO_ERROR);
22824     OSTRACE(( "TEST WR-LOCK %d %d (remote)\n", pFile->h, r ));
22825   }
22826   *pOut = r;
22827   return SQLITE_OK;
22828 }
22829 
22830 /*
22831 ** Lower the locking level on file descriptor id to locktype.  locktype
22832 ** must be either NO_LOCK or SHARED_LOCK.
22833 **
22834 ** If the locking level of the file descriptor is already at or below
22835 ** the requested locking level, this routine is a no-op.
22836 **
22837 ** It is not possible for this routine to fail if the second argument
22838 ** is NO_LOCK.  If the second argument is SHARED_LOCK then this routine
22839 ** might return SQLITE_IOERR;
22840 */
22841 static int os2Unlock( sqlite3_file *id, int locktype ){
22842   int type;
22843   os2File *pFile = (os2File*)id;
22844   APIRET rc = SQLITE_OK;
22845   APIRET res = NO_ERROR;
22846   FILELOCK  LockArea,
22847             UnlockArea;
22848   memset(&LockArea, 0, sizeof(LockArea));
22849   memset(&UnlockArea, 0, sizeof(UnlockArea));
22850   assert( pFile!=0 );
22851   assert( locktype<=SHARED_LOCK );
22852   OSTRACE(( "UNLOCK %d to %d was %d\n", pFile->h, locktype, pFile->locktype ));
22853   type = pFile->locktype;
22854   if( type>=EXCLUSIVE_LOCK ){
22855     LockArea.lOffset = 0L;
22856     LockArea.lRange = 0L;
22857     UnlockArea.lOffset = SHARED_FIRST;
22858     UnlockArea.lRange = SHARED_SIZE;
22859     res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
22860     OSTRACE(( "UNLOCK %d exclusive lock res=%d\n", pFile->h, res ));
22861     if( locktype==SHARED_LOCK && getReadLock(pFile) != NO_ERROR ){
22862       /* This should never happen.  We should always be able to
22863       ** reacquire the read lock */
22864       OSTRACE(( "UNLOCK %d to %d getReadLock() failed\n", pFile->h, locktype ));
22865       rc = SQLITE_IOERR_UNLOCK;
22866     }
22867   }
22868   if( type>=RESERVED_LOCK ){
22869     LockArea.lOffset = 0L;
22870     LockArea.lRange = 0L;
22871     UnlockArea.lOffset = RESERVED_BYTE;
22872     UnlockArea.lRange = 1L;
22873     res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
22874     OSTRACE(( "UNLOCK %d reserved res=%d\n", pFile->h, res ));
22875   }
22876   if( locktype==NO_LOCK && type>=SHARED_LOCK ){
22877     res = unlockReadLock(pFile);
22878     OSTRACE(( "UNLOCK %d is %d want %d res=%d\n",
22879               pFile->h, type, locktype, res ));
22880   }
22881   if( type>=PENDING_LOCK ){
22882     LockArea.lOffset = 0L;
22883     LockArea.lRange = 0L;
22884     UnlockArea.lOffset = PENDING_BYTE;
22885     UnlockArea.lRange = 1L;
22886     res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
22887     OSTRACE(( "UNLOCK %d pending res=%d\n", pFile->h, res ));
22888   }
22889   pFile->locktype = locktype;
22890   OSTRACE(( "UNLOCK %d now %d\n", pFile->h, pFile->locktype ));
22891   return rc;
22892 }
22893 
22894 /*
22895 ** Control and query of the open file handle.
22896 */
22897 static int os2FileControl(sqlite3_file *id, int op, void *pArg){
22898   switch( op ){
22899     case SQLITE_FCNTL_LOCKSTATE: {
22900       *(int*)pArg = ((os2File*)id)->locktype;
22901       OSTRACE(( "FCNTL_LOCKSTATE %d lock=%d\n",
22902                 ((os2File*)id)->h, ((os2File*)id)->locktype ));
22903       return SQLITE_OK;
22904     }
22905     case SQLITE_FCNTL_CHUNK_SIZE: {
22906       ((os2File*)id)->szChunk = *(int*)pArg;
22907       return SQLITE_OK;
22908     }
22909     case SQLITE_FCNTL_SIZE_HINT: {
22910       sqlite3_int64 sz = *(sqlite3_int64*)pArg;
22911       SimulateIOErrorBenign(1);
22912       os2Truncate(id, sz);
22913       SimulateIOErrorBenign(0);
22914       return SQLITE_OK;
22915     }
22916     case SQLITE_FCNTL_SYNC_OMITTED: {
22917       return SQLITE_OK;
22918     }
22919   }
22920   return SQLITE_NOTFOUND;
22921 }
22922 
22923 /*
22924 ** Return the sector size in bytes of the underlying block device for
22925 ** the specified file. This is almost always 512 bytes, but may be
22926 ** larger for some devices.
22927 **
22928 ** SQLite code assumes this function cannot fail. It also assumes that
22929 ** if two files are created in the same file-system directory (i.e.
22930 ** a database and its journal file) that the sector size will be the
22931 ** same for both.
22932 */
22933 static int os2SectorSize(sqlite3_file *id){
22934   UNUSED_PARAMETER(id);
22935   return SQLITE_DEFAULT_SECTOR_SIZE;
22936 }
22937 
22938 /*
22939 ** Return a vector of device characteristics.
22940 */
22941 static int os2DeviceCharacteristics(sqlite3_file *id){
22942   UNUSED_PARAMETER(id);
22943   return SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN;
22944 }
22945 
22946 
22947 /*
22948 ** Character set conversion objects used by conversion routines.
22949 */
22950 static UconvObject ucUtf8 = NULL; /* convert between UTF-8 and UCS-2 */
22951 static UconvObject uclCp = NULL;  /* convert between local codepage and UCS-2 */
22952 
22953 /*
22954 ** Helper function to initialize the conversion objects from and to UTF-8.
22955 */
22956 static void initUconvObjects( void ){
22957   if( UniCreateUconvObject( UTF_8, &ucUtf8 ) != ULS_SUCCESS )
22958     ucUtf8 = NULL;
22959   if ( UniCreateUconvObject( (UniChar *)L"@path=yes", &uclCp ) != ULS_SUCCESS )
22960     uclCp = NULL;
22961 }
22962 
22963 /*
22964 ** Helper function to free the conversion objects from and to UTF-8.
22965 */
22966 static void freeUconvObjects( void ){
22967   if ( ucUtf8 )
22968     UniFreeUconvObject( ucUtf8 );
22969   if ( uclCp )
22970     UniFreeUconvObject( uclCp );
22971   ucUtf8 = NULL;
22972   uclCp = NULL;
22973 }
22974 
22975 /*
22976 ** Helper function to convert UTF-8 filenames to local OS/2 codepage.
22977 ** The two-step process: first convert the incoming UTF-8 string
22978 ** into UCS-2 and then from UCS-2 to the current codepage.
22979 ** The returned char pointer has to be freed.
22980 */
22981 static char *convertUtf8PathToCp( const char *in ){
22982   UniChar tempPath[CCHMAXPATH];
22983   char *out = (char *)calloc( CCHMAXPATH, 1 );
22984 
22985   if( !out )
22986     return NULL;
22987 
22988   if( !ucUtf8 || !uclCp )
22989     initUconvObjects();
22990 
22991   /* determine string for the conversion of UTF-8 which is CP1208 */
22992   if( UniStrToUcs( ucUtf8, tempPath, (char *)in, CCHMAXPATH ) != ULS_SUCCESS )
22993     return out; /* if conversion fails, return the empty string */
22994 
22995   /* conversion for current codepage which can be used for paths */
22996   UniStrFromUcs( uclCp, out, tempPath, CCHMAXPATH );
22997 
22998   return out;
22999 }
23000 
23001 /*
23002 ** Helper function to convert filenames from local codepage to UTF-8.
23003 ** The two-step process: first convert the incoming codepage-specific
23004 ** string into UCS-2 and then from UCS-2 to the codepage of UTF-8.
23005 ** The returned char pointer has to be freed.
23006 **
23007 ** This function is non-static to be able to use this in shell.c and
23008 ** similar applications that take command line arguments.
23009 */
23010 char *convertCpPathToUtf8( const char *in ){
23011   UniChar tempPath[CCHMAXPATH];
23012   char *out = (char *)calloc( CCHMAXPATH, 1 );
23013 
23014   if( !out )
23015     return NULL;
23016 
23017   if( !ucUtf8 || !uclCp )
23018     initUconvObjects();
23019 
23020   /* conversion for current codepage which can be used for paths */
23021   if( UniStrToUcs( uclCp, tempPath, (char *)in, CCHMAXPATH ) != ULS_SUCCESS )
23022     return out; /* if conversion fails, return the empty string */
23023 
23024   /* determine string for the conversion of UTF-8 which is CP1208 */
23025   UniStrFromUcs( ucUtf8, out, tempPath, CCHMAXPATH );
23026 
23027   return out;
23028 }
23029 
23030 
23031 #ifndef SQLITE_OMIT_WAL
23032 
23033 /*
23034 ** Use main database file for interprocess locking. If un-defined
23035 ** a separate file is created for this purpose. The file will be
23036 ** used only to set file locks. There will be no data written to it.
23037 */
23038 #define SQLITE_OS2_NO_WAL_LOCK_FILE
23039 
23040 #if 0
23041 static void _ERR_TRACE( const char *fmt, ... ) {
23042   va_list  ap;
23043   va_start(ap, fmt);
23044   vfprintf(stderr, fmt, ap);
23045   fflush(stderr);
23046 }
23047 #define ERR_TRACE(rc, msg)        \
23048         if( (rc) != SQLITE_OK ) _ERR_TRACE msg;
23049 #else
23050 #define ERR_TRACE(rc, msg)
23051 #endif
23052 
23053 /*
23054 ** Helper functions to obtain and relinquish the global mutex. The
23055 ** global mutex is used to protect os2ShmNodeList.
23056 **
23057 ** Function os2ShmMutexHeld() is used to assert() that the global mutex
23058 ** is held when required. This function is only used as part of assert()
23059 ** statements. e.g.
23060 **
23061 **   os2ShmEnterMutex()
23062 **     assert( os2ShmMutexHeld() );
23063 **   os2ShmLeaveMutex()
23064 */
23065 static void os2ShmEnterMutex(void){
23066   sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
23067 }
23068 static void os2ShmLeaveMutex(void){
23069   sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
23070 }
23071 #ifdef SQLITE_DEBUG
23072 static int os2ShmMutexHeld(void) {
23073   return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
23074 }
23075 int GetCurrentProcessId(void) {
23076   PPIB pib;
23077   DosGetInfoBlocks(NULL, &pib);
23078   return (int)pib->pib_ulpid;
23079 }
23080 #endif
23081 
23082 /*
23083 ** Object used to represent a the shared memory area for a single log file.
23084 ** When multiple threads all reference the same log-summary, each thread has
23085 ** its own os2File object, but they all point to a single instance of this
23086 ** object.  In other words, each log-summary is opened only once per process.
23087 **
23088 ** os2ShmMutexHeld() must be true when creating or destroying
23089 ** this object or while reading or writing the following fields:
23090 **
23091 **      nRef
23092 **      pNext
23093 **
23094 ** The following fields are read-only after the object is created:
23095 **
23096 **      szRegion
23097 **      hLockFile
23098 **      shmBaseName
23099 **
23100 ** Either os2ShmNode.mutex must be held or os2ShmNode.nRef==0 and
23101 ** os2ShmMutexHeld() is true when reading or writing any other field
23102 ** in this structure.
23103 **
23104 */
23105 struct os2ShmNode {
23106   sqlite3_mutex *mutex;      /* Mutex to access this object */
23107   os2ShmNode *pNext;         /* Next in list of all os2ShmNode objects */
23108 
23109   int szRegion;              /* Size of shared-memory regions */
23110 
23111   int nRegion;               /* Size of array apRegion */
23112   void **apRegion;           /* Array of pointers to shared-memory regions */
23113 
23114   int nRef;                  /* Number of os2ShmLink objects pointing to this */
23115   os2ShmLink *pFirst;        /* First os2ShmLink object pointing to this */
23116 
23117   HFILE hLockFile;           /* File used for inter-process memory locking */
23118   char shmBaseName[1];       /* Name of the memory object !!! must last !!! */
23119 };
23120 
23121 
23122 /*
23123 ** Structure used internally by this VFS to record the state of an
23124 ** open shared memory connection.
23125 **
23126 ** The following fields are initialized when this object is created and
23127 ** are read-only thereafter:
23128 **
23129 **    os2Shm.pShmNode
23130 **    os2Shm.id
23131 **
23132 ** All other fields are read/write.  The os2Shm.pShmNode->mutex must be held
23133 ** while accessing any read/write fields.
23134 */
23135 struct os2ShmLink {
23136   os2ShmNode *pShmNode;      /* The underlying os2ShmNode object */
23137   os2ShmLink *pNext;         /* Next os2Shm with the same os2ShmNode */
23138   u32 sharedMask;            /* Mask of shared locks held */
23139   u32 exclMask;              /* Mask of exclusive locks held */
23140 #ifdef SQLITE_DEBUG
23141   u8 id;                     /* Id of this connection with its os2ShmNode */
23142 #endif
23143 };
23144 
23145 
23146 /*
23147 ** A global list of all os2ShmNode objects.
23148 **
23149 ** The os2ShmMutexHeld() must be true while reading or writing this list.
23150 */
23151 static os2ShmNode *os2ShmNodeList = NULL;
23152 
23153 /*
23154 ** Constants used for locking
23155 */
23156 #ifdef  SQLITE_OS2_NO_WAL_LOCK_FILE
23157 #define OS2_SHM_BASE   (PENDING_BYTE + 0x10000)         /* first lock byte */
23158 #else
23159 #define OS2_SHM_BASE   ((22+SQLITE_SHM_NLOCK)*4)        /* first lock byte */
23160 #endif
23161 
23162 #define OS2_SHM_DMS    (OS2_SHM_BASE+SQLITE_SHM_NLOCK)  /* deadman switch */
23163 
23164 /*
23165 ** Apply advisory locks for all n bytes beginning at ofst.
23166 */
23167 #define _SHM_UNLCK  1   /* no lock */
23168 #define _SHM_RDLCK  2   /* shared lock, no wait */
23169 #define _SHM_WRLCK  3   /* exlusive lock, no wait */
23170 #define _SHM_WRLCK_WAIT 4 /* exclusive lock, wait */
23171 static int os2ShmSystemLock(
23172   os2ShmNode *pNode,    /* Apply locks to this open shared-memory segment */
23173   int lockType,         /* _SHM_UNLCK, _SHM_RDLCK, _SHM_WRLCK or _SHM_WRLCK_WAIT */
23174   int ofst,             /* Offset to first byte to be locked/unlocked */
23175   int nByte             /* Number of bytes to lock or unlock */
23176 ){
23177   APIRET rc;
23178   FILELOCK area;
23179   ULONG mode, timeout;
23180 
23181   /* Access to the os2ShmNode object is serialized by the caller */
23182   assert( sqlite3_mutex_held(pNode->mutex) || pNode->nRef==0 );
23183 
23184   mode = 1;     /* shared lock */
23185   timeout = 0;  /* no wait */
23186   area.lOffset = ofst;
23187   area.lRange = nByte;
23188 
23189   switch( lockType ) {
23190     case _SHM_WRLCK_WAIT:
23191       timeout = (ULONG)-1;      /* wait forever */
23192     case _SHM_WRLCK:
23193       mode = 0;                 /* exclusive lock */
23194     case _SHM_RDLCK:
23195       rc = DosSetFileLocks(pNode->hLockFile,
23196                            NULL, &area, timeout, mode);
23197       break;
23198     /* case _SHM_UNLCK: */
23199     default:
23200       rc = DosSetFileLocks(pNode->hLockFile,
23201                            &area, NULL, 0, 0);
23202       break;
23203   }
23204 
23205   OSTRACE(("SHM-LOCK %d %s %s 0x%08lx\n",
23206            pNode->hLockFile,
23207            rc==SQLITE_OK ? "ok" : "failed",
23208            lockType==_SHM_UNLCK ? "Unlock" : "Lock",
23209            rc));
23210 
23211   ERR_TRACE(rc, ("os2ShmSystemLock: %d %s\n", rc, pNode->shmBaseName))
23212 
23213   return ( rc == 0 ) ?  SQLITE_OK : SQLITE_BUSY;
23214 }
23215 
23216 /*
23217 ** Find an os2ShmNode in global list or allocate a new one, if not found.
23218 **
23219 ** This is not a VFS shared-memory method; it is a utility function called
23220 ** by VFS shared-memory methods.
23221 */
23222 static int os2OpenSharedMemory( os2File *fd, int szRegion ) {
23223   os2ShmLink *pLink;
23224   os2ShmNode *pNode;
23225   int cbShmName, rc = SQLITE_OK;
23226   char shmName[CCHMAXPATH + 30];
23227 #ifndef SQLITE_OS2_NO_WAL_LOCK_FILE
23228   ULONG action;
23229 #endif
23230 
23231   /* We need some additional space at the end to append the region number */
23232   cbShmName = sprintf(shmName, "\\SHAREMEM\\%s", fd->zFullPathCp );
23233   if( cbShmName >= CCHMAXPATH-8 )
23234     return SQLITE_IOERR_SHMOPEN;
23235 
23236   /* Replace colon in file name to form a valid shared memory name */
23237   shmName[10+1] = '!';
23238 
23239   /* Allocate link object (we free it later in case of failure) */
23240   pLink = sqlite3_malloc( sizeof(*pLink) );
23241   if( !pLink )
23242     return SQLITE_NOMEM;
23243 
23244   /* Access node list */
23245   os2ShmEnterMutex();
23246 
23247   /* Find node by it's shared memory base name */
23248   for( pNode = os2ShmNodeList;
23249        pNode && stricmp(shmName, pNode->shmBaseName) != 0;
23250        pNode = pNode->pNext )   ;
23251 
23252   /* Not found: allocate a new node */
23253   if( !pNode ) {
23254     pNode = sqlite3_malloc( sizeof(*pNode) + cbShmName );
23255     if( pNode ) {
23256       memset(pNode, 0, sizeof(*pNode) );
23257       pNode->szRegion = szRegion;
23258       pNode->hLockFile = (HFILE)-1;
23259       strcpy(pNode->shmBaseName, shmName);
23260 
23261 #ifdef SQLITE_OS2_NO_WAL_LOCK_FILE
23262       if( DosDupHandle(fd->h, &pNode->hLockFile) != 0 ) {
23263 #else
23264       sprintf(shmName, "%s-lck", fd->zFullPathCp);
23265       if( DosOpen((PSZ)shmName, &pNode->hLockFile, &action, 0, FILE_NORMAL,
23266                   OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_CREATE_IF_NEW,
23267                   OPEN_ACCESS_READWRITE | OPEN_SHARE_DENYNONE |
23268                   OPEN_FLAGS_NOINHERIT | OPEN_FLAGS_FAIL_ON_ERROR,
23269                   NULL) != 0 ) {
23270 #endif
23271         sqlite3_free(pNode);
23272         rc = SQLITE_IOERR;
23273       } else {
23274         pNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
23275         if( !pNode->mutex ) {
23276           sqlite3_free(pNode);
23277           rc = SQLITE_NOMEM;
23278         }
23279       }
23280     } else {
23281       rc = SQLITE_NOMEM;
23282     }
23283 
23284     if( rc == SQLITE_OK ) {
23285       pNode->pNext = os2ShmNodeList;
23286       os2ShmNodeList = pNode;
23287     } else {
23288       pNode = NULL;
23289     }
23290   } else if( pNode->szRegion != szRegion ) {
23291     rc = SQLITE_IOERR_SHMSIZE;
23292     pNode = NULL;
23293   }
23294 
23295   if( pNode ) {
23296     sqlite3_mutex_enter(pNode->mutex);
23297 
23298     memset(pLink, 0, sizeof(*pLink));
23299 
23300     pLink->pShmNode = pNode;
23301     pLink->pNext = pNode->pFirst;
23302     pNode->pFirst = pLink;
23303     pNode->nRef++;
23304 
23305     fd->pShmLink = pLink;
23306 
23307     sqlite3_mutex_leave(pNode->mutex);
23308 
23309   } else {
23310     /* Error occured. Free our link object. */
23311     sqlite3_free(pLink);
23312   }
23313 
23314   os2ShmLeaveMutex();
23315 
23316   ERR_TRACE(rc, ("os2OpenSharedMemory: %d  %s\n", rc, fd->zFullPathCp))
23317 
23318   return rc;
23319 }
23320 
23321 /*
23322 ** Purge the os2ShmNodeList list of all entries with nRef==0.
23323 **
23324 ** This is not a VFS shared-memory method; it is a utility function called
23325 ** by VFS shared-memory methods.
23326 */
23327 static void os2PurgeShmNodes( int deleteFlag ) {
23328   os2ShmNode *pNode;
23329   os2ShmNode **ppNode;
23330 
23331   os2ShmEnterMutex();
23332 
23333   ppNode = &os2ShmNodeList;
23334 
23335   while( *ppNode ) {
23336     pNode = *ppNode;
23337 
23338     if( pNode->nRef == 0 ) {
23339       *ppNode = pNode->pNext;
23340 
23341       if( pNode->apRegion ) {
23342         /* Prevent other processes from resizing the shared memory */
23343         os2ShmSystemLock(pNode, _SHM_WRLCK_WAIT, OS2_SHM_DMS, 1);
23344 
23345         while( pNode->nRegion-- ) {
23346 #ifdef SQLITE_DEBUG
23347           int rc =
23348 #endif
23349           DosFreeMem(pNode->apRegion[pNode->nRegion]);
23350 
23351           OSTRACE(("SHM-PURGE pid-%d unmap region=%d %s\n",
23352                   (int)GetCurrentProcessId(), pNode->nRegion,
23353                   rc == 0 ? "ok" : "failed"));
23354         }
23355 
23356         /* Allow other processes to resize the shared memory */
23357         os2ShmSystemLock(pNode, _SHM_UNLCK, OS2_SHM_DMS, 1);
23358 
23359         sqlite3_free(pNode->apRegion);
23360       }
23361 
23362       DosClose(pNode->hLockFile);
23363 
23364 #ifndef SQLITE_OS2_NO_WAL_LOCK_FILE
23365       if( deleteFlag ) {
23366          char fileName[CCHMAXPATH];
23367          /* Skip "\\SHAREMEM\\" */
23368          sprintf(fileName, "%s-lck", pNode->shmBaseName + 10);
23369          /* restore colon */
23370          fileName[1] = ':';
23371 
23372          DosForceDelete(fileName);
23373       }
23374 #endif
23375 
23376       sqlite3_mutex_free(pNode->mutex);
23377 
23378       sqlite3_free(pNode);
23379 
23380     } else {
23381       ppNode = &pNode->pNext;
23382     }
23383   }
23384 
23385   os2ShmLeaveMutex();
23386 }
23387 
23388 /*
23389 ** This function is called to obtain a pointer to region iRegion of the
23390 ** shared-memory associated with the database file id. Shared-memory regions
23391 ** are numbered starting from zero. Each shared-memory region is szRegion
23392 ** bytes in size.
23393 **
23394 ** If an error occurs, an error code is returned and *pp is set to NULL.
23395 **
23396 ** Otherwise, if the bExtend parameter is 0 and the requested shared-memory
23397 ** region has not been allocated (by any client, including one running in a
23398 ** separate process), then *pp is set to NULL and SQLITE_OK returned. If
23399 ** bExtend is non-zero and the requested shared-memory region has not yet
23400 ** been allocated, it is allocated by this function.
23401 **
23402 ** If the shared-memory region has already been allocated or is allocated by
23403 ** this call as described above, then it is mapped into this processes
23404 ** address space (if it is not already), *pp is set to point to the mapped
23405 ** memory and SQLITE_OK returned.
23406 */
23407 static int os2ShmMap(
23408   sqlite3_file *id,               /* Handle open on database file */
23409   int iRegion,                    /* Region to retrieve */
23410   int szRegion,                   /* Size of regions */
23411   int bExtend,                    /* True to extend block if necessary */
23412   void volatile **pp              /* OUT: Mapped memory */
23413 ){
23414   PVOID pvTemp;
23415   void **apRegion;
23416   os2ShmNode *pNode;
23417   int n, rc = SQLITE_OK;
23418   char shmName[CCHMAXPATH];
23419   os2File *pFile = (os2File*)id;
23420 
23421   *pp = NULL;
23422 
23423   if( !pFile->pShmLink )
23424     rc = os2OpenSharedMemory( pFile, szRegion );
23425 
23426   if( rc == SQLITE_OK ) {
23427     pNode = pFile->pShmLink->pShmNode ;
23428 
23429     sqlite3_mutex_enter(pNode->mutex);
23430 
23431     assert( szRegion==pNode->szRegion );
23432 
23433     /* Unmapped region ? */
23434     if( iRegion >= pNode->nRegion ) {
23435       /* Prevent other processes from resizing the shared memory */
23436       os2ShmSystemLock(pNode, _SHM_WRLCK_WAIT, OS2_SHM_DMS, 1);
23437 
23438       apRegion = sqlite3_realloc(
23439         pNode->apRegion, (iRegion + 1) * sizeof(apRegion[0]));
23440 
23441       if( apRegion ) {
23442         pNode->apRegion = apRegion;
23443 
23444         while( pNode->nRegion <= iRegion ) {
23445           sprintf(shmName, "%s-%u",
23446                   pNode->shmBaseName, pNode->nRegion);
23447 
23448           if( DosGetNamedSharedMem(&pvTemp, (PSZ)shmName,
23449                 PAG_READ | PAG_WRITE) != NO_ERROR ) {
23450             if( !bExtend )
23451               break;
23452 
23453             if( DosAllocSharedMem(&pvTemp, (PSZ)shmName, szRegion,
23454                   PAG_READ | PAG_WRITE | PAG_COMMIT | OBJ_ANY) != NO_ERROR &&
23455                 DosAllocSharedMem(&pvTemp, (PSZ)shmName, szRegion,
23456                   PAG_READ | PAG_WRITE | PAG_COMMIT) != NO_ERROR ) {
23457               rc = SQLITE_NOMEM;
23458               break;
23459             }
23460           }
23461 
23462           apRegion[pNode->nRegion++] = pvTemp;
23463         }
23464 
23465         /* zero out remaining entries */
23466         for( n = pNode->nRegion; n <= iRegion; n++ )
23467           pNode->apRegion[n] = NULL;
23468 
23469         /* Return this region (maybe zero) */
23470         *pp = pNode->apRegion[iRegion];
23471       } else {
23472         rc = SQLITE_NOMEM;
23473       }
23474 
23475       /* Allow other processes to resize the shared memory */
23476       os2ShmSystemLock(pNode, _SHM_UNLCK, OS2_SHM_DMS, 1);
23477 
23478     } else {
23479       /* Region has been mapped previously */
23480       *pp = pNode->apRegion[iRegion];
23481     }
23482 
23483     sqlite3_mutex_leave(pNode->mutex);
23484   }
23485 
23486   ERR_TRACE(rc, ("os2ShmMap: %s iRgn = %d, szRgn = %d, bExt = %d : %d\n",
23487                  pFile->zFullPathCp, iRegion, szRegion, bExtend, rc))
23488 
23489   return rc;
23490 }
23491 
23492 /*
23493 ** Close a connection to shared-memory.  Delete the underlying
23494 ** storage if deleteFlag is true.
23495 **
23496 ** If there is no shared memory associated with the connection then this
23497 ** routine is a harmless no-op.
23498 */
23499 static int os2ShmUnmap(
23500   sqlite3_file *id,               /* The underlying database file */
23501   int deleteFlag                  /* Delete shared-memory if true */
23502 ){
23503   os2File *pFile = (os2File*)id;
23504   os2ShmLink *pLink = pFile->pShmLink;
23505 
23506   if( pLink ) {
23507     int nRef = -1;
23508     os2ShmLink **ppLink;
23509     os2ShmNode *pNode = pLink->pShmNode;
23510 
23511     sqlite3_mutex_enter(pNode->mutex);
23512 
23513     for( ppLink = &pNode->pFirst;
23514          *ppLink && *ppLink != pLink;
23515          ppLink = &(*ppLink)->pNext )   ;
23516 
23517     assert(*ppLink);
23518 
23519     if( *ppLink ) {
23520       *ppLink = pLink->pNext;
23521       nRef = --pNode->nRef;
23522     } else {
23523       ERR_TRACE(1, ("os2ShmUnmap: link not found ! %s\n",
23524                     pNode->shmBaseName))
23525     }
23526 
23527     pFile->pShmLink = NULL;
23528     sqlite3_free(pLink);
23529 
23530     sqlite3_mutex_leave(pNode->mutex);
23531 
23532     if( nRef == 0 )
23533       os2PurgeShmNodes( deleteFlag );
23534   }
23535 
23536   return SQLITE_OK;
23537 }
23538 
23539 /*
23540 ** Change the lock state for a shared-memory segment.
23541 **
23542 ** Note that the relationship between SHAREd and EXCLUSIVE locks is a little
23543 ** different here than in posix.  In xShmLock(), one can go from unlocked
23544 ** to shared and back or from unlocked to exclusive and back.  But one may
23545 ** not go from shared to exclusive or from exclusive to shared.
23546 */
23547 static int os2ShmLock(
23548   sqlite3_file *id,          /* Database file holding the shared memory */
23549   int ofst,                  /* First lock to acquire or release */
23550   int n,                     /* Number of locks to acquire or release */
23551   int flags                  /* What to do with the lock */
23552 ){
23553   u32 mask;                             /* Mask of locks to take or release */
23554   int rc = SQLITE_OK;                   /* Result code */
23555   os2File *pFile = (os2File*)id;
23556   os2ShmLink *p = pFile->pShmLink;      /* The shared memory being locked */
23557   os2ShmLink *pX;                       /* For looping over all siblings */
23558   os2ShmNode *pShmNode = p->pShmNode;   /* Our node */
23559 
23560   assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK );
23561   assert( n>=1 );
23562   assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED)
23563        || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE)
23564        || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED)
23565        || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) );
23566   assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 );
23567 
23568   mask = (u32)((1U<<(ofst+n)) - (1U<<ofst));
23569   assert( n>1 || mask==(1<<ofst) );
23570 
23571 
23572   sqlite3_mutex_enter(pShmNode->mutex);
23573 
23574   if( flags & SQLITE_SHM_UNLOCK ){
23575     u32 allMask = 0; /* Mask of locks held by siblings */
23576 
23577     /* See if any siblings hold this same lock */
23578     for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
23579       if( pX==p ) continue;
23580       assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 );
23581       allMask |= pX->sharedMask;
23582     }
23583 
23584     /* Unlock the system-level locks */
23585     if( (mask & allMask)==0 ){
23586       rc = os2ShmSystemLock(pShmNode, _SHM_UNLCK, ofst+OS2_SHM_BASE, n);
23587     }else{
23588       rc = SQLITE_OK;
23589     }
23590 
23591     /* Undo the local locks */
23592     if( rc==SQLITE_OK ){
23593       p->exclMask &= ~mask;
23594       p->sharedMask &= ~mask;
23595     }
23596   }else if( flags & SQLITE_SHM_SHARED ){
23597     u32 allShared = 0;  /* Union of locks held by connections other than "p" */
23598 
23599     /* Find out which shared locks are already held by sibling connections.
23600     ** If any sibling already holds an exclusive lock, go ahead and return
23601     ** SQLITE_BUSY.
23602     */
23603     for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
23604       if( (pX->exclMask & mask)!=0 ){
23605         rc = SQLITE_BUSY;
23606         break;
23607       }
23608       allShared |= pX->sharedMask;
23609     }
23610 
23611     /* Get shared locks at the system level, if necessary */
23612     if( rc==SQLITE_OK ){
23613       if( (allShared & mask)==0 ){
23614         rc = os2ShmSystemLock(pShmNode, _SHM_RDLCK, ofst+OS2_SHM_BASE, n);
23615       }else{
23616         rc = SQLITE_OK;
23617       }
23618     }
23619 
23620     /* Get the local shared locks */
23621     if( rc==SQLITE_OK ){
23622       p->sharedMask |= mask;
23623     }
23624   }else{
23625     /* Make sure no sibling connections hold locks that will block this
23626     ** lock.  If any do, return SQLITE_BUSY right away.
23627     */
23628     for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
23629       if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){
23630         rc = SQLITE_BUSY;
23631         break;
23632       }
23633     }
23634 
23635     /* Get the exclusive locks at the system level.  Then if successful
23636     ** also mark the local connection as being locked.
23637     */
23638     if( rc==SQLITE_OK ){
23639       rc = os2ShmSystemLock(pShmNode, _SHM_WRLCK, ofst+OS2_SHM_BASE, n);
23640       if( rc==SQLITE_OK ){
23641         assert( (p->sharedMask & mask)==0 );
23642         p->exclMask |= mask;
23643       }
23644     }
23645   }
23646 
23647   sqlite3_mutex_leave(pShmNode->mutex);
23648 
23649   OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x %s\n",
23650            p->id, (int)GetCurrentProcessId(), p->sharedMask, p->exclMask,
23651            rc ? "failed" : "ok"));
23652 
23653   ERR_TRACE(rc, ("os2ShmLock: ofst = %d, n = %d, flags = 0x%x -> %d \n",
23654                  ofst, n, flags, rc))
23655 
23656   return rc;
23657 }
23658 
23659 /*
23660 ** Implement a memory barrier or memory fence on shared memory.
23661 **
23662 ** All loads and stores begun before the barrier must complete before
23663 ** any load or store begun after the barrier.
23664 */
23665 static void os2ShmBarrier(
23666   sqlite3_file *id                /* Database file holding the shared memory */
23667 ){
23668   UNUSED_PARAMETER(id);
23669   os2ShmEnterMutex();
23670   os2ShmLeaveMutex();
23671 }
23672 
23673 #else
23674 # define os2ShmMap     0
23675 # define os2ShmLock    0
23676 # define os2ShmBarrier 0
23677 # define os2ShmUnmap   0
23678 #endif /* #ifndef SQLITE_OMIT_WAL */
23679 
23680 
23681 /*
23682 ** This vector defines all the methods that can operate on an
23683 ** sqlite3_file for os2.
23684 */
23685 static const sqlite3_io_methods os2IoMethod = {
23686   2,                              /* iVersion */
23687   os2Close,                       /* xClose */
23688   os2Read,                        /* xRead */
23689   os2Write,                       /* xWrite */
23690   os2Truncate,                    /* xTruncate */
23691   os2Sync,                        /* xSync */
23692   os2FileSize,                    /* xFileSize */
23693   os2Lock,                        /* xLock */
23694   os2Unlock,                      /* xUnlock */
23695   os2CheckReservedLock,           /* xCheckReservedLock */
23696   os2FileControl,                 /* xFileControl */
23697   os2SectorSize,                  /* xSectorSize */
23698   os2DeviceCharacteristics,       /* xDeviceCharacteristics */
23699   os2ShmMap,                      /* xShmMap */
23700   os2ShmLock,                     /* xShmLock */
23701   os2ShmBarrier,                  /* xShmBarrier */
23702   os2ShmUnmap                     /* xShmUnmap */
23703 };
23704 
23705 
23706 /***************************************************************************
23707 ** Here ends the I/O methods that form the sqlite3_io_methods object.
23708 **
23709 ** The next block of code implements the VFS methods.
23710 ****************************************************************************/
23711 
23712 /*
23713 ** Create a temporary file name in zBuf.  zBuf must be big enough to
23714 ** hold at pVfs->mxPathname characters.
23715 */
23716 static int getTempname(int nBuf, char *zBuf ){
23717   static const char zChars[] =
23718     "abcdefghijklmnopqrstuvwxyz"
23719     "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
23720     "0123456789";
23721   int i, j;
23722   PSZ zTempPathCp;
23723   char zTempPath[CCHMAXPATH];
23724   ULONG ulDriveNum, ulDriveMap;
23725 
23726   /* It's odd to simulate an io-error here, but really this is just
23727   ** using the io-error infrastructure to test that SQLite handles this
23728   ** function failing.
23729   */
23730   SimulateIOError( return SQLITE_IOERR );
23731 
23732   if( sqlite3_temp_directory ) {
23733     sqlite3_snprintf(CCHMAXPATH-30, zTempPath, "%s", sqlite3_temp_directory);
23734   } else if( DosScanEnv( (PSZ)"TEMP",   &zTempPathCp ) == NO_ERROR ||
23735              DosScanEnv( (PSZ)"TMP",    &zTempPathCp ) == NO_ERROR ||
23736              DosScanEnv( (PSZ)"TMPDIR", &zTempPathCp ) == NO_ERROR ) {
23737     char *zTempPathUTF = convertCpPathToUtf8( (char *)zTempPathCp );
23738     sqlite3_snprintf(CCHMAXPATH-30, zTempPath, "%s", zTempPathUTF);
23739     free( zTempPathUTF );
23740   } else if( DosQueryCurrentDisk( &ulDriveNum, &ulDriveMap ) == NO_ERROR ) {
23741     zTempPath[0] = (char)('A' + ulDriveNum - 1);
23742     zTempPath[1] = ':';
23743     zTempPath[2] = '\0';
23744   } else {
23745     zTempPath[0] = '\0';
23746   }
23747 
23748   /* Strip off a trailing slashes or backslashes, otherwise we would get *
23749    * multiple (back)slashes which causes DosOpen() to fail.              *
23750    * Trailing spaces are not allowed, either.                            */
23751   j = sqlite3Strlen30(zTempPath);
23752   while( j > 0 && ( zTempPath[j-1] == '\\' || zTempPath[j-1] == '/' ||
23753                     zTempPath[j-1] == ' ' ) ){
23754     j--;
23755   }
23756   zTempPath[j] = '\0';
23757 
23758   /* We use 20 bytes to randomize the name */
23759   sqlite3_snprintf(nBuf-22, zBuf,
23760                    "%s\\"SQLITE_TEMP_FILE_PREFIX, zTempPath);
23761   j = sqlite3Strlen30(zBuf);
23762   sqlite3_randomness( 20, &zBuf[j] );
23763   for( i = 0; i < 20; i++, j++ ){
23764     zBuf[j] = zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
23765   }
23766   zBuf[j] = 0;
23767 
23768   OSTRACE(( "TEMP FILENAME: %s\n", zBuf ));
23769   return SQLITE_OK;
23770 }
23771 
23772 
23773 /*
23774 ** Turn a relative pathname into a full pathname.  Write the full
23775 ** pathname into zFull[].  zFull[] will be at least pVfs->mxPathname
23776 ** bytes in size.
23777 */
23778 static int os2FullPathname(
23779   sqlite3_vfs *pVfs,          /* Pointer to vfs object */
23780   const char *zRelative,      /* Possibly relative input path */
23781   int nFull,                  /* Size of output buffer in bytes */
23782   char *zFull                 /* Output buffer */
23783 ){
23784   char *zRelativeCp = convertUtf8PathToCp( zRelative );
23785   char zFullCp[CCHMAXPATH] = "\0";
23786   char *zFullUTF;
23787   APIRET rc = DosQueryPathInfo( (PSZ)zRelativeCp, FIL_QUERYFULLNAME,
23788                                 zFullCp, CCHMAXPATH );
23789   free( zRelativeCp );
23790   zFullUTF = convertCpPathToUtf8( zFullCp );
23791   sqlite3_snprintf( nFull, zFull, zFullUTF );
23792   free( zFullUTF );
23793   return rc == NO_ERROR ? SQLITE_OK : SQLITE_IOERR;
23794 }
23795 
23796 
23797 /*
23798 ** Open a file.
23799 */
23800 static int os2Open(
23801   sqlite3_vfs *pVfs,            /* Not used */
23802   const char *zName,            /* Name of the file (UTF-8) */
23803   sqlite3_file *id,             /* Write the SQLite file handle here */
23804   int flags,                    /* Open mode flags */
23805   int *pOutFlags                /* Status return flags */
23806 ){
23807   HFILE h;
23808   ULONG ulOpenFlags = 0;
23809   ULONG ulOpenMode = 0;
23810   ULONG ulAction = 0;
23811   ULONG rc;
23812   os2File *pFile = (os2File*)id;
23813   const char *zUtf8Name = zName;
23814   char *zNameCp;
23815   char  zTmpname[CCHMAXPATH];
23816 
23817   int isExclusive  = (flags & SQLITE_OPEN_EXCLUSIVE);
23818   int isCreate     = (flags & SQLITE_OPEN_CREATE);
23819   int isReadWrite  = (flags & SQLITE_OPEN_READWRITE);
23820 #ifndef NDEBUG
23821   int isDelete     = (flags & SQLITE_OPEN_DELETEONCLOSE);
23822   int isReadonly   = (flags & SQLITE_OPEN_READONLY);
23823   int eType        = (flags & 0xFFFFFF00);
23824   int isOpenJournal = (isCreate && (
23825         eType==SQLITE_OPEN_MASTER_JOURNAL
23826      || eType==SQLITE_OPEN_MAIN_JOURNAL
23827      || eType==SQLITE_OPEN_WAL
23828   ));
23829 #endif
23830 
23831   UNUSED_PARAMETER(pVfs);
23832   assert( id!=0 );
23833 
23834   /* Check the following statements are true:
23835   **
23836   **   (a) Exactly one of the READWRITE and READONLY flags must be set, and
23837   **   (b) if CREATE is set, then READWRITE must also be set, and
23838   **   (c) if EXCLUSIVE is set, then CREATE must also be set.
23839   **   (d) if DELETEONCLOSE is set, then CREATE must also be set.
23840   */
23841   assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly));
23842   assert(isCreate==0 || isReadWrite);
23843   assert(isExclusive==0 || isCreate);
23844   assert(isDelete==0 || isCreate);
23845 
23846   /* The main DB, main journal, WAL file and master journal are never
23847   ** automatically deleted. Nor are they ever temporary files.  */
23848   assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB );
23849   assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL );
23850   assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MASTER_JOURNAL );
23851   assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL );
23852 
23853   /* Assert that the upper layer has set one of the "file-type" flags. */
23854   assert( eType==SQLITE_OPEN_MAIN_DB      || eType==SQLITE_OPEN_TEMP_DB
23855        || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL
23856        || eType==SQLITE_OPEN_SUBJOURNAL   || eType==SQLITE_OPEN_MASTER_JOURNAL
23857        || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL
23858   );
23859 
23860   memset( pFile, 0, sizeof(*pFile) );
23861   pFile->h = (HFILE)-1;
23862 
23863   /* If the second argument to this function is NULL, generate a
23864   ** temporary file name to use
23865   */
23866   if( !zUtf8Name ){
23867     assert(isDelete && !isOpenJournal);
23868     rc = getTempname(CCHMAXPATH, zTmpname);
23869     if( rc!=SQLITE_OK ){
23870       return rc;
23871     }
23872     zUtf8Name = zTmpname;
23873   }
23874 
23875   if( isReadWrite ){
23876     ulOpenMode |= OPEN_ACCESS_READWRITE;
23877   }else{
23878     ulOpenMode |= OPEN_ACCESS_READONLY;
23879   }
23880 
23881   /* Open in random access mode for possibly better speed.  Allow full
23882   ** sharing because file locks will provide exclusive access when needed.
23883   ** The handle should not be inherited by child processes and we don't
23884   ** want popups from the critical error handler.
23885   */
23886   ulOpenMode |= OPEN_FLAGS_RANDOM | OPEN_SHARE_DENYNONE |
23887                 OPEN_FLAGS_NOINHERIT | OPEN_FLAGS_FAIL_ON_ERROR;
23888 
23889   /* SQLITE_OPEN_EXCLUSIVE is used to make sure that a new file is
23890   ** created. SQLite doesn't use it to indicate "exclusive access"
23891   ** as it is usually understood.
23892   */
23893   if( isExclusive ){
23894     /* Creates a new file, only if it does not already exist. */
23895     /* If the file exists, it fails. */
23896     ulOpenFlags |= OPEN_ACTION_CREATE_IF_NEW | OPEN_ACTION_FAIL_IF_EXISTS;
23897   }else if( isCreate ){
23898     /* Open existing file, or create if it doesn't exist */
23899     ulOpenFlags |= OPEN_ACTION_CREATE_IF_NEW | OPEN_ACTION_OPEN_IF_EXISTS;
23900   }else{
23901     /* Opens a file, only if it exists. */
23902     ulOpenFlags |= OPEN_ACTION_FAIL_IF_NEW | OPEN_ACTION_OPEN_IF_EXISTS;
23903   }
23904 
23905   zNameCp = convertUtf8PathToCp( zUtf8Name );
23906   rc = DosOpen( (PSZ)zNameCp,
23907                 &h,
23908                 &ulAction,
23909                 0L,
23910                 FILE_NORMAL,
23911                 ulOpenFlags,
23912                 ulOpenMode,
23913                 (PEAOP2)NULL );
23914   free( zNameCp );
23915 
23916   if( rc != NO_ERROR ){
23917     OSTRACE(( "OPEN Invalid handle rc=%d: zName=%s, ulAction=%#lx, ulFlags=%#lx, ulMode=%#lx\n",
23918               rc, zUtf8Name, ulAction, ulOpenFlags, ulOpenMode ));
23919 
23920     if( isReadWrite ){
23921       return os2Open( pVfs, zName, id,
23922                       ((flags|SQLITE_OPEN_READONLY)&~(SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE)),
23923                       pOutFlags );
23924     }else{
23925       return SQLITE_CANTOPEN;
23926     }
23927   }
23928 
23929   if( pOutFlags ){
23930     *pOutFlags = isReadWrite ? SQLITE_OPEN_READWRITE : SQLITE_OPEN_READONLY;
23931   }
23932 
23933   os2FullPathname( pVfs, zUtf8Name, sizeof( zTmpname ), zTmpname );
23934   pFile->zFullPathCp = convertUtf8PathToCp( zTmpname );
23935   pFile->pMethod = &os2IoMethod;
23936   pFile->flags = flags;
23937   pFile->h = h;
23938 
23939   OpenCounter(+1);
23940   OSTRACE(( "OPEN %d pOutFlags=%d\n", pFile->h, pOutFlags ));
23941   return SQLITE_OK;
23942 }
23943 
23944 /*
23945 ** Delete the named file.
23946 */
23947 static int os2Delete(
23948   sqlite3_vfs *pVfs,                     /* Not used on os2 */
23949   const char *zFilename,                 /* Name of file to delete */
23950   int syncDir                            /* Not used on os2 */
23951 ){
23952   APIRET rc;
23953   char *zFilenameCp;
23954   SimulateIOError( return SQLITE_IOERR_DELETE );
23955   zFilenameCp = convertUtf8PathToCp( zFilename );
23956   rc = DosDelete( (PSZ)zFilenameCp );
23957   free( zFilenameCp );
23958   OSTRACE(( "DELETE \"%s\"\n", zFilename ));
23959   return (rc == NO_ERROR ||
23960           rc == ERROR_FILE_NOT_FOUND ||
23961           rc == ERROR_PATH_NOT_FOUND ) ? SQLITE_OK : SQLITE_IOERR_DELETE;
23962 }
23963 
23964 /*
23965 ** Check the existance and status of a file.
23966 */
23967 static int os2Access(
23968   sqlite3_vfs *pVfs,        /* Not used on os2 */
23969   const char *zFilename,    /* Name of file to check */
23970   int flags,                /* Type of test to make on this file */
23971   int *pOut                 /* Write results here */
23972 ){
23973   APIRET rc;
23974   FILESTATUS3 fsts3ConfigInfo;
23975   char *zFilenameCp;
23976 
23977   UNUSED_PARAMETER(pVfs);
23978   SimulateIOError( return SQLITE_IOERR_ACCESS; );
23979 
23980   zFilenameCp = convertUtf8PathToCp( zFilename );
23981   rc = DosQueryPathInfo( (PSZ)zFilenameCp, FIL_STANDARD,
23982                          &fsts3ConfigInfo, sizeof(FILESTATUS3) );
23983   free( zFilenameCp );
23984   OSTRACE(( "ACCESS fsts3ConfigInfo.attrFile=%d flags=%d rc=%d\n",
23985             fsts3ConfigInfo.attrFile, flags, rc ));
23986 
23987   switch( flags ){
23988     case SQLITE_ACCESS_EXISTS:
23989       /* For an SQLITE_ACCESS_EXISTS query, treat a zero-length file
23990       ** as if it does not exist.
23991       */
23992       if( fsts3ConfigInfo.cbFile == 0 )
23993         rc = ERROR_FILE_NOT_FOUND;
23994       break;
23995     case SQLITE_ACCESS_READ:
23996       break;
23997     case SQLITE_ACCESS_READWRITE:
23998       if( fsts3ConfigInfo.attrFile & FILE_READONLY )
23999         rc = ERROR_ACCESS_DENIED;
24000       break;
24001     default:
24002       rc = ERROR_FILE_NOT_FOUND;
24003       assert( !"Invalid flags argument" );
24004   }
24005 
24006   *pOut = (rc == NO_ERROR);
24007   OSTRACE(( "ACCESS %s flags %d: rc=%d\n", zFilename, flags, *pOut ));
24008 
24009   return SQLITE_OK;
24010 }
24011 
24012 
24013 #ifndef SQLITE_OMIT_LOAD_EXTENSION
24014 /*
24015 ** Interfaces for opening a shared library, finding entry points
24016 ** within the shared library, and closing the shared library.
24017 */
24018 /*
24019 ** Interfaces for opening a shared library, finding entry points
24020 ** within the shared library, and closing the shared library.
24021 */
24022 static void *os2DlOpen(sqlite3_vfs *pVfs, const char *zFilename){
24023   HMODULE hmod;
24024   APIRET rc;
24025   char *zFilenameCp = convertUtf8PathToCp(zFilename);
24026   rc = DosLoadModule(NULL, 0, (PSZ)zFilenameCp, &hmod);
24027   free(zFilenameCp);
24028   return rc != NO_ERROR ? 0 : (void*)hmod;
24029 }
24030 /*
24031 ** A no-op since the error code is returned on the DosLoadModule call.
24032 ** os2Dlopen returns zero if DosLoadModule is not successful.
24033 */
24034 static void os2DlError(sqlite3_vfs *pVfs, int nBuf, char *zBufOut){
24035 /* no-op */
24036 }
24037 static void (*os2DlSym(sqlite3_vfs *pVfs, void *pHandle, const char *zSymbol))(void){
24038   PFN pfn;
24039   APIRET rc;
24040   rc = DosQueryProcAddr((HMODULE)pHandle, 0L, (PSZ)zSymbol, &pfn);
24041   if( rc != NO_ERROR ){
24042     /* if the symbol itself was not found, search again for the same
24043      * symbol with an extra underscore, that might be needed depending
24044      * on the calling convention */
24045     char _zSymbol[256] = "_";
24046     strncat(_zSymbol, zSymbol, 254);
24047     rc = DosQueryProcAddr((HMODULE)pHandle, 0L, (PSZ)_zSymbol, &pfn);
24048   }
24049   return rc != NO_ERROR ? 0 : (void(*)(void))pfn;
24050 }
24051 static void os2DlClose(sqlite3_vfs *pVfs, void *pHandle){
24052   DosFreeModule((HMODULE)pHandle);
24053 }
24054 #else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
24055   #define os2DlOpen 0
24056   #define os2DlError 0
24057   #define os2DlSym 0
24058   #define os2DlClose 0
24059 #endif
24060 
24061 
24062 /*
24063 ** Write up to nBuf bytes of randomness into zBuf.
24064 */
24065 static int os2Randomness(sqlite3_vfs *pVfs, int nBuf, char *zBuf ){
24066   int n = 0;
24067 #if defined(SQLITE_TEST)
24068   n = nBuf;
24069   memset(zBuf, 0, nBuf);
24070 #else
24071   int i;
24072   PPIB ppib;
24073   PTIB ptib;
24074   DATETIME dt;
24075   static unsigned c = 0;
24076   /* Ordered by variation probability */
24077   static ULONG svIdx[6] = { QSV_MS_COUNT, QSV_TIME_LOW,
24078                             QSV_MAXPRMEM, QSV_MAXSHMEM,
24079                             QSV_TOTAVAILMEM, QSV_TOTRESMEM };
24080 
24081   /* 8 bytes; timezone and weekday don't increase the randomness much */
24082   if( (int)sizeof(dt)-3 <= nBuf - n ){
24083     c += 0x0100;
24084     DosGetDateTime(&dt);
24085     dt.year = (USHORT)((dt.year - 1900) | c);
24086     memcpy(&zBuf[n], &dt, sizeof(dt)-3);
24087     n += sizeof(dt)-3;
24088   }
24089 
24090   /* 4 bytes; PIDs and TIDs are 16 bit internally, so combine them */
24091   if( (int)sizeof(ULONG) <= nBuf - n ){
24092     DosGetInfoBlocks(&ptib, &ppib);
24093     *(PULONG)&zBuf[n] = MAKELONG(ppib->pib_ulpid,
24094                                  ptib->tib_ptib2->tib2_ultid);
24095     n += sizeof(ULONG);
24096   }
24097 
24098   /* Up to 6 * 4 bytes; variables depend on the system state */
24099   for( i = 0; i < 6 && (int)sizeof(ULONG) <= nBuf - n; i++ ){
24100     DosQuerySysInfo(svIdx[i], svIdx[i],
24101                     (PULONG)&zBuf[n], sizeof(ULONG));
24102     n += sizeof(ULONG);
24103   }
24104 #endif
24105 
24106   return n;
24107 }
24108 
24109 /*
24110 ** Sleep for a little while.  Return the amount of time slept.
24111 ** The argument is the number of microseconds we want to sleep.
24112 ** The return value is the number of microseconds of sleep actually
24113 ** requested from the underlying operating system, a number which
24114 ** might be greater than or equal to the argument, but not less
24115 ** than the argument.
24116 */
24117 static int os2Sleep( sqlite3_vfs *pVfs, int microsec ){
24118   DosSleep( (microsec/1000) );
24119   return microsec;
24120 }
24121 
24122 /*
24123 ** The following variable, if set to a non-zero value, becomes the result
24124 ** returned from sqlite3OsCurrentTime().  This is used for testing.
24125 */
24126 #ifdef SQLITE_TEST
24127 SQLITE_API int sqlite3_current_time = 0;
24128 #endif
24129 
24130 /*
24131 ** Find the current time (in Universal Coordinated Time).  Write into *piNow
24132 ** the current time and date as a Julian Day number times 86_400_000.  In
24133 ** other words, write into *piNow the number of milliseconds since the Julian
24134 ** epoch of noon in Greenwich on November 24, 4714 B.C according to the
24135 ** proleptic Gregorian calendar.
24136 **
24137 ** On success, return 0.  Return 1 if the time and date cannot be found.
24138 */
24139 static int os2CurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *piNow){
24140 #ifdef SQLITE_TEST
24141   static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000;
24142 #endif
24143   int year, month, datepart, timepart;
24144 
24145   DATETIME dt;
24146   DosGetDateTime( &dt );
24147 
24148   year = dt.year;
24149   month = dt.month;
24150 
24151   /* Calculations from http://www.astro.keele.ac.uk/~rno/Astronomy/hjd.html
24152   ** http://www.astro.keele.ac.uk/~rno/Astronomy/hjd-0.1.c
24153   ** Calculate the Julian days
24154   */
24155   datepart = (int)dt.day - 32076 +
24156     1461*(year + 4800 + (month - 14)/12)/4 +
24157     367*(month - 2 - (month - 14)/12*12)/12 -
24158     3*((year + 4900 + (month - 14)/12)/100)/4;
24159 
24160   /* Time in milliseconds, hours to noon added */
24161   timepart = 12*3600*1000 + dt.hundredths*10 + dt.seconds*1000 +
24162     ((int)dt.minutes + dt.timezone)*60*1000 + dt.hours*3600*1000;
24163 
24164   *piNow = (sqlite3_int64)datepart*86400*1000 + timepart;
24165 
24166 #ifdef SQLITE_TEST
24167   if( sqlite3_current_time ){
24168     *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch;
24169   }
24170 #endif
24171 
24172   UNUSED_PARAMETER(pVfs);
24173   return 0;
24174 }
24175 
24176 /*
24177 ** Find the current time (in Universal Coordinated Time).  Write the
24178 ** current time and date as a Julian Day number into *prNow and
24179 ** return 0.  Return 1 if the time and date cannot be found.
24180 */
24181 static int os2CurrentTime( sqlite3_vfs *pVfs, double *prNow ){
24182   int rc;
24183   sqlite3_int64 i;
24184   rc = os2CurrentTimeInt64(pVfs, &i);
24185   if( !rc ){
24186     *prNow = i/86400000.0;
24187   }
24188   return rc;
24189 }
24190 
24191 /*
24192 ** The idea is that this function works like a combination of
24193 ** GetLastError() and FormatMessage() on windows (or errno and
24194 ** strerror_r() on unix). After an error is returned by an OS
24195 ** function, SQLite calls this function with zBuf pointing to
24196 ** a buffer of nBuf bytes. The OS layer should populate the
24197 ** buffer with a nul-terminated UTF-8 encoded error message
24198 ** describing the last IO error to have occurred within the calling
24199 ** thread.
24200 **
24201 ** If the error message is too large for the supplied buffer,
24202 ** it should be truncated. The return value of xGetLastError
24203 ** is zero if the error message fits in the buffer, or non-zero
24204 ** otherwise (if the message was truncated). If non-zero is returned,
24205 ** then it is not necessary to include the nul-terminator character
24206 ** in the output buffer.
24207 **
24208 ** Not supplying an error message will have no adverse effect
24209 ** on SQLite. It is fine to have an implementation that never
24210 ** returns an error message:
24211 **
24212 **   int xGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
24213 **     assert(zBuf[0]=='\0');
24214 **     return 0;
24215 **   }
24216 **
24217 ** However if an error message is supplied, it will be incorporated
24218 ** by sqlite into the error message available to the user using
24219 ** sqlite3_errmsg(), possibly making IO errors easier to debug.
24220 */
24221 static int os2GetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
24222   assert(zBuf[0]=='\0');
24223   return 0;
24224 }
24225 
24226 /*
24227 ** Initialize and deinitialize the operating system interface.
24228 */
24229 SQLITE_API int sqlite3_os_init(void){
24230   static sqlite3_vfs os2Vfs = {
24231     3,                 /* iVersion */
24232     sizeof(os2File),   /* szOsFile */
24233     CCHMAXPATH,        /* mxPathname */
24234     0,                 /* pNext */
24235     "os2",             /* zName */
24236     0,                 /* pAppData */
24237 
24238     os2Open,           /* xOpen */
24239     os2Delete,         /* xDelete */
24240     os2Access,         /* xAccess */
24241     os2FullPathname,   /* xFullPathname */
24242     os2DlOpen,         /* xDlOpen */
24243     os2DlError,        /* xDlError */
24244     os2DlSym,          /* xDlSym */
24245     os2DlClose,        /* xDlClose */
24246     os2Randomness,     /* xRandomness */
24247     os2Sleep,          /* xSleep */
24248     os2CurrentTime,    /* xCurrentTime */
24249     os2GetLastError,   /* xGetLastError */
24250     os2CurrentTimeInt64, /* xCurrentTimeInt64 */
24251     0,                 /* xSetSystemCall */
24252     0,                 /* xGetSystemCall */
24253     0                  /* xNextSystemCall */
24254   };
24255   sqlite3_vfs_register(&os2Vfs, 1);
24256   initUconvObjects();
24257 /*  sqlite3OSTrace = 1; */
24258   return SQLITE_OK;
24259 }
24260 SQLITE_API int sqlite3_os_end(void){
24261   freeUconvObjects();
24262   return SQLITE_OK;
24263 }
24264 
24265 #endif /* SQLITE_OS_OS2 */
24266 
24267 /************** End of os_os2.c **********************************************/
24268 /************** Begin file os_unix.c *****************************************/
24269 /*
24270 ** 2004 May 22
24271 **
24272 ** The author disclaims copyright to this source code.  In place of
24273 ** a legal notice, here is a blessing:
24274 **
24275 **    May you do good and not evil.
24276 **    May you find forgiveness for yourself and forgive others.
24277 **    May you share freely, never taking more than you give.
24278 **
24279 ******************************************************************************
24280 **
24281 ** This file contains the VFS implementation for unix-like operating systems
24282 ** include Linux, MacOSX, *BSD, QNX, VxWorks, AIX, HPUX, and others.
24283 **
24284 ** There are actually several different VFS implementations in this file.
24285 ** The differences are in the way that file locking is done.  The default
24286 ** implementation uses Posix Advisory Locks.  Alternative implementations
24287 ** use flock(), dot-files, various proprietary locking schemas, or simply
24288 ** skip locking all together.
24289 **
24290 ** This source file is organized into divisions where the logic for various
24291 ** subfunctions is contained within the appropriate division.  PLEASE
24292 ** KEEP THE STRUCTURE OF THIS FILE INTACT.  New code should be placed
24293 ** in the correct division and should be clearly labeled.
24294 **
24295 ** The layout of divisions is as follows:
24296 **
24297 **   *  General-purpose declarations and utility functions.
24298 **   *  Unique file ID logic used by VxWorks.
24299 **   *  Various locking primitive implementations (all except proxy locking):
24300 **      + for Posix Advisory Locks
24301 **      + for no-op locks
24302 **      + for dot-file locks
24303 **      + for flock() locking
24304 **      + for named semaphore locks (VxWorks only)
24305 **      + for AFP filesystem locks (MacOSX only)
24306 **   *  sqlite3_file methods not associated with locking.
24307 **   *  Definitions of sqlite3_io_methods objects for all locking
24308 **      methods plus "finder" functions for each locking method.
24309 **   *  sqlite3_vfs method implementations.
24310 **   *  Locking primitives for the proxy uber-locking-method. (MacOSX only)
24311 **   *  Definitions of sqlite3_vfs objects for all locking methods
24312 **      plus implementations of sqlite3_os_init() and sqlite3_os_end().
24313 */
24314 #if SQLITE_OS_UNIX              /* This file is used on unix only */
24315 
24316 /*
24317 ** There are various methods for file locking used for concurrency
24318 ** control:
24319 **
24320 **   1. POSIX locking (the default),
24321 **   2. No locking,
24322 **   3. Dot-file locking,
24323 **   4. flock() locking,
24324 **   5. AFP locking (OSX only),
24325 **   6. Named POSIX semaphores (VXWorks only),
24326 **   7. proxy locking. (OSX only)
24327 **
24328 ** Styles 4, 5, and 7 are only available of SQLITE_ENABLE_LOCKING_STYLE
24329 ** is defined to 1.  The SQLITE_ENABLE_LOCKING_STYLE also enables automatic
24330 ** selection of the appropriate locking style based on the filesystem
24331 ** where the database is located.
24332 */
24333 #if !defined(SQLITE_ENABLE_LOCKING_STYLE)
24334 #  if defined(__APPLE__)
24335 #    define SQLITE_ENABLE_LOCKING_STYLE 1
24336 #  else
24337 #    define SQLITE_ENABLE_LOCKING_STYLE 0
24338 #  endif
24339 #endif
24340 
24341 /*
24342 ** Define the OS_VXWORKS pre-processor macro to 1 if building on
24343 ** vxworks, or 0 otherwise.
24344 */
24345 #ifndef OS_VXWORKS
24346 #  if defined(__RTP__) || defined(_WRS_KERNEL)
24347 #    define OS_VXWORKS 1
24348 #  else
24349 #    define OS_VXWORKS 0
24350 #  endif
24351 #endif
24352 
24353 /*
24354 ** These #defines should enable >2GB file support on Posix if the
24355 ** underlying operating system supports it.  If the OS lacks
24356 ** large file support, these should be no-ops.
24357 **
24358 ** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch
24359 ** on the compiler command line.  This is necessary if you are compiling
24360 ** on a recent machine (ex: RedHat 7.2) but you want your code to work
24361 ** on an older machine (ex: RedHat 6.0).  If you compile on RedHat 7.2
24362 ** without this option, LFS is enable.  But LFS does not exist in the kernel
24363 ** in RedHat 6.0, so the code won't work.  Hence, for maximum binary
24364 ** portability you should omit LFS.
24365 **
24366 ** The previous paragraph was written in 2005.  (This paragraph is written
24367 ** on 2008-11-28.) These days, all Linux kernels support large files, so
24368 ** you should probably leave LFS enabled.  But some embedded platforms might
24369 ** lack LFS in which case the SQLITE_DISABLE_LFS macro might still be useful.
24370 */
24371 #ifndef SQLITE_DISABLE_LFS
24372 # define _LARGE_FILE       1
24373 # ifndef _FILE_OFFSET_BITS
24374 #   define _FILE_OFFSET_BITS 64
24375 # endif
24376 # define _LARGEFILE_SOURCE 1
24377 #endif
24378 
24379 /*
24380 ** standard include files.
24381 */
24382 #include <sys/types.h>
24383 #include <sys/stat.h>
24384 #include <fcntl.h>
24385 #include <unistd.h>
24386 #include <sys/time.h>
24387 #include <errno.h>
24388 #ifndef SQLITE_OMIT_WAL
24389 #include <sys/mman.h>
24390 #endif
24391 
24392 #if SQLITE_ENABLE_LOCKING_STYLE
24393 # include <sys/ioctl.h>
24394 # if OS_VXWORKS
24395 #  include <semaphore.h>
24396 #  include <limits.h>
24397 # else
24398 #  include <sys/file.h>
24399 #  include <sys/param.h>
24400 # endif
24401 #endif /* SQLITE_ENABLE_LOCKING_STYLE */
24402 
24403 #if defined(__APPLE__) || (SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS)
24404 # include <sys/mount.h>
24405 #endif
24406 
24407 #ifdef HAVE_UTIME
24408 # include <utime.h>
24409 #endif
24410 
24411 /*
24412 ** Allowed values of unixFile.fsFlags
24413 */
24414 #define SQLITE_FSFLAGS_IS_MSDOS     0x1
24415 
24416 /*
24417 ** If we are to be thread-safe, include the pthreads header and define
24418 ** the SQLITE_UNIX_THREADS macro.
24419 */
24420 #if SQLITE_THREADSAFE
24421 # define SQLITE_UNIX_THREADS 1
24422 #endif
24423 
24424 /*
24425 ** Default permissions when creating a new file
24426 */
24427 #ifndef SQLITE_DEFAULT_FILE_PERMISSIONS
24428 # define SQLITE_DEFAULT_FILE_PERMISSIONS 0644
24429 #endif
24430 
24431 /*
24432  ** Default permissions when creating auto proxy dir
24433  */
24434 #ifndef SQLITE_DEFAULT_PROXYDIR_PERMISSIONS
24435 # define SQLITE_DEFAULT_PROXYDIR_PERMISSIONS 0755
24436 #endif
24437 
24438 /*
24439 ** Maximum supported path-length.
24440 */
24441 #define MAX_PATHNAME 512
24442 
24443 /*
24444 ** Only set the lastErrno if the error code is a real error and not
24445 ** a normal expected return code of SQLITE_BUSY or SQLITE_OK
24446 */
24447 #define IS_LOCK_ERROR(x)  ((x != SQLITE_OK) && (x != SQLITE_BUSY))
24448 
24449 /* Forward references */
24450 typedef struct unixShm unixShm;               /* Connection shared memory */
24451 typedef struct unixShmNode unixShmNode;       /* Shared memory instance */
24452 typedef struct unixInodeInfo unixInodeInfo;   /* An i-node */
24453 typedef struct UnixUnusedFd UnixUnusedFd;     /* An unused file descriptor */
24454 
24455 /*
24456 ** Sometimes, after a file handle is closed by SQLite, the file descriptor
24457 ** cannot be closed immediately. In these cases, instances of the following
24458 ** structure are used to store the file descriptor while waiting for an
24459 ** opportunity to either close or reuse it.
24460 */
24461 struct UnixUnusedFd {
24462   int fd;                   /* File descriptor to close */
24463   int flags;                /* Flags this file descriptor was opened with */
24464   UnixUnusedFd *pNext;      /* Next unused file descriptor on same file */
24465 };
24466 
24467 /*
24468 ** The unixFile structure is subclass of sqlite3_file specific to the unix
24469 ** VFS implementations.
24470 */
24471 typedef struct unixFile unixFile;
24472 struct unixFile {
24473   sqlite3_io_methods const *pMethod;  /* Always the first entry */
24474   unixInodeInfo *pInode;              /* Info about locks on this inode */
24475   int h;                              /* The file descriptor */
24476   int dirfd;                          /* File descriptor for the directory */
24477   unsigned char eFileLock;            /* The type of lock held on this fd */
24478   unsigned char ctrlFlags;            /* Behavioral bits.  UNIXFILE_* flags */
24479   int lastErrno;                      /* The unix errno from last I/O error */
24480   void *lockingContext;               /* Locking style specific state */
24481   UnixUnusedFd *pUnused;              /* Pre-allocated UnixUnusedFd */
24482   const char *zPath;                  /* Name of the file */
24483   unixShm *pShm;                      /* Shared memory segment information */
24484   int szChunk;                        /* Configured by FCNTL_CHUNK_SIZE */
24485 #if SQLITE_ENABLE_LOCKING_STYLE
24486   int openFlags;                      /* The flags specified at open() */
24487 #endif
24488 #if SQLITE_ENABLE_LOCKING_STYLE || defined(__APPLE__)
24489   unsigned fsFlags;                   /* cached details from statfs() */
24490 #endif
24491 #if OS_VXWORKS
24492   int isDelete;                       /* Delete on close if true */
24493   struct vxworksFileId *pId;          /* Unique file ID */
24494 #endif
24495 #ifndef NDEBUG
24496   /* The next group of variables are used to track whether or not the
24497   ** transaction counter in bytes 24-27 of database files are updated
24498   ** whenever any part of the database changes.  An assertion fault will
24499   ** occur if a file is updated without also updating the transaction
24500   ** counter.  This test is made to avoid new problems similar to the
24501   ** one described by ticket #3584.
24502   */
24503   unsigned char transCntrChng;   /* True if the transaction counter changed */
24504   unsigned char dbUpdate;        /* True if any part of database file changed */
24505   unsigned char inNormalWrite;   /* True if in a normal write operation */
24506 #endif
24507 #ifdef SQLITE_TEST
24508   /* In test mode, increase the size of this structure a bit so that
24509   ** it is larger than the struct CrashFile defined in test6.c.
24510   */
24511   char aPadding[32];
24512 #endif
24513 };
24514 
24515 /*
24516 ** Allowed values for the unixFile.ctrlFlags bitmask:
24517 */
24518 #define UNIXFILE_EXCL   0x01     /* Connections from one process only */
24519 #define UNIXFILE_RDONLY 0x02     /* Connection is read only */
24520 
24521 /*
24522 ** Include code that is common to all os_*.c files
24523 */
24524 /************** Include os_common.h in the middle of os_unix.c ***************/
24525 /************** Begin file os_common.h ***************************************/
24526 /*
24527 ** 2004 May 22
24528 **
24529 ** The author disclaims copyright to this source code.  In place of
24530 ** a legal notice, here is a blessing:
24531 **
24532 **    May you do good and not evil.
24533 **    May you find forgiveness for yourself and forgive others.
24534 **    May you share freely, never taking more than you give.
24535 **
24536 ******************************************************************************
24537 **
24538 ** This file contains macros and a little bit of code that is common to
24539 ** all of the platform-specific files (os_*.c) and is #included into those
24540 ** files.
24541 **
24542 ** This file should be #included by the os_*.c files only.  It is not a
24543 ** general purpose header file.
24544 */
24545 #ifndef _OS_COMMON_H_
24546 #define _OS_COMMON_H_
24547 
24548 /*
24549 ** At least two bugs have slipped in because we changed the MEMORY_DEBUG
24550 ** macro to SQLITE_DEBUG and some older makefiles have not yet made the
24551 ** switch.  The following code should catch this problem at compile-time.
24552 */
24553 #ifdef MEMORY_DEBUG
24554 # error "The MEMORY_DEBUG macro is obsolete.  Use SQLITE_DEBUG instead."
24555 #endif
24556 
24557 #ifdef SQLITE_DEBUG
24558 SQLITE_PRIVATE int sqlite3OSTrace = 0;
24559 #define OSTRACE(X)          if( sqlite3OSTrace ) sqlite3DebugPrintf X
24560 #else
24561 #define OSTRACE(X)
24562 #endif
24563 
24564 /*
24565 ** Macros for performance tracing.  Normally turned off.  Only works
24566 ** on i486 hardware.
24567 */
24568 #ifdef SQLITE_PERFORMANCE_TRACE
24569 
24570 /*
24571 ** hwtime.h contains inline assembler code for implementing
24572 ** high-performance timing routines.
24573 */
24574 /************** Include hwtime.h in the middle of os_common.h ****************/
24575 /************** Begin file hwtime.h ******************************************/
24576 /*
24577 ** 2008 May 27
24578 **
24579 ** The author disclaims copyright to this source code.  In place of
24580 ** a legal notice, here is a blessing:
24581 **
24582 **    May you do good and not evil.
24583 **    May you find forgiveness for yourself and forgive others.
24584 **    May you share freely, never taking more than you give.
24585 **
24586 ******************************************************************************
24587 **
24588 ** This file contains inline asm code for retrieving "high-performance"
24589 ** counters for x86 class CPUs.
24590 */
24591 #ifndef _HWTIME_H_
24592 #define _HWTIME_H_
24593 
24594 /*
24595 ** The following routine only works on pentium-class (or newer) processors.
24596 ** It uses the RDTSC opcode to read the cycle count value out of the
24597 ** processor and returns that value.  This can be used for high-res
24598 ** profiling.
24599 */
24600 #if (defined(__GNUC__) || defined(_MSC_VER)) && \
24601       (defined(i386) || defined(__i386__) || defined(_M_IX86))
24602 
24603   #if defined(__GNUC__)
24604 
24605   __inline__ sqlite_uint64 sqlite3Hwtime(void){
24606      unsigned int lo, hi;
24607      __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
24608      return (sqlite_uint64)hi << 32 | lo;
24609   }
24610 
24611   #elif defined(_MSC_VER)
24612 
24613   __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
24614      __asm {
24615         rdtsc
24616         ret       ; return value at EDX:EAX
24617      }
24618   }
24619 
24620   #endif
24621 
24622 #elif (defined(__GNUC__) && defined(__x86_64__))
24623 
24624   __inline__ sqlite_uint64 sqlite3Hwtime(void){
24625       unsigned long val;
24626       __asm__ __volatile__ ("rdtsc" : "=A" (val));
24627       return val;
24628   }
24629 
24630 #elif (defined(__GNUC__) && defined(__ppc__))
24631 
24632   __inline__ sqlite_uint64 sqlite3Hwtime(void){
24633       unsigned long long retval;
24634       unsigned long junk;
24635       __asm__ __volatile__ ("\n\
24636           1:      mftbu   %1\n\
24637                   mftb    %L0\n\
24638                   mftbu   %0\n\
24639                   cmpw    %0,%1\n\
24640                   bne     1b"
24641                   : "=r" (retval), "=r" (junk));
24642       return retval;
24643   }
24644 
24645 #else
24646 
24647   #error Need implementation of sqlite3Hwtime() for your platform.
24648 
24649   /*
24650   ** To compile without implementing sqlite3Hwtime() for your platform,
24651   ** you can remove the above #error and use the following
24652   ** stub function.  You will lose timing support for many
24653   ** of the debugging and testing utilities, but it should at
24654   ** least compile and run.
24655   */
24656 SQLITE_PRIVATE   sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
24657 
24658 #endif
24659 
24660 #endif /* !defined(_HWTIME_H_) */
24661 
24662 /************** End of hwtime.h **********************************************/
24663 /************** Continuing where we left off in os_common.h ******************/
24664 
24665 static sqlite_uint64 g_start;
24666 static sqlite_uint64 g_elapsed;
24667 #define TIMER_START       g_start=sqlite3Hwtime()
24668 #define TIMER_END         g_elapsed=sqlite3Hwtime()-g_start
24669 #define TIMER_ELAPSED     g_elapsed
24670 #else
24671 #define TIMER_START
24672 #define TIMER_END
24673 #define TIMER_ELAPSED     ((sqlite_uint64)0)
24674 #endif
24675 
24676 /*
24677 ** If we compile with the SQLITE_TEST macro set, then the following block
24678 ** of code will give us the ability to simulate a disk I/O error.  This
24679 ** is used for testing the I/O recovery logic.
24680 */
24681 #ifdef SQLITE_TEST
24682 SQLITE_API int sqlite3_io_error_hit = 0;            /* Total number of I/O Errors */
24683 SQLITE_API int sqlite3_io_error_hardhit = 0;        /* Number of non-benign errors */
24684 SQLITE_API int sqlite3_io_error_pending = 0;        /* Count down to first I/O error */
24685 SQLITE_API int sqlite3_io_error_persist = 0;        /* True if I/O errors persist */
24686 SQLITE_API int sqlite3_io_error_benign = 0;         /* True if errors are benign */
24687 SQLITE_API int sqlite3_diskfull_pending = 0;
24688 SQLITE_API int sqlite3_diskfull = 0;
24689 #define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X)
24690 #define SimulateIOError(CODE)  \
24691   if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \
24692        || sqlite3_io_error_pending-- == 1 )  \
24693               { local_ioerr(); CODE; }
24694 static void local_ioerr(){
24695   IOTRACE(("IOERR\n"));
24696   sqlite3_io_error_hit++;
24697   if( !sqlite3_io_error_benign ) sqlite3_io_error_hardhit++;
24698 }
24699 #define SimulateDiskfullError(CODE) \
24700    if( sqlite3_diskfull_pending ){ \
24701      if( sqlite3_diskfull_pending == 1 ){ \
24702        local_ioerr(); \
24703        sqlite3_diskfull = 1; \
24704        sqlite3_io_error_hit = 1; \
24705        CODE; \
24706      }else{ \
24707        sqlite3_diskfull_pending--; \
24708      } \
24709    }
24710 #else
24711 #define SimulateIOErrorBenign(X)
24712 #define SimulateIOError(A)
24713 #define SimulateDiskfullError(A)
24714 #endif
24715 
24716 /*
24717 ** When testing, keep a count of the number of open files.
24718 */
24719 #ifdef SQLITE_TEST
24720 SQLITE_API int sqlite3_open_file_count = 0;
24721 #define OpenCounter(X)  sqlite3_open_file_count+=(X)
24722 #else
24723 #define OpenCounter(X)
24724 #endif
24725 
24726 #endif /* !defined(_OS_COMMON_H_) */
24727 
24728 /************** End of os_common.h *******************************************/
24729 /************** Continuing where we left off in os_unix.c ********************/
24730 
24731 /*
24732 ** Define various macros that are missing from some systems.
24733 */
24734 #ifndef O_LARGEFILE
24735 # define O_LARGEFILE 0
24736 #endif
24737 #ifdef SQLITE_DISABLE_LFS
24738 # undef O_LARGEFILE
24739 # define O_LARGEFILE 0
24740 #endif
24741 #ifndef O_NOFOLLOW
24742 # define O_NOFOLLOW 0
24743 #endif
24744 #ifndef O_BINARY
24745 # define O_BINARY 0
24746 #endif
24747 
24748 /*
24749 ** The threadid macro resolves to the thread-id or to 0.  Used for
24750 ** testing and debugging only.
24751 */
24752 #if SQLITE_THREADSAFE
24753 #define threadid pthread_self()
24754 #else
24755 #define threadid 0
24756 #endif
24757 
24758 /*
24759 ** Different Unix systems declare open() in different ways.  Same use
24760 ** open(const char*,int,mode_t).  Others use open(const char*,int,...).
24761 ** The difference is important when using a pointer to the function.
24762 **
24763 ** The safest way to deal with the problem is to always use this wrapper
24764 ** which always has the same well-defined interface.
24765 */
24766 static int posixOpen(const char *zFile, int flags, int mode){
24767   return open(zFile, flags, mode);
24768 }
24769 
24770 /*
24771 ** Many system calls are accessed through pointer-to-functions so that
24772 ** they may be overridden at runtime to facilitate fault injection during
24773 ** testing and sandboxing.  The following array holds the names and pointers
24774 ** to all overrideable system calls.
24775 */
24776 static struct unix_syscall {
24777   const char *zName;            /* Name of the sytem call */
24778   sqlite3_syscall_ptr pCurrent; /* Current value of the system call */
24779   sqlite3_syscall_ptr pDefault; /* Default value */
24780 } aSyscall[] = {
24781   { "open",         (sqlite3_syscall_ptr)posixOpen,  0  },
24782 #define osOpen      ((int(*)(const char*,int,int))aSyscall[0].pCurrent)
24783 
24784   { "close",        (sqlite3_syscall_ptr)close,      0  },
24785 #define osClose     ((int(*)(int))aSyscall[1].pCurrent)
24786 
24787   { "access",       (sqlite3_syscall_ptr)access,     0  },
24788 #define osAccess    ((int(*)(const char*,int))aSyscall[2].pCurrent)
24789 
24790   { "getcwd",       (sqlite3_syscall_ptr)getcwd,     0  },
24791 #define osGetcwd    ((char*(*)(char*,size_t))aSyscall[3].pCurrent)
24792 
24793   { "stat",         (sqlite3_syscall_ptr)stat,       0  },
24794 #define osStat      ((int(*)(const char*,struct stat*))aSyscall[4].pCurrent)
24795 
24796 /*
24797 ** The DJGPP compiler environment looks mostly like Unix, but it
24798 ** lacks the fcntl() system call.  So redefine fcntl() to be something
24799 ** that always succeeds.  This means that locking does not occur under
24800 ** DJGPP.  But it is DOS - what did you expect?
24801 */
24802 #ifdef __DJGPP__
24803   { "fstat",        0,                 0  },
24804 #define osFstat(a,b,c)    0
24805 #else
24806   { "fstat",        (sqlite3_syscall_ptr)fstat,      0  },
24807 #define osFstat     ((int(*)(int,struct stat*))aSyscall[5].pCurrent)
24808 #endif
24809 
24810   { "ftruncate",    (sqlite3_syscall_ptr)ftruncate,  0  },
24811 #define osFtruncate ((int(*)(int,off_t))aSyscall[6].pCurrent)
24812 
24813   { "fcntl",        (sqlite3_syscall_ptr)fcntl,      0  },
24814 #define osFcntl     ((int(*)(int,int,...))aSyscall[7].pCurrent)
24815 
24816   { "read",         (sqlite3_syscall_ptr)read,       0  },
24817 #define osRead      ((ssize_t(*)(int,void*,size_t))aSyscall[8].pCurrent)
24818 
24819 #if defined(USE_PREAD) || SQLITE_ENABLE_LOCKING_STYLE
24820   { "pread",        (sqlite3_syscall_ptr)pread,      0  },
24821 #else
24822   { "pread",        (sqlite3_syscall_ptr)0,          0  },
24823 #endif
24824 #define osPread     ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[9].pCurrent)
24825 
24826 #if defined(USE_PREAD64)
24827   { "pread64",      (sqlite3_syscall_ptr)pread64,    0  },
24828 #else
24829   { "pread64",      (sqlite3_syscall_ptr)0,          0  },
24830 #endif
24831 #define osPread64   ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[10].pCurrent)
24832 
24833   { "write",        (sqlite3_syscall_ptr)write,      0  },
24834 #define osWrite     ((ssize_t(*)(int,const void*,size_t))aSyscall[11].pCurrent)
24835 
24836 #if defined(USE_PREAD) || SQLITE_ENABLE_LOCKING_STYLE
24837   { "pwrite",       (sqlite3_syscall_ptr)pwrite,     0  },
24838 #else
24839   { "pwrite",       (sqlite3_syscall_ptr)0,          0  },
24840 #endif
24841 #define osPwrite    ((ssize_t(*)(int,const void*,size_t,off_t))\
24842                     aSyscall[12].pCurrent)
24843 
24844 #if defined(USE_PREAD64)
24845   { "pwrite64",     (sqlite3_syscall_ptr)pwrite64,   0  },
24846 #else
24847   { "pwrite64",     (sqlite3_syscall_ptr)0,          0  },
24848 #endif
24849 #define osPwrite64  ((ssize_t(*)(int,const void*,size_t,off_t))\
24850                     aSyscall[13].pCurrent)
24851 
24852 #if SQLITE_ENABLE_LOCKING_STYLE
24853   { "fchmod",       (sqlite3_syscall_ptr)fchmod,     0  },
24854 #else
24855   { "fchmod",       (sqlite3_syscall_ptr)0,          0  },
24856 #endif
24857 #define osFchmod    ((int(*)(int,mode_t))aSyscall[14].pCurrent)
24858 
24859 #if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE
24860   { "fallocate",    (sqlite3_syscall_ptr)posix_fallocate,  0 },
24861 #else
24862   { "fallocate",    (sqlite3_syscall_ptr)0,                0 },
24863 #endif
24864 #define osFallocate ((int(*)(int,off_t,off_t))aSyscall[15].pCurrent)
24865 
24866 }; /* End of the overrideable system calls */
24867 
24868 /*
24869 ** This is the xSetSystemCall() method of sqlite3_vfs for all of the
24870 ** "unix" VFSes.  Return SQLITE_OK opon successfully updating the
24871 ** system call pointer, or SQLITE_NOTFOUND if there is no configurable
24872 ** system call named zName.
24873 */
24874 static int unixSetSystemCall(
24875   sqlite3_vfs *pNotUsed,        /* The VFS pointer.  Not used */
24876   const char *zName,            /* Name of system call to override */
24877   sqlite3_syscall_ptr pNewFunc  /* Pointer to new system call value */
24878 ){
24879   unsigned int i;
24880   int rc = SQLITE_NOTFOUND;
24881 
24882   UNUSED_PARAMETER(pNotUsed);
24883   if( zName==0 ){
24884     /* If no zName is given, restore all system calls to their default
24885     ** settings and return NULL
24886     */
24887     rc = SQLITE_OK;
24888     for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
24889       if( aSyscall[i].pDefault ){
24890         aSyscall[i].pCurrent = aSyscall[i].pDefault;
24891       }
24892     }
24893   }else{
24894     /* If zName is specified, operate on only the one system call
24895     ** specified.
24896     */
24897     for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
24898       if( strcmp(zName, aSyscall[i].zName)==0 ){
24899         if( aSyscall[i].pDefault==0 ){
24900           aSyscall[i].pDefault = aSyscall[i].pCurrent;
24901         }
24902         rc = SQLITE_OK;
24903         if( pNewFunc==0 ) pNewFunc = aSyscall[i].pDefault;
24904         aSyscall[i].pCurrent = pNewFunc;
24905         break;
24906       }
24907     }
24908   }
24909   return rc;
24910 }
24911 
24912 /*
24913 ** Return the value of a system call.  Return NULL if zName is not a
24914 ** recognized system call name.  NULL is also returned if the system call
24915 ** is currently undefined.
24916 */
24917 static sqlite3_syscall_ptr unixGetSystemCall(
24918   sqlite3_vfs *pNotUsed,
24919   const char *zName
24920 ){
24921   unsigned int i;
24922 
24923   UNUSED_PARAMETER(pNotUsed);
24924   for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
24925     if( strcmp(zName, aSyscall[i].zName)==0 ) return aSyscall[i].pCurrent;
24926   }
24927   return 0;
24928 }
24929 
24930 /*
24931 ** Return the name of the first system call after zName.  If zName==NULL
24932 ** then return the name of the first system call.  Return NULL if zName
24933 ** is the last system call or if zName is not the name of a valid
24934 ** system call.
24935 */
24936 static const char *unixNextSystemCall(sqlite3_vfs *p, const char *zName){
24937   int i = -1;
24938 
24939   UNUSED_PARAMETER(p);
24940   if( zName ){
24941     for(i=0; i<ArraySize(aSyscall)-1; i++){
24942       if( strcmp(zName, aSyscall[i].zName)==0 ) break;
24943     }
24944   }
24945   for(i++; i<ArraySize(aSyscall); i++){
24946     if( aSyscall[i].pCurrent!=0 ) return aSyscall[i].zName;
24947   }
24948   return 0;
24949 }
24950 
24951 /*
24952 ** Retry open() calls that fail due to EINTR
24953 */
24954 static int robust_open(const char *z, int f, int m){
24955   int rc;
24956   do{ rc = osOpen(z,f,m); }while( rc<0 && errno==EINTR );
24957   return rc;
24958 }
24959 
24960 /*
24961 ** Helper functions to obtain and relinquish the global mutex. The
24962 ** global mutex is used to protect the unixInodeInfo and
24963 ** vxworksFileId objects used by this file, all of which may be
24964 ** shared by multiple threads.
24965 **
24966 ** Function unixMutexHeld() is used to assert() that the global mutex
24967 ** is held when required. This function is only used as part of assert()
24968 ** statements. e.g.
24969 **
24970 **   unixEnterMutex()
24971 **     assert( unixMutexHeld() );
24972 **   unixEnterLeave()
24973 */
24974 static void unixEnterMutex(void){
24975   sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
24976 }
24977 static void unixLeaveMutex(void){
24978   sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
24979 }
24980 #ifdef SQLITE_DEBUG
24981 static int unixMutexHeld(void) {
24982   return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
24983 }
24984 #endif
24985 
24986 
24987 #ifdef SQLITE_DEBUG
24988 /*
24989 ** Helper function for printing out trace information from debugging
24990 ** binaries. This returns the string represetation of the supplied
24991 ** integer lock-type.
24992 */
24993 static const char *azFileLock(int eFileLock){
24994   switch( eFileLock ){
24995     case NO_LOCK: return "NONE";
24996     case SHARED_LOCK: return "SHARED";
24997     case RESERVED_LOCK: return "RESERVED";
24998     case PENDING_LOCK: return "PENDING";
24999     case EXCLUSIVE_LOCK: return "EXCLUSIVE";
25000   }
25001   return "ERROR";
25002 }
25003 #endif
25004 
25005 #ifdef SQLITE_LOCK_TRACE
25006 /*
25007 ** Print out information about all locking operations.
25008 **
25009 ** This routine is used for troubleshooting locks on multithreaded
25010 ** platforms.  Enable by compiling with the -DSQLITE_LOCK_TRACE
25011 ** command-line option on the compiler.  This code is normally
25012 ** turned off.
25013 */
25014 static int lockTrace(int fd, int op, struct flock *p){
25015   char *zOpName, *zType;
25016   int s;
25017   int savedErrno;
25018   if( op==F_GETLK ){
25019     zOpName = "GETLK";
25020   }else if( op==F_SETLK ){
25021     zOpName = "SETLK";
25022   }else{
25023     s = osFcntl(fd, op, p);
25024     sqlite3DebugPrintf("fcntl unknown %d %d %d\n", fd, op, s);
25025     return s;
25026   }
25027   if( p->l_type==F_RDLCK ){
25028     zType = "RDLCK";
25029   }else if( p->l_type==F_WRLCK ){
25030     zType = "WRLCK";
25031   }else if( p->l_type==F_UNLCK ){
25032     zType = "UNLCK";
25033   }else{
25034     assert( 0 );
25035   }
25036   assert( p->l_whence==SEEK_SET );
25037   s = osFcntl(fd, op, p);
25038   savedErrno = errno;
25039   sqlite3DebugPrintf("fcntl %d %d %s %s %d %d %d %d\n",
25040      threadid, fd, zOpName, zType, (int)p->l_start, (int)p->l_len,
25041      (int)p->l_pid, s);
25042   if( s==(-1) && op==F_SETLK && (p->l_type==F_RDLCK || p->l_type==F_WRLCK) ){
25043     struct flock l2;
25044     l2 = *p;
25045     osFcntl(fd, F_GETLK, &l2);
25046     if( l2.l_type==F_RDLCK ){
25047       zType = "RDLCK";
25048     }else if( l2.l_type==F_WRLCK ){
25049       zType = "WRLCK";
25050     }else if( l2.l_type==F_UNLCK ){
25051       zType = "UNLCK";
25052     }else{
25053       assert( 0 );
25054     }
25055     sqlite3DebugPrintf("fcntl-failure-reason: %s %d %d %d\n",
25056        zType, (int)l2.l_start, (int)l2.l_len, (int)l2.l_pid);
25057   }
25058   errno = savedErrno;
25059   return s;
25060 }
25061 #undef osFcntl
25062 #define osFcntl lockTrace
25063 #endif /* SQLITE_LOCK_TRACE */
25064 
25065 /*
25066 ** Retry ftruncate() calls that fail due to EINTR
25067 */
25068 static int robust_ftruncate(int h, sqlite3_int64 sz){
25069   int rc;
25070   do{ rc = osFtruncate(h,sz); }while( rc<0 && errno==EINTR );
25071   return rc;
25072 }
25073 
25074 /*
25075 ** This routine translates a standard POSIX errno code into something
25076 ** useful to the clients of the sqlite3 functions.  Specifically, it is
25077 ** intended to translate a variety of "try again" errors into SQLITE_BUSY
25078 ** and a variety of "please close the file descriptor NOW" errors into
25079 ** SQLITE_IOERR
25080 **
25081 ** Errors during initialization of locks, or file system support for locks,
25082 ** should handle ENOLCK, ENOTSUP, EOPNOTSUPP separately.
25083 */
25084 static int sqliteErrorFromPosixError(int posixError, int sqliteIOErr) {
25085   switch (posixError) {
25086 #if 0
25087   /* At one point this code was not commented out. In theory, this branch
25088   ** should never be hit, as this function should only be called after
25089   ** a locking-related function (i.e. fcntl()) has returned non-zero with
25090   ** the value of errno as the first argument. Since a system call has failed,
25091   ** errno should be non-zero.
25092   **
25093   ** Despite this, if errno really is zero, we still don't want to return
25094   ** SQLITE_OK. The system call failed, and *some* SQLite error should be
25095   ** propagated back to the caller. Commenting this branch out means errno==0
25096   ** will be handled by the "default:" case below.
25097   */
25098   case 0:
25099     return SQLITE_OK;
25100 #endif
25101 
25102   case EAGAIN:
25103   case ETIMEDOUT:
25104   case EBUSY:
25105   case EINTR:
25106   case ENOLCK:
25107     /* random NFS retry error, unless during file system support
25108      * introspection, in which it actually means what it says */
25109     return SQLITE_BUSY;
25110 
25111   case EACCES:
25112     /* EACCES is like EAGAIN during locking operations, but not any other time*/
25113     if( (sqliteIOErr == SQLITE_IOERR_LOCK) ||
25114 	(sqliteIOErr == SQLITE_IOERR_UNLOCK) ||
25115 	(sqliteIOErr == SQLITE_IOERR_RDLOCK) ||
25116 	(sqliteIOErr == SQLITE_IOERR_CHECKRESERVEDLOCK) ){
25117       return SQLITE_BUSY;
25118     }
25119     /* else fall through */
25120   case EPERM:
25121     return SQLITE_PERM;
25122 
25123   /* EDEADLK is only possible if a call to fcntl(F_SETLKW) is made. And
25124   ** this module never makes such a call. And the code in SQLite itself
25125   ** asserts that SQLITE_IOERR_BLOCKED is never returned. For these reasons
25126   ** this case is also commented out. If the system does set errno to EDEADLK,
25127   ** the default SQLITE_IOERR_XXX code will be returned. */
25128 #if 0
25129   case EDEADLK:
25130     return SQLITE_IOERR_BLOCKED;
25131 #endif
25132 
25133 #if EOPNOTSUPP!=ENOTSUP
25134   case EOPNOTSUPP:
25135     /* something went terribly awry, unless during file system support
25136      * introspection, in which it actually means what it says */
25137 #endif
25138 #ifdef ENOTSUP
25139   case ENOTSUP:
25140     /* invalid fd, unless during file system support introspection, in which
25141      * it actually means what it says */
25142 #endif
25143   case EIO:
25144   case EBADF:
25145   case EINVAL:
25146   case ENOTCONN:
25147   case ENODEV:
25148   case ENXIO:
25149   case ENOENT:
25150   case ESTALE:
25151   case ENOSYS:
25152     /* these should force the client to close the file and reconnect */
25153 
25154   default:
25155     return sqliteIOErr;
25156   }
25157 }
25158 
25159 
25160 
25161 /******************************************************************************
25162 ****************** Begin Unique File ID Utility Used By VxWorks ***************
25163 **
25164 ** On most versions of unix, we can get a unique ID for a file by concatenating
25165 ** the device number and the inode number.  But this does not work on VxWorks.
25166 ** On VxWorks, a unique file id must be based on the canonical filename.
25167 **
25168 ** A pointer to an instance of the following structure can be used as a
25169 ** unique file ID in VxWorks.  Each instance of this structure contains
25170 ** a copy of the canonical filename.  There is also a reference count.
25171 ** The structure is reclaimed when the number of pointers to it drops to
25172 ** zero.
25173 **
25174 ** There are never very many files open at one time and lookups are not
25175 ** a performance-critical path, so it is sufficient to put these
25176 ** structures on a linked list.
25177 */
25178 struct vxworksFileId {
25179   struct vxworksFileId *pNext;  /* Next in a list of them all */
25180   int nRef;                     /* Number of references to this one */
25181   int nName;                    /* Length of the zCanonicalName[] string */
25182   char *zCanonicalName;         /* Canonical filename */
25183 };
25184 
25185 #if OS_VXWORKS
25186 /*
25187 ** All unique filenames are held on a linked list headed by this
25188 ** variable:
25189 */
25190 static struct vxworksFileId *vxworksFileList = 0;
25191 
25192 /*
25193 ** Simplify a filename into its canonical form
25194 ** by making the following changes:
25195 **
25196 **  * removing any trailing and duplicate /
25197 **  * convert /./ into just /
25198 **  * convert /A/../ where A is any simple name into just /
25199 **
25200 ** Changes are made in-place.  Return the new name length.
25201 **
25202 ** The original filename is in z[0..n-1].  Return the number of
25203 ** characters in the simplified name.
25204 */
25205 static int vxworksSimplifyName(char *z, int n){
25206   int i, j;
25207   while( n>1 && z[n-1]=='/' ){ n--; }
25208   for(i=j=0; i<n; i++){
25209     if( z[i]=='/' ){
25210       if( z[i+1]=='/' ) continue;
25211       if( z[i+1]=='.' && i+2<n && z[i+2]=='/' ){
25212         i += 1;
25213         continue;
25214       }
25215       if( z[i+1]=='.' && i+3<n && z[i+2]=='.' && z[i+3]=='/' ){
25216         while( j>0 && z[j-1]!='/' ){ j--; }
25217         if( j>0 ){ j--; }
25218         i += 2;
25219         continue;
25220       }
25221     }
25222     z[j++] = z[i];
25223   }
25224   z[j] = 0;
25225   return j;
25226 }
25227 
25228 /*
25229 ** Find a unique file ID for the given absolute pathname.  Return
25230 ** a pointer to the vxworksFileId object.  This pointer is the unique
25231 ** file ID.
25232 **
25233 ** The nRef field of the vxworksFileId object is incremented before
25234 ** the object is returned.  A new vxworksFileId object is created
25235 ** and added to the global list if necessary.
25236 **
25237 ** If a memory allocation error occurs, return NULL.
25238 */
25239 static struct vxworksFileId *vxworksFindFileId(const char *zAbsoluteName){
25240   struct vxworksFileId *pNew;         /* search key and new file ID */
25241   struct vxworksFileId *pCandidate;   /* For looping over existing file IDs */
25242   int n;                              /* Length of zAbsoluteName string */
25243 
25244   assert( zAbsoluteName[0]=='/' );
25245   n = (int)strlen(zAbsoluteName);
25246   pNew = sqlite3_malloc( sizeof(*pNew) + (n+1) );
25247   if( pNew==0 ) return 0;
25248   pNew->zCanonicalName = (char*)&pNew[1];
25249   memcpy(pNew->zCanonicalName, zAbsoluteName, n+1);
25250   n = vxworksSimplifyName(pNew->zCanonicalName, n);
25251 
25252   /* Search for an existing entry that matching the canonical name.
25253   ** If found, increment the reference count and return a pointer to
25254   ** the existing file ID.
25255   */
25256   unixEnterMutex();
25257   for(pCandidate=vxworksFileList; pCandidate; pCandidate=pCandidate->pNext){
25258     if( pCandidate->nName==n
25259      && memcmp(pCandidate->zCanonicalName, pNew->zCanonicalName, n)==0
25260     ){
25261        sqlite3_free(pNew);
25262        pCandidate->nRef++;
25263        unixLeaveMutex();
25264        return pCandidate;
25265     }
25266   }
25267 
25268   /* No match was found.  We will make a new file ID */
25269   pNew->nRef = 1;
25270   pNew->nName = n;
25271   pNew->pNext = vxworksFileList;
25272   vxworksFileList = pNew;
25273   unixLeaveMutex();
25274   return pNew;
25275 }
25276 
25277 /*
25278 ** Decrement the reference count on a vxworksFileId object.  Free
25279 ** the object when the reference count reaches zero.
25280 */
25281 static void vxworksReleaseFileId(struct vxworksFileId *pId){
25282   unixEnterMutex();
25283   assert( pId->nRef>0 );
25284   pId->nRef--;
25285   if( pId->nRef==0 ){
25286     struct vxworksFileId **pp;
25287     for(pp=&vxworksFileList; *pp && *pp!=pId; pp = &((*pp)->pNext)){}
25288     assert( *pp==pId );
25289     *pp = pId->pNext;
25290     sqlite3_free(pId);
25291   }
25292   unixLeaveMutex();
25293 }
25294 #endif /* OS_VXWORKS */
25295 /*************** End of Unique File ID Utility Used By VxWorks ****************
25296 ******************************************************************************/
25297 
25298 
25299 /******************************************************************************
25300 *************************** Posix Advisory Locking ****************************
25301 **
25302 ** POSIX advisory locks are broken by design.  ANSI STD 1003.1 (1996)
25303 ** section 6.5.2.2 lines 483 through 490 specify that when a process
25304 ** sets or clears a lock, that operation overrides any prior locks set
25305 ** by the same process.  It does not explicitly say so, but this implies
25306 ** that it overrides locks set by the same process using a different
25307 ** file descriptor.  Consider this test case:
25308 **
25309 **       int fd1 = open("./file1", O_RDWR|O_CREAT, 0644);
25310 **       int fd2 = open("./file2", O_RDWR|O_CREAT, 0644);
25311 **
25312 ** Suppose ./file1 and ./file2 are really the same file (because
25313 ** one is a hard or symbolic link to the other) then if you set
25314 ** an exclusive lock on fd1, then try to get an exclusive lock
25315 ** on fd2, it works.  I would have expected the second lock to
25316 ** fail since there was already a lock on the file due to fd1.
25317 ** But not so.  Since both locks came from the same process, the
25318 ** second overrides the first, even though they were on different
25319 ** file descriptors opened on different file names.
25320 **
25321 ** This means that we cannot use POSIX locks to synchronize file access
25322 ** among competing threads of the same process.  POSIX locks will work fine
25323 ** to synchronize access for threads in separate processes, but not
25324 ** threads within the same process.
25325 **
25326 ** To work around the problem, SQLite has to manage file locks internally
25327 ** on its own.  Whenever a new database is opened, we have to find the
25328 ** specific inode of the database file (the inode is determined by the
25329 ** st_dev and st_ino fields of the stat structure that fstat() fills in)
25330 ** and check for locks already existing on that inode.  When locks are
25331 ** created or removed, we have to look at our own internal record of the
25332 ** locks to see if another thread has previously set a lock on that same
25333 ** inode.
25334 **
25335 ** (Aside: The use of inode numbers as unique IDs does not work on VxWorks.
25336 ** For VxWorks, we have to use the alternative unique ID system based on
25337 ** canonical filename and implemented in the previous division.)
25338 **
25339 ** The sqlite3_file structure for POSIX is no longer just an integer file
25340 ** descriptor.  It is now a structure that holds the integer file
25341 ** descriptor and a pointer to a structure that describes the internal
25342 ** locks on the corresponding inode.  There is one locking structure
25343 ** per inode, so if the same inode is opened twice, both unixFile structures
25344 ** point to the same locking structure.  The locking structure keeps
25345 ** a reference count (so we will know when to delete it) and a "cnt"
25346 ** field that tells us its internal lock status.  cnt==0 means the
25347 ** file is unlocked.  cnt==-1 means the file has an exclusive lock.
25348 ** cnt>0 means there are cnt shared locks on the file.
25349 **
25350 ** Any attempt to lock or unlock a file first checks the locking
25351 ** structure.  The fcntl() system call is only invoked to set a
25352 ** POSIX lock if the internal lock structure transitions between
25353 ** a locked and an unlocked state.
25354 **
25355 ** But wait:  there are yet more problems with POSIX advisory locks.
25356 **
25357 ** If you close a file descriptor that points to a file that has locks,
25358 ** all locks on that file that are owned by the current process are
25359 ** released.  To work around this problem, each unixInodeInfo object
25360 ** maintains a count of the number of pending locks on tha inode.
25361 ** When an attempt is made to close an unixFile, if there are
25362 ** other unixFile open on the same inode that are holding locks, the call
25363 ** to close() the file descriptor is deferred until all of the locks clear.
25364 ** The unixInodeInfo structure keeps a list of file descriptors that need to
25365 ** be closed and that list is walked (and cleared) when the last lock
25366 ** clears.
25367 **
25368 ** Yet another problem:  LinuxThreads do not play well with posix locks.
25369 **
25370 ** Many older versions of linux use the LinuxThreads library which is
25371 ** not posix compliant.  Under LinuxThreads, a lock created by thread
25372 ** A cannot be modified or overridden by a different thread B.
25373 ** Only thread A can modify the lock.  Locking behavior is correct
25374 ** if the appliation uses the newer Native Posix Thread Library (NPTL)
25375 ** on linux - with NPTL a lock created by thread A can override locks
25376 ** in thread B.  But there is no way to know at compile-time which
25377 ** threading library is being used.  So there is no way to know at
25378 ** compile-time whether or not thread A can override locks on thread B.
25379 ** One has to do a run-time check to discover the behavior of the
25380 ** current process.
25381 **
25382 ** SQLite used to support LinuxThreads.  But support for LinuxThreads
25383 ** was dropped beginning with version 3.7.0.  SQLite will still work with
25384 ** LinuxThreads provided that (1) there is no more than one connection
25385 ** per database file in the same process and (2) database connections
25386 ** do not move across threads.
25387 */
25388 
25389 /*
25390 ** An instance of the following structure serves as the key used
25391 ** to locate a particular unixInodeInfo object.
25392 */
25393 struct unixFileId {
25394   dev_t dev;                  /* Device number */
25395 #if OS_VXWORKS
25396   struct vxworksFileId *pId;  /* Unique file ID for vxworks. */
25397 #else
25398   ino_t ino;                  /* Inode number */
25399 #endif
25400 };
25401 
25402 /*
25403 ** An instance of the following structure is allocated for each open
25404 ** inode.  Or, on LinuxThreads, there is one of these structures for
25405 ** each inode opened by each thread.
25406 **
25407 ** A single inode can have multiple file descriptors, so each unixFile
25408 ** structure contains a pointer to an instance of this object and this
25409 ** object keeps a count of the number of unixFile pointing to it.
25410 */
25411 struct unixInodeInfo {
25412   struct unixFileId fileId;       /* The lookup key */
25413   int nShared;                    /* Number of SHARED locks held */
25414   unsigned char eFileLock;        /* One of SHARED_LOCK, RESERVED_LOCK etc. */
25415   unsigned char bProcessLock;     /* An exclusive process lock is held */
25416   int nRef;                       /* Number of pointers to this structure */
25417   unixShmNode *pShmNode;          /* Shared memory associated with this inode */
25418   int nLock;                      /* Number of outstanding file locks */
25419   UnixUnusedFd *pUnused;          /* Unused file descriptors to close */
25420   unixInodeInfo *pNext;           /* List of all unixInodeInfo objects */
25421   unixInodeInfo *pPrev;           /*    .... doubly linked */
25422 #if SQLITE_ENABLE_LOCKING_STYLE
25423   unsigned long long sharedByte;  /* for AFP simulated shared lock */
25424 #endif
25425 #if OS_VXWORKS
25426   sem_t *pSem;                    /* Named POSIX semaphore */
25427   char aSemName[MAX_PATHNAME+2];  /* Name of that semaphore */
25428 #endif
25429 };
25430 
25431 /*
25432 ** A lists of all unixInodeInfo objects.
25433 */
25434 static unixInodeInfo *inodeList = 0;
25435 
25436 /*
25437 **
25438 ** This function - unixLogError_x(), is only ever called via the macro
25439 ** unixLogError().
25440 **
25441 ** It is invoked after an error occurs in an OS function and errno has been
25442 ** set. It logs a message using sqlite3_log() containing the current value of
25443 ** errno and, if possible, the human-readable equivalent from strerror() or
25444 ** strerror_r().
25445 **
25446 ** The first argument passed to the macro should be the error code that
25447 ** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN).
25448 ** The two subsequent arguments should be the name of the OS function that
25449 ** failed (e.g. "unlink", "open") and the the associated file-system path,
25450 ** if any.
25451 */
25452 #define unixLogError(a,b,c)     unixLogErrorAtLine(a,b,c,__LINE__)
25453 static int unixLogErrorAtLine(
25454   int errcode,                    /* SQLite error code */
25455   const char *zFunc,              /* Name of OS function that failed */
25456   const char *zPath,              /* File path associated with error */
25457   int iLine                       /* Source line number where error occurred */
25458 ){
25459   char *zErr;                     /* Message from strerror() or equivalent */
25460   int iErrno = errno;             /* Saved syscall error number */
25461 
25462   /* If this is not a threadsafe build (SQLITE_THREADSAFE==0), then use
25463   ** the strerror() function to obtain the human-readable error message
25464   ** equivalent to errno. Otherwise, use strerror_r().
25465   */
25466 #if SQLITE_THREADSAFE && defined(HAVE_STRERROR_R)
25467   char aErr[80];
25468   memset(aErr, 0, sizeof(aErr));
25469   zErr = aErr;
25470 
25471   /* If STRERROR_R_CHAR_P (set by autoconf scripts) or __USE_GNU is defined,
25472   ** assume that the system provides the the GNU version of strerror_r() that
25473   ** returns a pointer to a buffer containing the error message. That pointer
25474   ** may point to aErr[], or it may point to some static storage somewhere.
25475   ** Otherwise, assume that the system provides the POSIX version of
25476   ** strerror_r(), which always writes an error message into aErr[].
25477   **
25478   ** If the code incorrectly assumes that it is the POSIX version that is
25479   ** available, the error message will often be an empty string. Not a
25480   ** huge problem. Incorrectly concluding that the GNU version is available
25481   ** could lead to a segfault though.
25482   */
25483 #if defined(STRERROR_R_CHAR_P) || defined(__USE_GNU)
25484   zErr =
25485 # endif
25486   strerror_r(iErrno, aErr, sizeof(aErr)-1);
25487 
25488 #elif SQLITE_THREADSAFE
25489   /* This is a threadsafe build, but strerror_r() is not available. */
25490   zErr = "";
25491 #else
25492   /* Non-threadsafe build, use strerror(). */
25493   zErr = strerror(iErrno);
25494 #endif
25495 
25496   assert( errcode!=SQLITE_OK );
25497   if( zPath==0 ) zPath = "";
25498   sqlite3_log(errcode,
25499       "os_unix.c:%d: (%d) %s(%s) - %s",
25500       iLine, iErrno, zFunc, zPath, zErr
25501   );
25502 
25503   return errcode;
25504 }
25505 
25506 /*
25507 ** Close a file descriptor.
25508 **
25509 ** We assume that close() almost always works, since it is only in a
25510 ** very sick application or on a very sick platform that it might fail.
25511 ** If it does fail, simply leak the file descriptor, but do log the
25512 ** error.
25513 **
25514 ** Note that it is not safe to retry close() after EINTR since the
25515 ** file descriptor might have already been reused by another thread.
25516 ** So we don't even try to recover from an EINTR.  Just log the error
25517 ** and move on.
25518 */
25519 static void robust_close(unixFile *pFile, int h, int lineno){
25520   if( osClose(h) ){
25521     unixLogErrorAtLine(SQLITE_IOERR_CLOSE, "close",
25522                        pFile ? pFile->zPath : 0, lineno);
25523   }
25524 }
25525 
25526 /*
25527 ** Close all file descriptors accumuated in the unixInodeInfo->pUnused list.
25528 */
25529 static void closePendingFds(unixFile *pFile){
25530   unixInodeInfo *pInode = pFile->pInode;
25531   UnixUnusedFd *p;
25532   UnixUnusedFd *pNext;
25533   for(p=pInode->pUnused; p; p=pNext){
25534     pNext = p->pNext;
25535     robust_close(pFile, p->fd, __LINE__);
25536     sqlite3_free(p);
25537   }
25538   pInode->pUnused = 0;
25539 }
25540 
25541 /*
25542 ** Release a unixInodeInfo structure previously allocated by findInodeInfo().
25543 **
25544 ** The mutex entered using the unixEnterMutex() function must be held
25545 ** when this function is called.
25546 */
25547 static void releaseInodeInfo(unixFile *pFile){
25548   unixInodeInfo *pInode = pFile->pInode;
25549   assert( unixMutexHeld() );
25550   if( ALWAYS(pInode) ){
25551     pInode->nRef--;
25552     if( pInode->nRef==0 ){
25553       assert( pInode->pShmNode==0 );
25554       closePendingFds(pFile);
25555       if( pInode->pPrev ){
25556         assert( pInode->pPrev->pNext==pInode );
25557         pInode->pPrev->pNext = pInode->pNext;
25558       }else{
25559         assert( inodeList==pInode );
25560         inodeList = pInode->pNext;
25561       }
25562       if( pInode->pNext ){
25563         assert( pInode->pNext->pPrev==pInode );
25564         pInode->pNext->pPrev = pInode->pPrev;
25565       }
25566       sqlite3_free(pInode);
25567     }
25568   }
25569 }
25570 
25571 /*
25572 ** Given a file descriptor, locate the unixInodeInfo object that
25573 ** describes that file descriptor.  Create a new one if necessary.  The
25574 ** return value might be uninitialized if an error occurs.
25575 **
25576 ** The mutex entered using the unixEnterMutex() function must be held
25577 ** when this function is called.
25578 **
25579 ** Return an appropriate error code.
25580 */
25581 static int findInodeInfo(
25582   unixFile *pFile,               /* Unix file with file desc used in the key */
25583   unixInodeInfo **ppInode        /* Return the unixInodeInfo object here */
25584 ){
25585   int rc;                        /* System call return code */
25586   int fd;                        /* The file descriptor for pFile */
25587   struct unixFileId fileId;      /* Lookup key for the unixInodeInfo */
25588   struct stat statbuf;           /* Low-level file information */
25589   unixInodeInfo *pInode = 0;     /* Candidate unixInodeInfo object */
25590 
25591   assert( unixMutexHeld() );
25592 
25593   /* Get low-level information about the file that we can used to
25594   ** create a unique name for the file.
25595   */
25596   fd = pFile->h;
25597   rc = osFstat(fd, &statbuf);
25598   if( rc!=0 ){
25599     pFile->lastErrno = errno;
25600 #ifdef EOVERFLOW
25601     if( pFile->lastErrno==EOVERFLOW ) return SQLITE_NOLFS;
25602 #endif
25603     return SQLITE_IOERR;
25604   }
25605 
25606 #ifdef __APPLE__
25607   /* On OS X on an msdos filesystem, the inode number is reported
25608   ** incorrectly for zero-size files.  See ticket #3260.  To work
25609   ** around this problem (we consider it a bug in OS X, not SQLite)
25610   ** we always increase the file size to 1 by writing a single byte
25611   ** prior to accessing the inode number.  The one byte written is
25612   ** an ASCII 'S' character which also happens to be the first byte
25613   ** in the header of every SQLite database.  In this way, if there
25614   ** is a race condition such that another thread has already populated
25615   ** the first page of the database, no damage is done.
25616   */
25617   if( statbuf.st_size==0 && (pFile->fsFlags & SQLITE_FSFLAGS_IS_MSDOS)!=0 ){
25618     do{ rc = osWrite(fd, "S", 1); }while( rc<0 && errno==EINTR );
25619     if( rc!=1 ){
25620       pFile->lastErrno = errno;
25621       return SQLITE_IOERR;
25622     }
25623     rc = osFstat(fd, &statbuf);
25624     if( rc!=0 ){
25625       pFile->lastErrno = errno;
25626       return SQLITE_IOERR;
25627     }
25628   }
25629 #endif
25630 
25631   memset(&fileId, 0, sizeof(fileId));
25632   fileId.dev = statbuf.st_dev;
25633 #if OS_VXWORKS
25634   fileId.pId = pFile->pId;
25635 #else
25636   fileId.ino = statbuf.st_ino;
25637 #endif
25638   pInode = inodeList;
25639   while( pInode && memcmp(&fileId, &pInode->fileId, sizeof(fileId)) ){
25640     pInode = pInode->pNext;
25641   }
25642   if( pInode==0 ){
25643     pInode = sqlite3_malloc( sizeof(*pInode) );
25644     if( pInode==0 ){
25645       return SQLITE_NOMEM;
25646     }
25647     memset(pInode, 0, sizeof(*pInode));
25648     memcpy(&pInode->fileId, &fileId, sizeof(fileId));
25649     pInode->nRef = 1;
25650     pInode->pNext = inodeList;
25651     pInode->pPrev = 0;
25652     if( inodeList ) inodeList->pPrev = pInode;
25653     inodeList = pInode;
25654   }else{
25655     pInode->nRef++;
25656   }
25657   *ppInode = pInode;
25658   return SQLITE_OK;
25659 }
25660 
25661 
25662 /*
25663 ** This routine checks if there is a RESERVED lock held on the specified
25664 ** file by this or any other process. If such a lock is held, set *pResOut
25665 ** to a non-zero value otherwise *pResOut is set to zero.  The return value
25666 ** is set to SQLITE_OK unless an I/O error occurs during lock checking.
25667 */
25668 static int unixCheckReservedLock(sqlite3_file *id, int *pResOut){
25669   int rc = SQLITE_OK;
25670   int reserved = 0;
25671   unixFile *pFile = (unixFile*)id;
25672 
25673   SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
25674 
25675   assert( pFile );
25676   unixEnterMutex(); /* Because pFile->pInode is shared across threads */
25677 
25678   /* Check if a thread in this process holds such a lock */
25679   if( pFile->pInode->eFileLock>SHARED_LOCK ){
25680     reserved = 1;
25681   }
25682 
25683   /* Otherwise see if some other process holds it.
25684   */
25685 #ifndef __DJGPP__
25686   if( !reserved && !pFile->pInode->bProcessLock ){
25687     struct flock lock;
25688     lock.l_whence = SEEK_SET;
25689     lock.l_start = RESERVED_BYTE;
25690     lock.l_len = 1;
25691     lock.l_type = F_WRLCK;
25692     if( osFcntl(pFile->h, F_GETLK, &lock) ){
25693       rc = SQLITE_IOERR_CHECKRESERVEDLOCK;
25694       pFile->lastErrno = errno;
25695     } else if( lock.l_type!=F_UNLCK ){
25696       reserved = 1;
25697     }
25698   }
25699 #endif
25700 
25701   unixLeaveMutex();
25702   OSTRACE(("TEST WR-LOCK %d %d %d (unix)\n", pFile->h, rc, reserved));
25703 
25704   *pResOut = reserved;
25705   return rc;
25706 }
25707 
25708 /*
25709 ** Attempt to set a system-lock on the file pFile.  The lock is
25710 ** described by pLock.
25711 **
25712 ** If the pFile was opened read/write from unix-excl, then the only lock
25713 ** ever obtained is an exclusive lock, and it is obtained exactly once
25714 ** the first time any lock is attempted.  All subsequent system locking
25715 ** operations become no-ops.  Locking operations still happen internally,
25716 ** in order to coordinate access between separate database connections
25717 ** within this process, but all of that is handled in memory and the
25718 ** operating system does not participate.
25719 **
25720 ** This function is a pass-through to fcntl(F_SETLK) if pFile is using
25721 ** any VFS other than "unix-excl" or if pFile is opened on "unix-excl"
25722 ** and is read-only.
25723 **
25724 ** Zero is returned if the call completes successfully, or -1 if a call
25725 ** to fcntl() fails. In this case, errno is set appropriately (by fcntl()).
25726 */
25727 static int unixFileLock(unixFile *pFile, struct flock *pLock){
25728   int rc;
25729   unixInodeInfo *pInode = pFile->pInode;
25730   assert( unixMutexHeld() );
25731   assert( pInode!=0 );
25732   if( ((pFile->ctrlFlags & UNIXFILE_EXCL)!=0 || pInode->bProcessLock)
25733    && ((pFile->ctrlFlags & UNIXFILE_RDONLY)==0)
25734   ){
25735     if( pInode->bProcessLock==0 ){
25736       struct flock lock;
25737       assert( pInode->nLock==0 );
25738       lock.l_whence = SEEK_SET;
25739       lock.l_start = SHARED_FIRST;
25740       lock.l_len = SHARED_SIZE;
25741       lock.l_type = F_WRLCK;
25742       rc = osFcntl(pFile->h, F_SETLK, &lock);
25743       if( rc<0 ) return rc;
25744       pInode->bProcessLock = 1;
25745       pInode->nLock++;
25746     }else{
25747       rc = 0;
25748     }
25749   }else{
25750     rc = osFcntl(pFile->h, F_SETLK, pLock);
25751   }
25752   return rc;
25753 }
25754 
25755 /*
25756 ** Lock the file with the lock specified by parameter eFileLock - one
25757 ** of the following:
25758 **
25759 **     (1) SHARED_LOCK
25760 **     (2) RESERVED_LOCK
25761 **     (3) PENDING_LOCK
25762 **     (4) EXCLUSIVE_LOCK
25763 **
25764 ** Sometimes when requesting one lock state, additional lock states
25765 ** are inserted in between.  The locking might fail on one of the later
25766 ** transitions leaving the lock state different from what it started but
25767 ** still short of its goal.  The following chart shows the allowed
25768 ** transitions and the inserted intermediate states:
25769 **
25770 **    UNLOCKED -> SHARED
25771 **    SHARED -> RESERVED
25772 **    SHARED -> (PENDING) -> EXCLUSIVE
25773 **    RESERVED -> (PENDING) -> EXCLUSIVE
25774 **    PENDING -> EXCLUSIVE
25775 **
25776 ** This routine will only increase a lock.  Use the sqlite3OsUnlock()
25777 ** routine to lower a locking level.
25778 */
25779 static int unixLock(sqlite3_file *id, int eFileLock){
25780   /* The following describes the implementation of the various locks and
25781   ** lock transitions in terms of the POSIX advisory shared and exclusive
25782   ** lock primitives (called read-locks and write-locks below, to avoid
25783   ** confusion with SQLite lock names). The algorithms are complicated
25784   ** slightly in order to be compatible with windows systems simultaneously
25785   ** accessing the same database file, in case that is ever required.
25786   **
25787   ** Symbols defined in os.h indentify the 'pending byte' and the 'reserved
25788   ** byte', each single bytes at well known offsets, and the 'shared byte
25789   ** range', a range of 510 bytes at a well known offset.
25790   **
25791   ** To obtain a SHARED lock, a read-lock is obtained on the 'pending
25792   ** byte'.  If this is successful, a random byte from the 'shared byte
25793   ** range' is read-locked and the lock on the 'pending byte' released.
25794   **
25795   ** A process may only obtain a RESERVED lock after it has a SHARED lock.
25796   ** A RESERVED lock is implemented by grabbing a write-lock on the
25797   ** 'reserved byte'.
25798   **
25799   ** A process may only obtain a PENDING lock after it has obtained a
25800   ** SHARED lock. A PENDING lock is implemented by obtaining a write-lock
25801   ** on the 'pending byte'. This ensures that no new SHARED locks can be
25802   ** obtained, but existing SHARED locks are allowed to persist. A process
25803   ** does not have to obtain a RESERVED lock on the way to a PENDING lock.
25804   ** This property is used by the algorithm for rolling back a journal file
25805   ** after a crash.
25806   **
25807   ** An EXCLUSIVE lock, obtained after a PENDING lock is held, is
25808   ** implemented by obtaining a write-lock on the entire 'shared byte
25809   ** range'. Since all other locks require a read-lock on one of the bytes
25810   ** within this range, this ensures that no other locks are held on the
25811   ** database.
25812   **
25813   ** The reason a single byte cannot be used instead of the 'shared byte
25814   ** range' is that some versions of windows do not support read-locks. By
25815   ** locking a random byte from a range, concurrent SHARED locks may exist
25816   ** even if the locking primitive used is always a write-lock.
25817   */
25818   int rc = SQLITE_OK;
25819   unixFile *pFile = (unixFile*)id;
25820   unixInodeInfo *pInode = pFile->pInode;
25821   struct flock lock;
25822   int tErrno = 0;
25823 
25824   assert( pFile );
25825   OSTRACE(("LOCK    %d %s was %s(%s,%d) pid=%d (unix)\n", pFile->h,
25826       azFileLock(eFileLock), azFileLock(pFile->eFileLock),
25827       azFileLock(pInode->eFileLock), pInode->nShared , getpid()));
25828 
25829   /* If there is already a lock of this type or more restrictive on the
25830   ** unixFile, do nothing. Don't use the end_lock: exit path, as
25831   ** unixEnterMutex() hasn't been called yet.
25832   */
25833   if( pFile->eFileLock>=eFileLock ){
25834     OSTRACE(("LOCK    %d %s ok (already held) (unix)\n", pFile->h,
25835             azFileLock(eFileLock)));
25836     return SQLITE_OK;
25837   }
25838 
25839   /* Make sure the locking sequence is correct.
25840   **  (1) We never move from unlocked to anything higher than shared lock.
25841   **  (2) SQLite never explicitly requests a pendig lock.
25842   **  (3) A shared lock is always held when a reserve lock is requested.
25843   */
25844   assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK );
25845   assert( eFileLock!=PENDING_LOCK );
25846   assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK );
25847 
25848   /* This mutex is needed because pFile->pInode is shared across threads
25849   */
25850   unixEnterMutex();
25851   pInode = pFile->pInode;
25852 
25853   /* If some thread using this PID has a lock via a different unixFile*
25854   ** handle that precludes the requested lock, return BUSY.
25855   */
25856   if( (pFile->eFileLock!=pInode->eFileLock &&
25857           (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK))
25858   ){
25859     rc = SQLITE_BUSY;
25860     goto end_lock;
25861   }
25862 
25863   /* If a SHARED lock is requested, and some thread using this PID already
25864   ** has a SHARED or RESERVED lock, then increment reference counts and
25865   ** return SQLITE_OK.
25866   */
25867   if( eFileLock==SHARED_LOCK &&
25868       (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){
25869     assert( eFileLock==SHARED_LOCK );
25870     assert( pFile->eFileLock==0 );
25871     assert( pInode->nShared>0 );
25872     pFile->eFileLock = SHARED_LOCK;
25873     pInode->nShared++;
25874     pInode->nLock++;
25875     goto end_lock;
25876   }
25877 
25878 
25879   /* A PENDING lock is needed before acquiring a SHARED lock and before
25880   ** acquiring an EXCLUSIVE lock.  For the SHARED lock, the PENDING will
25881   ** be released.
25882   */
25883   lock.l_len = 1L;
25884   lock.l_whence = SEEK_SET;
25885   if( eFileLock==SHARED_LOCK
25886       || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK)
25887   ){
25888     lock.l_type = (eFileLock==SHARED_LOCK?F_RDLCK:F_WRLCK);
25889     lock.l_start = PENDING_BYTE;
25890     if( unixFileLock(pFile, &lock) ){
25891       tErrno = errno;
25892       rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
25893       if( rc!=SQLITE_BUSY ){
25894         pFile->lastErrno = tErrno;
25895       }
25896       goto end_lock;
25897     }
25898   }
25899 
25900 
25901   /* If control gets to this point, then actually go ahead and make
25902   ** operating system calls for the specified lock.
25903   */
25904   if( eFileLock==SHARED_LOCK ){
25905     assert( pInode->nShared==0 );
25906     assert( pInode->eFileLock==0 );
25907     assert( rc==SQLITE_OK );
25908 
25909     /* Now get the read-lock */
25910     lock.l_start = SHARED_FIRST;
25911     lock.l_len = SHARED_SIZE;
25912     if( unixFileLock(pFile, &lock) ){
25913       tErrno = errno;
25914       rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
25915     }
25916 
25917     /* Drop the temporary PENDING lock */
25918     lock.l_start = PENDING_BYTE;
25919     lock.l_len = 1L;
25920     lock.l_type = F_UNLCK;
25921     if( unixFileLock(pFile, &lock) && rc==SQLITE_OK ){
25922       /* This could happen with a network mount */
25923       tErrno = errno;
25924       rc = SQLITE_IOERR_UNLOCK;
25925     }
25926 
25927     if( rc ){
25928       if( rc!=SQLITE_BUSY ){
25929         pFile->lastErrno = tErrno;
25930       }
25931       goto end_lock;
25932     }else{
25933       pFile->eFileLock = SHARED_LOCK;
25934       pInode->nLock++;
25935       pInode->nShared = 1;
25936     }
25937   }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){
25938     /* We are trying for an exclusive lock but another thread in this
25939     ** same process is still holding a shared lock. */
25940     rc = SQLITE_BUSY;
25941   }else{
25942     /* The request was for a RESERVED or EXCLUSIVE lock.  It is
25943     ** assumed that there is a SHARED or greater lock on the file
25944     ** already.
25945     */
25946     assert( 0!=pFile->eFileLock );
25947     lock.l_type = F_WRLCK;
25948 
25949     assert( eFileLock==RESERVED_LOCK || eFileLock==EXCLUSIVE_LOCK );
25950     if( eFileLock==RESERVED_LOCK ){
25951       lock.l_start = RESERVED_BYTE;
25952       lock.l_len = 1L;
25953     }else{
25954       lock.l_start = SHARED_FIRST;
25955       lock.l_len = SHARED_SIZE;
25956     }
25957 
25958     if( unixFileLock(pFile, &lock) ){
25959       tErrno = errno;
25960       rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
25961       if( rc!=SQLITE_BUSY ){
25962         pFile->lastErrno = tErrno;
25963       }
25964     }
25965   }
25966 
25967 
25968 #ifndef NDEBUG
25969   /* Set up the transaction-counter change checking flags when
25970   ** transitioning from a SHARED to a RESERVED lock.  The change
25971   ** from SHARED to RESERVED marks the beginning of a normal
25972   ** write operation (not a hot journal rollback).
25973   */
25974   if( rc==SQLITE_OK
25975    && pFile->eFileLock<=SHARED_LOCK
25976    && eFileLock==RESERVED_LOCK
25977   ){
25978     pFile->transCntrChng = 0;
25979     pFile->dbUpdate = 0;
25980     pFile->inNormalWrite = 1;
25981   }
25982 #endif
25983 
25984 
25985   if( rc==SQLITE_OK ){
25986     pFile->eFileLock = eFileLock;
25987     pInode->eFileLock = eFileLock;
25988   }else if( eFileLock==EXCLUSIVE_LOCK ){
25989     pFile->eFileLock = PENDING_LOCK;
25990     pInode->eFileLock = PENDING_LOCK;
25991   }
25992 
25993 end_lock:
25994   unixLeaveMutex();
25995   OSTRACE(("LOCK    %d %s %s (unix)\n", pFile->h, azFileLock(eFileLock),
25996       rc==SQLITE_OK ? "ok" : "failed"));
25997   return rc;
25998 }
25999 
26000 /*
26001 ** Add the file descriptor used by file handle pFile to the corresponding
26002 ** pUnused list.
26003 */
26004 static void setPendingFd(unixFile *pFile){
26005   unixInodeInfo *pInode = pFile->pInode;
26006   UnixUnusedFd *p = pFile->pUnused;
26007   p->pNext = pInode->pUnused;
26008   pInode->pUnused = p;
26009   pFile->h = -1;
26010   pFile->pUnused = 0;
26011 }
26012 
26013 /*
26014 ** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
26015 ** must be either NO_LOCK or SHARED_LOCK.
26016 **
26017 ** If the locking level of the file descriptor is already at or below
26018 ** the requested locking level, this routine is a no-op.
26019 **
26020 ** If handleNFSUnlock is true, then on downgrading an EXCLUSIVE_LOCK to SHARED
26021 ** the byte range is divided into 2 parts and the first part is unlocked then
26022 ** set to a read lock, then the other part is simply unlocked.  This works
26023 ** around a bug in BSD NFS lockd (also seen on MacOSX 10.3+) that fails to
26024 ** remove the write lock on a region when a read lock is set.
26025 */
26026 static int posixUnlock(sqlite3_file *id, int eFileLock, int handleNFSUnlock){
26027   unixFile *pFile = (unixFile*)id;
26028   unixInodeInfo *pInode;
26029   struct flock lock;
26030   int rc = SQLITE_OK;
26031   int h;
26032 
26033   assert( pFile );
26034   OSTRACE(("UNLOCK  %d %d was %d(%d,%d) pid=%d (unix)\n", pFile->h, eFileLock,
26035       pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
26036       getpid()));
26037 
26038   assert( eFileLock<=SHARED_LOCK );
26039   if( pFile->eFileLock<=eFileLock ){
26040     return SQLITE_OK;
26041   }
26042   unixEnterMutex();
26043   h = pFile->h;
26044   pInode = pFile->pInode;
26045   assert( pInode->nShared!=0 );
26046   if( pFile->eFileLock>SHARED_LOCK ){
26047     assert( pInode->eFileLock==pFile->eFileLock );
26048     SimulateIOErrorBenign(1);
26049     SimulateIOError( h=(-1) )
26050     SimulateIOErrorBenign(0);
26051 
26052 #ifndef NDEBUG
26053     /* When reducing a lock such that other processes can start
26054     ** reading the database file again, make sure that the
26055     ** transaction counter was updated if any part of the database
26056     ** file changed.  If the transaction counter is not updated,
26057     ** other connections to the same file might not realize that
26058     ** the file has changed and hence might not know to flush their
26059     ** cache.  The use of a stale cache can lead to database corruption.
26060     */
26061 #if 0
26062     assert( pFile->inNormalWrite==0
26063          || pFile->dbUpdate==0
26064          || pFile->transCntrChng==1 );
26065 #endif
26066     pFile->inNormalWrite = 0;
26067 #endif
26068 
26069     /* downgrading to a shared lock on NFS involves clearing the write lock
26070     ** before establishing the readlock - to avoid a race condition we downgrade
26071     ** the lock in 2 blocks, so that part of the range will be covered by a
26072     ** write lock until the rest is covered by a read lock:
26073     **  1:   [WWWWW]
26074     **  2:   [....W]
26075     **  3:   [RRRRW]
26076     **  4:   [RRRR.]
26077     */
26078     if( eFileLock==SHARED_LOCK ){
26079 
26080 #if !defined(__APPLE__) || !SQLITE_ENABLE_LOCKING_STYLE
26081       (void)handleNFSUnlock;
26082       assert( handleNFSUnlock==0 );
26083 #endif
26084 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
26085       if( handleNFSUnlock ){
26086         int tErrno;               /* Error code from system call errors */
26087         off_t divSize = SHARED_SIZE - 1;
26088 
26089         lock.l_type = F_UNLCK;
26090         lock.l_whence = SEEK_SET;
26091         lock.l_start = SHARED_FIRST;
26092         lock.l_len = divSize;
26093         if( unixFileLock(pFile, &lock)==(-1) ){
26094           tErrno = errno;
26095           rc = SQLITE_IOERR_UNLOCK;
26096           if( IS_LOCK_ERROR(rc) ){
26097             pFile->lastErrno = tErrno;
26098           }
26099           goto end_unlock;
26100         }
26101         lock.l_type = F_RDLCK;
26102         lock.l_whence = SEEK_SET;
26103         lock.l_start = SHARED_FIRST;
26104         lock.l_len = divSize;
26105         if( unixFileLock(pFile, &lock)==(-1) ){
26106           tErrno = errno;
26107           rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_RDLOCK);
26108           if( IS_LOCK_ERROR(rc) ){
26109             pFile->lastErrno = tErrno;
26110           }
26111           goto end_unlock;
26112         }
26113         lock.l_type = F_UNLCK;
26114         lock.l_whence = SEEK_SET;
26115         lock.l_start = SHARED_FIRST+divSize;
26116         lock.l_len = SHARED_SIZE-divSize;
26117         if( unixFileLock(pFile, &lock)==(-1) ){
26118           tErrno = errno;
26119           rc = SQLITE_IOERR_UNLOCK;
26120           if( IS_LOCK_ERROR(rc) ){
26121             pFile->lastErrno = tErrno;
26122           }
26123           goto end_unlock;
26124         }
26125       }else
26126 #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
26127       {
26128         lock.l_type = F_RDLCK;
26129         lock.l_whence = SEEK_SET;
26130         lock.l_start = SHARED_FIRST;
26131         lock.l_len = SHARED_SIZE;
26132         if( unixFileLock(pFile, &lock) ){
26133           /* In theory, the call to unixFileLock() cannot fail because another
26134           ** process is holding an incompatible lock. If it does, this
26135           ** indicates that the other process is not following the locking
26136           ** protocol. If this happens, return SQLITE_IOERR_RDLOCK. Returning
26137           ** SQLITE_BUSY would confuse the upper layer (in practice it causes
26138           ** an assert to fail). */
26139           rc = SQLITE_IOERR_RDLOCK;
26140           pFile->lastErrno = errno;
26141           goto end_unlock;
26142         }
26143       }
26144     }
26145     lock.l_type = F_UNLCK;
26146     lock.l_whence = SEEK_SET;
26147     lock.l_start = PENDING_BYTE;
26148     lock.l_len = 2L;  assert( PENDING_BYTE+1==RESERVED_BYTE );
26149     if( unixFileLock(pFile, &lock)==0 ){
26150       pInode->eFileLock = SHARED_LOCK;
26151     }else{
26152       rc = SQLITE_IOERR_UNLOCK;
26153       pFile->lastErrno = errno;
26154       goto end_unlock;
26155     }
26156   }
26157   if( eFileLock==NO_LOCK ){
26158     /* Decrement the shared lock counter.  Release the lock using an
26159     ** OS call only when all threads in this same process have released
26160     ** the lock.
26161     */
26162     pInode->nShared--;
26163     if( pInode->nShared==0 ){
26164       lock.l_type = F_UNLCK;
26165       lock.l_whence = SEEK_SET;
26166       lock.l_start = lock.l_len = 0L;
26167       SimulateIOErrorBenign(1);
26168       SimulateIOError( h=(-1) )
26169       SimulateIOErrorBenign(0);
26170       if( unixFileLock(pFile, &lock)==0 ){
26171         pInode->eFileLock = NO_LOCK;
26172       }else{
26173         rc = SQLITE_IOERR_UNLOCK;
26174 	pFile->lastErrno = errno;
26175         pInode->eFileLock = NO_LOCK;
26176         pFile->eFileLock = NO_LOCK;
26177       }
26178     }
26179 
26180     /* Decrement the count of locks against this same file.  When the
26181     ** count reaches zero, close any other file descriptors whose close
26182     ** was deferred because of outstanding locks.
26183     */
26184     pInode->nLock--;
26185     assert( pInode->nLock>=0 );
26186     if( pInode->nLock==0 ){
26187       closePendingFds(pFile);
26188     }
26189   }
26190 
26191 end_unlock:
26192   unixLeaveMutex();
26193   if( rc==SQLITE_OK ) pFile->eFileLock = eFileLock;
26194   return rc;
26195 }
26196 
26197 /*
26198 ** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
26199 ** must be either NO_LOCK or SHARED_LOCK.
26200 **
26201 ** If the locking level of the file descriptor is already at or below
26202 ** the requested locking level, this routine is a no-op.
26203 */
26204 static int unixUnlock(sqlite3_file *id, int eFileLock){
26205   return posixUnlock(id, eFileLock, 0);
26206 }
26207 
26208 /*
26209 ** This function performs the parts of the "close file" operation
26210 ** common to all locking schemes. It closes the directory and file
26211 ** handles, if they are valid, and sets all fields of the unixFile
26212 ** structure to 0.
26213 **
26214 ** It is *not* necessary to hold the mutex when this routine is called,
26215 ** even on VxWorks.  A mutex will be acquired on VxWorks by the
26216 ** vxworksReleaseFileId() routine.
26217 */
26218 static int closeUnixFile(sqlite3_file *id){
26219   unixFile *pFile = (unixFile*)id;
26220   if( pFile->dirfd>=0 ){
26221     robust_close(pFile, pFile->dirfd, __LINE__);
26222     pFile->dirfd=-1;
26223   }
26224   if( pFile->h>=0 ){
26225     robust_close(pFile, pFile->h, __LINE__);
26226     pFile->h = -1;
26227   }
26228 #if OS_VXWORKS
26229   if( pFile->pId ){
26230     if( pFile->isDelete ){
26231       unlink(pFile->pId->zCanonicalName);
26232     }
26233     vxworksReleaseFileId(pFile->pId);
26234     pFile->pId = 0;
26235   }
26236 #endif
26237   OSTRACE(("CLOSE   %-3d\n", pFile->h));
26238   OpenCounter(-1);
26239   sqlite3_free(pFile->pUnused);
26240   memset(pFile, 0, sizeof(unixFile));
26241   return SQLITE_OK;
26242 }
26243 
26244 /*
26245 ** Close a file.
26246 */
26247 static int unixClose(sqlite3_file *id){
26248   int rc = SQLITE_OK;
26249   unixFile *pFile = (unixFile *)id;
26250   unixUnlock(id, NO_LOCK);
26251   unixEnterMutex();
26252 
26253   /* unixFile.pInode is always valid here. Otherwise, a different close
26254   ** routine (e.g. nolockClose()) would be called instead.
26255   */
26256   assert( pFile->pInode->nLock>0 || pFile->pInode->bProcessLock==0 );
26257   if( ALWAYS(pFile->pInode) && pFile->pInode->nLock ){
26258     /* If there are outstanding locks, do not actually close the file just
26259     ** yet because that would clear those locks.  Instead, add the file
26260     ** descriptor to pInode->pUnused list.  It will be automatically closed
26261     ** when the last lock is cleared.
26262     */
26263     setPendingFd(pFile);
26264   }
26265   releaseInodeInfo(pFile);
26266   rc = closeUnixFile(id);
26267   unixLeaveMutex();
26268   return rc;
26269 }
26270 
26271 /************** End of the posix advisory lock implementation *****************
26272 ******************************************************************************/
26273 
26274 /******************************************************************************
26275 ****************************** No-op Locking **********************************
26276 **
26277 ** Of the various locking implementations available, this is by far the
26278 ** simplest:  locking is ignored.  No attempt is made to lock the database
26279 ** file for reading or writing.
26280 **
26281 ** This locking mode is appropriate for use on read-only databases
26282 ** (ex: databases that are burned into CD-ROM, for example.)  It can
26283 ** also be used if the application employs some external mechanism to
26284 ** prevent simultaneous access of the same database by two or more
26285 ** database connections.  But there is a serious risk of database
26286 ** corruption if this locking mode is used in situations where multiple
26287 ** database connections are accessing the same database file at the same
26288 ** time and one or more of those connections are writing.
26289 */
26290 
26291 static int nolockCheckReservedLock(sqlite3_file *NotUsed, int *pResOut){
26292   UNUSED_PARAMETER(NotUsed);
26293   *pResOut = 0;
26294   return SQLITE_OK;
26295 }
26296 static int nolockLock(sqlite3_file *NotUsed, int NotUsed2){
26297   UNUSED_PARAMETER2(NotUsed, NotUsed2);
26298   return SQLITE_OK;
26299 }
26300 static int nolockUnlock(sqlite3_file *NotUsed, int NotUsed2){
26301   UNUSED_PARAMETER2(NotUsed, NotUsed2);
26302   return SQLITE_OK;
26303 }
26304 
26305 /*
26306 ** Close the file.
26307 */
26308 static int nolockClose(sqlite3_file *id) {
26309   return closeUnixFile(id);
26310 }
26311 
26312 /******************* End of the no-op lock implementation *********************
26313 ******************************************************************************/
26314 
26315 /******************************************************************************
26316 ************************* Begin dot-file Locking ******************************
26317 **
26318 ** The dotfile locking implementation uses the existance of separate lock
26319 ** files in order to control access to the database.  This works on just
26320 ** about every filesystem imaginable.  But there are serious downsides:
26321 **
26322 **    (1)  There is zero concurrency.  A single reader blocks all other
26323 **         connections from reading or writing the database.
26324 **
26325 **    (2)  An application crash or power loss can leave stale lock files
26326 **         sitting around that need to be cleared manually.
26327 **
26328 ** Nevertheless, a dotlock is an appropriate locking mode for use if no
26329 ** other locking strategy is available.
26330 **
26331 ** Dotfile locking works by creating a file in the same directory as the
26332 ** database and with the same name but with a ".lock" extension added.
26333 ** The existance of a lock file implies an EXCLUSIVE lock.  All other lock
26334 ** types (SHARED, RESERVED, PENDING) are mapped into EXCLUSIVE.
26335 */
26336 
26337 /*
26338 ** The file suffix added to the data base filename in order to create the
26339 ** lock file.
26340 */
26341 #define DOTLOCK_SUFFIX ".lock"
26342 
26343 /*
26344 ** This routine checks if there is a RESERVED lock held on the specified
26345 ** file by this or any other process. If such a lock is held, set *pResOut
26346 ** to a non-zero value otherwise *pResOut is set to zero.  The return value
26347 ** is set to SQLITE_OK unless an I/O error occurs during lock checking.
26348 **
26349 ** In dotfile locking, either a lock exists or it does not.  So in this
26350 ** variation of CheckReservedLock(), *pResOut is set to true if any lock
26351 ** is held on the file and false if the file is unlocked.
26352 */
26353 static int dotlockCheckReservedLock(sqlite3_file *id, int *pResOut) {
26354   int rc = SQLITE_OK;
26355   int reserved = 0;
26356   unixFile *pFile = (unixFile*)id;
26357 
26358   SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
26359 
26360   assert( pFile );
26361 
26362   /* Check if a thread in this process holds such a lock */
26363   if( pFile->eFileLock>SHARED_LOCK ){
26364     /* Either this connection or some other connection in the same process
26365     ** holds a lock on the file.  No need to check further. */
26366     reserved = 1;
26367   }else{
26368     /* The lock is held if and only if the lockfile exists */
26369     const char *zLockFile = (const char*)pFile->lockingContext;
26370     reserved = osAccess(zLockFile, 0)==0;
26371   }
26372   OSTRACE(("TEST WR-LOCK %d %d %d (dotlock)\n", pFile->h, rc, reserved));
26373   *pResOut = reserved;
26374   return rc;
26375 }
26376 
26377 /*
26378 ** Lock the file with the lock specified by parameter eFileLock - one
26379 ** of the following:
26380 **
26381 **     (1) SHARED_LOCK
26382 **     (2) RESERVED_LOCK
26383 **     (3) PENDING_LOCK
26384 **     (4) EXCLUSIVE_LOCK
26385 **
26386 ** Sometimes when requesting one lock state, additional lock states
26387 ** are inserted in between.  The locking might fail on one of the later
26388 ** transitions leaving the lock state different from what it started but
26389 ** still short of its goal.  The following chart shows the allowed
26390 ** transitions and the inserted intermediate states:
26391 **
26392 **    UNLOCKED -> SHARED
26393 **    SHARED -> RESERVED
26394 **    SHARED -> (PENDING) -> EXCLUSIVE
26395 **    RESERVED -> (PENDING) -> EXCLUSIVE
26396 **    PENDING -> EXCLUSIVE
26397 **
26398 ** This routine will only increase a lock.  Use the sqlite3OsUnlock()
26399 ** routine to lower a locking level.
26400 **
26401 ** With dotfile locking, we really only support state (4): EXCLUSIVE.
26402 ** But we track the other locking levels internally.
26403 */
26404 static int dotlockLock(sqlite3_file *id, int eFileLock) {
26405   unixFile *pFile = (unixFile*)id;
26406   int fd;
26407   char *zLockFile = (char *)pFile->lockingContext;
26408   int rc = SQLITE_OK;
26409 
26410 
26411   /* If we have any lock, then the lock file already exists.  All we have
26412   ** to do is adjust our internal record of the lock level.
26413   */
26414   if( pFile->eFileLock > NO_LOCK ){
26415     pFile->eFileLock = eFileLock;
26416     /* Always update the timestamp on the old file */
26417 #ifdef HAVE_UTIME
26418     utime(zLockFile, NULL);
26419 #else
26420     utimes(zLockFile, NULL);
26421 #endif
26422     return SQLITE_OK;
26423   }
26424 
26425   /* grab an exclusive lock */
26426   fd = robust_open(zLockFile,O_RDONLY|O_CREAT|O_EXCL,0600);
26427   if( fd<0 ){
26428     /* failed to open/create the file, someone else may have stolen the lock */
26429     int tErrno = errno;
26430     if( EEXIST == tErrno ){
26431       rc = SQLITE_BUSY;
26432     } else {
26433       rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
26434       if( IS_LOCK_ERROR(rc) ){
26435         pFile->lastErrno = tErrno;
26436       }
26437     }
26438     return rc;
26439   }
26440   robust_close(pFile, fd, __LINE__);
26441 
26442   /* got it, set the type and return ok */
26443   pFile->eFileLock = eFileLock;
26444   return rc;
26445 }
26446 
26447 /*
26448 ** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
26449 ** must be either NO_LOCK or SHARED_LOCK.
26450 **
26451 ** If the locking level of the file descriptor is already at or below
26452 ** the requested locking level, this routine is a no-op.
26453 **
26454 ** When the locking level reaches NO_LOCK, delete the lock file.
26455 */
26456 static int dotlockUnlock(sqlite3_file *id, int eFileLock) {
26457   unixFile *pFile = (unixFile*)id;
26458   char *zLockFile = (char *)pFile->lockingContext;
26459 
26460   assert( pFile );
26461   OSTRACE(("UNLOCK  %d %d was %d pid=%d (dotlock)\n", pFile->h, eFileLock,
26462 	   pFile->eFileLock, getpid()));
26463   assert( eFileLock<=SHARED_LOCK );
26464 
26465   /* no-op if possible */
26466   if( pFile->eFileLock==eFileLock ){
26467     return SQLITE_OK;
26468   }
26469 
26470   /* To downgrade to shared, simply update our internal notion of the
26471   ** lock state.  No need to mess with the file on disk.
26472   */
26473   if( eFileLock==SHARED_LOCK ){
26474     pFile->eFileLock = SHARED_LOCK;
26475     return SQLITE_OK;
26476   }
26477 
26478   /* To fully unlock the database, delete the lock file */
26479   assert( eFileLock==NO_LOCK );
26480   if( unlink(zLockFile) ){
26481     int rc = 0;
26482     int tErrno = errno;
26483     if( ENOENT != tErrno ){
26484       rc = SQLITE_IOERR_UNLOCK;
26485     }
26486     if( IS_LOCK_ERROR(rc) ){
26487       pFile->lastErrno = tErrno;
26488     }
26489     return rc;
26490   }
26491   pFile->eFileLock = NO_LOCK;
26492   return SQLITE_OK;
26493 }
26494 
26495 /*
26496 ** Close a file.  Make sure the lock has been released before closing.
26497 */
26498 static int dotlockClose(sqlite3_file *id) {
26499   int rc;
26500   if( id ){
26501     unixFile *pFile = (unixFile*)id;
26502     dotlockUnlock(id, NO_LOCK);
26503     sqlite3_free(pFile->lockingContext);
26504   }
26505   rc = closeUnixFile(id);
26506   return rc;
26507 }
26508 /****************** End of the dot-file lock implementation *******************
26509 ******************************************************************************/
26510 
26511 /******************************************************************************
26512 ************************** Begin flock Locking ********************************
26513 **
26514 ** Use the flock() system call to do file locking.
26515 **
26516 ** flock() locking is like dot-file locking in that the various
26517 ** fine-grain locking levels supported by SQLite are collapsed into
26518 ** a single exclusive lock.  In other words, SHARED, RESERVED, and
26519 ** PENDING locks are the same thing as an EXCLUSIVE lock.  SQLite
26520 ** still works when you do this, but concurrency is reduced since
26521 ** only a single process can be reading the database at a time.
26522 **
26523 ** Omit this section if SQLITE_ENABLE_LOCKING_STYLE is turned off or if
26524 ** compiling for VXWORKS.
26525 */
26526 #if SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS
26527 
26528 /*
26529 ** Retry flock() calls that fail with EINTR
26530 */
26531 #ifdef EINTR
26532 static int robust_flock(int fd, int op){
26533   int rc;
26534   do{ rc = flock(fd,op); }while( rc<0 && errno==EINTR );
26535   return rc;
26536 }
26537 #else
26538 # define robust_flock(a,b) flock(a,b)
26539 #endif
26540 
26541 
26542 /*
26543 ** This routine checks if there is a RESERVED lock held on the specified
26544 ** file by this or any other process. If such a lock is held, set *pResOut
26545 ** to a non-zero value otherwise *pResOut is set to zero.  The return value
26546 ** is set to SQLITE_OK unless an I/O error occurs during lock checking.
26547 */
26548 static int flockCheckReservedLock(sqlite3_file *id, int *pResOut){
26549   int rc = SQLITE_OK;
26550   int reserved = 0;
26551   unixFile *pFile = (unixFile*)id;
26552 
26553   SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
26554 
26555   assert( pFile );
26556 
26557   /* Check if a thread in this process holds such a lock */
26558   if( pFile->eFileLock>SHARED_LOCK ){
26559     reserved = 1;
26560   }
26561 
26562   /* Otherwise see if some other process holds it. */
26563   if( !reserved ){
26564     /* attempt to get the lock */
26565     int lrc = robust_flock(pFile->h, LOCK_EX | LOCK_NB);
26566     if( !lrc ){
26567       /* got the lock, unlock it */
26568       lrc = robust_flock(pFile->h, LOCK_UN);
26569       if ( lrc ) {
26570         int tErrno = errno;
26571         /* unlock failed with an error */
26572         lrc = SQLITE_IOERR_UNLOCK;
26573         if( IS_LOCK_ERROR(lrc) ){
26574           pFile->lastErrno = tErrno;
26575           rc = lrc;
26576         }
26577       }
26578     } else {
26579       int tErrno = errno;
26580       reserved = 1;
26581       /* someone else might have it reserved */
26582       lrc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
26583       if( IS_LOCK_ERROR(lrc) ){
26584         pFile->lastErrno = tErrno;
26585         rc = lrc;
26586       }
26587     }
26588   }
26589   OSTRACE(("TEST WR-LOCK %d %d %d (flock)\n", pFile->h, rc, reserved));
26590 
26591 #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
26592   if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){
26593     rc = SQLITE_OK;
26594     reserved=1;
26595   }
26596 #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
26597   *pResOut = reserved;
26598   return rc;
26599 }
26600 
26601 /*
26602 ** Lock the file with the lock specified by parameter eFileLock - one
26603 ** of the following:
26604 **
26605 **     (1) SHARED_LOCK
26606 **     (2) RESERVED_LOCK
26607 **     (3) PENDING_LOCK
26608 **     (4) EXCLUSIVE_LOCK
26609 **
26610 ** Sometimes when requesting one lock state, additional lock states
26611 ** are inserted in between.  The locking might fail on one of the later
26612 ** transitions leaving the lock state different from what it started but
26613 ** still short of its goal.  The following chart shows the allowed
26614 ** transitions and the inserted intermediate states:
26615 **
26616 **    UNLOCKED -> SHARED
26617 **    SHARED -> RESERVED
26618 **    SHARED -> (PENDING) -> EXCLUSIVE
26619 **    RESERVED -> (PENDING) -> EXCLUSIVE
26620 **    PENDING -> EXCLUSIVE
26621 **
26622 ** flock() only really support EXCLUSIVE locks.  We track intermediate
26623 ** lock states in the sqlite3_file structure, but all locks SHARED or
26624 ** above are really EXCLUSIVE locks and exclude all other processes from
26625 ** access the file.
26626 **
26627 ** This routine will only increase a lock.  Use the sqlite3OsUnlock()
26628 ** routine to lower a locking level.
26629 */
26630 static int flockLock(sqlite3_file *id, int eFileLock) {
26631   int rc = SQLITE_OK;
26632   unixFile *pFile = (unixFile*)id;
26633 
26634   assert( pFile );
26635 
26636   /* if we already have a lock, it is exclusive.
26637   ** Just adjust level and punt on outta here. */
26638   if (pFile->eFileLock > NO_LOCK) {
26639     pFile->eFileLock = eFileLock;
26640     return SQLITE_OK;
26641   }
26642 
26643   /* grab an exclusive lock */
26644 
26645   if (robust_flock(pFile->h, LOCK_EX | LOCK_NB)) {
26646     int tErrno = errno;
26647     /* didn't get, must be busy */
26648     rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
26649     if( IS_LOCK_ERROR(rc) ){
26650       pFile->lastErrno = tErrno;
26651     }
26652   } else {
26653     /* got it, set the type and return ok */
26654     pFile->eFileLock = eFileLock;
26655   }
26656   OSTRACE(("LOCK    %d %s %s (flock)\n", pFile->h, azFileLock(eFileLock),
26657            rc==SQLITE_OK ? "ok" : "failed"));
26658 #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
26659   if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){
26660     rc = SQLITE_BUSY;
26661   }
26662 #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
26663   return rc;
26664 }
26665 
26666 
26667 /*
26668 ** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
26669 ** must be either NO_LOCK or SHARED_LOCK.
26670 **
26671 ** If the locking level of the file descriptor is already at or below
26672 ** the requested locking level, this routine is a no-op.
26673 */
26674 static int flockUnlock(sqlite3_file *id, int eFileLock) {
26675   unixFile *pFile = (unixFile*)id;
26676 
26677   assert( pFile );
26678   OSTRACE(("UNLOCK  %d %d was %d pid=%d (flock)\n", pFile->h, eFileLock,
26679            pFile->eFileLock, getpid()));
26680   assert( eFileLock<=SHARED_LOCK );
26681 
26682   /* no-op if possible */
26683   if( pFile->eFileLock==eFileLock ){
26684     return SQLITE_OK;
26685   }
26686 
26687   /* shared can just be set because we always have an exclusive */
26688   if (eFileLock==SHARED_LOCK) {
26689     pFile->eFileLock = eFileLock;
26690     return SQLITE_OK;
26691   }
26692 
26693   /* no, really, unlock. */
26694   if( robust_flock(pFile->h, LOCK_UN) ){
26695 #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
26696     return SQLITE_OK;
26697 #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
26698     return SQLITE_IOERR_UNLOCK;
26699   }else{
26700     pFile->eFileLock = NO_LOCK;
26701     return SQLITE_OK;
26702   }
26703 }
26704 
26705 /*
26706 ** Close a file.
26707 */
26708 static int flockClose(sqlite3_file *id) {
26709   if( id ){
26710     flockUnlock(id, NO_LOCK);
26711   }
26712   return closeUnixFile(id);
26713 }
26714 
26715 #endif /* SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORK */
26716 
26717 /******************* End of the flock lock implementation *********************
26718 ******************************************************************************/
26719 
26720 /******************************************************************************
26721 ************************ Begin Named Semaphore Locking ************************
26722 **
26723 ** Named semaphore locking is only supported on VxWorks.
26724 **
26725 ** Semaphore locking is like dot-lock and flock in that it really only
26726 ** supports EXCLUSIVE locking.  Only a single process can read or write
26727 ** the database file at a time.  This reduces potential concurrency, but
26728 ** makes the lock implementation much easier.
26729 */
26730 #if OS_VXWORKS
26731 
26732 /*
26733 ** This routine checks if there is a RESERVED lock held on the specified
26734 ** file by this or any other process. If such a lock is held, set *pResOut
26735 ** to a non-zero value otherwise *pResOut is set to zero.  The return value
26736 ** is set to SQLITE_OK unless an I/O error occurs during lock checking.
26737 */
26738 static int semCheckReservedLock(sqlite3_file *id, int *pResOut) {
26739   int rc = SQLITE_OK;
26740   int reserved = 0;
26741   unixFile *pFile = (unixFile*)id;
26742 
26743   SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
26744 
26745   assert( pFile );
26746 
26747   /* Check if a thread in this process holds such a lock */
26748   if( pFile->eFileLock>SHARED_LOCK ){
26749     reserved = 1;
26750   }
26751 
26752   /* Otherwise see if some other process holds it. */
26753   if( !reserved ){
26754     sem_t *pSem = pFile->pInode->pSem;
26755     struct stat statBuf;
26756 
26757     if( sem_trywait(pSem)==-1 ){
26758       int tErrno = errno;
26759       if( EAGAIN != tErrno ){
26760         rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK);
26761         pFile->lastErrno = tErrno;
26762       } else {
26763         /* someone else has the lock when we are in NO_LOCK */
26764         reserved = (pFile->eFileLock < SHARED_LOCK);
26765       }
26766     }else{
26767       /* we could have it if we want it */
26768       sem_post(pSem);
26769     }
26770   }
26771   OSTRACE(("TEST WR-LOCK %d %d %d (sem)\n", pFile->h, rc, reserved));
26772 
26773   *pResOut = reserved;
26774   return rc;
26775 }
26776 
26777 /*
26778 ** Lock the file with the lock specified by parameter eFileLock - one
26779 ** of the following:
26780 **
26781 **     (1) SHARED_LOCK
26782 **     (2) RESERVED_LOCK
26783 **     (3) PENDING_LOCK
26784 **     (4) EXCLUSIVE_LOCK
26785 **
26786 ** Sometimes when requesting one lock state, additional lock states
26787 ** are inserted in between.  The locking might fail on one of the later
26788 ** transitions leaving the lock state different from what it started but
26789 ** still short of its goal.  The following chart shows the allowed
26790 ** transitions and the inserted intermediate states:
26791 **
26792 **    UNLOCKED -> SHARED
26793 **    SHARED -> RESERVED
26794 **    SHARED -> (PENDING) -> EXCLUSIVE
26795 **    RESERVED -> (PENDING) -> EXCLUSIVE
26796 **    PENDING -> EXCLUSIVE
26797 **
26798 ** Semaphore locks only really support EXCLUSIVE locks.  We track intermediate
26799 ** lock states in the sqlite3_file structure, but all locks SHARED or
26800 ** above are really EXCLUSIVE locks and exclude all other processes from
26801 ** access the file.
26802 **
26803 ** This routine will only increase a lock.  Use the sqlite3OsUnlock()
26804 ** routine to lower a locking level.
26805 */
26806 static int semLock(sqlite3_file *id, int eFileLock) {
26807   unixFile *pFile = (unixFile*)id;
26808   int fd;
26809   sem_t *pSem = pFile->pInode->pSem;
26810   int rc = SQLITE_OK;
26811 
26812   /* if we already have a lock, it is exclusive.
26813   ** Just adjust level and punt on outta here. */
26814   if (pFile->eFileLock > NO_LOCK) {
26815     pFile->eFileLock = eFileLock;
26816     rc = SQLITE_OK;
26817     goto sem_end_lock;
26818   }
26819 
26820   /* lock semaphore now but bail out when already locked. */
26821   if( sem_trywait(pSem)==-1 ){
26822     rc = SQLITE_BUSY;
26823     goto sem_end_lock;
26824   }
26825 
26826   /* got it, set the type and return ok */
26827   pFile->eFileLock = eFileLock;
26828 
26829  sem_end_lock:
26830   return rc;
26831 }
26832 
26833 /*
26834 ** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
26835 ** must be either NO_LOCK or SHARED_LOCK.
26836 **
26837 ** If the locking level of the file descriptor is already at or below
26838 ** the requested locking level, this routine is a no-op.
26839 */
26840 static int semUnlock(sqlite3_file *id, int eFileLock) {
26841   unixFile *pFile = (unixFile*)id;
26842   sem_t *pSem = pFile->pInode->pSem;
26843 
26844   assert( pFile );
26845   assert( pSem );
26846   OSTRACE(("UNLOCK  %d %d was %d pid=%d (sem)\n", pFile->h, eFileLock,
26847 	   pFile->eFileLock, getpid()));
26848   assert( eFileLock<=SHARED_LOCK );
26849 
26850   /* no-op if possible */
26851   if( pFile->eFileLock==eFileLock ){
26852     return SQLITE_OK;
26853   }
26854 
26855   /* shared can just be set because we always have an exclusive */
26856   if (eFileLock==SHARED_LOCK) {
26857     pFile->eFileLock = eFileLock;
26858     return SQLITE_OK;
26859   }
26860 
26861   /* no, really unlock. */
26862   if ( sem_post(pSem)==-1 ) {
26863     int rc, tErrno = errno;
26864     rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
26865     if( IS_LOCK_ERROR(rc) ){
26866       pFile->lastErrno = tErrno;
26867     }
26868     return rc;
26869   }
26870   pFile->eFileLock = NO_LOCK;
26871   return SQLITE_OK;
26872 }
26873 
26874 /*
26875  ** Close a file.
26876  */
26877 static int semClose(sqlite3_file *id) {
26878   if( id ){
26879     unixFile *pFile = (unixFile*)id;
26880     semUnlock(id, NO_LOCK);
26881     assert( pFile );
26882     unixEnterMutex();
26883     releaseInodeInfo(pFile);
26884     unixLeaveMutex();
26885     closeUnixFile(id);
26886   }
26887   return SQLITE_OK;
26888 }
26889 
26890 #endif /* OS_VXWORKS */
26891 /*
26892 ** Named semaphore locking is only available on VxWorks.
26893 **
26894 *************** End of the named semaphore lock implementation ****************
26895 ******************************************************************************/
26896 
26897 
26898 /******************************************************************************
26899 *************************** Begin AFP Locking *********************************
26900 **
26901 ** AFP is the Apple Filing Protocol.  AFP is a network filesystem found
26902 ** on Apple Macintosh computers - both OS9 and OSX.
26903 **
26904 ** Third-party implementations of AFP are available.  But this code here
26905 ** only works on OSX.
26906 */
26907 
26908 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
26909 /*
26910 ** The afpLockingContext structure contains all afp lock specific state
26911 */
26912 typedef struct afpLockingContext afpLockingContext;
26913 struct afpLockingContext {
26914   int reserved;
26915   const char *dbPath;             /* Name of the open file */
26916 };
26917 
26918 struct ByteRangeLockPB2
26919 {
26920   unsigned long long offset;        /* offset to first byte to lock */
26921   unsigned long long length;        /* nbr of bytes to lock */
26922   unsigned long long retRangeStart; /* nbr of 1st byte locked if successful */
26923   unsigned char unLockFlag;         /* 1 = unlock, 0 = lock */
26924   unsigned char startEndFlag;       /* 1=rel to end of fork, 0=rel to start */
26925   int fd;                           /* file desc to assoc this lock with */
26926 };
26927 
26928 #define afpfsByteRangeLock2FSCTL        _IOWR('z', 23, struct ByteRangeLockPB2)
26929 
26930 /*
26931 ** This is a utility for setting or clearing a bit-range lock on an
26932 ** AFP filesystem.
26933 **
26934 ** Return SQLITE_OK on success, SQLITE_BUSY on failure.
26935 */
26936 static int afpSetLock(
26937   const char *path,              /* Name of the file to be locked or unlocked */
26938   unixFile *pFile,               /* Open file descriptor on path */
26939   unsigned long long offset,     /* First byte to be locked */
26940   unsigned long long length,     /* Number of bytes to lock */
26941   int setLockFlag                /* True to set lock.  False to clear lock */
26942 ){
26943   struct ByteRangeLockPB2 pb;
26944   int err;
26945 
26946   pb.unLockFlag = setLockFlag ? 0 : 1;
26947   pb.startEndFlag = 0;
26948   pb.offset = offset;
26949   pb.length = length;
26950   pb.fd = pFile->h;
26951 
26952   OSTRACE(("AFPSETLOCK [%s] for %d%s in range %llx:%llx\n",
26953     (setLockFlag?"ON":"OFF"), pFile->h, (pb.fd==-1?"[testval-1]":""),
26954     offset, length));
26955   err = fsctl(path, afpfsByteRangeLock2FSCTL, &pb, 0);
26956   if ( err==-1 ) {
26957     int rc;
26958     int tErrno = errno;
26959     OSTRACE(("AFPSETLOCK failed to fsctl() '%s' %d %s\n",
26960              path, tErrno, strerror(tErrno)));
26961 #ifdef SQLITE_IGNORE_AFP_LOCK_ERRORS
26962     rc = SQLITE_BUSY;
26963 #else
26964     rc = sqliteErrorFromPosixError(tErrno,
26965                     setLockFlag ? SQLITE_IOERR_LOCK : SQLITE_IOERR_UNLOCK);
26966 #endif /* SQLITE_IGNORE_AFP_LOCK_ERRORS */
26967     if( IS_LOCK_ERROR(rc) ){
26968       pFile->lastErrno = tErrno;
26969     }
26970     return rc;
26971   } else {
26972     return SQLITE_OK;
26973   }
26974 }
26975 
26976 /*
26977 ** This routine checks if there is a RESERVED lock held on the specified
26978 ** file by this or any other process. If such a lock is held, set *pResOut
26979 ** to a non-zero value otherwise *pResOut is set to zero.  The return value
26980 ** is set to SQLITE_OK unless an I/O error occurs during lock checking.
26981 */
26982 static int afpCheckReservedLock(sqlite3_file *id, int *pResOut){
26983   int rc = SQLITE_OK;
26984   int reserved = 0;
26985   unixFile *pFile = (unixFile*)id;
26986 
26987   SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
26988 
26989   assert( pFile );
26990   afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
26991   if( context->reserved ){
26992     *pResOut = 1;
26993     return SQLITE_OK;
26994   }
26995   unixEnterMutex(); /* Because pFile->pInode is shared across threads */
26996 
26997   /* Check if a thread in this process holds such a lock */
26998   if( pFile->pInode->eFileLock>SHARED_LOCK ){
26999     reserved = 1;
27000   }
27001 
27002   /* Otherwise see if some other process holds it.
27003    */
27004   if( !reserved ){
27005     /* lock the RESERVED byte */
27006     int lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1);
27007     if( SQLITE_OK==lrc ){
27008       /* if we succeeded in taking the reserved lock, unlock it to restore
27009       ** the original state */
27010       lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0);
27011     } else {
27012       /* if we failed to get the lock then someone else must have it */
27013       reserved = 1;
27014     }
27015     if( IS_LOCK_ERROR(lrc) ){
27016       rc=lrc;
27017     }
27018   }
27019 
27020   unixLeaveMutex();
27021   OSTRACE(("TEST WR-LOCK %d %d %d (afp)\n", pFile->h, rc, reserved));
27022 
27023   *pResOut = reserved;
27024   return rc;
27025 }
27026 
27027 /*
27028 ** Lock the file with the lock specified by parameter eFileLock - one
27029 ** of the following:
27030 **
27031 **     (1) SHARED_LOCK
27032 **     (2) RESERVED_LOCK
27033 **     (3) PENDING_LOCK
27034 **     (4) EXCLUSIVE_LOCK
27035 **
27036 ** Sometimes when requesting one lock state, additional lock states
27037 ** are inserted in between.  The locking might fail on one of the later
27038 ** transitions leaving the lock state different from what it started but
27039 ** still short of its goal.  The following chart shows the allowed
27040 ** transitions and the inserted intermediate states:
27041 **
27042 **    UNLOCKED -> SHARED
27043 **    SHARED -> RESERVED
27044 **    SHARED -> (PENDING) -> EXCLUSIVE
27045 **    RESERVED -> (PENDING) -> EXCLUSIVE
27046 **    PENDING -> EXCLUSIVE
27047 **
27048 ** This routine will only increase a lock.  Use the sqlite3OsUnlock()
27049 ** routine to lower a locking level.
27050 */
27051 static int afpLock(sqlite3_file *id, int eFileLock){
27052   int rc = SQLITE_OK;
27053   unixFile *pFile = (unixFile*)id;
27054   unixInodeInfo *pInode = pFile->pInode;
27055   afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
27056 
27057   assert( pFile );
27058   OSTRACE(("LOCK    %d %s was %s(%s,%d) pid=%d (afp)\n", pFile->h,
27059            azFileLock(eFileLock), azFileLock(pFile->eFileLock),
27060            azFileLock(pInode->eFileLock), pInode->nShared , getpid()));
27061 
27062   /* If there is already a lock of this type or more restrictive on the
27063   ** unixFile, do nothing. Don't use the afp_end_lock: exit path, as
27064   ** unixEnterMutex() hasn't been called yet.
27065   */
27066   if( pFile->eFileLock>=eFileLock ){
27067     OSTRACE(("LOCK    %d %s ok (already held) (afp)\n", pFile->h,
27068            azFileLock(eFileLock)));
27069     return SQLITE_OK;
27070   }
27071 
27072   /* Make sure the locking sequence is correct
27073   **  (1) We never move from unlocked to anything higher than shared lock.
27074   **  (2) SQLite never explicitly requests a pendig lock.
27075   **  (3) A shared lock is always held when a reserve lock is requested.
27076   */
27077   assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK );
27078   assert( eFileLock!=PENDING_LOCK );
27079   assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK );
27080 
27081   /* This mutex is needed because pFile->pInode is shared across threads
27082   */
27083   unixEnterMutex();
27084   pInode = pFile->pInode;
27085 
27086   /* If some thread using this PID has a lock via a different unixFile*
27087   ** handle that precludes the requested lock, return BUSY.
27088   */
27089   if( (pFile->eFileLock!=pInode->eFileLock &&
27090        (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK))
27091      ){
27092     rc = SQLITE_BUSY;
27093     goto afp_end_lock;
27094   }
27095 
27096   /* If a SHARED lock is requested, and some thread using this PID already
27097   ** has a SHARED or RESERVED lock, then increment reference counts and
27098   ** return SQLITE_OK.
27099   */
27100   if( eFileLock==SHARED_LOCK &&
27101      (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){
27102     assert( eFileLock==SHARED_LOCK );
27103     assert( pFile->eFileLock==0 );
27104     assert( pInode->nShared>0 );
27105     pFile->eFileLock = SHARED_LOCK;
27106     pInode->nShared++;
27107     pInode->nLock++;
27108     goto afp_end_lock;
27109   }
27110 
27111   /* A PENDING lock is needed before acquiring a SHARED lock and before
27112   ** acquiring an EXCLUSIVE lock.  For the SHARED lock, the PENDING will
27113   ** be released.
27114   */
27115   if( eFileLock==SHARED_LOCK
27116       || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK)
27117   ){
27118     int failed;
27119     failed = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 1);
27120     if (failed) {
27121       rc = failed;
27122       goto afp_end_lock;
27123     }
27124   }
27125 
27126   /* If control gets to this point, then actually go ahead and make
27127   ** operating system calls for the specified lock.
27128   */
27129   if( eFileLock==SHARED_LOCK ){
27130     int lrc1, lrc2, lrc1Errno;
27131     long lk, mask;
27132 
27133     assert( pInode->nShared==0 );
27134     assert( pInode->eFileLock==0 );
27135 
27136     mask = (sizeof(long)==8) ? LARGEST_INT64 : 0x7fffffff;
27137     /* Now get the read-lock SHARED_LOCK */
27138     /* note that the quality of the randomness doesn't matter that much */
27139     lk = random();
27140     pInode->sharedByte = (lk & mask)%(SHARED_SIZE - 1);
27141     lrc1 = afpSetLock(context->dbPath, pFile,
27142           SHARED_FIRST+pInode->sharedByte, 1, 1);
27143     if( IS_LOCK_ERROR(lrc1) ){
27144       lrc1Errno = pFile->lastErrno;
27145     }
27146     /* Drop the temporary PENDING lock */
27147     lrc2 = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0);
27148 
27149     if( IS_LOCK_ERROR(lrc1) ) {
27150       pFile->lastErrno = lrc1Errno;
27151       rc = lrc1;
27152       goto afp_end_lock;
27153     } else if( IS_LOCK_ERROR(lrc2) ){
27154       rc = lrc2;
27155       goto afp_end_lock;
27156     } else if( lrc1 != SQLITE_OK ) {
27157       rc = lrc1;
27158     } else {
27159       pFile->eFileLock = SHARED_LOCK;
27160       pInode->nLock++;
27161       pInode->nShared = 1;
27162     }
27163   }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){
27164     /* We are trying for an exclusive lock but another thread in this
27165      ** same process is still holding a shared lock. */
27166     rc = SQLITE_BUSY;
27167   }else{
27168     /* The request was for a RESERVED or EXCLUSIVE lock.  It is
27169     ** assumed that there is a SHARED or greater lock on the file
27170     ** already.
27171     */
27172     int failed = 0;
27173     assert( 0!=pFile->eFileLock );
27174     if (eFileLock >= RESERVED_LOCK && pFile->eFileLock < RESERVED_LOCK) {
27175         /* Acquire a RESERVED lock */
27176         failed = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1);
27177       if( !failed ){
27178         context->reserved = 1;
27179       }
27180     }
27181     if (!failed && eFileLock == EXCLUSIVE_LOCK) {
27182       /* Acquire an EXCLUSIVE lock */
27183 
27184       /* Remove the shared lock before trying the range.  we'll need to
27185       ** reestablish the shared lock if we can't get the  afpUnlock
27186       */
27187       if( !(failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST +
27188                          pInode->sharedByte, 1, 0)) ){
27189         int failed2 = SQLITE_OK;
27190         /* now attemmpt to get the exclusive lock range */
27191         failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST,
27192                                SHARED_SIZE, 1);
27193         if( failed && (failed2 = afpSetLock(context->dbPath, pFile,
27194                        SHARED_FIRST + pInode->sharedByte, 1, 1)) ){
27195           /* Can't reestablish the shared lock.  Sqlite can't deal, this is
27196           ** a critical I/O error
27197           */
27198           rc = ((failed & SQLITE_IOERR) == SQLITE_IOERR) ? failed2 :
27199                SQLITE_IOERR_LOCK;
27200           goto afp_end_lock;
27201         }
27202       }else{
27203         rc = failed;
27204       }
27205     }
27206     if( failed ){
27207       rc = failed;
27208     }
27209   }
27210 
27211   if( rc==SQLITE_OK ){
27212     pFile->eFileLock = eFileLock;
27213     pInode->eFileLock = eFileLock;
27214   }else if( eFileLock==EXCLUSIVE_LOCK ){
27215     pFile->eFileLock = PENDING_LOCK;
27216     pInode->eFileLock = PENDING_LOCK;
27217   }
27218 
27219 afp_end_lock:
27220   unixLeaveMutex();
27221   OSTRACE(("LOCK    %d %s %s (afp)\n", pFile->h, azFileLock(eFileLock),
27222          rc==SQLITE_OK ? "ok" : "failed"));
27223   return rc;
27224 }
27225 
27226 /*
27227 ** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
27228 ** must be either NO_LOCK or SHARED_LOCK.
27229 **
27230 ** If the locking level of the file descriptor is already at or below
27231 ** the requested locking level, this routine is a no-op.
27232 */
27233 static int afpUnlock(sqlite3_file *id, int eFileLock) {
27234   int rc = SQLITE_OK;
27235   unixFile *pFile = (unixFile*)id;
27236   unixInodeInfo *pInode;
27237   afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
27238   int skipShared = 0;
27239 #ifdef SQLITE_TEST
27240   int h = pFile->h;
27241 #endif
27242 
27243   assert( pFile );
27244   OSTRACE(("UNLOCK  %d %d was %d(%d,%d) pid=%d (afp)\n", pFile->h, eFileLock,
27245            pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
27246            getpid()));
27247 
27248   assert( eFileLock<=SHARED_LOCK );
27249   if( pFile->eFileLock<=eFileLock ){
27250     return SQLITE_OK;
27251   }
27252   unixEnterMutex();
27253   pInode = pFile->pInode;
27254   assert( pInode->nShared!=0 );
27255   if( pFile->eFileLock>SHARED_LOCK ){
27256     assert( pInode->eFileLock==pFile->eFileLock );
27257     SimulateIOErrorBenign(1);
27258     SimulateIOError( h=(-1) )
27259     SimulateIOErrorBenign(0);
27260 
27261 #ifndef NDEBUG
27262     /* When reducing a lock such that other processes can start
27263     ** reading the database file again, make sure that the
27264     ** transaction counter was updated if any part of the database
27265     ** file changed.  If the transaction counter is not updated,
27266     ** other connections to the same file might not realize that
27267     ** the file has changed and hence might not know to flush their
27268     ** cache.  The use of a stale cache can lead to database corruption.
27269     */
27270     assert( pFile->inNormalWrite==0
27271            || pFile->dbUpdate==0
27272            || pFile->transCntrChng==1 );
27273     pFile->inNormalWrite = 0;
27274 #endif
27275 
27276     if( pFile->eFileLock==EXCLUSIVE_LOCK ){
27277       rc = afpSetLock(context->dbPath, pFile, SHARED_FIRST, SHARED_SIZE, 0);
27278       if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1) ){
27279         /* only re-establish the shared lock if necessary */
27280         int sharedLockByte = SHARED_FIRST+pInode->sharedByte;
27281         rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 1);
27282       } else {
27283         skipShared = 1;
27284       }
27285     }
27286     if( rc==SQLITE_OK && pFile->eFileLock>=PENDING_LOCK ){
27287       rc = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0);
27288     }
27289     if( rc==SQLITE_OK && pFile->eFileLock>=RESERVED_LOCK && context->reserved ){
27290       rc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0);
27291       if( !rc ){
27292         context->reserved = 0;
27293       }
27294     }
27295     if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1)){
27296       pInode->eFileLock = SHARED_LOCK;
27297     }
27298   }
27299   if( rc==SQLITE_OK && eFileLock==NO_LOCK ){
27300 
27301     /* Decrement the shared lock counter.  Release the lock using an
27302     ** OS call only when all threads in this same process have released
27303     ** the lock.
27304     */
27305     unsigned long long sharedLockByte = SHARED_FIRST+pInode->sharedByte;
27306     pInode->nShared--;
27307     if( pInode->nShared==0 ){
27308       SimulateIOErrorBenign(1);
27309       SimulateIOError( h=(-1) )
27310       SimulateIOErrorBenign(0);
27311       if( !skipShared ){
27312         rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 0);
27313       }
27314       if( !rc ){
27315         pInode->eFileLock = NO_LOCK;
27316         pFile->eFileLock = NO_LOCK;
27317       }
27318     }
27319     if( rc==SQLITE_OK ){
27320       pInode->nLock--;
27321       assert( pInode->nLock>=0 );
27322       if( pInode->nLock==0 ){
27323         closePendingFds(pFile);
27324       }
27325     }
27326   }
27327 
27328   unixLeaveMutex();
27329   if( rc==SQLITE_OK ) pFile->eFileLock = eFileLock;
27330   return rc;
27331 }
27332 
27333 /*
27334 ** Close a file & cleanup AFP specific locking context
27335 */
27336 static int afpClose(sqlite3_file *id) {
27337   int rc = SQLITE_OK;
27338   if( id ){
27339     unixFile *pFile = (unixFile*)id;
27340     afpUnlock(id, NO_LOCK);
27341     unixEnterMutex();
27342     if( pFile->pInode && pFile->pInode->nLock ){
27343       /* If there are outstanding locks, do not actually close the file just
27344       ** yet because that would clear those locks.  Instead, add the file
27345       ** descriptor to pInode->aPending.  It will be automatically closed when
27346       ** the last lock is cleared.
27347       */
27348       setPendingFd(pFile);
27349     }
27350     releaseInodeInfo(pFile);
27351     sqlite3_free(pFile->lockingContext);
27352     rc = closeUnixFile(id);
27353     unixLeaveMutex();
27354   }
27355   return rc;
27356 }
27357 
27358 #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
27359 /*
27360 ** The code above is the AFP lock implementation.  The code is specific
27361 ** to MacOSX and does not work on other unix platforms.  No alternative
27362 ** is available.  If you don't compile for a mac, then the "unix-afp"
27363 ** VFS is not available.
27364 **
27365 ********************* End of the AFP lock implementation **********************
27366 ******************************************************************************/
27367 
27368 /******************************************************************************
27369 *************************** Begin NFS Locking ********************************/
27370 
27371 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
27372 /*
27373  ** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
27374  ** must be either NO_LOCK or SHARED_LOCK.
27375  **
27376  ** If the locking level of the file descriptor is already at or below
27377  ** the requested locking level, this routine is a no-op.
27378  */
27379 static int nfsUnlock(sqlite3_file *id, int eFileLock){
27380   return posixUnlock(id, eFileLock, 1);
27381 }
27382 
27383 #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
27384 /*
27385 ** The code above is the NFS lock implementation.  The code is specific
27386 ** to MacOSX and does not work on other unix platforms.  No alternative
27387 ** is available.
27388 **
27389 ********************* End of the NFS lock implementation **********************
27390 ******************************************************************************/
27391 
27392 /******************************************************************************
27393 **************** Non-locking sqlite3_file methods *****************************
27394 **
27395 ** The next division contains implementations for all methods of the
27396 ** sqlite3_file object other than the locking methods.  The locking
27397 ** methods were defined in divisions above (one locking method per
27398 ** division).  Those methods that are common to all locking modes
27399 ** are gather together into this division.
27400 */
27401 
27402 /*
27403 ** Seek to the offset passed as the second argument, then read cnt
27404 ** bytes into pBuf. Return the number of bytes actually read.
27405 **
27406 ** NB:  If you define USE_PREAD or USE_PREAD64, then it might also
27407 ** be necessary to define _XOPEN_SOURCE to be 500.  This varies from
27408 ** one system to another.  Since SQLite does not define USE_PREAD
27409 ** any any form by default, we will not attempt to define _XOPEN_SOURCE.
27410 ** See tickets #2741 and #2681.
27411 **
27412 ** To avoid stomping the errno value on a failed read the lastErrno value
27413 ** is set before returning.
27414 */
27415 static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt){
27416   int got;
27417 #if (!defined(USE_PREAD) && !defined(USE_PREAD64))
27418   i64 newOffset;
27419 #endif
27420   TIMER_START;
27421 #if defined(USE_PREAD)
27422   do{ got = osPread(id->h, pBuf, cnt, offset); }while( got<0 && errno==EINTR );
27423   SimulateIOError( got = -1 );
27424 #elif defined(USE_PREAD64)
27425   do{ got = osPread64(id->h, pBuf, cnt, offset); }while( got<0 && errno==EINTR);
27426   SimulateIOError( got = -1 );
27427 #else
27428   newOffset = lseek(id->h, offset, SEEK_SET);
27429   SimulateIOError( newOffset-- );
27430   if( newOffset!=offset ){
27431     if( newOffset == -1 ){
27432       ((unixFile*)id)->lastErrno = errno;
27433     }else{
27434       ((unixFile*)id)->lastErrno = 0;
27435     }
27436     return -1;
27437   }
27438   do{ got = osRead(id->h, pBuf, cnt); }while( got<0 && errno==EINTR );
27439 #endif
27440   TIMER_END;
27441   if( got<0 ){
27442     ((unixFile*)id)->lastErrno = errno;
27443   }
27444   OSTRACE(("READ    %-3d %5d %7lld %llu\n", id->h, got, offset, TIMER_ELAPSED));
27445   return got;
27446 }
27447 
27448 /*
27449 ** Read data from a file into a buffer.  Return SQLITE_OK if all
27450 ** bytes were read successfully and SQLITE_IOERR if anything goes
27451 ** wrong.
27452 */
27453 static int unixRead(
27454   sqlite3_file *id,
27455   void *pBuf,
27456   int amt,
27457   sqlite3_int64 offset
27458 ){
27459   unixFile *pFile = (unixFile *)id;
27460   int got;
27461   assert( id );
27462 
27463   /* If this is a database file (not a journal, master-journal or temp
27464   ** file), the bytes in the locking range should never be read or written. */
27465 #if 0
27466   assert( pFile->pUnused==0
27467        || offset>=PENDING_BYTE+512
27468        || offset+amt<=PENDING_BYTE
27469   );
27470 #endif
27471 
27472   got = seekAndRead(pFile, offset, pBuf, amt);
27473   if( got==amt ){
27474     return SQLITE_OK;
27475   }else if( got<0 ){
27476     /* lastErrno set by seekAndRead */
27477     return SQLITE_IOERR_READ;
27478   }else{
27479     pFile->lastErrno = 0; /* not a system error */
27480     /* Unread parts of the buffer must be zero-filled */
27481     memset(&((char*)pBuf)[got], 0, amt-got);
27482     return SQLITE_IOERR_SHORT_READ;
27483   }
27484 }
27485 
27486 /*
27487 ** Seek to the offset in id->offset then read cnt bytes into pBuf.
27488 ** Return the number of bytes actually read.  Update the offset.
27489 **
27490 ** To avoid stomping the errno value on a failed write the lastErrno value
27491 ** is set before returning.
27492 */
27493 static int seekAndWrite(unixFile *id, i64 offset, const void *pBuf, int cnt){
27494   int got;
27495 #if (!defined(USE_PREAD) && !defined(USE_PREAD64))
27496   i64 newOffset;
27497 #endif
27498   TIMER_START;
27499 #if defined(USE_PREAD)
27500   do{ got = osPwrite(id->h, pBuf, cnt, offset); }while( got<0 && errno==EINTR );
27501 #elif defined(USE_PREAD64)
27502   do{ got = osPwrite64(id->h, pBuf, cnt, offset);}while( got<0 && errno==EINTR);
27503 #else
27504   newOffset = lseek(id->h, offset, SEEK_SET);
27505   SimulateIOError( newOffset-- );
27506   if( newOffset!=offset ){
27507     if( newOffset == -1 ){
27508       ((unixFile*)id)->lastErrno = errno;
27509     }else{
27510       ((unixFile*)id)->lastErrno = 0;
27511     }
27512     return -1;
27513   }
27514   do{ got = osWrite(id->h, pBuf, cnt); }while( got<0 && errno==EINTR );
27515 #endif
27516   TIMER_END;
27517   if( got<0 ){
27518     ((unixFile*)id)->lastErrno = errno;
27519   }
27520 
27521   OSTRACE(("WRITE   %-3d %5d %7lld %llu\n", id->h, got, offset, TIMER_ELAPSED));
27522   return got;
27523 }
27524 
27525 
27526 /*
27527 ** Write data from a buffer into a file.  Return SQLITE_OK on success
27528 ** or some other error code on failure.
27529 */
27530 static int unixWrite(
27531   sqlite3_file *id,
27532   const void *pBuf,
27533   int amt,
27534   sqlite3_int64 offset
27535 ){
27536   unixFile *pFile = (unixFile*)id;
27537   int wrote = 0;
27538   assert( id );
27539   assert( amt>0 );
27540 
27541   /* If this is a database file (not a journal, master-journal or temp
27542   ** file), the bytes in the locking range should never be read or written. */
27543 #if 0
27544   assert( pFile->pUnused==0
27545        || offset>=PENDING_BYTE+512
27546        || offset+amt<=PENDING_BYTE
27547   );
27548 #endif
27549 
27550 #ifndef NDEBUG
27551   /* If we are doing a normal write to a database file (as opposed to
27552   ** doing a hot-journal rollback or a write to some file other than a
27553   ** normal database file) then record the fact that the database
27554   ** has changed.  If the transaction counter is modified, record that
27555   ** fact too.
27556   */
27557   if( pFile->inNormalWrite ){
27558     pFile->dbUpdate = 1;  /* The database has been modified */
27559     if( offset<=24 && offset+amt>=27 ){
27560       int rc;
27561       char oldCntr[4];
27562       SimulateIOErrorBenign(1);
27563       rc = seekAndRead(pFile, 24, oldCntr, 4);
27564       SimulateIOErrorBenign(0);
27565       if( rc!=4 || memcmp(oldCntr, &((char*)pBuf)[24-offset], 4)!=0 ){
27566         pFile->transCntrChng = 1;  /* The transaction counter has changed */
27567       }
27568     }
27569   }
27570 #endif
27571 
27572   while( amt>0 && (wrote = seekAndWrite(pFile, offset, pBuf, amt))>0 ){
27573     amt -= wrote;
27574     offset += wrote;
27575     pBuf = &((char*)pBuf)[wrote];
27576   }
27577   SimulateIOError(( wrote=(-1), amt=1 ));
27578   SimulateDiskfullError(( wrote=0, amt=1 ));
27579 
27580   if( amt>0 ){
27581     if( wrote<0 && pFile->lastErrno!=ENOSPC ){
27582       /* lastErrno set by seekAndWrite */
27583       return SQLITE_IOERR_WRITE;
27584     }else{
27585       pFile->lastErrno = 0; /* not a system error */
27586       return SQLITE_FULL;
27587     }
27588   }
27589 
27590   return SQLITE_OK;
27591 }
27592 
27593 #ifdef SQLITE_TEST
27594 /*
27595 ** Count the number of fullsyncs and normal syncs.  This is used to test
27596 ** that syncs and fullsyncs are occurring at the right times.
27597 */
27598 SQLITE_API int sqlite3_sync_count = 0;
27599 SQLITE_API int sqlite3_fullsync_count = 0;
27600 #endif
27601 
27602 /*
27603 ** We do not trust systems to provide a working fdatasync().  Some do.
27604 ** Others do no.  To be safe, we will stick with the (slower) fsync().
27605 ** If you know that your system does support fdatasync() correctly,
27606 ** then simply compile with -Dfdatasync=fdatasync
27607 */
27608 #if !defined(fdatasync) && !defined(__linux__)
27609 # define fdatasync fsync
27610 #endif
27611 
27612 /*
27613 ** Define HAVE_FULLFSYNC to 0 or 1 depending on whether or not
27614 ** the F_FULLFSYNC macro is defined.  F_FULLFSYNC is currently
27615 ** only available on Mac OS X.  But that could change.
27616 */
27617 #ifdef F_FULLFSYNC
27618 # define HAVE_FULLFSYNC 1
27619 #else
27620 # define HAVE_FULLFSYNC 0
27621 #endif
27622 
27623 
27624 /*
27625 ** The fsync() system call does not work as advertised on many
27626 ** unix systems.  The following procedure is an attempt to make
27627 ** it work better.
27628 **
27629 ** The SQLITE_NO_SYNC macro disables all fsync()s.  This is useful
27630 ** for testing when we want to run through the test suite quickly.
27631 ** You are strongly advised *not* to deploy with SQLITE_NO_SYNC
27632 ** enabled, however, since with SQLITE_NO_SYNC enabled, an OS crash
27633 ** or power failure will likely corrupt the database file.
27634 **
27635 ** SQLite sets the dataOnly flag if the size of the file is unchanged.
27636 ** The idea behind dataOnly is that it should only write the file content
27637 ** to disk, not the inode.  We only set dataOnly if the file size is
27638 ** unchanged since the file size is part of the inode.  However,
27639 ** Ted Ts'o tells us that fdatasync() will also write the inode if the
27640 ** file size has changed.  The only real difference between fdatasync()
27641 ** and fsync(), Ted tells us, is that fdatasync() will not flush the
27642 ** inode if the mtime or owner or other inode attributes have changed.
27643 ** We only care about the file size, not the other file attributes, so
27644 ** as far as SQLite is concerned, an fdatasync() is always adequate.
27645 ** So, we always use fdatasync() if it is available, regardless of
27646 ** the value of the dataOnly flag.
27647 */
27648 static int full_fsync(int fd, int fullSync, int dataOnly){
27649   int rc;
27650 
27651   /* The following "ifdef/elif/else/" block has the same structure as
27652   ** the one below. It is replicated here solely to avoid cluttering
27653   ** up the real code with the UNUSED_PARAMETER() macros.
27654   */
27655 #ifdef SQLITE_NO_SYNC
27656   UNUSED_PARAMETER(fd);
27657   UNUSED_PARAMETER(fullSync);
27658   UNUSED_PARAMETER(dataOnly);
27659 #elif HAVE_FULLFSYNC
27660   UNUSED_PARAMETER(dataOnly);
27661 #else
27662   UNUSED_PARAMETER(fullSync);
27663   UNUSED_PARAMETER(dataOnly);
27664 #endif
27665 
27666   /* Record the number of times that we do a normal fsync() and
27667   ** FULLSYNC.  This is used during testing to verify that this procedure
27668   ** gets called with the correct arguments.
27669   */
27670 #ifdef SQLITE_TEST
27671   if( fullSync ) sqlite3_fullsync_count++;
27672   sqlite3_sync_count++;
27673 #endif
27674 
27675   /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
27676   ** no-op
27677   */
27678 #ifdef SQLITE_NO_SYNC
27679   rc = SQLITE_OK;
27680 #elif HAVE_FULLFSYNC
27681   if( fullSync ){
27682     rc = osFcntl(fd, F_FULLFSYNC, 0);
27683   }else{
27684     rc = 1;
27685   }
27686   /* If the FULLFSYNC failed, fall back to attempting an fsync().
27687   ** It shouldn't be possible for fullfsync to fail on the local
27688   ** file system (on OSX), so failure indicates that FULLFSYNC
27689   ** isn't supported for this file system. So, attempt an fsync
27690   ** and (for now) ignore the overhead of a superfluous fcntl call.
27691   ** It'd be better to detect fullfsync support once and avoid
27692   ** the fcntl call every time sync is called.
27693   */
27694   if( rc ) rc = fsync(fd);
27695 
27696 #elif defined(__APPLE__)
27697   /* fdatasync() on HFS+ doesn't yet flush the file size if it changed correctly
27698   ** so currently we default to the macro that redefines fdatasync to fsync
27699   */
27700   rc = fsync(fd);
27701 #else
27702   rc = fdatasync(fd);
27703 #if OS_VXWORKS
27704   if( rc==-1 && errno==ENOTSUP ){
27705     rc = fsync(fd);
27706   }
27707 #endif /* OS_VXWORKS */
27708 #endif /* ifdef SQLITE_NO_SYNC elif HAVE_FULLFSYNC */
27709 
27710   if( OS_VXWORKS && rc!= -1 ){
27711     rc = 0;
27712   }
27713   return rc;
27714 }
27715 
27716 /*
27717 ** Make sure all writes to a particular file are committed to disk.
27718 **
27719 ** If dataOnly==0 then both the file itself and its metadata (file
27720 ** size, access time, etc) are synced.  If dataOnly!=0 then only the
27721 ** file data is synced.
27722 **
27723 ** Under Unix, also make sure that the directory entry for the file
27724 ** has been created by fsync-ing the directory that contains the file.
27725 ** If we do not do this and we encounter a power failure, the directory
27726 ** entry for the journal might not exist after we reboot.  The next
27727 ** SQLite to access the file will not know that the journal exists (because
27728 ** the directory entry for the journal was never created) and the transaction
27729 ** will not roll back - possibly leading to database corruption.
27730 */
27731 static int unixSync(sqlite3_file *id, int flags){
27732   int rc;
27733   unixFile *pFile = (unixFile*)id;
27734 
27735   int isDataOnly = (flags&SQLITE_SYNC_DATAONLY);
27736   int isFullsync = (flags&0x0F)==SQLITE_SYNC_FULL;
27737 
27738   /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */
27739   assert((flags&0x0F)==SQLITE_SYNC_NORMAL
27740       || (flags&0x0F)==SQLITE_SYNC_FULL
27741   );
27742 
27743   /* Unix cannot, but some systems may return SQLITE_FULL from here. This
27744   ** line is to test that doing so does not cause any problems.
27745   */
27746   SimulateDiskfullError( return SQLITE_FULL );
27747 
27748   assert( pFile );
27749   OSTRACE(("SYNC    %-3d\n", pFile->h));
27750   rc = full_fsync(pFile->h, isFullsync, isDataOnly);
27751   SimulateIOError( rc=1 );
27752   if( rc ){
27753     pFile->lastErrno = errno;
27754     return unixLogError(SQLITE_IOERR_FSYNC, "full_fsync", pFile->zPath);
27755   }
27756   if( pFile->dirfd>=0 ){
27757     OSTRACE(("DIRSYNC %-3d (have_fullfsync=%d fullsync=%d)\n", pFile->dirfd,
27758             HAVE_FULLFSYNC, isFullsync));
27759 #ifndef SQLITE_DISABLE_DIRSYNC
27760     /* The directory sync is only attempted if full_fsync is
27761     ** turned off or unavailable.  If a full_fsync occurred above,
27762     ** then the directory sync is superfluous.
27763     */
27764     if( (!HAVE_FULLFSYNC || !isFullsync) && full_fsync(pFile->dirfd,0,0) ){
27765        /*
27766        ** We have received multiple reports of fsync() returning
27767        ** errors when applied to directories on certain file systems.
27768        ** A failed directory sync is not a big deal.  So it seems
27769        ** better to ignore the error.  Ticket #1657
27770        */
27771        /* pFile->lastErrno = errno; */
27772        /* return SQLITE_IOERR; */
27773     }
27774 #endif
27775     /* Only need to sync once, so close the  directory when we are done */
27776     robust_close(pFile, pFile->dirfd, __LINE__);
27777     pFile->dirfd = -1;
27778   }
27779   return rc;
27780 }
27781 
27782 /*
27783 ** Truncate an open file to a specified size
27784 */
27785 static int unixTruncate(sqlite3_file *id, i64 nByte){
27786   unixFile *pFile = (unixFile *)id;
27787   int rc;
27788   assert( pFile );
27789   SimulateIOError( return SQLITE_IOERR_TRUNCATE );
27790 
27791   /* If the user has configured a chunk-size for this file, truncate the
27792   ** file so that it consists of an integer number of chunks (i.e. the
27793   ** actual file size after the operation may be larger than the requested
27794   ** size).
27795   */
27796   if( pFile->szChunk ){
27797     nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
27798   }
27799 
27800   rc = robust_ftruncate(pFile->h, (off_t)nByte);
27801   if( rc ){
27802     pFile->lastErrno = errno;
27803     return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath);
27804   }else{
27805 #ifndef NDEBUG
27806     /* If we are doing a normal write to a database file (as opposed to
27807     ** doing a hot-journal rollback or a write to some file other than a
27808     ** normal database file) and we truncate the file to zero length,
27809     ** that effectively updates the change counter.  This might happen
27810     ** when restoring a database using the backup API from a zero-length
27811     ** source.
27812     */
27813     if( pFile->inNormalWrite && nByte==0 ){
27814       pFile->transCntrChng = 1;
27815     }
27816 #endif
27817 
27818     return SQLITE_OK;
27819   }
27820 }
27821 
27822 /*
27823 ** Determine the current size of a file in bytes
27824 */
27825 static int unixFileSize(sqlite3_file *id, i64 *pSize){
27826   int rc;
27827   struct stat buf;
27828   assert( id );
27829   rc = osFstat(((unixFile*)id)->h, &buf);
27830   SimulateIOError( rc=1 );
27831   if( rc!=0 ){
27832     ((unixFile*)id)->lastErrno = errno;
27833     return SQLITE_IOERR_FSTAT;
27834   }
27835   *pSize = buf.st_size;
27836 
27837   /* When opening a zero-size database, the findInodeInfo() procedure
27838   ** writes a single byte into that file in order to work around a bug
27839   ** in the OS-X msdos filesystem.  In order to avoid problems with upper
27840   ** layers, we need to report this file size as zero even though it is
27841   ** really 1.   Ticket #3260.
27842   */
27843   if( *pSize==1 ) *pSize = 0;
27844 
27845 
27846   return SQLITE_OK;
27847 }
27848 
27849 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
27850 /*
27851 ** Handler for proxy-locking file-control verbs.  Defined below in the
27852 ** proxying locking division.
27853 */
27854 static int proxyFileControl(sqlite3_file*,int,void*);
27855 #endif
27856 
27857 /*
27858 ** This function is called to handle the SQLITE_FCNTL_SIZE_HINT
27859 ** file-control operation.
27860 **
27861 ** If the user has configured a chunk-size for this file, it could be
27862 ** that the file needs to be extended at this point. Otherwise, the
27863 ** SQLITE_FCNTL_SIZE_HINT operation is a no-op for Unix.
27864 */
27865 static int fcntlSizeHint(unixFile *pFile, i64 nByte){
27866   if( pFile->szChunk ){
27867     i64 nSize;                    /* Required file size */
27868     struct stat buf;              /* Used to hold return values of fstat() */
27869 
27870     if( osFstat(pFile->h, &buf) ) return SQLITE_IOERR_FSTAT;
27871 
27872     nSize = ((nByte+pFile->szChunk-1) / pFile->szChunk) * pFile->szChunk;
27873     if( nSize>(i64)buf.st_size ){
27874 
27875 #if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE
27876       /* The code below is handling the return value of osFallocate()
27877       ** correctly. posix_fallocate() is defined to "returns zero on success,
27878       ** or an error number on  failure". See the manpage for details. */
27879       int err;
27880       do{
27881         err = osFallocate(pFile->h, buf.st_size, nSize-buf.st_size);
27882       }while( err==EINTR );
27883       if( err ) return SQLITE_IOERR_WRITE;
27884 #else
27885       /* If the OS does not have posix_fallocate(), fake it. First use
27886       ** ftruncate() to set the file size, then write a single byte to
27887       ** the last byte in each block within the extended region. This
27888       ** is the same technique used by glibc to implement posix_fallocate()
27889       ** on systems that do not have a real fallocate() system call.
27890       */
27891       int nBlk = buf.st_blksize;  /* File-system block size */
27892       i64 iWrite;                 /* Next offset to write to */
27893 
27894       if( robust_ftruncate(pFile->h, nSize) ){
27895         pFile->lastErrno = errno;
27896         return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath);
27897       }
27898       iWrite = ((buf.st_size + 2*nBlk - 1)/nBlk)*nBlk-1;
27899       while( iWrite<nSize ){
27900         int nWrite = seekAndWrite(pFile, iWrite, "", 1);
27901         if( nWrite!=1 ) return SQLITE_IOERR_WRITE;
27902         iWrite += nBlk;
27903       }
27904 #endif
27905     }
27906   }
27907 
27908   return SQLITE_OK;
27909 }
27910 
27911 /*
27912 ** Information and control of an open file handle.
27913 */
27914 static int unixFileControl(sqlite3_file *id, int op, void *pArg){
27915   switch( op ){
27916     case SQLITE_FCNTL_LOCKSTATE: {
27917       *(int*)pArg = ((unixFile*)id)->eFileLock;
27918       return SQLITE_OK;
27919     }
27920     case SQLITE_LAST_ERRNO: {
27921       *(int*)pArg = ((unixFile*)id)->lastErrno;
27922       return SQLITE_OK;
27923     }
27924     case SQLITE_FCNTL_CHUNK_SIZE: {
27925       ((unixFile*)id)->szChunk = *(int *)pArg;
27926       return SQLITE_OK;
27927     }
27928     case SQLITE_FCNTL_SIZE_HINT: {
27929       return fcntlSizeHint((unixFile *)id, *(i64 *)pArg);
27930     }
27931 #ifndef NDEBUG
27932     /* The pager calls this method to signal that it has done
27933     ** a rollback and that the database is therefore unchanged and
27934     ** it hence it is OK for the transaction change counter to be
27935     ** unchanged.
27936     */
27937     case SQLITE_FCNTL_DB_UNCHANGED: {
27938       ((unixFile*)id)->dbUpdate = 0;
27939       return SQLITE_OK;
27940     }
27941 #endif
27942 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
27943     case SQLITE_SET_LOCKPROXYFILE:
27944     case SQLITE_GET_LOCKPROXYFILE: {
27945       return proxyFileControl(id,op,pArg);
27946     }
27947 #endif /* SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) */
27948     case SQLITE_FCNTL_SYNC_OMITTED: {
27949       return SQLITE_OK;  /* A no-op */
27950     }
27951   }
27952   return SQLITE_NOTFOUND;
27953 }
27954 
27955 /*
27956 ** Return the sector size in bytes of the underlying block device for
27957 ** the specified file. This is almost always 512 bytes, but may be
27958 ** larger for some devices.
27959 **
27960 ** SQLite code assumes this function cannot fail. It also assumes that
27961 ** if two files are created in the same file-system directory (i.e.
27962 ** a database and its journal file) that the sector size will be the
27963 ** same for both.
27964 */
27965 static int unixSectorSize(sqlite3_file *NotUsed){
27966   UNUSED_PARAMETER(NotUsed);
27967   return SQLITE_DEFAULT_SECTOR_SIZE;
27968 }
27969 
27970 /*
27971 ** Return the device characteristics for the file. This is always 0 for unix.
27972 */
27973 static int unixDeviceCharacteristics(sqlite3_file *NotUsed){
27974   UNUSED_PARAMETER(NotUsed);
27975   return 0;
27976 }
27977 
27978 #ifndef SQLITE_OMIT_WAL
27979 
27980 
27981 /*
27982 ** Object used to represent an shared memory buffer.
27983 **
27984 ** When multiple threads all reference the same wal-index, each thread
27985 ** has its own unixShm object, but they all point to a single instance
27986 ** of this unixShmNode object.  In other words, each wal-index is opened
27987 ** only once per process.
27988 **
27989 ** Each unixShmNode object is connected to a single unixInodeInfo object.
27990 ** We could coalesce this object into unixInodeInfo, but that would mean
27991 ** every open file that does not use shared memory (in other words, most
27992 ** open files) would have to carry around this extra information.  So
27993 ** the unixInodeInfo object contains a pointer to this unixShmNode object
27994 ** and the unixShmNode object is created only when needed.
27995 **
27996 ** unixMutexHeld() must be true when creating or destroying
27997 ** this object or while reading or writing the following fields:
27998 **
27999 **      nRef
28000 **
28001 ** The following fields are read-only after the object is created:
28002 **
28003 **      fid
28004 **      zFilename
28005 **
28006 ** Either unixShmNode.mutex must be held or unixShmNode.nRef==0 and
28007 ** unixMutexHeld() is true when reading or writing any other field
28008 ** in this structure.
28009 */
28010 struct unixShmNode {
28011   unixInodeInfo *pInode;     /* unixInodeInfo that owns this SHM node */
28012   sqlite3_mutex *mutex;      /* Mutex to access this object */
28013   char *zFilename;           /* Name of the mmapped file */
28014   int h;                     /* Open file descriptor */
28015   int szRegion;              /* Size of shared-memory regions */
28016   u16 nRegion;               /* Size of array apRegion */
28017   u8 isReadonly;             /* True if read-only */
28018   char **apRegion;           /* Array of mapped shared-memory regions */
28019   int nRef;                  /* Number of unixShm objects pointing to this */
28020   unixShm *pFirst;           /* All unixShm objects pointing to this */
28021 #ifdef SQLITE_DEBUG
28022   u8 exclMask;               /* Mask of exclusive locks held */
28023   u8 sharedMask;             /* Mask of shared locks held */
28024   u8 nextShmId;              /* Next available unixShm.id value */
28025 #endif
28026 };
28027 
28028 /*
28029 ** Structure used internally by this VFS to record the state of an
28030 ** open shared memory connection.
28031 **
28032 ** The following fields are initialized when this object is created and
28033 ** are read-only thereafter:
28034 **
28035 **    unixShm.pFile
28036 **    unixShm.id
28037 **
28038 ** All other fields are read/write.  The unixShm.pFile->mutex must be held
28039 ** while accessing any read/write fields.
28040 */
28041 struct unixShm {
28042   unixShmNode *pShmNode;     /* The underlying unixShmNode object */
28043   unixShm *pNext;            /* Next unixShm with the same unixShmNode */
28044   u8 hasMutex;               /* True if holding the unixShmNode mutex */
28045   u16 sharedMask;            /* Mask of shared locks held */
28046   u16 exclMask;              /* Mask of exclusive locks held */
28047 #ifdef SQLITE_DEBUG
28048   u8 id;                     /* Id of this connection within its unixShmNode */
28049 #endif
28050 };
28051 
28052 /*
28053 ** Constants used for locking
28054 */
28055 #define UNIX_SHM_BASE   ((22+SQLITE_SHM_NLOCK)*4)         /* first lock byte */
28056 #define UNIX_SHM_DMS    (UNIX_SHM_BASE+SQLITE_SHM_NLOCK)  /* deadman switch */
28057 
28058 /*
28059 ** Apply posix advisory locks for all bytes from ofst through ofst+n-1.
28060 **
28061 ** Locks block if the mask is exactly UNIX_SHM_C and are non-blocking
28062 ** otherwise.
28063 */
28064 static int unixShmSystemLock(
28065   unixShmNode *pShmNode, /* Apply locks to this open shared-memory segment */
28066   int lockType,          /* F_UNLCK, F_RDLCK, or F_WRLCK */
28067   int ofst,              /* First byte of the locking range */
28068   int n                  /* Number of bytes to lock */
28069 ){
28070   struct flock f;       /* The posix advisory locking structure */
28071   int rc = SQLITE_OK;   /* Result code form fcntl() */
28072 
28073   /* Access to the unixShmNode object is serialized by the caller */
28074   assert( sqlite3_mutex_held(pShmNode->mutex) || pShmNode->nRef==0 );
28075 
28076   /* Shared locks never span more than one byte */
28077   assert( n==1 || lockType!=F_RDLCK );
28078 
28079   /* Locks are within range */
28080   assert( n>=1 && n<SQLITE_SHM_NLOCK );
28081 
28082   if( pShmNode->h>=0 ){
28083     /* Initialize the locking parameters */
28084     memset(&f, 0, sizeof(f));
28085     f.l_type = lockType;
28086     f.l_whence = SEEK_SET;
28087     f.l_start = ofst;
28088     f.l_len = n;
28089 
28090     rc = osFcntl(pShmNode->h, F_SETLK, &f);
28091     rc = (rc!=(-1)) ? SQLITE_OK : SQLITE_BUSY;
28092   }
28093 
28094   /* Update the global lock state and do debug tracing */
28095 #ifdef SQLITE_DEBUG
28096   { u16 mask;
28097   OSTRACE(("SHM-LOCK "));
28098   mask = (1<<(ofst+n)) - (1<<ofst);
28099   if( rc==SQLITE_OK ){
28100     if( lockType==F_UNLCK ){
28101       OSTRACE(("unlock %d ok", ofst));
28102       pShmNode->exclMask &= ~mask;
28103       pShmNode->sharedMask &= ~mask;
28104     }else if( lockType==F_RDLCK ){
28105       OSTRACE(("read-lock %d ok", ofst));
28106       pShmNode->exclMask &= ~mask;
28107       pShmNode->sharedMask |= mask;
28108     }else{
28109       assert( lockType==F_WRLCK );
28110       OSTRACE(("write-lock %d ok", ofst));
28111       pShmNode->exclMask |= mask;
28112       pShmNode->sharedMask &= ~mask;
28113     }
28114   }else{
28115     if( lockType==F_UNLCK ){
28116       OSTRACE(("unlock %d failed", ofst));
28117     }else if( lockType==F_RDLCK ){
28118       OSTRACE(("read-lock failed"));
28119     }else{
28120       assert( lockType==F_WRLCK );
28121       OSTRACE(("write-lock %d failed", ofst));
28122     }
28123   }
28124   OSTRACE((" - afterwards %03x,%03x\n",
28125            pShmNode->sharedMask, pShmNode->exclMask));
28126   }
28127 #endif
28128 
28129   return rc;
28130 }
28131 
28132 
28133 /*
28134 ** Purge the unixShmNodeList list of all entries with unixShmNode.nRef==0.
28135 **
28136 ** This is not a VFS shared-memory method; it is a utility function called
28137 ** by VFS shared-memory methods.
28138 */
28139 static void unixShmPurge(unixFile *pFd){
28140   unixShmNode *p = pFd->pInode->pShmNode;
28141   assert( unixMutexHeld() );
28142   if( p && p->nRef==0 ){
28143     int i;
28144     assert( p->pInode==pFd->pInode );
28145     if( p->mutex ) sqlite3_mutex_free(p->mutex);
28146     for(i=0; i<p->nRegion; i++){
28147       if( p->h>=0 ){
28148         munmap(p->apRegion[i], p->szRegion);
28149       }else{
28150         sqlite3_free(p->apRegion[i]);
28151       }
28152     }
28153     sqlite3_free(p->apRegion);
28154     if( p->h>=0 ){
28155       robust_close(pFd, p->h, __LINE__);
28156       p->h = -1;
28157     }
28158     p->pInode->pShmNode = 0;
28159     sqlite3_free(p);
28160   }
28161 }
28162 
28163 /*
28164 ** Open a shared-memory area associated with open database file pDbFd.
28165 ** This particular implementation uses mmapped files.
28166 **
28167 ** The file used to implement shared-memory is in the same directory
28168 ** as the open database file and has the same name as the open database
28169 ** file with the "-shm" suffix added.  For example, if the database file
28170 ** is "/home/user1/config.db" then the file that is created and mmapped
28171 ** for shared memory will be called "/home/user1/config.db-shm".
28172 **
28173 ** Another approach to is to use files in /dev/shm or /dev/tmp or an
28174 ** some other tmpfs mount. But if a file in a different directory
28175 ** from the database file is used, then differing access permissions
28176 ** or a chroot() might cause two different processes on the same
28177 ** database to end up using different files for shared memory -
28178 ** meaning that their memory would not really be shared - resulting
28179 ** in database corruption.  Nevertheless, this tmpfs file usage
28180 ** can be enabled at compile-time using -DSQLITE_SHM_DIRECTORY="/dev/shm"
28181 ** or the equivalent.  The use of the SQLITE_SHM_DIRECTORY compile-time
28182 ** option results in an incompatible build of SQLite;  builds of SQLite
28183 ** that with differing SQLITE_SHM_DIRECTORY settings attempt to use the
28184 ** same database file at the same time, database corruption will likely
28185 ** result. The SQLITE_SHM_DIRECTORY compile-time option is considered
28186 ** "unsupported" and may go away in a future SQLite release.
28187 **
28188 ** When opening a new shared-memory file, if no other instances of that
28189 ** file are currently open, in this process or in other processes, then
28190 ** the file must be truncated to zero length or have its header cleared.
28191 **
28192 ** If the original database file (pDbFd) is using the "unix-excl" VFS
28193 ** that means that an exclusive lock is held on the database file and
28194 ** that no other processes are able to read or write the database.  In
28195 ** that case, we do not really need shared memory.  No shared memory
28196 ** file is created.  The shared memory will be simulated with heap memory.
28197 */
28198 static int unixOpenSharedMemory(unixFile *pDbFd){
28199   struct unixShm *p = 0;          /* The connection to be opened */
28200   struct unixShmNode *pShmNode;   /* The underlying mmapped file */
28201   int rc;                         /* Result code */
28202   unixInodeInfo *pInode;          /* The inode of fd */
28203   char *zShmFilename;             /* Name of the file used for SHM */
28204   int nShmFilename;               /* Size of the SHM filename in bytes */
28205 
28206   /* Allocate space for the new unixShm object. */
28207   p = sqlite3_malloc( sizeof(*p) );
28208   if( p==0 ) return SQLITE_NOMEM;
28209   memset(p, 0, sizeof(*p));
28210   assert( pDbFd->pShm==0 );
28211 
28212   /* Check to see if a unixShmNode object already exists. Reuse an existing
28213   ** one if present. Create a new one if necessary.
28214   */
28215   unixEnterMutex();
28216   pInode = pDbFd->pInode;
28217   pShmNode = pInode->pShmNode;
28218   if( pShmNode==0 ){
28219     struct stat sStat;                 /* fstat() info for database file */
28220 
28221     /* Call fstat() to figure out the permissions on the database file. If
28222     ** a new *-shm file is created, an attempt will be made to create it
28223     ** with the same permissions. The actual permissions the file is created
28224     ** with are subject to the current umask setting.
28225     */
28226     if( osFstat(pDbFd->h, &sStat) && pInode->bProcessLock==0 ){
28227       rc = SQLITE_IOERR_FSTAT;
28228       goto shm_open_err;
28229     }
28230 
28231 #ifdef SQLITE_SHM_DIRECTORY
28232     nShmFilename = sizeof(SQLITE_SHM_DIRECTORY) + 30;
28233 #else
28234     nShmFilename = 5 + (int)strlen(pDbFd->zPath);
28235 #endif
28236     pShmNode = sqlite3_malloc( sizeof(*pShmNode) + nShmFilename );
28237     if( pShmNode==0 ){
28238       rc = SQLITE_NOMEM;
28239       goto shm_open_err;
28240     }
28241     memset(pShmNode, 0, sizeof(*pShmNode));
28242     zShmFilename = pShmNode->zFilename = (char*)&pShmNode[1];
28243 #ifdef SQLITE_SHM_DIRECTORY
28244     sqlite3_snprintf(nShmFilename, zShmFilename,
28245                      SQLITE_SHM_DIRECTORY "/sqlite-shm-%x-%x",
28246                      (u32)sStat.st_ino, (u32)sStat.st_dev);
28247 #else
28248     sqlite3_snprintf(nShmFilename, zShmFilename, "%s-shm", pDbFd->zPath);
28249     sqlite3FileSuffix3(pDbFd->zPath, zShmFilename);
28250 #endif
28251     pShmNode->h = -1;
28252     pDbFd->pInode->pShmNode = pShmNode;
28253     pShmNode->pInode = pDbFd->pInode;
28254     pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
28255     if( pShmNode->mutex==0 ){
28256       rc = SQLITE_NOMEM;
28257       goto shm_open_err;
28258     }
28259 
28260     if( pInode->bProcessLock==0 ){
28261       pShmNode->h = robust_open(zShmFilename, O_RDWR|O_CREAT,
28262                                (sStat.st_mode & 0777));
28263       if( pShmNode->h<0 ){
28264         const char *zRO;
28265         zRO = sqlite3_uri_parameter(pDbFd->zPath, "readonly_shm");
28266         if( zRO && sqlite3GetBoolean(zRO) ){
28267           pShmNode->h = robust_open(zShmFilename, O_RDONLY,
28268                                     (sStat.st_mode & 0777));
28269           pShmNode->isReadonly = 1;
28270         }
28271         if( pShmNode->h<0 ){
28272           rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zShmFilename);
28273           goto shm_open_err;
28274         }
28275       }
28276 
28277       /* Check to see if another process is holding the dead-man switch.
28278       ** If not, truncate the file to zero length.
28279       */
28280       rc = SQLITE_OK;
28281       if( unixShmSystemLock(pShmNode, F_WRLCK, UNIX_SHM_DMS, 1)==SQLITE_OK ){
28282         if( robust_ftruncate(pShmNode->h, 0) ){
28283           rc = unixLogError(SQLITE_IOERR_SHMOPEN, "ftruncate", zShmFilename);
28284         }
28285       }
28286       if( rc==SQLITE_OK ){
28287         rc = unixShmSystemLock(pShmNode, F_RDLCK, UNIX_SHM_DMS, 1);
28288       }
28289       if( rc ) goto shm_open_err;
28290     }
28291   }
28292 
28293   /* Make the new connection a child of the unixShmNode */
28294   p->pShmNode = pShmNode;
28295 #ifdef SQLITE_DEBUG
28296   p->id = pShmNode->nextShmId++;
28297 #endif
28298   pShmNode->nRef++;
28299   pDbFd->pShm = p;
28300   unixLeaveMutex();
28301 
28302   /* The reference count on pShmNode has already been incremented under
28303   ** the cover of the unixEnterMutex() mutex and the pointer from the
28304   ** new (struct unixShm) object to the pShmNode has been set. All that is
28305   ** left to do is to link the new object into the linked list starting
28306   ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex
28307   ** mutex.
28308   */
28309   sqlite3_mutex_enter(pShmNode->mutex);
28310   p->pNext = pShmNode->pFirst;
28311   pShmNode->pFirst = p;
28312   sqlite3_mutex_leave(pShmNode->mutex);
28313   return SQLITE_OK;
28314 
28315   /* Jump here on any error */
28316 shm_open_err:
28317   unixShmPurge(pDbFd);       /* This call frees pShmNode if required */
28318   sqlite3_free(p);
28319   unixLeaveMutex();
28320   return rc;
28321 }
28322 
28323 /*
28324 ** This function is called to obtain a pointer to region iRegion of the
28325 ** shared-memory associated with the database file fd. Shared-memory regions
28326 ** are numbered starting from zero. Each shared-memory region is szRegion
28327 ** bytes in size.
28328 **
28329 ** If an error occurs, an error code is returned and *pp is set to NULL.
28330 **
28331 ** Otherwise, if the bExtend parameter is 0 and the requested shared-memory
28332 ** region has not been allocated (by any client, including one running in a
28333 ** separate process), then *pp is set to NULL and SQLITE_OK returned. If
28334 ** bExtend is non-zero and the requested shared-memory region has not yet
28335 ** been allocated, it is allocated by this function.
28336 **
28337 ** If the shared-memory region has already been allocated or is allocated by
28338 ** this call as described above, then it is mapped into this processes
28339 ** address space (if it is not already), *pp is set to point to the mapped
28340 ** memory and SQLITE_OK returned.
28341 */
28342 static int unixShmMap(
28343   sqlite3_file *fd,               /* Handle open on database file */
28344   int iRegion,                    /* Region to retrieve */
28345   int szRegion,                   /* Size of regions */
28346   int bExtend,                    /* True to extend file if necessary */
28347   void volatile **pp              /* OUT: Mapped memory */
28348 ){
28349   unixFile *pDbFd = (unixFile*)fd;
28350   unixShm *p;
28351   unixShmNode *pShmNode;
28352   int rc = SQLITE_OK;
28353 
28354   /* If the shared-memory file has not yet been opened, open it now. */
28355   if( pDbFd->pShm==0 ){
28356     rc = unixOpenSharedMemory(pDbFd);
28357     if( rc!=SQLITE_OK ) return rc;
28358   }
28359 
28360   p = pDbFd->pShm;
28361   pShmNode = p->pShmNode;
28362   sqlite3_mutex_enter(pShmNode->mutex);
28363   assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 );
28364   assert( pShmNode->pInode==pDbFd->pInode );
28365   assert( pShmNode->h>=0 || pDbFd->pInode->bProcessLock==1 );
28366   assert( pShmNode->h<0 || pDbFd->pInode->bProcessLock==0 );
28367 
28368   if( pShmNode->nRegion<=iRegion ){
28369     char **apNew;                      /* New apRegion[] array */
28370     int nByte = (iRegion+1)*szRegion;  /* Minimum required file size */
28371     struct stat sStat;                 /* Used by fstat() */
28372 
28373     pShmNode->szRegion = szRegion;
28374 
28375     if( pShmNode->h>=0 ){
28376       /* The requested region is not mapped into this processes address space.
28377       ** Check to see if it has been allocated (i.e. if the wal-index file is
28378       ** large enough to contain the requested region).
28379       */
28380       if( osFstat(pShmNode->h, &sStat) ){
28381         rc = SQLITE_IOERR_SHMSIZE;
28382         goto shmpage_out;
28383       }
28384 
28385       if( sStat.st_size<nByte ){
28386         /* The requested memory region does not exist. If bExtend is set to
28387         ** false, exit early. *pp will be set to NULL and SQLITE_OK returned.
28388         **
28389         ** Alternatively, if bExtend is true, use ftruncate() to allocate
28390         ** the requested memory region.
28391         */
28392         if( !bExtend ) goto shmpage_out;
28393         if( robust_ftruncate(pShmNode->h, nByte) ){
28394           rc = unixLogError(SQLITE_IOERR_SHMSIZE, "ftruncate",
28395                             pShmNode->zFilename);
28396           goto shmpage_out;
28397         }
28398       }
28399     }
28400 
28401     /* Map the requested memory region into this processes address space. */
28402     apNew = (char **)sqlite3_realloc(
28403         pShmNode->apRegion, (iRegion+1)*sizeof(char *)
28404     );
28405     if( !apNew ){
28406       rc = SQLITE_IOERR_NOMEM;
28407       goto shmpage_out;
28408     }
28409     pShmNode->apRegion = apNew;
28410     while(pShmNode->nRegion<=iRegion){
28411       void *pMem;
28412       if( pShmNode->h>=0 ){
28413         pMem = mmap(0, szRegion,
28414             pShmNode->isReadonly ? PROT_READ : PROT_READ|PROT_WRITE,
28415             MAP_SHARED, pShmNode->h, pShmNode->nRegion*szRegion
28416         );
28417         if( pMem==MAP_FAILED ){
28418           rc = unixLogError(SQLITE_IOERR_SHMMAP, "mmap", pShmNode->zFilename);
28419           goto shmpage_out;
28420         }
28421       }else{
28422         pMem = sqlite3_malloc(szRegion);
28423         if( pMem==0 ){
28424           rc = SQLITE_NOMEM;
28425           goto shmpage_out;
28426         }
28427         memset(pMem, 0, szRegion);
28428       }
28429       pShmNode->apRegion[pShmNode->nRegion] = pMem;
28430       pShmNode->nRegion++;
28431     }
28432   }
28433 
28434 shmpage_out:
28435   if( pShmNode->nRegion>iRegion ){
28436     *pp = pShmNode->apRegion[iRegion];
28437   }else{
28438     *pp = 0;
28439   }
28440   if( pShmNode->isReadonly && rc==SQLITE_OK ) rc = SQLITE_READONLY;
28441   sqlite3_mutex_leave(pShmNode->mutex);
28442   return rc;
28443 }
28444 
28445 /*
28446 ** Change the lock state for a shared-memory segment.
28447 **
28448 ** Note that the relationship between SHAREd and EXCLUSIVE locks is a little
28449 ** different here than in posix.  In xShmLock(), one can go from unlocked
28450 ** to shared and back or from unlocked to exclusive and back.  But one may
28451 ** not go from shared to exclusive or from exclusive to shared.
28452 */
28453 static int unixShmLock(
28454   sqlite3_file *fd,          /* Database file holding the shared memory */
28455   int ofst,                  /* First lock to acquire or release */
28456   int n,                     /* Number of locks to acquire or release */
28457   int flags                  /* What to do with the lock */
28458 ){
28459   unixFile *pDbFd = (unixFile*)fd;      /* Connection holding shared memory */
28460   unixShm *p = pDbFd->pShm;             /* The shared memory being locked */
28461   unixShm *pX;                          /* For looping over all siblings */
28462   unixShmNode *pShmNode = p->pShmNode;  /* The underlying file iNode */
28463   int rc = SQLITE_OK;                   /* Result code */
28464   u16 mask;                             /* Mask of locks to take or release */
28465 
28466   assert( pShmNode==pDbFd->pInode->pShmNode );
28467   assert( pShmNode->pInode==pDbFd->pInode );
28468   assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK );
28469   assert( n>=1 );
28470   assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED)
28471        || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE)
28472        || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED)
28473        || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) );
28474   assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 );
28475   assert( pShmNode->h>=0 || pDbFd->pInode->bProcessLock==1 );
28476   assert( pShmNode->h<0 || pDbFd->pInode->bProcessLock==0 );
28477 
28478   mask = (1<<(ofst+n)) - (1<<ofst);
28479   assert( n>1 || mask==(1<<ofst) );
28480   sqlite3_mutex_enter(pShmNode->mutex);
28481   if( flags & SQLITE_SHM_UNLOCK ){
28482     u16 allMask = 0; /* Mask of locks held by siblings */
28483 
28484     /* See if any siblings hold this same lock */
28485     for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
28486       if( pX==p ) continue;
28487       assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 );
28488       allMask |= pX->sharedMask;
28489     }
28490 
28491     /* Unlock the system-level locks */
28492     if( (mask & allMask)==0 ){
28493       rc = unixShmSystemLock(pShmNode, F_UNLCK, ofst+UNIX_SHM_BASE, n);
28494     }else{
28495       rc = SQLITE_OK;
28496     }
28497 
28498     /* Undo the local locks */
28499     if( rc==SQLITE_OK ){
28500       p->exclMask &= ~mask;
28501       p->sharedMask &= ~mask;
28502     }
28503   }else if( flags & SQLITE_SHM_SHARED ){
28504     u16 allShared = 0;  /* Union of locks held by connections other than "p" */
28505 
28506     /* Find out which shared locks are already held by sibling connections.
28507     ** If any sibling already holds an exclusive lock, go ahead and return
28508     ** SQLITE_BUSY.
28509     */
28510     for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
28511       if( (pX->exclMask & mask)!=0 ){
28512         rc = SQLITE_BUSY;
28513         break;
28514       }
28515       allShared |= pX->sharedMask;
28516     }
28517 
28518     /* Get shared locks at the system level, if necessary */
28519     if( rc==SQLITE_OK ){
28520       if( (allShared & mask)==0 ){
28521         rc = unixShmSystemLock(pShmNode, F_RDLCK, ofst+UNIX_SHM_BASE, n);
28522       }else{
28523         rc = SQLITE_OK;
28524       }
28525     }
28526 
28527     /* Get the local shared locks */
28528     if( rc==SQLITE_OK ){
28529       p->sharedMask |= mask;
28530     }
28531   }else{
28532     /* Make sure no sibling connections hold locks that will block this
28533     ** lock.  If any do, return SQLITE_BUSY right away.
28534     */
28535     for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
28536       if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){
28537         rc = SQLITE_BUSY;
28538         break;
28539       }
28540     }
28541 
28542     /* Get the exclusive locks at the system level.  Then if successful
28543     ** also mark the local connection as being locked.
28544     */
28545     if( rc==SQLITE_OK ){
28546       rc = unixShmSystemLock(pShmNode, F_WRLCK, ofst+UNIX_SHM_BASE, n);
28547       if( rc==SQLITE_OK ){
28548         assert( (p->sharedMask & mask)==0 );
28549         p->exclMask |= mask;
28550       }
28551     }
28552   }
28553   sqlite3_mutex_leave(pShmNode->mutex);
28554   OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x\n",
28555            p->id, getpid(), p->sharedMask, p->exclMask));
28556   return rc;
28557 }
28558 
28559 /*
28560 ** Implement a memory barrier or memory fence on shared memory.
28561 **
28562 ** All loads and stores begun before the barrier must complete before
28563 ** any load or store begun after the barrier.
28564 */
28565 static void unixShmBarrier(
28566   sqlite3_file *fd                /* Database file holding the shared memory */
28567 ){
28568   UNUSED_PARAMETER(fd);
28569   unixEnterMutex();
28570   unixLeaveMutex();
28571 }
28572 
28573 /*
28574 ** Close a connection to shared-memory.  Delete the underlying
28575 ** storage if deleteFlag is true.
28576 **
28577 ** If there is no shared memory associated with the connection then this
28578 ** routine is a harmless no-op.
28579 */
28580 static int unixShmUnmap(
28581   sqlite3_file *fd,               /* The underlying database file */
28582   int deleteFlag                  /* Delete shared-memory if true */
28583 ){
28584   unixShm *p;                     /* The connection to be closed */
28585   unixShmNode *pShmNode;          /* The underlying shared-memory file */
28586   unixShm **pp;                   /* For looping over sibling connections */
28587   unixFile *pDbFd;                /* The underlying database file */
28588 
28589   pDbFd = (unixFile*)fd;
28590   p = pDbFd->pShm;
28591   if( p==0 ) return SQLITE_OK;
28592   pShmNode = p->pShmNode;
28593 
28594   assert( pShmNode==pDbFd->pInode->pShmNode );
28595   assert( pShmNode->pInode==pDbFd->pInode );
28596 
28597   /* Remove connection p from the set of connections associated
28598   ** with pShmNode */
28599   sqlite3_mutex_enter(pShmNode->mutex);
28600   for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){}
28601   *pp = p->pNext;
28602 
28603   /* Free the connection p */
28604   sqlite3_free(p);
28605   pDbFd->pShm = 0;
28606   sqlite3_mutex_leave(pShmNode->mutex);
28607 
28608   /* If pShmNode->nRef has reached 0, then close the underlying
28609   ** shared-memory file, too */
28610   unixEnterMutex();
28611   assert( pShmNode->nRef>0 );
28612   pShmNode->nRef--;
28613   if( pShmNode->nRef==0 ){
28614     if( deleteFlag && pShmNode->h>=0 ) unlink(pShmNode->zFilename);
28615     unixShmPurge(pDbFd);
28616   }
28617   unixLeaveMutex();
28618 
28619   return SQLITE_OK;
28620 }
28621 
28622 
28623 #else
28624 # define unixShmMap     0
28625 # define unixShmLock    0
28626 # define unixShmBarrier 0
28627 # define unixShmUnmap   0
28628 #endif /* #ifndef SQLITE_OMIT_WAL */
28629 
28630 /*
28631 ** Here ends the implementation of all sqlite3_file methods.
28632 **
28633 ********************** End sqlite3_file Methods *******************************
28634 ******************************************************************************/
28635 
28636 /*
28637 ** This division contains definitions of sqlite3_io_methods objects that
28638 ** implement various file locking strategies.  It also contains definitions
28639 ** of "finder" functions.  A finder-function is used to locate the appropriate
28640 ** sqlite3_io_methods object for a particular database file.  The pAppData
28641 ** field of the sqlite3_vfs VFS objects are initialized to be pointers to
28642 ** the correct finder-function for that VFS.
28643 **
28644 ** Most finder functions return a pointer to a fixed sqlite3_io_methods
28645 ** object.  The only interesting finder-function is autolockIoFinder, which
28646 ** looks at the filesystem type and tries to guess the best locking
28647 ** strategy from that.
28648 **
28649 ** For finder-funtion F, two objects are created:
28650 **
28651 **    (1) The real finder-function named "FImpt()".
28652 **
28653 **    (2) A constant pointer to this function named just "F".
28654 **
28655 **
28656 ** A pointer to the F pointer is used as the pAppData value for VFS
28657 ** objects.  We have to do this instead of letting pAppData point
28658 ** directly at the finder-function since C90 rules prevent a void*
28659 ** from be cast into a function pointer.
28660 **
28661 **
28662 ** Each instance of this macro generates two objects:
28663 **
28664 **   *  A constant sqlite3_io_methods object call METHOD that has locking
28665 **      methods CLOSE, LOCK, UNLOCK, CKRESLOCK.
28666 **
28667 **   *  An I/O method finder function called FINDER that returns a pointer
28668 **      to the METHOD object in the previous bullet.
28669 */
28670 #define IOMETHODS(FINDER, METHOD, VERSION, CLOSE, LOCK, UNLOCK, CKLOCK)      \
28671 static const sqlite3_io_methods METHOD = {                                   \
28672    VERSION,                    /* iVersion */                                \
28673    CLOSE,                      /* xClose */                                  \
28674    unixRead,                   /* xRead */                                   \
28675    unixWrite,                  /* xWrite */                                  \
28676    unixTruncate,               /* xTruncate */                               \
28677    unixSync,                   /* xSync */                                   \
28678    unixFileSize,               /* xFileSize */                               \
28679    LOCK,                       /* xLock */                                   \
28680    UNLOCK,                     /* xUnlock */                                 \
28681    CKLOCK,                     /* xCheckReservedLock */                      \
28682    unixFileControl,            /* xFileControl */                            \
28683    unixSectorSize,             /* xSectorSize */                             \
28684    unixDeviceCharacteristics,  /* xDeviceCapabilities */                     \
28685    unixShmMap,                 /* xShmMap */                                 \
28686    unixShmLock,                /* xShmLock */                                \
28687    unixShmBarrier,             /* xShmBarrier */                             \
28688    unixShmUnmap                /* xShmUnmap */                               \
28689 };                                                                           \
28690 static const sqlite3_io_methods *FINDER##Impl(const char *z, unixFile *p){   \
28691   UNUSED_PARAMETER(z); UNUSED_PARAMETER(p);                                  \
28692   return &METHOD;                                                            \
28693 }                                                                            \
28694 static const sqlite3_io_methods *(*const FINDER)(const char*,unixFile *p)    \
28695     = FINDER##Impl;
28696 
28697 /*
28698 ** Here are all of the sqlite3_io_methods objects for each of the
28699 ** locking strategies.  Functions that return pointers to these methods
28700 ** are also created.
28701 */
28702 IOMETHODS(
28703   posixIoFinder,            /* Finder function name */
28704   posixIoMethods,           /* sqlite3_io_methods object name */
28705   2,                        /* shared memory is enabled */
28706   unixClose,                /* xClose method */
28707   unixLock,                 /* xLock method */
28708   unixUnlock,               /* xUnlock method */
28709   unixCheckReservedLock     /* xCheckReservedLock method */
28710 )
28711 IOMETHODS(
28712   nolockIoFinder,           /* Finder function name */
28713   nolockIoMethods,          /* sqlite3_io_methods object name */
28714   1,                        /* shared memory is disabled */
28715   nolockClose,              /* xClose method */
28716   nolockLock,               /* xLock method */
28717   nolockUnlock,             /* xUnlock method */
28718   nolockCheckReservedLock   /* xCheckReservedLock method */
28719 )
28720 IOMETHODS(
28721   dotlockIoFinder,          /* Finder function name */
28722   dotlockIoMethods,         /* sqlite3_io_methods object name */
28723   1,                        /* shared memory is disabled */
28724   dotlockClose,             /* xClose method */
28725   dotlockLock,              /* xLock method */
28726   dotlockUnlock,            /* xUnlock method */
28727   dotlockCheckReservedLock  /* xCheckReservedLock method */
28728 )
28729 
28730 #if SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS
28731 IOMETHODS(
28732   flockIoFinder,            /* Finder function name */
28733   flockIoMethods,           /* sqlite3_io_methods object name */
28734   1,                        /* shared memory is disabled */
28735   flockClose,               /* xClose method */
28736   flockLock,                /* xLock method */
28737   flockUnlock,              /* xUnlock method */
28738   flockCheckReservedLock    /* xCheckReservedLock method */
28739 )
28740 #endif
28741 
28742 #if OS_VXWORKS
28743 IOMETHODS(
28744   semIoFinder,              /* Finder function name */
28745   semIoMethods,             /* sqlite3_io_methods object name */
28746   1,                        /* shared memory is disabled */
28747   semClose,                 /* xClose method */
28748   semLock,                  /* xLock method */
28749   semUnlock,                /* xUnlock method */
28750   semCheckReservedLock      /* xCheckReservedLock method */
28751 )
28752 #endif
28753 
28754 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
28755 IOMETHODS(
28756   afpIoFinder,              /* Finder function name */
28757   afpIoMethods,             /* sqlite3_io_methods object name */
28758   1,                        /* shared memory is disabled */
28759   afpClose,                 /* xClose method */
28760   afpLock,                  /* xLock method */
28761   afpUnlock,                /* xUnlock method */
28762   afpCheckReservedLock      /* xCheckReservedLock method */
28763 )
28764 #endif
28765 
28766 /*
28767 ** The proxy locking method is a "super-method" in the sense that it
28768 ** opens secondary file descriptors for the conch and lock files and
28769 ** it uses proxy, dot-file, AFP, and flock() locking methods on those
28770 ** secondary files.  For this reason, the division that implements
28771 ** proxy locking is located much further down in the file.  But we need
28772 ** to go ahead and define the sqlite3_io_methods and finder function
28773 ** for proxy locking here.  So we forward declare the I/O methods.
28774 */
28775 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
28776 static int proxyClose(sqlite3_file*);
28777 static int proxyLock(sqlite3_file*, int);
28778 static int proxyUnlock(sqlite3_file*, int);
28779 static int proxyCheckReservedLock(sqlite3_file*, int*);
28780 IOMETHODS(
28781   proxyIoFinder,            /* Finder function name */
28782   proxyIoMethods,           /* sqlite3_io_methods object name */
28783   1,                        /* shared memory is disabled */
28784   proxyClose,               /* xClose method */
28785   proxyLock,                /* xLock method */
28786   proxyUnlock,              /* xUnlock method */
28787   proxyCheckReservedLock    /* xCheckReservedLock method */
28788 )
28789 #endif
28790 
28791 /* nfs lockd on OSX 10.3+ doesn't clear write locks when a read lock is set */
28792 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
28793 IOMETHODS(
28794   nfsIoFinder,               /* Finder function name */
28795   nfsIoMethods,              /* sqlite3_io_methods object name */
28796   1,                         /* shared memory is disabled */
28797   unixClose,                 /* xClose method */
28798   unixLock,                  /* xLock method */
28799   nfsUnlock,                 /* xUnlock method */
28800   unixCheckReservedLock      /* xCheckReservedLock method */
28801 )
28802 #endif
28803 
28804 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
28805 /*
28806 ** This "finder" function attempts to determine the best locking strategy
28807 ** for the database file "filePath".  It then returns the sqlite3_io_methods
28808 ** object that implements that strategy.
28809 **
28810 ** This is for MacOSX only.
28811 */
28812 static const sqlite3_io_methods *autolockIoFinderImpl(
28813   const char *filePath,    /* name of the database file */
28814   unixFile *pNew           /* open file object for the database file */
28815 ){
28816   static const struct Mapping {
28817     const char *zFilesystem;              /* Filesystem type name */
28818     const sqlite3_io_methods *pMethods;   /* Appropriate locking method */
28819   } aMap[] = {
28820     { "hfs",    &posixIoMethods },
28821     { "ufs",    &posixIoMethods },
28822     { "afpfs",  &afpIoMethods },
28823     { "smbfs",  &afpIoMethods },
28824     { "webdav", &nolockIoMethods },
28825     { 0, 0 }
28826   };
28827   int i;
28828   struct statfs fsInfo;
28829   struct flock lockInfo;
28830 
28831   if( !filePath ){
28832     /* If filePath==NULL that means we are dealing with a transient file
28833     ** that does not need to be locked. */
28834     return &nolockIoMethods;
28835   }
28836   if( statfs(filePath, &fsInfo) != -1 ){
28837     if( fsInfo.f_flags & MNT_RDONLY ){
28838       return &nolockIoMethods;
28839     }
28840     for(i=0; aMap[i].zFilesystem; i++){
28841       if( strcmp(fsInfo.f_fstypename, aMap[i].zFilesystem)==0 ){
28842         return aMap[i].pMethods;
28843       }
28844     }
28845   }
28846 
28847   /* Default case. Handles, amongst others, "nfs".
28848   ** Test byte-range lock using fcntl(). If the call succeeds,
28849   ** assume that the file-system supports POSIX style locks.
28850   */
28851   lockInfo.l_len = 1;
28852   lockInfo.l_start = 0;
28853   lockInfo.l_whence = SEEK_SET;
28854   lockInfo.l_type = F_RDLCK;
28855   if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) {
28856     if( strcmp(fsInfo.f_fstypename, "nfs")==0 ){
28857       return &nfsIoMethods;
28858     } else {
28859       return &posixIoMethods;
28860     }
28861   }else{
28862     return &dotlockIoMethods;
28863   }
28864 }
28865 static const sqlite3_io_methods
28866   *(*const autolockIoFinder)(const char*,unixFile*) = autolockIoFinderImpl;
28867 
28868 #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
28869 
28870 #if OS_VXWORKS && SQLITE_ENABLE_LOCKING_STYLE
28871 /*
28872 ** This "finder" function attempts to determine the best locking strategy
28873 ** for the database file "filePath".  It then returns the sqlite3_io_methods
28874 ** object that implements that strategy.
28875 **
28876 ** This is for VXWorks only.
28877 */
28878 static const sqlite3_io_methods *autolockIoFinderImpl(
28879   const char *filePath,    /* name of the database file */
28880   unixFile *pNew           /* the open file object */
28881 ){
28882   struct flock lockInfo;
28883 
28884   if( !filePath ){
28885     /* If filePath==NULL that means we are dealing with a transient file
28886     ** that does not need to be locked. */
28887     return &nolockIoMethods;
28888   }
28889 
28890   /* Test if fcntl() is supported and use POSIX style locks.
28891   ** Otherwise fall back to the named semaphore method.
28892   */
28893   lockInfo.l_len = 1;
28894   lockInfo.l_start = 0;
28895   lockInfo.l_whence = SEEK_SET;
28896   lockInfo.l_type = F_RDLCK;
28897   if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) {
28898     return &posixIoMethods;
28899   }else{
28900     return &semIoMethods;
28901   }
28902 }
28903 static const sqlite3_io_methods
28904   *(*const autolockIoFinder)(const char*,unixFile*) = autolockIoFinderImpl;
28905 
28906 #endif /* OS_VXWORKS && SQLITE_ENABLE_LOCKING_STYLE */
28907 
28908 /*
28909 ** An abstract type for a pointer to a IO method finder function:
28910 */
28911 typedef const sqlite3_io_methods *(*finder_type)(const char*,unixFile*);
28912 
28913 
28914 /****************************************************************************
28915 **************************** sqlite3_vfs methods ****************************
28916 **
28917 ** This division contains the implementation of methods on the
28918 ** sqlite3_vfs object.
28919 */
28920 
28921 /*
28922 ** Initialize the contents of the unixFile structure pointed to by pId.
28923 */
28924 static int fillInUnixFile(
28925   sqlite3_vfs *pVfs,      /* Pointer to vfs object */
28926   int h,                  /* Open file descriptor of file being opened */
28927   int dirfd,              /* Directory file descriptor */
28928   sqlite3_file *pId,      /* Write to the unixFile structure here */
28929   const char *zFilename,  /* Name of the file being opened */
28930   int noLock,             /* Omit locking if true */
28931   int isDelete,           /* Delete on close if true */
28932   int isReadOnly          /* True if the file is opened read-only */
28933 ){
28934   const sqlite3_io_methods *pLockingStyle;
28935   unixFile *pNew = (unixFile *)pId;
28936   int rc = SQLITE_OK;
28937 
28938   assert( pNew->pInode==NULL );
28939 
28940   /* Parameter isDelete is only used on vxworks. Express this explicitly
28941   ** here to prevent compiler warnings about unused parameters.
28942   */
28943   UNUSED_PARAMETER(isDelete);
28944 
28945   /* Usually the path zFilename should not be a relative pathname. The
28946   ** exception is when opening the proxy "conch" file in builds that
28947   ** include the special Apple locking styles.
28948   */
28949 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
28950   assert( zFilename==0 || zFilename[0]=='/'
28951     || pVfs->pAppData==(void*)&autolockIoFinder );
28952 #else
28953   assert( zFilename==0 || zFilename[0]=='/' );
28954 #endif
28955 
28956   OSTRACE(("OPEN    %-3d %s\n", h, zFilename));
28957   pNew->h = h;
28958   pNew->dirfd = dirfd;
28959   pNew->zPath = zFilename;
28960   if( memcmp(pVfs->zName,"unix-excl",10)==0 ){
28961     pNew->ctrlFlags = UNIXFILE_EXCL;
28962   }else{
28963     pNew->ctrlFlags = 0;
28964   }
28965   if( isReadOnly ){
28966     pNew->ctrlFlags |= UNIXFILE_RDONLY;
28967   }
28968 
28969 #if OS_VXWORKS
28970   pNew->pId = vxworksFindFileId(zFilename);
28971   if( pNew->pId==0 ){
28972     noLock = 1;
28973     rc = SQLITE_NOMEM;
28974   }
28975 #endif
28976 
28977   if( noLock ){
28978     pLockingStyle = &nolockIoMethods;
28979   }else{
28980     pLockingStyle = (**(finder_type*)pVfs->pAppData)(zFilename, pNew);
28981 #if SQLITE_ENABLE_LOCKING_STYLE
28982     /* Cache zFilename in the locking context (AFP and dotlock override) for
28983     ** proxyLock activation is possible (remote proxy is based on db name)
28984     ** zFilename remains valid until file is closed, to support */
28985     pNew->lockingContext = (void*)zFilename;
28986 #endif
28987   }
28988 
28989   if( pLockingStyle == &posixIoMethods
28990 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
28991     || pLockingStyle == &nfsIoMethods
28992 #endif
28993   ){
28994     unixEnterMutex();
28995     rc = findInodeInfo(pNew, &pNew->pInode);
28996     if( rc!=SQLITE_OK ){
28997       /* If an error occured in findInodeInfo(), close the file descriptor
28998       ** immediately, before releasing the mutex. findInodeInfo() may fail
28999       ** in two scenarios:
29000       **
29001       **   (a) A call to fstat() failed.
29002       **   (b) A malloc failed.
29003       **
29004       ** Scenario (b) may only occur if the process is holding no other
29005       ** file descriptors open on the same file. If there were other file
29006       ** descriptors on this file, then no malloc would be required by
29007       ** findInodeInfo(). If this is the case, it is quite safe to close
29008       ** handle h - as it is guaranteed that no posix locks will be released
29009       ** by doing so.
29010       **
29011       ** If scenario (a) caused the error then things are not so safe. The
29012       ** implicit assumption here is that if fstat() fails, things are in
29013       ** such bad shape that dropping a lock or two doesn't matter much.
29014       */
29015       robust_close(pNew, h, __LINE__);
29016       h = -1;
29017     }
29018     unixLeaveMutex();
29019   }
29020 
29021 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
29022   else if( pLockingStyle == &afpIoMethods ){
29023     /* AFP locking uses the file path so it needs to be included in
29024     ** the afpLockingContext.
29025     */
29026     afpLockingContext *pCtx;
29027     pNew->lockingContext = pCtx = sqlite3_malloc( sizeof(*pCtx) );
29028     if( pCtx==0 ){
29029       rc = SQLITE_NOMEM;
29030     }else{
29031       /* NB: zFilename exists and remains valid until the file is closed
29032       ** according to requirement F11141.  So we do not need to make a
29033       ** copy of the filename. */
29034       pCtx->dbPath = zFilename;
29035       pCtx->reserved = 0;
29036       srandomdev();
29037       unixEnterMutex();
29038       rc = findInodeInfo(pNew, &pNew->pInode);
29039       if( rc!=SQLITE_OK ){
29040         sqlite3_free(pNew->lockingContext);
29041         robust_close(pNew, h, __LINE__);
29042         h = -1;
29043       }
29044       unixLeaveMutex();
29045     }
29046   }
29047 #endif
29048 
29049   else if( pLockingStyle == &dotlockIoMethods ){
29050     /* Dotfile locking uses the file path so it needs to be included in
29051     ** the dotlockLockingContext
29052     */
29053     char *zLockFile;
29054     int nFilename;
29055     nFilename = (int)strlen(zFilename) + 6;
29056     zLockFile = (char *)sqlite3_malloc(nFilename);
29057     if( zLockFile==0 ){
29058       rc = SQLITE_NOMEM;
29059     }else{
29060       sqlite3_snprintf(nFilename, zLockFile, "%s" DOTLOCK_SUFFIX, zFilename);
29061     }
29062     pNew->lockingContext = zLockFile;
29063   }
29064 
29065 #if OS_VXWORKS
29066   else if( pLockingStyle == &semIoMethods ){
29067     /* Named semaphore locking uses the file path so it needs to be
29068     ** included in the semLockingContext
29069     */
29070     unixEnterMutex();
29071     rc = findInodeInfo(pNew, &pNew->pInode);
29072     if( (rc==SQLITE_OK) && (pNew->pInode->pSem==NULL) ){
29073       char *zSemName = pNew->pInode->aSemName;
29074       int n;
29075       sqlite3_snprintf(MAX_PATHNAME, zSemName, "/%s.sem",
29076                        pNew->pId->zCanonicalName);
29077       for( n=1; zSemName[n]; n++ )
29078         if( zSemName[n]=='/' ) zSemName[n] = '_';
29079       pNew->pInode->pSem = sem_open(zSemName, O_CREAT, 0666, 1);
29080       if( pNew->pInode->pSem == SEM_FAILED ){
29081         rc = SQLITE_NOMEM;
29082         pNew->pInode->aSemName[0] = '\0';
29083       }
29084     }
29085     unixLeaveMutex();
29086   }
29087 #endif
29088 
29089   pNew->lastErrno = 0;
29090 #if OS_VXWORKS
29091   if( rc!=SQLITE_OK ){
29092     if( h>=0 ) robust_close(pNew, h, __LINE__);
29093     h = -1;
29094     unlink(zFilename);
29095     isDelete = 0;
29096   }
29097   pNew->isDelete = isDelete;
29098 #endif
29099   if( rc!=SQLITE_OK ){
29100     if( dirfd>=0 ) robust_close(pNew, dirfd, __LINE__);
29101     if( h>=0 ) robust_close(pNew, h, __LINE__);
29102   }else{
29103     pNew->pMethod = pLockingStyle;
29104     OpenCounter(+1);
29105   }
29106   return rc;
29107 }
29108 
29109 /*
29110 ** Open a file descriptor to the directory containing file zFilename.
29111 ** If successful, *pFd is set to the opened file descriptor and
29112 ** SQLITE_OK is returned. If an error occurs, either SQLITE_NOMEM
29113 ** or SQLITE_CANTOPEN is returned and *pFd is set to an undefined
29114 ** value.
29115 **
29116 ** If SQLITE_OK is returned, the caller is responsible for closing
29117 ** the file descriptor *pFd using close().
29118 */
29119 static int openDirectory(const char *zFilename, int *pFd){
29120   int ii;
29121   int fd = -1;
29122   char zDirname[MAX_PATHNAME+1];
29123 
29124   sqlite3_snprintf(MAX_PATHNAME, zDirname, "%s", zFilename);
29125   for(ii=(int)strlen(zDirname); ii>1 && zDirname[ii]!='/'; ii--);
29126   if( ii>0 ){
29127     zDirname[ii] = '\0';
29128     fd = robust_open(zDirname, O_RDONLY|O_BINARY, 0);
29129     if( fd>=0 ){
29130 #ifdef FD_CLOEXEC
29131       osFcntl(fd, F_SETFD, osFcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
29132 #endif
29133       OSTRACE(("OPENDIR %-3d %s\n", fd, zDirname));
29134     }
29135   }
29136   *pFd = fd;
29137   return (fd>=0?SQLITE_OK:unixLogError(SQLITE_CANTOPEN_BKPT, "open", zDirname));
29138 }
29139 
29140 /*
29141 ** Return the name of a directory in which to put temporary files.
29142 ** If no suitable temporary file directory can be found, return NULL.
29143 */
29144 static const char *unixTempFileDir(void){
29145   static const char *azDirs[] = {
29146      0,
29147      0,
29148      "/var/tmp",
29149      "/usr/tmp",
29150      "/tmp",
29151      0        /* List terminator */
29152   };
29153   unsigned int i;
29154   struct stat buf;
29155   const char *zDir = 0;
29156 
29157   azDirs[0] = sqlite3_temp_directory;
29158   if( !azDirs[1] ) azDirs[1] = getenv("TMPDIR");
29159   for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); zDir=azDirs[i++]){
29160     if( zDir==0 ) continue;
29161     if( osStat(zDir, &buf) ) continue;
29162     if( !S_ISDIR(buf.st_mode) ) continue;
29163     if( osAccess(zDir, 07) ) continue;
29164     break;
29165   }
29166   return zDir;
29167 }
29168 
29169 /*
29170 ** Create a temporary file name in zBuf.  zBuf must be allocated
29171 ** by the calling process and must be big enough to hold at least
29172 ** pVfs->mxPathname bytes.
29173 */
29174 static int unixGetTempname(int nBuf, char *zBuf){
29175   static const unsigned char zChars[] =
29176     "abcdefghijklmnopqrstuvwxyz"
29177     "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
29178     "0123456789";
29179   unsigned int i, j;
29180   const char *zDir;
29181 
29182   /* It's odd to simulate an io-error here, but really this is just
29183   ** using the io-error infrastructure to test that SQLite handles this
29184   ** function failing.
29185   */
29186   SimulateIOError( return SQLITE_IOERR );
29187 
29188   zDir = unixTempFileDir();
29189   if( zDir==0 ) zDir = ".";
29190 
29191   /* Check that the output buffer is large enough for the temporary file
29192   ** name. If it is not, return SQLITE_ERROR.
29193   */
29194   if( (strlen(zDir) + strlen(SQLITE_TEMP_FILE_PREFIX) + 17) >= (size_t)nBuf ){
29195     return SQLITE_ERROR;
29196   }
29197 
29198   do{
29199     sqlite3_snprintf(nBuf-17, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX, zDir);
29200     j = (int)strlen(zBuf);
29201     sqlite3_randomness(15, &zBuf[j]);
29202     for(i=0; i<15; i++, j++){
29203       zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
29204     }
29205     zBuf[j] = 0;
29206   }while( osAccess(zBuf,0)==0 );
29207   return SQLITE_OK;
29208 }
29209 
29210 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
29211 /*
29212 ** Routine to transform a unixFile into a proxy-locking unixFile.
29213 ** Implementation in the proxy-lock division, but used by unixOpen()
29214 ** if SQLITE_PREFER_PROXY_LOCKING is defined.
29215 */
29216 static int proxyTransformUnixFile(unixFile*, const char*);
29217 #endif
29218 
29219 /*
29220 ** Search for an unused file descriptor that was opened on the database
29221 ** file (not a journal or master-journal file) identified by pathname
29222 ** zPath with SQLITE_OPEN_XXX flags matching those passed as the second
29223 ** argument to this function.
29224 **
29225 ** Such a file descriptor may exist if a database connection was closed
29226 ** but the associated file descriptor could not be closed because some
29227 ** other file descriptor open on the same file is holding a file-lock.
29228 ** Refer to comments in the unixClose() function and the lengthy comment
29229 ** describing "Posix Advisory Locking" at the start of this file for
29230 ** further details. Also, ticket #4018.
29231 **
29232 ** If a suitable file descriptor is found, then it is returned. If no
29233 ** such file descriptor is located, -1 is returned.
29234 */
29235 static UnixUnusedFd *findReusableFd(const char *zPath, int flags){
29236   UnixUnusedFd *pUnused = 0;
29237 
29238   /* Do not search for an unused file descriptor on vxworks. Not because
29239   ** vxworks would not benefit from the change (it might, we're not sure),
29240   ** but because no way to test it is currently available. It is better
29241   ** not to risk breaking vxworks support for the sake of such an obscure
29242   ** feature.  */
29243 #if !OS_VXWORKS
29244   struct stat sStat;                   /* Results of stat() call */
29245 
29246   /* A stat() call may fail for various reasons. If this happens, it is
29247   ** almost certain that an open() call on the same path will also fail.
29248   ** For this reason, if an error occurs in the stat() call here, it is
29249   ** ignored and -1 is returned. The caller will try to open a new file
29250   ** descriptor on the same path, fail, and return an error to SQLite.
29251   **
29252   ** Even if a subsequent open() call does succeed, the consequences of
29253   ** not searching for a resusable file descriptor are not dire.  */
29254   if( 0==stat(zPath, &sStat) ){
29255     unixInodeInfo *pInode;
29256 
29257     unixEnterMutex();
29258     pInode = inodeList;
29259     while( pInode && (pInode->fileId.dev!=sStat.st_dev
29260                      || pInode->fileId.ino!=sStat.st_ino) ){
29261        pInode = pInode->pNext;
29262     }
29263     if( pInode ){
29264       UnixUnusedFd **pp;
29265       for(pp=&pInode->pUnused; *pp && (*pp)->flags!=flags; pp=&((*pp)->pNext));
29266       pUnused = *pp;
29267       if( pUnused ){
29268         *pp = pUnused->pNext;
29269       }
29270     }
29271     unixLeaveMutex();
29272   }
29273 #endif    /* if !OS_VXWORKS */
29274   return pUnused;
29275 }
29276 
29277 /*
29278 ** This function is called by unixOpen() to determine the unix permissions
29279 ** to create new files with. If no error occurs, then SQLITE_OK is returned
29280 ** and a value suitable for passing as the third argument to open(2) is
29281 ** written to *pMode. If an IO error occurs, an SQLite error code is
29282 ** returned and the value of *pMode is not modified.
29283 **
29284 ** If the file being opened is a temporary file, it is always created with
29285 ** the octal permissions 0600 (read/writable by owner only). If the file
29286 ** is a database or master journal file, it is created with the permissions
29287 ** mask SQLITE_DEFAULT_FILE_PERMISSIONS.
29288 **
29289 ** Finally, if the file being opened is a WAL or regular journal file, then
29290 ** this function queries the file-system for the permissions on the
29291 ** corresponding database file and sets *pMode to this value. Whenever
29292 ** possible, WAL and journal files are created using the same permissions
29293 ** as the associated database file.
29294 **
29295 ** If the SQLITE_ENABLE_8_3_NAMES option is enabled, then the
29296 ** original filename is unavailable.  But 8_3_NAMES is only used for
29297 ** FAT filesystems and permissions do not matter there, so just use
29298 ** the default permissions.
29299 */
29300 static int findCreateFileMode(
29301   const char *zPath,              /* Path of file (possibly) being created */
29302   int flags,                      /* Flags passed as 4th argument to xOpen() */
29303   mode_t *pMode                   /* OUT: Permissions to open file with */
29304 ){
29305   int rc = SQLITE_OK;             /* Return Code */
29306   *pMode = SQLITE_DEFAULT_FILE_PERMISSIONS;
29307   if( flags & (SQLITE_OPEN_WAL|SQLITE_OPEN_MAIN_JOURNAL) ){
29308     char zDb[MAX_PATHNAME+1];     /* Database file path */
29309     int nDb;                      /* Number of valid bytes in zDb */
29310     struct stat sStat;            /* Output of stat() on database file */
29311 
29312     /* zPath is a path to a WAL or journal file. The following block derives
29313     ** the path to the associated database file from zPath. This block handles
29314     ** the following naming conventions:
29315     **
29316     **   "<path to db>-journal"
29317     **   "<path to db>-wal"
29318     **   "<path to db>-journalNN"
29319     **   "<path to db>-walNN"
29320     **
29321     ** where NN is a 4 digit decimal number. The NN naming schemes are
29322     ** used by the test_multiplex.c module.
29323     */
29324     nDb = sqlite3Strlen30(zPath) - 1;
29325     while( nDb>0 && zPath[nDb]!='-' ) nDb--;
29326     if( nDb==0 ) return SQLITE_OK;
29327     memcpy(zDb, zPath, nDb);
29328     zDb[nDb] = '\0';
29329 
29330     if( 0==stat(zDb, &sStat) ){
29331       *pMode = sStat.st_mode & 0777;
29332     }else{
29333       rc = SQLITE_IOERR_FSTAT;
29334     }
29335   }else if( flags & SQLITE_OPEN_DELETEONCLOSE ){
29336     *pMode = 0600;
29337   }
29338   return rc;
29339 }
29340 
29341 /*
29342 ** Open the file zPath.
29343 **
29344 ** Previously, the SQLite OS layer used three functions in place of this
29345 ** one:
29346 **
29347 **     sqlite3OsOpenReadWrite();
29348 **     sqlite3OsOpenReadOnly();
29349 **     sqlite3OsOpenExclusive();
29350 **
29351 ** These calls correspond to the following combinations of flags:
29352 **
29353 **     ReadWrite() ->     (READWRITE | CREATE)
29354 **     ReadOnly()  ->     (READONLY)
29355 **     OpenExclusive() -> (READWRITE | CREATE | EXCLUSIVE)
29356 **
29357 ** The old OpenExclusive() accepted a boolean argument - "delFlag". If
29358 ** true, the file was configured to be automatically deleted when the
29359 ** file handle closed. To achieve the same effect using this new
29360 ** interface, add the DELETEONCLOSE flag to those specified above for
29361 ** OpenExclusive().
29362 */
29363 static int unixOpen(
29364   sqlite3_vfs *pVfs,           /* The VFS for which this is the xOpen method */
29365   const char *zPath,           /* Pathname of file to be opened */
29366   sqlite3_file *pFile,         /* The file descriptor to be filled in */
29367   int flags,                   /* Input flags to control the opening */
29368   int *pOutFlags               /* Output flags returned to SQLite core */
29369 ){
29370   unixFile *p = (unixFile *)pFile;
29371   int fd = -1;                   /* File descriptor returned by open() */
29372   int dirfd = -1;                /* Directory file descriptor */
29373   int openFlags = 0;             /* Flags to pass to open() */
29374   int eType = flags&0xFFFFFF00;  /* Type of file to open */
29375   int noLock;                    /* True to omit locking primitives */
29376   int rc = SQLITE_OK;            /* Function Return Code */
29377 
29378   int isExclusive  = (flags & SQLITE_OPEN_EXCLUSIVE);
29379   int isDelete     = (flags & SQLITE_OPEN_DELETEONCLOSE);
29380   int isCreate     = (flags & SQLITE_OPEN_CREATE);
29381   int isReadonly   = (flags & SQLITE_OPEN_READONLY);
29382   int isReadWrite  = (flags & SQLITE_OPEN_READWRITE);
29383 #if SQLITE_ENABLE_LOCKING_STYLE
29384   int isAutoProxy  = (flags & SQLITE_OPEN_AUTOPROXY);
29385 #endif
29386 
29387   /* If creating a master or main-file journal, this function will open
29388   ** a file-descriptor on the directory too. The first time unixSync()
29389   ** is called the directory file descriptor will be fsync()ed and close()d.
29390   */
29391   int isOpenDirectory = (isCreate && (
29392         eType==SQLITE_OPEN_MASTER_JOURNAL
29393      || eType==SQLITE_OPEN_MAIN_JOURNAL
29394      || eType==SQLITE_OPEN_WAL
29395   ));
29396 
29397   /* If argument zPath is a NULL pointer, this function is required to open
29398   ** a temporary file. Use this buffer to store the file name in.
29399   */
29400   char zTmpname[MAX_PATHNAME+1];
29401   const char *zName = zPath;
29402 
29403   /* Check the following statements are true:
29404   **
29405   **   (a) Exactly one of the READWRITE and READONLY flags must be set, and
29406   **   (b) if CREATE is set, then READWRITE must also be set, and
29407   **   (c) if EXCLUSIVE is set, then CREATE must also be set.
29408   **   (d) if DELETEONCLOSE is set, then CREATE must also be set.
29409   */
29410   assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly));
29411   assert(isCreate==0 || isReadWrite);
29412   assert(isExclusive==0 || isCreate);
29413   assert(isDelete==0 || isCreate);
29414 
29415   /* The main DB, main journal, WAL file and master journal are never
29416   ** automatically deleted. Nor are they ever temporary files.  */
29417   assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB );
29418   assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL );
29419   assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MASTER_JOURNAL );
29420   assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL );
29421 
29422   /* Assert that the upper layer has set one of the "file-type" flags. */
29423   assert( eType==SQLITE_OPEN_MAIN_DB      || eType==SQLITE_OPEN_TEMP_DB
29424        || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL
29425        || eType==SQLITE_OPEN_SUBJOURNAL   || eType==SQLITE_OPEN_MASTER_JOURNAL
29426        || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL
29427   );
29428 
29429   memset(p, 0, sizeof(unixFile));
29430 
29431   if( eType==SQLITE_OPEN_MAIN_DB ){
29432     UnixUnusedFd *pUnused;
29433     pUnused = findReusableFd(zName, flags);
29434     if( pUnused ){
29435       fd = pUnused->fd;
29436     }else{
29437       pUnused = sqlite3_malloc(sizeof(*pUnused));
29438       if( !pUnused ){
29439         return SQLITE_NOMEM;
29440       }
29441     }
29442     p->pUnused = pUnused;
29443   }else if( !zName ){
29444     /* If zName is NULL, the upper layer is requesting a temp file. */
29445     assert(isDelete && !isOpenDirectory);
29446     rc = unixGetTempname(MAX_PATHNAME+1, zTmpname);
29447     if( rc!=SQLITE_OK ){
29448       return rc;
29449     }
29450     zName = zTmpname;
29451   }
29452 
29453   /* Determine the value of the flags parameter passed to POSIX function
29454   ** open(). These must be calculated even if open() is not called, as
29455   ** they may be stored as part of the file handle and used by the
29456   ** 'conch file' locking functions later on.  */
29457   if( isReadonly )  openFlags |= O_RDONLY;
29458   if( isReadWrite ) openFlags |= O_RDWR;
29459   if( isCreate )    openFlags |= O_CREAT;
29460   if( isExclusive ) openFlags |= (O_EXCL|O_NOFOLLOW);
29461   openFlags |= (O_LARGEFILE|O_BINARY);
29462 
29463   if( fd<0 ){
29464     mode_t openMode;              /* Permissions to create file with */
29465     rc = findCreateFileMode(zName, flags, &openMode);
29466     if( rc!=SQLITE_OK ){
29467       assert( !p->pUnused );
29468       assert( eType==SQLITE_OPEN_WAL || eType==SQLITE_OPEN_MAIN_JOURNAL );
29469       return rc;
29470     }
29471     fd = robust_open(zName, openFlags, openMode);
29472     OSTRACE(("OPENX   %-3d %s 0%o\n", fd, zName, openFlags));
29473     if( fd<0 && errno!=EISDIR && isReadWrite && !isExclusive ){
29474       /* Failed to open the file for read/write access. Try read-only. */
29475       flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
29476       openFlags &= ~(O_RDWR|O_CREAT);
29477       flags |= SQLITE_OPEN_READONLY;
29478       openFlags |= O_RDONLY;
29479       isReadonly = 1;
29480       fd = robust_open(zName, openFlags, openMode);
29481     }
29482     if( fd<0 ){
29483       rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zName);
29484       goto open_finished;
29485     }
29486   }
29487   assert( fd>=0 );
29488   if( pOutFlags ){
29489     *pOutFlags = flags;
29490   }
29491 
29492   if( p->pUnused ){
29493     p->pUnused->fd = fd;
29494     p->pUnused->flags = flags;
29495   }
29496 
29497   if( isDelete ){
29498 #if OS_VXWORKS
29499     zPath = zName;
29500 #else
29501     unlink(zName);
29502 #endif
29503   }
29504 #if SQLITE_ENABLE_LOCKING_STYLE
29505   else{
29506     p->openFlags = openFlags;
29507   }
29508 #endif
29509 
29510   if( isOpenDirectory ){
29511     rc = openDirectory(zPath, &dirfd);
29512     if( rc!=SQLITE_OK ){
29513       /* It is safe to close fd at this point, because it is guaranteed not
29514       ** to be open on a database file. If it were open on a database file,
29515       ** it would not be safe to close as this would release any locks held
29516       ** on the file by this process.  */
29517       assert( eType!=SQLITE_OPEN_MAIN_DB );
29518       robust_close(p, fd, __LINE__);
29519       goto open_finished;
29520     }
29521   }
29522 
29523 #ifdef FD_CLOEXEC
29524   osFcntl(fd, F_SETFD, osFcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
29525 #endif
29526 
29527   noLock = eType!=SQLITE_OPEN_MAIN_DB;
29528 
29529 
29530 #if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE
29531   struct statfs fsInfo;
29532   if( fstatfs(fd, &fsInfo) == -1 ){
29533     ((unixFile*)pFile)->lastErrno = errno;
29534     if( dirfd>=0 ) robust_close(p, dirfd, __LINE__);
29535     robust_close(p, fd, __LINE__);
29536     return SQLITE_IOERR_ACCESS;
29537   }
29538   if (0 == strncmp("msdos", fsInfo.f_fstypename, 5)) {
29539     ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS;
29540   }
29541 #endif
29542 
29543 #if SQLITE_ENABLE_LOCKING_STYLE
29544 #if SQLITE_PREFER_PROXY_LOCKING
29545   isAutoProxy = 1;
29546 #endif
29547   if( isAutoProxy && (zPath!=NULL) && (!noLock) && pVfs->xOpen ){
29548     char *envforce = getenv("SQLITE_FORCE_PROXY_LOCKING");
29549     int useProxy = 0;
29550 
29551     /* SQLITE_FORCE_PROXY_LOCKING==1 means force always use proxy, 0 means
29552     ** never use proxy, NULL means use proxy for non-local files only.  */
29553     if( envforce!=NULL ){
29554       useProxy = atoi(envforce)>0;
29555     }else{
29556       struct statfs fsInfo;
29557       if( statfs(zPath, &fsInfo) == -1 ){
29558         /* In theory, the close(fd) call is sub-optimal. If the file opened
29559         ** with fd is a database file, and there are other connections open
29560         ** on that file that are currently holding advisory locks on it,
29561         ** then the call to close() will cancel those locks. In practice,
29562         ** we're assuming that statfs() doesn't fail very often. At least
29563         ** not while other file descriptors opened by the same process on
29564         ** the same file are working.  */
29565         p->lastErrno = errno;
29566         if( dirfd>=0 ){
29567           robust_close(p, dirfd, __LINE__);
29568         }
29569         robust_close(p, fd, __LINE__);
29570         rc = SQLITE_IOERR_ACCESS;
29571         goto open_finished;
29572       }
29573       useProxy = !(fsInfo.f_flags&MNT_LOCAL);
29574     }
29575     if( useProxy ){
29576       rc = fillInUnixFile(pVfs, fd, dirfd, pFile, zPath, noLock,
29577                           isDelete, isReadonly);
29578       if( rc==SQLITE_OK ){
29579         rc = proxyTransformUnixFile((unixFile*)pFile, ":auto:");
29580         if( rc!=SQLITE_OK ){
29581           /* Use unixClose to clean up the resources added in fillInUnixFile
29582           ** and clear all the structure's references.  Specifically,
29583           ** pFile->pMethods will be NULL so sqlite3OsClose will be a no-op
29584           */
29585           unixClose(pFile);
29586           return rc;
29587         }
29588       }
29589       goto open_finished;
29590     }
29591   }
29592 #endif
29593 
29594   rc = fillInUnixFile(pVfs, fd, dirfd, pFile, zPath, noLock,
29595                       isDelete, isReadonly);
29596 open_finished:
29597   if( rc!=SQLITE_OK ){
29598     sqlite3_free(p->pUnused);
29599   }
29600   return rc;
29601 }
29602 
29603 
29604 /*
29605 ** Delete the file at zPath. If the dirSync argument is true, fsync()
29606 ** the directory after deleting the file.
29607 */
29608 static int unixDelete(
29609   sqlite3_vfs *NotUsed,     /* VFS containing this as the xDelete method */
29610   const char *zPath,        /* Name of file to be deleted */
29611   int dirSync               /* If true, fsync() directory after deleting file */
29612 ){
29613   int rc = SQLITE_OK;
29614   UNUSED_PARAMETER(NotUsed);
29615   SimulateIOError(return SQLITE_IOERR_DELETE);
29616   if( unlink(zPath)==(-1) && errno!=ENOENT ){
29617     return unixLogError(SQLITE_IOERR_DELETE, "unlink", zPath);
29618   }
29619 #ifndef SQLITE_DISABLE_DIRSYNC
29620   if( dirSync ){
29621     int fd;
29622     rc = openDirectory(zPath, &fd);
29623     if( rc==SQLITE_OK ){
29624 #if OS_VXWORKS
29625       if( fsync(fd)==-1 )
29626 #else
29627       if( fsync(fd) )
29628 #endif
29629       {
29630         rc = unixLogError(SQLITE_IOERR_DIR_FSYNC, "fsync", zPath);
29631       }
29632       robust_close(0, fd, __LINE__);
29633     }
29634   }
29635 #endif
29636   return rc;
29637 }
29638 
29639 /*
29640 ** Test the existance of or access permissions of file zPath. The
29641 ** test performed depends on the value of flags:
29642 **
29643 **     SQLITE_ACCESS_EXISTS: Return 1 if the file exists
29644 **     SQLITE_ACCESS_READWRITE: Return 1 if the file is read and writable.
29645 **     SQLITE_ACCESS_READONLY: Return 1 if the file is readable.
29646 **
29647 ** Otherwise return 0.
29648 */
29649 static int unixAccess(
29650   sqlite3_vfs *NotUsed,   /* The VFS containing this xAccess method */
29651   const char *zPath,      /* Path of the file to examine */
29652   int flags,              /* What do we want to learn about the zPath file? */
29653   int *pResOut            /* Write result boolean here */
29654 ){
29655   int amode = 0;
29656   UNUSED_PARAMETER(NotUsed);
29657   SimulateIOError( return SQLITE_IOERR_ACCESS; );
29658   switch( flags ){
29659     case SQLITE_ACCESS_EXISTS:
29660       amode = F_OK;
29661       break;
29662     case SQLITE_ACCESS_READWRITE:
29663       amode = W_OK|R_OK;
29664       break;
29665     case SQLITE_ACCESS_READ:
29666       amode = R_OK;
29667       break;
29668 
29669     default:
29670       assert(!"Invalid flags argument");
29671   }
29672   *pResOut = (osAccess(zPath, amode)==0);
29673   if( flags==SQLITE_ACCESS_EXISTS && *pResOut ){
29674     struct stat buf;
29675     if( 0==stat(zPath, &buf) && buf.st_size==0 ){
29676       *pResOut = 0;
29677     }
29678   }
29679   return SQLITE_OK;
29680 }
29681 
29682 
29683 /*
29684 ** Turn a relative pathname into a full pathname. The relative path
29685 ** is stored as a nul-terminated string in the buffer pointed to by
29686 ** zPath.
29687 **
29688 ** zOut points to a buffer of at least sqlite3_vfs.mxPathname bytes
29689 ** (in this case, MAX_PATHNAME bytes). The full-path is written to
29690 ** this buffer before returning.
29691 */
29692 static int unixFullPathname(
29693   sqlite3_vfs *pVfs,            /* Pointer to vfs object */
29694   const char *zPath,            /* Possibly relative input path */
29695   int nOut,                     /* Size of output buffer in bytes */
29696   char *zOut                    /* Output buffer */
29697 ){
29698 
29699   /* It's odd to simulate an io-error here, but really this is just
29700   ** using the io-error infrastructure to test that SQLite handles this
29701   ** function failing. This function could fail if, for example, the
29702   ** current working directory has been unlinked.
29703   */
29704   SimulateIOError( return SQLITE_ERROR );
29705 
29706   assert( pVfs->mxPathname==MAX_PATHNAME );
29707   UNUSED_PARAMETER(pVfs);
29708 
29709   zOut[nOut-1] = '\0';
29710   if( zPath[0]=='/' ){
29711     sqlite3_snprintf(nOut, zOut, "%s", zPath);
29712   }else{
29713     int nCwd;
29714     if( osGetcwd(zOut, nOut-1)==0 ){
29715       return unixLogError(SQLITE_CANTOPEN_BKPT, "getcwd", zPath);
29716     }
29717     nCwd = (int)strlen(zOut);
29718     sqlite3_snprintf(nOut-nCwd, &zOut[nCwd], "/%s", zPath);
29719   }
29720   return SQLITE_OK;
29721 }
29722 
29723 
29724 #ifndef SQLITE_OMIT_LOAD_EXTENSION
29725 /*
29726 ** Interfaces for opening a shared library, finding entry points
29727 ** within the shared library, and closing the shared library.
29728 */
29729 #include <dlfcn.h>
29730 static void *unixDlOpen(sqlite3_vfs *NotUsed, const char *zFilename){
29731   UNUSED_PARAMETER(NotUsed);
29732   return dlopen(zFilename, RTLD_NOW | RTLD_GLOBAL);
29733 }
29734 
29735 /*
29736 ** SQLite calls this function immediately after a call to unixDlSym() or
29737 ** unixDlOpen() fails (returns a null pointer). If a more detailed error
29738 ** message is available, it is written to zBufOut. If no error message
29739 ** is available, zBufOut is left unmodified and SQLite uses a default
29740 ** error message.
29741 */
29742 static void unixDlError(sqlite3_vfs *NotUsed, int nBuf, char *zBufOut){
29743   const char *zErr;
29744   UNUSED_PARAMETER(NotUsed);
29745   unixEnterMutex();
29746   zErr = dlerror();
29747   if( zErr ){
29748     sqlite3_snprintf(nBuf, zBufOut, "%s", zErr);
29749   }
29750   unixLeaveMutex();
29751 }
29752 static void (*unixDlSym(sqlite3_vfs *NotUsed, void *p, const char*zSym))(void){
29753   /*
29754   ** GCC with -pedantic-errors says that C90 does not allow a void* to be
29755   ** cast into a pointer to a function.  And yet the library dlsym() routine
29756   ** returns a void* which is really a pointer to a function.  So how do we
29757   ** use dlsym() with -pedantic-errors?
29758   **
29759   ** Variable x below is defined to be a pointer to a function taking
29760   ** parameters void* and const char* and returning a pointer to a function.
29761   ** We initialize x by assigning it a pointer to the dlsym() function.
29762   ** (That assignment requires a cast.)  Then we call the function that
29763   ** x points to.
29764   **
29765   ** This work-around is unlikely to work correctly on any system where
29766   ** you really cannot cast a function pointer into void*.  But then, on the
29767   ** other hand, dlsym() will not work on such a system either, so we have
29768   ** not really lost anything.
29769   */
29770   void (*(*x)(void*,const char*))(void);
29771   UNUSED_PARAMETER(NotUsed);
29772   x = (void(*(*)(void*,const char*))(void))dlsym;
29773   return (*x)(p, zSym);
29774 }
29775 static void unixDlClose(sqlite3_vfs *NotUsed, void *pHandle){
29776   UNUSED_PARAMETER(NotUsed);
29777   dlclose(pHandle);
29778 }
29779 #else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
29780   #define unixDlOpen  0
29781   #define unixDlError 0
29782   #define unixDlSym   0
29783   #define unixDlClose 0
29784 #endif
29785 
29786 /*
29787 ** Write nBuf bytes of random data to the supplied buffer zBuf.
29788 */
29789 static int unixRandomness(sqlite3_vfs *NotUsed, int nBuf, char *zBuf){
29790   UNUSED_PARAMETER(NotUsed);
29791   assert((size_t)nBuf>=(sizeof(time_t)+sizeof(int)));
29792 
29793   /* We have to initialize zBuf to prevent valgrind from reporting
29794   ** errors.  The reports issued by valgrind are incorrect - we would
29795   ** prefer that the randomness be increased by making use of the
29796   ** uninitialized space in zBuf - but valgrind errors tend to worry
29797   ** some users.  Rather than argue, it seems easier just to initialize
29798   ** the whole array and silence valgrind, even if that means less randomness
29799   ** in the random seed.
29800   **
29801   ** When testing, initializing zBuf[] to zero is all we do.  That means
29802   ** that we always use the same random number sequence.  This makes the
29803   ** tests repeatable.
29804   */
29805   memset(zBuf, 0, nBuf);
29806 #if !defined(SQLITE_TEST)
29807   {
29808     int pid, fd;
29809     fd = robust_open("/dev/urandom", O_RDONLY, 0);
29810     if( fd<0 ){
29811       time_t t;
29812       time(&t);
29813       memcpy(zBuf, &t, sizeof(t));
29814       pid = getpid();
29815       memcpy(&zBuf[sizeof(t)], &pid, sizeof(pid));
29816       assert( sizeof(t)+sizeof(pid)<=(size_t)nBuf );
29817       nBuf = sizeof(t) + sizeof(pid);
29818     }else{
29819       do{ nBuf = osRead(fd, zBuf, nBuf); }while( nBuf<0 && errno==EINTR );
29820       robust_close(0, fd, __LINE__);
29821     }
29822   }
29823 #endif
29824   return nBuf;
29825 }
29826 
29827 
29828 /*
29829 ** Sleep for a little while.  Return the amount of time slept.
29830 ** The argument is the number of microseconds we want to sleep.
29831 ** The return value is the number of microseconds of sleep actually
29832 ** requested from the underlying operating system, a number which
29833 ** might be greater than or equal to the argument, but not less
29834 ** than the argument.
29835 */
29836 static int unixSleep(sqlite3_vfs *NotUsed, int microseconds){
29837 #if OS_VXWORKS
29838   struct timespec sp;
29839 
29840   sp.tv_sec = microseconds / 1000000;
29841   sp.tv_nsec = (microseconds % 1000000) * 1000;
29842   nanosleep(&sp, NULL);
29843   UNUSED_PARAMETER(NotUsed);
29844   return microseconds;
29845 #elif defined(HAVE_USLEEP) && HAVE_USLEEP
29846   usleep(microseconds);
29847   UNUSED_PARAMETER(NotUsed);
29848   return microseconds;
29849 #else
29850   int seconds = (microseconds+999999)/1000000;
29851   sleep(seconds);
29852   UNUSED_PARAMETER(NotUsed);
29853   return seconds*1000000;
29854 #endif
29855 }
29856 
29857 /*
29858 ** The following variable, if set to a non-zero value, is interpreted as
29859 ** the number of seconds since 1970 and is used to set the result of
29860 ** sqlite3OsCurrentTime() during testing.
29861 */
29862 #ifdef SQLITE_TEST
29863 SQLITE_API int sqlite3_current_time = 0;  /* Fake system time in seconds since 1970. */
29864 #endif
29865 
29866 /*
29867 ** Find the current time (in Universal Coordinated Time).  Write into *piNow
29868 ** the current time and date as a Julian Day number times 86_400_000.  In
29869 ** other words, write into *piNow the number of milliseconds since the Julian
29870 ** epoch of noon in Greenwich on November 24, 4714 B.C according to the
29871 ** proleptic Gregorian calendar.
29872 **
29873 ** On success, return 0.  Return 1 if the time and date cannot be found.
29874 */
29875 static int unixCurrentTimeInt64(sqlite3_vfs *NotUsed, sqlite3_int64 *piNow){
29876   static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000;
29877 #if defined(NO_GETTOD)
29878   time_t t;
29879   time(&t);
29880   *piNow = ((sqlite3_int64)t)*1000 + unixEpoch;
29881 #elif OS_VXWORKS
29882   struct timespec sNow;
29883   clock_gettime(CLOCK_REALTIME, &sNow);
29884   *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_nsec/1000000;
29885 #else
29886   struct timeval sNow;
29887   gettimeofday(&sNow, 0);
29888   *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_usec/1000;
29889 #endif
29890 
29891 #ifdef SQLITE_TEST
29892   if( sqlite3_current_time ){
29893     *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch;
29894   }
29895 #endif
29896   UNUSED_PARAMETER(NotUsed);
29897   return 0;
29898 }
29899 
29900 /*
29901 ** Find the current time (in Universal Coordinated Time).  Write the
29902 ** current time and date as a Julian Day number into *prNow and
29903 ** return 0.  Return 1 if the time and date cannot be found.
29904 */
29905 static int unixCurrentTime(sqlite3_vfs *NotUsed, double *prNow){
29906   sqlite3_int64 i;
29907   UNUSED_PARAMETER(NotUsed);
29908   unixCurrentTimeInt64(0, &i);
29909   *prNow = i/86400000.0;
29910   return 0;
29911 }
29912 
29913 /*
29914 ** We added the xGetLastError() method with the intention of providing
29915 ** better low-level error messages when operating-system problems come up
29916 ** during SQLite operation.  But so far, none of that has been implemented
29917 ** in the core.  So this routine is never called.  For now, it is merely
29918 ** a place-holder.
29919 */
29920 static int unixGetLastError(sqlite3_vfs *NotUsed, int NotUsed2, char *NotUsed3){
29921   UNUSED_PARAMETER(NotUsed);
29922   UNUSED_PARAMETER(NotUsed2);
29923   UNUSED_PARAMETER(NotUsed3);
29924   return 0;
29925 }
29926 
29927 
29928 /*
29929 ************************ End of sqlite3_vfs methods ***************************
29930 ******************************************************************************/
29931 
29932 /******************************************************************************
29933 ************************** Begin Proxy Locking ********************************
29934 **
29935 ** Proxy locking is a "uber-locking-method" in this sense:  It uses the
29936 ** other locking methods on secondary lock files.  Proxy locking is a
29937 ** meta-layer over top of the primitive locking implemented above.  For
29938 ** this reason, the division that implements of proxy locking is deferred
29939 ** until late in the file (here) after all of the other I/O methods have
29940 ** been defined - so that the primitive locking methods are available
29941 ** as services to help with the implementation of proxy locking.
29942 **
29943 ****
29944 **
29945 ** The default locking schemes in SQLite use byte-range locks on the
29946 ** database file to coordinate safe, concurrent access by multiple readers
29947 ** and writers [http://sqlite.org/lockingv3.html].  The five file locking
29948 ** states (UNLOCKED, PENDING, SHARED, RESERVED, EXCLUSIVE) are implemented
29949 ** as POSIX read & write locks over fixed set of locations (via fsctl),
29950 ** on AFP and SMB only exclusive byte-range locks are available via fsctl
29951 ** with _IOWR('z', 23, struct ByteRangeLockPB2) to track the same 5 states.
29952 ** To simulate a F_RDLCK on the shared range, on AFP a randomly selected
29953 ** address in the shared range is taken for a SHARED lock, the entire
29954 ** shared range is taken for an EXCLUSIVE lock):
29955 **
29956 **      PENDING_BYTE        0x40000000
29957 **      RESERVED_BYTE       0x40000001
29958 **      SHARED_RANGE        0x40000002 -> 0x40000200
29959 **
29960 ** This works well on the local file system, but shows a nearly 100x
29961 ** slowdown in read performance on AFP because the AFP client disables
29962 ** the read cache when byte-range locks are present.  Enabling the read
29963 ** cache exposes a cache coherency problem that is present on all OS X
29964 ** supported network file systems.  NFS and AFP both observe the
29965 ** close-to-open semantics for ensuring cache coherency
29966 ** [http://nfs.sourceforge.net/#faq_a8], which does not effectively
29967 ** address the requirements for concurrent database access by multiple
29968 ** readers and writers
29969 ** [http://www.nabble.com/SQLite-on-NFS-cache-coherency-td15655701.html].
29970 **
29971 ** To address the performance and cache coherency issues, proxy file locking
29972 ** changes the way database access is controlled by limiting access to a
29973 ** single host at a time and moving file locks off of the database file
29974 ** and onto a proxy file on the local file system.
29975 **
29976 **
29977 ** Using proxy locks
29978 ** -----------------
29979 **
29980 ** C APIs
29981 **
29982 **  sqlite3_file_control(db, dbname, SQLITE_SET_LOCKPROXYFILE,
29983 **                       <proxy_path> | ":auto:");
29984 **  sqlite3_file_control(db, dbname, SQLITE_GET_LOCKPROXYFILE, &<proxy_path>);
29985 **
29986 **
29987 ** SQL pragmas
29988 **
29989 **  PRAGMA [database.]lock_proxy_file=<proxy_path> | :auto:
29990 **  PRAGMA [database.]lock_proxy_file
29991 **
29992 ** Specifying ":auto:" means that if there is a conch file with a matching
29993 ** host ID in it, the proxy path in the conch file will be used, otherwise
29994 ** a proxy path based on the user's temp dir
29995 ** (via confstr(_CS_DARWIN_USER_TEMP_DIR,...)) will be used and the
29996 ** actual proxy file name is generated from the name and path of the
29997 ** database file.  For example:
29998 **
29999 **       For database path "/Users/me/foo.db"
30000 **       The lock path will be "<tmpdir>/sqliteplocks/_Users_me_foo.db:auto:")
30001 **
30002 ** Once a lock proxy is configured for a database connection, it can not
30003 ** be removed, however it may be switched to a different proxy path via
30004 ** the above APIs (assuming the conch file is not being held by another
30005 ** connection or process).
30006 **
30007 **
30008 ** How proxy locking works
30009 ** -----------------------
30010 **
30011 ** Proxy file locking relies primarily on two new supporting files:
30012 **
30013 **   *  conch file to limit access to the database file to a single host
30014 **      at a time
30015 **
30016 **   *  proxy file to act as a proxy for the advisory locks normally
30017 **      taken on the database
30018 **
30019 ** The conch file - to use a proxy file, sqlite must first "hold the conch"
30020 ** by taking an sqlite-style shared lock on the conch file, reading the
30021 ** contents and comparing the host's unique host ID (see below) and lock
30022 ** proxy path against the values stored in the conch.  The conch file is
30023 ** stored in the same directory as the database file and the file name
30024 ** is patterned after the database file name as ".<databasename>-conch".
30025 ** If the conch file does not exist, or it's contents do not match the
30026 ** host ID and/or proxy path, then the lock is escalated to an exclusive
30027 ** lock and the conch file contents is updated with the host ID and proxy
30028 ** path and the lock is downgraded to a shared lock again.  If the conch
30029 ** is held by another process (with a shared lock), the exclusive lock
30030 ** will fail and SQLITE_BUSY is returned.
30031 **
30032 ** The proxy file - a single-byte file used for all advisory file locks
30033 ** normally taken on the database file.   This allows for safe sharing
30034 ** of the database file for multiple readers and writers on the same
30035 ** host (the conch ensures that they all use the same local lock file).
30036 **
30037 ** Requesting the lock proxy does not immediately take the conch, it is
30038 ** only taken when the first request to lock database file is made.
30039 ** This matches the semantics of the traditional locking behavior, where
30040 ** opening a connection to a database file does not take a lock on it.
30041 ** The shared lock and an open file descriptor are maintained until
30042 ** the connection to the database is closed.
30043 **
30044 ** The proxy file and the lock file are never deleted so they only need
30045 ** to be created the first time they are used.
30046 **
30047 ** Configuration options
30048 ** ---------------------
30049 **
30050 **  SQLITE_PREFER_PROXY_LOCKING
30051 **
30052 **       Database files accessed on non-local file systems are
30053 **       automatically configured for proxy locking, lock files are
30054 **       named automatically using the same logic as
30055 **       PRAGMA lock_proxy_file=":auto:"
30056 **
30057 **  SQLITE_PROXY_DEBUG
30058 **
30059 **       Enables the logging of error messages during host id file
30060 **       retrieval and creation
30061 **
30062 **  LOCKPROXYDIR
30063 **
30064 **       Overrides the default directory used for lock proxy files that
30065 **       are named automatically via the ":auto:" setting
30066 **
30067 **  SQLITE_DEFAULT_PROXYDIR_PERMISSIONS
30068 **
30069 **       Permissions to use when creating a directory for storing the
30070 **       lock proxy files, only used when LOCKPROXYDIR is not set.
30071 **
30072 **
30073 ** As mentioned above, when compiled with SQLITE_PREFER_PROXY_LOCKING,
30074 ** setting the environment variable SQLITE_FORCE_PROXY_LOCKING to 1 will
30075 ** force proxy locking to be used for every database file opened, and 0
30076 ** will force automatic proxy locking to be disabled for all database
30077 ** files (explicity calling the SQLITE_SET_LOCKPROXYFILE pragma or
30078 ** sqlite_file_control API is not affected by SQLITE_FORCE_PROXY_LOCKING).
30079 */
30080 
30081 /*
30082 ** Proxy locking is only available on MacOSX
30083 */
30084 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
30085 
30086 /*
30087 ** The proxyLockingContext has the path and file structures for the remote
30088 ** and local proxy files in it
30089 */
30090 typedef struct proxyLockingContext proxyLockingContext;
30091 struct proxyLockingContext {
30092   unixFile *conchFile;         /* Open conch file */
30093   char *conchFilePath;         /* Name of the conch file */
30094   unixFile *lockProxy;         /* Open proxy lock file */
30095   char *lockProxyPath;         /* Name of the proxy lock file */
30096   char *dbPath;                /* Name of the open file */
30097   int conchHeld;               /* 1 if the conch is held, -1 if lockless */
30098   void *oldLockingContext;     /* Original lockingcontext to restore on close */
30099   sqlite3_io_methods const *pOldMethod;     /* Original I/O methods for close */
30100 };
30101 
30102 /*
30103 ** The proxy lock file path for the database at dbPath is written into lPath,
30104 ** which must point to valid, writable memory large enough for a maxLen length
30105 ** file path.
30106 */
30107 static int proxyGetLockPath(const char *dbPath, char *lPath, size_t maxLen){
30108   int len;
30109   int dbLen;
30110   int i;
30111 
30112 #ifdef LOCKPROXYDIR
30113   len = strlcpy(lPath, LOCKPROXYDIR, maxLen);
30114 #else
30115 # ifdef _CS_DARWIN_USER_TEMP_DIR
30116   {
30117     if( !confstr(_CS_DARWIN_USER_TEMP_DIR, lPath, maxLen) ){
30118       OSTRACE(("GETLOCKPATH  failed %s errno=%d pid=%d\n",
30119                lPath, errno, getpid()));
30120       return SQLITE_IOERR_LOCK;
30121     }
30122     len = strlcat(lPath, "sqliteplocks", maxLen);
30123   }
30124 # else
30125   len = strlcpy(lPath, "/tmp/", maxLen);
30126 # endif
30127 #endif
30128 
30129   if( lPath[len-1]!='/' ){
30130     len = strlcat(lPath, "/", maxLen);
30131   }
30132 
30133   /* transform the db path to a unique cache name */
30134   dbLen = (int)strlen(dbPath);
30135   for( i=0; i<dbLen && (i+len+7)<(int)maxLen; i++){
30136     char c = dbPath[i];
30137     lPath[i+len] = (c=='/')?'_':c;
30138   }
30139   lPath[i+len]='\0';
30140   strlcat(lPath, ":auto:", maxLen);
30141   OSTRACE(("GETLOCKPATH  proxy lock path=%s pid=%d\n", lPath, getpid()));
30142   return SQLITE_OK;
30143 }
30144 
30145 /*
30146  ** Creates the lock file and any missing directories in lockPath
30147  */
30148 static int proxyCreateLockPath(const char *lockPath){
30149   int i, len;
30150   char buf[MAXPATHLEN];
30151   int start = 0;
30152 
30153   assert(lockPath!=NULL);
30154   /* try to create all the intermediate directories */
30155   len = (int)strlen(lockPath);
30156   buf[0] = lockPath[0];
30157   for( i=1; i<len; i++ ){
30158     if( lockPath[i] == '/' && (i - start > 0) ){
30159       /* only mkdir if leaf dir != "." or "/" or ".." */
30160       if( i-start>2 || (i-start==1 && buf[start] != '.' && buf[start] != '/')
30161          || (i-start==2 && buf[start] != '.' && buf[start+1] != '.') ){
30162         buf[i]='\0';
30163         if( mkdir(buf, SQLITE_DEFAULT_PROXYDIR_PERMISSIONS) ){
30164           int err=errno;
30165           if( err!=EEXIST ) {
30166             OSTRACE(("CREATELOCKPATH  FAILED creating %s, "
30167                      "'%s' proxy lock path=%s pid=%d\n",
30168                      buf, strerror(err), lockPath, getpid()));
30169             return err;
30170           }
30171         }
30172       }
30173       start=i+1;
30174     }
30175     buf[i] = lockPath[i];
30176   }
30177   OSTRACE(("CREATELOCKPATH  proxy lock path=%s pid=%d\n", lockPath, getpid()));
30178   return 0;
30179 }
30180 
30181 /*
30182 ** Create a new VFS file descriptor (stored in memory obtained from
30183 ** sqlite3_malloc) and open the file named "path" in the file descriptor.
30184 **
30185 ** The caller is responsible not only for closing the file descriptor
30186 ** but also for freeing the memory associated with the file descriptor.
30187 */
30188 static int proxyCreateUnixFile(
30189     const char *path,        /* path for the new unixFile */
30190     unixFile **ppFile,       /* unixFile created and returned by ref */
30191     int islockfile           /* if non zero missing dirs will be created */
30192 ) {
30193   int fd = -1;
30194   int dirfd = -1;
30195   unixFile *pNew;
30196   int rc = SQLITE_OK;
30197   int openFlags = O_RDWR | O_CREAT;
30198   sqlite3_vfs dummyVfs;
30199   int terrno = 0;
30200   UnixUnusedFd *pUnused = NULL;
30201 
30202   /* 1. first try to open/create the file
30203   ** 2. if that fails, and this is a lock file (not-conch), try creating
30204   ** the parent directories and then try again.
30205   ** 3. if that fails, try to open the file read-only
30206   ** otherwise return BUSY (if lock file) or CANTOPEN for the conch file
30207   */
30208   pUnused = findReusableFd(path, openFlags);
30209   if( pUnused ){
30210     fd = pUnused->fd;
30211   }else{
30212     pUnused = sqlite3_malloc(sizeof(*pUnused));
30213     if( !pUnused ){
30214       return SQLITE_NOMEM;
30215     }
30216   }
30217   if( fd<0 ){
30218     fd = robust_open(path, openFlags, SQLITE_DEFAULT_FILE_PERMISSIONS);
30219     terrno = errno;
30220     if( fd<0 && errno==ENOENT && islockfile ){
30221       if( proxyCreateLockPath(path) == SQLITE_OK ){
30222         fd = robust_open(path, openFlags, SQLITE_DEFAULT_FILE_PERMISSIONS);
30223       }
30224     }
30225   }
30226   if( fd<0 ){
30227     openFlags = O_RDONLY;
30228     fd = robust_open(path, openFlags, SQLITE_DEFAULT_FILE_PERMISSIONS);
30229     terrno = errno;
30230   }
30231   if( fd<0 ){
30232     if( islockfile ){
30233       return SQLITE_BUSY;
30234     }
30235     switch (terrno) {
30236       case EACCES:
30237         return SQLITE_PERM;
30238       case EIO:
30239         return SQLITE_IOERR_LOCK; /* even though it is the conch */
30240       default:
30241         return SQLITE_CANTOPEN_BKPT;
30242     }
30243   }
30244 
30245   pNew = (unixFile *)sqlite3_malloc(sizeof(*pNew));
30246   if( pNew==NULL ){
30247     rc = SQLITE_NOMEM;
30248     goto end_create_proxy;
30249   }
30250   memset(pNew, 0, sizeof(unixFile));
30251   pNew->openFlags = openFlags;
30252   memset(&dummyVfs, 0, sizeof(dummyVfs));
30253   dummyVfs.pAppData = (void*)&autolockIoFinder;
30254   dummyVfs.zName = "dummy";
30255   pUnused->fd = fd;
30256   pUnused->flags = openFlags;
30257   pNew->pUnused = pUnused;
30258 
30259   rc = fillInUnixFile(&dummyVfs, fd, dirfd, (sqlite3_file*)pNew, path, 0, 0, 0);
30260   if( rc==SQLITE_OK ){
30261     *ppFile = pNew;
30262     return SQLITE_OK;
30263   }
30264 end_create_proxy:
30265   robust_close(pNew, fd, __LINE__);
30266   sqlite3_free(pNew);
30267   sqlite3_free(pUnused);
30268   return rc;
30269 }
30270 
30271 #ifdef SQLITE_TEST
30272 /* simulate multiple hosts by creating unique hostid file paths */
30273 SQLITE_API int sqlite3_hostid_num = 0;
30274 #endif
30275 
30276 #define PROXY_HOSTIDLEN    16  /* conch file host id length */
30277 
30278 /* Not always defined in the headers as it ought to be */
30279 extern int gethostuuid(uuid_t id, const struct timespec *wait);
30280 
30281 /* get the host ID via gethostuuid(), pHostID must point to PROXY_HOSTIDLEN
30282 ** bytes of writable memory.
30283 */
30284 static int proxyGetHostID(unsigned char *pHostID, int *pError){
30285   assert(PROXY_HOSTIDLEN == sizeof(uuid_t));
30286   memset(pHostID, 0, PROXY_HOSTIDLEN);
30287 #if defined(__MAX_OS_X_VERSION_MIN_REQUIRED)\
30288                && __MAC_OS_X_VERSION_MIN_REQUIRED<1050
30289   {
30290     static const struct timespec timeout = {1, 0}; /* 1 sec timeout */
30291     if( gethostuuid(pHostID, &timeout) ){
30292       int err = errno;
30293       if( pError ){
30294         *pError = err;
30295       }
30296       return SQLITE_IOERR;
30297     }
30298   }
30299 #endif
30300 #ifdef SQLITE_TEST
30301   /* simulate multiple hosts by creating unique hostid file paths */
30302   if( sqlite3_hostid_num != 0){
30303     pHostID[0] = (char)(pHostID[0] + (char)(sqlite3_hostid_num & 0xFF));
30304   }
30305 #endif
30306 
30307   return SQLITE_OK;
30308 }
30309 
30310 /* The conch file contains the header, host id and lock file path
30311  */
30312 #define PROXY_CONCHVERSION 2   /* 1-byte header, 16-byte host id, path */
30313 #define PROXY_HEADERLEN    1   /* conch file header length */
30314 #define PROXY_PATHINDEX    (PROXY_HEADERLEN+PROXY_HOSTIDLEN)
30315 #define PROXY_MAXCONCHLEN  (PROXY_HEADERLEN+PROXY_HOSTIDLEN+MAXPATHLEN)
30316 
30317 /*
30318 ** Takes an open conch file, copies the contents to a new path and then moves
30319 ** it back.  The newly created file's file descriptor is assigned to the
30320 ** conch file structure and finally the original conch file descriptor is
30321 ** closed.  Returns zero if successful.
30322 */
30323 static int proxyBreakConchLock(unixFile *pFile, uuid_t myHostID){
30324   proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
30325   unixFile *conchFile = pCtx->conchFile;
30326   char tPath[MAXPATHLEN];
30327   char buf[PROXY_MAXCONCHLEN];
30328   char *cPath = pCtx->conchFilePath;
30329   size_t readLen = 0;
30330   size_t pathLen = 0;
30331   char errmsg[64] = "";
30332   int fd = -1;
30333   int rc = -1;
30334   UNUSED_PARAMETER(myHostID);
30335 
30336   /* create a new path by replace the trailing '-conch' with '-break' */
30337   pathLen = strlcpy(tPath, cPath, MAXPATHLEN);
30338   if( pathLen>MAXPATHLEN || pathLen<6 ||
30339      (strlcpy(&tPath[pathLen-5], "break", 6) != 5) ){
30340     sqlite3_snprintf(sizeof(errmsg),errmsg,"path error (len %d)",(int)pathLen);
30341     goto end_breaklock;
30342   }
30343   /* read the conch content */
30344   readLen = osPread(conchFile->h, buf, PROXY_MAXCONCHLEN, 0);
30345   if( readLen<PROXY_PATHINDEX ){
30346     sqlite3_snprintf(sizeof(errmsg),errmsg,"read error (len %d)",(int)readLen);
30347     goto end_breaklock;
30348   }
30349   /* write it out to the temporary break file */
30350   fd = robust_open(tPath, (O_RDWR|O_CREAT|O_EXCL),
30351                    SQLITE_DEFAULT_FILE_PERMISSIONS);
30352   if( fd<0 ){
30353     sqlite3_snprintf(sizeof(errmsg), errmsg, "create failed (%d)", errno);
30354     goto end_breaklock;
30355   }
30356   if( osPwrite(fd, buf, readLen, 0) != (ssize_t)readLen ){
30357     sqlite3_snprintf(sizeof(errmsg), errmsg, "write failed (%d)", errno);
30358     goto end_breaklock;
30359   }
30360   if( rename(tPath, cPath) ){
30361     sqlite3_snprintf(sizeof(errmsg), errmsg, "rename failed (%d)", errno);
30362     goto end_breaklock;
30363   }
30364   rc = 0;
30365   fprintf(stderr, "broke stale lock on %s\n", cPath);
30366   robust_close(pFile, conchFile->h, __LINE__);
30367   conchFile->h = fd;
30368   conchFile->openFlags = O_RDWR | O_CREAT;
30369 
30370 end_breaklock:
30371   if( rc ){
30372     if( fd>=0 ){
30373       unlink(tPath);
30374       robust_close(pFile, fd, __LINE__);
30375     }
30376     fprintf(stderr, "failed to break stale lock on %s, %s\n", cPath, errmsg);
30377   }
30378   return rc;
30379 }
30380 
30381 /* Take the requested lock on the conch file and break a stale lock if the
30382 ** host id matches.
30383 */
30384 static int proxyConchLock(unixFile *pFile, uuid_t myHostID, int lockType){
30385   proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
30386   unixFile *conchFile = pCtx->conchFile;
30387   int rc = SQLITE_OK;
30388   int nTries = 0;
30389   struct timespec conchModTime;
30390 
30391   do {
30392     rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType);
30393     nTries ++;
30394     if( rc==SQLITE_BUSY ){
30395       /* If the lock failed (busy):
30396        * 1st try: get the mod time of the conch, wait 0.5s and try again.
30397        * 2nd try: fail if the mod time changed or host id is different, wait
30398        *           10 sec and try again
30399        * 3rd try: break the lock unless the mod time has changed.
30400        */
30401       struct stat buf;
30402       if( osFstat(conchFile->h, &buf) ){
30403         pFile->lastErrno = errno;
30404         return SQLITE_IOERR_LOCK;
30405       }
30406 
30407       if( nTries==1 ){
30408         conchModTime = buf.st_mtimespec;
30409         usleep(500000); /* wait 0.5 sec and try the lock again*/
30410         continue;
30411       }
30412 
30413       assert( nTries>1 );
30414       if( conchModTime.tv_sec != buf.st_mtimespec.tv_sec ||
30415          conchModTime.tv_nsec != buf.st_mtimespec.tv_nsec ){
30416         return SQLITE_BUSY;
30417       }
30418 
30419       if( nTries==2 ){
30420         char tBuf[PROXY_MAXCONCHLEN];
30421         int len = osPread(conchFile->h, tBuf, PROXY_MAXCONCHLEN, 0);
30422         if( len<0 ){
30423           pFile->lastErrno = errno;
30424           return SQLITE_IOERR_LOCK;
30425         }
30426         if( len>PROXY_PATHINDEX && tBuf[0]==(char)PROXY_CONCHVERSION){
30427           /* don't break the lock if the host id doesn't match */
30428           if( 0!=memcmp(&tBuf[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN) ){
30429             return SQLITE_BUSY;
30430           }
30431         }else{
30432           /* don't break the lock on short read or a version mismatch */
30433           return SQLITE_BUSY;
30434         }
30435         usleep(10000000); /* wait 10 sec and try the lock again */
30436         continue;
30437       }
30438 
30439       assert( nTries==3 );
30440       if( 0==proxyBreakConchLock(pFile, myHostID) ){
30441         rc = SQLITE_OK;
30442         if( lockType==EXCLUSIVE_LOCK ){
30443           rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, SHARED_LOCK);
30444         }
30445         if( !rc ){
30446           rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType);
30447         }
30448       }
30449     }
30450   } while( rc==SQLITE_BUSY && nTries<3 );
30451 
30452   return rc;
30453 }
30454 
30455 /* Takes the conch by taking a shared lock and read the contents conch, if
30456 ** lockPath is non-NULL, the host ID and lock file path must match.  A NULL
30457 ** lockPath means that the lockPath in the conch file will be used if the
30458 ** host IDs match, or a new lock path will be generated automatically
30459 ** and written to the conch file.
30460 */
30461 static int proxyTakeConch(unixFile *pFile){
30462   proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
30463 
30464   if( pCtx->conchHeld!=0 ){
30465     return SQLITE_OK;
30466   }else{
30467     unixFile *conchFile = pCtx->conchFile;
30468     uuid_t myHostID;
30469     int pError = 0;
30470     char readBuf[PROXY_MAXCONCHLEN];
30471     char lockPath[MAXPATHLEN];
30472     char *tempLockPath = NULL;
30473     int rc = SQLITE_OK;
30474     int createConch = 0;
30475     int hostIdMatch = 0;
30476     int readLen = 0;
30477     int tryOldLockPath = 0;
30478     int forceNewLockPath = 0;
30479 
30480     OSTRACE(("TAKECONCH  %d for %s pid=%d\n", conchFile->h,
30481              (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"), getpid()));
30482 
30483     rc = proxyGetHostID(myHostID, &pError);
30484     if( (rc&0xff)==SQLITE_IOERR ){
30485       pFile->lastErrno = pError;
30486       goto end_takeconch;
30487     }
30488     rc = proxyConchLock(pFile, myHostID, SHARED_LOCK);
30489     if( rc!=SQLITE_OK ){
30490       goto end_takeconch;
30491     }
30492     /* read the existing conch file */
30493     readLen = seekAndRead((unixFile*)conchFile, 0, readBuf, PROXY_MAXCONCHLEN);
30494     if( readLen<0 ){
30495       /* I/O error: lastErrno set by seekAndRead */
30496       pFile->lastErrno = conchFile->lastErrno;
30497       rc = SQLITE_IOERR_READ;
30498       goto end_takeconch;
30499     }else if( readLen<=(PROXY_HEADERLEN+PROXY_HOSTIDLEN) ||
30500              readBuf[0]!=(char)PROXY_CONCHVERSION ){
30501       /* a short read or version format mismatch means we need to create a new
30502       ** conch file.
30503       */
30504       createConch = 1;
30505     }
30506     /* if the host id matches and the lock path already exists in the conch
30507     ** we'll try to use the path there, if we can't open that path, we'll
30508     ** retry with a new auto-generated path
30509     */
30510     do { /* in case we need to try again for an :auto: named lock file */
30511 
30512       if( !createConch && !forceNewLockPath ){
30513         hostIdMatch = !memcmp(&readBuf[PROXY_HEADERLEN], myHostID,
30514                                   PROXY_HOSTIDLEN);
30515         /* if the conch has data compare the contents */
30516         if( !pCtx->lockProxyPath ){
30517           /* for auto-named local lock file, just check the host ID and we'll
30518            ** use the local lock file path that's already in there
30519            */
30520           if( hostIdMatch ){
30521             size_t pathLen = (readLen - PROXY_PATHINDEX);
30522 
30523             if( pathLen>=MAXPATHLEN ){
30524               pathLen=MAXPATHLEN-1;
30525             }
30526             memcpy(lockPath, &readBuf[PROXY_PATHINDEX], pathLen);
30527             lockPath[pathLen] = 0;
30528             tempLockPath = lockPath;
30529             tryOldLockPath = 1;
30530             /* create a copy of the lock path if the conch is taken */
30531             goto end_takeconch;
30532           }
30533         }else if( hostIdMatch
30534                && !strncmp(pCtx->lockProxyPath, &readBuf[PROXY_PATHINDEX],
30535                            readLen-PROXY_PATHINDEX)
30536         ){
30537           /* conch host and lock path match */
30538           goto end_takeconch;
30539         }
30540       }
30541 
30542       /* if the conch isn't writable and doesn't match, we can't take it */
30543       if( (conchFile->openFlags&O_RDWR) == 0 ){
30544         rc = SQLITE_BUSY;
30545         goto end_takeconch;
30546       }
30547 
30548       /* either the conch didn't match or we need to create a new one */
30549       if( !pCtx->lockProxyPath ){
30550         proxyGetLockPath(pCtx->dbPath, lockPath, MAXPATHLEN);
30551         tempLockPath = lockPath;
30552         /* create a copy of the lock path _only_ if the conch is taken */
30553       }
30554 
30555       /* update conch with host and path (this will fail if other process
30556       ** has a shared lock already), if the host id matches, use the big
30557       ** stick.
30558       */
30559       futimes(conchFile->h, NULL);
30560       if( hostIdMatch && !createConch ){
30561         if( conchFile->pInode && conchFile->pInode->nShared>1 ){
30562           /* We are trying for an exclusive lock but another thread in this
30563            ** same process is still holding a shared lock. */
30564           rc = SQLITE_BUSY;
30565         } else {
30566           rc = proxyConchLock(pFile, myHostID, EXCLUSIVE_LOCK);
30567         }
30568       }else{
30569         rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, EXCLUSIVE_LOCK);
30570       }
30571       if( rc==SQLITE_OK ){
30572         char writeBuffer[PROXY_MAXCONCHLEN];
30573         int writeSize = 0;
30574 
30575         writeBuffer[0] = (char)PROXY_CONCHVERSION;
30576         memcpy(&writeBuffer[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN);
30577         if( pCtx->lockProxyPath!=NULL ){
30578           strlcpy(&writeBuffer[PROXY_PATHINDEX], pCtx->lockProxyPath, MAXPATHLEN);
30579         }else{
30580           strlcpy(&writeBuffer[PROXY_PATHINDEX], tempLockPath, MAXPATHLEN);
30581         }
30582         writeSize = PROXY_PATHINDEX + strlen(&writeBuffer[PROXY_PATHINDEX]);
30583         robust_ftruncate(conchFile->h, writeSize);
30584         rc = unixWrite((sqlite3_file *)conchFile, writeBuffer, writeSize, 0);
30585         fsync(conchFile->h);
30586         /* If we created a new conch file (not just updated the contents of a
30587          ** valid conch file), try to match the permissions of the database
30588          */
30589         if( rc==SQLITE_OK && createConch ){
30590           struct stat buf;
30591           int err = osFstat(pFile->h, &buf);
30592           if( err==0 ){
30593             mode_t cmode = buf.st_mode&(S_IRUSR|S_IWUSR | S_IRGRP|S_IWGRP |
30594                                         S_IROTH|S_IWOTH);
30595             /* try to match the database file R/W permissions, ignore failure */
30596 #ifndef SQLITE_PROXY_DEBUG
30597             osFchmod(conchFile->h, cmode);
30598 #else
30599             do{
30600               rc = osFchmod(conchFile->h, cmode);
30601             }while( rc==(-1) && errno==EINTR );
30602             if( rc!=0 ){
30603               int code = errno;
30604               fprintf(stderr, "fchmod %o FAILED with %d %s\n",
30605                       cmode, code, strerror(code));
30606             } else {
30607               fprintf(stderr, "fchmod %o SUCCEDED\n",cmode);
30608             }
30609           }else{
30610             int code = errno;
30611             fprintf(stderr, "STAT FAILED[%d] with %d %s\n",
30612                     err, code, strerror(code));
30613 #endif
30614           }
30615         }
30616       }
30617       conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, SHARED_LOCK);
30618 
30619     end_takeconch:
30620       OSTRACE(("TRANSPROXY: CLOSE  %d\n", pFile->h));
30621       if( rc==SQLITE_OK && pFile->openFlags ){
30622         if( pFile->h>=0 ){
30623           robust_close(pFile, pFile->h, __LINE__);
30624         }
30625         pFile->h = -1;
30626         int fd = robust_open(pCtx->dbPath, pFile->openFlags,
30627                       SQLITE_DEFAULT_FILE_PERMISSIONS);
30628         OSTRACE(("TRANSPROXY: OPEN  %d\n", fd));
30629         if( fd>=0 ){
30630           pFile->h = fd;
30631         }else{
30632           rc=SQLITE_CANTOPEN_BKPT; /* SQLITE_BUSY? proxyTakeConch called
30633            during locking */
30634         }
30635       }
30636       if( rc==SQLITE_OK && !pCtx->lockProxy ){
30637         char *path = tempLockPath ? tempLockPath : pCtx->lockProxyPath;
30638         rc = proxyCreateUnixFile(path, &pCtx->lockProxy, 1);
30639         if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM && tryOldLockPath ){
30640           /* we couldn't create the proxy lock file with the old lock file path
30641            ** so try again via auto-naming
30642            */
30643           forceNewLockPath = 1;
30644           tryOldLockPath = 0;
30645           continue; /* go back to the do {} while start point, try again */
30646         }
30647       }
30648       if( rc==SQLITE_OK ){
30649         /* Need to make a copy of path if we extracted the value
30650          ** from the conch file or the path was allocated on the stack
30651          */
30652         if( tempLockPath ){
30653           pCtx->lockProxyPath = sqlite3DbStrDup(0, tempLockPath);
30654           if( !pCtx->lockProxyPath ){
30655             rc = SQLITE_NOMEM;
30656           }
30657         }
30658       }
30659       if( rc==SQLITE_OK ){
30660         pCtx->conchHeld = 1;
30661 
30662         if( pCtx->lockProxy->pMethod == &afpIoMethods ){
30663           afpLockingContext *afpCtx;
30664           afpCtx = (afpLockingContext *)pCtx->lockProxy->lockingContext;
30665           afpCtx->dbPath = pCtx->lockProxyPath;
30666         }
30667       } else {
30668         conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
30669       }
30670       OSTRACE(("TAKECONCH  %d %s\n", conchFile->h,
30671                rc==SQLITE_OK?"ok":"failed"));
30672       return rc;
30673     } while (1); /* in case we need to retry the :auto: lock file -
30674                  ** we should never get here except via the 'continue' call. */
30675   }
30676 }
30677 
30678 /*
30679 ** If pFile holds a lock on a conch file, then release that lock.
30680 */
30681 static int proxyReleaseConch(unixFile *pFile){
30682   int rc = SQLITE_OK;         /* Subroutine return code */
30683   proxyLockingContext *pCtx;  /* The locking context for the proxy lock */
30684   unixFile *conchFile;        /* Name of the conch file */
30685 
30686   pCtx = (proxyLockingContext *)pFile->lockingContext;
30687   conchFile = pCtx->conchFile;
30688   OSTRACE(("RELEASECONCH  %d for %s pid=%d\n", conchFile->h,
30689            (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"),
30690            getpid()));
30691   if( pCtx->conchHeld>0 ){
30692     rc = conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
30693   }
30694   pCtx->conchHeld = 0;
30695   OSTRACE(("RELEASECONCH  %d %s\n", conchFile->h,
30696            (rc==SQLITE_OK ? "ok" : "failed")));
30697   return rc;
30698 }
30699 
30700 /*
30701 ** Given the name of a database file, compute the name of its conch file.
30702 ** Store the conch filename in memory obtained from sqlite3_malloc().
30703 ** Make *pConchPath point to the new name.  Return SQLITE_OK on success
30704 ** or SQLITE_NOMEM if unable to obtain memory.
30705 **
30706 ** The caller is responsible for ensuring that the allocated memory
30707 ** space is eventually freed.
30708 **
30709 ** *pConchPath is set to NULL if a memory allocation error occurs.
30710 */
30711 static int proxyCreateConchPathname(char *dbPath, char **pConchPath){
30712   int i;                        /* Loop counter */
30713   int len = (int)strlen(dbPath); /* Length of database filename - dbPath */
30714   char *conchPath;              /* buffer in which to construct conch name */
30715 
30716   /* Allocate space for the conch filename and initialize the name to
30717   ** the name of the original database file. */
30718   *pConchPath = conchPath = (char *)sqlite3_malloc(len + 8);
30719   if( conchPath==0 ){
30720     return SQLITE_NOMEM;
30721   }
30722   memcpy(conchPath, dbPath, len+1);
30723 
30724   /* now insert a "." before the last / character */
30725   for( i=(len-1); i>=0; i-- ){
30726     if( conchPath[i]=='/' ){
30727       i++;
30728       break;
30729     }
30730   }
30731   conchPath[i]='.';
30732   while ( i<len ){
30733     conchPath[i+1]=dbPath[i];
30734     i++;
30735   }
30736 
30737   /* append the "-conch" suffix to the file */
30738   memcpy(&conchPath[i+1], "-conch", 7);
30739   assert( (int)strlen(conchPath) == len+7 );
30740 
30741   return SQLITE_OK;
30742 }
30743 
30744 
30745 /* Takes a fully configured proxy locking-style unix file and switches
30746 ** the local lock file path
30747 */
30748 static int switchLockProxyPath(unixFile *pFile, const char *path) {
30749   proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
30750   char *oldPath = pCtx->lockProxyPath;
30751   int rc = SQLITE_OK;
30752 
30753   if( pFile->eFileLock!=NO_LOCK ){
30754     return SQLITE_BUSY;
30755   }
30756 
30757   /* nothing to do if the path is NULL, :auto: or matches the existing path */
30758   if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ||
30759     (oldPath && !strncmp(oldPath, path, MAXPATHLEN)) ){
30760     return SQLITE_OK;
30761   }else{
30762     unixFile *lockProxy = pCtx->lockProxy;
30763     pCtx->lockProxy=NULL;
30764     pCtx->conchHeld = 0;
30765     if( lockProxy!=NULL ){
30766       rc=lockProxy->pMethod->xClose((sqlite3_file *)lockProxy);
30767       if( rc ) return rc;
30768       sqlite3_free(lockProxy);
30769     }
30770     sqlite3_free(oldPath);
30771     pCtx->lockProxyPath = sqlite3DbStrDup(0, path);
30772   }
30773 
30774   return rc;
30775 }
30776 
30777 /*
30778 ** pFile is a file that has been opened by a prior xOpen call.  dbPath
30779 ** is a string buffer at least MAXPATHLEN+1 characters in size.
30780 **
30781 ** This routine find the filename associated with pFile and writes it
30782 ** int dbPath.
30783 */
30784 static int proxyGetDbPathForUnixFile(unixFile *pFile, char *dbPath){
30785 #if defined(__APPLE__)
30786   if( pFile->pMethod == &afpIoMethods ){
30787     /* afp style keeps a reference to the db path in the filePath field
30788     ** of the struct */
30789     assert( (int)strlen((char*)pFile->lockingContext)<=MAXPATHLEN );
30790     strlcpy(dbPath, ((afpLockingContext *)pFile->lockingContext)->dbPath, MAXPATHLEN);
30791   } else
30792 #endif
30793   if( pFile->pMethod == &dotlockIoMethods ){
30794     /* dot lock style uses the locking context to store the dot lock
30795     ** file path */
30796     int len = strlen((char *)pFile->lockingContext) - strlen(DOTLOCK_SUFFIX);
30797     memcpy(dbPath, (char *)pFile->lockingContext, len + 1);
30798   }else{
30799     /* all other styles use the locking context to store the db file path */
30800     assert( strlen((char*)pFile->lockingContext)<=MAXPATHLEN );
30801     strlcpy(dbPath, (char *)pFile->lockingContext, MAXPATHLEN);
30802   }
30803   return SQLITE_OK;
30804 }
30805 
30806 /*
30807 ** Takes an already filled in unix file and alters it so all file locking
30808 ** will be performed on the local proxy lock file.  The following fields
30809 ** are preserved in the locking context so that they can be restored and
30810 ** the unix structure properly cleaned up at close time:
30811 **  ->lockingContext
30812 **  ->pMethod
30813 */
30814 static int proxyTransformUnixFile(unixFile *pFile, const char *path) {
30815   proxyLockingContext *pCtx;
30816   char dbPath[MAXPATHLEN+1];       /* Name of the database file */
30817   char *lockPath=NULL;
30818   int rc = SQLITE_OK;
30819 
30820   if( pFile->eFileLock!=NO_LOCK ){
30821     return SQLITE_BUSY;
30822   }
30823   proxyGetDbPathForUnixFile(pFile, dbPath);
30824   if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ){
30825     lockPath=NULL;
30826   }else{
30827     lockPath=(char *)path;
30828   }
30829 
30830   OSTRACE(("TRANSPROXY  %d for %s pid=%d\n", pFile->h,
30831            (lockPath ? lockPath : ":auto:"), getpid()));
30832 
30833   pCtx = sqlite3_malloc( sizeof(*pCtx) );
30834   if( pCtx==0 ){
30835     return SQLITE_NOMEM;
30836   }
30837   memset(pCtx, 0, sizeof(*pCtx));
30838 
30839   rc = proxyCreateConchPathname(dbPath, &pCtx->conchFilePath);
30840   if( rc==SQLITE_OK ){
30841     rc = proxyCreateUnixFile(pCtx->conchFilePath, &pCtx->conchFile, 0);
30842     if( rc==SQLITE_CANTOPEN && ((pFile->openFlags&O_RDWR) == 0) ){
30843       /* if (a) the open flags are not O_RDWR, (b) the conch isn't there, and
30844       ** (c) the file system is read-only, then enable no-locking access.
30845       ** Ugh, since O_RDONLY==0x0000 we test for !O_RDWR since unixOpen asserts
30846       ** that openFlags will have only one of O_RDONLY or O_RDWR.
30847       */
30848       struct statfs fsInfo;
30849       struct stat conchInfo;
30850       int goLockless = 0;
30851 
30852       if( osStat(pCtx->conchFilePath, &conchInfo) == -1 ) {
30853         int err = errno;
30854         if( (err==ENOENT) && (statfs(dbPath, &fsInfo) != -1) ){
30855           goLockless = (fsInfo.f_flags&MNT_RDONLY) == MNT_RDONLY;
30856         }
30857       }
30858       if( goLockless ){
30859         pCtx->conchHeld = -1; /* read only FS/ lockless */
30860         rc = SQLITE_OK;
30861       }
30862     }
30863   }
30864   if( rc==SQLITE_OK && lockPath ){
30865     pCtx->lockProxyPath = sqlite3DbStrDup(0, lockPath);
30866   }
30867 
30868   if( rc==SQLITE_OK ){
30869     pCtx->dbPath = sqlite3DbStrDup(0, dbPath);
30870     if( pCtx->dbPath==NULL ){
30871       rc = SQLITE_NOMEM;
30872     }
30873   }
30874   if( rc==SQLITE_OK ){
30875     /* all memory is allocated, proxys are created and assigned,
30876     ** switch the locking context and pMethod then return.
30877     */
30878     pCtx->oldLockingContext = pFile->lockingContext;
30879     pFile->lockingContext = pCtx;
30880     pCtx->pOldMethod = pFile->pMethod;
30881     pFile->pMethod = &proxyIoMethods;
30882   }else{
30883     if( pCtx->conchFile ){
30884       pCtx->conchFile->pMethod->xClose((sqlite3_file *)pCtx->conchFile);
30885       sqlite3_free(pCtx->conchFile);
30886     }
30887     sqlite3DbFree(0, pCtx->lockProxyPath);
30888     sqlite3_free(pCtx->conchFilePath);
30889     sqlite3_free(pCtx);
30890   }
30891   OSTRACE(("TRANSPROXY  %d %s\n", pFile->h,
30892            (rc==SQLITE_OK ? "ok" : "failed")));
30893   return rc;
30894 }
30895 
30896 
30897 /*
30898 ** This routine handles sqlite3_file_control() calls that are specific
30899 ** to proxy locking.
30900 */
30901 static int proxyFileControl(sqlite3_file *id, int op, void *pArg){
30902   switch( op ){
30903     case SQLITE_GET_LOCKPROXYFILE: {
30904       unixFile *pFile = (unixFile*)id;
30905       if( pFile->pMethod == &proxyIoMethods ){
30906         proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
30907         proxyTakeConch(pFile);
30908         if( pCtx->lockProxyPath ){
30909           *(const char **)pArg = pCtx->lockProxyPath;
30910         }else{
30911           *(const char **)pArg = ":auto: (not held)";
30912         }
30913       } else {
30914         *(const char **)pArg = NULL;
30915       }
30916       return SQLITE_OK;
30917     }
30918     case SQLITE_SET_LOCKPROXYFILE: {
30919       unixFile *pFile = (unixFile*)id;
30920       int rc = SQLITE_OK;
30921       int isProxyStyle = (pFile->pMethod == &proxyIoMethods);
30922       if( pArg==NULL || (const char *)pArg==0 ){
30923         if( isProxyStyle ){
30924           /* turn off proxy locking - not supported */
30925           rc = SQLITE_ERROR /*SQLITE_PROTOCOL? SQLITE_MISUSE?*/;
30926         }else{
30927           /* turn off proxy locking - already off - NOOP */
30928           rc = SQLITE_OK;
30929         }
30930       }else{
30931         const char *proxyPath = (const char *)pArg;
30932         if( isProxyStyle ){
30933           proxyLockingContext *pCtx =
30934             (proxyLockingContext*)pFile->lockingContext;
30935           if( !strcmp(pArg, ":auto:")
30936            || (pCtx->lockProxyPath &&
30937                !strncmp(pCtx->lockProxyPath, proxyPath, MAXPATHLEN))
30938           ){
30939             rc = SQLITE_OK;
30940           }else{
30941             rc = switchLockProxyPath(pFile, proxyPath);
30942           }
30943         }else{
30944           /* turn on proxy file locking */
30945           rc = proxyTransformUnixFile(pFile, proxyPath);
30946         }
30947       }
30948       return rc;
30949     }
30950     default: {
30951       assert( 0 );  /* The call assures that only valid opcodes are sent */
30952     }
30953   }
30954   /*NOTREACHED*/
30955   return SQLITE_ERROR;
30956 }
30957 
30958 /*
30959 ** Within this division (the proxying locking implementation) the procedures
30960 ** above this point are all utilities.  The lock-related methods of the
30961 ** proxy-locking sqlite3_io_method object follow.
30962 */
30963 
30964 
30965 /*
30966 ** This routine checks if there is a RESERVED lock held on the specified
30967 ** file by this or any other process. If such a lock is held, set *pResOut
30968 ** to a non-zero value otherwise *pResOut is set to zero.  The return value
30969 ** is set to SQLITE_OK unless an I/O error occurs during lock checking.
30970 */
30971 static int proxyCheckReservedLock(sqlite3_file *id, int *pResOut) {
30972   unixFile *pFile = (unixFile*)id;
30973   int rc = proxyTakeConch(pFile);
30974   if( rc==SQLITE_OK ){
30975     proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
30976     if( pCtx->conchHeld>0 ){
30977       unixFile *proxy = pCtx->lockProxy;
30978       return proxy->pMethod->xCheckReservedLock((sqlite3_file*)proxy, pResOut);
30979     }else{ /* conchHeld < 0 is lockless */
30980       pResOut=0;
30981     }
30982   }
30983   return rc;
30984 }
30985 
30986 /*
30987 ** Lock the file with the lock specified by parameter eFileLock - one
30988 ** of the following:
30989 **
30990 **     (1) SHARED_LOCK
30991 **     (2) RESERVED_LOCK
30992 **     (3) PENDING_LOCK
30993 **     (4) EXCLUSIVE_LOCK
30994 **
30995 ** Sometimes when requesting one lock state, additional lock states
30996 ** are inserted in between.  The locking might fail on one of the later
30997 ** transitions leaving the lock state different from what it started but
30998 ** still short of its goal.  The following chart shows the allowed
30999 ** transitions and the inserted intermediate states:
31000 **
31001 **    UNLOCKED -> SHARED
31002 **    SHARED -> RESERVED
31003 **    SHARED -> (PENDING) -> EXCLUSIVE
31004 **    RESERVED -> (PENDING) -> EXCLUSIVE
31005 **    PENDING -> EXCLUSIVE
31006 **
31007 ** This routine will only increase a lock.  Use the sqlite3OsUnlock()
31008 ** routine to lower a locking level.
31009 */
31010 static int proxyLock(sqlite3_file *id, int eFileLock) {
31011   unixFile *pFile = (unixFile*)id;
31012   int rc = proxyTakeConch(pFile);
31013   if( rc==SQLITE_OK ){
31014     proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
31015     if( pCtx->conchHeld>0 ){
31016       unixFile *proxy = pCtx->lockProxy;
31017       rc = proxy->pMethod->xLock((sqlite3_file*)proxy, eFileLock);
31018       pFile->eFileLock = proxy->eFileLock;
31019     }else{
31020       /* conchHeld < 0 is lockless */
31021     }
31022   }
31023   return rc;
31024 }
31025 
31026 
31027 /*
31028 ** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
31029 ** must be either NO_LOCK or SHARED_LOCK.
31030 **
31031 ** If the locking level of the file descriptor is already at or below
31032 ** the requested locking level, this routine is a no-op.
31033 */
31034 static int proxyUnlock(sqlite3_file *id, int eFileLock) {
31035   unixFile *pFile = (unixFile*)id;
31036   int rc = proxyTakeConch(pFile);
31037   if( rc==SQLITE_OK ){
31038     proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
31039     if( pCtx->conchHeld>0 ){
31040       unixFile *proxy = pCtx->lockProxy;
31041       rc = proxy->pMethod->xUnlock((sqlite3_file*)proxy, eFileLock);
31042       pFile->eFileLock = proxy->eFileLock;
31043     }else{
31044       /* conchHeld < 0 is lockless */
31045     }
31046   }
31047   return rc;
31048 }
31049 
31050 /*
31051 ** Close a file that uses proxy locks.
31052 */
31053 static int proxyClose(sqlite3_file *id) {
31054   if( id ){
31055     unixFile *pFile = (unixFile*)id;
31056     proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
31057     unixFile *lockProxy = pCtx->lockProxy;
31058     unixFile *conchFile = pCtx->conchFile;
31059     int rc = SQLITE_OK;
31060 
31061     if( lockProxy ){
31062       rc = lockProxy->pMethod->xUnlock((sqlite3_file*)lockProxy, NO_LOCK);
31063       if( rc ) return rc;
31064       rc = lockProxy->pMethod->xClose((sqlite3_file*)lockProxy);
31065       if( rc ) return rc;
31066       sqlite3_free(lockProxy);
31067       pCtx->lockProxy = 0;
31068     }
31069     if( conchFile ){
31070       if( pCtx->conchHeld ){
31071         rc = proxyReleaseConch(pFile);
31072         if( rc ) return rc;
31073       }
31074       rc = conchFile->pMethod->xClose((sqlite3_file*)conchFile);
31075       if( rc ) return rc;
31076       sqlite3_free(conchFile);
31077     }
31078     sqlite3DbFree(0, pCtx->lockProxyPath);
31079     sqlite3_free(pCtx->conchFilePath);
31080     sqlite3DbFree(0, pCtx->dbPath);
31081     /* restore the original locking context and pMethod then close it */
31082     pFile->lockingContext = pCtx->oldLockingContext;
31083     pFile->pMethod = pCtx->pOldMethod;
31084     sqlite3_free(pCtx);
31085     return pFile->pMethod->xClose(id);
31086   }
31087   return SQLITE_OK;
31088 }
31089 
31090 
31091 
31092 #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
31093 /*
31094 ** The proxy locking style is intended for use with AFP filesystems.
31095 ** And since AFP is only supported on MacOSX, the proxy locking is also
31096 ** restricted to MacOSX.
31097 **
31098 **
31099 ******************* End of the proxy lock implementation **********************
31100 ******************************************************************************/
31101 
31102 /*
31103 ** Initialize the operating system interface.
31104 **
31105 ** This routine registers all VFS implementations for unix-like operating
31106 ** systems.  This routine, and the sqlite3_os_end() routine that follows,
31107 ** should be the only routines in this file that are visible from other
31108 ** files.
31109 **
31110 ** This routine is called once during SQLite initialization and by a
31111 ** single thread.  The memory allocation and mutex subsystems have not
31112 ** necessarily been initialized when this routine is called, and so they
31113 ** should not be used.
31114 */
31115 SQLITE_API int sqlite3_os_init(void){
31116   /*
31117   ** The following macro defines an initializer for an sqlite3_vfs object.
31118   ** The name of the VFS is NAME.  The pAppData is a pointer to a pointer
31119   ** to the "finder" function.  (pAppData is a pointer to a pointer because
31120   ** silly C90 rules prohibit a void* from being cast to a function pointer
31121   ** and so we have to go through the intermediate pointer to avoid problems
31122   ** when compiling with -pedantic-errors on GCC.)
31123   **
31124   ** The FINDER parameter to this macro is the name of the pointer to the
31125   ** finder-function.  The finder-function returns a pointer to the
31126   ** sqlite_io_methods object that implements the desired locking
31127   ** behaviors.  See the division above that contains the IOMETHODS
31128   ** macro for addition information on finder-functions.
31129   **
31130   ** Most finders simply return a pointer to a fixed sqlite3_io_methods
31131   ** object.  But the "autolockIoFinder" available on MacOSX does a little
31132   ** more than that; it looks at the filesystem type that hosts the
31133   ** database file and tries to choose an locking method appropriate for
31134   ** that filesystem time.
31135   */
31136   #define UNIXVFS(VFSNAME, FINDER) {                        \
31137     3,                    /* iVersion */                    \
31138     sizeof(unixFile),     /* szOsFile */                    \
31139     MAX_PATHNAME,         /* mxPathname */                  \
31140     0,                    /* pNext */                       \
31141     VFSNAME,              /* zName */                       \
31142     (void*)&FINDER,       /* pAppData */                    \
31143     unixOpen,             /* xOpen */                       \
31144     unixDelete,           /* xDelete */                     \
31145     unixAccess,           /* xAccess */                     \
31146     unixFullPathname,     /* xFullPathname */               \
31147     unixDlOpen,           /* xDlOpen */                     \
31148     unixDlError,          /* xDlError */                    \
31149     unixDlSym,            /* xDlSym */                      \
31150     unixDlClose,          /* xDlClose */                    \
31151     unixRandomness,       /* xRandomness */                 \
31152     unixSleep,            /* xSleep */                      \
31153     unixCurrentTime,      /* xCurrentTime */                \
31154     unixGetLastError,     /* xGetLastError */               \
31155     unixCurrentTimeInt64, /* xCurrentTimeInt64 */           \
31156     unixSetSystemCall,    /* xSetSystemCall */              \
31157     unixGetSystemCall,    /* xGetSystemCall */              \
31158     unixNextSystemCall,   /* xNextSystemCall */             \
31159   }
31160 
31161   /*
31162   ** All default VFSes for unix are contained in the following array.
31163   **
31164   ** Note that the sqlite3_vfs.pNext field of the VFS object is modified
31165   ** by the SQLite core when the VFS is registered.  So the following
31166   ** array cannot be const.
31167   */
31168   static sqlite3_vfs aVfs[] = {
31169 #if SQLITE_ENABLE_LOCKING_STYLE && (OS_VXWORKS || defined(__APPLE__))
31170     UNIXVFS("unix",          autolockIoFinder ),
31171 #else
31172     UNIXVFS("unix",          posixIoFinder ),
31173 #endif
31174     UNIXVFS("unix-none",     nolockIoFinder ),
31175     UNIXVFS("unix-dotfile",  dotlockIoFinder ),
31176     UNIXVFS("unix-excl",     posixIoFinder ),
31177 #if OS_VXWORKS
31178     UNIXVFS("unix-namedsem", semIoFinder ),
31179 #endif
31180 #if SQLITE_ENABLE_LOCKING_STYLE
31181     UNIXVFS("unix-posix",    posixIoFinder ),
31182 #if !OS_VXWORKS
31183     UNIXVFS("unix-flock",    flockIoFinder ),
31184 #endif
31185 #endif
31186 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
31187     UNIXVFS("unix-afp",      afpIoFinder ),
31188     UNIXVFS("unix-nfs",      nfsIoFinder ),
31189     UNIXVFS("unix-proxy",    proxyIoFinder ),
31190 #endif
31191   };
31192   unsigned int i;          /* Loop counter */
31193 
31194   /* Double-check that the aSyscall[] array has been constructed
31195   ** correctly.  See ticket [bb3a86e890c8e96ab] */
31196   assert( ArraySize(aSyscall)==16 );
31197 
31198   /* Register all VFSes defined in the aVfs[] array */
31199   for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){
31200     sqlite3_vfs_register(&aVfs[i], i==0);
31201   }
31202   return SQLITE_OK;
31203 }
31204 
31205 /*
31206 ** Shutdown the operating system interface.
31207 **
31208 ** Some operating systems might need to do some cleanup in this routine,
31209 ** to release dynamically allocated objects.  But not on unix.
31210 ** This routine is a no-op for unix.
31211 */
31212 SQLITE_API int sqlite3_os_end(void){
31213   return SQLITE_OK;
31214 }
31215 
31216 #endif /* SQLITE_OS_UNIX */
31217 
31218 /************** End of os_unix.c *********************************************/
31219 /************** Begin file os_win.c ******************************************/
31220 /*
31221 ** 2004 May 22
31222 **
31223 ** The author disclaims copyright to this source code.  In place of
31224 ** a legal notice, here is a blessing:
31225 **
31226 **    May you do good and not evil.
31227 **    May you find forgiveness for yourself and forgive others.
31228 **    May you share freely, never taking more than you give.
31229 **
31230 ******************************************************************************
31231 **
31232 ** This file contains code that is specific to windows.
31233 */
31234 #if SQLITE_OS_WIN               /* This file is used for windows only */
31235 
31236 
31237 /*
31238 ** A Note About Memory Allocation:
31239 **
31240 ** This driver uses malloc()/free() directly rather than going through
31241 ** the SQLite-wrappers sqlite3_malloc()/sqlite3_free().  Those wrappers
31242 ** are designed for use on embedded systems where memory is scarce and
31243 ** malloc failures happen frequently.  Win32 does not typically run on
31244 ** embedded systems, and when it does the developers normally have bigger
31245 ** problems to worry about than running out of memory.  So there is not
31246 ** a compelling need to use the wrappers.
31247 **
31248 ** But there is a good reason to not use the wrappers.  If we use the
31249 ** wrappers then we will get simulated malloc() failures within this
31250 ** driver.  And that causes all kinds of problems for our tests.  We
31251 ** could enhance SQLite to deal with simulated malloc failures within
31252 ** the OS driver, but the code to deal with those failure would not
31253 ** be exercised on Linux (which does not need to malloc() in the driver)
31254 ** and so we would have difficulty writing coverage tests for that
31255 ** code.  Better to leave the code out, we think.
31256 **
31257 ** The point of this discussion is as follows:  When creating a new
31258 ** OS layer for an embedded system, if you use this file as an example,
31259 ** avoid the use of malloc()/free().  Those routines work ok on windows
31260 ** desktops but not so well in embedded systems.
31261 */
31262 
31263 #include <winbase.h>
31264 
31265 #ifdef __CYGWIN__
31266 # include <sys/cygwin.h>
31267 #endif
31268 
31269 /*
31270 ** Macros used to determine whether or not to use threads.
31271 */
31272 #if defined(THREADSAFE) && THREADSAFE
31273 # define SQLITE_W32_THREADS 1
31274 #endif
31275 
31276 /*
31277 ** Include code that is common to all os_*.c files
31278 */
31279 /************** Include os_common.h in the middle of os_win.c ****************/
31280 /************** Begin file os_common.h ***************************************/
31281 /*
31282 ** 2004 May 22
31283 **
31284 ** The author disclaims copyright to this source code.  In place of
31285 ** a legal notice, here is a blessing:
31286 **
31287 **    May you do good and not evil.
31288 **    May you find forgiveness for yourself and forgive others.
31289 **    May you share freely, never taking more than you give.
31290 **
31291 ******************************************************************************
31292 **
31293 ** This file contains macros and a little bit of code that is common to
31294 ** all of the platform-specific files (os_*.c) and is #included into those
31295 ** files.
31296 **
31297 ** This file should be #included by the os_*.c files only.  It is not a
31298 ** general purpose header file.
31299 */
31300 #ifndef _OS_COMMON_H_
31301 #define _OS_COMMON_H_
31302 
31303 /*
31304 ** At least two bugs have slipped in because we changed the MEMORY_DEBUG
31305 ** macro to SQLITE_DEBUG and some older makefiles have not yet made the
31306 ** switch.  The following code should catch this problem at compile-time.
31307 */
31308 #ifdef MEMORY_DEBUG
31309 # error "The MEMORY_DEBUG macro is obsolete.  Use SQLITE_DEBUG instead."
31310 #endif
31311 
31312 #ifdef SQLITE_DEBUG
31313 SQLITE_PRIVATE int sqlite3OSTrace = 0;
31314 #define OSTRACE(X)          if( sqlite3OSTrace ) sqlite3DebugPrintf X
31315 #else
31316 #define OSTRACE(X)
31317 #endif
31318 
31319 /*
31320 ** Macros for performance tracing.  Normally turned off.  Only works
31321 ** on i486 hardware.
31322 */
31323 #ifdef SQLITE_PERFORMANCE_TRACE
31324 
31325 /*
31326 ** hwtime.h contains inline assembler code for implementing
31327 ** high-performance timing routines.
31328 */
31329 /************** Include hwtime.h in the middle of os_common.h ****************/
31330 /************** Begin file hwtime.h ******************************************/
31331 /*
31332 ** 2008 May 27
31333 **
31334 ** The author disclaims copyright to this source code.  In place of
31335 ** a legal notice, here is a blessing:
31336 **
31337 **    May you do good and not evil.
31338 **    May you find forgiveness for yourself and forgive others.
31339 **    May you share freely, never taking more than you give.
31340 **
31341 ******************************************************************************
31342 **
31343 ** This file contains inline asm code for retrieving "high-performance"
31344 ** counters for x86 class CPUs.
31345 */
31346 #ifndef _HWTIME_H_
31347 #define _HWTIME_H_
31348 
31349 /*
31350 ** The following routine only works on pentium-class (or newer) processors.
31351 ** It uses the RDTSC opcode to read the cycle count value out of the
31352 ** processor and returns that value.  This can be used for high-res
31353 ** profiling.
31354 */
31355 #if (defined(__GNUC__) || defined(_MSC_VER)) && \
31356       (defined(i386) || defined(__i386__) || defined(_M_IX86))
31357 
31358   #if defined(__GNUC__)
31359 
31360   __inline__ sqlite_uint64 sqlite3Hwtime(void){
31361      unsigned int lo, hi;
31362      __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
31363      return (sqlite_uint64)hi << 32 | lo;
31364   }
31365 
31366   #elif defined(_MSC_VER)
31367 
31368   __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
31369      __asm {
31370         rdtsc
31371         ret       ; return value at EDX:EAX
31372      }
31373   }
31374 
31375   #endif
31376 
31377 #elif (defined(__GNUC__) && defined(__x86_64__))
31378 
31379   __inline__ sqlite_uint64 sqlite3Hwtime(void){
31380       unsigned long val;
31381       __asm__ __volatile__ ("rdtsc" : "=A" (val));
31382       return val;
31383   }
31384 
31385 #elif (defined(__GNUC__) && defined(__ppc__))
31386 
31387   __inline__ sqlite_uint64 sqlite3Hwtime(void){
31388       unsigned long long retval;
31389       unsigned long junk;
31390       __asm__ __volatile__ ("\n\
31391           1:      mftbu   %1\n\
31392                   mftb    %L0\n\
31393                   mftbu   %0\n\
31394                   cmpw    %0,%1\n\
31395                   bne     1b"
31396                   : "=r" (retval), "=r" (junk));
31397       return retval;
31398   }
31399 
31400 #else
31401 
31402   #error Need implementation of sqlite3Hwtime() for your platform.
31403 
31404   /*
31405   ** To compile without implementing sqlite3Hwtime() for your platform,
31406   ** you can remove the above #error and use the following
31407   ** stub function.  You will lose timing support for many
31408   ** of the debugging and testing utilities, but it should at
31409   ** least compile and run.
31410   */
31411 SQLITE_PRIVATE   sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
31412 
31413 #endif
31414 
31415 #endif /* !defined(_HWTIME_H_) */
31416 
31417 /************** End of hwtime.h **********************************************/
31418 /************** Continuing where we left off in os_common.h ******************/
31419 
31420 static sqlite_uint64 g_start;
31421 static sqlite_uint64 g_elapsed;
31422 #define TIMER_START       g_start=sqlite3Hwtime()
31423 #define TIMER_END         g_elapsed=sqlite3Hwtime()-g_start
31424 #define TIMER_ELAPSED     g_elapsed
31425 #else
31426 #define TIMER_START
31427 #define TIMER_END
31428 #define TIMER_ELAPSED     ((sqlite_uint64)0)
31429 #endif
31430 
31431 /*
31432 ** If we compile with the SQLITE_TEST macro set, then the following block
31433 ** of code will give us the ability to simulate a disk I/O error.  This
31434 ** is used for testing the I/O recovery logic.
31435 */
31436 #ifdef SQLITE_TEST
31437 SQLITE_API int sqlite3_io_error_hit = 0;            /* Total number of I/O Errors */
31438 SQLITE_API int sqlite3_io_error_hardhit = 0;        /* Number of non-benign errors */
31439 SQLITE_API int sqlite3_io_error_pending = 0;        /* Count down to first I/O error */
31440 SQLITE_API int sqlite3_io_error_persist = 0;        /* True if I/O errors persist */
31441 SQLITE_API int sqlite3_io_error_benign = 0;         /* True if errors are benign */
31442 SQLITE_API int sqlite3_diskfull_pending = 0;
31443 SQLITE_API int sqlite3_diskfull = 0;
31444 #define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X)
31445 #define SimulateIOError(CODE)  \
31446   if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \
31447        || sqlite3_io_error_pending-- == 1 )  \
31448               { local_ioerr(); CODE; }
31449 static void local_ioerr(){
31450   IOTRACE(("IOERR\n"));
31451   sqlite3_io_error_hit++;
31452   if( !sqlite3_io_error_benign ) sqlite3_io_error_hardhit++;
31453 }
31454 #define SimulateDiskfullError(CODE) \
31455    if( sqlite3_diskfull_pending ){ \
31456      if( sqlite3_diskfull_pending == 1 ){ \
31457        local_ioerr(); \
31458        sqlite3_diskfull = 1; \
31459        sqlite3_io_error_hit = 1; \
31460        CODE; \
31461      }else{ \
31462        sqlite3_diskfull_pending--; \
31463      } \
31464    }
31465 #else
31466 #define SimulateIOErrorBenign(X)
31467 #define SimulateIOError(A)
31468 #define SimulateDiskfullError(A)
31469 #endif
31470 
31471 /*
31472 ** When testing, keep a count of the number of open files.
31473 */
31474 #ifdef SQLITE_TEST
31475 SQLITE_API int sqlite3_open_file_count = 0;
31476 #define OpenCounter(X)  sqlite3_open_file_count+=(X)
31477 #else
31478 #define OpenCounter(X)
31479 #endif
31480 
31481 #endif /* !defined(_OS_COMMON_H_) */
31482 
31483 /************** End of os_common.h *******************************************/
31484 /************** Continuing where we left off in os_win.c *********************/
31485 
31486 /*
31487 ** Some microsoft compilers lack this definition.
31488 */
31489 #ifndef INVALID_FILE_ATTRIBUTES
31490 # define INVALID_FILE_ATTRIBUTES ((DWORD)-1)
31491 #endif
31492 
31493 /*
31494 ** Determine if we are dealing with WindowsCE - which has a much
31495 ** reduced API.
31496 */
31497 #if SQLITE_OS_WINCE
31498 # define AreFileApisANSI() 1
31499 # define FormatMessageW(a,b,c,d,e,f,g) 0
31500 #endif
31501 
31502 /* Forward references */
31503 typedef struct winShm winShm;           /* A connection to shared-memory */
31504 typedef struct winShmNode winShmNode;   /* A region of shared-memory */
31505 
31506 /*
31507 ** WinCE lacks native support for file locking so we have to fake it
31508 ** with some code of our own.
31509 */
31510 #if SQLITE_OS_WINCE
31511 typedef struct winceLock {
31512   int nReaders;       /* Number of reader locks obtained */
31513   BOOL bPending;      /* Indicates a pending lock has been obtained */
31514   BOOL bReserved;     /* Indicates a reserved lock has been obtained */
31515   BOOL bExclusive;    /* Indicates an exclusive lock has been obtained */
31516 } winceLock;
31517 #endif
31518 
31519 /*
31520 ** The winFile structure is a subclass of sqlite3_file* specific to the win32
31521 ** portability layer.
31522 */
31523 typedef struct winFile winFile;
31524 struct winFile {
31525   const sqlite3_io_methods *pMethod; /*** Must be first ***/
31526   sqlite3_vfs *pVfs;      /* The VFS used to open this file */
31527   HANDLE h;               /* Handle for accessing the file */
31528   unsigned char locktype; /* Type of lock currently held on this file */
31529   short sharedLockByte;   /* Randomly chosen byte used as a shared lock */
31530   DWORD lastErrno;        /* The Windows errno from the last I/O error */
31531   DWORD sectorSize;       /* Sector size of the device file is on */
31532   winShm *pShm;           /* Instance of shared memory on this file */
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   WCHAR *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 };
31543 
31544 
31545 /*
31546 ** Forward prototypes.
31547 */
31548 static int getSectorSize(
31549     sqlite3_vfs *pVfs,
31550     const char *zRelative     /* UTF-8 file name */
31551 );
31552 
31553 /*
31554 ** The following variable is (normally) set once and never changes
31555 ** thereafter.  It records whether the operating system is Win95
31556 ** or WinNT.
31557 **
31558 ** 0:   Operating system unknown.
31559 ** 1:   Operating system is Win95.
31560 ** 2:   Operating system is WinNT.
31561 **
31562 ** In order to facilitate testing on a WinNT system, the test fixture
31563 ** can manually set this value to 1 to emulate Win98 behavior.
31564 */
31565 #ifdef SQLITE_TEST
31566 SQLITE_API int sqlite3_os_type = 0;
31567 #else
31568 static int sqlite3_os_type = 0;
31569 #endif
31570 
31571 /*
31572 ** Return true (non-zero) if we are running under WinNT, Win2K, WinXP,
31573 ** or WinCE.  Return false (zero) for Win95, Win98, or WinME.
31574 **
31575 ** Here is an interesting observation:  Win95, Win98, and WinME lack
31576 ** the LockFileEx() API.  But we can still statically link against that
31577 ** API as long as we don't call it when running Win95/98/ME.  A call to
31578 ** this routine is used to determine if the host is Win95/98/ME or
31579 ** WinNT/2K/XP so that we will know whether or not we can safely call
31580 ** the LockFileEx() API.
31581 */
31582 #if SQLITE_OS_WINCE
31583 # define isNT()  (1)
31584 #else
31585   static int isNT(void){
31586     if( sqlite3_os_type==0 ){
31587       OSVERSIONINFO sInfo;
31588       sInfo.dwOSVersionInfoSize = sizeof(sInfo);
31589       GetVersionEx(&sInfo);
31590       sqlite3_os_type = sInfo.dwPlatformId==VER_PLATFORM_WIN32_NT ? 2 : 1;
31591     }
31592     return sqlite3_os_type==2;
31593   }
31594 #endif /* SQLITE_OS_WINCE */
31595 
31596 /*
31597 ** Convert a UTF-8 string to microsoft unicode (UTF-16?).
31598 **
31599 ** Space to hold the returned string is obtained from malloc.
31600 */
31601 static WCHAR *utf8ToUnicode(const char *zFilename){
31602   int nChar;
31603   WCHAR *zWideFilename;
31604 
31605   nChar = MultiByteToWideChar(CP_UTF8, 0, zFilename, -1, NULL, 0);
31606   zWideFilename = malloc( nChar*sizeof(zWideFilename[0]) );
31607   if( zWideFilename==0 ){
31608     return 0;
31609   }
31610   nChar = MultiByteToWideChar(CP_UTF8, 0, zFilename, -1, zWideFilename, nChar);
31611   if( nChar==0 ){
31612     free(zWideFilename);
31613     zWideFilename = 0;
31614   }
31615   return zWideFilename;
31616 }
31617 
31618 /*
31619 ** Convert microsoft unicode to UTF-8.  Space to hold the returned string is
31620 ** obtained from malloc().
31621 */
31622 static char *unicodeToUtf8(const WCHAR *zWideFilename){
31623   int nByte;
31624   char *zFilename;
31625 
31626   nByte = WideCharToMultiByte(CP_UTF8, 0, zWideFilename, -1, 0, 0, 0, 0);
31627   zFilename = malloc( nByte );
31628   if( zFilename==0 ){
31629     return 0;
31630   }
31631   nByte = WideCharToMultiByte(CP_UTF8, 0, zWideFilename, -1, zFilename, nByte,
31632                               0, 0);
31633   if( nByte == 0 ){
31634     free(zFilename);
31635     zFilename = 0;
31636   }
31637   return zFilename;
31638 }
31639 
31640 /*
31641 ** Convert an ansi string to microsoft unicode, based on the
31642 ** current codepage settings for file apis.
31643 **
31644 ** Space to hold the returned string is obtained
31645 ** from malloc.
31646 */
31647 static WCHAR *mbcsToUnicode(const char *zFilename){
31648   int nByte;
31649   WCHAR *zMbcsFilename;
31650   int codepage = AreFileApisANSI() ? CP_ACP : CP_OEMCP;
31651 
31652   nByte = MultiByteToWideChar(codepage, 0, zFilename, -1, NULL,0)*sizeof(WCHAR);
31653   zMbcsFilename = malloc( nByte*sizeof(zMbcsFilename[0]) );
31654   if( zMbcsFilename==0 ){
31655     return 0;
31656   }
31657   nByte = MultiByteToWideChar(codepage, 0, zFilename, -1, zMbcsFilename, nByte);
31658   if( nByte==0 ){
31659     free(zMbcsFilename);
31660     zMbcsFilename = 0;
31661   }
31662   return zMbcsFilename;
31663 }
31664 
31665 /*
31666 ** Convert microsoft unicode to multibyte character string, based on the
31667 ** user's Ansi codepage.
31668 **
31669 ** Space to hold the returned string is obtained from
31670 ** malloc().
31671 */
31672 static char *unicodeToMbcs(const WCHAR *zWideFilename){
31673   int nByte;
31674   char *zFilename;
31675   int codepage = AreFileApisANSI() ? CP_ACP : CP_OEMCP;
31676 
31677   nByte = WideCharToMultiByte(codepage, 0, zWideFilename, -1, 0, 0, 0, 0);
31678   zFilename = malloc( nByte );
31679   if( zFilename==0 ){
31680     return 0;
31681   }
31682   nByte = WideCharToMultiByte(codepage, 0, zWideFilename, -1, zFilename, nByte,
31683                               0, 0);
31684   if( nByte == 0 ){
31685     free(zFilename);
31686     zFilename = 0;
31687   }
31688   return zFilename;
31689 }
31690 
31691 /*
31692 ** Convert multibyte character string to UTF-8.  Space to hold the
31693 ** returned string is obtained from malloc().
31694 */
31695 SQLITE_API char *sqlite3_win32_mbcs_to_utf8(const char *zFilename){
31696   char *zFilenameUtf8;
31697   WCHAR *zTmpWide;
31698 
31699   zTmpWide = mbcsToUnicode(zFilename);
31700   if( zTmpWide==0 ){
31701     return 0;
31702   }
31703   zFilenameUtf8 = unicodeToUtf8(zTmpWide);
31704   free(zTmpWide);
31705   return zFilenameUtf8;
31706 }
31707 
31708 /*
31709 ** Convert UTF-8 to multibyte character string.  Space to hold the
31710 ** returned string is obtained from malloc().
31711 */
31712 SQLITE_API char *sqlite3_win32_utf8_to_mbcs(const char *zFilename){
31713   char *zFilenameMbcs;
31714   WCHAR *zTmpWide;
31715 
31716   zTmpWide = utf8ToUnicode(zFilename);
31717   if( zTmpWide==0 ){
31718     return 0;
31719   }
31720   zFilenameMbcs = unicodeToMbcs(zTmpWide);
31721   free(zTmpWide);
31722   return zFilenameMbcs;
31723 }
31724 
31725 
31726 /*
31727 ** The return value of getLastErrorMsg
31728 ** is zero if the error message fits in the buffer, or non-zero
31729 ** otherwise (if the message was truncated).
31730 */
31731 static int getLastErrorMsg(int nBuf, char *zBuf){
31732   /* FormatMessage returns 0 on failure.  Otherwise it
31733   ** returns the number of TCHARs written to the output
31734   ** buffer, excluding the terminating null char.
31735   */
31736   DWORD error = GetLastError();
31737   DWORD dwLen = 0;
31738   char *zOut = 0;
31739 
31740   if( isNT() ){
31741     WCHAR *zTempWide = NULL;
31742     dwLen = FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
31743                            NULL,
31744                            error,
31745                            0,
31746                            (LPWSTR) &zTempWide,
31747                            0,
31748                            0);
31749     if( dwLen > 0 ){
31750       /* allocate a buffer and convert to UTF8 */
31751       zOut = unicodeToUtf8(zTempWide);
31752       /* free the system buffer allocated by FormatMessage */
31753       LocalFree(zTempWide);
31754     }
31755 /* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed.
31756 ** Since the ASCII version of these Windows API do not exist for WINCE,
31757 ** it's important to not reference them for WINCE builds.
31758 */
31759 #if SQLITE_OS_WINCE==0
31760   }else{
31761     char *zTemp = NULL;
31762     dwLen = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
31763                            NULL,
31764                            error,
31765                            0,
31766                            (LPSTR) &zTemp,
31767                            0,
31768                            0);
31769     if( dwLen > 0 ){
31770       /* allocate a buffer and convert to UTF8 */
31771       zOut = sqlite3_win32_mbcs_to_utf8(zTemp);
31772       /* free the system buffer allocated by FormatMessage */
31773       LocalFree(zTemp);
31774     }
31775 #endif
31776   }
31777   if( 0 == dwLen ){
31778     sqlite3_snprintf(nBuf, zBuf, "OsError 0x%x (%u)", error, error);
31779   }else{
31780     /* copy a maximum of nBuf chars to output buffer */
31781     sqlite3_snprintf(nBuf, zBuf, "%s", zOut);
31782     /* free the UTF8 buffer */
31783     free(zOut);
31784   }
31785   return 0;
31786 }
31787 
31788 /*
31789 **
31790 ** This function - winLogErrorAtLine() - is only ever called via the macro
31791 ** winLogError().
31792 **
31793 ** This routine is invoked after an error occurs in an OS function.
31794 ** It logs a message using sqlite3_log() containing the current value of
31795 ** error code and, if possible, the human-readable equivalent from
31796 ** FormatMessage.
31797 **
31798 ** The first argument passed to the macro should be the error code that
31799 ** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN).
31800 ** The two subsequent arguments should be the name of the OS function that
31801 ** failed and the the associated file-system path, if any.
31802 */
31803 #define winLogError(a,b,c)     winLogErrorAtLine(a,b,c,__LINE__)
31804 static int winLogErrorAtLine(
31805   int errcode,                    /* SQLite error code */
31806   const char *zFunc,              /* Name of OS function that failed */
31807   const char *zPath,              /* File path associated with error */
31808   int iLine                       /* Source line number where error occurred */
31809 ){
31810   char zMsg[500];                 /* Human readable error text */
31811   int i;                          /* Loop counter */
31812   DWORD iErrno = GetLastError();  /* Error code */
31813 
31814   zMsg[0] = 0;
31815   getLastErrorMsg(sizeof(zMsg), zMsg);
31816   assert( errcode!=SQLITE_OK );
31817   if( zPath==0 ) zPath = "";
31818   for(i=0; zMsg[i] && zMsg[i]!='\r' && zMsg[i]!='\n'; i++){}
31819   zMsg[i] = 0;
31820   sqlite3_log(errcode,
31821       "os_win.c:%d: (%d) %s(%s) - %s",
31822       iLine, iErrno, zFunc, zPath, zMsg
31823   );
31824 
31825   return errcode;
31826 }
31827 
31828 #if SQLITE_OS_WINCE
31829 /*************************************************************************
31830 ** This section contains code for WinCE only.
31831 */
31832 /*
31833 ** WindowsCE does not have a localtime() function.  So create a
31834 ** substitute.
31835 */
31836 struct tm *__cdecl localtime(const time_t *t)
31837 {
31838   static struct tm y;
31839   FILETIME uTm, lTm;
31840   SYSTEMTIME pTm;
31841   sqlite3_int64 t64;
31842   t64 = *t;
31843   t64 = (t64 + 11644473600)*10000000;
31844   uTm.dwLowDateTime = (DWORD)(t64 & 0xFFFFFFFF);
31845   uTm.dwHighDateTime= (DWORD)(t64 >> 32);
31846   FileTimeToLocalFileTime(&uTm,&lTm);
31847   FileTimeToSystemTime(&lTm,&pTm);
31848   y.tm_year = pTm.wYear - 1900;
31849   y.tm_mon = pTm.wMonth - 1;
31850   y.tm_wday = pTm.wDayOfWeek;
31851   y.tm_mday = pTm.wDay;
31852   y.tm_hour = pTm.wHour;
31853   y.tm_min = pTm.wMinute;
31854   y.tm_sec = pTm.wSecond;
31855   return &y;
31856 }
31857 
31858 /* This will never be called, but defined to make the code compile */
31859 #define GetTempPathA(a,b)
31860 
31861 #define LockFile(a,b,c,d,e)       winceLockFile(&a, b, c, d, e)
31862 #define UnlockFile(a,b,c,d,e)     winceUnlockFile(&a, b, c, d, e)
31863 #define LockFileEx(a,b,c,d,e,f)   winceLockFileEx(&a, b, c, d, e, f)
31864 
31865 #define HANDLE_TO_WINFILE(a) (winFile*)&((char*)a)[-(int)offsetof(winFile,h)]
31866 
31867 /*
31868 ** Acquire a lock on the handle h
31869 */
31870 static void winceMutexAcquire(HANDLE h){
31871    DWORD dwErr;
31872    do {
31873      dwErr = WaitForSingleObject(h, INFINITE);
31874    } while (dwErr != WAIT_OBJECT_0 && dwErr != WAIT_ABANDONED);
31875 }
31876 /*
31877 ** Release a lock acquired by winceMutexAcquire()
31878 */
31879 #define winceMutexRelease(h) ReleaseMutex(h)
31880 
31881 /*
31882 ** Create the mutex and shared memory used for locking in the file
31883 ** descriptor pFile
31884 */
31885 static BOOL winceCreateLock(const char *zFilename, winFile *pFile){
31886   WCHAR *zTok;
31887   WCHAR *zName = utf8ToUnicode(zFilename);
31888   BOOL bInit = TRUE;
31889 
31890   /* Initialize the local lockdata */
31891   ZeroMemory(&pFile->local, sizeof(pFile->local));
31892 
31893   /* Replace the backslashes from the filename and lowercase it
31894   ** to derive a mutex name. */
31895   zTok = CharLowerW(zName);
31896   for (;*zTok;zTok++){
31897     if (*zTok == '\\') *zTok = '_';
31898   }
31899 
31900   /* Create/open the named mutex */
31901   pFile->hMutex = CreateMutexW(NULL, FALSE, zName);
31902   if (!pFile->hMutex){
31903     pFile->lastErrno = GetLastError();
31904     winLogError(SQLITE_ERROR, "winceCreateLock1", zFilename);
31905     free(zName);
31906     return FALSE;
31907   }
31908 
31909   /* Acquire the mutex before continuing */
31910   winceMutexAcquire(pFile->hMutex);
31911 
31912   /* Since the names of named mutexes, semaphores, file mappings etc are
31913   ** case-sensitive, take advantage of that by uppercasing the mutex name
31914   ** and using that as the shared filemapping name.
31915   */
31916   CharUpperW(zName);
31917   pFile->hShared = CreateFileMappingW(INVALID_HANDLE_VALUE, NULL,
31918                                        PAGE_READWRITE, 0, sizeof(winceLock),
31919                                        zName);
31920 
31921   /* Set a flag that indicates we're the first to create the memory so it
31922   ** must be zero-initialized */
31923   if (GetLastError() == ERROR_ALREADY_EXISTS){
31924     bInit = FALSE;
31925   }
31926 
31927   free(zName);
31928 
31929   /* If we succeeded in making the shared memory handle, map it. */
31930   if (pFile->hShared){
31931     pFile->shared = (winceLock*)MapViewOfFile(pFile->hShared,
31932              FILE_MAP_READ|FILE_MAP_WRITE, 0, 0, sizeof(winceLock));
31933     /* If mapping failed, close the shared memory handle and erase it */
31934     if (!pFile->shared){
31935       pFile->lastErrno = GetLastError();
31936       winLogError(SQLITE_ERROR, "winceCreateLock2", zFilename);
31937       CloseHandle(pFile->hShared);
31938       pFile->hShared = NULL;
31939     }
31940   }
31941 
31942   /* If shared memory could not be created, then close the mutex and fail */
31943   if (pFile->hShared == NULL){
31944     winceMutexRelease(pFile->hMutex);
31945     CloseHandle(pFile->hMutex);
31946     pFile->hMutex = NULL;
31947     return FALSE;
31948   }
31949 
31950   /* Initialize the shared memory if we're supposed to */
31951   if (bInit) {
31952     ZeroMemory(pFile->shared, sizeof(winceLock));
31953   }
31954 
31955   winceMutexRelease(pFile->hMutex);
31956   return TRUE;
31957 }
31958 
31959 /*
31960 ** Destroy the part of winFile that deals with wince locks
31961 */
31962 static void winceDestroyLock(winFile *pFile){
31963   if (pFile->hMutex){
31964     /* Acquire the mutex */
31965     winceMutexAcquire(pFile->hMutex);
31966 
31967     /* The following blocks should probably assert in debug mode, but they
31968        are to cleanup in case any locks remained open */
31969     if (pFile->local.nReaders){
31970       pFile->shared->nReaders --;
31971     }
31972     if (pFile->local.bReserved){
31973       pFile->shared->bReserved = FALSE;
31974     }
31975     if (pFile->local.bPending){
31976       pFile->shared->bPending = FALSE;
31977     }
31978     if (pFile->local.bExclusive){
31979       pFile->shared->bExclusive = FALSE;
31980     }
31981 
31982     /* De-reference and close our copy of the shared memory handle */
31983     UnmapViewOfFile(pFile->shared);
31984     CloseHandle(pFile->hShared);
31985 
31986     /* Done with the mutex */
31987     winceMutexRelease(pFile->hMutex);
31988     CloseHandle(pFile->hMutex);
31989     pFile->hMutex = NULL;
31990   }
31991 }
31992 
31993 /*
31994 ** An implementation of the LockFile() API of windows for wince
31995 */
31996 static BOOL winceLockFile(
31997   HANDLE *phFile,
31998   DWORD dwFileOffsetLow,
31999   DWORD dwFileOffsetHigh,
32000   DWORD nNumberOfBytesToLockLow,
32001   DWORD nNumberOfBytesToLockHigh
32002 ){
32003   winFile *pFile = HANDLE_TO_WINFILE(phFile);
32004   BOOL bReturn = FALSE;
32005 
32006   UNUSED_PARAMETER(dwFileOffsetHigh);
32007   UNUSED_PARAMETER(nNumberOfBytesToLockHigh);
32008 
32009   if (!pFile->hMutex) return TRUE;
32010   winceMutexAcquire(pFile->hMutex);
32011 
32012   /* Wanting an exclusive lock? */
32013   if (dwFileOffsetLow == (DWORD)SHARED_FIRST
32014        && nNumberOfBytesToLockLow == (DWORD)SHARED_SIZE){
32015     if (pFile->shared->nReaders == 0 && pFile->shared->bExclusive == 0){
32016        pFile->shared->bExclusive = TRUE;
32017        pFile->local.bExclusive = TRUE;
32018        bReturn = TRUE;
32019     }
32020   }
32021 
32022   /* Want a read-only lock? */
32023   else if (dwFileOffsetLow == (DWORD)SHARED_FIRST &&
32024            nNumberOfBytesToLockLow == 1){
32025     if (pFile->shared->bExclusive == 0){
32026       pFile->local.nReaders ++;
32027       if (pFile->local.nReaders == 1){
32028         pFile->shared->nReaders ++;
32029       }
32030       bReturn = TRUE;
32031     }
32032   }
32033 
32034   /* Want a pending lock? */
32035   else if (dwFileOffsetLow == (DWORD)PENDING_BYTE && nNumberOfBytesToLockLow == 1){
32036     /* If no pending lock has been acquired, then acquire it */
32037     if (pFile->shared->bPending == 0) {
32038       pFile->shared->bPending = TRUE;
32039       pFile->local.bPending = TRUE;
32040       bReturn = TRUE;
32041     }
32042   }
32043 
32044   /* Want a reserved lock? */
32045   else if (dwFileOffsetLow == (DWORD)RESERVED_BYTE && nNumberOfBytesToLockLow == 1){
32046     if (pFile->shared->bReserved == 0) {
32047       pFile->shared->bReserved = TRUE;
32048       pFile->local.bReserved = TRUE;
32049       bReturn = TRUE;
32050     }
32051   }
32052 
32053   winceMutexRelease(pFile->hMutex);
32054   return bReturn;
32055 }
32056 
32057 /*
32058 ** An implementation of the UnlockFile API of windows for wince
32059 */
32060 static BOOL winceUnlockFile(
32061   HANDLE *phFile,
32062   DWORD dwFileOffsetLow,
32063   DWORD dwFileOffsetHigh,
32064   DWORD nNumberOfBytesToUnlockLow,
32065   DWORD nNumberOfBytesToUnlockHigh
32066 ){
32067   winFile *pFile = HANDLE_TO_WINFILE(phFile);
32068   BOOL bReturn = FALSE;
32069 
32070   UNUSED_PARAMETER(dwFileOffsetHigh);
32071   UNUSED_PARAMETER(nNumberOfBytesToUnlockHigh);
32072 
32073   if (!pFile->hMutex) return TRUE;
32074   winceMutexAcquire(pFile->hMutex);
32075 
32076   /* Releasing a reader lock or an exclusive lock */
32077   if (dwFileOffsetLow == (DWORD)SHARED_FIRST){
32078     /* Did we have an exclusive lock? */
32079     if (pFile->local.bExclusive){
32080       assert(nNumberOfBytesToUnlockLow == (DWORD)SHARED_SIZE);
32081       pFile->local.bExclusive = FALSE;
32082       pFile->shared->bExclusive = FALSE;
32083       bReturn = TRUE;
32084     }
32085 
32086     /* Did we just have a reader lock? */
32087     else if (pFile->local.nReaders){
32088       assert(nNumberOfBytesToUnlockLow == (DWORD)SHARED_SIZE || nNumberOfBytesToUnlockLow == 1);
32089       pFile->local.nReaders --;
32090       if (pFile->local.nReaders == 0)
32091       {
32092         pFile->shared->nReaders --;
32093       }
32094       bReturn = TRUE;
32095     }
32096   }
32097 
32098   /* Releasing a pending lock */
32099   else if (dwFileOffsetLow == (DWORD)PENDING_BYTE && nNumberOfBytesToUnlockLow == 1){
32100     if (pFile->local.bPending){
32101       pFile->local.bPending = FALSE;
32102       pFile->shared->bPending = FALSE;
32103       bReturn = TRUE;
32104     }
32105   }
32106   /* Releasing a reserved lock */
32107   else if (dwFileOffsetLow == (DWORD)RESERVED_BYTE && nNumberOfBytesToUnlockLow == 1){
32108     if (pFile->local.bReserved) {
32109       pFile->local.bReserved = FALSE;
32110       pFile->shared->bReserved = FALSE;
32111       bReturn = TRUE;
32112     }
32113   }
32114 
32115   winceMutexRelease(pFile->hMutex);
32116   return bReturn;
32117 }
32118 
32119 /*
32120 ** An implementation of the LockFileEx() API of windows for wince
32121 */
32122 static BOOL winceLockFileEx(
32123   HANDLE *phFile,
32124   DWORD dwFlags,
32125   DWORD dwReserved,
32126   DWORD nNumberOfBytesToLockLow,
32127   DWORD nNumberOfBytesToLockHigh,
32128   LPOVERLAPPED lpOverlapped
32129 ){
32130   UNUSED_PARAMETER(dwReserved);
32131   UNUSED_PARAMETER(nNumberOfBytesToLockHigh);
32132 
32133   /* If the caller wants a shared read lock, forward this call
32134   ** to winceLockFile */
32135   if (lpOverlapped->Offset == (DWORD)SHARED_FIRST &&
32136       dwFlags == 1 &&
32137       nNumberOfBytesToLockLow == (DWORD)SHARED_SIZE){
32138     return winceLockFile(phFile, SHARED_FIRST, 0, 1, 0);
32139   }
32140   return FALSE;
32141 }
32142 /*
32143 ** End of the special code for wince
32144 *****************************************************************************/
32145 #endif /* SQLITE_OS_WINCE */
32146 
32147 /*****************************************************************************
32148 ** The next group of routines implement the I/O methods specified
32149 ** by the sqlite3_io_methods object.
32150 ******************************************************************************/
32151 
32152 /*
32153 ** Some microsoft compilers lack this definition.
32154 */
32155 #ifndef INVALID_SET_FILE_POINTER
32156 # define INVALID_SET_FILE_POINTER ((DWORD)-1)
32157 #endif
32158 
32159 /*
32160 ** Move the current position of the file handle passed as the first
32161 ** argument to offset iOffset within the file. If successful, return 0.
32162 ** Otherwise, set pFile->lastErrno and return non-zero.
32163 */
32164 static int seekWinFile(winFile *pFile, sqlite3_int64 iOffset){
32165   LONG upperBits;                 /* Most sig. 32 bits of new offset */
32166   LONG lowerBits;                 /* Least sig. 32 bits of new offset */
32167   DWORD dwRet;                    /* Value returned by SetFilePointer() */
32168 
32169   upperBits = (LONG)((iOffset>>32) & 0x7fffffff);
32170   lowerBits = (LONG)(iOffset & 0xffffffff);
32171 
32172   /* API oddity: If successful, SetFilePointer() returns a dword
32173   ** containing the lower 32-bits of the new file-offset. Or, if it fails,
32174   ** it returns INVALID_SET_FILE_POINTER. However according to MSDN,
32175   ** INVALID_SET_FILE_POINTER may also be a valid new offset. So to determine
32176   ** whether an error has actually occured, it is also necessary to call
32177   ** GetLastError().
32178   */
32179   dwRet = SetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN);
32180   if( (dwRet==INVALID_SET_FILE_POINTER && GetLastError()!=NO_ERROR) ){
32181     pFile->lastErrno = GetLastError();
32182     winLogError(SQLITE_IOERR_SEEK, "seekWinFile", pFile->zPath);
32183     return 1;
32184   }
32185 
32186   return 0;
32187 }
32188 
32189 /*
32190 ** Close a file.
32191 **
32192 ** It is reported that an attempt to close a handle might sometimes
32193 ** fail.  This is a very unreasonable result, but windows is notorious
32194 ** for being unreasonable so I do not doubt that it might happen.  If
32195 ** the close fails, we pause for 100 milliseconds and try again.  As
32196 ** many as MX_CLOSE_ATTEMPT attempts to close the handle are made before
32197 ** giving up and returning an error.
32198 */
32199 #define MX_CLOSE_ATTEMPT 3
32200 static int winClose(sqlite3_file *id){
32201   int rc, cnt = 0;
32202   winFile *pFile = (winFile*)id;
32203 
32204   assert( id!=0 );
32205   assert( pFile->pShm==0 );
32206   OSTRACE(("CLOSE %d\n", pFile->h));
32207   do{
32208     rc = CloseHandle(pFile->h);
32209     /* SimulateIOError( rc=0; cnt=MX_CLOSE_ATTEMPT; ); */
32210   }while( rc==0 && ++cnt < MX_CLOSE_ATTEMPT && (Sleep(100), 1) );
32211 #if SQLITE_OS_WINCE
32212 #define WINCE_DELETION_ATTEMPTS 3
32213   winceDestroyLock(pFile);
32214   if( pFile->zDeleteOnClose ){
32215     int cnt = 0;
32216     while(
32217            DeleteFileW(pFile->zDeleteOnClose)==0
32218         && GetFileAttributesW(pFile->zDeleteOnClose)!=0xffffffff
32219         && cnt++ < WINCE_DELETION_ATTEMPTS
32220     ){
32221        Sleep(100);  /* Wait a little before trying again */
32222     }
32223     free(pFile->zDeleteOnClose);
32224   }
32225 #endif
32226   OSTRACE(("CLOSE %d %s\n", pFile->h, rc ? "ok" : "failed"));
32227   OpenCounter(-1);
32228   return rc ? SQLITE_OK
32229             : winLogError(SQLITE_IOERR_CLOSE, "winClose", pFile->zPath);
32230 }
32231 
32232 /*
32233 ** Read data from a file into a buffer.  Return SQLITE_OK if all
32234 ** bytes were read successfully and SQLITE_IOERR if anything goes
32235 ** wrong.
32236 */
32237 static int winRead(
32238   sqlite3_file *id,          /* File to read from */
32239   void *pBuf,                /* Write content into this buffer */
32240   int amt,                   /* Number of bytes to read */
32241   sqlite3_int64 offset       /* Begin reading at this offset */
32242 ){
32243   winFile *pFile = (winFile*)id;  /* file handle */
32244   DWORD nRead;                    /* Number of bytes actually read from file */
32245 
32246   assert( id!=0 );
32247   SimulateIOError(return SQLITE_IOERR_READ);
32248   OSTRACE(("READ %d lock=%d\n", pFile->h, pFile->locktype));
32249 
32250   if( seekWinFile(pFile, offset) ){
32251     return SQLITE_FULL;
32252   }
32253   if( !ReadFile(pFile->h, pBuf, amt, &nRead, 0) ){
32254     pFile->lastErrno = GetLastError();
32255     return winLogError(SQLITE_IOERR_READ, "winRead", pFile->zPath);
32256   }
32257   if( nRead<(DWORD)amt ){
32258     /* Unread parts of the buffer must be zero-filled */
32259     memset(&((char*)pBuf)[nRead], 0, amt-nRead);
32260     return SQLITE_IOERR_SHORT_READ;
32261   }
32262 
32263   return SQLITE_OK;
32264 }
32265 
32266 /*
32267 ** Write data from a buffer into a file.  Return SQLITE_OK on success
32268 ** or some other error code on failure.
32269 */
32270 static int winWrite(
32271   sqlite3_file *id,               /* File to write into */
32272   const void *pBuf,               /* The bytes to be written */
32273   int amt,                        /* Number of bytes to write */
32274   sqlite3_int64 offset            /* Offset into the file to begin writing at */
32275 ){
32276   int rc;                         /* True if error has occured, else false */
32277   winFile *pFile = (winFile*)id;  /* File handle */
32278 
32279   assert( amt>0 );
32280   assert( pFile );
32281   SimulateIOError(return SQLITE_IOERR_WRITE);
32282   SimulateDiskfullError(return SQLITE_FULL);
32283 
32284   OSTRACE(("WRITE %d lock=%d\n", pFile->h, pFile->locktype));
32285 
32286   rc = seekWinFile(pFile, offset);
32287   if( rc==0 ){
32288     u8 *aRem = (u8 *)pBuf;        /* Data yet to be written */
32289     int nRem = amt;               /* Number of bytes yet to be written */
32290     DWORD nWrite;                 /* Bytes written by each WriteFile() call */
32291 
32292     while( nRem>0 && WriteFile(pFile->h, aRem, nRem, &nWrite, 0) && nWrite>0 ){
32293       aRem += nWrite;
32294       nRem -= nWrite;
32295     }
32296     if( nRem>0 ){
32297       pFile->lastErrno = GetLastError();
32298       rc = 1;
32299     }
32300   }
32301 
32302   if( rc ){
32303     if(   ( pFile->lastErrno==ERROR_HANDLE_DISK_FULL )
32304        || ( pFile->lastErrno==ERROR_DISK_FULL )){
32305       return SQLITE_FULL;
32306     }
32307     return winLogError(SQLITE_IOERR_WRITE, "winWrite", pFile->zPath);
32308   }
32309   return SQLITE_OK;
32310 }
32311 
32312 /*
32313 ** Truncate an open file to a specified size
32314 */
32315 static int winTruncate(sqlite3_file *id, sqlite3_int64 nByte){
32316   winFile *pFile = (winFile*)id;  /* File handle object */
32317   int rc = SQLITE_OK;             /* Return code for this function */
32318 
32319   assert( pFile );
32320 
32321   OSTRACE(("TRUNCATE %d %lld\n", pFile->h, nByte));
32322   SimulateIOError(return SQLITE_IOERR_TRUNCATE);
32323 
32324   /* If the user has configured a chunk-size for this file, truncate the
32325   ** file so that it consists of an integer number of chunks (i.e. the
32326   ** actual file size after the operation may be larger than the requested
32327   ** size).
32328   */
32329   if( pFile->szChunk ){
32330     nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
32331   }
32332 
32333   /* SetEndOfFile() returns non-zero when successful, or zero when it fails. */
32334   if( seekWinFile(pFile, nByte) ){
32335     rc = winLogError(SQLITE_IOERR_TRUNCATE, "winTruncate1", pFile->zPath);
32336   }else if( 0==SetEndOfFile(pFile->h) ){
32337     pFile->lastErrno = GetLastError();
32338     rc = winLogError(SQLITE_IOERR_TRUNCATE, "winTruncate2", pFile->zPath);
32339   }
32340 
32341   OSTRACE(("TRUNCATE %d %lld %s\n", pFile->h, nByte, rc ? "failed" : "ok"));
32342   return rc;
32343 }
32344 
32345 #ifdef SQLITE_TEST
32346 /*
32347 ** Count the number of fullsyncs and normal syncs.  This is used to test
32348 ** that syncs and fullsyncs are occuring at the right times.
32349 */
32350 SQLITE_API int sqlite3_sync_count = 0;
32351 SQLITE_API int sqlite3_fullsync_count = 0;
32352 #endif
32353 
32354 /*
32355 ** Make sure all writes to a particular file are committed to disk.
32356 */
32357 static int winSync(sqlite3_file *id, int flags){
32358 #if !defined(NDEBUG) || !defined(SQLITE_NO_SYNC) || defined(SQLITE_DEBUG)
32359   winFile *pFile = (winFile*)id;
32360   BOOL rc;
32361 #else
32362   UNUSED_PARAMETER(id);
32363 #endif
32364 
32365   assert( pFile );
32366   /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */
32367   assert((flags&0x0F)==SQLITE_SYNC_NORMAL
32368       || (flags&0x0F)==SQLITE_SYNC_FULL
32369   );
32370 
32371   OSTRACE(("SYNC %d lock=%d\n", pFile->h, pFile->locktype));
32372 
32373   /* Unix cannot, but some systems may return SQLITE_FULL from here. This
32374   ** line is to test that doing so does not cause any problems.
32375   */
32376   SimulateDiskfullError( return SQLITE_FULL );
32377 
32378 #ifndef SQLITE_TEST
32379   UNUSED_PARAMETER(flags);
32380 #else
32381   if( (flags&0x0F)==SQLITE_SYNC_FULL ){
32382     sqlite3_fullsync_count++;
32383   }
32384   sqlite3_sync_count++;
32385 #endif
32386 
32387   /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
32388   ** no-op
32389   */
32390 #ifdef SQLITE_NO_SYNC
32391   return SQLITE_OK;
32392 #else
32393   rc = FlushFileBuffers(pFile->h);
32394   SimulateIOError( rc=FALSE );
32395   if( rc ){
32396     return SQLITE_OK;
32397   }else{
32398     pFile->lastErrno = GetLastError();
32399     return winLogError(SQLITE_IOERR_FSYNC, "winSync", pFile->zPath);
32400   }
32401 #endif
32402 }
32403 
32404 /*
32405 ** Determine the current size of a file in bytes
32406 */
32407 static int winFileSize(sqlite3_file *id, sqlite3_int64 *pSize){
32408   DWORD upperBits;
32409   DWORD lowerBits;
32410   winFile *pFile = (winFile*)id;
32411   DWORD error;
32412 
32413   assert( id!=0 );
32414   SimulateIOError(return SQLITE_IOERR_FSTAT);
32415   lowerBits = GetFileSize(pFile->h, &upperBits);
32416   if(   (lowerBits == INVALID_FILE_SIZE)
32417      && ((error = GetLastError()) != NO_ERROR) )
32418   {
32419     pFile->lastErrno = error;
32420     return winLogError(SQLITE_IOERR_FSTAT, "winFileSize", pFile->zPath);
32421   }
32422   *pSize = (((sqlite3_int64)upperBits)<<32) + lowerBits;
32423   return SQLITE_OK;
32424 }
32425 
32426 /*
32427 ** LOCKFILE_FAIL_IMMEDIATELY is undefined on some Windows systems.
32428 */
32429 #ifndef LOCKFILE_FAIL_IMMEDIATELY
32430 # define LOCKFILE_FAIL_IMMEDIATELY 1
32431 #endif
32432 
32433 /*
32434 ** Acquire a reader lock.
32435 ** Different API routines are called depending on whether or not this
32436 ** is Win95 or WinNT.
32437 */
32438 static int getReadLock(winFile *pFile){
32439   int res;
32440   if( isNT() ){
32441     OVERLAPPED ovlp;
32442     ovlp.Offset = SHARED_FIRST;
32443     ovlp.OffsetHigh = 0;
32444     ovlp.hEvent = 0;
32445     res = LockFileEx(pFile->h, LOCKFILE_FAIL_IMMEDIATELY,
32446                      0, SHARED_SIZE, 0, &ovlp);
32447 /* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed.
32448 */
32449 #if SQLITE_OS_WINCE==0
32450   }else{
32451     int lk;
32452     sqlite3_randomness(sizeof(lk), &lk);
32453     pFile->sharedLockByte = (short)((lk & 0x7fffffff)%(SHARED_SIZE - 1));
32454     res = LockFile(pFile->h, SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0);
32455 #endif
32456   }
32457   if( res == 0 ){
32458     pFile->lastErrno = GetLastError();
32459     /* No need to log a failure to lock */
32460   }
32461   return res;
32462 }
32463 
32464 /*
32465 ** Undo a readlock
32466 */
32467 static int unlockReadLock(winFile *pFile){
32468   int res;
32469   if( isNT() ){
32470     res = UnlockFile(pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
32471 /* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed.
32472 */
32473 #if SQLITE_OS_WINCE==0
32474   }else{
32475     res = UnlockFile(pFile->h, SHARED_FIRST + pFile->sharedLockByte, 0, 1, 0);
32476 #endif
32477   }
32478   if( res==0 && GetLastError()!=ERROR_NOT_LOCKED ){
32479     pFile->lastErrno = GetLastError();
32480     winLogError(SQLITE_IOERR_UNLOCK, "unlockReadLock", pFile->zPath);
32481   }
32482   return res;
32483 }
32484 
32485 /*
32486 ** Lock the file with the lock specified by parameter locktype - one
32487 ** of the following:
32488 **
32489 **     (1) SHARED_LOCK
32490 **     (2) RESERVED_LOCK
32491 **     (3) PENDING_LOCK
32492 **     (4) EXCLUSIVE_LOCK
32493 **
32494 ** Sometimes when requesting one lock state, additional lock states
32495 ** are inserted in between.  The locking might fail on one of the later
32496 ** transitions leaving the lock state different from what it started but
32497 ** still short of its goal.  The following chart shows the allowed
32498 ** transitions and the inserted intermediate states:
32499 **
32500 **    UNLOCKED -> SHARED
32501 **    SHARED -> RESERVED
32502 **    SHARED -> (PENDING) -> EXCLUSIVE
32503 **    RESERVED -> (PENDING) -> EXCLUSIVE
32504 **    PENDING -> EXCLUSIVE
32505 **
32506 ** This routine will only increase a lock.  The winUnlock() routine
32507 ** erases all locks at once and returns us immediately to locking level 0.
32508 ** It is not possible to lower the locking level one step at a time.  You
32509 ** must go straight to locking level 0.
32510 */
32511 static int winLock(sqlite3_file *id, int locktype){
32512   int rc = SQLITE_OK;    /* Return code from subroutines */
32513   int res = 1;           /* Result of a windows lock call */
32514   int newLocktype;       /* Set pFile->locktype to this value before exiting */
32515   int gotPendingLock = 0;/* True if we acquired a PENDING lock this time */
32516   winFile *pFile = (winFile*)id;
32517   DWORD error = NO_ERROR;
32518 
32519   assert( id!=0 );
32520   OSTRACE(("LOCK %d %d was %d(%d)\n",
32521            pFile->h, locktype, pFile->locktype, pFile->sharedLockByte));
32522 
32523   /* If there is already a lock of this type or more restrictive on the
32524   ** OsFile, do nothing. Don't use the end_lock: exit path, as
32525   ** sqlite3OsEnterMutex() hasn't been called yet.
32526   */
32527   if( pFile->locktype>=locktype ){
32528     return SQLITE_OK;
32529   }
32530 
32531   /* Make sure the locking sequence is correct
32532   */
32533   assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
32534   assert( locktype!=PENDING_LOCK );
32535   assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
32536 
32537   /* Lock the PENDING_LOCK byte if we need to acquire a PENDING lock or
32538   ** a SHARED lock.  If we are acquiring a SHARED lock, the acquisition of
32539   ** the PENDING_LOCK byte is temporary.
32540   */
32541   newLocktype = pFile->locktype;
32542   if(   (pFile->locktype==NO_LOCK)
32543      || (   (locktype==EXCLUSIVE_LOCK)
32544          && (pFile->locktype==RESERVED_LOCK))
32545   ){
32546     int cnt = 3;
32547     while( cnt-->0 && (res = LockFile(pFile->h, PENDING_BYTE, 0, 1, 0))==0 ){
32548       /* Try 3 times to get the pending lock.  The pending lock might be
32549       ** held by another reader process who will release it momentarily.
32550       */
32551       OSTRACE(("could not get a PENDING lock. cnt=%d\n", cnt));
32552       Sleep(1);
32553     }
32554     gotPendingLock = res;
32555     if( !res ){
32556       error = GetLastError();
32557     }
32558   }
32559 
32560   /* Acquire a shared lock
32561   */
32562   if( locktype==SHARED_LOCK && res ){
32563     assert( pFile->locktype==NO_LOCK );
32564     res = getReadLock(pFile);
32565     if( res ){
32566       newLocktype = SHARED_LOCK;
32567     }else{
32568       error = GetLastError();
32569     }
32570   }
32571 
32572   /* Acquire a RESERVED lock
32573   */
32574   if( locktype==RESERVED_LOCK && res ){
32575     assert( pFile->locktype==SHARED_LOCK );
32576     res = LockFile(pFile->h, RESERVED_BYTE, 0, 1, 0);
32577     if( res ){
32578       newLocktype = RESERVED_LOCK;
32579     }else{
32580       error = GetLastError();
32581     }
32582   }
32583 
32584   /* Acquire a PENDING lock
32585   */
32586   if( locktype==EXCLUSIVE_LOCK && res ){
32587     newLocktype = PENDING_LOCK;
32588     gotPendingLock = 0;
32589   }
32590 
32591   /* Acquire an EXCLUSIVE lock
32592   */
32593   if( locktype==EXCLUSIVE_LOCK && res ){
32594     assert( pFile->locktype>=SHARED_LOCK );
32595     res = unlockReadLock(pFile);
32596     OSTRACE(("unreadlock = %d\n", res));
32597     res = LockFile(pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
32598     if( res ){
32599       newLocktype = EXCLUSIVE_LOCK;
32600     }else{
32601       error = GetLastError();
32602       OSTRACE(("error-code = %d\n", error));
32603       getReadLock(pFile);
32604     }
32605   }
32606 
32607   /* If we are holding a PENDING lock that ought to be released, then
32608   ** release it now.
32609   */
32610   if( gotPendingLock && locktype==SHARED_LOCK ){
32611     UnlockFile(pFile->h, PENDING_BYTE, 0, 1, 0);
32612   }
32613 
32614   /* Update the state of the lock has held in the file descriptor then
32615   ** return the appropriate result code.
32616   */
32617   if( res ){
32618     rc = SQLITE_OK;
32619   }else{
32620     OSTRACE(("LOCK FAILED %d trying for %d but got %d\n", pFile->h,
32621            locktype, newLocktype));
32622     pFile->lastErrno = error;
32623     rc = SQLITE_BUSY;
32624   }
32625   pFile->locktype = (u8)newLocktype;
32626   return rc;
32627 }
32628 
32629 /*
32630 ** This routine checks if there is a RESERVED lock held on the specified
32631 ** file by this or any other process. If such a lock is held, return
32632 ** non-zero, otherwise zero.
32633 */
32634 static int winCheckReservedLock(sqlite3_file *id, int *pResOut){
32635   int rc;
32636   winFile *pFile = (winFile*)id;
32637 
32638   SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
32639 
32640   assert( id!=0 );
32641   if( pFile->locktype>=RESERVED_LOCK ){
32642     rc = 1;
32643     OSTRACE(("TEST WR-LOCK %d %d (local)\n", pFile->h, rc));
32644   }else{
32645     rc = LockFile(pFile->h, RESERVED_BYTE, 0, 1, 0);
32646     if( rc ){
32647       UnlockFile(pFile->h, RESERVED_BYTE, 0, 1, 0);
32648     }
32649     rc = !rc;
32650     OSTRACE(("TEST WR-LOCK %d %d (remote)\n", pFile->h, rc));
32651   }
32652   *pResOut = rc;
32653   return SQLITE_OK;
32654 }
32655 
32656 /*
32657 ** Lower the locking level on file descriptor id to locktype.  locktype
32658 ** must be either NO_LOCK or SHARED_LOCK.
32659 **
32660 ** If the locking level of the file descriptor is already at or below
32661 ** the requested locking level, this routine is a no-op.
32662 **
32663 ** It is not possible for this routine to fail if the second argument
32664 ** is NO_LOCK.  If the second argument is SHARED_LOCK then this routine
32665 ** might return SQLITE_IOERR;
32666 */
32667 static int winUnlock(sqlite3_file *id, int locktype){
32668   int type;
32669   winFile *pFile = (winFile*)id;
32670   int rc = SQLITE_OK;
32671   assert( pFile!=0 );
32672   assert( locktype<=SHARED_LOCK );
32673   OSTRACE(("UNLOCK %d to %d was %d(%d)\n", pFile->h, locktype,
32674           pFile->locktype, pFile->sharedLockByte));
32675   type = pFile->locktype;
32676   if( type>=EXCLUSIVE_LOCK ){
32677     UnlockFile(pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
32678     if( locktype==SHARED_LOCK && !getReadLock(pFile) ){
32679       /* This should never happen.  We should always be able to
32680       ** reacquire the read lock */
32681       rc = winLogError(SQLITE_IOERR_UNLOCK, "winUnlock", pFile->zPath);
32682     }
32683   }
32684   if( type>=RESERVED_LOCK ){
32685     UnlockFile(pFile->h, RESERVED_BYTE, 0, 1, 0);
32686   }
32687   if( locktype==NO_LOCK && type>=SHARED_LOCK ){
32688     unlockReadLock(pFile);
32689   }
32690   if( type>=PENDING_LOCK ){
32691     UnlockFile(pFile->h, PENDING_BYTE, 0, 1, 0);
32692   }
32693   pFile->locktype = (u8)locktype;
32694   return rc;
32695 }
32696 
32697 /*
32698 ** Control and query of the open file handle.
32699 */
32700 static int winFileControl(sqlite3_file *id, int op, void *pArg){
32701   switch( op ){
32702     case SQLITE_FCNTL_LOCKSTATE: {
32703       *(int*)pArg = ((winFile*)id)->locktype;
32704       return SQLITE_OK;
32705     }
32706     case SQLITE_LAST_ERRNO: {
32707       *(int*)pArg = (int)((winFile*)id)->lastErrno;
32708       return SQLITE_OK;
32709     }
32710     case SQLITE_FCNTL_CHUNK_SIZE: {
32711       ((winFile*)id)->szChunk = *(int *)pArg;
32712       return SQLITE_OK;
32713     }
32714     case SQLITE_FCNTL_SIZE_HINT: {
32715       sqlite3_int64 sz = *(sqlite3_int64*)pArg;
32716       SimulateIOErrorBenign(1);
32717       winTruncate(id, sz);
32718       SimulateIOErrorBenign(0);
32719       return SQLITE_OK;
32720     }
32721     case SQLITE_FCNTL_SYNC_OMITTED: {
32722       return SQLITE_OK;
32723     }
32724   }
32725   return SQLITE_NOTFOUND;
32726 }
32727 
32728 /*
32729 ** Return the sector size in bytes of the underlying block device for
32730 ** the specified file. This is almost always 512 bytes, but may be
32731 ** larger for some devices.
32732 **
32733 ** SQLite code assumes this function cannot fail. It also assumes that
32734 ** if two files are created in the same file-system directory (i.e.
32735 ** a database and its journal file) that the sector size will be the
32736 ** same for both.
32737 */
32738 static int winSectorSize(sqlite3_file *id){
32739   assert( id!=0 );
32740   return (int)(((winFile*)id)->sectorSize);
32741 }
32742 
32743 /*
32744 ** Return a vector of device characteristics.
32745 */
32746 static int winDeviceCharacteristics(sqlite3_file *id){
32747   UNUSED_PARAMETER(id);
32748   return SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN;
32749 }
32750 
32751 #ifndef SQLITE_OMIT_WAL
32752 
32753 /*
32754 ** Windows will only let you create file view mappings
32755 ** on allocation size granularity boundaries.
32756 ** During sqlite3_os_init() we do a GetSystemInfo()
32757 ** to get the granularity size.
32758 */
32759 SYSTEM_INFO winSysInfo;
32760 
32761 /*
32762 ** Helper functions to obtain and relinquish the global mutex. The
32763 ** global mutex is used to protect the winLockInfo objects used by
32764 ** this file, all of which may be shared by multiple threads.
32765 **
32766 ** Function winShmMutexHeld() is used to assert() that the global mutex
32767 ** is held when required. This function is only used as part of assert()
32768 ** statements. e.g.
32769 **
32770 **   winShmEnterMutex()
32771 **     assert( winShmMutexHeld() );
32772 **   winShmLeaveMutex()
32773 */
32774 static void winShmEnterMutex(void){
32775   sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
32776 }
32777 static void winShmLeaveMutex(void){
32778   sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
32779 }
32780 #ifdef SQLITE_DEBUG
32781 static int winShmMutexHeld(void) {
32782   return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
32783 }
32784 #endif
32785 
32786 /*
32787 ** Object used to represent a single file opened and mmapped to provide
32788 ** shared memory.  When multiple threads all reference the same
32789 ** log-summary, each thread has its own winFile object, but they all
32790 ** point to a single instance of this object.  In other words, each
32791 ** log-summary is opened only once per process.
32792 **
32793 ** winShmMutexHeld() must be true when creating or destroying
32794 ** this object or while reading or writing the following fields:
32795 **
32796 **      nRef
32797 **      pNext
32798 **
32799 ** The following fields are read-only after the object is created:
32800 **
32801 **      fid
32802 **      zFilename
32803 **
32804 ** Either winShmNode.mutex must be held or winShmNode.nRef==0 and
32805 ** winShmMutexHeld() is true when reading or writing any other field
32806 ** in this structure.
32807 **
32808 */
32809 struct winShmNode {
32810   sqlite3_mutex *mutex;      /* Mutex to access this object */
32811   char *zFilename;           /* Name of the file */
32812   winFile hFile;             /* File handle from winOpen */
32813 
32814   int szRegion;              /* Size of shared-memory regions */
32815   int nRegion;               /* Size of array apRegion */
32816   struct ShmRegion {
32817     HANDLE hMap;             /* File handle from CreateFileMapping */
32818     void *pMap;
32819   } *aRegion;
32820   DWORD lastErrno;           /* The Windows errno from the last I/O error */
32821 
32822   int nRef;                  /* Number of winShm objects pointing to this */
32823   winShm *pFirst;            /* All winShm objects pointing to this */
32824   winShmNode *pNext;         /* Next in list of all winShmNode objects */
32825 #ifdef SQLITE_DEBUG
32826   u8 nextShmId;              /* Next available winShm.id value */
32827 #endif
32828 };
32829 
32830 /*
32831 ** A global array of all winShmNode objects.
32832 **
32833 ** The winShmMutexHeld() must be true while reading or writing this list.
32834 */
32835 static winShmNode *winShmNodeList = 0;
32836 
32837 /*
32838 ** Structure used internally by this VFS to record the state of an
32839 ** open shared memory connection.
32840 **
32841 ** The following fields are initialized when this object is created and
32842 ** are read-only thereafter:
32843 **
32844 **    winShm.pShmNode
32845 **    winShm.id
32846 **
32847 ** All other fields are read/write.  The winShm.pShmNode->mutex must be held
32848 ** while accessing any read/write fields.
32849 */
32850 struct winShm {
32851   winShmNode *pShmNode;      /* The underlying winShmNode object */
32852   winShm *pNext;             /* Next winShm with the same winShmNode */
32853   u8 hasMutex;               /* True if holding the winShmNode mutex */
32854   u16 sharedMask;            /* Mask of shared locks held */
32855   u16 exclMask;              /* Mask of exclusive locks held */
32856 #ifdef SQLITE_DEBUG
32857   u8 id;                     /* Id of this connection with its winShmNode */
32858 #endif
32859 };
32860 
32861 /*
32862 ** Constants used for locking
32863 */
32864 #define WIN_SHM_BASE   ((22+SQLITE_SHM_NLOCK)*4)        /* first lock byte */
32865 #define WIN_SHM_DMS    (WIN_SHM_BASE+SQLITE_SHM_NLOCK)  /* deadman switch */
32866 
32867 /*
32868 ** Apply advisory locks for all n bytes beginning at ofst.
32869 */
32870 #define _SHM_UNLCK  1
32871 #define _SHM_RDLCK  2
32872 #define _SHM_WRLCK  3
32873 static int winShmSystemLock(
32874   winShmNode *pFile,    /* Apply locks to this open shared-memory segment */
32875   int lockType,         /* _SHM_UNLCK, _SHM_RDLCK, or _SHM_WRLCK */
32876   int ofst,             /* Offset to first byte to be locked/unlocked */
32877   int nByte             /* Number of bytes to lock or unlock */
32878 ){
32879   OVERLAPPED ovlp;
32880   DWORD dwFlags;
32881   int rc = 0;           /* Result code form Lock/UnlockFileEx() */
32882 
32883   /* Access to the winShmNode object is serialized by the caller */
32884   assert( sqlite3_mutex_held(pFile->mutex) || pFile->nRef==0 );
32885 
32886   /* Initialize the locking parameters */
32887   dwFlags = LOCKFILE_FAIL_IMMEDIATELY;
32888   if( lockType == _SHM_WRLCK ) dwFlags |= LOCKFILE_EXCLUSIVE_LOCK;
32889 
32890   memset(&ovlp, 0, sizeof(OVERLAPPED));
32891   ovlp.Offset = ofst;
32892 
32893   /* Release/Acquire the system-level lock */
32894   if( lockType==_SHM_UNLCK ){
32895     rc = UnlockFileEx(pFile->hFile.h, 0, nByte, 0, &ovlp);
32896   }else{
32897     rc = LockFileEx(pFile->hFile.h, dwFlags, 0, nByte, 0, &ovlp);
32898   }
32899 
32900   if( rc!= 0 ){
32901     rc = SQLITE_OK;
32902   }else{
32903     pFile->lastErrno =  GetLastError();
32904     rc = SQLITE_BUSY;
32905   }
32906 
32907   OSTRACE(("SHM-LOCK %d %s %s 0x%08lx\n",
32908            pFile->hFile.h,
32909            rc==SQLITE_OK ? "ok" : "failed",
32910            lockType==_SHM_UNLCK ? "UnlockFileEx" : "LockFileEx",
32911            pFile->lastErrno));
32912 
32913   return rc;
32914 }
32915 
32916 /* Forward references to VFS methods */
32917 static int winOpen(sqlite3_vfs*,const char*,sqlite3_file*,int,int*);
32918 static int winDelete(sqlite3_vfs *,const char*,int);
32919 
32920 /*
32921 ** Purge the winShmNodeList list of all entries with winShmNode.nRef==0.
32922 **
32923 ** This is not a VFS shared-memory method; it is a utility function called
32924 ** by VFS shared-memory methods.
32925 */
32926 static void winShmPurge(sqlite3_vfs *pVfs, int deleteFlag){
32927   winShmNode **pp;
32928   winShmNode *p;
32929   BOOL bRc;
32930   assert( winShmMutexHeld() );
32931   pp = &winShmNodeList;
32932   while( (p = *pp)!=0 ){
32933     if( p->nRef==0 ){
32934       int i;
32935       if( p->mutex ) sqlite3_mutex_free(p->mutex);
32936       for(i=0; i<p->nRegion; i++){
32937         bRc = UnmapViewOfFile(p->aRegion[i].pMap);
32938         OSTRACE(("SHM-PURGE pid-%d unmap region=%d %s\n",
32939                  (int)GetCurrentProcessId(), i,
32940                  bRc ? "ok" : "failed"));
32941         bRc = CloseHandle(p->aRegion[i].hMap);
32942         OSTRACE(("SHM-PURGE pid-%d close region=%d %s\n",
32943                  (int)GetCurrentProcessId(), i,
32944                  bRc ? "ok" : "failed"));
32945       }
32946       if( p->hFile.h != INVALID_HANDLE_VALUE ){
32947         SimulateIOErrorBenign(1);
32948         winClose((sqlite3_file *)&p->hFile);
32949         SimulateIOErrorBenign(0);
32950       }
32951       if( deleteFlag ){
32952         SimulateIOErrorBenign(1);
32953         winDelete(pVfs, p->zFilename, 0);
32954         SimulateIOErrorBenign(0);
32955       }
32956       *pp = p->pNext;
32957       sqlite3_free(p->aRegion);
32958       sqlite3_free(p);
32959     }else{
32960       pp = &p->pNext;
32961     }
32962   }
32963 }
32964 
32965 /*
32966 ** Open the shared-memory area associated with database file pDbFd.
32967 **
32968 ** When opening a new shared-memory file, if no other instances of that
32969 ** file are currently open, in this process or in other processes, then
32970 ** the file must be truncated to zero length or have its header cleared.
32971 */
32972 static int winOpenSharedMemory(winFile *pDbFd){
32973   struct winShm *p;                  /* The connection to be opened */
32974   struct winShmNode *pShmNode = 0;   /* The underlying mmapped file */
32975   int rc;                            /* Result code */
32976   struct winShmNode *pNew;           /* Newly allocated winShmNode */
32977   int nName;                         /* Size of zName in bytes */
32978 
32979   assert( pDbFd->pShm==0 );    /* Not previously opened */
32980 
32981   /* Allocate space for the new sqlite3_shm object.  Also speculatively
32982   ** allocate space for a new winShmNode and filename.
32983   */
32984   p = sqlite3_malloc( sizeof(*p) );
32985   if( p==0 ) return SQLITE_NOMEM;
32986   memset(p, 0, sizeof(*p));
32987   nName = sqlite3Strlen30(pDbFd->zPath);
32988   pNew = sqlite3_malloc( sizeof(*pShmNode) + nName + 15 );
32989   if( pNew==0 ){
32990     sqlite3_free(p);
32991     return SQLITE_NOMEM;
32992   }
32993   memset(pNew, 0, sizeof(*pNew));
32994   pNew->zFilename = (char*)&pNew[1];
32995   sqlite3_snprintf(nName+15, pNew->zFilename, "%s-shm", pDbFd->zPath);
32996   sqlite3FileSuffix3(pDbFd->zPath, pNew->zFilename);
32997 
32998   /* Look to see if there is an existing winShmNode that can be used.
32999   ** If no matching winShmNode currently exists, create a new one.
33000   */
33001   winShmEnterMutex();
33002   for(pShmNode = winShmNodeList; pShmNode; pShmNode=pShmNode->pNext){
33003     /* TBD need to come up with better match here.  Perhaps
33004     ** use FILE_ID_BOTH_DIR_INFO Structure.
33005     */
33006     if( sqlite3StrICmp(pShmNode->zFilename, pNew->zFilename)==0 ) break;
33007   }
33008   if( pShmNode ){
33009     sqlite3_free(pNew);
33010   }else{
33011     pShmNode = pNew;
33012     pNew = 0;
33013     ((winFile*)(&pShmNode->hFile))->h = INVALID_HANDLE_VALUE;
33014     pShmNode->pNext = winShmNodeList;
33015     winShmNodeList = pShmNode;
33016 
33017     pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
33018     if( pShmNode->mutex==0 ){
33019       rc = SQLITE_NOMEM;
33020       goto shm_open_err;
33021     }
33022 
33023     rc = winOpen(pDbFd->pVfs,
33024                  pShmNode->zFilename,             /* Name of the file (UTF-8) */
33025                  (sqlite3_file*)&pShmNode->hFile,  /* File handle here */
33026                  SQLITE_OPEN_WAL | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, /* Mode flags */
33027                  0);
33028     if( SQLITE_OK!=rc ){
33029       rc = SQLITE_CANTOPEN_BKPT;
33030       goto shm_open_err;
33031     }
33032 
33033     /* Check to see if another process is holding the dead-man switch.
33034     ** If not, truncate the file to zero length.
33035     */
33036     if( winShmSystemLock(pShmNode, _SHM_WRLCK, WIN_SHM_DMS, 1)==SQLITE_OK ){
33037       rc = winTruncate((sqlite3_file *)&pShmNode->hFile, 0);
33038       if( rc!=SQLITE_OK ){
33039         rc = winLogError(SQLITE_IOERR_SHMOPEN, "winOpenShm", pDbFd->zPath);
33040       }
33041     }
33042     if( rc==SQLITE_OK ){
33043       winShmSystemLock(pShmNode, _SHM_UNLCK, WIN_SHM_DMS, 1);
33044       rc = winShmSystemLock(pShmNode, _SHM_RDLCK, WIN_SHM_DMS, 1);
33045     }
33046     if( rc ) goto shm_open_err;
33047   }
33048 
33049   /* Make the new connection a child of the winShmNode */
33050   p->pShmNode = pShmNode;
33051 #ifdef SQLITE_DEBUG
33052   p->id = pShmNode->nextShmId++;
33053 #endif
33054   pShmNode->nRef++;
33055   pDbFd->pShm = p;
33056   winShmLeaveMutex();
33057 
33058   /* The reference count on pShmNode has already been incremented under
33059   ** the cover of the winShmEnterMutex() mutex and the pointer from the
33060   ** new (struct winShm) object to the pShmNode has been set. All that is
33061   ** left to do is to link the new object into the linked list starting
33062   ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex
33063   ** mutex.
33064   */
33065   sqlite3_mutex_enter(pShmNode->mutex);
33066   p->pNext = pShmNode->pFirst;
33067   pShmNode->pFirst = p;
33068   sqlite3_mutex_leave(pShmNode->mutex);
33069   return SQLITE_OK;
33070 
33071   /* Jump here on any error */
33072 shm_open_err:
33073   winShmSystemLock(pShmNode, _SHM_UNLCK, WIN_SHM_DMS, 1);
33074   winShmPurge(pDbFd->pVfs, 0);      /* This call frees pShmNode if required */
33075   sqlite3_free(p);
33076   sqlite3_free(pNew);
33077   winShmLeaveMutex();
33078   return rc;
33079 }
33080 
33081 /*
33082 ** Close a connection to shared-memory.  Delete the underlying
33083 ** storage if deleteFlag is true.
33084 */
33085 static int winShmUnmap(
33086   sqlite3_file *fd,          /* Database holding shared memory */
33087   int deleteFlag             /* Delete after closing if true */
33088 ){
33089   winFile *pDbFd;       /* Database holding shared-memory */
33090   winShm *p;            /* The connection to be closed */
33091   winShmNode *pShmNode; /* The underlying shared-memory file */
33092   winShm **pp;          /* For looping over sibling connections */
33093 
33094   pDbFd = (winFile*)fd;
33095   p = pDbFd->pShm;
33096   if( p==0 ) return SQLITE_OK;
33097   pShmNode = p->pShmNode;
33098 
33099   /* Remove connection p from the set of connections associated
33100   ** with pShmNode */
33101   sqlite3_mutex_enter(pShmNode->mutex);
33102   for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){}
33103   *pp = p->pNext;
33104 
33105   /* Free the connection p */
33106   sqlite3_free(p);
33107   pDbFd->pShm = 0;
33108   sqlite3_mutex_leave(pShmNode->mutex);
33109 
33110   /* If pShmNode->nRef has reached 0, then close the underlying
33111   ** shared-memory file, too */
33112   winShmEnterMutex();
33113   assert( pShmNode->nRef>0 );
33114   pShmNode->nRef--;
33115   if( pShmNode->nRef==0 ){
33116     winShmPurge(pDbFd->pVfs, deleteFlag);
33117   }
33118   winShmLeaveMutex();
33119 
33120   return SQLITE_OK;
33121 }
33122 
33123 /*
33124 ** Change the lock state for a shared-memory segment.
33125 */
33126 static int winShmLock(
33127   sqlite3_file *fd,          /* Database file holding the shared memory */
33128   int ofst,                  /* First lock to acquire or release */
33129   int n,                     /* Number of locks to acquire or release */
33130   int flags                  /* What to do with the lock */
33131 ){
33132   winFile *pDbFd = (winFile*)fd;        /* Connection holding shared memory */
33133   winShm *p = pDbFd->pShm;              /* The shared memory being locked */
33134   winShm *pX;                           /* For looping over all siblings */
33135   winShmNode *pShmNode = p->pShmNode;
33136   int rc = SQLITE_OK;                   /* Result code */
33137   u16 mask;                             /* Mask of locks to take or release */
33138 
33139   assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK );
33140   assert( n>=1 );
33141   assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED)
33142        || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE)
33143        || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED)
33144        || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) );
33145   assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 );
33146 
33147   mask = (u16)((1U<<(ofst+n)) - (1U<<ofst));
33148   assert( n>1 || mask==(1<<ofst) );
33149   sqlite3_mutex_enter(pShmNode->mutex);
33150   if( flags & SQLITE_SHM_UNLOCK ){
33151     u16 allMask = 0; /* Mask of locks held by siblings */
33152 
33153     /* See if any siblings hold this same lock */
33154     for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
33155       if( pX==p ) continue;
33156       assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 );
33157       allMask |= pX->sharedMask;
33158     }
33159 
33160     /* Unlock the system-level locks */
33161     if( (mask & allMask)==0 ){
33162       rc = winShmSystemLock(pShmNode, _SHM_UNLCK, ofst+WIN_SHM_BASE, n);
33163     }else{
33164       rc = SQLITE_OK;
33165     }
33166 
33167     /* Undo the local locks */
33168     if( rc==SQLITE_OK ){
33169       p->exclMask &= ~mask;
33170       p->sharedMask &= ~mask;
33171     }
33172   }else if( flags & SQLITE_SHM_SHARED ){
33173     u16 allShared = 0;  /* Union of locks held by connections other than "p" */
33174 
33175     /* Find out which shared locks are already held by sibling connections.
33176     ** If any sibling already holds an exclusive lock, go ahead and return
33177     ** SQLITE_BUSY.
33178     */
33179     for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
33180       if( (pX->exclMask & mask)!=0 ){
33181         rc = SQLITE_BUSY;
33182         break;
33183       }
33184       allShared |= pX->sharedMask;
33185     }
33186 
33187     /* Get shared locks at the system level, if necessary */
33188     if( rc==SQLITE_OK ){
33189       if( (allShared & mask)==0 ){
33190         rc = winShmSystemLock(pShmNode, _SHM_RDLCK, ofst+WIN_SHM_BASE, n);
33191       }else{
33192         rc = SQLITE_OK;
33193       }
33194     }
33195 
33196     /* Get the local shared locks */
33197     if( rc==SQLITE_OK ){
33198       p->sharedMask |= mask;
33199     }
33200   }else{
33201     /* Make sure no sibling connections hold locks that will block this
33202     ** lock.  If any do, return SQLITE_BUSY right away.
33203     */
33204     for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
33205       if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){
33206         rc = SQLITE_BUSY;
33207         break;
33208       }
33209     }
33210 
33211     /* Get the exclusive locks at the system level.  Then if successful
33212     ** also mark the local connection as being locked.
33213     */
33214     if( rc==SQLITE_OK ){
33215       rc = winShmSystemLock(pShmNode, _SHM_WRLCK, ofst+WIN_SHM_BASE, n);
33216       if( rc==SQLITE_OK ){
33217         assert( (p->sharedMask & mask)==0 );
33218         p->exclMask |= mask;
33219       }
33220     }
33221   }
33222   sqlite3_mutex_leave(pShmNode->mutex);
33223   OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x %s\n",
33224            p->id, (int)GetCurrentProcessId(), p->sharedMask, p->exclMask,
33225            rc ? "failed" : "ok"));
33226   return rc;
33227 }
33228 
33229 /*
33230 ** Implement a memory barrier or memory fence on shared memory.
33231 **
33232 ** All loads and stores begun before the barrier must complete before
33233 ** any load or store begun after the barrier.
33234 */
33235 static void winShmBarrier(
33236   sqlite3_file *fd          /* Database holding the shared memory */
33237 ){
33238   UNUSED_PARAMETER(fd);
33239   /* MemoryBarrier(); // does not work -- do not know why not */
33240   winShmEnterMutex();
33241   winShmLeaveMutex();
33242 }
33243 
33244 /*
33245 ** This function is called to obtain a pointer to region iRegion of the
33246 ** shared-memory associated with the database file fd. Shared-memory regions
33247 ** are numbered starting from zero. Each shared-memory region is szRegion
33248 ** bytes in size.
33249 **
33250 ** If an error occurs, an error code is returned and *pp is set to NULL.
33251 **
33252 ** Otherwise, if the isWrite parameter is 0 and the requested shared-memory
33253 ** region has not been allocated (by any client, including one running in a
33254 ** separate process), then *pp is set to NULL and SQLITE_OK returned. If
33255 ** isWrite is non-zero and the requested shared-memory region has not yet
33256 ** been allocated, it is allocated by this function.
33257 **
33258 ** If the shared-memory region has already been allocated or is allocated by
33259 ** this call as described above, then it is mapped into this processes
33260 ** address space (if it is not already), *pp is set to point to the mapped
33261 ** memory and SQLITE_OK returned.
33262 */
33263 static int winShmMap(
33264   sqlite3_file *fd,               /* Handle open on database file */
33265   int iRegion,                    /* Region to retrieve */
33266   int szRegion,                   /* Size of regions */
33267   int isWrite,                    /* True to extend file if necessary */
33268   void volatile **pp              /* OUT: Mapped memory */
33269 ){
33270   winFile *pDbFd = (winFile*)fd;
33271   winShm *p = pDbFd->pShm;
33272   winShmNode *pShmNode;
33273   int rc = SQLITE_OK;
33274 
33275   if( !p ){
33276     rc = winOpenSharedMemory(pDbFd);
33277     if( rc!=SQLITE_OK ) return rc;
33278     p = pDbFd->pShm;
33279   }
33280   pShmNode = p->pShmNode;
33281 
33282   sqlite3_mutex_enter(pShmNode->mutex);
33283   assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 );
33284 
33285   if( pShmNode->nRegion<=iRegion ){
33286     struct ShmRegion *apNew;           /* New aRegion[] array */
33287     int nByte = (iRegion+1)*szRegion;  /* Minimum required file size */
33288     sqlite3_int64 sz;                  /* Current size of wal-index file */
33289 
33290     pShmNode->szRegion = szRegion;
33291 
33292     /* The requested region is not mapped into this processes address space.
33293     ** Check to see if it has been allocated (i.e. if the wal-index file is
33294     ** large enough to contain the requested region).
33295     */
33296     rc = winFileSize((sqlite3_file *)&pShmNode->hFile, &sz);
33297     if( rc!=SQLITE_OK ){
33298       rc = winLogError(SQLITE_IOERR_SHMSIZE, "winShmMap1", pDbFd->zPath);
33299       goto shmpage_out;
33300     }
33301 
33302     if( sz<nByte ){
33303       /* The requested memory region does not exist. If isWrite is set to
33304       ** zero, exit early. *pp will be set to NULL and SQLITE_OK returned.
33305       **
33306       ** Alternatively, if isWrite is non-zero, use ftruncate() to allocate
33307       ** the requested memory region.
33308       */
33309       if( !isWrite ) goto shmpage_out;
33310       rc = winTruncate((sqlite3_file *)&pShmNode->hFile, nByte);
33311       if( rc!=SQLITE_OK ){
33312         rc = winLogError(SQLITE_IOERR_SHMSIZE, "winShmMap2", pDbFd->zPath);
33313         goto shmpage_out;
33314       }
33315     }
33316 
33317     /* Map the requested memory region into this processes address space. */
33318     apNew = (struct ShmRegion *)sqlite3_realloc(
33319         pShmNode->aRegion, (iRegion+1)*sizeof(apNew[0])
33320     );
33321     if( !apNew ){
33322       rc = SQLITE_IOERR_NOMEM;
33323       goto shmpage_out;
33324     }
33325     pShmNode->aRegion = apNew;
33326 
33327     while( pShmNode->nRegion<=iRegion ){
33328       HANDLE hMap;                /* file-mapping handle */
33329       void *pMap = 0;             /* Mapped memory region */
33330 
33331       hMap = CreateFileMapping(pShmNode->hFile.h,
33332           NULL, PAGE_READWRITE, 0, nByte, NULL
33333       );
33334       OSTRACE(("SHM-MAP pid-%d create region=%d nbyte=%d %s\n",
33335                (int)GetCurrentProcessId(), pShmNode->nRegion, nByte,
33336                hMap ? "ok" : "failed"));
33337       if( hMap ){
33338         int iOffset = pShmNode->nRegion*szRegion;
33339         int iOffsetShift = iOffset % winSysInfo.dwAllocationGranularity;
33340         pMap = MapViewOfFile(hMap, FILE_MAP_WRITE | FILE_MAP_READ,
33341             0, iOffset - iOffsetShift, szRegion + iOffsetShift
33342         );
33343         OSTRACE(("SHM-MAP pid-%d map region=%d offset=%d size=%d %s\n",
33344                  (int)GetCurrentProcessId(), pShmNode->nRegion, iOffset, szRegion,
33345                  pMap ? "ok" : "failed"));
33346       }
33347       if( !pMap ){
33348         pShmNode->lastErrno = GetLastError();
33349         rc = winLogError(SQLITE_IOERR_SHMMAP, "winShmMap3", pDbFd->zPath);
33350         if( hMap ) CloseHandle(hMap);
33351         goto shmpage_out;
33352       }
33353 
33354       pShmNode->aRegion[pShmNode->nRegion].pMap = pMap;
33355       pShmNode->aRegion[pShmNode->nRegion].hMap = hMap;
33356       pShmNode->nRegion++;
33357     }
33358   }
33359 
33360 shmpage_out:
33361   if( pShmNode->nRegion>iRegion ){
33362     int iOffset = iRegion*szRegion;
33363     int iOffsetShift = iOffset % winSysInfo.dwAllocationGranularity;
33364     char *p = (char *)pShmNode->aRegion[iRegion].pMap;
33365     *pp = (void *)&p[iOffsetShift];
33366   }else{
33367     *pp = 0;
33368   }
33369   sqlite3_mutex_leave(pShmNode->mutex);
33370   return rc;
33371 }
33372 
33373 #else
33374 # define winShmMap     0
33375 # define winShmLock    0
33376 # define winShmBarrier 0
33377 # define winShmUnmap   0
33378 #endif /* #ifndef SQLITE_OMIT_WAL */
33379 
33380 /*
33381 ** Here ends the implementation of all sqlite3_file methods.
33382 **
33383 ********************** End sqlite3_file Methods *******************************
33384 ******************************************************************************/
33385 
33386 /*
33387 ** This vector defines all the methods that can operate on an
33388 ** sqlite3_file for win32.
33389 */
33390 static const sqlite3_io_methods winIoMethod = {
33391   2,                              /* iVersion */
33392   winClose,                       /* xClose */
33393   winRead,                        /* xRead */
33394   winWrite,                       /* xWrite */
33395   winTruncate,                    /* xTruncate */
33396   winSync,                        /* xSync */
33397   winFileSize,                    /* xFileSize */
33398   winLock,                        /* xLock */
33399   winUnlock,                      /* xUnlock */
33400   winCheckReservedLock,           /* xCheckReservedLock */
33401   winFileControl,                 /* xFileControl */
33402   winSectorSize,                  /* xSectorSize */
33403   winDeviceCharacteristics,       /* xDeviceCharacteristics */
33404   winShmMap,                      /* xShmMap */
33405   winShmLock,                     /* xShmLock */
33406   winShmBarrier,                  /* xShmBarrier */
33407   winShmUnmap                     /* xShmUnmap */
33408 };
33409 
33410 /****************************************************************************
33411 **************************** sqlite3_vfs methods ****************************
33412 **
33413 ** This division contains the implementation of methods on the
33414 ** sqlite3_vfs object.
33415 */
33416 
33417 /*
33418 ** Convert a UTF-8 filename into whatever form the underlying
33419 ** operating system wants filenames in.  Space to hold the result
33420 ** is obtained from malloc and must be freed by the calling
33421 ** function.
33422 */
33423 static void *convertUtf8Filename(const char *zFilename){
33424   void *zConverted = 0;
33425   if( isNT() ){
33426     zConverted = utf8ToUnicode(zFilename);
33427 /* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed.
33428 */
33429 #if SQLITE_OS_WINCE==0
33430   }else{
33431     zConverted = sqlite3_win32_utf8_to_mbcs(zFilename);
33432 #endif
33433   }
33434   /* caller will handle out of memory */
33435   return zConverted;
33436 }
33437 
33438 /*
33439 ** Create a temporary file name in zBuf.  zBuf must be big enough to
33440 ** hold at pVfs->mxPathname characters.
33441 */
33442 static int getTempname(int nBuf, char *zBuf){
33443   static char zChars[] =
33444     "abcdefghijklmnopqrstuvwxyz"
33445     "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
33446     "0123456789";
33447   size_t i, j;
33448   char zTempPath[MAX_PATH+1];
33449 
33450   /* It's odd to simulate an io-error here, but really this is just
33451   ** using the io-error infrastructure to test that SQLite handles this
33452   ** function failing.
33453   */
33454   SimulateIOError( return SQLITE_IOERR );
33455 
33456   if( sqlite3_temp_directory ){
33457     sqlite3_snprintf(MAX_PATH-30, zTempPath, "%s", sqlite3_temp_directory);
33458   }else if( isNT() ){
33459     char *zMulti;
33460     WCHAR zWidePath[MAX_PATH];
33461     GetTempPathW(MAX_PATH-30, zWidePath);
33462     zMulti = unicodeToUtf8(zWidePath);
33463     if( zMulti ){
33464       sqlite3_snprintf(MAX_PATH-30, zTempPath, "%s", zMulti);
33465       free(zMulti);
33466     }else{
33467       return SQLITE_NOMEM;
33468     }
33469 /* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed.
33470 ** Since the ASCII version of these Windows API do not exist for WINCE,
33471 ** it's important to not reference them for WINCE builds.
33472 */
33473 #if SQLITE_OS_WINCE==0
33474   }else{
33475     char *zUtf8;
33476     char zMbcsPath[MAX_PATH];
33477     GetTempPathA(MAX_PATH-30, zMbcsPath);
33478     zUtf8 = sqlite3_win32_mbcs_to_utf8(zMbcsPath);
33479     if( zUtf8 ){
33480       sqlite3_snprintf(MAX_PATH-30, zTempPath, "%s", zUtf8);
33481       free(zUtf8);
33482     }else{
33483       return SQLITE_NOMEM;
33484     }
33485 #endif
33486   }
33487 
33488   /* Check that the output buffer is large enough for the temporary file
33489   ** name. If it is not, return SQLITE_ERROR.
33490   */
33491   if( (sqlite3Strlen30(zTempPath) + sqlite3Strlen30(SQLITE_TEMP_FILE_PREFIX) + 17) >= nBuf ){
33492     return SQLITE_ERROR;
33493   }
33494 
33495   for(i=sqlite3Strlen30(zTempPath); i>0 && zTempPath[i-1]=='\\'; i--){}
33496   zTempPath[i] = 0;
33497 
33498   sqlite3_snprintf(nBuf-17, zBuf,
33499                    "%s\\"SQLITE_TEMP_FILE_PREFIX, zTempPath);
33500   j = sqlite3Strlen30(zBuf);
33501   sqlite3_randomness(15, &zBuf[j]);
33502   for(i=0; i<15; i++, j++){
33503     zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
33504   }
33505   zBuf[j] = 0;
33506 
33507   OSTRACE(("TEMP FILENAME: %s\n", zBuf));
33508   return SQLITE_OK;
33509 }
33510 
33511 /*
33512 ** Open a file.
33513 */
33514 static int winOpen(
33515   sqlite3_vfs *pVfs,        /* Not used */
33516   const char *zName,        /* Name of the file (UTF-8) */
33517   sqlite3_file *id,         /* Write the SQLite file handle here */
33518   int flags,                /* Open mode flags */
33519   int *pOutFlags            /* Status return flags */
33520 ){
33521   HANDLE h;
33522   DWORD dwDesiredAccess;
33523   DWORD dwShareMode;
33524   DWORD dwCreationDisposition;
33525   DWORD dwFlagsAndAttributes = 0;
33526 #if SQLITE_OS_WINCE
33527   int isTemp = 0;
33528 #endif
33529   winFile *pFile = (winFile*)id;
33530   void *zConverted;              /* Filename in OS encoding */
33531   const char *zUtf8Name = zName; /* Filename in UTF-8 encoding */
33532 
33533   /* If argument zPath is a NULL pointer, this function is required to open
33534   ** a temporary file. Use this buffer to store the file name in.
33535   */
33536   char zTmpname[MAX_PATH+1];     /* Buffer used to create temp filename */
33537 
33538   int rc = SQLITE_OK;            /* Function Return Code */
33539 #if !defined(NDEBUG) || SQLITE_OS_WINCE
33540   int eType = flags&0xFFFFFF00;  /* Type of file to open */
33541 #endif
33542 
33543   int isExclusive  = (flags & SQLITE_OPEN_EXCLUSIVE);
33544   int isDelete     = (flags & SQLITE_OPEN_DELETEONCLOSE);
33545   int isCreate     = (flags & SQLITE_OPEN_CREATE);
33546 #ifndef NDEBUG
33547   int isReadonly   = (flags & SQLITE_OPEN_READONLY);
33548 #endif
33549   int isReadWrite  = (flags & SQLITE_OPEN_READWRITE);
33550 
33551 #ifndef NDEBUG
33552   int isOpenJournal = (isCreate && (
33553         eType==SQLITE_OPEN_MASTER_JOURNAL
33554      || eType==SQLITE_OPEN_MAIN_JOURNAL
33555      || eType==SQLITE_OPEN_WAL
33556   ));
33557 #endif
33558 
33559   /* Check the following statements are true:
33560   **
33561   **   (a) Exactly one of the READWRITE and READONLY flags must be set, and
33562   **   (b) if CREATE is set, then READWRITE must also be set, and
33563   **   (c) if EXCLUSIVE is set, then CREATE must also be set.
33564   **   (d) if DELETEONCLOSE is set, then CREATE must also be set.
33565   */
33566   assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly));
33567   assert(isCreate==0 || isReadWrite);
33568   assert(isExclusive==0 || isCreate);
33569   assert(isDelete==0 || isCreate);
33570 
33571   /* The main DB, main journal, WAL file and master journal are never
33572   ** automatically deleted. Nor are they ever temporary files.  */
33573   assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB );
33574   assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL );
33575   assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MASTER_JOURNAL );
33576   assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL );
33577 
33578   /* Assert that the upper layer has set one of the "file-type" flags. */
33579   assert( eType==SQLITE_OPEN_MAIN_DB      || eType==SQLITE_OPEN_TEMP_DB
33580        || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL
33581        || eType==SQLITE_OPEN_SUBJOURNAL   || eType==SQLITE_OPEN_MASTER_JOURNAL
33582        || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL
33583   );
33584 
33585   assert( id!=0 );
33586   UNUSED_PARAMETER(pVfs);
33587 
33588   pFile->h = INVALID_HANDLE_VALUE;
33589 
33590   /* If the second argument to this function is NULL, generate a
33591   ** temporary file name to use
33592   */
33593   if( !zUtf8Name ){
33594     assert(isDelete && !isOpenJournal);
33595     rc = getTempname(MAX_PATH+1, zTmpname);
33596     if( rc!=SQLITE_OK ){
33597       return rc;
33598     }
33599     zUtf8Name = zTmpname;
33600   }
33601 
33602   /* Convert the filename to the system encoding. */
33603   zConverted = convertUtf8Filename(zUtf8Name);
33604   if( zConverted==0 ){
33605     return SQLITE_NOMEM;
33606   }
33607 
33608   if( isReadWrite ){
33609     dwDesiredAccess = GENERIC_READ | GENERIC_WRITE;
33610   }else{
33611     dwDesiredAccess = GENERIC_READ;
33612   }
33613 
33614   /* SQLITE_OPEN_EXCLUSIVE is used to make sure that a new file is
33615   ** created. SQLite doesn't use it to indicate "exclusive access"
33616   ** as it is usually understood.
33617   */
33618   if( isExclusive ){
33619     /* Creates a new file, only if it does not already exist. */
33620     /* If the file exists, it fails. */
33621     dwCreationDisposition = CREATE_NEW;
33622   }else if( isCreate ){
33623     /* Open existing file, or create if it doesn't exist */
33624     dwCreationDisposition = OPEN_ALWAYS;
33625   }else{
33626     /* Opens a file, only if it exists. */
33627     dwCreationDisposition = OPEN_EXISTING;
33628   }
33629 
33630   dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;
33631 
33632   if( isDelete ){
33633 #if SQLITE_OS_WINCE
33634     dwFlagsAndAttributes = FILE_ATTRIBUTE_HIDDEN;
33635     isTemp = 1;
33636 #else
33637     dwFlagsAndAttributes = FILE_ATTRIBUTE_TEMPORARY
33638                                | FILE_ATTRIBUTE_HIDDEN
33639                                | FILE_FLAG_DELETE_ON_CLOSE;
33640 #endif
33641   }else{
33642     dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL;
33643   }
33644   /* Reports from the internet are that performance is always
33645   ** better if FILE_FLAG_RANDOM_ACCESS is used.  Ticket #2699. */
33646 #if SQLITE_OS_WINCE
33647   dwFlagsAndAttributes |= FILE_FLAG_RANDOM_ACCESS;
33648 #endif
33649 
33650   if( isNT() ){
33651     h = CreateFileW((WCHAR*)zConverted,
33652        dwDesiredAccess,
33653        dwShareMode,
33654        NULL,
33655        dwCreationDisposition,
33656        dwFlagsAndAttributes,
33657        NULL
33658     );
33659 /* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed.
33660 ** Since the ASCII version of these Windows API do not exist for WINCE,
33661 ** it's important to not reference them for WINCE builds.
33662 */
33663 #if SQLITE_OS_WINCE==0
33664   }else{
33665     h = CreateFileA((char*)zConverted,
33666        dwDesiredAccess,
33667        dwShareMode,
33668        NULL,
33669        dwCreationDisposition,
33670        dwFlagsAndAttributes,
33671        NULL
33672     );
33673 #endif
33674   }
33675 
33676   OSTRACE(("OPEN %d %s 0x%lx %s\n",
33677            h, zName, dwDesiredAccess,
33678            h==INVALID_HANDLE_VALUE ? "failed" : "ok"));
33679 
33680   if( h==INVALID_HANDLE_VALUE ){
33681     pFile->lastErrno = GetLastError();
33682     winLogError(SQLITE_CANTOPEN, "winOpen", zUtf8Name);
33683     free(zConverted);
33684     if( isReadWrite ){
33685       return winOpen(pVfs, zName, id,
33686              ((flags|SQLITE_OPEN_READONLY)&~(SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE)), pOutFlags);
33687     }else{
33688       return SQLITE_CANTOPEN_BKPT;
33689     }
33690   }
33691 
33692   if( pOutFlags ){
33693     if( isReadWrite ){
33694       *pOutFlags = SQLITE_OPEN_READWRITE;
33695     }else{
33696       *pOutFlags = SQLITE_OPEN_READONLY;
33697     }
33698   }
33699 
33700   memset(pFile, 0, sizeof(*pFile));
33701   pFile->pMethod = &winIoMethod;
33702   pFile->h = h;
33703   pFile->lastErrno = NO_ERROR;
33704   pFile->pVfs = pVfs;
33705   pFile->pShm = 0;
33706   pFile->zPath = zName;
33707   pFile->sectorSize = getSectorSize(pVfs, zUtf8Name);
33708 
33709 #if SQLITE_OS_WINCE
33710   if( isReadWrite && eType==SQLITE_OPEN_MAIN_DB
33711        && !winceCreateLock(zName, pFile)
33712   ){
33713     CloseHandle(h);
33714     free(zConverted);
33715     return SQLITE_CANTOPEN_BKPT;
33716   }
33717   if( isTemp ){
33718     pFile->zDeleteOnClose = zConverted;
33719   }else
33720 #endif
33721   {
33722     free(zConverted);
33723   }
33724 
33725   OpenCounter(+1);
33726   return rc;
33727 }
33728 
33729 /*
33730 ** Delete the named file.
33731 **
33732 ** Note that windows does not allow a file to be deleted if some other
33733 ** process has it open.  Sometimes a virus scanner or indexing program
33734 ** will open a journal file shortly after it is created in order to do
33735 ** whatever it does.  While this other process is holding the
33736 ** file open, we will be unable to delete it.  To work around this
33737 ** problem, we delay 100 milliseconds and try to delete again.  Up
33738 ** to MX_DELETION_ATTEMPTs deletion attempts are run before giving
33739 ** up and returning an error.
33740 */
33741 #define MX_DELETION_ATTEMPTS 5
33742 static int winDelete(
33743   sqlite3_vfs *pVfs,          /* Not used on win32 */
33744   const char *zFilename,      /* Name of file to delete */
33745   int syncDir                 /* Not used on win32 */
33746 ){
33747   int cnt = 0;
33748   DWORD rc;
33749   DWORD error = 0;
33750   void *zConverted;
33751   UNUSED_PARAMETER(pVfs);
33752   UNUSED_PARAMETER(syncDir);
33753 
33754   SimulateIOError(return SQLITE_IOERR_DELETE);
33755   zConverted = convertUtf8Filename(zFilename);
33756   if( zConverted==0 ){
33757     return SQLITE_NOMEM;
33758   }
33759   if( isNT() ){
33760     do{
33761       DeleteFileW(zConverted);
33762     }while(   (   ((rc = GetFileAttributesW(zConverted)) != INVALID_FILE_ATTRIBUTES)
33763                || ((error = GetLastError()) == ERROR_ACCESS_DENIED))
33764            && (++cnt < MX_DELETION_ATTEMPTS)
33765            && (Sleep(100), 1) );
33766 /* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed.
33767 ** Since the ASCII version of these Windows API do not exist for WINCE,
33768 ** it's important to not reference them for WINCE builds.
33769 */
33770 #if SQLITE_OS_WINCE==0
33771   }else{
33772     do{
33773       DeleteFileA(zConverted);
33774     }while(   (   ((rc = GetFileAttributesA(zConverted)) != INVALID_FILE_ATTRIBUTES)
33775                || ((error = GetLastError()) == ERROR_ACCESS_DENIED))
33776            && (++cnt < MX_DELETION_ATTEMPTS)
33777            && (Sleep(100), 1) );
33778 #endif
33779   }
33780   free(zConverted);
33781   OSTRACE(("DELETE \"%s\" %s\n", zFilename,
33782        ( (rc==INVALID_FILE_ATTRIBUTES) && (error==ERROR_FILE_NOT_FOUND)) ?
33783          "ok" : "failed" ));
33784 
33785   return (   (rc == INVALID_FILE_ATTRIBUTES)
33786           && (error == ERROR_FILE_NOT_FOUND)) ? SQLITE_OK :
33787                  winLogError(SQLITE_IOERR_DELETE, "winDelete", zFilename);
33788 }
33789 
33790 /*
33791 ** Check the existance and status of a file.
33792 */
33793 static int winAccess(
33794   sqlite3_vfs *pVfs,         /* Not used on win32 */
33795   const char *zFilename,     /* Name of file to check */
33796   int flags,                 /* Type of test to make on this file */
33797   int *pResOut               /* OUT: Result */
33798 ){
33799   DWORD attr;
33800   int rc = 0;
33801   void *zConverted;
33802   UNUSED_PARAMETER(pVfs);
33803 
33804   SimulateIOError( return SQLITE_IOERR_ACCESS; );
33805   zConverted = convertUtf8Filename(zFilename);
33806   if( zConverted==0 ){
33807     return SQLITE_NOMEM;
33808   }
33809   if( isNT() ){
33810     WIN32_FILE_ATTRIBUTE_DATA sAttrData;
33811     memset(&sAttrData, 0, sizeof(sAttrData));
33812     if( GetFileAttributesExW((WCHAR*)zConverted,
33813                              GetFileExInfoStandard,
33814                              &sAttrData) ){
33815       /* For an SQLITE_ACCESS_EXISTS query, treat a zero-length file
33816       ** as if it does not exist.
33817       */
33818       if(    flags==SQLITE_ACCESS_EXISTS
33819           && sAttrData.nFileSizeHigh==0
33820           && sAttrData.nFileSizeLow==0 ){
33821         attr = INVALID_FILE_ATTRIBUTES;
33822       }else{
33823         attr = sAttrData.dwFileAttributes;
33824       }
33825     }else{
33826       if( GetLastError()!=ERROR_FILE_NOT_FOUND ){
33827         winLogError(SQLITE_IOERR_ACCESS, "winAccess", zFilename);
33828         free(zConverted);
33829         return SQLITE_IOERR_ACCESS;
33830       }else{
33831         attr = INVALID_FILE_ATTRIBUTES;
33832       }
33833     }
33834 /* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed.
33835 ** Since the ASCII version of these Windows API do not exist for WINCE,
33836 ** it's important to not reference them for WINCE builds.
33837 */
33838 #if SQLITE_OS_WINCE==0
33839   }else{
33840     attr = GetFileAttributesA((char*)zConverted);
33841 #endif
33842   }
33843   free(zConverted);
33844   switch( flags ){
33845     case SQLITE_ACCESS_READ:
33846     case SQLITE_ACCESS_EXISTS:
33847       rc = attr!=INVALID_FILE_ATTRIBUTES;
33848       break;
33849     case SQLITE_ACCESS_READWRITE:
33850       rc = (attr & FILE_ATTRIBUTE_READONLY)==0;
33851       break;
33852     default:
33853       assert(!"Invalid flags argument");
33854   }
33855   *pResOut = rc;
33856   return SQLITE_OK;
33857 }
33858 
33859 
33860 /*
33861 ** Turn a relative pathname into a full pathname.  Write the full
33862 ** pathname into zOut[].  zOut[] will be at least pVfs->mxPathname
33863 ** bytes in size.
33864 */
33865 static int winFullPathname(
33866   sqlite3_vfs *pVfs,            /* Pointer to vfs object */
33867   const char *zRelative,        /* Possibly relative input path */
33868   int nFull,                    /* Size of output buffer in bytes */
33869   char *zFull                   /* Output buffer */
33870 ){
33871 
33872 #if defined(__CYGWIN__)
33873   SimulateIOError( return SQLITE_ERROR );
33874   UNUSED_PARAMETER(nFull);
33875   cygwin_conv_to_full_win32_path(zRelative, zFull);
33876   return SQLITE_OK;
33877 #endif
33878 
33879 #if SQLITE_OS_WINCE
33880   SimulateIOError( return SQLITE_ERROR );
33881   UNUSED_PARAMETER(nFull);
33882   /* WinCE has no concept of a relative pathname, or so I am told. */
33883   sqlite3_snprintf(pVfs->mxPathname, zFull, "%s", zRelative);
33884   return SQLITE_OK;
33885 #endif
33886 
33887 #if !SQLITE_OS_WINCE && !defined(__CYGWIN__)
33888   int nByte;
33889   void *zConverted;
33890   char *zOut;
33891 
33892   /* If this path name begins with "/X:", where "X" is any alphabetic
33893   ** character, discard the initial "/" from the pathname.
33894   */
33895   if( zRelative[0]=='/' && sqlite3Isalpha(zRelative[1]) && zRelative[2]==':' ){
33896     zRelative++;
33897   }
33898 
33899   /* It's odd to simulate an io-error here, but really this is just
33900   ** using the io-error infrastructure to test that SQLite handles this
33901   ** function failing. This function could fail if, for example, the
33902   ** current working directory has been unlinked.
33903   */
33904   SimulateIOError( return SQLITE_ERROR );
33905   UNUSED_PARAMETER(nFull);
33906   zConverted = convertUtf8Filename(zRelative);
33907   if( isNT() ){
33908     WCHAR *zTemp;
33909     nByte = GetFullPathNameW((WCHAR*)zConverted, 0, 0, 0) + 3;
33910     zTemp = malloc( nByte*sizeof(zTemp[0]) );
33911     if( zTemp==0 ){
33912       free(zConverted);
33913       return SQLITE_NOMEM;
33914     }
33915     GetFullPathNameW((WCHAR*)zConverted, nByte, zTemp, 0);
33916     free(zConverted);
33917     zOut = unicodeToUtf8(zTemp);
33918     free(zTemp);
33919 /* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed.
33920 ** Since the ASCII version of these Windows API do not exist for WINCE,
33921 ** it's important to not reference them for WINCE builds.
33922 */
33923 #if SQLITE_OS_WINCE==0
33924   }else{
33925     char *zTemp;
33926     nByte = GetFullPathNameA((char*)zConverted, 0, 0, 0) + 3;
33927     zTemp = malloc( nByte*sizeof(zTemp[0]) );
33928     if( zTemp==0 ){
33929       free(zConverted);
33930       return SQLITE_NOMEM;
33931     }
33932     GetFullPathNameA((char*)zConverted, nByte, zTemp, 0);
33933     free(zConverted);
33934     zOut = sqlite3_win32_mbcs_to_utf8(zTemp);
33935     free(zTemp);
33936 #endif
33937   }
33938   if( zOut ){
33939     sqlite3_snprintf(pVfs->mxPathname, zFull, "%s", zOut);
33940     free(zOut);
33941     return SQLITE_OK;
33942   }else{
33943     return SQLITE_NOMEM;
33944   }
33945 #endif
33946 }
33947 
33948 /*
33949 ** Get the sector size of the device used to store
33950 ** file.
33951 */
33952 static int getSectorSize(
33953     sqlite3_vfs *pVfs,
33954     const char *zRelative     /* UTF-8 file name */
33955 ){
33956   DWORD bytesPerSector = SQLITE_DEFAULT_SECTOR_SIZE;
33957   /* GetDiskFreeSpace is not supported under WINCE */
33958 #if SQLITE_OS_WINCE
33959   UNUSED_PARAMETER(pVfs);
33960   UNUSED_PARAMETER(zRelative);
33961 #else
33962   char zFullpath[MAX_PATH+1];
33963   int rc;
33964   DWORD dwRet = 0;
33965   DWORD dwDummy;
33966 
33967   /*
33968   ** We need to get the full path name of the file
33969   ** to get the drive letter to look up the sector
33970   ** size.
33971   */
33972   SimulateIOErrorBenign(1);
33973   rc = winFullPathname(pVfs, zRelative, MAX_PATH, zFullpath);
33974   SimulateIOErrorBenign(0);
33975   if( rc == SQLITE_OK )
33976   {
33977     void *zConverted = convertUtf8Filename(zFullpath);
33978     if( zConverted ){
33979       if( isNT() ){
33980         /* trim path to just drive reference */
33981         WCHAR *p = zConverted;
33982         for(;*p;p++){
33983           if( *p == '\\' ){
33984             *p = '\0';
33985             break;
33986           }
33987         }
33988         dwRet = GetDiskFreeSpaceW((WCHAR*)zConverted,
33989                                   &dwDummy,
33990                                   &bytesPerSector,
33991                                   &dwDummy,
33992                                   &dwDummy);
33993       }else{
33994         /* trim path to just drive reference */
33995         char *p = (char *)zConverted;
33996         for(;*p;p++){
33997           if( *p == '\\' ){
33998             *p = '\0';
33999             break;
34000           }
34001         }
34002         dwRet = GetDiskFreeSpaceA((char*)zConverted,
34003                                   &dwDummy,
34004                                   &bytesPerSector,
34005                                   &dwDummy,
34006                                   &dwDummy);
34007       }
34008       free(zConverted);
34009     }
34010     if( !dwRet ){
34011       bytesPerSector = SQLITE_DEFAULT_SECTOR_SIZE;
34012     }
34013   }
34014 #endif
34015   return (int) bytesPerSector;
34016 }
34017 
34018 #ifndef SQLITE_OMIT_LOAD_EXTENSION
34019 /*
34020 ** Interfaces for opening a shared library, finding entry points
34021 ** within the shared library, and closing the shared library.
34022 */
34023 /*
34024 ** Interfaces for opening a shared library, finding entry points
34025 ** within the shared library, and closing the shared library.
34026 */
34027 static void *winDlOpen(sqlite3_vfs *pVfs, const char *zFilename){
34028   HANDLE h;
34029   void *zConverted = convertUtf8Filename(zFilename);
34030   UNUSED_PARAMETER(pVfs);
34031   if( zConverted==0 ){
34032     return 0;
34033   }
34034   if( isNT() ){
34035     h = LoadLibraryW((WCHAR*)zConverted);
34036 /* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed.
34037 ** Since the ASCII version of these Windows API do not exist for WINCE,
34038 ** it's important to not reference them for WINCE builds.
34039 */
34040 #if SQLITE_OS_WINCE==0
34041   }else{
34042     h = LoadLibraryA((char*)zConverted);
34043 #endif
34044   }
34045   free(zConverted);
34046   return (void*)h;
34047 }
34048 static void winDlError(sqlite3_vfs *pVfs, int nBuf, char *zBufOut){
34049   UNUSED_PARAMETER(pVfs);
34050   getLastErrorMsg(nBuf, zBufOut);
34051 }
34052 void (*winDlSym(sqlite3_vfs *pVfs, void *pHandle, const char *zSymbol))(void){
34053   UNUSED_PARAMETER(pVfs);
34054 #if SQLITE_OS_WINCE
34055   /* The GetProcAddressA() routine is only available on wince. */
34056   return (void(*)(void))GetProcAddressA((HANDLE)pHandle, zSymbol);
34057 #else
34058   /* All other windows platforms expect GetProcAddress() to take
34059   ** an Ansi string regardless of the _UNICODE setting */
34060   return (void(*)(void))GetProcAddress((HANDLE)pHandle, zSymbol);
34061 #endif
34062 }
34063 void winDlClose(sqlite3_vfs *pVfs, void *pHandle){
34064   UNUSED_PARAMETER(pVfs);
34065   FreeLibrary((HANDLE)pHandle);
34066 }
34067 #else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
34068   #define winDlOpen  0
34069   #define winDlError 0
34070   #define winDlSym   0
34071   #define winDlClose 0
34072 #endif
34073 
34074 
34075 /*
34076 ** Write up to nBuf bytes of randomness into zBuf.
34077 */
34078 static int winRandomness(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
34079   int n = 0;
34080   UNUSED_PARAMETER(pVfs);
34081 #if defined(SQLITE_TEST)
34082   n = nBuf;
34083   memset(zBuf, 0, nBuf);
34084 #else
34085   if( sizeof(SYSTEMTIME)<=nBuf-n ){
34086     SYSTEMTIME x;
34087     GetSystemTime(&x);
34088     memcpy(&zBuf[n], &x, sizeof(x));
34089     n += sizeof(x);
34090   }
34091   if( sizeof(DWORD)<=nBuf-n ){
34092     DWORD pid = GetCurrentProcessId();
34093     memcpy(&zBuf[n], &pid, sizeof(pid));
34094     n += sizeof(pid);
34095   }
34096   if( sizeof(DWORD)<=nBuf-n ){
34097     DWORD cnt = GetTickCount();
34098     memcpy(&zBuf[n], &cnt, sizeof(cnt));
34099     n += sizeof(cnt);
34100   }
34101   if( sizeof(LARGE_INTEGER)<=nBuf-n ){
34102     LARGE_INTEGER i;
34103     QueryPerformanceCounter(&i);
34104     memcpy(&zBuf[n], &i, sizeof(i));
34105     n += sizeof(i);
34106   }
34107 #endif
34108   return n;
34109 }
34110 
34111 
34112 /*
34113 ** Sleep for a little while.  Return the amount of time slept.
34114 */
34115 static int winSleep(sqlite3_vfs *pVfs, int microsec){
34116   Sleep((microsec+999)/1000);
34117   UNUSED_PARAMETER(pVfs);
34118   return ((microsec+999)/1000)*1000;
34119 }
34120 
34121 /*
34122 ** The following variable, if set to a non-zero value, is interpreted as
34123 ** the number of seconds since 1970 and is used to set the result of
34124 ** sqlite3OsCurrentTime() during testing.
34125 */
34126 #ifdef SQLITE_TEST
34127 SQLITE_API int sqlite3_current_time = 0;  /* Fake system time in seconds since 1970. */
34128 #endif
34129 
34130 /*
34131 ** Find the current time (in Universal Coordinated Time).  Write into *piNow
34132 ** the current time and date as a Julian Day number times 86_400_000.  In
34133 ** other words, write into *piNow the number of milliseconds since the Julian
34134 ** epoch of noon in Greenwich on November 24, 4714 B.C according to the
34135 ** proleptic Gregorian calendar.
34136 **
34137 ** On success, return 0.  Return 1 if the time and date cannot be found.
34138 */
34139 static int winCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *piNow){
34140   /* FILETIME structure is a 64-bit value representing the number of
34141      100-nanosecond intervals since January 1, 1601 (= JD 2305813.5).
34142   */
34143   FILETIME ft;
34144   static const sqlite3_int64 winFiletimeEpoch = 23058135*(sqlite3_int64)8640000;
34145 #ifdef SQLITE_TEST
34146   static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000;
34147 #endif
34148   /* 2^32 - to avoid use of LL and warnings in gcc */
34149   static const sqlite3_int64 max32BitValue =
34150       (sqlite3_int64)2000000000 + (sqlite3_int64)2000000000 + (sqlite3_int64)294967296;
34151 
34152 #if SQLITE_OS_WINCE
34153   SYSTEMTIME time;
34154   GetSystemTime(&time);
34155   /* if SystemTimeToFileTime() fails, it returns zero. */
34156   if (!SystemTimeToFileTime(&time,&ft)){
34157     return 1;
34158   }
34159 #else
34160   GetSystemTimeAsFileTime( &ft );
34161 #endif
34162 
34163   *piNow = winFiletimeEpoch +
34164             ((((sqlite3_int64)ft.dwHighDateTime)*max32BitValue) +
34165                (sqlite3_int64)ft.dwLowDateTime)/(sqlite3_int64)10000;
34166 
34167 #ifdef SQLITE_TEST
34168   if( sqlite3_current_time ){
34169     *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch;
34170   }
34171 #endif
34172   UNUSED_PARAMETER(pVfs);
34173   return 0;
34174 }
34175 
34176 /*
34177 ** Find the current time (in Universal Coordinated Time).  Write the
34178 ** current time and date as a Julian Day number into *prNow and
34179 ** return 0.  Return 1 if the time and date cannot be found.
34180 */
34181 int winCurrentTime(sqlite3_vfs *pVfs, double *prNow){
34182   int rc;
34183   sqlite3_int64 i;
34184   rc = winCurrentTimeInt64(pVfs, &i);
34185   if( !rc ){
34186     *prNow = i/86400000.0;
34187   }
34188   return rc;
34189 }
34190 
34191 /*
34192 ** The idea is that this function works like a combination of
34193 ** GetLastError() and FormatMessage() on windows (or errno and
34194 ** strerror_r() on unix). After an error is returned by an OS
34195 ** function, SQLite calls this function with zBuf pointing to
34196 ** a buffer of nBuf bytes. The OS layer should populate the
34197 ** buffer with a nul-terminated UTF-8 encoded error message
34198 ** describing the last IO error to have occurred within the calling
34199 ** thread.
34200 **
34201 ** If the error message is too large for the supplied buffer,
34202 ** it should be truncated. The return value of xGetLastError
34203 ** is zero if the error message fits in the buffer, or non-zero
34204 ** otherwise (if the message was truncated). If non-zero is returned,
34205 ** then it is not necessary to include the nul-terminator character
34206 ** in the output buffer.
34207 **
34208 ** Not supplying an error message will have no adverse effect
34209 ** on SQLite. It is fine to have an implementation that never
34210 ** returns an error message:
34211 **
34212 **   int xGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
34213 **     assert(zBuf[0]=='\0');
34214 **     return 0;
34215 **   }
34216 **
34217 ** However if an error message is supplied, it will be incorporated
34218 ** by sqlite into the error message available to the user using
34219 ** sqlite3_errmsg(), possibly making IO errors easier to debug.
34220 */
34221 static int winGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
34222   UNUSED_PARAMETER(pVfs);
34223   return getLastErrorMsg(nBuf, zBuf);
34224 }
34225 
34226 
34227 
34228 /*
34229 ** Initialize and deinitialize the operating system interface.
34230 */
34231 SQLITE_API int sqlite3_os_init(void){
34232   static sqlite3_vfs winVfs = {
34233     3,                   /* iVersion */
34234     sizeof(winFile),     /* szOsFile */
34235     MAX_PATH,            /* mxPathname */
34236     0,                   /* pNext */
34237     "win32",             /* zName */
34238     0,                   /* pAppData */
34239     winOpen,             /* xOpen */
34240     winDelete,           /* xDelete */
34241     winAccess,           /* xAccess */
34242     winFullPathname,     /* xFullPathname */
34243     winDlOpen,           /* xDlOpen */
34244     winDlError,          /* xDlError */
34245     winDlSym,            /* xDlSym */
34246     winDlClose,          /* xDlClose */
34247     winRandomness,       /* xRandomness */
34248     winSleep,            /* xSleep */
34249     winCurrentTime,      /* xCurrentTime */
34250     winGetLastError,     /* xGetLastError */
34251     winCurrentTimeInt64, /* xCurrentTimeInt64 */
34252     0,                   /* xSetSystemCall */
34253     0,                   /* xGetSystemCall */
34254     0,                   /* xNextSystemCall */
34255   };
34256 
34257 #ifndef SQLITE_OMIT_WAL
34258   /* get memory map allocation granularity */
34259   memset(&winSysInfo, 0, sizeof(SYSTEM_INFO));
34260   GetSystemInfo(&winSysInfo);
34261   assert(winSysInfo.dwAllocationGranularity > 0);
34262 #endif
34263 
34264   sqlite3_vfs_register(&winVfs, 1);
34265   return SQLITE_OK;
34266 }
34267 SQLITE_API int sqlite3_os_end(void){
34268   return SQLITE_OK;
34269 }
34270 
34271 #endif /* SQLITE_OS_WIN */
34272 
34273 /************** End of os_win.c **********************************************/
34274 /************** Begin file bitvec.c ******************************************/
34275 /*
34276 ** 2008 February 16
34277 **
34278 ** The author disclaims copyright to this source code.  In place of
34279 ** a legal notice, here is a blessing:
34280 **
34281 **    May you do good and not evil.
34282 **    May you find forgiveness for yourself and forgive others.
34283 **    May you share freely, never taking more than you give.
34284 **
34285 *************************************************************************
34286 ** This file implements an object that represents a fixed-length
34287 ** bitmap.  Bits are numbered starting with 1.
34288 **
34289 ** A bitmap is used to record which pages of a database file have been
34290 ** journalled during a transaction, or which pages have the "dont-write"
34291 ** property.  Usually only a few pages are meet either condition.
34292 ** So the bitmap is usually sparse and has low cardinality.
34293 ** But sometimes (for example when during a DROP of a large table) most
34294 ** or all of the pages in a database can get journalled.  In those cases,
34295 ** the bitmap becomes dense with high cardinality.  The algorithm needs
34296 ** to handle both cases well.
34297 **
34298 ** The size of the bitmap is fixed when the object is created.
34299 **
34300 ** All bits are clear when the bitmap is created.  Individual bits
34301 ** may be set or cleared one at a time.
34302 **
34303 ** Test operations are about 100 times more common that set operations.
34304 ** Clear operations are exceedingly rare.  There are usually between
34305 ** 5 and 500 set operations per Bitvec object, though the number of sets can
34306 ** sometimes grow into tens of thousands or larger.  The size of the
34307 ** Bitvec object is the number of pages in the database file at the
34308 ** start of a transaction, and is thus usually less than a few thousand,
34309 ** but can be as large as 2 billion for a really big database.
34310 */
34311 
34312 /* Size of the Bitvec structure in bytes. */
34313 #define BITVEC_SZ        512
34314 
34315 /* Round the union size down to the nearest pointer boundary, since that's how
34316 ** it will be aligned within the Bitvec struct. */
34317 #define BITVEC_USIZE     (((BITVEC_SZ-(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))
34318 
34319 /* Type of the array "element" for the bitmap representation.
34320 ** Should be a power of 2, and ideally, evenly divide into BITVEC_USIZE.
34321 ** Setting this to the "natural word" size of your CPU may improve
34322 ** performance. */
34323 #define BITVEC_TELEM     u8
34324 /* Size, in bits, of the bitmap element. */
34325 #define BITVEC_SZELEM    8
34326 /* Number of elements in a bitmap array. */
34327 #define BITVEC_NELEM     (BITVEC_USIZE/sizeof(BITVEC_TELEM))
34328 /* Number of bits in the bitmap array. */
34329 #define BITVEC_NBIT      (BITVEC_NELEM*BITVEC_SZELEM)
34330 
34331 /* Number of u32 values in hash table. */
34332 #define BITVEC_NINT      (BITVEC_USIZE/sizeof(u32))
34333 /* Maximum number of entries in hash table before
34334 ** sub-dividing and re-hashing. */
34335 #define BITVEC_MXHASH    (BITVEC_NINT/2)
34336 /* Hashing function for the aHash representation.
34337 ** Empirical testing showed that the *37 multiplier
34338 ** (an arbitrary prime)in the hash function provided
34339 ** no fewer collisions than the no-op *1. */
34340 #define BITVEC_HASH(X)   (((X)*1)%BITVEC_NINT)
34341 
34342 #define BITVEC_NPTR      (BITVEC_USIZE/sizeof(Bitvec *))
34343 
34344 
34345 /*
34346 ** A bitmap is an instance of the following structure.
34347 **
34348 ** This bitmap records the existance of zero or more bits
34349 ** with values between 1 and iSize, inclusive.
34350 **
34351 ** There are three possible representations of the bitmap.
34352 ** If iSize<=BITVEC_NBIT, then Bitvec.u.aBitmap[] is a straight
34353 ** bitmap.  The least significant bit is bit 1.
34354 **
34355 ** If iSize>BITVEC_NBIT and iDivisor==0 then Bitvec.u.aHash[] is
34356 ** a hash table that will hold up to BITVEC_MXHASH distinct values.
34357 **
34358 ** Otherwise, the value i is redirected into one of BITVEC_NPTR
34359 ** sub-bitmaps pointed to by Bitvec.u.apSub[].  Each subbitmap
34360 ** handles up to iDivisor separate values of i.  apSub[0] holds
34361 ** values between 1 and iDivisor.  apSub[1] holds values between
34362 ** iDivisor+1 and 2*iDivisor.  apSub[N] holds values between
34363 ** N*iDivisor+1 and (N+1)*iDivisor.  Each subbitmap is normalized
34364 ** to hold deal with values between 1 and iDivisor.
34365 */
34366 struct Bitvec {
34367   u32 iSize;      /* Maximum bit index.  Max iSize is 4,294,967,296. */
34368   u32 nSet;       /* Number of bits that are set - only valid for aHash
34369                   ** element.  Max is BITVEC_NINT.  For BITVEC_SZ of 512,
34370                   ** this would be 125. */
34371   u32 iDivisor;   /* Number of bits handled by each apSub[] entry. */
34372                   /* Should >=0 for apSub element. */
34373                   /* Max iDivisor is max(u32) / BITVEC_NPTR + 1.  */
34374                   /* For a BITVEC_SZ of 512, this would be 34,359,739. */
34375   union {
34376     BITVEC_TELEM aBitmap[BITVEC_NELEM];    /* Bitmap representation */
34377     u32 aHash[BITVEC_NINT];      /* Hash table representation */
34378     Bitvec *apSub[BITVEC_NPTR];  /* Recursive representation */
34379   } u;
34380 };
34381 
34382 /*
34383 ** Create a new bitmap object able to handle bits between 0 and iSize,
34384 ** inclusive.  Return a pointer to the new object.  Return NULL if
34385 ** malloc fails.
34386 */
34387 SQLITE_PRIVATE Bitvec *sqlite3BitvecCreate(u32 iSize){
34388   Bitvec *p;
34389   assert( sizeof(*p)==BITVEC_SZ );
34390   p = sqlite3MallocZero( sizeof(*p) );
34391   if( p ){
34392     p->iSize = iSize;
34393   }
34394   return p;
34395 }
34396 
34397 /*
34398 ** Check to see if the i-th bit is set.  Return true or false.
34399 ** If p is NULL (if the bitmap has not been created) or if
34400 ** i is out of range, then return false.
34401 */
34402 SQLITE_PRIVATE int sqlite3BitvecTest(Bitvec *p, u32 i){
34403   if( p==0 ) return 0;
34404   if( i>p->iSize || i==0 ) return 0;
34405   i--;
34406   while( p->iDivisor ){
34407     u32 bin = i/p->iDivisor;
34408     i = i%p->iDivisor;
34409     p = p->u.apSub[bin];
34410     if (!p) {
34411       return 0;
34412     }
34413   }
34414   if( p->iSize<=BITVEC_NBIT ){
34415     return (p->u.aBitmap[i/BITVEC_SZELEM] & (1<<(i&(BITVEC_SZELEM-1))))!=0;
34416   } else{
34417     u32 h = BITVEC_HASH(i++);
34418     while( p->u.aHash[h] ){
34419       if( p->u.aHash[h]==i ) return 1;
34420       h = (h+1) % BITVEC_NINT;
34421     }
34422     return 0;
34423   }
34424 }
34425 
34426 /*
34427 ** Set the i-th bit.  Return 0 on success and an error code if
34428 ** anything goes wrong.
34429 **
34430 ** This routine might cause sub-bitmaps to be allocated.  Failing
34431 ** to get the memory needed to hold the sub-bitmap is the only
34432 ** that can go wrong with an insert, assuming p and i are valid.
34433 **
34434 ** The calling function must ensure that p is a valid Bitvec object
34435 ** and that the value for "i" is within range of the Bitvec object.
34436 ** Otherwise the behavior is undefined.
34437 */
34438 SQLITE_PRIVATE int sqlite3BitvecSet(Bitvec *p, u32 i){
34439   u32 h;
34440   if( p==0 ) return SQLITE_OK;
34441   assert( i>0 );
34442   assert( i<=p->iSize );
34443   i--;
34444   while((p->iSize > BITVEC_NBIT) && p->iDivisor) {
34445     u32 bin = i/p->iDivisor;
34446     i = i%p->iDivisor;
34447     if( p->u.apSub[bin]==0 ){
34448       p->u.apSub[bin] = sqlite3BitvecCreate( p->iDivisor );
34449       if( p->u.apSub[bin]==0 ) return SQLITE_NOMEM;
34450     }
34451     p = p->u.apSub[bin];
34452   }
34453   if( p->iSize<=BITVEC_NBIT ){
34454     p->u.aBitmap[i/BITVEC_SZELEM] |= 1 << (i&(BITVEC_SZELEM-1));
34455     return SQLITE_OK;
34456   }
34457   h = BITVEC_HASH(i++);
34458   /* if there wasn't a hash collision, and this doesn't */
34459   /* completely fill the hash, then just add it without */
34460   /* worring about sub-dividing and re-hashing. */
34461   if( !p->u.aHash[h] ){
34462     if (p->nSet<(BITVEC_NINT-1)) {
34463       goto bitvec_set_end;
34464     } else {
34465       goto bitvec_set_rehash;
34466     }
34467   }
34468   /* there was a collision, check to see if it's already */
34469   /* in hash, if not, try to find a spot for it */
34470   do {
34471     if( p->u.aHash[h]==i ) return SQLITE_OK;
34472     h++;
34473     if( h>=BITVEC_NINT ) h = 0;
34474   } while( p->u.aHash[h] );
34475   /* we didn't find it in the hash.  h points to the first */
34476   /* available free spot. check to see if this is going to */
34477   /* make our hash too "full".  */
34478 bitvec_set_rehash:
34479   if( p->nSet>=BITVEC_MXHASH ){
34480     unsigned int j;
34481     int rc;
34482     u32 *aiValues = sqlite3StackAllocRaw(0, sizeof(p->u.aHash));
34483     if( aiValues==0 ){
34484       return SQLITE_NOMEM;
34485     }else{
34486       memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash));
34487       memset(p->u.apSub, 0, sizeof(p->u.apSub));
34488       p->iDivisor = (p->iSize + BITVEC_NPTR - 1)/BITVEC_NPTR;
34489       rc = sqlite3BitvecSet(p, i);
34490       for(j=0; j<BITVEC_NINT; j++){
34491         if( aiValues[j] ) rc |= sqlite3BitvecSet(p, aiValues[j]);
34492       }
34493       sqlite3StackFree(0, aiValues);
34494       return rc;
34495     }
34496   }
34497 bitvec_set_end:
34498   p->nSet++;
34499   p->u.aHash[h] = i;
34500   return SQLITE_OK;
34501 }
34502 
34503 /*
34504 ** Clear the i-th bit.
34505 **
34506 ** pBuf must be a pointer to at least BITVEC_SZ bytes of temporary storage
34507 ** that BitvecClear can use to rebuilt its hash table.
34508 */
34509 SQLITE_PRIVATE void sqlite3BitvecClear(Bitvec *p, u32 i, void *pBuf){
34510   if( p==0 ) return;
34511   assert( i>0 );
34512   i--;
34513   while( p->iDivisor ){
34514     u32 bin = i/p->iDivisor;
34515     i = i%p->iDivisor;
34516     p = p->u.apSub[bin];
34517     if (!p) {
34518       return;
34519     }
34520   }
34521   if( p->iSize<=BITVEC_NBIT ){
34522     p->u.aBitmap[i/BITVEC_SZELEM] &= ~(1 << (i&(BITVEC_SZELEM-1)));
34523   }else{
34524     unsigned int j;
34525     u32 *aiValues = pBuf;
34526     memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash));
34527     memset(p->u.aHash, 0, sizeof(p->u.aHash));
34528     p->nSet = 0;
34529     for(j=0; j<BITVEC_NINT; j++){
34530       if( aiValues[j] && aiValues[j]!=(i+1) ){
34531         u32 h = BITVEC_HASH(aiValues[j]-1);
34532         p->nSet++;
34533         while( p->u.aHash[h] ){
34534           h++;
34535           if( h>=BITVEC_NINT ) h = 0;
34536         }
34537         p->u.aHash[h] = aiValues[j];
34538       }
34539     }
34540   }
34541 }
34542 
34543 /*
34544 ** Destroy a bitmap object.  Reclaim all memory used.
34545 */
34546 SQLITE_PRIVATE void sqlite3BitvecDestroy(Bitvec *p){
34547   if( p==0 ) return;
34548   if( p->iDivisor ){
34549     unsigned int i;
34550     for(i=0; i<BITVEC_NPTR; i++){
34551       sqlite3BitvecDestroy(p->u.apSub[i]);
34552     }
34553   }
34554   sqlite3_free(p);
34555 }
34556 
34557 /*
34558 ** Return the value of the iSize parameter specified when Bitvec *p
34559 ** was created.
34560 */
34561 SQLITE_PRIVATE u32 sqlite3BitvecSize(Bitvec *p){
34562   return p->iSize;
34563 }
34564 
34565 #ifndef SQLITE_OMIT_BUILTIN_TEST
34566 /*
34567 ** Let V[] be an array of unsigned characters sufficient to hold
34568 ** up to N bits.  Let I be an integer between 0 and N.  0<=I<N.
34569 ** Then the following macros can be used to set, clear, or test
34570 ** individual bits within V.
34571 */
34572 #define SETBIT(V,I)      V[I>>3] |= (1<<(I&7))
34573 #define CLEARBIT(V,I)    V[I>>3] &= ~(1<<(I&7))
34574 #define TESTBIT(V,I)     (V[I>>3]&(1<<(I&7)))!=0
34575 
34576 /*
34577 ** This routine runs an extensive test of the Bitvec code.
34578 **
34579 ** The input is an array of integers that acts as a program
34580 ** to test the Bitvec.  The integers are opcodes followed
34581 ** by 0, 1, or 3 operands, depending on the opcode.  Another
34582 ** opcode follows immediately after the last operand.
34583 **
34584 ** There are 6 opcodes numbered from 0 through 5.  0 is the
34585 ** "halt" opcode and causes the test to end.
34586 **
34587 **    0          Halt and return the number of errors
34588 **    1 N S X    Set N bits beginning with S and incrementing by X
34589 **    2 N S X    Clear N bits beginning with S and incrementing by X
34590 **    3 N        Set N randomly chosen bits
34591 **    4 N        Clear N randomly chosen bits
34592 **    5 N S X    Set N bits from S increment X in array only, not in bitvec
34593 **
34594 ** The opcodes 1 through 4 perform set and clear operations are performed
34595 ** on both a Bitvec object and on a linear array of bits obtained from malloc.
34596 ** Opcode 5 works on the linear array only, not on the Bitvec.
34597 ** Opcode 5 is used to deliberately induce a fault in order to
34598 ** confirm that error detection works.
34599 **
34600 ** At the conclusion of the test the linear array is compared
34601 ** against the Bitvec object.  If there are any differences,
34602 ** an error is returned.  If they are the same, zero is returned.
34603 **
34604 ** If a memory allocation error occurs, return -1.
34605 */
34606 SQLITE_PRIVATE int sqlite3BitvecBuiltinTest(int sz, int *aOp){
34607   Bitvec *pBitvec = 0;
34608   unsigned char *pV = 0;
34609   int rc = -1;
34610   int i, nx, pc, op;
34611   void *pTmpSpace;
34612 
34613   /* Allocate the Bitvec to be tested and a linear array of
34614   ** bits to act as the reference */
34615   pBitvec = sqlite3BitvecCreate( sz );
34616   pV = sqlite3_malloc( (sz+7)/8 + 1 );
34617   pTmpSpace = sqlite3_malloc(BITVEC_SZ);
34618   if( pBitvec==0 || pV==0 || pTmpSpace==0  ) goto bitvec_end;
34619   memset(pV, 0, (sz+7)/8 + 1);
34620 
34621   /* NULL pBitvec tests */
34622   sqlite3BitvecSet(0, 1);
34623   sqlite3BitvecClear(0, 1, pTmpSpace);
34624 
34625   /* Run the program */
34626   pc = 0;
34627   while( (op = aOp[pc])!=0 ){
34628     switch( op ){
34629       case 1:
34630       case 2:
34631       case 5: {
34632         nx = 4;
34633         i = aOp[pc+2] - 1;
34634         aOp[pc+2] += aOp[pc+3];
34635         break;
34636       }
34637       case 3:
34638       case 4:
34639       default: {
34640         nx = 2;
34641         sqlite3_randomness(sizeof(i), &i);
34642         break;
34643       }
34644     }
34645     if( (--aOp[pc+1]) > 0 ) nx = 0;
34646     pc += nx;
34647     i = (i & 0x7fffffff)%sz;
34648     if( (op & 1)!=0 ){
34649       SETBIT(pV, (i+1));
34650       if( op!=5 ){
34651         if( sqlite3BitvecSet(pBitvec, i+1) ) goto bitvec_end;
34652       }
34653     }else{
34654       CLEARBIT(pV, (i+1));
34655       sqlite3BitvecClear(pBitvec, i+1, pTmpSpace);
34656     }
34657   }
34658 
34659   /* Test to make sure the linear array exactly matches the
34660   ** Bitvec object.  Start with the assumption that they do
34661   ** match (rc==0).  Change rc to non-zero if a discrepancy
34662   ** is found.
34663   */
34664   rc = sqlite3BitvecTest(0,0) + sqlite3BitvecTest(pBitvec, sz+1)
34665           + sqlite3BitvecTest(pBitvec, 0)
34666           + (sqlite3BitvecSize(pBitvec) - sz);
34667   for(i=1; i<=sz; i++){
34668     if(  (TESTBIT(pV,i))!=sqlite3BitvecTest(pBitvec,i) ){
34669       rc = i;
34670       break;
34671     }
34672   }
34673 
34674   /* Free allocated structure */
34675 bitvec_end:
34676   sqlite3_free(pTmpSpace);
34677   sqlite3_free(pV);
34678   sqlite3BitvecDestroy(pBitvec);
34679   return rc;
34680 }
34681 #endif /* SQLITE_OMIT_BUILTIN_TEST */
34682 
34683 /************** End of bitvec.c **********************************************/
34684 /************** Begin file pcache.c ******************************************/
34685 /*
34686 ** 2008 August 05
34687 **
34688 ** The author disclaims copyright to this source code.  In place of
34689 ** a legal notice, here is a blessing:
34690 **
34691 **    May you do good and not evil.
34692 **    May you find forgiveness for yourself and forgive others.
34693 **    May you share freely, never taking more than you give.
34694 **
34695 *************************************************************************
34696 ** This file implements that page cache.
34697 */
34698 
34699 /*
34700 ** A complete page cache is an instance of this structure.
34701 */
34702 struct PCache {
34703   PgHdr *pDirty, *pDirtyTail;         /* List of dirty pages in LRU order */
34704   PgHdr *pSynced;                     /* Last synced page in dirty page list */
34705   int nRef;                           /* Number of referenced pages */
34706   int nMax;                           /* Configured cache size */
34707   int szPage;                         /* Size of every page in this cache */
34708   int szExtra;                        /* Size of extra space for each page */
34709   int bPurgeable;                     /* True if pages are on backing store */
34710   int (*xStress)(void*,PgHdr*);       /* Call to try make a page clean */
34711   void *pStress;                      /* Argument to xStress */
34712   sqlite3_pcache *pCache;             /* Pluggable cache module */
34713   PgHdr *pPage1;                      /* Reference to page 1 */
34714 };
34715 
34716 /*
34717 ** Some of the assert() macros in this code are too expensive to run
34718 ** even during normal debugging.  Use them only rarely on long-running
34719 ** tests.  Enable the expensive asserts using the
34720 ** -DSQLITE_ENABLE_EXPENSIVE_ASSERT=1 compile-time option.
34721 */
34722 #ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
34723 # define expensive_assert(X)  assert(X)
34724 #else
34725 # define expensive_assert(X)
34726 #endif
34727 
34728 /********************************** Linked List Management ********************/
34729 
34730 #if !defined(NDEBUG) && defined(SQLITE_ENABLE_EXPENSIVE_ASSERT)
34731 /*
34732 ** Check that the pCache->pSynced variable is set correctly. If it
34733 ** is not, either fail an assert or return zero. Otherwise, return
34734 ** non-zero. This is only used in debugging builds, as follows:
34735 **
34736 **   expensive_assert( pcacheCheckSynced(pCache) );
34737 */
34738 static int pcacheCheckSynced(PCache *pCache){
34739   PgHdr *p;
34740   for(p=pCache->pDirtyTail; p!=pCache->pSynced; p=p->pDirtyPrev){
34741     assert( p->nRef || (p->flags&PGHDR_NEED_SYNC) );
34742   }
34743   return (p==0 || p->nRef || (p->flags&PGHDR_NEED_SYNC)==0);
34744 }
34745 #endif /* !NDEBUG && SQLITE_ENABLE_EXPENSIVE_ASSERT */
34746 
34747 /*
34748 ** Remove page pPage from the list of dirty pages.
34749 */
34750 static void pcacheRemoveFromDirtyList(PgHdr *pPage){
34751   PCache *p = pPage->pCache;
34752 
34753   assert( pPage->pDirtyNext || pPage==p->pDirtyTail );
34754   assert( pPage->pDirtyPrev || pPage==p->pDirty );
34755 
34756   /* Update the PCache1.pSynced variable if necessary. */
34757   if( p->pSynced==pPage ){
34758     PgHdr *pSynced = pPage->pDirtyPrev;
34759     while( pSynced && (pSynced->flags&PGHDR_NEED_SYNC) ){
34760       pSynced = pSynced->pDirtyPrev;
34761     }
34762     p->pSynced = pSynced;
34763   }
34764 
34765   if( pPage->pDirtyNext ){
34766     pPage->pDirtyNext->pDirtyPrev = pPage->pDirtyPrev;
34767   }else{
34768     assert( pPage==p->pDirtyTail );
34769     p->pDirtyTail = pPage->pDirtyPrev;
34770   }
34771   if( pPage->pDirtyPrev ){
34772     pPage->pDirtyPrev->pDirtyNext = pPage->pDirtyNext;
34773   }else{
34774     assert( pPage==p->pDirty );
34775     p->pDirty = pPage->pDirtyNext;
34776   }
34777   pPage->pDirtyNext = 0;
34778   pPage->pDirtyPrev = 0;
34779 
34780   expensive_assert( pcacheCheckSynced(p) );
34781 }
34782 
34783 /*
34784 ** Add page pPage to the head of the dirty list (PCache1.pDirty is set to
34785 ** pPage).
34786 */
34787 static void pcacheAddToDirtyList(PgHdr *pPage){
34788   PCache *p = pPage->pCache;
34789 
34790   assert( pPage->pDirtyNext==0 && pPage->pDirtyPrev==0 && p->pDirty!=pPage );
34791 
34792   pPage->pDirtyNext = p->pDirty;
34793   if( pPage->pDirtyNext ){
34794     assert( pPage->pDirtyNext->pDirtyPrev==0 );
34795     pPage->pDirtyNext->pDirtyPrev = pPage;
34796   }
34797   p->pDirty = pPage;
34798   if( !p->pDirtyTail ){
34799     p->pDirtyTail = pPage;
34800   }
34801   if( !p->pSynced && 0==(pPage->flags&PGHDR_NEED_SYNC) ){
34802     p->pSynced = pPage;
34803   }
34804   expensive_assert( pcacheCheckSynced(p) );
34805 }
34806 
34807 /*
34808 ** Wrapper around the pluggable caches xUnpin method. If the cache is
34809 ** being used for an in-memory database, this function is a no-op.
34810 */
34811 static void pcacheUnpin(PgHdr *p){
34812   PCache *pCache = p->pCache;
34813   if( pCache->bPurgeable ){
34814     if( p->pgno==1 ){
34815       pCache->pPage1 = 0;
34816     }
34817     sqlite3GlobalConfig.pcache.xUnpin(pCache->pCache, p, 0);
34818   }
34819 }
34820 
34821 /*************************************************** General Interfaces ******
34822 **
34823 ** Initialize and shutdown the page cache subsystem. Neither of these
34824 ** functions are threadsafe.
34825 */
34826 SQLITE_PRIVATE int sqlite3PcacheInitialize(void){
34827   if( sqlite3GlobalConfig.pcache.xInit==0 ){
34828     /* IMPLEMENTATION-OF: R-26801-64137 If the xInit() method is NULL, then the
34829     ** built-in default page cache is used instead of the application defined
34830     ** page cache. */
34831     sqlite3PCacheSetDefault();
34832   }
34833   return sqlite3GlobalConfig.pcache.xInit(sqlite3GlobalConfig.pcache.pArg);
34834 }
34835 SQLITE_PRIVATE void sqlite3PcacheShutdown(void){
34836   if( sqlite3GlobalConfig.pcache.xShutdown ){
34837     /* IMPLEMENTATION-OF: R-26000-56589 The xShutdown() method may be NULL. */
34838     sqlite3GlobalConfig.pcache.xShutdown(sqlite3GlobalConfig.pcache.pArg);
34839   }
34840 }
34841 
34842 /*
34843 ** Return the size in bytes of a PCache object.
34844 */
34845 SQLITE_PRIVATE int sqlite3PcacheSize(void){ return sizeof(PCache); }
34846 
34847 /*
34848 ** Create a new PCache object. Storage space to hold the object
34849 ** has already been allocated and is passed in as the p pointer.
34850 ** The caller discovers how much space needs to be allocated by
34851 ** calling sqlite3PcacheSize().
34852 */
34853 SQLITE_PRIVATE void sqlite3PcacheOpen(
34854   int szPage,                  /* Size of every page */
34855   int szExtra,                 /* Extra space associated with each page */
34856   int bPurgeable,              /* True if pages are on backing store */
34857   int (*xStress)(void*,PgHdr*),/* Call to try to make pages clean */
34858   void *pStress,               /* Argument to xStress */
34859   PCache *p                    /* Preallocated space for the PCache */
34860 ){
34861   memset(p, 0, sizeof(PCache));
34862   p->szPage = szPage;
34863   p->szExtra = szExtra;
34864   p->bPurgeable = bPurgeable;
34865   p->xStress = xStress;
34866   p->pStress = pStress;
34867   p->nMax = 100;
34868 }
34869 
34870 /*
34871 ** Change the page size for PCache object. The caller must ensure that there
34872 ** are no outstanding page references when this function is called.
34873 */
34874 SQLITE_PRIVATE void sqlite3PcacheSetPageSize(PCache *pCache, int szPage){
34875   assert( pCache->nRef==0 && pCache->pDirty==0 );
34876   if( pCache->pCache ){
34877     sqlite3GlobalConfig.pcache.xDestroy(pCache->pCache);
34878     pCache->pCache = 0;
34879     pCache->pPage1 = 0;
34880   }
34881   pCache->szPage = szPage;
34882 }
34883 
34884 /*
34885 ** Try to obtain a page from the cache.
34886 */
34887 SQLITE_PRIVATE int sqlite3PcacheFetch(
34888   PCache *pCache,       /* Obtain the page from this cache */
34889   Pgno pgno,            /* Page number to obtain */
34890   int createFlag,       /* If true, create page if it does not exist already */
34891   PgHdr **ppPage        /* Write the page here */
34892 ){
34893   PgHdr *pPage = 0;
34894   int eCreate;
34895 
34896   assert( pCache!=0 );
34897   assert( createFlag==1 || createFlag==0 );
34898   assert( pgno>0 );
34899 
34900   /* If the pluggable cache (sqlite3_pcache*) has not been allocated,
34901   ** allocate it now.
34902   */
34903   if( !pCache->pCache && createFlag ){
34904     sqlite3_pcache *p;
34905     int nByte;
34906     nByte = pCache->szPage + pCache->szExtra + sizeof(PgHdr);
34907     p = sqlite3GlobalConfig.pcache.xCreate(nByte, pCache->bPurgeable);
34908     if( !p ){
34909       return SQLITE_NOMEM;
34910     }
34911     sqlite3GlobalConfig.pcache.xCachesize(p, pCache->nMax);
34912     pCache->pCache = p;
34913   }
34914 
34915   eCreate = createFlag * (1 + (!pCache->bPurgeable || !pCache->pDirty));
34916   if( pCache->pCache ){
34917     pPage = sqlite3GlobalConfig.pcache.xFetch(pCache->pCache, pgno, eCreate);
34918   }
34919 
34920   if( !pPage && eCreate==1 ){
34921     PgHdr *pPg;
34922 
34923     /* Find a dirty page to write-out and recycle. First try to find a
34924     ** page that does not require a journal-sync (one with PGHDR_NEED_SYNC
34925     ** cleared), but if that is not possible settle for any other
34926     ** unreferenced dirty page.
34927     */
34928     expensive_assert( pcacheCheckSynced(pCache) );
34929     for(pPg=pCache->pSynced;
34930         pPg && (pPg->nRef || (pPg->flags&PGHDR_NEED_SYNC));
34931         pPg=pPg->pDirtyPrev
34932     );
34933     pCache->pSynced = pPg;
34934     if( !pPg ){
34935       for(pPg=pCache->pDirtyTail; pPg && pPg->nRef; pPg=pPg->pDirtyPrev);
34936     }
34937     if( pPg ){
34938       int rc;
34939 #ifdef SQLITE_LOG_CACHE_SPILL
34940       sqlite3_log(SQLITE_FULL,
34941                   "spill page %d making room for %d - cache used: %d/%d",
34942                   pPg->pgno, pgno,
34943                   sqlite3GlobalConfig.pcache.xPagecount(pCache->pCache),
34944                   pCache->nMax);
34945 #endif
34946       rc = pCache->xStress(pCache->pStress, pPg);
34947       if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
34948         return rc;
34949       }
34950     }
34951 
34952     pPage = sqlite3GlobalConfig.pcache.xFetch(pCache->pCache, pgno, 2);
34953   }
34954 
34955   if( pPage ){
34956     if( !pPage->pData ){
34957       memset(pPage, 0, sizeof(PgHdr));
34958       pPage->pData = (void *)&pPage[1];
34959       pPage->pExtra = (void*)&((char *)pPage->pData)[pCache->szPage];
34960       memset(pPage->pExtra, 0, pCache->szExtra);
34961       pPage->pCache = pCache;
34962       pPage->pgno = pgno;
34963     }
34964     assert( pPage->pCache==pCache );
34965     assert( pPage->pgno==pgno );
34966     assert( pPage->pData==(void *)&pPage[1] );
34967     assert( pPage->pExtra==(void *)&((char *)&pPage[1])[pCache->szPage] );
34968 
34969     if( 0==pPage->nRef ){
34970       pCache->nRef++;
34971     }
34972     pPage->nRef++;
34973     if( pgno==1 ){
34974       pCache->pPage1 = pPage;
34975     }
34976   }
34977   *ppPage = pPage;
34978   return (pPage==0 && eCreate) ? SQLITE_NOMEM : SQLITE_OK;
34979 }
34980 
34981 /*
34982 ** Decrement the reference count on a page. If the page is clean and the
34983 ** reference count drops to 0, then it is made elible for recycling.
34984 */
34985 SQLITE_PRIVATE void sqlite3PcacheRelease(PgHdr *p){
34986   assert( p->nRef>0 );
34987   p->nRef--;
34988   if( p->nRef==0 ){
34989     PCache *pCache = p->pCache;
34990     pCache->nRef--;
34991     if( (p->flags&PGHDR_DIRTY)==0 ){
34992       pcacheUnpin(p);
34993     }else{
34994       /* Move the page to the head of the dirty list. */
34995       pcacheRemoveFromDirtyList(p);
34996       pcacheAddToDirtyList(p);
34997     }
34998   }
34999 }
35000 
35001 /*
35002 ** Increase the reference count of a supplied page by 1.
35003 */
35004 SQLITE_PRIVATE void sqlite3PcacheRef(PgHdr *p){
35005   assert(p->nRef>0);
35006   p->nRef++;
35007 }
35008 
35009 /*
35010 ** Drop a page from the cache. There must be exactly one reference to the
35011 ** page. This function deletes that reference, so after it returns the
35012 ** page pointed to by p is invalid.
35013 */
35014 SQLITE_PRIVATE void sqlite3PcacheDrop(PgHdr *p){
35015   PCache *pCache;
35016   assert( p->nRef==1 );
35017   if( p->flags&PGHDR_DIRTY ){
35018     pcacheRemoveFromDirtyList(p);
35019   }
35020   pCache = p->pCache;
35021   pCache->nRef--;
35022   if( p->pgno==1 ){
35023     pCache->pPage1 = 0;
35024   }
35025   sqlite3GlobalConfig.pcache.xUnpin(pCache->pCache, p, 1);
35026 }
35027 
35028 /*
35029 ** Make sure the page is marked as dirty. If it isn't dirty already,
35030 ** make it so.
35031 */
35032 SQLITE_PRIVATE void sqlite3PcacheMakeDirty(PgHdr *p){
35033   p->flags &= ~PGHDR_DONT_WRITE;
35034   assert( p->nRef>0 );
35035   if( 0==(p->flags & PGHDR_DIRTY) ){
35036     p->flags |= PGHDR_DIRTY;
35037     pcacheAddToDirtyList( p);
35038   }
35039 }
35040 
35041 /*
35042 ** Make sure the page is marked as clean. If it isn't clean already,
35043 ** make it so.
35044 */
35045 SQLITE_PRIVATE void sqlite3PcacheMakeClean(PgHdr *p){
35046   if( (p->flags & PGHDR_DIRTY) ){
35047     pcacheRemoveFromDirtyList(p);
35048     p->flags &= ~(PGHDR_DIRTY|PGHDR_NEED_SYNC);
35049     if( p->nRef==0 ){
35050       pcacheUnpin(p);
35051     }
35052   }
35053 }
35054 
35055 /*
35056 ** Make every page in the cache clean.
35057 */
35058 SQLITE_PRIVATE void sqlite3PcacheCleanAll(PCache *pCache){
35059   PgHdr *p;
35060   while( (p = pCache->pDirty)!=0 ){
35061     sqlite3PcacheMakeClean(p);
35062   }
35063 }
35064 
35065 /*
35066 ** Clear the PGHDR_NEED_SYNC flag from all dirty pages.
35067 */
35068 SQLITE_PRIVATE void sqlite3PcacheClearSyncFlags(PCache *pCache){
35069   PgHdr *p;
35070   for(p=pCache->pDirty; p; p=p->pDirtyNext){
35071     p->flags &= ~PGHDR_NEED_SYNC;
35072   }
35073   pCache->pSynced = pCache->pDirtyTail;
35074 }
35075 
35076 /*
35077 ** Change the page number of page p to newPgno.
35078 */
35079 SQLITE_PRIVATE void sqlite3PcacheMove(PgHdr *p, Pgno newPgno){
35080   PCache *pCache = p->pCache;
35081   assert( p->nRef>0 );
35082   assert( newPgno>0 );
35083   sqlite3GlobalConfig.pcache.xRekey(pCache->pCache, p, p->pgno, newPgno);
35084   p->pgno = newPgno;
35085   if( (p->flags&PGHDR_DIRTY) && (p->flags&PGHDR_NEED_SYNC) ){
35086     pcacheRemoveFromDirtyList(p);
35087     pcacheAddToDirtyList(p);
35088   }
35089 }
35090 
35091 /*
35092 ** Drop every cache entry whose page number is greater than "pgno". The
35093 ** caller must ensure that there are no outstanding references to any pages
35094 ** other than page 1 with a page number greater than pgno.
35095 **
35096 ** If there is a reference to page 1 and the pgno parameter passed to this
35097 ** function is 0, then the data area associated with page 1 is zeroed, but
35098 ** the page object is not dropped.
35099 */
35100 SQLITE_PRIVATE void sqlite3PcacheTruncate(PCache *pCache, Pgno pgno){
35101   if( pCache->pCache ){
35102     PgHdr *p;
35103     PgHdr *pNext;
35104     for(p=pCache->pDirty; p; p=pNext){
35105       pNext = p->pDirtyNext;
35106       /* This routine never gets call with a positive pgno except right
35107       ** after sqlite3PcacheCleanAll().  So if there are dirty pages,
35108       ** it must be that pgno==0.
35109       */
35110       assert( p->pgno>0 );
35111       if( ALWAYS(p->pgno>pgno) ){
35112         assert( p->flags&PGHDR_DIRTY );
35113         sqlite3PcacheMakeClean(p);
35114       }
35115     }
35116     if( pgno==0 && pCache->pPage1 ){
35117       memset(pCache->pPage1->pData, 0, pCache->szPage);
35118       pgno = 1;
35119     }
35120     sqlite3GlobalConfig.pcache.xTruncate(pCache->pCache, pgno+1);
35121   }
35122 }
35123 
35124 /*
35125 ** Close a cache.
35126 */
35127 SQLITE_PRIVATE void sqlite3PcacheClose(PCache *pCache){
35128   if( pCache->pCache ){
35129     sqlite3GlobalConfig.pcache.xDestroy(pCache->pCache);
35130   }
35131 }
35132 
35133 /*
35134 ** Discard the contents of the cache.
35135 */
35136 SQLITE_PRIVATE void sqlite3PcacheClear(PCache *pCache){
35137   sqlite3PcacheTruncate(pCache, 0);
35138 }
35139 
35140 /*
35141 ** Merge two lists of pages connected by pDirty and in pgno order.
35142 ** Do not both fixing the pDirtyPrev pointers.
35143 */
35144 static PgHdr *pcacheMergeDirtyList(PgHdr *pA, PgHdr *pB){
35145   PgHdr result, *pTail;
35146   pTail = &result;
35147   while( pA && pB ){
35148     if( pA->pgno<pB->pgno ){
35149       pTail->pDirty = pA;
35150       pTail = pA;
35151       pA = pA->pDirty;
35152     }else{
35153       pTail->pDirty = pB;
35154       pTail = pB;
35155       pB = pB->pDirty;
35156     }
35157   }
35158   if( pA ){
35159     pTail->pDirty = pA;
35160   }else if( pB ){
35161     pTail->pDirty = pB;
35162   }else{
35163     pTail->pDirty = 0;
35164   }
35165   return result.pDirty;
35166 }
35167 
35168 /*
35169 ** Sort the list of pages in accending order by pgno.  Pages are
35170 ** connected by pDirty pointers.  The pDirtyPrev pointers are
35171 ** corrupted by this sort.
35172 **
35173 ** Since there cannot be more than 2^31 distinct pages in a database,
35174 ** there cannot be more than 31 buckets required by the merge sorter.
35175 ** One extra bucket is added to catch overflow in case something
35176 ** ever changes to make the previous sentence incorrect.
35177 */
35178 #define N_SORT_BUCKET  32
35179 static PgHdr *pcacheSortDirtyList(PgHdr *pIn){
35180   PgHdr *a[N_SORT_BUCKET], *p;
35181   int i;
35182   memset(a, 0, sizeof(a));
35183   while( pIn ){
35184     p = pIn;
35185     pIn = p->pDirty;
35186     p->pDirty = 0;
35187     for(i=0; ALWAYS(i<N_SORT_BUCKET-1); i++){
35188       if( a[i]==0 ){
35189         a[i] = p;
35190         break;
35191       }else{
35192         p = pcacheMergeDirtyList(a[i], p);
35193         a[i] = 0;
35194       }
35195     }
35196     if( NEVER(i==N_SORT_BUCKET-1) ){
35197       /* To get here, there need to be 2^(N_SORT_BUCKET) elements in
35198       ** the input list.  But that is impossible.
35199       */
35200       a[i] = pcacheMergeDirtyList(a[i], p);
35201     }
35202   }
35203   p = a[0];
35204   for(i=1; i<N_SORT_BUCKET; i++){
35205     p = pcacheMergeDirtyList(p, a[i]);
35206   }
35207   return p;
35208 }
35209 
35210 /*
35211 ** Return a list of all dirty pages in the cache, sorted by page number.
35212 */
35213 SQLITE_PRIVATE PgHdr *sqlite3PcacheDirtyList(PCache *pCache){
35214   PgHdr *p;
35215   for(p=pCache->pDirty; p; p=p->pDirtyNext){
35216     p->pDirty = p->pDirtyNext;
35217   }
35218   return pcacheSortDirtyList(pCache->pDirty);
35219 }
35220 
35221 /*
35222 ** Return the total number of referenced pages held by the cache.
35223 */
35224 SQLITE_PRIVATE int sqlite3PcacheRefCount(PCache *pCache){
35225   return pCache->nRef;
35226 }
35227 
35228 /*
35229 ** Return the number of references to the page supplied as an argument.
35230 */
35231 SQLITE_PRIVATE int sqlite3PcachePageRefcount(PgHdr *p){
35232   return p->nRef;
35233 }
35234 
35235 /*
35236 ** Return the total number of pages in the cache.
35237 */
35238 SQLITE_PRIVATE int sqlite3PcachePagecount(PCache *pCache){
35239   int nPage = 0;
35240   if( pCache->pCache ){
35241     nPage = sqlite3GlobalConfig.pcache.xPagecount(pCache->pCache);
35242   }
35243   return nPage;
35244 }
35245 
35246 #ifdef SQLITE_TEST
35247 /*
35248 ** Get the suggested cache-size value.
35249 */
35250 SQLITE_PRIVATE int sqlite3PcacheGetCachesize(PCache *pCache){
35251   return pCache->nMax;
35252 }
35253 #endif
35254 
35255 /*
35256 ** Set the suggested cache-size value.
35257 */
35258 SQLITE_PRIVATE void sqlite3PcacheSetCachesize(PCache *pCache, int mxPage){
35259   pCache->nMax = mxPage;
35260   if( pCache->pCache ){
35261     sqlite3GlobalConfig.pcache.xCachesize(pCache->pCache, mxPage);
35262   }
35263 }
35264 
35265 #if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG)
35266 /*
35267 ** For all dirty pages currently in the cache, invoke the specified
35268 ** callback. This is only used if the SQLITE_CHECK_PAGES macro is
35269 ** defined.
35270 */
35271 SQLITE_PRIVATE void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *)){
35272   PgHdr *pDirty;
35273   for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext){
35274     xIter(pDirty);
35275   }
35276 }
35277 #endif
35278 
35279 /************** End of pcache.c **********************************************/
35280 /************** Begin file pcache1.c *****************************************/
35281 /*
35282 ** 2008 November 05
35283 **
35284 ** The author disclaims copyright to this source code.  In place of
35285 ** a legal notice, here is a blessing:
35286 **
35287 **    May you do good and not evil.
35288 **    May you find forgiveness for yourself and forgive others.
35289 **    May you share freely, never taking more than you give.
35290 **
35291 *************************************************************************
35292 **
35293 ** This file implements the default page cache implementation (the
35294 ** sqlite3_pcache interface). It also contains part of the implementation
35295 ** of the SQLITE_CONFIG_PAGECACHE and sqlite3_release_memory() features.
35296 ** If the default page cache implementation is overriden, then neither of
35297 ** these two features are available.
35298 */
35299 
35300 
35301 typedef struct PCache1 PCache1;
35302 typedef struct PgHdr1 PgHdr1;
35303 typedef struct PgFreeslot PgFreeslot;
35304 typedef struct PGroup PGroup;
35305 
35306 /* Each page cache (or PCache) belongs to a PGroup.  A PGroup is a set
35307 ** of one or more PCaches that are able to recycle each others unpinned
35308 ** pages when they are under memory pressure.  A PGroup is an instance of
35309 ** the following object.
35310 **
35311 ** This page cache implementation works in one of two modes:
35312 **
35313 **   (1)  Every PCache is the sole member of its own PGroup.  There is
35314 **        one PGroup per PCache.
35315 **
35316 **   (2)  There is a single global PGroup that all PCaches are a member
35317 **        of.
35318 **
35319 ** Mode 1 uses more memory (since PCache instances are not able to rob
35320 ** unused pages from other PCaches) but it also operates without a mutex,
35321 ** and is therefore often faster.  Mode 2 requires a mutex in order to be
35322 ** threadsafe, but is able recycle pages more efficient.
35323 **
35324 ** For mode (1), PGroup.mutex is NULL.  For mode (2) there is only a single
35325 ** PGroup which is the pcache1.grp global variable and its mutex is
35326 ** SQLITE_MUTEX_STATIC_LRU.
35327 */
35328 struct PGroup {
35329   sqlite3_mutex *mutex;          /* MUTEX_STATIC_LRU or NULL */
35330   int nMaxPage;                  /* Sum of nMax for purgeable caches */
35331   int nMinPage;                  /* Sum of nMin for purgeable caches */
35332   int mxPinned;                  /* nMaxpage + 10 - nMinPage */
35333   int nCurrentPage;              /* Number of purgeable pages allocated */
35334   PgHdr1 *pLruHead, *pLruTail;   /* LRU list of unpinned pages */
35335 };
35336 
35337 /* Each page cache is an instance of the following object.  Every
35338 ** open database file (including each in-memory database and each
35339 ** temporary or transient database) has a single page cache which
35340 ** is an instance of this object.
35341 **
35342 ** Pointers to structures of this type are cast and returned as
35343 ** opaque sqlite3_pcache* handles.
35344 */
35345 struct PCache1 {
35346   /* Cache configuration parameters. Page size (szPage) and the purgeable
35347   ** flag (bPurgeable) are set when the cache is created. nMax may be
35348   ** modified at any time by a call to the pcache1CacheSize() method.
35349   ** The PGroup mutex must be held when accessing nMax.
35350   */
35351   PGroup *pGroup;                     /* PGroup this cache belongs to */
35352   int szPage;                         /* Size of allocated pages in bytes */
35353   int bPurgeable;                     /* True if cache is purgeable */
35354   unsigned int nMin;                  /* Minimum number of pages reserved */
35355   unsigned int nMax;                  /* Configured "cache_size" value */
35356   unsigned int n90pct;                /* nMax*9/10 */
35357 
35358   /* Hash table of all pages. The following variables may only be accessed
35359   ** when the accessor is holding the PGroup mutex.
35360   */
35361   unsigned int nRecyclable;           /* Number of pages in the LRU list */
35362   unsigned int nPage;                 /* Total number of pages in apHash */
35363   unsigned int nHash;                 /* Number of slots in apHash[] */
35364   PgHdr1 **apHash;                    /* Hash table for fast lookup by key */
35365 
35366   unsigned int iMaxKey;               /* Largest key seen since xTruncate() */
35367 };
35368 
35369 /*
35370 ** Each cache entry is represented by an instance of the following
35371 ** structure. A buffer of PgHdr1.pCache->szPage bytes is allocated
35372 ** directly before this structure in memory (see the PGHDR1_TO_PAGE()
35373 ** macro below).
35374 */
35375 struct PgHdr1 {
35376   unsigned int iKey;             /* Key value (page number) */
35377   PgHdr1 *pNext;                 /* Next in hash table chain */
35378   PCache1 *pCache;               /* Cache that currently owns this page */
35379   PgHdr1 *pLruNext;              /* Next in LRU list of unpinned pages */
35380   PgHdr1 *pLruPrev;              /* Previous in LRU list of unpinned pages */
35381 };
35382 
35383 /*
35384 ** Free slots in the allocator used to divide up the buffer provided using
35385 ** the SQLITE_CONFIG_PAGECACHE mechanism.
35386 */
35387 struct PgFreeslot {
35388   PgFreeslot *pNext;  /* Next free slot */
35389 };
35390 
35391 /*
35392 ** Global data used by this cache.
35393 */
35394 static SQLITE_WSD struct PCacheGlobal {
35395   PGroup grp;                    /* The global PGroup for mode (2) */
35396 
35397   /* Variables related to SQLITE_CONFIG_PAGECACHE settings.  The
35398   ** szSlot, nSlot, pStart, pEnd, nReserve, and isInit values are all
35399   ** fixed at sqlite3_initialize() time and do not require mutex protection.
35400   ** The nFreeSlot and pFree values do require mutex protection.
35401   */
35402   int isInit;                    /* True if initialized */
35403   int szSlot;                    /* Size of each free slot */
35404   int nSlot;                     /* The number of pcache slots */
35405   int nReserve;                  /* Try to keep nFreeSlot above this */
35406   void *pStart, *pEnd;           /* Bounds of pagecache malloc range */
35407   /* Above requires no mutex.  Use mutex below for variable that follow. */
35408   sqlite3_mutex *mutex;          /* Mutex for accessing the following: */
35409   int nFreeSlot;                 /* Number of unused pcache slots */
35410   PgFreeslot *pFree;             /* Free page blocks */
35411   /* The following value requires a mutex to change.  We skip the mutex on
35412   ** reading because (1) most platforms read a 32-bit integer atomically and
35413   ** (2) even if an incorrect value is read, no great harm is done since this
35414   ** is really just an optimization. */
35415   int bUnderPressure;            /* True if low on PAGECACHE memory */
35416 } pcache1_g;
35417 
35418 /*
35419 ** All code in this file should access the global structure above via the
35420 ** alias "pcache1". This ensures that the WSD emulation is used when
35421 ** compiling for systems that do not support real WSD.
35422 */
35423 #define pcache1 (GLOBAL(struct PCacheGlobal, pcache1_g))
35424 
35425 /*
35426 ** When a PgHdr1 structure is allocated, the associated PCache1.szPage
35427 ** bytes of data are located directly before it in memory (i.e. the total
35428 ** size of the allocation is sizeof(PgHdr1)+PCache1.szPage byte). The
35429 ** PGHDR1_TO_PAGE() macro takes a pointer to a PgHdr1 structure as
35430 ** an argument and returns a pointer to the associated block of szPage
35431 ** bytes. The PAGE_TO_PGHDR1() macro does the opposite: its argument is
35432 ** a pointer to a block of szPage bytes of data and the return value is
35433 ** a pointer to the associated PgHdr1 structure.
35434 **
35435 **   assert( PGHDR1_TO_PAGE(PAGE_TO_PGHDR1(pCache, X))==X );
35436 */
35437 #define PGHDR1_TO_PAGE(p)    (void*)(((char*)p) - p->pCache->szPage)
35438 #define PAGE_TO_PGHDR1(c, p) (PgHdr1*)(((char*)p) + c->szPage)
35439 
35440 /*
35441 ** Macros to enter and leave the PCache LRU mutex.
35442 */
35443 #define pcache1EnterMutex(X) sqlite3_mutex_enter((X)->mutex)
35444 #define pcache1LeaveMutex(X) sqlite3_mutex_leave((X)->mutex)
35445 
35446 /******************************************************************************/
35447 /******** Page Allocation/SQLITE_CONFIG_PCACHE Related Functions **************/
35448 
35449 /*
35450 ** This function is called during initialization if a static buffer is
35451 ** supplied to use for the page-cache by passing the SQLITE_CONFIG_PAGECACHE
35452 ** verb to sqlite3_config(). Parameter pBuf points to an allocation large
35453 ** enough to contain 'n' buffers of 'sz' bytes each.
35454 **
35455 ** This routine is called from sqlite3_initialize() and so it is guaranteed
35456 ** to be serialized already.  There is no need for further mutexing.
35457 */
35458 SQLITE_PRIVATE void sqlite3PCacheBufferSetup(void *pBuf, int sz, int n){
35459   if( pcache1.isInit ){
35460     PgFreeslot *p;
35461     sz = ROUNDDOWN8(sz);
35462     pcache1.szSlot = sz;
35463     pcache1.nSlot = pcache1.nFreeSlot = n;
35464     pcache1.nReserve = n>90 ? 10 : (n/10 + 1);
35465     pcache1.pStart = pBuf;
35466     pcache1.pFree = 0;
35467     pcache1.bUnderPressure = 0;
35468     while( n-- ){
35469       p = (PgFreeslot*)pBuf;
35470       p->pNext = pcache1.pFree;
35471       pcache1.pFree = p;
35472       pBuf = (void*)&((char*)pBuf)[sz];
35473     }
35474     pcache1.pEnd = pBuf;
35475   }
35476 }
35477 
35478 /*
35479 ** Malloc function used within this file to allocate space from the buffer
35480 ** configured using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no
35481 ** such buffer exists or there is no space left in it, this function falls
35482 ** back to sqlite3Malloc().
35483 **
35484 ** Multiple threads can run this routine at the same time.  Global variables
35485 ** in pcache1 need to be protected via mutex.
35486 */
35487 static void *pcache1Alloc(int nByte){
35488   void *p = 0;
35489   assert( sqlite3_mutex_notheld(pcache1.grp.mutex) );
35490   sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, nByte);
35491   if( nByte<=pcache1.szSlot ){
35492     sqlite3_mutex_enter(pcache1.mutex);
35493     p = (PgHdr1 *)pcache1.pFree;
35494     if( p ){
35495       pcache1.pFree = pcache1.pFree->pNext;
35496       pcache1.nFreeSlot--;
35497       pcache1.bUnderPressure = pcache1.nFreeSlot<pcache1.nReserve;
35498       assert( pcache1.nFreeSlot>=0 );
35499       sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, 1);
35500     }
35501     sqlite3_mutex_leave(pcache1.mutex);
35502   }
35503   if( p==0 ){
35504     /* Memory is not available in the SQLITE_CONFIG_PAGECACHE pool.  Get
35505     ** it from sqlite3Malloc instead.
35506     */
35507     p = sqlite3Malloc(nByte);
35508     if( p ){
35509       int sz = sqlite3MallocSize(p);
35510       sqlite3_mutex_enter(pcache1.mutex);
35511       sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, sz);
35512       sqlite3_mutex_leave(pcache1.mutex);
35513     }
35514     sqlite3MemdebugSetType(p, MEMTYPE_PCACHE);
35515   }
35516   return p;
35517 }
35518 
35519 /*
35520 ** Free an allocated buffer obtained from pcache1Alloc().
35521 */
35522 static void pcache1Free(void *p){
35523   if( p==0 ) return;
35524   if( p>=pcache1.pStart && p<pcache1.pEnd ){
35525     PgFreeslot *pSlot;
35526     sqlite3_mutex_enter(pcache1.mutex);
35527     sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, -1);
35528     pSlot = (PgFreeslot*)p;
35529     pSlot->pNext = pcache1.pFree;
35530     pcache1.pFree = pSlot;
35531     pcache1.nFreeSlot++;
35532     pcache1.bUnderPressure = pcache1.nFreeSlot<pcache1.nReserve;
35533     assert( pcache1.nFreeSlot<=pcache1.nSlot );
35534     sqlite3_mutex_leave(pcache1.mutex);
35535   }else{
35536     int iSize;
35537     assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) );
35538     sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
35539     iSize = sqlite3MallocSize(p);
35540     sqlite3_mutex_enter(pcache1.mutex);
35541     sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, -iSize);
35542     sqlite3_mutex_leave(pcache1.mutex);
35543     sqlite3_free(p);
35544   }
35545 }
35546 
35547 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
35548 /*
35549 ** Return the size of a pcache allocation
35550 */
35551 static int pcache1MemSize(void *p){
35552   if( p>=pcache1.pStart && p<pcache1.pEnd ){
35553     return pcache1.szSlot;
35554   }else{
35555     int iSize;
35556     assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) );
35557     sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
35558     iSize = sqlite3MallocSize(p);
35559     sqlite3MemdebugSetType(p, MEMTYPE_PCACHE);
35560     return iSize;
35561   }
35562 }
35563 #endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
35564 
35565 /*
35566 ** Allocate a new page object initially associated with cache pCache.
35567 */
35568 static PgHdr1 *pcache1AllocPage(PCache1 *pCache){
35569   int nByte = sizeof(PgHdr1) + pCache->szPage;
35570   void *pPg = pcache1Alloc(nByte);
35571   PgHdr1 *p;
35572   if( pPg ){
35573     p = PAGE_TO_PGHDR1(pCache, pPg);
35574     if( pCache->bPurgeable ){
35575       pCache->pGroup->nCurrentPage++;
35576     }
35577   }else{
35578     p = 0;
35579   }
35580   return p;
35581 }
35582 
35583 /*
35584 ** Free a page object allocated by pcache1AllocPage().
35585 **
35586 ** The pointer is allowed to be NULL, which is prudent.  But it turns out
35587 ** that the current implementation happens to never call this routine
35588 ** with a NULL pointer, so we mark the NULL test with ALWAYS().
35589 */
35590 static void pcache1FreePage(PgHdr1 *p){
35591   if( ALWAYS(p) ){
35592     PCache1 *pCache = p->pCache;
35593     if( pCache->bPurgeable ){
35594       pCache->pGroup->nCurrentPage--;
35595     }
35596     pcache1Free(PGHDR1_TO_PAGE(p));
35597   }
35598 }
35599 
35600 /*
35601 ** Malloc function used by SQLite to obtain space from the buffer configured
35602 ** using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no such buffer
35603 ** exists, this function falls back to sqlite3Malloc().
35604 */
35605 SQLITE_PRIVATE void *sqlite3PageMalloc(int sz){
35606   return pcache1Alloc(sz);
35607 }
35608 
35609 /*
35610 ** Free an allocated buffer obtained from sqlite3PageMalloc().
35611 */
35612 SQLITE_PRIVATE void sqlite3PageFree(void *p){
35613   pcache1Free(p);
35614 }
35615 
35616 
35617 /*
35618 ** Return true if it desirable to avoid allocating a new page cache
35619 ** entry.
35620 **
35621 ** If memory was allocated specifically to the page cache using
35622 ** SQLITE_CONFIG_PAGECACHE but that memory has all been used, then
35623 ** it is desirable to avoid allocating a new page cache entry because
35624 ** presumably SQLITE_CONFIG_PAGECACHE was suppose to be sufficient
35625 ** for all page cache needs and we should not need to spill the
35626 ** allocation onto the heap.
35627 **
35628 ** Or, the heap is used for all page cache memory put the heap is
35629 ** under memory pressure, then again it is desirable to avoid
35630 ** allocating a new page cache entry in order to avoid stressing
35631 ** the heap even further.
35632 */
35633 static int pcache1UnderMemoryPressure(PCache1 *pCache){
35634   if( pcache1.nSlot && pCache->szPage<=pcache1.szSlot ){
35635     return pcache1.bUnderPressure;
35636   }else{
35637     return sqlite3HeapNearlyFull();
35638   }
35639 }
35640 
35641 /******************************************************************************/
35642 /******** General Implementation Functions ************************************/
35643 
35644 /*
35645 ** This function is used to resize the hash table used by the cache passed
35646 ** as the first argument.
35647 **
35648 ** The PCache mutex must be held when this function is called.
35649 */
35650 static int pcache1ResizeHash(PCache1 *p){
35651   PgHdr1 **apNew;
35652   unsigned int nNew;
35653   unsigned int i;
35654 
35655   assert( sqlite3_mutex_held(p->pGroup->mutex) );
35656 
35657   nNew = p->nHash*2;
35658   if( nNew<256 ){
35659     nNew = 256;
35660   }
35661 
35662   pcache1LeaveMutex(p->pGroup);
35663   if( p->nHash ){ sqlite3BeginBenignMalloc(); }
35664   apNew = (PgHdr1 **)sqlite3_malloc(sizeof(PgHdr1 *)*nNew);
35665   if( p->nHash ){ sqlite3EndBenignMalloc(); }
35666   pcache1EnterMutex(p->pGroup);
35667   if( apNew ){
35668     memset(apNew, 0, sizeof(PgHdr1 *)*nNew);
35669     for(i=0; i<p->nHash; i++){
35670       PgHdr1 *pPage;
35671       PgHdr1 *pNext = p->apHash[i];
35672       while( (pPage = pNext)!=0 ){
35673         unsigned int h = pPage->iKey % nNew;
35674         pNext = pPage->pNext;
35675         pPage->pNext = apNew[h];
35676         apNew[h] = pPage;
35677       }
35678     }
35679     sqlite3_free(p->apHash);
35680     p->apHash = apNew;
35681     p->nHash = nNew;
35682   }
35683 
35684   return (p->apHash ? SQLITE_OK : SQLITE_NOMEM);
35685 }
35686 
35687 /*
35688 ** This function is used internally to remove the page pPage from the
35689 ** PGroup LRU list, if is part of it. If pPage is not part of the PGroup
35690 ** LRU list, then this function is a no-op.
35691 **
35692 ** The PGroup mutex must be held when this function is called.
35693 **
35694 ** If pPage is NULL then this routine is a no-op.
35695 */
35696 static void pcache1PinPage(PgHdr1 *pPage){
35697   PCache1 *pCache;
35698   PGroup *pGroup;
35699 
35700   if( pPage==0 ) return;
35701   pCache = pPage->pCache;
35702   pGroup = pCache->pGroup;
35703   assert( sqlite3_mutex_held(pGroup->mutex) );
35704   if( pPage->pLruNext || pPage==pGroup->pLruTail ){
35705     if( pPage->pLruPrev ){
35706       pPage->pLruPrev->pLruNext = pPage->pLruNext;
35707     }
35708     if( pPage->pLruNext ){
35709       pPage->pLruNext->pLruPrev = pPage->pLruPrev;
35710     }
35711     if( pGroup->pLruHead==pPage ){
35712       pGroup->pLruHead = pPage->pLruNext;
35713     }
35714     if( pGroup->pLruTail==pPage ){
35715       pGroup->pLruTail = pPage->pLruPrev;
35716     }
35717     pPage->pLruNext = 0;
35718     pPage->pLruPrev = 0;
35719     pPage->pCache->nRecyclable--;
35720   }
35721 }
35722 
35723 
35724 /*
35725 ** Remove the page supplied as an argument from the hash table
35726 ** (PCache1.apHash structure) that it is currently stored in.
35727 **
35728 ** The PGroup mutex must be held when this function is called.
35729 */
35730 static void pcache1RemoveFromHash(PgHdr1 *pPage){
35731   unsigned int h;
35732   PCache1 *pCache = pPage->pCache;
35733   PgHdr1 **pp;
35734 
35735   assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
35736   h = pPage->iKey % pCache->nHash;
35737   for(pp=&pCache->apHash[h]; (*pp)!=pPage; pp=&(*pp)->pNext);
35738   *pp = (*pp)->pNext;
35739 
35740   pCache->nPage--;
35741 }
35742 
35743 /*
35744 ** If there are currently more than nMaxPage pages allocated, try
35745 ** to recycle pages to reduce the number allocated to nMaxPage.
35746 */
35747 static void pcache1EnforceMaxPage(PGroup *pGroup){
35748   assert( sqlite3_mutex_held(pGroup->mutex) );
35749   while( pGroup->nCurrentPage>pGroup->nMaxPage && pGroup->pLruTail ){
35750     PgHdr1 *p = pGroup->pLruTail;
35751     assert( p->pCache->pGroup==pGroup );
35752     pcache1PinPage(p);
35753     pcache1RemoveFromHash(p);
35754     pcache1FreePage(p);
35755   }
35756 }
35757 
35758 /*
35759 ** Discard all pages from cache pCache with a page number (key value)
35760 ** greater than or equal to iLimit. Any pinned pages that meet this
35761 ** criteria are unpinned before they are discarded.
35762 **
35763 ** The PCache mutex must be held when this function is called.
35764 */
35765 static void pcache1TruncateUnsafe(
35766   PCache1 *pCache,             /* The cache to truncate */
35767   unsigned int iLimit          /* Drop pages with this pgno or larger */
35768 ){
35769   TESTONLY( unsigned int nPage = 0; )  /* To assert pCache->nPage is correct */
35770   unsigned int h;
35771   assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
35772   for(h=0; h<pCache->nHash; h++){
35773     PgHdr1 **pp = &pCache->apHash[h];
35774     PgHdr1 *pPage;
35775     while( (pPage = *pp)!=0 ){
35776       if( pPage->iKey>=iLimit ){
35777         pCache->nPage--;
35778         *pp = pPage->pNext;
35779         pcache1PinPage(pPage);
35780         pcache1FreePage(pPage);
35781       }else{
35782         pp = &pPage->pNext;
35783         TESTONLY( nPage++; )
35784       }
35785     }
35786   }
35787   assert( pCache->nPage==nPage );
35788 }
35789 
35790 /******************************************************************************/
35791 /******** sqlite3_pcache Methods **********************************************/
35792 
35793 /*
35794 ** Implementation of the sqlite3_pcache.xInit method.
35795 */
35796 static int pcache1Init(void *NotUsed){
35797   UNUSED_PARAMETER(NotUsed);
35798   assert( pcache1.isInit==0 );
35799   memset(&pcache1, 0, sizeof(pcache1));
35800   if( sqlite3GlobalConfig.bCoreMutex ){
35801     pcache1.grp.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU);
35802     pcache1.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_PMEM);
35803   }
35804   pcache1.grp.mxPinned = 10;
35805   pcache1.isInit = 1;
35806   return SQLITE_OK;
35807 }
35808 
35809 /*
35810 ** Implementation of the sqlite3_pcache.xShutdown method.
35811 ** Note that the static mutex allocated in xInit does
35812 ** not need to be freed.
35813 */
35814 static void pcache1Shutdown(void *NotUsed){
35815   UNUSED_PARAMETER(NotUsed);
35816   assert( pcache1.isInit!=0 );
35817   memset(&pcache1, 0, sizeof(pcache1));
35818 }
35819 
35820 /*
35821 ** Implementation of the sqlite3_pcache.xCreate method.
35822 **
35823 ** Allocate a new cache.
35824 */
35825 static sqlite3_pcache *pcache1Create(int szPage, int bPurgeable){
35826   PCache1 *pCache;      /* The newly created page cache */
35827   PGroup *pGroup;       /* The group the new page cache will belong to */
35828   int sz;               /* Bytes of memory required to allocate the new cache */
35829 
35830   /*
35831   ** The seperateCache variable is true if each PCache has its own private
35832   ** PGroup.  In other words, separateCache is true for mode (1) where no
35833   ** mutexing is required.
35834   **
35835   **   *  Always use a unified cache (mode-2) if ENABLE_MEMORY_MANAGEMENT
35836   **
35837   **   *  Always use a unified cache in single-threaded applications
35838   **
35839   **   *  Otherwise (if multi-threaded and ENABLE_MEMORY_MANAGEMENT is off)
35840   **      use separate caches (mode-1)
35841   */
35842 #if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) || SQLITE_THREADSAFE==0
35843   const int separateCache = 0;
35844 #else
35845   int separateCache = sqlite3GlobalConfig.bCoreMutex>0;
35846 #endif
35847 
35848   sz = sizeof(PCache1) + sizeof(PGroup)*separateCache;
35849   pCache = (PCache1 *)sqlite3_malloc(sz);
35850   if( pCache ){
35851     memset(pCache, 0, sz);
35852     if( separateCache ){
35853       pGroup = (PGroup*)&pCache[1];
35854       pGroup->mxPinned = 10;
35855     }else{
35856       pGroup = &pcache1.grp;
35857     }
35858     pCache->pGroup = pGroup;
35859     pCache->szPage = szPage;
35860     pCache->bPurgeable = (bPurgeable ? 1 : 0);
35861     if( bPurgeable ){
35862       pCache->nMin = 10;
35863       pcache1EnterMutex(pGroup);
35864       pGroup->nMinPage += pCache->nMin;
35865       pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
35866       pcache1LeaveMutex(pGroup);
35867     }
35868   }
35869   return (sqlite3_pcache *)pCache;
35870 }
35871 
35872 /*
35873 ** Implementation of the sqlite3_pcache.xCachesize method.
35874 **
35875 ** Configure the cache_size limit for a cache.
35876 */
35877 static void pcache1Cachesize(sqlite3_pcache *p, int nMax){
35878   PCache1 *pCache = (PCache1 *)p;
35879   if( pCache->bPurgeable ){
35880     PGroup *pGroup = pCache->pGroup;
35881     pcache1EnterMutex(pGroup);
35882     pGroup->nMaxPage += (nMax - pCache->nMax);
35883     pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
35884     pCache->nMax = nMax;
35885     pCache->n90pct = pCache->nMax*9/10;
35886     pcache1EnforceMaxPage(pGroup);
35887     pcache1LeaveMutex(pGroup);
35888   }
35889 }
35890 
35891 /*
35892 ** Implementation of the sqlite3_pcache.xPagecount method.
35893 */
35894 static int pcache1Pagecount(sqlite3_pcache *p){
35895   int n;
35896   PCache1 *pCache = (PCache1*)p;
35897   pcache1EnterMutex(pCache->pGroup);
35898   n = pCache->nPage;
35899   pcache1LeaveMutex(pCache->pGroup);
35900   return n;
35901 }
35902 
35903 /*
35904 ** Implementation of the sqlite3_pcache.xFetch method.
35905 **
35906 ** Fetch a page by key value.
35907 **
35908 ** Whether or not a new page may be allocated by this function depends on
35909 ** the value of the createFlag argument.  0 means do not allocate a new
35910 ** page.  1 means allocate a new page if space is easily available.  2
35911 ** means to try really hard to allocate a new page.
35912 **
35913 ** For a non-purgeable cache (a cache used as the storage for an in-memory
35914 ** database) there is really no difference between createFlag 1 and 2.  So
35915 ** the calling function (pcache.c) will never have a createFlag of 1 on
35916 ** a non-purgable cache.
35917 **
35918 ** There are three different approaches to obtaining space for a page,
35919 ** depending on the value of parameter createFlag (which may be 0, 1 or 2).
35920 **
35921 **   1. Regardless of the value of createFlag, the cache is searched for a
35922 **      copy of the requested page. If one is found, it is returned.
35923 **
35924 **   2. If createFlag==0 and the page is not already in the cache, NULL is
35925 **      returned.
35926 **
35927 **   3. If createFlag is 1, and the page is not already in the cache, then
35928 **      return NULL (do not allocate a new page) if any of the following
35929 **      conditions are true:
35930 **
35931 **       (a) the number of pages pinned by the cache is greater than
35932 **           PCache1.nMax, or
35933 **
35934 **       (b) the number of pages pinned by the cache is greater than
35935 **           the sum of nMax for all purgeable caches, less the sum of
35936 **           nMin for all other purgeable caches, or
35937 **
35938 **   4. If none of the first three conditions apply and the cache is marked
35939 **      as purgeable, and if one of the following is true:
35940 **
35941 **       (a) The number of pages allocated for the cache is already
35942 **           PCache1.nMax, or
35943 **
35944 **       (b) The number of pages allocated for all purgeable caches is
35945 **           already equal to or greater than the sum of nMax for all
35946 **           purgeable caches,
35947 **
35948 **       (c) The system is under memory pressure and wants to avoid
35949 **           unnecessary pages cache entry allocations
35950 **
35951 **      then attempt to recycle a page from the LRU list. If it is the right
35952 **      size, return the recycled buffer. Otherwise, free the buffer and
35953 **      proceed to step 5.
35954 **
35955 **   5. Otherwise, allocate and return a new page buffer.
35956 */
35957 static void *pcache1Fetch(sqlite3_pcache *p, unsigned int iKey, int createFlag){
35958   int nPinned;
35959   PCache1 *pCache = (PCache1 *)p;
35960   PGroup *pGroup;
35961   PgHdr1 *pPage = 0;
35962 
35963   assert( pCache->bPurgeable || createFlag!=1 );
35964   assert( pCache->bPurgeable || pCache->nMin==0 );
35965   assert( pCache->bPurgeable==0 || pCache->nMin==10 );
35966   assert( pCache->nMin==0 || pCache->bPurgeable );
35967   pcache1EnterMutex(pGroup = pCache->pGroup);
35968 
35969   /* Step 1: Search the hash table for an existing entry. */
35970   if( pCache->nHash>0 ){
35971     unsigned int h = iKey % pCache->nHash;
35972     for(pPage=pCache->apHash[h]; pPage&&pPage->iKey!=iKey; pPage=pPage->pNext);
35973   }
35974 
35975   /* Step 2: Abort if no existing page is found and createFlag is 0 */
35976   if( pPage || createFlag==0 ){
35977     pcache1PinPage(pPage);
35978     goto fetch_out;
35979   }
35980 
35981   /* The pGroup local variable will normally be initialized by the
35982   ** pcache1EnterMutex() macro above.  But if SQLITE_MUTEX_OMIT is defined,
35983   ** then pcache1EnterMutex() is a no-op, so we have to initialize the
35984   ** local variable here.  Delaying the initialization of pGroup is an
35985   ** optimization:  The common case is to exit the module before reaching
35986   ** this point.
35987   */
35988 #ifdef SQLITE_MUTEX_OMIT
35989   pGroup = pCache->pGroup;
35990 #endif
35991 
35992 
35993   /* Step 3: Abort if createFlag is 1 but the cache is nearly full */
35994   nPinned = pCache->nPage - pCache->nRecyclable;
35995   assert( nPinned>=0 );
35996   assert( pGroup->mxPinned == pGroup->nMaxPage + 10 - pGroup->nMinPage );
35997   assert( pCache->n90pct == pCache->nMax*9/10 );
35998   if( createFlag==1 && (
35999         nPinned>=pGroup->mxPinned
36000      || nPinned>=(int)pCache->n90pct
36001      || pcache1UnderMemoryPressure(pCache)
36002   )){
36003     goto fetch_out;
36004   }
36005 
36006   if( pCache->nPage>=pCache->nHash && pcache1ResizeHash(pCache) ){
36007     goto fetch_out;
36008   }
36009 
36010   /* Step 4. Try to recycle a page. */
36011   if( pCache->bPurgeable && pGroup->pLruTail && (
36012          (pCache->nPage+1>=pCache->nMax)
36013       || pGroup->nCurrentPage>=pGroup->nMaxPage
36014       || pcache1UnderMemoryPressure(pCache)
36015   )){
36016     PCache1 *pOtherCache;
36017     pPage = pGroup->pLruTail;
36018     pcache1RemoveFromHash(pPage);
36019     pcache1PinPage(pPage);
36020     if( (pOtherCache = pPage->pCache)->szPage!=pCache->szPage ){
36021       pcache1FreePage(pPage);
36022       pPage = 0;
36023     }else{
36024       pGroup->nCurrentPage -=
36025                (pOtherCache->bPurgeable - pCache->bPurgeable);
36026     }
36027   }
36028 
36029   /* Step 5. If a usable page buffer has still not been found,
36030   ** attempt to allocate a new one.
36031   */
36032   if( !pPage ){
36033     if( createFlag==1 ) sqlite3BeginBenignMalloc();
36034     pcache1LeaveMutex(pGroup);
36035     pPage = pcache1AllocPage(pCache);
36036     pcache1EnterMutex(pGroup);
36037     if( createFlag==1 ) sqlite3EndBenignMalloc();
36038   }
36039 
36040   if( pPage ){
36041     unsigned int h = iKey % pCache->nHash;
36042     pCache->nPage++;
36043     pPage->iKey = iKey;
36044     pPage->pNext = pCache->apHash[h];
36045     pPage->pCache = pCache;
36046     pPage->pLruPrev = 0;
36047     pPage->pLruNext = 0;
36048     *(void **)(PGHDR1_TO_PAGE(pPage)) = 0;
36049     pCache->apHash[h] = pPage;
36050   }
36051 
36052 fetch_out:
36053   if( pPage && iKey>pCache->iMaxKey ){
36054     pCache->iMaxKey = iKey;
36055   }
36056   pcache1LeaveMutex(pGroup);
36057   return (pPage ? PGHDR1_TO_PAGE(pPage) : 0);
36058 }
36059 
36060 
36061 /*
36062 ** Implementation of the sqlite3_pcache.xUnpin method.
36063 **
36064 ** Mark a page as unpinned (eligible for asynchronous recycling).
36065 */
36066 static void pcache1Unpin(sqlite3_pcache *p, void *pPg, int reuseUnlikely){
36067   PCache1 *pCache = (PCache1 *)p;
36068   PgHdr1 *pPage = PAGE_TO_PGHDR1(pCache, pPg);
36069   PGroup *pGroup = pCache->pGroup;
36070 
36071   assert( pPage->pCache==pCache );
36072   pcache1EnterMutex(pGroup);
36073 
36074   /* It is an error to call this function if the page is already
36075   ** part of the PGroup LRU list.
36076   */
36077   assert( pPage->pLruPrev==0 && pPage->pLruNext==0 );
36078   assert( pGroup->pLruHead!=pPage && pGroup->pLruTail!=pPage );
36079 
36080   if( reuseUnlikely || pGroup->nCurrentPage>pGroup->nMaxPage ){
36081     pcache1RemoveFromHash(pPage);
36082     pcache1FreePage(pPage);
36083   }else{
36084     /* Add the page to the PGroup LRU list. */
36085     if( pGroup->pLruHead ){
36086       pGroup->pLruHead->pLruPrev = pPage;
36087       pPage->pLruNext = pGroup->pLruHead;
36088       pGroup->pLruHead = pPage;
36089     }else{
36090       pGroup->pLruTail = pPage;
36091       pGroup->pLruHead = pPage;
36092     }
36093     pCache->nRecyclable++;
36094   }
36095 
36096   pcache1LeaveMutex(pCache->pGroup);
36097 }
36098 
36099 /*
36100 ** Implementation of the sqlite3_pcache.xRekey method.
36101 */
36102 static void pcache1Rekey(
36103   sqlite3_pcache *p,
36104   void *pPg,
36105   unsigned int iOld,
36106   unsigned int iNew
36107 ){
36108   PCache1 *pCache = (PCache1 *)p;
36109   PgHdr1 *pPage = PAGE_TO_PGHDR1(pCache, pPg);
36110   PgHdr1 **pp;
36111   unsigned int h;
36112   assert( pPage->iKey==iOld );
36113   assert( pPage->pCache==pCache );
36114 
36115   pcache1EnterMutex(pCache->pGroup);
36116 
36117   h = iOld%pCache->nHash;
36118   pp = &pCache->apHash[h];
36119   while( (*pp)!=pPage ){
36120     pp = &(*pp)->pNext;
36121   }
36122   *pp = pPage->pNext;
36123 
36124   h = iNew%pCache->nHash;
36125   pPage->iKey = iNew;
36126   pPage->pNext = pCache->apHash[h];
36127   pCache->apHash[h] = pPage;
36128   if( iNew>pCache->iMaxKey ){
36129     pCache->iMaxKey = iNew;
36130   }
36131 
36132   pcache1LeaveMutex(pCache->pGroup);
36133 }
36134 
36135 /*
36136 ** Implementation of the sqlite3_pcache.xTruncate method.
36137 **
36138 ** Discard all unpinned pages in the cache with a page number equal to
36139 ** or greater than parameter iLimit. Any pinned pages with a page number
36140 ** equal to or greater than iLimit are implicitly unpinned.
36141 */
36142 static void pcache1Truncate(sqlite3_pcache *p, unsigned int iLimit){
36143   PCache1 *pCache = (PCache1 *)p;
36144   pcache1EnterMutex(pCache->pGroup);
36145   if( iLimit<=pCache->iMaxKey ){
36146     pcache1TruncateUnsafe(pCache, iLimit);
36147     pCache->iMaxKey = iLimit-1;
36148   }
36149   pcache1LeaveMutex(pCache->pGroup);
36150 }
36151 
36152 /*
36153 ** Implementation of the sqlite3_pcache.xDestroy method.
36154 **
36155 ** Destroy a cache allocated using pcache1Create().
36156 */
36157 static void pcache1Destroy(sqlite3_pcache *p){
36158   PCache1 *pCache = (PCache1 *)p;
36159   PGroup *pGroup = pCache->pGroup;
36160   assert( pCache->bPurgeable || (pCache->nMax==0 && pCache->nMin==0) );
36161   pcache1EnterMutex(pGroup);
36162   pcache1TruncateUnsafe(pCache, 0);
36163   pGroup->nMaxPage -= pCache->nMax;
36164   pGroup->nMinPage -= pCache->nMin;
36165   pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
36166   pcache1EnforceMaxPage(pGroup);
36167   pcache1LeaveMutex(pGroup);
36168   sqlite3_free(pCache->apHash);
36169   sqlite3_free(pCache);
36170 }
36171 
36172 /*
36173 ** This function is called during initialization (sqlite3_initialize()) to
36174 ** install the default pluggable cache module, assuming the user has not
36175 ** already provided an alternative.
36176 */
36177 SQLITE_PRIVATE void sqlite3PCacheSetDefault(void){
36178   static const sqlite3_pcache_methods defaultMethods = {
36179     0,                       /* pArg */
36180     pcache1Init,             /* xInit */
36181     pcache1Shutdown,         /* xShutdown */
36182     pcache1Create,           /* xCreate */
36183     pcache1Cachesize,        /* xCachesize */
36184     pcache1Pagecount,        /* xPagecount */
36185     pcache1Fetch,            /* xFetch */
36186     pcache1Unpin,            /* xUnpin */
36187     pcache1Rekey,            /* xRekey */
36188     pcache1Truncate,         /* xTruncate */
36189     pcache1Destroy           /* xDestroy */
36190   };
36191   sqlite3_config(SQLITE_CONFIG_PCACHE, &defaultMethods);
36192 }
36193 
36194 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
36195 /*
36196 ** This function is called to free superfluous dynamically allocated memory
36197 ** held by the pager system. Memory in use by any SQLite pager allocated
36198 ** by the current thread may be sqlite3_free()ed.
36199 **
36200 ** nReq is the number of bytes of memory required. Once this much has
36201 ** been released, the function returns. The return value is the total number
36202 ** of bytes of memory released.
36203 */
36204 SQLITE_PRIVATE int sqlite3PcacheReleaseMemory(int nReq){
36205   int nFree = 0;
36206   assert( sqlite3_mutex_notheld(pcache1.grp.mutex) );
36207   assert( sqlite3_mutex_notheld(pcache1.mutex) );
36208   if( pcache1.pStart==0 ){
36209     PgHdr1 *p;
36210     pcache1EnterMutex(&pcache1.grp);
36211     while( (nReq<0 || nFree<nReq) && ((p=pcache1.grp.pLruTail)!=0) ){
36212       nFree += pcache1MemSize(PGHDR1_TO_PAGE(p));
36213       pcache1PinPage(p);
36214       pcache1RemoveFromHash(p);
36215       pcache1FreePage(p);
36216     }
36217     pcache1LeaveMutex(&pcache1.grp);
36218   }
36219   return nFree;
36220 }
36221 #endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
36222 
36223 #ifdef SQLITE_TEST
36224 /*
36225 ** This function is used by test procedures to inspect the internal state
36226 ** of the global cache.
36227 */
36228 SQLITE_PRIVATE void sqlite3PcacheStats(
36229   int *pnCurrent,      /* OUT: Total number of pages cached */
36230   int *pnMax,          /* OUT: Global maximum cache size */
36231   int *pnMin,          /* OUT: Sum of PCache1.nMin for purgeable caches */
36232   int *pnRecyclable    /* OUT: Total number of pages available for recycling */
36233 ){
36234   PgHdr1 *p;
36235   int nRecyclable = 0;
36236   for(p=pcache1.grp.pLruHead; p; p=p->pLruNext){
36237     nRecyclable++;
36238   }
36239   *pnCurrent = pcache1.grp.nCurrentPage;
36240   *pnMax = pcache1.grp.nMaxPage;
36241   *pnMin = pcache1.grp.nMinPage;
36242   *pnRecyclable = nRecyclable;
36243 }
36244 #endif
36245 
36246 /************** End of pcache1.c *********************************************/
36247 /************** Begin file rowset.c ******************************************/
36248 /*
36249 ** 2008 December 3
36250 **
36251 ** The author disclaims copyright to this source code.  In place of
36252 ** a legal notice, here is a blessing:
36253 **
36254 **    May you do good and not evil.
36255 **    May you find forgiveness for yourself and forgive others.
36256 **    May you share freely, never taking more than you give.
36257 **
36258 *************************************************************************
36259 **
36260 ** This module implements an object we call a "RowSet".
36261 **
36262 ** The RowSet object is a collection of rowids.  Rowids
36263 ** are inserted into the RowSet in an arbitrary order.  Inserts
36264 ** can be intermixed with tests to see if a given rowid has been
36265 ** previously inserted into the RowSet.
36266 **
36267 ** After all inserts are finished, it is possible to extract the
36268 ** elements of the RowSet in sorted order.  Once this extraction
36269 ** process has started, no new elements may be inserted.
36270 **
36271 ** Hence, the primitive operations for a RowSet are:
36272 **
36273 **    CREATE
36274 **    INSERT
36275 **    TEST
36276 **    SMALLEST
36277 **    DESTROY
36278 **
36279 ** The CREATE and DESTROY primitives are the constructor and destructor,
36280 ** obviously.  The INSERT primitive adds a new element to the RowSet.
36281 ** TEST checks to see if an element is already in the RowSet.  SMALLEST
36282 ** extracts the least value from the RowSet.
36283 **
36284 ** The INSERT primitive might allocate additional memory.  Memory is
36285 ** allocated in chunks so most INSERTs do no allocation.  There is an
36286 ** upper bound on the size of allocated memory.  No memory is freed
36287 ** until DESTROY.
36288 **
36289 ** The TEST primitive includes a "batch" number.  The TEST primitive
36290 ** will only see elements that were inserted before the last change
36291 ** in the batch number.  In other words, if an INSERT occurs between
36292 ** two TESTs where the TESTs have the same batch nubmer, then the
36293 ** value added by the INSERT will not be visible to the second TEST.
36294 ** The initial batch number is zero, so if the very first TEST contains
36295 ** a non-zero batch number, it will see all prior INSERTs.
36296 **
36297 ** No INSERTs may occurs after a SMALLEST.  An assertion will fail if
36298 ** that is attempted.
36299 **
36300 ** The cost of an INSERT is roughly constant.  (Sometime new memory
36301 ** has to be allocated on an INSERT.)  The cost of a TEST with a new
36302 ** batch number is O(NlogN) where N is the number of elements in the RowSet.
36303 ** The cost of a TEST using the same batch number is O(logN).  The cost
36304 ** of the first SMALLEST is O(NlogN).  Second and subsequent SMALLEST
36305 ** primitives are constant time.  The cost of DESTROY is O(N).
36306 **
36307 ** There is an added cost of O(N) when switching between TEST and
36308 ** SMALLEST primitives.
36309 */
36310 
36311 
36312 /*
36313 ** Target size for allocation chunks.
36314 */
36315 #define ROWSET_ALLOCATION_SIZE 1024
36316 
36317 /*
36318 ** The number of rowset entries per allocation chunk.
36319 */
36320 #define ROWSET_ENTRY_PER_CHUNK  \
36321                        ((ROWSET_ALLOCATION_SIZE-8)/sizeof(struct RowSetEntry))
36322 
36323 /*
36324 ** Each entry in a RowSet is an instance of the following object.
36325 */
36326 struct RowSetEntry {
36327   i64 v;                        /* ROWID value for this entry */
36328   struct RowSetEntry *pRight;   /* Right subtree (larger entries) or list */
36329   struct RowSetEntry *pLeft;    /* Left subtree (smaller entries) */
36330 };
36331 
36332 /*
36333 ** RowSetEntry objects are allocated in large chunks (instances of the
36334 ** following structure) to reduce memory allocation overhead.  The
36335 ** chunks are kept on a linked list so that they can be deallocated
36336 ** when the RowSet is destroyed.
36337 */
36338 struct RowSetChunk {
36339   struct RowSetChunk *pNextChunk;        /* Next chunk on list of them all */
36340   struct RowSetEntry aEntry[ROWSET_ENTRY_PER_CHUNK]; /* Allocated entries */
36341 };
36342 
36343 /*
36344 ** A RowSet in an instance of the following structure.
36345 **
36346 ** A typedef of this structure if found in sqliteInt.h.
36347 */
36348 struct RowSet {
36349   struct RowSetChunk *pChunk;    /* List of all chunk allocations */
36350   sqlite3 *db;                   /* The database connection */
36351   struct RowSetEntry *pEntry;    /* List of entries using pRight */
36352   struct RowSetEntry *pLast;     /* Last entry on the pEntry list */
36353   struct RowSetEntry *pFresh;    /* Source of new entry objects */
36354   struct RowSetEntry *pTree;     /* Binary tree of entries */
36355   u16 nFresh;                    /* Number of objects on pFresh */
36356   u8 isSorted;                   /* True if pEntry is sorted */
36357   u8 iBatch;                     /* Current insert batch */
36358 };
36359 
36360 /*
36361 ** Turn bulk memory into a RowSet object.  N bytes of memory
36362 ** are available at pSpace.  The db pointer is used as a memory context
36363 ** for any subsequent allocations that need to occur.
36364 ** Return a pointer to the new RowSet object.
36365 **
36366 ** It must be the case that N is sufficient to make a Rowset.  If not
36367 ** an assertion fault occurs.
36368 **
36369 ** If N is larger than the minimum, use the surplus as an initial
36370 ** allocation of entries available to be filled.
36371 */
36372 SQLITE_PRIVATE RowSet *sqlite3RowSetInit(sqlite3 *db, void *pSpace, unsigned int N){
36373   RowSet *p;
36374   assert( N >= ROUND8(sizeof(*p)) );
36375   p = pSpace;
36376   p->pChunk = 0;
36377   p->db = db;
36378   p->pEntry = 0;
36379   p->pLast = 0;
36380   p->pTree = 0;
36381   p->pFresh = (struct RowSetEntry*)(ROUND8(sizeof(*p)) + (char*)p);
36382   p->nFresh = (u16)((N - ROUND8(sizeof(*p)))/sizeof(struct RowSetEntry));
36383   p->isSorted = 1;
36384   p->iBatch = 0;
36385   return p;
36386 }
36387 
36388 /*
36389 ** Deallocate all chunks from a RowSet.  This frees all memory that
36390 ** the RowSet has allocated over its lifetime.  This routine is
36391 ** the destructor for the RowSet.
36392 */
36393 SQLITE_PRIVATE void sqlite3RowSetClear(RowSet *p){
36394   struct RowSetChunk *pChunk, *pNextChunk;
36395   for(pChunk=p->pChunk; pChunk; pChunk = pNextChunk){
36396     pNextChunk = pChunk->pNextChunk;
36397     sqlite3DbFree(p->db, pChunk);
36398   }
36399   p->pChunk = 0;
36400   p->nFresh = 0;
36401   p->pEntry = 0;
36402   p->pLast = 0;
36403   p->pTree = 0;
36404   p->isSorted = 1;
36405 }
36406 
36407 /*
36408 ** Insert a new value into a RowSet.
36409 **
36410 ** The mallocFailed flag of the database connection is set if a
36411 ** memory allocation fails.
36412 */
36413 SQLITE_PRIVATE void sqlite3RowSetInsert(RowSet *p, i64 rowid){
36414   struct RowSetEntry *pEntry;  /* The new entry */
36415   struct RowSetEntry *pLast;   /* The last prior entry */
36416   assert( p!=0 );
36417   if( p->nFresh==0 ){
36418     struct RowSetChunk *pNew;
36419     pNew = sqlite3DbMallocRaw(p->db, sizeof(*pNew));
36420     if( pNew==0 ){
36421       return;
36422     }
36423     pNew->pNextChunk = p->pChunk;
36424     p->pChunk = pNew;
36425     p->pFresh = pNew->aEntry;
36426     p->nFresh = ROWSET_ENTRY_PER_CHUNK;
36427   }
36428   pEntry = p->pFresh++;
36429   p->nFresh--;
36430   pEntry->v = rowid;
36431   pEntry->pRight = 0;
36432   pLast = p->pLast;
36433   if( pLast ){
36434     if( p->isSorted && rowid<=pLast->v ){
36435       p->isSorted = 0;
36436     }
36437     pLast->pRight = pEntry;
36438   }else{
36439     assert( p->pEntry==0 ); /* Fires if INSERT after SMALLEST */
36440     p->pEntry = pEntry;
36441   }
36442   p->pLast = pEntry;
36443 }
36444 
36445 /*
36446 ** Merge two lists of RowSetEntry objects.  Remove duplicates.
36447 **
36448 ** The input lists are connected via pRight pointers and are
36449 ** assumed to each already be in sorted order.
36450 */
36451 static struct RowSetEntry *rowSetMerge(
36452   struct RowSetEntry *pA,    /* First sorted list to be merged */
36453   struct RowSetEntry *pB     /* Second sorted list to be merged */
36454 ){
36455   struct RowSetEntry head;
36456   struct RowSetEntry *pTail;
36457 
36458   pTail = &head;
36459   while( pA && pB ){
36460     assert( pA->pRight==0 || pA->v<=pA->pRight->v );
36461     assert( pB->pRight==0 || pB->v<=pB->pRight->v );
36462     if( pA->v<pB->v ){
36463       pTail->pRight = pA;
36464       pA = pA->pRight;
36465       pTail = pTail->pRight;
36466     }else if( pB->v<pA->v ){
36467       pTail->pRight = pB;
36468       pB = pB->pRight;
36469       pTail = pTail->pRight;
36470     }else{
36471       pA = pA->pRight;
36472     }
36473   }
36474   if( pA ){
36475     assert( pA->pRight==0 || pA->v<=pA->pRight->v );
36476     pTail->pRight = pA;
36477   }else{
36478     assert( pB==0 || pB->pRight==0 || pB->v<=pB->pRight->v );
36479     pTail->pRight = pB;
36480   }
36481   return head.pRight;
36482 }
36483 
36484 /*
36485 ** Sort all elements on the pEntry list of the RowSet into ascending order.
36486 */
36487 static void rowSetSort(RowSet *p){
36488   unsigned int i;
36489   struct RowSetEntry *pEntry;
36490   struct RowSetEntry *aBucket[40];
36491 
36492   assert( p->isSorted==0 );
36493   memset(aBucket, 0, sizeof(aBucket));
36494   while( p->pEntry ){
36495     pEntry = p->pEntry;
36496     p->pEntry = pEntry->pRight;
36497     pEntry->pRight = 0;
36498     for(i=0; aBucket[i]; i++){
36499       pEntry = rowSetMerge(aBucket[i], pEntry);
36500       aBucket[i] = 0;
36501     }
36502     aBucket[i] = pEntry;
36503   }
36504   pEntry = 0;
36505   for(i=0; i<sizeof(aBucket)/sizeof(aBucket[0]); i++){
36506     pEntry = rowSetMerge(pEntry, aBucket[i]);
36507   }
36508   p->pEntry = pEntry;
36509   p->pLast = 0;
36510   p->isSorted = 1;
36511 }
36512 
36513 
36514 /*
36515 ** The input, pIn, is a binary tree (or subtree) of RowSetEntry objects.
36516 ** Convert this tree into a linked list connected by the pRight pointers
36517 ** and return pointers to the first and last elements of the new list.
36518 */
36519 static void rowSetTreeToList(
36520   struct RowSetEntry *pIn,         /* Root of the input tree */
36521   struct RowSetEntry **ppFirst,    /* Write head of the output list here */
36522   struct RowSetEntry **ppLast      /* Write tail of the output list here */
36523 ){
36524   assert( pIn!=0 );
36525   if( pIn->pLeft ){
36526     struct RowSetEntry *p;
36527     rowSetTreeToList(pIn->pLeft, ppFirst, &p);
36528     p->pRight = pIn;
36529   }else{
36530     *ppFirst = pIn;
36531   }
36532   if( pIn->pRight ){
36533     rowSetTreeToList(pIn->pRight, &pIn->pRight, ppLast);
36534   }else{
36535     *ppLast = pIn;
36536   }
36537   assert( (*ppLast)->pRight==0 );
36538 }
36539 
36540 
36541 /*
36542 ** Convert a sorted list of elements (connected by pRight) into a binary
36543 ** tree with depth of iDepth.  A depth of 1 means the tree contains a single
36544 ** node taken from the head of *ppList.  A depth of 2 means a tree with
36545 ** three nodes.  And so forth.
36546 **
36547 ** Use as many entries from the input list as required and update the
36548 ** *ppList to point to the unused elements of the list.  If the input
36549 ** list contains too few elements, then construct an incomplete tree
36550 ** and leave *ppList set to NULL.
36551 **
36552 ** Return a pointer to the root of the constructed binary tree.
36553 */
36554 static struct RowSetEntry *rowSetNDeepTree(
36555   struct RowSetEntry **ppList,
36556   int iDepth
36557 ){
36558   struct RowSetEntry *p;         /* Root of the new tree */
36559   struct RowSetEntry *pLeft;     /* Left subtree */
36560   if( *ppList==0 ){
36561     return 0;
36562   }
36563   if( iDepth==1 ){
36564     p = *ppList;
36565     *ppList = p->pRight;
36566     p->pLeft = p->pRight = 0;
36567     return p;
36568   }
36569   pLeft = rowSetNDeepTree(ppList, iDepth-1);
36570   p = *ppList;
36571   if( p==0 ){
36572     return pLeft;
36573   }
36574   p->pLeft = pLeft;
36575   *ppList = p->pRight;
36576   p->pRight = rowSetNDeepTree(ppList, iDepth-1);
36577   return p;
36578 }
36579 
36580 /*
36581 ** Convert a sorted list of elements into a binary tree. Make the tree
36582 ** as deep as it needs to be in order to contain the entire list.
36583 */
36584 static struct RowSetEntry *rowSetListToTree(struct RowSetEntry *pList){
36585   int iDepth;           /* Depth of the tree so far */
36586   struct RowSetEntry *p;       /* Current tree root */
36587   struct RowSetEntry *pLeft;   /* Left subtree */
36588 
36589   assert( pList!=0 );
36590   p = pList;
36591   pList = p->pRight;
36592   p->pLeft = p->pRight = 0;
36593   for(iDepth=1; pList; iDepth++){
36594     pLeft = p;
36595     p = pList;
36596     pList = p->pRight;
36597     p->pLeft = pLeft;
36598     p->pRight = rowSetNDeepTree(&pList, iDepth);
36599   }
36600   return p;
36601 }
36602 
36603 /*
36604 ** Convert the list in p->pEntry into a sorted list if it is not
36605 ** sorted already.  If there is a binary tree on p->pTree, then
36606 ** convert it into a list too and merge it into the p->pEntry list.
36607 */
36608 static void rowSetToList(RowSet *p){
36609   if( !p->isSorted ){
36610     rowSetSort(p);
36611   }
36612   if( p->pTree ){
36613     struct RowSetEntry *pHead, *pTail;
36614     rowSetTreeToList(p->pTree, &pHead, &pTail);
36615     p->pTree = 0;
36616     p->pEntry = rowSetMerge(p->pEntry, pHead);
36617   }
36618 }
36619 
36620 /*
36621 ** Extract the smallest element from the RowSet.
36622 ** Write the element into *pRowid.  Return 1 on success.  Return
36623 ** 0 if the RowSet is already empty.
36624 **
36625 ** After this routine has been called, the sqlite3RowSetInsert()
36626 ** routine may not be called again.
36627 */
36628 SQLITE_PRIVATE int sqlite3RowSetNext(RowSet *p, i64 *pRowid){
36629   rowSetToList(p);
36630   if( p->pEntry ){
36631     *pRowid = p->pEntry->v;
36632     p->pEntry = p->pEntry->pRight;
36633     if( p->pEntry==0 ){
36634       sqlite3RowSetClear(p);
36635     }
36636     return 1;
36637   }else{
36638     return 0;
36639   }
36640 }
36641 
36642 /*
36643 ** Check to see if element iRowid was inserted into the the rowset as
36644 ** part of any insert batch prior to iBatch.  Return 1 or 0.
36645 */
36646 SQLITE_PRIVATE int sqlite3RowSetTest(RowSet *pRowSet, u8 iBatch, sqlite3_int64 iRowid){
36647   struct RowSetEntry *p;
36648   if( iBatch!=pRowSet->iBatch ){
36649     if( pRowSet->pEntry ){
36650       rowSetToList(pRowSet);
36651       pRowSet->pTree = rowSetListToTree(pRowSet->pEntry);
36652       pRowSet->pEntry = 0;
36653       pRowSet->pLast = 0;
36654     }
36655     pRowSet->iBatch = iBatch;
36656   }
36657   p = pRowSet->pTree;
36658   while( p ){
36659     if( p->v<iRowid ){
36660       p = p->pRight;
36661     }else if( p->v>iRowid ){
36662       p = p->pLeft;
36663     }else{
36664       return 1;
36665     }
36666   }
36667   return 0;
36668 }
36669 
36670 /************** End of rowset.c **********************************************/
36671 /************** Begin file pager.c *******************************************/
36672 /*
36673 ** 2001 September 15
36674 **
36675 ** The author disclaims copyright to this source code.  In place of
36676 ** a legal notice, here is a blessing:
36677 **
36678 **    May you do good and not evil.
36679 **    May you find forgiveness for yourself and forgive others.
36680 **    May you share freely, never taking more than you give.
36681 **
36682 *************************************************************************
36683 ** This is the implementation of the page cache subsystem or "pager".
36684 **
36685 ** The pager is used to access a database disk file.  It implements
36686 ** atomic commit and rollback through the use of a journal file that
36687 ** is separate from the database file.  The pager also implements file
36688 ** locking to prevent two processes from writing the same database
36689 ** file simultaneously, or one process from reading the database while
36690 ** another is writing.
36691 */
36692 #ifndef SQLITE_OMIT_DISKIO
36693 /************** Include wal.h in the middle of pager.c ***********************/
36694 /************** Begin file wal.h *********************************************/
36695 /*
36696 ** 2010 February 1
36697 **
36698 ** The author disclaims copyright to this source code.  In place of
36699 ** a legal notice, here is a blessing:
36700 **
36701 **    May you do good and not evil.
36702 **    May you find forgiveness for yourself and forgive others.
36703 **    May you share freely, never taking more than you give.
36704 **
36705 *************************************************************************
36706 ** This header file defines the interface to the write-ahead logging
36707 ** system. Refer to the comments below and the header comment attached to
36708 ** the implementation of each function in log.c for further details.
36709 */
36710 
36711 #ifndef _WAL_H_
36712 #define _WAL_H_
36713 
36714 
36715 #ifdef SQLITE_OMIT_WAL
36716 # define sqlite3WalOpen(x,y,z)                   0
36717 # define sqlite3WalLimit(x,y)
36718 # define sqlite3WalClose(w,x,y,z)                0
36719 # define sqlite3WalBeginReadTransaction(y,z)     0
36720 # define sqlite3WalEndReadTransaction(z)
36721 # define sqlite3WalRead(v,w,x,y,z)               0
36722 # define sqlite3WalDbsize(y)                     0
36723 # define sqlite3WalBeginWriteTransaction(y)      0
36724 # define sqlite3WalEndWriteTransaction(x)        0
36725 # define sqlite3WalUndo(x,y,z)                   0
36726 # define sqlite3WalSavepoint(y,z)
36727 # define sqlite3WalSavepointUndo(y,z)            0
36728 # define sqlite3WalFrames(u,v,w,x,y,z)           0
36729 # define sqlite3WalCheckpoint(r,s,t,u,v,w,x,y,z) 0
36730 # define sqlite3WalCallback(z)                   0
36731 # define sqlite3WalExclusiveMode(y,z)            0
36732 # define sqlite3WalHeapMemory(z)                 0
36733 #else
36734 
36735 #define WAL_SAVEPOINT_NDATA 4
36736 
36737 /* Connection to a write-ahead log (WAL) file.
36738 ** There is one object of this type for each pager.
36739 */
36740 typedef struct Wal Wal;
36741 
36742 /* Open and close a connection to a write-ahead log. */
36743 SQLITE_PRIVATE int sqlite3WalOpen(sqlite3_vfs*, sqlite3_file*, const char *, int, i64, Wal**);
36744 SQLITE_PRIVATE int sqlite3WalClose(Wal *pWal, int sync_flags, int, u8 *);
36745 
36746 /* Set the limiting size of a WAL file. */
36747 SQLITE_PRIVATE void sqlite3WalLimit(Wal*, i64);
36748 
36749 /* Used by readers to open (lock) and close (unlock) a snapshot.  A
36750 ** snapshot is like a read-transaction.  It is the state of the database
36751 ** at an instant in time.  sqlite3WalOpenSnapshot gets a read lock and
36752 ** preserves the current state even if the other threads or processes
36753 ** write to or checkpoint the WAL.  sqlite3WalCloseSnapshot() closes the
36754 ** transaction and releases the lock.
36755 */
36756 SQLITE_PRIVATE int sqlite3WalBeginReadTransaction(Wal *pWal, int *);
36757 SQLITE_PRIVATE void sqlite3WalEndReadTransaction(Wal *pWal);
36758 
36759 /* Read a page from the write-ahead log, if it is present. */
36760 SQLITE_PRIVATE int sqlite3WalRead(Wal *pWal, Pgno pgno, int *pInWal, int nOut, u8 *pOut);
36761 
36762 /* If the WAL is not empty, return the size of the database. */
36763 SQLITE_PRIVATE Pgno sqlite3WalDbsize(Wal *pWal);
36764 
36765 /* Obtain or release the WRITER lock. */
36766 SQLITE_PRIVATE int sqlite3WalBeginWriteTransaction(Wal *pWal);
36767 SQLITE_PRIVATE int sqlite3WalEndWriteTransaction(Wal *pWal);
36768 
36769 /* Undo any frames written (but not committed) to the log */
36770 SQLITE_PRIVATE int sqlite3WalUndo(Wal *pWal, int (*xUndo)(void *, Pgno), void *pUndoCtx);
36771 
36772 /* Return an integer that records the current (uncommitted) write
36773 ** position in the WAL */
36774 SQLITE_PRIVATE void sqlite3WalSavepoint(Wal *pWal, u32 *aWalData);
36775 
36776 /* Move the write position of the WAL back to iFrame.  Called in
36777 ** response to a ROLLBACK TO command. */
36778 SQLITE_PRIVATE int sqlite3WalSavepointUndo(Wal *pWal, u32 *aWalData);
36779 
36780 /* Write a frame or frames to the log. */
36781 SQLITE_PRIVATE int sqlite3WalFrames(Wal *pWal, int, PgHdr *, Pgno, int, int);
36782 
36783 /* Copy pages from the log to the database file */
36784 SQLITE_PRIVATE int sqlite3WalCheckpoint(
36785   Wal *pWal,                      /* Write-ahead log connection */
36786   int eMode,                      /* One of PASSIVE, FULL and RESTART */
36787   int (*xBusy)(void*),            /* Function to call when busy */
36788   void *pBusyArg,                 /* Context argument for xBusyHandler */
36789   int sync_flags,                 /* Flags to sync db file with (or 0) */
36790   int nBuf,                       /* Size of buffer nBuf */
36791   u8 *zBuf,                       /* Temporary buffer to use */
36792   int *pnLog,                     /* OUT: Number of frames in WAL */
36793   int *pnCkpt                     /* OUT: Number of backfilled frames in WAL */
36794 );
36795 
36796 /* Return the value to pass to a sqlite3_wal_hook callback, the
36797 ** number of frames in the WAL at the point of the last commit since
36798 ** sqlite3WalCallback() was called.  If no commits have occurred since
36799 ** the last call, then return 0.
36800 */
36801 SQLITE_PRIVATE int sqlite3WalCallback(Wal *pWal);
36802 
36803 /* Tell the wal layer that an EXCLUSIVE lock has been obtained (or released)
36804 ** by the pager layer on the database file.
36805 */
36806 SQLITE_PRIVATE int sqlite3WalExclusiveMode(Wal *pWal, int op);
36807 
36808 /* Return true if the argument is non-NULL and the WAL module is using
36809 ** heap-memory for the wal-index. Otherwise, if the argument is NULL or the
36810 ** WAL module is using shared-memory, return false.
36811 */
36812 SQLITE_PRIVATE int sqlite3WalHeapMemory(Wal *pWal);
36813 
36814 #endif /* ifndef SQLITE_OMIT_WAL */
36815 #endif /* _WAL_H_ */
36816 
36817 /************** End of wal.h *************************************************/
36818 /************** Continuing where we left off in pager.c **********************/
36819 
36820 
36821 /******************* NOTES ON THE DESIGN OF THE PAGER ************************
36822 **
36823 ** This comment block describes invariants that hold when using a rollback
36824 ** journal.  These invariants do not apply for journal_mode=WAL,
36825 ** journal_mode=MEMORY, or journal_mode=OFF.
36826 **
36827 ** Within this comment block, a page is deemed to have been synced
36828 ** automatically as soon as it is written when PRAGMA synchronous=OFF.
36829 ** Otherwise, the page is not synced until the xSync method of the VFS
36830 ** is called successfully on the file containing the page.
36831 **
36832 ** Definition:  A page of the database file is said to be "overwriteable" if
36833 ** one or more of the following are true about the page:
36834 **
36835 **     (a)  The original content of the page as it was at the beginning of
36836 **          the transaction has been written into the rollback journal and
36837 **          synced.
36838 **
36839 **     (b)  The page was a freelist leaf page at the start of the transaction.
36840 **
36841 **     (c)  The page number is greater than the largest page that existed in
36842 **          the database file at the start of the transaction.
36843 **
36844 ** (1) A page of the database file is never overwritten unless one of the
36845 **     following are true:
36846 **
36847 **     (a) The page and all other pages on the same sector are overwriteable.
36848 **
36849 **     (b) The atomic page write optimization is enabled, and the entire
36850 **         transaction other than the update of the transaction sequence
36851 **         number consists of a single page change.
36852 **
36853 ** (2) The content of a page written into the rollback journal exactly matches
36854 **     both the content in the database when the rollback journal was written
36855 **     and the content in the database at the beginning of the current
36856 **     transaction.
36857 **
36858 ** (3) Writes to the database file are an integer multiple of the page size
36859 **     in length and are aligned on a page boundary.
36860 **
36861 ** (4) Reads from the database file are either aligned on a page boundary and
36862 **     an integer multiple of the page size in length or are taken from the
36863 **     first 100 bytes of the database file.
36864 **
36865 ** (5) All writes to the database file are synced prior to the rollback journal
36866 **     being deleted, truncated, or zeroed.
36867 **
36868 ** (6) If a master journal file is used, then all writes to the database file
36869 **     are synced prior to the master journal being deleted.
36870 **
36871 ** Definition: Two databases (or the same database at two points it time)
36872 ** are said to be "logically equivalent" if they give the same answer to
36873 ** all queries.  Note in particular the the content of freelist leaf
36874 ** pages can be changed arbitarily without effecting the logical equivalence
36875 ** of the database.
36876 **
36877 ** (7) At any time, if any subset, including the empty set and the total set,
36878 **     of the unsynced changes to a rollback journal are removed and the
36879 **     journal is rolled back, the resulting database file will be logical
36880 **     equivalent to the database file at the beginning of the transaction.
36881 **
36882 ** (8) When a transaction is rolled back, the xTruncate method of the VFS
36883 **     is called to restore the database file to the same size it was at
36884 **     the beginning of the transaction.  (In some VFSes, the xTruncate
36885 **     method is a no-op, but that does not change the fact the SQLite will
36886 **     invoke it.)
36887 **
36888 ** (9) Whenever the database file is modified, at least one bit in the range
36889 **     of bytes from 24 through 39 inclusive will be changed prior to releasing
36890 **     the EXCLUSIVE lock, thus signaling other connections on the same
36891 **     database to flush their caches.
36892 **
36893 ** (10) The pattern of bits in bytes 24 through 39 shall not repeat in less
36894 **      than one billion transactions.
36895 **
36896 ** (11) A database file is well-formed at the beginning and at the conclusion
36897 **      of every transaction.
36898 **
36899 ** (12) An EXCLUSIVE lock is held on the database file when writing to
36900 **      the database file.
36901 **
36902 ** (13) A SHARED lock is held on the database file while reading any
36903 **      content out of the database file.
36904 **
36905 ******************************************************************************/
36906 
36907 /*
36908 ** Macros for troubleshooting.  Normally turned off
36909 */
36910 #if 0
36911 int sqlite3PagerTrace=1;  /* True to enable tracing */
36912 #define sqlite3DebugPrintf printf
36913 #define PAGERTRACE(X)     if( sqlite3PagerTrace ){ sqlite3DebugPrintf X; }
36914 #else
36915 #define PAGERTRACE(X)
36916 #endif
36917 
36918 /*
36919 ** The following two macros are used within the PAGERTRACE() macros above
36920 ** to print out file-descriptors.
36921 **
36922 ** PAGERID() takes a pointer to a Pager struct as its argument. The
36923 ** associated file-descriptor is returned. FILEHANDLEID() takes an sqlite3_file
36924 ** struct as its argument.
36925 */
36926 #define PAGERID(p) ((int)(p->fd))
36927 #define FILEHANDLEID(fd) ((int)fd)
36928 
36929 /*
36930 ** The Pager.eState variable stores the current 'state' of a pager. A
36931 ** pager may be in any one of the seven states shown in the following
36932 ** state diagram.
36933 **
36934 **                            OPEN <------+------+
36935 **                              |         |      |
36936 **                              V         |      |
36937 **               +---------> READER-------+      |
36938 **               |              |                |
36939 **               |              V                |
36940 **               |<-------WRITER_LOCKED------> ERROR
36941 **               |              |                ^
36942 **               |              V                |
36943 **               |<------WRITER_CACHEMOD-------->|
36944 **               |              |                |
36945 **               |              V                |
36946 **               |<-------WRITER_DBMOD---------->|
36947 **               |              |                |
36948 **               |              V                |
36949 **               +<------WRITER_FINISHED-------->+
36950 **
36951 **
36952 ** List of state transitions and the C [function] that performs each:
36953 **
36954 **   OPEN              -> READER              [sqlite3PagerSharedLock]
36955 **   READER            -> OPEN                [pager_unlock]
36956 **
36957 **   READER            -> WRITER_LOCKED       [sqlite3PagerBegin]
36958 **   WRITER_LOCKED     -> WRITER_CACHEMOD     [pager_open_journal]
36959 **   WRITER_CACHEMOD   -> WRITER_DBMOD        [syncJournal]
36960 **   WRITER_DBMOD      -> WRITER_FINISHED     [sqlite3PagerCommitPhaseOne]
36961 **   WRITER_***        -> READER              [pager_end_transaction]
36962 **
36963 **   WRITER_***        -> ERROR               [pager_error]
36964 **   ERROR             -> OPEN                [pager_unlock]
36965 **
36966 **
36967 **  OPEN:
36968 **
36969 **    The pager starts up in this state. Nothing is guaranteed in this
36970 **    state - the file may or may not be locked and the database size is
36971 **    unknown. The database may not be read or written.
36972 **
36973 **    * No read or write transaction is active.
36974 **    * Any lock, or no lock at all, may be held on the database file.
36975 **    * The dbSize, dbOrigSize and dbFileSize variables may not be trusted.
36976 **
36977 **  READER:
36978 **
36979 **    In this state all the requirements for reading the database in
36980 **    rollback (non-WAL) mode are met. Unless the pager is (or recently
36981 **    was) in exclusive-locking mode, a user-level read transaction is
36982 **    open. The database size is known in this state.
36983 **
36984 **    A connection running with locking_mode=normal enters this state when
36985 **    it opens a read-transaction on the database and returns to state
36986 **    OPEN after the read-transaction is completed. However a connection
36987 **    running in locking_mode=exclusive (including temp databases) remains in
36988 **    this state even after the read-transaction is closed. The only way
36989 **    a locking_mode=exclusive connection can transition from READER to OPEN
36990 **    is via the ERROR state (see below).
36991 **
36992 **    * A read transaction may be active (but a write-transaction cannot).
36993 **    * A SHARED or greater lock is held on the database file.
36994 **    * The dbSize variable may be trusted (even if a user-level read
36995 **      transaction is not active). The dbOrigSize and dbFileSize variables
36996 **      may not be trusted at this point.
36997 **    * If the database is a WAL database, then the WAL connection is open.
36998 **    * Even if a read-transaction is not open, it is guaranteed that
36999 **      there is no hot-journal in the file-system.
37000 **
37001 **  WRITER_LOCKED:
37002 **
37003 **    The pager moves to this state from READER when a write-transaction
37004 **    is first opened on the database. In WRITER_LOCKED state, all locks
37005 **    required to start a write-transaction are held, but no actual
37006 **    modifications to the cache or database have taken place.
37007 **
37008 **    In rollback mode, a RESERVED or (if the transaction was opened with
37009 **    BEGIN EXCLUSIVE) EXCLUSIVE lock is obtained on the database file when
37010 **    moving to this state, but the journal file is not written to or opened
37011 **    to in this state. If the transaction is committed or rolled back while
37012 **    in WRITER_LOCKED state, all that is required is to unlock the database
37013 **    file.
37014 **
37015 **    IN WAL mode, WalBeginWriteTransaction() is called to lock the log file.
37016 **    If the connection is running with locking_mode=exclusive, an attempt
37017 **    is made to obtain an EXCLUSIVE lock on the database file.
37018 **
37019 **    * A write transaction is active.
37020 **    * If the connection is open in rollback-mode, a RESERVED or greater
37021 **      lock is held on the database file.
37022 **    * If the connection is open in WAL-mode, a WAL write transaction
37023 **      is open (i.e. sqlite3WalBeginWriteTransaction() has been successfully
37024 **      called).
37025 **    * The dbSize, dbOrigSize and dbFileSize variables are all valid.
37026 **    * The contents of the pager cache have not been modified.
37027 **    * The journal file may or may not be open.
37028 **    * Nothing (not even the first header) has been written to the journal.
37029 **
37030 **  WRITER_CACHEMOD:
37031 **
37032 **    A pager moves from WRITER_LOCKED state to this state when a page is
37033 **    first modified by the upper layer. In rollback mode the journal file
37034 **    is opened (if it is not already open) and a header written to the
37035 **    start of it. The database file on disk has not been modified.
37036 **
37037 **    * A write transaction is active.
37038 **    * A RESERVED or greater lock is held on the database file.
37039 **    * The journal file is open and the first header has been written
37040 **      to it, but the header has not been synced to disk.
37041 **    * The contents of the page cache have been modified.
37042 **
37043 **  WRITER_DBMOD:
37044 **
37045 **    The pager transitions from WRITER_CACHEMOD into WRITER_DBMOD state
37046 **    when it modifies the contents of the database file. WAL connections
37047 **    never enter this state (since they do not modify the database file,
37048 **    just the log file).
37049 **
37050 **    * A write transaction is active.
37051 **    * An EXCLUSIVE or greater lock is held on the database file.
37052 **    * The journal file is open and the first header has been written
37053 **      and synced to disk.
37054 **    * The contents of the page cache have been modified (and possibly
37055 **      written to disk).
37056 **
37057 **  WRITER_FINISHED:
37058 **
37059 **    It is not possible for a WAL connection to enter this state.
37060 **
37061 **    A rollback-mode pager changes to WRITER_FINISHED state from WRITER_DBMOD
37062 **    state after the entire transaction has been successfully written into the
37063 **    database file. In this state the transaction may be committed simply
37064 **    by finalizing the journal file. Once in WRITER_FINISHED state, it is
37065 **    not possible to modify the database further. At this point, the upper
37066 **    layer must either commit or rollback the transaction.
37067 **
37068 **    * A write transaction is active.
37069 **    * An EXCLUSIVE or greater lock is held on the database file.
37070 **    * All writing and syncing of journal and database data has finished.
37071 **      If no error occured, all that remains is to finalize the journal to
37072 **      commit the transaction. If an error did occur, the caller will need
37073 **      to rollback the transaction.
37074 **
37075 **  ERROR:
37076 **
37077 **    The ERROR state is entered when an IO or disk-full error (including
37078 **    SQLITE_IOERR_NOMEM) occurs at a point in the code that makes it
37079 **    difficult to be sure that the in-memory pager state (cache contents,
37080 **    db size etc.) are consistent with the contents of the file-system.
37081 **
37082 **    Temporary pager files may enter the ERROR state, but in-memory pagers
37083 **    cannot.
37084 **
37085 **    For example, if an IO error occurs while performing a rollback,
37086 **    the contents of the page-cache may be left in an inconsistent state.
37087 **    At this point it would be dangerous to change back to READER state
37088 **    (as usually happens after a rollback). Any subsequent readers might
37089 **    report database corruption (due to the inconsistent cache), and if
37090 **    they upgrade to writers, they may inadvertently corrupt the database
37091 **    file. To avoid this hazard, the pager switches into the ERROR state
37092 **    instead of READER following such an error.
37093 **
37094 **    Once it has entered the ERROR state, any attempt to use the pager
37095 **    to read or write data returns an error. Eventually, once all
37096 **    outstanding transactions have been abandoned, the pager is able to
37097 **    transition back to OPEN state, discarding the contents of the
37098 **    page-cache and any other in-memory state at the same time. Everything
37099 **    is reloaded from disk (and, if necessary, hot-journal rollback peformed)
37100 **    when a read-transaction is next opened on the pager (transitioning
37101 **    the pager into READER state). At that point the system has recovered
37102 **    from the error.
37103 **
37104 **    Specifically, the pager jumps into the ERROR state if:
37105 **
37106 **      1. An error occurs while attempting a rollback. This happens in
37107 **         function sqlite3PagerRollback().
37108 **
37109 **      2. An error occurs while attempting to finalize a journal file
37110 **         following a commit in function sqlite3PagerCommitPhaseTwo().
37111 **
37112 **      3. An error occurs while attempting to write to the journal or
37113 **         database file in function pagerStress() in order to free up
37114 **         memory.
37115 **
37116 **    In other cases, the error is returned to the b-tree layer. The b-tree
37117 **    layer then attempts a rollback operation. If the error condition
37118 **    persists, the pager enters the ERROR state via condition (1) above.
37119 **
37120 **    Condition (3) is necessary because it can be triggered by a read-only
37121 **    statement executed within a transaction. In this case, if the error
37122 **    code were simply returned to the user, the b-tree layer would not
37123 **    automatically attempt a rollback, as it assumes that an error in a
37124 **    read-only statement cannot leave the pager in an internally inconsistent
37125 **    state.
37126 **
37127 **    * The Pager.errCode variable is set to something other than SQLITE_OK.
37128 **    * There are one or more outstanding references to pages (after the
37129 **      last reference is dropped the pager should move back to OPEN state).
37130 **    * The pager is not an in-memory pager.
37131 **
37132 **
37133 ** Notes:
37134 **
37135 **   * A pager is never in WRITER_DBMOD or WRITER_FINISHED state if the
37136 **     connection is open in WAL mode. A WAL connection is always in one
37137 **     of the first four states.
37138 **
37139 **   * Normally, a connection open in exclusive mode is never in PAGER_OPEN
37140 **     state. There are two exceptions: immediately after exclusive-mode has
37141 **     been turned on (and before any read or write transactions are
37142 **     executed), and when the pager is leaving the "error state".
37143 **
37144 **   * See also: assert_pager_state().
37145 */
37146 #define PAGER_OPEN                  0
37147 #define PAGER_READER                1
37148 #define PAGER_WRITER_LOCKED         2
37149 #define PAGER_WRITER_CACHEMOD       3
37150 #define PAGER_WRITER_DBMOD          4
37151 #define PAGER_WRITER_FINISHED       5
37152 #define PAGER_ERROR                 6
37153 
37154 /*
37155 ** The Pager.eLock variable is almost always set to one of the
37156 ** following locking-states, according to the lock currently held on
37157 ** the database file: NO_LOCK, SHARED_LOCK, RESERVED_LOCK or EXCLUSIVE_LOCK.
37158 ** This variable is kept up to date as locks are taken and released by
37159 ** the pagerLockDb() and pagerUnlockDb() wrappers.
37160 **
37161 ** If the VFS xLock() or xUnlock() returns an error other than SQLITE_BUSY
37162 ** (i.e. one of the SQLITE_IOERR subtypes), it is not clear whether or not
37163 ** the operation was successful. In these circumstances pagerLockDb() and
37164 ** pagerUnlockDb() take a conservative approach - eLock is always updated
37165 ** when unlocking the file, and only updated when locking the file if the
37166 ** VFS call is successful. This way, the Pager.eLock variable may be set
37167 ** to a less exclusive (lower) value than the lock that is actually held
37168 ** at the system level, but it is never set to a more exclusive value.
37169 **
37170 ** This is usually safe. If an xUnlock fails or appears to fail, there may
37171 ** be a few redundant xLock() calls or a lock may be held for longer than
37172 ** required, but nothing really goes wrong.
37173 **
37174 ** The exception is when the database file is unlocked as the pager moves
37175 ** from ERROR to OPEN state. At this point there may be a hot-journal file
37176 ** in the file-system that needs to be rolled back (as part of a OPEN->SHARED
37177 ** transition, by the same pager or any other). If the call to xUnlock()
37178 ** fails at this point and the pager is left holding an EXCLUSIVE lock, this
37179 ** can confuse the call to xCheckReservedLock() call made later as part
37180 ** of hot-journal detection.
37181 **
37182 ** xCheckReservedLock() is defined as returning true "if there is a RESERVED
37183 ** lock held by this process or any others". So xCheckReservedLock may
37184 ** return true because the caller itself is holding an EXCLUSIVE lock (but
37185 ** doesn't know it because of a previous error in xUnlock). If this happens
37186 ** a hot-journal may be mistaken for a journal being created by an active
37187 ** transaction in another process, causing SQLite to read from the database
37188 ** without rolling it back.
37189 **
37190 ** To work around this, if a call to xUnlock() fails when unlocking the
37191 ** database in the ERROR state, Pager.eLock is set to UNKNOWN_LOCK. It
37192 ** is only changed back to a real locking state after a successful call
37193 ** to xLock(EXCLUSIVE). Also, the code to do the OPEN->SHARED state transition
37194 ** omits the check for a hot-journal if Pager.eLock is set to UNKNOWN_LOCK
37195 ** lock. Instead, it assumes a hot-journal exists and obtains an EXCLUSIVE
37196 ** lock on the database file before attempting to roll it back. See function
37197 ** PagerSharedLock() for more detail.
37198 **
37199 ** Pager.eLock may only be set to UNKNOWN_LOCK when the pager is in
37200 ** PAGER_OPEN state.
37201 */
37202 #define UNKNOWN_LOCK                (EXCLUSIVE_LOCK+1)
37203 
37204 /*
37205 ** A macro used for invoking the codec if there is one
37206 */
37207 #ifdef SQLITE_HAS_CODEC
37208 # define CODEC1(P,D,N,X,E) \
37209     if( P->xCodec && P->xCodec(P->pCodec,D,N,X)==0 ){ E; }
37210 # define CODEC2(P,D,N,X,E,O) \
37211     if( P->xCodec==0 ){ O=(char*)D; }else \
37212     if( (O=(char*)(P->xCodec(P->pCodec,D,N,X)))==0 ){ E; }
37213 #else
37214 # define CODEC1(P,D,N,X,E)   /* NO-OP */
37215 # define CODEC2(P,D,N,X,E,O) O=(char*)D
37216 #endif
37217 
37218 /*
37219 ** The maximum allowed sector size. 64KiB. If the xSectorsize() method
37220 ** returns a value larger than this, then MAX_SECTOR_SIZE is used instead.
37221 ** This could conceivably cause corruption following a power failure on
37222 ** such a system. This is currently an undocumented limit.
37223 */
37224 #define MAX_SECTOR_SIZE 0x10000
37225 
37226 /*
37227 ** An instance of the following structure is allocated for each active
37228 ** savepoint and statement transaction in the system. All such structures
37229 ** are stored in the Pager.aSavepoint[] array, which is allocated and
37230 ** resized using sqlite3Realloc().
37231 **
37232 ** When a savepoint is created, the PagerSavepoint.iHdrOffset field is
37233 ** set to 0. If a journal-header is written into the main journal while
37234 ** the savepoint is active, then iHdrOffset is set to the byte offset
37235 ** immediately following the last journal record written into the main
37236 ** journal before the journal-header. This is required during savepoint
37237 ** rollback (see pagerPlaybackSavepoint()).
37238 */
37239 typedef struct PagerSavepoint PagerSavepoint;
37240 struct PagerSavepoint {
37241   i64 iOffset;                 /* Starting offset in main journal */
37242   i64 iHdrOffset;              /* See above */
37243   Bitvec *pInSavepoint;        /* Set of pages in this savepoint */
37244   Pgno nOrig;                  /* Original number of pages in file */
37245   Pgno iSubRec;                /* Index of first record in sub-journal */
37246 #ifndef SQLITE_OMIT_WAL
37247   u32 aWalData[WAL_SAVEPOINT_NDATA];        /* WAL savepoint context */
37248 #endif
37249 };
37250 
37251 /*
37252 ** A open page cache is an instance of struct Pager. A description of
37253 ** some of the more important member variables follows:
37254 **
37255 ** eState
37256 **
37257 **   The current 'state' of the pager object. See the comment and state
37258 **   diagram above for a description of the pager state.
37259 **
37260 ** eLock
37261 **
37262 **   For a real on-disk database, the current lock held on the database file -
37263 **   NO_LOCK, SHARED_LOCK, RESERVED_LOCK or EXCLUSIVE_LOCK.
37264 **
37265 **   For a temporary or in-memory database (neither of which require any
37266 **   locks), this variable is always set to EXCLUSIVE_LOCK. Since such
37267 **   databases always have Pager.exclusiveMode==1, this tricks the pager
37268 **   logic into thinking that it already has all the locks it will ever
37269 **   need (and no reason to release them).
37270 **
37271 **   In some (obscure) circumstances, this variable may also be set to
37272 **   UNKNOWN_LOCK. See the comment above the #define of UNKNOWN_LOCK for
37273 **   details.
37274 **
37275 ** changeCountDone
37276 **
37277 **   This boolean variable is used to make sure that the change-counter
37278 **   (the 4-byte header field at byte offset 24 of the database file) is
37279 **   not updated more often than necessary.
37280 **
37281 **   It is set to true when the change-counter field is updated, which
37282 **   can only happen if an exclusive lock is held on the database file.
37283 **   It is cleared (set to false) whenever an exclusive lock is
37284 **   relinquished on the database file. Each time a transaction is committed,
37285 **   The changeCountDone flag is inspected. If it is true, the work of
37286 **   updating the change-counter is omitted for the current transaction.
37287 **
37288 **   This mechanism means that when running in exclusive mode, a connection
37289 **   need only update the change-counter once, for the first transaction
37290 **   committed.
37291 **
37292 ** setMaster
37293 **
37294 **   When PagerCommitPhaseOne() is called to commit a transaction, it may
37295 **   (or may not) specify a master-journal name to be written into the
37296 **   journal file before it is synced to disk.
37297 **
37298 **   Whether or not a journal file contains a master-journal pointer affects
37299 **   the way in which the journal file is finalized after the transaction is
37300 **   committed or rolled back when running in "journal_mode=PERSIST" mode.
37301 **   If a journal file does not contain a master-journal pointer, it is
37302 **   finalized by overwriting the first journal header with zeroes. If
37303 **   it does contain a master-journal pointer the journal file is finalized
37304 **   by truncating it to zero bytes, just as if the connection were
37305 **   running in "journal_mode=truncate" mode.
37306 **
37307 **   Journal files that contain master journal pointers cannot be finalized
37308 **   simply by overwriting the first journal-header with zeroes, as the
37309 **   master journal pointer could interfere with hot-journal rollback of any
37310 **   subsequently interrupted transaction that reuses the journal file.
37311 **
37312 **   The flag is cleared as soon as the journal file is finalized (either
37313 **   by PagerCommitPhaseTwo or PagerRollback). If an IO error prevents the
37314 **   journal file from being successfully finalized, the setMaster flag
37315 **   is cleared anyway (and the pager will move to ERROR state).
37316 **
37317 ** doNotSpill, doNotSyncSpill
37318 **
37319 **   These two boolean variables control the behaviour of cache-spills
37320 **   (calls made by the pcache module to the pagerStress() routine to
37321 **   write cached data to the file-system in order to free up memory).
37322 **
37323 **   When doNotSpill is non-zero, writing to the database from pagerStress()
37324 **   is disabled altogether. This is done in a very obscure case that
37325 **   comes up during savepoint rollback that requires the pcache module
37326 **   to allocate a new page to prevent the journal file from being written
37327 **   while it is being traversed by code in pager_playback().
37328 **
37329 **   If doNotSyncSpill is non-zero, writing to the database from pagerStress()
37330 **   is permitted, but syncing the journal file is not. This flag is set
37331 **   by sqlite3PagerWrite() when the file-system sector-size is larger than
37332 **   the database page-size in order to prevent a journal sync from happening
37333 **   in between the journalling of two pages on the same sector.
37334 **
37335 ** subjInMemory
37336 **
37337 **   This is a boolean variable. If true, then any required sub-journal
37338 **   is opened as an in-memory journal file. If false, then in-memory
37339 **   sub-journals are only used for in-memory pager files.
37340 **
37341 **   This variable is updated by the upper layer each time a new
37342 **   write-transaction is opened.
37343 **
37344 ** dbSize, dbOrigSize, dbFileSize
37345 **
37346 **   Variable dbSize is set to the number of pages in the database file.
37347 **   It is valid in PAGER_READER and higher states (all states except for
37348 **   OPEN and ERROR).
37349 **
37350 **   dbSize is set based on the size of the database file, which may be
37351 **   larger than the size of the database (the value stored at offset
37352 **   28 of the database header by the btree). If the size of the file
37353 **   is not an integer multiple of the page-size, the value stored in
37354 **   dbSize is rounded down (i.e. a 5KB file with 2K page-size has dbSize==2).
37355 **   Except, any file that is greater than 0 bytes in size is considered
37356 **   to have at least one page. (i.e. a 1KB file with 2K page-size leads
37357 **   to dbSize==1).
37358 **
37359 **   During a write-transaction, if pages with page-numbers greater than
37360 **   dbSize are modified in the cache, dbSize is updated accordingly.
37361 **   Similarly, if the database is truncated using PagerTruncateImage(),
37362 **   dbSize is updated.
37363 **
37364 **   Variables dbOrigSize and dbFileSize are valid in states
37365 **   PAGER_WRITER_LOCKED and higher. dbOrigSize is a copy of the dbSize
37366 **   variable at the start of the transaction. It is used during rollback,
37367 **   and to determine whether or not pages need to be journalled before
37368 **   being modified.
37369 **
37370 **   Throughout a write-transaction, dbFileSize contains the size of
37371 **   the file on disk in pages. It is set to a copy of dbSize when the
37372 **   write-transaction is first opened, and updated when VFS calls are made
37373 **   to write or truncate the database file on disk.
37374 **
37375 **   The only reason the dbFileSize variable is required is to suppress
37376 **   unnecessary calls to xTruncate() after committing a transaction. If,
37377 **   when a transaction is committed, the dbFileSize variable indicates
37378 **   that the database file is larger than the database image (Pager.dbSize),
37379 **   pager_truncate() is called. The pager_truncate() call uses xFilesize()
37380 **   to measure the database file on disk, and then truncates it if required.
37381 **   dbFileSize is not used when rolling back a transaction. In this case
37382 **   pager_truncate() is called unconditionally (which means there may be
37383 **   a call to xFilesize() that is not strictly required). In either case,
37384 **   pager_truncate() may cause the file to become smaller or larger.
37385 **
37386 ** dbHintSize
37387 **
37388 **   The dbHintSize variable is used to limit the number of calls made to
37389 **   the VFS xFileControl(FCNTL_SIZE_HINT) method.
37390 **
37391 **   dbHintSize is set to a copy of the dbSize variable when a
37392 **   write-transaction is opened (at the same time as dbFileSize and
37393 **   dbOrigSize). If the xFileControl(FCNTL_SIZE_HINT) method is called,
37394 **   dbHintSize is increased to the number of pages that correspond to the
37395 **   size-hint passed to the method call. See pager_write_pagelist() for
37396 **   details.
37397 **
37398 ** errCode
37399 **
37400 **   The Pager.errCode variable is only ever used in PAGER_ERROR state. It
37401 **   is set to zero in all other states. In PAGER_ERROR state, Pager.errCode
37402 **   is always set to SQLITE_FULL, SQLITE_IOERR or one of the SQLITE_IOERR_XXX
37403 **   sub-codes.
37404 */
37405 struct Pager {
37406   sqlite3_vfs *pVfs;          /* OS functions to use for IO */
37407   u8 exclusiveMode;           /* Boolean. True if locking_mode==EXCLUSIVE */
37408   u8 journalMode;             /* One of the PAGER_JOURNALMODE_* values */
37409   u8 useJournal;              /* Use a rollback journal on this file */
37410   u8 noReadlock;              /* Do not bother to obtain readlocks */
37411   u8 noSync;                  /* Do not sync the journal if true */
37412   u8 fullSync;                /* Do extra syncs of the journal for robustness */
37413   u8 ckptSyncFlags;           /* SYNC_NORMAL or SYNC_FULL for checkpoint */
37414   u8 syncFlags;               /* SYNC_NORMAL or SYNC_FULL otherwise */
37415   u8 tempFile;                /* zFilename is a temporary file */
37416   u8 readOnly;                /* True for a read-only database */
37417   u8 memDb;                   /* True to inhibit all file I/O */
37418 
37419   /**************************************************************************
37420   ** The following block contains those class members that change during
37421   ** routine opertion.  Class members not in this block are either fixed
37422   ** when the pager is first created or else only change when there is a
37423   ** significant mode change (such as changing the page_size, locking_mode,
37424   ** or the journal_mode).  From another view, these class members describe
37425   ** the "state" of the pager, while other class members describe the
37426   ** "configuration" of the pager.
37427   */
37428   u8 eState;                  /* Pager state (OPEN, READER, WRITER_LOCKED..) */
37429   u8 eLock;                   /* Current lock held on database file */
37430   u8 changeCountDone;         /* Set after incrementing the change-counter */
37431   u8 setMaster;               /* True if a m-j name has been written to jrnl */
37432   u8 doNotSpill;              /* Do not spill the cache when non-zero */
37433   u8 doNotSyncSpill;          /* Do not do a spill that requires jrnl sync */
37434   u8 subjInMemory;            /* True to use in-memory sub-journals */
37435   Pgno dbSize;                /* Number of pages in the database */
37436   Pgno dbOrigSize;            /* dbSize before the current transaction */
37437   Pgno dbFileSize;            /* Number of pages in the database file */
37438   Pgno dbHintSize;            /* Value passed to FCNTL_SIZE_HINT call */
37439   int errCode;                /* One of several kinds of errors */
37440   int nRec;                   /* Pages journalled since last j-header written */
37441   u32 cksumInit;              /* Quasi-random value added to every checksum */
37442   u32 nSubRec;                /* Number of records written to sub-journal */
37443   Bitvec *pInJournal;         /* One bit for each page in the database file */
37444   sqlite3_file *fd;           /* File descriptor for database */
37445   sqlite3_file *jfd;          /* File descriptor for main journal */
37446   sqlite3_file *sjfd;         /* File descriptor for sub-journal */
37447   i64 journalOff;             /* Current write offset in the journal file */
37448   i64 journalHdr;             /* Byte offset to previous journal header */
37449   sqlite3_backup *pBackup;    /* Pointer to list of ongoing backup processes */
37450   PagerSavepoint *aSavepoint; /* Array of active savepoints */
37451   int nSavepoint;             /* Number of elements in aSavepoint[] */
37452   char dbFileVers[16];        /* Changes whenever database file changes */
37453   /*
37454   ** End of the routinely-changing class members
37455   ***************************************************************************/
37456 
37457   u16 nExtra;                 /* Add this many bytes to each in-memory page */
37458   i16 nReserve;               /* Number of unused bytes at end of each page */
37459   u32 vfsFlags;               /* Flags for sqlite3_vfs.xOpen() */
37460   u32 sectorSize;             /* Assumed sector size during rollback */
37461   int pageSize;               /* Number of bytes in a page */
37462   Pgno mxPgno;                /* Maximum allowed size of the database */
37463   i64 journalSizeLimit;       /* Size limit for persistent journal files */
37464   char *zFilename;            /* Name of the database file */
37465   char *zJournal;             /* Name of the journal file */
37466   int (*xBusyHandler)(void*); /* Function to call when busy */
37467   void *pBusyHandlerArg;      /* Context argument for xBusyHandler */
37468 #ifdef SQLITE_TEST
37469   int nHit, nMiss;            /* Cache hits and missing */
37470   int nRead, nWrite;          /* Database pages read/written */
37471 #endif
37472   void (*xReiniter)(DbPage*); /* Call this routine when reloading pages */
37473 #ifdef SQLITE_HAS_CODEC
37474   void *(*xCodec)(void*,void*,Pgno,int); /* Routine for en/decoding data */
37475   void (*xCodecSizeChng)(void*,int,int); /* Notify of page size changes */
37476   void (*xCodecFree)(void*);             /* Destructor for the codec */
37477   void *pCodec;               /* First argument to xCodec... methods */
37478 #endif
37479   char *pTmpSpace;            /* Pager.pageSize bytes of space for tmp use */
37480   PCache *pPCache;            /* Pointer to page cache object */
37481 #ifndef SQLITE_OMIT_WAL
37482   Wal *pWal;                  /* Write-ahead log used by "journal_mode=wal" */
37483   char *zWal;                 /* File name for write-ahead log */
37484 #endif
37485 };
37486 
37487 /*
37488 ** The following global variables hold counters used for
37489 ** testing purposes only.  These variables do not exist in
37490 ** a non-testing build.  These variables are not thread-safe.
37491 */
37492 #ifdef SQLITE_TEST
37493 SQLITE_API int sqlite3_pager_readdb_count = 0;    /* Number of full pages read from DB */
37494 SQLITE_API int sqlite3_pager_writedb_count = 0;   /* Number of full pages written to DB */
37495 SQLITE_API int sqlite3_pager_writej_count = 0;    /* Number of pages written to journal */
37496 # define PAGER_INCR(v)  v++
37497 #else
37498 # define PAGER_INCR(v)
37499 #endif
37500 
37501 
37502 
37503 /*
37504 ** Journal files begin with the following magic string.  The data
37505 ** was obtained from /dev/random.  It is used only as a sanity check.
37506 **
37507 ** Since version 2.8.0, the journal format contains additional sanity
37508 ** checking information.  If the power fails while the journal is being
37509 ** written, semi-random garbage data might appear in the journal
37510 ** file after power is restored.  If an attempt is then made
37511 ** to roll the journal back, the database could be corrupted.  The additional
37512 ** sanity checking data is an attempt to discover the garbage in the
37513 ** journal and ignore it.
37514 **
37515 ** The sanity checking information for the new journal format consists
37516 ** of a 32-bit checksum on each page of data.  The checksum covers both
37517 ** the page number and the pPager->pageSize bytes of data for the page.
37518 ** This cksum is initialized to a 32-bit random value that appears in the
37519 ** journal file right after the header.  The random initializer is important,
37520 ** because garbage data that appears at the end of a journal is likely
37521 ** data that was once in other files that have now been deleted.  If the
37522 ** garbage data came from an obsolete journal file, the checksums might
37523 ** be correct.  But by initializing the checksum to random value which
37524 ** is different for every journal, we minimize that risk.
37525 */
37526 static const unsigned char aJournalMagic[] = {
37527   0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd7,
37528 };
37529 
37530 /*
37531 ** The size of the of each page record in the journal is given by
37532 ** the following macro.
37533 */
37534 #define JOURNAL_PG_SZ(pPager)  ((pPager->pageSize) + 8)
37535 
37536 /*
37537 ** The journal header size for this pager. This is usually the same
37538 ** size as a single disk sector. See also setSectorSize().
37539 */
37540 #define JOURNAL_HDR_SZ(pPager) (pPager->sectorSize)
37541 
37542 /*
37543 ** The macro MEMDB is true if we are dealing with an in-memory database.
37544 ** We do this as a macro so that if the SQLITE_OMIT_MEMORYDB macro is set,
37545 ** the value of MEMDB will be a constant and the compiler will optimize
37546 ** out code that would never execute.
37547 */
37548 #ifdef SQLITE_OMIT_MEMORYDB
37549 # define MEMDB 0
37550 #else
37551 # define MEMDB pPager->memDb
37552 #endif
37553 
37554 /*
37555 ** The maximum legal page number is (2^31 - 1).
37556 */
37557 #define PAGER_MAX_PGNO 2147483647
37558 
37559 /*
37560 ** The argument to this macro is a file descriptor (type sqlite3_file*).
37561 ** Return 0 if it is not open, or non-zero (but not 1) if it is.
37562 **
37563 ** This is so that expressions can be written as:
37564 **
37565 **   if( isOpen(pPager->jfd) ){ ...
37566 **
37567 ** instead of
37568 **
37569 **   if( pPager->jfd->pMethods ){ ...
37570 */
37571 #define isOpen(pFd) ((pFd)->pMethods)
37572 
37573 /*
37574 ** Return true if this pager uses a write-ahead log instead of the usual
37575 ** rollback journal. Otherwise false.
37576 */
37577 #ifndef SQLITE_OMIT_WAL
37578 static int pagerUseWal(Pager *pPager){
37579   return (pPager->pWal!=0);
37580 }
37581 #else
37582 # define pagerUseWal(x) 0
37583 # define pagerRollbackWal(x) 0
37584 # define pagerWalFrames(v,w,x,y,z) 0
37585 # define pagerOpenWalIfPresent(z) SQLITE_OK
37586 # define pagerBeginReadTransaction(z) SQLITE_OK
37587 #endif
37588 
37589 #ifndef NDEBUG
37590 /*
37591 ** Usage:
37592 **
37593 **   assert( assert_pager_state(pPager) );
37594 **
37595 ** This function runs many asserts to try to find inconsistencies in
37596 ** the internal state of the Pager object.
37597 */
37598 static int assert_pager_state(Pager *p){
37599   Pager *pPager = p;
37600 
37601   /* State must be valid. */
37602   assert( p->eState==PAGER_OPEN
37603        || p->eState==PAGER_READER
37604        || p->eState==PAGER_WRITER_LOCKED
37605        || p->eState==PAGER_WRITER_CACHEMOD
37606        || p->eState==PAGER_WRITER_DBMOD
37607        || p->eState==PAGER_WRITER_FINISHED
37608        || p->eState==PAGER_ERROR
37609   );
37610 
37611   /* Regardless of the current state, a temp-file connection always behaves
37612   ** as if it has an exclusive lock on the database file. It never updates
37613   ** the change-counter field, so the changeCountDone flag is always set.
37614   */
37615   assert( p->tempFile==0 || p->eLock==EXCLUSIVE_LOCK );
37616   assert( p->tempFile==0 || pPager->changeCountDone );
37617 
37618   /* If the useJournal flag is clear, the journal-mode must be "OFF".
37619   ** And if the journal-mode is "OFF", the journal file must not be open.
37620   */
37621   assert( p->journalMode==PAGER_JOURNALMODE_OFF || p->useJournal );
37622   assert( p->journalMode!=PAGER_JOURNALMODE_OFF || !isOpen(p->jfd) );
37623 
37624   /* Check that MEMDB implies noSync. And an in-memory journal. Since
37625   ** this means an in-memory pager performs no IO at all, it cannot encounter
37626   ** either SQLITE_IOERR or SQLITE_FULL during rollback or while finalizing
37627   ** a journal file. (although the in-memory journal implementation may
37628   ** return SQLITE_IOERR_NOMEM while the journal file is being written). It
37629   ** is therefore not possible for an in-memory pager to enter the ERROR
37630   ** state.
37631   */
37632   if( MEMDB ){
37633     assert( p->noSync );
37634     assert( p->journalMode==PAGER_JOURNALMODE_OFF
37635          || p->journalMode==PAGER_JOURNALMODE_MEMORY
37636     );
37637     assert( p->eState!=PAGER_ERROR && p->eState!=PAGER_OPEN );
37638     assert( pagerUseWal(p)==0 );
37639   }
37640 
37641   /* If changeCountDone is set, a RESERVED lock or greater must be held
37642   ** on the file.
37643   */
37644   assert( pPager->changeCountDone==0 || pPager->eLock>=RESERVED_LOCK );
37645   assert( p->eLock!=PENDING_LOCK );
37646 
37647   switch( p->eState ){
37648     case PAGER_OPEN:
37649       assert( !MEMDB );
37650       assert( pPager->errCode==SQLITE_OK );
37651       assert( sqlite3PcacheRefCount(pPager->pPCache)==0 || pPager->tempFile );
37652       break;
37653 
37654     case PAGER_READER:
37655       assert( pPager->errCode==SQLITE_OK );
37656       assert( p->eLock!=UNKNOWN_LOCK );
37657       assert( p->eLock>=SHARED_LOCK || p->noReadlock );
37658       break;
37659 
37660     case PAGER_WRITER_LOCKED:
37661       assert( p->eLock!=UNKNOWN_LOCK );
37662       assert( pPager->errCode==SQLITE_OK );
37663       if( !pagerUseWal(pPager) ){
37664         assert( p->eLock>=RESERVED_LOCK );
37665       }
37666       assert( pPager->dbSize==pPager->dbOrigSize );
37667       assert( pPager->dbOrigSize==pPager->dbFileSize );
37668       assert( pPager->dbOrigSize==pPager->dbHintSize );
37669       assert( pPager->setMaster==0 );
37670       break;
37671 
37672     case PAGER_WRITER_CACHEMOD:
37673       assert( p->eLock!=UNKNOWN_LOCK );
37674       assert( pPager->errCode==SQLITE_OK );
37675       if( !pagerUseWal(pPager) ){
37676         /* It is possible that if journal_mode=wal here that neither the
37677         ** journal file nor the WAL file are open. This happens during
37678         ** a rollback transaction that switches from journal_mode=off
37679         ** to journal_mode=wal.
37680         */
37681         assert( p->eLock>=RESERVED_LOCK );
37682         assert( isOpen(p->jfd)
37683              || p->journalMode==PAGER_JOURNALMODE_OFF
37684              || p->journalMode==PAGER_JOURNALMODE_WAL
37685         );
37686       }
37687       assert( pPager->dbOrigSize==pPager->dbFileSize );
37688       assert( pPager->dbOrigSize==pPager->dbHintSize );
37689       break;
37690 
37691     case PAGER_WRITER_DBMOD:
37692       assert( p->eLock==EXCLUSIVE_LOCK );
37693       assert( pPager->errCode==SQLITE_OK );
37694       assert( !pagerUseWal(pPager) );
37695       assert( p->eLock>=EXCLUSIVE_LOCK );
37696       assert( isOpen(p->jfd)
37697            || p->journalMode==PAGER_JOURNALMODE_OFF
37698            || p->journalMode==PAGER_JOURNALMODE_WAL
37699       );
37700       assert( pPager->dbOrigSize<=pPager->dbHintSize );
37701       break;
37702 
37703     case PAGER_WRITER_FINISHED:
37704       assert( p->eLock==EXCLUSIVE_LOCK );
37705       assert( pPager->errCode==SQLITE_OK );
37706       assert( !pagerUseWal(pPager) );
37707       assert( isOpen(p->jfd)
37708            || p->journalMode==PAGER_JOURNALMODE_OFF
37709            || p->journalMode==PAGER_JOURNALMODE_WAL
37710       );
37711       break;
37712 
37713     case PAGER_ERROR:
37714       /* There must be at least one outstanding reference to the pager if
37715       ** in ERROR state. Otherwise the pager should have already dropped
37716       ** back to OPEN state.
37717       */
37718       assert( pPager->errCode!=SQLITE_OK );
37719       assert( sqlite3PcacheRefCount(pPager->pPCache)>0 );
37720       break;
37721   }
37722 
37723   return 1;
37724 }
37725 #endif /* ifndef NDEBUG */
37726 
37727 #ifdef SQLITE_DEBUG
37728 /*
37729 ** Return a pointer to a human readable string in a static buffer
37730 ** containing the state of the Pager object passed as an argument. This
37731 ** is intended to be used within debuggers. For example, as an alternative
37732 ** to "print *pPager" in gdb:
37733 **
37734 ** (gdb) printf "%s", print_pager_state(pPager)
37735 */
37736 static char *print_pager_state(Pager *p){
37737   static char zRet[1024];
37738 
37739   sqlite3_snprintf(1024, zRet,
37740       "Filename:      %s\n"
37741       "State:         %s errCode=%d\n"
37742       "Lock:          %s\n"
37743       "Locking mode:  locking_mode=%s\n"
37744       "Journal mode:  journal_mode=%s\n"
37745       "Backing store: tempFile=%d memDb=%d useJournal=%d\n"
37746       "Journal:       journalOff=%lld journalHdr=%lld\n"
37747       "Size:          dbsize=%d dbOrigSize=%d dbFileSize=%d\n"
37748       , p->zFilename
37749       , p->eState==PAGER_OPEN            ? "OPEN" :
37750         p->eState==PAGER_READER          ? "READER" :
37751         p->eState==PAGER_WRITER_LOCKED   ? "WRITER_LOCKED" :
37752         p->eState==PAGER_WRITER_CACHEMOD ? "WRITER_CACHEMOD" :
37753         p->eState==PAGER_WRITER_DBMOD    ? "WRITER_DBMOD" :
37754         p->eState==PAGER_WRITER_FINISHED ? "WRITER_FINISHED" :
37755         p->eState==PAGER_ERROR           ? "ERROR" : "?error?"
37756       , (int)p->errCode
37757       , p->eLock==NO_LOCK         ? "NO_LOCK" :
37758         p->eLock==RESERVED_LOCK   ? "RESERVED" :
37759         p->eLock==EXCLUSIVE_LOCK  ? "EXCLUSIVE" :
37760         p->eLock==SHARED_LOCK     ? "SHARED" :
37761         p->eLock==UNKNOWN_LOCK    ? "UNKNOWN" : "?error?"
37762       , p->exclusiveMode ? "exclusive" : "normal"
37763       , p->journalMode==PAGER_JOURNALMODE_MEMORY   ? "memory" :
37764         p->journalMode==PAGER_JOURNALMODE_OFF      ? "off" :
37765         p->journalMode==PAGER_JOURNALMODE_DELETE   ? "delete" :
37766         p->journalMode==PAGER_JOURNALMODE_PERSIST  ? "persist" :
37767         p->journalMode==PAGER_JOURNALMODE_TRUNCATE ? "truncate" :
37768         p->journalMode==PAGER_JOURNALMODE_WAL      ? "wal" : "?error?"
37769       , (int)p->tempFile, (int)p->memDb, (int)p->useJournal
37770       , p->journalOff, p->journalHdr
37771       , (int)p->dbSize, (int)p->dbOrigSize, (int)p->dbFileSize
37772   );
37773 
37774   return zRet;
37775 }
37776 #endif
37777 
37778 /*
37779 ** Return true if it is necessary to write page *pPg into the sub-journal.
37780 ** A page needs to be written into the sub-journal if there exists one
37781 ** or more open savepoints for which:
37782 **
37783 **   * The page-number is less than or equal to PagerSavepoint.nOrig, and
37784 **   * The bit corresponding to the page-number is not set in
37785 **     PagerSavepoint.pInSavepoint.
37786 */
37787 static int subjRequiresPage(PgHdr *pPg){
37788   Pgno pgno = pPg->pgno;
37789   Pager *pPager = pPg->pPager;
37790   int i;
37791   for(i=0; i<pPager->nSavepoint; i++){
37792     PagerSavepoint *p = &pPager->aSavepoint[i];
37793     if( p->nOrig>=pgno && 0==sqlite3BitvecTest(p->pInSavepoint, pgno) ){
37794       return 1;
37795     }
37796   }
37797   return 0;
37798 }
37799 
37800 /*
37801 ** Return true if the page is already in the journal file.
37802 */
37803 static int pageInJournal(PgHdr *pPg){
37804   return sqlite3BitvecTest(pPg->pPager->pInJournal, pPg->pgno);
37805 }
37806 
37807 /*
37808 ** Read a 32-bit integer from the given file descriptor.  Store the integer
37809 ** that is read in *pRes.  Return SQLITE_OK if everything worked, or an
37810 ** error code is something goes wrong.
37811 **
37812 ** All values are stored on disk as big-endian.
37813 */
37814 static int read32bits(sqlite3_file *fd, i64 offset, u32 *pRes){
37815   unsigned char ac[4];
37816   int rc = sqlite3OsRead(fd, ac, sizeof(ac), offset);
37817   if( rc==SQLITE_OK ){
37818     *pRes = sqlite3Get4byte(ac);
37819   }
37820   return rc;
37821 }
37822 
37823 /*
37824 ** Write a 32-bit integer into a string buffer in big-endian byte order.
37825 */
37826 #define put32bits(A,B)  sqlite3Put4byte((u8*)A,B)
37827 
37828 
37829 /*
37830 ** Write a 32-bit integer into the given file descriptor.  Return SQLITE_OK
37831 ** on success or an error code is something goes wrong.
37832 */
37833 static int write32bits(sqlite3_file *fd, i64 offset, u32 val){
37834   char ac[4];
37835   put32bits(ac, val);
37836   return sqlite3OsWrite(fd, ac, 4, offset);
37837 }
37838 
37839 /*
37840 ** Unlock the database file to level eLock, which must be either NO_LOCK
37841 ** or SHARED_LOCK. Regardless of whether or not the call to xUnlock()
37842 ** succeeds, set the Pager.eLock variable to match the (attempted) new lock.
37843 **
37844 ** Except, if Pager.eLock is set to UNKNOWN_LOCK when this function is
37845 ** called, do not modify it. See the comment above the #define of
37846 ** UNKNOWN_LOCK for an explanation of this.
37847 */
37848 static int pagerUnlockDb(Pager *pPager, int eLock){
37849   int rc = SQLITE_OK;
37850 
37851   assert( !pPager->exclusiveMode || pPager->eLock==eLock );
37852   assert( eLock==NO_LOCK || eLock==SHARED_LOCK );
37853   assert( eLock!=NO_LOCK || pagerUseWal(pPager)==0 );
37854   if( isOpen(pPager->fd) ){
37855     assert( pPager->eLock>=eLock );
37856     rc = sqlite3OsUnlock(pPager->fd, eLock);
37857     if( pPager->eLock!=UNKNOWN_LOCK ){
37858       pPager->eLock = (u8)eLock;
37859     }
37860     IOTRACE(("UNLOCK %p %d\n", pPager, eLock))
37861   }
37862   return rc;
37863 }
37864 
37865 /*
37866 ** Lock the database file to level eLock, which must be either SHARED_LOCK,
37867 ** RESERVED_LOCK or EXCLUSIVE_LOCK. If the caller is successful, set the
37868 ** Pager.eLock variable to the new locking state.
37869 **
37870 ** Except, if Pager.eLock is set to UNKNOWN_LOCK when this function is
37871 ** called, do not modify it unless the new locking state is EXCLUSIVE_LOCK.
37872 ** See the comment above the #define of UNKNOWN_LOCK for an explanation
37873 ** of this.
37874 */
37875 static int pagerLockDb(Pager *pPager, int eLock){
37876   int rc = SQLITE_OK;
37877 
37878   assert( eLock==SHARED_LOCK || eLock==RESERVED_LOCK || eLock==EXCLUSIVE_LOCK );
37879   if( pPager->eLock<eLock || pPager->eLock==UNKNOWN_LOCK ){
37880     rc = sqlite3OsLock(pPager->fd, eLock);
37881     if( rc==SQLITE_OK && (pPager->eLock!=UNKNOWN_LOCK||eLock==EXCLUSIVE_LOCK) ){
37882       pPager->eLock = (u8)eLock;
37883       IOTRACE(("LOCK %p %d\n", pPager, eLock))
37884     }
37885   }
37886   return rc;
37887 }
37888 
37889 /*
37890 ** This function determines whether or not the atomic-write optimization
37891 ** can be used with this pager. The optimization can be used if:
37892 **
37893 **  (a) the value returned by OsDeviceCharacteristics() indicates that
37894 **      a database page may be written atomically, and
37895 **  (b) the value returned by OsSectorSize() is less than or equal
37896 **      to the page size.
37897 **
37898 ** The optimization is also always enabled for temporary files. It is
37899 ** an error to call this function if pPager is opened on an in-memory
37900 ** database.
37901 **
37902 ** If the optimization cannot be used, 0 is returned. If it can be used,
37903 ** then the value returned is the size of the journal file when it
37904 ** contains rollback data for exactly one page.
37905 */
37906 #ifdef SQLITE_ENABLE_ATOMIC_WRITE
37907 static int jrnlBufferSize(Pager *pPager){
37908   assert( !MEMDB );
37909   if( !pPager->tempFile ){
37910     int dc;                           /* Device characteristics */
37911     int nSector;                      /* Sector size */
37912     int szPage;                       /* Page size */
37913 
37914     assert( isOpen(pPager->fd) );
37915     dc = sqlite3OsDeviceCharacteristics(pPager->fd);
37916     nSector = pPager->sectorSize;
37917     szPage = pPager->pageSize;
37918 
37919     assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
37920     assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
37921     if( 0==(dc&(SQLITE_IOCAP_ATOMIC|(szPage>>8)) || nSector>szPage) ){
37922       return 0;
37923     }
37924   }
37925 
37926   return JOURNAL_HDR_SZ(pPager) + JOURNAL_PG_SZ(pPager);
37927 }
37928 #endif
37929 
37930 /*
37931 ** If SQLITE_CHECK_PAGES is defined then we do some sanity checking
37932 ** on the cache using a hash function.  This is used for testing
37933 ** and debugging only.
37934 */
37935 #ifdef SQLITE_CHECK_PAGES
37936 /*
37937 ** Return a 32-bit hash of the page data for pPage.
37938 */
37939 static u32 pager_datahash(int nByte, unsigned char *pData){
37940   u32 hash = 0;
37941   int i;
37942   for(i=0; i<nByte; i++){
37943     hash = (hash*1039) + pData[i];
37944   }
37945   return hash;
37946 }
37947 static u32 pager_pagehash(PgHdr *pPage){
37948   return pager_datahash(pPage->pPager->pageSize, (unsigned char *)pPage->pData);
37949 }
37950 static void pager_set_pagehash(PgHdr *pPage){
37951   pPage->pageHash = pager_pagehash(pPage);
37952 }
37953 
37954 /*
37955 ** The CHECK_PAGE macro takes a PgHdr* as an argument. If SQLITE_CHECK_PAGES
37956 ** is defined, and NDEBUG is not defined, an assert() statement checks
37957 ** that the page is either dirty or still matches the calculated page-hash.
37958 */
37959 #define CHECK_PAGE(x) checkPage(x)
37960 static void checkPage(PgHdr *pPg){
37961   Pager *pPager = pPg->pPager;
37962   assert( pPager->eState!=PAGER_ERROR );
37963   assert( (pPg->flags&PGHDR_DIRTY) || pPg->pageHash==pager_pagehash(pPg) );
37964 }
37965 
37966 #else
37967 #define pager_datahash(X,Y)  0
37968 #define pager_pagehash(X)  0
37969 #define pager_set_pagehash(X)
37970 #define CHECK_PAGE(x)
37971 #endif  /* SQLITE_CHECK_PAGES */
37972 
37973 /*
37974 ** When this is called the journal file for pager pPager must be open.
37975 ** This function attempts to read a master journal file name from the
37976 ** end of the file and, if successful, copies it into memory supplied
37977 ** by the caller. See comments above writeMasterJournal() for the format
37978 ** used to store a master journal file name at the end of a journal file.
37979 **
37980 ** zMaster must point to a buffer of at least nMaster bytes allocated by
37981 ** the caller. This should be sqlite3_vfs.mxPathname+1 (to ensure there is
37982 ** enough space to write the master journal name). If the master journal
37983 ** name in the journal is longer than nMaster bytes (including a
37984 ** nul-terminator), then this is handled as if no master journal name
37985 ** were present in the journal.
37986 **
37987 ** If a master journal file name is present at the end of the journal
37988 ** file, then it is copied into the buffer pointed to by zMaster. A
37989 ** nul-terminator byte is appended to the buffer following the master
37990 ** journal file name.
37991 **
37992 ** If it is determined that no master journal file name is present
37993 ** zMaster[0] is set to 0 and SQLITE_OK returned.
37994 **
37995 ** If an error occurs while reading from the journal file, an SQLite
37996 ** error code is returned.
37997 */
37998 static int readMasterJournal(sqlite3_file *pJrnl, char *zMaster, u32 nMaster){
37999   int rc;                    /* Return code */
38000   u32 len;                   /* Length in bytes of master journal name */
38001   i64 szJ;                   /* Total size in bytes of journal file pJrnl */
38002   u32 cksum;                 /* MJ checksum value read from journal */
38003   u32 u;                     /* Unsigned loop counter */
38004   unsigned char aMagic[8];   /* A buffer to hold the magic header */
38005   zMaster[0] = '\0';
38006 
38007   if( SQLITE_OK!=(rc = sqlite3OsFileSize(pJrnl, &szJ))
38008    || szJ<16
38009    || SQLITE_OK!=(rc = read32bits(pJrnl, szJ-16, &len))
38010    || len>=nMaster
38011    || SQLITE_OK!=(rc = read32bits(pJrnl, szJ-12, &cksum))
38012    || SQLITE_OK!=(rc = sqlite3OsRead(pJrnl, aMagic, 8, szJ-8))
38013    || memcmp(aMagic, aJournalMagic, 8)
38014    || SQLITE_OK!=(rc = sqlite3OsRead(pJrnl, zMaster, len, szJ-16-len))
38015   ){
38016     return rc;
38017   }
38018 
38019   /* See if the checksum matches the master journal name */
38020   for(u=0; u<len; u++){
38021     cksum -= zMaster[u];
38022   }
38023   if( cksum ){
38024     /* If the checksum doesn't add up, then one or more of the disk sectors
38025     ** containing the master journal filename is corrupted. This means
38026     ** definitely roll back, so just return SQLITE_OK and report a (nul)
38027     ** master-journal filename.
38028     */
38029     len = 0;
38030   }
38031   zMaster[len] = '\0';
38032 
38033   return SQLITE_OK;
38034 }
38035 
38036 /*
38037 ** Return the offset of the sector boundary at or immediately
38038 ** following the value in pPager->journalOff, assuming a sector
38039 ** size of pPager->sectorSize bytes.
38040 **
38041 ** i.e for a sector size of 512:
38042 **
38043 **   Pager.journalOff          Return value
38044 **   ---------------------------------------
38045 **   0                         0
38046 **   512                       512
38047 **   100                       512
38048 **   2000                      2048
38049 **
38050 */
38051 static i64 journalHdrOffset(Pager *pPager){
38052   i64 offset = 0;
38053   i64 c = pPager->journalOff;
38054   if( c ){
38055     offset = ((c-1)/JOURNAL_HDR_SZ(pPager) + 1) * JOURNAL_HDR_SZ(pPager);
38056   }
38057   assert( offset%JOURNAL_HDR_SZ(pPager)==0 );
38058   assert( offset>=c );
38059   assert( (offset-c)<JOURNAL_HDR_SZ(pPager) );
38060   return offset;
38061 }
38062 
38063 /*
38064 ** The journal file must be open when this function is called.
38065 **
38066 ** This function is a no-op if the journal file has not been written to
38067 ** within the current transaction (i.e. if Pager.journalOff==0).
38068 **
38069 ** If doTruncate is non-zero or the Pager.journalSizeLimit variable is
38070 ** set to 0, then truncate the journal file to zero bytes in size. Otherwise,
38071 ** zero the 28-byte header at the start of the journal file. In either case,
38072 ** if the pager is not in no-sync mode, sync the journal file immediately
38073 ** after writing or truncating it.
38074 **
38075 ** If Pager.journalSizeLimit is set to a positive, non-zero value, and
38076 ** following the truncation or zeroing described above the size of the
38077 ** journal file in bytes is larger than this value, then truncate the
38078 ** journal file to Pager.journalSizeLimit bytes. The journal file does
38079 ** not need to be synced following this operation.
38080 **
38081 ** If an IO error occurs, abandon processing and return the IO error code.
38082 ** Otherwise, return SQLITE_OK.
38083 */
38084 static int zeroJournalHdr(Pager *pPager, int doTruncate){
38085   int rc = SQLITE_OK;                               /* Return code */
38086   assert( isOpen(pPager->jfd) );
38087   if( pPager->journalOff ){
38088     const i64 iLimit = pPager->journalSizeLimit;    /* Local cache of jsl */
38089 
38090     IOTRACE(("JZEROHDR %p\n", pPager))
38091     if( doTruncate || iLimit==0 ){
38092       rc = sqlite3OsTruncate(pPager->jfd, 0);
38093     }else{
38094       static const char zeroHdr[28] = {0};
38095       rc = sqlite3OsWrite(pPager->jfd, zeroHdr, sizeof(zeroHdr), 0);
38096     }
38097     if( rc==SQLITE_OK && !pPager->noSync ){
38098       rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_DATAONLY|pPager->syncFlags);
38099     }
38100 
38101     /* At this point the transaction is committed but the write lock
38102     ** is still held on the file. If there is a size limit configured for
38103     ** the persistent journal and the journal file currently consumes more
38104     ** space than that limit allows for, truncate it now. There is no need
38105     ** to sync the file following this operation.
38106     */
38107     if( rc==SQLITE_OK && iLimit>0 ){
38108       i64 sz;
38109       rc = sqlite3OsFileSize(pPager->jfd, &sz);
38110       if( rc==SQLITE_OK && sz>iLimit ){
38111         rc = sqlite3OsTruncate(pPager->jfd, iLimit);
38112       }
38113     }
38114   }
38115   return rc;
38116 }
38117 
38118 /*
38119 ** The journal file must be open when this routine is called. A journal
38120 ** header (JOURNAL_HDR_SZ bytes) is written into the journal file at the
38121 ** current location.
38122 **
38123 ** The format for the journal header is as follows:
38124 ** - 8 bytes: Magic identifying journal format.
38125 ** - 4 bytes: Number of records in journal, or -1 no-sync mode is on.
38126 ** - 4 bytes: Random number used for page hash.
38127 ** - 4 bytes: Initial database page count.
38128 ** - 4 bytes: Sector size used by the process that wrote this journal.
38129 ** - 4 bytes: Database page size.
38130 **
38131 ** Followed by (JOURNAL_HDR_SZ - 28) bytes of unused space.
38132 */
38133 static int writeJournalHdr(Pager *pPager){
38134   int rc = SQLITE_OK;                 /* Return code */
38135   char *zHeader = pPager->pTmpSpace;  /* Temporary space used to build header */
38136   u32 nHeader = (u32)pPager->pageSize;/* Size of buffer pointed to by zHeader */
38137   u32 nWrite;                         /* Bytes of header sector written */
38138   int ii;                             /* Loop counter */
38139 
38140   assert( isOpen(pPager->jfd) );      /* Journal file must be open. */
38141 
38142   if( nHeader>JOURNAL_HDR_SZ(pPager) ){
38143     nHeader = JOURNAL_HDR_SZ(pPager);
38144   }
38145 
38146   /* If there are active savepoints and any of them were created
38147   ** since the most recent journal header was written, update the
38148   ** PagerSavepoint.iHdrOffset fields now.
38149   */
38150   for(ii=0; ii<pPager->nSavepoint; ii++){
38151     if( pPager->aSavepoint[ii].iHdrOffset==0 ){
38152       pPager->aSavepoint[ii].iHdrOffset = pPager->journalOff;
38153     }
38154   }
38155 
38156   pPager->journalHdr = pPager->journalOff = journalHdrOffset(pPager);
38157 
38158   /*
38159   ** Write the nRec Field - the number of page records that follow this
38160   ** journal header. Normally, zero is written to this value at this time.
38161   ** After the records are added to the journal (and the journal synced,
38162   ** if in full-sync mode), the zero is overwritten with the true number
38163   ** of records (see syncJournal()).
38164   **
38165   ** A faster alternative is to write 0xFFFFFFFF to the nRec field. When
38166   ** reading the journal this value tells SQLite to assume that the
38167   ** rest of the journal file contains valid page records. This assumption
38168   ** is dangerous, as if a failure occurred whilst writing to the journal
38169   ** file it may contain some garbage data. There are two scenarios
38170   ** where this risk can be ignored:
38171   **
38172   **   * When the pager is in no-sync mode. Corruption can follow a
38173   **     power failure in this case anyway.
38174   **
38175   **   * When the SQLITE_IOCAP_SAFE_APPEND flag is set. This guarantees
38176   **     that garbage data is never appended to the journal file.
38177   */
38178   assert( isOpen(pPager->fd) || pPager->noSync );
38179   if( pPager->noSync || (pPager->journalMode==PAGER_JOURNALMODE_MEMORY)
38180    || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND)
38181   ){
38182     memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic));
38183     put32bits(&zHeader[sizeof(aJournalMagic)], 0xffffffff);
38184   }else{
38185     memset(zHeader, 0, sizeof(aJournalMagic)+4);
38186   }
38187 
38188   /* The random check-hash initialiser */
38189   sqlite3_randomness(sizeof(pPager->cksumInit), &pPager->cksumInit);
38190   put32bits(&zHeader[sizeof(aJournalMagic)+4], pPager->cksumInit);
38191   /* The initial database size */
38192   put32bits(&zHeader[sizeof(aJournalMagic)+8], pPager->dbOrigSize);
38193   /* The assumed sector size for this process */
38194   put32bits(&zHeader[sizeof(aJournalMagic)+12], pPager->sectorSize);
38195 
38196   /* The page size */
38197   put32bits(&zHeader[sizeof(aJournalMagic)+16], pPager->pageSize);
38198 
38199   /* Initializing the tail of the buffer is not necessary.  Everything
38200   ** works find if the following memset() is omitted.  But initializing
38201   ** the memory prevents valgrind from complaining, so we are willing to
38202   ** take the performance hit.
38203   */
38204   memset(&zHeader[sizeof(aJournalMagic)+20], 0,
38205          nHeader-(sizeof(aJournalMagic)+20));
38206 
38207   /* In theory, it is only necessary to write the 28 bytes that the
38208   ** journal header consumes to the journal file here. Then increment the
38209   ** Pager.journalOff variable by JOURNAL_HDR_SZ so that the next
38210   ** record is written to the following sector (leaving a gap in the file
38211   ** that will be implicitly filled in by the OS).
38212   **
38213   ** However it has been discovered that on some systems this pattern can
38214   ** be significantly slower than contiguously writing data to the file,
38215   ** even if that means explicitly writing data to the block of
38216   ** (JOURNAL_HDR_SZ - 28) bytes that will not be used. So that is what
38217   ** is done.
38218   **
38219   ** The loop is required here in case the sector-size is larger than the
38220   ** database page size. Since the zHeader buffer is only Pager.pageSize
38221   ** bytes in size, more than one call to sqlite3OsWrite() may be required
38222   ** to populate the entire journal header sector.
38223   */
38224   for(nWrite=0; rc==SQLITE_OK&&nWrite<JOURNAL_HDR_SZ(pPager); nWrite+=nHeader){
38225     IOTRACE(("JHDR %p %lld %d\n", pPager, pPager->journalHdr, nHeader))
38226     rc = sqlite3OsWrite(pPager->jfd, zHeader, nHeader, pPager->journalOff);
38227     assert( pPager->journalHdr <= pPager->journalOff );
38228     pPager->journalOff += nHeader;
38229   }
38230 
38231   return rc;
38232 }
38233 
38234 /*
38235 ** The journal file must be open when this is called. A journal header file
38236 ** (JOURNAL_HDR_SZ bytes) is read from the current location in the journal
38237 ** file. The current location in the journal file is given by
38238 ** pPager->journalOff. See comments above function writeJournalHdr() for
38239 ** a description of the journal header format.
38240 **
38241 ** If the header is read successfully, *pNRec is set to the number of
38242 ** page records following this header and *pDbSize is set to the size of the
38243 ** database before the transaction began, in pages. Also, pPager->cksumInit
38244 ** is set to the value read from the journal header. SQLITE_OK is returned
38245 ** in this case.
38246 **
38247 ** If the journal header file appears to be corrupted, SQLITE_DONE is
38248 ** returned and *pNRec and *PDbSize are undefined.  If JOURNAL_HDR_SZ bytes
38249 ** cannot be read from the journal file an error code is returned.
38250 */
38251 static int readJournalHdr(
38252   Pager *pPager,               /* Pager object */
38253   int isHot,
38254   i64 journalSize,             /* Size of the open journal file in bytes */
38255   u32 *pNRec,                  /* OUT: Value read from the nRec field */
38256   u32 *pDbSize                 /* OUT: Value of original database size field */
38257 ){
38258   int rc;                      /* Return code */
38259   unsigned char aMagic[8];     /* A buffer to hold the magic header */
38260   i64 iHdrOff;                 /* Offset of journal header being read */
38261 
38262   assert( isOpen(pPager->jfd) );      /* Journal file must be open. */
38263 
38264   /* Advance Pager.journalOff to the start of the next sector. If the
38265   ** journal file is too small for there to be a header stored at this
38266   ** point, return SQLITE_DONE.
38267   */
38268   pPager->journalOff = journalHdrOffset(pPager);
38269   if( pPager->journalOff+JOURNAL_HDR_SZ(pPager) > journalSize ){
38270     return SQLITE_DONE;
38271   }
38272   iHdrOff = pPager->journalOff;
38273 
38274   /* Read in the first 8 bytes of the journal header. If they do not match
38275   ** the  magic string found at the start of each journal header, return
38276   ** SQLITE_DONE. If an IO error occurs, return an error code. Otherwise,
38277   ** proceed.
38278   */
38279   if( isHot || iHdrOff!=pPager->journalHdr ){
38280     rc = sqlite3OsRead(pPager->jfd, aMagic, sizeof(aMagic), iHdrOff);
38281     if( rc ){
38282       return rc;
38283     }
38284     if( memcmp(aMagic, aJournalMagic, sizeof(aMagic))!=0 ){
38285       return SQLITE_DONE;
38286     }
38287   }
38288 
38289   /* Read the first three 32-bit fields of the journal header: The nRec
38290   ** field, the checksum-initializer and the database size at the start
38291   ** of the transaction. Return an error code if anything goes wrong.
38292   */
38293   if( SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+8, pNRec))
38294    || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+12, &pPager->cksumInit))
38295    || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+16, pDbSize))
38296   ){
38297     return rc;
38298   }
38299 
38300   if( pPager->journalOff==0 ){
38301     u32 iPageSize;               /* Page-size field of journal header */
38302     u32 iSectorSize;             /* Sector-size field of journal header */
38303 
38304     /* Read the page-size and sector-size journal header fields. */
38305     if( SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+20, &iSectorSize))
38306      || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+24, &iPageSize))
38307     ){
38308       return rc;
38309     }
38310 
38311     /* Versions of SQLite prior to 3.5.8 set the page-size field of the
38312     ** journal header to zero. In this case, assume that the Pager.pageSize
38313     ** variable is already set to the correct page size.
38314     */
38315     if( iPageSize==0 ){
38316       iPageSize = pPager->pageSize;
38317     }
38318 
38319     /* Check that the values read from the page-size and sector-size fields
38320     ** are within range. To be 'in range', both values need to be a power
38321     ** of two greater than or equal to 512 or 32, and not greater than their
38322     ** respective compile time maximum limits.
38323     */
38324     if( iPageSize<512                  || iSectorSize<32
38325      || iPageSize>SQLITE_MAX_PAGE_SIZE || iSectorSize>MAX_SECTOR_SIZE
38326      || ((iPageSize-1)&iPageSize)!=0   || ((iSectorSize-1)&iSectorSize)!=0
38327     ){
38328       /* If the either the page-size or sector-size in the journal-header is
38329       ** invalid, then the process that wrote the journal-header must have
38330       ** crashed before the header was synced. In this case stop reading
38331       ** the journal file here.
38332       */
38333       return SQLITE_DONE;
38334     }
38335 
38336     /* Update the page-size to match the value read from the journal.
38337     ** Use a testcase() macro to make sure that malloc failure within
38338     ** PagerSetPagesize() is tested.
38339     */
38340     rc = sqlite3PagerSetPagesize(pPager, &iPageSize, -1);
38341     testcase( rc!=SQLITE_OK );
38342 
38343     /* Update the assumed sector-size to match the value used by
38344     ** the process that created this journal. If this journal was
38345     ** created by a process other than this one, then this routine
38346     ** is being called from within pager_playback(). The local value
38347     ** of Pager.sectorSize is restored at the end of that routine.
38348     */
38349     pPager->sectorSize = iSectorSize;
38350   }
38351 
38352   pPager->journalOff += JOURNAL_HDR_SZ(pPager);
38353   return rc;
38354 }
38355 
38356 
38357 /*
38358 ** Write the supplied master journal name into the journal file for pager
38359 ** pPager at the current location. The master journal name must be the last
38360 ** thing written to a journal file. If the pager is in full-sync mode, the
38361 ** journal file descriptor is advanced to the next sector boundary before
38362 ** anything is written. The format is:
38363 **
38364 **   + 4 bytes: PAGER_MJ_PGNO.
38365 **   + N bytes: Master journal filename in utf-8.
38366 **   + 4 bytes: N (length of master journal name in bytes, no nul-terminator).
38367 **   + 4 bytes: Master journal name checksum.
38368 **   + 8 bytes: aJournalMagic[].
38369 **
38370 ** The master journal page checksum is the sum of the bytes in the master
38371 ** journal name, where each byte is interpreted as a signed 8-bit integer.
38372 **
38373 ** If zMaster is a NULL pointer (occurs for a single database transaction),
38374 ** this call is a no-op.
38375 */
38376 static int writeMasterJournal(Pager *pPager, const char *zMaster){
38377   int rc;                          /* Return code */
38378   int nMaster;                     /* Length of string zMaster */
38379   i64 iHdrOff;                     /* Offset of header in journal file */
38380   i64 jrnlSize;                    /* Size of journal file on disk */
38381   u32 cksum = 0;                   /* Checksum of string zMaster */
38382 
38383   assert( pPager->setMaster==0 );
38384   assert( !pagerUseWal(pPager) );
38385 
38386   if( !zMaster
38387    || pPager->journalMode==PAGER_JOURNALMODE_MEMORY
38388    || pPager->journalMode==PAGER_JOURNALMODE_OFF
38389   ){
38390     return SQLITE_OK;
38391   }
38392   pPager->setMaster = 1;
38393   assert( isOpen(pPager->jfd) );
38394   assert( pPager->journalHdr <= pPager->journalOff );
38395 
38396   /* Calculate the length in bytes and the checksum of zMaster */
38397   for(nMaster=0; zMaster[nMaster]; nMaster++){
38398     cksum += zMaster[nMaster];
38399   }
38400 
38401   /* If in full-sync mode, advance to the next disk sector before writing
38402   ** the master journal name. This is in case the previous page written to
38403   ** the journal has already been synced.
38404   */
38405   if( pPager->fullSync ){
38406     pPager->journalOff = journalHdrOffset(pPager);
38407   }
38408   iHdrOff = pPager->journalOff;
38409 
38410   /* Write the master journal data to the end of the journal file. If
38411   ** an error occurs, return the error code to the caller.
38412   */
38413   if( (0 != (rc = write32bits(pPager->jfd, iHdrOff, PAGER_MJ_PGNO(pPager))))
38414    || (0 != (rc = sqlite3OsWrite(pPager->jfd, zMaster, nMaster, iHdrOff+4)))
38415    || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nMaster, nMaster)))
38416    || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nMaster+4, cksum)))
38417    || (0 != (rc = sqlite3OsWrite(pPager->jfd, aJournalMagic, 8, iHdrOff+4+nMaster+8)))
38418   ){
38419     return rc;
38420   }
38421   pPager->journalOff += (nMaster+20);
38422 
38423   /* If the pager is in peristent-journal mode, then the physical
38424   ** journal-file may extend past the end of the master-journal name
38425   ** and 8 bytes of magic data just written to the file. This is
38426   ** dangerous because the code to rollback a hot-journal file
38427   ** will not be able to find the master-journal name to determine
38428   ** whether or not the journal is hot.
38429   **
38430   ** Easiest thing to do in this scenario is to truncate the journal
38431   ** file to the required size.
38432   */
38433   if( SQLITE_OK==(rc = sqlite3OsFileSize(pPager->jfd, &jrnlSize))
38434    && jrnlSize>pPager->journalOff
38435   ){
38436     rc = sqlite3OsTruncate(pPager->jfd, pPager->journalOff);
38437   }
38438   return rc;
38439 }
38440 
38441 /*
38442 ** Find a page in the hash table given its page number. Return
38443 ** a pointer to the page or NULL if the requested page is not
38444 ** already in memory.
38445 */
38446 static PgHdr *pager_lookup(Pager *pPager, Pgno pgno){
38447   PgHdr *p;                         /* Return value */
38448 
38449   /* It is not possible for a call to PcacheFetch() with createFlag==0 to
38450   ** fail, since no attempt to allocate dynamic memory will be made.
38451   */
38452   (void)sqlite3PcacheFetch(pPager->pPCache, pgno, 0, &p);
38453   return p;
38454 }
38455 
38456 /*
38457 ** Discard the entire contents of the in-memory page-cache.
38458 */
38459 static void pager_reset(Pager *pPager){
38460   sqlite3BackupRestart(pPager->pBackup);
38461   sqlite3PcacheClear(pPager->pPCache);
38462 }
38463 
38464 /*
38465 ** Free all structures in the Pager.aSavepoint[] array and set both
38466 ** Pager.aSavepoint and Pager.nSavepoint to zero. Close the sub-journal
38467 ** if it is open and the pager is not in exclusive mode.
38468 */
38469 static void releaseAllSavepoints(Pager *pPager){
38470   int ii;               /* Iterator for looping through Pager.aSavepoint */
38471   for(ii=0; ii<pPager->nSavepoint; ii++){
38472     sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint);
38473   }
38474   if( !pPager->exclusiveMode || sqlite3IsMemJournal(pPager->sjfd) ){
38475     sqlite3OsClose(pPager->sjfd);
38476   }
38477   sqlite3_free(pPager->aSavepoint);
38478   pPager->aSavepoint = 0;
38479   pPager->nSavepoint = 0;
38480   pPager->nSubRec = 0;
38481 }
38482 
38483 /*
38484 ** Set the bit number pgno in the PagerSavepoint.pInSavepoint
38485 ** bitvecs of all open savepoints. Return SQLITE_OK if successful
38486 ** or SQLITE_NOMEM if a malloc failure occurs.
38487 */
38488 static int addToSavepointBitvecs(Pager *pPager, Pgno pgno){
38489   int ii;                   /* Loop counter */
38490   int rc = SQLITE_OK;       /* Result code */
38491 
38492   for(ii=0; ii<pPager->nSavepoint; ii++){
38493     PagerSavepoint *p = &pPager->aSavepoint[ii];
38494     if( pgno<=p->nOrig ){
38495       rc |= sqlite3BitvecSet(p->pInSavepoint, pgno);
38496       testcase( rc==SQLITE_NOMEM );
38497       assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
38498     }
38499   }
38500   return rc;
38501 }
38502 
38503 /*
38504 ** This function is a no-op if the pager is in exclusive mode and not
38505 ** in the ERROR state. Otherwise, it switches the pager to PAGER_OPEN
38506 ** state.
38507 **
38508 ** If the pager is not in exclusive-access mode, the database file is
38509 ** completely unlocked. If the file is unlocked and the file-system does
38510 ** not exhibit the UNDELETABLE_WHEN_OPEN property, the journal file is
38511 ** closed (if it is open).
38512 **
38513 ** If the pager is in ERROR state when this function is called, the
38514 ** contents of the pager cache are discarded before switching back to
38515 ** the OPEN state. Regardless of whether the pager is in exclusive-mode
38516 ** or not, any journal file left in the file-system will be treated
38517 ** as a hot-journal and rolled back the next time a read-transaction
38518 ** is opened (by this or by any other connection).
38519 */
38520 static void pager_unlock(Pager *pPager){
38521 
38522   assert( pPager->eState==PAGER_READER
38523        || pPager->eState==PAGER_OPEN
38524        || pPager->eState==PAGER_ERROR
38525   );
38526 
38527   sqlite3BitvecDestroy(pPager->pInJournal);
38528   pPager->pInJournal = 0;
38529   releaseAllSavepoints(pPager);
38530 
38531   if( pagerUseWal(pPager) ){
38532     assert( !isOpen(pPager->jfd) );
38533     sqlite3WalEndReadTransaction(pPager->pWal);
38534     pPager->eState = PAGER_OPEN;
38535   }else if( !pPager->exclusiveMode ){
38536     int rc;                       /* Error code returned by pagerUnlockDb() */
38537     int iDc = isOpen(pPager->fd)?sqlite3OsDeviceCharacteristics(pPager->fd):0;
38538 
38539     /* If the operating system support deletion of open files, then
38540     ** close the journal file when dropping the database lock.  Otherwise
38541     ** another connection with journal_mode=delete might delete the file
38542     ** out from under us.
38543     */
38544     assert( (PAGER_JOURNALMODE_MEMORY   & 5)!=1 );
38545     assert( (PAGER_JOURNALMODE_OFF      & 5)!=1 );
38546     assert( (PAGER_JOURNALMODE_WAL      & 5)!=1 );
38547     assert( (PAGER_JOURNALMODE_DELETE   & 5)!=1 );
38548     assert( (PAGER_JOURNALMODE_TRUNCATE & 5)==1 );
38549     assert( (PAGER_JOURNALMODE_PERSIST  & 5)==1 );
38550     if( 0==(iDc & SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN)
38551      || 1!=(pPager->journalMode & 5)
38552     ){
38553       sqlite3OsClose(pPager->jfd);
38554     }
38555 
38556     /* If the pager is in the ERROR state and the call to unlock the database
38557     ** file fails, set the current lock to UNKNOWN_LOCK. See the comment
38558     ** above the #define for UNKNOWN_LOCK for an explanation of why this
38559     ** is necessary.
38560     */
38561     rc = pagerUnlockDb(pPager, NO_LOCK);
38562     if( rc!=SQLITE_OK && pPager->eState==PAGER_ERROR ){
38563       pPager->eLock = UNKNOWN_LOCK;
38564     }
38565 
38566     /* The pager state may be changed from PAGER_ERROR to PAGER_OPEN here
38567     ** without clearing the error code. This is intentional - the error
38568     ** code is cleared and the cache reset in the block below.
38569     */
38570     assert( pPager->errCode || pPager->eState!=PAGER_ERROR );
38571     pPager->changeCountDone = 0;
38572     pPager->eState = PAGER_OPEN;
38573   }
38574 
38575   /* If Pager.errCode is set, the contents of the pager cache cannot be
38576   ** trusted. Now that there are no outstanding references to the pager,
38577   ** it can safely move back to PAGER_OPEN state. This happens in both
38578   ** normal and exclusive-locking mode.
38579   */
38580   if( pPager->errCode ){
38581     assert( !MEMDB );
38582     pager_reset(pPager);
38583     pPager->changeCountDone = pPager->tempFile;
38584     pPager->eState = PAGER_OPEN;
38585     pPager->errCode = SQLITE_OK;
38586   }
38587 
38588   pPager->journalOff = 0;
38589   pPager->journalHdr = 0;
38590   pPager->setMaster = 0;
38591 }
38592 
38593 /*
38594 ** This function is called whenever an IOERR or FULL error that requires
38595 ** the pager to transition into the ERROR state may ahve occurred.
38596 ** The first argument is a pointer to the pager structure, the second
38597 ** the error-code about to be returned by a pager API function. The
38598 ** value returned is a copy of the second argument to this function.
38599 **
38600 ** If the second argument is SQLITE_FULL, SQLITE_IOERR or one of the
38601 ** IOERR sub-codes, the pager enters the ERROR state and the error code
38602 ** is stored in Pager.errCode. While the pager remains in the ERROR state,
38603 ** all major API calls on the Pager will immediately return Pager.errCode.
38604 **
38605 ** The ERROR state indicates that the contents of the pager-cache
38606 ** cannot be trusted. This state can be cleared by completely discarding
38607 ** the contents of the pager-cache. If a transaction was active when
38608 ** the persistent error occurred, then the rollback journal may need
38609 ** to be replayed to restore the contents of the database file (as if
38610 ** it were a hot-journal).
38611 */
38612 static int pager_error(Pager *pPager, int rc){
38613   int rc2 = rc & 0xff;
38614   assert( rc==SQLITE_OK || !MEMDB );
38615   assert(
38616        pPager->errCode==SQLITE_FULL ||
38617        pPager->errCode==SQLITE_OK ||
38618        (pPager->errCode & 0xff)==SQLITE_IOERR
38619   );
38620   if( rc2==SQLITE_FULL || rc2==SQLITE_IOERR ){
38621     pPager->errCode = rc;
38622     pPager->eState = PAGER_ERROR;
38623   }
38624   return rc;
38625 }
38626 
38627 /*
38628 ** This routine ends a transaction. A transaction is usually ended by
38629 ** either a COMMIT or a ROLLBACK operation. This routine may be called
38630 ** after rollback of a hot-journal, or if an error occurs while opening
38631 ** the journal file or writing the very first journal-header of a
38632 ** database transaction.
38633 **
38634 ** This routine is never called in PAGER_ERROR state. If it is called
38635 ** in PAGER_NONE or PAGER_SHARED state and the lock held is less
38636 ** exclusive than a RESERVED lock, it is a no-op.
38637 **
38638 ** Otherwise, any active savepoints are released.
38639 **
38640 ** If the journal file is open, then it is "finalized". Once a journal
38641 ** file has been finalized it is not possible to use it to roll back a
38642 ** transaction. Nor will it be considered to be a hot-journal by this
38643 ** or any other database connection. Exactly how a journal is finalized
38644 ** depends on whether or not the pager is running in exclusive mode and
38645 ** the current journal-mode (Pager.journalMode value), as follows:
38646 **
38647 **   journalMode==MEMORY
38648 **     Journal file descriptor is simply closed. This destroys an
38649 **     in-memory journal.
38650 **
38651 **   journalMode==TRUNCATE
38652 **     Journal file is truncated to zero bytes in size.
38653 **
38654 **   journalMode==PERSIST
38655 **     The first 28 bytes of the journal file are zeroed. This invalidates
38656 **     the first journal header in the file, and hence the entire journal
38657 **     file. An invalid journal file cannot be rolled back.
38658 **
38659 **   journalMode==DELETE
38660 **     The journal file is closed and deleted using sqlite3OsDelete().
38661 **
38662 **     If the pager is running in exclusive mode, this method of finalizing
38663 **     the journal file is never used. Instead, if the journalMode is
38664 **     DELETE and the pager is in exclusive mode, the method described under
38665 **     journalMode==PERSIST is used instead.
38666 **
38667 ** After the journal is finalized, the pager moves to PAGER_READER state.
38668 ** If running in non-exclusive rollback mode, the lock on the file is
38669 ** downgraded to a SHARED_LOCK.
38670 **
38671 ** SQLITE_OK is returned if no error occurs. If an error occurs during
38672 ** any of the IO operations to finalize the journal file or unlock the
38673 ** database then the IO error code is returned to the user. If the
38674 ** operation to finalize the journal file fails, then the code still
38675 ** tries to unlock the database file if not in exclusive mode. If the
38676 ** unlock operation fails as well, then the first error code related
38677 ** to the first error encountered (the journal finalization one) is
38678 ** returned.
38679 */
38680 static int pager_end_transaction(Pager *pPager, int hasMaster){
38681   int rc = SQLITE_OK;      /* Error code from journal finalization operation */
38682   int rc2 = SQLITE_OK;     /* Error code from db file unlock operation */
38683 
38684   /* Do nothing if the pager does not have an open write transaction
38685   ** or at least a RESERVED lock. This function may be called when there
38686   ** is no write-transaction active but a RESERVED or greater lock is
38687   ** held under two circumstances:
38688   **
38689   **   1. After a successful hot-journal rollback, it is called with
38690   **      eState==PAGER_NONE and eLock==EXCLUSIVE_LOCK.
38691   **
38692   **   2. If a connection with locking_mode=exclusive holding an EXCLUSIVE
38693   **      lock switches back to locking_mode=normal and then executes a
38694   **      read-transaction, this function is called with eState==PAGER_READER
38695   **      and eLock==EXCLUSIVE_LOCK when the read-transaction is closed.
38696   */
38697   assert( assert_pager_state(pPager) );
38698   assert( pPager->eState!=PAGER_ERROR );
38699   if( pPager->eState<PAGER_WRITER_LOCKED && pPager->eLock<RESERVED_LOCK ){
38700     return SQLITE_OK;
38701   }
38702 
38703   releaseAllSavepoints(pPager);
38704   assert( isOpen(pPager->jfd) || pPager->pInJournal==0 );
38705   if( isOpen(pPager->jfd) ){
38706     assert( !pagerUseWal(pPager) );
38707 
38708     /* Finalize the journal file. */
38709     if( sqlite3IsMemJournal(pPager->jfd) ){
38710       assert( pPager->journalMode==PAGER_JOURNALMODE_MEMORY );
38711       sqlite3OsClose(pPager->jfd);
38712     }else if( pPager->journalMode==PAGER_JOURNALMODE_TRUNCATE ){
38713       if( pPager->journalOff==0 ){
38714         rc = SQLITE_OK;
38715       }else{
38716         rc = sqlite3OsTruncate(pPager->jfd, 0);
38717       }
38718       pPager->journalOff = 0;
38719     }else if( pPager->journalMode==PAGER_JOURNALMODE_PERSIST
38720       || (pPager->exclusiveMode && pPager->journalMode!=PAGER_JOURNALMODE_WAL)
38721     ){
38722       rc = zeroJournalHdr(pPager, hasMaster);
38723       pPager->journalOff = 0;
38724     }else{
38725       /* This branch may be executed with Pager.journalMode==MEMORY if
38726       ** a hot-journal was just rolled back. In this case the journal
38727       ** file should be closed and deleted. If this connection writes to
38728       ** the database file, it will do so using an in-memory journal.
38729       */
38730       assert( pPager->journalMode==PAGER_JOURNALMODE_DELETE
38731            || pPager->journalMode==PAGER_JOURNALMODE_MEMORY
38732            || pPager->journalMode==PAGER_JOURNALMODE_WAL
38733       );
38734       sqlite3OsClose(pPager->jfd);
38735       if( !pPager->tempFile ){
38736         rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
38737       }
38738     }
38739   }
38740 
38741 #ifdef SQLITE_CHECK_PAGES
38742   sqlite3PcacheIterateDirty(pPager->pPCache, pager_set_pagehash);
38743   if( pPager->dbSize==0 && sqlite3PcacheRefCount(pPager->pPCache)>0 ){
38744     PgHdr *p = pager_lookup(pPager, 1);
38745     if( p ){
38746       p->pageHash = 0;
38747       sqlite3PagerUnref(p);
38748     }
38749   }
38750 #endif
38751 
38752   sqlite3BitvecDestroy(pPager->pInJournal);
38753   pPager->pInJournal = 0;
38754   pPager->nRec = 0;
38755   sqlite3PcacheCleanAll(pPager->pPCache);
38756   sqlite3PcacheTruncate(pPager->pPCache, pPager->dbSize);
38757 
38758   if( pagerUseWal(pPager) ){
38759     /* Drop the WAL write-lock, if any. Also, if the connection was in
38760     ** locking_mode=exclusive mode but is no longer, drop the EXCLUSIVE
38761     ** lock held on the database file.
38762     */
38763     rc2 = sqlite3WalEndWriteTransaction(pPager->pWal);
38764     assert( rc2==SQLITE_OK );
38765   }
38766   if( !pPager->exclusiveMode
38767    && (!pagerUseWal(pPager) || sqlite3WalExclusiveMode(pPager->pWal, 0))
38768   ){
38769     rc2 = pagerUnlockDb(pPager, SHARED_LOCK);
38770     pPager->changeCountDone = 0;
38771   }
38772   pPager->eState = PAGER_READER;
38773   pPager->setMaster = 0;
38774 
38775   return (rc==SQLITE_OK?rc2:rc);
38776 }
38777 
38778 /*
38779 ** Execute a rollback if a transaction is active and unlock the
38780 ** database file.
38781 **
38782 ** If the pager has already entered the ERROR state, do not attempt
38783 ** the rollback at this time. Instead, pager_unlock() is called. The
38784 ** call to pager_unlock() will discard all in-memory pages, unlock
38785 ** the database file and move the pager back to OPEN state. If this
38786 ** means that there is a hot-journal left in the file-system, the next
38787 ** connection to obtain a shared lock on the pager (which may be this one)
38788 ** will roll it back.
38789 **
38790 ** If the pager has not already entered the ERROR state, but an IO or
38791 ** malloc error occurs during a rollback, then this will itself cause
38792 ** the pager to enter the ERROR state. Which will be cleared by the
38793 ** call to pager_unlock(), as described above.
38794 */
38795 static void pagerUnlockAndRollback(Pager *pPager){
38796   if( pPager->eState!=PAGER_ERROR && pPager->eState!=PAGER_OPEN ){
38797     assert( assert_pager_state(pPager) );
38798     if( pPager->eState>=PAGER_WRITER_LOCKED ){
38799       sqlite3BeginBenignMalloc();
38800       sqlite3PagerRollback(pPager);
38801       sqlite3EndBenignMalloc();
38802     }else if( !pPager->exclusiveMode ){
38803       assert( pPager->eState==PAGER_READER );
38804       pager_end_transaction(pPager, 0);
38805     }
38806   }
38807   pager_unlock(pPager);
38808 }
38809 
38810 /*
38811 ** Parameter aData must point to a buffer of pPager->pageSize bytes
38812 ** of data. Compute and return a checksum based ont the contents of the
38813 ** page of data and the current value of pPager->cksumInit.
38814 **
38815 ** This is not a real checksum. It is really just the sum of the
38816 ** random initial value (pPager->cksumInit) and every 200th byte
38817 ** of the page data, starting with byte offset (pPager->pageSize%200).
38818 ** Each byte is interpreted as an 8-bit unsigned integer.
38819 **
38820 ** Changing the formula used to compute this checksum results in an
38821 ** incompatible journal file format.
38822 **
38823 ** If journal corruption occurs due to a power failure, the most likely
38824 ** scenario is that one end or the other of the record will be changed.
38825 ** It is much less likely that the two ends of the journal record will be
38826 ** correct and the middle be corrupt.  Thus, this "checksum" scheme,
38827 ** though fast and simple, catches the mostly likely kind of corruption.
38828 */
38829 static u32 pager_cksum(Pager *pPager, const u8 *aData){
38830   u32 cksum = pPager->cksumInit;         /* Checksum value to return */
38831   int i = pPager->pageSize-200;          /* Loop counter */
38832   while( i>0 ){
38833     cksum += aData[i];
38834     i -= 200;
38835   }
38836   return cksum;
38837 }
38838 
38839 /*
38840 ** Report the current page size and number of reserved bytes back
38841 ** to the codec.
38842 */
38843 #ifdef SQLITE_HAS_CODEC
38844 static void pagerReportSize(Pager *pPager){
38845   if( pPager->xCodecSizeChng ){
38846     pPager->xCodecSizeChng(pPager->pCodec, pPager->pageSize,
38847                            (int)pPager->nReserve);
38848   }
38849 }
38850 #else
38851 # define pagerReportSize(X)     /* No-op if we do not support a codec */
38852 #endif
38853 
38854 /*
38855 ** Read a single page from either the journal file (if isMainJrnl==1) or
38856 ** from the sub-journal (if isMainJrnl==0) and playback that page.
38857 ** The page begins at offset *pOffset into the file. The *pOffset
38858 ** value is increased to the start of the next page in the journal.
38859 **
38860 ** The main rollback journal uses checksums - the statement journal does
38861 ** not.
38862 **
38863 ** If the page number of the page record read from the (sub-)journal file
38864 ** is greater than the current value of Pager.dbSize, then playback is
38865 ** skipped and SQLITE_OK is returned.
38866 **
38867 ** If pDone is not NULL, then it is a record of pages that have already
38868 ** been played back.  If the page at *pOffset has already been played back
38869 ** (if the corresponding pDone bit is set) then skip the playback.
38870 ** Make sure the pDone bit corresponding to the *pOffset page is set
38871 ** prior to returning.
38872 **
38873 ** If the page record is successfully read from the (sub-)journal file
38874 ** and played back, then SQLITE_OK is returned. If an IO error occurs
38875 ** while reading the record from the (sub-)journal file or while writing
38876 ** to the database file, then the IO error code is returned. If data
38877 ** is successfully read from the (sub-)journal file but appears to be
38878 ** corrupted, SQLITE_DONE is returned. Data is considered corrupted in
38879 ** two circumstances:
38880 **
38881 **   * If the record page-number is illegal (0 or PAGER_MJ_PGNO), or
38882 **   * If the record is being rolled back from the main journal file
38883 **     and the checksum field does not match the record content.
38884 **
38885 ** Neither of these two scenarios are possible during a savepoint rollback.
38886 **
38887 ** If this is a savepoint rollback, then memory may have to be dynamically
38888 ** allocated by this function. If this is the case and an allocation fails,
38889 ** SQLITE_NOMEM is returned.
38890 */
38891 static int pager_playback_one_page(
38892   Pager *pPager,                /* The pager being played back */
38893   i64 *pOffset,                 /* Offset of record to playback */
38894   Bitvec *pDone,                /* Bitvec of pages already played back */
38895   int isMainJrnl,               /* 1 -> main journal. 0 -> sub-journal. */
38896   int isSavepnt                 /* True for a savepoint rollback */
38897 ){
38898   int rc;
38899   PgHdr *pPg;                   /* An existing page in the cache */
38900   Pgno pgno;                    /* The page number of a page in journal */
38901   u32 cksum;                    /* Checksum used for sanity checking */
38902   char *aData;                  /* Temporary storage for the page */
38903   sqlite3_file *jfd;            /* The file descriptor for the journal file */
38904   int isSynced;                 /* True if journal page is synced */
38905 
38906   assert( (isMainJrnl&~1)==0 );      /* isMainJrnl is 0 or 1 */
38907   assert( (isSavepnt&~1)==0 );       /* isSavepnt is 0 or 1 */
38908   assert( isMainJrnl || pDone );     /* pDone always used on sub-journals */
38909   assert( isSavepnt || pDone==0 );   /* pDone never used on non-savepoint */
38910 
38911   aData = pPager->pTmpSpace;
38912   assert( aData );         /* Temp storage must have already been allocated */
38913   assert( pagerUseWal(pPager)==0 || (!isMainJrnl && isSavepnt) );
38914 
38915   /* Either the state is greater than PAGER_WRITER_CACHEMOD (a transaction
38916   ** or savepoint rollback done at the request of the caller) or this is
38917   ** a hot-journal rollback. If it is a hot-journal rollback, the pager
38918   ** is in state OPEN and holds an EXCLUSIVE lock. Hot-journal rollback
38919   ** only reads from the main journal, not the sub-journal.
38920   */
38921   assert( pPager->eState>=PAGER_WRITER_CACHEMOD
38922        || (pPager->eState==PAGER_OPEN && pPager->eLock==EXCLUSIVE_LOCK)
38923   );
38924   assert( pPager->eState>=PAGER_WRITER_CACHEMOD || isMainJrnl );
38925 
38926   /* Read the page number and page data from the journal or sub-journal
38927   ** file. Return an error code to the caller if an IO error occurs.
38928   */
38929   jfd = isMainJrnl ? pPager->jfd : pPager->sjfd;
38930   rc = read32bits(jfd, *pOffset, &pgno);
38931   if( rc!=SQLITE_OK ) return rc;
38932   rc = sqlite3OsRead(jfd, (u8*)aData, pPager->pageSize, (*pOffset)+4);
38933   if( rc!=SQLITE_OK ) return rc;
38934   *pOffset += pPager->pageSize + 4 + isMainJrnl*4;
38935 
38936   /* Sanity checking on the page.  This is more important that I originally
38937   ** thought.  If a power failure occurs while the journal is being written,
38938   ** it could cause invalid data to be written into the journal.  We need to
38939   ** detect this invalid data (with high probability) and ignore it.
38940   */
38941   if( pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){
38942     assert( !isSavepnt );
38943     return SQLITE_DONE;
38944   }
38945   if( pgno>(Pgno)pPager->dbSize || sqlite3BitvecTest(pDone, pgno) ){
38946     return SQLITE_OK;
38947   }
38948   if( isMainJrnl ){
38949     rc = read32bits(jfd, (*pOffset)-4, &cksum);
38950     if( rc ) return rc;
38951     if( !isSavepnt && pager_cksum(pPager, (u8*)aData)!=cksum ){
38952       return SQLITE_DONE;
38953     }
38954   }
38955 
38956   /* If this page has already been played by before during the current
38957   ** rollback, then don't bother to play it back again.
38958   */
38959   if( pDone && (rc = sqlite3BitvecSet(pDone, pgno))!=SQLITE_OK ){
38960     return rc;
38961   }
38962 
38963   /* When playing back page 1, restore the nReserve setting
38964   */
38965   if( pgno==1 && pPager->nReserve!=((u8*)aData)[20] ){
38966     pPager->nReserve = ((u8*)aData)[20];
38967     pagerReportSize(pPager);
38968   }
38969 
38970   /* If the pager is in CACHEMOD state, then there must be a copy of this
38971   ** page in the pager cache. In this case just update the pager cache,
38972   ** not the database file. The page is left marked dirty in this case.
38973   **
38974   ** An exception to the above rule: If the database is in no-sync mode
38975   ** and a page is moved during an incremental vacuum then the page may
38976   ** not be in the pager cache. Later: if a malloc() or IO error occurs
38977   ** during a Movepage() call, then the page may not be in the cache
38978   ** either. So the condition described in the above paragraph is not
38979   ** assert()able.
38980   **
38981   ** If in WRITER_DBMOD, WRITER_FINISHED or OPEN state, then we update the
38982   ** pager cache if it exists and the main file. The page is then marked
38983   ** not dirty. Since this code is only executed in PAGER_OPEN state for
38984   ** a hot-journal rollback, it is guaranteed that the page-cache is empty
38985   ** if the pager is in OPEN state.
38986   **
38987   ** Ticket #1171:  The statement journal might contain page content that is
38988   ** different from the page content at the start of the transaction.
38989   ** This occurs when a page is changed prior to the start of a statement
38990   ** then changed again within the statement.  When rolling back such a
38991   ** statement we must not write to the original database unless we know
38992   ** for certain that original page contents are synced into the main rollback
38993   ** journal.  Otherwise, a power loss might leave modified data in the
38994   ** database file without an entry in the rollback journal that can
38995   ** restore the database to its original form.  Two conditions must be
38996   ** met before writing to the database files. (1) the database must be
38997   ** locked.  (2) we know that the original page content is fully synced
38998   ** in the main journal either because the page is not in cache or else
38999   ** the page is marked as needSync==0.
39000   **
39001   ** 2008-04-14:  When attempting to vacuum a corrupt database file, it
39002   ** is possible to fail a statement on a database that does not yet exist.
39003   ** Do not attempt to write if database file has never been opened.
39004   */
39005   if( pagerUseWal(pPager) ){
39006     pPg = 0;
39007   }else{
39008     pPg = pager_lookup(pPager, pgno);
39009   }
39010   assert( pPg || !MEMDB );
39011   assert( pPager->eState!=PAGER_OPEN || pPg==0 );
39012   PAGERTRACE(("PLAYBACK %d page %d hash(%08x) %s\n",
39013            PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, (u8*)aData),
39014            (isMainJrnl?"main-journal":"sub-journal")
39015   ));
39016   if( isMainJrnl ){
39017     isSynced = pPager->noSync || (*pOffset <= pPager->journalHdr);
39018   }else{
39019     isSynced = (pPg==0 || 0==(pPg->flags & PGHDR_NEED_SYNC));
39020   }
39021   if( isOpen(pPager->fd)
39022    && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
39023    && isSynced
39024   ){
39025     i64 ofst = (pgno-1)*(i64)pPager->pageSize;
39026     testcase( !isSavepnt && pPg!=0 && (pPg->flags&PGHDR_NEED_SYNC)!=0 );
39027     assert( !pagerUseWal(pPager) );
39028     rc = sqlite3OsWrite(pPager->fd, (u8*)aData, pPager->pageSize, ofst);
39029     if( pgno>pPager->dbFileSize ){
39030       pPager->dbFileSize = pgno;
39031     }
39032     if( pPager->pBackup ){
39033       CODEC1(pPager, aData, pgno, 3, rc=SQLITE_NOMEM);
39034       sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)aData);
39035       CODEC2(pPager, aData, pgno, 7, rc=SQLITE_NOMEM, aData);
39036     }
39037   }else if( !isMainJrnl && pPg==0 ){
39038     /* If this is a rollback of a savepoint and data was not written to
39039     ** the database and the page is not in-memory, there is a potential
39040     ** problem. When the page is next fetched by the b-tree layer, it
39041     ** will be read from the database file, which may or may not be
39042     ** current.
39043     **
39044     ** There are a couple of different ways this can happen. All are quite
39045     ** obscure. When running in synchronous mode, this can only happen
39046     ** if the page is on the free-list at the start of the transaction, then
39047     ** populated, then moved using sqlite3PagerMovepage().
39048     **
39049     ** The solution is to add an in-memory page to the cache containing
39050     ** the data just read from the sub-journal. Mark the page as dirty
39051     ** and if the pager requires a journal-sync, then mark the page as
39052     ** requiring a journal-sync before it is written.
39053     */
39054     assert( isSavepnt );
39055     assert( pPager->doNotSpill==0 );
39056     pPager->doNotSpill++;
39057     rc = sqlite3PagerAcquire(pPager, pgno, &pPg, 1);
39058     assert( pPager->doNotSpill==1 );
39059     pPager->doNotSpill--;
39060     if( rc!=SQLITE_OK ) return rc;
39061     pPg->flags &= ~PGHDR_NEED_READ;
39062     sqlite3PcacheMakeDirty(pPg);
39063   }
39064   if( pPg ){
39065     /* No page should ever be explicitly rolled back that is in use, except
39066     ** for page 1 which is held in use in order to keep the lock on the
39067     ** database active. However such a page may be rolled back as a result
39068     ** of an internal error resulting in an automatic call to
39069     ** sqlite3PagerRollback().
39070     */
39071     void *pData;
39072     pData = pPg->pData;
39073     memcpy(pData, (u8*)aData, pPager->pageSize);
39074     pPager->xReiniter(pPg);
39075     if( isMainJrnl && (!isSavepnt || *pOffset<=pPager->journalHdr) ){
39076       /* If the contents of this page were just restored from the main
39077       ** journal file, then its content must be as they were when the
39078       ** transaction was first opened. In this case we can mark the page
39079       ** as clean, since there will be no need to write it out to the
39080       ** database.
39081       **
39082       ** There is one exception to this rule. If the page is being rolled
39083       ** back as part of a savepoint (or statement) rollback from an
39084       ** unsynced portion of the main journal file, then it is not safe
39085       ** to mark the page as clean. This is because marking the page as
39086       ** clean will clear the PGHDR_NEED_SYNC flag. Since the page is
39087       ** already in the journal file (recorded in Pager.pInJournal) and
39088       ** the PGHDR_NEED_SYNC flag is cleared, if the page is written to
39089       ** again within this transaction, it will be marked as dirty but
39090       ** the PGHDR_NEED_SYNC flag will not be set. It could then potentially
39091       ** be written out into the database file before its journal file
39092       ** segment is synced. If a crash occurs during or following this,
39093       ** database corruption may ensue.
39094       */
39095       assert( !pagerUseWal(pPager) );
39096       sqlite3PcacheMakeClean(pPg);
39097     }
39098     pager_set_pagehash(pPg);
39099 
39100     /* If this was page 1, then restore the value of Pager.dbFileVers.
39101     ** Do this before any decoding. */
39102     if( pgno==1 ){
39103       memcpy(&pPager->dbFileVers, &((u8*)pData)[24],sizeof(pPager->dbFileVers));
39104     }
39105 
39106     /* Decode the page just read from disk */
39107     CODEC1(pPager, pData, pPg->pgno, 3, rc=SQLITE_NOMEM);
39108     sqlite3PcacheRelease(pPg);
39109   }
39110   return rc;
39111 }
39112 
39113 /*
39114 ** Parameter zMaster is the name of a master journal file. A single journal
39115 ** file that referred to the master journal file has just been rolled back.
39116 ** This routine checks if it is possible to delete the master journal file,
39117 ** and does so if it is.
39118 **
39119 ** Argument zMaster may point to Pager.pTmpSpace. So that buffer is not
39120 ** available for use within this function.
39121 **
39122 ** When a master journal file is created, it is populated with the names
39123 ** of all of its child journals, one after another, formatted as utf-8
39124 ** encoded text. The end of each child journal file is marked with a
39125 ** nul-terminator byte (0x00). i.e. the entire contents of a master journal
39126 ** file for a transaction involving two databases might be:
39127 **
39128 **   "/home/bill/a.db-journal\x00/home/bill/b.db-journal\x00"
39129 **
39130 ** A master journal file may only be deleted once all of its child
39131 ** journals have been rolled back.
39132 **
39133 ** This function reads the contents of the master-journal file into
39134 ** memory and loops through each of the child journal names. For
39135 ** each child journal, it checks if:
39136 **
39137 **   * if the child journal exists, and if so
39138 **   * if the child journal contains a reference to master journal
39139 **     file zMaster
39140 **
39141 ** If a child journal can be found that matches both of the criteria
39142 ** above, this function returns without doing anything. Otherwise, if
39143 ** no such child journal can be found, file zMaster is deleted from
39144 ** the file-system using sqlite3OsDelete().
39145 **
39146 ** If an IO error within this function, an error code is returned. This
39147 ** function allocates memory by calling sqlite3Malloc(). If an allocation
39148 ** fails, SQLITE_NOMEM is returned. Otherwise, if no IO or malloc errors
39149 ** occur, SQLITE_OK is returned.
39150 **
39151 ** TODO: This function allocates a single block of memory to load
39152 ** the entire contents of the master journal file. This could be
39153 ** a couple of kilobytes or so - potentially larger than the page
39154 ** size.
39155 */
39156 static int pager_delmaster(Pager *pPager, const char *zMaster){
39157   sqlite3_vfs *pVfs = pPager->pVfs;
39158   int rc;                   /* Return code */
39159   sqlite3_file *pMaster;    /* Malloc'd master-journal file descriptor */
39160   sqlite3_file *pJournal;   /* Malloc'd child-journal file descriptor */
39161   char *zMasterJournal = 0; /* Contents of master journal file */
39162   i64 nMasterJournal;       /* Size of master journal file */
39163   char *zJournal;           /* Pointer to one journal within MJ file */
39164   char *zMasterPtr;         /* Space to hold MJ filename from a journal file */
39165   int nMasterPtr;           /* Amount of space allocated to zMasterPtr[] */
39166 
39167   /* Allocate space for both the pJournal and pMaster file descriptors.
39168   ** If successful, open the master journal file for reading.
39169   */
39170   pMaster = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile * 2);
39171   pJournal = (sqlite3_file *)(((u8 *)pMaster) + pVfs->szOsFile);
39172   if( !pMaster ){
39173     rc = SQLITE_NOMEM;
39174   }else{
39175     const int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MASTER_JOURNAL);
39176     rc = sqlite3OsOpen(pVfs, zMaster, pMaster, flags, 0);
39177   }
39178   if( rc!=SQLITE_OK ) goto delmaster_out;
39179 
39180   /* Load the entire master journal file into space obtained from
39181   ** sqlite3_malloc() and pointed to by zMasterJournal.   Also obtain
39182   ** sufficient space (in zMasterPtr) to hold the names of master
39183   ** journal files extracted from regular rollback-journals.
39184   */
39185   rc = sqlite3OsFileSize(pMaster, &nMasterJournal);
39186   if( rc!=SQLITE_OK ) goto delmaster_out;
39187   nMasterPtr = pVfs->mxPathname+1;
39188   zMasterJournal = sqlite3Malloc((int)nMasterJournal + nMasterPtr + 1);
39189   if( !zMasterJournal ){
39190     rc = SQLITE_NOMEM;
39191     goto delmaster_out;
39192   }
39193   zMasterPtr = &zMasterJournal[nMasterJournal+1];
39194   rc = sqlite3OsRead(pMaster, zMasterJournal, (int)nMasterJournal, 0);
39195   if( rc!=SQLITE_OK ) goto delmaster_out;
39196   zMasterJournal[nMasterJournal] = 0;
39197 
39198   zJournal = zMasterJournal;
39199   while( (zJournal-zMasterJournal)<nMasterJournal ){
39200     int exists;
39201     rc = sqlite3OsAccess(pVfs, zJournal, SQLITE_ACCESS_EXISTS, &exists);
39202     if( rc!=SQLITE_OK ){
39203       goto delmaster_out;
39204     }
39205     if( exists ){
39206       /* One of the journals pointed to by the master journal exists.
39207       ** Open it and check if it points at the master journal. If
39208       ** so, return without deleting the master journal file.
39209       */
39210       int c;
39211       int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MAIN_JOURNAL);
39212       rc = sqlite3OsOpen(pVfs, zJournal, pJournal, flags, 0);
39213       if( rc!=SQLITE_OK ){
39214         goto delmaster_out;
39215       }
39216 
39217       rc = readMasterJournal(pJournal, zMasterPtr, nMasterPtr);
39218       sqlite3OsClose(pJournal);
39219       if( rc!=SQLITE_OK ){
39220         goto delmaster_out;
39221       }
39222 
39223       c = zMasterPtr[0]!=0 && strcmp(zMasterPtr, zMaster)==0;
39224       if( c ){
39225         /* We have a match. Do not delete the master journal file. */
39226         goto delmaster_out;
39227       }
39228     }
39229     zJournal += (sqlite3Strlen30(zJournal)+1);
39230   }
39231 
39232   sqlite3OsClose(pMaster);
39233   rc = sqlite3OsDelete(pVfs, zMaster, 0);
39234 
39235 delmaster_out:
39236   sqlite3_free(zMasterJournal);
39237   if( pMaster ){
39238     sqlite3OsClose(pMaster);
39239     assert( !isOpen(pJournal) );
39240     sqlite3_free(pMaster);
39241   }
39242   return rc;
39243 }
39244 
39245 
39246 /*
39247 ** This function is used to change the actual size of the database
39248 ** file in the file-system. This only happens when committing a transaction,
39249 ** or rolling back a transaction (including rolling back a hot-journal).
39250 **
39251 ** If the main database file is not open, or the pager is not in either
39252 ** DBMOD or OPEN state, this function is a no-op. Otherwise, the size
39253 ** of the file is changed to nPage pages (nPage*pPager->pageSize bytes).
39254 ** If the file on disk is currently larger than nPage pages, then use the VFS
39255 ** xTruncate() method to truncate it.
39256 **
39257 ** Or, it might might be the case that the file on disk is smaller than
39258 ** nPage pages. Some operating system implementations can get confused if
39259 ** you try to truncate a file to some size that is larger than it
39260 ** currently is, so detect this case and write a single zero byte to
39261 ** the end of the new file instead.
39262 **
39263 ** If successful, return SQLITE_OK. If an IO error occurs while modifying
39264 ** the database file, return the error code to the caller.
39265 */
39266 static int pager_truncate(Pager *pPager, Pgno nPage){
39267   int rc = SQLITE_OK;
39268   assert( pPager->eState!=PAGER_ERROR );
39269   assert( pPager->eState!=PAGER_READER );
39270 
39271   if( isOpen(pPager->fd)
39272    && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
39273   ){
39274     i64 currentSize, newSize;
39275     int szPage = pPager->pageSize;
39276     assert( pPager->eLock==EXCLUSIVE_LOCK );
39277     /* TODO: Is it safe to use Pager.dbFileSize here? */
39278     rc = sqlite3OsFileSize(pPager->fd, &currentSize);
39279     newSize = szPage*(i64)nPage;
39280     if( rc==SQLITE_OK && currentSize!=newSize ){
39281       if( currentSize>newSize ){
39282         rc = sqlite3OsTruncate(pPager->fd, newSize);
39283       }else{
39284         char *pTmp = pPager->pTmpSpace;
39285         memset(pTmp, 0, szPage);
39286         testcase( (newSize-szPage) <  currentSize );
39287         testcase( (newSize-szPage) == currentSize );
39288         testcase( (newSize-szPage) >  currentSize );
39289         rc = sqlite3OsWrite(pPager->fd, pTmp, szPage, newSize-szPage);
39290       }
39291       if( rc==SQLITE_OK ){
39292         pPager->dbFileSize = nPage;
39293       }
39294     }
39295   }
39296   return rc;
39297 }
39298 
39299 /*
39300 ** Set the value of the Pager.sectorSize variable for the given
39301 ** pager based on the value returned by the xSectorSize method
39302 ** of the open database file. The sector size will be used used
39303 ** to determine the size and alignment of journal header and
39304 ** master journal pointers within created journal files.
39305 **
39306 ** For temporary files the effective sector size is always 512 bytes.
39307 **
39308 ** Otherwise, for non-temporary files, the effective sector size is
39309 ** the value returned by the xSectorSize() method rounded up to 32 if
39310 ** it is less than 32, or rounded down to MAX_SECTOR_SIZE if it
39311 ** is greater than MAX_SECTOR_SIZE.
39312 */
39313 static void setSectorSize(Pager *pPager){
39314   assert( isOpen(pPager->fd) || pPager->tempFile );
39315 
39316   if( !pPager->tempFile ){
39317     /* Sector size doesn't matter for temporary files. Also, the file
39318     ** may not have been opened yet, in which case the OsSectorSize()
39319     ** call will segfault.
39320     */
39321     pPager->sectorSize = sqlite3OsSectorSize(pPager->fd);
39322   }
39323   if( pPager->sectorSize<32 ){
39324     pPager->sectorSize = 512;
39325   }
39326   if( pPager->sectorSize>MAX_SECTOR_SIZE ){
39327     assert( MAX_SECTOR_SIZE>=512 );
39328     pPager->sectorSize = MAX_SECTOR_SIZE;
39329   }
39330 }
39331 
39332 /*
39333 ** Playback the journal and thus restore the database file to
39334 ** the state it was in before we started making changes.
39335 **
39336 ** The journal file format is as follows:
39337 **
39338 **  (1)  8 byte prefix.  A copy of aJournalMagic[].
39339 **  (2)  4 byte big-endian integer which is the number of valid page records
39340 **       in the journal.  If this value is 0xffffffff, then compute the
39341 **       number of page records from the journal size.
39342 **  (3)  4 byte big-endian integer which is the initial value for the
39343 **       sanity checksum.
39344 **  (4)  4 byte integer which is the number of pages to truncate the
39345 **       database to during a rollback.
39346 **  (5)  4 byte big-endian integer which is the sector size.  The header
39347 **       is this many bytes in size.
39348 **  (6)  4 byte big-endian integer which is the page size.
39349 **  (7)  zero padding out to the next sector size.
39350 **  (8)  Zero or more pages instances, each as follows:
39351 **        +  4 byte page number.
39352 **        +  pPager->pageSize bytes of data.
39353 **        +  4 byte checksum
39354 **
39355 ** When we speak of the journal header, we mean the first 7 items above.
39356 ** Each entry in the journal is an instance of the 8th item.
39357 **
39358 ** Call the value from the second bullet "nRec".  nRec is the number of
39359 ** valid page entries in the journal.  In most cases, you can compute the
39360 ** value of nRec from the size of the journal file.  But if a power
39361 ** failure occurred while the journal was being written, it could be the
39362 ** case that the size of the journal file had already been increased but
39363 ** the extra entries had not yet made it safely to disk.  In such a case,
39364 ** the value of nRec computed from the file size would be too large.  For
39365 ** that reason, we always use the nRec value in the header.
39366 **
39367 ** If the nRec value is 0xffffffff it means that nRec should be computed
39368 ** from the file size.  This value is used when the user selects the
39369 ** no-sync option for the journal.  A power failure could lead to corruption
39370 ** in this case.  But for things like temporary table (which will be
39371 ** deleted when the power is restored) we don't care.
39372 **
39373 ** If the file opened as the journal file is not a well-formed
39374 ** journal file then all pages up to the first corrupted page are rolled
39375 ** back (or no pages if the journal header is corrupted). The journal file
39376 ** is then deleted and SQLITE_OK returned, just as if no corruption had
39377 ** been encountered.
39378 **
39379 ** If an I/O or malloc() error occurs, the journal-file is not deleted
39380 ** and an error code is returned.
39381 **
39382 ** The isHot parameter indicates that we are trying to rollback a journal
39383 ** that might be a hot journal.  Or, it could be that the journal is
39384 ** preserved because of JOURNALMODE_PERSIST or JOURNALMODE_TRUNCATE.
39385 ** If the journal really is hot, reset the pager cache prior rolling
39386 ** back any content.  If the journal is merely persistent, no reset is
39387 ** needed.
39388 */
39389 static int pager_playback(Pager *pPager, int isHot){
39390   sqlite3_vfs *pVfs = pPager->pVfs;
39391   i64 szJ;                 /* Size of the journal file in bytes */
39392   u32 nRec;                /* Number of Records in the journal */
39393   u32 u;                   /* Unsigned loop counter */
39394   Pgno mxPg = 0;           /* Size of the original file in pages */
39395   int rc;                  /* Result code of a subroutine */
39396   int res = 1;             /* Value returned by sqlite3OsAccess() */
39397   char *zMaster = 0;       /* Name of master journal file if any */
39398   int needPagerReset;      /* True to reset page prior to first page rollback */
39399 
39400   /* Figure out how many records are in the journal.  Abort early if
39401   ** the journal is empty.
39402   */
39403   assert( isOpen(pPager->jfd) );
39404   rc = sqlite3OsFileSize(pPager->jfd, &szJ);
39405   if( rc!=SQLITE_OK ){
39406     goto end_playback;
39407   }
39408 
39409   /* Read the master journal name from the journal, if it is present.
39410   ** If a master journal file name is specified, but the file is not
39411   ** present on disk, then the journal is not hot and does not need to be
39412   ** played back.
39413   **
39414   ** TODO: Technically the following is an error because it assumes that
39415   ** buffer Pager.pTmpSpace is (mxPathname+1) bytes or larger. i.e. that
39416   ** (pPager->pageSize >= pPager->pVfs->mxPathname+1). Using os_unix.c,
39417   **  mxPathname is 512, which is the same as the minimum allowable value
39418   ** for pageSize.
39419   */
39420   zMaster = pPager->pTmpSpace;
39421   rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
39422   if( rc==SQLITE_OK && zMaster[0] ){
39423     rc = sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS, &res);
39424   }
39425   zMaster = 0;
39426   if( rc!=SQLITE_OK || !res ){
39427     goto end_playback;
39428   }
39429   pPager->journalOff = 0;
39430   needPagerReset = isHot;
39431 
39432   /* This loop terminates either when a readJournalHdr() or
39433   ** pager_playback_one_page() call returns SQLITE_DONE or an IO error
39434   ** occurs.
39435   */
39436   while( 1 ){
39437     /* Read the next journal header from the journal file.  If there are
39438     ** not enough bytes left in the journal file for a complete header, or
39439     ** it is corrupted, then a process must have failed while writing it.
39440     ** This indicates nothing more needs to be rolled back.
39441     */
39442     rc = readJournalHdr(pPager, isHot, szJ, &nRec, &mxPg);
39443     if( rc!=SQLITE_OK ){
39444       if( rc==SQLITE_DONE ){
39445         rc = SQLITE_OK;
39446       }
39447       goto end_playback;
39448     }
39449 
39450     /* If nRec is 0xffffffff, then this journal was created by a process
39451     ** working in no-sync mode. This means that the rest of the journal
39452     ** file consists of pages, there are no more journal headers. Compute
39453     ** the value of nRec based on this assumption.
39454     */
39455     if( nRec==0xffffffff ){
39456       assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) );
39457       nRec = (int)((szJ - JOURNAL_HDR_SZ(pPager))/JOURNAL_PG_SZ(pPager));
39458     }
39459 
39460     /* If nRec is 0 and this rollback is of a transaction created by this
39461     ** process and if this is the final header in the journal, then it means
39462     ** that this part of the journal was being filled but has not yet been
39463     ** synced to disk.  Compute the number of pages based on the remaining
39464     ** size of the file.
39465     **
39466     ** The third term of the test was added to fix ticket #2565.
39467     ** When rolling back a hot journal, nRec==0 always means that the next
39468     ** chunk of the journal contains zero pages to be rolled back.  But
39469     ** when doing a ROLLBACK and the nRec==0 chunk is the last chunk in
39470     ** the journal, it means that the journal might contain additional
39471     ** pages that need to be rolled back and that the number of pages
39472     ** should be computed based on the journal file size.
39473     */
39474     if( nRec==0 && !isHot &&
39475         pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff ){
39476       nRec = (int)((szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager));
39477     }
39478 
39479     /* If this is the first header read from the journal, truncate the
39480     ** database file back to its original size.
39481     */
39482     if( pPager->journalOff==JOURNAL_HDR_SZ(pPager) ){
39483       rc = pager_truncate(pPager, mxPg);
39484       if( rc!=SQLITE_OK ){
39485         goto end_playback;
39486       }
39487       pPager->dbSize = mxPg;
39488     }
39489 
39490     /* Copy original pages out of the journal and back into the
39491     ** database file and/or page cache.
39492     */
39493     for(u=0; u<nRec; u++){
39494       if( needPagerReset ){
39495         pager_reset(pPager);
39496         needPagerReset = 0;
39497       }
39498       rc = pager_playback_one_page(pPager,&pPager->journalOff,0,1,0);
39499       if( rc!=SQLITE_OK ){
39500         if( rc==SQLITE_DONE ){
39501           rc = SQLITE_OK;
39502           pPager->journalOff = szJ;
39503           break;
39504         }else if( rc==SQLITE_IOERR_SHORT_READ ){
39505           /* If the journal has been truncated, simply stop reading and
39506           ** processing the journal. This might happen if the journal was
39507           ** not completely written and synced prior to a crash.  In that
39508           ** case, the database should have never been written in the
39509           ** first place so it is OK to simply abandon the rollback. */
39510           rc = SQLITE_OK;
39511           goto end_playback;
39512         }else{
39513           /* If we are unable to rollback, quit and return the error
39514           ** code.  This will cause the pager to enter the error state
39515           ** so that no further harm will be done.  Perhaps the next
39516           ** process to come along will be able to rollback the database.
39517           */
39518           goto end_playback;
39519         }
39520       }
39521     }
39522   }
39523   /*NOTREACHED*/
39524   assert( 0 );
39525 
39526 end_playback:
39527   /* Following a rollback, the database file should be back in its original
39528   ** state prior to the start of the transaction, so invoke the
39529   ** SQLITE_FCNTL_DB_UNCHANGED file-control method to disable the
39530   ** assertion that the transaction counter was modified.
39531   */
39532   assert(
39533     pPager->fd->pMethods==0 ||
39534     sqlite3OsFileControl(pPager->fd,SQLITE_FCNTL_DB_UNCHANGED,0)>=SQLITE_OK
39535   );
39536 
39537   /* If this playback is happening automatically as a result of an IO or
39538   ** malloc error that occurred after the change-counter was updated but
39539   ** before the transaction was committed, then the change-counter
39540   ** modification may just have been reverted. If this happens in exclusive
39541   ** mode, then subsequent transactions performed by the connection will not
39542   ** update the change-counter at all. This may lead to cache inconsistency
39543   ** problems for other processes at some point in the future. So, just
39544   ** in case this has happened, clear the changeCountDone flag now.
39545   */
39546   pPager->changeCountDone = pPager->tempFile;
39547 
39548   if( rc==SQLITE_OK ){
39549     zMaster = pPager->pTmpSpace;
39550     rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
39551     testcase( rc!=SQLITE_OK );
39552   }
39553   if( rc==SQLITE_OK
39554    && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
39555   ){
39556     rc = sqlite3PagerSync(pPager);
39557   }
39558   if( rc==SQLITE_OK ){
39559     rc = pager_end_transaction(pPager, zMaster[0]!='\0');
39560     testcase( rc!=SQLITE_OK );
39561   }
39562   if( rc==SQLITE_OK && zMaster[0] && res ){
39563     /* If there was a master journal and this routine will return success,
39564     ** see if it is possible to delete the master journal.
39565     */
39566     rc = pager_delmaster(pPager, zMaster);
39567     testcase( rc!=SQLITE_OK );
39568   }
39569 
39570   /* The Pager.sectorSize variable may have been updated while rolling
39571   ** back a journal created by a process with a different sector size
39572   ** value. Reset it to the correct value for this process.
39573   */
39574   setSectorSize(pPager);
39575   return rc;
39576 }
39577 
39578 
39579 /*
39580 ** Read the content for page pPg out of the database file and into
39581 ** pPg->pData. A shared lock or greater must be held on the database
39582 ** file before this function is called.
39583 **
39584 ** If page 1 is read, then the value of Pager.dbFileVers[] is set to
39585 ** the value read from the database file.
39586 **
39587 ** If an IO error occurs, then the IO error is returned to the caller.
39588 ** Otherwise, SQLITE_OK is returned.
39589 */
39590 static int readDbPage(PgHdr *pPg){
39591   Pager *pPager = pPg->pPager; /* Pager object associated with page pPg */
39592   Pgno pgno = pPg->pgno;       /* Page number to read */
39593   int rc = SQLITE_OK;          /* Return code */
39594   int isInWal = 0;             /* True if page is in log file */
39595   int pgsz = pPager->pageSize; /* Number of bytes to read */
39596 
39597   assert( pPager->eState>=PAGER_READER && !MEMDB );
39598   assert( isOpen(pPager->fd) );
39599 
39600   if( NEVER(!isOpen(pPager->fd)) ){
39601     assert( pPager->tempFile );
39602     memset(pPg->pData, 0, pPager->pageSize);
39603     return SQLITE_OK;
39604   }
39605 
39606   if( pagerUseWal(pPager) ){
39607     /* Try to pull the page from the write-ahead log. */
39608     rc = sqlite3WalRead(pPager->pWal, pgno, &isInWal, pgsz, pPg->pData);
39609   }
39610   if( rc==SQLITE_OK && !isInWal ){
39611     i64 iOffset = (pgno-1)*(i64)pPager->pageSize;
39612     rc = sqlite3OsRead(pPager->fd, pPg->pData, pgsz, iOffset);
39613     if( rc==SQLITE_IOERR_SHORT_READ ){
39614       rc = SQLITE_OK;
39615     }
39616   }
39617 
39618   if( pgno==1 ){
39619     if( rc ){
39620       /* If the read is unsuccessful, set the dbFileVers[] to something
39621       ** that will never be a valid file version.  dbFileVers[] is a copy
39622       ** of bytes 24..39 of the database.  Bytes 28..31 should always be
39623       ** zero or the size of the database in page. Bytes 32..35 and 35..39
39624       ** should be page numbers which are never 0xffffffff.  So filling
39625       ** pPager->dbFileVers[] with all 0xff bytes should suffice.
39626       **
39627       ** For an encrypted database, the situation is more complex:  bytes
39628       ** 24..39 of the database are white noise.  But the probability of
39629       ** white noising equaling 16 bytes of 0xff is vanishingly small so
39630       ** we should still be ok.
39631       */
39632       memset(pPager->dbFileVers, 0xff, sizeof(pPager->dbFileVers));
39633     }else{
39634       u8 *dbFileVers = &((u8*)pPg->pData)[24];
39635       memcpy(&pPager->dbFileVers, dbFileVers, sizeof(pPager->dbFileVers));
39636     }
39637   }
39638   CODEC1(pPager, pPg->pData, pgno, 3, rc = SQLITE_NOMEM);
39639 
39640   PAGER_INCR(sqlite3_pager_readdb_count);
39641   PAGER_INCR(pPager->nRead);
39642   IOTRACE(("PGIN %p %d\n", pPager, pgno));
39643   PAGERTRACE(("FETCH %d page %d hash(%08x)\n",
39644                PAGERID(pPager), pgno, pager_pagehash(pPg)));
39645 
39646   return rc;
39647 }
39648 
39649 /*
39650 ** Update the value of the change-counter at offsets 24 and 92 in
39651 ** the header and the sqlite version number at offset 96.
39652 **
39653 ** This is an unconditional update.  See also the pager_incr_changecounter()
39654 ** routine which only updates the change-counter if the update is actually
39655 ** needed, as determined by the pPager->changeCountDone state variable.
39656 */
39657 static void pager_write_changecounter(PgHdr *pPg){
39658   u32 change_counter;
39659 
39660   /* Increment the value just read and write it back to byte 24. */
39661   change_counter = sqlite3Get4byte((u8*)pPg->pPager->dbFileVers)+1;
39662   put32bits(((char*)pPg->pData)+24, change_counter);
39663 
39664   /* Also store the SQLite version number in bytes 96..99 and in
39665   ** bytes 92..95 store the change counter for which the version number
39666   ** is valid. */
39667   put32bits(((char*)pPg->pData)+92, change_counter);
39668   put32bits(((char*)pPg->pData)+96, SQLITE_VERSION_NUMBER);
39669 }
39670 
39671 #ifndef SQLITE_OMIT_WAL
39672 /*
39673 ** This function is invoked once for each page that has already been
39674 ** written into the log file when a WAL transaction is rolled back.
39675 ** Parameter iPg is the page number of said page. The pCtx argument
39676 ** is actually a pointer to the Pager structure.
39677 **
39678 ** If page iPg is present in the cache, and has no outstanding references,
39679 ** it is discarded. Otherwise, if there are one or more outstanding
39680 ** references, the page content is reloaded from the database. If the
39681 ** attempt to reload content from the database is required and fails,
39682 ** return an SQLite error code. Otherwise, SQLITE_OK.
39683 */
39684 static int pagerUndoCallback(void *pCtx, Pgno iPg){
39685   int rc = SQLITE_OK;
39686   Pager *pPager = (Pager *)pCtx;
39687   PgHdr *pPg;
39688 
39689   pPg = sqlite3PagerLookup(pPager, iPg);
39690   if( pPg ){
39691     if( sqlite3PcachePageRefcount(pPg)==1 ){
39692       sqlite3PcacheDrop(pPg);
39693     }else{
39694       rc = readDbPage(pPg);
39695       if( rc==SQLITE_OK ){
39696         pPager->xReiniter(pPg);
39697       }
39698       sqlite3PagerUnref(pPg);
39699     }
39700   }
39701 
39702   /* Normally, if a transaction is rolled back, any backup processes are
39703   ** updated as data is copied out of the rollback journal and into the
39704   ** database. This is not generally possible with a WAL database, as
39705   ** rollback involves simply truncating the log file. Therefore, if one
39706   ** or more frames have already been written to the log (and therefore
39707   ** also copied into the backup databases) as part of this transaction,
39708   ** the backups must be restarted.
39709   */
39710   sqlite3BackupRestart(pPager->pBackup);
39711 
39712   return rc;
39713 }
39714 
39715 /*
39716 ** This function is called to rollback a transaction on a WAL database.
39717 */
39718 static int pagerRollbackWal(Pager *pPager){
39719   int rc;                         /* Return Code */
39720   PgHdr *pList;                   /* List of dirty pages to revert */
39721 
39722   /* For all pages in the cache that are currently dirty or have already
39723   ** been written (but not committed) to the log file, do one of the
39724   ** following:
39725   **
39726   **   + Discard the cached page (if refcount==0), or
39727   **   + Reload page content from the database (if refcount>0).
39728   */
39729   pPager->dbSize = pPager->dbOrigSize;
39730   rc = sqlite3WalUndo(pPager->pWal, pagerUndoCallback, (void *)pPager);
39731   pList = sqlite3PcacheDirtyList(pPager->pPCache);
39732   while( pList && rc==SQLITE_OK ){
39733     PgHdr *pNext = pList->pDirty;
39734     rc = pagerUndoCallback((void *)pPager, pList->pgno);
39735     pList = pNext;
39736   }
39737 
39738   return rc;
39739 }
39740 
39741 /*
39742 ** This function is a wrapper around sqlite3WalFrames(). As well as logging
39743 ** the contents of the list of pages headed by pList (connected by pDirty),
39744 ** this function notifies any active backup processes that the pages have
39745 ** changed.
39746 **
39747 ** The list of pages passed into this routine is always sorted by page number.
39748 ** Hence, if page 1 appears anywhere on the list, it will be the first page.
39749 */
39750 static int pagerWalFrames(
39751   Pager *pPager,                  /* Pager object */
39752   PgHdr *pList,                   /* List of frames to log */
39753   Pgno nTruncate,                 /* Database size after this commit */
39754   int isCommit,                   /* True if this is a commit */
39755   int syncFlags                   /* Flags to pass to OsSync() (or 0) */
39756 ){
39757   int rc;                         /* Return code */
39758 #if defined(SQLITE_DEBUG) || defined(SQLITE_CHECK_PAGES)
39759   PgHdr *p;                       /* For looping over pages */
39760 #endif
39761 
39762   assert( pPager->pWal );
39763 #ifdef SQLITE_DEBUG
39764   /* Verify that the page list is in accending order */
39765   for(p=pList; p && p->pDirty; p=p->pDirty){
39766     assert( p->pgno < p->pDirty->pgno );
39767   }
39768 #endif
39769 
39770   if( isCommit ){
39771     /* If a WAL transaction is being committed, there is no point in writing
39772     ** any pages with page numbers greater than nTruncate into the WAL file.
39773     ** They will never be read by any client. So remove them from the pDirty
39774     ** list here. */
39775     PgHdr *p;
39776     PgHdr **ppNext = &pList;
39777     for(p=pList; (*ppNext = p); p=p->pDirty){
39778       if( p->pgno<=nTruncate ) ppNext = &p->pDirty;
39779     }
39780     assert( pList );
39781   }
39782 
39783   if( pList->pgno==1 ) pager_write_changecounter(pList);
39784   rc = sqlite3WalFrames(pPager->pWal,
39785       pPager->pageSize, pList, nTruncate, isCommit, syncFlags
39786   );
39787   if( rc==SQLITE_OK && pPager->pBackup ){
39788     PgHdr *p;
39789     for(p=pList; p; p=p->pDirty){
39790       sqlite3BackupUpdate(pPager->pBackup, p->pgno, (u8 *)p->pData);
39791     }
39792   }
39793 
39794 #ifdef SQLITE_CHECK_PAGES
39795   pList = sqlite3PcacheDirtyList(pPager->pPCache);
39796   for(p=pList; p; p=p->pDirty){
39797     pager_set_pagehash(p);
39798   }
39799 #endif
39800 
39801   return rc;
39802 }
39803 
39804 /*
39805 ** Begin a read transaction on the WAL.
39806 **
39807 ** This routine used to be called "pagerOpenSnapshot()" because it essentially
39808 ** makes a snapshot of the database at the current point in time and preserves
39809 ** that snapshot for use by the reader in spite of concurrently changes by
39810 ** other writers or checkpointers.
39811 */
39812 static int pagerBeginReadTransaction(Pager *pPager){
39813   int rc;                         /* Return code */
39814   int changed = 0;                /* True if cache must be reset */
39815 
39816   assert( pagerUseWal(pPager) );
39817   assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER );
39818 
39819   /* sqlite3WalEndReadTransaction() was not called for the previous
39820   ** transaction in locking_mode=EXCLUSIVE.  So call it now.  If we
39821   ** are in locking_mode=NORMAL and EndRead() was previously called,
39822   ** the duplicate call is harmless.
39823   */
39824   sqlite3WalEndReadTransaction(pPager->pWal);
39825 
39826   rc = sqlite3WalBeginReadTransaction(pPager->pWal, &changed);
39827   if( rc!=SQLITE_OK || changed ){
39828     pager_reset(pPager);
39829   }
39830 
39831   return rc;
39832 }
39833 #endif
39834 
39835 /*
39836 ** This function is called as part of the transition from PAGER_OPEN
39837 ** to PAGER_READER state to determine the size of the database file
39838 ** in pages (assuming the page size currently stored in Pager.pageSize).
39839 **
39840 ** If no error occurs, SQLITE_OK is returned and the size of the database
39841 ** in pages is stored in *pnPage. Otherwise, an error code (perhaps
39842 ** SQLITE_IOERR_FSTAT) is returned and *pnPage is left unmodified.
39843 */
39844 static int pagerPagecount(Pager *pPager, Pgno *pnPage){
39845   Pgno nPage;                     /* Value to return via *pnPage */
39846 
39847   /* Query the WAL sub-system for the database size. The WalDbsize()
39848   ** function returns zero if the WAL is not open (i.e. Pager.pWal==0), or
39849   ** if the database size is not available. The database size is not
39850   ** available from the WAL sub-system if the log file is empty or
39851   ** contains no valid committed transactions.
39852   */
39853   assert( pPager->eState==PAGER_OPEN );
39854   assert( pPager->eLock>=SHARED_LOCK || pPager->noReadlock );
39855   nPage = sqlite3WalDbsize(pPager->pWal);
39856 
39857   /* If the database size was not available from the WAL sub-system,
39858   ** determine it based on the size of the database file. If the size
39859   ** of the database file is not an integer multiple of the page-size,
39860   ** round down to the nearest page. Except, any file larger than 0
39861   ** bytes in size is considered to contain at least one page.
39862   */
39863   if( nPage==0 ){
39864     i64 n = 0;                    /* Size of db file in bytes */
39865     assert( isOpen(pPager->fd) || pPager->tempFile );
39866     if( isOpen(pPager->fd) ){
39867       int rc = sqlite3OsFileSize(pPager->fd, &n);
39868       if( rc!=SQLITE_OK ){
39869         return rc;
39870       }
39871     }
39872     nPage = (Pgno)(n / pPager->pageSize);
39873     if( nPage==0 && n>0 ){
39874       nPage = 1;
39875     }
39876   }
39877 
39878   /* If the current number of pages in the file is greater than the
39879   ** configured maximum pager number, increase the allowed limit so
39880   ** that the file can be read.
39881   */
39882   if( nPage>pPager->mxPgno ){
39883     pPager->mxPgno = (Pgno)nPage;
39884   }
39885 
39886   *pnPage = nPage;
39887   return SQLITE_OK;
39888 }
39889 
39890 #ifndef SQLITE_OMIT_WAL
39891 /*
39892 ** Check if the *-wal file that corresponds to the database opened by pPager
39893 ** exists if the database is not empy, or verify that the *-wal file does
39894 ** not exist (by deleting it) if the database file is empty.
39895 **
39896 ** If the database is not empty and the *-wal file exists, open the pager
39897 ** in WAL mode.  If the database is empty or if no *-wal file exists and
39898 ** if no error occurs, make sure Pager.journalMode is not set to
39899 ** PAGER_JOURNALMODE_WAL.
39900 **
39901 ** Return SQLITE_OK or an error code.
39902 **
39903 ** The caller must hold a SHARED lock on the database file to call this
39904 ** function. Because an EXCLUSIVE lock on the db file is required to delete
39905 ** a WAL on a none-empty database, this ensures there is no race condition
39906 ** between the xAccess() below and an xDelete() being executed by some
39907 ** other connection.
39908 */
39909 static int pagerOpenWalIfPresent(Pager *pPager){
39910   int rc = SQLITE_OK;
39911   assert( pPager->eState==PAGER_OPEN );
39912   assert( pPager->eLock>=SHARED_LOCK || pPager->noReadlock );
39913 
39914   if( !pPager->tempFile ){
39915     int isWal;                    /* True if WAL file exists */
39916     Pgno nPage;                   /* Size of the database file */
39917 
39918     rc = pagerPagecount(pPager, &nPage);
39919     if( rc ) return rc;
39920     if( nPage==0 ){
39921       rc = sqlite3OsDelete(pPager->pVfs, pPager->zWal, 0);
39922       isWal = 0;
39923     }else{
39924       rc = sqlite3OsAccess(
39925           pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS, &isWal
39926       );
39927     }
39928     if( rc==SQLITE_OK ){
39929       if( isWal ){
39930         testcase( sqlite3PcachePagecount(pPager->pPCache)==0 );
39931         rc = sqlite3PagerOpenWal(pPager, 0);
39932       }else if( pPager->journalMode==PAGER_JOURNALMODE_WAL ){
39933         pPager->journalMode = PAGER_JOURNALMODE_DELETE;
39934       }
39935     }
39936   }
39937   return rc;
39938 }
39939 #endif
39940 
39941 /*
39942 ** Playback savepoint pSavepoint. Or, if pSavepoint==NULL, then playback
39943 ** the entire master journal file. The case pSavepoint==NULL occurs when
39944 ** a ROLLBACK TO command is invoked on a SAVEPOINT that is a transaction
39945 ** savepoint.
39946 **
39947 ** When pSavepoint is not NULL (meaning a non-transaction savepoint is
39948 ** being rolled back), then the rollback consists of up to three stages,
39949 ** performed in the order specified:
39950 **
39951 **   * Pages are played back from the main journal starting at byte
39952 **     offset PagerSavepoint.iOffset and continuing to
39953 **     PagerSavepoint.iHdrOffset, or to the end of the main journal
39954 **     file if PagerSavepoint.iHdrOffset is zero.
39955 **
39956 **   * If PagerSavepoint.iHdrOffset is not zero, then pages are played
39957 **     back starting from the journal header immediately following
39958 **     PagerSavepoint.iHdrOffset to the end of the main journal file.
39959 **
39960 **   * Pages are then played back from the sub-journal file, starting
39961 **     with the PagerSavepoint.iSubRec and continuing to the end of
39962 **     the journal file.
39963 **
39964 ** Throughout the rollback process, each time a page is rolled back, the
39965 ** corresponding bit is set in a bitvec structure (variable pDone in the
39966 ** implementation below). This is used to ensure that a page is only
39967 ** rolled back the first time it is encountered in either journal.
39968 **
39969 ** If pSavepoint is NULL, then pages are only played back from the main
39970 ** journal file. There is no need for a bitvec in this case.
39971 **
39972 ** In either case, before playback commences the Pager.dbSize variable
39973 ** is reset to the value that it held at the start of the savepoint
39974 ** (or transaction). No page with a page-number greater than this value
39975 ** is played back. If one is encountered it is simply skipped.
39976 */
39977 static int pagerPlaybackSavepoint(Pager *pPager, PagerSavepoint *pSavepoint){
39978   i64 szJ;                 /* Effective size of the main journal */
39979   i64 iHdrOff;             /* End of first segment of main-journal records */
39980   int rc = SQLITE_OK;      /* Return code */
39981   Bitvec *pDone = 0;       /* Bitvec to ensure pages played back only once */
39982 
39983   assert( pPager->eState!=PAGER_ERROR );
39984   assert( pPager->eState>=PAGER_WRITER_LOCKED );
39985 
39986   /* Allocate a bitvec to use to store the set of pages rolled back */
39987   if( pSavepoint ){
39988     pDone = sqlite3BitvecCreate(pSavepoint->nOrig);
39989     if( !pDone ){
39990       return SQLITE_NOMEM;
39991     }
39992   }
39993 
39994   /* Set the database size back to the value it was before the savepoint
39995   ** being reverted was opened.
39996   */
39997   pPager->dbSize = pSavepoint ? pSavepoint->nOrig : pPager->dbOrigSize;
39998   pPager->changeCountDone = pPager->tempFile;
39999 
40000   if( !pSavepoint && pagerUseWal(pPager) ){
40001     return pagerRollbackWal(pPager);
40002   }
40003 
40004   /* Use pPager->journalOff as the effective size of the main rollback
40005   ** journal.  The actual file might be larger than this in
40006   ** PAGER_JOURNALMODE_TRUNCATE or PAGER_JOURNALMODE_PERSIST.  But anything
40007   ** past pPager->journalOff is off-limits to us.
40008   */
40009   szJ = pPager->journalOff;
40010   assert( pagerUseWal(pPager)==0 || szJ==0 );
40011 
40012   /* Begin by rolling back records from the main journal starting at
40013   ** PagerSavepoint.iOffset and continuing to the next journal header.
40014   ** There might be records in the main journal that have a page number
40015   ** greater than the current database size (pPager->dbSize) but those
40016   ** will be skipped automatically.  Pages are added to pDone as they
40017   ** are played back.
40018   */
40019   if( pSavepoint && !pagerUseWal(pPager) ){
40020     iHdrOff = pSavepoint->iHdrOffset ? pSavepoint->iHdrOffset : szJ;
40021     pPager->journalOff = pSavepoint->iOffset;
40022     while( rc==SQLITE_OK && pPager->journalOff<iHdrOff ){
40023       rc = pager_playback_one_page(pPager, &pPager->journalOff, pDone, 1, 1);
40024     }
40025     assert( rc!=SQLITE_DONE );
40026   }else{
40027     pPager->journalOff = 0;
40028   }
40029 
40030   /* Continue rolling back records out of the main journal starting at
40031   ** the first journal header seen and continuing until the effective end
40032   ** of the main journal file.  Continue to skip out-of-range pages and
40033   ** continue adding pages rolled back to pDone.
40034   */
40035   while( rc==SQLITE_OK && pPager->journalOff<szJ ){
40036     u32 ii;            /* Loop counter */
40037     u32 nJRec = 0;     /* Number of Journal Records */
40038     u32 dummy;
40039     rc = readJournalHdr(pPager, 0, szJ, &nJRec, &dummy);
40040     assert( rc!=SQLITE_DONE );
40041 
40042     /*
40043     ** The "pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff"
40044     ** test is related to ticket #2565.  See the discussion in the
40045     ** pager_playback() function for additional information.
40046     */
40047     if( nJRec==0
40048      && pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff
40049     ){
40050       nJRec = (u32)((szJ - pPager->journalOff)/JOURNAL_PG_SZ(pPager));
40051     }
40052     for(ii=0; rc==SQLITE_OK && ii<nJRec && pPager->journalOff<szJ; ii++){
40053       rc = pager_playback_one_page(pPager, &pPager->journalOff, pDone, 1, 1);
40054     }
40055     assert( rc!=SQLITE_DONE );
40056   }
40057   assert( rc!=SQLITE_OK || pPager->journalOff>=szJ );
40058 
40059   /* Finally,  rollback pages from the sub-journal.  Page that were
40060   ** previously rolled back out of the main journal (and are hence in pDone)
40061   ** will be skipped.  Out-of-range pages are also skipped.
40062   */
40063   if( pSavepoint ){
40064     u32 ii;            /* Loop counter */
40065     i64 offset = pSavepoint->iSubRec*(4+pPager->pageSize);
40066 
40067     if( pagerUseWal(pPager) ){
40068       rc = sqlite3WalSavepointUndo(pPager->pWal, pSavepoint->aWalData);
40069     }
40070     for(ii=pSavepoint->iSubRec; rc==SQLITE_OK && ii<pPager->nSubRec; ii++){
40071       assert( offset==ii*(4+pPager->pageSize) );
40072       rc = pager_playback_one_page(pPager, &offset, pDone, 0, 1);
40073     }
40074     assert( rc!=SQLITE_DONE );
40075   }
40076 
40077   sqlite3BitvecDestroy(pDone);
40078   if( rc==SQLITE_OK ){
40079     pPager->journalOff = szJ;
40080   }
40081 
40082   return rc;
40083 }
40084 
40085 /*
40086 ** Change the maximum number of in-memory pages that are allowed.
40087 */
40088 SQLITE_PRIVATE void sqlite3PagerSetCachesize(Pager *pPager, int mxPage){
40089   sqlite3PcacheSetCachesize(pPager->pPCache, mxPage);
40090 }
40091 
40092 /*
40093 ** Adjust the robustness of the database to damage due to OS crashes
40094 ** or power failures by changing the number of syncs()s when writing
40095 ** the rollback journal.  There are three levels:
40096 **
40097 **    OFF       sqlite3OsSync() is never called.  This is the default
40098 **              for temporary and transient files.
40099 **
40100 **    NORMAL    The journal is synced once before writes begin on the
40101 **              database.  This is normally adequate protection, but
40102 **              it is theoretically possible, though very unlikely,
40103 **              that an inopertune power failure could leave the journal
40104 **              in a state which would cause damage to the database
40105 **              when it is rolled back.
40106 **
40107 **    FULL      The journal is synced twice before writes begin on the
40108 **              database (with some additional information - the nRec field
40109 **              of the journal header - being written in between the two
40110 **              syncs).  If we assume that writing a
40111 **              single disk sector is atomic, then this mode provides
40112 **              assurance that the journal will not be corrupted to the
40113 **              point of causing damage to the database during rollback.
40114 **
40115 ** The above is for a rollback-journal mode.  For WAL mode, OFF continues
40116 ** to mean that no syncs ever occur.  NORMAL means that the WAL is synced
40117 ** prior to the start of checkpoint and that the database file is synced
40118 ** at the conclusion of the checkpoint if the entire content of the WAL
40119 ** was written back into the database.  But no sync operations occur for
40120 ** an ordinary commit in NORMAL mode with WAL.  FULL means that the WAL
40121 ** file is synced following each commit operation, in addition to the
40122 ** syncs associated with NORMAL.
40123 **
40124 ** Do not confuse synchronous=FULL with SQLITE_SYNC_FULL.  The
40125 ** SQLITE_SYNC_FULL macro means to use the MacOSX-style full-fsync
40126 ** using fcntl(F_FULLFSYNC).  SQLITE_SYNC_NORMAL means to do an
40127 ** ordinary fsync() call.  There is no difference between SQLITE_SYNC_FULL
40128 ** and SQLITE_SYNC_NORMAL on platforms other than MacOSX.  But the
40129 ** synchronous=FULL versus synchronous=NORMAL setting determines when
40130 ** the xSync primitive is called and is relevant to all platforms.
40131 **
40132 ** Numeric values associated with these states are OFF==1, NORMAL=2,
40133 ** and FULL=3.
40134 */
40135 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
40136 SQLITE_PRIVATE void sqlite3PagerSetSafetyLevel(
40137   Pager *pPager,        /* The pager to set safety level for */
40138   int level,            /* PRAGMA synchronous.  1=OFF, 2=NORMAL, 3=FULL */
40139   int bFullFsync,       /* PRAGMA fullfsync */
40140   int bCkptFullFsync    /* PRAGMA checkpoint_fullfsync */
40141 ){
40142   assert( level>=1 && level<=3 );
40143   pPager->noSync =  (level==1 || pPager->tempFile) ?1:0;
40144   pPager->fullSync = (level==3 && !pPager->tempFile) ?1:0;
40145   if( pPager->noSync ){
40146     pPager->syncFlags = 0;
40147     pPager->ckptSyncFlags = 0;
40148   }else if( bFullFsync ){
40149     pPager->syncFlags = SQLITE_SYNC_FULL;
40150     pPager->ckptSyncFlags = SQLITE_SYNC_FULL;
40151   }else if( bCkptFullFsync ){
40152     pPager->syncFlags = SQLITE_SYNC_NORMAL;
40153     pPager->ckptSyncFlags = SQLITE_SYNC_FULL;
40154   }else{
40155     pPager->syncFlags = SQLITE_SYNC_NORMAL;
40156     pPager->ckptSyncFlags = SQLITE_SYNC_NORMAL;
40157   }
40158 }
40159 #endif
40160 
40161 /*
40162 ** The following global variable is incremented whenever the library
40163 ** attempts to open a temporary file.  This information is used for
40164 ** testing and analysis only.
40165 */
40166 #ifdef SQLITE_TEST
40167 SQLITE_API int sqlite3_opentemp_count = 0;
40168 #endif
40169 
40170 /*
40171 ** Open a temporary file.
40172 **
40173 ** Write the file descriptor into *pFile. Return SQLITE_OK on success
40174 ** or some other error code if we fail. The OS will automatically
40175 ** delete the temporary file when it is closed.
40176 **
40177 ** The flags passed to the VFS layer xOpen() call are those specified
40178 ** by parameter vfsFlags ORed with the following:
40179 **
40180 **     SQLITE_OPEN_READWRITE
40181 **     SQLITE_OPEN_CREATE
40182 **     SQLITE_OPEN_EXCLUSIVE
40183 **     SQLITE_OPEN_DELETEONCLOSE
40184 */
40185 static int pagerOpentemp(
40186   Pager *pPager,        /* The pager object */
40187   sqlite3_file *pFile,  /* Write the file descriptor here */
40188   int vfsFlags          /* Flags passed through to the VFS */
40189 ){
40190   int rc;               /* Return code */
40191 
40192 #ifdef SQLITE_TEST
40193   sqlite3_opentemp_count++;  /* Used for testing and analysis only */
40194 #endif
40195 
40196   vfsFlags |=  SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
40197             SQLITE_OPEN_EXCLUSIVE | SQLITE_OPEN_DELETEONCLOSE;
40198   rc = sqlite3OsOpen(pPager->pVfs, 0, pFile, vfsFlags, 0);
40199   assert( rc!=SQLITE_OK || isOpen(pFile) );
40200   return rc;
40201 }
40202 
40203 /*
40204 ** Set the busy handler function.
40205 **
40206 ** The pager invokes the busy-handler if sqlite3OsLock() returns
40207 ** SQLITE_BUSY when trying to upgrade from no-lock to a SHARED lock,
40208 ** or when trying to upgrade from a RESERVED lock to an EXCLUSIVE
40209 ** lock. It does *not* invoke the busy handler when upgrading from
40210 ** SHARED to RESERVED, or when upgrading from SHARED to EXCLUSIVE
40211 ** (which occurs during hot-journal rollback). Summary:
40212 **
40213 **   Transition                        | Invokes xBusyHandler
40214 **   --------------------------------------------------------
40215 **   NO_LOCK       -> SHARED_LOCK      | Yes
40216 **   SHARED_LOCK   -> RESERVED_LOCK    | No
40217 **   SHARED_LOCK   -> EXCLUSIVE_LOCK   | No
40218 **   RESERVED_LOCK -> EXCLUSIVE_LOCK   | Yes
40219 **
40220 ** If the busy-handler callback returns non-zero, the lock is
40221 ** retried. If it returns zero, then the SQLITE_BUSY error is
40222 ** returned to the caller of the pager API function.
40223 */
40224 SQLITE_PRIVATE void sqlite3PagerSetBusyhandler(
40225   Pager *pPager,                       /* Pager object */
40226   int (*xBusyHandler)(void *),         /* Pointer to busy-handler function */
40227   void *pBusyHandlerArg                /* Argument to pass to xBusyHandler */
40228 ){
40229   pPager->xBusyHandler = xBusyHandler;
40230   pPager->pBusyHandlerArg = pBusyHandlerArg;
40231 }
40232 
40233 /*
40234 ** Change the page size used by the Pager object. The new page size
40235 ** is passed in *pPageSize.
40236 **
40237 ** If the pager is in the error state when this function is called, it
40238 ** is a no-op. The value returned is the error state error code (i.e.
40239 ** one of SQLITE_IOERR, an SQLITE_IOERR_xxx sub-code or SQLITE_FULL).
40240 **
40241 ** Otherwise, if all of the following are true:
40242 **
40243 **   * the new page size (value of *pPageSize) is valid (a power
40244 **     of two between 512 and SQLITE_MAX_PAGE_SIZE, inclusive), and
40245 **
40246 **   * there are no outstanding page references, and
40247 **
40248 **   * the database is either not an in-memory database or it is
40249 **     an in-memory database that currently consists of zero pages.
40250 **
40251 ** then the pager object page size is set to *pPageSize.
40252 **
40253 ** If the page size is changed, then this function uses sqlite3PagerMalloc()
40254 ** to obtain a new Pager.pTmpSpace buffer. If this allocation attempt
40255 ** fails, SQLITE_NOMEM is returned and the page size remains unchanged.
40256 ** In all other cases, SQLITE_OK is returned.
40257 **
40258 ** If the page size is not changed, either because one of the enumerated
40259 ** conditions above is not true, the pager was in error state when this
40260 ** function was called, or because the memory allocation attempt failed,
40261 ** then *pPageSize is set to the old, retained page size before returning.
40262 */
40263 SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager *pPager, u32 *pPageSize, int nReserve){
40264   int rc = SQLITE_OK;
40265 
40266   /* It is not possible to do a full assert_pager_state() here, as this
40267   ** function may be called from within PagerOpen(), before the state
40268   ** of the Pager object is internally consistent.
40269   **
40270   ** At one point this function returned an error if the pager was in
40271   ** PAGER_ERROR state. But since PAGER_ERROR state guarantees that
40272   ** there is at least one outstanding page reference, this function
40273   ** is a no-op for that case anyhow.
40274   */
40275 
40276   u32 pageSize = *pPageSize;
40277   assert( pageSize==0 || (pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE) );
40278   if( (pPager->memDb==0 || pPager->dbSize==0)
40279    && sqlite3PcacheRefCount(pPager->pPCache)==0
40280    && pageSize && pageSize!=(u32)pPager->pageSize
40281   ){
40282     char *pNew = NULL;             /* New temp space */
40283     i64 nByte = 0;
40284 
40285     if( pPager->eState>PAGER_OPEN && isOpen(pPager->fd) ){
40286       rc = sqlite3OsFileSize(pPager->fd, &nByte);
40287     }
40288     if( rc==SQLITE_OK ){
40289       pNew = (char *)sqlite3PageMalloc(pageSize);
40290       if( !pNew ) rc = SQLITE_NOMEM;
40291     }
40292 
40293     if( rc==SQLITE_OK ){
40294       pager_reset(pPager);
40295       pPager->dbSize = (Pgno)(nByte/pageSize);
40296       pPager->pageSize = pageSize;
40297       sqlite3PageFree(pPager->pTmpSpace);
40298       pPager->pTmpSpace = pNew;
40299       sqlite3PcacheSetPageSize(pPager->pPCache, pageSize);
40300     }
40301   }
40302 
40303   *pPageSize = pPager->pageSize;
40304   if( rc==SQLITE_OK ){
40305     if( nReserve<0 ) nReserve = pPager->nReserve;
40306     assert( nReserve>=0 && nReserve<1000 );
40307     pPager->nReserve = (i16)nReserve;
40308     pagerReportSize(pPager);
40309   }
40310   return rc;
40311 }
40312 
40313 /*
40314 ** Return a pointer to the "temporary page" buffer held internally
40315 ** by the pager.  This is a buffer that is big enough to hold the
40316 ** entire content of a database page.  This buffer is used internally
40317 ** during rollback and will be overwritten whenever a rollback
40318 ** occurs.  But other modules are free to use it too, as long as
40319 ** no rollbacks are happening.
40320 */
40321 SQLITE_PRIVATE void *sqlite3PagerTempSpace(Pager *pPager){
40322   return pPager->pTmpSpace;
40323 }
40324 
40325 /*
40326 ** Attempt to set the maximum database page count if mxPage is positive.
40327 ** Make no changes if mxPage is zero or negative.  And never reduce the
40328 ** maximum page count below the current size of the database.
40329 **
40330 ** Regardless of mxPage, return the current maximum page count.
40331 */
40332 SQLITE_PRIVATE int sqlite3PagerMaxPageCount(Pager *pPager, int mxPage){
40333   if( mxPage>0 ){
40334     pPager->mxPgno = mxPage;
40335   }
40336   assert( pPager->eState!=PAGER_OPEN );      /* Called only by OP_MaxPgcnt */
40337   assert( pPager->mxPgno>=pPager->dbSize );  /* OP_MaxPgcnt enforces this */
40338   return pPager->mxPgno;
40339 }
40340 
40341 /*
40342 ** The following set of routines are used to disable the simulated
40343 ** I/O error mechanism.  These routines are used to avoid simulated
40344 ** errors in places where we do not care about errors.
40345 **
40346 ** Unless -DSQLITE_TEST=1 is used, these routines are all no-ops
40347 ** and generate no code.
40348 */
40349 #ifdef SQLITE_TEST
40350 SQLITE_API extern int sqlite3_io_error_pending;
40351 SQLITE_API extern int sqlite3_io_error_hit;
40352 static int saved_cnt;
40353 void disable_simulated_io_errors(void){
40354   saved_cnt = sqlite3_io_error_pending;
40355   sqlite3_io_error_pending = -1;
40356 }
40357 void enable_simulated_io_errors(void){
40358   sqlite3_io_error_pending = saved_cnt;
40359 }
40360 #else
40361 # define disable_simulated_io_errors()
40362 # define enable_simulated_io_errors()
40363 #endif
40364 
40365 /*
40366 ** Read the first N bytes from the beginning of the file into memory
40367 ** that pDest points to.
40368 **
40369 ** If the pager was opened on a transient file (zFilename==""), or
40370 ** opened on a file less than N bytes in size, the output buffer is
40371 ** zeroed and SQLITE_OK returned. The rationale for this is that this
40372 ** function is used to read database headers, and a new transient or
40373 ** zero sized database has a header than consists entirely of zeroes.
40374 **
40375 ** If any IO error apart from SQLITE_IOERR_SHORT_READ is encountered,
40376 ** the error code is returned to the caller and the contents of the
40377 ** output buffer undefined.
40378 */
40379 SQLITE_PRIVATE int sqlite3PagerReadFileheader(Pager *pPager, int N, unsigned char *pDest){
40380   int rc = SQLITE_OK;
40381   memset(pDest, 0, N);
40382   assert( isOpen(pPager->fd) || pPager->tempFile );
40383 
40384   /* This routine is only called by btree immediately after creating
40385   ** the Pager object.  There has not been an opportunity to transition
40386   ** to WAL mode yet.
40387   */
40388   assert( !pagerUseWal(pPager) );
40389 
40390   if( isOpen(pPager->fd) ){
40391     IOTRACE(("DBHDR %p 0 %d\n", pPager, N))
40392     rc = sqlite3OsRead(pPager->fd, pDest, N, 0);
40393     if( rc==SQLITE_IOERR_SHORT_READ ){
40394       rc = SQLITE_OK;
40395     }
40396   }
40397   return rc;
40398 }
40399 
40400 /*
40401 ** This function may only be called when a read-transaction is open on
40402 ** the pager. It returns the total number of pages in the database.
40403 **
40404 ** However, if the file is between 1 and <page-size> bytes in size, then
40405 ** this is considered a 1 page file.
40406 */
40407 SQLITE_PRIVATE void sqlite3PagerPagecount(Pager *pPager, int *pnPage){
40408   assert( pPager->eState>=PAGER_READER );
40409   assert( pPager->eState!=PAGER_WRITER_FINISHED );
40410   *pnPage = (int)pPager->dbSize;
40411 }
40412 
40413 
40414 /*
40415 ** Try to obtain a lock of type locktype on the database file. If
40416 ** a similar or greater lock is already held, this function is a no-op
40417 ** (returning SQLITE_OK immediately).
40418 **
40419 ** Otherwise, attempt to obtain the lock using sqlite3OsLock(). Invoke
40420 ** the busy callback if the lock is currently not available. Repeat
40421 ** until the busy callback returns false or until the attempt to
40422 ** obtain the lock succeeds.
40423 **
40424 ** Return SQLITE_OK on success and an error code if we cannot obtain
40425 ** the lock. If the lock is obtained successfully, set the Pager.state
40426 ** variable to locktype before returning.
40427 */
40428 static int pager_wait_on_lock(Pager *pPager, int locktype){
40429   int rc;                              /* Return code */
40430 
40431   /* Check that this is either a no-op (because the requested lock is
40432   ** already held, or one of the transistions that the busy-handler
40433   ** may be invoked during, according to the comment above
40434   ** sqlite3PagerSetBusyhandler().
40435   */
40436   assert( (pPager->eLock>=locktype)
40437        || (pPager->eLock==NO_LOCK && locktype==SHARED_LOCK)
40438        || (pPager->eLock==RESERVED_LOCK && locktype==EXCLUSIVE_LOCK)
40439   );
40440 
40441   do {
40442     rc = pagerLockDb(pPager, locktype);
40443   }while( rc==SQLITE_BUSY && pPager->xBusyHandler(pPager->pBusyHandlerArg) );
40444   return rc;
40445 }
40446 
40447 /*
40448 ** Function assertTruncateConstraint(pPager) checks that one of the
40449 ** following is true for all dirty pages currently in the page-cache:
40450 **
40451 **   a) The page number is less than or equal to the size of the
40452 **      current database image, in pages, OR
40453 **
40454 **   b) if the page content were written at this time, it would not
40455 **      be necessary to write the current content out to the sub-journal
40456 **      (as determined by function subjRequiresPage()).
40457 **
40458 ** If the condition asserted by this function were not true, and the
40459 ** dirty page were to be discarded from the cache via the pagerStress()
40460 ** routine, pagerStress() would not write the current page content to
40461 ** the database file. If a savepoint transaction were rolled back after
40462 ** this happened, the correct behaviour would be to restore the current
40463 ** content of the page. However, since this content is not present in either
40464 ** the database file or the portion of the rollback journal and
40465 ** sub-journal rolled back the content could not be restored and the
40466 ** database image would become corrupt. It is therefore fortunate that
40467 ** this circumstance cannot arise.
40468 */
40469 #if defined(SQLITE_DEBUG)
40470 static void assertTruncateConstraintCb(PgHdr *pPg){
40471   assert( pPg->flags&PGHDR_DIRTY );
40472   assert( !subjRequiresPage(pPg) || pPg->pgno<=pPg->pPager->dbSize );
40473 }
40474 static void assertTruncateConstraint(Pager *pPager){
40475   sqlite3PcacheIterateDirty(pPager->pPCache, assertTruncateConstraintCb);
40476 }
40477 #else
40478 # define assertTruncateConstraint(pPager)
40479 #endif
40480 
40481 /*
40482 ** Truncate the in-memory database file image to nPage pages. This
40483 ** function does not actually modify the database file on disk. It
40484 ** just sets the internal state of the pager object so that the
40485 ** truncation will be done when the current transaction is committed.
40486 */
40487 SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager *pPager, Pgno nPage){
40488   assert( pPager->dbSize>=nPage );
40489   assert( pPager->eState>=PAGER_WRITER_CACHEMOD );
40490   pPager->dbSize = nPage;
40491   assertTruncateConstraint(pPager);
40492 }
40493 
40494 
40495 /*
40496 ** This function is called before attempting a hot-journal rollback. It
40497 ** syncs the journal file to disk, then sets pPager->journalHdr to the
40498 ** size of the journal file so that the pager_playback() routine knows
40499 ** that the entire journal file has been synced.
40500 **
40501 ** Syncing a hot-journal to disk before attempting to roll it back ensures
40502 ** that if a power-failure occurs during the rollback, the process that
40503 ** attempts rollback following system recovery sees the same journal
40504 ** content as this process.
40505 **
40506 ** If everything goes as planned, SQLITE_OK is returned. Otherwise,
40507 ** an SQLite error code.
40508 */
40509 static int pagerSyncHotJournal(Pager *pPager){
40510   int rc = SQLITE_OK;
40511   if( !pPager->noSync ){
40512     rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_NORMAL);
40513   }
40514   if( rc==SQLITE_OK ){
40515     rc = sqlite3OsFileSize(pPager->jfd, &pPager->journalHdr);
40516   }
40517   return rc;
40518 }
40519 
40520 /*
40521 ** Shutdown the page cache.  Free all memory and close all files.
40522 **
40523 ** If a transaction was in progress when this routine is called, that
40524 ** transaction is rolled back.  All outstanding pages are invalidated
40525 ** and their memory is freed.  Any attempt to use a page associated
40526 ** with this page cache after this function returns will likely
40527 ** result in a coredump.
40528 **
40529 ** This function always succeeds. If a transaction is active an attempt
40530 ** is made to roll it back. If an error occurs during the rollback
40531 ** a hot journal may be left in the filesystem but no error is returned
40532 ** to the caller.
40533 */
40534 SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager){
40535   u8 *pTmp = (u8 *)pPager->pTmpSpace;
40536 
40537   disable_simulated_io_errors();
40538   sqlite3BeginBenignMalloc();
40539   /* pPager->errCode = 0; */
40540   pPager->exclusiveMode = 0;
40541 #ifndef SQLITE_OMIT_WAL
40542   sqlite3WalClose(pPager->pWal, pPager->ckptSyncFlags, pPager->pageSize, pTmp);
40543   pPager->pWal = 0;
40544 #endif
40545   pager_reset(pPager);
40546   if( MEMDB ){
40547     pager_unlock(pPager);
40548   }else{
40549     /* If it is open, sync the journal file before calling UnlockAndRollback.
40550     ** If this is not done, then an unsynced portion of the open journal
40551     ** file may be played back into the database. If a power failure occurs
40552     ** while this is happening, the database could become corrupt.
40553     **
40554     ** If an error occurs while trying to sync the journal, shift the pager
40555     ** into the ERROR state. This causes UnlockAndRollback to unlock the
40556     ** database and close the journal file without attempting to roll it
40557     ** back or finalize it. The next database user will have to do hot-journal
40558     ** rollback before accessing the database file.
40559     */
40560     if( isOpen(pPager->jfd) ){
40561       pager_error(pPager, pagerSyncHotJournal(pPager));
40562     }
40563     pagerUnlockAndRollback(pPager);
40564   }
40565   sqlite3EndBenignMalloc();
40566   enable_simulated_io_errors();
40567   PAGERTRACE(("CLOSE %d\n", PAGERID(pPager)));
40568   IOTRACE(("CLOSE %p\n", pPager))
40569   sqlite3OsClose(pPager->jfd);
40570   sqlite3OsClose(pPager->fd);
40571   sqlite3PageFree(pTmp);
40572   sqlite3PcacheClose(pPager->pPCache);
40573 
40574 #ifdef SQLITE_HAS_CODEC
40575   if( pPager->xCodecFree ) pPager->xCodecFree(pPager->pCodec);
40576 #endif
40577 
40578   assert( !pPager->aSavepoint && !pPager->pInJournal );
40579   assert( !isOpen(pPager->jfd) && !isOpen(pPager->sjfd) );
40580 
40581   sqlite3_free(pPager);
40582   return SQLITE_OK;
40583 }
40584 
40585 #if !defined(NDEBUG) || defined(SQLITE_TEST)
40586 /*
40587 ** Return the page number for page pPg.
40588 */
40589 SQLITE_PRIVATE Pgno sqlite3PagerPagenumber(DbPage *pPg){
40590   return pPg->pgno;
40591 }
40592 #endif
40593 
40594 /*
40595 ** Increment the reference count for page pPg.
40596 */
40597 SQLITE_PRIVATE void sqlite3PagerRef(DbPage *pPg){
40598   sqlite3PcacheRef(pPg);
40599 }
40600 
40601 /*
40602 ** Sync the journal. In other words, make sure all the pages that have
40603 ** been written to the journal have actually reached the surface of the
40604 ** disk and can be restored in the event of a hot-journal rollback.
40605 **
40606 ** If the Pager.noSync flag is set, then this function is a no-op.
40607 ** Otherwise, the actions required depend on the journal-mode and the
40608 ** device characteristics of the the file-system, as follows:
40609 **
40610 **   * If the journal file is an in-memory journal file, no action need
40611 **     be taken.
40612 **
40613 **   * Otherwise, if the device does not support the SAFE_APPEND property,
40614 **     then the nRec field of the most recently written journal header
40615 **     is updated to contain the number of journal records that have
40616 **     been written following it. If the pager is operating in full-sync
40617 **     mode, then the journal file is synced before this field is updated.
40618 **
40619 **   * If the device does not support the SEQUENTIAL property, then
40620 **     journal file is synced.
40621 **
40622 ** Or, in pseudo-code:
40623 **
40624 **   if( NOT <in-memory journal> ){
40625 **     if( NOT SAFE_APPEND ){
40626 **       if( <full-sync mode> ) xSync(<journal file>);
40627 **       <update nRec field>
40628 **     }
40629 **     if( NOT SEQUENTIAL ) xSync(<journal file>);
40630 **   }
40631 **
40632 ** If successful, this routine clears the PGHDR_NEED_SYNC flag of every
40633 ** page currently held in memory before returning SQLITE_OK. If an IO
40634 ** error is encountered, then the IO error code is returned to the caller.
40635 */
40636 static int syncJournal(Pager *pPager, int newHdr){
40637   int rc;                         /* Return code */
40638 
40639   assert( pPager->eState==PAGER_WRITER_CACHEMOD
40640        || pPager->eState==PAGER_WRITER_DBMOD
40641   );
40642   assert( assert_pager_state(pPager) );
40643   assert( !pagerUseWal(pPager) );
40644 
40645   rc = sqlite3PagerExclusiveLock(pPager);
40646   if( rc!=SQLITE_OK ) return rc;
40647 
40648   if( !pPager->noSync ){
40649     assert( !pPager->tempFile );
40650     if( isOpen(pPager->jfd) && pPager->journalMode!=PAGER_JOURNALMODE_MEMORY ){
40651       const int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
40652       assert( isOpen(pPager->jfd) );
40653 
40654       if( 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){
40655         /* This block deals with an obscure problem. If the last connection
40656         ** that wrote to this database was operating in persistent-journal
40657         ** mode, then the journal file may at this point actually be larger
40658         ** than Pager.journalOff bytes. If the next thing in the journal
40659         ** file happens to be a journal-header (written as part of the
40660         ** previous connection's transaction), and a crash or power-failure
40661         ** occurs after nRec is updated but before this connection writes
40662         ** anything else to the journal file (or commits/rolls back its
40663         ** transaction), then SQLite may become confused when doing the
40664         ** hot-journal rollback following recovery. It may roll back all
40665         ** of this connections data, then proceed to rolling back the old,
40666         ** out-of-date data that follows it. Database corruption.
40667         **
40668         ** To work around this, if the journal file does appear to contain
40669         ** a valid header following Pager.journalOff, then write a 0x00
40670         ** byte to the start of it to prevent it from being recognized.
40671         **
40672         ** Variable iNextHdrOffset is set to the offset at which this
40673         ** problematic header will occur, if it exists. aMagic is used
40674         ** as a temporary buffer to inspect the first couple of bytes of
40675         ** the potential journal header.
40676         */
40677         i64 iNextHdrOffset;
40678         u8 aMagic[8];
40679         u8 zHeader[sizeof(aJournalMagic)+4];
40680 
40681         memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic));
40682         put32bits(&zHeader[sizeof(aJournalMagic)], pPager->nRec);
40683 
40684         iNextHdrOffset = journalHdrOffset(pPager);
40685         rc = sqlite3OsRead(pPager->jfd, aMagic, 8, iNextHdrOffset);
40686         if( rc==SQLITE_OK && 0==memcmp(aMagic, aJournalMagic, 8) ){
40687           static const u8 zerobyte = 0;
40688           rc = sqlite3OsWrite(pPager->jfd, &zerobyte, 1, iNextHdrOffset);
40689         }
40690         if( rc!=SQLITE_OK && rc!=SQLITE_IOERR_SHORT_READ ){
40691           return rc;
40692         }
40693 
40694         /* Write the nRec value into the journal file header. If in
40695         ** full-synchronous mode, sync the journal first. This ensures that
40696         ** all data has really hit the disk before nRec is updated to mark
40697         ** it as a candidate for rollback.
40698         **
40699         ** This is not required if the persistent media supports the
40700         ** SAFE_APPEND property. Because in this case it is not possible
40701         ** for garbage data to be appended to the file, the nRec field
40702         ** is populated with 0xFFFFFFFF when the journal header is written
40703         ** and never needs to be updated.
40704         */
40705         if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
40706           PAGERTRACE(("SYNC journal of %d\n", PAGERID(pPager)));
40707           IOTRACE(("JSYNC %p\n", pPager))
40708           rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags);
40709           if( rc!=SQLITE_OK ) return rc;
40710         }
40711         IOTRACE(("JHDR %p %lld\n", pPager, pPager->journalHdr));
40712         rc = sqlite3OsWrite(
40713             pPager->jfd, zHeader, sizeof(zHeader), pPager->journalHdr
40714         );
40715         if( rc!=SQLITE_OK ) return rc;
40716       }
40717       if( 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
40718         PAGERTRACE(("SYNC journal of %d\n", PAGERID(pPager)));
40719         IOTRACE(("JSYNC %p\n", pPager))
40720         rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags|
40721           (pPager->syncFlags==SQLITE_SYNC_FULL?SQLITE_SYNC_DATAONLY:0)
40722         );
40723         if( rc!=SQLITE_OK ) return rc;
40724       }
40725 
40726       pPager->journalHdr = pPager->journalOff;
40727       if( newHdr && 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){
40728         pPager->nRec = 0;
40729         rc = writeJournalHdr(pPager);
40730         if( rc!=SQLITE_OK ) return rc;
40731       }
40732     }else{
40733       pPager->journalHdr = pPager->journalOff;
40734     }
40735   }
40736 
40737   /* Unless the pager is in noSync mode, the journal file was just
40738   ** successfully synced. Either way, clear the PGHDR_NEED_SYNC flag on
40739   ** all pages.
40740   */
40741   sqlite3PcacheClearSyncFlags(pPager->pPCache);
40742   pPager->eState = PAGER_WRITER_DBMOD;
40743   assert( assert_pager_state(pPager) );
40744   return SQLITE_OK;
40745 }
40746 
40747 /*
40748 ** The argument is the first in a linked list of dirty pages connected
40749 ** by the PgHdr.pDirty pointer. This function writes each one of the
40750 ** in-memory pages in the list to the database file. The argument may
40751 ** be NULL, representing an empty list. In this case this function is
40752 ** a no-op.
40753 **
40754 ** The pager must hold at least a RESERVED lock when this function
40755 ** is called. Before writing anything to the database file, this lock
40756 ** is upgraded to an EXCLUSIVE lock. If the lock cannot be obtained,
40757 ** SQLITE_BUSY is returned and no data is written to the database file.
40758 **
40759 ** If the pager is a temp-file pager and the actual file-system file
40760 ** is not yet open, it is created and opened before any data is
40761 ** written out.
40762 **
40763 ** Once the lock has been upgraded and, if necessary, the file opened,
40764 ** the pages are written out to the database file in list order. Writing
40765 ** a page is skipped if it meets either of the following criteria:
40766 **
40767 **   * The page number is greater than Pager.dbSize, or
40768 **   * The PGHDR_DONT_WRITE flag is set on the page.
40769 **
40770 ** If writing out a page causes the database file to grow, Pager.dbFileSize
40771 ** is updated accordingly. If page 1 is written out, then the value cached
40772 ** in Pager.dbFileVers[] is updated to match the new value stored in
40773 ** the database file.
40774 **
40775 ** If everything is successful, SQLITE_OK is returned. If an IO error
40776 ** occurs, an IO error code is returned. Or, if the EXCLUSIVE lock cannot
40777 ** be obtained, SQLITE_BUSY is returned.
40778 */
40779 static int pager_write_pagelist(Pager *pPager, PgHdr *pList){
40780   int rc = SQLITE_OK;                  /* Return code */
40781 
40782   /* This function is only called for rollback pagers in WRITER_DBMOD state. */
40783   assert( !pagerUseWal(pPager) );
40784   assert( pPager->eState==PAGER_WRITER_DBMOD );
40785   assert( pPager->eLock==EXCLUSIVE_LOCK );
40786 
40787   /* If the file is a temp-file has not yet been opened, open it now. It
40788   ** is not possible for rc to be other than SQLITE_OK if this branch
40789   ** is taken, as pager_wait_on_lock() is a no-op for temp-files.
40790   */
40791   if( !isOpen(pPager->fd) ){
40792     assert( pPager->tempFile && rc==SQLITE_OK );
40793     rc = pagerOpentemp(pPager, pPager->fd, pPager->vfsFlags);
40794   }
40795 
40796   /* Before the first write, give the VFS a hint of what the final
40797   ** file size will be.
40798   */
40799   assert( rc!=SQLITE_OK || isOpen(pPager->fd) );
40800   if( rc==SQLITE_OK && pPager->dbSize>pPager->dbHintSize ){
40801     sqlite3_int64 szFile = pPager->pageSize * (sqlite3_int64)pPager->dbSize;
40802     sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_SIZE_HINT, &szFile);
40803     pPager->dbHintSize = pPager->dbSize;
40804   }
40805 
40806   while( rc==SQLITE_OK && pList ){
40807     Pgno pgno = pList->pgno;
40808 
40809     /* If there are dirty pages in the page cache with page numbers greater
40810     ** than Pager.dbSize, this means sqlite3PagerTruncateImage() was called to
40811     ** make the file smaller (presumably by auto-vacuum code). Do not write
40812     ** any such pages to the file.
40813     **
40814     ** Also, do not write out any page that has the PGHDR_DONT_WRITE flag
40815     ** set (set by sqlite3PagerDontWrite()).
40816     */
40817     if( pgno<=pPager->dbSize && 0==(pList->flags&PGHDR_DONT_WRITE) ){
40818       i64 offset = (pgno-1)*(i64)pPager->pageSize;   /* Offset to write */
40819       char *pData;                                   /* Data to write */
40820 
40821       assert( (pList->flags&PGHDR_NEED_SYNC)==0 );
40822       if( pList->pgno==1 ) pager_write_changecounter(pList);
40823 
40824       /* Encode the database */
40825       CODEC2(pPager, pList->pData, pgno, 6, return SQLITE_NOMEM, pData);
40826 
40827       /* Write out the page data. */
40828       rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize, offset);
40829 
40830       /* If page 1 was just written, update Pager.dbFileVers to match
40831       ** the value now stored in the database file. If writing this
40832       ** page caused the database file to grow, update dbFileSize.
40833       */
40834       if( pgno==1 ){
40835         memcpy(&pPager->dbFileVers, &pData[24], sizeof(pPager->dbFileVers));
40836       }
40837       if( pgno>pPager->dbFileSize ){
40838         pPager->dbFileSize = pgno;
40839       }
40840 
40841       /* Update any backup objects copying the contents of this pager. */
40842       sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)pList->pData);
40843 
40844       PAGERTRACE(("STORE %d page %d hash(%08x)\n",
40845                    PAGERID(pPager), pgno, pager_pagehash(pList)));
40846       IOTRACE(("PGOUT %p %d\n", pPager, pgno));
40847       PAGER_INCR(sqlite3_pager_writedb_count);
40848       PAGER_INCR(pPager->nWrite);
40849     }else{
40850       PAGERTRACE(("NOSTORE %d page %d\n", PAGERID(pPager), pgno));
40851     }
40852     pager_set_pagehash(pList);
40853     pList = pList->pDirty;
40854   }
40855 
40856   return rc;
40857 }
40858 
40859 /*
40860 ** Ensure that the sub-journal file is open. If it is already open, this
40861 ** function is a no-op.
40862 **
40863 ** SQLITE_OK is returned if everything goes according to plan. An
40864 ** SQLITE_IOERR_XXX error code is returned if a call to sqlite3OsOpen()
40865 ** fails.
40866 */
40867 static int openSubJournal(Pager *pPager){
40868   int rc = SQLITE_OK;
40869   if( !isOpen(pPager->sjfd) ){
40870     if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY || pPager->subjInMemory ){
40871       sqlite3MemJournalOpen(pPager->sjfd);
40872     }else{
40873       rc = pagerOpentemp(pPager, pPager->sjfd, SQLITE_OPEN_SUBJOURNAL);
40874     }
40875   }
40876   return rc;
40877 }
40878 
40879 /*
40880 ** Append a record of the current state of page pPg to the sub-journal.
40881 ** It is the callers responsibility to use subjRequiresPage() to check
40882 ** that it is really required before calling this function.
40883 **
40884 ** If successful, set the bit corresponding to pPg->pgno in the bitvecs
40885 ** for all open savepoints before returning.
40886 **
40887 ** This function returns SQLITE_OK if everything is successful, an IO
40888 ** error code if the attempt to write to the sub-journal fails, or
40889 ** SQLITE_NOMEM if a malloc fails while setting a bit in a savepoint
40890 ** bitvec.
40891 */
40892 static int subjournalPage(PgHdr *pPg){
40893   int rc = SQLITE_OK;
40894   Pager *pPager = pPg->pPager;
40895   if( pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
40896 
40897     /* Open the sub-journal, if it has not already been opened */
40898     assert( pPager->useJournal );
40899     assert( isOpen(pPager->jfd) || pagerUseWal(pPager) );
40900     assert( isOpen(pPager->sjfd) || pPager->nSubRec==0 );
40901     assert( pagerUseWal(pPager)
40902          || pageInJournal(pPg)
40903          || pPg->pgno>pPager->dbOrigSize
40904     );
40905     rc = openSubJournal(pPager);
40906 
40907     /* If the sub-journal was opened successfully (or was already open),
40908     ** write the journal record into the file.  */
40909     if( rc==SQLITE_OK ){
40910       void *pData = pPg->pData;
40911       i64 offset = pPager->nSubRec*(4+pPager->pageSize);
40912       char *pData2;
40913 
40914       CODEC2(pPager, pData, pPg->pgno, 7, return SQLITE_NOMEM, pData2);
40915       PAGERTRACE(("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno));
40916       rc = write32bits(pPager->sjfd, offset, pPg->pgno);
40917       if( rc==SQLITE_OK ){
40918         rc = sqlite3OsWrite(pPager->sjfd, pData2, pPager->pageSize, offset+4);
40919       }
40920     }
40921   }
40922   if( rc==SQLITE_OK ){
40923     pPager->nSubRec++;
40924     assert( pPager->nSavepoint>0 );
40925     rc = addToSavepointBitvecs(pPager, pPg->pgno);
40926   }
40927   return rc;
40928 }
40929 
40930 /*
40931 ** This function is called by the pcache layer when it has reached some
40932 ** soft memory limit. The first argument is a pointer to a Pager object
40933 ** (cast as a void*). The pager is always 'purgeable' (not an in-memory
40934 ** database). The second argument is a reference to a page that is
40935 ** currently dirty but has no outstanding references. The page
40936 ** is always associated with the Pager object passed as the first
40937 ** argument.
40938 **
40939 ** The job of this function is to make pPg clean by writing its contents
40940 ** out to the database file, if possible. This may involve syncing the
40941 ** journal file.
40942 **
40943 ** If successful, sqlite3PcacheMakeClean() is called on the page and
40944 ** SQLITE_OK returned. If an IO error occurs while trying to make the
40945 ** page clean, the IO error code is returned. If the page cannot be
40946 ** made clean for some other reason, but no error occurs, then SQLITE_OK
40947 ** is returned by sqlite3PcacheMakeClean() is not called.
40948 */
40949 static int pagerStress(void *p, PgHdr *pPg){
40950   Pager *pPager = (Pager *)p;
40951   int rc = SQLITE_OK;
40952 
40953   assert( pPg->pPager==pPager );
40954   assert( pPg->flags&PGHDR_DIRTY );
40955 
40956   /* The doNotSyncSpill flag is set during times when doing a sync of
40957   ** journal (and adding a new header) is not allowed.  This occurs
40958   ** during calls to sqlite3PagerWrite() while trying to journal multiple
40959   ** pages belonging to the same sector.
40960   **
40961   ** The doNotSpill flag inhibits all cache spilling regardless of whether
40962   ** or not a sync is required.  This is set during a rollback.
40963   **
40964   ** Spilling is also prohibited when in an error state since that could
40965   ** lead to database corruption.   In the current implementaton it
40966   ** is impossible for sqlite3PCacheFetch() to be called with createFlag==1
40967   ** while in the error state, hence it is impossible for this routine to
40968   ** be called in the error state.  Nevertheless, we include a NEVER()
40969   ** test for the error state as a safeguard against future changes.
40970   */
40971   if( NEVER(pPager->errCode) ) return SQLITE_OK;
40972   if( pPager->doNotSpill ) return SQLITE_OK;
40973   if( pPager->doNotSyncSpill && (pPg->flags & PGHDR_NEED_SYNC)!=0 ){
40974     return SQLITE_OK;
40975   }
40976 
40977   pPg->pDirty = 0;
40978   if( pagerUseWal(pPager) ){
40979     /* Write a single frame for this page to the log. */
40980     if( subjRequiresPage(pPg) ){
40981       rc = subjournalPage(pPg);
40982     }
40983     if( rc==SQLITE_OK ){
40984       rc = pagerWalFrames(pPager, pPg, 0, 0, 0);
40985     }
40986   }else{
40987 
40988     /* Sync the journal file if required. */
40989     if( pPg->flags&PGHDR_NEED_SYNC
40990      || pPager->eState==PAGER_WRITER_CACHEMOD
40991     ){
40992       rc = syncJournal(pPager, 1);
40993     }
40994 
40995     /* If the page number of this page is larger than the current size of
40996     ** the database image, it may need to be written to the sub-journal.
40997     ** This is because the call to pager_write_pagelist() below will not
40998     ** actually write data to the file in this case.
40999     **
41000     ** Consider the following sequence of events:
41001     **
41002     **   BEGIN;
41003     **     <journal page X>
41004     **     <modify page X>
41005     **     SAVEPOINT sp;
41006     **       <shrink database file to Y pages>
41007     **       pagerStress(page X)
41008     **     ROLLBACK TO sp;
41009     **
41010     ** If (X>Y), then when pagerStress is called page X will not be written
41011     ** out to the database file, but will be dropped from the cache. Then,
41012     ** following the "ROLLBACK TO sp" statement, reading page X will read
41013     ** data from the database file. This will be the copy of page X as it
41014     ** was when the transaction started, not as it was when "SAVEPOINT sp"
41015     ** was executed.
41016     **
41017     ** The solution is to write the current data for page X into the
41018     ** sub-journal file now (if it is not already there), so that it will
41019     ** be restored to its current value when the "ROLLBACK TO sp" is
41020     ** executed.
41021     */
41022     if( NEVER(
41023         rc==SQLITE_OK && pPg->pgno>pPager->dbSize && subjRequiresPage(pPg)
41024     ) ){
41025       rc = subjournalPage(pPg);
41026     }
41027 
41028     /* Write the contents of the page out to the database file. */
41029     if( rc==SQLITE_OK ){
41030       assert( (pPg->flags&PGHDR_NEED_SYNC)==0 );
41031       rc = pager_write_pagelist(pPager, pPg);
41032     }
41033   }
41034 
41035   /* Mark the page as clean. */
41036   if( rc==SQLITE_OK ){
41037     PAGERTRACE(("STRESS %d page %d\n", PAGERID(pPager), pPg->pgno));
41038     sqlite3PcacheMakeClean(pPg);
41039   }
41040 
41041   return pager_error(pPager, rc);
41042 }
41043 
41044 
41045 /*
41046 ** Allocate and initialize a new Pager object and put a pointer to it
41047 ** in *ppPager. The pager should eventually be freed by passing it
41048 ** to sqlite3PagerClose().
41049 **
41050 ** The zFilename argument is the path to the database file to open.
41051 ** If zFilename is NULL then a randomly-named temporary file is created
41052 ** and used as the file to be cached. Temporary files are be deleted
41053 ** automatically when they are closed. If zFilename is ":memory:" then
41054 ** all information is held in cache. It is never written to disk.
41055 ** This can be used to implement an in-memory database.
41056 **
41057 ** The nExtra parameter specifies the number of bytes of space allocated
41058 ** along with each page reference. This space is available to the user
41059 ** via the sqlite3PagerGetExtra() API.
41060 **
41061 ** The flags argument is used to specify properties that affect the
41062 ** operation of the pager. It should be passed some bitwise combination
41063 ** of the PAGER_OMIT_JOURNAL and PAGER_NO_READLOCK flags.
41064 **
41065 ** The vfsFlags parameter is a bitmask to pass to the flags parameter
41066 ** of the xOpen() method of the supplied VFS when opening files.
41067 **
41068 ** If the pager object is allocated and the specified file opened
41069 ** successfully, SQLITE_OK is returned and *ppPager set to point to
41070 ** the new pager object. If an error occurs, *ppPager is set to NULL
41071 ** and error code returned. This function may return SQLITE_NOMEM
41072 ** (sqlite3Malloc() is used to allocate memory), SQLITE_CANTOPEN or
41073 ** various SQLITE_IO_XXX errors.
41074 */
41075 SQLITE_PRIVATE int sqlite3PagerOpen(
41076   sqlite3_vfs *pVfs,       /* The virtual file system to use */
41077   Pager **ppPager,         /* OUT: Return the Pager structure here */
41078   const char *zFilename,   /* Name of the database file to open */
41079   int nExtra,              /* Extra bytes append to each in-memory page */
41080   int flags,               /* flags controlling this file */
41081   int vfsFlags,            /* flags passed through to sqlite3_vfs.xOpen() */
41082   void (*xReinit)(DbPage*) /* Function to reinitialize pages */
41083 ){
41084   u8 *pPtr;
41085   Pager *pPager = 0;       /* Pager object to allocate and return */
41086   int rc = SQLITE_OK;      /* Return code */
41087   int tempFile = 0;        /* True for temp files (incl. in-memory files) */
41088   int memDb = 0;           /* True if this is an in-memory file */
41089   int readOnly = 0;        /* True if this is a read-only file */
41090   int journalFileSize;     /* Bytes to allocate for each journal fd */
41091   char *zPathname = 0;     /* Full path to database file */
41092   int nPathname = 0;       /* Number of bytes in zPathname */
41093   int useJournal = (flags & PAGER_OMIT_JOURNAL)==0; /* False to omit journal */
41094   int noReadlock = (flags & PAGER_NO_READLOCK)!=0;  /* True to omit read-lock */
41095   int pcacheSize = sqlite3PcacheSize();       /* Bytes to allocate for PCache */
41096   u32 szPageDflt = SQLITE_DEFAULT_PAGE_SIZE;  /* Default page size */
41097   const char *zUri = 0;    /* URI args to copy */
41098   int nUri = 0;            /* Number of bytes of URI args at *zUri */
41099 
41100   /* Figure out how much space is required for each journal file-handle
41101   ** (there are two of them, the main journal and the sub-journal). This
41102   ** is the maximum space required for an in-memory journal file handle
41103   ** and a regular journal file-handle. Note that a "regular journal-handle"
41104   ** may be a wrapper capable of caching the first portion of the journal
41105   ** file in memory to implement the atomic-write optimization (see
41106   ** source file journal.c).
41107   */
41108   if( sqlite3JournalSize(pVfs)>sqlite3MemJournalSize() ){
41109     journalFileSize = ROUND8(sqlite3JournalSize(pVfs));
41110   }else{
41111     journalFileSize = ROUND8(sqlite3MemJournalSize());
41112   }
41113 
41114   /* Set the output variable to NULL in case an error occurs. */
41115   *ppPager = 0;
41116 
41117 #ifndef SQLITE_OMIT_MEMORYDB
41118   if( flags & PAGER_MEMORY ){
41119     memDb = 1;
41120     zFilename = 0;
41121   }
41122 #endif
41123 
41124   /* Compute and store the full pathname in an allocated buffer pointed
41125   ** to by zPathname, length nPathname. Or, if this is a temporary file,
41126   ** leave both nPathname and zPathname set to 0.
41127   */
41128   if( zFilename && zFilename[0] ){
41129     const char *z;
41130     nPathname = pVfs->mxPathname+1;
41131     zPathname = sqlite3Malloc(nPathname*2);
41132     if( zPathname==0 ){
41133       return SQLITE_NOMEM;
41134     }
41135     zPathname[0] = 0; /* Make sure initialized even if FullPathname() fails */
41136     rc = sqlite3OsFullPathname(pVfs, zFilename, nPathname, zPathname);
41137     nPathname = sqlite3Strlen30(zPathname);
41138     z = zUri = &zFilename[sqlite3Strlen30(zFilename)+1];
41139     while( *z ){
41140       z += sqlite3Strlen30(z)+1;
41141       z += sqlite3Strlen30(z)+1;
41142     }
41143     nUri = &z[1] - zUri;
41144     if( rc==SQLITE_OK && nPathname+8>pVfs->mxPathname ){
41145       /* This branch is taken when the journal path required by
41146       ** the database being opened will be more than pVfs->mxPathname
41147       ** bytes in length. This means the database cannot be opened,
41148       ** as it will not be possible to open the journal file or even
41149       ** check for a hot-journal before reading.
41150       */
41151       rc = SQLITE_CANTOPEN_BKPT;
41152     }
41153     if( rc!=SQLITE_OK ){
41154       sqlite3_free(zPathname);
41155       return rc;
41156     }
41157   }
41158 
41159   /* Allocate memory for the Pager structure, PCache object, the
41160   ** three file descriptors, the database file name and the journal
41161   ** file name. The layout in memory is as follows:
41162   **
41163   **     Pager object                    (sizeof(Pager) bytes)
41164   **     PCache object                   (sqlite3PcacheSize() bytes)
41165   **     Database file handle            (pVfs->szOsFile bytes)
41166   **     Sub-journal file handle         (journalFileSize bytes)
41167   **     Main journal file handle        (journalFileSize bytes)
41168   **     Database file name              (nPathname+1 bytes)
41169   **     Journal file name               (nPathname+8+1 bytes)
41170   */
41171   pPtr = (u8 *)sqlite3MallocZero(
41172     ROUND8(sizeof(*pPager)) +      /* Pager structure */
41173     ROUND8(pcacheSize) +           /* PCache object */
41174     ROUND8(pVfs->szOsFile) +       /* The main db file */
41175     journalFileSize * 2 +          /* The two journal files */
41176     nPathname + 1 + nUri +         /* zFilename */
41177     nPathname + 8 + 1              /* zJournal */
41178 #ifndef SQLITE_OMIT_WAL
41179     + nPathname + 4 + 1              /* zWal */
41180 #endif
41181   );
41182   assert( EIGHT_BYTE_ALIGNMENT(SQLITE_INT_TO_PTR(journalFileSize)) );
41183   if( !pPtr ){
41184     sqlite3_free(zPathname);
41185     return SQLITE_NOMEM;
41186   }
41187   pPager =              (Pager*)(pPtr);
41188   pPager->pPCache =    (PCache*)(pPtr += ROUND8(sizeof(*pPager)));
41189   pPager->fd =   (sqlite3_file*)(pPtr += ROUND8(pcacheSize));
41190   pPager->sjfd = (sqlite3_file*)(pPtr += ROUND8(pVfs->szOsFile));
41191   pPager->jfd =  (sqlite3_file*)(pPtr += journalFileSize);
41192   pPager->zFilename =    (char*)(pPtr += journalFileSize);
41193   assert( EIGHT_BYTE_ALIGNMENT(pPager->jfd) );
41194 
41195   /* Fill in the Pager.zFilename and Pager.zJournal buffers, if required. */
41196   if( zPathname ){
41197     assert( nPathname>0 );
41198     pPager->zJournal =   (char*)(pPtr += nPathname + 1 + nUri);
41199     memcpy(pPager->zFilename, zPathname, nPathname);
41200     memcpy(&pPager->zFilename[nPathname+1], zUri, nUri);
41201     memcpy(pPager->zJournal, zPathname, nPathname);
41202     memcpy(&pPager->zJournal[nPathname], "-journal", 8);
41203     sqlite3FileSuffix3(pPager->zFilename, pPager->zJournal);
41204 #ifndef SQLITE_OMIT_WAL
41205     pPager->zWal = &pPager->zJournal[nPathname+8+1];
41206     memcpy(pPager->zWal, zPathname, nPathname);
41207     memcpy(&pPager->zWal[nPathname], "-wal", 4);
41208     sqlite3FileSuffix3(pPager->zFilename, pPager->zWal);
41209 #endif
41210     sqlite3_free(zPathname);
41211   }
41212   pPager->pVfs = pVfs;
41213   pPager->vfsFlags = vfsFlags;
41214 
41215   /* Open the pager file.
41216   */
41217   if( zFilename && zFilename[0] ){
41218     int fout = 0;                    /* VFS flags returned by xOpen() */
41219     rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd, vfsFlags, &fout);
41220     assert( !memDb );
41221     readOnly = (fout&SQLITE_OPEN_READONLY);
41222 
41223     /* If the file was successfully opened for read/write access,
41224     ** choose a default page size in case we have to create the
41225     ** database file. The default page size is the maximum of:
41226     **
41227     **    + SQLITE_DEFAULT_PAGE_SIZE,
41228     **    + The value returned by sqlite3OsSectorSize()
41229     **    + The largest page size that can be written atomically.
41230     */
41231     if( rc==SQLITE_OK && !readOnly ){
41232       setSectorSize(pPager);
41233       assert(SQLITE_DEFAULT_PAGE_SIZE<=SQLITE_MAX_DEFAULT_PAGE_SIZE);
41234       if( szPageDflt<pPager->sectorSize ){
41235         if( pPager->sectorSize>SQLITE_MAX_DEFAULT_PAGE_SIZE ){
41236           szPageDflt = SQLITE_MAX_DEFAULT_PAGE_SIZE;
41237         }else{
41238           szPageDflt = (u32)pPager->sectorSize;
41239         }
41240       }
41241 #ifdef SQLITE_ENABLE_ATOMIC_WRITE
41242       {
41243         int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
41244         int ii;
41245         assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
41246         assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
41247         assert(SQLITE_MAX_DEFAULT_PAGE_SIZE<=65536);
41248         for(ii=szPageDflt; ii<=SQLITE_MAX_DEFAULT_PAGE_SIZE; ii=ii*2){
41249           if( iDc&(SQLITE_IOCAP_ATOMIC|(ii>>8)) ){
41250             szPageDflt = ii;
41251           }
41252         }
41253       }
41254 #endif
41255     }
41256   }else{
41257     /* If a temporary file is requested, it is not opened immediately.
41258     ** In this case we accept the default page size and delay actually
41259     ** opening the file until the first call to OsWrite().
41260     **
41261     ** This branch is also run for an in-memory database. An in-memory
41262     ** database is the same as a temp-file that is never written out to
41263     ** disk and uses an in-memory rollback journal.
41264     */
41265     tempFile = 1;
41266     pPager->eState = PAGER_READER;
41267     pPager->eLock = EXCLUSIVE_LOCK;
41268     readOnly = (vfsFlags&SQLITE_OPEN_READONLY);
41269   }
41270 
41271   /* The following call to PagerSetPagesize() serves to set the value of
41272   ** Pager.pageSize and to allocate the Pager.pTmpSpace buffer.
41273   */
41274   if( rc==SQLITE_OK ){
41275     assert( pPager->memDb==0 );
41276     rc = sqlite3PagerSetPagesize(pPager, &szPageDflt, -1);
41277     testcase( rc!=SQLITE_OK );
41278   }
41279 
41280   /* If an error occurred in either of the blocks above, free the
41281   ** Pager structure and close the file.
41282   */
41283   if( rc!=SQLITE_OK ){
41284     assert( !pPager->pTmpSpace );
41285     sqlite3OsClose(pPager->fd);
41286     sqlite3_free(pPager);
41287     return rc;
41288   }
41289 
41290   /* Initialize the PCache object. */
41291   assert( nExtra<1000 );
41292   nExtra = ROUND8(nExtra);
41293   sqlite3PcacheOpen(szPageDflt, nExtra, !memDb,
41294                     !memDb?pagerStress:0, (void *)pPager, pPager->pPCache);
41295 
41296   PAGERTRACE(("OPEN %d %s\n", FILEHANDLEID(pPager->fd), pPager->zFilename));
41297   IOTRACE(("OPEN %p %s\n", pPager, pPager->zFilename))
41298 
41299   pPager->useJournal = (u8)useJournal;
41300   pPager->noReadlock = (noReadlock && readOnly) ?1:0;
41301   /* pPager->stmtOpen = 0; */
41302   /* pPager->stmtInUse = 0; */
41303   /* pPager->nRef = 0; */
41304   /* pPager->stmtSize = 0; */
41305   /* pPager->stmtJSize = 0; */
41306   /* pPager->nPage = 0; */
41307   pPager->mxPgno = SQLITE_MAX_PAGE_COUNT;
41308   /* pPager->state = PAGER_UNLOCK; */
41309 #if 0
41310   assert( pPager->state == (tempFile ? PAGER_EXCLUSIVE : PAGER_UNLOCK) );
41311 #endif
41312   /* pPager->errMask = 0; */
41313   pPager->tempFile = (u8)tempFile;
41314   assert( tempFile==PAGER_LOCKINGMODE_NORMAL
41315           || tempFile==PAGER_LOCKINGMODE_EXCLUSIVE );
41316   assert( PAGER_LOCKINGMODE_EXCLUSIVE==1 );
41317   pPager->exclusiveMode = (u8)tempFile;
41318   pPager->changeCountDone = pPager->tempFile;
41319   pPager->memDb = (u8)memDb;
41320   pPager->readOnly = (u8)readOnly;
41321   assert( useJournal || pPager->tempFile );
41322   pPager->noSync = pPager->tempFile;
41323   pPager->fullSync = pPager->noSync ?0:1;
41324   pPager->syncFlags = pPager->noSync ? 0 : SQLITE_SYNC_NORMAL;
41325   pPager->ckptSyncFlags = pPager->syncFlags;
41326   /* pPager->pFirst = 0; */
41327   /* pPager->pFirstSynced = 0; */
41328   /* pPager->pLast = 0; */
41329   pPager->nExtra = (u16)nExtra;
41330   pPager->journalSizeLimit = SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT;
41331   assert( isOpen(pPager->fd) || tempFile );
41332   setSectorSize(pPager);
41333   if( !useJournal ){
41334     pPager->journalMode = PAGER_JOURNALMODE_OFF;
41335   }else if( memDb ){
41336     pPager->journalMode = PAGER_JOURNALMODE_MEMORY;
41337   }
41338   /* pPager->xBusyHandler = 0; */
41339   /* pPager->pBusyHandlerArg = 0; */
41340   pPager->xReiniter = xReinit;
41341   /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
41342 
41343   *ppPager = pPager;
41344   return SQLITE_OK;
41345 }
41346 
41347 
41348 
41349 /*
41350 ** This function is called after transitioning from PAGER_UNLOCK to
41351 ** PAGER_SHARED state. It tests if there is a hot journal present in
41352 ** the file-system for the given pager. A hot journal is one that
41353 ** needs to be played back. According to this function, a hot-journal
41354 ** file exists if the following criteria are met:
41355 **
41356 **   * The journal file exists in the file system, and
41357 **   * No process holds a RESERVED or greater lock on the database file, and
41358 **   * The database file itself is greater than 0 bytes in size, and
41359 **   * The first byte of the journal file exists and is not 0x00.
41360 **
41361 ** If the current size of the database file is 0 but a journal file
41362 ** exists, that is probably an old journal left over from a prior
41363 ** database with the same name. In this case the journal file is
41364 ** just deleted using OsDelete, *pExists is set to 0 and SQLITE_OK
41365 ** is returned.
41366 **
41367 ** This routine does not check if there is a master journal filename
41368 ** at the end of the file. If there is, and that master journal file
41369 ** does not exist, then the journal file is not really hot. In this
41370 ** case this routine will return a false-positive. The pager_playback()
41371 ** routine will discover that the journal file is not really hot and
41372 ** will not roll it back.
41373 **
41374 ** If a hot-journal file is found to exist, *pExists is set to 1 and
41375 ** SQLITE_OK returned. If no hot-journal file is present, *pExists is
41376 ** set to 0 and SQLITE_OK returned. If an IO error occurs while trying
41377 ** to determine whether or not a hot-journal file exists, the IO error
41378 ** code is returned and the value of *pExists is undefined.
41379 */
41380 static int hasHotJournal(Pager *pPager, int *pExists){
41381   sqlite3_vfs * const pVfs = pPager->pVfs;
41382   int rc = SQLITE_OK;           /* Return code */
41383   int exists = 1;               /* True if a journal file is present */
41384   int jrnlOpen = !!isOpen(pPager->jfd);
41385 
41386   assert( pPager->useJournal );
41387   assert( isOpen(pPager->fd) );
41388   assert( pPager->eState==PAGER_OPEN );
41389 
41390   assert( jrnlOpen==0 || ( sqlite3OsDeviceCharacteristics(pPager->jfd) &
41391     SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN
41392   ));
41393 
41394   *pExists = 0;
41395   if( !jrnlOpen ){
41396     rc = sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS, &exists);
41397   }
41398   if( rc==SQLITE_OK && exists ){
41399     int locked = 0;             /* True if some process holds a RESERVED lock */
41400 
41401     /* Race condition here:  Another process might have been holding the
41402     ** the RESERVED lock and have a journal open at the sqlite3OsAccess()
41403     ** call above, but then delete the journal and drop the lock before
41404     ** we get to the following sqlite3OsCheckReservedLock() call.  If that
41405     ** is the case, this routine might think there is a hot journal when
41406     ** in fact there is none.  This results in a false-positive which will
41407     ** be dealt with by the playback routine.  Ticket #3883.
41408     */
41409     rc = sqlite3OsCheckReservedLock(pPager->fd, &locked);
41410     if( rc==SQLITE_OK && !locked ){
41411       Pgno nPage;                 /* Number of pages in database file */
41412 
41413       /* Check the size of the database file. If it consists of 0 pages,
41414       ** then delete the journal file. See the header comment above for
41415       ** the reasoning here.  Delete the obsolete journal file under
41416       ** a RESERVED lock to avoid race conditions and to avoid violating
41417       ** [H33020].
41418       */
41419       rc = pagerPagecount(pPager, &nPage);
41420       if( rc==SQLITE_OK ){
41421         if( nPage==0 ){
41422           sqlite3BeginBenignMalloc();
41423           if( pagerLockDb(pPager, RESERVED_LOCK)==SQLITE_OK ){
41424             sqlite3OsDelete(pVfs, pPager->zJournal, 0);
41425             if( !pPager->exclusiveMode ) pagerUnlockDb(pPager, SHARED_LOCK);
41426           }
41427           sqlite3EndBenignMalloc();
41428         }else{
41429           /* The journal file exists and no other connection has a reserved
41430           ** or greater lock on the database file. Now check that there is
41431           ** at least one non-zero bytes at the start of the journal file.
41432           ** If there is, then we consider this journal to be hot. If not,
41433           ** it can be ignored.
41434           */
41435           if( !jrnlOpen ){
41436             int f = SQLITE_OPEN_READONLY|SQLITE_OPEN_MAIN_JOURNAL;
41437             rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &f);
41438           }
41439           if( rc==SQLITE_OK ){
41440             u8 first = 0;
41441             rc = sqlite3OsRead(pPager->jfd, (void *)&first, 1, 0);
41442             if( rc==SQLITE_IOERR_SHORT_READ ){
41443               rc = SQLITE_OK;
41444             }
41445             if( !jrnlOpen ){
41446               sqlite3OsClose(pPager->jfd);
41447             }
41448             *pExists = (first!=0);
41449           }else if( rc==SQLITE_CANTOPEN ){
41450             /* If we cannot open the rollback journal file in order to see if
41451             ** its has a zero header, that might be due to an I/O error, or
41452             ** it might be due to the race condition described above and in
41453             ** ticket #3883.  Either way, assume that the journal is hot.
41454             ** This might be a false positive.  But if it is, then the
41455             ** automatic journal playback and recovery mechanism will deal
41456             ** with it under an EXCLUSIVE lock where we do not need to
41457             ** worry so much with race conditions.
41458             */
41459             *pExists = 1;
41460             rc = SQLITE_OK;
41461           }
41462         }
41463       }
41464     }
41465   }
41466 
41467   return rc;
41468 }
41469 
41470 /*
41471 ** This function is called to obtain a shared lock on the database file.
41472 ** It is illegal to call sqlite3PagerAcquire() until after this function
41473 ** has been successfully called. If a shared-lock is already held when
41474 ** this function is called, it is a no-op.
41475 **
41476 ** The following operations are also performed by this function.
41477 **
41478 **   1) If the pager is currently in PAGER_OPEN state (no lock held
41479 **      on the database file), then an attempt is made to obtain a
41480 **      SHARED lock on the database file. Immediately after obtaining
41481 **      the SHARED lock, the file-system is checked for a hot-journal,
41482 **      which is played back if present. Following any hot-journal
41483 **      rollback, the contents of the cache are validated by checking
41484 **      the 'change-counter' field of the database file header and
41485 **      discarded if they are found to be invalid.
41486 **
41487 **   2) If the pager is running in exclusive-mode, and there are currently
41488 **      no outstanding references to any pages, and is in the error state,
41489 **      then an attempt is made to clear the error state by discarding
41490 **      the contents of the page cache and rolling back any open journal
41491 **      file.
41492 **
41493 ** If everything is successful, SQLITE_OK is returned. If an IO error
41494 ** occurs while locking the database, checking for a hot-journal file or
41495 ** rolling back a journal file, the IO error code is returned.
41496 */
41497 SQLITE_PRIVATE int sqlite3PagerSharedLock(Pager *pPager){
41498   int rc = SQLITE_OK;                /* Return code */
41499 
41500   /* This routine is only called from b-tree and only when there are no
41501   ** outstanding pages. This implies that the pager state should either
41502   ** be OPEN or READER. READER is only possible if the pager is or was in
41503   ** exclusive access mode.
41504   */
41505   assert( sqlite3PcacheRefCount(pPager->pPCache)==0 );
41506   assert( assert_pager_state(pPager) );
41507   assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER );
41508   if( NEVER(MEMDB && pPager->errCode) ){ return pPager->errCode; }
41509 
41510   if( !pagerUseWal(pPager) && pPager->eState==PAGER_OPEN ){
41511     int bHotJournal = 1;          /* True if there exists a hot journal-file */
41512 
41513     assert( !MEMDB );
41514     assert( pPager->noReadlock==0 || pPager->readOnly );
41515 
41516     if( pPager->noReadlock==0 ){
41517       rc = pager_wait_on_lock(pPager, SHARED_LOCK);
41518       if( rc!=SQLITE_OK ){
41519         assert( pPager->eLock==NO_LOCK || pPager->eLock==UNKNOWN_LOCK );
41520         goto failed;
41521       }
41522     }
41523 
41524     /* If a journal file exists, and there is no RESERVED lock on the
41525     ** database file, then it either needs to be played back or deleted.
41526     */
41527     if( pPager->eLock<=SHARED_LOCK ){
41528       rc = hasHotJournal(pPager, &bHotJournal);
41529     }
41530     if( rc!=SQLITE_OK ){
41531       goto failed;
41532     }
41533     if( bHotJournal ){
41534       /* Get an EXCLUSIVE lock on the database file. At this point it is
41535       ** important that a RESERVED lock is not obtained on the way to the
41536       ** EXCLUSIVE lock. If it were, another process might open the
41537       ** database file, detect the RESERVED lock, and conclude that the
41538       ** database is safe to read while this process is still rolling the
41539       ** hot-journal back.
41540       **
41541       ** Because the intermediate RESERVED lock is not requested, any
41542       ** other process attempting to access the database file will get to
41543       ** this point in the code and fail to obtain its own EXCLUSIVE lock
41544       ** on the database file.
41545       **
41546       ** Unless the pager is in locking_mode=exclusive mode, the lock is
41547       ** downgraded to SHARED_LOCK before this function returns.
41548       */
41549       rc = pagerLockDb(pPager, EXCLUSIVE_LOCK);
41550       if( rc!=SQLITE_OK ){
41551         goto failed;
41552       }
41553 
41554       /* If it is not already open and the file exists on disk, open the
41555       ** journal for read/write access. Write access is required because
41556       ** in exclusive-access mode the file descriptor will be kept open
41557       ** and possibly used for a transaction later on. Also, write-access
41558       ** is usually required to finalize the journal in journal_mode=persist
41559       ** mode (and also for journal_mode=truncate on some systems).
41560       **
41561       ** If the journal does not exist, it usually means that some
41562       ** other connection managed to get in and roll it back before
41563       ** this connection obtained the exclusive lock above. Or, it
41564       ** may mean that the pager was in the error-state when this
41565       ** function was called and the journal file does not exist.
41566       */
41567       if( !isOpen(pPager->jfd) ){
41568         sqlite3_vfs * const pVfs = pPager->pVfs;
41569         int bExists;              /* True if journal file exists */
41570         rc = sqlite3OsAccess(
41571             pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS, &bExists);
41572         if( rc==SQLITE_OK && bExists ){
41573           int fout = 0;
41574           int f = SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_JOURNAL;
41575           assert( !pPager->tempFile );
41576           rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &fout);
41577           assert( rc!=SQLITE_OK || isOpen(pPager->jfd) );
41578           if( rc==SQLITE_OK && fout&SQLITE_OPEN_READONLY ){
41579             rc = SQLITE_CANTOPEN_BKPT;
41580             sqlite3OsClose(pPager->jfd);
41581           }
41582         }
41583       }
41584 
41585       /* Playback and delete the journal.  Drop the database write
41586       ** lock and reacquire the read lock. Purge the cache before
41587       ** playing back the hot-journal so that we don't end up with
41588       ** an inconsistent cache.  Sync the hot journal before playing
41589       ** it back since the process that crashed and left the hot journal
41590       ** probably did not sync it and we are required to always sync
41591       ** the journal before playing it back.
41592       */
41593       if( isOpen(pPager->jfd) ){
41594         assert( rc==SQLITE_OK );
41595         rc = pagerSyncHotJournal(pPager);
41596         if( rc==SQLITE_OK ){
41597           rc = pager_playback(pPager, 1);
41598           pPager->eState = PAGER_OPEN;
41599         }
41600       }else if( !pPager->exclusiveMode ){
41601         pagerUnlockDb(pPager, SHARED_LOCK);
41602       }
41603 
41604       if( rc!=SQLITE_OK ){
41605         /* This branch is taken if an error occurs while trying to open
41606         ** or roll back a hot-journal while holding an EXCLUSIVE lock. The
41607         ** pager_unlock() routine will be called before returning to unlock
41608         ** the file. If the unlock attempt fails, then Pager.eLock must be
41609         ** set to UNKNOWN_LOCK (see the comment above the #define for
41610         ** UNKNOWN_LOCK above for an explanation).
41611         **
41612         ** In order to get pager_unlock() to do this, set Pager.eState to
41613         ** PAGER_ERROR now. This is not actually counted as a transition
41614         ** to ERROR state in the state diagram at the top of this file,
41615         ** since we know that the same call to pager_unlock() will very
41616         ** shortly transition the pager object to the OPEN state. Calling
41617         ** assert_pager_state() would fail now, as it should not be possible
41618         ** to be in ERROR state when there are zero outstanding page
41619         ** references.
41620         */
41621         pager_error(pPager, rc);
41622         goto failed;
41623       }
41624 
41625       assert( pPager->eState==PAGER_OPEN );
41626       assert( (pPager->eLock==SHARED_LOCK)
41627            || (pPager->exclusiveMode && pPager->eLock>SHARED_LOCK)
41628       );
41629     }
41630 
41631     if( !pPager->tempFile
41632      && (pPager->pBackup || sqlite3PcachePagecount(pPager->pPCache)>0)
41633     ){
41634       /* The shared-lock has just been acquired on the database file
41635       ** and there are already pages in the cache (from a previous
41636       ** read or write transaction).  Check to see if the database
41637       ** has been modified.  If the database has changed, flush the
41638       ** cache.
41639       **
41640       ** Database changes is detected by looking at 15 bytes beginning
41641       ** at offset 24 into the file.  The first 4 of these 16 bytes are
41642       ** a 32-bit counter that is incremented with each change.  The
41643       ** other bytes change randomly with each file change when
41644       ** a codec is in use.
41645       **
41646       ** There is a vanishingly small chance that a change will not be
41647       ** detected.  The chance of an undetected change is so small that
41648       ** it can be neglected.
41649       */
41650       Pgno nPage = 0;
41651       char dbFileVers[sizeof(pPager->dbFileVers)];
41652 
41653       rc = pagerPagecount(pPager, &nPage);
41654       if( rc ) goto failed;
41655 
41656       if( nPage>0 ){
41657         IOTRACE(("CKVERS %p %d\n", pPager, sizeof(dbFileVers)));
41658         rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24);
41659         if( rc!=SQLITE_OK ){
41660           goto failed;
41661         }
41662       }else{
41663         memset(dbFileVers, 0, sizeof(dbFileVers));
41664       }
41665 
41666       if( memcmp(pPager->dbFileVers, dbFileVers, sizeof(dbFileVers))!=0 ){
41667         pager_reset(pPager);
41668       }
41669     }
41670 
41671     /* If there is a WAL file in the file-system, open this database in WAL
41672     ** mode. Otherwise, the following function call is a no-op.
41673     */
41674     rc = pagerOpenWalIfPresent(pPager);
41675 #ifndef SQLITE_OMIT_WAL
41676     assert( pPager->pWal==0 || rc==SQLITE_OK );
41677 #endif
41678   }
41679 
41680   if( pagerUseWal(pPager) ){
41681     assert( rc==SQLITE_OK );
41682     rc = pagerBeginReadTransaction(pPager);
41683   }
41684 
41685   if( pPager->eState==PAGER_OPEN && rc==SQLITE_OK ){
41686     rc = pagerPagecount(pPager, &pPager->dbSize);
41687   }
41688 
41689  failed:
41690   if( rc!=SQLITE_OK ){
41691     assert( !MEMDB );
41692     pager_unlock(pPager);
41693     assert( pPager->eState==PAGER_OPEN );
41694   }else{
41695     pPager->eState = PAGER_READER;
41696   }
41697   return rc;
41698 }
41699 
41700 /*
41701 ** If the reference count has reached zero, rollback any active
41702 ** transaction and unlock the pager.
41703 **
41704 ** Except, in locking_mode=EXCLUSIVE when there is nothing to in
41705 ** the rollback journal, the unlock is not performed and there is
41706 ** nothing to rollback, so this routine is a no-op.
41707 */
41708 static void pagerUnlockIfUnused(Pager *pPager){
41709   if( (sqlite3PcacheRefCount(pPager->pPCache)==0) ){
41710     pagerUnlockAndRollback(pPager);
41711   }
41712 }
41713 
41714 /*
41715 ** Acquire a reference to page number pgno in pager pPager (a page
41716 ** reference has type DbPage*). If the requested reference is
41717 ** successfully obtained, it is copied to *ppPage and SQLITE_OK returned.
41718 **
41719 ** If the requested page is already in the cache, it is returned.
41720 ** Otherwise, a new page object is allocated and populated with data
41721 ** read from the database file. In some cases, the pcache module may
41722 ** choose not to allocate a new page object and may reuse an existing
41723 ** object with no outstanding references.
41724 **
41725 ** The extra data appended to a page is always initialized to zeros the
41726 ** first time a page is loaded into memory. If the page requested is
41727 ** already in the cache when this function is called, then the extra
41728 ** data is left as it was when the page object was last used.
41729 **
41730 ** If the database image is smaller than the requested page or if a
41731 ** non-zero value is passed as the noContent parameter and the
41732 ** requested page is not already stored in the cache, then no
41733 ** actual disk read occurs. In this case the memory image of the
41734 ** page is initialized to all zeros.
41735 **
41736 ** If noContent is true, it means that we do not care about the contents
41737 ** of the page. This occurs in two seperate scenarios:
41738 **
41739 **   a) When reading a free-list leaf page from the database, and
41740 **
41741 **   b) When a savepoint is being rolled back and we need to load
41742 **      a new page into the cache to be filled with the data read
41743 **      from the savepoint journal.
41744 **
41745 ** If noContent is true, then the data returned is zeroed instead of
41746 ** being read from the database. Additionally, the bits corresponding
41747 ** to pgno in Pager.pInJournal (bitvec of pages already written to the
41748 ** journal file) and the PagerSavepoint.pInSavepoint bitvecs of any open
41749 ** savepoints are set. This means if the page is made writable at any
41750 ** point in the future, using a call to sqlite3PagerWrite(), its contents
41751 ** will not be journaled. This saves IO.
41752 **
41753 ** The acquisition might fail for several reasons.  In all cases,
41754 ** an appropriate error code is returned and *ppPage is set to NULL.
41755 **
41756 ** See also sqlite3PagerLookup().  Both this routine and Lookup() attempt
41757 ** to find a page in the in-memory cache first.  If the page is not already
41758 ** in memory, this routine goes to disk to read it in whereas Lookup()
41759 ** just returns 0.  This routine acquires a read-lock the first time it
41760 ** has to go to disk, and could also playback an old journal if necessary.
41761 ** Since Lookup() never goes to disk, it never has to deal with locks
41762 ** or journal files.
41763 */
41764 SQLITE_PRIVATE int sqlite3PagerAcquire(
41765   Pager *pPager,      /* The pager open on the database file */
41766   Pgno pgno,          /* Page number to fetch */
41767   DbPage **ppPage,    /* Write a pointer to the page here */
41768   int noContent       /* Do not bother reading content from disk if true */
41769 ){
41770   int rc;
41771   PgHdr *pPg;
41772 
41773   assert( pPager->eState>=PAGER_READER );
41774   assert( assert_pager_state(pPager) );
41775 
41776   if( pgno==0 ){
41777     return SQLITE_CORRUPT_BKPT;
41778   }
41779 
41780   /* If the pager is in the error state, return an error immediately.
41781   ** Otherwise, request the page from the PCache layer. */
41782   if( pPager->errCode!=SQLITE_OK ){
41783     rc = pPager->errCode;
41784   }else{
41785     rc = sqlite3PcacheFetch(pPager->pPCache, pgno, 1, ppPage);
41786   }
41787 
41788   if( rc!=SQLITE_OK ){
41789     /* Either the call to sqlite3PcacheFetch() returned an error or the
41790     ** pager was already in the error-state when this function was called.
41791     ** Set pPg to 0 and jump to the exception handler.  */
41792     pPg = 0;
41793     goto pager_acquire_err;
41794   }
41795   assert( (*ppPage)->pgno==pgno );
41796   assert( (*ppPage)->pPager==pPager || (*ppPage)->pPager==0 );
41797 
41798   if( (*ppPage)->pPager && !noContent ){
41799     /* In this case the pcache already contains an initialized copy of
41800     ** the page. Return without further ado.  */
41801     assert( pgno<=PAGER_MAX_PGNO && pgno!=PAGER_MJ_PGNO(pPager) );
41802     PAGER_INCR(pPager->nHit);
41803     return SQLITE_OK;
41804 
41805   }else{
41806     /* The pager cache has created a new page. Its content needs to
41807     ** be initialized.  */
41808 
41809     PAGER_INCR(pPager->nMiss);
41810     pPg = *ppPage;
41811     pPg->pPager = pPager;
41812 
41813     /* The maximum page number is 2^31. Return SQLITE_CORRUPT if a page
41814     ** number greater than this, or the unused locking-page, is requested. */
41815     if( pgno>PAGER_MAX_PGNO || pgno==PAGER_MJ_PGNO(pPager) ){
41816       rc = SQLITE_CORRUPT_BKPT;
41817       goto pager_acquire_err;
41818     }
41819 
41820     if( MEMDB || pPager->dbSize<pgno || noContent || !isOpen(pPager->fd) ){
41821       if( pgno>pPager->mxPgno ){
41822         rc = SQLITE_FULL;
41823         goto pager_acquire_err;
41824       }
41825       if( noContent ){
41826         /* Failure to set the bits in the InJournal bit-vectors is benign.
41827         ** It merely means that we might do some extra work to journal a
41828         ** page that does not need to be journaled.  Nevertheless, be sure
41829         ** to test the case where a malloc error occurs while trying to set
41830         ** a bit in a bit vector.
41831         */
41832         sqlite3BeginBenignMalloc();
41833         if( pgno<=pPager->dbOrigSize ){
41834           TESTONLY( rc = ) sqlite3BitvecSet(pPager->pInJournal, pgno);
41835           testcase( rc==SQLITE_NOMEM );
41836         }
41837         TESTONLY( rc = ) addToSavepointBitvecs(pPager, pgno);
41838         testcase( rc==SQLITE_NOMEM );
41839         sqlite3EndBenignMalloc();
41840       }
41841       memset(pPg->pData, 0, pPager->pageSize);
41842       IOTRACE(("ZERO %p %d\n", pPager, pgno));
41843     }else{
41844       assert( pPg->pPager==pPager );
41845       rc = readDbPage(pPg);
41846       if( rc!=SQLITE_OK ){
41847         goto pager_acquire_err;
41848       }
41849     }
41850     pager_set_pagehash(pPg);
41851   }
41852 
41853   return SQLITE_OK;
41854 
41855 pager_acquire_err:
41856   assert( rc!=SQLITE_OK );
41857   if( pPg ){
41858     sqlite3PcacheDrop(pPg);
41859   }
41860   pagerUnlockIfUnused(pPager);
41861 
41862   *ppPage = 0;
41863   return rc;
41864 }
41865 
41866 /*
41867 ** Acquire a page if it is already in the in-memory cache.  Do
41868 ** not read the page from disk.  Return a pointer to the page,
41869 ** or 0 if the page is not in cache.
41870 **
41871 ** See also sqlite3PagerGet().  The difference between this routine
41872 ** and sqlite3PagerGet() is that _get() will go to the disk and read
41873 ** in the page if the page is not already in cache.  This routine
41874 ** returns NULL if the page is not in cache or if a disk I/O error
41875 ** has ever happened.
41876 */
41877 SQLITE_PRIVATE DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno){
41878   PgHdr *pPg = 0;
41879   assert( pPager!=0 );
41880   assert( pgno!=0 );
41881   assert( pPager->pPCache!=0 );
41882   assert( pPager->eState>=PAGER_READER && pPager->eState!=PAGER_ERROR );
41883   sqlite3PcacheFetch(pPager->pPCache, pgno, 0, &pPg);
41884   return pPg;
41885 }
41886 
41887 /*
41888 ** Release a page reference.
41889 **
41890 ** If the number of references to the page drop to zero, then the
41891 ** page is added to the LRU list.  When all references to all pages
41892 ** are released, a rollback occurs and the lock on the database is
41893 ** removed.
41894 */
41895 SQLITE_PRIVATE void sqlite3PagerUnref(DbPage *pPg){
41896   if( pPg ){
41897     Pager *pPager = pPg->pPager;
41898     sqlite3PcacheRelease(pPg);
41899     pagerUnlockIfUnused(pPager);
41900   }
41901 }
41902 
41903 /*
41904 ** This function is called at the start of every write transaction.
41905 ** There must already be a RESERVED or EXCLUSIVE lock on the database
41906 ** file when this routine is called.
41907 **
41908 ** Open the journal file for pager pPager and write a journal header
41909 ** to the start of it. If there are active savepoints, open the sub-journal
41910 ** as well. This function is only used when the journal file is being
41911 ** opened to write a rollback log for a transaction. It is not used
41912 ** when opening a hot journal file to roll it back.
41913 **
41914 ** If the journal file is already open (as it may be in exclusive mode),
41915 ** then this function just writes a journal header to the start of the
41916 ** already open file.
41917 **
41918 ** Whether or not the journal file is opened by this function, the
41919 ** Pager.pInJournal bitvec structure is allocated.
41920 **
41921 ** Return SQLITE_OK if everything is successful. Otherwise, return
41922 ** SQLITE_NOMEM if the attempt to allocate Pager.pInJournal fails, or
41923 ** an IO error code if opening or writing the journal file fails.
41924 */
41925 static int pager_open_journal(Pager *pPager){
41926   int rc = SQLITE_OK;                        /* Return code */
41927   sqlite3_vfs * const pVfs = pPager->pVfs;   /* Local cache of vfs pointer */
41928 
41929   assert( pPager->eState==PAGER_WRITER_LOCKED );
41930   assert( assert_pager_state(pPager) );
41931   assert( pPager->pInJournal==0 );
41932 
41933   /* If already in the error state, this function is a no-op.  But on
41934   ** the other hand, this routine is never called if we are already in
41935   ** an error state. */
41936   if( NEVER(pPager->errCode) ) return pPager->errCode;
41937 
41938   if( !pagerUseWal(pPager) && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
41939     pPager->pInJournal = sqlite3BitvecCreate(pPager->dbSize);
41940     if( pPager->pInJournal==0 ){
41941       return SQLITE_NOMEM;
41942     }
41943 
41944     /* Open the journal file if it is not already open. */
41945     if( !isOpen(pPager->jfd) ){
41946       if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ){
41947         sqlite3MemJournalOpen(pPager->jfd);
41948       }else{
41949         const int flags =                   /* VFS flags to open journal file */
41950           SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|
41951           (pPager->tempFile ?
41952             (SQLITE_OPEN_DELETEONCLOSE|SQLITE_OPEN_TEMP_JOURNAL):
41953             (SQLITE_OPEN_MAIN_JOURNAL)
41954           );
41955   #ifdef SQLITE_ENABLE_ATOMIC_WRITE
41956         rc = sqlite3JournalOpen(
41957             pVfs, pPager->zJournal, pPager->jfd, flags, jrnlBufferSize(pPager)
41958         );
41959   #else
41960         rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, flags, 0);
41961   #endif
41962       }
41963       assert( rc!=SQLITE_OK || isOpen(pPager->jfd) );
41964     }
41965 
41966 
41967     /* Write the first journal header to the journal file and open
41968     ** the sub-journal if necessary.
41969     */
41970     if( rc==SQLITE_OK ){
41971       /* TODO: Check if all of these are really required. */
41972       pPager->nRec = 0;
41973       pPager->journalOff = 0;
41974       pPager->setMaster = 0;
41975       pPager->journalHdr = 0;
41976       rc = writeJournalHdr(pPager);
41977     }
41978   }
41979 
41980   if( rc!=SQLITE_OK ){
41981     sqlite3BitvecDestroy(pPager->pInJournal);
41982     pPager->pInJournal = 0;
41983   }else{
41984     assert( pPager->eState==PAGER_WRITER_LOCKED );
41985     pPager->eState = PAGER_WRITER_CACHEMOD;
41986   }
41987 
41988   return rc;
41989 }
41990 
41991 /*
41992 ** Begin a write-transaction on the specified pager object. If a
41993 ** write-transaction has already been opened, this function is a no-op.
41994 **
41995 ** If the exFlag argument is false, then acquire at least a RESERVED
41996 ** lock on the database file. If exFlag is true, then acquire at least
41997 ** an EXCLUSIVE lock. If such a lock is already held, no locking
41998 ** functions need be called.
41999 **
42000 ** If the subjInMemory argument is non-zero, then any sub-journal opened
42001 ** within this transaction will be opened as an in-memory file. This
42002 ** has no effect if the sub-journal is already opened (as it may be when
42003 ** running in exclusive mode) or if the transaction does not require a
42004 ** sub-journal. If the subjInMemory argument is zero, then any required
42005 ** sub-journal is implemented in-memory if pPager is an in-memory database,
42006 ** or using a temporary file otherwise.
42007 */
42008 SQLITE_PRIVATE int sqlite3PagerBegin(Pager *pPager, int exFlag, int subjInMemory){
42009   int rc = SQLITE_OK;
42010 
42011   if( pPager->errCode ) return pPager->errCode;
42012   assert( pPager->eState>=PAGER_READER && pPager->eState<PAGER_ERROR );
42013   pPager->subjInMemory = (u8)subjInMemory;
42014 
42015   if( ALWAYS(pPager->eState==PAGER_READER) ){
42016     assert( pPager->pInJournal==0 );
42017 
42018     if( pagerUseWal(pPager) ){
42019       /* If the pager is configured to use locking_mode=exclusive, and an
42020       ** exclusive lock on the database is not already held, obtain it now.
42021       */
42022       if( pPager->exclusiveMode && sqlite3WalExclusiveMode(pPager->pWal, -1) ){
42023         rc = pagerLockDb(pPager, EXCLUSIVE_LOCK);
42024         if( rc!=SQLITE_OK ){
42025           return rc;
42026         }
42027         sqlite3WalExclusiveMode(pPager->pWal, 1);
42028       }
42029 
42030       /* Grab the write lock on the log file. If successful, upgrade to
42031       ** PAGER_RESERVED state. Otherwise, return an error code to the caller.
42032       ** The busy-handler is not invoked if another connection already
42033       ** holds the write-lock. If possible, the upper layer will call it.
42034       */
42035       rc = sqlite3WalBeginWriteTransaction(pPager->pWal);
42036     }else{
42037       /* Obtain a RESERVED lock on the database file. If the exFlag parameter
42038       ** is true, then immediately upgrade this to an EXCLUSIVE lock. The
42039       ** busy-handler callback can be used when upgrading to the EXCLUSIVE
42040       ** lock, but not when obtaining the RESERVED lock.
42041       */
42042       rc = pagerLockDb(pPager, RESERVED_LOCK);
42043       if( rc==SQLITE_OK && exFlag ){
42044         rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
42045       }
42046     }
42047 
42048     if( rc==SQLITE_OK ){
42049       /* Change to WRITER_LOCKED state.
42050       **
42051       ** WAL mode sets Pager.eState to PAGER_WRITER_LOCKED or CACHEMOD
42052       ** when it has an open transaction, but never to DBMOD or FINISHED.
42053       ** This is because in those states the code to roll back savepoint
42054       ** transactions may copy data from the sub-journal into the database
42055       ** file as well as into the page cache. Which would be incorrect in
42056       ** WAL mode.
42057       */
42058       pPager->eState = PAGER_WRITER_LOCKED;
42059       pPager->dbHintSize = pPager->dbSize;
42060       pPager->dbFileSize = pPager->dbSize;
42061       pPager->dbOrigSize = pPager->dbSize;
42062       pPager->journalOff = 0;
42063     }
42064 
42065     assert( rc==SQLITE_OK || pPager->eState==PAGER_READER );
42066     assert( rc!=SQLITE_OK || pPager->eState==PAGER_WRITER_LOCKED );
42067     assert( assert_pager_state(pPager) );
42068   }
42069 
42070   PAGERTRACE(("TRANSACTION %d\n", PAGERID(pPager)));
42071   return rc;
42072 }
42073 
42074 /*
42075 ** Mark a single data page as writeable. The page is written into the
42076 ** main journal or sub-journal as required. If the page is written into
42077 ** one of the journals, the corresponding bit is set in the
42078 ** Pager.pInJournal bitvec and the PagerSavepoint.pInSavepoint bitvecs
42079 ** of any open savepoints as appropriate.
42080 */
42081 static int pager_write(PgHdr *pPg){
42082   void *pData = pPg->pData;
42083   Pager *pPager = pPg->pPager;
42084   int rc = SQLITE_OK;
42085 
42086   /* This routine is not called unless a write-transaction has already
42087   ** been started. The journal file may or may not be open at this point.
42088   ** It is never called in the ERROR state.
42089   */
42090   assert( pPager->eState==PAGER_WRITER_LOCKED
42091        || pPager->eState==PAGER_WRITER_CACHEMOD
42092        || pPager->eState==PAGER_WRITER_DBMOD
42093   );
42094   assert( assert_pager_state(pPager) );
42095 
42096   /* If an error has been previously detected, report the same error
42097   ** again. This should not happen, but the check provides robustness. */
42098   if( NEVER(pPager->errCode) )  return pPager->errCode;
42099 
42100   /* Higher-level routines never call this function if database is not
42101   ** writable.  But check anyway, just for robustness. */
42102   if( NEVER(pPager->readOnly) ) return SQLITE_PERM;
42103 
42104   CHECK_PAGE(pPg);
42105 
42106   /* The journal file needs to be opened. Higher level routines have already
42107   ** obtained the necessary locks to begin the write-transaction, but the
42108   ** rollback journal might not yet be open. Open it now if this is the case.
42109   **
42110   ** This is done before calling sqlite3PcacheMakeDirty() on the page.
42111   ** Otherwise, if it were done after calling sqlite3PcacheMakeDirty(), then
42112   ** an error might occur and the pager would end up in WRITER_LOCKED state
42113   ** with pages marked as dirty in the cache.
42114   */
42115   if( pPager->eState==PAGER_WRITER_LOCKED ){
42116     rc = pager_open_journal(pPager);
42117     if( rc!=SQLITE_OK ) return rc;
42118   }
42119   assert( pPager->eState>=PAGER_WRITER_CACHEMOD );
42120   assert( assert_pager_state(pPager) );
42121 
42122   /* Mark the page as dirty.  If the page has already been written
42123   ** to the journal then we can return right away.
42124   */
42125   sqlite3PcacheMakeDirty(pPg);
42126   if( pageInJournal(pPg) && !subjRequiresPage(pPg) ){
42127     assert( !pagerUseWal(pPager) );
42128   }else{
42129 
42130     /* The transaction journal now exists and we have a RESERVED or an
42131     ** EXCLUSIVE lock on the main database file.  Write the current page to
42132     ** the transaction journal if it is not there already.
42133     */
42134     if( !pageInJournal(pPg) && !pagerUseWal(pPager) ){
42135       assert( pagerUseWal(pPager)==0 );
42136       if( pPg->pgno<=pPager->dbOrigSize && isOpen(pPager->jfd) ){
42137         u32 cksum;
42138         char *pData2;
42139         i64 iOff = pPager->journalOff;
42140 
42141         /* We should never write to the journal file the page that
42142         ** contains the database locks.  The following assert verifies
42143         ** that we do not. */
42144         assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) );
42145 
42146         assert( pPager->journalHdr<=pPager->journalOff );
42147         CODEC2(pPager, pData, pPg->pgno, 7, return SQLITE_NOMEM, pData2);
42148         cksum = pager_cksum(pPager, (u8*)pData2);
42149 
42150         /* Even if an IO or diskfull error occurs while journalling the
42151         ** page in the block above, set the need-sync flag for the page.
42152         ** Otherwise, when the transaction is rolled back, the logic in
42153         ** playback_one_page() will think that the page needs to be restored
42154         ** in the database file. And if an IO error occurs while doing so,
42155         ** then corruption may follow.
42156         */
42157         pPg->flags |= PGHDR_NEED_SYNC;
42158 
42159         rc = write32bits(pPager->jfd, iOff, pPg->pgno);
42160         if( rc!=SQLITE_OK ) return rc;
42161         rc = sqlite3OsWrite(pPager->jfd, pData2, pPager->pageSize, iOff+4);
42162         if( rc!=SQLITE_OK ) return rc;
42163         rc = write32bits(pPager->jfd, iOff+pPager->pageSize+4, cksum);
42164         if( rc!=SQLITE_OK ) return rc;
42165 
42166         IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno,
42167                  pPager->journalOff, pPager->pageSize));
42168         PAGER_INCR(sqlite3_pager_writej_count);
42169         PAGERTRACE(("JOURNAL %d page %d needSync=%d hash(%08x)\n",
42170              PAGERID(pPager), pPg->pgno,
42171              ((pPg->flags&PGHDR_NEED_SYNC)?1:0), pager_pagehash(pPg)));
42172 
42173         pPager->journalOff += 8 + pPager->pageSize;
42174         pPager->nRec++;
42175         assert( pPager->pInJournal!=0 );
42176         rc = sqlite3BitvecSet(pPager->pInJournal, pPg->pgno);
42177         testcase( rc==SQLITE_NOMEM );
42178         assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
42179         rc |= addToSavepointBitvecs(pPager, pPg->pgno);
42180         if( rc!=SQLITE_OK ){
42181           assert( rc==SQLITE_NOMEM );
42182           return rc;
42183         }
42184       }else{
42185         if( pPager->eState!=PAGER_WRITER_DBMOD ){
42186           pPg->flags |= PGHDR_NEED_SYNC;
42187         }
42188         PAGERTRACE(("APPEND %d page %d needSync=%d\n",
42189                 PAGERID(pPager), pPg->pgno,
42190                ((pPg->flags&PGHDR_NEED_SYNC)?1:0)));
42191       }
42192     }
42193 
42194     /* If the statement journal is open and the page is not in it,
42195     ** then write the current page to the statement journal.  Note that
42196     ** the statement journal format differs from the standard journal format
42197     ** in that it omits the checksums and the header.
42198     */
42199     if( subjRequiresPage(pPg) ){
42200       rc = subjournalPage(pPg);
42201     }
42202   }
42203 
42204   /* Update the database size and return.
42205   */
42206   if( pPager->dbSize<pPg->pgno ){
42207     pPager->dbSize = pPg->pgno;
42208   }
42209   return rc;
42210 }
42211 
42212 /*
42213 ** Mark a data page as writeable. This routine must be called before
42214 ** making changes to a page. The caller must check the return value
42215 ** of this function and be careful not to change any page data unless
42216 ** this routine returns SQLITE_OK.
42217 **
42218 ** The difference between this function and pager_write() is that this
42219 ** function also deals with the special case where 2 or more pages
42220 ** fit on a single disk sector. In this case all co-resident pages
42221 ** must have been written to the journal file before returning.
42222 **
42223 ** If an error occurs, SQLITE_NOMEM or an IO error code is returned
42224 ** as appropriate. Otherwise, SQLITE_OK.
42225 */
42226 SQLITE_PRIVATE int sqlite3PagerWrite(DbPage *pDbPage){
42227   int rc = SQLITE_OK;
42228 
42229   PgHdr *pPg = pDbPage;
42230   Pager *pPager = pPg->pPager;
42231   Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
42232 
42233   assert( pPager->eState>=PAGER_WRITER_LOCKED );
42234   assert( pPager->eState!=PAGER_ERROR );
42235   assert( assert_pager_state(pPager) );
42236 
42237   if( nPagePerSector>1 ){
42238     Pgno nPageCount;          /* Total number of pages in database file */
42239     Pgno pg1;                 /* First page of the sector pPg is located on. */
42240     int nPage = 0;            /* Number of pages starting at pg1 to journal */
42241     int ii;                   /* Loop counter */
42242     int needSync = 0;         /* True if any page has PGHDR_NEED_SYNC */
42243 
42244     /* Set the doNotSyncSpill flag to 1. This is because we cannot allow
42245     ** a journal header to be written between the pages journaled by
42246     ** this function.
42247     */
42248     assert( !MEMDB );
42249     assert( pPager->doNotSyncSpill==0 );
42250     pPager->doNotSyncSpill++;
42251 
42252     /* This trick assumes that both the page-size and sector-size are
42253     ** an integer power of 2. It sets variable pg1 to the identifier
42254     ** of the first page of the sector pPg is located on.
42255     */
42256     pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1;
42257 
42258     nPageCount = pPager->dbSize;
42259     if( pPg->pgno>nPageCount ){
42260       nPage = (pPg->pgno - pg1)+1;
42261     }else if( (pg1+nPagePerSector-1)>nPageCount ){
42262       nPage = nPageCount+1-pg1;
42263     }else{
42264       nPage = nPagePerSector;
42265     }
42266     assert(nPage>0);
42267     assert(pg1<=pPg->pgno);
42268     assert((pg1+nPage)>pPg->pgno);
42269 
42270     for(ii=0; ii<nPage && rc==SQLITE_OK; ii++){
42271       Pgno pg = pg1+ii;
42272       PgHdr *pPage;
42273       if( pg==pPg->pgno || !sqlite3BitvecTest(pPager->pInJournal, pg) ){
42274         if( pg!=PAGER_MJ_PGNO(pPager) ){
42275           rc = sqlite3PagerGet(pPager, pg, &pPage);
42276           if( rc==SQLITE_OK ){
42277             rc = pager_write(pPage);
42278             if( pPage->flags&PGHDR_NEED_SYNC ){
42279               needSync = 1;
42280             }
42281             sqlite3PagerUnref(pPage);
42282           }
42283         }
42284       }else if( (pPage = pager_lookup(pPager, pg))!=0 ){
42285         if( pPage->flags&PGHDR_NEED_SYNC ){
42286           needSync = 1;
42287         }
42288         sqlite3PagerUnref(pPage);
42289       }
42290     }
42291 
42292     /* If the PGHDR_NEED_SYNC flag is set for any of the nPage pages
42293     ** starting at pg1, then it needs to be set for all of them. Because
42294     ** writing to any of these nPage pages may damage the others, the
42295     ** journal file must contain sync()ed copies of all of them
42296     ** before any of them can be written out to the database file.
42297     */
42298     if( rc==SQLITE_OK && needSync ){
42299       assert( !MEMDB );
42300       for(ii=0; ii<nPage; ii++){
42301         PgHdr *pPage = pager_lookup(pPager, pg1+ii);
42302         if( pPage ){
42303           pPage->flags |= PGHDR_NEED_SYNC;
42304           sqlite3PagerUnref(pPage);
42305         }
42306       }
42307     }
42308 
42309     assert( pPager->doNotSyncSpill==1 );
42310     pPager->doNotSyncSpill--;
42311   }else{
42312     rc = pager_write(pDbPage);
42313   }
42314   return rc;
42315 }
42316 
42317 /*
42318 ** Return TRUE if the page given in the argument was previously passed
42319 ** to sqlite3PagerWrite().  In other words, return TRUE if it is ok
42320 ** to change the content of the page.
42321 */
42322 #ifndef NDEBUG
42323 SQLITE_PRIVATE int sqlite3PagerIswriteable(DbPage *pPg){
42324   return pPg->flags&PGHDR_DIRTY;
42325 }
42326 #endif
42327 
42328 /*
42329 ** A call to this routine tells the pager that it is not necessary to
42330 ** write the information on page pPg back to the disk, even though
42331 ** that page might be marked as dirty.  This happens, for example, when
42332 ** the page has been added as a leaf of the freelist and so its
42333 ** content no longer matters.
42334 **
42335 ** The overlying software layer calls this routine when all of the data
42336 ** on the given page is unused. The pager marks the page as clean so
42337 ** that it does not get written to disk.
42338 **
42339 ** Tests show that this optimization can quadruple the speed of large
42340 ** DELETE operations.
42341 */
42342 SQLITE_PRIVATE void sqlite3PagerDontWrite(PgHdr *pPg){
42343   Pager *pPager = pPg->pPager;
42344   if( (pPg->flags&PGHDR_DIRTY) && pPager->nSavepoint==0 ){
42345     PAGERTRACE(("DONT_WRITE page %d of %d\n", pPg->pgno, PAGERID(pPager)));
42346     IOTRACE(("CLEAN %p %d\n", pPager, pPg->pgno))
42347     pPg->flags |= PGHDR_DONT_WRITE;
42348     pager_set_pagehash(pPg);
42349   }
42350 }
42351 
42352 /*
42353 ** This routine is called to increment the value of the database file
42354 ** change-counter, stored as a 4-byte big-endian integer starting at
42355 ** byte offset 24 of the pager file.  The secondary change counter at
42356 ** 92 is also updated, as is the SQLite version number at offset 96.
42357 **
42358 ** But this only happens if the pPager->changeCountDone flag is false.
42359 ** To avoid excess churning of page 1, the update only happens once.
42360 ** See also the pager_write_changecounter() routine that does an
42361 ** unconditional update of the change counters.
42362 **
42363 ** If the isDirectMode flag is zero, then this is done by calling
42364 ** sqlite3PagerWrite() on page 1, then modifying the contents of the
42365 ** page data. In this case the file will be updated when the current
42366 ** transaction is committed.
42367 **
42368 ** The isDirectMode flag may only be non-zero if the library was compiled
42369 ** with the SQLITE_ENABLE_ATOMIC_WRITE macro defined. In this case,
42370 ** if isDirect is non-zero, then the database file is updated directly
42371 ** by writing an updated version of page 1 using a call to the
42372 ** sqlite3OsWrite() function.
42373 */
42374 static int pager_incr_changecounter(Pager *pPager, int isDirectMode){
42375   int rc = SQLITE_OK;
42376 
42377   assert( pPager->eState==PAGER_WRITER_CACHEMOD
42378        || pPager->eState==PAGER_WRITER_DBMOD
42379   );
42380   assert( assert_pager_state(pPager) );
42381 
42382   /* Declare and initialize constant integer 'isDirect'. If the
42383   ** atomic-write optimization is enabled in this build, then isDirect
42384   ** is initialized to the value passed as the isDirectMode parameter
42385   ** to this function. Otherwise, it is always set to zero.
42386   **
42387   ** The idea is that if the atomic-write optimization is not
42388   ** enabled at compile time, the compiler can omit the tests of
42389   ** 'isDirect' below, as well as the block enclosed in the
42390   ** "if( isDirect )" condition.
42391   */
42392 #ifndef SQLITE_ENABLE_ATOMIC_WRITE
42393 # define DIRECT_MODE 0
42394   assert( isDirectMode==0 );
42395   UNUSED_PARAMETER(isDirectMode);
42396 #else
42397 # define DIRECT_MODE isDirectMode
42398 #endif
42399 
42400   if( !pPager->changeCountDone && pPager->dbSize>0 ){
42401     PgHdr *pPgHdr;                /* Reference to page 1 */
42402 
42403     assert( !pPager->tempFile && isOpen(pPager->fd) );
42404 
42405     /* Open page 1 of the file for writing. */
42406     rc = sqlite3PagerGet(pPager, 1, &pPgHdr);
42407     assert( pPgHdr==0 || rc==SQLITE_OK );
42408 
42409     /* If page one was fetched successfully, and this function is not
42410     ** operating in direct-mode, make page 1 writable.  When not in
42411     ** direct mode, page 1 is always held in cache and hence the PagerGet()
42412     ** above is always successful - hence the ALWAYS on rc==SQLITE_OK.
42413     */
42414     if( !DIRECT_MODE && ALWAYS(rc==SQLITE_OK) ){
42415       rc = sqlite3PagerWrite(pPgHdr);
42416     }
42417 
42418     if( rc==SQLITE_OK ){
42419       /* Actually do the update of the change counter */
42420       pager_write_changecounter(pPgHdr);
42421 
42422       /* If running in direct mode, write the contents of page 1 to the file. */
42423       if( DIRECT_MODE ){
42424         const void *zBuf;
42425         assert( pPager->dbFileSize>0 );
42426         CODEC2(pPager, pPgHdr->pData, 1, 6, rc=SQLITE_NOMEM, zBuf);
42427         if( rc==SQLITE_OK ){
42428           rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0);
42429         }
42430         if( rc==SQLITE_OK ){
42431           pPager->changeCountDone = 1;
42432         }
42433       }else{
42434         pPager->changeCountDone = 1;
42435       }
42436     }
42437 
42438     /* Release the page reference. */
42439     sqlite3PagerUnref(pPgHdr);
42440   }
42441   return rc;
42442 }
42443 
42444 /*
42445 ** Sync the database file to disk. This is a no-op for in-memory databases
42446 ** or pages with the Pager.noSync flag set.
42447 **
42448 ** If successful, or if called on a pager for which it is a no-op, this
42449 ** function returns SQLITE_OK. Otherwise, an IO error code is returned.
42450 */
42451 SQLITE_PRIVATE int sqlite3PagerSync(Pager *pPager){
42452   int rc = SQLITE_OK;
42453   if( !pPager->noSync ){
42454     assert( !MEMDB );
42455     rc = sqlite3OsSync(pPager->fd, pPager->syncFlags);
42456   }else if( isOpen(pPager->fd) ){
42457     assert( !MEMDB );
42458     sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_SYNC_OMITTED, (void *)&rc);
42459   }
42460   return rc;
42461 }
42462 
42463 /*
42464 ** This function may only be called while a write-transaction is active in
42465 ** rollback. If the connection is in WAL mode, this call is a no-op.
42466 ** Otherwise, if the connection does not already have an EXCLUSIVE lock on
42467 ** the database file, an attempt is made to obtain one.
42468 **
42469 ** If the EXCLUSIVE lock is already held or the attempt to obtain it is
42470 ** successful, or the connection is in WAL mode, SQLITE_OK is returned.
42471 ** Otherwise, either SQLITE_BUSY or an SQLITE_IOERR_XXX error code is
42472 ** returned.
42473 */
42474 SQLITE_PRIVATE int sqlite3PagerExclusiveLock(Pager *pPager){
42475   int rc = SQLITE_OK;
42476   assert( pPager->eState==PAGER_WRITER_CACHEMOD
42477        || pPager->eState==PAGER_WRITER_DBMOD
42478        || pPager->eState==PAGER_WRITER_LOCKED
42479   );
42480   assert( assert_pager_state(pPager) );
42481   if( 0==pagerUseWal(pPager) ){
42482     rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
42483   }
42484   return rc;
42485 }
42486 
42487 /*
42488 ** Sync the database file for the pager pPager. zMaster points to the name
42489 ** of a master journal file that should be written into the individual
42490 ** journal file. zMaster may be NULL, which is interpreted as no master
42491 ** journal (a single database transaction).
42492 **
42493 ** This routine ensures that:
42494 **
42495 **   * The database file change-counter is updated,
42496 **   * the journal is synced (unless the atomic-write optimization is used),
42497 **   * all dirty pages are written to the database file,
42498 **   * the database file is truncated (if required), and
42499 **   * the database file synced.
42500 **
42501 ** The only thing that remains to commit the transaction is to finalize
42502 ** (delete, truncate or zero the first part of) the journal file (or
42503 ** delete the master journal file if specified).
42504 **
42505 ** Note that if zMaster==NULL, this does not overwrite a previous value
42506 ** passed to an sqlite3PagerCommitPhaseOne() call.
42507 **
42508 ** If the final parameter - noSync - is true, then the database file itself
42509 ** is not synced. The caller must call sqlite3PagerSync() directly to
42510 ** sync the database file before calling CommitPhaseTwo() to delete the
42511 ** journal file in this case.
42512 */
42513 SQLITE_PRIVATE int sqlite3PagerCommitPhaseOne(
42514   Pager *pPager,                  /* Pager object */
42515   const char *zMaster,            /* If not NULL, the master journal name */
42516   int noSync                      /* True to omit the xSync on the db file */
42517 ){
42518   int rc = SQLITE_OK;             /* Return code */
42519 
42520   assert( pPager->eState==PAGER_WRITER_LOCKED
42521        || pPager->eState==PAGER_WRITER_CACHEMOD
42522        || pPager->eState==PAGER_WRITER_DBMOD
42523        || pPager->eState==PAGER_ERROR
42524   );
42525   assert( assert_pager_state(pPager) );
42526 
42527   /* If a prior error occurred, report that error again. */
42528   if( NEVER(pPager->errCode) ) return pPager->errCode;
42529 
42530   PAGERTRACE(("DATABASE SYNC: File=%s zMaster=%s nSize=%d\n",
42531       pPager->zFilename, zMaster, pPager->dbSize));
42532 
42533   /* If no database changes have been made, return early. */
42534   if( pPager->eState<PAGER_WRITER_CACHEMOD ) return SQLITE_OK;
42535 
42536   if( MEMDB ){
42537     /* If this is an in-memory db, or no pages have been written to, or this
42538     ** function has already been called, it is mostly a no-op.  However, any
42539     ** backup in progress needs to be restarted.
42540     */
42541     sqlite3BackupRestart(pPager->pBackup);
42542   }else{
42543     if( pagerUseWal(pPager) ){
42544       PgHdr *pList = sqlite3PcacheDirtyList(pPager->pPCache);
42545       PgHdr *pPageOne = 0;
42546       if( pList==0 ){
42547         /* Must have at least one page for the WAL commit flag.
42548         ** Ticket [2d1a5c67dfc2363e44f29d9bbd57f] 2011-05-18 */
42549         rc = sqlite3PagerGet(pPager, 1, &pPageOne);
42550         pList = pPageOne;
42551         pList->pDirty = 0;
42552       }
42553       assert( rc==SQLITE_OK );
42554       if( ALWAYS(pList) ){
42555         rc = pagerWalFrames(pPager, pList, pPager->dbSize, 1,
42556             (pPager->fullSync ? pPager->syncFlags : 0)
42557         );
42558       }
42559       sqlite3PagerUnref(pPageOne);
42560       if( rc==SQLITE_OK ){
42561         sqlite3PcacheCleanAll(pPager->pPCache);
42562       }
42563     }else{
42564       /* The following block updates the change-counter. Exactly how it
42565       ** does this depends on whether or not the atomic-update optimization
42566       ** was enabled at compile time, and if this transaction meets the
42567       ** runtime criteria to use the operation:
42568       **
42569       **    * The file-system supports the atomic-write property for
42570       **      blocks of size page-size, and
42571       **    * This commit is not part of a multi-file transaction, and
42572       **    * Exactly one page has been modified and store in the journal file.
42573       **
42574       ** If the optimization was not enabled at compile time, then the
42575       ** pager_incr_changecounter() function is called to update the change
42576       ** counter in 'indirect-mode'. If the optimization is compiled in but
42577       ** is not applicable to this transaction, call sqlite3JournalCreate()
42578       ** to make sure the journal file has actually been created, then call
42579       ** pager_incr_changecounter() to update the change-counter in indirect
42580       ** mode.
42581       **
42582       ** Otherwise, if the optimization is both enabled and applicable,
42583       ** then call pager_incr_changecounter() to update the change-counter
42584       ** in 'direct' mode. In this case the journal file will never be
42585       ** created for this transaction.
42586       */
42587   #ifdef SQLITE_ENABLE_ATOMIC_WRITE
42588       PgHdr *pPg;
42589       assert( isOpen(pPager->jfd)
42590            || pPager->journalMode==PAGER_JOURNALMODE_OFF
42591            || pPager->journalMode==PAGER_JOURNALMODE_WAL
42592       );
42593       if( !zMaster && isOpen(pPager->jfd)
42594        && pPager->journalOff==jrnlBufferSize(pPager)
42595        && pPager->dbSize>=pPager->dbOrigSize
42596        && (0==(pPg = sqlite3PcacheDirtyList(pPager->pPCache)) || 0==pPg->pDirty)
42597       ){
42598         /* Update the db file change counter via the direct-write method. The
42599         ** following call will modify the in-memory representation of page 1
42600         ** to include the updated change counter and then write page 1
42601         ** directly to the database file. Because of the atomic-write
42602         ** property of the host file-system, this is safe.
42603         */
42604         rc = pager_incr_changecounter(pPager, 1);
42605       }else{
42606         rc = sqlite3JournalCreate(pPager->jfd);
42607         if( rc==SQLITE_OK ){
42608           rc = pager_incr_changecounter(pPager, 0);
42609         }
42610       }
42611   #else
42612       rc = pager_incr_changecounter(pPager, 0);
42613   #endif
42614       if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
42615 
42616       /* If this transaction has made the database smaller, then all pages
42617       ** being discarded by the truncation must be written to the journal
42618       ** file. This can only happen in auto-vacuum mode.
42619       **
42620       ** Before reading the pages with page numbers larger than the
42621       ** current value of Pager.dbSize, set dbSize back to the value
42622       ** that it took at the start of the transaction. Otherwise, the
42623       ** calls to sqlite3PagerGet() return zeroed pages instead of
42624       ** reading data from the database file.
42625       */
42626   #ifndef SQLITE_OMIT_AUTOVACUUM
42627       if( pPager->dbSize<pPager->dbOrigSize
42628        && pPager->journalMode!=PAGER_JOURNALMODE_OFF
42629       ){
42630         Pgno i;                                   /* Iterator variable */
42631         const Pgno iSkip = PAGER_MJ_PGNO(pPager); /* Pending lock page */
42632         const Pgno dbSize = pPager->dbSize;       /* Database image size */
42633         pPager->dbSize = pPager->dbOrigSize;
42634         for( i=dbSize+1; i<=pPager->dbOrigSize; i++ ){
42635           if( !sqlite3BitvecTest(pPager->pInJournal, i) && i!=iSkip ){
42636             PgHdr *pPage;             /* Page to journal */
42637             rc = sqlite3PagerGet(pPager, i, &pPage);
42638             if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
42639             rc = sqlite3PagerWrite(pPage);
42640             sqlite3PagerUnref(pPage);
42641             if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
42642           }
42643         }
42644         pPager->dbSize = dbSize;
42645       }
42646   #endif
42647 
42648       /* Write the master journal name into the journal file. If a master
42649       ** journal file name has already been written to the journal file,
42650       ** or if zMaster is NULL (no master journal), then this call is a no-op.
42651       */
42652       rc = writeMasterJournal(pPager, zMaster);
42653       if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
42654 
42655       /* Sync the journal file and write all dirty pages to the database.
42656       ** If the atomic-update optimization is being used, this sync will not
42657       ** create the journal file or perform any real IO.
42658       **
42659       ** Because the change-counter page was just modified, unless the
42660       ** atomic-update optimization is used it is almost certain that the
42661       ** journal requires a sync here. However, in locking_mode=exclusive
42662       ** on a system under memory pressure it is just possible that this is
42663       ** not the case. In this case it is likely enough that the redundant
42664       ** xSync() call will be changed to a no-op by the OS anyhow.
42665       */
42666       rc = syncJournal(pPager, 0);
42667       if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
42668 
42669       rc = pager_write_pagelist(pPager,sqlite3PcacheDirtyList(pPager->pPCache));
42670       if( rc!=SQLITE_OK ){
42671         assert( rc!=SQLITE_IOERR_BLOCKED );
42672         goto commit_phase_one_exit;
42673       }
42674       sqlite3PcacheCleanAll(pPager->pPCache);
42675 
42676       /* If the file on disk is not the same size as the database image,
42677       ** then use pager_truncate to grow or shrink the file here.
42678       */
42679       if( pPager->dbSize!=pPager->dbFileSize ){
42680         Pgno nNew = pPager->dbSize - (pPager->dbSize==PAGER_MJ_PGNO(pPager));
42681         assert( pPager->eState==PAGER_WRITER_DBMOD );
42682         rc = pager_truncate(pPager, nNew);
42683         if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
42684       }
42685 
42686       /* Finally, sync the database file. */
42687       if( !noSync ){
42688         rc = sqlite3PagerSync(pPager);
42689       }
42690       IOTRACE(("DBSYNC %p\n", pPager))
42691     }
42692   }
42693 
42694 commit_phase_one_exit:
42695   if( rc==SQLITE_OK && !pagerUseWal(pPager) ){
42696     pPager->eState = PAGER_WRITER_FINISHED;
42697   }
42698   return rc;
42699 }
42700 
42701 
42702 /*
42703 ** When this function is called, the database file has been completely
42704 ** updated to reflect the changes made by the current transaction and
42705 ** synced to disk. The journal file still exists in the file-system
42706 ** though, and if a failure occurs at this point it will eventually
42707 ** be used as a hot-journal and the current transaction rolled back.
42708 **
42709 ** This function finalizes the journal file, either by deleting,
42710 ** truncating or partially zeroing it, so that it cannot be used
42711 ** for hot-journal rollback. Once this is done the transaction is
42712 ** irrevocably committed.
42713 **
42714 ** If an error occurs, an IO error code is returned and the pager
42715 ** moves into the error state. Otherwise, SQLITE_OK is returned.
42716 */
42717 SQLITE_PRIVATE int sqlite3PagerCommitPhaseTwo(Pager *pPager){
42718   int rc = SQLITE_OK;                  /* Return code */
42719 
42720   /* This routine should not be called if a prior error has occurred.
42721   ** But if (due to a coding error elsewhere in the system) it does get
42722   ** called, just return the same error code without doing anything. */
42723   if( NEVER(pPager->errCode) ) return pPager->errCode;
42724 
42725   assert( pPager->eState==PAGER_WRITER_LOCKED
42726        || pPager->eState==PAGER_WRITER_FINISHED
42727        || (pagerUseWal(pPager) && pPager->eState==PAGER_WRITER_CACHEMOD)
42728   );
42729   assert( assert_pager_state(pPager) );
42730 
42731   /* An optimization. If the database was not actually modified during
42732   ** this transaction, the pager is running in exclusive-mode and is
42733   ** using persistent journals, then this function is a no-op.
42734   **
42735   ** The start of the journal file currently contains a single journal
42736   ** header with the nRec field set to 0. If such a journal is used as
42737   ** a hot-journal during hot-journal rollback, 0 changes will be made
42738   ** to the database file. So there is no need to zero the journal
42739   ** header. Since the pager is in exclusive mode, there is no need
42740   ** to drop any locks either.
42741   */
42742   if( pPager->eState==PAGER_WRITER_LOCKED
42743    && pPager->exclusiveMode
42744    && pPager->journalMode==PAGER_JOURNALMODE_PERSIST
42745   ){
42746     assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) || !pPager->journalOff );
42747     pPager->eState = PAGER_READER;
42748     return SQLITE_OK;
42749   }
42750 
42751   PAGERTRACE(("COMMIT %d\n", PAGERID(pPager)));
42752   rc = pager_end_transaction(pPager, pPager->setMaster);
42753   return pager_error(pPager, rc);
42754 }
42755 
42756 /*
42757 ** If a write transaction is open, then all changes made within the
42758 ** transaction are reverted and the current write-transaction is closed.
42759 ** The pager falls back to PAGER_READER state if successful, or PAGER_ERROR
42760 ** state if an error occurs.
42761 **
42762 ** If the pager is already in PAGER_ERROR state when this function is called,
42763 ** it returns Pager.errCode immediately. No work is performed in this case.
42764 **
42765 ** Otherwise, in rollback mode, this function performs two functions:
42766 **
42767 **   1) It rolls back the journal file, restoring all database file and
42768 **      in-memory cache pages to the state they were in when the transaction
42769 **      was opened, and
42770 **
42771 **   2) It finalizes the journal file, so that it is not used for hot
42772 **      rollback at any point in the future.
42773 **
42774 ** Finalization of the journal file (task 2) is only performed if the
42775 ** rollback is successful.
42776 **
42777 ** In WAL mode, all cache-entries containing data modified within the
42778 ** current transaction are either expelled from the cache or reverted to
42779 ** their pre-transaction state by re-reading data from the database or
42780 ** WAL files. The WAL transaction is then closed.
42781 */
42782 SQLITE_PRIVATE int sqlite3PagerRollback(Pager *pPager){
42783   int rc = SQLITE_OK;                  /* Return code */
42784   PAGERTRACE(("ROLLBACK %d\n", PAGERID(pPager)));
42785 
42786   /* PagerRollback() is a no-op if called in READER or OPEN state. If
42787   ** the pager is already in the ERROR state, the rollback is not
42788   ** attempted here. Instead, the error code is returned to the caller.
42789   */
42790   assert( assert_pager_state(pPager) );
42791   if( pPager->eState==PAGER_ERROR ) return pPager->errCode;
42792   if( pPager->eState<=PAGER_READER ) return SQLITE_OK;
42793 
42794   if( pagerUseWal(pPager) ){
42795     int rc2;
42796     rc = sqlite3PagerSavepoint(pPager, SAVEPOINT_ROLLBACK, -1);
42797     rc2 = pager_end_transaction(pPager, pPager->setMaster);
42798     if( rc==SQLITE_OK ) rc = rc2;
42799   }else if( !isOpen(pPager->jfd) || pPager->eState==PAGER_WRITER_LOCKED ){
42800     int eState = pPager->eState;
42801     rc = pager_end_transaction(pPager, 0);
42802     if( !MEMDB && eState>PAGER_WRITER_LOCKED ){
42803       /* This can happen using journal_mode=off. Move the pager to the error
42804       ** state to indicate that the contents of the cache may not be trusted.
42805       ** Any active readers will get SQLITE_ABORT.
42806       */
42807       pPager->errCode = SQLITE_ABORT;
42808       pPager->eState = PAGER_ERROR;
42809       return rc;
42810     }
42811   }else{
42812     rc = pager_playback(pPager, 0);
42813   }
42814 
42815   assert( pPager->eState==PAGER_READER || rc!=SQLITE_OK );
42816   assert( rc==SQLITE_OK || rc==SQLITE_FULL || (rc&0xFF)==SQLITE_IOERR );
42817 
42818   /* If an error occurs during a ROLLBACK, we can no longer trust the pager
42819   ** cache. So call pager_error() on the way out to make any error persistent.
42820   */
42821   return pager_error(pPager, rc);
42822 }
42823 
42824 /*
42825 ** Return TRUE if the database file is opened read-only.  Return FALSE
42826 ** if the database is (in theory) writable.
42827 */
42828 SQLITE_PRIVATE u8 sqlite3PagerIsreadonly(Pager *pPager){
42829   return pPager->readOnly;
42830 }
42831 
42832 /*
42833 ** Return the number of references to the pager.
42834 */
42835 SQLITE_PRIVATE int sqlite3PagerRefcount(Pager *pPager){
42836   return sqlite3PcacheRefCount(pPager->pPCache);
42837 }
42838 
42839 /*
42840 ** Return the approximate number of bytes of memory currently
42841 ** used by the pager and its associated cache.
42842 */
42843 SQLITE_PRIVATE int sqlite3PagerMemUsed(Pager *pPager){
42844   int perPageSize = pPager->pageSize + pPager->nExtra + sizeof(PgHdr)
42845                                      + 5*sizeof(void*);
42846   return perPageSize*sqlite3PcachePagecount(pPager->pPCache)
42847            + sqlite3MallocSize(pPager)
42848            + pPager->pageSize;
42849 }
42850 
42851 /*
42852 ** Return the number of references to the specified page.
42853 */
42854 SQLITE_PRIVATE int sqlite3PagerPageRefcount(DbPage *pPage){
42855   return sqlite3PcachePageRefcount(pPage);
42856 }
42857 
42858 #ifdef SQLITE_TEST
42859 /*
42860 ** This routine is used for testing and analysis only.
42861 */
42862 SQLITE_PRIVATE int *sqlite3PagerStats(Pager *pPager){
42863   static int a[11];
42864   a[0] = sqlite3PcacheRefCount(pPager->pPCache);
42865   a[1] = sqlite3PcachePagecount(pPager->pPCache);
42866   a[2] = sqlite3PcacheGetCachesize(pPager->pPCache);
42867   a[3] = pPager->eState==PAGER_OPEN ? -1 : (int) pPager->dbSize;
42868   a[4] = pPager->eState;
42869   a[5] = pPager->errCode;
42870   a[6] = pPager->nHit;
42871   a[7] = pPager->nMiss;
42872   a[8] = 0;  /* Used to be pPager->nOvfl */
42873   a[9] = pPager->nRead;
42874   a[10] = pPager->nWrite;
42875   return a;
42876 }
42877 #endif
42878 
42879 /*
42880 ** Return true if this is an in-memory pager.
42881 */
42882 SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager *pPager){
42883   return MEMDB;
42884 }
42885 
42886 /*
42887 ** Check that there are at least nSavepoint savepoints open. If there are
42888 ** currently less than nSavepoints open, then open one or more savepoints
42889 ** to make up the difference. If the number of savepoints is already
42890 ** equal to nSavepoint, then this function is a no-op.
42891 **
42892 ** If a memory allocation fails, SQLITE_NOMEM is returned. If an error
42893 ** occurs while opening the sub-journal file, then an IO error code is
42894 ** returned. Otherwise, SQLITE_OK.
42895 */
42896 SQLITE_PRIVATE int sqlite3PagerOpenSavepoint(Pager *pPager, int nSavepoint){
42897   int rc = SQLITE_OK;                       /* Return code */
42898   int nCurrent = pPager->nSavepoint;        /* Current number of savepoints */
42899 
42900   assert( pPager->eState>=PAGER_WRITER_LOCKED );
42901   assert( assert_pager_state(pPager) );
42902 
42903   if( nSavepoint>nCurrent && pPager->useJournal ){
42904     int ii;                                 /* Iterator variable */
42905     PagerSavepoint *aNew;                   /* New Pager.aSavepoint array */
42906 
42907     /* Grow the Pager.aSavepoint array using realloc(). Return SQLITE_NOMEM
42908     ** if the allocation fails. Otherwise, zero the new portion in case a
42909     ** malloc failure occurs while populating it in the for(...) loop below.
42910     */
42911     aNew = (PagerSavepoint *)sqlite3Realloc(
42912         pPager->aSavepoint, sizeof(PagerSavepoint)*nSavepoint
42913     );
42914     if( !aNew ){
42915       return SQLITE_NOMEM;
42916     }
42917     memset(&aNew[nCurrent], 0, (nSavepoint-nCurrent) * sizeof(PagerSavepoint));
42918     pPager->aSavepoint = aNew;
42919 
42920     /* Populate the PagerSavepoint structures just allocated. */
42921     for(ii=nCurrent; ii<nSavepoint; ii++){
42922       aNew[ii].nOrig = pPager->dbSize;
42923       if( isOpen(pPager->jfd) && pPager->journalOff>0 ){
42924         aNew[ii].iOffset = pPager->journalOff;
42925       }else{
42926         aNew[ii].iOffset = JOURNAL_HDR_SZ(pPager);
42927       }
42928       aNew[ii].iSubRec = pPager->nSubRec;
42929       aNew[ii].pInSavepoint = sqlite3BitvecCreate(pPager->dbSize);
42930       if( !aNew[ii].pInSavepoint ){
42931         return SQLITE_NOMEM;
42932       }
42933       if( pagerUseWal(pPager) ){
42934         sqlite3WalSavepoint(pPager->pWal, aNew[ii].aWalData);
42935       }
42936       pPager->nSavepoint = ii+1;
42937     }
42938     assert( pPager->nSavepoint==nSavepoint );
42939     assertTruncateConstraint(pPager);
42940   }
42941 
42942   return rc;
42943 }
42944 
42945 /*
42946 ** This function is called to rollback or release (commit) a savepoint.
42947 ** The savepoint to release or rollback need not be the most recently
42948 ** created savepoint.
42949 **
42950 ** Parameter op is always either SAVEPOINT_ROLLBACK or SAVEPOINT_RELEASE.
42951 ** If it is SAVEPOINT_RELEASE, then release and destroy the savepoint with
42952 ** index iSavepoint. If it is SAVEPOINT_ROLLBACK, then rollback all changes
42953 ** that have occurred since the specified savepoint was created.
42954 **
42955 ** The savepoint to rollback or release is identified by parameter
42956 ** iSavepoint. A value of 0 means to operate on the outermost savepoint
42957 ** (the first created). A value of (Pager.nSavepoint-1) means operate
42958 ** on the most recently created savepoint. If iSavepoint is greater than
42959 ** (Pager.nSavepoint-1), then this function is a no-op.
42960 **
42961 ** If a negative value is passed to this function, then the current
42962 ** transaction is rolled back. This is different to calling
42963 ** sqlite3PagerRollback() because this function does not terminate
42964 ** the transaction or unlock the database, it just restores the
42965 ** contents of the database to its original state.
42966 **
42967 ** In any case, all savepoints with an index greater than iSavepoint
42968 ** are destroyed. If this is a release operation (op==SAVEPOINT_RELEASE),
42969 ** then savepoint iSavepoint is also destroyed.
42970 **
42971 ** This function may return SQLITE_NOMEM if a memory allocation fails,
42972 ** or an IO error code if an IO error occurs while rolling back a
42973 ** savepoint. If no errors occur, SQLITE_OK is returned.
42974 */
42975 SQLITE_PRIVATE int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint){
42976   int rc = pPager->errCode;       /* Return code */
42977 
42978   assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK );
42979   assert( iSavepoint>=0 || op==SAVEPOINT_ROLLBACK );
42980 
42981   if( rc==SQLITE_OK && iSavepoint<pPager->nSavepoint ){
42982     int ii;            /* Iterator variable */
42983     int nNew;          /* Number of remaining savepoints after this op. */
42984 
42985     /* Figure out how many savepoints will still be active after this
42986     ** operation. Store this value in nNew. Then free resources associated
42987     ** with any savepoints that are destroyed by this operation.
42988     */
42989     nNew = iSavepoint + (( op==SAVEPOINT_RELEASE ) ? 0 : 1);
42990     for(ii=nNew; ii<pPager->nSavepoint; ii++){
42991       sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint);
42992     }
42993     pPager->nSavepoint = nNew;
42994 
42995     /* If this is a release of the outermost savepoint, truncate
42996     ** the sub-journal to zero bytes in size. */
42997     if( op==SAVEPOINT_RELEASE ){
42998       if( nNew==0 && isOpen(pPager->sjfd) ){
42999         /* Only truncate if it is an in-memory sub-journal. */
43000         if( sqlite3IsMemJournal(pPager->sjfd) ){
43001           rc = sqlite3OsTruncate(pPager->sjfd, 0);
43002           assert( rc==SQLITE_OK );
43003         }
43004         pPager->nSubRec = 0;
43005       }
43006     }
43007     /* Else this is a rollback operation, playback the specified savepoint.
43008     ** If this is a temp-file, it is possible that the journal file has
43009     ** not yet been opened. In this case there have been no changes to
43010     ** the database file, so the playback operation can be skipped.
43011     */
43012     else if( pagerUseWal(pPager) || isOpen(pPager->jfd) ){
43013       PagerSavepoint *pSavepoint = (nNew==0)?0:&pPager->aSavepoint[nNew-1];
43014       rc = pagerPlaybackSavepoint(pPager, pSavepoint);
43015       assert(rc!=SQLITE_DONE);
43016     }
43017   }
43018 
43019   return rc;
43020 }
43021 
43022 /*
43023 ** Return the full pathname of the database file.
43024 */
43025 SQLITE_PRIVATE const char *sqlite3PagerFilename(Pager *pPager){
43026   return pPager->zFilename;
43027 }
43028 
43029 /*
43030 ** Return the VFS structure for the pager.
43031 */
43032 SQLITE_PRIVATE const sqlite3_vfs *sqlite3PagerVfs(Pager *pPager){
43033   return pPager->pVfs;
43034 }
43035 
43036 /*
43037 ** Return the file handle for the database file associated
43038 ** with the pager.  This might return NULL if the file has
43039 ** not yet been opened.
43040 */
43041 SQLITE_PRIVATE sqlite3_file *sqlite3PagerFile(Pager *pPager){
43042   return pPager->fd;
43043 }
43044 
43045 /*
43046 ** Return the full pathname of the journal file.
43047 */
43048 SQLITE_PRIVATE const char *sqlite3PagerJournalname(Pager *pPager){
43049   return pPager->zJournal;
43050 }
43051 
43052 /*
43053 ** Return true if fsync() calls are disabled for this pager.  Return FALSE
43054 ** if fsync()s are executed normally.
43055 */
43056 SQLITE_PRIVATE int sqlite3PagerNosync(Pager *pPager){
43057   return pPager->noSync;
43058 }
43059 
43060 #ifdef SQLITE_HAS_CODEC
43061 /*
43062 ** Set or retrieve the codec for this pager
43063 */
43064 SQLITE_PRIVATE void sqlite3PagerSetCodec(
43065   Pager *pPager,
43066   void *(*xCodec)(void*,void*,Pgno,int),
43067   void (*xCodecSizeChng)(void*,int,int),
43068   void (*xCodecFree)(void*),
43069   void *pCodec
43070 ){
43071   if( pPager->xCodecFree ) pPager->xCodecFree(pPager->pCodec);
43072   pPager->xCodec = pPager->memDb ? 0 : xCodec;
43073   pPager->xCodecSizeChng = xCodecSizeChng;
43074   pPager->xCodecFree = xCodecFree;
43075   pPager->pCodec = pCodec;
43076   pagerReportSize(pPager);
43077 }
43078 SQLITE_PRIVATE void *sqlite3PagerGetCodec(Pager *pPager){
43079   return pPager->pCodec;
43080 }
43081 #endif
43082 
43083 #ifndef SQLITE_OMIT_AUTOVACUUM
43084 /*
43085 ** Move the page pPg to location pgno in the file.
43086 **
43087 ** There must be no references to the page previously located at
43088 ** pgno (which we call pPgOld) though that page is allowed to be
43089 ** in cache.  If the page previously located at pgno is not already
43090 ** in the rollback journal, it is not put there by by this routine.
43091 **
43092 ** References to the page pPg remain valid. Updating any
43093 ** meta-data associated with pPg (i.e. data stored in the nExtra bytes
43094 ** allocated along with the page) is the responsibility of the caller.
43095 **
43096 ** A transaction must be active when this routine is called. It used to be
43097 ** required that a statement transaction was not active, but this restriction
43098 ** has been removed (CREATE INDEX needs to move a page when a statement
43099 ** transaction is active).
43100 **
43101 ** If the fourth argument, isCommit, is non-zero, then this page is being
43102 ** moved as part of a database reorganization just before the transaction
43103 ** is being committed. In this case, it is guaranteed that the database page
43104 ** pPg refers to will not be written to again within this transaction.
43105 **
43106 ** This function may return SQLITE_NOMEM or an IO error code if an error
43107 ** occurs. Otherwise, it returns SQLITE_OK.
43108 */
43109 SQLITE_PRIVATE int sqlite3PagerMovepage(Pager *pPager, DbPage *pPg, Pgno pgno, int isCommit){
43110   PgHdr *pPgOld;               /* The page being overwritten. */
43111   Pgno needSyncPgno = 0;       /* Old value of pPg->pgno, if sync is required */
43112   int rc;                      /* Return code */
43113   Pgno origPgno;               /* The original page number */
43114 
43115   assert( pPg->nRef>0 );
43116   assert( pPager->eState==PAGER_WRITER_CACHEMOD
43117        || pPager->eState==PAGER_WRITER_DBMOD
43118   );
43119   assert( assert_pager_state(pPager) );
43120 
43121   /* In order to be able to rollback, an in-memory database must journal
43122   ** the page we are moving from.
43123   */
43124   if( MEMDB ){
43125     rc = sqlite3PagerWrite(pPg);
43126     if( rc ) return rc;
43127   }
43128 
43129   /* If the page being moved is dirty and has not been saved by the latest
43130   ** savepoint, then save the current contents of the page into the
43131   ** sub-journal now. This is required to handle the following scenario:
43132   **
43133   **   BEGIN;
43134   **     <journal page X, then modify it in memory>
43135   **     SAVEPOINT one;
43136   **       <Move page X to location Y>
43137   **     ROLLBACK TO one;
43138   **
43139   ** If page X were not written to the sub-journal here, it would not
43140   ** be possible to restore its contents when the "ROLLBACK TO one"
43141   ** statement were is processed.
43142   **
43143   ** subjournalPage() may need to allocate space to store pPg->pgno into
43144   ** one or more savepoint bitvecs. This is the reason this function
43145   ** may return SQLITE_NOMEM.
43146   */
43147   if( pPg->flags&PGHDR_DIRTY
43148    && subjRequiresPage(pPg)
43149    && SQLITE_OK!=(rc = subjournalPage(pPg))
43150   ){
43151     return rc;
43152   }
43153 
43154   PAGERTRACE(("MOVE %d page %d (needSync=%d) moves to %d\n",
43155       PAGERID(pPager), pPg->pgno, (pPg->flags&PGHDR_NEED_SYNC)?1:0, pgno));
43156   IOTRACE(("MOVE %p %d %d\n", pPager, pPg->pgno, pgno))
43157 
43158   /* If the journal needs to be sync()ed before page pPg->pgno can
43159   ** be written to, store pPg->pgno in local variable needSyncPgno.
43160   **
43161   ** If the isCommit flag is set, there is no need to remember that
43162   ** the journal needs to be sync()ed before database page pPg->pgno
43163   ** can be written to. The caller has already promised not to write to it.
43164   */
43165   if( (pPg->flags&PGHDR_NEED_SYNC) && !isCommit ){
43166     needSyncPgno = pPg->pgno;
43167     assert( pageInJournal(pPg) || pPg->pgno>pPager->dbOrigSize );
43168     assert( pPg->flags&PGHDR_DIRTY );
43169   }
43170 
43171   /* If the cache contains a page with page-number pgno, remove it
43172   ** from its hash chain. Also, if the PGHDR_NEED_SYNC flag was set for
43173   ** page pgno before the 'move' operation, it needs to be retained
43174   ** for the page moved there.
43175   */
43176   pPg->flags &= ~PGHDR_NEED_SYNC;
43177   pPgOld = pager_lookup(pPager, pgno);
43178   assert( !pPgOld || pPgOld->nRef==1 );
43179   if( pPgOld ){
43180     pPg->flags |= (pPgOld->flags&PGHDR_NEED_SYNC);
43181     if( MEMDB ){
43182       /* Do not discard pages from an in-memory database since we might
43183       ** need to rollback later.  Just move the page out of the way. */
43184       sqlite3PcacheMove(pPgOld, pPager->dbSize+1);
43185     }else{
43186       sqlite3PcacheDrop(pPgOld);
43187     }
43188   }
43189 
43190   origPgno = pPg->pgno;
43191   sqlite3PcacheMove(pPg, pgno);
43192   sqlite3PcacheMakeDirty(pPg);
43193 
43194   /* For an in-memory database, make sure the original page continues
43195   ** to exist, in case the transaction needs to roll back.  Use pPgOld
43196   ** as the original page since it has already been allocated.
43197   */
43198   if( MEMDB ){
43199     assert( pPgOld );
43200     sqlite3PcacheMove(pPgOld, origPgno);
43201     sqlite3PagerUnref(pPgOld);
43202   }
43203 
43204   if( needSyncPgno ){
43205     /* If needSyncPgno is non-zero, then the journal file needs to be
43206     ** sync()ed before any data is written to database file page needSyncPgno.
43207     ** Currently, no such page exists in the page-cache and the
43208     ** "is journaled" bitvec flag has been set. This needs to be remedied by
43209     ** loading the page into the pager-cache and setting the PGHDR_NEED_SYNC
43210     ** flag.
43211     **
43212     ** If the attempt to load the page into the page-cache fails, (due
43213     ** to a malloc() or IO failure), clear the bit in the pInJournal[]
43214     ** array. Otherwise, if the page is loaded and written again in
43215     ** this transaction, it may be written to the database file before
43216     ** it is synced into the journal file. This way, it may end up in
43217     ** the journal file twice, but that is not a problem.
43218     */
43219     PgHdr *pPgHdr;
43220     rc = sqlite3PagerGet(pPager, needSyncPgno, &pPgHdr);
43221     if( rc!=SQLITE_OK ){
43222       if( needSyncPgno<=pPager->dbOrigSize ){
43223         assert( pPager->pTmpSpace!=0 );
43224         sqlite3BitvecClear(pPager->pInJournal, needSyncPgno, pPager->pTmpSpace);
43225       }
43226       return rc;
43227     }
43228     pPgHdr->flags |= PGHDR_NEED_SYNC;
43229     sqlite3PcacheMakeDirty(pPgHdr);
43230     sqlite3PagerUnref(pPgHdr);
43231   }
43232 
43233   return SQLITE_OK;
43234 }
43235 #endif
43236 
43237 /*
43238 ** Return a pointer to the data for the specified page.
43239 */
43240 SQLITE_PRIVATE void *sqlite3PagerGetData(DbPage *pPg){
43241   assert( pPg->nRef>0 || pPg->pPager->memDb );
43242   return pPg->pData;
43243 }
43244 
43245 /*
43246 ** Return a pointer to the Pager.nExtra bytes of "extra" space
43247 ** allocated along with the specified page.
43248 */
43249 SQLITE_PRIVATE void *sqlite3PagerGetExtra(DbPage *pPg){
43250   return pPg->pExtra;
43251 }
43252 
43253 /*
43254 ** Get/set the locking-mode for this pager. Parameter eMode must be one
43255 ** of PAGER_LOCKINGMODE_QUERY, PAGER_LOCKINGMODE_NORMAL or
43256 ** PAGER_LOCKINGMODE_EXCLUSIVE. If the parameter is not _QUERY, then
43257 ** the locking-mode is set to the value specified.
43258 **
43259 ** The returned value is either PAGER_LOCKINGMODE_NORMAL or
43260 ** PAGER_LOCKINGMODE_EXCLUSIVE, indicating the current (possibly updated)
43261 ** locking-mode.
43262 */
43263 SQLITE_PRIVATE int sqlite3PagerLockingMode(Pager *pPager, int eMode){
43264   assert( eMode==PAGER_LOCKINGMODE_QUERY
43265             || eMode==PAGER_LOCKINGMODE_NORMAL
43266             || eMode==PAGER_LOCKINGMODE_EXCLUSIVE );
43267   assert( PAGER_LOCKINGMODE_QUERY<0 );
43268   assert( PAGER_LOCKINGMODE_NORMAL>=0 && PAGER_LOCKINGMODE_EXCLUSIVE>=0 );
43269   assert( pPager->exclusiveMode || 0==sqlite3WalHeapMemory(pPager->pWal) );
43270   if( eMode>=0 && !pPager->tempFile && !sqlite3WalHeapMemory(pPager->pWal) ){
43271     pPager->exclusiveMode = (u8)eMode;
43272   }
43273   return (int)pPager->exclusiveMode;
43274 }
43275 
43276 /*
43277 ** Set the journal-mode for this pager. Parameter eMode must be one of:
43278 **
43279 **    PAGER_JOURNALMODE_DELETE
43280 **    PAGER_JOURNALMODE_TRUNCATE
43281 **    PAGER_JOURNALMODE_PERSIST
43282 **    PAGER_JOURNALMODE_OFF
43283 **    PAGER_JOURNALMODE_MEMORY
43284 **    PAGER_JOURNALMODE_WAL
43285 **
43286 ** The journalmode is set to the value specified if the change is allowed.
43287 ** The change may be disallowed for the following reasons:
43288 **
43289 **   *  An in-memory database can only have its journal_mode set to _OFF
43290 **      or _MEMORY.
43291 **
43292 **   *  Temporary databases cannot have _WAL journalmode.
43293 **
43294 ** The returned indicate the current (possibly updated) journal-mode.
43295 */
43296 SQLITE_PRIVATE int sqlite3PagerSetJournalMode(Pager *pPager, int eMode){
43297   u8 eOld = pPager->journalMode;    /* Prior journalmode */
43298 
43299 #ifdef SQLITE_DEBUG
43300   /* The print_pager_state() routine is intended to be used by the debugger
43301   ** only.  We invoke it once here to suppress a compiler warning. */
43302   print_pager_state(pPager);
43303 #endif
43304 
43305 
43306   /* The eMode parameter is always valid */
43307   assert(      eMode==PAGER_JOURNALMODE_DELETE
43308             || eMode==PAGER_JOURNALMODE_TRUNCATE
43309             || eMode==PAGER_JOURNALMODE_PERSIST
43310             || eMode==PAGER_JOURNALMODE_OFF
43311             || eMode==PAGER_JOURNALMODE_WAL
43312             || eMode==PAGER_JOURNALMODE_MEMORY );
43313 
43314   /* This routine is only called from the OP_JournalMode opcode, and
43315   ** the logic there will never allow a temporary file to be changed
43316   ** to WAL mode.
43317   */
43318   assert( pPager->tempFile==0 || eMode!=PAGER_JOURNALMODE_WAL );
43319 
43320   /* Do allow the journalmode of an in-memory database to be set to
43321   ** anything other than MEMORY or OFF
43322   */
43323   if( MEMDB ){
43324     assert( eOld==PAGER_JOURNALMODE_MEMORY || eOld==PAGER_JOURNALMODE_OFF );
43325     if( eMode!=PAGER_JOURNALMODE_MEMORY && eMode!=PAGER_JOURNALMODE_OFF ){
43326       eMode = eOld;
43327     }
43328   }
43329 
43330   if( eMode!=eOld ){
43331 
43332     /* Change the journal mode. */
43333     assert( pPager->eState!=PAGER_ERROR );
43334     pPager->journalMode = (u8)eMode;
43335 
43336     /* When transistioning from TRUNCATE or PERSIST to any other journal
43337     ** mode except WAL, unless the pager is in locking_mode=exclusive mode,
43338     ** delete the journal file.
43339     */
43340     assert( (PAGER_JOURNALMODE_TRUNCATE & 5)==1 );
43341     assert( (PAGER_JOURNALMODE_PERSIST & 5)==1 );
43342     assert( (PAGER_JOURNALMODE_DELETE & 5)==0 );
43343     assert( (PAGER_JOURNALMODE_MEMORY & 5)==4 );
43344     assert( (PAGER_JOURNALMODE_OFF & 5)==0 );
43345     assert( (PAGER_JOURNALMODE_WAL & 5)==5 );
43346 
43347     assert( isOpen(pPager->fd) || pPager->exclusiveMode );
43348     if( !pPager->exclusiveMode && (eOld & 5)==1 && (eMode & 1)==0 ){
43349 
43350       /* In this case we would like to delete the journal file. If it is
43351       ** not possible, then that is not a problem. Deleting the journal file
43352       ** here is an optimization only.
43353       **
43354       ** Before deleting the journal file, obtain a RESERVED lock on the
43355       ** database file. This ensures that the journal file is not deleted
43356       ** while it is in use by some other client.
43357       */
43358       sqlite3OsClose(pPager->jfd);
43359       if( pPager->eLock>=RESERVED_LOCK ){
43360         sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
43361       }else{
43362         int rc = SQLITE_OK;
43363         int state = pPager->eState;
43364         assert( state==PAGER_OPEN || state==PAGER_READER );
43365         if( state==PAGER_OPEN ){
43366           rc = sqlite3PagerSharedLock(pPager);
43367         }
43368         if( pPager->eState==PAGER_READER ){
43369           assert( rc==SQLITE_OK );
43370           rc = pagerLockDb(pPager, RESERVED_LOCK);
43371         }
43372         if( rc==SQLITE_OK ){
43373           sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
43374         }
43375         if( rc==SQLITE_OK && state==PAGER_READER ){
43376           pagerUnlockDb(pPager, SHARED_LOCK);
43377         }else if( state==PAGER_OPEN ){
43378           pager_unlock(pPager);
43379         }
43380         assert( state==pPager->eState );
43381       }
43382     }
43383   }
43384 
43385   /* Return the new journal mode */
43386   return (int)pPager->journalMode;
43387 }
43388 
43389 /*
43390 ** Return the current journal mode.
43391 */
43392 SQLITE_PRIVATE int sqlite3PagerGetJournalMode(Pager *pPager){
43393   return (int)pPager->journalMode;
43394 }
43395 
43396 /*
43397 ** Return TRUE if the pager is in a state where it is OK to change the
43398 ** journalmode.  Journalmode changes can only happen when the database
43399 ** is unmodified.
43400 */
43401 SQLITE_PRIVATE int sqlite3PagerOkToChangeJournalMode(Pager *pPager){
43402   assert( assert_pager_state(pPager) );
43403   if( pPager->eState>=PAGER_WRITER_CACHEMOD ) return 0;
43404   if( NEVER(isOpen(pPager->jfd) && pPager->journalOff>0) ) return 0;
43405   return 1;
43406 }
43407 
43408 /*
43409 ** Get/set the size-limit used for persistent journal files.
43410 **
43411 ** Setting the size limit to -1 means no limit is enforced.
43412 ** An attempt to set a limit smaller than -1 is a no-op.
43413 */
43414 SQLITE_PRIVATE i64 sqlite3PagerJournalSizeLimit(Pager *pPager, i64 iLimit){
43415   if( iLimit>=-1 ){
43416     pPager->journalSizeLimit = iLimit;
43417     sqlite3WalLimit(pPager->pWal, iLimit);
43418   }
43419   return pPager->journalSizeLimit;
43420 }
43421 
43422 /*
43423 ** Return a pointer to the pPager->pBackup variable. The backup module
43424 ** in backup.c maintains the content of this variable. This module
43425 ** uses it opaquely as an argument to sqlite3BackupRestart() and
43426 ** sqlite3BackupUpdate() only.
43427 */
43428 SQLITE_PRIVATE sqlite3_backup **sqlite3PagerBackupPtr(Pager *pPager){
43429   return &pPager->pBackup;
43430 }
43431 
43432 #ifndef SQLITE_OMIT_WAL
43433 /*
43434 ** This function is called when the user invokes "PRAGMA wal_checkpoint",
43435 ** "PRAGMA wal_blocking_checkpoint" or calls the sqlite3_wal_checkpoint()
43436 ** or wal_blocking_checkpoint() API functions.
43437 **
43438 ** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART.
43439 */
43440 SQLITE_PRIVATE int sqlite3PagerCheckpoint(Pager *pPager, int eMode, int *pnLog, int *pnCkpt){
43441   int rc = SQLITE_OK;
43442   if( pPager->pWal ){
43443     rc = sqlite3WalCheckpoint(pPager->pWal, eMode,
43444         pPager->xBusyHandler, pPager->pBusyHandlerArg,
43445         pPager->ckptSyncFlags, pPager->pageSize, (u8 *)pPager->pTmpSpace,
43446         pnLog, pnCkpt
43447     );
43448   }
43449   return rc;
43450 }
43451 
43452 SQLITE_PRIVATE int sqlite3PagerWalCallback(Pager *pPager){
43453   return sqlite3WalCallback(pPager->pWal);
43454 }
43455 
43456 /*
43457 ** Return true if the underlying VFS for the given pager supports the
43458 ** primitives necessary for write-ahead logging.
43459 */
43460 SQLITE_PRIVATE int sqlite3PagerWalSupported(Pager *pPager){
43461   const sqlite3_io_methods *pMethods = pPager->fd->pMethods;
43462   return pPager->exclusiveMode || (pMethods->iVersion>=2 && pMethods->xShmMap);
43463 }
43464 
43465 /*
43466 ** Attempt to take an exclusive lock on the database file. If a PENDING lock
43467 ** is obtained instead, immediately release it.
43468 */
43469 static int pagerExclusiveLock(Pager *pPager){
43470   int rc;                         /* Return code */
43471 
43472   assert( pPager->eLock==SHARED_LOCK || pPager->eLock==EXCLUSIVE_LOCK );
43473   rc = pagerLockDb(pPager, EXCLUSIVE_LOCK);
43474   if( rc!=SQLITE_OK ){
43475     /* If the attempt to grab the exclusive lock failed, release the
43476     ** pending lock that may have been obtained instead.  */
43477     pagerUnlockDb(pPager, SHARED_LOCK);
43478   }
43479 
43480   return rc;
43481 }
43482 
43483 /*
43484 ** Call sqlite3WalOpen() to open the WAL handle. If the pager is in
43485 ** exclusive-locking mode when this function is called, take an EXCLUSIVE
43486 ** lock on the database file and use heap-memory to store the wal-index
43487 ** in. Otherwise, use the normal shared-memory.
43488 */
43489 static int pagerOpenWal(Pager *pPager){
43490   int rc = SQLITE_OK;
43491 
43492   assert( pPager->pWal==0 && pPager->tempFile==0 );
43493   assert( pPager->eLock==SHARED_LOCK || pPager->eLock==EXCLUSIVE_LOCK || pPager->noReadlock);
43494 
43495   /* If the pager is already in exclusive-mode, the WAL module will use
43496   ** heap-memory for the wal-index instead of the VFS shared-memory
43497   ** implementation. Take the exclusive lock now, before opening the WAL
43498   ** file, to make sure this is safe.
43499   */
43500   if( pPager->exclusiveMode ){
43501     rc = pagerExclusiveLock(pPager);
43502   }
43503 
43504   /* Open the connection to the log file. If this operation fails,
43505   ** (e.g. due to malloc() failure), return an error code.
43506   */
43507   if( rc==SQLITE_OK ){
43508     rc = sqlite3WalOpen(pPager->pVfs,
43509         pPager->fd, pPager->zWal, pPager->exclusiveMode,
43510         pPager->journalSizeLimit, &pPager->pWal
43511     );
43512   }
43513 
43514   return rc;
43515 }
43516 
43517 
43518 /*
43519 ** The caller must be holding a SHARED lock on the database file to call
43520 ** this function.
43521 **
43522 ** If the pager passed as the first argument is open on a real database
43523 ** file (not a temp file or an in-memory database), and the WAL file
43524 ** is not already open, make an attempt to open it now. If successful,
43525 ** return SQLITE_OK. If an error occurs or the VFS used by the pager does
43526 ** not support the xShmXXX() methods, return an error code. *pbOpen is
43527 ** not modified in either case.
43528 **
43529 ** If the pager is open on a temp-file (or in-memory database), or if
43530 ** the WAL file is already open, set *pbOpen to 1 and return SQLITE_OK
43531 ** without doing anything.
43532 */
43533 SQLITE_PRIVATE int sqlite3PagerOpenWal(
43534   Pager *pPager,                  /* Pager object */
43535   int *pbOpen                     /* OUT: Set to true if call is a no-op */
43536 ){
43537   int rc = SQLITE_OK;             /* Return code */
43538 
43539   assert( assert_pager_state(pPager) );
43540   assert( pPager->eState==PAGER_OPEN   || pbOpen );
43541   assert( pPager->eState==PAGER_READER || !pbOpen );
43542   assert( pbOpen==0 || *pbOpen==0 );
43543   assert( pbOpen!=0 || (!pPager->tempFile && !pPager->pWal) );
43544 
43545   if( !pPager->tempFile && !pPager->pWal ){
43546     if( !sqlite3PagerWalSupported(pPager) ) return SQLITE_CANTOPEN;
43547 
43548     /* Close any rollback journal previously open */
43549     sqlite3OsClose(pPager->jfd);
43550 
43551     rc = pagerOpenWal(pPager);
43552     if( rc==SQLITE_OK ){
43553       pPager->journalMode = PAGER_JOURNALMODE_WAL;
43554       pPager->eState = PAGER_OPEN;
43555     }
43556   }else{
43557     *pbOpen = 1;
43558   }
43559 
43560   return rc;
43561 }
43562 
43563 /*
43564 ** This function is called to close the connection to the log file prior
43565 ** to switching from WAL to rollback mode.
43566 **
43567 ** Before closing the log file, this function attempts to take an
43568 ** EXCLUSIVE lock on the database file. If this cannot be obtained, an
43569 ** error (SQLITE_BUSY) is returned and the log connection is not closed.
43570 ** If successful, the EXCLUSIVE lock is not released before returning.
43571 */
43572 SQLITE_PRIVATE int sqlite3PagerCloseWal(Pager *pPager){
43573   int rc = SQLITE_OK;
43574 
43575   assert( pPager->journalMode==PAGER_JOURNALMODE_WAL );
43576 
43577   /* If the log file is not already open, but does exist in the file-system,
43578   ** it may need to be checkpointed before the connection can switch to
43579   ** rollback mode. Open it now so this can happen.
43580   */
43581   if( !pPager->pWal ){
43582     int logexists = 0;
43583     rc = pagerLockDb(pPager, SHARED_LOCK);
43584     if( rc==SQLITE_OK ){
43585       rc = sqlite3OsAccess(
43586           pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS, &logexists
43587       );
43588     }
43589     if( rc==SQLITE_OK && logexists ){
43590       rc = pagerOpenWal(pPager);
43591     }
43592   }
43593 
43594   /* Checkpoint and close the log. Because an EXCLUSIVE lock is held on
43595   ** the database file, the log and log-summary files will be deleted.
43596   */
43597   if( rc==SQLITE_OK && pPager->pWal ){
43598     rc = pagerExclusiveLock(pPager);
43599     if( rc==SQLITE_OK ){
43600       rc = sqlite3WalClose(pPager->pWal, pPager->ckptSyncFlags,
43601                            pPager->pageSize, (u8*)pPager->pTmpSpace);
43602       pPager->pWal = 0;
43603     }
43604   }
43605   return rc;
43606 }
43607 
43608 #ifdef SQLITE_HAS_CODEC
43609 /*
43610 ** This function is called by the wal module when writing page content
43611 ** into the log file.
43612 **
43613 ** This function returns a pointer to a buffer containing the encrypted
43614 ** page content. If a malloc fails, this function may return NULL.
43615 */
43616 SQLITE_PRIVATE void *sqlite3PagerCodec(PgHdr *pPg){
43617   void *aData = 0;
43618   CODEC2(pPg->pPager, pPg->pData, pPg->pgno, 6, return 0, aData);
43619   return aData;
43620 }
43621 #endif /* SQLITE_HAS_CODEC */
43622 
43623 #endif /* !SQLITE_OMIT_WAL */
43624 
43625 #endif /* SQLITE_OMIT_DISKIO */
43626 
43627 /************** End of pager.c ***********************************************/
43628 /************** Begin file wal.c *********************************************/
43629 /*
43630 ** 2010 February 1
43631 **
43632 ** The author disclaims copyright to this source code.  In place of
43633 ** a legal notice, here is a blessing:
43634 **
43635 **    May you do good and not evil.
43636 **    May you find forgiveness for yourself and forgive others.
43637 **    May you share freely, never taking more than you give.
43638 **
43639 *************************************************************************
43640 **
43641 ** This file contains the implementation of a write-ahead log (WAL) used in
43642 ** "journal_mode=WAL" mode.
43643 **
43644 ** WRITE-AHEAD LOG (WAL) FILE FORMAT
43645 **
43646 ** A WAL file consists of a header followed by zero or more "frames".
43647 ** Each frame records the revised content of a single page from the
43648 ** database file.  All changes to the database are recorded by writing
43649 ** frames into the WAL.  Transactions commit when a frame is written that
43650 ** contains a commit marker.  A single WAL can and usually does record
43651 ** multiple transactions.  Periodically, the content of the WAL is
43652 ** transferred back into the database file in an operation called a
43653 ** "checkpoint".
43654 **
43655 ** A single WAL file can be used multiple times.  In other words, the
43656 ** WAL can fill up with frames and then be checkpointed and then new
43657 ** frames can overwrite the old ones.  A WAL always grows from beginning
43658 ** toward the end.  Checksums and counters attached to each frame are
43659 ** used to determine which frames within the WAL are valid and which
43660 ** are leftovers from prior checkpoints.
43661 **
43662 ** The WAL header is 32 bytes in size and consists of the following eight
43663 ** big-endian 32-bit unsigned integer values:
43664 **
43665 **     0: Magic number.  0x377f0682 or 0x377f0683
43666 **     4: File format version.  Currently 3007000
43667 **     8: Database page size.  Example: 1024
43668 **    12: Checkpoint sequence number
43669 **    16: Salt-1, random integer incremented with each checkpoint
43670 **    20: Salt-2, a different random integer changing with each ckpt
43671 **    24: Checksum-1 (first part of checksum for first 24 bytes of header).
43672 **    28: Checksum-2 (second part of checksum for first 24 bytes of header).
43673 **
43674 ** Immediately following the wal-header are zero or more frames. Each
43675 ** frame consists of a 24-byte frame-header followed by a <page-size> bytes
43676 ** of page data. The frame-header is six big-endian 32-bit unsigned
43677 ** integer values, as follows:
43678 **
43679 **     0: Page number.
43680 **     4: For commit records, the size of the database image in pages
43681 **        after the commit. For all other records, zero.
43682 **     8: Salt-1 (copied from the header)
43683 **    12: Salt-2 (copied from the header)
43684 **    16: Checksum-1.
43685 **    20: Checksum-2.
43686 **
43687 ** A frame is considered valid if and only if the following conditions are
43688 ** true:
43689 **
43690 **    (1) The salt-1 and salt-2 values in the frame-header match
43691 **        salt values in the wal-header
43692 **
43693 **    (2) The checksum values in the final 8 bytes of the frame-header
43694 **        exactly match the checksum computed consecutively on the
43695 **        WAL header and the first 8 bytes and the content of all frames
43696 **        up to and including the current frame.
43697 **
43698 ** The checksum is computed using 32-bit big-endian integers if the
43699 ** magic number in the first 4 bytes of the WAL is 0x377f0683 and it
43700 ** is computed using little-endian if the magic number is 0x377f0682.
43701 ** The checksum values are always stored in the frame header in a
43702 ** big-endian format regardless of which byte order is used to compute
43703 ** the checksum.  The checksum is computed by interpreting the input as
43704 ** an even number of unsigned 32-bit integers: x[0] through x[N].  The
43705 ** algorithm used for the checksum is as follows:
43706 **
43707 **   for i from 0 to n-1 step 2:
43708 **     s0 += x[i] + s1;
43709 **     s1 += x[i+1] + s0;
43710 **   endfor
43711 **
43712 ** Note that s0 and s1 are both weighted checksums using fibonacci weights
43713 ** in reverse order (the largest fibonacci weight occurs on the first element
43714 ** of the sequence being summed.)  The s1 value spans all 32-bit
43715 ** terms of the sequence whereas s0 omits the final term.
43716 **
43717 ** On a checkpoint, the WAL is first VFS.xSync-ed, then valid content of the
43718 ** WAL is transferred into the database, then the database is VFS.xSync-ed.
43719 ** The VFS.xSync operations serve as write barriers - all writes launched
43720 ** before the xSync must complete before any write that launches after the
43721 ** xSync begins.
43722 **
43723 ** After each checkpoint, the salt-1 value is incremented and the salt-2
43724 ** value is randomized.  This prevents old and new frames in the WAL from
43725 ** being considered valid at the same time and being checkpointing together
43726 ** following a crash.
43727 **
43728 ** READER ALGORITHM
43729 **
43730 ** To read a page from the database (call it page number P), a reader
43731 ** first checks the WAL to see if it contains page P.  If so, then the
43732 ** last valid instance of page P that is a followed by a commit frame
43733 ** or is a commit frame itself becomes the value read.  If the WAL
43734 ** contains no copies of page P that are valid and which are a commit
43735 ** frame or are followed by a commit frame, then page P is read from
43736 ** the database file.
43737 **
43738 ** To start a read transaction, the reader records the index of the last
43739 ** valid frame in the WAL.  The reader uses this recorded "mxFrame" value
43740 ** for all subsequent read operations.  New transactions can be appended
43741 ** to the WAL, but as long as the reader uses its original mxFrame value
43742 ** and ignores the newly appended content, it will see a consistent snapshot
43743 ** of the database from a single point in time.  This technique allows
43744 ** multiple concurrent readers to view different versions of the database
43745 ** content simultaneously.
43746 **
43747 ** The reader algorithm in the previous paragraphs works correctly, but
43748 ** because frames for page P can appear anywhere within the WAL, the
43749 ** reader has to scan the entire WAL looking for page P frames.  If the
43750 ** WAL is large (multiple megabytes is typical) that scan can be slow,
43751 ** and read performance suffers.  To overcome this problem, a separate
43752 ** data structure called the wal-index is maintained to expedite the
43753 ** search for frames of a particular page.
43754 **
43755 ** WAL-INDEX FORMAT
43756 **
43757 ** Conceptually, the wal-index is shared memory, though VFS implementations
43758 ** might choose to implement the wal-index using a mmapped file.  Because
43759 ** the wal-index is shared memory, SQLite does not support journal_mode=WAL
43760 ** on a network filesystem.  All users of the database must be able to
43761 ** share memory.
43762 **
43763 ** The wal-index is transient.  After a crash, the wal-index can (and should
43764 ** be) reconstructed from the original WAL file.  In fact, the VFS is required
43765 ** to either truncate or zero the header of the wal-index when the last
43766 ** connection to it closes.  Because the wal-index is transient, it can
43767 ** use an architecture-specific format; it does not have to be cross-platform.
43768 ** Hence, unlike the database and WAL file formats which store all values
43769 ** as big endian, the wal-index can store multi-byte values in the native
43770 ** byte order of the host computer.
43771 **
43772 ** The purpose of the wal-index is to answer this question quickly:  Given
43773 ** a page number P, return the index of the last frame for page P in the WAL,
43774 ** or return NULL if there are no frames for page P in the WAL.
43775 **
43776 ** The wal-index consists of a header region, followed by an one or
43777 ** more index blocks.
43778 **
43779 ** The wal-index header contains the total number of frames within the WAL
43780 ** in the the mxFrame field.
43781 **
43782 ** Each index block except for the first contains information on
43783 ** HASHTABLE_NPAGE frames. The first index block contains information on
43784 ** HASHTABLE_NPAGE_ONE frames. The values of HASHTABLE_NPAGE_ONE and
43785 ** HASHTABLE_NPAGE are selected so that together the wal-index header and
43786 ** first index block are the same size as all other index blocks in the
43787 ** wal-index.
43788 **
43789 ** Each index block contains two sections, a page-mapping that contains the
43790 ** database page number associated with each wal frame, and a hash-table
43791 ** that allows readers to query an index block for a specific page number.
43792 ** The page-mapping is an array of HASHTABLE_NPAGE (or HASHTABLE_NPAGE_ONE
43793 ** for the first index block) 32-bit page numbers. The first entry in the
43794 ** first index-block contains the database page number corresponding to the
43795 ** first frame in the WAL file. The first entry in the second index block
43796 ** in the WAL file corresponds to the (HASHTABLE_NPAGE_ONE+1)th frame in
43797 ** the log, and so on.
43798 **
43799 ** The last index block in a wal-index usually contains less than the full
43800 ** complement of HASHTABLE_NPAGE (or HASHTABLE_NPAGE_ONE) page-numbers,
43801 ** depending on the contents of the WAL file. This does not change the
43802 ** allocated size of the page-mapping array - the page-mapping array merely
43803 ** contains unused entries.
43804 **
43805 ** Even without using the hash table, the last frame for page P
43806 ** can be found by scanning the page-mapping sections of each index block
43807 ** starting with the last index block and moving toward the first, and
43808 ** within each index block, starting at the end and moving toward the
43809 ** beginning.  The first entry that equals P corresponds to the frame
43810 ** holding the content for that page.
43811 **
43812 ** The hash table consists of HASHTABLE_NSLOT 16-bit unsigned integers.
43813 ** HASHTABLE_NSLOT = 2*HASHTABLE_NPAGE, and there is one entry in the
43814 ** hash table for each page number in the mapping section, so the hash
43815 ** table is never more than half full.  The expected number of collisions
43816 ** prior to finding a match is 1.  Each entry of the hash table is an
43817 ** 1-based index of an entry in the mapping section of the same
43818 ** index block.   Let K be the 1-based index of the largest entry in
43819 ** the mapping section.  (For index blocks other than the last, K will
43820 ** always be exactly HASHTABLE_NPAGE (4096) and for the last index block
43821 ** K will be (mxFrame%HASHTABLE_NPAGE).)  Unused slots of the hash table
43822 ** contain a value of 0.
43823 **
43824 ** To look for page P in the hash table, first compute a hash iKey on
43825 ** P as follows:
43826 **
43827 **      iKey = (P * 383) % HASHTABLE_NSLOT
43828 **
43829 ** Then start scanning entries of the hash table, starting with iKey
43830 ** (wrapping around to the beginning when the end of the hash table is
43831 ** reached) until an unused hash slot is found. Let the first unused slot
43832 ** be at index iUnused.  (iUnused might be less than iKey if there was
43833 ** wrap-around.) Because the hash table is never more than half full,
43834 ** the search is guaranteed to eventually hit an unused entry.  Let
43835 ** iMax be the value between iKey and iUnused, closest to iUnused,
43836 ** where aHash[iMax]==P.  If there is no iMax entry (if there exists
43837 ** no hash slot such that aHash[i]==p) then page P is not in the
43838 ** current index block.  Otherwise the iMax-th mapping entry of the
43839 ** current index block corresponds to the last entry that references
43840 ** page P.
43841 **
43842 ** A hash search begins with the last index block and moves toward the
43843 ** first index block, looking for entries corresponding to page P.  On
43844 ** average, only two or three slots in each index block need to be
43845 ** examined in order to either find the last entry for page P, or to
43846 ** establish that no such entry exists in the block.  Each index block
43847 ** holds over 4000 entries.  So two or three index blocks are sufficient
43848 ** to cover a typical 10 megabyte WAL file, assuming 1K pages.  8 or 10
43849 ** comparisons (on average) suffice to either locate a frame in the
43850 ** WAL or to establish that the frame does not exist in the WAL.  This
43851 ** is much faster than scanning the entire 10MB WAL.
43852 **
43853 ** Note that entries are added in order of increasing K.  Hence, one
43854 ** reader might be using some value K0 and a second reader that started
43855 ** at a later time (after additional transactions were added to the WAL
43856 ** and to the wal-index) might be using a different value K1, where K1>K0.
43857 ** Both readers can use the same hash table and mapping section to get
43858 ** the correct result.  There may be entries in the hash table with
43859 ** K>K0 but to the first reader, those entries will appear to be unused
43860 ** slots in the hash table and so the first reader will get an answer as
43861 ** if no values greater than K0 had ever been inserted into the hash table
43862 ** in the first place - which is what reader one wants.  Meanwhile, the
43863 ** second reader using K1 will see additional values that were inserted
43864 ** later, which is exactly what reader two wants.
43865 **
43866 ** When a rollback occurs, the value of K is decreased. Hash table entries
43867 ** that correspond to frames greater than the new K value are removed
43868 ** from the hash table at this point.
43869 */
43870 #ifndef SQLITE_OMIT_WAL
43871 
43872 
43873 /*
43874 ** Trace output macros
43875 */
43876 #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
43877 SQLITE_PRIVATE int sqlite3WalTrace = 0;
43878 # define WALTRACE(X)  if(sqlite3WalTrace) sqlite3DebugPrintf X
43879 #else
43880 # define WALTRACE(X)
43881 #endif
43882 
43883 /*
43884 ** The maximum (and only) versions of the wal and wal-index formats
43885 ** that may be interpreted by this version of SQLite.
43886 **
43887 ** If a client begins recovering a WAL file and finds that (a) the checksum
43888 ** values in the wal-header are correct and (b) the version field is not
43889 ** WAL_MAX_VERSION, recovery fails and SQLite returns SQLITE_CANTOPEN.
43890 **
43891 ** Similarly, if a client successfully reads a wal-index header (i.e. the
43892 ** checksum test is successful) and finds that the version field is not
43893 ** WALINDEX_MAX_VERSION, then no read-transaction is opened and SQLite
43894 ** returns SQLITE_CANTOPEN.
43895 */
43896 #define WAL_MAX_VERSION      3007000
43897 #define WALINDEX_MAX_VERSION 3007000
43898 
43899 /*
43900 ** Indices of various locking bytes.   WAL_NREADER is the number
43901 ** of available reader locks and should be at least 3.
43902 */
43903 #define WAL_WRITE_LOCK         0
43904 #define WAL_ALL_BUT_WRITE      1
43905 #define WAL_CKPT_LOCK          1
43906 #define WAL_RECOVER_LOCK       2
43907 #define WAL_READ_LOCK(I)       (3+(I))
43908 #define WAL_NREADER            (SQLITE_SHM_NLOCK-3)
43909 
43910 
43911 /* Object declarations */
43912 typedef struct WalIndexHdr WalIndexHdr;
43913 typedef struct WalIterator WalIterator;
43914 typedef struct WalCkptInfo WalCkptInfo;
43915 
43916 
43917 /*
43918 ** The following object holds a copy of the wal-index header content.
43919 **
43920 ** The actual header in the wal-index consists of two copies of this
43921 ** object.
43922 **
43923 ** The szPage value can be any power of 2 between 512 and 32768, inclusive.
43924 ** Or it can be 1 to represent a 65536-byte page.  The latter case was
43925 ** added in 3.7.1 when support for 64K pages was added.
43926 */
43927 struct WalIndexHdr {
43928   u32 iVersion;                   /* Wal-index version */
43929   u32 unused;                     /* Unused (padding) field */
43930   u32 iChange;                    /* Counter incremented each transaction */
43931   u8 isInit;                      /* 1 when initialized */
43932   u8 bigEndCksum;                 /* True if checksums in WAL are big-endian */
43933   u16 szPage;                     /* Database page size in bytes. 1==64K */
43934   u32 mxFrame;                    /* Index of last valid frame in the WAL */
43935   u32 nPage;                      /* Size of database in pages */
43936   u32 aFrameCksum[2];             /* Checksum of last frame in log */
43937   u32 aSalt[2];                   /* Two salt values copied from WAL header */
43938   u32 aCksum[2];                  /* Checksum over all prior fields */
43939 };
43940 
43941 /*
43942 ** A copy of the following object occurs in the wal-index immediately
43943 ** following the second copy of the WalIndexHdr.  This object stores
43944 ** information used by checkpoint.
43945 **
43946 ** nBackfill is the number of frames in the WAL that have been written
43947 ** back into the database. (We call the act of moving content from WAL to
43948 ** database "backfilling".)  The nBackfill number is never greater than
43949 ** WalIndexHdr.mxFrame.  nBackfill can only be increased by threads
43950 ** holding the WAL_CKPT_LOCK lock (which includes a recovery thread).
43951 ** However, a WAL_WRITE_LOCK thread can move the value of nBackfill from
43952 ** mxFrame back to zero when the WAL is reset.
43953 **
43954 ** There is one entry in aReadMark[] for each reader lock.  If a reader
43955 ** holds read-lock K, then the value in aReadMark[K] is no greater than
43956 ** the mxFrame for that reader.  The value READMARK_NOT_USED (0xffffffff)
43957 ** for any aReadMark[] means that entry is unused.  aReadMark[0] is
43958 ** a special case; its value is never used and it exists as a place-holder
43959 ** to avoid having to offset aReadMark[] indexs by one.  Readers holding
43960 ** WAL_READ_LOCK(0) always ignore the entire WAL and read all content
43961 ** directly from the database.
43962 **
43963 ** The value of aReadMark[K] may only be changed by a thread that
43964 ** is holding an exclusive lock on WAL_READ_LOCK(K).  Thus, the value of
43965 ** aReadMark[K] cannot changed while there is a reader is using that mark
43966 ** since the reader will be holding a shared lock on WAL_READ_LOCK(K).
43967 **
43968 ** The checkpointer may only transfer frames from WAL to database where
43969 ** the frame numbers are less than or equal to every aReadMark[] that is
43970 ** in use (that is, every aReadMark[j] for which there is a corresponding
43971 ** WAL_READ_LOCK(j)).  New readers (usually) pick the aReadMark[] with the
43972 ** largest value and will increase an unused aReadMark[] to mxFrame if there
43973 ** is not already an aReadMark[] equal to mxFrame.  The exception to the
43974 ** previous sentence is when nBackfill equals mxFrame (meaning that everything
43975 ** in the WAL has been backfilled into the database) then new readers
43976 ** will choose aReadMark[0] which has value 0 and hence such reader will
43977 ** get all their all content directly from the database file and ignore
43978 ** the WAL.
43979 **
43980 ** Writers normally append new frames to the end of the WAL.  However,
43981 ** if nBackfill equals mxFrame (meaning that all WAL content has been
43982 ** written back into the database) and if no readers are using the WAL
43983 ** (in other words, if there are no WAL_READ_LOCK(i) where i>0) then
43984 ** the writer will first "reset" the WAL back to the beginning and start
43985 ** writing new content beginning at frame 1.
43986 **
43987 ** We assume that 32-bit loads are atomic and so no locks are needed in
43988 ** order to read from any aReadMark[] entries.
43989 */
43990 struct WalCkptInfo {
43991   u32 nBackfill;                  /* Number of WAL frames backfilled into DB */
43992   u32 aReadMark[WAL_NREADER];     /* Reader marks */
43993 };
43994 #define READMARK_NOT_USED  0xffffffff
43995 
43996 
43997 /* A block of WALINDEX_LOCK_RESERVED bytes beginning at
43998 ** WALINDEX_LOCK_OFFSET is reserved for locks. Since some systems
43999 ** only support mandatory file-locks, we do not read or write data
44000 ** from the region of the file on which locks are applied.
44001 */
44002 #define WALINDEX_LOCK_OFFSET   (sizeof(WalIndexHdr)*2 + sizeof(WalCkptInfo))
44003 #define WALINDEX_LOCK_RESERVED 16
44004 #define WALINDEX_HDR_SIZE      (WALINDEX_LOCK_OFFSET+WALINDEX_LOCK_RESERVED)
44005 
44006 /* Size of header before each frame in wal */
44007 #define WAL_FRAME_HDRSIZE 24
44008 
44009 /* Size of write ahead log header, including checksum. */
44010 /* #define WAL_HDRSIZE 24 */
44011 #define WAL_HDRSIZE 32
44012 
44013 /* WAL magic value. Either this value, or the same value with the least
44014 ** significant bit also set (WAL_MAGIC | 0x00000001) is stored in 32-bit
44015 ** big-endian format in the first 4 bytes of a WAL file.
44016 **
44017 ** If the LSB is set, then the checksums for each frame within the WAL
44018 ** file are calculated by treating all data as an array of 32-bit
44019 ** big-endian words. Otherwise, they are calculated by interpreting
44020 ** all data as 32-bit little-endian words.
44021 */
44022 #define WAL_MAGIC 0x377f0682
44023 
44024 /*
44025 ** Return the offset of frame iFrame in the write-ahead log file,
44026 ** assuming a database page size of szPage bytes. The offset returned
44027 ** is to the start of the write-ahead log frame-header.
44028 */
44029 #define walFrameOffset(iFrame, szPage) (                               \
44030   WAL_HDRSIZE + ((iFrame)-1)*(i64)((szPage)+WAL_FRAME_HDRSIZE)         \
44031 )
44032 
44033 /*
44034 ** An open write-ahead log file is represented by an instance of the
44035 ** following object.
44036 */
44037 struct Wal {
44038   sqlite3_vfs *pVfs;         /* The VFS used to create pDbFd */
44039   sqlite3_file *pDbFd;       /* File handle for the database file */
44040   sqlite3_file *pWalFd;      /* File handle for WAL file */
44041   u32 iCallback;             /* Value to pass to log callback (or 0) */
44042   i64 mxWalSize;             /* Truncate WAL to this size upon reset */
44043   int nWiData;               /* Size of array apWiData */
44044   volatile u32 **apWiData;   /* Pointer to wal-index content in memory */
44045   u32 szPage;                /* Database page size */
44046   i16 readLock;              /* Which read lock is being held.  -1 for none */
44047   u8 exclusiveMode;          /* Non-zero if connection is in exclusive mode */
44048   u8 writeLock;              /* True if in a write transaction */
44049   u8 ckptLock;               /* True if holding a checkpoint lock */
44050   u8 readOnly;               /* WAL_RDWR, WAL_RDONLY, or WAL_SHM_RDONLY */
44051   WalIndexHdr hdr;           /* Wal-index header for current transaction */
44052   const char *zWalName;      /* Name of WAL file */
44053   u32 nCkpt;                 /* Checkpoint sequence counter in the wal-header */
44054 #ifdef SQLITE_DEBUG
44055   u8 lockError;              /* True if a locking error has occurred */
44056 #endif
44057 };
44058 
44059 /*
44060 ** Candidate values for Wal.exclusiveMode.
44061 */
44062 #define WAL_NORMAL_MODE     0
44063 #define WAL_EXCLUSIVE_MODE  1
44064 #define WAL_HEAPMEMORY_MODE 2
44065 
44066 /*
44067 ** Possible values for WAL.readOnly
44068 */
44069 #define WAL_RDWR        0    /* Normal read/write connection */
44070 #define WAL_RDONLY      1    /* The WAL file is readonly */
44071 #define WAL_SHM_RDONLY  2    /* The SHM file is readonly */
44072 
44073 /*
44074 ** Each page of the wal-index mapping contains a hash-table made up of
44075 ** an array of HASHTABLE_NSLOT elements of the following type.
44076 */
44077 typedef u16 ht_slot;
44078 
44079 /*
44080 ** This structure is used to implement an iterator that loops through
44081 ** all frames in the WAL in database page order. Where two or more frames
44082 ** correspond to the same database page, the iterator visits only the
44083 ** frame most recently written to the WAL (in other words, the frame with
44084 ** the largest index).
44085 **
44086 ** The internals of this structure are only accessed by:
44087 **
44088 **   walIteratorInit() - Create a new iterator,
44089 **   walIteratorNext() - Step an iterator,
44090 **   walIteratorFree() - Free an iterator.
44091 **
44092 ** This functionality is used by the checkpoint code (see walCheckpoint()).
44093 */
44094 struct WalIterator {
44095   int iPrior;                     /* Last result returned from the iterator */
44096   int nSegment;                   /* Number of entries in aSegment[] */
44097   struct WalSegment {
44098     int iNext;                    /* Next slot in aIndex[] not yet returned */
44099     ht_slot *aIndex;              /* i0, i1, i2... such that aPgno[iN] ascend */
44100     u32 *aPgno;                   /* Array of page numbers. */
44101     int nEntry;                   /* Nr. of entries in aPgno[] and aIndex[] */
44102     int iZero;                    /* Frame number associated with aPgno[0] */
44103   } aSegment[1];                  /* One for every 32KB page in the wal-index */
44104 };
44105 
44106 /*
44107 ** Define the parameters of the hash tables in the wal-index file. There
44108 ** is a hash-table following every HASHTABLE_NPAGE page numbers in the
44109 ** wal-index.
44110 **
44111 ** Changing any of these constants will alter the wal-index format and
44112 ** create incompatibilities.
44113 */
44114 #define HASHTABLE_NPAGE      4096                 /* Must be power of 2 */
44115 #define HASHTABLE_HASH_1     383                  /* Should be prime */
44116 #define HASHTABLE_NSLOT      (HASHTABLE_NPAGE*2)  /* Must be a power of 2 */
44117 
44118 /*
44119 ** The block of page numbers associated with the first hash-table in a
44120 ** wal-index is smaller than usual. This is so that there is a complete
44121 ** hash-table on each aligned 32KB page of the wal-index.
44122 */
44123 #define HASHTABLE_NPAGE_ONE  (HASHTABLE_NPAGE - (WALINDEX_HDR_SIZE/sizeof(u32)))
44124 
44125 /* The wal-index is divided into pages of WALINDEX_PGSZ bytes each. */
44126 #define WALINDEX_PGSZ   (                                         \
44127     sizeof(ht_slot)*HASHTABLE_NSLOT + HASHTABLE_NPAGE*sizeof(u32) \
44128 )
44129 
44130 /*
44131 ** Obtain a pointer to the iPage'th page of the wal-index. The wal-index
44132 ** is broken into pages of WALINDEX_PGSZ bytes. Wal-index pages are
44133 ** numbered from zero.
44134 **
44135 ** If this call is successful, *ppPage is set to point to the wal-index
44136 ** page and SQLITE_OK is returned. If an error (an OOM or VFS error) occurs,
44137 ** then an SQLite error code is returned and *ppPage is set to 0.
44138 */
44139 static int walIndexPage(Wal *pWal, int iPage, volatile u32 **ppPage){
44140   int rc = SQLITE_OK;
44141 
44142   /* Enlarge the pWal->apWiData[] array if required */
44143   if( pWal->nWiData<=iPage ){
44144     int nByte = sizeof(u32*)*(iPage+1);
44145     volatile u32 **apNew;
44146     apNew = (volatile u32 **)sqlite3_realloc((void *)pWal->apWiData, nByte);
44147     if( !apNew ){
44148       *ppPage = 0;
44149       return SQLITE_NOMEM;
44150     }
44151     memset((void*)&apNew[pWal->nWiData], 0,
44152            sizeof(u32*)*(iPage+1-pWal->nWiData));
44153     pWal->apWiData = apNew;
44154     pWal->nWiData = iPage+1;
44155   }
44156 
44157   /* Request a pointer to the required page from the VFS */
44158   if( pWal->apWiData[iPage]==0 ){
44159     if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE ){
44160       pWal->apWiData[iPage] = (u32 volatile *)sqlite3MallocZero(WALINDEX_PGSZ);
44161       if( !pWal->apWiData[iPage] ) rc = SQLITE_NOMEM;
44162     }else{
44163       rc = sqlite3OsShmMap(pWal->pDbFd, iPage, WALINDEX_PGSZ,
44164           pWal->writeLock, (void volatile **)&pWal->apWiData[iPage]
44165       );
44166       if( rc==SQLITE_READONLY ){
44167         pWal->readOnly |= WAL_SHM_RDONLY;
44168         rc = SQLITE_OK;
44169       }
44170     }
44171   }
44172 
44173   *ppPage = pWal->apWiData[iPage];
44174   assert( iPage==0 || *ppPage || rc!=SQLITE_OK );
44175   return rc;
44176 }
44177 
44178 /*
44179 ** Return a pointer to the WalCkptInfo structure in the wal-index.
44180 */
44181 static volatile WalCkptInfo *walCkptInfo(Wal *pWal){
44182   assert( pWal->nWiData>0 && pWal->apWiData[0] );
44183   return (volatile WalCkptInfo*)&(pWal->apWiData[0][sizeof(WalIndexHdr)/2]);
44184 }
44185 
44186 /*
44187 ** Return a pointer to the WalIndexHdr structure in the wal-index.
44188 */
44189 static volatile WalIndexHdr *walIndexHdr(Wal *pWal){
44190   assert( pWal->nWiData>0 && pWal->apWiData[0] );
44191   return (volatile WalIndexHdr*)pWal->apWiData[0];
44192 }
44193 
44194 /*
44195 ** The argument to this macro must be of type u32. On a little-endian
44196 ** architecture, it returns the u32 value that results from interpreting
44197 ** the 4 bytes as a big-endian value. On a big-endian architecture, it
44198 ** returns the value that would be produced by intepreting the 4 bytes
44199 ** of the input value as a little-endian integer.
44200 */
44201 #define BYTESWAP32(x) ( \
44202     (((x)&0x000000FF)<<24) + (((x)&0x0000FF00)<<8)  \
44203   + (((x)&0x00FF0000)>>8)  + (((x)&0xFF000000)>>24) \
44204 )
44205 
44206 /*
44207 ** Generate or extend an 8 byte checksum based on the data in
44208 ** array aByte[] and the initial values of aIn[0] and aIn[1] (or
44209 ** initial values of 0 and 0 if aIn==NULL).
44210 **
44211 ** The checksum is written back into aOut[] before returning.
44212 **
44213 ** nByte must be a positive multiple of 8.
44214 */
44215 static void walChecksumBytes(
44216   int nativeCksum, /* True for native byte-order, false for non-native */
44217   u8 *a,           /* Content to be checksummed */
44218   int nByte,       /* Bytes of content in a[].  Must be a multiple of 8. */
44219   const u32 *aIn,  /* Initial checksum value input */
44220   u32 *aOut        /* OUT: Final checksum value output */
44221 ){
44222   u32 s1, s2;
44223   u32 *aData = (u32 *)a;
44224   u32 *aEnd = (u32 *)&a[nByte];
44225 
44226   if( aIn ){
44227     s1 = aIn[0];
44228     s2 = aIn[1];
44229   }else{
44230     s1 = s2 = 0;
44231   }
44232 
44233   assert( nByte>=8 );
44234   assert( (nByte&0x00000007)==0 );
44235 
44236   if( nativeCksum ){
44237     do {
44238       s1 += *aData++ + s2;
44239       s2 += *aData++ + s1;
44240     }while( aData<aEnd );
44241   }else{
44242     do {
44243       s1 += BYTESWAP32(aData[0]) + s2;
44244       s2 += BYTESWAP32(aData[1]) + s1;
44245       aData += 2;
44246     }while( aData<aEnd );
44247   }
44248 
44249   aOut[0] = s1;
44250   aOut[1] = s2;
44251 }
44252 
44253 static void walShmBarrier(Wal *pWal){
44254   if( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE ){
44255     sqlite3OsShmBarrier(pWal->pDbFd);
44256   }
44257 }
44258 
44259 /*
44260 ** Write the header information in pWal->hdr into the wal-index.
44261 **
44262 ** The checksum on pWal->hdr is updated before it is written.
44263 */
44264 static void walIndexWriteHdr(Wal *pWal){
44265   volatile WalIndexHdr *aHdr = walIndexHdr(pWal);
44266   const int nCksum = offsetof(WalIndexHdr, aCksum);
44267 
44268   assert( pWal->writeLock );
44269   pWal->hdr.isInit = 1;
44270   pWal->hdr.iVersion = WALINDEX_MAX_VERSION;
44271   walChecksumBytes(1, (u8*)&pWal->hdr, nCksum, 0, pWal->hdr.aCksum);
44272   memcpy((void *)&aHdr[1], (void *)&pWal->hdr, sizeof(WalIndexHdr));
44273   walShmBarrier(pWal);
44274   memcpy((void *)&aHdr[0], (void *)&pWal->hdr, sizeof(WalIndexHdr));
44275 }
44276 
44277 /*
44278 ** This function encodes a single frame header and writes it to a buffer
44279 ** supplied by the caller. A frame-header is made up of a series of
44280 ** 4-byte big-endian integers, as follows:
44281 **
44282 **     0: Page number.
44283 **     4: For commit records, the size of the database image in pages
44284 **        after the commit. For all other records, zero.
44285 **     8: Salt-1 (copied from the wal-header)
44286 **    12: Salt-2 (copied from the wal-header)
44287 **    16: Checksum-1.
44288 **    20: Checksum-2.
44289 */
44290 static void walEncodeFrame(
44291   Wal *pWal,                      /* The write-ahead log */
44292   u32 iPage,                      /* Database page number for frame */
44293   u32 nTruncate,                  /* New db size (or 0 for non-commit frames) */
44294   u8 *aData,                      /* Pointer to page data */
44295   u8 *aFrame                      /* OUT: Write encoded frame here */
44296 ){
44297   int nativeCksum;                /* True for native byte-order checksums */
44298   u32 *aCksum = pWal->hdr.aFrameCksum;
44299   assert( WAL_FRAME_HDRSIZE==24 );
44300   sqlite3Put4byte(&aFrame[0], iPage);
44301   sqlite3Put4byte(&aFrame[4], nTruncate);
44302   memcpy(&aFrame[8], pWal->hdr.aSalt, 8);
44303 
44304   nativeCksum = (pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN);
44305   walChecksumBytes(nativeCksum, aFrame, 8, aCksum, aCksum);
44306   walChecksumBytes(nativeCksum, aData, pWal->szPage, aCksum, aCksum);
44307 
44308   sqlite3Put4byte(&aFrame[16], aCksum[0]);
44309   sqlite3Put4byte(&aFrame[20], aCksum[1]);
44310 }
44311 
44312 /*
44313 ** Check to see if the frame with header in aFrame[] and content
44314 ** in aData[] is valid.  If it is a valid frame, fill *piPage and
44315 ** *pnTruncate and return true.  Return if the frame is not valid.
44316 */
44317 static int walDecodeFrame(
44318   Wal *pWal,                      /* The write-ahead log */
44319   u32 *piPage,                    /* OUT: Database page number for frame */
44320   u32 *pnTruncate,                /* OUT: New db size (or 0 if not commit) */
44321   u8 *aData,                      /* Pointer to page data (for checksum) */
44322   u8 *aFrame                      /* Frame data */
44323 ){
44324   int nativeCksum;                /* True for native byte-order checksums */
44325   u32 *aCksum = pWal->hdr.aFrameCksum;
44326   u32 pgno;                       /* Page number of the frame */
44327   assert( WAL_FRAME_HDRSIZE==24 );
44328 
44329   /* A frame is only valid if the salt values in the frame-header
44330   ** match the salt values in the wal-header.
44331   */
44332   if( memcmp(&pWal->hdr.aSalt, &aFrame[8], 8)!=0 ){
44333     return 0;
44334   }
44335 
44336   /* A frame is only valid if the page number is creater than zero.
44337   */
44338   pgno = sqlite3Get4byte(&aFrame[0]);
44339   if( pgno==0 ){
44340     return 0;
44341   }
44342 
44343   /* A frame is only valid if a checksum of the WAL header,
44344   ** all prior frams, the first 16 bytes of this frame-header,
44345   ** and the frame-data matches the checksum in the last 8
44346   ** bytes of this frame-header.
44347   */
44348   nativeCksum = (pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN);
44349   walChecksumBytes(nativeCksum, aFrame, 8, aCksum, aCksum);
44350   walChecksumBytes(nativeCksum, aData, pWal->szPage, aCksum, aCksum);
44351   if( aCksum[0]!=sqlite3Get4byte(&aFrame[16])
44352    || aCksum[1]!=sqlite3Get4byte(&aFrame[20])
44353   ){
44354     /* Checksum failed. */
44355     return 0;
44356   }
44357 
44358   /* If we reach this point, the frame is valid.  Return the page number
44359   ** and the new database size.
44360   */
44361   *piPage = pgno;
44362   *pnTruncate = sqlite3Get4byte(&aFrame[4]);
44363   return 1;
44364 }
44365 
44366 
44367 #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
44368 /*
44369 ** Names of locks.  This routine is used to provide debugging output and is not
44370 ** a part of an ordinary build.
44371 */
44372 static const char *walLockName(int lockIdx){
44373   if( lockIdx==WAL_WRITE_LOCK ){
44374     return "WRITE-LOCK";
44375   }else if( lockIdx==WAL_CKPT_LOCK ){
44376     return "CKPT-LOCK";
44377   }else if( lockIdx==WAL_RECOVER_LOCK ){
44378     return "RECOVER-LOCK";
44379   }else{
44380     static char zName[15];
44381     sqlite3_snprintf(sizeof(zName), zName, "READ-LOCK[%d]",
44382                      lockIdx-WAL_READ_LOCK(0));
44383     return zName;
44384   }
44385 }
44386 #endif /*defined(SQLITE_TEST) || defined(SQLITE_DEBUG) */
44387 
44388 
44389 /*
44390 ** Set or release locks on the WAL.  Locks are either shared or exclusive.
44391 ** A lock cannot be moved directly between shared and exclusive - it must go
44392 ** through the unlocked state first.
44393 **
44394 ** In locking_mode=EXCLUSIVE, all of these routines become no-ops.
44395 */
44396 static int walLockShared(Wal *pWal, int lockIdx){
44397   int rc;
44398   if( pWal->exclusiveMode ) return SQLITE_OK;
44399   rc = sqlite3OsShmLock(pWal->pDbFd, lockIdx, 1,
44400                         SQLITE_SHM_LOCK | SQLITE_SHM_SHARED);
44401   WALTRACE(("WAL%p: acquire SHARED-%s %s\n", pWal,
44402             walLockName(lockIdx), rc ? "failed" : "ok"));
44403   VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && rc!=SQLITE_BUSY); )
44404   return rc;
44405 }
44406 static void walUnlockShared(Wal *pWal, int lockIdx){
44407   if( pWal->exclusiveMode ) return;
44408   (void)sqlite3OsShmLock(pWal->pDbFd, lockIdx, 1,
44409                          SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED);
44410   WALTRACE(("WAL%p: release SHARED-%s\n", pWal, walLockName(lockIdx)));
44411 }
44412 static int walLockExclusive(Wal *pWal, int lockIdx, int n){
44413   int rc;
44414   if( pWal->exclusiveMode ) return SQLITE_OK;
44415   rc = sqlite3OsShmLock(pWal->pDbFd, lockIdx, n,
44416                         SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE);
44417   WALTRACE(("WAL%p: acquire EXCLUSIVE-%s cnt=%d %s\n", pWal,
44418             walLockName(lockIdx), n, rc ? "failed" : "ok"));
44419   VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && rc!=SQLITE_BUSY); )
44420   return rc;
44421 }
44422 static void walUnlockExclusive(Wal *pWal, int lockIdx, int n){
44423   if( pWal->exclusiveMode ) return;
44424   (void)sqlite3OsShmLock(pWal->pDbFd, lockIdx, n,
44425                          SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE);
44426   WALTRACE(("WAL%p: release EXCLUSIVE-%s cnt=%d\n", pWal,
44427              walLockName(lockIdx), n));
44428 }
44429 
44430 /*
44431 ** Compute a hash on a page number.  The resulting hash value must land
44432 ** between 0 and (HASHTABLE_NSLOT-1).  The walHashNext() function advances
44433 ** the hash to the next value in the event of a collision.
44434 */
44435 static int walHash(u32 iPage){
44436   assert( iPage>0 );
44437   assert( (HASHTABLE_NSLOT & (HASHTABLE_NSLOT-1))==0 );
44438   return (iPage*HASHTABLE_HASH_1) & (HASHTABLE_NSLOT-1);
44439 }
44440 static int walNextHash(int iPriorHash){
44441   return (iPriorHash+1)&(HASHTABLE_NSLOT-1);
44442 }
44443 
44444 /*
44445 ** Return pointers to the hash table and page number array stored on
44446 ** page iHash of the wal-index. The wal-index is broken into 32KB pages
44447 ** numbered starting from 0.
44448 **
44449 ** Set output variable *paHash to point to the start of the hash table
44450 ** in the wal-index file. Set *piZero to one less than the frame
44451 ** number of the first frame indexed by this hash table. If a
44452 ** slot in the hash table is set to N, it refers to frame number
44453 ** (*piZero+N) in the log.
44454 **
44455 ** Finally, set *paPgno so that *paPgno[1] is the page number of the
44456 ** first frame indexed by the hash table, frame (*piZero+1).
44457 */
44458 static int walHashGet(
44459   Wal *pWal,                      /* WAL handle */
44460   int iHash,                      /* Find the iHash'th table */
44461   volatile ht_slot **paHash,      /* OUT: Pointer to hash index */
44462   volatile u32 **paPgno,          /* OUT: Pointer to page number array */
44463   u32 *piZero                     /* OUT: Frame associated with *paPgno[0] */
44464 ){
44465   int rc;                         /* Return code */
44466   volatile u32 *aPgno;
44467 
44468   rc = walIndexPage(pWal, iHash, &aPgno);
44469   assert( rc==SQLITE_OK || iHash>0 );
44470 
44471   if( rc==SQLITE_OK ){
44472     u32 iZero;
44473     volatile ht_slot *aHash;
44474 
44475     aHash = (volatile ht_slot *)&aPgno[HASHTABLE_NPAGE];
44476     if( iHash==0 ){
44477       aPgno = &aPgno[WALINDEX_HDR_SIZE/sizeof(u32)];
44478       iZero = 0;
44479     }else{
44480       iZero = HASHTABLE_NPAGE_ONE + (iHash-1)*HASHTABLE_NPAGE;
44481     }
44482 
44483     *paPgno = &aPgno[-1];
44484     *paHash = aHash;
44485     *piZero = iZero;
44486   }
44487   return rc;
44488 }
44489 
44490 /*
44491 ** Return the number of the wal-index page that contains the hash-table
44492 ** and page-number array that contain entries corresponding to WAL frame
44493 ** iFrame. The wal-index is broken up into 32KB pages. Wal-index pages
44494 ** are numbered starting from 0.
44495 */
44496 static int walFramePage(u32 iFrame){
44497   int iHash = (iFrame+HASHTABLE_NPAGE-HASHTABLE_NPAGE_ONE-1) / HASHTABLE_NPAGE;
44498   assert( (iHash==0 || iFrame>HASHTABLE_NPAGE_ONE)
44499        && (iHash>=1 || iFrame<=HASHTABLE_NPAGE_ONE)
44500        && (iHash<=1 || iFrame>(HASHTABLE_NPAGE_ONE+HASHTABLE_NPAGE))
44501        && (iHash>=2 || iFrame<=HASHTABLE_NPAGE_ONE+HASHTABLE_NPAGE)
44502        && (iHash<=2 || iFrame>(HASHTABLE_NPAGE_ONE+2*HASHTABLE_NPAGE))
44503   );
44504   return iHash;
44505 }
44506 
44507 /*
44508 ** Return the page number associated with frame iFrame in this WAL.
44509 */
44510 static u32 walFramePgno(Wal *pWal, u32 iFrame){
44511   int iHash = walFramePage(iFrame);
44512   if( iHash==0 ){
44513     return pWal->apWiData[0][WALINDEX_HDR_SIZE/sizeof(u32) + iFrame - 1];
44514   }
44515   return pWal->apWiData[iHash][(iFrame-1-HASHTABLE_NPAGE_ONE)%HASHTABLE_NPAGE];
44516 }
44517 
44518 /*
44519 ** Remove entries from the hash table that point to WAL slots greater
44520 ** than pWal->hdr.mxFrame.
44521 **
44522 ** This function is called whenever pWal->hdr.mxFrame is decreased due
44523 ** to a rollback or savepoint.
44524 **
44525 ** At most only the hash table containing pWal->hdr.mxFrame needs to be
44526 ** updated.  Any later hash tables will be automatically cleared when
44527 ** pWal->hdr.mxFrame advances to the point where those hash tables are
44528 ** actually needed.
44529 */
44530 static void walCleanupHash(Wal *pWal){
44531   volatile ht_slot *aHash = 0;    /* Pointer to hash table to clear */
44532   volatile u32 *aPgno = 0;        /* Page number array for hash table */
44533   u32 iZero = 0;                  /* frame == (aHash[x]+iZero) */
44534   int iLimit = 0;                 /* Zero values greater than this */
44535   int nByte;                      /* Number of bytes to zero in aPgno[] */
44536   int i;                          /* Used to iterate through aHash[] */
44537 
44538   assert( pWal->writeLock );
44539   testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE-1 );
44540   testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE );
44541   testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE+1 );
44542 
44543   if( pWal->hdr.mxFrame==0 ) return;
44544 
44545   /* Obtain pointers to the hash-table and page-number array containing
44546   ** the entry that corresponds to frame pWal->hdr.mxFrame. It is guaranteed
44547   ** that the page said hash-table and array reside on is already mapped.
44548   */
44549   assert( pWal->nWiData>walFramePage(pWal->hdr.mxFrame) );
44550   assert( pWal->apWiData[walFramePage(pWal->hdr.mxFrame)] );
44551   walHashGet(pWal, walFramePage(pWal->hdr.mxFrame), &aHash, &aPgno, &iZero);
44552 
44553   /* Zero all hash-table entries that correspond to frame numbers greater
44554   ** than pWal->hdr.mxFrame.
44555   */
44556   iLimit = pWal->hdr.mxFrame - iZero;
44557   assert( iLimit>0 );
44558   for(i=0; i<HASHTABLE_NSLOT; i++){
44559     if( aHash[i]>iLimit ){
44560       aHash[i] = 0;
44561     }
44562   }
44563 
44564   /* Zero the entries in the aPgno array that correspond to frames with
44565   ** frame numbers greater than pWal->hdr.mxFrame.
44566   */
44567   nByte = (int)((char *)aHash - (char *)&aPgno[iLimit+1]);
44568   memset((void *)&aPgno[iLimit+1], 0, nByte);
44569 
44570 #ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
44571   /* Verify that the every entry in the mapping region is still reachable
44572   ** via the hash table even after the cleanup.
44573   */
44574   if( iLimit ){
44575     int i;           /* Loop counter */
44576     int iKey;        /* Hash key */
44577     for(i=1; i<=iLimit; i++){
44578       for(iKey=walHash(aPgno[i]); aHash[iKey]; iKey=walNextHash(iKey)){
44579         if( aHash[iKey]==i ) break;
44580       }
44581       assert( aHash[iKey]==i );
44582     }
44583   }
44584 #endif /* SQLITE_ENABLE_EXPENSIVE_ASSERT */
44585 }
44586 
44587 
44588 /*
44589 ** Set an entry in the wal-index that will map database page number
44590 ** pPage into WAL frame iFrame.
44591 */
44592 static int walIndexAppend(Wal *pWal, u32 iFrame, u32 iPage){
44593   int rc;                         /* Return code */
44594   u32 iZero = 0;                  /* One less than frame number of aPgno[1] */
44595   volatile u32 *aPgno = 0;        /* Page number array */
44596   volatile ht_slot *aHash = 0;    /* Hash table */
44597 
44598   rc = walHashGet(pWal, walFramePage(iFrame), &aHash, &aPgno, &iZero);
44599 
44600   /* Assuming the wal-index file was successfully mapped, populate the
44601   ** page number array and hash table entry.
44602   */
44603   if( rc==SQLITE_OK ){
44604     int iKey;                     /* Hash table key */
44605     int idx;                      /* Value to write to hash-table slot */
44606     int nCollide;                 /* Number of hash collisions */
44607 
44608     idx = iFrame - iZero;
44609     assert( idx <= HASHTABLE_NSLOT/2 + 1 );
44610 
44611     /* If this is the first entry to be added to this hash-table, zero the
44612     ** entire hash table and aPgno[] array before proceding.
44613     */
44614     if( idx==1 ){
44615       int nByte = (int)((u8 *)&aHash[HASHTABLE_NSLOT] - (u8 *)&aPgno[1]);
44616       memset((void*)&aPgno[1], 0, nByte);
44617     }
44618 
44619     /* If the entry in aPgno[] is already set, then the previous writer
44620     ** must have exited unexpectedly in the middle of a transaction (after
44621     ** writing one or more dirty pages to the WAL to free up memory).
44622     ** Remove the remnants of that writers uncommitted transaction from
44623     ** the hash-table before writing any new entries.
44624     */
44625     if( aPgno[idx] ){
44626       walCleanupHash(pWal);
44627       assert( !aPgno[idx] );
44628     }
44629 
44630     /* Write the aPgno[] array entry and the hash-table slot. */
44631     nCollide = idx;
44632     for(iKey=walHash(iPage); aHash[iKey]; iKey=walNextHash(iKey)){
44633       if( (nCollide--)==0 ) return SQLITE_CORRUPT_BKPT;
44634     }
44635     aPgno[idx] = iPage;
44636     aHash[iKey] = (ht_slot)idx;
44637 
44638 #ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
44639     /* Verify that the number of entries in the hash table exactly equals
44640     ** the number of entries in the mapping region.
44641     */
44642     {
44643       int i;           /* Loop counter */
44644       int nEntry = 0;  /* Number of entries in the hash table */
44645       for(i=0; i<HASHTABLE_NSLOT; i++){ if( aHash[i] ) nEntry++; }
44646       assert( nEntry==idx );
44647     }
44648 
44649     /* Verify that the every entry in the mapping region is reachable
44650     ** via the hash table.  This turns out to be a really, really expensive
44651     ** thing to check, so only do this occasionally - not on every
44652     ** iteration.
44653     */
44654     if( (idx&0x3ff)==0 ){
44655       int i;           /* Loop counter */
44656       for(i=1; i<=idx; i++){
44657         for(iKey=walHash(aPgno[i]); aHash[iKey]; iKey=walNextHash(iKey)){
44658           if( aHash[iKey]==i ) break;
44659         }
44660         assert( aHash[iKey]==i );
44661       }
44662     }
44663 #endif /* SQLITE_ENABLE_EXPENSIVE_ASSERT */
44664   }
44665 
44666 
44667   return rc;
44668 }
44669 
44670 
44671 /*
44672 ** Recover the wal-index by reading the write-ahead log file.
44673 **
44674 ** This routine first tries to establish an exclusive lock on the
44675 ** wal-index to prevent other threads/processes from doing anything
44676 ** with the WAL or wal-index while recovery is running.  The
44677 ** WAL_RECOVER_LOCK is also held so that other threads will know
44678 ** that this thread is running recovery.  If unable to establish
44679 ** the necessary locks, this routine returns SQLITE_BUSY.
44680 */
44681 static int walIndexRecover(Wal *pWal){
44682   int rc;                         /* Return Code */
44683   i64 nSize;                      /* Size of log file */
44684   u32 aFrameCksum[2] = {0, 0};
44685   int iLock;                      /* Lock offset to lock for checkpoint */
44686   int nLock;                      /* Number of locks to hold */
44687 
44688   /* Obtain an exclusive lock on all byte in the locking range not already
44689   ** locked by the caller. The caller is guaranteed to have locked the
44690   ** WAL_WRITE_LOCK byte, and may have also locked the WAL_CKPT_LOCK byte.
44691   ** If successful, the same bytes that are locked here are unlocked before
44692   ** this function returns.
44693   */
44694   assert( pWal->ckptLock==1 || pWal->ckptLock==0 );
44695   assert( WAL_ALL_BUT_WRITE==WAL_WRITE_LOCK+1 );
44696   assert( WAL_CKPT_LOCK==WAL_ALL_BUT_WRITE );
44697   assert( pWal->writeLock );
44698   iLock = WAL_ALL_BUT_WRITE + pWal->ckptLock;
44699   nLock = SQLITE_SHM_NLOCK - iLock;
44700   rc = walLockExclusive(pWal, iLock, nLock);
44701   if( rc ){
44702     return rc;
44703   }
44704   WALTRACE(("WAL%p: recovery begin...\n", pWal));
44705 
44706   memset(&pWal->hdr, 0, sizeof(WalIndexHdr));
44707 
44708   rc = sqlite3OsFileSize(pWal->pWalFd, &nSize);
44709   if( rc!=SQLITE_OK ){
44710     goto recovery_error;
44711   }
44712 
44713   if( nSize>WAL_HDRSIZE ){
44714     u8 aBuf[WAL_HDRSIZE];         /* Buffer to load WAL header into */
44715     u8 *aFrame = 0;               /* Malloc'd buffer to load entire frame */
44716     int szFrame;                  /* Number of bytes in buffer aFrame[] */
44717     u8 *aData;                    /* Pointer to data part of aFrame buffer */
44718     int iFrame;                   /* Index of last frame read */
44719     i64 iOffset;                  /* Next offset to read from log file */
44720     int szPage;                   /* Page size according to the log */
44721     u32 magic;                    /* Magic value read from WAL header */
44722     u32 version;                  /* Magic value read from WAL header */
44723 
44724     /* Read in the WAL header. */
44725     rc = sqlite3OsRead(pWal->pWalFd, aBuf, WAL_HDRSIZE, 0);
44726     if( rc!=SQLITE_OK ){
44727       goto recovery_error;
44728     }
44729 
44730     /* If the database page size is not a power of two, or is greater than
44731     ** SQLITE_MAX_PAGE_SIZE, conclude that the WAL file contains no valid
44732     ** data. Similarly, if the 'magic' value is invalid, ignore the whole
44733     ** WAL file.
44734     */
44735     magic = sqlite3Get4byte(&aBuf[0]);
44736     szPage = sqlite3Get4byte(&aBuf[8]);
44737     if( (magic&0xFFFFFFFE)!=WAL_MAGIC
44738      || szPage&(szPage-1)
44739      || szPage>SQLITE_MAX_PAGE_SIZE
44740      || szPage<512
44741     ){
44742       goto finished;
44743     }
44744     pWal->hdr.bigEndCksum = (u8)(magic&0x00000001);
44745     pWal->szPage = szPage;
44746     pWal->nCkpt = sqlite3Get4byte(&aBuf[12]);
44747     memcpy(&pWal->hdr.aSalt, &aBuf[16], 8);
44748 
44749     /* Verify that the WAL header checksum is correct */
44750     walChecksumBytes(pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN,
44751         aBuf, WAL_HDRSIZE-2*4, 0, pWal->hdr.aFrameCksum
44752     );
44753     if( pWal->hdr.aFrameCksum[0]!=sqlite3Get4byte(&aBuf[24])
44754      || pWal->hdr.aFrameCksum[1]!=sqlite3Get4byte(&aBuf[28])
44755     ){
44756       goto finished;
44757     }
44758 
44759     /* Verify that the version number on the WAL format is one that
44760     ** are able to understand */
44761     version = sqlite3Get4byte(&aBuf[4]);
44762     if( version!=WAL_MAX_VERSION ){
44763       rc = SQLITE_CANTOPEN_BKPT;
44764       goto finished;
44765     }
44766 
44767     /* Malloc a buffer to read frames into. */
44768     szFrame = szPage + WAL_FRAME_HDRSIZE;
44769     aFrame = (u8 *)sqlite3_malloc(szFrame);
44770     if( !aFrame ){
44771       rc = SQLITE_NOMEM;
44772       goto recovery_error;
44773     }
44774     aData = &aFrame[WAL_FRAME_HDRSIZE];
44775 
44776     /* Read all frames from the log file. */
44777     iFrame = 0;
44778     for(iOffset=WAL_HDRSIZE; (iOffset+szFrame)<=nSize; iOffset+=szFrame){
44779       u32 pgno;                   /* Database page number for frame */
44780       u32 nTruncate;              /* dbsize field from frame header */
44781       int isValid;                /* True if this frame is valid */
44782 
44783       /* Read and decode the next log frame. */
44784       rc = sqlite3OsRead(pWal->pWalFd, aFrame, szFrame, iOffset);
44785       if( rc!=SQLITE_OK ) break;
44786       isValid = walDecodeFrame(pWal, &pgno, &nTruncate, aData, aFrame);
44787       if( !isValid ) break;
44788       rc = walIndexAppend(pWal, ++iFrame, pgno);
44789       if( rc!=SQLITE_OK ) break;
44790 
44791       /* If nTruncate is non-zero, this is a commit record. */
44792       if( nTruncate ){
44793         pWal->hdr.mxFrame = iFrame;
44794         pWal->hdr.nPage = nTruncate;
44795         pWal->hdr.szPage = (u16)((szPage&0xff00) | (szPage>>16));
44796         testcase( szPage<=32768 );
44797         testcase( szPage>=65536 );
44798         aFrameCksum[0] = pWal->hdr.aFrameCksum[0];
44799         aFrameCksum[1] = pWal->hdr.aFrameCksum[1];
44800       }
44801     }
44802 
44803     sqlite3_free(aFrame);
44804   }
44805 
44806 finished:
44807   if( rc==SQLITE_OK ){
44808     volatile WalCkptInfo *pInfo;
44809     int i;
44810     pWal->hdr.aFrameCksum[0] = aFrameCksum[0];
44811     pWal->hdr.aFrameCksum[1] = aFrameCksum[1];
44812     walIndexWriteHdr(pWal);
44813 
44814     /* Reset the checkpoint-header. This is safe because this thread is
44815     ** currently holding locks that exclude all other readers, writers and
44816     ** checkpointers.
44817     */
44818     pInfo = walCkptInfo(pWal);
44819     pInfo->nBackfill = 0;
44820     pInfo->aReadMark[0] = 0;
44821     for(i=1; i<WAL_NREADER; i++) pInfo->aReadMark[i] = READMARK_NOT_USED;
44822 
44823     /* If more than one frame was recovered from the log file, report an
44824     ** event via sqlite3_log(). This is to help with identifying performance
44825     ** problems caused by applications routinely shutting down without
44826     ** checkpointing the log file.
44827     */
44828     if( pWal->hdr.nPage ){
44829       sqlite3_log(SQLITE_OK, "Recovered %d frames from WAL file %s",
44830           pWal->hdr.nPage, pWal->zWalName
44831       );
44832     }
44833   }
44834 
44835 recovery_error:
44836   WALTRACE(("WAL%p: recovery %s\n", pWal, rc ? "failed" : "ok"));
44837   walUnlockExclusive(pWal, iLock, nLock);
44838   return rc;
44839 }
44840 
44841 /*
44842 ** Close an open wal-index.
44843 */
44844 static void walIndexClose(Wal *pWal, int isDelete){
44845   if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE ){
44846     int i;
44847     for(i=0; i<pWal->nWiData; i++){
44848       sqlite3_free((void *)pWal->apWiData[i]);
44849       pWal->apWiData[i] = 0;
44850     }
44851   }else{
44852     sqlite3OsShmUnmap(pWal->pDbFd, isDelete);
44853   }
44854 }
44855 
44856 /*
44857 ** Open a connection to the WAL file zWalName. The database file must
44858 ** already be opened on connection pDbFd. The buffer that zWalName points
44859 ** to must remain valid for the lifetime of the returned Wal* handle.
44860 **
44861 ** A SHARED lock should be held on the database file when this function
44862 ** is called. The purpose of this SHARED lock is to prevent any other
44863 ** client from unlinking the WAL or wal-index file. If another process
44864 ** were to do this just after this client opened one of these files, the
44865 ** system would be badly broken.
44866 **
44867 ** If the log file is successfully opened, SQLITE_OK is returned and
44868 ** *ppWal is set to point to a new WAL handle. If an error occurs,
44869 ** an SQLite error code is returned and *ppWal is left unmodified.
44870 */
44871 SQLITE_PRIVATE int sqlite3WalOpen(
44872   sqlite3_vfs *pVfs,              /* vfs module to open wal and wal-index */
44873   sqlite3_file *pDbFd,            /* The open database file */
44874   const char *zWalName,           /* Name of the WAL file */
44875   int bNoShm,                     /* True to run in heap-memory mode */
44876   i64 mxWalSize,                  /* Truncate WAL to this size on reset */
44877   Wal **ppWal                     /* OUT: Allocated Wal handle */
44878 ){
44879   int rc;                         /* Return Code */
44880   Wal *pRet;                      /* Object to allocate and return */
44881   int flags;                      /* Flags passed to OsOpen() */
44882 
44883   assert( zWalName && zWalName[0] );
44884   assert( pDbFd );
44885 
44886   /* In the amalgamation, the os_unix.c and os_win.c source files come before
44887   ** this source file.  Verify that the #defines of the locking byte offsets
44888   ** in os_unix.c and os_win.c agree with the WALINDEX_LOCK_OFFSET value.
44889   */
44890 #ifdef WIN_SHM_BASE
44891   assert( WIN_SHM_BASE==WALINDEX_LOCK_OFFSET );
44892 #endif
44893 #ifdef UNIX_SHM_BASE
44894   assert( UNIX_SHM_BASE==WALINDEX_LOCK_OFFSET );
44895 #endif
44896 
44897 
44898   /* Allocate an instance of struct Wal to return. */
44899   *ppWal = 0;
44900   pRet = (Wal*)sqlite3MallocZero(sizeof(Wal) + pVfs->szOsFile);
44901   if( !pRet ){
44902     return SQLITE_NOMEM;
44903   }
44904 
44905   pRet->pVfs = pVfs;
44906   pRet->pWalFd = (sqlite3_file *)&pRet[1];
44907   pRet->pDbFd = pDbFd;
44908   pRet->readLock = -1;
44909   pRet->mxWalSize = mxWalSize;
44910   pRet->zWalName = zWalName;
44911   pRet->exclusiveMode = (bNoShm ? WAL_HEAPMEMORY_MODE: WAL_NORMAL_MODE);
44912 
44913   /* Open file handle on the write-ahead log file. */
44914   flags = (SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|SQLITE_OPEN_WAL);
44915   rc = sqlite3OsOpen(pVfs, zWalName, pRet->pWalFd, flags, &flags);
44916   if( rc==SQLITE_OK && flags&SQLITE_OPEN_READONLY ){
44917     pRet->readOnly = WAL_RDONLY;
44918   }
44919 
44920   if( rc!=SQLITE_OK ){
44921     walIndexClose(pRet, 0);
44922     sqlite3OsClose(pRet->pWalFd);
44923     sqlite3_free(pRet);
44924   }else{
44925     *ppWal = pRet;
44926     WALTRACE(("WAL%d: opened\n", pRet));
44927   }
44928   return rc;
44929 }
44930 
44931 /*
44932 ** Change the size to which the WAL file is trucated on each reset.
44933 */
44934 SQLITE_PRIVATE void sqlite3WalLimit(Wal *pWal, i64 iLimit){
44935   if( pWal ) pWal->mxWalSize = iLimit;
44936 }
44937 
44938 /*
44939 ** Find the smallest page number out of all pages held in the WAL that
44940 ** has not been returned by any prior invocation of this method on the
44941 ** same WalIterator object.   Write into *piFrame the frame index where
44942 ** that page was last written into the WAL.  Write into *piPage the page
44943 ** number.
44944 **
44945 ** Return 0 on success.  If there are no pages in the WAL with a page
44946 ** number larger than *piPage, then return 1.
44947 */
44948 static int walIteratorNext(
44949   WalIterator *p,               /* Iterator */
44950   u32 *piPage,                  /* OUT: The page number of the next page */
44951   u32 *piFrame                  /* OUT: Wal frame index of next page */
44952 ){
44953   u32 iMin;                     /* Result pgno must be greater than iMin */
44954   u32 iRet = 0xFFFFFFFF;        /* 0xffffffff is never a valid page number */
44955   int i;                        /* For looping through segments */
44956 
44957   iMin = p->iPrior;
44958   assert( iMin<0xffffffff );
44959   for(i=p->nSegment-1; i>=0; i--){
44960     struct WalSegment *pSegment = &p->aSegment[i];
44961     while( pSegment->iNext<pSegment->nEntry ){
44962       u32 iPg = pSegment->aPgno[pSegment->aIndex[pSegment->iNext]];
44963       if( iPg>iMin ){
44964         if( iPg<iRet ){
44965           iRet = iPg;
44966           *piFrame = pSegment->iZero + pSegment->aIndex[pSegment->iNext];
44967         }
44968         break;
44969       }
44970       pSegment->iNext++;
44971     }
44972   }
44973 
44974   *piPage = p->iPrior = iRet;
44975   return (iRet==0xFFFFFFFF);
44976 }
44977 
44978 /*
44979 ** This function merges two sorted lists into a single sorted list.
44980 **
44981 ** aLeft[] and aRight[] are arrays of indices.  The sort key is
44982 ** aContent[aLeft[]] and aContent[aRight[]].  Upon entry, the following
44983 ** is guaranteed for all J<K:
44984 **
44985 **        aContent[aLeft[J]] < aContent[aLeft[K]]
44986 **        aContent[aRight[J]] < aContent[aRight[K]]
44987 **
44988 ** This routine overwrites aRight[] with a new (probably longer) sequence
44989 ** of indices such that the aRight[] contains every index that appears in
44990 ** either aLeft[] or the old aRight[] and such that the second condition
44991 ** above is still met.
44992 **
44993 ** The aContent[aLeft[X]] values will be unique for all X.  And the
44994 ** aContent[aRight[X]] values will be unique too.  But there might be
44995 ** one or more combinations of X and Y such that
44996 **
44997 **      aLeft[X]!=aRight[Y]  &&  aContent[aLeft[X]] == aContent[aRight[Y]]
44998 **
44999 ** When that happens, omit the aLeft[X] and use the aRight[Y] index.
45000 */
45001 static void walMerge(
45002   const u32 *aContent,            /* Pages in wal - keys for the sort */
45003   ht_slot *aLeft,                 /* IN: Left hand input list */
45004   int nLeft,                      /* IN: Elements in array *paLeft */
45005   ht_slot **paRight,              /* IN/OUT: Right hand input list */
45006   int *pnRight,                   /* IN/OUT: Elements in *paRight */
45007   ht_slot *aTmp                   /* Temporary buffer */
45008 ){
45009   int iLeft = 0;                  /* Current index in aLeft */
45010   int iRight = 0;                 /* Current index in aRight */
45011   int iOut = 0;                   /* Current index in output buffer */
45012   int nRight = *pnRight;
45013   ht_slot *aRight = *paRight;
45014 
45015   assert( nLeft>0 && nRight>0 );
45016   while( iRight<nRight || iLeft<nLeft ){
45017     ht_slot logpage;
45018     Pgno dbpage;
45019 
45020     if( (iLeft<nLeft)
45021      && (iRight>=nRight || aContent[aLeft[iLeft]]<aContent[aRight[iRight]])
45022     ){
45023       logpage = aLeft[iLeft++];
45024     }else{
45025       logpage = aRight[iRight++];
45026     }
45027     dbpage = aContent[logpage];
45028 
45029     aTmp[iOut++] = logpage;
45030     if( iLeft<nLeft && aContent[aLeft[iLeft]]==dbpage ) iLeft++;
45031 
45032     assert( iLeft>=nLeft || aContent[aLeft[iLeft]]>dbpage );
45033     assert( iRight>=nRight || aContent[aRight[iRight]]>dbpage );
45034   }
45035 
45036   *paRight = aLeft;
45037   *pnRight = iOut;
45038   memcpy(aLeft, aTmp, sizeof(aTmp[0])*iOut);
45039 }
45040 
45041 /*
45042 ** Sort the elements in list aList using aContent[] as the sort key.
45043 ** Remove elements with duplicate keys, preferring to keep the
45044 ** larger aList[] values.
45045 **
45046 ** The aList[] entries are indices into aContent[].  The values in
45047 ** aList[] are to be sorted so that for all J<K:
45048 **
45049 **      aContent[aList[J]] < aContent[aList[K]]
45050 **
45051 ** For any X and Y such that
45052 **
45053 **      aContent[aList[X]] == aContent[aList[Y]]
45054 **
45055 ** Keep the larger of the two values aList[X] and aList[Y] and discard
45056 ** the smaller.
45057 */
45058 static void walMergesort(
45059   const u32 *aContent,            /* Pages in wal */
45060   ht_slot *aBuffer,               /* Buffer of at least *pnList items to use */
45061   ht_slot *aList,                 /* IN/OUT: List to sort */
45062   int *pnList                     /* IN/OUT: Number of elements in aList[] */
45063 ){
45064   struct Sublist {
45065     int nList;                    /* Number of elements in aList */
45066     ht_slot *aList;               /* Pointer to sub-list content */
45067   };
45068 
45069   const int nList = *pnList;      /* Size of input list */
45070   int nMerge = 0;                 /* Number of elements in list aMerge */
45071   ht_slot *aMerge = 0;            /* List to be merged */
45072   int iList;                      /* Index into input list */
45073   int iSub = 0;                   /* Index into aSub array */
45074   struct Sublist aSub[13];        /* Array of sub-lists */
45075 
45076   memset(aSub, 0, sizeof(aSub));
45077   assert( nList<=HASHTABLE_NPAGE && nList>0 );
45078   assert( HASHTABLE_NPAGE==(1<<(ArraySize(aSub)-1)) );
45079 
45080   for(iList=0; iList<nList; iList++){
45081     nMerge = 1;
45082     aMerge = &aList[iList];
45083     for(iSub=0; iList & (1<<iSub); iSub++){
45084       struct Sublist *p = &aSub[iSub];
45085       assert( p->aList && p->nList<=(1<<iSub) );
45086       assert( p->aList==&aList[iList&~((2<<iSub)-1)] );
45087       walMerge(aContent, p->aList, p->nList, &aMerge, &nMerge, aBuffer);
45088     }
45089     aSub[iSub].aList = aMerge;
45090     aSub[iSub].nList = nMerge;
45091   }
45092 
45093   for(iSub++; iSub<ArraySize(aSub); iSub++){
45094     if( nList & (1<<iSub) ){
45095       struct Sublist *p = &aSub[iSub];
45096       assert( p->nList<=(1<<iSub) );
45097       assert( p->aList==&aList[nList&~((2<<iSub)-1)] );
45098       walMerge(aContent, p->aList, p->nList, &aMerge, &nMerge, aBuffer);
45099     }
45100   }
45101   assert( aMerge==aList );
45102   *pnList = nMerge;
45103 
45104 #ifdef SQLITE_DEBUG
45105   {
45106     int i;
45107     for(i=1; i<*pnList; i++){
45108       assert( aContent[aList[i]] > aContent[aList[i-1]] );
45109     }
45110   }
45111 #endif
45112 }
45113 
45114 /*
45115 ** Free an iterator allocated by walIteratorInit().
45116 */
45117 static void walIteratorFree(WalIterator *p){
45118   sqlite3ScratchFree(p);
45119 }
45120 
45121 /*
45122 ** Construct a WalInterator object that can be used to loop over all
45123 ** pages in the WAL in ascending order. The caller must hold the checkpoint
45124 ** lock.
45125 **
45126 ** On success, make *pp point to the newly allocated WalInterator object
45127 ** return SQLITE_OK. Otherwise, return an error code. If this routine
45128 ** returns an error, the value of *pp is undefined.
45129 **
45130 ** The calling routine should invoke walIteratorFree() to destroy the
45131 ** WalIterator object when it has finished with it.
45132 */
45133 static int walIteratorInit(Wal *pWal, WalIterator **pp){
45134   WalIterator *p;                 /* Return value */
45135   int nSegment;                   /* Number of segments to merge */
45136   u32 iLast;                      /* Last frame in log */
45137   int nByte;                      /* Number of bytes to allocate */
45138   int i;                          /* Iterator variable */
45139   ht_slot *aTmp;                  /* Temp space used by merge-sort */
45140   int rc = SQLITE_OK;             /* Return Code */
45141 
45142   /* This routine only runs while holding the checkpoint lock. And
45143   ** it only runs if there is actually content in the log (mxFrame>0).
45144   */
45145   assert( pWal->ckptLock && pWal->hdr.mxFrame>0 );
45146   iLast = pWal->hdr.mxFrame;
45147 
45148   /* Allocate space for the WalIterator object. */
45149   nSegment = walFramePage(iLast) + 1;
45150   nByte = sizeof(WalIterator)
45151         + (nSegment-1)*sizeof(struct WalSegment)
45152         + iLast*sizeof(ht_slot);
45153   p = (WalIterator *)sqlite3ScratchMalloc(nByte);
45154   if( !p ){
45155     return SQLITE_NOMEM;
45156   }
45157   memset(p, 0, nByte);
45158   p->nSegment = nSegment;
45159 
45160   /* Allocate temporary space used by the merge-sort routine. This block
45161   ** of memory will be freed before this function returns.
45162   */
45163   aTmp = (ht_slot *)sqlite3ScratchMalloc(
45164       sizeof(ht_slot) * (iLast>HASHTABLE_NPAGE?HASHTABLE_NPAGE:iLast)
45165   );
45166   if( !aTmp ){
45167     rc = SQLITE_NOMEM;
45168   }
45169 
45170   for(i=0; rc==SQLITE_OK && i<nSegment; i++){
45171     volatile ht_slot *aHash;
45172     u32 iZero;
45173     volatile u32 *aPgno;
45174 
45175     rc = walHashGet(pWal, i, &aHash, &aPgno, &iZero);
45176     if( rc==SQLITE_OK ){
45177       int j;                      /* Counter variable */
45178       int nEntry;                 /* Number of entries in this segment */
45179       ht_slot *aIndex;            /* Sorted index for this segment */
45180 
45181       aPgno++;
45182       if( (i+1)==nSegment ){
45183         nEntry = (int)(iLast - iZero);
45184       }else{
45185         nEntry = (int)((u32*)aHash - (u32*)aPgno);
45186       }
45187       aIndex = &((ht_slot *)&p->aSegment[p->nSegment])[iZero];
45188       iZero++;
45189 
45190       for(j=0; j<nEntry; j++){
45191         aIndex[j] = (ht_slot)j;
45192       }
45193       walMergesort((u32 *)aPgno, aTmp, aIndex, &nEntry);
45194       p->aSegment[i].iZero = iZero;
45195       p->aSegment[i].nEntry = nEntry;
45196       p->aSegment[i].aIndex = aIndex;
45197       p->aSegment[i].aPgno = (u32 *)aPgno;
45198     }
45199   }
45200   sqlite3ScratchFree(aTmp);
45201 
45202   if( rc!=SQLITE_OK ){
45203     walIteratorFree(p);
45204   }
45205   *pp = p;
45206   return rc;
45207 }
45208 
45209 /*
45210 ** Attempt to obtain the exclusive WAL lock defined by parameters lockIdx and
45211 ** n. If the attempt fails and parameter xBusy is not NULL, then it is a
45212 ** busy-handler function. Invoke it and retry the lock until either the
45213 ** lock is successfully obtained or the busy-handler returns 0.
45214 */
45215 static int walBusyLock(
45216   Wal *pWal,                      /* WAL connection */
45217   int (*xBusy)(void*),            /* Function to call when busy */
45218   void *pBusyArg,                 /* Context argument for xBusyHandler */
45219   int lockIdx,                    /* Offset of first byte to lock */
45220   int n                           /* Number of bytes to lock */
45221 ){
45222   int rc;
45223   do {
45224     rc = walLockExclusive(pWal, lockIdx, n);
45225   }while( xBusy && rc==SQLITE_BUSY && xBusy(pBusyArg) );
45226   return rc;
45227 }
45228 
45229 /*
45230 ** The cache of the wal-index header must be valid to call this function.
45231 ** Return the page-size in bytes used by the database.
45232 */
45233 static int walPagesize(Wal *pWal){
45234   return (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16);
45235 }
45236 
45237 /*
45238 ** Copy as much content as we can from the WAL back into the database file
45239 ** in response to an sqlite3_wal_checkpoint() request or the equivalent.
45240 **
45241 ** The amount of information copies from WAL to database might be limited
45242 ** by active readers.  This routine will never overwrite a database page
45243 ** that a concurrent reader might be using.
45244 **
45245 ** All I/O barrier operations (a.k.a fsyncs) occur in this routine when
45246 ** SQLite is in WAL-mode in synchronous=NORMAL.  That means that if
45247 ** checkpoints are always run by a background thread or background
45248 ** process, foreground threads will never block on a lengthy fsync call.
45249 **
45250 ** Fsync is called on the WAL before writing content out of the WAL and
45251 ** into the database.  This ensures that if the new content is persistent
45252 ** in the WAL and can be recovered following a power-loss or hard reset.
45253 **
45254 ** Fsync is also called on the database file if (and only if) the entire
45255 ** WAL content is copied into the database file.  This second fsync makes
45256 ** it safe to delete the WAL since the new content will persist in the
45257 ** database file.
45258 **
45259 ** This routine uses and updates the nBackfill field of the wal-index header.
45260 ** This is the only routine tha will increase the value of nBackfill.
45261 ** (A WAL reset or recovery will revert nBackfill to zero, but not increase
45262 ** its value.)
45263 **
45264 ** The caller must be holding sufficient locks to ensure that no other
45265 ** checkpoint is running (in any other thread or process) at the same
45266 ** time.
45267 */
45268 static int walCheckpoint(
45269   Wal *pWal,                      /* Wal connection */
45270   int eMode,                      /* One of PASSIVE, FULL or RESTART */
45271   int (*xBusyCall)(void*),        /* Function to call when busy */
45272   void *pBusyArg,                 /* Context argument for xBusyHandler */
45273   int sync_flags,                 /* Flags for OsSync() (or 0) */
45274   u8 *zBuf                        /* Temporary buffer to use */
45275 ){
45276   int rc;                         /* Return code */
45277   int szPage;                     /* Database page-size */
45278   WalIterator *pIter = 0;         /* Wal iterator context */
45279   u32 iDbpage = 0;                /* Next database page to write */
45280   u32 iFrame = 0;                 /* Wal frame containing data for iDbpage */
45281   u32 mxSafeFrame;                /* Max frame that can be backfilled */
45282   u32 mxPage;                     /* Max database page to write */
45283   int i;                          /* Loop counter */
45284   volatile WalCkptInfo *pInfo;    /* The checkpoint status information */
45285   int (*xBusy)(void*) = 0;        /* Function to call when waiting for locks */
45286 
45287   szPage = walPagesize(pWal);
45288   testcase( szPage<=32768 );
45289   testcase( szPage>=65536 );
45290   pInfo = walCkptInfo(pWal);
45291   if( pInfo->nBackfill>=pWal->hdr.mxFrame ) return SQLITE_OK;
45292 
45293   /* Allocate the iterator */
45294   rc = walIteratorInit(pWal, &pIter);
45295   if( rc!=SQLITE_OK ){
45296     return rc;
45297   }
45298   assert( pIter );
45299 
45300   if( eMode!=SQLITE_CHECKPOINT_PASSIVE ) xBusy = xBusyCall;
45301 
45302   /* Compute in mxSafeFrame the index of the last frame of the WAL that is
45303   ** safe to write into the database.  Frames beyond mxSafeFrame might
45304   ** overwrite database pages that are in use by active readers and thus
45305   ** cannot be backfilled from the WAL.
45306   */
45307   mxSafeFrame = pWal->hdr.mxFrame;
45308   mxPage = pWal->hdr.nPage;
45309   for(i=1; i<WAL_NREADER; i++){
45310     u32 y = pInfo->aReadMark[i];
45311     if( mxSafeFrame>y ){
45312       assert( y<=pWal->hdr.mxFrame );
45313       rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(i), 1);
45314       if( rc==SQLITE_OK ){
45315         pInfo->aReadMark[i] = READMARK_NOT_USED;
45316         walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1);
45317       }else if( rc==SQLITE_BUSY ){
45318         mxSafeFrame = y;
45319         xBusy = 0;
45320       }else{
45321         goto walcheckpoint_out;
45322       }
45323     }
45324   }
45325 
45326   if( pInfo->nBackfill<mxSafeFrame
45327    && (rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(0), 1))==SQLITE_OK
45328   ){
45329     i64 nSize;                    /* Current size of database file */
45330     u32 nBackfill = pInfo->nBackfill;
45331 
45332     /* Sync the WAL to disk */
45333     if( sync_flags ){
45334       rc = sqlite3OsSync(pWal->pWalFd, sync_flags);
45335     }
45336 
45337     /* If the database file may grow as a result of this checkpoint, hint
45338     ** about the eventual size of the db file to the VFS layer.
45339     */
45340     if( rc==SQLITE_OK ){
45341       i64 nReq = ((i64)mxPage * szPage);
45342       rc = sqlite3OsFileSize(pWal->pDbFd, &nSize);
45343       if( rc==SQLITE_OK && nSize<nReq ){
45344         sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_SIZE_HINT, &nReq);
45345       }
45346     }
45347 
45348     /* Iterate through the contents of the WAL, copying data to the db file. */
45349     while( rc==SQLITE_OK && 0==walIteratorNext(pIter, &iDbpage, &iFrame) ){
45350       i64 iOffset;
45351       assert( walFramePgno(pWal, iFrame)==iDbpage );
45352       if( iFrame<=nBackfill || iFrame>mxSafeFrame || iDbpage>mxPage ) continue;
45353       iOffset = walFrameOffset(iFrame, szPage) + WAL_FRAME_HDRSIZE;
45354       /* testcase( IS_BIG_INT(iOffset) ); // requires a 4GiB WAL file */
45355       rc = sqlite3OsRead(pWal->pWalFd, zBuf, szPage, iOffset);
45356       if( rc!=SQLITE_OK ) break;
45357       iOffset = (iDbpage-1)*(i64)szPage;
45358       testcase( IS_BIG_INT(iOffset) );
45359       rc = sqlite3OsWrite(pWal->pDbFd, zBuf, szPage, iOffset);
45360       if( rc!=SQLITE_OK ) break;
45361     }
45362 
45363     /* If work was actually accomplished... */
45364     if( rc==SQLITE_OK ){
45365       if( mxSafeFrame==walIndexHdr(pWal)->mxFrame ){
45366         i64 szDb = pWal->hdr.nPage*(i64)szPage;
45367         testcase( IS_BIG_INT(szDb) );
45368         rc = sqlite3OsTruncate(pWal->pDbFd, szDb);
45369         if( rc==SQLITE_OK && sync_flags ){
45370           rc = sqlite3OsSync(pWal->pDbFd, sync_flags);
45371         }
45372       }
45373       if( rc==SQLITE_OK ){
45374         pInfo->nBackfill = mxSafeFrame;
45375       }
45376     }
45377 
45378     /* Release the reader lock held while backfilling */
45379     walUnlockExclusive(pWal, WAL_READ_LOCK(0), 1);
45380   }
45381 
45382   if( rc==SQLITE_BUSY ){
45383     /* Reset the return code so as not to report a checkpoint failure
45384     ** just because there are active readers.  */
45385     rc = SQLITE_OK;
45386   }
45387 
45388   /* If this is an SQLITE_CHECKPOINT_RESTART operation, and the entire wal
45389   ** file has been copied into the database file, then block until all
45390   ** readers have finished using the wal file. This ensures that the next
45391   ** process to write to the database restarts the wal file.
45392   */
45393   if( rc==SQLITE_OK && eMode!=SQLITE_CHECKPOINT_PASSIVE ){
45394     assert( pWal->writeLock );
45395     if( pInfo->nBackfill<pWal->hdr.mxFrame ){
45396       rc = SQLITE_BUSY;
45397     }else if( eMode==SQLITE_CHECKPOINT_RESTART ){
45398       assert( mxSafeFrame==pWal->hdr.mxFrame );
45399       rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(1), WAL_NREADER-1);
45400       if( rc==SQLITE_OK ){
45401         walUnlockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
45402       }
45403     }
45404   }
45405 
45406  walcheckpoint_out:
45407   walIteratorFree(pIter);
45408   return rc;
45409 }
45410 
45411 /*
45412 ** Close a connection to a log file.
45413 */
45414 SQLITE_PRIVATE int sqlite3WalClose(
45415   Wal *pWal,                      /* Wal to close */
45416   int sync_flags,                 /* Flags to pass to OsSync() (or 0) */
45417   int nBuf,
45418   u8 *zBuf                        /* Buffer of at least nBuf bytes */
45419 ){
45420   int rc = SQLITE_OK;
45421   if( pWal ){
45422     int isDelete = 0;             /* True to unlink wal and wal-index files */
45423 
45424     /* If an EXCLUSIVE lock can be obtained on the database file (using the
45425     ** ordinary, rollback-mode locking methods, this guarantees that the
45426     ** connection associated with this log file is the only connection to
45427     ** the database. In this case checkpoint the database and unlink both
45428     ** the wal and wal-index files.
45429     **
45430     ** The EXCLUSIVE lock is not released before returning.
45431     */
45432     rc = sqlite3OsLock(pWal->pDbFd, SQLITE_LOCK_EXCLUSIVE);
45433     if( rc==SQLITE_OK ){
45434       if( pWal->exclusiveMode==WAL_NORMAL_MODE ){
45435         pWal->exclusiveMode = WAL_EXCLUSIVE_MODE;
45436       }
45437       rc = sqlite3WalCheckpoint(
45438           pWal, SQLITE_CHECKPOINT_PASSIVE, 0, 0, sync_flags, nBuf, zBuf, 0, 0
45439       );
45440       if( rc==SQLITE_OK ){
45441         isDelete = 1;
45442       }
45443     }
45444 
45445     walIndexClose(pWal, isDelete);
45446     sqlite3OsClose(pWal->pWalFd);
45447     if( isDelete ){
45448       sqlite3OsDelete(pWal->pVfs, pWal->zWalName, 0);
45449     }
45450     WALTRACE(("WAL%p: closed\n", pWal));
45451     sqlite3_free((void *)pWal->apWiData);
45452     sqlite3_free(pWal);
45453   }
45454   return rc;
45455 }
45456 
45457 /*
45458 ** Try to read the wal-index header.  Return 0 on success and 1 if
45459 ** there is a problem.
45460 **
45461 ** The wal-index is in shared memory.  Another thread or process might
45462 ** be writing the header at the same time this procedure is trying to
45463 ** read it, which might result in inconsistency.  A dirty read is detected
45464 ** by verifying that both copies of the header are the same and also by
45465 ** a checksum on the header.
45466 **
45467 ** If and only if the read is consistent and the header is different from
45468 ** pWal->hdr, then pWal->hdr is updated to the content of the new header
45469 ** and *pChanged is set to 1.
45470 **
45471 ** If the checksum cannot be verified return non-zero. If the header
45472 ** is read successfully and the checksum verified, return zero.
45473 */
45474 static int walIndexTryHdr(Wal *pWal, int *pChanged){
45475   u32 aCksum[2];                  /* Checksum on the header content */
45476   WalIndexHdr h1, h2;             /* Two copies of the header content */
45477   WalIndexHdr volatile *aHdr;     /* Header in shared memory */
45478 
45479   /* The first page of the wal-index must be mapped at this point. */
45480   assert( pWal->nWiData>0 && pWal->apWiData[0] );
45481 
45482   /* Read the header. This might happen concurrently with a write to the
45483   ** same area of shared memory on a different CPU in a SMP,
45484   ** meaning it is possible that an inconsistent snapshot is read
45485   ** from the file. If this happens, return non-zero.
45486   **
45487   ** There are two copies of the header at the beginning of the wal-index.
45488   ** When reading, read [0] first then [1].  Writes are in the reverse order.
45489   ** Memory barriers are used to prevent the compiler or the hardware from
45490   ** reordering the reads and writes.
45491   */
45492   aHdr = walIndexHdr(pWal);
45493   memcpy(&h1, (void *)&aHdr[0], sizeof(h1));
45494   walShmBarrier(pWal);
45495   memcpy(&h2, (void *)&aHdr[1], sizeof(h2));
45496 
45497   if( memcmp(&h1, &h2, sizeof(h1))!=0 ){
45498     return 1;   /* Dirty read */
45499   }
45500   if( h1.isInit==0 ){
45501     return 1;   /* Malformed header - probably all zeros */
45502   }
45503   walChecksumBytes(1, (u8*)&h1, sizeof(h1)-sizeof(h1.aCksum), 0, aCksum);
45504   if( aCksum[0]!=h1.aCksum[0] || aCksum[1]!=h1.aCksum[1] ){
45505     return 1;   /* Checksum does not match */
45506   }
45507 
45508   if( memcmp(&pWal->hdr, &h1, sizeof(WalIndexHdr)) ){
45509     *pChanged = 1;
45510     memcpy(&pWal->hdr, &h1, sizeof(WalIndexHdr));
45511     pWal->szPage = (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16);
45512     testcase( pWal->szPage<=32768 );
45513     testcase( pWal->szPage>=65536 );
45514   }
45515 
45516   /* The header was successfully read. Return zero. */
45517   return 0;
45518 }
45519 
45520 /*
45521 ** Read the wal-index header from the wal-index and into pWal->hdr.
45522 ** If the wal-header appears to be corrupt, try to reconstruct the
45523 ** wal-index from the WAL before returning.
45524 **
45525 ** Set *pChanged to 1 if the wal-index header value in pWal->hdr is
45526 ** changed by this opertion.  If pWal->hdr is unchanged, set *pChanged
45527 ** to 0.
45528 **
45529 ** If the wal-index header is successfully read, return SQLITE_OK.
45530 ** Otherwise an SQLite error code.
45531 */
45532 static int walIndexReadHdr(Wal *pWal, int *pChanged){
45533   int rc;                         /* Return code */
45534   int badHdr;                     /* True if a header read failed */
45535   volatile u32 *page0;            /* Chunk of wal-index containing header */
45536 
45537   /* Ensure that page 0 of the wal-index (the page that contains the
45538   ** wal-index header) is mapped. Return early if an error occurs here.
45539   */
45540   assert( pChanged );
45541   rc = walIndexPage(pWal, 0, &page0);
45542   if( rc!=SQLITE_OK ){
45543     return rc;
45544   };
45545   assert( page0 || pWal->writeLock==0 );
45546 
45547   /* If the first page of the wal-index has been mapped, try to read the
45548   ** wal-index header immediately, without holding any lock. This usually
45549   ** works, but may fail if the wal-index header is corrupt or currently
45550   ** being modified by another thread or process.
45551   */
45552   badHdr = (page0 ? walIndexTryHdr(pWal, pChanged) : 1);
45553 
45554   /* If the first attempt failed, it might have been due to a race
45555   ** with a writer.  So get a WRITE lock and try again.
45556   */
45557   assert( badHdr==0 || pWal->writeLock==0 );
45558   if( badHdr ){
45559     if( pWal->readOnly & WAL_SHM_RDONLY ){
45560       if( SQLITE_OK==(rc = walLockShared(pWal, WAL_WRITE_LOCK)) ){
45561         walUnlockShared(pWal, WAL_WRITE_LOCK);
45562         rc = SQLITE_READONLY_RECOVERY;
45563       }
45564     }else if( SQLITE_OK==(rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1)) ){
45565       pWal->writeLock = 1;
45566       if( SQLITE_OK==(rc = walIndexPage(pWal, 0, &page0)) ){
45567         badHdr = walIndexTryHdr(pWal, pChanged);
45568         if( badHdr ){
45569           /* If the wal-index header is still malformed even while holding
45570           ** a WRITE lock, it can only mean that the header is corrupted and
45571           ** needs to be reconstructed.  So run recovery to do exactly that.
45572           */
45573           rc = walIndexRecover(pWal);
45574           *pChanged = 1;
45575         }
45576       }
45577       pWal->writeLock = 0;
45578       walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1);
45579     }
45580   }
45581 
45582   /* If the header is read successfully, check the version number to make
45583   ** sure the wal-index was not constructed with some future format that
45584   ** this version of SQLite cannot understand.
45585   */
45586   if( badHdr==0 && pWal->hdr.iVersion!=WALINDEX_MAX_VERSION ){
45587     rc = SQLITE_CANTOPEN_BKPT;
45588   }
45589 
45590   return rc;
45591 }
45592 
45593 /*
45594 ** This is the value that walTryBeginRead returns when it needs to
45595 ** be retried.
45596 */
45597 #define WAL_RETRY  (-1)
45598 
45599 /*
45600 ** Attempt to start a read transaction.  This might fail due to a race or
45601 ** other transient condition.  When that happens, it returns WAL_RETRY to
45602 ** indicate to the caller that it is safe to retry immediately.
45603 **
45604 ** On success return SQLITE_OK.  On a permanent failure (such an
45605 ** I/O error or an SQLITE_BUSY because another process is running
45606 ** recovery) return a positive error code.
45607 **
45608 ** The useWal parameter is true to force the use of the WAL and disable
45609 ** the case where the WAL is bypassed because it has been completely
45610 ** checkpointed.  If useWal==0 then this routine calls walIndexReadHdr()
45611 ** to make a copy of the wal-index header into pWal->hdr.  If the
45612 ** wal-index header has changed, *pChanged is set to 1 (as an indication
45613 ** to the caller that the local paget cache is obsolete and needs to be
45614 ** flushed.)  When useWal==1, the wal-index header is assumed to already
45615 ** be loaded and the pChanged parameter is unused.
45616 **
45617 ** The caller must set the cnt parameter to the number of prior calls to
45618 ** this routine during the current read attempt that returned WAL_RETRY.
45619 ** This routine will start taking more aggressive measures to clear the
45620 ** race conditions after multiple WAL_RETRY returns, and after an excessive
45621 ** number of errors will ultimately return SQLITE_PROTOCOL.  The
45622 ** SQLITE_PROTOCOL return indicates that some other process has gone rogue
45623 ** and is not honoring the locking protocol.  There is a vanishingly small
45624 ** chance that SQLITE_PROTOCOL could be returned because of a run of really
45625 ** bad luck when there is lots of contention for the wal-index, but that
45626 ** possibility is so small that it can be safely neglected, we believe.
45627 **
45628 ** On success, this routine obtains a read lock on
45629 ** WAL_READ_LOCK(pWal->readLock).  The pWal->readLock integer is
45630 ** in the range 0 <= pWal->readLock < WAL_NREADER.  If pWal->readLock==(-1)
45631 ** that means the Wal does not hold any read lock.  The reader must not
45632 ** access any database page that is modified by a WAL frame up to and
45633 ** including frame number aReadMark[pWal->readLock].  The reader will
45634 ** use WAL frames up to and including pWal->hdr.mxFrame if pWal->readLock>0
45635 ** Or if pWal->readLock==0, then the reader will ignore the WAL
45636 ** completely and get all content directly from the database file.
45637 ** If the useWal parameter is 1 then the WAL will never be ignored and
45638 ** this routine will always set pWal->readLock>0 on success.
45639 ** When the read transaction is completed, the caller must release the
45640 ** lock on WAL_READ_LOCK(pWal->readLock) and set pWal->readLock to -1.
45641 **
45642 ** This routine uses the nBackfill and aReadMark[] fields of the header
45643 ** to select a particular WAL_READ_LOCK() that strives to let the
45644 ** checkpoint process do as much work as possible.  This routine might
45645 ** update values of the aReadMark[] array in the header, but if it does
45646 ** so it takes care to hold an exclusive lock on the corresponding
45647 ** WAL_READ_LOCK() while changing values.
45648 */
45649 static int walTryBeginRead(Wal *pWal, int *pChanged, int useWal, int cnt){
45650   volatile WalCkptInfo *pInfo;    /* Checkpoint information in wal-index */
45651   u32 mxReadMark;                 /* Largest aReadMark[] value */
45652   int mxI;                        /* Index of largest aReadMark[] value */
45653   int i;                          /* Loop counter */
45654   int rc = SQLITE_OK;             /* Return code  */
45655 
45656   assert( pWal->readLock<0 );     /* Not currently locked */
45657 
45658   /* Take steps to avoid spinning forever if there is a protocol error.
45659   **
45660   ** Circumstances that cause a RETRY should only last for the briefest
45661   ** instances of time.  No I/O or other system calls are done while the
45662   ** locks are held, so the locks should not be held for very long. But
45663   ** if we are unlucky, another process that is holding a lock might get
45664   ** paged out or take a page-fault that is time-consuming to resolve,
45665   ** during the few nanoseconds that it is holding the lock.  In that case,
45666   ** it might take longer than normal for the lock to free.
45667   **
45668   ** After 5 RETRYs, we begin calling sqlite3OsSleep().  The first few
45669   ** calls to sqlite3OsSleep() have a delay of 1 microsecond.  Really this
45670   ** is more of a scheduler yield than an actual delay.  But on the 10th
45671   ** an subsequent retries, the delays start becoming longer and longer,
45672   ** so that on the 100th (and last) RETRY we delay for 21 milliseconds.
45673   ** The total delay time before giving up is less than 1 second.
45674   */
45675   if( cnt>5 ){
45676     int nDelay = 1;                      /* Pause time in microseconds */
45677     if( cnt>100 ){
45678       VVA_ONLY( pWal->lockError = 1; )
45679       return SQLITE_PROTOCOL;
45680     }
45681     if( cnt>=10 ) nDelay = (cnt-9)*238;  /* Max delay 21ms. Total delay 996ms */
45682     sqlite3OsSleep(pWal->pVfs, nDelay);
45683   }
45684 
45685   if( !useWal ){
45686     rc = walIndexReadHdr(pWal, pChanged);
45687     if( rc==SQLITE_BUSY ){
45688       /* If there is not a recovery running in another thread or process
45689       ** then convert BUSY errors to WAL_RETRY.  If recovery is known to
45690       ** be running, convert BUSY to BUSY_RECOVERY.  There is a race here
45691       ** which might cause WAL_RETRY to be returned even if BUSY_RECOVERY
45692       ** would be technically correct.  But the race is benign since with
45693       ** WAL_RETRY this routine will be called again and will probably be
45694       ** right on the second iteration.
45695       */
45696       if( pWal->apWiData[0]==0 ){
45697         /* This branch is taken when the xShmMap() method returns SQLITE_BUSY.
45698         ** We assume this is a transient condition, so return WAL_RETRY. The
45699         ** xShmMap() implementation used by the default unix and win32 VFS
45700         ** modules may return SQLITE_BUSY due to a race condition in the
45701         ** code that determines whether or not the shared-memory region
45702         ** must be zeroed before the requested page is returned.
45703         */
45704         rc = WAL_RETRY;
45705       }else if( SQLITE_OK==(rc = walLockShared(pWal, WAL_RECOVER_LOCK)) ){
45706         walUnlockShared(pWal, WAL_RECOVER_LOCK);
45707         rc = WAL_RETRY;
45708       }else if( rc==SQLITE_BUSY ){
45709         rc = SQLITE_BUSY_RECOVERY;
45710       }
45711     }
45712     if( rc!=SQLITE_OK ){
45713       return rc;
45714     }
45715   }
45716 
45717   pInfo = walCkptInfo(pWal);
45718   if( !useWal && pInfo->nBackfill==pWal->hdr.mxFrame ){
45719     /* The WAL has been completely backfilled (or it is empty).
45720     ** and can be safely ignored.
45721     */
45722     rc = walLockShared(pWal, WAL_READ_LOCK(0));
45723     walShmBarrier(pWal);
45724     if( rc==SQLITE_OK ){
45725       if( memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr)) ){
45726         /* It is not safe to allow the reader to continue here if frames
45727         ** may have been appended to the log before READ_LOCK(0) was obtained.
45728         ** When holding READ_LOCK(0), the reader ignores the entire log file,
45729         ** which implies that the database file contains a trustworthy
45730         ** snapshoT. Since holding READ_LOCK(0) prevents a checkpoint from
45731         ** happening, this is usually correct.
45732         **
45733         ** However, if frames have been appended to the log (or if the log
45734         ** is wrapped and written for that matter) before the READ_LOCK(0)
45735         ** is obtained, that is not necessarily true. A checkpointer may
45736         ** have started to backfill the appended frames but crashed before
45737         ** it finished. Leaving a corrupt image in the database file.
45738         */
45739         walUnlockShared(pWal, WAL_READ_LOCK(0));
45740         return WAL_RETRY;
45741       }
45742       pWal->readLock = 0;
45743       return SQLITE_OK;
45744     }else if( rc!=SQLITE_BUSY ){
45745       return rc;
45746     }
45747   }
45748 
45749   /* If we get this far, it means that the reader will want to use
45750   ** the WAL to get at content from recent commits.  The job now is
45751   ** to select one of the aReadMark[] entries that is closest to
45752   ** but not exceeding pWal->hdr.mxFrame and lock that entry.
45753   */
45754   mxReadMark = 0;
45755   mxI = 0;
45756   for(i=1; i<WAL_NREADER; i++){
45757     u32 thisMark = pInfo->aReadMark[i];
45758     if( mxReadMark<=thisMark && thisMark<=pWal->hdr.mxFrame ){
45759       assert( thisMark!=READMARK_NOT_USED );
45760       mxReadMark = thisMark;
45761       mxI = i;
45762     }
45763   }
45764   /* There was once an "if" here. The extra "{" is to preserve indentation. */
45765   {
45766     if( (pWal->readOnly & WAL_SHM_RDONLY)==0
45767      && (mxReadMark<pWal->hdr.mxFrame || mxI==0)
45768     ){
45769       for(i=1; i<WAL_NREADER; i++){
45770         rc = walLockExclusive(pWal, WAL_READ_LOCK(i), 1);
45771         if( rc==SQLITE_OK ){
45772           mxReadMark = pInfo->aReadMark[i] = pWal->hdr.mxFrame;
45773           mxI = i;
45774           walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1);
45775           break;
45776         }else if( rc!=SQLITE_BUSY ){
45777           return rc;
45778         }
45779       }
45780     }
45781     if( mxI==0 ){
45782       assert( rc==SQLITE_BUSY || (pWal->readOnly & WAL_SHM_RDONLY)!=0 );
45783       return rc==SQLITE_BUSY ? WAL_RETRY : SQLITE_READONLY_CANTLOCK;
45784     }
45785 
45786     rc = walLockShared(pWal, WAL_READ_LOCK(mxI));
45787     if( rc ){
45788       return rc==SQLITE_BUSY ? WAL_RETRY : rc;
45789     }
45790     /* Now that the read-lock has been obtained, check that neither the
45791     ** value in the aReadMark[] array or the contents of the wal-index
45792     ** header have changed.
45793     **
45794     ** It is necessary to check that the wal-index header did not change
45795     ** between the time it was read and when the shared-lock was obtained
45796     ** on WAL_READ_LOCK(mxI) was obtained to account for the possibility
45797     ** that the log file may have been wrapped by a writer, or that frames
45798     ** that occur later in the log than pWal->hdr.mxFrame may have been
45799     ** copied into the database by a checkpointer. If either of these things
45800     ** happened, then reading the database with the current value of
45801     ** pWal->hdr.mxFrame risks reading a corrupted snapshot. So, retry
45802     ** instead.
45803     **
45804     ** This does not guarantee that the copy of the wal-index header is up to
45805     ** date before proceeding. That would not be possible without somehow
45806     ** blocking writers. It only guarantees that a dangerous checkpoint or
45807     ** log-wrap (either of which would require an exclusive lock on
45808     ** WAL_READ_LOCK(mxI)) has not occurred since the snapshot was valid.
45809     */
45810     walShmBarrier(pWal);
45811     if( pInfo->aReadMark[mxI]!=mxReadMark
45812      || memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr))
45813     ){
45814       walUnlockShared(pWal, WAL_READ_LOCK(mxI));
45815       return WAL_RETRY;
45816     }else{
45817       assert( mxReadMark<=pWal->hdr.mxFrame );
45818       pWal->readLock = (i16)mxI;
45819     }
45820   }
45821   return rc;
45822 }
45823 
45824 /*
45825 ** Begin a read transaction on the database.
45826 **
45827 ** This routine used to be called sqlite3OpenSnapshot() and with good reason:
45828 ** it takes a snapshot of the state of the WAL and wal-index for the current
45829 ** instant in time.  The current thread will continue to use this snapshot.
45830 ** Other threads might append new content to the WAL and wal-index but
45831 ** that extra content is ignored by the current thread.
45832 **
45833 ** If the database contents have changes since the previous read
45834 ** transaction, then *pChanged is set to 1 before returning.  The
45835 ** Pager layer will use this to know that is cache is stale and
45836 ** needs to be flushed.
45837 */
45838 SQLITE_PRIVATE int sqlite3WalBeginReadTransaction(Wal *pWal, int *pChanged){
45839   int rc;                         /* Return code */
45840   int cnt = 0;                    /* Number of TryBeginRead attempts */
45841 
45842   do{
45843     rc = walTryBeginRead(pWal, pChanged, 0, ++cnt);
45844   }while( rc==WAL_RETRY );
45845   testcase( (rc&0xff)==SQLITE_BUSY );
45846   testcase( (rc&0xff)==SQLITE_IOERR );
45847   testcase( rc==SQLITE_PROTOCOL );
45848   testcase( rc==SQLITE_OK );
45849   return rc;
45850 }
45851 
45852 /*
45853 ** Finish with a read transaction.  All this does is release the
45854 ** read-lock.
45855 */
45856 SQLITE_PRIVATE void sqlite3WalEndReadTransaction(Wal *pWal){
45857   sqlite3WalEndWriteTransaction(pWal);
45858   if( pWal->readLock>=0 ){
45859     walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock));
45860     pWal->readLock = -1;
45861   }
45862 }
45863 
45864 /*
45865 ** Read a page from the WAL, if it is present in the WAL and if the
45866 ** current read transaction is configured to use the WAL.
45867 **
45868 ** The *pInWal is set to 1 if the requested page is in the WAL and
45869 ** has been loaded.  Or *pInWal is set to 0 if the page was not in
45870 ** the WAL and needs to be read out of the database.
45871 */
45872 SQLITE_PRIVATE int sqlite3WalRead(
45873   Wal *pWal,                      /* WAL handle */
45874   Pgno pgno,                      /* Database page number to read data for */
45875   int *pInWal,                    /* OUT: True if data is read from WAL */
45876   int nOut,                       /* Size of buffer pOut in bytes */
45877   u8 *pOut                        /* Buffer to write page data to */
45878 ){
45879   u32 iRead = 0;                  /* If !=0, WAL frame to return data from */
45880   u32 iLast = pWal->hdr.mxFrame;  /* Last page in WAL for this reader */
45881   int iHash;                      /* Used to loop through N hash tables */
45882 
45883   /* This routine is only be called from within a read transaction. */
45884   assert( pWal->readLock>=0 || pWal->lockError );
45885 
45886   /* If the "last page" field of the wal-index header snapshot is 0, then
45887   ** no data will be read from the wal under any circumstances. Return early
45888   ** in this case as an optimization.  Likewise, if pWal->readLock==0,
45889   ** then the WAL is ignored by the reader so return early, as if the
45890   ** WAL were empty.
45891   */
45892   if( iLast==0 || pWal->readLock==0 ){
45893     *pInWal = 0;
45894     return SQLITE_OK;
45895   }
45896 
45897   /* Search the hash table or tables for an entry matching page number
45898   ** pgno. Each iteration of the following for() loop searches one
45899   ** hash table (each hash table indexes up to HASHTABLE_NPAGE frames).
45900   **
45901   ** This code might run concurrently to the code in walIndexAppend()
45902   ** that adds entries to the wal-index (and possibly to this hash
45903   ** table). This means the value just read from the hash
45904   ** slot (aHash[iKey]) may have been added before or after the
45905   ** current read transaction was opened. Values added after the
45906   ** read transaction was opened may have been written incorrectly -
45907   ** i.e. these slots may contain garbage data. However, we assume
45908   ** that any slots written before the current read transaction was
45909   ** opened remain unmodified.
45910   **
45911   ** For the reasons above, the if(...) condition featured in the inner
45912   ** loop of the following block is more stringent that would be required
45913   ** if we had exclusive access to the hash-table:
45914   **
45915   **   (aPgno[iFrame]==pgno):
45916   **     This condition filters out normal hash-table collisions.
45917   **
45918   **   (iFrame<=iLast):
45919   **     This condition filters out entries that were added to the hash
45920   **     table after the current read-transaction had started.
45921   */
45922   for(iHash=walFramePage(iLast); iHash>=0 && iRead==0; iHash--){
45923     volatile ht_slot *aHash;      /* Pointer to hash table */
45924     volatile u32 *aPgno;          /* Pointer to array of page numbers */
45925     u32 iZero;                    /* Frame number corresponding to aPgno[0] */
45926     int iKey;                     /* Hash slot index */
45927     int nCollide;                 /* Number of hash collisions remaining */
45928     int rc;                       /* Error code */
45929 
45930     rc = walHashGet(pWal, iHash, &aHash, &aPgno, &iZero);
45931     if( rc!=SQLITE_OK ){
45932       return rc;
45933     }
45934     nCollide = HASHTABLE_NSLOT;
45935     for(iKey=walHash(pgno); aHash[iKey]; iKey=walNextHash(iKey)){
45936       u32 iFrame = aHash[iKey] + iZero;
45937       if( iFrame<=iLast && aPgno[aHash[iKey]]==pgno ){
45938         assert( iFrame>iRead );
45939         iRead = iFrame;
45940       }
45941       if( (nCollide--)==0 ){
45942         return SQLITE_CORRUPT_BKPT;
45943       }
45944     }
45945   }
45946 
45947 #ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
45948   /* If expensive assert() statements are available, do a linear search
45949   ** of the wal-index file content. Make sure the results agree with the
45950   ** result obtained using the hash indexes above.  */
45951   {
45952     u32 iRead2 = 0;
45953     u32 iTest;
45954     for(iTest=iLast; iTest>0; iTest--){
45955       if( walFramePgno(pWal, iTest)==pgno ){
45956         iRead2 = iTest;
45957         break;
45958       }
45959     }
45960     assert( iRead==iRead2 );
45961   }
45962 #endif
45963 
45964   /* If iRead is non-zero, then it is the log frame number that contains the
45965   ** required page. Read and return data from the log file.
45966   */
45967   if( iRead ){
45968     int sz;
45969     i64 iOffset;
45970     sz = pWal->hdr.szPage;
45971     sz = (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16);
45972     testcase( sz<=32768 );
45973     testcase( sz>=65536 );
45974     iOffset = walFrameOffset(iRead, sz) + WAL_FRAME_HDRSIZE;
45975     *pInWal = 1;
45976     /* testcase( IS_BIG_INT(iOffset) ); // requires a 4GiB WAL */
45977     return sqlite3OsRead(pWal->pWalFd, pOut, nOut, iOffset);
45978   }
45979 
45980   *pInWal = 0;
45981   return SQLITE_OK;
45982 }
45983 
45984 
45985 /*
45986 ** Return the size of the database in pages (or zero, if unknown).
45987 */
45988 SQLITE_PRIVATE Pgno sqlite3WalDbsize(Wal *pWal){
45989   if( pWal && ALWAYS(pWal->readLock>=0) ){
45990     return pWal->hdr.nPage;
45991   }
45992   return 0;
45993 }
45994 
45995 
45996 /*
45997 ** This function starts a write transaction on the WAL.
45998 **
45999 ** A read transaction must have already been started by a prior call
46000 ** to sqlite3WalBeginReadTransaction().
46001 **
46002 ** If another thread or process has written into the database since
46003 ** the read transaction was started, then it is not possible for this
46004 ** thread to write as doing so would cause a fork.  So this routine
46005 ** returns SQLITE_BUSY in that case and no write transaction is started.
46006 **
46007 ** There can only be a single writer active at a time.
46008 */
46009 SQLITE_PRIVATE int sqlite3WalBeginWriteTransaction(Wal *pWal){
46010   int rc;
46011 
46012   /* Cannot start a write transaction without first holding a read
46013   ** transaction. */
46014   assert( pWal->readLock>=0 );
46015 
46016   if( pWal->readOnly ){
46017     return SQLITE_READONLY;
46018   }
46019 
46020   /* Only one writer allowed at a time.  Get the write lock.  Return
46021   ** SQLITE_BUSY if unable.
46022   */
46023   rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1);
46024   if( rc ){
46025     return rc;
46026   }
46027   pWal->writeLock = 1;
46028 
46029   /* If another connection has written to the database file since the
46030   ** time the read transaction on this connection was started, then
46031   ** the write is disallowed.
46032   */
46033   if( memcmp(&pWal->hdr, (void *)walIndexHdr(pWal), sizeof(WalIndexHdr))!=0 ){
46034     walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1);
46035     pWal->writeLock = 0;
46036     rc = SQLITE_BUSY;
46037   }
46038 
46039   return rc;
46040 }
46041 
46042 /*
46043 ** End a write transaction.  The commit has already been done.  This
46044 ** routine merely releases the lock.
46045 */
46046 SQLITE_PRIVATE int sqlite3WalEndWriteTransaction(Wal *pWal){
46047   if( pWal->writeLock ){
46048     walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1);
46049     pWal->writeLock = 0;
46050   }
46051   return SQLITE_OK;
46052 }
46053 
46054 /*
46055 ** If any data has been written (but not committed) to the log file, this
46056 ** function moves the write-pointer back to the start of the transaction.
46057 **
46058 ** Additionally, the callback function is invoked for each frame written
46059 ** to the WAL since the start of the transaction. If the callback returns
46060 ** other than SQLITE_OK, it is not invoked again and the error code is
46061 ** returned to the caller.
46062 **
46063 ** Otherwise, if the callback function does not return an error, this
46064 ** function returns SQLITE_OK.
46065 */
46066 SQLITE_PRIVATE int sqlite3WalUndo(Wal *pWal, int (*xUndo)(void *, Pgno), void *pUndoCtx){
46067   int rc = SQLITE_OK;
46068   if( ALWAYS(pWal->writeLock) ){
46069     Pgno iMax = pWal->hdr.mxFrame;
46070     Pgno iFrame;
46071 
46072     /* Restore the clients cache of the wal-index header to the state it
46073     ** was in before the client began writing to the database.
46074     */
46075     memcpy(&pWal->hdr, (void *)walIndexHdr(pWal), sizeof(WalIndexHdr));
46076 
46077     for(iFrame=pWal->hdr.mxFrame+1;
46078         ALWAYS(rc==SQLITE_OK) && iFrame<=iMax;
46079         iFrame++
46080     ){
46081       /* This call cannot fail. Unless the page for which the page number
46082       ** is passed as the second argument is (a) in the cache and
46083       ** (b) has an outstanding reference, then xUndo is either a no-op
46084       ** (if (a) is false) or simply expels the page from the cache (if (b)
46085       ** is false).
46086       **
46087       ** If the upper layer is doing a rollback, it is guaranteed that there
46088       ** are no outstanding references to any page other than page 1. And
46089       ** page 1 is never written to the log until the transaction is
46090       ** committed. As a result, the call to xUndo may not fail.
46091       */
46092       assert( walFramePgno(pWal, iFrame)!=1 );
46093       rc = xUndo(pUndoCtx, walFramePgno(pWal, iFrame));
46094     }
46095     walCleanupHash(pWal);
46096   }
46097   assert( rc==SQLITE_OK );
46098   return rc;
46099 }
46100 
46101 /*
46102 ** Argument aWalData must point to an array of WAL_SAVEPOINT_NDATA u32
46103 ** values. This function populates the array with values required to
46104 ** "rollback" the write position of the WAL handle back to the current
46105 ** point in the event of a savepoint rollback (via WalSavepointUndo()).
46106 */
46107 SQLITE_PRIVATE void sqlite3WalSavepoint(Wal *pWal, u32 *aWalData){
46108   assert( pWal->writeLock );
46109   aWalData[0] = pWal->hdr.mxFrame;
46110   aWalData[1] = pWal->hdr.aFrameCksum[0];
46111   aWalData[2] = pWal->hdr.aFrameCksum[1];
46112   aWalData[3] = pWal->nCkpt;
46113 }
46114 
46115 /*
46116 ** Move the write position of the WAL back to the point identified by
46117 ** the values in the aWalData[] array. aWalData must point to an array
46118 ** of WAL_SAVEPOINT_NDATA u32 values that has been previously populated
46119 ** by a call to WalSavepoint().
46120 */
46121 SQLITE_PRIVATE int sqlite3WalSavepointUndo(Wal *pWal, u32 *aWalData){
46122   int rc = SQLITE_OK;
46123 
46124   assert( pWal->writeLock );
46125   assert( aWalData[3]!=pWal->nCkpt || aWalData[0]<=pWal->hdr.mxFrame );
46126 
46127   if( aWalData[3]!=pWal->nCkpt ){
46128     /* This savepoint was opened immediately after the write-transaction
46129     ** was started. Right after that, the writer decided to wrap around
46130     ** to the start of the log. Update the savepoint values to match.
46131     */
46132     aWalData[0] = 0;
46133     aWalData[3] = pWal->nCkpt;
46134   }
46135 
46136   if( aWalData[0]<pWal->hdr.mxFrame ){
46137     pWal->hdr.mxFrame = aWalData[0];
46138     pWal->hdr.aFrameCksum[0] = aWalData[1];
46139     pWal->hdr.aFrameCksum[1] = aWalData[2];
46140     walCleanupHash(pWal);
46141   }
46142 
46143   return rc;
46144 }
46145 
46146 /*
46147 ** This function is called just before writing a set of frames to the log
46148 ** file (see sqlite3WalFrames()). It checks to see if, instead of appending
46149 ** to the current log file, it is possible to overwrite the start of the
46150 ** existing log file with the new frames (i.e. "reset" the log). If so,
46151 ** it sets pWal->hdr.mxFrame to 0. Otherwise, pWal->hdr.mxFrame is left
46152 ** unchanged.
46153 **
46154 ** SQLITE_OK is returned if no error is encountered (regardless of whether
46155 ** or not pWal->hdr.mxFrame is modified). An SQLite error code is returned
46156 ** if an error occurs.
46157 */
46158 static int walRestartLog(Wal *pWal){
46159   int rc = SQLITE_OK;
46160   int cnt;
46161 
46162   if( pWal->readLock==0 ){
46163     volatile WalCkptInfo *pInfo = walCkptInfo(pWal);
46164     assert( pInfo->nBackfill==pWal->hdr.mxFrame );
46165     if( pInfo->nBackfill>0 ){
46166       u32 salt1;
46167       sqlite3_randomness(4, &salt1);
46168       rc = walLockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
46169       if( rc==SQLITE_OK ){
46170         /* If all readers are using WAL_READ_LOCK(0) (in other words if no
46171         ** readers are currently using the WAL), then the transactions
46172         ** frames will overwrite the start of the existing log. Update the
46173         ** wal-index header to reflect this.
46174         **
46175         ** In theory it would be Ok to update the cache of the header only
46176         ** at this point. But updating the actual wal-index header is also
46177         ** safe and means there is no special case for sqlite3WalUndo()
46178         ** to handle if this transaction is rolled back.
46179         */
46180         int i;                    /* Loop counter */
46181         u32 *aSalt = pWal->hdr.aSalt;       /* Big-endian salt values */
46182 
46183         /* Limit the size of WAL file if the journal_size_limit PRAGMA is
46184         ** set to a non-negative value.  Log errors encountered
46185         ** during the truncation attempt. */
46186         if( pWal->mxWalSize>=0 ){
46187           i64 sz;
46188           int rx;
46189           sqlite3BeginBenignMalloc();
46190           rx = sqlite3OsFileSize(pWal->pWalFd, &sz);
46191           if( rx==SQLITE_OK && (sz > pWal->mxWalSize) ){
46192             rx = sqlite3OsTruncate(pWal->pWalFd, pWal->mxWalSize);
46193           }
46194           sqlite3EndBenignMalloc();
46195           if( rx ){
46196             sqlite3_log(rx, "cannot limit WAL size: %s", pWal->zWalName);
46197           }
46198         }
46199 
46200         pWal->nCkpt++;
46201         pWal->hdr.mxFrame = 0;
46202         sqlite3Put4byte((u8*)&aSalt[0], 1 + sqlite3Get4byte((u8*)&aSalt[0]));
46203         aSalt[1] = salt1;
46204         walIndexWriteHdr(pWal);
46205         pInfo->nBackfill = 0;
46206         for(i=1; i<WAL_NREADER; i++) pInfo->aReadMark[i] = READMARK_NOT_USED;
46207         assert( pInfo->aReadMark[0]==0 );
46208         walUnlockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
46209       }else if( rc!=SQLITE_BUSY ){
46210         return rc;
46211       }
46212     }
46213     walUnlockShared(pWal, WAL_READ_LOCK(0));
46214     pWal->readLock = -1;
46215     cnt = 0;
46216     do{
46217       int notUsed;
46218       rc = walTryBeginRead(pWal, &notUsed, 1, ++cnt);
46219     }while( rc==WAL_RETRY );
46220     assert( (rc&0xff)!=SQLITE_BUSY ); /* BUSY not possible when useWal==1 */
46221     testcase( (rc&0xff)==SQLITE_IOERR );
46222     testcase( rc==SQLITE_PROTOCOL );
46223     testcase( rc==SQLITE_OK );
46224   }
46225   return rc;
46226 }
46227 
46228 /*
46229 ** Write a set of frames to the log. The caller must hold the write-lock
46230 ** on the log file (obtained using sqlite3WalBeginWriteTransaction()).
46231 */
46232 SQLITE_PRIVATE int sqlite3WalFrames(
46233   Wal *pWal,                      /* Wal handle to write to */
46234   int szPage,                     /* Database page-size in bytes */
46235   PgHdr *pList,                   /* List of dirty pages to write */
46236   Pgno nTruncate,                 /* Database size after this commit */
46237   int isCommit,                   /* True if this is a commit */
46238   int sync_flags                  /* Flags to pass to OsSync() (or 0) */
46239 ){
46240   int rc;                         /* Used to catch return codes */
46241   u32 iFrame;                     /* Next frame address */
46242   u8 aFrame[WAL_FRAME_HDRSIZE];   /* Buffer to assemble frame-header in */
46243   PgHdr *p;                       /* Iterator to run through pList with. */
46244   PgHdr *pLast = 0;               /* Last frame in list */
46245   int nLast = 0;                  /* Number of extra copies of last page */
46246 
46247   assert( pList );
46248   assert( pWal->writeLock );
46249 
46250 #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
46251   { int cnt; for(cnt=0, p=pList; p; p=p->pDirty, cnt++){}
46252     WALTRACE(("WAL%p: frame write begin. %d frames. mxFrame=%d. %s\n",
46253               pWal, cnt, pWal->hdr.mxFrame, isCommit ? "Commit" : "Spill"));
46254   }
46255 #endif
46256 
46257   /* See if it is possible to write these frames into the start of the
46258   ** log file, instead of appending to it at pWal->hdr.mxFrame.
46259   */
46260   if( SQLITE_OK!=(rc = walRestartLog(pWal)) ){
46261     return rc;
46262   }
46263 
46264   /* If this is the first frame written into the log, write the WAL
46265   ** header to the start of the WAL file. See comments at the top of
46266   ** this source file for a description of the WAL header format.
46267   */
46268   iFrame = pWal->hdr.mxFrame;
46269   if( iFrame==0 ){
46270     u8 aWalHdr[WAL_HDRSIZE];      /* Buffer to assemble wal-header in */
46271     u32 aCksum[2];                /* Checksum for wal-header */
46272 
46273     sqlite3Put4byte(&aWalHdr[0], (WAL_MAGIC | SQLITE_BIGENDIAN));
46274     sqlite3Put4byte(&aWalHdr[4], WAL_MAX_VERSION);
46275     sqlite3Put4byte(&aWalHdr[8], szPage);
46276     sqlite3Put4byte(&aWalHdr[12], pWal->nCkpt);
46277     sqlite3_randomness(8, pWal->hdr.aSalt);
46278     memcpy(&aWalHdr[16], pWal->hdr.aSalt, 8);
46279     walChecksumBytes(1, aWalHdr, WAL_HDRSIZE-2*4, 0, aCksum);
46280     sqlite3Put4byte(&aWalHdr[24], aCksum[0]);
46281     sqlite3Put4byte(&aWalHdr[28], aCksum[1]);
46282 
46283     pWal->szPage = szPage;
46284     pWal->hdr.bigEndCksum = SQLITE_BIGENDIAN;
46285     pWal->hdr.aFrameCksum[0] = aCksum[0];
46286     pWal->hdr.aFrameCksum[1] = aCksum[1];
46287 
46288     rc = sqlite3OsWrite(pWal->pWalFd, aWalHdr, sizeof(aWalHdr), 0);
46289     WALTRACE(("WAL%p: wal-header write %s\n", pWal, rc ? "failed" : "ok"));
46290     if( rc!=SQLITE_OK ){
46291       return rc;
46292     }
46293   }
46294   assert( (int)pWal->szPage==szPage );
46295 
46296   /* Write the log file. */
46297   for(p=pList; p; p=p->pDirty){
46298     u32 nDbsize;                  /* Db-size field for frame header */
46299     i64 iOffset;                  /* Write offset in log file */
46300     void *pData;
46301 
46302     iOffset = walFrameOffset(++iFrame, szPage);
46303     /* testcase( IS_BIG_INT(iOffset) ); // requires a 4GiB WAL */
46304 
46305     /* Populate and write the frame header */
46306     nDbsize = (isCommit && p->pDirty==0) ? nTruncate : 0;
46307 #if defined(SQLITE_HAS_CODEC)
46308     if( (pData = sqlite3PagerCodec(p))==0 ) return SQLITE_NOMEM;
46309 #else
46310     pData = p->pData;
46311 #endif
46312     walEncodeFrame(pWal, p->pgno, nDbsize, pData, aFrame);
46313     rc = sqlite3OsWrite(pWal->pWalFd, aFrame, sizeof(aFrame), iOffset);
46314     if( rc!=SQLITE_OK ){
46315       return rc;
46316     }
46317 
46318     /* Write the page data */
46319     rc = sqlite3OsWrite(pWal->pWalFd, pData, szPage, iOffset+sizeof(aFrame));
46320     if( rc!=SQLITE_OK ){
46321       return rc;
46322     }
46323     pLast = p;
46324   }
46325 
46326   /* Sync the log file if the 'isSync' flag was specified. */
46327   if( sync_flags ){
46328     i64 iSegment = sqlite3OsSectorSize(pWal->pWalFd);
46329     i64 iOffset = walFrameOffset(iFrame+1, szPage);
46330 
46331     assert( isCommit );
46332     assert( iSegment>0 );
46333 
46334     iSegment = (((iOffset+iSegment-1)/iSegment) * iSegment);
46335     while( iOffset<iSegment ){
46336       void *pData;
46337 #if defined(SQLITE_HAS_CODEC)
46338       if( (pData = sqlite3PagerCodec(pLast))==0 ) return SQLITE_NOMEM;
46339 #else
46340       pData = pLast->pData;
46341 #endif
46342       walEncodeFrame(pWal, pLast->pgno, nTruncate, pData, aFrame);
46343       /* testcase( IS_BIG_INT(iOffset) ); // requires a 4GiB WAL */
46344       rc = sqlite3OsWrite(pWal->pWalFd, aFrame, sizeof(aFrame), iOffset);
46345       if( rc!=SQLITE_OK ){
46346         return rc;
46347       }
46348       iOffset += WAL_FRAME_HDRSIZE;
46349       rc = sqlite3OsWrite(pWal->pWalFd, pData, szPage, iOffset);
46350       if( rc!=SQLITE_OK ){
46351         return rc;
46352       }
46353       nLast++;
46354       iOffset += szPage;
46355     }
46356 
46357     rc = sqlite3OsSync(pWal->pWalFd, sync_flags);
46358   }
46359 
46360   /* Append data to the wal-index. It is not necessary to lock the
46361   ** wal-index to do this as the SQLITE_SHM_WRITE lock held on the wal-index
46362   ** guarantees that there are no other writers, and no data that may
46363   ** be in use by existing readers is being overwritten.
46364   */
46365   iFrame = pWal->hdr.mxFrame;
46366   for(p=pList; p && rc==SQLITE_OK; p=p->pDirty){
46367     iFrame++;
46368     rc = walIndexAppend(pWal, iFrame, p->pgno);
46369   }
46370   while( nLast>0 && rc==SQLITE_OK ){
46371     iFrame++;
46372     nLast--;
46373     rc = walIndexAppend(pWal, iFrame, pLast->pgno);
46374   }
46375 
46376   if( rc==SQLITE_OK ){
46377     /* Update the private copy of the header. */
46378     pWal->hdr.szPage = (u16)((szPage&0xff00) | (szPage>>16));
46379     testcase( szPage<=32768 );
46380     testcase( szPage>=65536 );
46381     pWal->hdr.mxFrame = iFrame;
46382     if( isCommit ){
46383       pWal->hdr.iChange++;
46384       pWal->hdr.nPage = nTruncate;
46385     }
46386     /* If this is a commit, update the wal-index header too. */
46387     if( isCommit ){
46388       walIndexWriteHdr(pWal);
46389       pWal->iCallback = iFrame;
46390     }
46391   }
46392 
46393   WALTRACE(("WAL%p: frame write %s\n", pWal, rc ? "failed" : "ok"));
46394   return rc;
46395 }
46396 
46397 /*
46398 ** This routine is called to implement sqlite3_wal_checkpoint() and
46399 ** related interfaces.
46400 **
46401 ** Obtain a CHECKPOINT lock and then backfill as much information as
46402 ** we can from WAL into the database.
46403 **
46404 ** If parameter xBusy is not NULL, it is a pointer to a busy-handler
46405 ** callback. In this case this function runs a blocking checkpoint.
46406 */
46407 SQLITE_PRIVATE int sqlite3WalCheckpoint(
46408   Wal *pWal,                      /* Wal connection */
46409   int eMode,                      /* PASSIVE, FULL or RESTART */
46410   int (*xBusy)(void*),            /* Function to call when busy */
46411   void *pBusyArg,                 /* Context argument for xBusyHandler */
46412   int sync_flags,                 /* Flags to sync db file with (or 0) */
46413   int nBuf,                       /* Size of temporary buffer */
46414   u8 *zBuf,                       /* Temporary buffer to use */
46415   int *pnLog,                     /* OUT: Number of frames in WAL */
46416   int *pnCkpt                     /* OUT: Number of backfilled frames in WAL */
46417 ){
46418   int rc;                         /* Return code */
46419   int isChanged = 0;              /* True if a new wal-index header is loaded */
46420   int eMode2 = eMode;             /* Mode to pass to walCheckpoint() */
46421 
46422   assert( pWal->ckptLock==0 );
46423   assert( pWal->writeLock==0 );
46424 
46425   if( pWal->readOnly ) return SQLITE_READONLY;
46426   WALTRACE(("WAL%p: checkpoint begins\n", pWal));
46427   rc = walLockExclusive(pWal, WAL_CKPT_LOCK, 1);
46428   if( rc ){
46429     /* Usually this is SQLITE_BUSY meaning that another thread or process
46430     ** is already running a checkpoint, or maybe a recovery.  But it might
46431     ** also be SQLITE_IOERR. */
46432     return rc;
46433   }
46434   pWal->ckptLock = 1;
46435 
46436   /* If this is a blocking-checkpoint, then obtain the write-lock as well
46437   ** to prevent any writers from running while the checkpoint is underway.
46438   ** This has to be done before the call to walIndexReadHdr() below.
46439   **
46440   ** If the writer lock cannot be obtained, then a passive checkpoint is
46441   ** run instead. Since the checkpointer is not holding the writer lock,
46442   ** there is no point in blocking waiting for any readers. Assuming no
46443   ** other error occurs, this function will return SQLITE_BUSY to the caller.
46444   */
46445   if( eMode!=SQLITE_CHECKPOINT_PASSIVE ){
46446     rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_WRITE_LOCK, 1);
46447     if( rc==SQLITE_OK ){
46448       pWal->writeLock = 1;
46449     }else if( rc==SQLITE_BUSY ){
46450       eMode2 = SQLITE_CHECKPOINT_PASSIVE;
46451       rc = SQLITE_OK;
46452     }
46453   }
46454 
46455   /* Read the wal-index header. */
46456   if( rc==SQLITE_OK ){
46457     rc = walIndexReadHdr(pWal, &isChanged);
46458   }
46459 
46460   /* Copy data from the log to the database file. */
46461   if( rc==SQLITE_OK ){
46462     if( pWal->hdr.mxFrame && walPagesize(pWal)!=nBuf ){
46463       rc = SQLITE_CORRUPT_BKPT;
46464     }else{
46465       rc = walCheckpoint(pWal, eMode2, xBusy, pBusyArg, sync_flags, zBuf);
46466     }
46467 
46468     /* If no error occurred, set the output variables. */
46469     if( rc==SQLITE_OK || rc==SQLITE_BUSY ){
46470       if( pnLog ) *pnLog = (int)pWal->hdr.mxFrame;
46471       if( pnCkpt ) *pnCkpt = (int)(walCkptInfo(pWal)->nBackfill);
46472     }
46473   }
46474 
46475   if( isChanged ){
46476     /* If a new wal-index header was loaded before the checkpoint was
46477     ** performed, then the pager-cache associated with pWal is now
46478     ** out of date. So zero the cached wal-index header to ensure that
46479     ** next time the pager opens a snapshot on this database it knows that
46480     ** the cache needs to be reset.
46481     */
46482     memset(&pWal->hdr, 0, sizeof(WalIndexHdr));
46483   }
46484 
46485   /* Release the locks. */
46486   sqlite3WalEndWriteTransaction(pWal);
46487   walUnlockExclusive(pWal, WAL_CKPT_LOCK, 1);
46488   pWal->ckptLock = 0;
46489   WALTRACE(("WAL%p: checkpoint %s\n", pWal, rc ? "failed" : "ok"));
46490   return (rc==SQLITE_OK && eMode!=eMode2 ? SQLITE_BUSY : rc);
46491 }
46492 
46493 /* Return the value to pass to a sqlite3_wal_hook callback, the
46494 ** number of frames in the WAL at the point of the last commit since
46495 ** sqlite3WalCallback() was called.  If no commits have occurred since
46496 ** the last call, then return 0.
46497 */
46498 SQLITE_PRIVATE int sqlite3WalCallback(Wal *pWal){
46499   u32 ret = 0;
46500   if( pWal ){
46501     ret = pWal->iCallback;
46502     pWal->iCallback = 0;
46503   }
46504   return (int)ret;
46505 }
46506 
46507 /*
46508 ** This function is called to change the WAL subsystem into or out
46509 ** of locking_mode=EXCLUSIVE.
46510 **
46511 ** If op is zero, then attempt to change from locking_mode=EXCLUSIVE
46512 ** into locking_mode=NORMAL.  This means that we must acquire a lock
46513 ** on the pWal->readLock byte.  If the WAL is already in locking_mode=NORMAL
46514 ** or if the acquisition of the lock fails, then return 0.  If the
46515 ** transition out of exclusive-mode is successful, return 1.  This
46516 ** operation must occur while the pager is still holding the exclusive
46517 ** lock on the main database file.
46518 **
46519 ** If op is one, then change from locking_mode=NORMAL into
46520 ** locking_mode=EXCLUSIVE.  This means that the pWal->readLock must
46521 ** be released.  Return 1 if the transition is made and 0 if the
46522 ** WAL is already in exclusive-locking mode - meaning that this
46523 ** routine is a no-op.  The pager must already hold the exclusive lock
46524 ** on the main database file before invoking this operation.
46525 **
46526 ** If op is negative, then do a dry-run of the op==1 case but do
46527 ** not actually change anything. The pager uses this to see if it
46528 ** should acquire the database exclusive lock prior to invoking
46529 ** the op==1 case.
46530 */
46531 SQLITE_PRIVATE int sqlite3WalExclusiveMode(Wal *pWal, int op){
46532   int rc;
46533   assert( pWal->writeLock==0 );
46534   assert( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE || op==-1 );
46535 
46536   /* pWal->readLock is usually set, but might be -1 if there was a
46537   ** prior error while attempting to acquire are read-lock. This cannot
46538   ** happen if the connection is actually in exclusive mode (as no xShmLock
46539   ** locks are taken in this case). Nor should the pager attempt to
46540   ** upgrade to exclusive-mode following such an error.
46541   */
46542   assert( pWal->readLock>=0 || pWal->lockError );
46543   assert( pWal->readLock>=0 || (op<=0 && pWal->exclusiveMode==0) );
46544 
46545   if( op==0 ){
46546     if( pWal->exclusiveMode ){
46547       pWal->exclusiveMode = 0;
46548       if( walLockShared(pWal, WAL_READ_LOCK(pWal->readLock))!=SQLITE_OK ){
46549         pWal->exclusiveMode = 1;
46550       }
46551       rc = pWal->exclusiveMode==0;
46552     }else{
46553       /* Already in locking_mode=NORMAL */
46554       rc = 0;
46555     }
46556   }else if( op>0 ){
46557     assert( pWal->exclusiveMode==0 );
46558     assert( pWal->readLock>=0 );
46559     walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock));
46560     pWal->exclusiveMode = 1;
46561     rc = 1;
46562   }else{
46563     rc = pWal->exclusiveMode==0;
46564   }
46565   return rc;
46566 }
46567 
46568 /*
46569 ** Return true if the argument is non-NULL and the WAL module is using
46570 ** heap-memory for the wal-index. Otherwise, if the argument is NULL or the
46571 ** WAL module is using shared-memory, return false.
46572 */
46573 SQLITE_PRIVATE int sqlite3WalHeapMemory(Wal *pWal){
46574   return (pWal && pWal->exclusiveMode==WAL_HEAPMEMORY_MODE );
46575 }
46576 
46577 #endif /* #ifndef SQLITE_OMIT_WAL */
46578 
46579 /************** End of wal.c *************************************************/
46580 /************** Begin file btmutex.c *****************************************/
46581 /*
46582 ** 2007 August 27
46583 **
46584 ** The author disclaims copyright to this source code.  In place of
46585 ** a legal notice, here is a blessing:
46586 **
46587 **    May you do good and not evil.
46588 **    May you find forgiveness for yourself and forgive others.
46589 **    May you share freely, never taking more than you give.
46590 **
46591 *************************************************************************
46592 **
46593 ** This file contains code used to implement mutexes on Btree objects.
46594 ** This code really belongs in btree.c.  But btree.c is getting too
46595 ** big and we want to break it down some.  This packaged seemed like
46596 ** a good breakout.
46597 */
46598 /************** Include btreeInt.h in the middle of btmutex.c ****************/
46599 /************** Begin file btreeInt.h ****************************************/
46600 /*
46601 ** 2004 April 6
46602 **
46603 ** The author disclaims copyright to this source code.  In place of
46604 ** a legal notice, here is a blessing:
46605 **
46606 **    May you do good and not evil.
46607 **    May you find forgiveness for yourself and forgive others.
46608 **    May you share freely, never taking more than you give.
46609 **
46610 *************************************************************************
46611 ** This file implements a external (disk-based) database using BTrees.
46612 ** For a detailed discussion of BTrees, refer to
46613 **
46614 **     Donald E. Knuth, THE ART OF COMPUTER PROGRAMMING, Volume 3:
46615 **     "Sorting And Searching", pages 473-480. Addison-Wesley
46616 **     Publishing Company, Reading, Massachusetts.
46617 **
46618 ** The basic idea is that each page of the file contains N database
46619 ** entries and N+1 pointers to subpages.
46620 **
46621 **   ----------------------------------------------------------------
46622 **   |  Ptr(0) | Key(0) | Ptr(1) | Key(1) | ... | Key(N-1) | Ptr(N) |
46623 **   ----------------------------------------------------------------
46624 **
46625 ** All of the keys on the page that Ptr(0) points to have values less
46626 ** than Key(0).  All of the keys on page Ptr(1) and its subpages have
46627 ** values greater than Key(0) and less than Key(1).  All of the keys
46628 ** on Ptr(N) and its subpages have values greater than Key(N-1).  And
46629 ** so forth.
46630 **
46631 ** Finding a particular key requires reading O(log(M)) pages from the
46632 ** disk where M is the number of entries in the tree.
46633 **
46634 ** In this implementation, a single file can hold one or more separate
46635 ** BTrees.  Each BTree is identified by the index of its root page.  The
46636 ** key and data for any entry are combined to form the "payload".  A
46637 ** fixed amount of payload can be carried directly on the database
46638 ** page.  If the payload is larger than the preset amount then surplus
46639 ** bytes are stored on overflow pages.  The payload for an entry
46640 ** and the preceding pointer are combined to form a "Cell".  Each
46641 ** page has a small header which contains the Ptr(N) pointer and other
46642 ** information such as the size of key and data.
46643 **
46644 ** FORMAT DETAILS
46645 **
46646 ** The file is divided into pages.  The first page is called page 1,
46647 ** the second is page 2, and so forth.  A page number of zero indicates
46648 ** "no such page".  The page size can be any power of 2 between 512 and 65536.
46649 ** Each page can be either a btree page, a freelist page, an overflow
46650 ** page, or a pointer-map page.
46651 **
46652 ** The first page is always a btree page.  The first 100 bytes of the first
46653 ** page contain a special header (the "file header") that describes the file.
46654 ** The format of the file header is as follows:
46655 **
46656 **   OFFSET   SIZE    DESCRIPTION
46657 **      0      16     Header string: "SQLite format 3\000"
46658 **     16       2     Page size in bytes.
46659 **     18       1     File format write version
46660 **     19       1     File format read version
46661 **     20       1     Bytes of unused space at the end of each page
46662 **     21       1     Max embedded payload fraction
46663 **     22       1     Min embedded payload fraction
46664 **     23       1     Min leaf payload fraction
46665 **     24       4     File change counter
46666 **     28       4     Reserved for future use
46667 **     32       4     First freelist page
46668 **     36       4     Number of freelist pages in the file
46669 **     40      60     15 4-byte meta values passed to higher layers
46670 **
46671 **     40       4     Schema cookie
46672 **     44       4     File format of schema layer
46673 **     48       4     Size of page cache
46674 **     52       4     Largest root-page (auto/incr_vacuum)
46675 **     56       4     1=UTF-8 2=UTF16le 3=UTF16be
46676 **     60       4     User version
46677 **     64       4     Incremental vacuum mode
46678 **     68       4     unused
46679 **     72       4     unused
46680 **     76       4     unused
46681 **
46682 ** All of the integer values are big-endian (most significant byte first).
46683 **
46684 ** The file change counter is incremented when the database is changed
46685 ** This counter allows other processes to know when the file has changed
46686 ** and thus when they need to flush their cache.
46687 **
46688 ** The max embedded payload fraction is the amount of the total usable
46689 ** space in a page that can be consumed by a single cell for standard
46690 ** B-tree (non-LEAFDATA) tables.  A value of 255 means 100%.  The default
46691 ** is to limit the maximum cell size so that at least 4 cells will fit
46692 ** on one page.  Thus the default max embedded payload fraction is 64.
46693 **
46694 ** If the payload for a cell is larger than the max payload, then extra
46695 ** payload is spilled to overflow pages.  Once an overflow page is allocated,
46696 ** as many bytes as possible are moved into the overflow pages without letting
46697 ** the cell size drop below the min embedded payload fraction.
46698 **
46699 ** The min leaf payload fraction is like the min embedded payload fraction
46700 ** except that it applies to leaf nodes in a LEAFDATA tree.  The maximum
46701 ** payload fraction for a LEAFDATA tree is always 100% (or 255) and it
46702 ** not specified in the header.
46703 **
46704 ** Each btree pages is divided into three sections:  The header, the
46705 ** cell pointer array, and the cell content area.  Page 1 also has a 100-byte
46706 ** file header that occurs before the page header.
46707 **
46708 **      |----------------|
46709 **      | file header    |   100 bytes.  Page 1 only.
46710 **      |----------------|
46711 **      | page header    |   8 bytes for leaves.  12 bytes for interior nodes
46712 **      |----------------|
46713 **      | cell pointer   |   |  2 bytes per cell.  Sorted order.
46714 **      | array          |   |  Grows downward
46715 **      |                |   v
46716 **      |----------------|
46717 **      | unallocated    |
46718 **      | space          |
46719 **      |----------------|   ^  Grows upwards
46720 **      | cell content   |   |  Arbitrary order interspersed with freeblocks.
46721 **      | area           |   |  and free space fragments.
46722 **      |----------------|
46723 **
46724 ** The page headers looks like this:
46725 **
46726 **   OFFSET   SIZE     DESCRIPTION
46727 **      0       1      Flags. 1: intkey, 2: zerodata, 4: leafdata, 8: leaf
46728 **      1       2      byte offset to the first freeblock
46729 **      3       2      number of cells on this page
46730 **      5       2      first byte of the cell content area
46731 **      7       1      number of fragmented free bytes
46732 **      8       4      Right child (the Ptr(N) value).  Omitted on leaves.
46733 **
46734 ** The flags define the format of this btree page.  The leaf flag means that
46735 ** this page has no children.  The zerodata flag means that this page carries
46736 ** only keys and no data.  The intkey flag means that the key is a integer
46737 ** which is stored in the key size entry of the cell header rather than in
46738 ** the payload area.
46739 **
46740 ** The cell pointer array begins on the first byte after the page header.
46741 ** The cell pointer array contains zero or more 2-byte numbers which are
46742 ** offsets from the beginning of the page to the cell content in the cell
46743 ** content area.  The cell pointers occur in sorted order.  The system strives
46744 ** to keep free space after the last cell pointer so that new cells can
46745 ** be easily added without having to defragment the page.
46746 **
46747 ** Cell content is stored at the very end of the page and grows toward the
46748 ** beginning of the page.
46749 **
46750 ** Unused space within the cell content area is collected into a linked list of
46751 ** freeblocks.  Each freeblock is at least 4 bytes in size.  The byte offset
46752 ** to the first freeblock is given in the header.  Freeblocks occur in
46753 ** increasing order.  Because a freeblock must be at least 4 bytes in size,
46754 ** any group of 3 or fewer unused bytes in the cell content area cannot
46755 ** exist on the freeblock chain.  A group of 3 or fewer free bytes is called
46756 ** a fragment.  The total number of bytes in all fragments is recorded.
46757 ** in the page header at offset 7.
46758 **
46759 **    SIZE    DESCRIPTION
46760 **      2     Byte offset of the next freeblock
46761 **      2     Bytes in this freeblock
46762 **
46763 ** Cells are of variable length.  Cells are stored in the cell content area at
46764 ** the end of the page.  Pointers to the cells are in the cell pointer array
46765 ** that immediately follows the page header.  Cells is not necessarily
46766 ** contiguous or in order, but cell pointers are contiguous and in order.
46767 **
46768 ** Cell content makes use of variable length integers.  A variable
46769 ** length integer is 1 to 9 bytes where the lower 7 bits of each
46770 ** byte are used.  The integer consists of all bytes that have bit 8 set and
46771 ** the first byte with bit 8 clear.  The most significant byte of the integer
46772 ** appears first.  A variable-length integer may not be more than 9 bytes long.
46773 ** As a special case, all 8 bytes of the 9th byte are used as data.  This
46774 ** allows a 64-bit integer to be encoded in 9 bytes.
46775 **
46776 **    0x00                      becomes  0x00000000
46777 **    0x7f                      becomes  0x0000007f
46778 **    0x81 0x00                 becomes  0x00000080
46779 **    0x82 0x00                 becomes  0x00000100
46780 **    0x80 0x7f                 becomes  0x0000007f
46781 **    0x8a 0x91 0xd1 0xac 0x78  becomes  0x12345678
46782 **    0x81 0x81 0x81 0x81 0x01  becomes  0x10204081
46783 **
46784 ** Variable length integers are used for rowids and to hold the number of
46785 ** bytes of key and data in a btree cell.
46786 **
46787 ** The content of a cell looks like this:
46788 **
46789 **    SIZE    DESCRIPTION
46790 **      4     Page number of the left child. Omitted if leaf flag is set.
46791 **     var    Number of bytes of data. Omitted if the zerodata flag is set.
46792 **     var    Number of bytes of key. Or the key itself if intkey flag is set.
46793 **      *     Payload
46794 **      4     First page of the overflow chain.  Omitted if no overflow
46795 **
46796 ** Overflow pages form a linked list.  Each page except the last is completely
46797 ** filled with data (pagesize - 4 bytes).  The last page can have as little
46798 ** as 1 byte of data.
46799 **
46800 **    SIZE    DESCRIPTION
46801 **      4     Page number of next overflow page
46802 **      *     Data
46803 **
46804 ** Freelist pages come in two subtypes: trunk pages and leaf pages.  The
46805 ** file header points to the first in a linked list of trunk page.  Each trunk
46806 ** page points to multiple leaf pages.  The content of a leaf page is
46807 ** unspecified.  A trunk page looks like this:
46808 **
46809 **    SIZE    DESCRIPTION
46810 **      4     Page number of next trunk page
46811 **      4     Number of leaf pointers on this page
46812 **      *     zero or more pages numbers of leaves
46813 */
46814 
46815 
46816 /* The following value is the maximum cell size assuming a maximum page
46817 ** size give above.
46818 */
46819 #define MX_CELL_SIZE(pBt)  ((int)(pBt->pageSize-8))
46820 
46821 /* The maximum number of cells on a single page of the database.  This
46822 ** assumes a minimum cell size of 6 bytes  (4 bytes for the cell itself
46823 ** plus 2 bytes for the index to the cell in the page header).  Such
46824 ** small cells will be rare, but they are possible.
46825 */
46826 #define MX_CELL(pBt) ((pBt->pageSize-8)/6)
46827 
46828 /* Forward declarations */
46829 typedef struct MemPage MemPage;
46830 typedef struct BtLock BtLock;
46831 
46832 /*
46833 ** This is a magic string that appears at the beginning of every
46834 ** SQLite database in order to identify the file as a real database.
46835 **
46836 ** You can change this value at compile-time by specifying a
46837 ** -DSQLITE_FILE_HEADER="..." on the compiler command-line.  The
46838 ** header must be exactly 16 bytes including the zero-terminator so
46839 ** the string itself should be 15 characters long.  If you change
46840 ** the header, then your custom library will not be able to read
46841 ** databases generated by the standard tools and the standard tools
46842 ** will not be able to read databases created by your custom library.
46843 */
46844 #ifndef SQLITE_FILE_HEADER /* 123456789 123456 */
46845 #  define SQLITE_FILE_HEADER "SQLite format 3"
46846 #endif
46847 
46848 /*
46849 ** Page type flags.  An ORed combination of these flags appear as the
46850 ** first byte of on-disk image of every BTree page.
46851 */
46852 #define PTF_INTKEY    0x01
46853 #define PTF_ZERODATA  0x02
46854 #define PTF_LEAFDATA  0x04
46855 #define PTF_LEAF      0x08
46856 
46857 /*
46858 ** As each page of the file is loaded into memory, an instance of the following
46859 ** structure is appended and initialized to zero.  This structure stores
46860 ** information about the page that is decoded from the raw file page.
46861 **
46862 ** The pParent field points back to the parent page.  This allows us to
46863 ** walk up the BTree from any leaf to the root.  Care must be taken to
46864 ** unref() the parent page pointer when this page is no longer referenced.
46865 ** The pageDestructor() routine handles that chore.
46866 **
46867 ** Access to all fields of this structure is controlled by the mutex
46868 ** stored in MemPage.pBt->mutex.
46869 */
46870 struct MemPage {
46871   u8 isInit;           /* True if previously initialized. MUST BE FIRST! */
46872   u8 nOverflow;        /* Number of overflow cell bodies in aCell[] */
46873   u8 intKey;           /* True if intkey flag is set */
46874   u8 leaf;             /* True if leaf flag is set */
46875   u8 hasData;          /* True if this page stores data */
46876   u8 hdrOffset;        /* 100 for page 1.  0 otherwise */
46877   u8 childPtrSize;     /* 0 if leaf==1.  4 if leaf==0 */
46878   u16 maxLocal;        /* Copy of BtShared.maxLocal or BtShared.maxLeaf */
46879   u16 minLocal;        /* Copy of BtShared.minLocal or BtShared.minLeaf */
46880   u16 cellOffset;      /* Index in aData of first cell pointer */
46881   u16 nFree;           /* Number of free bytes on the page */
46882   u16 nCell;           /* Number of cells on this page, local and ovfl */
46883   u16 maskPage;        /* Mask for page offset */
46884   struct _OvflCell {   /* Cells that will not fit on aData[] */
46885     u8 *pCell;          /* Pointers to the body of the overflow cell */
46886     u16 idx;            /* Insert this cell before idx-th non-overflow cell */
46887   } aOvfl[5];
46888   BtShared *pBt;       /* Pointer to BtShared that this page is part of */
46889   u8 *aData;           /* Pointer to disk image of the page data */
46890   DbPage *pDbPage;     /* Pager page handle */
46891   Pgno pgno;           /* Page number for this page */
46892 };
46893 
46894 /*
46895 ** The in-memory image of a disk page has the auxiliary information appended
46896 ** to the end.  EXTRA_SIZE is the number of bytes of space needed to hold
46897 ** that extra information.
46898 */
46899 #define EXTRA_SIZE sizeof(MemPage)
46900 
46901 /*
46902 ** A linked list of the following structures is stored at BtShared.pLock.
46903 ** Locks are added (or upgraded from READ_LOCK to WRITE_LOCK) when a cursor
46904 ** is opened on the table with root page BtShared.iTable. Locks are removed
46905 ** from this list when a transaction is committed or rolled back, or when
46906 ** a btree handle is closed.
46907 */
46908 struct BtLock {
46909   Btree *pBtree;        /* Btree handle holding this lock */
46910   Pgno iTable;          /* Root page of table */
46911   u8 eLock;             /* READ_LOCK or WRITE_LOCK */
46912   BtLock *pNext;        /* Next in BtShared.pLock list */
46913 };
46914 
46915 /* Candidate values for BtLock.eLock */
46916 #define READ_LOCK     1
46917 #define WRITE_LOCK    2
46918 
46919 /* A Btree handle
46920 **
46921 ** A database connection contains a pointer to an instance of
46922 ** this object for every database file that it has open.  This structure
46923 ** is opaque to the database connection.  The database connection cannot
46924 ** see the internals of this structure and only deals with pointers to
46925 ** this structure.
46926 **
46927 ** For some database files, the same underlying database cache might be
46928 ** shared between multiple connections.  In that case, each connection
46929 ** has it own instance of this object.  But each instance of this object
46930 ** points to the same BtShared object.  The database cache and the
46931 ** schema associated with the database file are all contained within
46932 ** the BtShared object.
46933 **
46934 ** All fields in this structure are accessed under sqlite3.mutex.
46935 ** The pBt pointer itself may not be changed while there exists cursors
46936 ** in the referenced BtShared that point back to this Btree since those
46937 ** cursors have to go through this Btree to find their BtShared and
46938 ** they often do so without holding sqlite3.mutex.
46939 */
46940 struct Btree {
46941   sqlite3 *db;       /* The database connection holding this btree */
46942   BtShared *pBt;     /* Sharable content of this btree */
46943   u8 inTrans;        /* TRANS_NONE, TRANS_READ or TRANS_WRITE */
46944   u8 sharable;       /* True if we can share pBt with another db */
46945   u8 locked;         /* True if db currently has pBt locked */
46946   int wantToLock;    /* Number of nested calls to sqlite3BtreeEnter() */
46947   int nBackup;       /* Number of backup operations reading this btree */
46948   Btree *pNext;      /* List of other sharable Btrees from the same db */
46949   Btree *pPrev;      /* Back pointer of the same list */
46950 #ifndef SQLITE_OMIT_SHARED_CACHE
46951   BtLock lock;       /* Object used to lock page 1 */
46952 #endif
46953 };
46954 
46955 /*
46956 ** Btree.inTrans may take one of the following values.
46957 **
46958 ** If the shared-data extension is enabled, there may be multiple users
46959 ** of the Btree structure. At most one of these may open a write transaction,
46960 ** but any number may have active read transactions.
46961 */
46962 #define TRANS_NONE  0
46963 #define TRANS_READ  1
46964 #define TRANS_WRITE 2
46965 
46966 /*
46967 ** An instance of this object represents a single database file.
46968 **
46969 ** A single database file can be in use as the same time by two
46970 ** or more database connections.  When two or more connections are
46971 ** sharing the same database file, each connection has it own
46972 ** private Btree object for the file and each of those Btrees points
46973 ** to this one BtShared object.  BtShared.nRef is the number of
46974 ** connections currently sharing this database file.
46975 **
46976 ** Fields in this structure are accessed under the BtShared.mutex
46977 ** mutex, except for nRef and pNext which are accessed under the
46978 ** global SQLITE_MUTEX_STATIC_MASTER mutex.  The pPager field
46979 ** may not be modified once it is initially set as long as nRef>0.
46980 ** The pSchema field may be set once under BtShared.mutex and
46981 ** thereafter is unchanged as long as nRef>0.
46982 **
46983 ** isPending:
46984 **
46985 **   If a BtShared client fails to obtain a write-lock on a database
46986 **   table (because there exists one or more read-locks on the table),
46987 **   the shared-cache enters 'pending-lock' state and isPending is
46988 **   set to true.
46989 **
46990 **   The shared-cache leaves the 'pending lock' state when either of
46991 **   the following occur:
46992 **
46993 **     1) The current writer (BtShared.pWriter) concludes its transaction, OR
46994 **     2) The number of locks held by other connections drops to zero.
46995 **
46996 **   while in the 'pending-lock' state, no connection may start a new
46997 **   transaction.
46998 **
46999 **   This feature is included to help prevent writer-starvation.
47000 */
47001 struct BtShared {
47002   Pager *pPager;        /* The page cache */
47003   sqlite3 *db;          /* Database connection currently using this Btree */
47004   BtCursor *pCursor;    /* A list of all open cursors */
47005   MemPage *pPage1;      /* First page of the database */
47006   u8 readOnly;          /* True if the underlying file is readonly */
47007   u8 pageSizeFixed;     /* True if the page size can no longer be changed */
47008   u8 secureDelete;      /* True if secure_delete is enabled */
47009   u8 initiallyEmpty;    /* Database is empty at start of transaction */
47010   u8 openFlags;         /* Flags to sqlite3BtreeOpen() */
47011 #ifndef SQLITE_OMIT_AUTOVACUUM
47012   u8 autoVacuum;        /* True if auto-vacuum is enabled */
47013   u8 incrVacuum;        /* True if incr-vacuum is enabled */
47014 #endif
47015   u8 inTransaction;     /* Transaction state */
47016   u8 doNotUseWAL;       /* If true, do not open write-ahead-log file */
47017   u16 maxLocal;         /* Maximum local payload in non-LEAFDATA tables */
47018   u16 minLocal;         /* Minimum local payload in non-LEAFDATA tables */
47019   u16 maxLeaf;          /* Maximum local payload in a LEAFDATA table */
47020   u16 minLeaf;          /* Minimum local payload in a LEAFDATA table */
47021   u32 pageSize;         /* Total number of bytes on a page */
47022   u32 usableSize;       /* Number of usable bytes on each page */
47023   int nTransaction;     /* Number of open transactions (read + write) */
47024   u32 nPage;            /* Number of pages in the database */
47025   void *pSchema;        /* Pointer to space allocated by sqlite3BtreeSchema() */
47026   void (*xFreeSchema)(void*);  /* Destructor for BtShared.pSchema */
47027   sqlite3_mutex *mutex; /* Non-recursive mutex required to access this object */
47028   Bitvec *pHasContent;  /* Set of pages moved to free-list this transaction */
47029 #ifndef SQLITE_OMIT_SHARED_CACHE
47030   int nRef;             /* Number of references to this structure */
47031   BtShared *pNext;      /* Next on a list of sharable BtShared structs */
47032   BtLock *pLock;        /* List of locks held on this shared-btree struct */
47033   Btree *pWriter;       /* Btree with currently open write transaction */
47034   u8 isExclusive;       /* True if pWriter has an EXCLUSIVE lock on the db */
47035   u8 isPending;         /* If waiting for read-locks to clear */
47036 #endif
47037   u8 *pTmpSpace;        /* BtShared.pageSize bytes of space for tmp use */
47038 };
47039 
47040 /*
47041 ** An instance of the following structure is used to hold information
47042 ** about a cell.  The parseCellPtr() function fills in this structure
47043 ** based on information extract from the raw disk page.
47044 */
47045 typedef struct CellInfo CellInfo;
47046 struct CellInfo {
47047   i64 nKey;      /* The key for INTKEY tables, or number of bytes in key */
47048   u8 *pCell;     /* Pointer to the start of cell content */
47049   u32 nData;     /* Number of bytes of data */
47050   u32 nPayload;  /* Total amount of payload */
47051   u16 nHeader;   /* Size of the cell content header in bytes */
47052   u16 nLocal;    /* Amount of payload held locally */
47053   u16 iOverflow; /* Offset to overflow page number.  Zero if no overflow */
47054   u16 nSize;     /* Size of the cell content on the main b-tree page */
47055 };
47056 
47057 /*
47058 ** Maximum depth of an SQLite B-Tree structure. Any B-Tree deeper than
47059 ** this will be declared corrupt. This value is calculated based on a
47060 ** maximum database size of 2^31 pages a minimum fanout of 2 for a
47061 ** root-node and 3 for all other internal nodes.
47062 **
47063 ** If a tree that appears to be taller than this is encountered, it is
47064 ** assumed that the database is corrupt.
47065 */
47066 #define BTCURSOR_MAX_DEPTH 20
47067 
47068 /*
47069 ** A cursor is a pointer to a particular entry within a particular
47070 ** b-tree within a database file.
47071 **
47072 ** The entry is identified by its MemPage and the index in
47073 ** MemPage.aCell[] of the entry.
47074 **
47075 ** A single database file can shared by two more database connections,
47076 ** but cursors cannot be shared.  Each cursor is associated with a
47077 ** particular database connection identified BtCursor.pBtree.db.
47078 **
47079 ** Fields in this structure are accessed under the BtShared.mutex
47080 ** found at self->pBt->mutex.
47081 */
47082 struct BtCursor {
47083   Btree *pBtree;            /* The Btree to which this cursor belongs */
47084   BtShared *pBt;            /* The BtShared this cursor points to */
47085   BtCursor *pNext, *pPrev;  /* Forms a linked list of all cursors */
47086   struct KeyInfo *pKeyInfo; /* Argument passed to comparison function */
47087   Pgno pgnoRoot;            /* The root page of this tree */
47088   sqlite3_int64 cachedRowid; /* Next rowid cache.  0 means not valid */
47089   CellInfo info;            /* A parse of the cell we are pointing at */
47090   i64 nKey;        /* Size of pKey, or last integer key */
47091   void *pKey;      /* Saved key that was cursor's last known position */
47092   int skipNext;    /* Prev() is noop if negative. Next() is noop if positive */
47093   u8 wrFlag;                /* True if writable */
47094   u8 atLast;                /* Cursor pointing to the last entry */
47095   u8 validNKey;             /* True if info.nKey is valid */
47096   u8 eState;                /* One of the CURSOR_XXX constants (see below) */
47097 #ifndef SQLITE_OMIT_INCRBLOB
47098   Pgno *aOverflow;          /* Cache of overflow page locations */
47099   u8 isIncrblobHandle;      /* True if this cursor is an incr. io handle */
47100 #endif
47101   i16 iPage;                            /* Index of current page in apPage */
47102   u16 aiIdx[BTCURSOR_MAX_DEPTH];        /* Current index in apPage[i] */
47103   MemPage *apPage[BTCURSOR_MAX_DEPTH];  /* Pages from root to current page */
47104 };
47105 
47106 /*
47107 ** Potential values for BtCursor.eState.
47108 **
47109 ** CURSOR_VALID:
47110 **   Cursor points to a valid entry. getPayload() etc. may be called.
47111 **
47112 ** CURSOR_INVALID:
47113 **   Cursor does not point to a valid entry. This can happen (for example)
47114 **   because the table is empty or because BtreeCursorFirst() has not been
47115 **   called.
47116 **
47117 ** CURSOR_REQUIRESEEK:
47118 **   The table that this cursor was opened on still exists, but has been
47119 **   modified since the cursor was last used. The cursor position is saved
47120 **   in variables BtCursor.pKey and BtCursor.nKey. When a cursor is in
47121 **   this state, restoreCursorPosition() can be called to attempt to
47122 **   seek the cursor to the saved position.
47123 **
47124 ** CURSOR_FAULT:
47125 **   A unrecoverable error (an I/O error or a malloc failure) has occurred
47126 **   on a different connection that shares the BtShared cache with this
47127 **   cursor.  The error has left the cache in an inconsistent state.
47128 **   Do nothing else with this cursor.  Any attempt to use the cursor
47129 **   should return the error code stored in BtCursor.skip
47130 */
47131 #define CURSOR_INVALID           0
47132 #define CURSOR_VALID             1
47133 #define CURSOR_REQUIRESEEK       2
47134 #define CURSOR_FAULT             3
47135 
47136 /*
47137 ** The database page the PENDING_BYTE occupies. This page is never used.
47138 */
47139 # define PENDING_BYTE_PAGE(pBt) PAGER_MJ_PGNO(pBt)
47140 
47141 /*
47142 ** These macros define the location of the pointer-map entry for a
47143 ** database page. The first argument to each is the number of usable
47144 ** bytes on each page of the database (often 1024). The second is the
47145 ** page number to look up in the pointer map.
47146 **
47147 ** PTRMAP_PAGENO returns the database page number of the pointer-map
47148 ** page that stores the required pointer. PTRMAP_PTROFFSET returns
47149 ** the offset of the requested map entry.
47150 **
47151 ** If the pgno argument passed to PTRMAP_PAGENO is a pointer-map page,
47152 ** then pgno is returned. So (pgno==PTRMAP_PAGENO(pgsz, pgno)) can be
47153 ** used to test if pgno is a pointer-map page. PTRMAP_ISPAGE implements
47154 ** this test.
47155 */
47156 #define PTRMAP_PAGENO(pBt, pgno) ptrmapPageno(pBt, pgno)
47157 #define PTRMAP_PTROFFSET(pgptrmap, pgno) (5*(pgno-pgptrmap-1))
47158 #define PTRMAP_ISPAGE(pBt, pgno) (PTRMAP_PAGENO((pBt),(pgno))==(pgno))
47159 
47160 /*
47161 ** The pointer map is a lookup table that identifies the parent page for
47162 ** each child page in the database file.  The parent page is the page that
47163 ** contains a pointer to the child.  Every page in the database contains
47164 ** 0 or 1 parent pages.  (In this context 'database page' refers
47165 ** to any page that is not part of the pointer map itself.)  Each pointer map
47166 ** entry consists of a single byte 'type' and a 4 byte parent page number.
47167 ** The PTRMAP_XXX identifiers below are the valid types.
47168 **
47169 ** The purpose of the pointer map is to facility moving pages from one
47170 ** position in the file to another as part of autovacuum.  When a page
47171 ** is moved, the pointer in its parent must be updated to point to the
47172 ** new location.  The pointer map is used to locate the parent page quickly.
47173 **
47174 ** PTRMAP_ROOTPAGE: The database page is a root-page. The page-number is not
47175 **                  used in this case.
47176 **
47177 ** PTRMAP_FREEPAGE: The database page is an unused (free) page. The page-number
47178 **                  is not used in this case.
47179 **
47180 ** PTRMAP_OVERFLOW1: The database page is the first page in a list of
47181 **                   overflow pages. The page number identifies the page that
47182 **                   contains the cell with a pointer to this overflow page.
47183 **
47184 ** PTRMAP_OVERFLOW2: The database page is the second or later page in a list of
47185 **                   overflow pages. The page-number identifies the previous
47186 **                   page in the overflow page list.
47187 **
47188 ** PTRMAP_BTREE: The database page is a non-root btree page. The page number
47189 **               identifies the parent page in the btree.
47190 */
47191 #define PTRMAP_ROOTPAGE 1
47192 #define PTRMAP_FREEPAGE 2
47193 #define PTRMAP_OVERFLOW1 3
47194 #define PTRMAP_OVERFLOW2 4
47195 #define PTRMAP_BTREE 5
47196 
47197 /* A bunch of assert() statements to check the transaction state variables
47198 ** of handle p (type Btree*) are internally consistent.
47199 */
47200 #define btreeIntegrity(p) \
47201   assert( p->pBt->inTransaction!=TRANS_NONE || p->pBt->nTransaction==0 ); \
47202   assert( p->pBt->inTransaction>=p->inTrans );
47203 
47204 
47205 /*
47206 ** The ISAUTOVACUUM macro is used within balance_nonroot() to determine
47207 ** if the database supports auto-vacuum or not. Because it is used
47208 ** within an expression that is an argument to another macro
47209 ** (sqliteMallocRaw), it is not possible to use conditional compilation.
47210 ** So, this macro is defined instead.
47211 */
47212 #ifndef SQLITE_OMIT_AUTOVACUUM
47213 #define ISAUTOVACUUM (pBt->autoVacuum)
47214 #else
47215 #define ISAUTOVACUUM 0
47216 #endif
47217 
47218 
47219 /*
47220 ** This structure is passed around through all the sanity checking routines
47221 ** in order to keep track of some global state information.
47222 */
47223 typedef struct IntegrityCk IntegrityCk;
47224 struct IntegrityCk {
47225   BtShared *pBt;    /* The tree being checked out */
47226   Pager *pPager;    /* The associated pager.  Also accessible by pBt->pPager */
47227   Pgno nPage;       /* Number of pages in the database */
47228   int *anRef;       /* Number of times each page is referenced */
47229   int mxErr;        /* Stop accumulating errors when this reaches zero */
47230   int nErr;         /* Number of messages written to zErrMsg so far */
47231   int mallocFailed; /* A memory allocation error has occurred */
47232   StrAccum errMsg;  /* Accumulate the error message text here */
47233 };
47234 
47235 /*
47236 ** Read or write a two- and four-byte big-endian integer values.
47237 */
47238 #define get2byte(x)   ((x)[0]<<8 | (x)[1])
47239 #define put2byte(p,v) ((p)[0] = (u8)((v)>>8), (p)[1] = (u8)(v))
47240 #define get4byte sqlite3Get4byte
47241 #define put4byte sqlite3Put4byte
47242 
47243 /************** End of btreeInt.h ********************************************/
47244 /************** Continuing where we left off in btmutex.c ********************/
47245 #ifndef SQLITE_OMIT_SHARED_CACHE
47246 #if SQLITE_THREADSAFE
47247 
47248 /*
47249 ** Obtain the BtShared mutex associated with B-Tree handle p. Also,
47250 ** set BtShared.db to the database handle associated with p and the
47251 ** p->locked boolean to true.
47252 */
47253 static void lockBtreeMutex(Btree *p){
47254   assert( p->locked==0 );
47255   assert( sqlite3_mutex_notheld(p->pBt->mutex) );
47256   assert( sqlite3_mutex_held(p->db->mutex) );
47257 
47258   sqlite3_mutex_enter(p->pBt->mutex);
47259   p->pBt->db = p->db;
47260   p->locked = 1;
47261 }
47262 
47263 /*
47264 ** Release the BtShared mutex associated with B-Tree handle p and
47265 ** clear the p->locked boolean.
47266 */
47267 static void unlockBtreeMutex(Btree *p){
47268   BtShared *pBt = p->pBt;
47269   assert( p->locked==1 );
47270   assert( sqlite3_mutex_held(pBt->mutex) );
47271   assert( sqlite3_mutex_held(p->db->mutex) );
47272   assert( p->db==pBt->db );
47273 
47274   sqlite3_mutex_leave(pBt->mutex);
47275   p->locked = 0;
47276 }
47277 
47278 /*
47279 ** Enter a mutex on the given BTree object.
47280 **
47281 ** If the object is not sharable, then no mutex is ever required
47282 ** and this routine is a no-op.  The underlying mutex is non-recursive.
47283 ** But we keep a reference count in Btree.wantToLock so the behavior
47284 ** of this interface is recursive.
47285 **
47286 ** To avoid deadlocks, multiple Btrees are locked in the same order
47287 ** by all database connections.  The p->pNext is a list of other
47288 ** Btrees belonging to the same database connection as the p Btree
47289 ** which need to be locked after p.  If we cannot get a lock on
47290 ** p, then first unlock all of the others on p->pNext, then wait
47291 ** for the lock to become available on p, then relock all of the
47292 ** subsequent Btrees that desire a lock.
47293 */
47294 SQLITE_PRIVATE void sqlite3BtreeEnter(Btree *p){
47295   Btree *pLater;
47296 
47297   /* Some basic sanity checking on the Btree.  The list of Btrees
47298   ** connected by pNext and pPrev should be in sorted order by
47299   ** Btree.pBt value. All elements of the list should belong to
47300   ** the same connection. Only shared Btrees are on the list. */
47301   assert( p->pNext==0 || p->pNext->pBt>p->pBt );
47302   assert( p->pPrev==0 || p->pPrev->pBt<p->pBt );
47303   assert( p->pNext==0 || p->pNext->db==p->db );
47304   assert( p->pPrev==0 || p->pPrev->db==p->db );
47305   assert( p->sharable || (p->pNext==0 && p->pPrev==0) );
47306 
47307   /* Check for locking consistency */
47308   assert( !p->locked || p->wantToLock>0 );
47309   assert( p->sharable || p->wantToLock==0 );
47310 
47311   /* We should already hold a lock on the database connection */
47312   assert( sqlite3_mutex_held(p->db->mutex) );
47313 
47314   /* Unless the database is sharable and unlocked, then BtShared.db
47315   ** should already be set correctly. */
47316   assert( (p->locked==0 && p->sharable) || p->pBt->db==p->db );
47317 
47318   if( !p->sharable ) return;
47319   p->wantToLock++;
47320   if( p->locked ) return;
47321 
47322   /* In most cases, we should be able to acquire the lock we
47323   ** want without having to go throught the ascending lock
47324   ** procedure that follows.  Just be sure not to block.
47325   */
47326   if( sqlite3_mutex_try(p->pBt->mutex)==SQLITE_OK ){
47327     p->pBt->db = p->db;
47328     p->locked = 1;
47329     return;
47330   }
47331 
47332   /* To avoid deadlock, first release all locks with a larger
47333   ** BtShared address.  Then acquire our lock.  Then reacquire
47334   ** the other BtShared locks that we used to hold in ascending
47335   ** order.
47336   */
47337   for(pLater=p->pNext; pLater; pLater=pLater->pNext){
47338     assert( pLater->sharable );
47339     assert( pLater->pNext==0 || pLater->pNext->pBt>pLater->pBt );
47340     assert( !pLater->locked || pLater->wantToLock>0 );
47341     if( pLater->locked ){
47342       unlockBtreeMutex(pLater);
47343     }
47344   }
47345   lockBtreeMutex(p);
47346   for(pLater=p->pNext; pLater; pLater=pLater->pNext){
47347     if( pLater->wantToLock ){
47348       lockBtreeMutex(pLater);
47349     }
47350   }
47351 }
47352 
47353 /*
47354 ** Exit the recursive mutex on a Btree.
47355 */
47356 SQLITE_PRIVATE void sqlite3BtreeLeave(Btree *p){
47357   if( p->sharable ){
47358     assert( p->wantToLock>0 );
47359     p->wantToLock--;
47360     if( p->wantToLock==0 ){
47361       unlockBtreeMutex(p);
47362     }
47363   }
47364 }
47365 
47366 #ifndef NDEBUG
47367 /*
47368 ** Return true if the BtShared mutex is held on the btree, or if the
47369 ** B-Tree is not marked as sharable.
47370 **
47371 ** This routine is used only from within assert() statements.
47372 */
47373 SQLITE_PRIVATE int sqlite3BtreeHoldsMutex(Btree *p){
47374   assert( p->sharable==0 || p->locked==0 || p->wantToLock>0 );
47375   assert( p->sharable==0 || p->locked==0 || p->db==p->pBt->db );
47376   assert( p->sharable==0 || p->locked==0 || sqlite3_mutex_held(p->pBt->mutex) );
47377   assert( p->sharable==0 || p->locked==0 || sqlite3_mutex_held(p->db->mutex) );
47378 
47379   return (p->sharable==0 || p->locked);
47380 }
47381 #endif
47382 
47383 
47384 #ifndef SQLITE_OMIT_INCRBLOB
47385 /*
47386 ** Enter and leave a mutex on a Btree given a cursor owned by that
47387 ** Btree.  These entry points are used by incremental I/O and can be
47388 ** omitted if that module is not used.
47389 */
47390 SQLITE_PRIVATE void sqlite3BtreeEnterCursor(BtCursor *pCur){
47391   sqlite3BtreeEnter(pCur->pBtree);
47392 }
47393 SQLITE_PRIVATE void sqlite3BtreeLeaveCursor(BtCursor *pCur){
47394   sqlite3BtreeLeave(pCur->pBtree);
47395 }
47396 #endif /* SQLITE_OMIT_INCRBLOB */
47397 
47398 
47399 /*
47400 ** Enter the mutex on every Btree associated with a database
47401 ** connection.  This is needed (for example) prior to parsing
47402 ** a statement since we will be comparing table and column names
47403 ** against all schemas and we do not want those schemas being
47404 ** reset out from under us.
47405 **
47406 ** There is a corresponding leave-all procedures.
47407 **
47408 ** Enter the mutexes in accending order by BtShared pointer address
47409 ** to avoid the possibility of deadlock when two threads with
47410 ** two or more btrees in common both try to lock all their btrees
47411 ** at the same instant.
47412 */
47413 SQLITE_PRIVATE void sqlite3BtreeEnterAll(sqlite3 *db){
47414   int i;
47415   Btree *p;
47416   assert( sqlite3_mutex_held(db->mutex) );
47417   for(i=0; i<db->nDb; i++){
47418     p = db->aDb[i].pBt;
47419     if( p ) sqlite3BtreeEnter(p);
47420   }
47421 }
47422 SQLITE_PRIVATE void sqlite3BtreeLeaveAll(sqlite3 *db){
47423   int i;
47424   Btree *p;
47425   assert( sqlite3_mutex_held(db->mutex) );
47426   for(i=0; i<db->nDb; i++){
47427     p = db->aDb[i].pBt;
47428     if( p ) sqlite3BtreeLeave(p);
47429   }
47430 }
47431 
47432 /*
47433 ** Return true if a particular Btree requires a lock.  Return FALSE if
47434 ** no lock is ever required since it is not sharable.
47435 */
47436 SQLITE_PRIVATE int sqlite3BtreeSharable(Btree *p){
47437   return p->sharable;
47438 }
47439 
47440 #ifndef NDEBUG
47441 /*
47442 ** Return true if the current thread holds the database connection
47443 ** mutex and all required BtShared mutexes.
47444 **
47445 ** This routine is used inside assert() statements only.
47446 */
47447 SQLITE_PRIVATE int sqlite3BtreeHoldsAllMutexes(sqlite3 *db){
47448   int i;
47449   if( !sqlite3_mutex_held(db->mutex) ){
47450     return 0;
47451   }
47452   for(i=0; i<db->nDb; i++){
47453     Btree *p;
47454     p = db->aDb[i].pBt;
47455     if( p && p->sharable &&
47456          (p->wantToLock==0 || !sqlite3_mutex_held(p->pBt->mutex)) ){
47457       return 0;
47458     }
47459   }
47460   return 1;
47461 }
47462 #endif /* NDEBUG */
47463 
47464 #ifndef NDEBUG
47465 /*
47466 ** Return true if the correct mutexes are held for accessing the
47467 ** db->aDb[iDb].pSchema structure.  The mutexes required for schema
47468 ** access are:
47469 **
47470 **   (1) The mutex on db
47471 **   (2) if iDb!=1, then the mutex on db->aDb[iDb].pBt.
47472 **
47473 ** If pSchema is not NULL, then iDb is computed from pSchema and
47474 ** db using sqlite3SchemaToIndex().
47475 */
47476 SQLITE_PRIVATE int sqlite3SchemaMutexHeld(sqlite3 *db, int iDb, Schema *pSchema){
47477   Btree *p;
47478   assert( db!=0 );
47479   if( pSchema ) iDb = sqlite3SchemaToIndex(db, pSchema);
47480   assert( iDb>=0 && iDb<db->nDb );
47481   if( !sqlite3_mutex_held(db->mutex) ) return 0;
47482   if( iDb==1 ) return 1;
47483   p = db->aDb[iDb].pBt;
47484   assert( p!=0 );
47485   return p->sharable==0 || p->locked==1;
47486 }
47487 #endif /* NDEBUG */
47488 
47489 #else /* SQLITE_THREADSAFE>0 above.  SQLITE_THREADSAFE==0 below */
47490 /*
47491 ** The following are special cases for mutex enter routines for use
47492 ** in single threaded applications that use shared cache.  Except for
47493 ** these two routines, all mutex operations are no-ops in that case and
47494 ** are null #defines in btree.h.
47495 **
47496 ** If shared cache is disabled, then all btree mutex routines, including
47497 ** the ones below, are no-ops and are null #defines in btree.h.
47498 */
47499 
47500 SQLITE_PRIVATE void sqlite3BtreeEnter(Btree *p){
47501   p->pBt->db = p->db;
47502 }
47503 SQLITE_PRIVATE void sqlite3BtreeEnterAll(sqlite3 *db){
47504   int i;
47505   for(i=0; i<db->nDb; i++){
47506     Btree *p = db->aDb[i].pBt;
47507     if( p ){
47508       p->pBt->db = p->db;
47509     }
47510   }
47511 }
47512 #endif /* if SQLITE_THREADSAFE */
47513 #endif /* ifndef SQLITE_OMIT_SHARED_CACHE */
47514 
47515 /************** End of btmutex.c *********************************************/
47516 /************** Begin file btree.c *******************************************/
47517 /*
47518 ** 2004 April 6
47519 **
47520 ** The author disclaims copyright to this source code.  In place of
47521 ** a legal notice, here is a blessing:
47522 **
47523 **    May you do good and not evil.
47524 **    May you find forgiveness for yourself and forgive others.
47525 **    May you share freely, never taking more than you give.
47526 **
47527 *************************************************************************
47528 ** This file implements a external (disk-based) database using BTrees.
47529 ** See the header comment on "btreeInt.h" for additional information.
47530 ** Including a description of file format and an overview of operation.
47531 */
47532 
47533 /*
47534 ** The header string that appears at the beginning of every
47535 ** SQLite database.
47536 */
47537 static const char zMagicHeader[] = SQLITE_FILE_HEADER;
47538 
47539 /*
47540 ** Set this global variable to 1 to enable tracing using the TRACE
47541 ** macro.
47542 */
47543 #if 0
47544 int sqlite3BtreeTrace=1;  /* True to enable tracing */
47545 # define TRACE(X)  if(sqlite3BtreeTrace){printf X;fflush(stdout);}
47546 #else
47547 # define TRACE(X)
47548 #endif
47549 
47550 /*
47551 ** Extract a 2-byte big-endian integer from an array of unsigned bytes.
47552 ** But if the value is zero, make it 65536.
47553 **
47554 ** This routine is used to extract the "offset to cell content area" value
47555 ** from the header of a btree page.  If the page size is 65536 and the page
47556 ** is empty, the offset should be 65536, but the 2-byte value stores zero.
47557 ** This routine makes the necessary adjustment to 65536.
47558 */
47559 #define get2byteNotZero(X)  (((((int)get2byte(X))-1)&0xffff)+1)
47560 
47561 #ifndef SQLITE_OMIT_SHARED_CACHE
47562 /*
47563 ** A list of BtShared objects that are eligible for participation
47564 ** in shared cache.  This variable has file scope during normal builds,
47565 ** but the test harness needs to access it so we make it global for
47566 ** test builds.
47567 **
47568 ** Access to this variable is protected by SQLITE_MUTEX_STATIC_MASTER.
47569 */
47570 #ifdef SQLITE_TEST
47571 SQLITE_PRIVATE BtShared *SQLITE_WSD sqlite3SharedCacheList = 0;
47572 #else
47573 static BtShared *SQLITE_WSD sqlite3SharedCacheList = 0;
47574 #endif
47575 #endif /* SQLITE_OMIT_SHARED_CACHE */
47576 
47577 #ifndef SQLITE_OMIT_SHARED_CACHE
47578 /*
47579 ** Enable or disable the shared pager and schema features.
47580 **
47581 ** This routine has no effect on existing database connections.
47582 ** The shared cache setting effects only future calls to
47583 ** sqlite3_open(), sqlite3_open16(), or sqlite3_open_v2().
47584 */
47585 SQLITE_API int sqlite3_enable_shared_cache(int enable){
47586   sqlite3GlobalConfig.sharedCacheEnabled = enable;
47587   return SQLITE_OK;
47588 }
47589 #endif
47590 
47591 
47592 
47593 #ifdef SQLITE_OMIT_SHARED_CACHE
47594   /*
47595   ** The functions querySharedCacheTableLock(), setSharedCacheTableLock(),
47596   ** and clearAllSharedCacheTableLocks()
47597   ** manipulate entries in the BtShared.pLock linked list used to store
47598   ** shared-cache table level locks. If the library is compiled with the
47599   ** shared-cache feature disabled, then there is only ever one user
47600   ** of each BtShared structure and so this locking is not necessary.
47601   ** So define the lock related functions as no-ops.
47602   */
47603   #define querySharedCacheTableLock(a,b,c) SQLITE_OK
47604   #define setSharedCacheTableLock(a,b,c) SQLITE_OK
47605   #define clearAllSharedCacheTableLocks(a)
47606   #define downgradeAllSharedCacheTableLocks(a)
47607   #define hasSharedCacheTableLock(a,b,c,d) 1
47608   #define hasReadConflicts(a, b) 0
47609 #endif
47610 
47611 #ifndef SQLITE_OMIT_SHARED_CACHE
47612 
47613 #ifdef SQLITE_DEBUG
47614 /*
47615 **** This function is only used as part of an assert() statement. ***
47616 **
47617 ** Check to see if pBtree holds the required locks to read or write to the
47618 ** table with root page iRoot.   Return 1 if it does and 0 if not.
47619 **
47620 ** For example, when writing to a table with root-page iRoot via
47621 ** Btree connection pBtree:
47622 **
47623 **    assert( hasSharedCacheTableLock(pBtree, iRoot, 0, WRITE_LOCK) );
47624 **
47625 ** When writing to an index that resides in a sharable database, the
47626 ** caller should have first obtained a lock specifying the root page of
47627 ** the corresponding table. This makes things a bit more complicated,
47628 ** as this module treats each table as a separate structure. To determine
47629 ** the table corresponding to the index being written, this
47630 ** function has to search through the database schema.
47631 **
47632 ** Instead of a lock on the table/index rooted at page iRoot, the caller may
47633 ** hold a write-lock on the schema table (root page 1). This is also
47634 ** acceptable.
47635 */
47636 static int hasSharedCacheTableLock(
47637   Btree *pBtree,         /* Handle that must hold lock */
47638   Pgno iRoot,            /* Root page of b-tree */
47639   int isIndex,           /* True if iRoot is the root of an index b-tree */
47640   int eLockType          /* Required lock type (READ_LOCK or WRITE_LOCK) */
47641 ){
47642   Schema *pSchema = (Schema *)pBtree->pBt->pSchema;
47643   Pgno iTab = 0;
47644   BtLock *pLock;
47645 
47646   /* If this database is not shareable, or if the client is reading
47647   ** and has the read-uncommitted flag set, then no lock is required.
47648   ** Return true immediately.
47649   */
47650   if( (pBtree->sharable==0)
47651    || (eLockType==READ_LOCK && (pBtree->db->flags & SQLITE_ReadUncommitted))
47652   ){
47653     return 1;
47654   }
47655 
47656   /* If the client is reading  or writing an index and the schema is
47657   ** not loaded, then it is too difficult to actually check to see if
47658   ** the correct locks are held.  So do not bother - just return true.
47659   ** This case does not come up very often anyhow.
47660   */
47661   if( isIndex && (!pSchema || (pSchema->flags&DB_SchemaLoaded)==0) ){
47662     return 1;
47663   }
47664 
47665   /* Figure out the root-page that the lock should be held on. For table
47666   ** b-trees, this is just the root page of the b-tree being read or
47667   ** written. For index b-trees, it is the root page of the associated
47668   ** table.  */
47669   if( isIndex ){
47670     HashElem *p;
47671     for(p=sqliteHashFirst(&pSchema->idxHash); p; p=sqliteHashNext(p)){
47672       Index *pIdx = (Index *)sqliteHashData(p);
47673       if( pIdx->tnum==(int)iRoot ){
47674         iTab = pIdx->pTable->tnum;
47675       }
47676     }
47677   }else{
47678     iTab = iRoot;
47679   }
47680 
47681   /* Search for the required lock. Either a write-lock on root-page iTab, a
47682   ** write-lock on the schema table, or (if the client is reading) a
47683   ** read-lock on iTab will suffice. Return 1 if any of these are found.  */
47684   for(pLock=pBtree->pBt->pLock; pLock; pLock=pLock->pNext){
47685     if( pLock->pBtree==pBtree
47686      && (pLock->iTable==iTab || (pLock->eLock==WRITE_LOCK && pLock->iTable==1))
47687      && pLock->eLock>=eLockType
47688     ){
47689       return 1;
47690     }
47691   }
47692 
47693   /* Failed to find the required lock. */
47694   return 0;
47695 }
47696 #endif /* SQLITE_DEBUG */
47697 
47698 #ifdef SQLITE_DEBUG
47699 /*
47700 **** This function may be used as part of assert() statements only. ****
47701 **
47702 ** Return true if it would be illegal for pBtree to write into the
47703 ** table or index rooted at iRoot because other shared connections are
47704 ** simultaneously reading that same table or index.
47705 **
47706 ** It is illegal for pBtree to write if some other Btree object that
47707 ** shares the same BtShared object is currently reading or writing
47708 ** the iRoot table.  Except, if the other Btree object has the
47709 ** read-uncommitted flag set, then it is OK for the other object to
47710 ** have a read cursor.
47711 **
47712 ** For example, before writing to any part of the table or index
47713 ** rooted at page iRoot, one should call:
47714 **
47715 **    assert( !hasReadConflicts(pBtree, iRoot) );
47716 */
47717 static int hasReadConflicts(Btree *pBtree, Pgno iRoot){
47718   BtCursor *p;
47719   for(p=pBtree->pBt->pCursor; p; p=p->pNext){
47720     if( p->pgnoRoot==iRoot
47721      && p->pBtree!=pBtree
47722      && 0==(p->pBtree->db->flags & SQLITE_ReadUncommitted)
47723     ){
47724       return 1;
47725     }
47726   }
47727   return 0;
47728 }
47729 #endif    /* #ifdef SQLITE_DEBUG */
47730 
47731 /*
47732 ** Query to see if Btree handle p may obtain a lock of type eLock
47733 ** (READ_LOCK or WRITE_LOCK) on the table with root-page iTab. Return
47734 ** SQLITE_OK if the lock may be obtained (by calling
47735 ** setSharedCacheTableLock()), or SQLITE_LOCKED if not.
47736 */
47737 static int querySharedCacheTableLock(Btree *p, Pgno iTab, u8 eLock){
47738   BtShared *pBt = p->pBt;
47739   BtLock *pIter;
47740 
47741   assert( sqlite3BtreeHoldsMutex(p) );
47742   assert( eLock==READ_LOCK || eLock==WRITE_LOCK );
47743   assert( p->db!=0 );
47744   assert( !(p->db->flags&SQLITE_ReadUncommitted)||eLock==WRITE_LOCK||iTab==1 );
47745 
47746   /* If requesting a write-lock, then the Btree must have an open write
47747   ** transaction on this file. And, obviously, for this to be so there
47748   ** must be an open write transaction on the file itself.
47749   */
47750   assert( eLock==READ_LOCK || (p==pBt->pWriter && p->inTrans==TRANS_WRITE) );
47751   assert( eLock==READ_LOCK || pBt->inTransaction==TRANS_WRITE );
47752 
47753   /* This routine is a no-op if the shared-cache is not enabled */
47754   if( !p->sharable ){
47755     return SQLITE_OK;
47756   }
47757 
47758   /* If some other connection is holding an exclusive lock, the
47759   ** requested lock may not be obtained.
47760   */
47761   if( pBt->pWriter!=p && pBt->isExclusive ){
47762     sqlite3ConnectionBlocked(p->db, pBt->pWriter->db);
47763     return SQLITE_LOCKED_SHAREDCACHE;
47764   }
47765 
47766   for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
47767     /* The condition (pIter->eLock!=eLock) in the following if(...)
47768     ** statement is a simplification of:
47769     **
47770     **   (eLock==WRITE_LOCK || pIter->eLock==WRITE_LOCK)
47771     **
47772     ** since we know that if eLock==WRITE_LOCK, then no other connection
47773     ** may hold a WRITE_LOCK on any table in this file (since there can
47774     ** only be a single writer).
47775     */
47776     assert( pIter->eLock==READ_LOCK || pIter->eLock==WRITE_LOCK );
47777     assert( eLock==READ_LOCK || pIter->pBtree==p || pIter->eLock==READ_LOCK);
47778     if( pIter->pBtree!=p && pIter->iTable==iTab && pIter->eLock!=eLock ){
47779       sqlite3ConnectionBlocked(p->db, pIter->pBtree->db);
47780       if( eLock==WRITE_LOCK ){
47781         assert( p==pBt->pWriter );
47782         pBt->isPending = 1;
47783       }
47784       return SQLITE_LOCKED_SHAREDCACHE;
47785     }
47786   }
47787   return SQLITE_OK;
47788 }
47789 #endif /* !SQLITE_OMIT_SHARED_CACHE */
47790 
47791 #ifndef SQLITE_OMIT_SHARED_CACHE
47792 /*
47793 ** Add a lock on the table with root-page iTable to the shared-btree used
47794 ** by Btree handle p. Parameter eLock must be either READ_LOCK or
47795 ** WRITE_LOCK.
47796 **
47797 ** This function assumes the following:
47798 **
47799 **   (a) The specified Btree object p is connected to a sharable
47800 **       database (one with the BtShared.sharable flag set), and
47801 **
47802 **   (b) No other Btree objects hold a lock that conflicts
47803 **       with the requested lock (i.e. querySharedCacheTableLock() has
47804 **       already been called and returned SQLITE_OK).
47805 **
47806 ** SQLITE_OK is returned if the lock is added successfully. SQLITE_NOMEM
47807 ** is returned if a malloc attempt fails.
47808 */
47809 static int setSharedCacheTableLock(Btree *p, Pgno iTable, u8 eLock){
47810   BtShared *pBt = p->pBt;
47811   BtLock *pLock = 0;
47812   BtLock *pIter;
47813 
47814   assert( sqlite3BtreeHoldsMutex(p) );
47815   assert( eLock==READ_LOCK || eLock==WRITE_LOCK );
47816   assert( p->db!=0 );
47817 
47818   /* A connection with the read-uncommitted flag set will never try to
47819   ** obtain a read-lock using this function. The only read-lock obtained
47820   ** by a connection in read-uncommitted mode is on the sqlite_master
47821   ** table, and that lock is obtained in BtreeBeginTrans().  */
47822   assert( 0==(p->db->flags&SQLITE_ReadUncommitted) || eLock==WRITE_LOCK );
47823 
47824   /* This function should only be called on a sharable b-tree after it
47825   ** has been determined that no other b-tree holds a conflicting lock.  */
47826   assert( p->sharable );
47827   assert( SQLITE_OK==querySharedCacheTableLock(p, iTable, eLock) );
47828 
47829   /* First search the list for an existing lock on this table. */
47830   for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
47831     if( pIter->iTable==iTable && pIter->pBtree==p ){
47832       pLock = pIter;
47833       break;
47834     }
47835   }
47836 
47837   /* If the above search did not find a BtLock struct associating Btree p
47838   ** with table iTable, allocate one and link it into the list.
47839   */
47840   if( !pLock ){
47841     pLock = (BtLock *)sqlite3MallocZero(sizeof(BtLock));
47842     if( !pLock ){
47843       return SQLITE_NOMEM;
47844     }
47845     pLock->iTable = iTable;
47846     pLock->pBtree = p;
47847     pLock->pNext = pBt->pLock;
47848     pBt->pLock = pLock;
47849   }
47850 
47851   /* Set the BtLock.eLock variable to the maximum of the current lock
47852   ** and the requested lock. This means if a write-lock was already held
47853   ** and a read-lock requested, we don't incorrectly downgrade the lock.
47854   */
47855   assert( WRITE_LOCK>READ_LOCK );
47856   if( eLock>pLock->eLock ){
47857     pLock->eLock = eLock;
47858   }
47859 
47860   return SQLITE_OK;
47861 }
47862 #endif /* !SQLITE_OMIT_SHARED_CACHE */
47863 
47864 #ifndef SQLITE_OMIT_SHARED_CACHE
47865 /*
47866 ** Release all the table locks (locks obtained via calls to
47867 ** the setSharedCacheTableLock() procedure) held by Btree object p.
47868 **
47869 ** This function assumes that Btree p has an open read or write
47870 ** transaction. If it does not, then the BtShared.isPending variable
47871 ** may be incorrectly cleared.
47872 */
47873 static void clearAllSharedCacheTableLocks(Btree *p){
47874   BtShared *pBt = p->pBt;
47875   BtLock **ppIter = &pBt->pLock;
47876 
47877   assert( sqlite3BtreeHoldsMutex(p) );
47878   assert( p->sharable || 0==*ppIter );
47879   assert( p->inTrans>0 );
47880 
47881   while( *ppIter ){
47882     BtLock *pLock = *ppIter;
47883     assert( pBt->isExclusive==0 || pBt->pWriter==pLock->pBtree );
47884     assert( pLock->pBtree->inTrans>=pLock->eLock );
47885     if( pLock->pBtree==p ){
47886       *ppIter = pLock->pNext;
47887       assert( pLock->iTable!=1 || pLock==&p->lock );
47888       if( pLock->iTable!=1 ){
47889         sqlite3_free(pLock);
47890       }
47891     }else{
47892       ppIter = &pLock->pNext;
47893     }
47894   }
47895 
47896   assert( pBt->isPending==0 || pBt->pWriter );
47897   if( pBt->pWriter==p ){
47898     pBt->pWriter = 0;
47899     pBt->isExclusive = 0;
47900     pBt->isPending = 0;
47901   }else if( pBt->nTransaction==2 ){
47902     /* This function is called when Btree p is concluding its
47903     ** transaction. If there currently exists a writer, and p is not
47904     ** that writer, then the number of locks held by connections other
47905     ** than the writer must be about to drop to zero. In this case
47906     ** set the isPending flag to 0.
47907     **
47908     ** If there is not currently a writer, then BtShared.isPending must
47909     ** be zero already. So this next line is harmless in that case.
47910     */
47911     pBt->isPending = 0;
47912   }
47913 }
47914 
47915 /*
47916 ** This function changes all write-locks held by Btree p into read-locks.
47917 */
47918 static void downgradeAllSharedCacheTableLocks(Btree *p){
47919   BtShared *pBt = p->pBt;
47920   if( pBt->pWriter==p ){
47921     BtLock *pLock;
47922     pBt->pWriter = 0;
47923     pBt->isExclusive = 0;
47924     pBt->isPending = 0;
47925     for(pLock=pBt->pLock; pLock; pLock=pLock->pNext){
47926       assert( pLock->eLock==READ_LOCK || pLock->pBtree==p );
47927       pLock->eLock = READ_LOCK;
47928     }
47929   }
47930 }
47931 
47932 #endif /* SQLITE_OMIT_SHARED_CACHE */
47933 
47934 static void releasePage(MemPage *pPage);  /* Forward reference */
47935 
47936 /*
47937 ***** This routine is used inside of assert() only ****
47938 **
47939 ** Verify that the cursor holds the mutex on its BtShared
47940 */
47941 #ifdef SQLITE_DEBUG
47942 static int cursorHoldsMutex(BtCursor *p){
47943   return sqlite3_mutex_held(p->pBt->mutex);
47944 }
47945 #endif
47946 
47947 
47948 #ifndef SQLITE_OMIT_INCRBLOB
47949 /*
47950 ** Invalidate the overflow page-list cache for cursor pCur, if any.
47951 */
47952 static void invalidateOverflowCache(BtCursor *pCur){
47953   assert( cursorHoldsMutex(pCur) );
47954   sqlite3_free(pCur->aOverflow);
47955   pCur->aOverflow = 0;
47956 }
47957 
47958 /*
47959 ** Invalidate the overflow page-list cache for all cursors opened
47960 ** on the shared btree structure pBt.
47961 */
47962 static void invalidateAllOverflowCache(BtShared *pBt){
47963   BtCursor *p;
47964   assert( sqlite3_mutex_held(pBt->mutex) );
47965   for(p=pBt->pCursor; p; p=p->pNext){
47966     invalidateOverflowCache(p);
47967   }
47968 }
47969 
47970 /*
47971 ** This function is called before modifying the contents of a table
47972 ** to invalidate any incrblob cursors that are open on the
47973 ** row or one of the rows being modified.
47974 **
47975 ** If argument isClearTable is true, then the entire contents of the
47976 ** table is about to be deleted. In this case invalidate all incrblob
47977 ** cursors open on any row within the table with root-page pgnoRoot.
47978 **
47979 ** Otherwise, if argument isClearTable is false, then the row with
47980 ** rowid iRow is being replaced or deleted. In this case invalidate
47981 ** only those incrblob cursors open on that specific row.
47982 */
47983 static void invalidateIncrblobCursors(
47984   Btree *pBtree,          /* The database file to check */
47985   i64 iRow,               /* The rowid that might be changing */
47986   int isClearTable        /* True if all rows are being deleted */
47987 ){
47988   BtCursor *p;
47989   BtShared *pBt = pBtree->pBt;
47990   assert( sqlite3BtreeHoldsMutex(pBtree) );
47991   for(p=pBt->pCursor; p; p=p->pNext){
47992     if( p->isIncrblobHandle && (isClearTable || p->info.nKey==iRow) ){
47993       p->eState = CURSOR_INVALID;
47994     }
47995   }
47996 }
47997 
47998 #else
47999   /* Stub functions when INCRBLOB is omitted */
48000   #define invalidateOverflowCache(x)
48001   #define invalidateAllOverflowCache(x)
48002   #define invalidateIncrblobCursors(x,y,z)
48003 #endif /* SQLITE_OMIT_INCRBLOB */
48004 
48005 /*
48006 ** Set bit pgno of the BtShared.pHasContent bitvec. This is called
48007 ** when a page that previously contained data becomes a free-list leaf
48008 ** page.
48009 **
48010 ** The BtShared.pHasContent bitvec exists to work around an obscure
48011 ** bug caused by the interaction of two useful IO optimizations surrounding
48012 ** free-list leaf pages:
48013 **
48014 **   1) When all data is deleted from a page and the page becomes
48015 **      a free-list leaf page, the page is not written to the database
48016 **      (as free-list leaf pages contain no meaningful data). Sometimes
48017 **      such a page is not even journalled (as it will not be modified,
48018 **      why bother journalling it?).
48019 **
48020 **   2) When a free-list leaf page is reused, its content is not read
48021 **      from the database or written to the journal file (why should it
48022 **      be, if it is not at all meaningful?).
48023 **
48024 ** By themselves, these optimizations work fine and provide a handy
48025 ** performance boost to bulk delete or insert operations. However, if
48026 ** a page is moved to the free-list and then reused within the same
48027 ** transaction, a problem comes up. If the page is not journalled when
48028 ** it is moved to the free-list and it is also not journalled when it
48029 ** is extracted from the free-list and reused, then the original data
48030 ** may be lost. In the event of a rollback, it may not be possible
48031 ** to restore the database to its original configuration.
48032 **
48033 ** The solution is the BtShared.pHasContent bitvec. Whenever a page is
48034 ** moved to become a free-list leaf page, the corresponding bit is
48035 ** set in the bitvec. Whenever a leaf page is extracted from the free-list,
48036 ** optimization 2 above is omitted if the corresponding bit is already
48037 ** set in BtShared.pHasContent. The contents of the bitvec are cleared
48038 ** at the end of every transaction.
48039 */
48040 static int btreeSetHasContent(BtShared *pBt, Pgno pgno){
48041   int rc = SQLITE_OK;
48042   if( !pBt->pHasContent ){
48043     assert( pgno<=pBt->nPage );
48044     pBt->pHasContent = sqlite3BitvecCreate(pBt->nPage);
48045     if( !pBt->pHasContent ){
48046       rc = SQLITE_NOMEM;
48047     }
48048   }
48049   if( rc==SQLITE_OK && pgno<=sqlite3BitvecSize(pBt->pHasContent) ){
48050     rc = sqlite3BitvecSet(pBt->pHasContent, pgno);
48051   }
48052   return rc;
48053 }
48054 
48055 /*
48056 ** Query the BtShared.pHasContent vector.
48057 **
48058 ** This function is called when a free-list leaf page is removed from the
48059 ** free-list for reuse. It returns false if it is safe to retrieve the
48060 ** page from the pager layer with the 'no-content' flag set. True otherwise.
48061 */
48062 static int btreeGetHasContent(BtShared *pBt, Pgno pgno){
48063   Bitvec *p = pBt->pHasContent;
48064   return (p && (pgno>sqlite3BitvecSize(p) || sqlite3BitvecTest(p, pgno)));
48065 }
48066 
48067 /*
48068 ** Clear (destroy) the BtShared.pHasContent bitvec. This should be
48069 ** invoked at the conclusion of each write-transaction.
48070 */
48071 static void btreeClearHasContent(BtShared *pBt){
48072   sqlite3BitvecDestroy(pBt->pHasContent);
48073   pBt->pHasContent = 0;
48074 }
48075 
48076 /*
48077 ** Save the current cursor position in the variables BtCursor.nKey
48078 ** and BtCursor.pKey. The cursor's state is set to CURSOR_REQUIRESEEK.
48079 **
48080 ** The caller must ensure that the cursor is valid (has eState==CURSOR_VALID)
48081 ** prior to calling this routine.
48082 */
48083 static int saveCursorPosition(BtCursor *pCur){
48084   int rc;
48085 
48086   assert( CURSOR_VALID==pCur->eState );
48087   assert( 0==pCur->pKey );
48088   assert( cursorHoldsMutex(pCur) );
48089 
48090   rc = sqlite3BtreeKeySize(pCur, &pCur->nKey);
48091   assert( rc==SQLITE_OK );  /* KeySize() cannot fail */
48092 
48093   /* If this is an intKey table, then the above call to BtreeKeySize()
48094   ** stores the integer key in pCur->nKey. In this case this value is
48095   ** all that is required. Otherwise, if pCur is not open on an intKey
48096   ** table, then malloc space for and store the pCur->nKey bytes of key
48097   ** data.
48098   */
48099   if( 0==pCur->apPage[0]->intKey ){
48100     void *pKey = sqlite3Malloc( (int)pCur->nKey );
48101     if( pKey ){
48102       rc = sqlite3BtreeKey(pCur, 0, (int)pCur->nKey, pKey);
48103       if( rc==SQLITE_OK ){
48104         pCur->pKey = pKey;
48105       }else{
48106         sqlite3_free(pKey);
48107       }
48108     }else{
48109       rc = SQLITE_NOMEM;
48110     }
48111   }
48112   assert( !pCur->apPage[0]->intKey || !pCur->pKey );
48113 
48114   if( rc==SQLITE_OK ){
48115     int i;
48116     for(i=0; i<=pCur->iPage; i++){
48117       releasePage(pCur->apPage[i]);
48118       pCur->apPage[i] = 0;
48119     }
48120     pCur->iPage = -1;
48121     pCur->eState = CURSOR_REQUIRESEEK;
48122   }
48123 
48124   invalidateOverflowCache(pCur);
48125   return rc;
48126 }
48127 
48128 /*
48129 ** Save the positions of all cursors (except pExcept) that are open on
48130 ** the table  with root-page iRoot. Usually, this is called just before cursor
48131 ** pExcept is used to modify the table (BtreeDelete() or BtreeInsert()).
48132 */
48133 static int saveAllCursors(BtShared *pBt, Pgno iRoot, BtCursor *pExcept){
48134   BtCursor *p;
48135   assert( sqlite3_mutex_held(pBt->mutex) );
48136   assert( pExcept==0 || pExcept->pBt==pBt );
48137   for(p=pBt->pCursor; p; p=p->pNext){
48138     if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) &&
48139         p->eState==CURSOR_VALID ){
48140       int rc = saveCursorPosition(p);
48141       if( SQLITE_OK!=rc ){
48142         return rc;
48143       }
48144     }
48145   }
48146   return SQLITE_OK;
48147 }
48148 
48149 /*
48150 ** Clear the current cursor position.
48151 */
48152 SQLITE_PRIVATE void sqlite3BtreeClearCursor(BtCursor *pCur){
48153   assert( cursorHoldsMutex(pCur) );
48154   sqlite3_free(pCur->pKey);
48155   pCur->pKey = 0;
48156   pCur->eState = CURSOR_INVALID;
48157 }
48158 
48159 /*
48160 ** In this version of BtreeMoveto, pKey is a packed index record
48161 ** such as is generated by the OP_MakeRecord opcode.  Unpack the
48162 ** record and then call BtreeMovetoUnpacked() to do the work.
48163 */
48164 static int btreeMoveto(
48165   BtCursor *pCur,     /* Cursor open on the btree to be searched */
48166   const void *pKey,   /* Packed key if the btree is an index */
48167   i64 nKey,           /* Integer key for tables.  Size of pKey for indices */
48168   int bias,           /* Bias search to the high end */
48169   int *pRes           /* Write search results here */
48170 ){
48171   int rc;                    /* Status code */
48172   UnpackedRecord *pIdxKey;   /* Unpacked index key */
48173   char aSpace[150];          /* Temp space for pIdxKey - to avoid a malloc */
48174 
48175   if( pKey ){
48176     assert( nKey==(i64)(int)nKey );
48177     pIdxKey = sqlite3VdbeRecordUnpack(pCur->pKeyInfo, (int)nKey, pKey,
48178                                       aSpace, sizeof(aSpace));
48179     if( pIdxKey==0 ) return SQLITE_NOMEM;
48180   }else{
48181     pIdxKey = 0;
48182   }
48183   rc = sqlite3BtreeMovetoUnpacked(pCur, pIdxKey, nKey, bias, pRes);
48184   if( pKey ){
48185     sqlite3VdbeDeleteUnpackedRecord(pIdxKey);
48186   }
48187   return rc;
48188 }
48189 
48190 /*
48191 ** Restore the cursor to the position it was in (or as close to as possible)
48192 ** when saveCursorPosition() was called. Note that this call deletes the
48193 ** saved position info stored by saveCursorPosition(), so there can be
48194 ** at most one effective restoreCursorPosition() call after each
48195 ** saveCursorPosition().
48196 */
48197 static int btreeRestoreCursorPosition(BtCursor *pCur){
48198   int rc;
48199   assert( cursorHoldsMutex(pCur) );
48200   assert( pCur->eState>=CURSOR_REQUIRESEEK );
48201   if( pCur->eState==CURSOR_FAULT ){
48202     return pCur->skipNext;
48203   }
48204   pCur->eState = CURSOR_INVALID;
48205   rc = btreeMoveto(pCur, pCur->pKey, pCur->nKey, 0, &pCur->skipNext);
48206   if( rc==SQLITE_OK ){
48207     sqlite3_free(pCur->pKey);
48208     pCur->pKey = 0;
48209     assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_INVALID );
48210   }
48211   return rc;
48212 }
48213 
48214 #define restoreCursorPosition(p) \
48215   (p->eState>=CURSOR_REQUIRESEEK ? \
48216          btreeRestoreCursorPosition(p) : \
48217          SQLITE_OK)
48218 
48219 /*
48220 ** Determine whether or not a cursor has moved from the position it
48221 ** was last placed at.  Cursors can move when the row they are pointing
48222 ** at is deleted out from under them.
48223 **
48224 ** This routine returns an error code if something goes wrong.  The
48225 ** integer *pHasMoved is set to one if the cursor has moved and 0 if not.
48226 */
48227 SQLITE_PRIVATE int sqlite3BtreeCursorHasMoved(BtCursor *pCur, int *pHasMoved){
48228   int rc;
48229 
48230   rc = restoreCursorPosition(pCur);
48231   if( rc ){
48232     *pHasMoved = 1;
48233     return rc;
48234   }
48235   if( pCur->eState!=CURSOR_VALID || pCur->skipNext!=0 ){
48236     *pHasMoved = 1;
48237   }else{
48238     *pHasMoved = 0;
48239   }
48240   return SQLITE_OK;
48241 }
48242 
48243 #ifndef SQLITE_OMIT_AUTOVACUUM
48244 /*
48245 ** Given a page number of a regular database page, return the page
48246 ** number for the pointer-map page that contains the entry for the
48247 ** input page number.
48248 **
48249 ** Return 0 (not a valid page) for pgno==1 since there is
48250 ** no pointer map associated with page 1.  The integrity_check logic
48251 ** requires that ptrmapPageno(*,1)!=1.
48252 */
48253 static Pgno ptrmapPageno(BtShared *pBt, Pgno pgno){
48254   int nPagesPerMapPage;
48255   Pgno iPtrMap, ret;
48256   assert( sqlite3_mutex_held(pBt->mutex) );
48257   if( pgno<2 ) return 0;
48258   nPagesPerMapPage = (pBt->usableSize/5)+1;
48259   iPtrMap = (pgno-2)/nPagesPerMapPage;
48260   ret = (iPtrMap*nPagesPerMapPage) + 2;
48261   if( ret==PENDING_BYTE_PAGE(pBt) ){
48262     ret++;
48263   }
48264   return ret;
48265 }
48266 
48267 /*
48268 ** Write an entry into the pointer map.
48269 **
48270 ** This routine updates the pointer map entry for page number 'key'
48271 ** so that it maps to type 'eType' and parent page number 'pgno'.
48272 **
48273 ** If *pRC is initially non-zero (non-SQLITE_OK) then this routine is
48274 ** a no-op.  If an error occurs, the appropriate error code is written
48275 ** into *pRC.
48276 */
48277 static void ptrmapPut(BtShared *pBt, Pgno key, u8 eType, Pgno parent, int *pRC){
48278   DbPage *pDbPage;  /* The pointer map page */
48279   u8 *pPtrmap;      /* The pointer map data */
48280   Pgno iPtrmap;     /* The pointer map page number */
48281   int offset;       /* Offset in pointer map page */
48282   int rc;           /* Return code from subfunctions */
48283 
48284   if( *pRC ) return;
48285 
48286   assert( sqlite3_mutex_held(pBt->mutex) );
48287   /* The master-journal page number must never be used as a pointer map page */
48288   assert( 0==PTRMAP_ISPAGE(pBt, PENDING_BYTE_PAGE(pBt)) );
48289 
48290   assert( pBt->autoVacuum );
48291   if( key==0 ){
48292     *pRC = SQLITE_CORRUPT_BKPT;
48293     return;
48294   }
48295   iPtrmap = PTRMAP_PAGENO(pBt, key);
48296   rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage);
48297   if( rc!=SQLITE_OK ){
48298     *pRC = rc;
48299     return;
48300   }
48301   offset = PTRMAP_PTROFFSET(iPtrmap, key);
48302   if( offset<0 ){
48303     *pRC = SQLITE_CORRUPT_BKPT;
48304     goto ptrmap_exit;
48305   }
48306   assert( offset <= (int)pBt->usableSize-5 );
48307   pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage);
48308 
48309   if( eType!=pPtrmap[offset] || get4byte(&pPtrmap[offset+1])!=parent ){
48310     TRACE(("PTRMAP_UPDATE: %d->(%d,%d)\n", key, eType, parent));
48311     *pRC= rc = sqlite3PagerWrite(pDbPage);
48312     if( rc==SQLITE_OK ){
48313       pPtrmap[offset] = eType;
48314       put4byte(&pPtrmap[offset+1], parent);
48315     }
48316   }
48317 
48318 ptrmap_exit:
48319   sqlite3PagerUnref(pDbPage);
48320 }
48321 
48322 /*
48323 ** Read an entry from the pointer map.
48324 **
48325 ** This routine retrieves the pointer map entry for page 'key', writing
48326 ** the type and parent page number to *pEType and *pPgno respectively.
48327 ** An error code is returned if something goes wrong, otherwise SQLITE_OK.
48328 */
48329 static int ptrmapGet(BtShared *pBt, Pgno key, u8 *pEType, Pgno *pPgno){
48330   DbPage *pDbPage;   /* The pointer map page */
48331   int iPtrmap;       /* Pointer map page index */
48332   u8 *pPtrmap;       /* Pointer map page data */
48333   int offset;        /* Offset of entry in pointer map */
48334   int rc;
48335 
48336   assert( sqlite3_mutex_held(pBt->mutex) );
48337 
48338   iPtrmap = PTRMAP_PAGENO(pBt, key);
48339   rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage);
48340   if( rc!=0 ){
48341     return rc;
48342   }
48343   pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage);
48344 
48345   offset = PTRMAP_PTROFFSET(iPtrmap, key);
48346   if( offset<0 ){
48347     sqlite3PagerUnref(pDbPage);
48348     return SQLITE_CORRUPT_BKPT;
48349   }
48350   assert( offset <= (int)pBt->usableSize-5 );
48351   assert( pEType!=0 );
48352   *pEType = pPtrmap[offset];
48353   if( pPgno ) *pPgno = get4byte(&pPtrmap[offset+1]);
48354 
48355   sqlite3PagerUnref(pDbPage);
48356   if( *pEType<1 || *pEType>5 ) return SQLITE_CORRUPT_BKPT;
48357   return SQLITE_OK;
48358 }
48359 
48360 #else /* if defined SQLITE_OMIT_AUTOVACUUM */
48361   #define ptrmapPut(w,x,y,z,rc)
48362   #define ptrmapGet(w,x,y,z) SQLITE_OK
48363   #define ptrmapPutOvflPtr(x, y, rc)
48364 #endif
48365 
48366 /*
48367 ** Given a btree page and a cell index (0 means the first cell on
48368 ** the page, 1 means the second cell, and so forth) return a pointer
48369 ** to the cell content.
48370 **
48371 ** This routine works only for pages that do not contain overflow cells.
48372 */
48373 #define findCell(P,I) \
48374   ((P)->aData + ((P)->maskPage & get2byte(&(P)->aData[(P)->cellOffset+2*(I)])))
48375 #define findCellv2(D,M,O,I) (D+(M&get2byte(D+(O+2*(I)))))
48376 
48377 
48378 /*
48379 ** This a more complex version of findCell() that works for
48380 ** pages that do contain overflow cells.
48381 */
48382 static u8 *findOverflowCell(MemPage *pPage, int iCell){
48383   int i;
48384   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
48385   for(i=pPage->nOverflow-1; i>=0; i--){
48386     int k;
48387     struct _OvflCell *pOvfl;
48388     pOvfl = &pPage->aOvfl[i];
48389     k = pOvfl->idx;
48390     if( k<=iCell ){
48391       if( k==iCell ){
48392         return pOvfl->pCell;
48393       }
48394       iCell--;
48395     }
48396   }
48397   return findCell(pPage, iCell);
48398 }
48399 
48400 /*
48401 ** Parse a cell content block and fill in the CellInfo structure.  There
48402 ** are two versions of this function.  btreeParseCell() takes a
48403 ** cell index as the second argument and btreeParseCellPtr()
48404 ** takes a pointer to the body of the cell as its second argument.
48405 **
48406 ** Within this file, the parseCell() macro can be called instead of
48407 ** btreeParseCellPtr(). Using some compilers, this will be faster.
48408 */
48409 static void btreeParseCellPtr(
48410   MemPage *pPage,         /* Page containing the cell */
48411   u8 *pCell,              /* Pointer to the cell text. */
48412   CellInfo *pInfo         /* Fill in this structure */
48413 ){
48414   u16 n;                  /* Number bytes in cell content header */
48415   u32 nPayload;           /* Number of bytes of cell payload */
48416 
48417   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
48418 
48419   pInfo->pCell = pCell;
48420   assert( pPage->leaf==0 || pPage->leaf==1 );
48421   n = pPage->childPtrSize;
48422   assert( n==4-4*pPage->leaf );
48423   if( pPage->intKey ){
48424     if( pPage->hasData ){
48425       n += getVarint32(&pCell[n], nPayload);
48426     }else{
48427       nPayload = 0;
48428     }
48429     n += getVarint(&pCell[n], (u64*)&pInfo->nKey);
48430     pInfo->nData = nPayload;
48431   }else{
48432     pInfo->nData = 0;
48433     n += getVarint32(&pCell[n], nPayload);
48434     pInfo->nKey = nPayload;
48435   }
48436   pInfo->nPayload = nPayload;
48437   pInfo->nHeader = n;
48438   testcase( nPayload==pPage->maxLocal );
48439   testcase( nPayload==pPage->maxLocal+1 );
48440   if( likely(nPayload<=pPage->maxLocal) ){
48441     /* This is the (easy) common case where the entire payload fits
48442     ** on the local page.  No overflow is required.
48443     */
48444     if( (pInfo->nSize = (u16)(n+nPayload))<4 ) pInfo->nSize = 4;
48445     pInfo->nLocal = (u16)nPayload;
48446     pInfo->iOverflow = 0;
48447   }else{
48448     /* If the payload will not fit completely on the local page, we have
48449     ** to decide how much to store locally and how much to spill onto
48450     ** overflow pages.  The strategy is to minimize the amount of unused
48451     ** space on overflow pages while keeping the amount of local storage
48452     ** in between minLocal and maxLocal.
48453     **
48454     ** Warning:  changing the way overflow payload is distributed in any
48455     ** way will result in an incompatible file format.
48456     */
48457     int minLocal;  /* Minimum amount of payload held locally */
48458     int maxLocal;  /* Maximum amount of payload held locally */
48459     int surplus;   /* Overflow payload available for local storage */
48460 
48461     minLocal = pPage->minLocal;
48462     maxLocal = pPage->maxLocal;
48463     surplus = minLocal + (nPayload - minLocal)%(pPage->pBt->usableSize - 4);
48464     testcase( surplus==maxLocal );
48465     testcase( surplus==maxLocal+1 );
48466     if( surplus <= maxLocal ){
48467       pInfo->nLocal = (u16)surplus;
48468     }else{
48469       pInfo->nLocal = (u16)minLocal;
48470     }
48471     pInfo->iOverflow = (u16)(pInfo->nLocal + n);
48472     pInfo->nSize = pInfo->iOverflow + 4;
48473   }
48474 }
48475 #define parseCell(pPage, iCell, pInfo) \
48476   btreeParseCellPtr((pPage), findCell((pPage), (iCell)), (pInfo))
48477 static void btreeParseCell(
48478   MemPage *pPage,         /* Page containing the cell */
48479   int iCell,              /* The cell index.  First cell is 0 */
48480   CellInfo *pInfo         /* Fill in this structure */
48481 ){
48482   parseCell(pPage, iCell, pInfo);
48483 }
48484 
48485 /*
48486 ** Compute the total number of bytes that a Cell needs in the cell
48487 ** data area of the btree-page.  The return number includes the cell
48488 ** data header and the local payload, but not any overflow page or
48489 ** the space used by the cell pointer.
48490 */
48491 static u16 cellSizePtr(MemPage *pPage, u8 *pCell){
48492   u8 *pIter = &pCell[pPage->childPtrSize];
48493   u32 nSize;
48494 
48495 #ifdef SQLITE_DEBUG
48496   /* The value returned by this function should always be the same as
48497   ** the (CellInfo.nSize) value found by doing a full parse of the
48498   ** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of
48499   ** this function verifies that this invariant is not violated. */
48500   CellInfo debuginfo;
48501   btreeParseCellPtr(pPage, pCell, &debuginfo);
48502 #endif
48503 
48504   if( pPage->intKey ){
48505     u8 *pEnd;
48506     if( pPage->hasData ){
48507       pIter += getVarint32(pIter, nSize);
48508     }else{
48509       nSize = 0;
48510     }
48511 
48512     /* pIter now points at the 64-bit integer key value, a variable length
48513     ** integer. The following block moves pIter to point at the first byte
48514     ** past the end of the key value. */
48515     pEnd = &pIter[9];
48516     while( (*pIter++)&0x80 && pIter<pEnd );
48517   }else{
48518     pIter += getVarint32(pIter, nSize);
48519   }
48520 
48521   testcase( nSize==pPage->maxLocal );
48522   testcase( nSize==pPage->maxLocal+1 );
48523   if( nSize>pPage->maxLocal ){
48524     int minLocal = pPage->minLocal;
48525     nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4);
48526     testcase( nSize==pPage->maxLocal );
48527     testcase( nSize==pPage->maxLocal+1 );
48528     if( nSize>pPage->maxLocal ){
48529       nSize = minLocal;
48530     }
48531     nSize += 4;
48532   }
48533   nSize += (u32)(pIter - pCell);
48534 
48535   /* The minimum size of any cell is 4 bytes. */
48536   if( nSize<4 ){
48537     nSize = 4;
48538   }
48539 
48540   assert( nSize==debuginfo.nSize );
48541   return (u16)nSize;
48542 }
48543 
48544 #ifdef SQLITE_DEBUG
48545 /* This variation on cellSizePtr() is used inside of assert() statements
48546 ** only. */
48547 static u16 cellSize(MemPage *pPage, int iCell){
48548   return cellSizePtr(pPage, findCell(pPage, iCell));
48549 }
48550 #endif
48551 
48552 #ifndef SQLITE_OMIT_AUTOVACUUM
48553 /*
48554 ** If the cell pCell, part of page pPage contains a pointer
48555 ** to an overflow page, insert an entry into the pointer-map
48556 ** for the overflow page.
48557 */
48558 static void ptrmapPutOvflPtr(MemPage *pPage, u8 *pCell, int *pRC){
48559   CellInfo info;
48560   if( *pRC ) return;
48561   assert( pCell!=0 );
48562   btreeParseCellPtr(pPage, pCell, &info);
48563   assert( (info.nData+(pPage->intKey?0:info.nKey))==info.nPayload );
48564   if( info.iOverflow ){
48565     Pgno ovfl = get4byte(&pCell[info.iOverflow]);
48566     ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno, pRC);
48567   }
48568 }
48569 #endif
48570 
48571 
48572 /*
48573 ** Defragment the page given.  All Cells are moved to the
48574 ** end of the page and all free space is collected into one
48575 ** big FreeBlk that occurs in between the header and cell
48576 ** pointer array and the cell content area.
48577 */
48578 static int defragmentPage(MemPage *pPage){
48579   int i;                     /* Loop counter */
48580   int pc;                    /* Address of a i-th cell */
48581   int hdr;                   /* Offset to the page header */
48582   int size;                  /* Size of a cell */
48583   int usableSize;            /* Number of usable bytes on a page */
48584   int cellOffset;            /* Offset to the cell pointer array */
48585   int cbrk;                  /* Offset to the cell content area */
48586   int nCell;                 /* Number of cells on the page */
48587   unsigned char *data;       /* The page data */
48588   unsigned char *temp;       /* Temp area for cell content */
48589   int iCellFirst;            /* First allowable cell index */
48590   int iCellLast;             /* Last possible cell index */
48591 
48592 
48593   assert( sqlite3PagerIswriteable(pPage->pDbPage) );
48594   assert( pPage->pBt!=0 );
48595   assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE );
48596   assert( pPage->nOverflow==0 );
48597   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
48598   temp = sqlite3PagerTempSpace(pPage->pBt->pPager);
48599   data = pPage->aData;
48600   hdr = pPage->hdrOffset;
48601   cellOffset = pPage->cellOffset;
48602   nCell = pPage->nCell;
48603   assert( nCell==get2byte(&data[hdr+3]) );
48604   usableSize = pPage->pBt->usableSize;
48605   cbrk = get2byte(&data[hdr+5]);
48606   memcpy(&temp[cbrk], &data[cbrk], usableSize - cbrk);
48607   cbrk = usableSize;
48608   iCellFirst = cellOffset + 2*nCell;
48609   iCellLast = usableSize - 4;
48610   for(i=0; i<nCell; i++){
48611     u8 *pAddr;     /* The i-th cell pointer */
48612     pAddr = &data[cellOffset + i*2];
48613     pc = get2byte(pAddr);
48614     testcase( pc==iCellFirst );
48615     testcase( pc==iCellLast );
48616 #if !defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK)
48617     /* These conditions have already been verified in btreeInitPage()
48618     ** if SQLITE_ENABLE_OVERSIZE_CELL_CHECK is defined
48619     */
48620     if( pc<iCellFirst || pc>iCellLast ){
48621       return SQLITE_CORRUPT_BKPT;
48622     }
48623 #endif
48624     assert( pc>=iCellFirst && pc<=iCellLast );
48625     size = cellSizePtr(pPage, &temp[pc]);
48626     cbrk -= size;
48627 #if defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK)
48628     if( cbrk<iCellFirst ){
48629       return SQLITE_CORRUPT_BKPT;
48630     }
48631 #else
48632     if( cbrk<iCellFirst || pc+size>usableSize ){
48633       return SQLITE_CORRUPT_BKPT;
48634     }
48635 #endif
48636     assert( cbrk+size<=usableSize && cbrk>=iCellFirst );
48637     testcase( cbrk+size==usableSize );
48638     testcase( pc+size==usableSize );
48639     memcpy(&data[cbrk], &temp[pc], size);
48640     put2byte(pAddr, cbrk);
48641   }
48642   assert( cbrk>=iCellFirst );
48643   put2byte(&data[hdr+5], cbrk);
48644   data[hdr+1] = 0;
48645   data[hdr+2] = 0;
48646   data[hdr+7] = 0;
48647   memset(&data[iCellFirst], 0, cbrk-iCellFirst);
48648   assert( sqlite3PagerIswriteable(pPage->pDbPage) );
48649   if( cbrk-iCellFirst!=pPage->nFree ){
48650     return SQLITE_CORRUPT_BKPT;
48651   }
48652   return SQLITE_OK;
48653 }
48654 
48655 /*
48656 ** Allocate nByte bytes of space from within the B-Tree page passed
48657 ** as the first argument. Write into *pIdx the index into pPage->aData[]
48658 ** of the first byte of allocated space. Return either SQLITE_OK or
48659 ** an error code (usually SQLITE_CORRUPT).
48660 **
48661 ** The caller guarantees that there is sufficient space to make the
48662 ** allocation.  This routine might need to defragment in order to bring
48663 ** all the space together, however.  This routine will avoid using
48664 ** the first two bytes past the cell pointer area since presumably this
48665 ** allocation is being made in order to insert a new cell, so we will
48666 ** also end up needing a new cell pointer.
48667 */
48668 static int allocateSpace(MemPage *pPage, int nByte, int *pIdx){
48669   const int hdr = pPage->hdrOffset;    /* Local cache of pPage->hdrOffset */
48670   u8 * const data = pPage->aData;      /* Local cache of pPage->aData */
48671   int nFrag;                           /* Number of fragmented bytes on pPage */
48672   int top;                             /* First byte of cell content area */
48673   int gap;        /* First byte of gap between cell pointers and cell content */
48674   int rc;         /* Integer return code */
48675   int usableSize; /* Usable size of the page */
48676 
48677   assert( sqlite3PagerIswriteable(pPage->pDbPage) );
48678   assert( pPage->pBt );
48679   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
48680   assert( nByte>=0 );  /* Minimum cell size is 4 */
48681   assert( pPage->nFree>=nByte );
48682   assert( pPage->nOverflow==0 );
48683   usableSize = pPage->pBt->usableSize;
48684   assert( nByte < usableSize-8 );
48685 
48686   nFrag = data[hdr+7];
48687   assert( pPage->cellOffset == hdr + 12 - 4*pPage->leaf );
48688   gap = pPage->cellOffset + 2*pPage->nCell;
48689   top = get2byteNotZero(&data[hdr+5]);
48690   if( gap>top ) return SQLITE_CORRUPT_BKPT;
48691   testcase( gap+2==top );
48692   testcase( gap+1==top );
48693   testcase( gap==top );
48694 
48695   if( nFrag>=60 ){
48696     /* Always defragment highly fragmented pages */
48697     rc = defragmentPage(pPage);
48698     if( rc ) return rc;
48699     top = get2byteNotZero(&data[hdr+5]);
48700   }else if( gap+2<=top ){
48701     /* Search the freelist looking for a free slot big enough to satisfy
48702     ** the request. The allocation is made from the first free slot in
48703     ** the list that is large enough to accomadate it.
48704     */
48705     int pc, addr;
48706     for(addr=hdr+1; (pc = get2byte(&data[addr]))>0; addr=pc){
48707       int size;            /* Size of the free slot */
48708       if( pc>usableSize-4 || pc<addr+4 ){
48709         return SQLITE_CORRUPT_BKPT;
48710       }
48711       size = get2byte(&data[pc+2]);
48712       if( size>=nByte ){
48713         int x = size - nByte;
48714         testcase( x==4 );
48715         testcase( x==3 );
48716         if( x<4 ){
48717           /* Remove the slot from the free-list. Update the number of
48718           ** fragmented bytes within the page. */
48719           memcpy(&data[addr], &data[pc], 2);
48720           data[hdr+7] = (u8)(nFrag + x);
48721         }else if( size+pc > usableSize ){
48722           return SQLITE_CORRUPT_BKPT;
48723         }else{
48724           /* The slot remains on the free-list. Reduce its size to account
48725           ** for the portion used by the new allocation. */
48726           put2byte(&data[pc+2], x);
48727         }
48728         *pIdx = pc + x;
48729         return SQLITE_OK;
48730       }
48731     }
48732   }
48733 
48734   /* Check to make sure there is enough space in the gap to satisfy
48735   ** the allocation.  If not, defragment.
48736   */
48737   testcase( gap+2+nByte==top );
48738   if( gap+2+nByte>top ){
48739     rc = defragmentPage(pPage);
48740     if( rc ) return rc;
48741     top = get2byteNotZero(&data[hdr+5]);
48742     assert( gap+nByte<=top );
48743   }
48744 
48745 
48746   /* Allocate memory from the gap in between the cell pointer array
48747   ** and the cell content area.  The btreeInitPage() call has already
48748   ** validated the freelist.  Given that the freelist is valid, there
48749   ** is no way that the allocation can extend off the end of the page.
48750   ** The assert() below verifies the previous sentence.
48751   */
48752   top -= nByte;
48753   put2byte(&data[hdr+5], top);
48754   assert( top+nByte <= (int)pPage->pBt->usableSize );
48755   *pIdx = top;
48756   return SQLITE_OK;
48757 }
48758 
48759 /*
48760 ** Return a section of the pPage->aData to the freelist.
48761 ** The first byte of the new free block is pPage->aDisk[start]
48762 ** and the size of the block is "size" bytes.
48763 **
48764 ** Most of the effort here is involved in coalesing adjacent
48765 ** free blocks into a single big free block.
48766 */
48767 static int freeSpace(MemPage *pPage, int start, int size){
48768   int addr, pbegin, hdr;
48769   int iLast;                        /* Largest possible freeblock offset */
48770   unsigned char *data = pPage->aData;
48771 
48772   assert( pPage->pBt!=0 );
48773   assert( sqlite3PagerIswriteable(pPage->pDbPage) );
48774   assert( start>=pPage->hdrOffset+6+pPage->childPtrSize );
48775   assert( (start + size) <= (int)pPage->pBt->usableSize );
48776   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
48777   assert( size>=0 );   /* Minimum cell size is 4 */
48778 
48779   if( pPage->pBt->secureDelete ){
48780     /* Overwrite deleted information with zeros when the secure_delete
48781     ** option is enabled */
48782     memset(&data[start], 0, size);
48783   }
48784 
48785   /* Add the space back into the linked list of freeblocks.  Note that
48786   ** even though the freeblock list was checked by btreeInitPage(),
48787   ** btreeInitPage() did not detect overlapping cells or
48788   ** freeblocks that overlapped cells.   Nor does it detect when the
48789   ** cell content area exceeds the value in the page header.  If these
48790   ** situations arise, then subsequent insert operations might corrupt
48791   ** the freelist.  So we do need to check for corruption while scanning
48792   ** the freelist.
48793   */
48794   hdr = pPage->hdrOffset;
48795   addr = hdr + 1;
48796   iLast = pPage->pBt->usableSize - 4;
48797   assert( start<=iLast );
48798   while( (pbegin = get2byte(&data[addr]))<start && pbegin>0 ){
48799     if( pbegin<addr+4 ){
48800       return SQLITE_CORRUPT_BKPT;
48801     }
48802     addr = pbegin;
48803   }
48804   if( pbegin>iLast ){
48805     return SQLITE_CORRUPT_BKPT;
48806   }
48807   assert( pbegin>addr || pbegin==0 );
48808   put2byte(&data[addr], start);
48809   put2byte(&data[start], pbegin);
48810   put2byte(&data[start+2], size);
48811   pPage->nFree = pPage->nFree + (u16)size;
48812 
48813   /* Coalesce adjacent free blocks */
48814   addr = hdr + 1;
48815   while( (pbegin = get2byte(&data[addr]))>0 ){
48816     int pnext, psize, x;
48817     assert( pbegin>addr );
48818     assert( pbegin <= (int)pPage->pBt->usableSize-4 );
48819     pnext = get2byte(&data[pbegin]);
48820     psize = get2byte(&data[pbegin+2]);
48821     if( pbegin + psize + 3 >= pnext && pnext>0 ){
48822       int frag = pnext - (pbegin+psize);
48823       if( (frag<0) || (frag>(int)data[hdr+7]) ){
48824         return SQLITE_CORRUPT_BKPT;
48825       }
48826       data[hdr+7] -= (u8)frag;
48827       x = get2byte(&data[pnext]);
48828       put2byte(&data[pbegin], x);
48829       x = pnext + get2byte(&data[pnext+2]) - pbegin;
48830       put2byte(&data[pbegin+2], x);
48831     }else{
48832       addr = pbegin;
48833     }
48834   }
48835 
48836   /* If the cell content area begins with a freeblock, remove it. */
48837   if( data[hdr+1]==data[hdr+5] && data[hdr+2]==data[hdr+6] ){
48838     int top;
48839     pbegin = get2byte(&data[hdr+1]);
48840     memcpy(&data[hdr+1], &data[pbegin], 2);
48841     top = get2byte(&data[hdr+5]) + get2byte(&data[pbegin+2]);
48842     put2byte(&data[hdr+5], top);
48843   }
48844   assert( sqlite3PagerIswriteable(pPage->pDbPage) );
48845   return SQLITE_OK;
48846 }
48847 
48848 /*
48849 ** Decode the flags byte (the first byte of the header) for a page
48850 ** and initialize fields of the MemPage structure accordingly.
48851 **
48852 ** Only the following combinations are supported.  Anything different
48853 ** indicates a corrupt database files:
48854 **
48855 **         PTF_ZERODATA
48856 **         PTF_ZERODATA | PTF_LEAF
48857 **         PTF_LEAFDATA | PTF_INTKEY
48858 **         PTF_LEAFDATA | PTF_INTKEY | PTF_LEAF
48859 */
48860 static int decodeFlags(MemPage *pPage, int flagByte){
48861   BtShared *pBt;     /* A copy of pPage->pBt */
48862 
48863   assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) );
48864   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
48865   pPage->leaf = (u8)(flagByte>>3);  assert( PTF_LEAF == 1<<3 );
48866   flagByte &= ~PTF_LEAF;
48867   pPage->childPtrSize = 4-4*pPage->leaf;
48868   pBt = pPage->pBt;
48869   if( flagByte==(PTF_LEAFDATA | PTF_INTKEY) ){
48870     pPage->intKey = 1;
48871     pPage->hasData = pPage->leaf;
48872     pPage->maxLocal = pBt->maxLeaf;
48873     pPage->minLocal = pBt->minLeaf;
48874   }else if( flagByte==PTF_ZERODATA ){
48875     pPage->intKey = 0;
48876     pPage->hasData = 0;
48877     pPage->maxLocal = pBt->maxLocal;
48878     pPage->minLocal = pBt->minLocal;
48879   }else{
48880     return SQLITE_CORRUPT_BKPT;
48881   }
48882   return SQLITE_OK;
48883 }
48884 
48885 /*
48886 ** Initialize the auxiliary information for a disk block.
48887 **
48888 ** Return SQLITE_OK on success.  If we see that the page does
48889 ** not contain a well-formed database page, then return
48890 ** SQLITE_CORRUPT.  Note that a return of SQLITE_OK does not
48891 ** guarantee that the page is well-formed.  It only shows that
48892 ** we failed to detect any corruption.
48893 */
48894 static int btreeInitPage(MemPage *pPage){
48895 
48896   assert( pPage->pBt!=0 );
48897   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
48898   assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) );
48899   assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) );
48900   assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) );
48901 
48902   if( !pPage->isInit ){
48903     u16 pc;            /* Address of a freeblock within pPage->aData[] */
48904     u8 hdr;            /* Offset to beginning of page header */
48905     u8 *data;          /* Equal to pPage->aData */
48906     BtShared *pBt;        /* The main btree structure */
48907     int usableSize;    /* Amount of usable space on each page */
48908     u16 cellOffset;    /* Offset from start of page to first cell pointer */
48909     int nFree;         /* Number of unused bytes on the page */
48910     int top;           /* First byte of the cell content area */
48911     int iCellFirst;    /* First allowable cell or freeblock offset */
48912     int iCellLast;     /* Last possible cell or freeblock offset */
48913 
48914     pBt = pPage->pBt;
48915 
48916     hdr = pPage->hdrOffset;
48917     data = pPage->aData;
48918     if( decodeFlags(pPage, data[hdr]) ) return SQLITE_CORRUPT_BKPT;
48919     assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
48920     pPage->maskPage = (u16)(pBt->pageSize - 1);
48921     pPage->nOverflow = 0;
48922     usableSize = pBt->usableSize;
48923     pPage->cellOffset = cellOffset = hdr + 12 - 4*pPage->leaf;
48924     top = get2byteNotZero(&data[hdr+5]);
48925     pPage->nCell = get2byte(&data[hdr+3]);
48926     if( pPage->nCell>MX_CELL(pBt) ){
48927       /* To many cells for a single page.  The page must be corrupt */
48928       return SQLITE_CORRUPT_BKPT;
48929     }
48930     testcase( pPage->nCell==MX_CELL(pBt) );
48931 
48932     /* A malformed database page might cause us to read past the end
48933     ** of page when parsing a cell.
48934     **
48935     ** The following block of code checks early to see if a cell extends
48936     ** past the end of a page boundary and causes SQLITE_CORRUPT to be
48937     ** returned if it does.
48938     */
48939     iCellFirst = cellOffset + 2*pPage->nCell;
48940     iCellLast = usableSize - 4;
48941 #if defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK)
48942     {
48943       int i;            /* Index into the cell pointer array */
48944       int sz;           /* Size of a cell */
48945 
48946       if( !pPage->leaf ) iCellLast--;
48947       for(i=0; i<pPage->nCell; i++){
48948         pc = get2byte(&data[cellOffset+i*2]);
48949         testcase( pc==iCellFirst );
48950         testcase( pc==iCellLast );
48951         if( pc<iCellFirst || pc>iCellLast ){
48952           return SQLITE_CORRUPT_BKPT;
48953         }
48954         sz = cellSizePtr(pPage, &data[pc]);
48955         testcase( pc+sz==usableSize );
48956         if( pc+sz>usableSize ){
48957           return SQLITE_CORRUPT_BKPT;
48958         }
48959       }
48960       if( !pPage->leaf ) iCellLast++;
48961     }
48962 #endif
48963 
48964     /* Compute the total free space on the page */
48965     pc = get2byte(&data[hdr+1]);
48966     nFree = data[hdr+7] + top;
48967     while( pc>0 ){
48968       u16 next, size;
48969       if( pc<iCellFirst || pc>iCellLast ){
48970         /* Start of free block is off the page */
48971         return SQLITE_CORRUPT_BKPT;
48972       }
48973       next = get2byte(&data[pc]);
48974       size = get2byte(&data[pc+2]);
48975       if( (next>0 && next<=pc+size+3) || pc+size>usableSize ){
48976         /* Free blocks must be in ascending order. And the last byte of
48977 	** the free-block must lie on the database page.  */
48978         return SQLITE_CORRUPT_BKPT;
48979       }
48980       nFree = nFree + size;
48981       pc = next;
48982     }
48983 
48984     /* At this point, nFree contains the sum of the offset to the start
48985     ** of the cell-content area plus the number of free bytes within
48986     ** the cell-content area. If this is greater than the usable-size
48987     ** of the page, then the page must be corrupted. This check also
48988     ** serves to verify that the offset to the start of the cell-content
48989     ** area, according to the page header, lies within the page.
48990     */
48991     if( nFree>usableSize ){
48992       return SQLITE_CORRUPT_BKPT;
48993     }
48994     pPage->nFree = (u16)(nFree - iCellFirst);
48995     pPage->isInit = 1;
48996   }
48997   return SQLITE_OK;
48998 }
48999 
49000 /*
49001 ** Set up a raw page so that it looks like a database page holding
49002 ** no entries.
49003 */
49004 static void zeroPage(MemPage *pPage, int flags){
49005   unsigned char *data = pPage->aData;
49006   BtShared *pBt = pPage->pBt;
49007   u8 hdr = pPage->hdrOffset;
49008   u16 first;
49009 
49010   assert( sqlite3PagerPagenumber(pPage->pDbPage)==pPage->pgno );
49011   assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
49012   assert( sqlite3PagerGetData(pPage->pDbPage) == data );
49013   assert( sqlite3PagerIswriteable(pPage->pDbPage) );
49014   assert( sqlite3_mutex_held(pBt->mutex) );
49015   if( pBt->secureDelete ){
49016     memset(&data[hdr], 0, pBt->usableSize - hdr);
49017   }
49018   data[hdr] = (char)flags;
49019   first = hdr + 8 + 4*((flags&PTF_LEAF)==0 ?1:0);
49020   memset(&data[hdr+1], 0, 4);
49021   data[hdr+7] = 0;
49022   put2byte(&data[hdr+5], pBt->usableSize);
49023   pPage->nFree = (u16)(pBt->usableSize - first);
49024   decodeFlags(pPage, flags);
49025   pPage->hdrOffset = hdr;
49026   pPage->cellOffset = first;
49027   pPage->nOverflow = 0;
49028   assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
49029   pPage->maskPage = (u16)(pBt->pageSize - 1);
49030   pPage->nCell = 0;
49031   pPage->isInit = 1;
49032 }
49033 
49034 
49035 /*
49036 ** Convert a DbPage obtained from the pager into a MemPage used by
49037 ** the btree layer.
49038 */
49039 static MemPage *btreePageFromDbPage(DbPage *pDbPage, Pgno pgno, BtShared *pBt){
49040   MemPage *pPage = (MemPage*)sqlite3PagerGetExtra(pDbPage);
49041   pPage->aData = sqlite3PagerGetData(pDbPage);
49042   pPage->pDbPage = pDbPage;
49043   pPage->pBt = pBt;
49044   pPage->pgno = pgno;
49045   pPage->hdrOffset = pPage->pgno==1 ? 100 : 0;
49046   return pPage;
49047 }
49048 
49049 /*
49050 ** Get a page from the pager.  Initialize the MemPage.pBt and
49051 ** MemPage.aData elements if needed.
49052 **
49053 ** If the noContent flag is set, it means that we do not care about
49054 ** the content of the page at this time.  So do not go to the disk
49055 ** to fetch the content.  Just fill in the content with zeros for now.
49056 ** If in the future we call sqlite3PagerWrite() on this page, that
49057 ** means we have started to be concerned about content and the disk
49058 ** read should occur at that point.
49059 */
49060 static int btreeGetPage(
49061   BtShared *pBt,       /* The btree */
49062   Pgno pgno,           /* Number of the page to fetch */
49063   MemPage **ppPage,    /* Return the page in this parameter */
49064   int noContent        /* Do not load page content if true */
49065 ){
49066   int rc;
49067   DbPage *pDbPage;
49068 
49069   assert( sqlite3_mutex_held(pBt->mutex) );
49070   rc = sqlite3PagerAcquire(pBt->pPager, pgno, (DbPage**)&pDbPage, noContent);
49071   if( rc ) return rc;
49072   *ppPage = btreePageFromDbPage(pDbPage, pgno, pBt);
49073   return SQLITE_OK;
49074 }
49075 
49076 /*
49077 ** Retrieve a page from the pager cache. If the requested page is not
49078 ** already in the pager cache return NULL. Initialize the MemPage.pBt and
49079 ** MemPage.aData elements if needed.
49080 */
49081 static MemPage *btreePageLookup(BtShared *pBt, Pgno pgno){
49082   DbPage *pDbPage;
49083   assert( sqlite3_mutex_held(pBt->mutex) );
49084   pDbPage = sqlite3PagerLookup(pBt->pPager, pgno);
49085   if( pDbPage ){
49086     return btreePageFromDbPage(pDbPage, pgno, pBt);
49087   }
49088   return 0;
49089 }
49090 
49091 /*
49092 ** Return the size of the database file in pages. If there is any kind of
49093 ** error, return ((unsigned int)-1).
49094 */
49095 static Pgno btreePagecount(BtShared *pBt){
49096   return pBt->nPage;
49097 }
49098 SQLITE_PRIVATE u32 sqlite3BtreeLastPage(Btree *p){
49099   assert( sqlite3BtreeHoldsMutex(p) );
49100   assert( ((p->pBt->nPage)&0x8000000)==0 );
49101   return (int)btreePagecount(p->pBt);
49102 }
49103 
49104 /*
49105 ** Get a page from the pager and initialize it.  This routine is just a
49106 ** convenience wrapper around separate calls to btreeGetPage() and
49107 ** btreeInitPage().
49108 **
49109 ** If an error occurs, then the value *ppPage is set to is undefined. It
49110 ** may remain unchanged, or it may be set to an invalid value.
49111 */
49112 static int getAndInitPage(
49113   BtShared *pBt,          /* The database file */
49114   Pgno pgno,           /* Number of the page to get */
49115   MemPage **ppPage     /* Write the page pointer here */
49116 ){
49117   int rc;
49118   assert( sqlite3_mutex_held(pBt->mutex) );
49119 
49120   if( pgno>btreePagecount(pBt) ){
49121     rc = SQLITE_CORRUPT_BKPT;
49122   }else{
49123     rc = btreeGetPage(pBt, pgno, ppPage, 0);
49124     if( rc==SQLITE_OK ){
49125       rc = btreeInitPage(*ppPage);
49126       if( rc!=SQLITE_OK ){
49127         releasePage(*ppPage);
49128       }
49129     }
49130   }
49131 
49132   testcase( pgno==0 );
49133   assert( pgno!=0 || rc==SQLITE_CORRUPT );
49134   return rc;
49135 }
49136 
49137 /*
49138 ** Release a MemPage.  This should be called once for each prior
49139 ** call to btreeGetPage.
49140 */
49141 static void releasePage(MemPage *pPage){
49142   if( pPage ){
49143     assert( pPage->aData );
49144     assert( pPage->pBt );
49145     assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
49146     assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData );
49147     assert( sqlite3_mutex_held(pPage->pBt->mutex) );
49148     sqlite3PagerUnref(pPage->pDbPage);
49149   }
49150 }
49151 
49152 /*
49153 ** During a rollback, when the pager reloads information into the cache
49154 ** so that the cache is restored to its original state at the start of
49155 ** the transaction, for each page restored this routine is called.
49156 **
49157 ** This routine needs to reset the extra data section at the end of the
49158 ** page to agree with the restored data.
49159 */
49160 static void pageReinit(DbPage *pData){
49161   MemPage *pPage;
49162   pPage = (MemPage *)sqlite3PagerGetExtra(pData);
49163   assert( sqlite3PagerPageRefcount(pData)>0 );
49164   if( pPage->isInit ){
49165     assert( sqlite3_mutex_held(pPage->pBt->mutex) );
49166     pPage->isInit = 0;
49167     if( sqlite3PagerPageRefcount(pData)>1 ){
49168       /* pPage might not be a btree page;  it might be an overflow page
49169       ** or ptrmap page or a free page.  In those cases, the following
49170       ** call to btreeInitPage() will likely return SQLITE_CORRUPT.
49171       ** But no harm is done by this.  And it is very important that
49172       ** btreeInitPage() be called on every btree page so we make
49173       ** the call for every page that comes in for re-initing. */
49174       btreeInitPage(pPage);
49175     }
49176   }
49177 }
49178 
49179 /*
49180 ** Invoke the busy handler for a btree.
49181 */
49182 static int btreeInvokeBusyHandler(void *pArg){
49183   BtShared *pBt = (BtShared*)pArg;
49184   assert( pBt->db );
49185   assert( sqlite3_mutex_held(pBt->db->mutex) );
49186   return sqlite3InvokeBusyHandler(&pBt->db->busyHandler);
49187 }
49188 
49189 /*
49190 ** Open a database file.
49191 **
49192 ** zFilename is the name of the database file.  If zFilename is NULL
49193 ** then an ephemeral database is created.  The ephemeral database might
49194 ** be exclusively in memory, or it might use a disk-based memory cache.
49195 ** Either way, the ephemeral database will be automatically deleted
49196 ** when sqlite3BtreeClose() is called.
49197 **
49198 ** If zFilename is ":memory:" then an in-memory database is created
49199 ** that is automatically destroyed when it is closed.
49200 **
49201 ** The "flags" parameter is a bitmask that might contain bits
49202 ** BTREE_OMIT_JOURNAL and/or BTREE_NO_READLOCK.  The BTREE_NO_READLOCK
49203 ** bit is also set if the SQLITE_NoReadlock flags is set in db->flags.
49204 ** These flags are passed through into sqlite3PagerOpen() and must
49205 ** be the same values as PAGER_OMIT_JOURNAL and PAGER_NO_READLOCK.
49206 **
49207 ** If the database is already opened in the same database connection
49208 ** and we are in shared cache mode, then the open will fail with an
49209 ** SQLITE_CONSTRAINT error.  We cannot allow two or more BtShared
49210 ** objects in the same database connection since doing so will lead
49211 ** to problems with locking.
49212 */
49213 SQLITE_PRIVATE int sqlite3BtreeOpen(
49214   sqlite3_vfs *pVfs,      /* VFS to use for this b-tree */
49215   const char *zFilename,  /* Name of the file containing the BTree database */
49216   sqlite3 *db,            /* Associated database handle */
49217   Btree **ppBtree,        /* Pointer to new Btree object written here */
49218   int flags,              /* Options */
49219   int vfsFlags            /* Flags passed through to sqlite3_vfs.xOpen() */
49220 ){
49221   BtShared *pBt = 0;             /* Shared part of btree structure */
49222   Btree *p;                      /* Handle to return */
49223   sqlite3_mutex *mutexOpen = 0;  /* Prevents a race condition. Ticket #3537 */
49224   int rc = SQLITE_OK;            /* Result code from this function */
49225   u8 nReserve;                   /* Byte of unused space on each page */
49226   unsigned char zDbHeader[100];  /* Database header content */
49227 
49228   /* True if opening an ephemeral, temporary database */
49229   const int isTempDb = zFilename==0 || zFilename[0]==0;
49230 
49231   /* Set the variable isMemdb to true for an in-memory database, or
49232   ** false for a file-based database.
49233   */
49234 #ifdef SQLITE_OMIT_MEMORYDB
49235   const int isMemdb = 0;
49236 #else
49237   const int isMemdb = (zFilename && strcmp(zFilename, ":memory:")==0)
49238                        || (isTempDb && sqlite3TempInMemory(db));
49239 #endif
49240 
49241   assert( db!=0 );
49242   assert( pVfs!=0 );
49243   assert( sqlite3_mutex_held(db->mutex) );
49244   assert( (flags&0xff)==flags );   /* flags fit in 8 bits */
49245 
49246   /* Only a BTREE_SINGLE database can be BTREE_UNORDERED */
49247   assert( (flags & BTREE_UNORDERED)==0 || (flags & BTREE_SINGLE)!=0 );
49248 
49249   /* A BTREE_SINGLE database is always a temporary and/or ephemeral */
49250   assert( (flags & BTREE_SINGLE)==0 || isTempDb );
49251 
49252   if( db->flags & SQLITE_NoReadlock ){
49253     flags |= BTREE_NO_READLOCK;
49254   }
49255   if( isMemdb ){
49256     flags |= BTREE_MEMORY;
49257   }
49258   if( (vfsFlags & SQLITE_OPEN_MAIN_DB)!=0 && (isMemdb || isTempDb) ){
49259     vfsFlags = (vfsFlags & ~SQLITE_OPEN_MAIN_DB) | SQLITE_OPEN_TEMP_DB;
49260   }
49261   p = sqlite3MallocZero(sizeof(Btree));
49262   if( !p ){
49263     return SQLITE_NOMEM;
49264   }
49265   p->inTrans = TRANS_NONE;
49266   p->db = db;
49267 #ifndef SQLITE_OMIT_SHARED_CACHE
49268   p->lock.pBtree = p;
49269   p->lock.iTable = 1;
49270 #endif
49271 
49272 #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
49273   /*
49274   ** If this Btree is a candidate for shared cache, try to find an
49275   ** existing BtShared object that we can share with
49276   */
49277   if( isMemdb==0 && isTempDb==0 ){
49278     if( vfsFlags & SQLITE_OPEN_SHAREDCACHE ){
49279       int nFullPathname = pVfs->mxPathname+1;
49280       char *zFullPathname = sqlite3Malloc(nFullPathname);
49281       sqlite3_mutex *mutexShared;
49282       p->sharable = 1;
49283       if( !zFullPathname ){
49284         sqlite3_free(p);
49285         return SQLITE_NOMEM;
49286       }
49287       sqlite3OsFullPathname(pVfs, zFilename, nFullPathname, zFullPathname);
49288       mutexOpen = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_OPEN);
49289       sqlite3_mutex_enter(mutexOpen);
49290       mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
49291       sqlite3_mutex_enter(mutexShared);
49292       for(pBt=GLOBAL(BtShared*,sqlite3SharedCacheList); pBt; pBt=pBt->pNext){
49293         assert( pBt->nRef>0 );
49294         if( 0==strcmp(zFullPathname, sqlite3PagerFilename(pBt->pPager))
49295                  && sqlite3PagerVfs(pBt->pPager)==pVfs ){
49296           int iDb;
49297           for(iDb=db->nDb-1; iDb>=0; iDb--){
49298             Btree *pExisting = db->aDb[iDb].pBt;
49299             if( pExisting && pExisting->pBt==pBt ){
49300               sqlite3_mutex_leave(mutexShared);
49301               sqlite3_mutex_leave(mutexOpen);
49302               sqlite3_free(zFullPathname);
49303               sqlite3_free(p);
49304               return SQLITE_CONSTRAINT;
49305             }
49306           }
49307           p->pBt = pBt;
49308           pBt->nRef++;
49309           break;
49310         }
49311       }
49312       sqlite3_mutex_leave(mutexShared);
49313       sqlite3_free(zFullPathname);
49314     }
49315 #ifdef SQLITE_DEBUG
49316     else{
49317       /* In debug mode, we mark all persistent databases as sharable
49318       ** even when they are not.  This exercises the locking code and
49319       ** gives more opportunity for asserts(sqlite3_mutex_held())
49320       ** statements to find locking problems.
49321       */
49322       p->sharable = 1;
49323     }
49324 #endif
49325   }
49326 #endif
49327   if( pBt==0 ){
49328     /*
49329     ** The following asserts make sure that structures used by the btree are
49330     ** the right size.  This is to guard against size changes that result
49331     ** when compiling on a different architecture.
49332     */
49333     assert( sizeof(i64)==8 || sizeof(i64)==4 );
49334     assert( sizeof(u64)==8 || sizeof(u64)==4 );
49335     assert( sizeof(u32)==4 );
49336     assert( sizeof(u16)==2 );
49337     assert( sizeof(Pgno)==4 );
49338 
49339     pBt = sqlite3MallocZero( sizeof(*pBt) );
49340     if( pBt==0 ){
49341       rc = SQLITE_NOMEM;
49342       goto btree_open_out;
49343     }
49344     rc = sqlite3PagerOpen(pVfs, &pBt->pPager, zFilename,
49345                           EXTRA_SIZE, flags, vfsFlags, pageReinit);
49346     if( rc==SQLITE_OK ){
49347       rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader);
49348     }
49349     if( rc!=SQLITE_OK ){
49350       goto btree_open_out;
49351     }
49352     pBt->openFlags = (u8)flags;
49353     pBt->db = db;
49354     sqlite3PagerSetBusyhandler(pBt->pPager, btreeInvokeBusyHandler, pBt);
49355     p->pBt = pBt;
49356 
49357     pBt->pCursor = 0;
49358     pBt->pPage1 = 0;
49359     pBt->readOnly = sqlite3PagerIsreadonly(pBt->pPager);
49360 #ifdef SQLITE_SECURE_DELETE
49361     pBt->secureDelete = 1;
49362 #endif
49363     pBt->pageSize = (zDbHeader[16]<<8) | (zDbHeader[17]<<16);
49364     if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE
49365          || ((pBt->pageSize-1)&pBt->pageSize)!=0 ){
49366       pBt->pageSize = 0;
49367 #ifndef SQLITE_OMIT_AUTOVACUUM
49368       /* If the magic name ":memory:" will create an in-memory database, then
49369       ** leave the autoVacuum mode at 0 (do not auto-vacuum), even if
49370       ** SQLITE_DEFAULT_AUTOVACUUM is true. On the other hand, if
49371       ** SQLITE_OMIT_MEMORYDB has been defined, then ":memory:" is just a
49372       ** regular file-name. In this case the auto-vacuum applies as per normal.
49373       */
49374       if( zFilename && !isMemdb ){
49375         pBt->autoVacuum = (SQLITE_DEFAULT_AUTOVACUUM ? 1 : 0);
49376         pBt->incrVacuum = (SQLITE_DEFAULT_AUTOVACUUM==2 ? 1 : 0);
49377       }
49378 #endif
49379       nReserve = 0;
49380     }else{
49381       nReserve = zDbHeader[20];
49382       pBt->pageSizeFixed = 1;
49383 #ifndef SQLITE_OMIT_AUTOVACUUM
49384       pBt->autoVacuum = (get4byte(&zDbHeader[36 + 4*4])?1:0);
49385       pBt->incrVacuum = (get4byte(&zDbHeader[36 + 7*4])?1:0);
49386 #endif
49387     }
49388     rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
49389     if( rc ) goto btree_open_out;
49390     pBt->usableSize = pBt->pageSize - nReserve;
49391     assert( (pBt->pageSize & 7)==0 );  /* 8-byte alignment of pageSize */
49392 
49393 #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
49394     /* Add the new BtShared object to the linked list sharable BtShareds.
49395     */
49396     if( p->sharable ){
49397       sqlite3_mutex *mutexShared;
49398       pBt->nRef = 1;
49399       mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
49400       if( SQLITE_THREADSAFE && sqlite3GlobalConfig.bCoreMutex ){
49401         pBt->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_FAST);
49402         if( pBt->mutex==0 ){
49403           rc = SQLITE_NOMEM;
49404           db->mallocFailed = 0;
49405           goto btree_open_out;
49406         }
49407       }
49408       sqlite3_mutex_enter(mutexShared);
49409       pBt->pNext = GLOBAL(BtShared*,sqlite3SharedCacheList);
49410       GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt;
49411       sqlite3_mutex_leave(mutexShared);
49412     }
49413 #endif
49414   }
49415 
49416 #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
49417   /* If the new Btree uses a sharable pBtShared, then link the new
49418   ** Btree into the list of all sharable Btrees for the same connection.
49419   ** The list is kept in ascending order by pBt address.
49420   */
49421   if( p->sharable ){
49422     int i;
49423     Btree *pSib;
49424     for(i=0; i<db->nDb; i++){
49425       if( (pSib = db->aDb[i].pBt)!=0 && pSib->sharable ){
49426         while( pSib->pPrev ){ pSib = pSib->pPrev; }
49427         if( p->pBt<pSib->pBt ){
49428           p->pNext = pSib;
49429           p->pPrev = 0;
49430           pSib->pPrev = p;
49431         }else{
49432           while( pSib->pNext && pSib->pNext->pBt<p->pBt ){
49433             pSib = pSib->pNext;
49434           }
49435           p->pNext = pSib->pNext;
49436           p->pPrev = pSib;
49437           if( p->pNext ){
49438             p->pNext->pPrev = p;
49439           }
49440           pSib->pNext = p;
49441         }
49442         break;
49443       }
49444     }
49445   }
49446 #endif
49447   *ppBtree = p;
49448 
49449 btree_open_out:
49450   if( rc!=SQLITE_OK ){
49451     if( pBt && pBt->pPager ){
49452       sqlite3PagerClose(pBt->pPager);
49453     }
49454     sqlite3_free(pBt);
49455     sqlite3_free(p);
49456     *ppBtree = 0;
49457   }else{
49458     /* If the B-Tree was successfully opened, set the pager-cache size to the
49459     ** default value. Except, when opening on an existing shared pager-cache,
49460     ** do not change the pager-cache size.
49461     */
49462     if( sqlite3BtreeSchema(p, 0, 0)==0 ){
49463       sqlite3PagerSetCachesize(p->pBt->pPager, SQLITE_DEFAULT_CACHE_SIZE);
49464     }
49465   }
49466   if( mutexOpen ){
49467     assert( sqlite3_mutex_held(mutexOpen) );
49468     sqlite3_mutex_leave(mutexOpen);
49469   }
49470   return rc;
49471 }
49472 
49473 /*
49474 ** Decrement the BtShared.nRef counter.  When it reaches zero,
49475 ** remove the BtShared structure from the sharing list.  Return
49476 ** true if the BtShared.nRef counter reaches zero and return
49477 ** false if it is still positive.
49478 */
49479 static int removeFromSharingList(BtShared *pBt){
49480 #ifndef SQLITE_OMIT_SHARED_CACHE
49481   sqlite3_mutex *pMaster;
49482   BtShared *pList;
49483   int removed = 0;
49484 
49485   assert( sqlite3_mutex_notheld(pBt->mutex) );
49486   pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
49487   sqlite3_mutex_enter(pMaster);
49488   pBt->nRef--;
49489   if( pBt->nRef<=0 ){
49490     if( GLOBAL(BtShared*,sqlite3SharedCacheList)==pBt ){
49491       GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt->pNext;
49492     }else{
49493       pList = GLOBAL(BtShared*,sqlite3SharedCacheList);
49494       while( ALWAYS(pList) && pList->pNext!=pBt ){
49495         pList=pList->pNext;
49496       }
49497       if( ALWAYS(pList) ){
49498         pList->pNext = pBt->pNext;
49499       }
49500     }
49501     if( SQLITE_THREADSAFE ){
49502       sqlite3_mutex_free(pBt->mutex);
49503     }
49504     removed = 1;
49505   }
49506   sqlite3_mutex_leave(pMaster);
49507   return removed;
49508 #else
49509   return 1;
49510 #endif
49511 }
49512 
49513 /*
49514 ** Make sure pBt->pTmpSpace points to an allocation of
49515 ** MX_CELL_SIZE(pBt) bytes.
49516 */
49517 static void allocateTempSpace(BtShared *pBt){
49518   if( !pBt->pTmpSpace ){
49519     pBt->pTmpSpace = sqlite3PageMalloc( pBt->pageSize );
49520   }
49521 }
49522 
49523 /*
49524 ** Free the pBt->pTmpSpace allocation
49525 */
49526 static void freeTempSpace(BtShared *pBt){
49527   sqlite3PageFree( pBt->pTmpSpace);
49528   pBt->pTmpSpace = 0;
49529 }
49530 
49531 /*
49532 ** Close an open database and invalidate all cursors.
49533 */
49534 SQLITE_PRIVATE int sqlite3BtreeClose(Btree *p){
49535   BtShared *pBt = p->pBt;
49536   BtCursor *pCur;
49537 
49538   /* Close all cursors opened via this handle.  */
49539   assert( sqlite3_mutex_held(p->db->mutex) );
49540   sqlite3BtreeEnter(p);
49541   pCur = pBt->pCursor;
49542   while( pCur ){
49543     BtCursor *pTmp = pCur;
49544     pCur = pCur->pNext;
49545     if( pTmp->pBtree==p ){
49546       sqlite3BtreeCloseCursor(pTmp);
49547     }
49548   }
49549 
49550   /* Rollback any active transaction and free the handle structure.
49551   ** The call to sqlite3BtreeRollback() drops any table-locks held by
49552   ** this handle.
49553   */
49554   sqlite3BtreeRollback(p);
49555   sqlite3BtreeLeave(p);
49556 
49557   /* If there are still other outstanding references to the shared-btree
49558   ** structure, return now. The remainder of this procedure cleans
49559   ** up the shared-btree.
49560   */
49561   assert( p->wantToLock==0 && p->locked==0 );
49562   if( !p->sharable || removeFromSharingList(pBt) ){
49563     /* The pBt is no longer on the sharing list, so we can access
49564     ** it without having to hold the mutex.
49565     **
49566     ** Clean out and delete the BtShared object.
49567     */
49568     assert( !pBt->pCursor );
49569     sqlite3PagerClose(pBt->pPager);
49570     if( pBt->xFreeSchema && pBt->pSchema ){
49571       pBt->xFreeSchema(pBt->pSchema);
49572     }
49573     sqlite3DbFree(0, pBt->pSchema);
49574     freeTempSpace(pBt);
49575     sqlite3_free(pBt);
49576   }
49577 
49578 #ifndef SQLITE_OMIT_SHARED_CACHE
49579   assert( p->wantToLock==0 );
49580   assert( p->locked==0 );
49581   if( p->pPrev ) p->pPrev->pNext = p->pNext;
49582   if( p->pNext ) p->pNext->pPrev = p->pPrev;
49583 #endif
49584 
49585   sqlite3_free(p);
49586   return SQLITE_OK;
49587 }
49588 
49589 /*
49590 ** Change the limit on the number of pages allowed in the cache.
49591 **
49592 ** The maximum number of cache pages is set to the absolute
49593 ** value of mxPage.  If mxPage is negative, the pager will
49594 ** operate asynchronously - it will not stop to do fsync()s
49595 ** to insure data is written to the disk surface before
49596 ** continuing.  Transactions still work if synchronous is off,
49597 ** and the database cannot be corrupted if this program
49598 ** crashes.  But if the operating system crashes or there is
49599 ** an abrupt power failure when synchronous is off, the database
49600 ** could be left in an inconsistent and unrecoverable state.
49601 ** Synchronous is on by default so database corruption is not
49602 ** normally a worry.
49603 */
49604 SQLITE_PRIVATE int sqlite3BtreeSetCacheSize(Btree *p, int mxPage){
49605   BtShared *pBt = p->pBt;
49606   assert( sqlite3_mutex_held(p->db->mutex) );
49607   sqlite3BtreeEnter(p);
49608   sqlite3PagerSetCachesize(pBt->pPager, mxPage);
49609   sqlite3BtreeLeave(p);
49610   return SQLITE_OK;
49611 }
49612 
49613 /*
49614 ** Change the way data is synced to disk in order to increase or decrease
49615 ** how well the database resists damage due to OS crashes and power
49616 ** failures.  Level 1 is the same as asynchronous (no syncs() occur and
49617 ** there is a high probability of damage)  Level 2 is the default.  There
49618 ** is a very low but non-zero probability of damage.  Level 3 reduces the
49619 ** probability of damage to near zero but with a write performance reduction.
49620 */
49621 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
49622 SQLITE_PRIVATE int sqlite3BtreeSetSafetyLevel(
49623   Btree *p,              /* The btree to set the safety level on */
49624   int level,             /* PRAGMA synchronous.  1=OFF, 2=NORMAL, 3=FULL */
49625   int fullSync,          /* PRAGMA fullfsync. */
49626   int ckptFullSync       /* PRAGMA checkpoint_fullfync */
49627 ){
49628   BtShared *pBt = p->pBt;
49629   assert( sqlite3_mutex_held(p->db->mutex) );
49630   assert( level>=1 && level<=3 );
49631   sqlite3BtreeEnter(p);
49632   sqlite3PagerSetSafetyLevel(pBt->pPager, level, fullSync, ckptFullSync);
49633   sqlite3BtreeLeave(p);
49634   return SQLITE_OK;
49635 }
49636 #endif
49637 
49638 /*
49639 ** Return TRUE if the given btree is set to safety level 1.  In other
49640 ** words, return TRUE if no sync() occurs on the disk files.
49641 */
49642 SQLITE_PRIVATE int sqlite3BtreeSyncDisabled(Btree *p){
49643   BtShared *pBt = p->pBt;
49644   int rc;
49645   assert( sqlite3_mutex_held(p->db->mutex) );
49646   sqlite3BtreeEnter(p);
49647   assert( pBt && pBt->pPager );
49648   rc = sqlite3PagerNosync(pBt->pPager);
49649   sqlite3BtreeLeave(p);
49650   return rc;
49651 }
49652 
49653 /*
49654 ** Change the default pages size and the number of reserved bytes per page.
49655 ** Or, if the page size has already been fixed, return SQLITE_READONLY
49656 ** without changing anything.
49657 **
49658 ** The page size must be a power of 2 between 512 and 65536.  If the page
49659 ** size supplied does not meet this constraint then the page size is not
49660 ** changed.
49661 **
49662 ** Page sizes are constrained to be a power of two so that the region
49663 ** of the database file used for locking (beginning at PENDING_BYTE,
49664 ** the first byte past the 1GB boundary, 0x40000000) needs to occur
49665 ** at the beginning of a page.
49666 **
49667 ** If parameter nReserve is less than zero, then the number of reserved
49668 ** bytes per page is left unchanged.
49669 **
49670 ** If the iFix!=0 then the pageSizeFixed flag is set so that the page size
49671 ** and autovacuum mode can no longer be changed.
49672 */
49673 SQLITE_PRIVATE int sqlite3BtreeSetPageSize(Btree *p, int pageSize, int nReserve, int iFix){
49674   int rc = SQLITE_OK;
49675   BtShared *pBt = p->pBt;
49676   assert( nReserve>=-1 && nReserve<=255 );
49677   sqlite3BtreeEnter(p);
49678   if( pBt->pageSizeFixed ){
49679     sqlite3BtreeLeave(p);
49680     return SQLITE_READONLY;
49681   }
49682   if( nReserve<0 ){
49683     nReserve = pBt->pageSize - pBt->usableSize;
49684   }
49685   assert( nReserve>=0 && nReserve<=255 );
49686   if( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE &&
49687         ((pageSize-1)&pageSize)==0 ){
49688     assert( (pageSize & 7)==0 );
49689     assert( !pBt->pPage1 && !pBt->pCursor );
49690     pBt->pageSize = (u32)pageSize;
49691     freeTempSpace(pBt);
49692   }
49693   rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
49694   pBt->usableSize = pBt->pageSize - (u16)nReserve;
49695   if( iFix ) pBt->pageSizeFixed = 1;
49696   sqlite3BtreeLeave(p);
49697   return rc;
49698 }
49699 
49700 /*
49701 ** Return the currently defined page size
49702 */
49703 SQLITE_PRIVATE int sqlite3BtreeGetPageSize(Btree *p){
49704   return p->pBt->pageSize;
49705 }
49706 
49707 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM)
49708 /*
49709 ** Return the number of bytes of space at the end of every page that
49710 ** are intentually left unused.  This is the "reserved" space that is
49711 ** sometimes used by extensions.
49712 */
49713 SQLITE_PRIVATE int sqlite3BtreeGetReserve(Btree *p){
49714   int n;
49715   sqlite3BtreeEnter(p);
49716   n = p->pBt->pageSize - p->pBt->usableSize;
49717   sqlite3BtreeLeave(p);
49718   return n;
49719 }
49720 
49721 /*
49722 ** Set the maximum page count for a database if mxPage is positive.
49723 ** No changes are made if mxPage is 0 or negative.
49724 ** Regardless of the value of mxPage, return the maximum page count.
49725 */
49726 SQLITE_PRIVATE int sqlite3BtreeMaxPageCount(Btree *p, int mxPage){
49727   int n;
49728   sqlite3BtreeEnter(p);
49729   n = sqlite3PagerMaxPageCount(p->pBt->pPager, mxPage);
49730   sqlite3BtreeLeave(p);
49731   return n;
49732 }
49733 
49734 /*
49735 ** Set the secureDelete flag if newFlag is 0 or 1.  If newFlag is -1,
49736 ** then make no changes.  Always return the value of the secureDelete
49737 ** setting after the change.
49738 */
49739 SQLITE_PRIVATE int sqlite3BtreeSecureDelete(Btree *p, int newFlag){
49740   int b;
49741   if( p==0 ) return 0;
49742   sqlite3BtreeEnter(p);
49743   if( newFlag>=0 ){
49744     p->pBt->secureDelete = (newFlag!=0) ? 1 : 0;
49745   }
49746   b = p->pBt->secureDelete;
49747   sqlite3BtreeLeave(p);
49748   return b;
49749 }
49750 #endif /* !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM) */
49751 
49752 /*
49753 ** Change the 'auto-vacuum' property of the database. If the 'autoVacuum'
49754 ** parameter is non-zero, then auto-vacuum mode is enabled. If zero, it
49755 ** is disabled. The default value for the auto-vacuum property is
49756 ** determined by the SQLITE_DEFAULT_AUTOVACUUM macro.
49757 */
49758 SQLITE_PRIVATE int sqlite3BtreeSetAutoVacuum(Btree *p, int autoVacuum){
49759 #ifdef SQLITE_OMIT_AUTOVACUUM
49760   return SQLITE_READONLY;
49761 #else
49762   BtShared *pBt = p->pBt;
49763   int rc = SQLITE_OK;
49764   u8 av = (u8)autoVacuum;
49765 
49766   sqlite3BtreeEnter(p);
49767   if( pBt->pageSizeFixed && (av ?1:0)!=pBt->autoVacuum ){
49768     rc = SQLITE_READONLY;
49769   }else{
49770     pBt->autoVacuum = av ?1:0;
49771     pBt->incrVacuum = av==2 ?1:0;
49772   }
49773   sqlite3BtreeLeave(p);
49774   return rc;
49775 #endif
49776 }
49777 
49778 /*
49779 ** Return the value of the 'auto-vacuum' property. If auto-vacuum is
49780 ** enabled 1 is returned. Otherwise 0.
49781 */
49782 SQLITE_PRIVATE int sqlite3BtreeGetAutoVacuum(Btree *p){
49783 #ifdef SQLITE_OMIT_AUTOVACUUM
49784   return BTREE_AUTOVACUUM_NONE;
49785 #else
49786   int rc;
49787   sqlite3BtreeEnter(p);
49788   rc = (
49789     (!p->pBt->autoVacuum)?BTREE_AUTOVACUUM_NONE:
49790     (!p->pBt->incrVacuum)?BTREE_AUTOVACUUM_FULL:
49791     BTREE_AUTOVACUUM_INCR
49792   );
49793   sqlite3BtreeLeave(p);
49794   return rc;
49795 #endif
49796 }
49797 
49798 
49799 /*
49800 ** Get a reference to pPage1 of the database file.  This will
49801 ** also acquire a readlock on that file.
49802 **
49803 ** SQLITE_OK is returned on success.  If the file is not a
49804 ** well-formed database file, then SQLITE_CORRUPT is returned.
49805 ** SQLITE_BUSY is returned if the database is locked.  SQLITE_NOMEM
49806 ** is returned if we run out of memory.
49807 */
49808 static int lockBtree(BtShared *pBt){
49809   int rc;              /* Result code from subfunctions */
49810   MemPage *pPage1;     /* Page 1 of the database file */
49811   int nPage;           /* Number of pages in the database */
49812   int nPageFile = 0;   /* Number of pages in the database file */
49813   int nPageHeader;     /* Number of pages in the database according to hdr */
49814 
49815   assert( sqlite3_mutex_held(pBt->mutex) );
49816   assert( pBt->pPage1==0 );
49817   rc = sqlite3PagerSharedLock(pBt->pPager);
49818   if( rc!=SQLITE_OK ) return rc;
49819   rc = btreeGetPage(pBt, 1, &pPage1, 0);
49820   if( rc!=SQLITE_OK ) return rc;
49821 
49822   /* Do some checking to help insure the file we opened really is
49823   ** a valid database file.
49824   */
49825   nPage = nPageHeader = get4byte(28+(u8*)pPage1->aData);
49826   sqlite3PagerPagecount(pBt->pPager, &nPageFile);
49827   if( nPage==0 || memcmp(24+(u8*)pPage1->aData, 92+(u8*)pPage1->aData,4)!=0 ){
49828     nPage = nPageFile;
49829   }
49830   if( nPage>0 ){
49831     u32 pageSize;
49832     u32 usableSize;
49833     u8 *page1 = pPage1->aData;
49834     rc = SQLITE_NOTADB;
49835     if( memcmp(page1, zMagicHeader, 16)!=0 ){
49836       goto page1_init_failed;
49837     }
49838 
49839 #ifdef SQLITE_OMIT_WAL
49840     if( page1[18]>1 ){
49841       pBt->readOnly = 1;
49842     }
49843     if( page1[19]>1 ){
49844       goto page1_init_failed;
49845     }
49846 #else
49847     if( page1[18]>2 ){
49848       pBt->readOnly = 1;
49849     }
49850     if( page1[19]>2 ){
49851       goto page1_init_failed;
49852     }
49853 
49854     /* If the write version is set to 2, this database should be accessed
49855     ** in WAL mode. If the log is not already open, open it now. Then
49856     ** return SQLITE_OK and return without populating BtShared.pPage1.
49857     ** The caller detects this and calls this function again. This is
49858     ** required as the version of page 1 currently in the page1 buffer
49859     ** may not be the latest version - there may be a newer one in the log
49860     ** file.
49861     */
49862     if( page1[19]==2 && pBt->doNotUseWAL==0 ){
49863       int isOpen = 0;
49864       rc = sqlite3PagerOpenWal(pBt->pPager, &isOpen);
49865       if( rc!=SQLITE_OK ){
49866         goto page1_init_failed;
49867       }else if( isOpen==0 ){
49868         releasePage(pPage1);
49869         return SQLITE_OK;
49870       }
49871       rc = SQLITE_NOTADB;
49872     }
49873 #endif
49874 
49875     /* The maximum embedded fraction must be exactly 25%.  And the minimum
49876     ** embedded fraction must be 12.5% for both leaf-data and non-leaf-data.
49877     ** The original design allowed these amounts to vary, but as of
49878     ** version 3.6.0, we require them to be fixed.
49879     */
49880     if( memcmp(&page1[21], "\100\040\040",3)!=0 ){
49881       goto page1_init_failed;
49882     }
49883     pageSize = (page1[16]<<8) | (page1[17]<<16);
49884     if( ((pageSize-1)&pageSize)!=0
49885      || pageSize>SQLITE_MAX_PAGE_SIZE
49886      || pageSize<=256
49887     ){
49888       goto page1_init_failed;
49889     }
49890     assert( (pageSize & 7)==0 );
49891     usableSize = pageSize - page1[20];
49892     if( (u32)pageSize!=pBt->pageSize ){
49893       /* After reading the first page of the database assuming a page size
49894       ** of BtShared.pageSize, we have discovered that the page-size is
49895       ** actually pageSize. Unlock the database, leave pBt->pPage1 at
49896       ** zero and return SQLITE_OK. The caller will call this function
49897       ** again with the correct page-size.
49898       */
49899       releasePage(pPage1);
49900       pBt->usableSize = usableSize;
49901       pBt->pageSize = pageSize;
49902       freeTempSpace(pBt);
49903       rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize,
49904                                    pageSize-usableSize);
49905       return rc;
49906     }
49907     if( (pBt->db->flags & SQLITE_RecoveryMode)==0 && nPage>nPageFile ){
49908       rc = SQLITE_CORRUPT_BKPT;
49909       goto page1_init_failed;
49910     }
49911     if( usableSize<480 ){
49912       goto page1_init_failed;
49913     }
49914     pBt->pageSize = pageSize;
49915     pBt->usableSize = usableSize;
49916 #ifndef SQLITE_OMIT_AUTOVACUUM
49917     pBt->autoVacuum = (get4byte(&page1[36 + 4*4])?1:0);
49918     pBt->incrVacuum = (get4byte(&page1[36 + 7*4])?1:0);
49919 #endif
49920   }
49921 
49922   /* maxLocal is the maximum amount of payload to store locally for
49923   ** a cell.  Make sure it is small enough so that at least minFanout
49924   ** cells can will fit on one page.  We assume a 10-byte page header.
49925   ** Besides the payload, the cell must store:
49926   **     2-byte pointer to the cell
49927   **     4-byte child pointer
49928   **     9-byte nKey value
49929   **     4-byte nData value
49930   **     4-byte overflow page pointer
49931   ** So a cell consists of a 2-byte pointer, a header which is as much as
49932   ** 17 bytes long, 0 to N bytes of payload, and an optional 4 byte overflow
49933   ** page pointer.
49934   */
49935   pBt->maxLocal = (u16)((pBt->usableSize-12)*64/255 - 23);
49936   pBt->minLocal = (u16)((pBt->usableSize-12)*32/255 - 23);
49937   pBt->maxLeaf = (u16)(pBt->usableSize - 35);
49938   pBt->minLeaf = (u16)((pBt->usableSize-12)*32/255 - 23);
49939   assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) );
49940   pBt->pPage1 = pPage1;
49941   pBt->nPage = nPage;
49942   return SQLITE_OK;
49943 
49944 page1_init_failed:
49945   releasePage(pPage1);
49946   pBt->pPage1 = 0;
49947   return rc;
49948 }
49949 
49950 /*
49951 ** If there are no outstanding cursors and we are not in the middle
49952 ** of a transaction but there is a read lock on the database, then
49953 ** this routine unrefs the first page of the database file which
49954 ** has the effect of releasing the read lock.
49955 **
49956 ** If there is a transaction in progress, this routine is a no-op.
49957 */
49958 static void unlockBtreeIfUnused(BtShared *pBt){
49959   assert( sqlite3_mutex_held(pBt->mutex) );
49960   assert( pBt->pCursor==0 || pBt->inTransaction>TRANS_NONE );
49961   if( pBt->inTransaction==TRANS_NONE && pBt->pPage1!=0 ){
49962     assert( pBt->pPage1->aData );
49963     assert( sqlite3PagerRefcount(pBt->pPager)==1 );
49964     assert( pBt->pPage1->aData );
49965     releasePage(pBt->pPage1);
49966     pBt->pPage1 = 0;
49967   }
49968 }
49969 
49970 /*
49971 ** If pBt points to an empty file then convert that empty file
49972 ** into a new empty database by initializing the first page of
49973 ** the database.
49974 */
49975 static int newDatabase(BtShared *pBt){
49976   MemPage *pP1;
49977   unsigned char *data;
49978   int rc;
49979 
49980   assert( sqlite3_mutex_held(pBt->mutex) );
49981   if( pBt->nPage>0 ){
49982     return SQLITE_OK;
49983   }
49984   pP1 = pBt->pPage1;
49985   assert( pP1!=0 );
49986   data = pP1->aData;
49987   rc = sqlite3PagerWrite(pP1->pDbPage);
49988   if( rc ) return rc;
49989   memcpy(data, zMagicHeader, sizeof(zMagicHeader));
49990   assert( sizeof(zMagicHeader)==16 );
49991   data[16] = (u8)((pBt->pageSize>>8)&0xff);
49992   data[17] = (u8)((pBt->pageSize>>16)&0xff);
49993   data[18] = 1;
49994   data[19] = 1;
49995   assert( pBt->usableSize<=pBt->pageSize && pBt->usableSize+255>=pBt->pageSize);
49996   data[20] = (u8)(pBt->pageSize - pBt->usableSize);
49997   data[21] = 64;
49998   data[22] = 32;
49999   data[23] = 32;
50000   memset(&data[24], 0, 100-24);
50001   zeroPage(pP1, PTF_INTKEY|PTF_LEAF|PTF_LEAFDATA );
50002   pBt->pageSizeFixed = 1;
50003 #ifndef SQLITE_OMIT_AUTOVACUUM
50004   assert( pBt->autoVacuum==1 || pBt->autoVacuum==0 );
50005   assert( pBt->incrVacuum==1 || pBt->incrVacuum==0 );
50006   put4byte(&data[36 + 4*4], pBt->autoVacuum);
50007   put4byte(&data[36 + 7*4], pBt->incrVacuum);
50008 #endif
50009   pBt->nPage = 1;
50010   data[31] = 1;
50011   return SQLITE_OK;
50012 }
50013 
50014 /*
50015 ** Attempt to start a new transaction. A write-transaction
50016 ** is started if the second argument is nonzero, otherwise a read-
50017 ** transaction.  If the second argument is 2 or more and exclusive
50018 ** transaction is started, meaning that no other process is allowed
50019 ** to access the database.  A preexisting transaction may not be
50020 ** upgraded to exclusive by calling this routine a second time - the
50021 ** exclusivity flag only works for a new transaction.
50022 **
50023 ** A write-transaction must be started before attempting any
50024 ** changes to the database.  None of the following routines
50025 ** will work unless a transaction is started first:
50026 **
50027 **      sqlite3BtreeCreateTable()
50028 **      sqlite3BtreeCreateIndex()
50029 **      sqlite3BtreeClearTable()
50030 **      sqlite3BtreeDropTable()
50031 **      sqlite3BtreeInsert()
50032 **      sqlite3BtreeDelete()
50033 **      sqlite3BtreeUpdateMeta()
50034 **
50035 ** If an initial attempt to acquire the lock fails because of lock contention
50036 ** and the database was previously unlocked, then invoke the busy handler
50037 ** if there is one.  But if there was previously a read-lock, do not
50038 ** invoke the busy handler - just return SQLITE_BUSY.  SQLITE_BUSY is
50039 ** returned when there is already a read-lock in order to avoid a deadlock.
50040 **
50041 ** Suppose there are two processes A and B.  A has a read lock and B has
50042 ** a reserved lock.  B tries to promote to exclusive but is blocked because
50043 ** of A's read lock.  A tries to promote to reserved but is blocked by B.
50044 ** One or the other of the two processes must give way or there can be
50045 ** no progress.  By returning SQLITE_BUSY and not invoking the busy callback
50046 ** when A already has a read lock, we encourage A to give up and let B
50047 ** proceed.
50048 */
50049 SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree *p, int wrflag){
50050   sqlite3 *pBlock = 0;
50051   BtShared *pBt = p->pBt;
50052   int rc = SQLITE_OK;
50053 
50054   sqlite3BtreeEnter(p);
50055   btreeIntegrity(p);
50056 
50057   /* If the btree is already in a write-transaction, or it
50058   ** is already in a read-transaction and a read-transaction
50059   ** is requested, this is a no-op.
50060   */
50061   if( p->inTrans==TRANS_WRITE || (p->inTrans==TRANS_READ && !wrflag) ){
50062     goto trans_begun;
50063   }
50064 
50065   /* Write transactions are not possible on a read-only database */
50066   if( pBt->readOnly && wrflag ){
50067     rc = SQLITE_READONLY;
50068     goto trans_begun;
50069   }
50070 
50071 #ifndef SQLITE_OMIT_SHARED_CACHE
50072   /* If another database handle has already opened a write transaction
50073   ** on this shared-btree structure and a second write transaction is
50074   ** requested, return SQLITE_LOCKED.
50075   */
50076   if( (wrflag && pBt->inTransaction==TRANS_WRITE) || pBt->isPending ){
50077     pBlock = pBt->pWriter->db;
50078   }else if( wrflag>1 ){
50079     BtLock *pIter;
50080     for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
50081       if( pIter->pBtree!=p ){
50082         pBlock = pIter->pBtree->db;
50083         break;
50084       }
50085     }
50086   }
50087   if( pBlock ){
50088     sqlite3ConnectionBlocked(p->db, pBlock);
50089     rc = SQLITE_LOCKED_SHAREDCACHE;
50090     goto trans_begun;
50091   }
50092 #endif
50093 
50094   /* Any read-only or read-write transaction implies a read-lock on
50095   ** page 1. So if some other shared-cache client already has a write-lock
50096   ** on page 1, the transaction cannot be opened. */
50097   rc = querySharedCacheTableLock(p, MASTER_ROOT, READ_LOCK);
50098   if( SQLITE_OK!=rc ) goto trans_begun;
50099 
50100   pBt->initiallyEmpty = (u8)(pBt->nPage==0);
50101   do {
50102     /* Call lockBtree() until either pBt->pPage1 is populated or
50103     ** lockBtree() returns something other than SQLITE_OK. lockBtree()
50104     ** may return SQLITE_OK but leave pBt->pPage1 set to 0 if after
50105     ** reading page 1 it discovers that the page-size of the database
50106     ** file is not pBt->pageSize. In this case lockBtree() will update
50107     ** pBt->pageSize to the page-size of the file on disk.
50108     */
50109     while( pBt->pPage1==0 && SQLITE_OK==(rc = lockBtree(pBt)) );
50110 
50111     if( rc==SQLITE_OK && wrflag ){
50112       if( pBt->readOnly ){
50113         rc = SQLITE_READONLY;
50114       }else{
50115         rc = sqlite3PagerBegin(pBt->pPager,wrflag>1,sqlite3TempInMemory(p->db));
50116         if( rc==SQLITE_OK ){
50117           rc = newDatabase(pBt);
50118         }
50119       }
50120     }
50121 
50122     if( rc!=SQLITE_OK ){
50123       unlockBtreeIfUnused(pBt);
50124     }
50125   }while( (rc&0xFF)==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE &&
50126           btreeInvokeBusyHandler(pBt) );
50127 
50128   if( rc==SQLITE_OK ){
50129     if( p->inTrans==TRANS_NONE ){
50130       pBt->nTransaction++;
50131 #ifndef SQLITE_OMIT_SHARED_CACHE
50132       if( p->sharable ){
50133 	assert( p->lock.pBtree==p && p->lock.iTable==1 );
50134         p->lock.eLock = READ_LOCK;
50135         p->lock.pNext = pBt->pLock;
50136         pBt->pLock = &p->lock;
50137       }
50138 #endif
50139     }
50140     p->inTrans = (wrflag?TRANS_WRITE:TRANS_READ);
50141     if( p->inTrans>pBt->inTransaction ){
50142       pBt->inTransaction = p->inTrans;
50143     }
50144     if( wrflag ){
50145       MemPage *pPage1 = pBt->pPage1;
50146 #ifndef SQLITE_OMIT_SHARED_CACHE
50147       assert( !pBt->pWriter );
50148       pBt->pWriter = p;
50149       pBt->isExclusive = (u8)(wrflag>1);
50150 #endif
50151 
50152       /* If the db-size header field is incorrect (as it may be if an old
50153       ** client has been writing the database file), update it now. Doing
50154       ** this sooner rather than later means the database size can safely
50155       ** re-read the database size from page 1 if a savepoint or transaction
50156       ** rollback occurs within the transaction.
50157       */
50158       if( pBt->nPage!=get4byte(&pPage1->aData[28]) ){
50159         rc = sqlite3PagerWrite(pPage1->pDbPage);
50160         if( rc==SQLITE_OK ){
50161           put4byte(&pPage1->aData[28], pBt->nPage);
50162         }
50163       }
50164     }
50165   }
50166 
50167 
50168 trans_begun:
50169   if( rc==SQLITE_OK && wrflag ){
50170     /* This call makes sure that the pager has the correct number of
50171     ** open savepoints. If the second parameter is greater than 0 and
50172     ** the sub-journal is not already open, then it will be opened here.
50173     */
50174     rc = sqlite3PagerOpenSavepoint(pBt->pPager, p->db->nSavepoint);
50175   }
50176 
50177   btreeIntegrity(p);
50178   sqlite3BtreeLeave(p);
50179   return rc;
50180 }
50181 
50182 #ifndef SQLITE_OMIT_AUTOVACUUM
50183 
50184 /*
50185 ** Set the pointer-map entries for all children of page pPage. Also, if
50186 ** pPage contains cells that point to overflow pages, set the pointer
50187 ** map entries for the overflow pages as well.
50188 */
50189 static int setChildPtrmaps(MemPage *pPage){
50190   int i;                             /* Counter variable */
50191   int nCell;                         /* Number of cells in page pPage */
50192   int rc;                            /* Return code */
50193   BtShared *pBt = pPage->pBt;
50194   u8 isInitOrig = pPage->isInit;
50195   Pgno pgno = pPage->pgno;
50196 
50197   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
50198   rc = btreeInitPage(pPage);
50199   if( rc!=SQLITE_OK ){
50200     goto set_child_ptrmaps_out;
50201   }
50202   nCell = pPage->nCell;
50203 
50204   for(i=0; i<nCell; i++){
50205     u8 *pCell = findCell(pPage, i);
50206 
50207     ptrmapPutOvflPtr(pPage, pCell, &rc);
50208 
50209     if( !pPage->leaf ){
50210       Pgno childPgno = get4byte(pCell);
50211       ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno, &rc);
50212     }
50213   }
50214 
50215   if( !pPage->leaf ){
50216     Pgno childPgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
50217     ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno, &rc);
50218   }
50219 
50220 set_child_ptrmaps_out:
50221   pPage->isInit = isInitOrig;
50222   return rc;
50223 }
50224 
50225 /*
50226 ** Somewhere on pPage is a pointer to page iFrom.  Modify this pointer so
50227 ** that it points to iTo. Parameter eType describes the type of pointer to
50228 ** be modified, as  follows:
50229 **
50230 ** PTRMAP_BTREE:     pPage is a btree-page. The pointer points at a child
50231 **                   page of pPage.
50232 **
50233 ** PTRMAP_OVERFLOW1: pPage is a btree-page. The pointer points at an overflow
50234 **                   page pointed to by one of the cells on pPage.
50235 **
50236 ** PTRMAP_OVERFLOW2: pPage is an overflow-page. The pointer points at the next
50237 **                   overflow page in the list.
50238 */
50239 static int modifyPagePointer(MemPage *pPage, Pgno iFrom, Pgno iTo, u8 eType){
50240   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
50241   assert( sqlite3PagerIswriteable(pPage->pDbPage) );
50242   if( eType==PTRMAP_OVERFLOW2 ){
50243     /* The pointer is always the first 4 bytes of the page in this case.  */
50244     if( get4byte(pPage->aData)!=iFrom ){
50245       return SQLITE_CORRUPT_BKPT;
50246     }
50247     put4byte(pPage->aData, iTo);
50248   }else{
50249     u8 isInitOrig = pPage->isInit;
50250     int i;
50251     int nCell;
50252 
50253     btreeInitPage(pPage);
50254     nCell = pPage->nCell;
50255 
50256     for(i=0; i<nCell; i++){
50257       u8 *pCell = findCell(pPage, i);
50258       if( eType==PTRMAP_OVERFLOW1 ){
50259         CellInfo info;
50260         btreeParseCellPtr(pPage, pCell, &info);
50261         if( info.iOverflow ){
50262           if( iFrom==get4byte(&pCell[info.iOverflow]) ){
50263             put4byte(&pCell[info.iOverflow], iTo);
50264             break;
50265           }
50266         }
50267       }else{
50268         if( get4byte(pCell)==iFrom ){
50269           put4byte(pCell, iTo);
50270           break;
50271         }
50272       }
50273     }
50274 
50275     if( i==nCell ){
50276       if( eType!=PTRMAP_BTREE ||
50277           get4byte(&pPage->aData[pPage->hdrOffset+8])!=iFrom ){
50278         return SQLITE_CORRUPT_BKPT;
50279       }
50280       put4byte(&pPage->aData[pPage->hdrOffset+8], iTo);
50281     }
50282 
50283     pPage->isInit = isInitOrig;
50284   }
50285   return SQLITE_OK;
50286 }
50287 
50288 
50289 /*
50290 ** Move the open database page pDbPage to location iFreePage in the
50291 ** database. The pDbPage reference remains valid.
50292 **
50293 ** The isCommit flag indicates that there is no need to remember that
50294 ** the journal needs to be sync()ed before database page pDbPage->pgno
50295 ** can be written to. The caller has already promised not to write to that
50296 ** page.
50297 */
50298 static int relocatePage(
50299   BtShared *pBt,           /* Btree */
50300   MemPage *pDbPage,        /* Open page to move */
50301   u8 eType,                /* Pointer map 'type' entry for pDbPage */
50302   Pgno iPtrPage,           /* Pointer map 'page-no' entry for pDbPage */
50303   Pgno iFreePage,          /* The location to move pDbPage to */
50304   int isCommit             /* isCommit flag passed to sqlite3PagerMovepage */
50305 ){
50306   MemPage *pPtrPage;   /* The page that contains a pointer to pDbPage */
50307   Pgno iDbPage = pDbPage->pgno;
50308   Pager *pPager = pBt->pPager;
50309   int rc;
50310 
50311   assert( eType==PTRMAP_OVERFLOW2 || eType==PTRMAP_OVERFLOW1 ||
50312       eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE );
50313   assert( sqlite3_mutex_held(pBt->mutex) );
50314   assert( pDbPage->pBt==pBt );
50315 
50316   /* Move page iDbPage from its current location to page number iFreePage */
50317   TRACE(("AUTOVACUUM: Moving %d to free page %d (ptr page %d type %d)\n",
50318       iDbPage, iFreePage, iPtrPage, eType));
50319   rc = sqlite3PagerMovepage(pPager, pDbPage->pDbPage, iFreePage, isCommit);
50320   if( rc!=SQLITE_OK ){
50321     return rc;
50322   }
50323   pDbPage->pgno = iFreePage;
50324 
50325   /* If pDbPage was a btree-page, then it may have child pages and/or cells
50326   ** that point to overflow pages. The pointer map entries for all these
50327   ** pages need to be changed.
50328   **
50329   ** If pDbPage is an overflow page, then the first 4 bytes may store a
50330   ** pointer to a subsequent overflow page. If this is the case, then
50331   ** the pointer map needs to be updated for the subsequent overflow page.
50332   */
50333   if( eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE ){
50334     rc = setChildPtrmaps(pDbPage);
50335     if( rc!=SQLITE_OK ){
50336       return rc;
50337     }
50338   }else{
50339     Pgno nextOvfl = get4byte(pDbPage->aData);
50340     if( nextOvfl!=0 ){
50341       ptrmapPut(pBt, nextOvfl, PTRMAP_OVERFLOW2, iFreePage, &rc);
50342       if( rc!=SQLITE_OK ){
50343         return rc;
50344       }
50345     }
50346   }
50347 
50348   /* Fix the database pointer on page iPtrPage that pointed at iDbPage so
50349   ** that it points at iFreePage. Also fix the pointer map entry for
50350   ** iPtrPage.
50351   */
50352   if( eType!=PTRMAP_ROOTPAGE ){
50353     rc = btreeGetPage(pBt, iPtrPage, &pPtrPage, 0);
50354     if( rc!=SQLITE_OK ){
50355       return rc;
50356     }
50357     rc = sqlite3PagerWrite(pPtrPage->pDbPage);
50358     if( rc!=SQLITE_OK ){
50359       releasePage(pPtrPage);
50360       return rc;
50361     }
50362     rc = modifyPagePointer(pPtrPage, iDbPage, iFreePage, eType);
50363     releasePage(pPtrPage);
50364     if( rc==SQLITE_OK ){
50365       ptrmapPut(pBt, iFreePage, eType, iPtrPage, &rc);
50366     }
50367   }
50368   return rc;
50369 }
50370 
50371 /* Forward declaration required by incrVacuumStep(). */
50372 static int allocateBtreePage(BtShared *, MemPage **, Pgno *, Pgno, u8);
50373 
50374 /*
50375 ** Perform a single step of an incremental-vacuum. If successful,
50376 ** return SQLITE_OK. If there is no work to do (and therefore no
50377 ** point in calling this function again), return SQLITE_DONE.
50378 **
50379 ** More specificly, this function attempts to re-organize the
50380 ** database so that the last page of the file currently in use
50381 ** is no longer in use.
50382 **
50383 ** If the nFin parameter is non-zero, this function assumes
50384 ** that the caller will keep calling incrVacuumStep() until
50385 ** it returns SQLITE_DONE or an error, and that nFin is the
50386 ** number of pages the database file will contain after this
50387 ** process is complete.  If nFin is zero, it is assumed that
50388 ** incrVacuumStep() will be called a finite amount of times
50389 ** which may or may not empty the freelist.  A full autovacuum
50390 ** has nFin>0.  A "PRAGMA incremental_vacuum" has nFin==0.
50391 */
50392 static int incrVacuumStep(BtShared *pBt, Pgno nFin, Pgno iLastPg){
50393   Pgno nFreeList;           /* Number of pages still on the free-list */
50394   int rc;
50395 
50396   assert( sqlite3_mutex_held(pBt->mutex) );
50397   assert( iLastPg>nFin );
50398 
50399   if( !PTRMAP_ISPAGE(pBt, iLastPg) && iLastPg!=PENDING_BYTE_PAGE(pBt) ){
50400     u8 eType;
50401     Pgno iPtrPage;
50402 
50403     nFreeList = get4byte(&pBt->pPage1->aData[36]);
50404     if( nFreeList==0 ){
50405       return SQLITE_DONE;
50406     }
50407 
50408     rc = ptrmapGet(pBt, iLastPg, &eType, &iPtrPage);
50409     if( rc!=SQLITE_OK ){
50410       return rc;
50411     }
50412     if( eType==PTRMAP_ROOTPAGE ){
50413       return SQLITE_CORRUPT_BKPT;
50414     }
50415 
50416     if( eType==PTRMAP_FREEPAGE ){
50417       if( nFin==0 ){
50418         /* Remove the page from the files free-list. This is not required
50419         ** if nFin is non-zero. In that case, the free-list will be
50420         ** truncated to zero after this function returns, so it doesn't
50421         ** matter if it still contains some garbage entries.
50422         */
50423         Pgno iFreePg;
50424         MemPage *pFreePg;
50425         rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, iLastPg, 1);
50426         if( rc!=SQLITE_OK ){
50427           return rc;
50428         }
50429         assert( iFreePg==iLastPg );
50430         releasePage(pFreePg);
50431       }
50432     } else {
50433       Pgno iFreePg;             /* Index of free page to move pLastPg to */
50434       MemPage *pLastPg;
50435 
50436       rc = btreeGetPage(pBt, iLastPg, &pLastPg, 0);
50437       if( rc!=SQLITE_OK ){
50438         return rc;
50439       }
50440 
50441       /* If nFin is zero, this loop runs exactly once and page pLastPg
50442       ** is swapped with the first free page pulled off the free list.
50443       **
50444       ** On the other hand, if nFin is greater than zero, then keep
50445       ** looping until a free-page located within the first nFin pages
50446       ** of the file is found.
50447       */
50448       do {
50449         MemPage *pFreePg;
50450         rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, 0, 0);
50451         if( rc!=SQLITE_OK ){
50452           releasePage(pLastPg);
50453           return rc;
50454         }
50455         releasePage(pFreePg);
50456       }while( nFin!=0 && iFreePg>nFin );
50457       assert( iFreePg<iLastPg );
50458 
50459       rc = sqlite3PagerWrite(pLastPg->pDbPage);
50460       if( rc==SQLITE_OK ){
50461         rc = relocatePage(pBt, pLastPg, eType, iPtrPage, iFreePg, nFin!=0);
50462       }
50463       releasePage(pLastPg);
50464       if( rc!=SQLITE_OK ){
50465         return rc;
50466       }
50467     }
50468   }
50469 
50470   if( nFin==0 ){
50471     iLastPg--;
50472     while( iLastPg==PENDING_BYTE_PAGE(pBt)||PTRMAP_ISPAGE(pBt, iLastPg) ){
50473       if( PTRMAP_ISPAGE(pBt, iLastPg) ){
50474         MemPage *pPg;
50475         rc = btreeGetPage(pBt, iLastPg, &pPg, 0);
50476         if( rc!=SQLITE_OK ){
50477           return rc;
50478         }
50479         rc = sqlite3PagerWrite(pPg->pDbPage);
50480         releasePage(pPg);
50481         if( rc!=SQLITE_OK ){
50482           return rc;
50483         }
50484       }
50485       iLastPg--;
50486     }
50487     sqlite3PagerTruncateImage(pBt->pPager, iLastPg);
50488     pBt->nPage = iLastPg;
50489   }
50490   return SQLITE_OK;
50491 }
50492 
50493 /*
50494 ** A write-transaction must be opened before calling this function.
50495 ** It performs a single unit of work towards an incremental vacuum.
50496 **
50497 ** If the incremental vacuum is finished after this function has run,
50498 ** SQLITE_DONE is returned. If it is not finished, but no error occurred,
50499 ** SQLITE_OK is returned. Otherwise an SQLite error code.
50500 */
50501 SQLITE_PRIVATE int sqlite3BtreeIncrVacuum(Btree *p){
50502   int rc;
50503   BtShared *pBt = p->pBt;
50504 
50505   sqlite3BtreeEnter(p);
50506   assert( pBt->inTransaction==TRANS_WRITE && p->inTrans==TRANS_WRITE );
50507   if( !pBt->autoVacuum ){
50508     rc = SQLITE_DONE;
50509   }else{
50510     invalidateAllOverflowCache(pBt);
50511     rc = incrVacuumStep(pBt, 0, btreePagecount(pBt));
50512     if( rc==SQLITE_OK ){
50513       rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
50514       put4byte(&pBt->pPage1->aData[28], pBt->nPage);
50515     }
50516   }
50517   sqlite3BtreeLeave(p);
50518   return rc;
50519 }
50520 
50521 /*
50522 ** This routine is called prior to sqlite3PagerCommit when a transaction
50523 ** is commited for an auto-vacuum database.
50524 **
50525 ** If SQLITE_OK is returned, then *pnTrunc is set to the number of pages
50526 ** the database file should be truncated to during the commit process.
50527 ** i.e. the database has been reorganized so that only the first *pnTrunc
50528 ** pages are in use.
50529 */
50530 static int autoVacuumCommit(BtShared *pBt){
50531   int rc = SQLITE_OK;
50532   Pager *pPager = pBt->pPager;
50533   VVA_ONLY( int nRef = sqlite3PagerRefcount(pPager) );
50534 
50535   assert( sqlite3_mutex_held(pBt->mutex) );
50536   invalidateAllOverflowCache(pBt);
50537   assert(pBt->autoVacuum);
50538   if( !pBt->incrVacuum ){
50539     Pgno nFin;         /* Number of pages in database after autovacuuming */
50540     Pgno nFree;        /* Number of pages on the freelist initially */
50541     Pgno nPtrmap;      /* Number of PtrMap pages to be freed */
50542     Pgno iFree;        /* The next page to be freed */
50543     int nEntry;        /* Number of entries on one ptrmap page */
50544     Pgno nOrig;        /* Database size before freeing */
50545 
50546     nOrig = btreePagecount(pBt);
50547     if( PTRMAP_ISPAGE(pBt, nOrig) || nOrig==PENDING_BYTE_PAGE(pBt) ){
50548       /* It is not possible to create a database for which the final page
50549       ** is either a pointer-map page or the pending-byte page. If one
50550       ** is encountered, this indicates corruption.
50551       */
50552       return SQLITE_CORRUPT_BKPT;
50553     }
50554 
50555     nFree = get4byte(&pBt->pPage1->aData[36]);
50556     nEntry = pBt->usableSize/5;
50557     nPtrmap = (nFree-nOrig+PTRMAP_PAGENO(pBt, nOrig)+nEntry)/nEntry;
50558     nFin = nOrig - nFree - nPtrmap;
50559     if( nOrig>PENDING_BYTE_PAGE(pBt) && nFin<PENDING_BYTE_PAGE(pBt) ){
50560       nFin--;
50561     }
50562     while( PTRMAP_ISPAGE(pBt, nFin) || nFin==PENDING_BYTE_PAGE(pBt) ){
50563       nFin--;
50564     }
50565     if( nFin>nOrig ) return SQLITE_CORRUPT_BKPT;
50566 
50567     for(iFree=nOrig; iFree>nFin && rc==SQLITE_OK; iFree--){
50568       rc = incrVacuumStep(pBt, nFin, iFree);
50569     }
50570     if( (rc==SQLITE_DONE || rc==SQLITE_OK) && nFree>0 ){
50571       rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
50572       put4byte(&pBt->pPage1->aData[32], 0);
50573       put4byte(&pBt->pPage1->aData[36], 0);
50574       put4byte(&pBt->pPage1->aData[28], nFin);
50575       sqlite3PagerTruncateImage(pBt->pPager, nFin);
50576       pBt->nPage = nFin;
50577     }
50578     if( rc!=SQLITE_OK ){
50579       sqlite3PagerRollback(pPager);
50580     }
50581   }
50582 
50583   assert( nRef==sqlite3PagerRefcount(pPager) );
50584   return rc;
50585 }
50586 
50587 #else /* ifndef SQLITE_OMIT_AUTOVACUUM */
50588 # define setChildPtrmaps(x) SQLITE_OK
50589 #endif
50590 
50591 /*
50592 ** This routine does the first phase of a two-phase commit.  This routine
50593 ** causes a rollback journal to be created (if it does not already exist)
50594 ** and populated with enough information so that if a power loss occurs
50595 ** the database can be restored to its original state by playing back
50596 ** the journal.  Then the contents of the journal are flushed out to
50597 ** the disk.  After the journal is safely on oxide, the changes to the
50598 ** database are written into the database file and flushed to oxide.
50599 ** At the end of this call, the rollback journal still exists on the
50600 ** disk and we are still holding all locks, so the transaction has not
50601 ** committed.  See sqlite3BtreeCommitPhaseTwo() for the second phase of the
50602 ** commit process.
50603 **
50604 ** This call is a no-op if no write-transaction is currently active on pBt.
50605 **
50606 ** Otherwise, sync the database file for the btree pBt. zMaster points to
50607 ** the name of a master journal file that should be written into the
50608 ** individual journal file, or is NULL, indicating no master journal file
50609 ** (single database transaction).
50610 **
50611 ** When this is called, the master journal should already have been
50612 ** created, populated with this journal pointer and synced to disk.
50613 **
50614 ** Once this is routine has returned, the only thing required to commit
50615 ** the write-transaction for this database file is to delete the journal.
50616 */
50617 SQLITE_PRIVATE int sqlite3BtreeCommitPhaseOne(Btree *p, const char *zMaster){
50618   int rc = SQLITE_OK;
50619   if( p->inTrans==TRANS_WRITE ){
50620     BtShared *pBt = p->pBt;
50621     sqlite3BtreeEnter(p);
50622 #ifndef SQLITE_OMIT_AUTOVACUUM
50623     if( pBt->autoVacuum ){
50624       rc = autoVacuumCommit(pBt);
50625       if( rc!=SQLITE_OK ){
50626         sqlite3BtreeLeave(p);
50627         return rc;
50628       }
50629     }
50630 #endif
50631     rc = sqlite3PagerCommitPhaseOne(pBt->pPager, zMaster, 0);
50632     sqlite3BtreeLeave(p);
50633   }
50634   return rc;
50635 }
50636 
50637 /*
50638 ** This function is called from both BtreeCommitPhaseTwo() and BtreeRollback()
50639 ** at the conclusion of a transaction.
50640 */
50641 static void btreeEndTransaction(Btree *p){
50642   BtShared *pBt = p->pBt;
50643   assert( sqlite3BtreeHoldsMutex(p) );
50644 
50645   btreeClearHasContent(pBt);
50646   if( p->inTrans>TRANS_NONE && p->db->activeVdbeCnt>1 ){
50647     /* If there are other active statements that belong to this database
50648     ** handle, downgrade to a read-only transaction. The other statements
50649     ** may still be reading from the database.  */
50650     downgradeAllSharedCacheTableLocks(p);
50651     p->inTrans = TRANS_READ;
50652   }else{
50653     /* If the handle had any kind of transaction open, decrement the
50654     ** transaction count of the shared btree. If the transaction count
50655     ** reaches 0, set the shared state to TRANS_NONE. The unlockBtreeIfUnused()
50656     ** call below will unlock the pager.  */
50657     if( p->inTrans!=TRANS_NONE ){
50658       clearAllSharedCacheTableLocks(p);
50659       pBt->nTransaction--;
50660       if( 0==pBt->nTransaction ){
50661         pBt->inTransaction = TRANS_NONE;
50662       }
50663     }
50664 
50665     /* Set the current transaction state to TRANS_NONE and unlock the
50666     ** pager if this call closed the only read or write transaction.  */
50667     p->inTrans = TRANS_NONE;
50668     unlockBtreeIfUnused(pBt);
50669   }
50670 
50671   btreeIntegrity(p);
50672 }
50673 
50674 /*
50675 ** Commit the transaction currently in progress.
50676 **
50677 ** This routine implements the second phase of a 2-phase commit.  The
50678 ** sqlite3BtreeCommitPhaseOne() routine does the first phase and should
50679 ** be invoked prior to calling this routine.  The sqlite3BtreeCommitPhaseOne()
50680 ** routine did all the work of writing information out to disk and flushing the
50681 ** contents so that they are written onto the disk platter.  All this
50682 ** routine has to do is delete or truncate or zero the header in the
50683 ** the rollback journal (which causes the transaction to commit) and
50684 ** drop locks.
50685 **
50686 ** Normally, if an error occurs while the pager layer is attempting to
50687 ** finalize the underlying journal file, this function returns an error and
50688 ** the upper layer will attempt a rollback. However, if the second argument
50689 ** is non-zero then this b-tree transaction is part of a multi-file
50690 ** transaction. In this case, the transaction has already been committed
50691 ** (by deleting a master journal file) and the caller will ignore this
50692 ** functions return code. So, even if an error occurs in the pager layer,
50693 ** reset the b-tree objects internal state to indicate that the write
50694 ** transaction has been closed. This is quite safe, as the pager will have
50695 ** transitioned to the error state.
50696 **
50697 ** This will release the write lock on the database file.  If there
50698 ** are no active cursors, it also releases the read lock.
50699 */
50700 SQLITE_PRIVATE int sqlite3BtreeCommitPhaseTwo(Btree *p, int bCleanup){
50701 
50702   if( p->inTrans==TRANS_NONE ) return SQLITE_OK;
50703   sqlite3BtreeEnter(p);
50704   btreeIntegrity(p);
50705 
50706   /* If the handle has a write-transaction open, commit the shared-btrees
50707   ** transaction and set the shared state to TRANS_READ.
50708   */
50709   if( p->inTrans==TRANS_WRITE ){
50710     int rc;
50711     BtShared *pBt = p->pBt;
50712     assert( pBt->inTransaction==TRANS_WRITE );
50713     assert( pBt->nTransaction>0 );
50714     rc = sqlite3PagerCommitPhaseTwo(pBt->pPager);
50715     if( rc!=SQLITE_OK && bCleanup==0 ){
50716       sqlite3BtreeLeave(p);
50717       return rc;
50718     }
50719     pBt->inTransaction = TRANS_READ;
50720   }
50721 
50722   btreeEndTransaction(p);
50723   sqlite3BtreeLeave(p);
50724   return SQLITE_OK;
50725 }
50726 
50727 /*
50728 ** Do both phases of a commit.
50729 */
50730 SQLITE_PRIVATE int sqlite3BtreeCommit(Btree *p){
50731   int rc;
50732   sqlite3BtreeEnter(p);
50733   rc = sqlite3BtreeCommitPhaseOne(p, 0);
50734   if( rc==SQLITE_OK ){
50735     rc = sqlite3BtreeCommitPhaseTwo(p, 0);
50736   }
50737   sqlite3BtreeLeave(p);
50738   return rc;
50739 }
50740 
50741 #ifndef NDEBUG
50742 /*
50743 ** Return the number of write-cursors open on this handle. This is for use
50744 ** in assert() expressions, so it is only compiled if NDEBUG is not
50745 ** defined.
50746 **
50747 ** For the purposes of this routine, a write-cursor is any cursor that
50748 ** is capable of writing to the databse.  That means the cursor was
50749 ** originally opened for writing and the cursor has not be disabled
50750 ** by having its state changed to CURSOR_FAULT.
50751 */
50752 static int countWriteCursors(BtShared *pBt){
50753   BtCursor *pCur;
50754   int r = 0;
50755   for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
50756     if( pCur->wrFlag && pCur->eState!=CURSOR_FAULT ) r++;
50757   }
50758   return r;
50759 }
50760 #endif
50761 
50762 /*
50763 ** This routine sets the state to CURSOR_FAULT and the error
50764 ** code to errCode for every cursor on BtShared that pBtree
50765 ** references.
50766 **
50767 ** Every cursor is tripped, including cursors that belong
50768 ** to other database connections that happen to be sharing
50769 ** the cache with pBtree.
50770 **
50771 ** This routine gets called when a rollback occurs.
50772 ** All cursors using the same cache must be tripped
50773 ** to prevent them from trying to use the btree after
50774 ** the rollback.  The rollback may have deleted tables
50775 ** or moved root pages, so it is not sufficient to
50776 ** save the state of the cursor.  The cursor must be
50777 ** invalidated.
50778 */
50779 SQLITE_PRIVATE void sqlite3BtreeTripAllCursors(Btree *pBtree, int errCode){
50780   BtCursor *p;
50781   sqlite3BtreeEnter(pBtree);
50782   for(p=pBtree->pBt->pCursor; p; p=p->pNext){
50783     int i;
50784     sqlite3BtreeClearCursor(p);
50785     p->eState = CURSOR_FAULT;
50786     p->skipNext = errCode;
50787     for(i=0; i<=p->iPage; i++){
50788       releasePage(p->apPage[i]);
50789       p->apPage[i] = 0;
50790     }
50791   }
50792   sqlite3BtreeLeave(pBtree);
50793 }
50794 
50795 /*
50796 ** Rollback the transaction in progress.  All cursors will be
50797 ** invalided by this operation.  Any attempt to use a cursor
50798 ** that was open at the beginning of this operation will result
50799 ** in an error.
50800 **
50801 ** This will release the write lock on the database file.  If there
50802 ** are no active cursors, it also releases the read lock.
50803 */
50804 SQLITE_PRIVATE int sqlite3BtreeRollback(Btree *p){
50805   int rc;
50806   BtShared *pBt = p->pBt;
50807   MemPage *pPage1;
50808 
50809   sqlite3BtreeEnter(p);
50810   rc = saveAllCursors(pBt, 0, 0);
50811 #ifndef SQLITE_OMIT_SHARED_CACHE
50812   if( rc!=SQLITE_OK ){
50813     /* This is a horrible situation. An IO or malloc() error occurred whilst
50814     ** trying to save cursor positions. If this is an automatic rollback (as
50815     ** the result of a constraint, malloc() failure or IO error) then
50816     ** the cache may be internally inconsistent (not contain valid trees) so
50817     ** we cannot simply return the error to the caller. Instead, abort
50818     ** all queries that may be using any of the cursors that failed to save.
50819     */
50820     sqlite3BtreeTripAllCursors(p, rc);
50821   }
50822 #endif
50823   btreeIntegrity(p);
50824 
50825   if( p->inTrans==TRANS_WRITE ){
50826     int rc2;
50827 
50828     assert( TRANS_WRITE==pBt->inTransaction );
50829     rc2 = sqlite3PagerRollback(pBt->pPager);
50830     if( rc2!=SQLITE_OK ){
50831       rc = rc2;
50832     }
50833 
50834     /* The rollback may have destroyed the pPage1->aData value.  So
50835     ** call btreeGetPage() on page 1 again to make
50836     ** sure pPage1->aData is set correctly. */
50837     if( btreeGetPage(pBt, 1, &pPage1, 0)==SQLITE_OK ){
50838       int nPage = get4byte(28+(u8*)pPage1->aData);
50839       testcase( nPage==0 );
50840       if( nPage==0 ) sqlite3PagerPagecount(pBt->pPager, &nPage);
50841       testcase( pBt->nPage!=nPage );
50842       pBt->nPage = nPage;
50843       releasePage(pPage1);
50844     }
50845     assert( countWriteCursors(pBt)==0 );
50846     pBt->inTransaction = TRANS_READ;
50847   }
50848 
50849   btreeEndTransaction(p);
50850   sqlite3BtreeLeave(p);
50851   return rc;
50852 }
50853 
50854 /*
50855 ** Start a statement subtransaction. The subtransaction can can be rolled
50856 ** back independently of the main transaction. You must start a transaction
50857 ** before starting a subtransaction. The subtransaction is ended automatically
50858 ** if the main transaction commits or rolls back.
50859 **
50860 ** Statement subtransactions are used around individual SQL statements
50861 ** that are contained within a BEGIN...COMMIT block.  If a constraint
50862 ** error occurs within the statement, the effect of that one statement
50863 ** can be rolled back without having to rollback the entire transaction.
50864 **
50865 ** A statement sub-transaction is implemented as an anonymous savepoint. The
50866 ** value passed as the second parameter is the total number of savepoints,
50867 ** including the new anonymous savepoint, open on the B-Tree. i.e. if there
50868 ** are no active savepoints and no other statement-transactions open,
50869 ** iStatement is 1. This anonymous savepoint can be released or rolled back
50870 ** using the sqlite3BtreeSavepoint() function.
50871 */
50872 SQLITE_PRIVATE int sqlite3BtreeBeginStmt(Btree *p, int iStatement){
50873   int rc;
50874   BtShared *pBt = p->pBt;
50875   sqlite3BtreeEnter(p);
50876   assert( p->inTrans==TRANS_WRITE );
50877   assert( pBt->readOnly==0 );
50878   assert( iStatement>0 );
50879   assert( iStatement>p->db->nSavepoint );
50880   assert( pBt->inTransaction==TRANS_WRITE );
50881   /* At the pager level, a statement transaction is a savepoint with
50882   ** an index greater than all savepoints created explicitly using
50883   ** SQL statements. It is illegal to open, release or rollback any
50884   ** such savepoints while the statement transaction savepoint is active.
50885   */
50886   rc = sqlite3PagerOpenSavepoint(pBt->pPager, iStatement);
50887   sqlite3BtreeLeave(p);
50888   return rc;
50889 }
50890 
50891 /*
50892 ** The second argument to this function, op, is always SAVEPOINT_ROLLBACK
50893 ** or SAVEPOINT_RELEASE. This function either releases or rolls back the
50894 ** savepoint identified by parameter iSavepoint, depending on the value
50895 ** of op.
50896 **
50897 ** Normally, iSavepoint is greater than or equal to zero. However, if op is
50898 ** SAVEPOINT_ROLLBACK, then iSavepoint may also be -1. In this case the
50899 ** contents of the entire transaction are rolled back. This is different
50900 ** from a normal transaction rollback, as no locks are released and the
50901 ** transaction remains open.
50902 */
50903 SQLITE_PRIVATE int sqlite3BtreeSavepoint(Btree *p, int op, int iSavepoint){
50904   int rc = SQLITE_OK;
50905   if( p && p->inTrans==TRANS_WRITE ){
50906     BtShared *pBt = p->pBt;
50907     assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK );
50908     assert( iSavepoint>=0 || (iSavepoint==-1 && op==SAVEPOINT_ROLLBACK) );
50909     sqlite3BtreeEnter(p);
50910     rc = sqlite3PagerSavepoint(pBt->pPager, op, iSavepoint);
50911     if( rc==SQLITE_OK ){
50912       if( iSavepoint<0 && pBt->initiallyEmpty ) pBt->nPage = 0;
50913       rc = newDatabase(pBt);
50914       pBt->nPage = get4byte(28 + pBt->pPage1->aData);
50915 
50916       /* The database size was written into the offset 28 of the header
50917       ** when the transaction started, so we know that the value at offset
50918       ** 28 is nonzero. */
50919       assert( pBt->nPage>0 );
50920     }
50921     sqlite3BtreeLeave(p);
50922   }
50923   return rc;
50924 }
50925 
50926 /*
50927 ** Create a new cursor for the BTree whose root is on the page
50928 ** iTable. If a read-only cursor is requested, it is assumed that
50929 ** the caller already has at least a read-only transaction open
50930 ** on the database already. If a write-cursor is requested, then
50931 ** the caller is assumed to have an open write transaction.
50932 **
50933 ** If wrFlag==0, then the cursor can only be used for reading.
50934 ** If wrFlag==1, then the cursor can be used for reading or for
50935 ** writing if other conditions for writing are also met.  These
50936 ** are the conditions that must be met in order for writing to
50937 ** be allowed:
50938 **
50939 ** 1:  The cursor must have been opened with wrFlag==1
50940 **
50941 ** 2:  Other database connections that share the same pager cache
50942 **     but which are not in the READ_UNCOMMITTED state may not have
50943 **     cursors open with wrFlag==0 on the same table.  Otherwise
50944 **     the changes made by this write cursor would be visible to
50945 **     the read cursors in the other database connection.
50946 **
50947 ** 3:  The database must be writable (not on read-only media)
50948 **
50949 ** 4:  There must be an active transaction.
50950 **
50951 ** No checking is done to make sure that page iTable really is the
50952 ** root page of a b-tree.  If it is not, then the cursor acquired
50953 ** will not work correctly.
50954 **
50955 ** It is assumed that the sqlite3BtreeCursorZero() has been called
50956 ** on pCur to initialize the memory space prior to invoking this routine.
50957 */
50958 static int btreeCursor(
50959   Btree *p,                              /* The btree */
50960   int iTable,                            /* Root page of table to open */
50961   int wrFlag,                            /* 1 to write. 0 read-only */
50962   struct KeyInfo *pKeyInfo,              /* First arg to comparison function */
50963   BtCursor *pCur                         /* Space for new cursor */
50964 ){
50965   BtShared *pBt = p->pBt;                /* Shared b-tree handle */
50966 
50967   assert( sqlite3BtreeHoldsMutex(p) );
50968   assert( wrFlag==0 || wrFlag==1 );
50969 
50970   /* The following assert statements verify that if this is a sharable
50971   ** b-tree database, the connection is holding the required table locks,
50972   ** and that no other connection has any open cursor that conflicts with
50973   ** this lock.  */
50974   assert( hasSharedCacheTableLock(p, iTable, pKeyInfo!=0, wrFlag+1) );
50975   assert( wrFlag==0 || !hasReadConflicts(p, iTable) );
50976 
50977   /* Assert that the caller has opened the required transaction. */
50978   assert( p->inTrans>TRANS_NONE );
50979   assert( wrFlag==0 || p->inTrans==TRANS_WRITE );
50980   assert( pBt->pPage1 && pBt->pPage1->aData );
50981 
50982   if( NEVER(wrFlag && pBt->readOnly) ){
50983     return SQLITE_READONLY;
50984   }
50985   if( iTable==1 && btreePagecount(pBt)==0 ){
50986     return SQLITE_EMPTY;
50987   }
50988 
50989   /* Now that no other errors can occur, finish filling in the BtCursor
50990   ** variables and link the cursor into the BtShared list.  */
50991   pCur->pgnoRoot = (Pgno)iTable;
50992   pCur->iPage = -1;
50993   pCur->pKeyInfo = pKeyInfo;
50994   pCur->pBtree = p;
50995   pCur->pBt = pBt;
50996   pCur->wrFlag = (u8)wrFlag;
50997   pCur->pNext = pBt->pCursor;
50998   if( pCur->pNext ){
50999     pCur->pNext->pPrev = pCur;
51000   }
51001   pBt->pCursor = pCur;
51002   pCur->eState = CURSOR_INVALID;
51003   pCur->cachedRowid = 0;
51004   return SQLITE_OK;
51005 }
51006 SQLITE_PRIVATE int sqlite3BtreeCursor(
51007   Btree *p,                                   /* The btree */
51008   int iTable,                                 /* Root page of table to open */
51009   int wrFlag,                                 /* 1 to write. 0 read-only */
51010   struct KeyInfo *pKeyInfo,                   /* First arg to xCompare() */
51011   BtCursor *pCur                              /* Write new cursor here */
51012 ){
51013   int rc;
51014   sqlite3BtreeEnter(p);
51015   rc = btreeCursor(p, iTable, wrFlag, pKeyInfo, pCur);
51016   sqlite3BtreeLeave(p);
51017   return rc;
51018 }
51019 
51020 /*
51021 ** Return the size of a BtCursor object in bytes.
51022 **
51023 ** This interfaces is needed so that users of cursors can preallocate
51024 ** sufficient storage to hold a cursor.  The BtCursor object is opaque
51025 ** to users so they cannot do the sizeof() themselves - they must call
51026 ** this routine.
51027 */
51028 SQLITE_PRIVATE int sqlite3BtreeCursorSize(void){
51029   return ROUND8(sizeof(BtCursor));
51030 }
51031 
51032 /*
51033 ** Initialize memory that will be converted into a BtCursor object.
51034 **
51035 ** The simple approach here would be to memset() the entire object
51036 ** to zero.  But it turns out that the apPage[] and aiIdx[] arrays
51037 ** do not need to be zeroed and they are large, so we can save a lot
51038 ** of run-time by skipping the initialization of those elements.
51039 */
51040 SQLITE_PRIVATE void sqlite3BtreeCursorZero(BtCursor *p){
51041   memset(p, 0, offsetof(BtCursor, iPage));
51042 }
51043 
51044 /*
51045 ** Set the cached rowid value of every cursor in the same database file
51046 ** as pCur and having the same root page number as pCur.  The value is
51047 ** set to iRowid.
51048 **
51049 ** Only positive rowid values are considered valid for this cache.
51050 ** The cache is initialized to zero, indicating an invalid cache.
51051 ** A btree will work fine with zero or negative rowids.  We just cannot
51052 ** cache zero or negative rowids, which means tables that use zero or
51053 ** negative rowids might run a little slower.  But in practice, zero
51054 ** or negative rowids are very uncommon so this should not be a problem.
51055 */
51056 SQLITE_PRIVATE void sqlite3BtreeSetCachedRowid(BtCursor *pCur, sqlite3_int64 iRowid){
51057   BtCursor *p;
51058   for(p=pCur->pBt->pCursor; p; p=p->pNext){
51059     if( p->pgnoRoot==pCur->pgnoRoot ) p->cachedRowid = iRowid;
51060   }
51061   assert( pCur->cachedRowid==iRowid );
51062 }
51063 
51064 /*
51065 ** Return the cached rowid for the given cursor.  A negative or zero
51066 ** return value indicates that the rowid cache is invalid and should be
51067 ** ignored.  If the rowid cache has never before been set, then a
51068 ** zero is returned.
51069 */
51070 SQLITE_PRIVATE sqlite3_int64 sqlite3BtreeGetCachedRowid(BtCursor *pCur){
51071   return pCur->cachedRowid;
51072 }
51073 
51074 /*
51075 ** Close a cursor.  The read lock on the database file is released
51076 ** when the last cursor is closed.
51077 */
51078 SQLITE_PRIVATE int sqlite3BtreeCloseCursor(BtCursor *pCur){
51079   Btree *pBtree = pCur->pBtree;
51080   if( pBtree ){
51081     int i;
51082     BtShared *pBt = pCur->pBt;
51083     sqlite3BtreeEnter(pBtree);
51084     sqlite3BtreeClearCursor(pCur);
51085     if( pCur->pPrev ){
51086       pCur->pPrev->pNext = pCur->pNext;
51087     }else{
51088       pBt->pCursor = pCur->pNext;
51089     }
51090     if( pCur->pNext ){
51091       pCur->pNext->pPrev = pCur->pPrev;
51092     }
51093     for(i=0; i<=pCur->iPage; i++){
51094       releasePage(pCur->apPage[i]);
51095     }
51096     unlockBtreeIfUnused(pBt);
51097     invalidateOverflowCache(pCur);
51098     /* sqlite3_free(pCur); */
51099     sqlite3BtreeLeave(pBtree);
51100   }
51101   return SQLITE_OK;
51102 }
51103 
51104 /*
51105 ** Make sure the BtCursor* given in the argument has a valid
51106 ** BtCursor.info structure.  If it is not already valid, call
51107 ** btreeParseCell() to fill it in.
51108 **
51109 ** BtCursor.info is a cache of the information in the current cell.
51110 ** Using this cache reduces the number of calls to btreeParseCell().
51111 **
51112 ** 2007-06-25:  There is a bug in some versions of MSVC that cause the
51113 ** compiler to crash when getCellInfo() is implemented as a macro.
51114 ** But there is a measureable speed advantage to using the macro on gcc
51115 ** (when less compiler optimizations like -Os or -O0 are used and the
51116 ** compiler is not doing agressive inlining.)  So we use a real function
51117 ** for MSVC and a macro for everything else.  Ticket #2457.
51118 */
51119 #ifndef NDEBUG
51120   static void assertCellInfo(BtCursor *pCur){
51121     CellInfo info;
51122     int iPage = pCur->iPage;
51123     memset(&info, 0, sizeof(info));
51124     btreeParseCell(pCur->apPage[iPage], pCur->aiIdx[iPage], &info);
51125     assert( memcmp(&info, &pCur->info, sizeof(info))==0 );
51126   }
51127 #else
51128   #define assertCellInfo(x)
51129 #endif
51130 #ifdef _MSC_VER
51131   /* Use a real function in MSVC to work around bugs in that compiler. */
51132   static void getCellInfo(BtCursor *pCur){
51133     if( pCur->info.nSize==0 ){
51134       int iPage = pCur->iPage;
51135       btreeParseCell(pCur->apPage[iPage],pCur->aiIdx[iPage],&pCur->info);
51136       pCur->validNKey = 1;
51137     }else{
51138       assertCellInfo(pCur);
51139     }
51140   }
51141 #else /* if not _MSC_VER */
51142   /* Use a macro in all other compilers so that the function is inlined */
51143 #define getCellInfo(pCur)                                                      \
51144   if( pCur->info.nSize==0 ){                                                   \
51145     int iPage = pCur->iPage;                                                   \
51146     btreeParseCell(pCur->apPage[iPage],pCur->aiIdx[iPage],&pCur->info); \
51147     pCur->validNKey = 1;                                                       \
51148   }else{                                                                       \
51149     assertCellInfo(pCur);                                                      \
51150   }
51151 #endif /* _MSC_VER */
51152 
51153 #ifndef NDEBUG  /* The next routine used only within assert() statements */
51154 /*
51155 ** Return true if the given BtCursor is valid.  A valid cursor is one
51156 ** that is currently pointing to a row in a (non-empty) table.
51157 ** This is a verification routine is used only within assert() statements.
51158 */
51159 SQLITE_PRIVATE int sqlite3BtreeCursorIsValid(BtCursor *pCur){
51160   return pCur && pCur->eState==CURSOR_VALID;
51161 }
51162 #endif /* NDEBUG */
51163 
51164 /*
51165 ** Set *pSize to the size of the buffer needed to hold the value of
51166 ** the key for the current entry.  If the cursor is not pointing
51167 ** to a valid entry, *pSize is set to 0.
51168 **
51169 ** For a table with the INTKEY flag set, this routine returns the key
51170 ** itself, not the number of bytes in the key.
51171 **
51172 ** The caller must position the cursor prior to invoking this routine.
51173 **
51174 ** This routine cannot fail.  It always returns SQLITE_OK.
51175 */
51176 SQLITE_PRIVATE int sqlite3BtreeKeySize(BtCursor *pCur, i64 *pSize){
51177   assert( cursorHoldsMutex(pCur) );
51178   assert( pCur->eState==CURSOR_INVALID || pCur->eState==CURSOR_VALID );
51179   if( pCur->eState!=CURSOR_VALID ){
51180     *pSize = 0;
51181   }else{
51182     getCellInfo(pCur);
51183     *pSize = pCur->info.nKey;
51184   }
51185   return SQLITE_OK;
51186 }
51187 
51188 /*
51189 ** Set *pSize to the number of bytes of data in the entry the
51190 ** cursor currently points to.
51191 **
51192 ** The caller must guarantee that the cursor is pointing to a non-NULL
51193 ** valid entry.  In other words, the calling procedure must guarantee
51194 ** that the cursor has Cursor.eState==CURSOR_VALID.
51195 **
51196 ** Failure is not possible.  This function always returns SQLITE_OK.
51197 ** It might just as well be a procedure (returning void) but we continue
51198 ** to return an integer result code for historical reasons.
51199 */
51200 SQLITE_PRIVATE int sqlite3BtreeDataSize(BtCursor *pCur, u32 *pSize){
51201   assert( cursorHoldsMutex(pCur) );
51202   assert( pCur->eState==CURSOR_VALID );
51203   getCellInfo(pCur);
51204   *pSize = pCur->info.nData;
51205   return SQLITE_OK;
51206 }
51207 
51208 /*
51209 ** Given the page number of an overflow page in the database (parameter
51210 ** ovfl), this function finds the page number of the next page in the
51211 ** linked list of overflow pages. If possible, it uses the auto-vacuum
51212 ** pointer-map data instead of reading the content of page ovfl to do so.
51213 **
51214 ** If an error occurs an SQLite error code is returned. Otherwise:
51215 **
51216 ** The page number of the next overflow page in the linked list is
51217 ** written to *pPgnoNext. If page ovfl is the last page in its linked
51218 ** list, *pPgnoNext is set to zero.
51219 **
51220 ** If ppPage is not NULL, and a reference to the MemPage object corresponding
51221 ** to page number pOvfl was obtained, then *ppPage is set to point to that
51222 ** reference. It is the responsibility of the caller to call releasePage()
51223 ** on *ppPage to free the reference. In no reference was obtained (because
51224 ** the pointer-map was used to obtain the value for *pPgnoNext), then
51225 ** *ppPage is set to zero.
51226 */
51227 static int getOverflowPage(
51228   BtShared *pBt,               /* The database file */
51229   Pgno ovfl,                   /* Current overflow page number */
51230   MemPage **ppPage,            /* OUT: MemPage handle (may be NULL) */
51231   Pgno *pPgnoNext              /* OUT: Next overflow page number */
51232 ){
51233   Pgno next = 0;
51234   MemPage *pPage = 0;
51235   int rc = SQLITE_OK;
51236 
51237   assert( sqlite3_mutex_held(pBt->mutex) );
51238   assert(pPgnoNext);
51239 
51240 #ifndef SQLITE_OMIT_AUTOVACUUM
51241   /* Try to find the next page in the overflow list using the
51242   ** autovacuum pointer-map pages. Guess that the next page in
51243   ** the overflow list is page number (ovfl+1). If that guess turns
51244   ** out to be wrong, fall back to loading the data of page
51245   ** number ovfl to determine the next page number.
51246   */
51247   if( pBt->autoVacuum ){
51248     Pgno pgno;
51249     Pgno iGuess = ovfl+1;
51250     u8 eType;
51251 
51252     while( PTRMAP_ISPAGE(pBt, iGuess) || iGuess==PENDING_BYTE_PAGE(pBt) ){
51253       iGuess++;
51254     }
51255 
51256     if( iGuess<=btreePagecount(pBt) ){
51257       rc = ptrmapGet(pBt, iGuess, &eType, &pgno);
51258       if( rc==SQLITE_OK && eType==PTRMAP_OVERFLOW2 && pgno==ovfl ){
51259         next = iGuess;
51260         rc = SQLITE_DONE;
51261       }
51262     }
51263   }
51264 #endif
51265 
51266   assert( next==0 || rc==SQLITE_DONE );
51267   if( rc==SQLITE_OK ){
51268     rc = btreeGetPage(pBt, ovfl, &pPage, 0);
51269     assert( rc==SQLITE_OK || pPage==0 );
51270     if( rc==SQLITE_OK ){
51271       next = get4byte(pPage->aData);
51272     }
51273   }
51274 
51275   *pPgnoNext = next;
51276   if( ppPage ){
51277     *ppPage = pPage;
51278   }else{
51279     releasePage(pPage);
51280   }
51281   return (rc==SQLITE_DONE ? SQLITE_OK : rc);
51282 }
51283 
51284 /*
51285 ** Copy data from a buffer to a page, or from a page to a buffer.
51286 **
51287 ** pPayload is a pointer to data stored on database page pDbPage.
51288 ** If argument eOp is false, then nByte bytes of data are copied
51289 ** from pPayload to the buffer pointed at by pBuf. If eOp is true,
51290 ** then sqlite3PagerWrite() is called on pDbPage and nByte bytes
51291 ** of data are copied from the buffer pBuf to pPayload.
51292 **
51293 ** SQLITE_OK is returned on success, otherwise an error code.
51294 */
51295 static int copyPayload(
51296   void *pPayload,           /* Pointer to page data */
51297   void *pBuf,               /* Pointer to buffer */
51298   int nByte,                /* Number of bytes to copy */
51299   int eOp,                  /* 0 -> copy from page, 1 -> copy to page */
51300   DbPage *pDbPage           /* Page containing pPayload */
51301 ){
51302   if( eOp ){
51303     /* Copy data from buffer to page (a write operation) */
51304     int rc = sqlite3PagerWrite(pDbPage);
51305     if( rc!=SQLITE_OK ){
51306       return rc;
51307     }
51308     memcpy(pPayload, pBuf, nByte);
51309   }else{
51310     /* Copy data from page to buffer (a read operation) */
51311     memcpy(pBuf, pPayload, nByte);
51312   }
51313   return SQLITE_OK;
51314 }
51315 
51316 /*
51317 ** This function is used to read or overwrite payload information
51318 ** for the entry that the pCur cursor is pointing to. If the eOp
51319 ** parameter is 0, this is a read operation (data copied into
51320 ** buffer pBuf). If it is non-zero, a write (data copied from
51321 ** buffer pBuf).
51322 **
51323 ** A total of "amt" bytes are read or written beginning at "offset".
51324 ** Data is read to or from the buffer pBuf.
51325 **
51326 ** The content being read or written might appear on the main page
51327 ** or be scattered out on multiple overflow pages.
51328 **
51329 ** If the BtCursor.isIncrblobHandle flag is set, and the current
51330 ** cursor entry uses one or more overflow pages, this function
51331 ** allocates space for and lazily popluates the overflow page-list
51332 ** cache array (BtCursor.aOverflow). Subsequent calls use this
51333 ** cache to make seeking to the supplied offset more efficient.
51334 **
51335 ** Once an overflow page-list cache has been allocated, it may be
51336 ** invalidated if some other cursor writes to the same table, or if
51337 ** the cursor is moved to a different row. Additionally, in auto-vacuum
51338 ** mode, the following events may invalidate an overflow page-list cache.
51339 **
51340 **   * An incremental vacuum,
51341 **   * A commit in auto_vacuum="full" mode,
51342 **   * Creating a table (may require moving an overflow page).
51343 */
51344 static int accessPayload(
51345   BtCursor *pCur,      /* Cursor pointing to entry to read from */
51346   u32 offset,          /* Begin reading this far into payload */
51347   u32 amt,             /* Read this many bytes */
51348   unsigned char *pBuf, /* Write the bytes into this buffer */
51349   int eOp              /* zero to read. non-zero to write. */
51350 ){
51351   unsigned char *aPayload;
51352   int rc = SQLITE_OK;
51353   u32 nKey;
51354   int iIdx = 0;
51355   MemPage *pPage = pCur->apPage[pCur->iPage]; /* Btree page of current entry */
51356   BtShared *pBt = pCur->pBt;                  /* Btree this cursor belongs to */
51357 
51358   assert( pPage );
51359   assert( pCur->eState==CURSOR_VALID );
51360   assert( pCur->aiIdx[pCur->iPage]<pPage->nCell );
51361   assert( cursorHoldsMutex(pCur) );
51362 
51363   getCellInfo(pCur);
51364   aPayload = pCur->info.pCell + pCur->info.nHeader;
51365   nKey = (pPage->intKey ? 0 : (int)pCur->info.nKey);
51366 
51367   if( NEVER(offset+amt > nKey+pCur->info.nData)
51368    || &aPayload[pCur->info.nLocal] > &pPage->aData[pBt->usableSize]
51369   ){
51370     /* Trying to read or write past the end of the data is an error */
51371     return SQLITE_CORRUPT_BKPT;
51372   }
51373 
51374   /* Check if data must be read/written to/from the btree page itself. */
51375   if( offset<pCur->info.nLocal ){
51376     int a = amt;
51377     if( a+offset>pCur->info.nLocal ){
51378       a = pCur->info.nLocal - offset;
51379     }
51380     rc = copyPayload(&aPayload[offset], pBuf, a, eOp, pPage->pDbPage);
51381     offset = 0;
51382     pBuf += a;
51383     amt -= a;
51384   }else{
51385     offset -= pCur->info.nLocal;
51386   }
51387 
51388   if( rc==SQLITE_OK && amt>0 ){
51389     const u32 ovflSize = pBt->usableSize - 4;  /* Bytes content per ovfl page */
51390     Pgno nextPage;
51391 
51392     nextPage = get4byte(&aPayload[pCur->info.nLocal]);
51393 
51394 #ifndef SQLITE_OMIT_INCRBLOB
51395     /* If the isIncrblobHandle flag is set and the BtCursor.aOverflow[]
51396     ** has not been allocated, allocate it now. The array is sized at
51397     ** one entry for each overflow page in the overflow chain. The
51398     ** page number of the first overflow page is stored in aOverflow[0],
51399     ** etc. A value of 0 in the aOverflow[] array means "not yet known"
51400     ** (the cache is lazily populated).
51401     */
51402     if( pCur->isIncrblobHandle && !pCur->aOverflow ){
51403       int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize;
51404       pCur->aOverflow = (Pgno *)sqlite3MallocZero(sizeof(Pgno)*nOvfl);
51405       /* nOvfl is always positive.  If it were zero, fetchPayload would have
51406       ** been used instead of this routine. */
51407       if( ALWAYS(nOvfl) && !pCur->aOverflow ){
51408         rc = SQLITE_NOMEM;
51409       }
51410     }
51411 
51412     /* If the overflow page-list cache has been allocated and the
51413     ** entry for the first required overflow page is valid, skip
51414     ** directly to it.
51415     */
51416     if( pCur->aOverflow && pCur->aOverflow[offset/ovflSize] ){
51417       iIdx = (offset/ovflSize);
51418       nextPage = pCur->aOverflow[iIdx];
51419       offset = (offset%ovflSize);
51420     }
51421 #endif
51422 
51423     for( ; rc==SQLITE_OK && amt>0 && nextPage; iIdx++){
51424 
51425 #ifndef SQLITE_OMIT_INCRBLOB
51426       /* If required, populate the overflow page-list cache. */
51427       if( pCur->aOverflow ){
51428         assert(!pCur->aOverflow[iIdx] || pCur->aOverflow[iIdx]==nextPage);
51429         pCur->aOverflow[iIdx] = nextPage;
51430       }
51431 #endif
51432 
51433       if( offset>=ovflSize ){
51434         /* The only reason to read this page is to obtain the page
51435         ** number for the next page in the overflow chain. The page
51436         ** data is not required. So first try to lookup the overflow
51437         ** page-list cache, if any, then fall back to the getOverflowPage()
51438         ** function.
51439         */
51440 #ifndef SQLITE_OMIT_INCRBLOB
51441         if( pCur->aOverflow && pCur->aOverflow[iIdx+1] ){
51442           nextPage = pCur->aOverflow[iIdx+1];
51443         } else
51444 #endif
51445           rc = getOverflowPage(pBt, nextPage, 0, &nextPage);
51446         offset -= ovflSize;
51447       }else{
51448         /* Need to read this page properly. It contains some of the
51449         ** range of data that is being read (eOp==0) or written (eOp!=0).
51450         */
51451         DbPage *pDbPage;
51452         int a = amt;
51453         rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage);
51454         if( rc==SQLITE_OK ){
51455           aPayload = sqlite3PagerGetData(pDbPage);
51456           nextPage = get4byte(aPayload);
51457           if( a + offset > ovflSize ){
51458             a = ovflSize - offset;
51459           }
51460           rc = copyPayload(&aPayload[offset+4], pBuf, a, eOp, pDbPage);
51461           sqlite3PagerUnref(pDbPage);
51462           offset = 0;
51463           amt -= a;
51464           pBuf += a;
51465         }
51466       }
51467     }
51468   }
51469 
51470   if( rc==SQLITE_OK && amt>0 ){
51471     return SQLITE_CORRUPT_BKPT;
51472   }
51473   return rc;
51474 }
51475 
51476 /*
51477 ** Read part of the key associated with cursor pCur.  Exactly
51478 ** "amt" bytes will be transfered into pBuf[].  The transfer
51479 ** begins at "offset".
51480 **
51481 ** The caller must ensure that pCur is pointing to a valid row
51482 ** in the table.
51483 **
51484 ** Return SQLITE_OK on success or an error code if anything goes
51485 ** wrong.  An error is returned if "offset+amt" is larger than
51486 ** the available payload.
51487 */
51488 SQLITE_PRIVATE int sqlite3BtreeKey(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
51489   assert( cursorHoldsMutex(pCur) );
51490   assert( pCur->eState==CURSOR_VALID );
51491   assert( pCur->iPage>=0 && pCur->apPage[pCur->iPage] );
51492   assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
51493   return accessPayload(pCur, offset, amt, (unsigned char*)pBuf, 0);
51494 }
51495 
51496 /*
51497 ** Read part of the data associated with cursor pCur.  Exactly
51498 ** "amt" bytes will be transfered into pBuf[].  The transfer
51499 ** begins at "offset".
51500 **
51501 ** Return SQLITE_OK on success or an error code if anything goes
51502 ** wrong.  An error is returned if "offset+amt" is larger than
51503 ** the available payload.
51504 */
51505 SQLITE_PRIVATE int sqlite3BtreeData(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
51506   int rc;
51507 
51508 #ifndef SQLITE_OMIT_INCRBLOB
51509   if ( pCur->eState==CURSOR_INVALID ){
51510     return SQLITE_ABORT;
51511   }
51512 #endif
51513 
51514   assert( cursorHoldsMutex(pCur) );
51515   rc = restoreCursorPosition(pCur);
51516   if( rc==SQLITE_OK ){
51517     assert( pCur->eState==CURSOR_VALID );
51518     assert( pCur->iPage>=0 && pCur->apPage[pCur->iPage] );
51519     assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
51520     rc = accessPayload(pCur, offset, amt, pBuf, 0);
51521   }
51522   return rc;
51523 }
51524 
51525 /*
51526 ** Return a pointer to payload information from the entry that the
51527 ** pCur cursor is pointing to.  The pointer is to the beginning of
51528 ** the key if skipKey==0 and it points to the beginning of data if
51529 ** skipKey==1.  The number of bytes of available key/data is written
51530 ** into *pAmt.  If *pAmt==0, then the value returned will not be
51531 ** a valid pointer.
51532 **
51533 ** This routine is an optimization.  It is common for the entire key
51534 ** and data to fit on the local page and for there to be no overflow
51535 ** pages.  When that is so, this routine can be used to access the
51536 ** key and data without making a copy.  If the key and/or data spills
51537 ** onto overflow pages, then accessPayload() must be used to reassemble
51538 ** the key/data and copy it into a preallocated buffer.
51539 **
51540 ** The pointer returned by this routine looks directly into the cached
51541 ** page of the database.  The data might change or move the next time
51542 ** any btree routine is called.
51543 */
51544 static const unsigned char *fetchPayload(
51545   BtCursor *pCur,      /* Cursor pointing to entry to read from */
51546   int *pAmt,           /* Write the number of available bytes here */
51547   int skipKey          /* read beginning at data if this is true */
51548 ){
51549   unsigned char *aPayload;
51550   MemPage *pPage;
51551   u32 nKey;
51552   u32 nLocal;
51553 
51554   assert( pCur!=0 && pCur->iPage>=0 && pCur->apPage[pCur->iPage]);
51555   assert( pCur->eState==CURSOR_VALID );
51556   assert( cursorHoldsMutex(pCur) );
51557   pPage = pCur->apPage[pCur->iPage];
51558   assert( pCur->aiIdx[pCur->iPage]<pPage->nCell );
51559   if( NEVER(pCur->info.nSize==0) ){
51560     btreeParseCell(pCur->apPage[pCur->iPage], pCur->aiIdx[pCur->iPage],
51561                    &pCur->info);
51562   }
51563   aPayload = pCur->info.pCell;
51564   aPayload += pCur->info.nHeader;
51565   if( pPage->intKey ){
51566     nKey = 0;
51567   }else{
51568     nKey = (int)pCur->info.nKey;
51569   }
51570   if( skipKey ){
51571     aPayload += nKey;
51572     nLocal = pCur->info.nLocal - nKey;
51573   }else{
51574     nLocal = pCur->info.nLocal;
51575     assert( nLocal<=nKey );
51576   }
51577   *pAmt = nLocal;
51578   return aPayload;
51579 }
51580 
51581 
51582 /*
51583 ** For the entry that cursor pCur is point to, return as
51584 ** many bytes of the key or data as are available on the local
51585 ** b-tree page.  Write the number of available bytes into *pAmt.
51586 **
51587 ** The pointer returned is ephemeral.  The key/data may move
51588 ** or be destroyed on the next call to any Btree routine,
51589 ** including calls from other threads against the same cache.
51590 ** Hence, a mutex on the BtShared should be held prior to calling
51591 ** this routine.
51592 **
51593 ** These routines is used to get quick access to key and data
51594 ** in the common case where no overflow pages are used.
51595 */
51596 SQLITE_PRIVATE const void *sqlite3BtreeKeyFetch(BtCursor *pCur, int *pAmt){
51597   const void *p = 0;
51598   assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
51599   assert( cursorHoldsMutex(pCur) );
51600   if( ALWAYS(pCur->eState==CURSOR_VALID) ){
51601     p = (const void*)fetchPayload(pCur, pAmt, 0);
51602   }
51603   return p;
51604 }
51605 SQLITE_PRIVATE const void *sqlite3BtreeDataFetch(BtCursor *pCur, int *pAmt){
51606   const void *p = 0;
51607   assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
51608   assert( cursorHoldsMutex(pCur) );
51609   if( ALWAYS(pCur->eState==CURSOR_VALID) ){
51610     p = (const void*)fetchPayload(pCur, pAmt, 1);
51611   }
51612   return p;
51613 }
51614 
51615 
51616 /*
51617 ** Move the cursor down to a new child page.  The newPgno argument is the
51618 ** page number of the child page to move to.
51619 **
51620 ** This function returns SQLITE_CORRUPT if the page-header flags field of
51621 ** the new child page does not match the flags field of the parent (i.e.
51622 ** if an intkey page appears to be the parent of a non-intkey page, or
51623 ** vice-versa).
51624 */
51625 static int moveToChild(BtCursor *pCur, u32 newPgno){
51626   int rc;
51627   int i = pCur->iPage;
51628   MemPage *pNewPage;
51629   BtShared *pBt = pCur->pBt;
51630 
51631   assert( cursorHoldsMutex(pCur) );
51632   assert( pCur->eState==CURSOR_VALID );
51633   assert( pCur->iPage<BTCURSOR_MAX_DEPTH );
51634   if( pCur->iPage>=(BTCURSOR_MAX_DEPTH-1) ){
51635     return SQLITE_CORRUPT_BKPT;
51636   }
51637   rc = getAndInitPage(pBt, newPgno, &pNewPage);
51638   if( rc ) return rc;
51639   pCur->apPage[i+1] = pNewPage;
51640   pCur->aiIdx[i+1] = 0;
51641   pCur->iPage++;
51642 
51643   pCur->info.nSize = 0;
51644   pCur->validNKey = 0;
51645   if( pNewPage->nCell<1 || pNewPage->intKey!=pCur->apPage[i]->intKey ){
51646     return SQLITE_CORRUPT_BKPT;
51647   }
51648   return SQLITE_OK;
51649 }
51650 
51651 #ifndef NDEBUG
51652 /*
51653 ** Page pParent is an internal (non-leaf) tree page. This function
51654 ** asserts that page number iChild is the left-child if the iIdx'th
51655 ** cell in page pParent. Or, if iIdx is equal to the total number of
51656 ** cells in pParent, that page number iChild is the right-child of
51657 ** the page.
51658 */
51659 static void assertParentIndex(MemPage *pParent, int iIdx, Pgno iChild){
51660   assert( iIdx<=pParent->nCell );
51661   if( iIdx==pParent->nCell ){
51662     assert( get4byte(&pParent->aData[pParent->hdrOffset+8])==iChild );
51663   }else{
51664     assert( get4byte(findCell(pParent, iIdx))==iChild );
51665   }
51666 }
51667 #else
51668 #  define assertParentIndex(x,y,z)
51669 #endif
51670 
51671 /*
51672 ** Move the cursor up to the parent page.
51673 **
51674 ** pCur->idx is set to the cell index that contains the pointer
51675 ** to the page we are coming from.  If we are coming from the
51676 ** right-most child page then pCur->idx is set to one more than
51677 ** the largest cell index.
51678 */
51679 static void moveToParent(BtCursor *pCur){
51680   assert( cursorHoldsMutex(pCur) );
51681   assert( pCur->eState==CURSOR_VALID );
51682   assert( pCur->iPage>0 );
51683   assert( pCur->apPage[pCur->iPage] );
51684   assertParentIndex(
51685     pCur->apPage[pCur->iPage-1],
51686     pCur->aiIdx[pCur->iPage-1],
51687     pCur->apPage[pCur->iPage]->pgno
51688   );
51689   releasePage(pCur->apPage[pCur->iPage]);
51690   pCur->iPage--;
51691   pCur->info.nSize = 0;
51692   pCur->validNKey = 0;
51693 }
51694 
51695 /*
51696 ** Move the cursor to point to the root page of its b-tree structure.
51697 **
51698 ** If the table has a virtual root page, then the cursor is moved to point
51699 ** to the virtual root page instead of the actual root page. A table has a
51700 ** virtual root page when the actual root page contains no cells and a
51701 ** single child page. This can only happen with the table rooted at page 1.
51702 **
51703 ** If the b-tree structure is empty, the cursor state is set to
51704 ** CURSOR_INVALID. Otherwise, the cursor is set to point to the first
51705 ** cell located on the root (or virtual root) page and the cursor state
51706 ** is set to CURSOR_VALID.
51707 **
51708 ** If this function returns successfully, it may be assumed that the
51709 ** page-header flags indicate that the [virtual] root-page is the expected
51710 ** kind of b-tree page (i.e. if when opening the cursor the caller did not
51711 ** specify a KeyInfo structure the flags byte is set to 0x05 or 0x0D,
51712 ** indicating a table b-tree, or if the caller did specify a KeyInfo
51713 ** structure the flags byte is set to 0x02 or 0x0A, indicating an index
51714 ** b-tree).
51715 */
51716 static int moveToRoot(BtCursor *pCur){
51717   MemPage *pRoot;
51718   int rc = SQLITE_OK;
51719   Btree *p = pCur->pBtree;
51720   BtShared *pBt = p->pBt;
51721 
51722   assert( cursorHoldsMutex(pCur) );
51723   assert( CURSOR_INVALID < CURSOR_REQUIRESEEK );
51724   assert( CURSOR_VALID   < CURSOR_REQUIRESEEK );
51725   assert( CURSOR_FAULT   > CURSOR_REQUIRESEEK );
51726   if( pCur->eState>=CURSOR_REQUIRESEEK ){
51727     if( pCur->eState==CURSOR_FAULT ){
51728       assert( pCur->skipNext!=SQLITE_OK );
51729       return pCur->skipNext;
51730     }
51731     sqlite3BtreeClearCursor(pCur);
51732   }
51733 
51734   if( pCur->iPage>=0 ){
51735     int i;
51736     for(i=1; i<=pCur->iPage; i++){
51737       releasePage(pCur->apPage[i]);
51738     }
51739     pCur->iPage = 0;
51740   }else{
51741     rc = getAndInitPage(pBt, pCur->pgnoRoot, &pCur->apPage[0]);
51742     if( rc!=SQLITE_OK ){
51743       pCur->eState = CURSOR_INVALID;
51744       return rc;
51745     }
51746     pCur->iPage = 0;
51747 
51748     /* If pCur->pKeyInfo is not NULL, then the caller that opened this cursor
51749     ** expected to open it on an index b-tree. Otherwise, if pKeyInfo is
51750     ** NULL, the caller expects a table b-tree. If this is not the case,
51751     ** return an SQLITE_CORRUPT error.  */
51752     assert( pCur->apPage[0]->intKey==1 || pCur->apPage[0]->intKey==0 );
51753     if( (pCur->pKeyInfo==0)!=pCur->apPage[0]->intKey ){
51754       return SQLITE_CORRUPT_BKPT;
51755     }
51756   }
51757 
51758   /* Assert that the root page is of the correct type. This must be the
51759   ** case as the call to this function that loaded the root-page (either
51760   ** this call or a previous invocation) would have detected corruption
51761   ** if the assumption were not true, and it is not possible for the flags
51762   ** byte to have been modified while this cursor is holding a reference
51763   ** to the page.  */
51764   pRoot = pCur->apPage[0];
51765   assert( pRoot->pgno==pCur->pgnoRoot );
51766   assert( pRoot->isInit && (pCur->pKeyInfo==0)==pRoot->intKey );
51767 
51768   pCur->aiIdx[0] = 0;
51769   pCur->info.nSize = 0;
51770   pCur->atLast = 0;
51771   pCur->validNKey = 0;
51772 
51773   if( pRoot->nCell==0 && !pRoot->leaf ){
51774     Pgno subpage;
51775     if( pRoot->pgno!=1 ) return SQLITE_CORRUPT_BKPT;
51776     subpage = get4byte(&pRoot->aData[pRoot->hdrOffset+8]);
51777     pCur->eState = CURSOR_VALID;
51778     rc = moveToChild(pCur, subpage);
51779   }else{
51780     pCur->eState = ((pRoot->nCell>0)?CURSOR_VALID:CURSOR_INVALID);
51781   }
51782   return rc;
51783 }
51784 
51785 /*
51786 ** Move the cursor down to the left-most leaf entry beneath the
51787 ** entry to which it is currently pointing.
51788 **
51789 ** The left-most leaf is the one with the smallest key - the first
51790 ** in ascending order.
51791 */
51792 static int moveToLeftmost(BtCursor *pCur){
51793   Pgno pgno;
51794   int rc = SQLITE_OK;
51795   MemPage *pPage;
51796 
51797   assert( cursorHoldsMutex(pCur) );
51798   assert( pCur->eState==CURSOR_VALID );
51799   while( rc==SQLITE_OK && !(pPage = pCur->apPage[pCur->iPage])->leaf ){
51800     assert( pCur->aiIdx[pCur->iPage]<pPage->nCell );
51801     pgno = get4byte(findCell(pPage, pCur->aiIdx[pCur->iPage]));
51802     rc = moveToChild(pCur, pgno);
51803   }
51804   return rc;
51805 }
51806 
51807 /*
51808 ** Move the cursor down to the right-most leaf entry beneath the
51809 ** page to which it is currently pointing.  Notice the difference
51810 ** between moveToLeftmost() and moveToRightmost().  moveToLeftmost()
51811 ** finds the left-most entry beneath the *entry* whereas moveToRightmost()
51812 ** finds the right-most entry beneath the *page*.
51813 **
51814 ** The right-most entry is the one with the largest key - the last
51815 ** key in ascending order.
51816 */
51817 static int moveToRightmost(BtCursor *pCur){
51818   Pgno pgno;
51819   int rc = SQLITE_OK;
51820   MemPage *pPage = 0;
51821 
51822   assert( cursorHoldsMutex(pCur) );
51823   assert( pCur->eState==CURSOR_VALID );
51824   while( rc==SQLITE_OK && !(pPage = pCur->apPage[pCur->iPage])->leaf ){
51825     pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
51826     pCur->aiIdx[pCur->iPage] = pPage->nCell;
51827     rc = moveToChild(pCur, pgno);
51828   }
51829   if( rc==SQLITE_OK ){
51830     pCur->aiIdx[pCur->iPage] = pPage->nCell-1;
51831     pCur->info.nSize = 0;
51832     pCur->validNKey = 0;
51833   }
51834   return rc;
51835 }
51836 
51837 /* Move the cursor to the first entry in the table.  Return SQLITE_OK
51838 ** on success.  Set *pRes to 0 if the cursor actually points to something
51839 ** or set *pRes to 1 if the table is empty.
51840 */
51841 SQLITE_PRIVATE int sqlite3BtreeFirst(BtCursor *pCur, int *pRes){
51842   int rc;
51843 
51844   assert( cursorHoldsMutex(pCur) );
51845   assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
51846   rc = moveToRoot(pCur);
51847   if( rc==SQLITE_OK ){
51848     if( pCur->eState==CURSOR_INVALID ){
51849       assert( pCur->apPage[pCur->iPage]->nCell==0 );
51850       *pRes = 1;
51851     }else{
51852       assert( pCur->apPage[pCur->iPage]->nCell>0 );
51853       *pRes = 0;
51854       rc = moveToLeftmost(pCur);
51855     }
51856   }
51857   return rc;
51858 }
51859 
51860 /* Move the cursor to the last entry in the table.  Return SQLITE_OK
51861 ** on success.  Set *pRes to 0 if the cursor actually points to something
51862 ** or set *pRes to 1 if the table is empty.
51863 */
51864 SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor *pCur, int *pRes){
51865   int rc;
51866 
51867   assert( cursorHoldsMutex(pCur) );
51868   assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
51869 
51870   /* If the cursor already points to the last entry, this is a no-op. */
51871   if( CURSOR_VALID==pCur->eState && pCur->atLast ){
51872 #ifdef SQLITE_DEBUG
51873     /* This block serves to assert() that the cursor really does point
51874     ** to the last entry in the b-tree. */
51875     int ii;
51876     for(ii=0; ii<pCur->iPage; ii++){
51877       assert( pCur->aiIdx[ii]==pCur->apPage[ii]->nCell );
51878     }
51879     assert( pCur->aiIdx[pCur->iPage]==pCur->apPage[pCur->iPage]->nCell-1 );
51880     assert( pCur->apPage[pCur->iPage]->leaf );
51881 #endif
51882     return SQLITE_OK;
51883   }
51884 
51885   rc = moveToRoot(pCur);
51886   if( rc==SQLITE_OK ){
51887     if( CURSOR_INVALID==pCur->eState ){
51888       assert( pCur->apPage[pCur->iPage]->nCell==0 );
51889       *pRes = 1;
51890     }else{
51891       assert( pCur->eState==CURSOR_VALID );
51892       *pRes = 0;
51893       rc = moveToRightmost(pCur);
51894       pCur->atLast = rc==SQLITE_OK ?1:0;
51895     }
51896   }
51897   return rc;
51898 }
51899 
51900 /* Move the cursor so that it points to an entry near the key
51901 ** specified by pIdxKey or intKey.   Return a success code.
51902 **
51903 ** For INTKEY tables, the intKey parameter is used.  pIdxKey
51904 ** must be NULL.  For index tables, pIdxKey is used and intKey
51905 ** is ignored.
51906 **
51907 ** If an exact match is not found, then the cursor is always
51908 ** left pointing at a leaf page which would hold the entry if it
51909 ** were present.  The cursor might point to an entry that comes
51910 ** before or after the key.
51911 **
51912 ** An integer is written into *pRes which is the result of
51913 ** comparing the key with the entry to which the cursor is
51914 ** pointing.  The meaning of the integer written into
51915 ** *pRes is as follows:
51916 **
51917 **     *pRes<0      The cursor is left pointing at an entry that
51918 **                  is smaller than intKey/pIdxKey or if the table is empty
51919 **                  and the cursor is therefore left point to nothing.
51920 **
51921 **     *pRes==0     The cursor is left pointing at an entry that
51922 **                  exactly matches intKey/pIdxKey.
51923 **
51924 **     *pRes>0      The cursor is left pointing at an entry that
51925 **                  is larger than intKey/pIdxKey.
51926 **
51927 */
51928 SQLITE_PRIVATE int sqlite3BtreeMovetoUnpacked(
51929   BtCursor *pCur,          /* The cursor to be moved */
51930   UnpackedRecord *pIdxKey, /* Unpacked index key */
51931   i64 intKey,              /* The table key */
51932   int biasRight,           /* If true, bias the search to the high end */
51933   int *pRes                /* Write search results here */
51934 ){
51935   int rc;
51936 
51937   assert( cursorHoldsMutex(pCur) );
51938   assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
51939   assert( pRes );
51940   assert( (pIdxKey==0)==(pCur->pKeyInfo==0) );
51941 
51942   /* If the cursor is already positioned at the point we are trying
51943   ** to move to, then just return without doing any work */
51944   if( pCur->eState==CURSOR_VALID && pCur->validNKey
51945    && pCur->apPage[0]->intKey
51946   ){
51947     if( pCur->info.nKey==intKey ){
51948       *pRes = 0;
51949       return SQLITE_OK;
51950     }
51951     if( pCur->atLast && pCur->info.nKey<intKey ){
51952       *pRes = -1;
51953       return SQLITE_OK;
51954     }
51955   }
51956 
51957   rc = moveToRoot(pCur);
51958   if( rc ){
51959     return rc;
51960   }
51961   assert( pCur->apPage[pCur->iPage] );
51962   assert( pCur->apPage[pCur->iPage]->isInit );
51963   assert( pCur->apPage[pCur->iPage]->nCell>0 || pCur->eState==CURSOR_INVALID );
51964   if( pCur->eState==CURSOR_INVALID ){
51965     *pRes = -1;
51966     assert( pCur->apPage[pCur->iPage]->nCell==0 );
51967     return SQLITE_OK;
51968   }
51969   assert( pCur->apPage[0]->intKey || pIdxKey );
51970   for(;;){
51971     int lwr, upr, idx;
51972     Pgno chldPg;
51973     MemPage *pPage = pCur->apPage[pCur->iPage];
51974     int c;
51975 
51976     /* pPage->nCell must be greater than zero. If this is the root-page
51977     ** the cursor would have been INVALID above and this for(;;) loop
51978     ** not run. If this is not the root-page, then the moveToChild() routine
51979     ** would have already detected db corruption. Similarly, pPage must
51980     ** be the right kind (index or table) of b-tree page. Otherwise
51981     ** a moveToChild() or moveToRoot() call would have detected corruption.  */
51982     assert( pPage->nCell>0 );
51983     assert( pPage->intKey==(pIdxKey==0) );
51984     lwr = 0;
51985     upr = pPage->nCell-1;
51986     if( biasRight ){
51987       pCur->aiIdx[pCur->iPage] = (u16)(idx = upr);
51988     }else{
51989       pCur->aiIdx[pCur->iPage] = (u16)(idx = (upr+lwr)/2);
51990     }
51991     for(;;){
51992       u8 *pCell;                          /* Pointer to current cell in pPage */
51993 
51994       assert( idx==pCur->aiIdx[pCur->iPage] );
51995       pCur->info.nSize = 0;
51996       pCell = findCell(pPage, idx) + pPage->childPtrSize;
51997       if( pPage->intKey ){
51998         i64 nCellKey;
51999         if( pPage->hasData ){
52000           u32 dummy;
52001           pCell += getVarint32(pCell, dummy);
52002         }
52003         getVarint(pCell, (u64*)&nCellKey);
52004         if( nCellKey==intKey ){
52005           c = 0;
52006         }else if( nCellKey<intKey ){
52007           c = -1;
52008         }else{
52009           assert( nCellKey>intKey );
52010           c = +1;
52011         }
52012         pCur->validNKey = 1;
52013         pCur->info.nKey = nCellKey;
52014       }else{
52015         /* The maximum supported page-size is 65536 bytes. This means that
52016         ** the maximum number of record bytes stored on an index B-Tree
52017         ** page is less than 16384 bytes and may be stored as a 2-byte
52018         ** varint. This information is used to attempt to avoid parsing
52019         ** the entire cell by checking for the cases where the record is
52020         ** stored entirely within the b-tree page by inspecting the first
52021         ** 2 bytes of the cell.
52022         */
52023         int nCell = pCell[0];
52024         if( !(nCell & 0x80) && nCell<=pPage->maxLocal ){
52025           /* This branch runs if the record-size field of the cell is a
52026           ** single byte varint and the record fits entirely on the main
52027           ** b-tree page.  */
52028           c = sqlite3VdbeRecordCompare(nCell, (void*)&pCell[1], pIdxKey);
52029         }else if( !(pCell[1] & 0x80)
52030           && (nCell = ((nCell&0x7f)<<7) + pCell[1])<=pPage->maxLocal
52031         ){
52032           /* The record-size field is a 2 byte varint and the record
52033           ** fits entirely on the main b-tree page.  */
52034           c = sqlite3VdbeRecordCompare(nCell, (void*)&pCell[2], pIdxKey);
52035         }else{
52036           /* The record flows over onto one or more overflow pages. In
52037           ** this case the whole cell needs to be parsed, a buffer allocated
52038           ** and accessPayload() used to retrieve the record into the
52039           ** buffer before VdbeRecordCompare() can be called. */
52040           void *pCellKey;
52041           u8 * const pCellBody = pCell - pPage->childPtrSize;
52042           btreeParseCellPtr(pPage, pCellBody, &pCur->info);
52043           nCell = (int)pCur->info.nKey;
52044           pCellKey = sqlite3Malloc( nCell );
52045           if( pCellKey==0 ){
52046             rc = SQLITE_NOMEM;
52047             goto moveto_finish;
52048           }
52049           rc = accessPayload(pCur, 0, nCell, (unsigned char*)pCellKey, 0);
52050           if( rc ){
52051             sqlite3_free(pCellKey);
52052             goto moveto_finish;
52053           }
52054           c = sqlite3VdbeRecordCompare(nCell, pCellKey, pIdxKey);
52055           sqlite3_free(pCellKey);
52056         }
52057       }
52058       if( c==0 ){
52059         if( pPage->intKey && !pPage->leaf ){
52060           lwr = idx;
52061           upr = lwr - 1;
52062           break;
52063         }else{
52064           *pRes = 0;
52065           rc = SQLITE_OK;
52066           goto moveto_finish;
52067         }
52068       }
52069       if( c<0 ){
52070         lwr = idx+1;
52071       }else{
52072         upr = idx-1;
52073       }
52074       if( lwr>upr ){
52075         break;
52076       }
52077       pCur->aiIdx[pCur->iPage] = (u16)(idx = (lwr+upr)/2);
52078     }
52079     assert( lwr==upr+1 );
52080     assert( pPage->isInit );
52081     if( pPage->leaf ){
52082       chldPg = 0;
52083     }else if( lwr>=pPage->nCell ){
52084       chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]);
52085     }else{
52086       chldPg = get4byte(findCell(pPage, lwr));
52087     }
52088     if( chldPg==0 ){
52089       assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
52090       *pRes = c;
52091       rc = SQLITE_OK;
52092       goto moveto_finish;
52093     }
52094     pCur->aiIdx[pCur->iPage] = (u16)lwr;
52095     pCur->info.nSize = 0;
52096     pCur->validNKey = 0;
52097     rc = moveToChild(pCur, chldPg);
52098     if( rc ) goto moveto_finish;
52099   }
52100 moveto_finish:
52101   return rc;
52102 }
52103 
52104 
52105 /*
52106 ** Return TRUE if the cursor is not pointing at an entry of the table.
52107 **
52108 ** TRUE will be returned after a call to sqlite3BtreeNext() moves
52109 ** past the last entry in the table or sqlite3BtreePrev() moves past
52110 ** the first entry.  TRUE is also returned if the table is empty.
52111 */
52112 SQLITE_PRIVATE int sqlite3BtreeEof(BtCursor *pCur){
52113   /* TODO: What if the cursor is in CURSOR_REQUIRESEEK but all table entries
52114   ** have been deleted? This API will need to change to return an error code
52115   ** as well as the boolean result value.
52116   */
52117   return (CURSOR_VALID!=pCur->eState);
52118 }
52119 
52120 /*
52121 ** Advance the cursor to the next entry in the database.  If
52122 ** successful then set *pRes=0.  If the cursor
52123 ** was already pointing to the last entry in the database before
52124 ** this routine was called, then set *pRes=1.
52125 */
52126 SQLITE_PRIVATE int sqlite3BtreeNext(BtCursor *pCur, int *pRes){
52127   int rc;
52128   int idx;
52129   MemPage *pPage;
52130 
52131   assert( cursorHoldsMutex(pCur) );
52132   rc = restoreCursorPosition(pCur);
52133   if( rc!=SQLITE_OK ){
52134     return rc;
52135   }
52136   assert( pRes!=0 );
52137   if( CURSOR_INVALID==pCur->eState ){
52138     *pRes = 1;
52139     return SQLITE_OK;
52140   }
52141   if( pCur->skipNext>0 ){
52142     pCur->skipNext = 0;
52143     *pRes = 0;
52144     return SQLITE_OK;
52145   }
52146   pCur->skipNext = 0;
52147 
52148   pPage = pCur->apPage[pCur->iPage];
52149   idx = ++pCur->aiIdx[pCur->iPage];
52150   assert( pPage->isInit );
52151   assert( idx<=pPage->nCell );
52152 
52153   pCur->info.nSize = 0;
52154   pCur->validNKey = 0;
52155   if( idx>=pPage->nCell ){
52156     if( !pPage->leaf ){
52157       rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
52158       if( rc ) return rc;
52159       rc = moveToLeftmost(pCur);
52160       *pRes = 0;
52161       return rc;
52162     }
52163     do{
52164       if( pCur->iPage==0 ){
52165         *pRes = 1;
52166         pCur->eState = CURSOR_INVALID;
52167         return SQLITE_OK;
52168       }
52169       moveToParent(pCur);
52170       pPage = pCur->apPage[pCur->iPage];
52171     }while( pCur->aiIdx[pCur->iPage]>=pPage->nCell );
52172     *pRes = 0;
52173     if( pPage->intKey ){
52174       rc = sqlite3BtreeNext(pCur, pRes);
52175     }else{
52176       rc = SQLITE_OK;
52177     }
52178     return rc;
52179   }
52180   *pRes = 0;
52181   if( pPage->leaf ){
52182     return SQLITE_OK;
52183   }
52184   rc = moveToLeftmost(pCur);
52185   return rc;
52186 }
52187 
52188 
52189 /*
52190 ** Step the cursor to the back to the previous entry in the database.  If
52191 ** successful then set *pRes=0.  If the cursor
52192 ** was already pointing to the first entry in the database before
52193 ** this routine was called, then set *pRes=1.
52194 */
52195 SQLITE_PRIVATE int sqlite3BtreePrevious(BtCursor *pCur, int *pRes){
52196   int rc;
52197   MemPage *pPage;
52198 
52199   assert( cursorHoldsMutex(pCur) );
52200   rc = restoreCursorPosition(pCur);
52201   if( rc!=SQLITE_OK ){
52202     return rc;
52203   }
52204   pCur->atLast = 0;
52205   if( CURSOR_INVALID==pCur->eState ){
52206     *pRes = 1;
52207     return SQLITE_OK;
52208   }
52209   if( pCur->skipNext<0 ){
52210     pCur->skipNext = 0;
52211     *pRes = 0;
52212     return SQLITE_OK;
52213   }
52214   pCur->skipNext = 0;
52215 
52216   pPage = pCur->apPage[pCur->iPage];
52217   assert( pPage->isInit );
52218   if( !pPage->leaf ){
52219     int idx = pCur->aiIdx[pCur->iPage];
52220     rc = moveToChild(pCur, get4byte(findCell(pPage, idx)));
52221     if( rc ){
52222       return rc;
52223     }
52224     rc = moveToRightmost(pCur);
52225   }else{
52226     while( pCur->aiIdx[pCur->iPage]==0 ){
52227       if( pCur->iPage==0 ){
52228         pCur->eState = CURSOR_INVALID;
52229         *pRes = 1;
52230         return SQLITE_OK;
52231       }
52232       moveToParent(pCur);
52233     }
52234     pCur->info.nSize = 0;
52235     pCur->validNKey = 0;
52236 
52237     pCur->aiIdx[pCur->iPage]--;
52238     pPage = pCur->apPage[pCur->iPage];
52239     if( pPage->intKey && !pPage->leaf ){
52240       rc = sqlite3BtreePrevious(pCur, pRes);
52241     }else{
52242       rc = SQLITE_OK;
52243     }
52244   }
52245   *pRes = 0;
52246   return rc;
52247 }
52248 
52249 /*
52250 ** Allocate a new page from the database file.
52251 **
52252 ** The new page is marked as dirty.  (In other words, sqlite3PagerWrite()
52253 ** has already been called on the new page.)  The new page has also
52254 ** been referenced and the calling routine is responsible for calling
52255 ** sqlite3PagerUnref() on the new page when it is done.
52256 **
52257 ** SQLITE_OK is returned on success.  Any other return value indicates
52258 ** an error.  *ppPage and *pPgno are undefined in the event of an error.
52259 ** Do not invoke sqlite3PagerUnref() on *ppPage if an error is returned.
52260 **
52261 ** If the "nearby" parameter is not 0, then a (feeble) effort is made to
52262 ** locate a page close to the page number "nearby".  This can be used in an
52263 ** attempt to keep related pages close to each other in the database file,
52264 ** which in turn can make database access faster.
52265 **
52266 ** If the "exact" parameter is not 0, and the page-number nearby exists
52267 ** anywhere on the free-list, then it is guarenteed to be returned. This
52268 ** is only used by auto-vacuum databases when allocating a new table.
52269 */
52270 static int allocateBtreePage(
52271   BtShared *pBt,
52272   MemPage **ppPage,
52273   Pgno *pPgno,
52274   Pgno nearby,
52275   u8 exact
52276 ){
52277   MemPage *pPage1;
52278   int rc;
52279   u32 n;     /* Number of pages on the freelist */
52280   u32 k;     /* Number of leaves on the trunk of the freelist */
52281   MemPage *pTrunk = 0;
52282   MemPage *pPrevTrunk = 0;
52283   Pgno mxPage;     /* Total size of the database file */
52284 
52285   assert( sqlite3_mutex_held(pBt->mutex) );
52286   pPage1 = pBt->pPage1;
52287   mxPage = btreePagecount(pBt);
52288   n = get4byte(&pPage1->aData[36]);
52289   testcase( n==mxPage-1 );
52290   if( n>=mxPage ){
52291     return SQLITE_CORRUPT_BKPT;
52292   }
52293   if( n>0 ){
52294     /* There are pages on the freelist.  Reuse one of those pages. */
52295     Pgno iTrunk;
52296     u8 searchList = 0; /* If the free-list must be searched for 'nearby' */
52297 
52298     /* If the 'exact' parameter was true and a query of the pointer-map
52299     ** shows that the page 'nearby' is somewhere on the free-list, then
52300     ** the entire-list will be searched for that page.
52301     */
52302 #ifndef SQLITE_OMIT_AUTOVACUUM
52303     if( exact && nearby<=mxPage ){
52304       u8 eType;
52305       assert( nearby>0 );
52306       assert( pBt->autoVacuum );
52307       rc = ptrmapGet(pBt, nearby, &eType, 0);
52308       if( rc ) return rc;
52309       if( eType==PTRMAP_FREEPAGE ){
52310         searchList = 1;
52311       }
52312       *pPgno = nearby;
52313     }
52314 #endif
52315 
52316     /* Decrement the free-list count by 1. Set iTrunk to the index of the
52317     ** first free-list trunk page. iPrevTrunk is initially 1.
52318     */
52319     rc = sqlite3PagerWrite(pPage1->pDbPage);
52320     if( rc ) return rc;
52321     put4byte(&pPage1->aData[36], n-1);
52322 
52323     /* The code within this loop is run only once if the 'searchList' variable
52324     ** is not true. Otherwise, it runs once for each trunk-page on the
52325     ** free-list until the page 'nearby' is located.
52326     */
52327     do {
52328       pPrevTrunk = pTrunk;
52329       if( pPrevTrunk ){
52330         iTrunk = get4byte(&pPrevTrunk->aData[0]);
52331       }else{
52332         iTrunk = get4byte(&pPage1->aData[32]);
52333       }
52334       testcase( iTrunk==mxPage );
52335       if( iTrunk>mxPage ){
52336         rc = SQLITE_CORRUPT_BKPT;
52337       }else{
52338         rc = btreeGetPage(pBt, iTrunk, &pTrunk, 0);
52339       }
52340       if( rc ){
52341         pTrunk = 0;
52342         goto end_allocate_page;
52343       }
52344 
52345       k = get4byte(&pTrunk->aData[4]); /* # of leaves on this trunk page */
52346       if( k==0 && !searchList ){
52347         /* The trunk has no leaves and the list is not being searched.
52348         ** So extract the trunk page itself and use it as the newly
52349         ** allocated page */
52350         assert( pPrevTrunk==0 );
52351         rc = sqlite3PagerWrite(pTrunk->pDbPage);
52352         if( rc ){
52353           goto end_allocate_page;
52354         }
52355         *pPgno = iTrunk;
52356         memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
52357         *ppPage = pTrunk;
52358         pTrunk = 0;
52359         TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
52360       }else if( k>(u32)(pBt->usableSize/4 - 2) ){
52361         /* Value of k is out of range.  Database corruption */
52362         rc = SQLITE_CORRUPT_BKPT;
52363         goto end_allocate_page;
52364 #ifndef SQLITE_OMIT_AUTOVACUUM
52365       }else if( searchList && nearby==iTrunk ){
52366         /* The list is being searched and this trunk page is the page
52367         ** to allocate, regardless of whether it has leaves.
52368         */
52369         assert( *pPgno==iTrunk );
52370         *ppPage = pTrunk;
52371         searchList = 0;
52372         rc = sqlite3PagerWrite(pTrunk->pDbPage);
52373         if( rc ){
52374           goto end_allocate_page;
52375         }
52376         if( k==0 ){
52377           if( !pPrevTrunk ){
52378             memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
52379           }else{
52380             rc = sqlite3PagerWrite(pPrevTrunk->pDbPage);
52381             if( rc!=SQLITE_OK ){
52382               goto end_allocate_page;
52383             }
52384             memcpy(&pPrevTrunk->aData[0], &pTrunk->aData[0], 4);
52385           }
52386         }else{
52387           /* The trunk page is required by the caller but it contains
52388           ** pointers to free-list leaves. The first leaf becomes a trunk
52389           ** page in this case.
52390           */
52391           MemPage *pNewTrunk;
52392           Pgno iNewTrunk = get4byte(&pTrunk->aData[8]);
52393           if( iNewTrunk>mxPage ){
52394             rc = SQLITE_CORRUPT_BKPT;
52395             goto end_allocate_page;
52396           }
52397           testcase( iNewTrunk==mxPage );
52398           rc = btreeGetPage(pBt, iNewTrunk, &pNewTrunk, 0);
52399           if( rc!=SQLITE_OK ){
52400             goto end_allocate_page;
52401           }
52402           rc = sqlite3PagerWrite(pNewTrunk->pDbPage);
52403           if( rc!=SQLITE_OK ){
52404             releasePage(pNewTrunk);
52405             goto end_allocate_page;
52406           }
52407           memcpy(&pNewTrunk->aData[0], &pTrunk->aData[0], 4);
52408           put4byte(&pNewTrunk->aData[4], k-1);
52409           memcpy(&pNewTrunk->aData[8], &pTrunk->aData[12], (k-1)*4);
52410           releasePage(pNewTrunk);
52411           if( !pPrevTrunk ){
52412             assert( sqlite3PagerIswriteable(pPage1->pDbPage) );
52413             put4byte(&pPage1->aData[32], iNewTrunk);
52414           }else{
52415             rc = sqlite3PagerWrite(pPrevTrunk->pDbPage);
52416             if( rc ){
52417               goto end_allocate_page;
52418             }
52419             put4byte(&pPrevTrunk->aData[0], iNewTrunk);
52420           }
52421         }
52422         pTrunk = 0;
52423         TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
52424 #endif
52425       }else if( k>0 ){
52426         /* Extract a leaf from the trunk */
52427         u32 closest;
52428         Pgno iPage;
52429         unsigned char *aData = pTrunk->aData;
52430         if( nearby>0 ){
52431           u32 i;
52432           int dist;
52433           closest = 0;
52434           dist = sqlite3AbsInt32(get4byte(&aData[8]) - nearby);
52435           for(i=1; i<k; i++){
52436             int d2 = sqlite3AbsInt32(get4byte(&aData[8+i*4]) - nearby);
52437             if( d2<dist ){
52438               closest = i;
52439               dist = d2;
52440             }
52441           }
52442         }else{
52443           closest = 0;
52444         }
52445 
52446         iPage = get4byte(&aData[8+closest*4]);
52447         testcase( iPage==mxPage );
52448         if( iPage>mxPage ){
52449           rc = SQLITE_CORRUPT_BKPT;
52450           goto end_allocate_page;
52451         }
52452         testcase( iPage==mxPage );
52453         if( !searchList || iPage==nearby ){
52454           int noContent;
52455           *pPgno = iPage;
52456           TRACE(("ALLOCATE: %d was leaf %d of %d on trunk %d"
52457                  ": %d more free pages\n",
52458                  *pPgno, closest+1, k, pTrunk->pgno, n-1));
52459           rc = sqlite3PagerWrite(pTrunk->pDbPage);
52460           if( rc ) goto end_allocate_page;
52461           if( closest<k-1 ){
52462             memcpy(&aData[8+closest*4], &aData[4+k*4], 4);
52463           }
52464           put4byte(&aData[4], k-1);
52465           noContent = !btreeGetHasContent(pBt, *pPgno);
52466           rc = btreeGetPage(pBt, *pPgno, ppPage, noContent);
52467           if( rc==SQLITE_OK ){
52468             rc = sqlite3PagerWrite((*ppPage)->pDbPage);
52469             if( rc!=SQLITE_OK ){
52470               releasePage(*ppPage);
52471             }
52472           }
52473           searchList = 0;
52474         }
52475       }
52476       releasePage(pPrevTrunk);
52477       pPrevTrunk = 0;
52478     }while( searchList );
52479   }else{
52480     /* There are no pages on the freelist, so create a new page at the
52481     ** end of the file */
52482     rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
52483     if( rc ) return rc;
52484     pBt->nPage++;
52485     if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ) pBt->nPage++;
52486 
52487 #ifndef SQLITE_OMIT_AUTOVACUUM
52488     if( pBt->autoVacuum && PTRMAP_ISPAGE(pBt, pBt->nPage) ){
52489       /* If *pPgno refers to a pointer-map page, allocate two new pages
52490       ** at the end of the file instead of one. The first allocated page
52491       ** becomes a new pointer-map page, the second is used by the caller.
52492       */
52493       MemPage *pPg = 0;
52494       TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", pBt->nPage));
52495       assert( pBt->nPage!=PENDING_BYTE_PAGE(pBt) );
52496       rc = btreeGetPage(pBt, pBt->nPage, &pPg, 1);
52497       if( rc==SQLITE_OK ){
52498         rc = sqlite3PagerWrite(pPg->pDbPage);
52499         releasePage(pPg);
52500       }
52501       if( rc ) return rc;
52502       pBt->nPage++;
52503       if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ){ pBt->nPage++; }
52504     }
52505 #endif
52506     put4byte(28 + (u8*)pBt->pPage1->aData, pBt->nPage);
52507     *pPgno = pBt->nPage;
52508 
52509     assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
52510     rc = btreeGetPage(pBt, *pPgno, ppPage, 1);
52511     if( rc ) return rc;
52512     rc = sqlite3PagerWrite((*ppPage)->pDbPage);
52513     if( rc!=SQLITE_OK ){
52514       releasePage(*ppPage);
52515     }
52516     TRACE(("ALLOCATE: %d from end of file\n", *pPgno));
52517   }
52518 
52519   assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
52520 
52521 end_allocate_page:
52522   releasePage(pTrunk);
52523   releasePage(pPrevTrunk);
52524   if( rc==SQLITE_OK ){
52525     if( sqlite3PagerPageRefcount((*ppPage)->pDbPage)>1 ){
52526       releasePage(*ppPage);
52527       return SQLITE_CORRUPT_BKPT;
52528     }
52529     (*ppPage)->isInit = 0;
52530   }else{
52531     *ppPage = 0;
52532   }
52533   assert( rc!=SQLITE_OK || sqlite3PagerIswriteable((*ppPage)->pDbPage) );
52534   return rc;
52535 }
52536 
52537 /*
52538 ** This function is used to add page iPage to the database file free-list.
52539 ** It is assumed that the page is not already a part of the free-list.
52540 **
52541 ** The value passed as the second argument to this function is optional.
52542 ** If the caller happens to have a pointer to the MemPage object
52543 ** corresponding to page iPage handy, it may pass it as the second value.
52544 ** Otherwise, it may pass NULL.
52545 **
52546 ** If a pointer to a MemPage object is passed as the second argument,
52547 ** its reference count is not altered by this function.
52548 */
52549 static int freePage2(BtShared *pBt, MemPage *pMemPage, Pgno iPage){
52550   MemPage *pTrunk = 0;                /* Free-list trunk page */
52551   Pgno iTrunk = 0;                    /* Page number of free-list trunk page */
52552   MemPage *pPage1 = pBt->pPage1;      /* Local reference to page 1 */
52553   MemPage *pPage;                     /* Page being freed. May be NULL. */
52554   int rc;                             /* Return Code */
52555   int nFree;                          /* Initial number of pages on free-list */
52556 
52557   assert( sqlite3_mutex_held(pBt->mutex) );
52558   assert( iPage>1 );
52559   assert( !pMemPage || pMemPage->pgno==iPage );
52560 
52561   if( pMemPage ){
52562     pPage = pMemPage;
52563     sqlite3PagerRef(pPage->pDbPage);
52564   }else{
52565     pPage = btreePageLookup(pBt, iPage);
52566   }
52567 
52568   /* Increment the free page count on pPage1 */
52569   rc = sqlite3PagerWrite(pPage1->pDbPage);
52570   if( rc ) goto freepage_out;
52571   nFree = get4byte(&pPage1->aData[36]);
52572   put4byte(&pPage1->aData[36], nFree+1);
52573 
52574   if( pBt->secureDelete ){
52575     /* If the secure_delete option is enabled, then
52576     ** always fully overwrite deleted information with zeros.
52577     */
52578     if( (!pPage && ((rc = btreeGetPage(pBt, iPage, &pPage, 0))!=0) )
52579      ||            ((rc = sqlite3PagerWrite(pPage->pDbPage))!=0)
52580     ){
52581       goto freepage_out;
52582     }
52583     memset(pPage->aData, 0, pPage->pBt->pageSize);
52584   }
52585 
52586   /* If the database supports auto-vacuum, write an entry in the pointer-map
52587   ** to indicate that the page is free.
52588   */
52589   if( ISAUTOVACUUM ){
52590     ptrmapPut(pBt, iPage, PTRMAP_FREEPAGE, 0, &rc);
52591     if( rc ) goto freepage_out;
52592   }
52593 
52594   /* Now manipulate the actual database free-list structure. There are two
52595   ** possibilities. If the free-list is currently empty, or if the first
52596   ** trunk page in the free-list is full, then this page will become a
52597   ** new free-list trunk page. Otherwise, it will become a leaf of the
52598   ** first trunk page in the current free-list. This block tests if it
52599   ** is possible to add the page as a new free-list leaf.
52600   */
52601   if( nFree!=0 ){
52602     u32 nLeaf;                /* Initial number of leaf cells on trunk page */
52603 
52604     iTrunk = get4byte(&pPage1->aData[32]);
52605     rc = btreeGetPage(pBt, iTrunk, &pTrunk, 0);
52606     if( rc!=SQLITE_OK ){
52607       goto freepage_out;
52608     }
52609 
52610     nLeaf = get4byte(&pTrunk->aData[4]);
52611     assert( pBt->usableSize>32 );
52612     if( nLeaf > (u32)pBt->usableSize/4 - 2 ){
52613       rc = SQLITE_CORRUPT_BKPT;
52614       goto freepage_out;
52615     }
52616     if( nLeaf < (u32)pBt->usableSize/4 - 8 ){
52617       /* In this case there is room on the trunk page to insert the page
52618       ** being freed as a new leaf.
52619       **
52620       ** Note that the trunk page is not really full until it contains
52621       ** usableSize/4 - 2 entries, not usableSize/4 - 8 entries as we have
52622       ** coded.  But due to a coding error in versions of SQLite prior to
52623       ** 3.6.0, databases with freelist trunk pages holding more than
52624       ** usableSize/4 - 8 entries will be reported as corrupt.  In order
52625       ** to maintain backwards compatibility with older versions of SQLite,
52626       ** we will continue to restrict the number of entries to usableSize/4 - 8
52627       ** for now.  At some point in the future (once everyone has upgraded
52628       ** to 3.6.0 or later) we should consider fixing the conditional above
52629       ** to read "usableSize/4-2" instead of "usableSize/4-8".
52630       */
52631       rc = sqlite3PagerWrite(pTrunk->pDbPage);
52632       if( rc==SQLITE_OK ){
52633         put4byte(&pTrunk->aData[4], nLeaf+1);
52634         put4byte(&pTrunk->aData[8+nLeaf*4], iPage);
52635         if( pPage && !pBt->secureDelete ){
52636           sqlite3PagerDontWrite(pPage->pDbPage);
52637         }
52638         rc = btreeSetHasContent(pBt, iPage);
52639       }
52640       TRACE(("FREE-PAGE: %d leaf on trunk page %d\n",pPage->pgno,pTrunk->pgno));
52641       goto freepage_out;
52642     }
52643   }
52644 
52645   /* If control flows to this point, then it was not possible to add the
52646   ** the page being freed as a leaf page of the first trunk in the free-list.
52647   ** Possibly because the free-list is empty, or possibly because the
52648   ** first trunk in the free-list is full. Either way, the page being freed
52649   ** will become the new first trunk page in the free-list.
52650   */
52651   if( pPage==0 && SQLITE_OK!=(rc = btreeGetPage(pBt, iPage, &pPage, 0)) ){
52652     goto freepage_out;
52653   }
52654   rc = sqlite3PagerWrite(pPage->pDbPage);
52655   if( rc!=SQLITE_OK ){
52656     goto freepage_out;
52657   }
52658   put4byte(pPage->aData, iTrunk);
52659   put4byte(&pPage->aData[4], 0);
52660   put4byte(&pPage1->aData[32], iPage);
52661   TRACE(("FREE-PAGE: %d new trunk page replacing %d\n", pPage->pgno, iTrunk));
52662 
52663 freepage_out:
52664   if( pPage ){
52665     pPage->isInit = 0;
52666   }
52667   releasePage(pPage);
52668   releasePage(pTrunk);
52669   return rc;
52670 }
52671 static void freePage(MemPage *pPage, int *pRC){
52672   if( (*pRC)==SQLITE_OK ){
52673     *pRC = freePage2(pPage->pBt, pPage, pPage->pgno);
52674   }
52675 }
52676 
52677 /*
52678 ** Free any overflow pages associated with the given Cell.
52679 */
52680 static int clearCell(MemPage *pPage, unsigned char *pCell){
52681   BtShared *pBt = pPage->pBt;
52682   CellInfo info;
52683   Pgno ovflPgno;
52684   int rc;
52685   int nOvfl;
52686   u32 ovflPageSize;
52687 
52688   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
52689   btreeParseCellPtr(pPage, pCell, &info);
52690   if( info.iOverflow==0 ){
52691     return SQLITE_OK;  /* No overflow pages. Return without doing anything */
52692   }
52693   ovflPgno = get4byte(&pCell[info.iOverflow]);
52694   assert( pBt->usableSize > 4 );
52695   ovflPageSize = pBt->usableSize - 4;
52696   nOvfl = (info.nPayload - info.nLocal + ovflPageSize - 1)/ovflPageSize;
52697   assert( ovflPgno==0 || nOvfl>0 );
52698   while( nOvfl-- ){
52699     Pgno iNext = 0;
52700     MemPage *pOvfl = 0;
52701     if( ovflPgno<2 || ovflPgno>btreePagecount(pBt) ){
52702       /* 0 is not a legal page number and page 1 cannot be an
52703       ** overflow page. Therefore if ovflPgno<2 or past the end of the
52704       ** file the database must be corrupt. */
52705       return SQLITE_CORRUPT_BKPT;
52706     }
52707     if( nOvfl ){
52708       rc = getOverflowPage(pBt, ovflPgno, &pOvfl, &iNext);
52709       if( rc ) return rc;
52710     }
52711 
52712     if( ( pOvfl || ((pOvfl = btreePageLookup(pBt, ovflPgno))!=0) )
52713      && sqlite3PagerPageRefcount(pOvfl->pDbPage)!=1
52714     ){
52715       /* There is no reason any cursor should have an outstanding reference
52716       ** to an overflow page belonging to a cell that is being deleted/updated.
52717       ** So if there exists more than one reference to this page, then it
52718       ** must not really be an overflow page and the database must be corrupt.
52719       ** It is helpful to detect this before calling freePage2(), as
52720       ** freePage2() may zero the page contents if secure-delete mode is
52721       ** enabled. If this 'overflow' page happens to be a page that the
52722       ** caller is iterating through or using in some other way, this
52723       ** can be problematic.
52724       */
52725       rc = SQLITE_CORRUPT_BKPT;
52726     }else{
52727       rc = freePage2(pBt, pOvfl, ovflPgno);
52728     }
52729 
52730     if( pOvfl ){
52731       sqlite3PagerUnref(pOvfl->pDbPage);
52732     }
52733     if( rc ) return rc;
52734     ovflPgno = iNext;
52735   }
52736   return SQLITE_OK;
52737 }
52738 
52739 /*
52740 ** Create the byte sequence used to represent a cell on page pPage
52741 ** and write that byte sequence into pCell[].  Overflow pages are
52742 ** allocated and filled in as necessary.  The calling procedure
52743 ** is responsible for making sure sufficient space has been allocated
52744 ** for pCell[].
52745 **
52746 ** Note that pCell does not necessary need to point to the pPage->aData
52747 ** area.  pCell might point to some temporary storage.  The cell will
52748 ** be constructed in this temporary area then copied into pPage->aData
52749 ** later.
52750 */
52751 static int fillInCell(
52752   MemPage *pPage,                /* The page that contains the cell */
52753   unsigned char *pCell,          /* Complete text of the cell */
52754   const void *pKey, i64 nKey,    /* The key */
52755   const void *pData,int nData,   /* The data */
52756   int nZero,                     /* Extra zero bytes to append to pData */
52757   int *pnSize                    /* Write cell size here */
52758 ){
52759   int nPayload;
52760   const u8 *pSrc;
52761   int nSrc, n, rc;
52762   int spaceLeft;
52763   MemPage *pOvfl = 0;
52764   MemPage *pToRelease = 0;
52765   unsigned char *pPrior;
52766   unsigned char *pPayload;
52767   BtShared *pBt = pPage->pBt;
52768   Pgno pgnoOvfl = 0;
52769   int nHeader;
52770   CellInfo info;
52771 
52772   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
52773 
52774   /* pPage is not necessarily writeable since pCell might be auxiliary
52775   ** buffer space that is separate from the pPage buffer area */
52776   assert( pCell<pPage->aData || pCell>=&pPage->aData[pBt->pageSize]
52777             || sqlite3PagerIswriteable(pPage->pDbPage) );
52778 
52779   /* Fill in the header. */
52780   nHeader = 0;
52781   if( !pPage->leaf ){
52782     nHeader += 4;
52783   }
52784   if( pPage->hasData ){
52785     nHeader += putVarint(&pCell[nHeader], nData+nZero);
52786   }else{
52787     nData = nZero = 0;
52788   }
52789   nHeader += putVarint(&pCell[nHeader], *(u64*)&nKey);
52790   btreeParseCellPtr(pPage, pCell, &info);
52791   assert( info.nHeader==nHeader );
52792   assert( info.nKey==nKey );
52793   assert( info.nData==(u32)(nData+nZero) );
52794 
52795   /* Fill in the payload */
52796   nPayload = nData + nZero;
52797   if( pPage->intKey ){
52798     pSrc = pData;
52799     nSrc = nData;
52800     nData = 0;
52801   }else{
52802     if( NEVER(nKey>0x7fffffff || pKey==0) ){
52803       return SQLITE_CORRUPT_BKPT;
52804     }
52805     nPayload += (int)nKey;
52806     pSrc = pKey;
52807     nSrc = (int)nKey;
52808   }
52809   *pnSize = info.nSize;
52810   spaceLeft = info.nLocal;
52811   pPayload = &pCell[nHeader];
52812   pPrior = &pCell[info.iOverflow];
52813 
52814   while( nPayload>0 ){
52815     if( spaceLeft==0 ){
52816 #ifndef SQLITE_OMIT_AUTOVACUUM
52817       Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */
52818       if( pBt->autoVacuum ){
52819         do{
52820           pgnoOvfl++;
52821         } while(
52822           PTRMAP_ISPAGE(pBt, pgnoOvfl) || pgnoOvfl==PENDING_BYTE_PAGE(pBt)
52823         );
52824       }
52825 #endif
52826       rc = allocateBtreePage(pBt, &pOvfl, &pgnoOvfl, pgnoOvfl, 0);
52827 #ifndef SQLITE_OMIT_AUTOVACUUM
52828       /* If the database supports auto-vacuum, and the second or subsequent
52829       ** overflow page is being allocated, add an entry to the pointer-map
52830       ** for that page now.
52831       **
52832       ** If this is the first overflow page, then write a partial entry
52833       ** to the pointer-map. If we write nothing to this pointer-map slot,
52834       ** then the optimistic overflow chain processing in clearCell()
52835       ** may misinterpret the uninitialised values and delete the
52836       ** wrong pages from the database.
52837       */
52838       if( pBt->autoVacuum && rc==SQLITE_OK ){
52839         u8 eType = (pgnoPtrmap?PTRMAP_OVERFLOW2:PTRMAP_OVERFLOW1);
52840         ptrmapPut(pBt, pgnoOvfl, eType, pgnoPtrmap, &rc);
52841         if( rc ){
52842           releasePage(pOvfl);
52843         }
52844       }
52845 #endif
52846       if( rc ){
52847         releasePage(pToRelease);
52848         return rc;
52849       }
52850 
52851       /* If pToRelease is not zero than pPrior points into the data area
52852       ** of pToRelease.  Make sure pToRelease is still writeable. */
52853       assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) );
52854 
52855       /* If pPrior is part of the data area of pPage, then make sure pPage
52856       ** is still writeable */
52857       assert( pPrior<pPage->aData || pPrior>=&pPage->aData[pBt->pageSize]
52858             || sqlite3PagerIswriteable(pPage->pDbPage) );
52859 
52860       put4byte(pPrior, pgnoOvfl);
52861       releasePage(pToRelease);
52862       pToRelease = pOvfl;
52863       pPrior = pOvfl->aData;
52864       put4byte(pPrior, 0);
52865       pPayload = &pOvfl->aData[4];
52866       spaceLeft = pBt->usableSize - 4;
52867     }
52868     n = nPayload;
52869     if( n>spaceLeft ) n = spaceLeft;
52870 
52871     /* If pToRelease is not zero than pPayload points into the data area
52872     ** of pToRelease.  Make sure pToRelease is still writeable. */
52873     assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) );
52874 
52875     /* If pPayload is part of the data area of pPage, then make sure pPage
52876     ** is still writeable */
52877     assert( pPayload<pPage->aData || pPayload>=&pPage->aData[pBt->pageSize]
52878             || sqlite3PagerIswriteable(pPage->pDbPage) );
52879 
52880     if( nSrc>0 ){
52881       if( n>nSrc ) n = nSrc;
52882       assert( pSrc );
52883       memcpy(pPayload, pSrc, n);
52884     }else{
52885       memset(pPayload, 0, n);
52886     }
52887     nPayload -= n;
52888     pPayload += n;
52889     pSrc += n;
52890     nSrc -= n;
52891     spaceLeft -= n;
52892     if( nSrc==0 ){
52893       nSrc = nData;
52894       pSrc = pData;
52895     }
52896   }
52897   releasePage(pToRelease);
52898   return SQLITE_OK;
52899 }
52900 
52901 /*
52902 ** Remove the i-th cell from pPage.  This routine effects pPage only.
52903 ** The cell content is not freed or deallocated.  It is assumed that
52904 ** the cell content has been copied someplace else.  This routine just
52905 ** removes the reference to the cell from pPage.
52906 **
52907 ** "sz" must be the number of bytes in the cell.
52908 */
52909 static void dropCell(MemPage *pPage, int idx, int sz, int *pRC){
52910   u32 pc;         /* Offset to cell content of cell being deleted */
52911   u8 *data;       /* pPage->aData */
52912   u8 *ptr;        /* Used to move bytes around within data[] */
52913   u8 *endPtr;     /* End of loop */
52914   int rc;         /* The return code */
52915   int hdr;        /* Beginning of the header.  0 most pages.  100 page 1 */
52916 
52917   if( *pRC ) return;
52918 
52919   assert( idx>=0 && idx<pPage->nCell );
52920   assert( sz==cellSize(pPage, idx) );
52921   assert( sqlite3PagerIswriteable(pPage->pDbPage) );
52922   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
52923   data = pPage->aData;
52924   ptr = &data[pPage->cellOffset + 2*idx];
52925   pc = get2byte(ptr);
52926   hdr = pPage->hdrOffset;
52927   testcase( pc==get2byte(&data[hdr+5]) );
52928   testcase( pc+sz==pPage->pBt->usableSize );
52929   if( pc < (u32)get2byte(&data[hdr+5]) || pc+sz > pPage->pBt->usableSize ){
52930     *pRC = SQLITE_CORRUPT_BKPT;
52931     return;
52932   }
52933   rc = freeSpace(pPage, pc, sz);
52934   if( rc ){
52935     *pRC = rc;
52936     return;
52937   }
52938   endPtr = &data[pPage->cellOffset + 2*pPage->nCell - 2];
52939   assert( (SQLITE_PTR_TO_INT(ptr)&1)==0 );  /* ptr is always 2-byte aligned */
52940   while( ptr<endPtr ){
52941     *(u16*)ptr = *(u16*)&ptr[2];
52942     ptr += 2;
52943   }
52944   pPage->nCell--;
52945   put2byte(&data[hdr+3], pPage->nCell);
52946   pPage->nFree += 2;
52947 }
52948 
52949 /*
52950 ** Insert a new cell on pPage at cell index "i".  pCell points to the
52951 ** content of the cell.
52952 **
52953 ** If the cell content will fit on the page, then put it there.  If it
52954 ** will not fit, then make a copy of the cell content into pTemp if
52955 ** pTemp is not null.  Regardless of pTemp, allocate a new entry
52956 ** in pPage->aOvfl[] and make it point to the cell content (either
52957 ** in pTemp or the original pCell) and also record its index.
52958 ** Allocating a new entry in pPage->aCell[] implies that
52959 ** pPage->nOverflow is incremented.
52960 **
52961 ** If nSkip is non-zero, then do not copy the first nSkip bytes of the
52962 ** cell. The caller will overwrite them after this function returns. If
52963 ** nSkip is non-zero, then pCell may not point to an invalid memory location
52964 ** (but pCell+nSkip is always valid).
52965 */
52966 static void insertCell(
52967   MemPage *pPage,   /* Page into which we are copying */
52968   int i,            /* New cell becomes the i-th cell of the page */
52969   u8 *pCell,        /* Content of the new cell */
52970   int sz,           /* Bytes of content in pCell */
52971   u8 *pTemp,        /* Temp storage space for pCell, if needed */
52972   Pgno iChild,      /* If non-zero, replace first 4 bytes with this value */
52973   int *pRC          /* Read and write return code from here */
52974 ){
52975   int idx = 0;      /* Where to write new cell content in data[] */
52976   int j;            /* Loop counter */
52977   int end;          /* First byte past the last cell pointer in data[] */
52978   int ins;          /* Index in data[] where new cell pointer is inserted */
52979   int cellOffset;   /* Address of first cell pointer in data[] */
52980   u8 *data;         /* The content of the whole page */
52981   u8 *ptr;          /* Used for moving information around in data[] */
52982   u8 *endPtr;       /* End of the loop */
52983 
52984   int nSkip = (iChild ? 4 : 0);
52985 
52986   if( *pRC ) return;
52987 
52988   assert( i>=0 && i<=pPage->nCell+pPage->nOverflow );
52989   assert( pPage->nCell<=MX_CELL(pPage->pBt) && MX_CELL(pPage->pBt)<=10921 );
52990   assert( pPage->nOverflow<=ArraySize(pPage->aOvfl) );
52991   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
52992   /* The cell should normally be sized correctly.  However, when moving a
52993   ** malformed cell from a leaf page to an interior page, if the cell size
52994   ** wanted to be less than 4 but got rounded up to 4 on the leaf, then size
52995   ** might be less than 8 (leaf-size + pointer) on the interior node.  Hence
52996   ** the term after the || in the following assert(). */
52997   assert( sz==cellSizePtr(pPage, pCell) || (sz==8 && iChild>0) );
52998   if( pPage->nOverflow || sz+2>pPage->nFree ){
52999     if( pTemp ){
53000       memcpy(pTemp+nSkip, pCell+nSkip, sz-nSkip);
53001       pCell = pTemp;
53002     }
53003     if( iChild ){
53004       put4byte(pCell, iChild);
53005     }
53006     j = pPage->nOverflow++;
53007     assert( j<(int)(sizeof(pPage->aOvfl)/sizeof(pPage->aOvfl[0])) );
53008     pPage->aOvfl[j].pCell = pCell;
53009     pPage->aOvfl[j].idx = (u16)i;
53010   }else{
53011     int rc = sqlite3PagerWrite(pPage->pDbPage);
53012     if( rc!=SQLITE_OK ){
53013       *pRC = rc;
53014       return;
53015     }
53016     assert( sqlite3PagerIswriteable(pPage->pDbPage) );
53017     data = pPage->aData;
53018     cellOffset = pPage->cellOffset;
53019     end = cellOffset + 2*pPage->nCell;
53020     ins = cellOffset + 2*i;
53021     rc = allocateSpace(pPage, sz, &idx);
53022     if( rc ){ *pRC = rc; return; }
53023     /* The allocateSpace() routine guarantees the following two properties
53024     ** if it returns success */
53025     assert( idx >= end+2 );
53026     assert( idx+sz <= (int)pPage->pBt->usableSize );
53027     pPage->nCell++;
53028     pPage->nFree -= (u16)(2 + sz);
53029     memcpy(&data[idx+nSkip], pCell+nSkip, sz-nSkip);
53030     if( iChild ){
53031       put4byte(&data[idx], iChild);
53032     }
53033     ptr = &data[end];
53034     endPtr = &data[ins];
53035     assert( (SQLITE_PTR_TO_INT(ptr)&1)==0 );  /* ptr is always 2-byte aligned */
53036     while( ptr>endPtr ){
53037       *(u16*)ptr = *(u16*)&ptr[-2];
53038       ptr -= 2;
53039     }
53040     put2byte(&data[ins], idx);
53041     put2byte(&data[pPage->hdrOffset+3], pPage->nCell);
53042 #ifndef SQLITE_OMIT_AUTOVACUUM
53043     if( pPage->pBt->autoVacuum ){
53044       /* The cell may contain a pointer to an overflow page. If so, write
53045       ** the entry for the overflow page into the pointer map.
53046       */
53047       ptrmapPutOvflPtr(pPage, pCell, pRC);
53048     }
53049 #endif
53050   }
53051 }
53052 
53053 /*
53054 ** Add a list of cells to a page.  The page should be initially empty.
53055 ** The cells are guaranteed to fit on the page.
53056 */
53057 static void assemblePage(
53058   MemPage *pPage,   /* The page to be assemblied */
53059   int nCell,        /* The number of cells to add to this page */
53060   u8 **apCell,      /* Pointers to cell bodies */
53061   u16 *aSize        /* Sizes of the cells */
53062 ){
53063   int i;            /* Loop counter */
53064   u8 *pCellptr;     /* Address of next cell pointer */
53065   int cellbody;     /* Address of next cell body */
53066   u8 * const data = pPage->aData;             /* Pointer to data for pPage */
53067   const int hdr = pPage->hdrOffset;           /* Offset of header on pPage */
53068   const int nUsable = pPage->pBt->usableSize; /* Usable size of page */
53069 
53070   assert( pPage->nOverflow==0 );
53071   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
53072   assert( nCell>=0 && nCell<=(int)MX_CELL(pPage->pBt)
53073             && (int)MX_CELL(pPage->pBt)<=10921);
53074   assert( sqlite3PagerIswriteable(pPage->pDbPage) );
53075 
53076   /* Check that the page has just been zeroed by zeroPage() */
53077   assert( pPage->nCell==0 );
53078   assert( get2byteNotZero(&data[hdr+5])==nUsable );
53079 
53080   pCellptr = &data[pPage->cellOffset + nCell*2];
53081   cellbody = nUsable;
53082   for(i=nCell-1; i>=0; i--){
53083     u16 sz = aSize[i];
53084     pCellptr -= 2;
53085     cellbody -= sz;
53086     put2byte(pCellptr, cellbody);
53087     memcpy(&data[cellbody], apCell[i], sz);
53088   }
53089   put2byte(&data[hdr+3], nCell);
53090   put2byte(&data[hdr+5], cellbody);
53091   pPage->nFree -= (nCell*2 + nUsable - cellbody);
53092   pPage->nCell = (u16)nCell;
53093 }
53094 
53095 /*
53096 ** The following parameters determine how many adjacent pages get involved
53097 ** in a balancing operation.  NN is the number of neighbors on either side
53098 ** of the page that participate in the balancing operation.  NB is the
53099 ** total number of pages that participate, including the target page and
53100 ** NN neighbors on either side.
53101 **
53102 ** The minimum value of NN is 1 (of course).  Increasing NN above 1
53103 ** (to 2 or 3) gives a modest improvement in SELECT and DELETE performance
53104 ** in exchange for a larger degradation in INSERT and UPDATE performance.
53105 ** The value of NN appears to give the best results overall.
53106 */
53107 #define NN 1             /* Number of neighbors on either side of pPage */
53108 #define NB (NN*2+1)      /* Total pages involved in the balance */
53109 
53110 
53111 #ifndef SQLITE_OMIT_QUICKBALANCE
53112 /*
53113 ** This version of balance() handles the common special case where
53114 ** a new entry is being inserted on the extreme right-end of the
53115 ** tree, in other words, when the new entry will become the largest
53116 ** entry in the tree.
53117 **
53118 ** Instead of trying to balance the 3 right-most leaf pages, just add
53119 ** a new page to the right-hand side and put the one new entry in
53120 ** that page.  This leaves the right side of the tree somewhat
53121 ** unbalanced.  But odds are that we will be inserting new entries
53122 ** at the end soon afterwards so the nearly empty page will quickly
53123 ** fill up.  On average.
53124 **
53125 ** pPage is the leaf page which is the right-most page in the tree.
53126 ** pParent is its parent.  pPage must have a single overflow entry
53127 ** which is also the right-most entry on the page.
53128 **
53129 ** The pSpace buffer is used to store a temporary copy of the divider
53130 ** cell that will be inserted into pParent. Such a cell consists of a 4
53131 ** byte page number followed by a variable length integer. In other
53132 ** words, at most 13 bytes. Hence the pSpace buffer must be at
53133 ** least 13 bytes in size.
53134 */
53135 static int balance_quick(MemPage *pParent, MemPage *pPage, u8 *pSpace){
53136   BtShared *const pBt = pPage->pBt;    /* B-Tree Database */
53137   MemPage *pNew;                       /* Newly allocated page */
53138   int rc;                              /* Return Code */
53139   Pgno pgnoNew;                        /* Page number of pNew */
53140 
53141   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
53142   assert( sqlite3PagerIswriteable(pParent->pDbPage) );
53143   assert( pPage->nOverflow==1 );
53144 
53145   /* This error condition is now caught prior to reaching this function */
53146   if( pPage->nCell<=0 ) return SQLITE_CORRUPT_BKPT;
53147 
53148   /* Allocate a new page. This page will become the right-sibling of
53149   ** pPage. Make the parent page writable, so that the new divider cell
53150   ** may be inserted. If both these operations are successful, proceed.
53151   */
53152   rc = allocateBtreePage(pBt, &pNew, &pgnoNew, 0, 0);
53153 
53154   if( rc==SQLITE_OK ){
53155 
53156     u8 *pOut = &pSpace[4];
53157     u8 *pCell = pPage->aOvfl[0].pCell;
53158     u16 szCell = cellSizePtr(pPage, pCell);
53159     u8 *pStop;
53160 
53161     assert( sqlite3PagerIswriteable(pNew->pDbPage) );
53162     assert( pPage->aData[0]==(PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF) );
53163     zeroPage(pNew, PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF);
53164     assemblePage(pNew, 1, &pCell, &szCell);
53165 
53166     /* If this is an auto-vacuum database, update the pointer map
53167     ** with entries for the new page, and any pointer from the
53168     ** cell on the page to an overflow page. If either of these
53169     ** operations fails, the return code is set, but the contents
53170     ** of the parent page are still manipulated by thh code below.
53171     ** That is Ok, at this point the parent page is guaranteed to
53172     ** be marked as dirty. Returning an error code will cause a
53173     ** rollback, undoing any changes made to the parent page.
53174     */
53175     if( ISAUTOVACUUM ){
53176       ptrmapPut(pBt, pgnoNew, PTRMAP_BTREE, pParent->pgno, &rc);
53177       if( szCell>pNew->minLocal ){
53178         ptrmapPutOvflPtr(pNew, pCell, &rc);
53179       }
53180     }
53181 
53182     /* Create a divider cell to insert into pParent. The divider cell
53183     ** consists of a 4-byte page number (the page number of pPage) and
53184     ** a variable length key value (which must be the same value as the
53185     ** largest key on pPage).
53186     **
53187     ** To find the largest key value on pPage, first find the right-most
53188     ** cell on pPage. The first two fields of this cell are the
53189     ** record-length (a variable length integer at most 32-bits in size)
53190     ** and the key value (a variable length integer, may have any value).
53191     ** The first of the while(...) loops below skips over the record-length
53192     ** field. The second while(...) loop copies the key value from the
53193     ** cell on pPage into the pSpace buffer.
53194     */
53195     pCell = findCell(pPage, pPage->nCell-1);
53196     pStop = &pCell[9];
53197     while( (*(pCell++)&0x80) && pCell<pStop );
53198     pStop = &pCell[9];
53199     while( ((*(pOut++) = *(pCell++))&0x80) && pCell<pStop );
53200 
53201     /* Insert the new divider cell into pParent. */
53202     insertCell(pParent, pParent->nCell, pSpace, (int)(pOut-pSpace),
53203                0, pPage->pgno, &rc);
53204 
53205     /* Set the right-child pointer of pParent to point to the new page. */
53206     put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew);
53207 
53208     /* Release the reference to the new page. */
53209     releasePage(pNew);
53210   }
53211 
53212   return rc;
53213 }
53214 #endif /* SQLITE_OMIT_QUICKBALANCE */
53215 
53216 #if 0
53217 /*
53218 ** This function does not contribute anything to the operation of SQLite.
53219 ** it is sometimes activated temporarily while debugging code responsible
53220 ** for setting pointer-map entries.
53221 */
53222 static int ptrmapCheckPages(MemPage **apPage, int nPage){
53223   int i, j;
53224   for(i=0; i<nPage; i++){
53225     Pgno n;
53226     u8 e;
53227     MemPage *pPage = apPage[i];
53228     BtShared *pBt = pPage->pBt;
53229     assert( pPage->isInit );
53230 
53231     for(j=0; j<pPage->nCell; j++){
53232       CellInfo info;
53233       u8 *z;
53234 
53235       z = findCell(pPage, j);
53236       btreeParseCellPtr(pPage, z, &info);
53237       if( info.iOverflow ){
53238         Pgno ovfl = get4byte(&z[info.iOverflow]);
53239         ptrmapGet(pBt, ovfl, &e, &n);
53240         assert( n==pPage->pgno && e==PTRMAP_OVERFLOW1 );
53241       }
53242       if( !pPage->leaf ){
53243         Pgno child = get4byte(z);
53244         ptrmapGet(pBt, child, &e, &n);
53245         assert( n==pPage->pgno && e==PTRMAP_BTREE );
53246       }
53247     }
53248     if( !pPage->leaf ){
53249       Pgno child = get4byte(&pPage->aData[pPage->hdrOffset+8]);
53250       ptrmapGet(pBt, child, &e, &n);
53251       assert( n==pPage->pgno && e==PTRMAP_BTREE );
53252     }
53253   }
53254   return 1;
53255 }
53256 #endif
53257 
53258 /*
53259 ** This function is used to copy the contents of the b-tree node stored
53260 ** on page pFrom to page pTo. If page pFrom was not a leaf page, then
53261 ** the pointer-map entries for each child page are updated so that the
53262 ** parent page stored in the pointer map is page pTo. If pFrom contained
53263 ** any cells with overflow page pointers, then the corresponding pointer
53264 ** map entries are also updated so that the parent page is page pTo.
53265 **
53266 ** If pFrom is currently carrying any overflow cells (entries in the
53267 ** MemPage.aOvfl[] array), they are not copied to pTo.
53268 **
53269 ** Before returning, page pTo is reinitialized using btreeInitPage().
53270 **
53271 ** The performance of this function is not critical. It is only used by
53272 ** the balance_shallower() and balance_deeper() procedures, neither of
53273 ** which are called often under normal circumstances.
53274 */
53275 static void copyNodeContent(MemPage *pFrom, MemPage *pTo, int *pRC){
53276   if( (*pRC)==SQLITE_OK ){
53277     BtShared * const pBt = pFrom->pBt;
53278     u8 * const aFrom = pFrom->aData;
53279     u8 * const aTo = pTo->aData;
53280     int const iFromHdr = pFrom->hdrOffset;
53281     int const iToHdr = ((pTo->pgno==1) ? 100 : 0);
53282     int rc;
53283     int iData;
53284 
53285 
53286     assert( pFrom->isInit );
53287     assert( pFrom->nFree>=iToHdr );
53288     assert( get2byte(&aFrom[iFromHdr+5]) <= (int)pBt->usableSize );
53289 
53290     /* Copy the b-tree node content from page pFrom to page pTo. */
53291     iData = get2byte(&aFrom[iFromHdr+5]);
53292     memcpy(&aTo[iData], &aFrom[iData], pBt->usableSize-iData);
53293     memcpy(&aTo[iToHdr], &aFrom[iFromHdr], pFrom->cellOffset + 2*pFrom->nCell);
53294 
53295     /* Reinitialize page pTo so that the contents of the MemPage structure
53296     ** match the new data. The initialization of pTo can actually fail under
53297     ** fairly obscure circumstances, even though it is a copy of initialized
53298     ** page pFrom.
53299     */
53300     pTo->isInit = 0;
53301     rc = btreeInitPage(pTo);
53302     if( rc!=SQLITE_OK ){
53303       *pRC = rc;
53304       return;
53305     }
53306 
53307     /* If this is an auto-vacuum database, update the pointer-map entries
53308     ** for any b-tree or overflow pages that pTo now contains the pointers to.
53309     */
53310     if( ISAUTOVACUUM ){
53311       *pRC = setChildPtrmaps(pTo);
53312     }
53313   }
53314 }
53315 
53316 /*
53317 ** This routine redistributes cells on the iParentIdx'th child of pParent
53318 ** (hereafter "the page") and up to 2 siblings so that all pages have about the
53319 ** same amount of free space. Usually a single sibling on either side of the
53320 ** page are used in the balancing, though both siblings might come from one
53321 ** side if the page is the first or last child of its parent. If the page
53322 ** has fewer than 2 siblings (something which can only happen if the page
53323 ** is a root page or a child of a root page) then all available siblings
53324 ** participate in the balancing.
53325 **
53326 ** The number of siblings of the page might be increased or decreased by
53327 ** one or two in an effort to keep pages nearly full but not over full.
53328 **
53329 ** Note that when this routine is called, some of the cells on the page
53330 ** might not actually be stored in MemPage.aData[]. This can happen
53331 ** if the page is overfull. This routine ensures that all cells allocated
53332 ** to the page and its siblings fit into MemPage.aData[] before returning.
53333 **
53334 ** In the course of balancing the page and its siblings, cells may be
53335 ** inserted into or removed from the parent page (pParent). Doing so
53336 ** may cause the parent page to become overfull or underfull. If this
53337 ** happens, it is the responsibility of the caller to invoke the correct
53338 ** balancing routine to fix this problem (see the balance() routine).
53339 **
53340 ** If this routine fails for any reason, it might leave the database
53341 ** in a corrupted state. So if this routine fails, the database should
53342 ** be rolled back.
53343 **
53344 ** The third argument to this function, aOvflSpace, is a pointer to a
53345 ** buffer big enough to hold one page. If while inserting cells into the parent
53346 ** page (pParent) the parent page becomes overfull, this buffer is
53347 ** used to store the parent's overflow cells. Because this function inserts
53348 ** a maximum of four divider cells into the parent page, and the maximum
53349 ** size of a cell stored within an internal node is always less than 1/4
53350 ** of the page-size, the aOvflSpace[] buffer is guaranteed to be large
53351 ** enough for all overflow cells.
53352 **
53353 ** If aOvflSpace is set to a null pointer, this function returns
53354 ** SQLITE_NOMEM.
53355 */
53356 static int balance_nonroot(
53357   MemPage *pParent,               /* Parent page of siblings being balanced */
53358   int iParentIdx,                 /* Index of "the page" in pParent */
53359   u8 *aOvflSpace,                 /* page-size bytes of space for parent ovfl */
53360   int isRoot                      /* True if pParent is a root-page */
53361 ){
53362   BtShared *pBt;               /* The whole database */
53363   int nCell = 0;               /* Number of cells in apCell[] */
53364   int nMaxCells = 0;           /* Allocated size of apCell, szCell, aFrom. */
53365   int nNew = 0;                /* Number of pages in apNew[] */
53366   int nOld;                    /* Number of pages in apOld[] */
53367   int i, j, k;                 /* Loop counters */
53368   int nxDiv;                   /* Next divider slot in pParent->aCell[] */
53369   int rc = SQLITE_OK;          /* The return code */
53370   u16 leafCorrection;          /* 4 if pPage is a leaf.  0 if not */
53371   int leafData;                /* True if pPage is a leaf of a LEAFDATA tree */
53372   int usableSpace;             /* Bytes in pPage beyond the header */
53373   int pageFlags;               /* Value of pPage->aData[0] */
53374   int subtotal;                /* Subtotal of bytes in cells on one page */
53375   int iSpace1 = 0;             /* First unused byte of aSpace1[] */
53376   int iOvflSpace = 0;          /* First unused byte of aOvflSpace[] */
53377   int szScratch;               /* Size of scratch memory requested */
53378   MemPage *apOld[NB];          /* pPage and up to two siblings */
53379   MemPage *apCopy[NB];         /* Private copies of apOld[] pages */
53380   MemPage *apNew[NB+2];        /* pPage and up to NB siblings after balancing */
53381   u8 *pRight;                  /* Location in parent of right-sibling pointer */
53382   u8 *apDiv[NB-1];             /* Divider cells in pParent */
53383   int cntNew[NB+2];            /* Index in aCell[] of cell after i-th page */
53384   int szNew[NB+2];             /* Combined size of cells place on i-th page */
53385   u8 **apCell = 0;             /* All cells begin balanced */
53386   u16 *szCell;                 /* Local size of all cells in apCell[] */
53387   u8 *aSpace1;                 /* Space for copies of dividers cells */
53388   Pgno pgno;                   /* Temp var to store a page number in */
53389 
53390   pBt = pParent->pBt;
53391   assert( sqlite3_mutex_held(pBt->mutex) );
53392   assert( sqlite3PagerIswriteable(pParent->pDbPage) );
53393 
53394 #if 0
53395   TRACE(("BALANCE: begin page %d child of %d\n", pPage->pgno, pParent->pgno));
53396 #endif
53397 
53398   /* At this point pParent may have at most one overflow cell. And if
53399   ** this overflow cell is present, it must be the cell with
53400   ** index iParentIdx. This scenario comes about when this function
53401   ** is called (indirectly) from sqlite3BtreeDelete().
53402   */
53403   assert( pParent->nOverflow==0 || pParent->nOverflow==1 );
53404   assert( pParent->nOverflow==0 || pParent->aOvfl[0].idx==iParentIdx );
53405 
53406   if( !aOvflSpace ){
53407     return SQLITE_NOMEM;
53408   }
53409 
53410   /* Find the sibling pages to balance. Also locate the cells in pParent
53411   ** that divide the siblings. An attempt is made to find NN siblings on
53412   ** either side of pPage. More siblings are taken from one side, however,
53413   ** if there are fewer than NN siblings on the other side. If pParent
53414   ** has NB or fewer children then all children of pParent are taken.
53415   **
53416   ** This loop also drops the divider cells from the parent page. This
53417   ** way, the remainder of the function does not have to deal with any
53418   ** overflow cells in the parent page, since if any existed they will
53419   ** have already been removed.
53420   */
53421   i = pParent->nOverflow + pParent->nCell;
53422   if( i<2 ){
53423     nxDiv = 0;
53424     nOld = i+1;
53425   }else{
53426     nOld = 3;
53427     if( iParentIdx==0 ){
53428       nxDiv = 0;
53429     }else if( iParentIdx==i ){
53430       nxDiv = i-2;
53431     }else{
53432       nxDiv = iParentIdx-1;
53433     }
53434     i = 2;
53435   }
53436   if( (i+nxDiv-pParent->nOverflow)==pParent->nCell ){
53437     pRight = &pParent->aData[pParent->hdrOffset+8];
53438   }else{
53439     pRight = findCell(pParent, i+nxDiv-pParent->nOverflow);
53440   }
53441   pgno = get4byte(pRight);
53442   while( 1 ){
53443     rc = getAndInitPage(pBt, pgno, &apOld[i]);
53444     if( rc ){
53445       memset(apOld, 0, (i+1)*sizeof(MemPage*));
53446       goto balance_cleanup;
53447     }
53448     nMaxCells += 1+apOld[i]->nCell+apOld[i]->nOverflow;
53449     if( (i--)==0 ) break;
53450 
53451     if( i+nxDiv==pParent->aOvfl[0].idx && pParent->nOverflow ){
53452       apDiv[i] = pParent->aOvfl[0].pCell;
53453       pgno = get4byte(apDiv[i]);
53454       szNew[i] = cellSizePtr(pParent, apDiv[i]);
53455       pParent->nOverflow = 0;
53456     }else{
53457       apDiv[i] = findCell(pParent, i+nxDiv-pParent->nOverflow);
53458       pgno = get4byte(apDiv[i]);
53459       szNew[i] = cellSizePtr(pParent, apDiv[i]);
53460 
53461       /* Drop the cell from the parent page. apDiv[i] still points to
53462       ** the cell within the parent, even though it has been dropped.
53463       ** This is safe because dropping a cell only overwrites the first
53464       ** four bytes of it, and this function does not need the first
53465       ** four bytes of the divider cell. So the pointer is safe to use
53466       ** later on.
53467       **
53468       ** Unless SQLite is compiled in secure-delete mode. In this case,
53469       ** the dropCell() routine will overwrite the entire cell with zeroes.
53470       ** In this case, temporarily copy the cell into the aOvflSpace[]
53471       ** buffer. It will be copied out again as soon as the aSpace[] buffer
53472       ** is allocated.  */
53473       if( pBt->secureDelete ){
53474         int iOff = SQLITE_PTR_TO_INT(apDiv[i]) - SQLITE_PTR_TO_INT(pParent->aData);
53475         if( (iOff+szNew[i])>(int)pBt->usableSize ){
53476           rc = SQLITE_CORRUPT_BKPT;
53477           memset(apOld, 0, (i+1)*sizeof(MemPage*));
53478           goto balance_cleanup;
53479         }else{
53480           memcpy(&aOvflSpace[iOff], apDiv[i], szNew[i]);
53481           apDiv[i] = &aOvflSpace[apDiv[i]-pParent->aData];
53482         }
53483       }
53484       dropCell(pParent, i+nxDiv-pParent->nOverflow, szNew[i], &rc);
53485     }
53486   }
53487 
53488   /* Make nMaxCells a multiple of 4 in order to preserve 8-byte
53489   ** alignment */
53490   nMaxCells = (nMaxCells + 3)&~3;
53491 
53492   /*
53493   ** Allocate space for memory structures
53494   */
53495   k = pBt->pageSize + ROUND8(sizeof(MemPage));
53496   szScratch =
53497        nMaxCells*sizeof(u8*)                       /* apCell */
53498      + nMaxCells*sizeof(u16)                       /* szCell */
53499      + pBt->pageSize                               /* aSpace1 */
53500      + k*nOld;                                     /* Page copies (apCopy) */
53501   apCell = sqlite3ScratchMalloc( szScratch );
53502   if( apCell==0 ){
53503     rc = SQLITE_NOMEM;
53504     goto balance_cleanup;
53505   }
53506   szCell = (u16*)&apCell[nMaxCells];
53507   aSpace1 = (u8*)&szCell[nMaxCells];
53508   assert( EIGHT_BYTE_ALIGNMENT(aSpace1) );
53509 
53510   /*
53511   ** Load pointers to all cells on sibling pages and the divider cells
53512   ** into the local apCell[] array.  Make copies of the divider cells
53513   ** into space obtained from aSpace1[] and remove the the divider Cells
53514   ** from pParent.
53515   **
53516   ** If the siblings are on leaf pages, then the child pointers of the
53517   ** divider cells are stripped from the cells before they are copied
53518   ** into aSpace1[].  In this way, all cells in apCell[] are without
53519   ** child pointers.  If siblings are not leaves, then all cell in
53520   ** apCell[] include child pointers.  Either way, all cells in apCell[]
53521   ** are alike.
53522   **
53523   ** leafCorrection:  4 if pPage is a leaf.  0 if pPage is not a leaf.
53524   **       leafData:  1 if pPage holds key+data and pParent holds only keys.
53525   */
53526   leafCorrection = apOld[0]->leaf*4;
53527   leafData = apOld[0]->hasData;
53528   for(i=0; i<nOld; i++){
53529     int limit;
53530 
53531     /* Before doing anything else, take a copy of the i'th original sibling
53532     ** The rest of this function will use data from the copies rather
53533     ** that the original pages since the original pages will be in the
53534     ** process of being overwritten.  */
53535     MemPage *pOld = apCopy[i] = (MemPage*)&aSpace1[pBt->pageSize + k*i];
53536     memcpy(pOld, apOld[i], sizeof(MemPage));
53537     pOld->aData = (void*)&pOld[1];
53538     memcpy(pOld->aData, apOld[i]->aData, pBt->pageSize);
53539 
53540     limit = pOld->nCell+pOld->nOverflow;
53541     if( pOld->nOverflow>0 ){
53542       for(j=0; j<limit; j++){
53543         assert( nCell<nMaxCells );
53544         apCell[nCell] = findOverflowCell(pOld, j);
53545         szCell[nCell] = cellSizePtr(pOld, apCell[nCell]);
53546         nCell++;
53547       }
53548     }else{
53549       u8 *aData = pOld->aData;
53550       u16 maskPage = pOld->maskPage;
53551       u16 cellOffset = pOld->cellOffset;
53552       for(j=0; j<limit; j++){
53553         assert( nCell<nMaxCells );
53554         apCell[nCell] = findCellv2(aData, maskPage, cellOffset, j);
53555         szCell[nCell] = cellSizePtr(pOld, apCell[nCell]);
53556         nCell++;
53557       }
53558     }
53559     if( i<nOld-1 && !leafData){
53560       u16 sz = (u16)szNew[i];
53561       u8 *pTemp;
53562       assert( nCell<nMaxCells );
53563       szCell[nCell] = sz;
53564       pTemp = &aSpace1[iSpace1];
53565       iSpace1 += sz;
53566       assert( sz<=pBt->maxLocal+23 );
53567       assert( iSpace1 <= (int)pBt->pageSize );
53568       memcpy(pTemp, apDiv[i], sz);
53569       apCell[nCell] = pTemp+leafCorrection;
53570       assert( leafCorrection==0 || leafCorrection==4 );
53571       szCell[nCell] = szCell[nCell] - leafCorrection;
53572       if( !pOld->leaf ){
53573         assert( leafCorrection==0 );
53574         assert( pOld->hdrOffset==0 );
53575         /* The right pointer of the child page pOld becomes the left
53576         ** pointer of the divider cell */
53577         memcpy(apCell[nCell], &pOld->aData[8], 4);
53578       }else{
53579         assert( leafCorrection==4 );
53580         if( szCell[nCell]<4 ){
53581           /* Do not allow any cells smaller than 4 bytes. */
53582           szCell[nCell] = 4;
53583         }
53584       }
53585       nCell++;
53586     }
53587   }
53588 
53589   /*
53590   ** Figure out the number of pages needed to hold all nCell cells.
53591   ** Store this number in "k".  Also compute szNew[] which is the total
53592   ** size of all cells on the i-th page and cntNew[] which is the index
53593   ** in apCell[] of the cell that divides page i from page i+1.
53594   ** cntNew[k] should equal nCell.
53595   **
53596   ** Values computed by this block:
53597   **
53598   **           k: The total number of sibling pages
53599   **    szNew[i]: Spaced used on the i-th sibling page.
53600   **   cntNew[i]: Index in apCell[] and szCell[] for the first cell to
53601   **              the right of the i-th sibling page.
53602   ** usableSpace: Number of bytes of space available on each sibling.
53603   **
53604   */
53605   usableSpace = pBt->usableSize - 12 + leafCorrection;
53606   for(subtotal=k=i=0; i<nCell; i++){
53607     assert( i<nMaxCells );
53608     subtotal += szCell[i] + 2;
53609     if( subtotal > usableSpace ){
53610       szNew[k] = subtotal - szCell[i];
53611       cntNew[k] = i;
53612       if( leafData ){ i--; }
53613       subtotal = 0;
53614       k++;
53615       if( k>NB+1 ){ rc = SQLITE_CORRUPT_BKPT; goto balance_cleanup; }
53616     }
53617   }
53618   szNew[k] = subtotal;
53619   cntNew[k] = nCell;
53620   k++;
53621 
53622   /*
53623   ** The packing computed by the previous block is biased toward the siblings
53624   ** on the left side.  The left siblings are always nearly full, while the
53625   ** right-most sibling might be nearly empty.  This block of code attempts
53626   ** to adjust the packing of siblings to get a better balance.
53627   **
53628   ** This adjustment is more than an optimization.  The packing above might
53629   ** be so out of balance as to be illegal.  For example, the right-most
53630   ** sibling might be completely empty.  This adjustment is not optional.
53631   */
53632   for(i=k-1; i>0; i--){
53633     int szRight = szNew[i];  /* Size of sibling on the right */
53634     int szLeft = szNew[i-1]; /* Size of sibling on the left */
53635     int r;              /* Index of right-most cell in left sibling */
53636     int d;              /* Index of first cell to the left of right sibling */
53637 
53638     r = cntNew[i-1] - 1;
53639     d = r + 1 - leafData;
53640     assert( d<nMaxCells );
53641     assert( r<nMaxCells );
53642     while( szRight==0 || szRight+szCell[d]+2<=szLeft-(szCell[r]+2) ){
53643       szRight += szCell[d] + 2;
53644       szLeft -= szCell[r] + 2;
53645       cntNew[i-1]--;
53646       r = cntNew[i-1] - 1;
53647       d = r + 1 - leafData;
53648     }
53649     szNew[i] = szRight;
53650     szNew[i-1] = szLeft;
53651   }
53652 
53653   /* Either we found one or more cells (cntnew[0])>0) or pPage is
53654   ** a virtual root page.  A virtual root page is when the real root
53655   ** page is page 1 and we are the only child of that page.
53656   */
53657   assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) );
53658 
53659   TRACE(("BALANCE: old: %d %d %d  ",
53660     apOld[0]->pgno,
53661     nOld>=2 ? apOld[1]->pgno : 0,
53662     nOld>=3 ? apOld[2]->pgno : 0
53663   ));
53664 
53665   /*
53666   ** Allocate k new pages.  Reuse old pages where possible.
53667   */
53668   if( apOld[0]->pgno<=1 ){
53669     rc = SQLITE_CORRUPT_BKPT;
53670     goto balance_cleanup;
53671   }
53672   pageFlags = apOld[0]->aData[0];
53673   for(i=0; i<k; i++){
53674     MemPage *pNew;
53675     if( i<nOld ){
53676       pNew = apNew[i] = apOld[i];
53677       apOld[i] = 0;
53678       rc = sqlite3PagerWrite(pNew->pDbPage);
53679       nNew++;
53680       if( rc ) goto balance_cleanup;
53681     }else{
53682       assert( i>0 );
53683       rc = allocateBtreePage(pBt, &pNew, &pgno, pgno, 0);
53684       if( rc ) goto balance_cleanup;
53685       apNew[i] = pNew;
53686       nNew++;
53687 
53688       /* Set the pointer-map entry for the new sibling page. */
53689       if( ISAUTOVACUUM ){
53690         ptrmapPut(pBt, pNew->pgno, PTRMAP_BTREE, pParent->pgno, &rc);
53691         if( rc!=SQLITE_OK ){
53692           goto balance_cleanup;
53693         }
53694       }
53695     }
53696   }
53697 
53698   /* Free any old pages that were not reused as new pages.
53699   */
53700   while( i<nOld ){
53701     freePage(apOld[i], &rc);
53702     if( rc ) goto balance_cleanup;
53703     releasePage(apOld[i]);
53704     apOld[i] = 0;
53705     i++;
53706   }
53707 
53708   /*
53709   ** Put the new pages in accending order.  This helps to
53710   ** keep entries in the disk file in order so that a scan
53711   ** of the table is a linear scan through the file.  That
53712   ** in turn helps the operating system to deliver pages
53713   ** from the disk more rapidly.
53714   **
53715   ** An O(n^2) insertion sort algorithm is used, but since
53716   ** n is never more than NB (a small constant), that should
53717   ** not be a problem.
53718   **
53719   ** When NB==3, this one optimization makes the database
53720   ** about 25% faster for large insertions and deletions.
53721   */
53722   for(i=0; i<k-1; i++){
53723     int minV = apNew[i]->pgno;
53724     int minI = i;
53725     for(j=i+1; j<k; j++){
53726       if( apNew[j]->pgno<(unsigned)minV ){
53727         minI = j;
53728         minV = apNew[j]->pgno;
53729       }
53730     }
53731     if( minI>i ){
53732       MemPage *pT;
53733       pT = apNew[i];
53734       apNew[i] = apNew[minI];
53735       apNew[minI] = pT;
53736     }
53737   }
53738   TRACE(("new: %d(%d) %d(%d) %d(%d) %d(%d) %d(%d)\n",
53739     apNew[0]->pgno, szNew[0],
53740     nNew>=2 ? apNew[1]->pgno : 0, nNew>=2 ? szNew[1] : 0,
53741     nNew>=3 ? apNew[2]->pgno : 0, nNew>=3 ? szNew[2] : 0,
53742     nNew>=4 ? apNew[3]->pgno : 0, nNew>=4 ? szNew[3] : 0,
53743     nNew>=5 ? apNew[4]->pgno : 0, nNew>=5 ? szNew[4] : 0));
53744 
53745   assert( sqlite3PagerIswriteable(pParent->pDbPage) );
53746   put4byte(pRight, apNew[nNew-1]->pgno);
53747 
53748   /*
53749   ** Evenly distribute the data in apCell[] across the new pages.
53750   ** Insert divider cells into pParent as necessary.
53751   */
53752   j = 0;
53753   for(i=0; i<nNew; i++){
53754     /* Assemble the new sibling page. */
53755     MemPage *pNew = apNew[i];
53756     assert( j<nMaxCells );
53757     zeroPage(pNew, pageFlags);
53758     assemblePage(pNew, cntNew[i]-j, &apCell[j], &szCell[j]);
53759     assert( pNew->nCell>0 || (nNew==1 && cntNew[0]==0) );
53760     assert( pNew->nOverflow==0 );
53761 
53762     j = cntNew[i];
53763 
53764     /* If the sibling page assembled above was not the right-most sibling,
53765     ** insert a divider cell into the parent page.
53766     */
53767     assert( i<nNew-1 || j==nCell );
53768     if( j<nCell ){
53769       u8 *pCell;
53770       u8 *pTemp;
53771       int sz;
53772 
53773       assert( j<nMaxCells );
53774       pCell = apCell[j];
53775       sz = szCell[j] + leafCorrection;
53776       pTemp = &aOvflSpace[iOvflSpace];
53777       if( !pNew->leaf ){
53778         memcpy(&pNew->aData[8], pCell, 4);
53779       }else if( leafData ){
53780         /* If the tree is a leaf-data tree, and the siblings are leaves,
53781         ** then there is no divider cell in apCell[]. Instead, the divider
53782         ** cell consists of the integer key for the right-most cell of
53783         ** the sibling-page assembled above only.
53784         */
53785         CellInfo info;
53786         j--;
53787         btreeParseCellPtr(pNew, apCell[j], &info);
53788         pCell = pTemp;
53789         sz = 4 + putVarint(&pCell[4], info.nKey);
53790         pTemp = 0;
53791       }else{
53792         pCell -= 4;
53793         /* Obscure case for non-leaf-data trees: If the cell at pCell was
53794         ** previously stored on a leaf node, and its reported size was 4
53795         ** bytes, then it may actually be smaller than this
53796         ** (see btreeParseCellPtr(), 4 bytes is the minimum size of
53797         ** any cell). But it is important to pass the correct size to
53798         ** insertCell(), so reparse the cell now.
53799         **
53800         ** Note that this can never happen in an SQLite data file, as all
53801         ** cells are at least 4 bytes. It only happens in b-trees used
53802         ** to evaluate "IN (SELECT ...)" and similar clauses.
53803         */
53804         if( szCell[j]==4 ){
53805           assert(leafCorrection==4);
53806           sz = cellSizePtr(pParent, pCell);
53807         }
53808       }
53809       iOvflSpace += sz;
53810       assert( sz<=pBt->maxLocal+23 );
53811       assert( iOvflSpace <= (int)pBt->pageSize );
53812       insertCell(pParent, nxDiv, pCell, sz, pTemp, pNew->pgno, &rc);
53813       if( rc!=SQLITE_OK ) goto balance_cleanup;
53814       assert( sqlite3PagerIswriteable(pParent->pDbPage) );
53815 
53816       j++;
53817       nxDiv++;
53818     }
53819   }
53820   assert( j==nCell );
53821   assert( nOld>0 );
53822   assert( nNew>0 );
53823   if( (pageFlags & PTF_LEAF)==0 ){
53824     u8 *zChild = &apCopy[nOld-1]->aData[8];
53825     memcpy(&apNew[nNew-1]->aData[8], zChild, 4);
53826   }
53827 
53828   if( isRoot && pParent->nCell==0 && pParent->hdrOffset<=apNew[0]->nFree ){
53829     /* The root page of the b-tree now contains no cells. The only sibling
53830     ** page is the right-child of the parent. Copy the contents of the
53831     ** child page into the parent, decreasing the overall height of the
53832     ** b-tree structure by one. This is described as the "balance-shallower"
53833     ** sub-algorithm in some documentation.
53834     **
53835     ** If this is an auto-vacuum database, the call to copyNodeContent()
53836     ** sets all pointer-map entries corresponding to database image pages
53837     ** for which the pointer is stored within the content being copied.
53838     **
53839     ** The second assert below verifies that the child page is defragmented
53840     ** (it must be, as it was just reconstructed using assemblePage()). This
53841     ** is important if the parent page happens to be page 1 of the database
53842     ** image.  */
53843     assert( nNew==1 );
53844     assert( apNew[0]->nFree ==
53845         (get2byte(&apNew[0]->aData[5])-apNew[0]->cellOffset-apNew[0]->nCell*2)
53846     );
53847     copyNodeContent(apNew[0], pParent, &rc);
53848     freePage(apNew[0], &rc);
53849   }else if( ISAUTOVACUUM ){
53850     /* Fix the pointer-map entries for all the cells that were shifted around.
53851     ** There are several different types of pointer-map entries that need to
53852     ** be dealt with by this routine. Some of these have been set already, but
53853     ** many have not. The following is a summary:
53854     **
53855     **   1) The entries associated with new sibling pages that were not
53856     **      siblings when this function was called. These have already
53857     **      been set. We don't need to worry about old siblings that were
53858     **      moved to the free-list - the freePage() code has taken care
53859     **      of those.
53860     **
53861     **   2) The pointer-map entries associated with the first overflow
53862     **      page in any overflow chains used by new divider cells. These
53863     **      have also already been taken care of by the insertCell() code.
53864     **
53865     **   3) If the sibling pages are not leaves, then the child pages of
53866     **      cells stored on the sibling pages may need to be updated.
53867     **
53868     **   4) If the sibling pages are not internal intkey nodes, then any
53869     **      overflow pages used by these cells may need to be updated
53870     **      (internal intkey nodes never contain pointers to overflow pages).
53871     **
53872     **   5) If the sibling pages are not leaves, then the pointer-map
53873     **      entries for the right-child pages of each sibling may need
53874     **      to be updated.
53875     **
53876     ** Cases 1 and 2 are dealt with above by other code. The next
53877     ** block deals with cases 3 and 4 and the one after that, case 5. Since
53878     ** setting a pointer map entry is a relatively expensive operation, this
53879     ** code only sets pointer map entries for child or overflow pages that have
53880     ** actually moved between pages.  */
53881     MemPage *pNew = apNew[0];
53882     MemPage *pOld = apCopy[0];
53883     int nOverflow = pOld->nOverflow;
53884     int iNextOld = pOld->nCell + nOverflow;
53885     int iOverflow = (nOverflow ? pOld->aOvfl[0].idx : -1);
53886     j = 0;                             /* Current 'old' sibling page */
53887     k = 0;                             /* Current 'new' sibling page */
53888     for(i=0; i<nCell; i++){
53889       int isDivider = 0;
53890       while( i==iNextOld ){
53891         /* Cell i is the cell immediately following the last cell on old
53892         ** sibling page j. If the siblings are not leaf pages of an
53893         ** intkey b-tree, then cell i was a divider cell. */
53894         pOld = apCopy[++j];
53895         iNextOld = i + !leafData + pOld->nCell + pOld->nOverflow;
53896         if( pOld->nOverflow ){
53897           nOverflow = pOld->nOverflow;
53898           iOverflow = i + !leafData + pOld->aOvfl[0].idx;
53899         }
53900         isDivider = !leafData;
53901       }
53902 
53903       assert(nOverflow>0 || iOverflow<i );
53904       assert(nOverflow<2 || pOld->aOvfl[0].idx==pOld->aOvfl[1].idx-1);
53905       assert(nOverflow<3 || pOld->aOvfl[1].idx==pOld->aOvfl[2].idx-1);
53906       if( i==iOverflow ){
53907         isDivider = 1;
53908         if( (--nOverflow)>0 ){
53909           iOverflow++;
53910         }
53911       }
53912 
53913       if( i==cntNew[k] ){
53914         /* Cell i is the cell immediately following the last cell on new
53915         ** sibling page k. If the siblings are not leaf pages of an
53916         ** intkey b-tree, then cell i is a divider cell.  */
53917         pNew = apNew[++k];
53918         if( !leafData ) continue;
53919       }
53920       assert( j<nOld );
53921       assert( k<nNew );
53922 
53923       /* If the cell was originally divider cell (and is not now) or
53924       ** an overflow cell, or if the cell was located on a different sibling
53925       ** page before the balancing, then the pointer map entries associated
53926       ** with any child or overflow pages need to be updated.  */
53927       if( isDivider || pOld->pgno!=pNew->pgno ){
53928         if( !leafCorrection ){
53929           ptrmapPut(pBt, get4byte(apCell[i]), PTRMAP_BTREE, pNew->pgno, &rc);
53930         }
53931         if( szCell[i]>pNew->minLocal ){
53932           ptrmapPutOvflPtr(pNew, apCell[i], &rc);
53933         }
53934       }
53935     }
53936 
53937     if( !leafCorrection ){
53938       for(i=0; i<nNew; i++){
53939         u32 key = get4byte(&apNew[i]->aData[8]);
53940         ptrmapPut(pBt, key, PTRMAP_BTREE, apNew[i]->pgno, &rc);
53941       }
53942     }
53943 
53944 #if 0
53945     /* The ptrmapCheckPages() contains assert() statements that verify that
53946     ** all pointer map pages are set correctly. This is helpful while
53947     ** debugging. This is usually disabled because a corrupt database may
53948     ** cause an assert() statement to fail.  */
53949     ptrmapCheckPages(apNew, nNew);
53950     ptrmapCheckPages(&pParent, 1);
53951 #endif
53952   }
53953 
53954   assert( pParent->isInit );
53955   TRACE(("BALANCE: finished: old=%d new=%d cells=%d\n",
53956           nOld, nNew, nCell));
53957 
53958   /*
53959   ** Cleanup before returning.
53960   */
53961 balance_cleanup:
53962   sqlite3ScratchFree(apCell);
53963   for(i=0; i<nOld; i++){
53964     releasePage(apOld[i]);
53965   }
53966   for(i=0; i<nNew; i++){
53967     releasePage(apNew[i]);
53968   }
53969 
53970   return rc;
53971 }
53972 
53973 
53974 /*
53975 ** This function is called when the root page of a b-tree structure is
53976 ** overfull (has one or more overflow pages).
53977 **
53978 ** A new child page is allocated and the contents of the current root
53979 ** page, including overflow cells, are copied into the child. The root
53980 ** page is then overwritten to make it an empty page with the right-child
53981 ** pointer pointing to the new page.
53982 **
53983 ** Before returning, all pointer-map entries corresponding to pages
53984 ** that the new child-page now contains pointers to are updated. The
53985 ** entry corresponding to the new right-child pointer of the root
53986 ** page is also updated.
53987 **
53988 ** If successful, *ppChild is set to contain a reference to the child
53989 ** page and SQLITE_OK is returned. In this case the caller is required
53990 ** to call releasePage() on *ppChild exactly once. If an error occurs,
53991 ** an error code is returned and *ppChild is set to 0.
53992 */
53993 static int balance_deeper(MemPage *pRoot, MemPage **ppChild){
53994   int rc;                        /* Return value from subprocedures */
53995   MemPage *pChild = 0;           /* Pointer to a new child page */
53996   Pgno pgnoChild = 0;            /* Page number of the new child page */
53997   BtShared *pBt = pRoot->pBt;    /* The BTree */
53998 
53999   assert( pRoot->nOverflow>0 );
54000   assert( sqlite3_mutex_held(pBt->mutex) );
54001 
54002   /* Make pRoot, the root page of the b-tree, writable. Allocate a new
54003   ** page that will become the new right-child of pPage. Copy the contents
54004   ** of the node stored on pRoot into the new child page.
54005   */
54006   rc = sqlite3PagerWrite(pRoot->pDbPage);
54007   if( rc==SQLITE_OK ){
54008     rc = allocateBtreePage(pBt,&pChild,&pgnoChild,pRoot->pgno,0);
54009     copyNodeContent(pRoot, pChild, &rc);
54010     if( ISAUTOVACUUM ){
54011       ptrmapPut(pBt, pgnoChild, PTRMAP_BTREE, pRoot->pgno, &rc);
54012     }
54013   }
54014   if( rc ){
54015     *ppChild = 0;
54016     releasePage(pChild);
54017     return rc;
54018   }
54019   assert( sqlite3PagerIswriteable(pChild->pDbPage) );
54020   assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
54021   assert( pChild->nCell==pRoot->nCell );
54022 
54023   TRACE(("BALANCE: copy root %d into %d\n", pRoot->pgno, pChild->pgno));
54024 
54025   /* Copy the overflow cells from pRoot to pChild */
54026   memcpy(pChild->aOvfl, pRoot->aOvfl, pRoot->nOverflow*sizeof(pRoot->aOvfl[0]));
54027   pChild->nOverflow = pRoot->nOverflow;
54028 
54029   /* Zero the contents of pRoot. Then install pChild as the right-child. */
54030   zeroPage(pRoot, pChild->aData[0] & ~PTF_LEAF);
54031   put4byte(&pRoot->aData[pRoot->hdrOffset+8], pgnoChild);
54032 
54033   *ppChild = pChild;
54034   return SQLITE_OK;
54035 }
54036 
54037 /*
54038 ** The page that pCur currently points to has just been modified in
54039 ** some way. This function figures out if this modification means the
54040 ** tree needs to be balanced, and if so calls the appropriate balancing
54041 ** routine. Balancing routines are:
54042 **
54043 **   balance_quick()
54044 **   balance_deeper()
54045 **   balance_nonroot()
54046 */
54047 static int balance(BtCursor *pCur){
54048   int rc = SQLITE_OK;
54049   const int nMin = pCur->pBt->usableSize * 2 / 3;
54050   u8 aBalanceQuickSpace[13];
54051   u8 *pFree = 0;
54052 
54053   TESTONLY( int balance_quick_called = 0 );
54054   TESTONLY( int balance_deeper_called = 0 );
54055 
54056   do {
54057     int iPage = pCur->iPage;
54058     MemPage *pPage = pCur->apPage[iPage];
54059 
54060     if( iPage==0 ){
54061       if( pPage->nOverflow ){
54062         /* The root page of the b-tree is overfull. In this case call the
54063         ** balance_deeper() function to create a new child for the root-page
54064         ** and copy the current contents of the root-page to it. The
54065         ** next iteration of the do-loop will balance the child page.
54066         */
54067         assert( (balance_deeper_called++)==0 );
54068         rc = balance_deeper(pPage, &pCur->apPage[1]);
54069         if( rc==SQLITE_OK ){
54070           pCur->iPage = 1;
54071           pCur->aiIdx[0] = 0;
54072           pCur->aiIdx[1] = 0;
54073           assert( pCur->apPage[1]->nOverflow );
54074         }
54075       }else{
54076         break;
54077       }
54078     }else if( pPage->nOverflow==0 && pPage->nFree<=nMin ){
54079       break;
54080     }else{
54081       MemPage * const pParent = pCur->apPage[iPage-1];
54082       int const iIdx = pCur->aiIdx[iPage-1];
54083 
54084       rc = sqlite3PagerWrite(pParent->pDbPage);
54085       if( rc==SQLITE_OK ){
54086 #ifndef SQLITE_OMIT_QUICKBALANCE
54087         if( pPage->hasData
54088          && pPage->nOverflow==1
54089          && pPage->aOvfl[0].idx==pPage->nCell
54090          && pParent->pgno!=1
54091          && pParent->nCell==iIdx
54092         ){
54093           /* Call balance_quick() to create a new sibling of pPage on which
54094           ** to store the overflow cell. balance_quick() inserts a new cell
54095           ** into pParent, which may cause pParent overflow. If this
54096           ** happens, the next interation of the do-loop will balance pParent
54097           ** use either balance_nonroot() or balance_deeper(). Until this
54098           ** happens, the overflow cell is stored in the aBalanceQuickSpace[]
54099           ** buffer.
54100           **
54101           ** The purpose of the following assert() is to check that only a
54102           ** single call to balance_quick() is made for each call to this
54103           ** function. If this were not verified, a subtle bug involving reuse
54104           ** of the aBalanceQuickSpace[] might sneak in.
54105           */
54106           assert( (balance_quick_called++)==0 );
54107           rc = balance_quick(pParent, pPage, aBalanceQuickSpace);
54108         }else
54109 #endif
54110         {
54111           /* In this case, call balance_nonroot() to redistribute cells
54112           ** between pPage and up to 2 of its sibling pages. This involves
54113           ** modifying the contents of pParent, which may cause pParent to
54114           ** become overfull or underfull. The next iteration of the do-loop
54115           ** will balance the parent page to correct this.
54116           **
54117           ** If the parent page becomes overfull, the overflow cell or cells
54118           ** are stored in the pSpace buffer allocated immediately below.
54119           ** A subsequent iteration of the do-loop will deal with this by
54120           ** calling balance_nonroot() (balance_deeper() may be called first,
54121           ** but it doesn't deal with overflow cells - just moves them to a
54122           ** different page). Once this subsequent call to balance_nonroot()
54123           ** has completed, it is safe to release the pSpace buffer used by
54124           ** the previous call, as the overflow cell data will have been
54125           ** copied either into the body of a database page or into the new
54126           ** pSpace buffer passed to the latter call to balance_nonroot().
54127           */
54128           u8 *pSpace = sqlite3PageMalloc(pCur->pBt->pageSize);
54129           rc = balance_nonroot(pParent, iIdx, pSpace, iPage==1);
54130           if( pFree ){
54131             /* If pFree is not NULL, it points to the pSpace buffer used
54132             ** by a previous call to balance_nonroot(). Its contents are
54133             ** now stored either on real database pages or within the
54134             ** new pSpace buffer, so it may be safely freed here. */
54135             sqlite3PageFree(pFree);
54136           }
54137 
54138           /* The pSpace buffer will be freed after the next call to
54139           ** balance_nonroot(), or just before this function returns, whichever
54140           ** comes first. */
54141           pFree = pSpace;
54142         }
54143       }
54144 
54145       pPage->nOverflow = 0;
54146 
54147       /* The next iteration of the do-loop balances the parent page. */
54148       releasePage(pPage);
54149       pCur->iPage--;
54150     }
54151   }while( rc==SQLITE_OK );
54152 
54153   if( pFree ){
54154     sqlite3PageFree(pFree);
54155   }
54156   return rc;
54157 }
54158 
54159 
54160 /*
54161 ** Insert a new record into the BTree.  The key is given by (pKey,nKey)
54162 ** and the data is given by (pData,nData).  The cursor is used only to
54163 ** define what table the record should be inserted into.  The cursor
54164 ** is left pointing at a random location.
54165 **
54166 ** For an INTKEY table, only the nKey value of the key is used.  pKey is
54167 ** ignored.  For a ZERODATA table, the pData and nData are both ignored.
54168 **
54169 ** If the seekResult parameter is non-zero, then a successful call to
54170 ** MovetoUnpacked() to seek cursor pCur to (pKey, nKey) has already
54171 ** been performed. seekResult is the search result returned (a negative
54172 ** number if pCur points at an entry that is smaller than (pKey, nKey), or
54173 ** a positive value if pCur points at an etry that is larger than
54174 ** (pKey, nKey)).
54175 **
54176 ** If the seekResult parameter is non-zero, then the caller guarantees that
54177 ** cursor pCur is pointing at the existing copy of a row that is to be
54178 ** overwritten.  If the seekResult parameter is 0, then cursor pCur may
54179 ** point to any entry or to no entry at all and so this function has to seek
54180 ** the cursor before the new key can be inserted.
54181 */
54182 SQLITE_PRIVATE int sqlite3BtreeInsert(
54183   BtCursor *pCur,                /* Insert data into the table of this cursor */
54184   const void *pKey, i64 nKey,    /* The key of the new record */
54185   const void *pData, int nData,  /* The data of the new record */
54186   int nZero,                     /* Number of extra 0 bytes to append to data */
54187   int appendBias,                /* True if this is likely an append */
54188   int seekResult                 /* Result of prior MovetoUnpacked() call */
54189 ){
54190   int rc;
54191   int loc = seekResult;          /* -1: before desired location  +1: after */
54192   int szNew = 0;
54193   int idx;
54194   MemPage *pPage;
54195   Btree *p = pCur->pBtree;
54196   BtShared *pBt = p->pBt;
54197   unsigned char *oldCell;
54198   unsigned char *newCell = 0;
54199 
54200   if( pCur->eState==CURSOR_FAULT ){
54201     assert( pCur->skipNext!=SQLITE_OK );
54202     return pCur->skipNext;
54203   }
54204 
54205   assert( cursorHoldsMutex(pCur) );
54206   assert( pCur->wrFlag && pBt->inTransaction==TRANS_WRITE && !pBt->readOnly );
54207   assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
54208 
54209   /* Assert that the caller has been consistent. If this cursor was opened
54210   ** expecting an index b-tree, then the caller should be inserting blob
54211   ** keys with no associated data. If the cursor was opened expecting an
54212   ** intkey table, the caller should be inserting integer keys with a
54213   ** blob of associated data.  */
54214   assert( (pKey==0)==(pCur->pKeyInfo==0) );
54215 
54216   /* If this is an insert into a table b-tree, invalidate any incrblob
54217   ** cursors open on the row being replaced (assuming this is a replace
54218   ** operation - if it is not, the following is a no-op).  */
54219   if( pCur->pKeyInfo==0 ){
54220     invalidateIncrblobCursors(p, nKey, 0);
54221   }
54222 
54223   /* Save the positions of any other cursors open on this table.
54224   **
54225   ** In some cases, the call to btreeMoveto() below is a no-op. For
54226   ** example, when inserting data into a table with auto-generated integer
54227   ** keys, the VDBE layer invokes sqlite3BtreeLast() to figure out the
54228   ** integer key to use. It then calls this function to actually insert the
54229   ** data into the intkey B-Tree. In this case btreeMoveto() recognizes
54230   ** that the cursor is already where it needs to be and returns without
54231   ** doing any work. To avoid thwarting these optimizations, it is important
54232   ** not to clear the cursor here.
54233   */
54234   rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
54235   if( rc ) return rc;
54236   if( !loc ){
54237     rc = btreeMoveto(pCur, pKey, nKey, appendBias, &loc);
54238     if( rc ) return rc;
54239   }
54240   assert( pCur->eState==CURSOR_VALID || (pCur->eState==CURSOR_INVALID && loc) );
54241 
54242   pPage = pCur->apPage[pCur->iPage];
54243   assert( pPage->intKey || nKey>=0 );
54244   assert( pPage->leaf || !pPage->intKey );
54245 
54246   TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n",
54247           pCur->pgnoRoot, nKey, nData, pPage->pgno,
54248           loc==0 ? "overwrite" : "new entry"));
54249   assert( pPage->isInit );
54250   allocateTempSpace(pBt);
54251   newCell = pBt->pTmpSpace;
54252   if( newCell==0 ) return SQLITE_NOMEM;
54253   rc = fillInCell(pPage, newCell, pKey, nKey, pData, nData, nZero, &szNew);
54254   if( rc ) goto end_insert;
54255   assert( szNew==cellSizePtr(pPage, newCell) );
54256   assert( szNew <= MX_CELL_SIZE(pBt) );
54257   idx = pCur->aiIdx[pCur->iPage];
54258   if( loc==0 ){
54259     u16 szOld;
54260     assert( idx<pPage->nCell );
54261     rc = sqlite3PagerWrite(pPage->pDbPage);
54262     if( rc ){
54263       goto end_insert;
54264     }
54265     oldCell = findCell(pPage, idx);
54266     if( !pPage->leaf ){
54267       memcpy(newCell, oldCell, 4);
54268     }
54269     szOld = cellSizePtr(pPage, oldCell);
54270     rc = clearCell(pPage, oldCell);
54271     dropCell(pPage, idx, szOld, &rc);
54272     if( rc ) goto end_insert;
54273   }else if( loc<0 && pPage->nCell>0 ){
54274     assert( pPage->leaf );
54275     idx = ++pCur->aiIdx[pCur->iPage];
54276   }else{
54277     assert( pPage->leaf );
54278   }
54279   insertCell(pPage, idx, newCell, szNew, 0, 0, &rc);
54280   assert( rc!=SQLITE_OK || pPage->nCell>0 || pPage->nOverflow>0 );
54281 
54282   /* If no error has occured and pPage has an overflow cell, call balance()
54283   ** to redistribute the cells within the tree. Since balance() may move
54284   ** the cursor, zero the BtCursor.info.nSize and BtCursor.validNKey
54285   ** variables.
54286   **
54287   ** Previous versions of SQLite called moveToRoot() to move the cursor
54288   ** back to the root page as balance() used to invalidate the contents
54289   ** of BtCursor.apPage[] and BtCursor.aiIdx[]. Instead of doing that,
54290   ** set the cursor state to "invalid". This makes common insert operations
54291   ** slightly faster.
54292   **
54293   ** There is a subtle but important optimization here too. When inserting
54294   ** multiple records into an intkey b-tree using a single cursor (as can
54295   ** happen while processing an "INSERT INTO ... SELECT" statement), it
54296   ** is advantageous to leave the cursor pointing to the last entry in
54297   ** the b-tree if possible. If the cursor is left pointing to the last
54298   ** entry in the table, and the next row inserted has an integer key
54299   ** larger than the largest existing key, it is possible to insert the
54300   ** row without seeking the cursor. This can be a big performance boost.
54301   */
54302   pCur->info.nSize = 0;
54303   pCur->validNKey = 0;
54304   if( rc==SQLITE_OK && pPage->nOverflow ){
54305     rc = balance(pCur);
54306 
54307     /* Must make sure nOverflow is reset to zero even if the balance()
54308     ** fails. Internal data structure corruption will result otherwise.
54309     ** Also, set the cursor state to invalid. This stops saveCursorPosition()
54310     ** from trying to save the current position of the cursor.  */
54311     pCur->apPage[pCur->iPage]->nOverflow = 0;
54312     pCur->eState = CURSOR_INVALID;
54313   }
54314   assert( pCur->apPage[pCur->iPage]->nOverflow==0 );
54315 
54316 end_insert:
54317   return rc;
54318 }
54319 
54320 /*
54321 ** Delete the entry that the cursor is pointing to.  The cursor
54322 ** is left pointing at a arbitrary location.
54323 */
54324 SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor *pCur){
54325   Btree *p = pCur->pBtree;
54326   BtShared *pBt = p->pBt;
54327   int rc;                              /* Return code */
54328   MemPage *pPage;                      /* Page to delete cell from */
54329   unsigned char *pCell;                /* Pointer to cell to delete */
54330   int iCellIdx;                        /* Index of cell to delete */
54331   int iCellDepth;                      /* Depth of node containing pCell */
54332 
54333   assert( cursorHoldsMutex(pCur) );
54334   assert( pBt->inTransaction==TRANS_WRITE );
54335   assert( !pBt->readOnly );
54336   assert( pCur->wrFlag );
54337   assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
54338   assert( !hasReadConflicts(p, pCur->pgnoRoot) );
54339 
54340   if( NEVER(pCur->aiIdx[pCur->iPage]>=pCur->apPage[pCur->iPage]->nCell)
54341    || NEVER(pCur->eState!=CURSOR_VALID)
54342   ){
54343     return SQLITE_ERROR;  /* Something has gone awry. */
54344   }
54345 
54346   /* If this is a delete operation to remove a row from a table b-tree,
54347   ** invalidate any incrblob cursors open on the row being deleted.  */
54348   if( pCur->pKeyInfo==0 ){
54349     invalidateIncrblobCursors(p, pCur->info.nKey, 0);
54350   }
54351 
54352   iCellDepth = pCur->iPage;
54353   iCellIdx = pCur->aiIdx[iCellDepth];
54354   pPage = pCur->apPage[iCellDepth];
54355   pCell = findCell(pPage, iCellIdx);
54356 
54357   /* If the page containing the entry to delete is not a leaf page, move
54358   ** the cursor to the largest entry in the tree that is smaller than
54359   ** the entry being deleted. This cell will replace the cell being deleted
54360   ** from the internal node. The 'previous' entry is used for this instead
54361   ** of the 'next' entry, as the previous entry is always a part of the
54362   ** sub-tree headed by the child page of the cell being deleted. This makes
54363   ** balancing the tree following the delete operation easier.  */
54364   if( !pPage->leaf ){
54365     int notUsed;
54366     rc = sqlite3BtreePrevious(pCur, &notUsed);
54367     if( rc ) return rc;
54368   }
54369 
54370   /* Save the positions of any other cursors open on this table before
54371   ** making any modifications. Make the page containing the entry to be
54372   ** deleted writable. Then free any overflow pages associated with the
54373   ** entry and finally remove the cell itself from within the page.
54374   */
54375   rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
54376   if( rc ) return rc;
54377   rc = sqlite3PagerWrite(pPage->pDbPage);
54378   if( rc ) return rc;
54379   rc = clearCell(pPage, pCell);
54380   dropCell(pPage, iCellIdx, cellSizePtr(pPage, pCell), &rc);
54381   if( rc ) return rc;
54382 
54383   /* If the cell deleted was not located on a leaf page, then the cursor
54384   ** is currently pointing to the largest entry in the sub-tree headed
54385   ** by the child-page of the cell that was just deleted from an internal
54386   ** node. The cell from the leaf node needs to be moved to the internal
54387   ** node to replace the deleted cell.  */
54388   if( !pPage->leaf ){
54389     MemPage *pLeaf = pCur->apPage[pCur->iPage];
54390     int nCell;
54391     Pgno n = pCur->apPage[iCellDepth+1]->pgno;
54392     unsigned char *pTmp;
54393 
54394     pCell = findCell(pLeaf, pLeaf->nCell-1);
54395     nCell = cellSizePtr(pLeaf, pCell);
54396     assert( MX_CELL_SIZE(pBt) >= nCell );
54397 
54398     allocateTempSpace(pBt);
54399     pTmp = pBt->pTmpSpace;
54400 
54401     rc = sqlite3PagerWrite(pLeaf->pDbPage);
54402     insertCell(pPage, iCellIdx, pCell-4, nCell+4, pTmp, n, &rc);
54403     dropCell(pLeaf, pLeaf->nCell-1, nCell, &rc);
54404     if( rc ) return rc;
54405   }
54406 
54407   /* Balance the tree. If the entry deleted was located on a leaf page,
54408   ** then the cursor still points to that page. In this case the first
54409   ** call to balance() repairs the tree, and the if(...) condition is
54410   ** never true.
54411   **
54412   ** Otherwise, if the entry deleted was on an internal node page, then
54413   ** pCur is pointing to the leaf page from which a cell was removed to
54414   ** replace the cell deleted from the internal node. This is slightly
54415   ** tricky as the leaf node may be underfull, and the internal node may
54416   ** be either under or overfull. In this case run the balancing algorithm
54417   ** on the leaf node first. If the balance proceeds far enough up the
54418   ** tree that we can be sure that any problem in the internal node has
54419   ** been corrected, so be it. Otherwise, after balancing the leaf node,
54420   ** walk the cursor up the tree to the internal node and balance it as
54421   ** well.  */
54422   rc = balance(pCur);
54423   if( rc==SQLITE_OK && pCur->iPage>iCellDepth ){
54424     while( pCur->iPage>iCellDepth ){
54425       releasePage(pCur->apPage[pCur->iPage--]);
54426     }
54427     rc = balance(pCur);
54428   }
54429 
54430   if( rc==SQLITE_OK ){
54431     moveToRoot(pCur);
54432   }
54433   return rc;
54434 }
54435 
54436 /*
54437 ** Create a new BTree table.  Write into *piTable the page
54438 ** number for the root page of the new table.
54439 **
54440 ** The type of type is determined by the flags parameter.  Only the
54441 ** following values of flags are currently in use.  Other values for
54442 ** flags might not work:
54443 **
54444 **     BTREE_INTKEY|BTREE_LEAFDATA     Used for SQL tables with rowid keys
54445 **     BTREE_ZERODATA                  Used for SQL indices
54446 */
54447 static int btreeCreateTable(Btree *p, int *piTable, int createTabFlags){
54448   BtShared *pBt = p->pBt;
54449   MemPage *pRoot;
54450   Pgno pgnoRoot;
54451   int rc;
54452   int ptfFlags;          /* Page-type flage for the root page of new table */
54453 
54454   assert( sqlite3BtreeHoldsMutex(p) );
54455   assert( pBt->inTransaction==TRANS_WRITE );
54456   assert( !pBt->readOnly );
54457 
54458 #ifdef SQLITE_OMIT_AUTOVACUUM
54459   rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0);
54460   if( rc ){
54461     return rc;
54462   }
54463 #else
54464   if( pBt->autoVacuum ){
54465     Pgno pgnoMove;      /* Move a page here to make room for the root-page */
54466     MemPage *pPageMove; /* The page to move to. */
54467 
54468     /* Creating a new table may probably require moving an existing database
54469     ** to make room for the new tables root page. In case this page turns
54470     ** out to be an overflow page, delete all overflow page-map caches
54471     ** held by open cursors.
54472     */
54473     invalidateAllOverflowCache(pBt);
54474 
54475     /* Read the value of meta[3] from the database to determine where the
54476     ** root page of the new table should go. meta[3] is the largest root-page
54477     ** created so far, so the new root-page is (meta[3]+1).
54478     */
54479     sqlite3BtreeGetMeta(p, BTREE_LARGEST_ROOT_PAGE, &pgnoRoot);
54480     pgnoRoot++;
54481 
54482     /* The new root-page may not be allocated on a pointer-map page, or the
54483     ** PENDING_BYTE page.
54484     */
54485     while( pgnoRoot==PTRMAP_PAGENO(pBt, pgnoRoot) ||
54486         pgnoRoot==PENDING_BYTE_PAGE(pBt) ){
54487       pgnoRoot++;
54488     }
54489     assert( pgnoRoot>=3 );
54490 
54491     /* Allocate a page. The page that currently resides at pgnoRoot will
54492     ** be moved to the allocated page (unless the allocated page happens
54493     ** to reside at pgnoRoot).
54494     */
54495     rc = allocateBtreePage(pBt, &pPageMove, &pgnoMove, pgnoRoot, 1);
54496     if( rc!=SQLITE_OK ){
54497       return rc;
54498     }
54499 
54500     if( pgnoMove!=pgnoRoot ){
54501       /* pgnoRoot is the page that will be used for the root-page of
54502       ** the new table (assuming an error did not occur). But we were
54503       ** allocated pgnoMove. If required (i.e. if it was not allocated
54504       ** by extending the file), the current page at position pgnoMove
54505       ** is already journaled.
54506       */
54507       u8 eType = 0;
54508       Pgno iPtrPage = 0;
54509 
54510       releasePage(pPageMove);
54511 
54512       /* Move the page currently at pgnoRoot to pgnoMove. */
54513       rc = btreeGetPage(pBt, pgnoRoot, &pRoot, 0);
54514       if( rc!=SQLITE_OK ){
54515         return rc;
54516       }
54517       rc = ptrmapGet(pBt, pgnoRoot, &eType, &iPtrPage);
54518       if( eType==PTRMAP_ROOTPAGE || eType==PTRMAP_FREEPAGE ){
54519         rc = SQLITE_CORRUPT_BKPT;
54520       }
54521       if( rc!=SQLITE_OK ){
54522         releasePage(pRoot);
54523         return rc;
54524       }
54525       assert( eType!=PTRMAP_ROOTPAGE );
54526       assert( eType!=PTRMAP_FREEPAGE );
54527       rc = relocatePage(pBt, pRoot, eType, iPtrPage, pgnoMove, 0);
54528       releasePage(pRoot);
54529 
54530       /* Obtain the page at pgnoRoot */
54531       if( rc!=SQLITE_OK ){
54532         return rc;
54533       }
54534       rc = btreeGetPage(pBt, pgnoRoot, &pRoot, 0);
54535       if( rc!=SQLITE_OK ){
54536         return rc;
54537       }
54538       rc = sqlite3PagerWrite(pRoot->pDbPage);
54539       if( rc!=SQLITE_OK ){
54540         releasePage(pRoot);
54541         return rc;
54542       }
54543     }else{
54544       pRoot = pPageMove;
54545     }
54546 
54547     /* Update the pointer-map and meta-data with the new root-page number. */
54548     ptrmapPut(pBt, pgnoRoot, PTRMAP_ROOTPAGE, 0, &rc);
54549     if( rc ){
54550       releasePage(pRoot);
54551       return rc;
54552     }
54553 
54554     /* When the new root page was allocated, page 1 was made writable in
54555     ** order either to increase the database filesize, or to decrement the
54556     ** freelist count.  Hence, the sqlite3BtreeUpdateMeta() call cannot fail.
54557     */
54558     assert( sqlite3PagerIswriteable(pBt->pPage1->pDbPage) );
54559     rc = sqlite3BtreeUpdateMeta(p, 4, pgnoRoot);
54560     if( NEVER(rc) ){
54561       releasePage(pRoot);
54562       return rc;
54563     }
54564 
54565   }else{
54566     rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0);
54567     if( rc ) return rc;
54568   }
54569 #endif
54570   assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
54571   if( createTabFlags & BTREE_INTKEY ){
54572     ptfFlags = PTF_INTKEY | PTF_LEAFDATA | PTF_LEAF;
54573   }else{
54574     ptfFlags = PTF_ZERODATA | PTF_LEAF;
54575   }
54576   zeroPage(pRoot, ptfFlags);
54577   sqlite3PagerUnref(pRoot->pDbPage);
54578   assert( (pBt->openFlags & BTREE_SINGLE)==0 || pgnoRoot==2 );
54579   *piTable = (int)pgnoRoot;
54580   return SQLITE_OK;
54581 }
54582 SQLITE_PRIVATE int sqlite3BtreeCreateTable(Btree *p, int *piTable, int flags){
54583   int rc;
54584   sqlite3BtreeEnter(p);
54585   rc = btreeCreateTable(p, piTable, flags);
54586   sqlite3BtreeLeave(p);
54587   return rc;
54588 }
54589 
54590 /*
54591 ** Erase the given database page and all its children.  Return
54592 ** the page to the freelist.
54593 */
54594 static int clearDatabasePage(
54595   BtShared *pBt,           /* The BTree that contains the table */
54596   Pgno pgno,               /* Page number to clear */
54597   int freePageFlag,        /* Deallocate page if true */
54598   int *pnChange            /* Add number of Cells freed to this counter */
54599 ){
54600   MemPage *pPage;
54601   int rc;
54602   unsigned char *pCell;
54603   int i;
54604 
54605   assert( sqlite3_mutex_held(pBt->mutex) );
54606   if( pgno>btreePagecount(pBt) ){
54607     return SQLITE_CORRUPT_BKPT;
54608   }
54609 
54610   rc = getAndInitPage(pBt, pgno, &pPage);
54611   if( rc ) return rc;
54612   for(i=0; i<pPage->nCell; i++){
54613     pCell = findCell(pPage, i);
54614     if( !pPage->leaf ){
54615       rc = clearDatabasePage(pBt, get4byte(pCell), 1, pnChange);
54616       if( rc ) goto cleardatabasepage_out;
54617     }
54618     rc = clearCell(pPage, pCell);
54619     if( rc ) goto cleardatabasepage_out;
54620   }
54621   if( !pPage->leaf ){
54622     rc = clearDatabasePage(pBt, get4byte(&pPage->aData[8]), 1, pnChange);
54623     if( rc ) goto cleardatabasepage_out;
54624   }else if( pnChange ){
54625     assert( pPage->intKey );
54626     *pnChange += pPage->nCell;
54627   }
54628   if( freePageFlag ){
54629     freePage(pPage, &rc);
54630   }else if( (rc = sqlite3PagerWrite(pPage->pDbPage))==0 ){
54631     zeroPage(pPage, pPage->aData[0] | PTF_LEAF);
54632   }
54633 
54634 cleardatabasepage_out:
54635   releasePage(pPage);
54636   return rc;
54637 }
54638 
54639 /*
54640 ** Delete all information from a single table in the database.  iTable is
54641 ** the page number of the root of the table.  After this routine returns,
54642 ** the root page is empty, but still exists.
54643 **
54644 ** This routine will fail with SQLITE_LOCKED if there are any open
54645 ** read cursors on the table.  Open write cursors are moved to the
54646 ** root of the table.
54647 **
54648 ** If pnChange is not NULL, then table iTable must be an intkey table. The
54649 ** integer value pointed to by pnChange is incremented by the number of
54650 ** entries in the table.
54651 */
54652 SQLITE_PRIVATE int sqlite3BtreeClearTable(Btree *p, int iTable, int *pnChange){
54653   int rc;
54654   BtShared *pBt = p->pBt;
54655   sqlite3BtreeEnter(p);
54656   assert( p->inTrans==TRANS_WRITE );
54657 
54658   /* Invalidate all incrblob cursors open on table iTable (assuming iTable
54659   ** is the root of a table b-tree - if it is not, the following call is
54660   ** a no-op).  */
54661   invalidateIncrblobCursors(p, 0, 1);
54662 
54663   rc = saveAllCursors(pBt, (Pgno)iTable, 0);
54664   if( SQLITE_OK==rc ){
54665     rc = clearDatabasePage(pBt, (Pgno)iTable, 0, pnChange);
54666   }
54667   sqlite3BtreeLeave(p);
54668   return rc;
54669 }
54670 
54671 /*
54672 ** Erase all information in a table and add the root of the table to
54673 ** the freelist.  Except, the root of the principle table (the one on
54674 ** page 1) is never added to the freelist.
54675 **
54676 ** This routine will fail with SQLITE_LOCKED if there are any open
54677 ** cursors on the table.
54678 **
54679 ** If AUTOVACUUM is enabled and the page at iTable is not the last
54680 ** root page in the database file, then the last root page
54681 ** in the database file is moved into the slot formerly occupied by
54682 ** iTable and that last slot formerly occupied by the last root page
54683 ** is added to the freelist instead of iTable.  In this say, all
54684 ** root pages are kept at the beginning of the database file, which
54685 ** is necessary for AUTOVACUUM to work right.  *piMoved is set to the
54686 ** page number that used to be the last root page in the file before
54687 ** the move.  If no page gets moved, *piMoved is set to 0.
54688 ** The last root page is recorded in meta[3] and the value of
54689 ** meta[3] is updated by this procedure.
54690 */
54691 static int btreeDropTable(Btree *p, Pgno iTable, int *piMoved){
54692   int rc;
54693   MemPage *pPage = 0;
54694   BtShared *pBt = p->pBt;
54695 
54696   assert( sqlite3BtreeHoldsMutex(p) );
54697   assert( p->inTrans==TRANS_WRITE );
54698 
54699   /* It is illegal to drop a table if any cursors are open on the
54700   ** database. This is because in auto-vacuum mode the backend may
54701   ** need to move another root-page to fill a gap left by the deleted
54702   ** root page. If an open cursor was using this page a problem would
54703   ** occur.
54704   **
54705   ** This error is caught long before control reaches this point.
54706   */
54707   if( NEVER(pBt->pCursor) ){
54708     sqlite3ConnectionBlocked(p->db, pBt->pCursor->pBtree->db);
54709     return SQLITE_LOCKED_SHAREDCACHE;
54710   }
54711 
54712   rc = btreeGetPage(pBt, (Pgno)iTable, &pPage, 0);
54713   if( rc ) return rc;
54714   rc = sqlite3BtreeClearTable(p, iTable, 0);
54715   if( rc ){
54716     releasePage(pPage);
54717     return rc;
54718   }
54719 
54720   *piMoved = 0;
54721 
54722   if( iTable>1 ){
54723 #ifdef SQLITE_OMIT_AUTOVACUUM
54724     freePage(pPage, &rc);
54725     releasePage(pPage);
54726 #else
54727     if( pBt->autoVacuum ){
54728       Pgno maxRootPgno;
54729       sqlite3BtreeGetMeta(p, BTREE_LARGEST_ROOT_PAGE, &maxRootPgno);
54730 
54731       if( iTable==maxRootPgno ){
54732         /* If the table being dropped is the table with the largest root-page
54733         ** number in the database, put the root page on the free list.
54734         */
54735         freePage(pPage, &rc);
54736         releasePage(pPage);
54737         if( rc!=SQLITE_OK ){
54738           return rc;
54739         }
54740       }else{
54741         /* The table being dropped does not have the largest root-page
54742         ** number in the database. So move the page that does into the
54743         ** gap left by the deleted root-page.
54744         */
54745         MemPage *pMove;
54746         releasePage(pPage);
54747         rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0);
54748         if( rc!=SQLITE_OK ){
54749           return rc;
54750         }
54751         rc = relocatePage(pBt, pMove, PTRMAP_ROOTPAGE, 0, iTable, 0);
54752         releasePage(pMove);
54753         if( rc!=SQLITE_OK ){
54754           return rc;
54755         }
54756         pMove = 0;
54757         rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0);
54758         freePage(pMove, &rc);
54759         releasePage(pMove);
54760         if( rc!=SQLITE_OK ){
54761           return rc;
54762         }
54763         *piMoved = maxRootPgno;
54764       }
54765 
54766       /* Set the new 'max-root-page' value in the database header. This
54767       ** is the old value less one, less one more if that happens to
54768       ** be a root-page number, less one again if that is the
54769       ** PENDING_BYTE_PAGE.
54770       */
54771       maxRootPgno--;
54772       while( maxRootPgno==PENDING_BYTE_PAGE(pBt)
54773              || PTRMAP_ISPAGE(pBt, maxRootPgno) ){
54774         maxRootPgno--;
54775       }
54776       assert( maxRootPgno!=PENDING_BYTE_PAGE(pBt) );
54777 
54778       rc = sqlite3BtreeUpdateMeta(p, 4, maxRootPgno);
54779     }else{
54780       freePage(pPage, &rc);
54781       releasePage(pPage);
54782     }
54783 #endif
54784   }else{
54785     /* If sqlite3BtreeDropTable was called on page 1.
54786     ** This really never should happen except in a corrupt
54787     ** database.
54788     */
54789     zeroPage(pPage, PTF_INTKEY|PTF_LEAF );
54790     releasePage(pPage);
54791   }
54792   return rc;
54793 }
54794 SQLITE_PRIVATE int sqlite3BtreeDropTable(Btree *p, int iTable, int *piMoved){
54795   int rc;
54796   sqlite3BtreeEnter(p);
54797   rc = btreeDropTable(p, iTable, piMoved);
54798   sqlite3BtreeLeave(p);
54799   return rc;
54800 }
54801 
54802 
54803 /*
54804 ** This function may only be called if the b-tree connection already
54805 ** has a read or write transaction open on the database.
54806 **
54807 ** Read the meta-information out of a database file.  Meta[0]
54808 ** is the number of free pages currently in the database.  Meta[1]
54809 ** through meta[15] are available for use by higher layers.  Meta[0]
54810 ** is read-only, the others are read/write.
54811 **
54812 ** The schema layer numbers meta values differently.  At the schema
54813 ** layer (and the SetCookie and ReadCookie opcodes) the number of
54814 ** free pages is not visible.  So Cookie[0] is the same as Meta[1].
54815 */
54816 SQLITE_PRIVATE void sqlite3BtreeGetMeta(Btree *p, int idx, u32 *pMeta){
54817   BtShared *pBt = p->pBt;
54818 
54819   sqlite3BtreeEnter(p);
54820   assert( p->inTrans>TRANS_NONE );
54821   assert( SQLITE_OK==querySharedCacheTableLock(p, MASTER_ROOT, READ_LOCK) );
54822   assert( pBt->pPage1 );
54823   assert( idx>=0 && idx<=15 );
54824 
54825   *pMeta = get4byte(&pBt->pPage1->aData[36 + idx*4]);
54826 
54827   /* If auto-vacuum is disabled in this build and this is an auto-vacuum
54828   ** database, mark the database as read-only.  */
54829 #ifdef SQLITE_OMIT_AUTOVACUUM
54830   if( idx==BTREE_LARGEST_ROOT_PAGE && *pMeta>0 ) pBt->readOnly = 1;
54831 #endif
54832 
54833   sqlite3BtreeLeave(p);
54834 }
54835 
54836 /*
54837 ** Write meta-information back into the database.  Meta[0] is
54838 ** read-only and may not be written.
54839 */
54840 SQLITE_PRIVATE int sqlite3BtreeUpdateMeta(Btree *p, int idx, u32 iMeta){
54841   BtShared *pBt = p->pBt;
54842   unsigned char *pP1;
54843   int rc;
54844   assert( idx>=1 && idx<=15 );
54845   sqlite3BtreeEnter(p);
54846   assert( p->inTrans==TRANS_WRITE );
54847   assert( pBt->pPage1!=0 );
54848   pP1 = pBt->pPage1->aData;
54849   rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
54850   if( rc==SQLITE_OK ){
54851     put4byte(&pP1[36 + idx*4], iMeta);
54852 #ifndef SQLITE_OMIT_AUTOVACUUM
54853     if( idx==BTREE_INCR_VACUUM ){
54854       assert( pBt->autoVacuum || iMeta==0 );
54855       assert( iMeta==0 || iMeta==1 );
54856       pBt->incrVacuum = (u8)iMeta;
54857     }
54858 #endif
54859   }
54860   sqlite3BtreeLeave(p);
54861   return rc;
54862 }
54863 
54864 #ifndef SQLITE_OMIT_BTREECOUNT
54865 /*
54866 ** The first argument, pCur, is a cursor opened on some b-tree. Count the
54867 ** number of entries in the b-tree and write the result to *pnEntry.
54868 **
54869 ** SQLITE_OK is returned if the operation is successfully executed.
54870 ** Otherwise, if an error is encountered (i.e. an IO error or database
54871 ** corruption) an SQLite error code is returned.
54872 */
54873 SQLITE_PRIVATE int sqlite3BtreeCount(BtCursor *pCur, i64 *pnEntry){
54874   i64 nEntry = 0;                      /* Value to return in *pnEntry */
54875   int rc;                              /* Return code */
54876   rc = moveToRoot(pCur);
54877 
54878   /* Unless an error occurs, the following loop runs one iteration for each
54879   ** page in the B-Tree structure (not including overflow pages).
54880   */
54881   while( rc==SQLITE_OK ){
54882     int iIdx;                          /* Index of child node in parent */
54883     MemPage *pPage;                    /* Current page of the b-tree */
54884 
54885     /* If this is a leaf page or the tree is not an int-key tree, then
54886     ** this page contains countable entries. Increment the entry counter
54887     ** accordingly.
54888     */
54889     pPage = pCur->apPage[pCur->iPage];
54890     if( pPage->leaf || !pPage->intKey ){
54891       nEntry += pPage->nCell;
54892     }
54893 
54894     /* pPage is a leaf node. This loop navigates the cursor so that it
54895     ** points to the first interior cell that it points to the parent of
54896     ** the next page in the tree that has not yet been visited. The
54897     ** pCur->aiIdx[pCur->iPage] value is set to the index of the parent cell
54898     ** of the page, or to the number of cells in the page if the next page
54899     ** to visit is the right-child of its parent.
54900     **
54901     ** If all pages in the tree have been visited, return SQLITE_OK to the
54902     ** caller.
54903     */
54904     if( pPage->leaf ){
54905       do {
54906         if( pCur->iPage==0 ){
54907           /* All pages of the b-tree have been visited. Return successfully. */
54908           *pnEntry = nEntry;
54909           return SQLITE_OK;
54910         }
54911         moveToParent(pCur);
54912       }while ( pCur->aiIdx[pCur->iPage]>=pCur->apPage[pCur->iPage]->nCell );
54913 
54914       pCur->aiIdx[pCur->iPage]++;
54915       pPage = pCur->apPage[pCur->iPage];
54916     }
54917 
54918     /* Descend to the child node of the cell that the cursor currently
54919     ** points at. This is the right-child if (iIdx==pPage->nCell).
54920     */
54921     iIdx = pCur->aiIdx[pCur->iPage];
54922     if( iIdx==pPage->nCell ){
54923       rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
54924     }else{
54925       rc = moveToChild(pCur, get4byte(findCell(pPage, iIdx)));
54926     }
54927   }
54928 
54929   /* An error has occurred. Return an error code. */
54930   return rc;
54931 }
54932 #endif
54933 
54934 /*
54935 ** Return the pager associated with a BTree.  This routine is used for
54936 ** testing and debugging only.
54937 */
54938 SQLITE_PRIVATE Pager *sqlite3BtreePager(Btree *p){
54939   return p->pBt->pPager;
54940 }
54941 
54942 #ifndef SQLITE_OMIT_INTEGRITY_CHECK
54943 /*
54944 ** Append a message to the error message string.
54945 */
54946 static void checkAppendMsg(
54947   IntegrityCk *pCheck,
54948   char *zMsg1,
54949   const char *zFormat,
54950   ...
54951 ){
54952   va_list ap;
54953   if( !pCheck->mxErr ) return;
54954   pCheck->mxErr--;
54955   pCheck->nErr++;
54956   va_start(ap, zFormat);
54957   if( pCheck->errMsg.nChar ){
54958     sqlite3StrAccumAppend(&pCheck->errMsg, "\n", 1);
54959   }
54960   if( zMsg1 ){
54961     sqlite3StrAccumAppend(&pCheck->errMsg, zMsg1, -1);
54962   }
54963   sqlite3VXPrintf(&pCheck->errMsg, 1, zFormat, ap);
54964   va_end(ap);
54965   if( pCheck->errMsg.mallocFailed ){
54966     pCheck->mallocFailed = 1;
54967   }
54968 }
54969 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
54970 
54971 #ifndef SQLITE_OMIT_INTEGRITY_CHECK
54972 /*
54973 ** Add 1 to the reference count for page iPage.  If this is the second
54974 ** reference to the page, add an error message to pCheck->zErrMsg.
54975 ** Return 1 if there are 2 ore more references to the page and 0 if
54976 ** if this is the first reference to the page.
54977 **
54978 ** Also check that the page number is in bounds.
54979 */
54980 static int checkRef(IntegrityCk *pCheck, Pgno iPage, char *zContext){
54981   if( iPage==0 ) return 1;
54982   if( iPage>pCheck->nPage ){
54983     checkAppendMsg(pCheck, zContext, "invalid page number %d", iPage);
54984     return 1;
54985   }
54986   if( pCheck->anRef[iPage]==1 ){
54987     checkAppendMsg(pCheck, zContext, "2nd reference to page %d", iPage);
54988     return 1;
54989   }
54990   return  (pCheck->anRef[iPage]++)>1;
54991 }
54992 
54993 #ifndef SQLITE_OMIT_AUTOVACUUM
54994 /*
54995 ** Check that the entry in the pointer-map for page iChild maps to
54996 ** page iParent, pointer type ptrType. If not, append an error message
54997 ** to pCheck.
54998 */
54999 static void checkPtrmap(
55000   IntegrityCk *pCheck,   /* Integrity check context */
55001   Pgno iChild,           /* Child page number */
55002   u8 eType,              /* Expected pointer map type */
55003   Pgno iParent,          /* Expected pointer map parent page number */
55004   char *zContext         /* Context description (used for error msg) */
55005 ){
55006   int rc;
55007   u8 ePtrmapType;
55008   Pgno iPtrmapParent;
55009 
55010   rc = ptrmapGet(pCheck->pBt, iChild, &ePtrmapType, &iPtrmapParent);
55011   if( rc!=SQLITE_OK ){
55012     if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ) pCheck->mallocFailed = 1;
55013     checkAppendMsg(pCheck, zContext, "Failed to read ptrmap key=%d", iChild);
55014     return;
55015   }
55016 
55017   if( ePtrmapType!=eType || iPtrmapParent!=iParent ){
55018     checkAppendMsg(pCheck, zContext,
55019       "Bad ptr map entry key=%d expected=(%d,%d) got=(%d,%d)",
55020       iChild, eType, iParent, ePtrmapType, iPtrmapParent);
55021   }
55022 }
55023 #endif
55024 
55025 /*
55026 ** Check the integrity of the freelist or of an overflow page list.
55027 ** Verify that the number of pages on the list is N.
55028 */
55029 static void checkList(
55030   IntegrityCk *pCheck,  /* Integrity checking context */
55031   int isFreeList,       /* True for a freelist.  False for overflow page list */
55032   int iPage,            /* Page number for first page in the list */
55033   int N,                /* Expected number of pages in the list */
55034   char *zContext        /* Context for error messages */
55035 ){
55036   int i;
55037   int expected = N;
55038   int iFirst = iPage;
55039   while( N-- > 0 && pCheck->mxErr ){
55040     DbPage *pOvflPage;
55041     unsigned char *pOvflData;
55042     if( iPage<1 ){
55043       checkAppendMsg(pCheck, zContext,
55044          "%d of %d pages missing from overflow list starting at %d",
55045           N+1, expected, iFirst);
55046       break;
55047     }
55048     if( checkRef(pCheck, iPage, zContext) ) break;
55049     if( sqlite3PagerGet(pCheck->pPager, (Pgno)iPage, &pOvflPage) ){
55050       checkAppendMsg(pCheck, zContext, "failed to get page %d", iPage);
55051       break;
55052     }
55053     pOvflData = (unsigned char *)sqlite3PagerGetData(pOvflPage);
55054     if( isFreeList ){
55055       int n = get4byte(&pOvflData[4]);
55056 #ifndef SQLITE_OMIT_AUTOVACUUM
55057       if( pCheck->pBt->autoVacuum ){
55058         checkPtrmap(pCheck, iPage, PTRMAP_FREEPAGE, 0, zContext);
55059       }
55060 #endif
55061       if( n>(int)pCheck->pBt->usableSize/4-2 ){
55062         checkAppendMsg(pCheck, zContext,
55063            "freelist leaf count too big on page %d", iPage);
55064         N--;
55065       }else{
55066         for(i=0; i<n; i++){
55067           Pgno iFreePage = get4byte(&pOvflData[8+i*4]);
55068 #ifndef SQLITE_OMIT_AUTOVACUUM
55069           if( pCheck->pBt->autoVacuum ){
55070             checkPtrmap(pCheck, iFreePage, PTRMAP_FREEPAGE, 0, zContext);
55071           }
55072 #endif
55073           checkRef(pCheck, iFreePage, zContext);
55074         }
55075         N -= n;
55076       }
55077     }
55078 #ifndef SQLITE_OMIT_AUTOVACUUM
55079     else{
55080       /* If this database supports auto-vacuum and iPage is not the last
55081       ** page in this overflow list, check that the pointer-map entry for
55082       ** the following page matches iPage.
55083       */
55084       if( pCheck->pBt->autoVacuum && N>0 ){
55085         i = get4byte(pOvflData);
55086         checkPtrmap(pCheck, i, PTRMAP_OVERFLOW2, iPage, zContext);
55087       }
55088     }
55089 #endif
55090     iPage = get4byte(pOvflData);
55091     sqlite3PagerUnref(pOvflPage);
55092   }
55093 }
55094 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
55095 
55096 #ifndef SQLITE_OMIT_INTEGRITY_CHECK
55097 /*
55098 ** Do various sanity checks on a single page of a tree.  Return
55099 ** the tree depth.  Root pages return 0.  Parents of root pages
55100 ** return 1, and so forth.
55101 **
55102 ** These checks are done:
55103 **
55104 **      1.  Make sure that cells and freeblocks do not overlap
55105 **          but combine to completely cover the page.
55106 **  NO  2.  Make sure cell keys are in order.
55107 **  NO  3.  Make sure no key is less than or equal to zLowerBound.
55108 **  NO  4.  Make sure no key is greater than or equal to zUpperBound.
55109 **      5.  Check the integrity of overflow pages.
55110 **      6.  Recursively call checkTreePage on all children.
55111 **      7.  Verify that the depth of all children is the same.
55112 **      8.  Make sure this page is at least 33% full or else it is
55113 **          the root of the tree.
55114 */
55115 static int checkTreePage(
55116   IntegrityCk *pCheck,  /* Context for the sanity check */
55117   int iPage,            /* Page number of the page to check */
55118   char *zParentContext, /* Parent context */
55119   i64 *pnParentMinKey,
55120   i64 *pnParentMaxKey
55121 ){
55122   MemPage *pPage;
55123   int i, rc, depth, d2, pgno, cnt;
55124   int hdr, cellStart;
55125   int nCell;
55126   u8 *data;
55127   BtShared *pBt;
55128   int usableSize;
55129   char zContext[100];
55130   char *hit = 0;
55131   i64 nMinKey = 0;
55132   i64 nMaxKey = 0;
55133 
55134   sqlite3_snprintf(sizeof(zContext), zContext, "Page %d: ", iPage);
55135 
55136   /* Check that the page exists
55137   */
55138   pBt = pCheck->pBt;
55139   usableSize = pBt->usableSize;
55140   if( iPage==0 ) return 0;
55141   if( checkRef(pCheck, iPage, zParentContext) ) return 0;
55142   if( (rc = btreeGetPage(pBt, (Pgno)iPage, &pPage, 0))!=0 ){
55143     checkAppendMsg(pCheck, zContext,
55144        "unable to get the page. error code=%d", rc);
55145     return 0;
55146   }
55147 
55148   /* Clear MemPage.isInit to make sure the corruption detection code in
55149   ** btreeInitPage() is executed.  */
55150   pPage->isInit = 0;
55151   if( (rc = btreeInitPage(pPage))!=0 ){
55152     assert( rc==SQLITE_CORRUPT );  /* The only possible error from InitPage */
55153     checkAppendMsg(pCheck, zContext,
55154                    "btreeInitPage() returns error code %d", rc);
55155     releasePage(pPage);
55156     return 0;
55157   }
55158 
55159   /* Check out all the cells.
55160   */
55161   depth = 0;
55162   for(i=0; i<pPage->nCell && pCheck->mxErr; i++){
55163     u8 *pCell;
55164     u32 sz;
55165     CellInfo info;
55166 
55167     /* Check payload overflow pages
55168     */
55169     sqlite3_snprintf(sizeof(zContext), zContext,
55170              "On tree page %d cell %d: ", iPage, i);
55171     pCell = findCell(pPage,i);
55172     btreeParseCellPtr(pPage, pCell, &info);
55173     sz = info.nData;
55174     if( !pPage->intKey ) sz += (int)info.nKey;
55175     /* For intKey pages, check that the keys are in order.
55176     */
55177     else if( i==0 ) nMinKey = nMaxKey = info.nKey;
55178     else{
55179       if( info.nKey <= nMaxKey ){
55180         checkAppendMsg(pCheck, zContext,
55181             "Rowid %lld out of order (previous was %lld)", info.nKey, nMaxKey);
55182       }
55183       nMaxKey = info.nKey;
55184     }
55185     assert( sz==info.nPayload );
55186     if( (sz>info.nLocal)
55187      && (&pCell[info.iOverflow]<=&pPage->aData[pBt->usableSize])
55188     ){
55189       int nPage = (sz - info.nLocal + usableSize - 5)/(usableSize - 4);
55190       Pgno pgnoOvfl = get4byte(&pCell[info.iOverflow]);
55191 #ifndef SQLITE_OMIT_AUTOVACUUM
55192       if( pBt->autoVacuum ){
55193         checkPtrmap(pCheck, pgnoOvfl, PTRMAP_OVERFLOW1, iPage, zContext);
55194       }
55195 #endif
55196       checkList(pCheck, 0, pgnoOvfl, nPage, zContext);
55197     }
55198 
55199     /* Check sanity of left child page.
55200     */
55201     if( !pPage->leaf ){
55202       pgno = get4byte(pCell);
55203 #ifndef SQLITE_OMIT_AUTOVACUUM
55204       if( pBt->autoVacuum ){
55205         checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, zContext);
55206       }
55207 #endif
55208       d2 = checkTreePage(pCheck, pgno, zContext, &nMinKey, i==0 ? NULL : &nMaxKey);
55209       if( i>0 && d2!=depth ){
55210         checkAppendMsg(pCheck, zContext, "Child page depth differs");
55211       }
55212       depth = d2;
55213     }
55214   }
55215 
55216   if( !pPage->leaf ){
55217     pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
55218     sqlite3_snprintf(sizeof(zContext), zContext,
55219                      "On page %d at right child: ", iPage);
55220 #ifndef SQLITE_OMIT_AUTOVACUUM
55221     if( pBt->autoVacuum ){
55222       checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, zContext);
55223     }
55224 #endif
55225     checkTreePage(pCheck, pgno, zContext, NULL, !pPage->nCell ? NULL : &nMaxKey);
55226   }
55227 
55228   /* For intKey leaf pages, check that the min/max keys are in order
55229   ** with any left/parent/right pages.
55230   */
55231   if( pPage->leaf && pPage->intKey ){
55232     /* if we are a left child page */
55233     if( pnParentMinKey ){
55234       /* if we are the left most child page */
55235       if( !pnParentMaxKey ){
55236         if( nMaxKey > *pnParentMinKey ){
55237           checkAppendMsg(pCheck, zContext,
55238               "Rowid %lld out of order (max larger than parent min of %lld)",
55239               nMaxKey, *pnParentMinKey);
55240         }
55241       }else{
55242         if( nMinKey <= *pnParentMinKey ){
55243           checkAppendMsg(pCheck, zContext,
55244               "Rowid %lld out of order (min less than parent min of %lld)",
55245               nMinKey, *pnParentMinKey);
55246         }
55247         if( nMaxKey > *pnParentMaxKey ){
55248           checkAppendMsg(pCheck, zContext,
55249               "Rowid %lld out of order (max larger than parent max of %lld)",
55250               nMaxKey, *pnParentMaxKey);
55251         }
55252         *pnParentMinKey = nMaxKey;
55253       }
55254     /* else if we're a right child page */
55255     } else if( pnParentMaxKey ){
55256       if( nMinKey <= *pnParentMaxKey ){
55257         checkAppendMsg(pCheck, zContext,
55258             "Rowid %lld out of order (min less than parent max of %lld)",
55259             nMinKey, *pnParentMaxKey);
55260       }
55261     }
55262   }
55263 
55264   /* Check for complete coverage of the page
55265   */
55266   data = pPage->aData;
55267   hdr = pPage->hdrOffset;
55268   hit = sqlite3PageMalloc( pBt->pageSize );
55269   if( hit==0 ){
55270     pCheck->mallocFailed = 1;
55271   }else{
55272     int contentOffset = get2byteNotZero(&data[hdr+5]);
55273     assert( contentOffset<=usableSize );  /* Enforced by btreeInitPage() */
55274     memset(hit+contentOffset, 0, usableSize-contentOffset);
55275     memset(hit, 1, contentOffset);
55276     nCell = get2byte(&data[hdr+3]);
55277     cellStart = hdr + 12 - 4*pPage->leaf;
55278     for(i=0; i<nCell; i++){
55279       int pc = get2byte(&data[cellStart+i*2]);
55280       u32 size = 65536;
55281       int j;
55282       if( pc<=usableSize-4 ){
55283         size = cellSizePtr(pPage, &data[pc]);
55284       }
55285       if( (int)(pc+size-1)>=usableSize ){
55286         checkAppendMsg(pCheck, 0,
55287             "Corruption detected in cell %d on page %d",i,iPage);
55288       }else{
55289         for(j=pc+size-1; j>=pc; j--) hit[j]++;
55290       }
55291     }
55292     i = get2byte(&data[hdr+1]);
55293     while( i>0 ){
55294       int size, j;
55295       assert( i<=usableSize-4 );     /* Enforced by btreeInitPage() */
55296       size = get2byte(&data[i+2]);
55297       assert( i+size<=usableSize );  /* Enforced by btreeInitPage() */
55298       for(j=i+size-1; j>=i; j--) hit[j]++;
55299       j = get2byte(&data[i]);
55300       assert( j==0 || j>i+size );  /* Enforced by btreeInitPage() */
55301       assert( j<=usableSize-4 );   /* Enforced by btreeInitPage() */
55302       i = j;
55303     }
55304     for(i=cnt=0; i<usableSize; i++){
55305       if( hit[i]==0 ){
55306         cnt++;
55307       }else if( hit[i]>1 ){
55308         checkAppendMsg(pCheck, 0,
55309           "Multiple uses for byte %d of page %d", i, iPage);
55310         break;
55311       }
55312     }
55313     if( cnt!=data[hdr+7] ){
55314       checkAppendMsg(pCheck, 0,
55315           "Fragmentation of %d bytes reported as %d on page %d",
55316           cnt, data[hdr+7], iPage);
55317     }
55318   }
55319   sqlite3PageFree(hit);
55320   releasePage(pPage);
55321   return depth+1;
55322 }
55323 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
55324 
55325 #ifndef SQLITE_OMIT_INTEGRITY_CHECK
55326 /*
55327 ** This routine does a complete check of the given BTree file.  aRoot[] is
55328 ** an array of pages numbers were each page number is the root page of
55329 ** a table.  nRoot is the number of entries in aRoot.
55330 **
55331 ** A read-only or read-write transaction must be opened before calling
55332 ** this function.
55333 **
55334 ** Write the number of error seen in *pnErr.  Except for some memory
55335 ** allocation errors,  an error message held in memory obtained from
55336 ** malloc is returned if *pnErr is non-zero.  If *pnErr==0 then NULL is
55337 ** returned.  If a memory allocation error occurs, NULL is returned.
55338 */
55339 SQLITE_PRIVATE char *sqlite3BtreeIntegrityCheck(
55340   Btree *p,     /* The btree to be checked */
55341   int *aRoot,   /* An array of root pages numbers for individual trees */
55342   int nRoot,    /* Number of entries in aRoot[] */
55343   int mxErr,    /* Stop reporting errors after this many */
55344   int *pnErr    /* Write number of errors seen to this variable */
55345 ){
55346   Pgno i;
55347   int nRef;
55348   IntegrityCk sCheck;
55349   BtShared *pBt = p->pBt;
55350   char zErr[100];
55351 
55352   sqlite3BtreeEnter(p);
55353   assert( p->inTrans>TRANS_NONE && pBt->inTransaction>TRANS_NONE );
55354   nRef = sqlite3PagerRefcount(pBt->pPager);
55355   sCheck.pBt = pBt;
55356   sCheck.pPager = pBt->pPager;
55357   sCheck.nPage = btreePagecount(sCheck.pBt);
55358   sCheck.mxErr = mxErr;
55359   sCheck.nErr = 0;
55360   sCheck.mallocFailed = 0;
55361   *pnErr = 0;
55362   if( sCheck.nPage==0 ){
55363     sqlite3BtreeLeave(p);
55364     return 0;
55365   }
55366   sCheck.anRef = sqlite3Malloc( (sCheck.nPage+1)*sizeof(sCheck.anRef[0]) );
55367   if( !sCheck.anRef ){
55368     *pnErr = 1;
55369     sqlite3BtreeLeave(p);
55370     return 0;
55371   }
55372   for(i=0; i<=sCheck.nPage; i++){ sCheck.anRef[i] = 0; }
55373   i = PENDING_BYTE_PAGE(pBt);
55374   if( i<=sCheck.nPage ){
55375     sCheck.anRef[i] = 1;
55376   }
55377   sqlite3StrAccumInit(&sCheck.errMsg, zErr, sizeof(zErr), 20000);
55378   sCheck.errMsg.useMalloc = 2;
55379 
55380   /* Check the integrity of the freelist
55381   */
55382   checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]),
55383             get4byte(&pBt->pPage1->aData[36]), "Main freelist: ");
55384 
55385   /* Check all the tables.
55386   */
55387   for(i=0; (int)i<nRoot && sCheck.mxErr; i++){
55388     if( aRoot[i]==0 ) continue;
55389 #ifndef SQLITE_OMIT_AUTOVACUUM
55390     if( pBt->autoVacuum && aRoot[i]>1 ){
55391       checkPtrmap(&sCheck, aRoot[i], PTRMAP_ROOTPAGE, 0, 0);
55392     }
55393 #endif
55394     checkTreePage(&sCheck, aRoot[i], "List of tree roots: ", NULL, NULL);
55395   }
55396 
55397   /* Make sure every page in the file is referenced
55398   */
55399   for(i=1; i<=sCheck.nPage && sCheck.mxErr; i++){
55400 #ifdef SQLITE_OMIT_AUTOVACUUM
55401     if( sCheck.anRef[i]==0 ){
55402       checkAppendMsg(&sCheck, 0, "Page %d is never used", i);
55403     }
55404 #else
55405     /* If the database supports auto-vacuum, make sure no tables contain
55406     ** references to pointer-map pages.
55407     */
55408     if( sCheck.anRef[i]==0 &&
55409        (PTRMAP_PAGENO(pBt, i)!=i || !pBt->autoVacuum) ){
55410       checkAppendMsg(&sCheck, 0, "Page %d is never used", i);
55411     }
55412     if( sCheck.anRef[i]!=0 &&
55413        (PTRMAP_PAGENO(pBt, i)==i && pBt->autoVacuum) ){
55414       checkAppendMsg(&sCheck, 0, "Pointer map page %d is referenced", i);
55415     }
55416 #endif
55417   }
55418 
55419   /* Make sure this analysis did not leave any unref() pages.
55420   ** This is an internal consistency check; an integrity check
55421   ** of the integrity check.
55422   */
55423   if( NEVER(nRef != sqlite3PagerRefcount(pBt->pPager)) ){
55424     checkAppendMsg(&sCheck, 0,
55425       "Outstanding page count goes from %d to %d during this analysis",
55426       nRef, sqlite3PagerRefcount(pBt->pPager)
55427     );
55428   }
55429 
55430   /* Clean  up and report errors.
55431   */
55432   sqlite3BtreeLeave(p);
55433   sqlite3_free(sCheck.anRef);
55434   if( sCheck.mallocFailed ){
55435     sqlite3StrAccumReset(&sCheck.errMsg);
55436     *pnErr = sCheck.nErr+1;
55437     return 0;
55438   }
55439   *pnErr = sCheck.nErr;
55440   if( sCheck.nErr==0 ) sqlite3StrAccumReset(&sCheck.errMsg);
55441   return sqlite3StrAccumFinish(&sCheck.errMsg);
55442 }
55443 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
55444 
55445 /*
55446 ** Return the full pathname of the underlying database file.
55447 **
55448 ** The pager filename is invariant as long as the pager is
55449 ** open so it is safe to access without the BtShared mutex.
55450 */
55451 SQLITE_PRIVATE const char *sqlite3BtreeGetFilename(Btree *p){
55452   assert( p->pBt->pPager!=0 );
55453   return sqlite3PagerFilename(p->pBt->pPager);
55454 }
55455 
55456 /*
55457 ** Return the pathname of the journal file for this database. The return
55458 ** value of this routine is the same regardless of whether the journal file
55459 ** has been created or not.
55460 **
55461 ** The pager journal filename is invariant as long as the pager is
55462 ** open so it is safe to access without the BtShared mutex.
55463 */
55464 SQLITE_PRIVATE const char *sqlite3BtreeGetJournalname(Btree *p){
55465   assert( p->pBt->pPager!=0 );
55466   return sqlite3PagerJournalname(p->pBt->pPager);
55467 }
55468 
55469 /*
55470 ** Return non-zero if a transaction is active.
55471 */
55472 SQLITE_PRIVATE int sqlite3BtreeIsInTrans(Btree *p){
55473   assert( p==0 || sqlite3_mutex_held(p->db->mutex) );
55474   return (p && (p->inTrans==TRANS_WRITE));
55475 }
55476 
55477 #ifndef SQLITE_OMIT_WAL
55478 /*
55479 ** Run a checkpoint on the Btree passed as the first argument.
55480 **
55481 ** Return SQLITE_LOCKED if this or any other connection has an open
55482 ** transaction on the shared-cache the argument Btree is connected to.
55483 **
55484 ** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART.
55485 */
55486 SQLITE_PRIVATE int sqlite3BtreeCheckpoint(Btree *p, int eMode, int *pnLog, int *pnCkpt){
55487   int rc = SQLITE_OK;
55488   if( p ){
55489     BtShared *pBt = p->pBt;
55490     sqlite3BtreeEnter(p);
55491     if( pBt->inTransaction!=TRANS_NONE ){
55492       rc = SQLITE_LOCKED;
55493     }else{
55494       rc = sqlite3PagerCheckpoint(pBt->pPager, eMode, pnLog, pnCkpt);
55495     }
55496     sqlite3BtreeLeave(p);
55497   }
55498   return rc;
55499 }
55500 #endif
55501 
55502 /*
55503 ** Return non-zero if a read (or write) transaction is active.
55504 */
55505 SQLITE_PRIVATE int sqlite3BtreeIsInReadTrans(Btree *p){
55506   assert( p );
55507   assert( sqlite3_mutex_held(p->db->mutex) );
55508   return p->inTrans!=TRANS_NONE;
55509 }
55510 
55511 SQLITE_PRIVATE int sqlite3BtreeIsInBackup(Btree *p){
55512   assert( p );
55513   assert( sqlite3_mutex_held(p->db->mutex) );
55514   return p->nBackup!=0;
55515 }
55516 
55517 /*
55518 ** This function returns a pointer to a blob of memory associated with
55519 ** a single shared-btree. The memory is used by client code for its own
55520 ** purposes (for example, to store a high-level schema associated with
55521 ** the shared-btree). The btree layer manages reference counting issues.
55522 **
55523 ** The first time this is called on a shared-btree, nBytes bytes of memory
55524 ** are allocated, zeroed, and returned to the caller. For each subsequent
55525 ** call the nBytes parameter is ignored and a pointer to the same blob
55526 ** of memory returned.
55527 **
55528 ** If the nBytes parameter is 0 and the blob of memory has not yet been
55529 ** allocated, a null pointer is returned. If the blob has already been
55530 ** allocated, it is returned as normal.
55531 **
55532 ** Just before the shared-btree is closed, the function passed as the
55533 ** xFree argument when the memory allocation was made is invoked on the
55534 ** blob of allocated memory. The xFree function should not call sqlite3_free()
55535 ** on the memory, the btree layer does that.
55536 */
55537 SQLITE_PRIVATE void *sqlite3BtreeSchema(Btree *p, int nBytes, void(*xFree)(void *)){
55538   BtShared *pBt = p->pBt;
55539   sqlite3BtreeEnter(p);
55540   if( !pBt->pSchema && nBytes ){
55541     pBt->pSchema = sqlite3DbMallocZero(0, nBytes);
55542     pBt->xFreeSchema = xFree;
55543   }
55544   sqlite3BtreeLeave(p);
55545   return pBt->pSchema;
55546 }
55547 
55548 /*
55549 ** Return SQLITE_LOCKED_SHAREDCACHE if another user of the same shared
55550 ** btree as the argument handle holds an exclusive lock on the
55551 ** sqlite_master table. Otherwise SQLITE_OK.
55552 */
55553 SQLITE_PRIVATE int sqlite3BtreeSchemaLocked(Btree *p){
55554   int rc;
55555   assert( sqlite3_mutex_held(p->db->mutex) );
55556   sqlite3BtreeEnter(p);
55557   rc = querySharedCacheTableLock(p, MASTER_ROOT, READ_LOCK);
55558   assert( rc==SQLITE_OK || rc==SQLITE_LOCKED_SHAREDCACHE );
55559   sqlite3BtreeLeave(p);
55560   return rc;
55561 }
55562 
55563 
55564 #ifndef SQLITE_OMIT_SHARED_CACHE
55565 /*
55566 ** Obtain a lock on the table whose root page is iTab.  The
55567 ** lock is a write lock if isWritelock is true or a read lock
55568 ** if it is false.
55569 */
55570 SQLITE_PRIVATE int sqlite3BtreeLockTable(Btree *p, int iTab, u8 isWriteLock){
55571   int rc = SQLITE_OK;
55572   assert( p->inTrans!=TRANS_NONE );
55573   if( p->sharable ){
55574     u8 lockType = READ_LOCK + isWriteLock;
55575     assert( READ_LOCK+1==WRITE_LOCK );
55576     assert( isWriteLock==0 || isWriteLock==1 );
55577 
55578     sqlite3BtreeEnter(p);
55579     rc = querySharedCacheTableLock(p, iTab, lockType);
55580     if( rc==SQLITE_OK ){
55581       rc = setSharedCacheTableLock(p, iTab, lockType);
55582     }
55583     sqlite3BtreeLeave(p);
55584   }
55585   return rc;
55586 }
55587 #endif
55588 
55589 #ifndef SQLITE_OMIT_INCRBLOB
55590 /*
55591 ** Argument pCsr must be a cursor opened for writing on an
55592 ** INTKEY table currently pointing at a valid table entry.
55593 ** This function modifies the data stored as part of that entry.
55594 **
55595 ** Only the data content may only be modified, it is not possible to
55596 ** change the length of the data stored. If this function is called with
55597 ** parameters that attempt to write past the end of the existing data,
55598 ** no modifications are made and SQLITE_CORRUPT is returned.
55599 */
55600 SQLITE_PRIVATE int sqlite3BtreePutData(BtCursor *pCsr, u32 offset, u32 amt, void *z){
55601   int rc;
55602   assert( cursorHoldsMutex(pCsr) );
55603   assert( sqlite3_mutex_held(pCsr->pBtree->db->mutex) );
55604   assert( pCsr->isIncrblobHandle );
55605 
55606   rc = restoreCursorPosition(pCsr);
55607   if( rc!=SQLITE_OK ){
55608     return rc;
55609   }
55610   assert( pCsr->eState!=CURSOR_REQUIRESEEK );
55611   if( pCsr->eState!=CURSOR_VALID ){
55612     return SQLITE_ABORT;
55613   }
55614 
55615   /* Check some assumptions:
55616   **   (a) the cursor is open for writing,
55617   **   (b) there is a read/write transaction open,
55618   **   (c) the connection holds a write-lock on the table (if required),
55619   **   (d) there are no conflicting read-locks, and
55620   **   (e) the cursor points at a valid row of an intKey table.
55621   */
55622   if( !pCsr->wrFlag ){
55623     return SQLITE_READONLY;
55624   }
55625   assert( !pCsr->pBt->readOnly && pCsr->pBt->inTransaction==TRANS_WRITE );
55626   assert( hasSharedCacheTableLock(pCsr->pBtree, pCsr->pgnoRoot, 0, 2) );
55627   assert( !hasReadConflicts(pCsr->pBtree, pCsr->pgnoRoot) );
55628   assert( pCsr->apPage[pCsr->iPage]->intKey );
55629 
55630   return accessPayload(pCsr, offset, amt, (unsigned char *)z, 1);
55631 }
55632 
55633 /*
55634 ** Set a flag on this cursor to cache the locations of pages from the
55635 ** overflow list for the current row. This is used by cursors opened
55636 ** for incremental blob IO only.
55637 **
55638 ** This function sets a flag only. The actual page location cache
55639 ** (stored in BtCursor.aOverflow[]) is allocated and used by function
55640 ** accessPayload() (the worker function for sqlite3BtreeData() and
55641 ** sqlite3BtreePutData()).
55642 */
55643 SQLITE_PRIVATE void sqlite3BtreeCacheOverflow(BtCursor *pCur){
55644   assert( cursorHoldsMutex(pCur) );
55645   assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
55646   invalidateOverflowCache(pCur);
55647   pCur->isIncrblobHandle = 1;
55648 }
55649 #endif
55650 
55651 /*
55652 ** Set both the "read version" (single byte at byte offset 18) and
55653 ** "write version" (single byte at byte offset 19) fields in the database
55654 ** header to iVersion.
55655 */
55656 SQLITE_PRIVATE int sqlite3BtreeSetVersion(Btree *pBtree, int iVersion){
55657   BtShared *pBt = pBtree->pBt;
55658   int rc;                         /* Return code */
55659 
55660   assert( pBtree->inTrans==TRANS_NONE );
55661   assert( iVersion==1 || iVersion==2 );
55662 
55663   /* If setting the version fields to 1, do not automatically open the
55664   ** WAL connection, even if the version fields are currently set to 2.
55665   */
55666   pBt->doNotUseWAL = (u8)(iVersion==1);
55667 
55668   rc = sqlite3BtreeBeginTrans(pBtree, 0);
55669   if( rc==SQLITE_OK ){
55670     u8 *aData = pBt->pPage1->aData;
55671     if( aData[18]!=(u8)iVersion || aData[19]!=(u8)iVersion ){
55672       rc = sqlite3BtreeBeginTrans(pBtree, 2);
55673       if( rc==SQLITE_OK ){
55674         rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
55675         if( rc==SQLITE_OK ){
55676           aData[18] = (u8)iVersion;
55677           aData[19] = (u8)iVersion;
55678         }
55679       }
55680     }
55681   }
55682 
55683   pBt->doNotUseWAL = 0;
55684   return rc;
55685 }
55686 
55687 /************** End of btree.c ***********************************************/
55688 /************** Begin file backup.c ******************************************/
55689 /*
55690 ** 2009 January 28
55691 **
55692 ** The author disclaims copyright to this source code.  In place of
55693 ** a legal notice, here is a blessing:
55694 **
55695 **    May you do good and not evil.
55696 **    May you find forgiveness for yourself and forgive others.
55697 **    May you share freely, never taking more than you give.
55698 **
55699 *************************************************************************
55700 ** This file contains the implementation of the sqlite3_backup_XXX()
55701 ** API functions and the related features.
55702 */
55703 
55704 /* Macro to find the minimum of two numeric values.
55705 */
55706 #ifndef MIN
55707 # define MIN(x,y) ((x)<(y)?(x):(y))
55708 #endif
55709 
55710 /*
55711 ** Structure allocated for each backup operation.
55712 */
55713 struct sqlite3_backup {
55714   sqlite3* pDestDb;        /* Destination database handle */
55715   Btree *pDest;            /* Destination b-tree file */
55716   u32 iDestSchema;         /* Original schema cookie in destination */
55717   int bDestLocked;         /* True once a write-transaction is open on pDest */
55718 
55719   Pgno iNext;              /* Page number of the next source page to copy */
55720   sqlite3* pSrcDb;         /* Source database handle */
55721   Btree *pSrc;             /* Source b-tree file */
55722 
55723   int rc;                  /* Backup process error code */
55724 
55725   /* These two variables are set by every call to backup_step(). They are
55726   ** read by calls to backup_remaining() and backup_pagecount().
55727   */
55728   Pgno nRemaining;         /* Number of pages left to copy */
55729   Pgno nPagecount;         /* Total number of pages to copy */
55730 
55731   int isAttached;          /* True once backup has been registered with pager */
55732   sqlite3_backup *pNext;   /* Next backup associated with source pager */
55733 };
55734 
55735 /*
55736 ** THREAD SAFETY NOTES:
55737 **
55738 **   Once it has been created using backup_init(), a single sqlite3_backup
55739 **   structure may be accessed via two groups of thread-safe entry points:
55740 **
55741 **     * Via the sqlite3_backup_XXX() API function backup_step() and
55742 **       backup_finish(). Both these functions obtain the source database
55743 **       handle mutex and the mutex associated with the source BtShared
55744 **       structure, in that order.
55745 **
55746 **     * Via the BackupUpdate() and BackupRestart() functions, which are
55747 **       invoked by the pager layer to report various state changes in
55748 **       the page cache associated with the source database. The mutex
55749 **       associated with the source database BtShared structure will always
55750 **       be held when either of these functions are invoked.
55751 **
55752 **   The other sqlite3_backup_XXX() API functions, backup_remaining() and
55753 **   backup_pagecount() are not thread-safe functions. If they are called
55754 **   while some other thread is calling backup_step() or backup_finish(),
55755 **   the values returned may be invalid. There is no way for a call to
55756 **   BackupUpdate() or BackupRestart() to interfere with backup_remaining()
55757 **   or backup_pagecount().
55758 **
55759 **   Depending on the SQLite configuration, the database handles and/or
55760 **   the Btree objects may have their own mutexes that require locking.
55761 **   Non-sharable Btrees (in-memory databases for example), do not have
55762 **   associated mutexes.
55763 */
55764 
55765 /*
55766 ** Return a pointer corresponding to database zDb (i.e. "main", "temp")
55767 ** in connection handle pDb. If such a database cannot be found, return
55768 ** a NULL pointer and write an error message to pErrorDb.
55769 **
55770 ** If the "temp" database is requested, it may need to be opened by this
55771 ** function. If an error occurs while doing so, return 0 and write an
55772 ** error message to pErrorDb.
55773 */
55774 static Btree *findBtree(sqlite3 *pErrorDb, sqlite3 *pDb, const char *zDb){
55775   int i = sqlite3FindDbName(pDb, zDb);
55776 
55777   if( i==1 ){
55778     Parse *pParse;
55779     int rc = 0;
55780     pParse = sqlite3StackAllocZero(pErrorDb, sizeof(*pParse));
55781     if( pParse==0 ){
55782       sqlite3Error(pErrorDb, SQLITE_NOMEM, "out of memory");
55783       rc = SQLITE_NOMEM;
55784     }else{
55785       pParse->db = pDb;
55786       if( sqlite3OpenTempDatabase(pParse) ){
55787         sqlite3Error(pErrorDb, pParse->rc, "%s", pParse->zErrMsg);
55788         rc = SQLITE_ERROR;
55789       }
55790       sqlite3DbFree(pErrorDb, pParse->zErrMsg);
55791       sqlite3StackFree(pErrorDb, pParse);
55792     }
55793     if( rc ){
55794       return 0;
55795     }
55796   }
55797 
55798   if( i<0 ){
55799     sqlite3Error(pErrorDb, SQLITE_ERROR, "unknown database %s", zDb);
55800     return 0;
55801   }
55802 
55803   return pDb->aDb[i].pBt;
55804 }
55805 
55806 /*
55807 ** Attempt to set the page size of the destination to match the page size
55808 ** of the source.
55809 */
55810 static int setDestPgsz(sqlite3_backup *p){
55811   int rc;
55812   rc = sqlite3BtreeSetPageSize(p->pDest,sqlite3BtreeGetPageSize(p->pSrc),-1,0);
55813   return rc;
55814 }
55815 
55816 /*
55817 ** Create an sqlite3_backup process to copy the contents of zSrcDb from
55818 ** connection handle pSrcDb to zDestDb in pDestDb. If successful, return
55819 ** a pointer to the new sqlite3_backup object.
55820 **
55821 ** If an error occurs, NULL is returned and an error code and error message
55822 ** stored in database handle pDestDb.
55823 */
55824 SQLITE_API sqlite3_backup *sqlite3_backup_init(
55825   sqlite3* pDestDb,                     /* Database to write to */
55826   const char *zDestDb,                  /* Name of database within pDestDb */
55827   sqlite3* pSrcDb,                      /* Database connection to read from */
55828   const char *zSrcDb                    /* Name of database within pSrcDb */
55829 ){
55830   sqlite3_backup *p;                    /* Value to return */
55831 
55832   /* Lock the source database handle. The destination database
55833   ** handle is not locked in this routine, but it is locked in
55834   ** sqlite3_backup_step(). The user is required to ensure that no
55835   ** other thread accesses the destination handle for the duration
55836   ** of the backup operation.  Any attempt to use the destination
55837   ** database connection while a backup is in progress may cause
55838   ** a malfunction or a deadlock.
55839   */
55840   sqlite3_mutex_enter(pSrcDb->mutex);
55841   sqlite3_mutex_enter(pDestDb->mutex);
55842 
55843   if( pSrcDb==pDestDb ){
55844     sqlite3Error(
55845         pDestDb, SQLITE_ERROR, "source and destination must be distinct"
55846     );
55847     p = 0;
55848   }else {
55849     /* Allocate space for a new sqlite3_backup object...
55850     ** EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
55851     ** call to sqlite3_backup_init() and is destroyed by a call to
55852     ** sqlite3_backup_finish(). */
55853     p = (sqlite3_backup *)sqlite3_malloc(sizeof(sqlite3_backup));
55854     if( !p ){
55855       sqlite3Error(pDestDb, SQLITE_NOMEM, 0);
55856     }
55857   }
55858 
55859   /* If the allocation succeeded, populate the new object. */
55860   if( p ){
55861     memset(p, 0, sizeof(sqlite3_backup));
55862     p->pSrc = findBtree(pDestDb, pSrcDb, zSrcDb);
55863     p->pDest = findBtree(pDestDb, pDestDb, zDestDb);
55864     p->pDestDb = pDestDb;
55865     p->pSrcDb = pSrcDb;
55866     p->iNext = 1;
55867     p->isAttached = 0;
55868 
55869     if( 0==p->pSrc || 0==p->pDest || setDestPgsz(p)==SQLITE_NOMEM ){
55870       /* One (or both) of the named databases did not exist or an OOM
55871       ** error was hit.  The error has already been written into the
55872       ** pDestDb handle.  All that is left to do here is free the
55873       ** sqlite3_backup structure.
55874       */
55875       sqlite3_free(p);
55876       p = 0;
55877     }
55878   }
55879   if( p ){
55880     p->pSrc->nBackup++;
55881   }
55882 
55883   sqlite3_mutex_leave(pDestDb->mutex);
55884   sqlite3_mutex_leave(pSrcDb->mutex);
55885   return p;
55886 }
55887 
55888 /*
55889 ** Argument rc is an SQLite error code. Return true if this error is
55890 ** considered fatal if encountered during a backup operation. All errors
55891 ** are considered fatal except for SQLITE_BUSY and SQLITE_LOCKED.
55892 */
55893 static int isFatalError(int rc){
55894   return (rc!=SQLITE_OK && rc!=SQLITE_BUSY && ALWAYS(rc!=SQLITE_LOCKED));
55895 }
55896 
55897 /*
55898 ** Parameter zSrcData points to a buffer containing the data for
55899 ** page iSrcPg from the source database. Copy this data into the
55900 ** destination database.
55901 */
55902 static int backupOnePage(sqlite3_backup *p, Pgno iSrcPg, const u8 *zSrcData){
55903   Pager * const pDestPager = sqlite3BtreePager(p->pDest);
55904   const int nSrcPgsz = sqlite3BtreeGetPageSize(p->pSrc);
55905   int nDestPgsz = sqlite3BtreeGetPageSize(p->pDest);
55906   const int nCopy = MIN(nSrcPgsz, nDestPgsz);
55907   const i64 iEnd = (i64)iSrcPg*(i64)nSrcPgsz;
55908 #ifdef SQLITE_HAS_CODEC
55909   int nSrcReserve = sqlite3BtreeGetReserve(p->pSrc);
55910   int nDestReserve = sqlite3BtreeGetReserve(p->pDest);
55911 #endif
55912 
55913   int rc = SQLITE_OK;
55914   i64 iOff;
55915 
55916   assert( p->bDestLocked );
55917   assert( !isFatalError(p->rc) );
55918   assert( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) );
55919   assert( zSrcData );
55920 
55921   /* Catch the case where the destination is an in-memory database and the
55922   ** page sizes of the source and destination differ.
55923   */
55924   if( nSrcPgsz!=nDestPgsz && sqlite3PagerIsMemdb(pDestPager) ){
55925     rc = SQLITE_READONLY;
55926   }
55927 
55928 #ifdef SQLITE_HAS_CODEC
55929   /* Backup is not possible if the page size of the destination is changing
55930   ** and a codec is in use.
55931   */
55932   if( nSrcPgsz!=nDestPgsz && sqlite3PagerGetCodec(pDestPager)!=0 ){
55933     rc = SQLITE_READONLY;
55934   }
55935 
55936   /* Backup is not possible if the number of bytes of reserve space differ
55937   ** between source and destination.  If there is a difference, try to
55938   ** fix the destination to agree with the source.  If that is not possible,
55939   ** then the backup cannot proceed.
55940   */
55941   if( nSrcReserve!=nDestReserve ){
55942     u32 newPgsz = nSrcPgsz;
55943     rc = sqlite3PagerSetPagesize(pDestPager, &newPgsz, nSrcReserve);
55944     if( rc==SQLITE_OK && newPgsz!=nSrcPgsz ) rc = SQLITE_READONLY;
55945   }
55946 #endif
55947 
55948   /* This loop runs once for each destination page spanned by the source
55949   ** page. For each iteration, variable iOff is set to the byte offset
55950   ** of the destination page.
55951   */
55952   for(iOff=iEnd-(i64)nSrcPgsz; rc==SQLITE_OK && iOff<iEnd; iOff+=nDestPgsz){
55953     DbPage *pDestPg = 0;
55954     Pgno iDest = (Pgno)(iOff/nDestPgsz)+1;
55955     if( iDest==PENDING_BYTE_PAGE(p->pDest->pBt) ) continue;
55956     if( SQLITE_OK==(rc = sqlite3PagerGet(pDestPager, iDest, &pDestPg))
55957      && SQLITE_OK==(rc = sqlite3PagerWrite(pDestPg))
55958     ){
55959       const u8 *zIn = &zSrcData[iOff%nSrcPgsz];
55960       u8 *zDestData = sqlite3PagerGetData(pDestPg);
55961       u8 *zOut = &zDestData[iOff%nDestPgsz];
55962 
55963       /* Copy the data from the source page into the destination page.
55964       ** Then clear the Btree layer MemPage.isInit flag. Both this module
55965       ** and the pager code use this trick (clearing the first byte
55966       ** of the page 'extra' space to invalidate the Btree layers
55967       ** cached parse of the page). MemPage.isInit is marked
55968       ** "MUST BE FIRST" for this purpose.
55969       */
55970       memcpy(zOut, zIn, nCopy);
55971       ((u8 *)sqlite3PagerGetExtra(pDestPg))[0] = 0;
55972     }
55973     sqlite3PagerUnref(pDestPg);
55974   }
55975 
55976   return rc;
55977 }
55978 
55979 /*
55980 ** If pFile is currently larger than iSize bytes, then truncate it to
55981 ** exactly iSize bytes. If pFile is not larger than iSize bytes, then
55982 ** this function is a no-op.
55983 **
55984 ** Return SQLITE_OK if everything is successful, or an SQLite error
55985 ** code if an error occurs.
55986 */
55987 static int backupTruncateFile(sqlite3_file *pFile, i64 iSize){
55988   i64 iCurrent;
55989   int rc = sqlite3OsFileSize(pFile, &iCurrent);
55990   if( rc==SQLITE_OK && iCurrent>iSize ){
55991     rc = sqlite3OsTruncate(pFile, iSize);
55992   }
55993   return rc;
55994 }
55995 
55996 /*
55997 ** Register this backup object with the associated source pager for
55998 ** callbacks when pages are changed or the cache invalidated.
55999 */
56000 static void attachBackupObject(sqlite3_backup *p){
56001   sqlite3_backup **pp;
56002   assert( sqlite3BtreeHoldsMutex(p->pSrc) );
56003   pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc));
56004   p->pNext = *pp;
56005   *pp = p;
56006   p->isAttached = 1;
56007 }
56008 
56009 /*
56010 ** Copy nPage pages from the source b-tree to the destination.
56011 */
56012 SQLITE_API int sqlite3_backup_step(sqlite3_backup *p, int nPage){
56013   int rc;
56014   int destMode;       /* Destination journal mode */
56015   int pgszSrc = 0;    /* Source page size */
56016   int pgszDest = 0;   /* Destination page size */
56017 
56018   sqlite3_mutex_enter(p->pSrcDb->mutex);
56019   sqlite3BtreeEnter(p->pSrc);
56020   if( p->pDestDb ){
56021     sqlite3_mutex_enter(p->pDestDb->mutex);
56022   }
56023 
56024   rc = p->rc;
56025   if( !isFatalError(rc) ){
56026     Pager * const pSrcPager = sqlite3BtreePager(p->pSrc);     /* Source pager */
56027     Pager * const pDestPager = sqlite3BtreePager(p->pDest);   /* Dest pager */
56028     int ii;                            /* Iterator variable */
56029     int nSrcPage = -1;                 /* Size of source db in pages */
56030     int bCloseTrans = 0;               /* True if src db requires unlocking */
56031 
56032     /* If the source pager is currently in a write-transaction, return
56033     ** SQLITE_BUSY immediately.
56034     */
56035     if( p->pDestDb && p->pSrc->pBt->inTransaction==TRANS_WRITE ){
56036       rc = SQLITE_BUSY;
56037     }else{
56038       rc = SQLITE_OK;
56039     }
56040 
56041     /* Lock the destination database, if it is not locked already. */
56042     if( SQLITE_OK==rc && p->bDestLocked==0
56043      && SQLITE_OK==(rc = sqlite3BtreeBeginTrans(p->pDest, 2))
56044     ){
56045       p->bDestLocked = 1;
56046       sqlite3BtreeGetMeta(p->pDest, BTREE_SCHEMA_VERSION, &p->iDestSchema);
56047     }
56048 
56049     /* If there is no open read-transaction on the source database, open
56050     ** one now. If a transaction is opened here, then it will be closed
56051     ** before this function exits.
56052     */
56053     if( rc==SQLITE_OK && 0==sqlite3BtreeIsInReadTrans(p->pSrc) ){
56054       rc = sqlite3BtreeBeginTrans(p->pSrc, 0);
56055       bCloseTrans = 1;
56056     }
56057 
56058     /* Do not allow backup if the destination database is in WAL mode
56059     ** and the page sizes are different between source and destination */
56060     pgszSrc = sqlite3BtreeGetPageSize(p->pSrc);
56061     pgszDest = sqlite3BtreeGetPageSize(p->pDest);
56062     destMode = sqlite3PagerGetJournalMode(sqlite3BtreePager(p->pDest));
56063     if( SQLITE_OK==rc && destMode==PAGER_JOURNALMODE_WAL && pgszSrc!=pgszDest ){
56064       rc = SQLITE_READONLY;
56065     }
56066 
56067     /* Now that there is a read-lock on the source database, query the
56068     ** source pager for the number of pages in the database.
56069     */
56070     nSrcPage = (int)sqlite3BtreeLastPage(p->pSrc);
56071     assert( nSrcPage>=0 );
56072     for(ii=0; (nPage<0 || ii<nPage) && p->iNext<=(Pgno)nSrcPage && !rc; ii++){
56073       const Pgno iSrcPg = p->iNext;                 /* Source page number */
56074       if( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) ){
56075         DbPage *pSrcPg;                             /* Source page object */
56076         rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg);
56077         if( rc==SQLITE_OK ){
56078           rc = backupOnePage(p, iSrcPg, sqlite3PagerGetData(pSrcPg));
56079           sqlite3PagerUnref(pSrcPg);
56080         }
56081       }
56082       p->iNext++;
56083     }
56084     if( rc==SQLITE_OK ){
56085       p->nPagecount = nSrcPage;
56086       p->nRemaining = nSrcPage+1-p->iNext;
56087       if( p->iNext>(Pgno)nSrcPage ){
56088         rc = SQLITE_DONE;
56089       }else if( !p->isAttached ){
56090         attachBackupObject(p);
56091       }
56092     }
56093 
56094     /* Update the schema version field in the destination database. This
56095     ** is to make sure that the schema-version really does change in
56096     ** the case where the source and destination databases have the
56097     ** same schema version.
56098     */
56099     if( rc==SQLITE_DONE
56100      && (rc = sqlite3BtreeUpdateMeta(p->pDest,1,p->iDestSchema+1))==SQLITE_OK
56101     ){
56102       int nDestTruncate;
56103 
56104       if( p->pDestDb ){
56105         sqlite3ResetInternalSchema(p->pDestDb, -1);
56106       }
56107 
56108       /* Set nDestTruncate to the final number of pages in the destination
56109       ** database. The complication here is that the destination page
56110       ** size may be different to the source page size.
56111       **
56112       ** If the source page size is smaller than the destination page size,
56113       ** round up. In this case the call to sqlite3OsTruncate() below will
56114       ** fix the size of the file. However it is important to call
56115       ** sqlite3PagerTruncateImage() here so that any pages in the
56116       ** destination file that lie beyond the nDestTruncate page mark are
56117       ** journalled by PagerCommitPhaseOne() before they are destroyed
56118       ** by the file truncation.
56119       */
56120       assert( pgszSrc==sqlite3BtreeGetPageSize(p->pSrc) );
56121       assert( pgszDest==sqlite3BtreeGetPageSize(p->pDest) );
56122       if( pgszSrc<pgszDest ){
56123         int ratio = pgszDest/pgszSrc;
56124         nDestTruncate = (nSrcPage+ratio-1)/ratio;
56125         if( nDestTruncate==(int)PENDING_BYTE_PAGE(p->pDest->pBt) ){
56126           nDestTruncate--;
56127         }
56128       }else{
56129         nDestTruncate = nSrcPage * (pgszSrc/pgszDest);
56130       }
56131       sqlite3PagerTruncateImage(pDestPager, nDestTruncate);
56132 
56133       if( pgszSrc<pgszDest ){
56134         /* If the source page-size is smaller than the destination page-size,
56135         ** two extra things may need to happen:
56136         **
56137         **   * The destination may need to be truncated, and
56138         **
56139         **   * Data stored on the pages immediately following the
56140         **     pending-byte page in the source database may need to be
56141         **     copied into the destination database.
56142         */
56143         const i64 iSize = (i64)pgszSrc * (i64)nSrcPage;
56144         sqlite3_file * const pFile = sqlite3PagerFile(pDestPager);
56145         i64 iOff;
56146         i64 iEnd;
56147 
56148         assert( pFile );
56149         assert( (i64)nDestTruncate*(i64)pgszDest >= iSize || (
56150               nDestTruncate==(int)(PENDING_BYTE_PAGE(p->pDest->pBt)-1)
56151            && iSize>=PENDING_BYTE && iSize<=PENDING_BYTE+pgszDest
56152         ));
56153 
56154         /* This call ensures that all data required to recreate the original
56155         ** database has been stored in the journal for pDestPager and the
56156         ** journal synced to disk. So at this point we may safely modify
56157         ** the database file in any way, knowing that if a power failure
56158         ** occurs, the original database will be reconstructed from the
56159         ** journal file.  */
56160         rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 1);
56161 
56162         /* Write the extra pages and truncate the database file as required. */
56163         iEnd = MIN(PENDING_BYTE + pgszDest, iSize);
56164         for(
56165           iOff=PENDING_BYTE+pgszSrc;
56166           rc==SQLITE_OK && iOff<iEnd;
56167           iOff+=pgszSrc
56168         ){
56169           PgHdr *pSrcPg = 0;
56170           const Pgno iSrcPg = (Pgno)((iOff/pgszSrc)+1);
56171           rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg);
56172           if( rc==SQLITE_OK ){
56173             u8 *zData = sqlite3PagerGetData(pSrcPg);
56174             rc = sqlite3OsWrite(pFile, zData, pgszSrc, iOff);
56175           }
56176           sqlite3PagerUnref(pSrcPg);
56177         }
56178         if( rc==SQLITE_OK ){
56179           rc = backupTruncateFile(pFile, iSize);
56180         }
56181 
56182         /* Sync the database file to disk. */
56183         if( rc==SQLITE_OK ){
56184           rc = sqlite3PagerSync(pDestPager);
56185         }
56186       }else{
56187         rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 0);
56188       }
56189 
56190       /* Finish committing the transaction to the destination database. */
56191       if( SQLITE_OK==rc
56192        && SQLITE_OK==(rc = sqlite3BtreeCommitPhaseTwo(p->pDest, 0))
56193       ){
56194         rc = SQLITE_DONE;
56195       }
56196     }
56197 
56198     /* If bCloseTrans is true, then this function opened a read transaction
56199     ** on the source database. Close the read transaction here. There is
56200     ** no need to check the return values of the btree methods here, as
56201     ** "committing" a read-only transaction cannot fail.
56202     */
56203     if( bCloseTrans ){
56204       TESTONLY( int rc2 );
56205       TESTONLY( rc2  = ) sqlite3BtreeCommitPhaseOne(p->pSrc, 0);
56206       TESTONLY( rc2 |= ) sqlite3BtreeCommitPhaseTwo(p->pSrc, 0);
56207       assert( rc2==SQLITE_OK );
56208     }
56209 
56210     if( rc==SQLITE_IOERR_NOMEM ){
56211       rc = SQLITE_NOMEM;
56212     }
56213     p->rc = rc;
56214   }
56215   if( p->pDestDb ){
56216     sqlite3_mutex_leave(p->pDestDb->mutex);
56217   }
56218   sqlite3BtreeLeave(p->pSrc);
56219   sqlite3_mutex_leave(p->pSrcDb->mutex);
56220   return rc;
56221 }
56222 
56223 /*
56224 ** Release all resources associated with an sqlite3_backup* handle.
56225 */
56226 SQLITE_API int sqlite3_backup_finish(sqlite3_backup *p){
56227   sqlite3_backup **pp;                 /* Ptr to head of pagers backup list */
56228   sqlite3_mutex *mutex;                /* Mutex to protect source database */
56229   int rc;                              /* Value to return */
56230 
56231   /* Enter the mutexes */
56232   if( p==0 ) return SQLITE_OK;
56233   sqlite3_mutex_enter(p->pSrcDb->mutex);
56234   sqlite3BtreeEnter(p->pSrc);
56235   mutex = p->pSrcDb->mutex;
56236   if( p->pDestDb ){
56237     sqlite3_mutex_enter(p->pDestDb->mutex);
56238   }
56239 
56240   /* Detach this backup from the source pager. */
56241   if( p->pDestDb ){
56242     p->pSrc->nBackup--;
56243   }
56244   if( p->isAttached ){
56245     pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc));
56246     while( *pp!=p ){
56247       pp = &(*pp)->pNext;
56248     }
56249     *pp = p->pNext;
56250   }
56251 
56252   /* If a transaction is still open on the Btree, roll it back. */
56253   sqlite3BtreeRollback(p->pDest);
56254 
56255   /* Set the error code of the destination database handle. */
56256   rc = (p->rc==SQLITE_DONE) ? SQLITE_OK : p->rc;
56257   sqlite3Error(p->pDestDb, rc, 0);
56258 
56259   /* Exit the mutexes and free the backup context structure. */
56260   if( p->pDestDb ){
56261     sqlite3_mutex_leave(p->pDestDb->mutex);
56262   }
56263   sqlite3BtreeLeave(p->pSrc);
56264   if( p->pDestDb ){
56265     /* EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
56266     ** call to sqlite3_backup_init() and is destroyed by a call to
56267     ** sqlite3_backup_finish(). */
56268     sqlite3_free(p);
56269   }
56270   sqlite3_mutex_leave(mutex);
56271   return rc;
56272 }
56273 
56274 /*
56275 ** Return the number of pages still to be backed up as of the most recent
56276 ** call to sqlite3_backup_step().
56277 */
56278 SQLITE_API int sqlite3_backup_remaining(sqlite3_backup *p){
56279   return p->nRemaining;
56280 }
56281 
56282 /*
56283 ** Return the total number of pages in the source database as of the most
56284 ** recent call to sqlite3_backup_step().
56285 */
56286 SQLITE_API int sqlite3_backup_pagecount(sqlite3_backup *p){
56287   return p->nPagecount;
56288 }
56289 
56290 /*
56291 ** This function is called after the contents of page iPage of the
56292 ** source database have been modified. If page iPage has already been
56293 ** copied into the destination database, then the data written to the
56294 ** destination is now invalidated. The destination copy of iPage needs
56295 ** to be updated with the new data before the backup operation is
56296 ** complete.
56297 **
56298 ** It is assumed that the mutex associated with the BtShared object
56299 ** corresponding to the source database is held when this function is
56300 ** called.
56301 */
56302 SQLITE_PRIVATE void sqlite3BackupUpdate(sqlite3_backup *pBackup, Pgno iPage, const u8 *aData){
56303   sqlite3_backup *p;                   /* Iterator variable */
56304   for(p=pBackup; p; p=p->pNext){
56305     assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) );
56306     if( !isFatalError(p->rc) && iPage<p->iNext ){
56307       /* The backup process p has already copied page iPage. But now it
56308       ** has been modified by a transaction on the source pager. Copy
56309       ** the new data into the backup.
56310       */
56311       int rc;
56312       assert( p->pDestDb );
56313       sqlite3_mutex_enter(p->pDestDb->mutex);
56314       rc = backupOnePage(p, iPage, aData);
56315       sqlite3_mutex_leave(p->pDestDb->mutex);
56316       assert( rc!=SQLITE_BUSY && rc!=SQLITE_LOCKED );
56317       if( rc!=SQLITE_OK ){
56318         p->rc = rc;
56319       }
56320     }
56321   }
56322 }
56323 
56324 /*
56325 ** Restart the backup process. This is called when the pager layer
56326 ** detects that the database has been modified by an external database
56327 ** connection. In this case there is no way of knowing which of the
56328 ** pages that have been copied into the destination database are still
56329 ** valid and which are not, so the entire process needs to be restarted.
56330 **
56331 ** It is assumed that the mutex associated with the BtShared object
56332 ** corresponding to the source database is held when this function is
56333 ** called.
56334 */
56335 SQLITE_PRIVATE void sqlite3BackupRestart(sqlite3_backup *pBackup){
56336   sqlite3_backup *p;                   /* Iterator variable */
56337   for(p=pBackup; p; p=p->pNext){
56338     assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) );
56339     p->iNext = 1;
56340   }
56341 }
56342 
56343 #ifndef SQLITE_OMIT_VACUUM
56344 /*
56345 ** Copy the complete content of pBtFrom into pBtTo.  A transaction
56346 ** must be active for both files.
56347 **
56348 ** The size of file pTo may be reduced by this operation. If anything
56349 ** goes wrong, the transaction on pTo is rolled back. If successful, the
56350 ** transaction is committed before returning.
56351 */
56352 SQLITE_PRIVATE int sqlite3BtreeCopyFile(Btree *pTo, Btree *pFrom){
56353   int rc;
56354   sqlite3_backup b;
56355   sqlite3BtreeEnter(pTo);
56356   sqlite3BtreeEnter(pFrom);
56357 
56358   /* Set up an sqlite3_backup object. sqlite3_backup.pDestDb must be set
56359   ** to 0. This is used by the implementations of sqlite3_backup_step()
56360   ** and sqlite3_backup_finish() to detect that they are being called
56361   ** from this function, not directly by the user.
56362   */
56363   memset(&b, 0, sizeof(b));
56364   b.pSrcDb = pFrom->db;
56365   b.pSrc = pFrom;
56366   b.pDest = pTo;
56367   b.iNext = 1;
56368 
56369   /* 0x7FFFFFFF is the hard limit for the number of pages in a database
56370   ** file. By passing this as the number of pages to copy to
56371   ** sqlite3_backup_step(), we can guarantee that the copy finishes
56372   ** within a single call (unless an error occurs). The assert() statement
56373   ** checks this assumption - (p->rc) should be set to either SQLITE_DONE
56374   ** or an error code.
56375   */
56376   sqlite3_backup_step(&b, 0x7FFFFFFF);
56377   assert( b.rc!=SQLITE_OK );
56378   rc = sqlite3_backup_finish(&b);
56379   if( rc==SQLITE_OK ){
56380     pTo->pBt->pageSizeFixed = 0;
56381   }
56382 
56383   sqlite3BtreeLeave(pFrom);
56384   sqlite3BtreeLeave(pTo);
56385   return rc;
56386 }
56387 #endif /* SQLITE_OMIT_VACUUM */
56388 
56389 /************** End of backup.c **********************************************/
56390 /************** Begin file vdbemem.c *****************************************/
56391 /*
56392 ** 2004 May 26
56393 **
56394 ** The author disclaims copyright to this source code.  In place of
56395 ** a legal notice, here is a blessing:
56396 **
56397 **    May you do good and not evil.
56398 **    May you find forgiveness for yourself and forgive others.
56399 **    May you share freely, never taking more than you give.
56400 **
56401 *************************************************************************
56402 **
56403 ** This file contains code use to manipulate "Mem" structure.  A "Mem"
56404 ** stores a single value in the VDBE.  Mem is an opaque structure visible
56405 ** only within the VDBE.  Interface routines refer to a Mem using the
56406 ** name sqlite_value
56407 */
56408 
56409 /*
56410 ** Call sqlite3VdbeMemExpandBlob() on the supplied value (type Mem*)
56411 ** P if required.
56412 */
56413 #define expandBlob(P) (((P)->flags&MEM_Zero)?sqlite3VdbeMemExpandBlob(P):0)
56414 
56415 /*
56416 ** If pMem is an object with a valid string representation, this routine
56417 ** ensures the internal encoding for the string representation is
56418 ** 'desiredEnc', one of SQLITE_UTF8, SQLITE_UTF16LE or SQLITE_UTF16BE.
56419 **
56420 ** If pMem is not a string object, or the encoding of the string
56421 ** representation is already stored using the requested encoding, then this
56422 ** routine is a no-op.
56423 **
56424 ** SQLITE_OK is returned if the conversion is successful (or not required).
56425 ** SQLITE_NOMEM may be returned if a malloc() fails during conversion
56426 ** between formats.
56427 */
56428 SQLITE_PRIVATE int sqlite3VdbeChangeEncoding(Mem *pMem, int desiredEnc){
56429   int rc;
56430   assert( (pMem->flags&MEM_RowSet)==0 );
56431   assert( desiredEnc==SQLITE_UTF8 || desiredEnc==SQLITE_UTF16LE
56432            || desiredEnc==SQLITE_UTF16BE );
56433   if( !(pMem->flags&MEM_Str) || pMem->enc==desiredEnc ){
56434     return SQLITE_OK;
56435   }
56436   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
56437 #ifdef SQLITE_OMIT_UTF16
56438   return SQLITE_ERROR;
56439 #else
56440 
56441   /* MemTranslate() may return SQLITE_OK or SQLITE_NOMEM. If NOMEM is returned,
56442   ** then the encoding of the value may not have changed.
56443   */
56444   rc = sqlite3VdbeMemTranslate(pMem, (u8)desiredEnc);
56445   assert(rc==SQLITE_OK    || rc==SQLITE_NOMEM);
56446   assert(rc==SQLITE_OK    || pMem->enc!=desiredEnc);
56447   assert(rc==SQLITE_NOMEM || pMem->enc==desiredEnc);
56448   return rc;
56449 #endif
56450 }
56451 
56452 /*
56453 ** Make sure pMem->z points to a writable allocation of at least
56454 ** n bytes.
56455 **
56456 ** If the memory cell currently contains string or blob data
56457 ** and the third argument passed to this function is true, the
56458 ** current content of the cell is preserved. Otherwise, it may
56459 ** be discarded.
56460 **
56461 ** This function sets the MEM_Dyn flag and clears any xDel callback.
56462 ** It also clears MEM_Ephem and MEM_Static. If the preserve flag is
56463 ** not set, Mem.n is zeroed.
56464 */
56465 SQLITE_PRIVATE int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve){
56466   assert( 1 >=
56467     ((pMem->zMalloc && pMem->zMalloc==pMem->z) ? 1 : 0) +
56468     (((pMem->flags&MEM_Dyn)&&pMem->xDel) ? 1 : 0) +
56469     ((pMem->flags&MEM_Ephem) ? 1 : 0) +
56470     ((pMem->flags&MEM_Static) ? 1 : 0)
56471   );
56472   assert( (pMem->flags&MEM_RowSet)==0 );
56473 
56474   if( n<32 ) n = 32;
56475   if( sqlite3DbMallocSize(pMem->db, pMem->zMalloc)<n ){
56476     if( preserve && pMem->z==pMem->zMalloc ){
56477       pMem->z = pMem->zMalloc = sqlite3DbReallocOrFree(pMem->db, pMem->z, n);
56478       preserve = 0;
56479     }else{
56480       sqlite3DbFree(pMem->db, pMem->zMalloc);
56481       pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, n);
56482     }
56483   }
56484 
56485   if( pMem->z && preserve && pMem->zMalloc && pMem->z!=pMem->zMalloc ){
56486     memcpy(pMem->zMalloc, pMem->z, pMem->n);
56487   }
56488   if( pMem->flags&MEM_Dyn && pMem->xDel ){
56489     pMem->xDel((void *)(pMem->z));
56490   }
56491 
56492   pMem->z = pMem->zMalloc;
56493   if( pMem->z==0 ){
56494     pMem->flags = MEM_Null;
56495   }else{
56496     pMem->flags &= ~(MEM_Ephem|MEM_Static);
56497   }
56498   pMem->xDel = 0;
56499   return (pMem->z ? SQLITE_OK : SQLITE_NOMEM);
56500 }
56501 
56502 /*
56503 ** Make the given Mem object MEM_Dyn.  In other words, make it so
56504 ** that any TEXT or BLOB content is stored in memory obtained from
56505 ** malloc().  In this way, we know that the memory is safe to be
56506 ** overwritten or altered.
56507 **
56508 ** Return SQLITE_OK on success or SQLITE_NOMEM if malloc fails.
56509 */
56510 SQLITE_PRIVATE int sqlite3VdbeMemMakeWriteable(Mem *pMem){
56511   int f;
56512   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
56513   assert( (pMem->flags&MEM_RowSet)==0 );
56514   expandBlob(pMem);
56515   f = pMem->flags;
56516   if( (f&(MEM_Str|MEM_Blob)) && pMem->z!=pMem->zMalloc ){
56517     if( sqlite3VdbeMemGrow(pMem, pMem->n + 2, 1) ){
56518       return SQLITE_NOMEM;
56519     }
56520     pMem->z[pMem->n] = 0;
56521     pMem->z[pMem->n+1] = 0;
56522     pMem->flags |= MEM_Term;
56523 #ifdef SQLITE_DEBUG
56524     pMem->pScopyFrom = 0;
56525 #endif
56526   }
56527 
56528   return SQLITE_OK;
56529 }
56530 
56531 /*
56532 ** If the given Mem* has a zero-filled tail, turn it into an ordinary
56533 ** blob stored in dynamically allocated space.
56534 */
56535 #ifndef SQLITE_OMIT_INCRBLOB
56536 SQLITE_PRIVATE int sqlite3VdbeMemExpandBlob(Mem *pMem){
56537   if( pMem->flags & MEM_Zero ){
56538     int nByte;
56539     assert( pMem->flags&MEM_Blob );
56540     assert( (pMem->flags&MEM_RowSet)==0 );
56541     assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
56542 
56543     /* Set nByte to the number of bytes required to store the expanded blob. */
56544     nByte = pMem->n + pMem->u.nZero;
56545     if( nByte<=0 ){
56546       nByte = 1;
56547     }
56548     if( sqlite3VdbeMemGrow(pMem, nByte, 1) ){
56549       return SQLITE_NOMEM;
56550     }
56551 
56552     memset(&pMem->z[pMem->n], 0, pMem->u.nZero);
56553     pMem->n += pMem->u.nZero;
56554     pMem->flags &= ~(MEM_Zero|MEM_Term);
56555   }
56556   return SQLITE_OK;
56557 }
56558 #endif
56559 
56560 
56561 /*
56562 ** Make sure the given Mem is \u0000 terminated.
56563 */
56564 SQLITE_PRIVATE int sqlite3VdbeMemNulTerminate(Mem *pMem){
56565   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
56566   if( (pMem->flags & MEM_Term)!=0 || (pMem->flags & MEM_Str)==0 ){
56567     return SQLITE_OK;   /* Nothing to do */
56568   }
56569   if( sqlite3VdbeMemGrow(pMem, pMem->n+2, 1) ){
56570     return SQLITE_NOMEM;
56571   }
56572   pMem->z[pMem->n] = 0;
56573   pMem->z[pMem->n+1] = 0;
56574   pMem->flags |= MEM_Term;
56575   return SQLITE_OK;
56576 }
56577 
56578 /*
56579 ** Add MEM_Str to the set of representations for the given Mem.  Numbers
56580 ** are converted using sqlite3_snprintf().  Converting a BLOB to a string
56581 ** is a no-op.
56582 **
56583 ** Existing representations MEM_Int and MEM_Real are *not* invalidated.
56584 **
56585 ** A MEM_Null value will never be passed to this function. This function is
56586 ** used for converting values to text for returning to the user (i.e. via
56587 ** sqlite3_value_text()), or for ensuring that values to be used as btree
56588 ** keys are strings. In the former case a NULL pointer is returned the
56589 ** user and the later is an internal programming error.
56590 */
56591 SQLITE_PRIVATE int sqlite3VdbeMemStringify(Mem *pMem, int enc){
56592   int rc = SQLITE_OK;
56593   int fg = pMem->flags;
56594   const int nByte = 32;
56595 
56596   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
56597   assert( !(fg&MEM_Zero) );
56598   assert( !(fg&(MEM_Str|MEM_Blob)) );
56599   assert( fg&(MEM_Int|MEM_Real) );
56600   assert( (pMem->flags&MEM_RowSet)==0 );
56601   assert( EIGHT_BYTE_ALIGNMENT(pMem) );
56602 
56603 
56604   if( sqlite3VdbeMemGrow(pMem, nByte, 0) ){
56605     return SQLITE_NOMEM;
56606   }
56607 
56608   /* For a Real or Integer, use sqlite3_mprintf() to produce the UTF-8
56609   ** string representation of the value. Then, if the required encoding
56610   ** is UTF-16le or UTF-16be do a translation.
56611   **
56612   ** FIX ME: It would be better if sqlite3_snprintf() could do UTF-16.
56613   */
56614   if( fg & MEM_Int ){
56615     sqlite3_snprintf(nByte, pMem->z, "%lld", pMem->u.i);
56616   }else{
56617     assert( fg & MEM_Real );
56618     sqlite3_snprintf(nByte, pMem->z, "%!.15g", pMem->r);
56619   }
56620   pMem->n = sqlite3Strlen30(pMem->z);
56621   pMem->enc = SQLITE_UTF8;
56622   pMem->flags |= MEM_Str|MEM_Term;
56623   sqlite3VdbeChangeEncoding(pMem, enc);
56624   return rc;
56625 }
56626 
56627 /*
56628 ** Memory cell pMem contains the context of an aggregate function.
56629 ** This routine calls the finalize method for that function.  The
56630 ** result of the aggregate is stored back into pMem.
56631 **
56632 ** Return SQLITE_ERROR if the finalizer reports an error.  SQLITE_OK
56633 ** otherwise.
56634 */
56635 SQLITE_PRIVATE int sqlite3VdbeMemFinalize(Mem *pMem, FuncDef *pFunc){
56636   int rc = SQLITE_OK;
56637   if( ALWAYS(pFunc && pFunc->xFinalize) ){
56638     sqlite3_context ctx;
56639     assert( (pMem->flags & MEM_Null)!=0 || pFunc==pMem->u.pDef );
56640     assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
56641     memset(&ctx, 0, sizeof(ctx));
56642     ctx.s.flags = MEM_Null;
56643     ctx.s.db = pMem->db;
56644     ctx.pMem = pMem;
56645     ctx.pFunc = pFunc;
56646     pFunc->xFinalize(&ctx); /* IMP: R-24505-23230 */
56647     assert( 0==(pMem->flags&MEM_Dyn) && !pMem->xDel );
56648     sqlite3DbFree(pMem->db, pMem->zMalloc);
56649     memcpy(pMem, &ctx.s, sizeof(ctx.s));
56650     rc = ctx.isError;
56651   }
56652   return rc;
56653 }
56654 
56655 /*
56656 ** If the memory cell contains a string value that must be freed by
56657 ** invoking an external callback, free it now. Calling this function
56658 ** does not free any Mem.zMalloc buffer.
56659 */
56660 SQLITE_PRIVATE void sqlite3VdbeMemReleaseExternal(Mem *p){
56661   assert( p->db==0 || sqlite3_mutex_held(p->db->mutex) );
56662   testcase( p->flags & MEM_Agg );
56663   testcase( p->flags & MEM_Dyn );
56664   testcase( p->flags & MEM_RowSet );
56665   testcase( p->flags & MEM_Frame );
56666   if( p->flags&(MEM_Agg|MEM_Dyn|MEM_RowSet|MEM_Frame) ){
56667     if( p->flags&MEM_Agg ){
56668       sqlite3VdbeMemFinalize(p, p->u.pDef);
56669       assert( (p->flags & MEM_Agg)==0 );
56670       sqlite3VdbeMemRelease(p);
56671     }else if( p->flags&MEM_Dyn && p->xDel ){
56672       assert( (p->flags&MEM_RowSet)==0 );
56673       p->xDel((void *)p->z);
56674       p->xDel = 0;
56675     }else if( p->flags&MEM_RowSet ){
56676       sqlite3RowSetClear(p->u.pRowSet);
56677     }else if( p->flags&MEM_Frame ){
56678       sqlite3VdbeMemSetNull(p);
56679     }
56680   }
56681 }
56682 
56683 /*
56684 ** Release any memory held by the Mem. This may leave the Mem in an
56685 ** inconsistent state, for example with (Mem.z==0) and
56686 ** (Mem.type==SQLITE_TEXT).
56687 */
56688 SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p){
56689   sqlite3VdbeMemReleaseExternal(p);
56690   sqlite3DbFree(p->db, p->zMalloc);
56691   p->z = 0;
56692   p->zMalloc = 0;
56693   p->xDel = 0;
56694 }
56695 
56696 /*
56697 ** Convert a 64-bit IEEE double into a 64-bit signed integer.
56698 ** If the double is too large, return 0x8000000000000000.
56699 **
56700 ** Most systems appear to do this simply by assigning
56701 ** variables and without the extra range tests.  But
56702 ** there are reports that windows throws an expection
56703 ** if the floating point value is out of range. (See ticket #2880.)
56704 ** Because we do not completely understand the problem, we will
56705 ** take the conservative approach and always do range tests
56706 ** before attempting the conversion.
56707 */
56708 static i64 doubleToInt64(double r){
56709 #ifdef SQLITE_OMIT_FLOATING_POINT
56710   /* When floating-point is omitted, double and int64 are the same thing */
56711   return r;
56712 #else
56713   /*
56714   ** Many compilers we encounter do not define constants for the
56715   ** minimum and maximum 64-bit integers, or they define them
56716   ** inconsistently.  And many do not understand the "LL" notation.
56717   ** So we define our own static constants here using nothing
56718   ** larger than a 32-bit integer constant.
56719   */
56720   static const i64 maxInt = LARGEST_INT64;
56721   static const i64 minInt = SMALLEST_INT64;
56722 
56723   if( r<(double)minInt ){
56724     return minInt;
56725   }else if( r>(double)maxInt ){
56726     /* minInt is correct here - not maxInt.  It turns out that assigning
56727     ** a very large positive number to an integer results in a very large
56728     ** negative integer.  This makes no sense, but it is what x86 hardware
56729     ** does so for compatibility we will do the same in software. */
56730     return minInt;
56731   }else{
56732     return (i64)r;
56733   }
56734 #endif
56735 }
56736 
56737 /*
56738 ** Return some kind of integer value which is the best we can do
56739 ** at representing the value that *pMem describes as an integer.
56740 ** If pMem is an integer, then the value is exact.  If pMem is
56741 ** a floating-point then the value returned is the integer part.
56742 ** If pMem is a string or blob, then we make an attempt to convert
56743 ** it into a integer and return that.  If pMem represents an
56744 ** an SQL-NULL value, return 0.
56745 **
56746 ** If pMem represents a string value, its encoding might be changed.
56747 */
56748 SQLITE_PRIVATE i64 sqlite3VdbeIntValue(Mem *pMem){
56749   int flags;
56750   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
56751   assert( EIGHT_BYTE_ALIGNMENT(pMem) );
56752   flags = pMem->flags;
56753   if( flags & MEM_Int ){
56754     return pMem->u.i;
56755   }else if( flags & MEM_Real ){
56756     return doubleToInt64(pMem->r);
56757   }else if( flags & (MEM_Str|MEM_Blob) ){
56758     i64 value = 0;
56759     assert( pMem->z || pMem->n==0 );
56760     testcase( pMem->z==0 );
56761     sqlite3Atoi64(pMem->z, &value, pMem->n, pMem->enc);
56762     return value;
56763   }else{
56764     return 0;
56765   }
56766 }
56767 
56768 /*
56769 ** Return the best representation of pMem that we can get into a
56770 ** double.  If pMem is already a double or an integer, return its
56771 ** value.  If it is a string or blob, try to convert it to a double.
56772 ** If it is a NULL, return 0.0.
56773 */
56774 SQLITE_PRIVATE double sqlite3VdbeRealValue(Mem *pMem){
56775   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
56776   assert( EIGHT_BYTE_ALIGNMENT(pMem) );
56777   if( pMem->flags & MEM_Real ){
56778     return pMem->r;
56779   }else if( pMem->flags & MEM_Int ){
56780     return (double)pMem->u.i;
56781   }else if( pMem->flags & (MEM_Str|MEM_Blob) ){
56782     /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
56783     double val = (double)0;
56784     sqlite3AtoF(pMem->z, &val, pMem->n, pMem->enc);
56785     return val;
56786   }else{
56787     /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
56788     return (double)0;
56789   }
56790 }
56791 
56792 /*
56793 ** The MEM structure is already a MEM_Real.  Try to also make it a
56794 ** MEM_Int if we can.
56795 */
56796 SQLITE_PRIVATE void sqlite3VdbeIntegerAffinity(Mem *pMem){
56797   assert( pMem->flags & MEM_Real );
56798   assert( (pMem->flags & MEM_RowSet)==0 );
56799   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
56800   assert( EIGHT_BYTE_ALIGNMENT(pMem) );
56801 
56802   pMem->u.i = doubleToInt64(pMem->r);
56803 
56804   /* Only mark the value as an integer if
56805   **
56806   **    (1) the round-trip conversion real->int->real is a no-op, and
56807   **    (2) The integer is neither the largest nor the smallest
56808   **        possible integer (ticket #3922)
56809   **
56810   ** The second and third terms in the following conditional enforces
56811   ** the second condition under the assumption that addition overflow causes
56812   ** values to wrap around.  On x86 hardware, the third term is always
56813   ** true and could be omitted.  But we leave it in because other
56814   ** architectures might behave differently.
56815   */
56816   if( pMem->r==(double)pMem->u.i && pMem->u.i>SMALLEST_INT64
56817       && ALWAYS(pMem->u.i<LARGEST_INT64) ){
56818     pMem->flags |= MEM_Int;
56819   }
56820 }
56821 
56822 /*
56823 ** Convert pMem to type integer.  Invalidate any prior representations.
56824 */
56825 SQLITE_PRIVATE int sqlite3VdbeMemIntegerify(Mem *pMem){
56826   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
56827   assert( (pMem->flags & MEM_RowSet)==0 );
56828   assert( EIGHT_BYTE_ALIGNMENT(pMem) );
56829 
56830   pMem->u.i = sqlite3VdbeIntValue(pMem);
56831   MemSetTypeFlag(pMem, MEM_Int);
56832   return SQLITE_OK;
56833 }
56834 
56835 /*
56836 ** Convert pMem so that it is of type MEM_Real.
56837 ** Invalidate any prior representations.
56838 */
56839 SQLITE_PRIVATE int sqlite3VdbeMemRealify(Mem *pMem){
56840   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
56841   assert( EIGHT_BYTE_ALIGNMENT(pMem) );
56842 
56843   pMem->r = sqlite3VdbeRealValue(pMem);
56844   MemSetTypeFlag(pMem, MEM_Real);
56845   return SQLITE_OK;
56846 }
56847 
56848 /*
56849 ** Convert pMem so that it has types MEM_Real or MEM_Int or both.
56850 ** Invalidate any prior representations.
56851 **
56852 ** Every effort is made to force the conversion, even if the input
56853 ** is a string that does not look completely like a number.  Convert
56854 ** as much of the string as we can and ignore the rest.
56855 */
56856 SQLITE_PRIVATE int sqlite3VdbeMemNumerify(Mem *pMem){
56857   if( (pMem->flags & (MEM_Int|MEM_Real|MEM_Null))==0 ){
56858     assert( (pMem->flags & (MEM_Blob|MEM_Str))!=0 );
56859     assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
56860     if( 0==sqlite3Atoi64(pMem->z, &pMem->u.i, pMem->n, pMem->enc) ){
56861       MemSetTypeFlag(pMem, MEM_Int);
56862     }else{
56863       pMem->r = sqlite3VdbeRealValue(pMem);
56864       MemSetTypeFlag(pMem, MEM_Real);
56865       sqlite3VdbeIntegerAffinity(pMem);
56866     }
56867   }
56868   assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_Null))!=0 );
56869   pMem->flags &= ~(MEM_Str|MEM_Blob);
56870   return SQLITE_OK;
56871 }
56872 
56873 /*
56874 ** Delete any previous value and set the value stored in *pMem to NULL.
56875 */
56876 SQLITE_PRIVATE void sqlite3VdbeMemSetNull(Mem *pMem){
56877   if( pMem->flags & MEM_Frame ){
56878     VdbeFrame *pFrame = pMem->u.pFrame;
56879     pFrame->pParent = pFrame->v->pDelFrame;
56880     pFrame->v->pDelFrame = pFrame;
56881   }
56882   if( pMem->flags & MEM_RowSet ){
56883     sqlite3RowSetClear(pMem->u.pRowSet);
56884   }
56885   MemSetTypeFlag(pMem, MEM_Null);
56886   pMem->type = SQLITE_NULL;
56887 }
56888 
56889 /*
56890 ** Delete any previous value and set the value to be a BLOB of length
56891 ** n containing all zeros.
56892 */
56893 SQLITE_PRIVATE void sqlite3VdbeMemSetZeroBlob(Mem *pMem, int n){
56894   sqlite3VdbeMemRelease(pMem);
56895   pMem->flags = MEM_Blob|MEM_Zero;
56896   pMem->type = SQLITE_BLOB;
56897   pMem->n = 0;
56898   if( n<0 ) n = 0;
56899   pMem->u.nZero = n;
56900   pMem->enc = SQLITE_UTF8;
56901 
56902 #ifdef SQLITE_OMIT_INCRBLOB
56903   sqlite3VdbeMemGrow(pMem, n, 0);
56904   if( pMem->z ){
56905     pMem->n = n;
56906     memset(pMem->z, 0, n);
56907   }
56908 #endif
56909 }
56910 
56911 /*
56912 ** Delete any previous value and set the value stored in *pMem to val,
56913 ** manifest type INTEGER.
56914 */
56915 SQLITE_PRIVATE void sqlite3VdbeMemSetInt64(Mem *pMem, i64 val){
56916   sqlite3VdbeMemRelease(pMem);
56917   pMem->u.i = val;
56918   pMem->flags = MEM_Int;
56919   pMem->type = SQLITE_INTEGER;
56920 }
56921 
56922 #ifndef SQLITE_OMIT_FLOATING_POINT
56923 /*
56924 ** Delete any previous value and set the value stored in *pMem to val,
56925 ** manifest type REAL.
56926 */
56927 SQLITE_PRIVATE void sqlite3VdbeMemSetDouble(Mem *pMem, double val){
56928   if( sqlite3IsNaN(val) ){
56929     sqlite3VdbeMemSetNull(pMem);
56930   }else{
56931     sqlite3VdbeMemRelease(pMem);
56932     pMem->r = val;
56933     pMem->flags = MEM_Real;
56934     pMem->type = SQLITE_FLOAT;
56935   }
56936 }
56937 #endif
56938 
56939 /*
56940 ** Delete any previous value and set the value of pMem to be an
56941 ** empty boolean index.
56942 */
56943 SQLITE_PRIVATE void sqlite3VdbeMemSetRowSet(Mem *pMem){
56944   sqlite3 *db = pMem->db;
56945   assert( db!=0 );
56946   assert( (pMem->flags & MEM_RowSet)==0 );
56947   sqlite3VdbeMemRelease(pMem);
56948   pMem->zMalloc = sqlite3DbMallocRaw(db, 64);
56949   if( db->mallocFailed ){
56950     pMem->flags = MEM_Null;
56951   }else{
56952     assert( pMem->zMalloc );
56953     pMem->u.pRowSet = sqlite3RowSetInit(db, pMem->zMalloc,
56954                                        sqlite3DbMallocSize(db, pMem->zMalloc));
56955     assert( pMem->u.pRowSet!=0 );
56956     pMem->flags = MEM_RowSet;
56957   }
56958 }
56959 
56960 /*
56961 ** Return true if the Mem object contains a TEXT or BLOB that is
56962 ** too large - whose size exceeds SQLITE_MAX_LENGTH.
56963 */
56964 SQLITE_PRIVATE int sqlite3VdbeMemTooBig(Mem *p){
56965   assert( p->db!=0 );
56966   if( p->flags & (MEM_Str|MEM_Blob) ){
56967     int n = p->n;
56968     if( p->flags & MEM_Zero ){
56969       n += p->u.nZero;
56970     }
56971     return n>p->db->aLimit[SQLITE_LIMIT_LENGTH];
56972   }
56973   return 0;
56974 }
56975 
56976 #ifdef SQLITE_DEBUG
56977 /*
56978 ** This routine prepares a memory cell for modication by breaking
56979 ** its link to a shallow copy and by marking any current shallow
56980 ** copies of this cell as invalid.
56981 **
56982 ** This is used for testing and debugging only - to make sure shallow
56983 ** copies are not misused.
56984 */
56985 SQLITE_PRIVATE void sqlite3VdbeMemPrepareToChange(Vdbe *pVdbe, Mem *pMem){
56986   int i;
56987   Mem *pX;
56988   for(i=1, pX=&pVdbe->aMem[1]; i<=pVdbe->nMem; i++, pX++){
56989     if( pX->pScopyFrom==pMem ){
56990       pX->flags |= MEM_Invalid;
56991       pX->pScopyFrom = 0;
56992     }
56993   }
56994   pMem->pScopyFrom = 0;
56995 }
56996 #endif /* SQLITE_DEBUG */
56997 
56998 /*
56999 ** Size of struct Mem not including the Mem.zMalloc member.
57000 */
57001 #define MEMCELLSIZE (size_t)(&(((Mem *)0)->zMalloc))
57002 
57003 /*
57004 ** Make an shallow copy of pFrom into pTo.  Prior contents of
57005 ** pTo are freed.  The pFrom->z field is not duplicated.  If
57006 ** pFrom->z is used, then pTo->z points to the same thing as pFrom->z
57007 ** and flags gets srcType (either MEM_Ephem or MEM_Static).
57008 */
57009 SQLITE_PRIVATE void sqlite3VdbeMemShallowCopy(Mem *pTo, const Mem *pFrom, int srcType){
57010   assert( (pFrom->flags & MEM_RowSet)==0 );
57011   sqlite3VdbeMemReleaseExternal(pTo);
57012   memcpy(pTo, pFrom, MEMCELLSIZE);
57013   pTo->xDel = 0;
57014   if( (pFrom->flags&MEM_Static)==0 ){
57015     pTo->flags &= ~(MEM_Dyn|MEM_Static|MEM_Ephem);
57016     assert( srcType==MEM_Ephem || srcType==MEM_Static );
57017     pTo->flags |= srcType;
57018   }
57019 }
57020 
57021 /*
57022 ** Make a full copy of pFrom into pTo.  Prior contents of pTo are
57023 ** freed before the copy is made.
57024 */
57025 SQLITE_PRIVATE int sqlite3VdbeMemCopy(Mem *pTo, const Mem *pFrom){
57026   int rc = SQLITE_OK;
57027 
57028   assert( (pFrom->flags & MEM_RowSet)==0 );
57029   sqlite3VdbeMemReleaseExternal(pTo);
57030   memcpy(pTo, pFrom, MEMCELLSIZE);
57031   pTo->flags &= ~MEM_Dyn;
57032 
57033   if( pTo->flags&(MEM_Str|MEM_Blob) ){
57034     if( 0==(pFrom->flags&MEM_Static) ){
57035       pTo->flags |= MEM_Ephem;
57036       rc = sqlite3VdbeMemMakeWriteable(pTo);
57037     }
57038   }
57039 
57040   return rc;
57041 }
57042 
57043 /*
57044 ** Transfer the contents of pFrom to pTo. Any existing value in pTo is
57045 ** freed. If pFrom contains ephemeral data, a copy is made.
57046 **
57047 ** pFrom contains an SQL NULL when this routine returns.
57048 */
57049 SQLITE_PRIVATE void sqlite3VdbeMemMove(Mem *pTo, Mem *pFrom){
57050   assert( pFrom->db==0 || sqlite3_mutex_held(pFrom->db->mutex) );
57051   assert( pTo->db==0 || sqlite3_mutex_held(pTo->db->mutex) );
57052   assert( pFrom->db==0 || pTo->db==0 || pFrom->db==pTo->db );
57053 
57054   sqlite3VdbeMemRelease(pTo);
57055   memcpy(pTo, pFrom, sizeof(Mem));
57056   pFrom->flags = MEM_Null;
57057   pFrom->xDel = 0;
57058   pFrom->zMalloc = 0;
57059 }
57060 
57061 /*
57062 ** Change the value of a Mem to be a string or a BLOB.
57063 **
57064 ** The memory management strategy depends on the value of the xDel
57065 ** parameter. If the value passed is SQLITE_TRANSIENT, then the
57066 ** string is copied into a (possibly existing) buffer managed by the
57067 ** Mem structure. Otherwise, any existing buffer is freed and the
57068 ** pointer copied.
57069 **
57070 ** If the string is too large (if it exceeds the SQLITE_LIMIT_LENGTH
57071 ** size limit) then no memory allocation occurs.  If the string can be
57072 ** stored without allocating memory, then it is.  If a memory allocation
57073 ** is required to store the string, then value of pMem is unchanged.  In
57074 ** either case, SQLITE_TOOBIG is returned.
57075 */
57076 SQLITE_PRIVATE int sqlite3VdbeMemSetStr(
57077   Mem *pMem,          /* Memory cell to set to string value */
57078   const char *z,      /* String pointer */
57079   int n,              /* Bytes in string, or negative */
57080   u8 enc,             /* Encoding of z.  0 for BLOBs */
57081   void (*xDel)(void*) /* Destructor function */
57082 ){
57083   int nByte = n;      /* New value for pMem->n */
57084   int iLimit;         /* Maximum allowed string or blob size */
57085   u16 flags = 0;      /* New value for pMem->flags */
57086 
57087   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
57088   assert( (pMem->flags & MEM_RowSet)==0 );
57089 
57090   /* If z is a NULL pointer, set pMem to contain an SQL NULL. */
57091   if( !z ){
57092     sqlite3VdbeMemSetNull(pMem);
57093     return SQLITE_OK;
57094   }
57095 
57096   if( pMem->db ){
57097     iLimit = pMem->db->aLimit[SQLITE_LIMIT_LENGTH];
57098   }else{
57099     iLimit = SQLITE_MAX_LENGTH;
57100   }
57101   flags = (enc==0?MEM_Blob:MEM_Str);
57102   if( nByte<0 ){
57103     assert( enc!=0 );
57104     if( enc==SQLITE_UTF8 ){
57105       for(nByte=0; nByte<=iLimit && z[nByte]; nByte++){}
57106     }else{
57107       for(nByte=0; nByte<=iLimit && (z[nByte] | z[nByte+1]); nByte+=2){}
57108     }
57109     flags |= MEM_Term;
57110   }
57111 
57112   /* The following block sets the new values of Mem.z and Mem.xDel. It
57113   ** also sets a flag in local variable "flags" to indicate the memory
57114   ** management (one of MEM_Dyn or MEM_Static).
57115   */
57116   if( xDel==SQLITE_TRANSIENT ){
57117     int nAlloc = nByte;
57118     if( flags&MEM_Term ){
57119       nAlloc += (enc==SQLITE_UTF8?1:2);
57120     }
57121     if( nByte>iLimit ){
57122       return SQLITE_TOOBIG;
57123     }
57124     if( sqlite3VdbeMemGrow(pMem, nAlloc, 0) ){
57125       return SQLITE_NOMEM;
57126     }
57127     memcpy(pMem->z, z, nAlloc);
57128   }else if( xDel==SQLITE_DYNAMIC ){
57129     sqlite3VdbeMemRelease(pMem);
57130     pMem->zMalloc = pMem->z = (char *)z;
57131     pMem->xDel = 0;
57132   }else{
57133     sqlite3VdbeMemRelease(pMem);
57134     pMem->z = (char *)z;
57135     pMem->xDel = xDel;
57136     flags |= ((xDel==SQLITE_STATIC)?MEM_Static:MEM_Dyn);
57137   }
57138 
57139   pMem->n = nByte;
57140   pMem->flags = flags;
57141   pMem->enc = (enc==0 ? SQLITE_UTF8 : enc);
57142   pMem->type = (enc==0 ? SQLITE_BLOB : SQLITE_TEXT);
57143 
57144 #ifndef SQLITE_OMIT_UTF16
57145   if( pMem->enc!=SQLITE_UTF8 && sqlite3VdbeMemHandleBom(pMem) ){
57146     return SQLITE_NOMEM;
57147   }
57148 #endif
57149 
57150   if( nByte>iLimit ){
57151     return SQLITE_TOOBIG;
57152   }
57153 
57154   return SQLITE_OK;
57155 }
57156 
57157 /*
57158 ** Compare the values contained by the two memory cells, returning
57159 ** negative, zero or positive if pMem1 is less than, equal to, or greater
57160 ** than pMem2. Sorting order is NULL's first, followed by numbers (integers
57161 ** and reals) sorted numerically, followed by text ordered by the collating
57162 ** sequence pColl and finally blob's ordered by memcmp().
57163 **
57164 ** Two NULL values are considered equal by this function.
57165 */
57166 SQLITE_PRIVATE int sqlite3MemCompare(const Mem *pMem1, const Mem *pMem2, const CollSeq *pColl){
57167   int rc;
57168   int f1, f2;
57169   int combined_flags;
57170 
57171   f1 = pMem1->flags;
57172   f2 = pMem2->flags;
57173   combined_flags = f1|f2;
57174   assert( (combined_flags & MEM_RowSet)==0 );
57175 
57176   /* If one value is NULL, it is less than the other. If both values
57177   ** are NULL, return 0.
57178   */
57179   if( combined_flags&MEM_Null ){
57180     return (f2&MEM_Null) - (f1&MEM_Null);
57181   }
57182 
57183   /* If one value is a number and the other is not, the number is less.
57184   ** If both are numbers, compare as reals if one is a real, or as integers
57185   ** if both values are integers.
57186   */
57187   if( combined_flags&(MEM_Int|MEM_Real) ){
57188     if( !(f1&(MEM_Int|MEM_Real)) ){
57189       return 1;
57190     }
57191     if( !(f2&(MEM_Int|MEM_Real)) ){
57192       return -1;
57193     }
57194     if( (f1 & f2 & MEM_Int)==0 ){
57195       double r1, r2;
57196       if( (f1&MEM_Real)==0 ){
57197         r1 = (double)pMem1->u.i;
57198       }else{
57199         r1 = pMem1->r;
57200       }
57201       if( (f2&MEM_Real)==0 ){
57202         r2 = (double)pMem2->u.i;
57203       }else{
57204         r2 = pMem2->r;
57205       }
57206       if( r1<r2 ) return -1;
57207       if( r1>r2 ) return 1;
57208       return 0;
57209     }else{
57210       assert( f1&MEM_Int );
57211       assert( f2&MEM_Int );
57212       if( pMem1->u.i < pMem2->u.i ) return -1;
57213       if( pMem1->u.i > pMem2->u.i ) return 1;
57214       return 0;
57215     }
57216   }
57217 
57218   /* If one value is a string and the other is a blob, the string is less.
57219   ** If both are strings, compare using the collating functions.
57220   */
57221   if( combined_flags&MEM_Str ){
57222     if( (f1 & MEM_Str)==0 ){
57223       return 1;
57224     }
57225     if( (f2 & MEM_Str)==0 ){
57226       return -1;
57227     }
57228 
57229     assert( pMem1->enc==pMem2->enc );
57230     assert( pMem1->enc==SQLITE_UTF8 ||
57231             pMem1->enc==SQLITE_UTF16LE || pMem1->enc==SQLITE_UTF16BE );
57232 
57233     /* The collation sequence must be defined at this point, even if
57234     ** the user deletes the collation sequence after the vdbe program is
57235     ** compiled (this was not always the case).
57236     */
57237     assert( !pColl || pColl->xCmp );
57238 
57239     if( pColl ){
57240       if( pMem1->enc==pColl->enc ){
57241         /* The strings are already in the correct encoding.  Call the
57242         ** comparison function directly */
57243         return pColl->xCmp(pColl->pUser,pMem1->n,pMem1->z,pMem2->n,pMem2->z);
57244       }else{
57245         const void *v1, *v2;
57246         int n1, n2;
57247         Mem c1;
57248         Mem c2;
57249         memset(&c1, 0, sizeof(c1));
57250         memset(&c2, 0, sizeof(c2));
57251         sqlite3VdbeMemShallowCopy(&c1, pMem1, MEM_Ephem);
57252         sqlite3VdbeMemShallowCopy(&c2, pMem2, MEM_Ephem);
57253         v1 = sqlite3ValueText((sqlite3_value*)&c1, pColl->enc);
57254         n1 = v1==0 ? 0 : c1.n;
57255         v2 = sqlite3ValueText((sqlite3_value*)&c2, pColl->enc);
57256         n2 = v2==0 ? 0 : c2.n;
57257         rc = pColl->xCmp(pColl->pUser, n1, v1, n2, v2);
57258         sqlite3VdbeMemRelease(&c1);
57259         sqlite3VdbeMemRelease(&c2);
57260         return rc;
57261       }
57262     }
57263     /* If a NULL pointer was passed as the collate function, fall through
57264     ** to the blob case and use memcmp().  */
57265   }
57266 
57267   /* Both values must be blobs.  Compare using memcmp().  */
57268   rc = memcmp(pMem1->z, pMem2->z, (pMem1->n>pMem2->n)?pMem2->n:pMem1->n);
57269   if( rc==0 ){
57270     rc = pMem1->n - pMem2->n;
57271   }
57272   return rc;
57273 }
57274 
57275 /*
57276 ** Move data out of a btree key or data field and into a Mem structure.
57277 ** The data or key is taken from the entry that pCur is currently pointing
57278 ** to.  offset and amt determine what portion of the data or key to retrieve.
57279 ** key is true to get the key or false to get data.  The result is written
57280 ** into the pMem element.
57281 **
57282 ** The pMem structure is assumed to be uninitialized.  Any prior content
57283 ** is overwritten without being freed.
57284 **
57285 ** If this routine fails for any reason (malloc returns NULL or unable
57286 ** to read from the disk) then the pMem is left in an inconsistent state.
57287 */
57288 SQLITE_PRIVATE int sqlite3VdbeMemFromBtree(
57289   BtCursor *pCur,   /* Cursor pointing at record to retrieve. */
57290   int offset,       /* Offset from the start of data to return bytes from. */
57291   int amt,          /* Number of bytes to return. */
57292   int key,          /* If true, retrieve from the btree key, not data. */
57293   Mem *pMem         /* OUT: Return data in this Mem structure. */
57294 ){
57295   char *zData;        /* Data from the btree layer */
57296   int available = 0;  /* Number of bytes available on the local btree page */
57297   int rc = SQLITE_OK; /* Return code */
57298 
57299   assert( sqlite3BtreeCursorIsValid(pCur) );
57300 
57301   /* Note: the calls to BtreeKeyFetch() and DataFetch() below assert()
57302   ** that both the BtShared and database handle mutexes are held. */
57303   assert( (pMem->flags & MEM_RowSet)==0 );
57304   if( key ){
57305     zData = (char *)sqlite3BtreeKeyFetch(pCur, &available);
57306   }else{
57307     zData = (char *)sqlite3BtreeDataFetch(pCur, &available);
57308   }
57309   assert( zData!=0 );
57310 
57311   if( offset+amt<=available && (pMem->flags&MEM_Dyn)==0 ){
57312     sqlite3VdbeMemRelease(pMem);
57313     pMem->z = &zData[offset];
57314     pMem->flags = MEM_Blob|MEM_Ephem;
57315   }else if( SQLITE_OK==(rc = sqlite3VdbeMemGrow(pMem, amt+2, 0)) ){
57316     pMem->flags = MEM_Blob|MEM_Dyn|MEM_Term;
57317     pMem->enc = 0;
57318     pMem->type = SQLITE_BLOB;
57319     if( key ){
57320       rc = sqlite3BtreeKey(pCur, offset, amt, pMem->z);
57321     }else{
57322       rc = sqlite3BtreeData(pCur, offset, amt, pMem->z);
57323     }
57324     pMem->z[amt] = 0;
57325     pMem->z[amt+1] = 0;
57326     if( rc!=SQLITE_OK ){
57327       sqlite3VdbeMemRelease(pMem);
57328     }
57329   }
57330   pMem->n = amt;
57331 
57332   return rc;
57333 }
57334 
57335 /* This function is only available internally, it is not part of the
57336 ** external API. It works in a similar way to sqlite3_value_text(),
57337 ** except the data returned is in the encoding specified by the second
57338 ** parameter, which must be one of SQLITE_UTF16BE, SQLITE_UTF16LE or
57339 ** SQLITE_UTF8.
57340 **
57341 ** (2006-02-16:)  The enc value can be or-ed with SQLITE_UTF16_ALIGNED.
57342 ** If that is the case, then the result must be aligned on an even byte
57343 ** boundary.
57344 */
57345 SQLITE_PRIVATE const void *sqlite3ValueText(sqlite3_value* pVal, u8 enc){
57346   if( !pVal ) return 0;
57347 
57348   assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) );
57349   assert( (enc&3)==(enc&~SQLITE_UTF16_ALIGNED) );
57350   assert( (pVal->flags & MEM_RowSet)==0 );
57351 
57352   if( pVal->flags&MEM_Null ){
57353     return 0;
57354   }
57355   assert( (MEM_Blob>>3) == MEM_Str );
57356   pVal->flags |= (pVal->flags & MEM_Blob)>>3;
57357   expandBlob(pVal);
57358   if( pVal->flags&MEM_Str ){
57359     sqlite3VdbeChangeEncoding(pVal, enc & ~SQLITE_UTF16_ALIGNED);
57360     if( (enc & SQLITE_UTF16_ALIGNED)!=0 && 1==(1&SQLITE_PTR_TO_INT(pVal->z)) ){
57361       assert( (pVal->flags & (MEM_Ephem|MEM_Static))!=0 );
57362       if( sqlite3VdbeMemMakeWriteable(pVal)!=SQLITE_OK ){
57363         return 0;
57364       }
57365     }
57366     sqlite3VdbeMemNulTerminate(pVal); /* IMP: R-59893-45467 */
57367   }else{
57368     assert( (pVal->flags&MEM_Blob)==0 );
57369     sqlite3VdbeMemStringify(pVal, enc);
57370     assert( 0==(1&SQLITE_PTR_TO_INT(pVal->z)) );
57371   }
57372   assert(pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) || pVal->db==0
57373               || pVal->db->mallocFailed );
57374   if( pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) ){
57375     return pVal->z;
57376   }else{
57377     return 0;
57378   }
57379 }
57380 
57381 /*
57382 ** Create a new sqlite3_value object.
57383 */
57384 SQLITE_PRIVATE sqlite3_value *sqlite3ValueNew(sqlite3 *db){
57385   Mem *p = sqlite3DbMallocZero(db, sizeof(*p));
57386   if( p ){
57387     p->flags = MEM_Null;
57388     p->type = SQLITE_NULL;
57389     p->db = db;
57390   }
57391   return p;
57392 }
57393 
57394 /*
57395 ** Create a new sqlite3_value object, containing the value of pExpr.
57396 **
57397 ** This only works for very simple expressions that consist of one constant
57398 ** token (i.e. "5", "5.1", "'a string'"). If the expression can
57399 ** be converted directly into a value, then the value is allocated and
57400 ** a pointer written to *ppVal. The caller is responsible for deallocating
57401 ** the value by passing it to sqlite3ValueFree() later on. If the expression
57402 ** cannot be converted to a value, then *ppVal is set to NULL.
57403 */
57404 SQLITE_PRIVATE int sqlite3ValueFromExpr(
57405   sqlite3 *db,              /* The database connection */
57406   Expr *pExpr,              /* The expression to evaluate */
57407   u8 enc,                   /* Encoding to use */
57408   u8 affinity,              /* Affinity to use */
57409   sqlite3_value **ppVal     /* Write the new value here */
57410 ){
57411   int op;
57412   char *zVal = 0;
57413   sqlite3_value *pVal = 0;
57414   int negInt = 1;
57415   const char *zNeg = "";
57416 
57417   if( !pExpr ){
57418     *ppVal = 0;
57419     return SQLITE_OK;
57420   }
57421   op = pExpr->op;
57422 
57423   /* op can only be TK_REGISTER if we have compiled with SQLITE_ENABLE_STAT2.
57424   ** The ifdef here is to enable us to achieve 100% branch test coverage even
57425   ** when SQLITE_ENABLE_STAT2 is omitted.
57426   */
57427 #ifdef SQLITE_ENABLE_STAT2
57428   if( op==TK_REGISTER ) op = pExpr->op2;
57429 #else
57430   if( NEVER(op==TK_REGISTER) ) op = pExpr->op2;
57431 #endif
57432 
57433   /* Handle negative integers in a single step.  This is needed in the
57434   ** case when the value is -9223372036854775808.
57435   */
57436   if( op==TK_UMINUS
57437    && (pExpr->pLeft->op==TK_INTEGER || pExpr->pLeft->op==TK_FLOAT) ){
57438     pExpr = pExpr->pLeft;
57439     op = pExpr->op;
57440     negInt = -1;
57441     zNeg = "-";
57442   }
57443 
57444   if( op==TK_STRING || op==TK_FLOAT || op==TK_INTEGER ){
57445     pVal = sqlite3ValueNew(db);
57446     if( pVal==0 ) goto no_mem;
57447     if( ExprHasProperty(pExpr, EP_IntValue) ){
57448       sqlite3VdbeMemSetInt64(pVal, (i64)pExpr->u.iValue*negInt);
57449     }else{
57450       zVal = sqlite3MPrintf(db, "%s%s", zNeg, pExpr->u.zToken);
57451       if( zVal==0 ) goto no_mem;
57452       sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF8, SQLITE_DYNAMIC);
57453       if( op==TK_FLOAT ) pVal->type = SQLITE_FLOAT;
57454     }
57455     if( (op==TK_INTEGER || op==TK_FLOAT ) && affinity==SQLITE_AFF_NONE ){
57456       sqlite3ValueApplyAffinity(pVal, SQLITE_AFF_NUMERIC, SQLITE_UTF8);
57457     }else{
57458       sqlite3ValueApplyAffinity(pVal, affinity, SQLITE_UTF8);
57459     }
57460     if( pVal->flags & (MEM_Int|MEM_Real) ) pVal->flags &= ~MEM_Str;
57461     if( enc!=SQLITE_UTF8 ){
57462       sqlite3VdbeChangeEncoding(pVal, enc);
57463     }
57464   }else if( op==TK_UMINUS ) {
57465     /* This branch happens for multiple negative signs.  Ex: -(-5) */
57466     if( SQLITE_OK==sqlite3ValueFromExpr(db,pExpr->pLeft,enc,affinity,&pVal) ){
57467       sqlite3VdbeMemNumerify(pVal);
57468       if( pVal->u.i==SMALLEST_INT64 ){
57469         pVal->flags &= MEM_Int;
57470         pVal->flags |= MEM_Real;
57471         pVal->r = (double)LARGEST_INT64;
57472       }else{
57473         pVal->u.i = -pVal->u.i;
57474       }
57475       pVal->r = -pVal->r;
57476       sqlite3ValueApplyAffinity(pVal, affinity, enc);
57477     }
57478   }else if( op==TK_NULL ){
57479     pVal = sqlite3ValueNew(db);
57480     if( pVal==0 ) goto no_mem;
57481   }
57482 #ifndef SQLITE_OMIT_BLOB_LITERAL
57483   else if( op==TK_BLOB ){
57484     int nVal;
57485     assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
57486     assert( pExpr->u.zToken[1]=='\'' );
57487     pVal = sqlite3ValueNew(db);
57488     if( !pVal ) goto no_mem;
57489     zVal = &pExpr->u.zToken[2];
57490     nVal = sqlite3Strlen30(zVal)-1;
57491     assert( zVal[nVal]=='\'' );
57492     sqlite3VdbeMemSetStr(pVal, sqlite3HexToBlob(db, zVal, nVal), nVal/2,
57493                          0, SQLITE_DYNAMIC);
57494   }
57495 #endif
57496 
57497   if( pVal ){
57498     sqlite3VdbeMemStoreType(pVal);
57499   }
57500   *ppVal = pVal;
57501   return SQLITE_OK;
57502 
57503 no_mem:
57504   db->mallocFailed = 1;
57505   sqlite3DbFree(db, zVal);
57506   sqlite3ValueFree(pVal);
57507   *ppVal = 0;
57508   return SQLITE_NOMEM;
57509 }
57510 
57511 /*
57512 ** Change the string value of an sqlite3_value object
57513 */
57514 SQLITE_PRIVATE void sqlite3ValueSetStr(
57515   sqlite3_value *v,     /* Value to be set */
57516   int n,                /* Length of string z */
57517   const void *z,        /* Text of the new string */
57518   u8 enc,               /* Encoding to use */
57519   void (*xDel)(void*)   /* Destructor for the string */
57520 ){
57521   if( v ) sqlite3VdbeMemSetStr((Mem *)v, z, n, enc, xDel);
57522 }
57523 
57524 /*
57525 ** Free an sqlite3_value object
57526 */
57527 SQLITE_PRIVATE void sqlite3ValueFree(sqlite3_value *v){
57528   if( !v ) return;
57529   sqlite3VdbeMemRelease((Mem *)v);
57530   sqlite3DbFree(((Mem*)v)->db, v);
57531 }
57532 
57533 /*
57534 ** Return the number of bytes in the sqlite3_value object assuming
57535 ** that it uses the encoding "enc"
57536 */
57537 SQLITE_PRIVATE int sqlite3ValueBytes(sqlite3_value *pVal, u8 enc){
57538   Mem *p = (Mem*)pVal;
57539   if( (p->flags & MEM_Blob)!=0 || sqlite3ValueText(pVal, enc) ){
57540     if( p->flags & MEM_Zero ){
57541       return p->n + p->u.nZero;
57542     }else{
57543       return p->n;
57544     }
57545   }
57546   return 0;
57547 }
57548 
57549 /************** End of vdbemem.c *********************************************/
57550 /************** Begin file vdbeaux.c *****************************************/
57551 /*
57552 ** 2003 September 6
57553 **
57554 ** The author disclaims copyright to this source code.  In place of
57555 ** a legal notice, here is a blessing:
57556 **
57557 **    May you do good and not evil.
57558 **    May you find forgiveness for yourself and forgive others.
57559 **    May you share freely, never taking more than you give.
57560 **
57561 *************************************************************************
57562 ** This file contains code used for creating, destroying, and populating
57563 ** a VDBE (or an "sqlite3_stmt" as it is known to the outside world.)  Prior
57564 ** to version 2.8.7, all this code was combined into the vdbe.c source file.
57565 ** But that file was getting too big so this subroutines were split out.
57566 */
57567 
57568 
57569 
57570 /*
57571 ** When debugging the code generator in a symbolic debugger, one can
57572 ** set the sqlite3VdbeAddopTrace to 1 and all opcodes will be printed
57573 ** as they are added to the instruction stream.
57574 */
57575 #ifdef SQLITE_DEBUG
57576 SQLITE_PRIVATE int sqlite3VdbeAddopTrace = 0;
57577 #endif
57578 
57579 
57580 /*
57581 ** Create a new virtual database engine.
57582 */
57583 SQLITE_PRIVATE Vdbe *sqlite3VdbeCreate(sqlite3 *db){
57584   Vdbe *p;
57585   p = sqlite3DbMallocZero(db, sizeof(Vdbe) );
57586   if( p==0 ) return 0;
57587   p->db = db;
57588   if( db->pVdbe ){
57589     db->pVdbe->pPrev = p;
57590   }
57591   p->pNext = db->pVdbe;
57592   p->pPrev = 0;
57593   db->pVdbe = p;
57594   p->magic = VDBE_MAGIC_INIT;
57595   return p;
57596 }
57597 
57598 /*
57599 ** Remember the SQL string for a prepared statement.
57600 */
57601 SQLITE_PRIVATE void sqlite3VdbeSetSql(Vdbe *p, const char *z, int n, int isPrepareV2){
57602   assert( isPrepareV2==1 || isPrepareV2==0 );
57603   if( p==0 ) return;
57604 #ifdef SQLITE_OMIT_TRACE
57605   if( !isPrepareV2 ) return;
57606 #endif
57607   assert( p->zSql==0 );
57608   p->zSql = sqlite3DbStrNDup(p->db, z, n);
57609   p->isPrepareV2 = (u8)isPrepareV2;
57610 }
57611 
57612 /*
57613 ** Return the SQL associated with a prepared statement
57614 */
57615 SQLITE_API const char *sqlite3_sql(sqlite3_stmt *pStmt){
57616   Vdbe *p = (Vdbe *)pStmt;
57617   return (p && p->isPrepareV2) ? p->zSql : 0;
57618 }
57619 
57620 /*
57621 ** Swap all content between two VDBE structures.
57622 */
57623 SQLITE_PRIVATE void sqlite3VdbeSwap(Vdbe *pA, Vdbe *pB){
57624   Vdbe tmp, *pTmp;
57625   char *zTmp;
57626   tmp = *pA;
57627   *pA = *pB;
57628   *pB = tmp;
57629   pTmp = pA->pNext;
57630   pA->pNext = pB->pNext;
57631   pB->pNext = pTmp;
57632   pTmp = pA->pPrev;
57633   pA->pPrev = pB->pPrev;
57634   pB->pPrev = pTmp;
57635   zTmp = pA->zSql;
57636   pA->zSql = pB->zSql;
57637   pB->zSql = zTmp;
57638   pB->isPrepareV2 = pA->isPrepareV2;
57639 }
57640 
57641 #ifdef SQLITE_DEBUG
57642 /*
57643 ** Turn tracing on or off
57644 */
57645 SQLITE_PRIVATE void sqlite3VdbeTrace(Vdbe *p, FILE *trace){
57646   p->trace = trace;
57647 }
57648 #endif
57649 
57650 /*
57651 ** Resize the Vdbe.aOp array so that it is at least one op larger than
57652 ** it was.
57653 **
57654 ** If an out-of-memory error occurs while resizing the array, return
57655 ** SQLITE_NOMEM. In this case Vdbe.aOp and Vdbe.nOpAlloc remain
57656 ** unchanged (this is so that any opcodes already allocated can be
57657 ** correctly deallocated along with the rest of the Vdbe).
57658 */
57659 static int growOpArray(Vdbe *p){
57660   VdbeOp *pNew;
57661   int nNew = (p->nOpAlloc ? p->nOpAlloc*2 : (int)(1024/sizeof(Op)));
57662   pNew = sqlite3DbRealloc(p->db, p->aOp, nNew*sizeof(Op));
57663   if( pNew ){
57664     p->nOpAlloc = sqlite3DbMallocSize(p->db, pNew)/sizeof(Op);
57665     p->aOp = pNew;
57666   }
57667   return (pNew ? SQLITE_OK : SQLITE_NOMEM);
57668 }
57669 
57670 /*
57671 ** Add a new instruction to the list of instructions current in the
57672 ** VDBE.  Return the address of the new instruction.
57673 **
57674 ** Parameters:
57675 **
57676 **    p               Pointer to the VDBE
57677 **
57678 **    op              The opcode for this instruction
57679 **
57680 **    p1, p2, p3      Operands
57681 **
57682 ** Use the sqlite3VdbeResolveLabel() function to fix an address and
57683 ** the sqlite3VdbeChangeP4() function to change the value of the P4
57684 ** operand.
57685 */
57686 SQLITE_PRIVATE int sqlite3VdbeAddOp3(Vdbe *p, int op, int p1, int p2, int p3){
57687   int i;
57688   VdbeOp *pOp;
57689 
57690   i = p->nOp;
57691   assert( p->magic==VDBE_MAGIC_INIT );
57692   assert( op>0 && op<0xff );
57693   if( p->nOpAlloc<=i ){
57694     if( growOpArray(p) ){
57695       return 1;
57696     }
57697   }
57698   p->nOp++;
57699   pOp = &p->aOp[i];
57700   pOp->opcode = (u8)op;
57701   pOp->p5 = 0;
57702   pOp->p1 = p1;
57703   pOp->p2 = p2;
57704   pOp->p3 = p3;
57705   pOp->p4.p = 0;
57706   pOp->p4type = P4_NOTUSED;
57707 #ifdef SQLITE_DEBUG
57708   pOp->zComment = 0;
57709   if( sqlite3VdbeAddopTrace ) sqlite3VdbePrintOp(0, i, &p->aOp[i]);
57710 #endif
57711 #ifdef VDBE_PROFILE
57712   pOp->cycles = 0;
57713   pOp->cnt = 0;
57714 #endif
57715   return i;
57716 }
57717 SQLITE_PRIVATE int sqlite3VdbeAddOp0(Vdbe *p, int op){
57718   return sqlite3VdbeAddOp3(p, op, 0, 0, 0);
57719 }
57720 SQLITE_PRIVATE int sqlite3VdbeAddOp1(Vdbe *p, int op, int p1){
57721   return sqlite3VdbeAddOp3(p, op, p1, 0, 0);
57722 }
57723 SQLITE_PRIVATE int sqlite3VdbeAddOp2(Vdbe *p, int op, int p1, int p2){
57724   return sqlite3VdbeAddOp3(p, op, p1, p2, 0);
57725 }
57726 
57727 
57728 /*
57729 ** Add an opcode that includes the p4 value as a pointer.
57730 */
57731 SQLITE_PRIVATE int sqlite3VdbeAddOp4(
57732   Vdbe *p,            /* Add the opcode to this VM */
57733   int op,             /* The new opcode */
57734   int p1,             /* The P1 operand */
57735   int p2,             /* The P2 operand */
57736   int p3,             /* The P3 operand */
57737   const char *zP4,    /* The P4 operand */
57738   int p4type          /* P4 operand type */
57739 ){
57740   int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3);
57741   sqlite3VdbeChangeP4(p, addr, zP4, p4type);
57742   return addr;
57743 }
57744 
57745 /*
57746 ** Add an OP_ParseSchema opcode.  This routine is broken out from
57747 ** sqlite3VdbeAddOp4() since it needs to also local all btrees.
57748 **
57749 ** The zWhere string must have been obtained from sqlite3_malloc().
57750 ** This routine will take ownership of the allocated memory.
57751 */
57752 SQLITE_PRIVATE void sqlite3VdbeAddParseSchemaOp(Vdbe *p, int iDb, char *zWhere){
57753   int j;
57754   int addr = sqlite3VdbeAddOp3(p, OP_ParseSchema, iDb, 0, 0);
57755   sqlite3VdbeChangeP4(p, addr, zWhere, P4_DYNAMIC);
57756   for(j=0; j<p->db->nDb; j++) sqlite3VdbeUsesBtree(p, j);
57757 }
57758 
57759 /*
57760 ** Add an opcode that includes the p4 value as an integer.
57761 */
57762 SQLITE_PRIVATE int sqlite3VdbeAddOp4Int(
57763   Vdbe *p,            /* Add the opcode to this VM */
57764   int op,             /* The new opcode */
57765   int p1,             /* The P1 operand */
57766   int p2,             /* The P2 operand */
57767   int p3,             /* The P3 operand */
57768   int p4              /* The P4 operand as an integer */
57769 ){
57770   int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3);
57771   sqlite3VdbeChangeP4(p, addr, SQLITE_INT_TO_PTR(p4), P4_INT32);
57772   return addr;
57773 }
57774 
57775 /*
57776 ** Create a new symbolic label for an instruction that has yet to be
57777 ** coded.  The symbolic label is really just a negative number.  The
57778 ** label can be used as the P2 value of an operation.  Later, when
57779 ** the label is resolved to a specific address, the VDBE will scan
57780 ** through its operation list and change all values of P2 which match
57781 ** the label into the resolved address.
57782 **
57783 ** The VDBE knows that a P2 value is a label because labels are
57784 ** always negative and P2 values are suppose to be non-negative.
57785 ** Hence, a negative P2 value is a label that has yet to be resolved.
57786 **
57787 ** Zero is returned if a malloc() fails.
57788 */
57789 SQLITE_PRIVATE int sqlite3VdbeMakeLabel(Vdbe *p){
57790   int i;
57791   i = p->nLabel++;
57792   assert( p->magic==VDBE_MAGIC_INIT );
57793   if( i>=p->nLabelAlloc ){
57794     int n = p->nLabelAlloc*2 + 5;
57795     p->aLabel = sqlite3DbReallocOrFree(p->db, p->aLabel,
57796                                        n*sizeof(p->aLabel[0]));
57797     p->nLabelAlloc = sqlite3DbMallocSize(p->db, p->aLabel)/sizeof(p->aLabel[0]);
57798   }
57799   if( p->aLabel ){
57800     p->aLabel[i] = -1;
57801   }
57802   return -1-i;
57803 }
57804 
57805 /*
57806 ** Resolve label "x" to be the address of the next instruction to
57807 ** be inserted.  The parameter "x" must have been obtained from
57808 ** a prior call to sqlite3VdbeMakeLabel().
57809 */
57810 SQLITE_PRIVATE void sqlite3VdbeResolveLabel(Vdbe *p, int x){
57811   int j = -1-x;
57812   assert( p->magic==VDBE_MAGIC_INIT );
57813   assert( j>=0 && j<p->nLabel );
57814   if( p->aLabel ){
57815     p->aLabel[j] = p->nOp;
57816   }
57817 }
57818 
57819 /*
57820 ** Mark the VDBE as one that can only be run one time.
57821 */
57822 SQLITE_PRIVATE void sqlite3VdbeRunOnlyOnce(Vdbe *p){
57823   p->runOnlyOnce = 1;
57824 }
57825 
57826 #ifdef SQLITE_DEBUG /* sqlite3AssertMayAbort() logic */
57827 
57828 /*
57829 ** The following type and function are used to iterate through all opcodes
57830 ** in a Vdbe main program and each of the sub-programs (triggers) it may
57831 ** invoke directly or indirectly. It should be used as follows:
57832 **
57833 **   Op *pOp;
57834 **   VdbeOpIter sIter;
57835 **
57836 **   memset(&sIter, 0, sizeof(sIter));
57837 **   sIter.v = v;                            // v is of type Vdbe*
57838 **   while( (pOp = opIterNext(&sIter)) ){
57839 **     // Do something with pOp
57840 **   }
57841 **   sqlite3DbFree(v->db, sIter.apSub);
57842 **
57843 */
57844 typedef struct VdbeOpIter VdbeOpIter;
57845 struct VdbeOpIter {
57846   Vdbe *v;                   /* Vdbe to iterate through the opcodes of */
57847   SubProgram **apSub;        /* Array of subprograms */
57848   int nSub;                  /* Number of entries in apSub */
57849   int iAddr;                 /* Address of next instruction to return */
57850   int iSub;                  /* 0 = main program, 1 = first sub-program etc. */
57851 };
57852 static Op *opIterNext(VdbeOpIter *p){
57853   Vdbe *v = p->v;
57854   Op *pRet = 0;
57855   Op *aOp;
57856   int nOp;
57857 
57858   if( p->iSub<=p->nSub ){
57859 
57860     if( p->iSub==0 ){
57861       aOp = v->aOp;
57862       nOp = v->nOp;
57863     }else{
57864       aOp = p->apSub[p->iSub-1]->aOp;
57865       nOp = p->apSub[p->iSub-1]->nOp;
57866     }
57867     assert( p->iAddr<nOp );
57868 
57869     pRet = &aOp[p->iAddr];
57870     p->iAddr++;
57871     if( p->iAddr==nOp ){
57872       p->iSub++;
57873       p->iAddr = 0;
57874     }
57875 
57876     if( pRet->p4type==P4_SUBPROGRAM ){
57877       int nByte = (p->nSub+1)*sizeof(SubProgram*);
57878       int j;
57879       for(j=0; j<p->nSub; j++){
57880         if( p->apSub[j]==pRet->p4.pProgram ) break;
57881       }
57882       if( j==p->nSub ){
57883         p->apSub = sqlite3DbReallocOrFree(v->db, p->apSub, nByte);
57884         if( !p->apSub ){
57885           pRet = 0;
57886         }else{
57887           p->apSub[p->nSub++] = pRet->p4.pProgram;
57888         }
57889       }
57890     }
57891   }
57892 
57893   return pRet;
57894 }
57895 
57896 /*
57897 ** Check if the program stored in the VM associated with pParse may
57898 ** throw an ABORT exception (causing the statement, but not entire transaction
57899 ** to be rolled back). This condition is true if the main program or any
57900 ** sub-programs contains any of the following:
57901 **
57902 **   *  OP_Halt with P1=SQLITE_CONSTRAINT and P2=OE_Abort.
57903 **   *  OP_HaltIfNull with P1=SQLITE_CONSTRAINT and P2=OE_Abort.
57904 **   *  OP_Destroy
57905 **   *  OP_VUpdate
57906 **   *  OP_VRename
57907 **   *  OP_FkCounter with P2==0 (immediate foreign key constraint)
57908 **
57909 ** Then check that the value of Parse.mayAbort is true if an
57910 ** ABORT may be thrown, or false otherwise. Return true if it does
57911 ** match, or false otherwise. This function is intended to be used as
57912 ** part of an assert statement in the compiler. Similar to:
57913 **
57914 **   assert( sqlite3VdbeAssertMayAbort(pParse->pVdbe, pParse->mayAbort) );
57915 */
57916 SQLITE_PRIVATE int sqlite3VdbeAssertMayAbort(Vdbe *v, int mayAbort){
57917   int hasAbort = 0;
57918   Op *pOp;
57919   VdbeOpIter sIter;
57920   memset(&sIter, 0, sizeof(sIter));
57921   sIter.v = v;
57922 
57923   while( (pOp = opIterNext(&sIter))!=0 ){
57924     int opcode = pOp->opcode;
57925     if( opcode==OP_Destroy || opcode==OP_VUpdate || opcode==OP_VRename
57926 #ifndef SQLITE_OMIT_FOREIGN_KEY
57927      || (opcode==OP_FkCounter && pOp->p1==0 && pOp->p2==1)
57928 #endif
57929      || ((opcode==OP_Halt || opcode==OP_HaltIfNull)
57930       && (pOp->p1==SQLITE_CONSTRAINT && pOp->p2==OE_Abort))
57931     ){
57932       hasAbort = 1;
57933       break;
57934     }
57935   }
57936   sqlite3DbFree(v->db, sIter.apSub);
57937 
57938   /* Return true if hasAbort==mayAbort. Or if a malloc failure occured.
57939   ** If malloc failed, then the while() loop above may not have iterated
57940   ** through all opcodes and hasAbort may be set incorrectly. Return
57941   ** true for this case to prevent the assert() in the callers frame
57942   ** from failing.  */
57943   return ( v->db->mallocFailed || hasAbort==mayAbort );
57944 }
57945 #endif /* SQLITE_DEBUG - the sqlite3AssertMayAbort() function */
57946 
57947 /*
57948 ** Loop through the program looking for P2 values that are negative
57949 ** on jump instructions.  Each such value is a label.  Resolve the
57950 ** label by setting the P2 value to its correct non-zero value.
57951 **
57952 ** This routine is called once after all opcodes have been inserted.
57953 **
57954 ** Variable *pMaxFuncArgs is set to the maximum value of any P2 argument
57955 ** to an OP_Function, OP_AggStep or OP_VFilter opcode. This is used by
57956 ** sqlite3VdbeMakeReady() to size the Vdbe.apArg[] array.
57957 **
57958 ** The Op.opflags field is set on all opcodes.
57959 */
57960 static void resolveP2Values(Vdbe *p, int *pMaxFuncArgs){
57961   int i;
57962   int nMaxArgs = *pMaxFuncArgs;
57963   Op *pOp;
57964   int *aLabel = p->aLabel;
57965   p->readOnly = 1;
57966   for(pOp=p->aOp, i=p->nOp-1; i>=0; i--, pOp++){
57967     u8 opcode = pOp->opcode;
57968 
57969     pOp->opflags = sqlite3OpcodeProperty[opcode];
57970     if( opcode==OP_Function || opcode==OP_AggStep ){
57971       if( pOp->p5>nMaxArgs ) nMaxArgs = pOp->p5;
57972     }else if( (opcode==OP_Transaction && pOp->p2!=0) || opcode==OP_Vacuum ){
57973       p->readOnly = 0;
57974 #ifndef SQLITE_OMIT_VIRTUALTABLE
57975     }else if( opcode==OP_VUpdate ){
57976       if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2;
57977     }else if( opcode==OP_VFilter ){
57978       int n;
57979       assert( p->nOp - i >= 3 );
57980       assert( pOp[-1].opcode==OP_Integer );
57981       n = pOp[-1].p1;
57982       if( n>nMaxArgs ) nMaxArgs = n;
57983 #endif
57984     }
57985 
57986     if( (pOp->opflags & OPFLG_JUMP)!=0 && pOp->p2<0 ){
57987       assert( -1-pOp->p2<p->nLabel );
57988       pOp->p2 = aLabel[-1-pOp->p2];
57989     }
57990   }
57991   sqlite3DbFree(p->db, p->aLabel);
57992   p->aLabel = 0;
57993 
57994   *pMaxFuncArgs = nMaxArgs;
57995 }
57996 
57997 /*
57998 ** Return the address of the next instruction to be inserted.
57999 */
58000 SQLITE_PRIVATE int sqlite3VdbeCurrentAddr(Vdbe *p){
58001   assert( p->magic==VDBE_MAGIC_INIT );
58002   return p->nOp;
58003 }
58004 
58005 /*
58006 ** This function returns a pointer to the array of opcodes associated with
58007 ** the Vdbe passed as the first argument. It is the callers responsibility
58008 ** to arrange for the returned array to be eventually freed using the
58009 ** vdbeFreeOpArray() function.
58010 **
58011 ** Before returning, *pnOp is set to the number of entries in the returned
58012 ** array. Also, *pnMaxArg is set to the larger of its current value and
58013 ** the number of entries in the Vdbe.apArg[] array required to execute the
58014 ** returned program.
58015 */
58016 SQLITE_PRIVATE VdbeOp *sqlite3VdbeTakeOpArray(Vdbe *p, int *pnOp, int *pnMaxArg){
58017   VdbeOp *aOp = p->aOp;
58018   assert( aOp && !p->db->mallocFailed );
58019 
58020   /* Check that sqlite3VdbeUsesBtree() was not called on this VM */
58021   assert( p->btreeMask==0 );
58022 
58023   resolveP2Values(p, pnMaxArg);
58024   *pnOp = p->nOp;
58025   p->aOp = 0;
58026   return aOp;
58027 }
58028 
58029 /*
58030 ** Add a whole list of operations to the operation stack.  Return the
58031 ** address of the first operation added.
58032 */
58033 SQLITE_PRIVATE int sqlite3VdbeAddOpList(Vdbe *p, int nOp, VdbeOpList const *aOp){
58034   int addr;
58035   assert( p->magic==VDBE_MAGIC_INIT );
58036   if( p->nOp + nOp > p->nOpAlloc && growOpArray(p) ){
58037     return 0;
58038   }
58039   addr = p->nOp;
58040   if( ALWAYS(nOp>0) ){
58041     int i;
58042     VdbeOpList const *pIn = aOp;
58043     for(i=0; i<nOp; i++, pIn++){
58044       int p2 = pIn->p2;
58045       VdbeOp *pOut = &p->aOp[i+addr];
58046       pOut->opcode = pIn->opcode;
58047       pOut->p1 = pIn->p1;
58048       if( p2<0 && (sqlite3OpcodeProperty[pOut->opcode] & OPFLG_JUMP)!=0 ){
58049         pOut->p2 = addr + ADDR(p2);
58050       }else{
58051         pOut->p2 = p2;
58052       }
58053       pOut->p3 = pIn->p3;
58054       pOut->p4type = P4_NOTUSED;
58055       pOut->p4.p = 0;
58056       pOut->p5 = 0;
58057 #ifdef SQLITE_DEBUG
58058       pOut->zComment = 0;
58059       if( sqlite3VdbeAddopTrace ){
58060         sqlite3VdbePrintOp(0, i+addr, &p->aOp[i+addr]);
58061       }
58062 #endif
58063     }
58064     p->nOp += nOp;
58065   }
58066   return addr;
58067 }
58068 
58069 /*
58070 ** Change the value of the P1 operand for a specific instruction.
58071 ** This routine is useful when a large program is loaded from a
58072 ** static array using sqlite3VdbeAddOpList but we want to make a
58073 ** few minor changes to the program.
58074 */
58075 SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe *p, int addr, int val){
58076   assert( p!=0 );
58077   assert( addr>=0 );
58078   if( p->nOp>addr ){
58079     p->aOp[addr].p1 = val;
58080   }
58081 }
58082 
58083 /*
58084 ** Change the value of the P2 operand for a specific instruction.
58085 ** This routine is useful for setting a jump destination.
58086 */
58087 SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe *p, int addr, int val){
58088   assert( p!=0 );
58089   assert( addr>=0 );
58090   if( p->nOp>addr ){
58091     p->aOp[addr].p2 = val;
58092   }
58093 }
58094 
58095 /*
58096 ** Change the value of the P3 operand for a specific instruction.
58097 */
58098 SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe *p, int addr, int val){
58099   assert( p!=0 );
58100   assert( addr>=0 );
58101   if( p->nOp>addr ){
58102     p->aOp[addr].p3 = val;
58103   }
58104 }
58105 
58106 /*
58107 ** Change the value of the P5 operand for the most recently
58108 ** added operation.
58109 */
58110 SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe *p, u8 val){
58111   assert( p!=0 );
58112   if( p->aOp ){
58113     assert( p->nOp>0 );
58114     p->aOp[p->nOp-1].p5 = val;
58115   }
58116 }
58117 
58118 /*
58119 ** Change the P2 operand of instruction addr so that it points to
58120 ** the address of the next instruction to be coded.
58121 */
58122 SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe *p, int addr){
58123   assert( addr>=0 );
58124   sqlite3VdbeChangeP2(p, addr, p->nOp);
58125 }
58126 
58127 
58128 /*
58129 ** If the input FuncDef structure is ephemeral, then free it.  If
58130 ** the FuncDef is not ephermal, then do nothing.
58131 */
58132 static void freeEphemeralFunction(sqlite3 *db, FuncDef *pDef){
58133   if( ALWAYS(pDef) && (pDef->flags & SQLITE_FUNC_EPHEM)!=0 ){
58134     sqlite3DbFree(db, pDef);
58135   }
58136 }
58137 
58138 static void vdbeFreeOpArray(sqlite3 *, Op *, int);
58139 
58140 /*
58141 ** Delete a P4 value if necessary.
58142 */
58143 static void freeP4(sqlite3 *db, int p4type, void *p4){
58144   if( p4 ){
58145     assert( db );
58146     switch( p4type ){
58147       case P4_REAL:
58148       case P4_INT64:
58149       case P4_DYNAMIC:
58150       case P4_KEYINFO:
58151       case P4_INTARRAY:
58152       case P4_KEYINFO_HANDOFF: {
58153         sqlite3DbFree(db, p4);
58154         break;
58155       }
58156       case P4_MPRINTF: {
58157         if( db->pnBytesFreed==0 ) sqlite3_free(p4);
58158         break;
58159       }
58160       case P4_VDBEFUNC: {
58161         VdbeFunc *pVdbeFunc = (VdbeFunc *)p4;
58162         freeEphemeralFunction(db, pVdbeFunc->pFunc);
58163         if( db->pnBytesFreed==0 ) sqlite3VdbeDeleteAuxData(pVdbeFunc, 0);
58164         sqlite3DbFree(db, pVdbeFunc);
58165         break;
58166       }
58167       case P4_FUNCDEF: {
58168         freeEphemeralFunction(db, (FuncDef*)p4);
58169         break;
58170       }
58171       case P4_MEM: {
58172         if( db->pnBytesFreed==0 ){
58173           sqlite3ValueFree((sqlite3_value*)p4);
58174         }else{
58175           Mem *p = (Mem*)p4;
58176           sqlite3DbFree(db, p->zMalloc);
58177           sqlite3DbFree(db, p);
58178         }
58179         break;
58180       }
58181       case P4_VTAB : {
58182         if( db->pnBytesFreed==0 ) sqlite3VtabUnlock((VTable *)p4);
58183         break;
58184       }
58185     }
58186   }
58187 }
58188 
58189 /*
58190 ** Free the space allocated for aOp and any p4 values allocated for the
58191 ** opcodes contained within. If aOp is not NULL it is assumed to contain
58192 ** nOp entries.
58193 */
58194 static void vdbeFreeOpArray(sqlite3 *db, Op *aOp, int nOp){
58195   if( aOp ){
58196     Op *pOp;
58197     for(pOp=aOp; pOp<&aOp[nOp]; pOp++){
58198       freeP4(db, pOp->p4type, pOp->p4.p);
58199 #ifdef SQLITE_DEBUG
58200       sqlite3DbFree(db, pOp->zComment);
58201 #endif
58202     }
58203   }
58204   sqlite3DbFree(db, aOp);
58205 }
58206 
58207 /*
58208 ** Link the SubProgram object passed as the second argument into the linked
58209 ** list at Vdbe.pSubProgram. This list is used to delete all sub-program
58210 ** objects when the VM is no longer required.
58211 */
58212 SQLITE_PRIVATE void sqlite3VdbeLinkSubProgram(Vdbe *pVdbe, SubProgram *p){
58213   p->pNext = pVdbe->pProgram;
58214   pVdbe->pProgram = p;
58215 }
58216 
58217 /*
58218 ** Change N opcodes starting at addr to No-ops.
58219 */
58220 SQLITE_PRIVATE void sqlite3VdbeChangeToNoop(Vdbe *p, int addr, int N){
58221   if( p->aOp ){
58222     VdbeOp *pOp = &p->aOp[addr];
58223     sqlite3 *db = p->db;
58224     while( N-- ){
58225       freeP4(db, pOp->p4type, pOp->p4.p);
58226       memset(pOp, 0, sizeof(pOp[0]));
58227       pOp->opcode = OP_Noop;
58228       pOp++;
58229     }
58230   }
58231 }
58232 
58233 /*
58234 ** Change the value of the P4 operand for a specific instruction.
58235 ** This routine is useful when a large program is loaded from a
58236 ** static array using sqlite3VdbeAddOpList but we want to make a
58237 ** few minor changes to the program.
58238 **
58239 ** If n>=0 then the P4 operand is dynamic, meaning that a copy of
58240 ** the string is made into memory obtained from sqlite3_malloc().
58241 ** A value of n==0 means copy bytes of zP4 up to and including the
58242 ** first null byte.  If n>0 then copy n+1 bytes of zP4.
58243 **
58244 ** If n==P4_KEYINFO it means that zP4 is a pointer to a KeyInfo structure.
58245 ** A copy is made of the KeyInfo structure into memory obtained from
58246 ** sqlite3_malloc, to be freed when the Vdbe is finalized.
58247 ** n==P4_KEYINFO_HANDOFF indicates that zP4 points to a KeyInfo structure
58248 ** stored in memory that the caller has obtained from sqlite3_malloc. The
58249 ** caller should not free the allocation, it will be freed when the Vdbe is
58250 ** finalized.
58251 **
58252 ** Other values of n (P4_STATIC, P4_COLLSEQ etc.) indicate that zP4 points
58253 ** to a string or structure that is guaranteed to exist for the lifetime of
58254 ** the Vdbe. In these cases we can just copy the pointer.
58255 **
58256 ** If addr<0 then change P4 on the most recently inserted instruction.
58257 */
58258 SQLITE_PRIVATE void sqlite3VdbeChangeP4(Vdbe *p, int addr, const char *zP4, int n){
58259   Op *pOp;
58260   sqlite3 *db;
58261   assert( p!=0 );
58262   db = p->db;
58263   assert( p->magic==VDBE_MAGIC_INIT );
58264   if( p->aOp==0 || db->mallocFailed ){
58265     if ( n!=P4_KEYINFO && n!=P4_VTAB ) {
58266       freeP4(db, n, (void*)*(char**)&zP4);
58267     }
58268     return;
58269   }
58270   assert( p->nOp>0 );
58271   assert( addr<p->nOp );
58272   if( addr<0 ){
58273     addr = p->nOp - 1;
58274   }
58275   pOp = &p->aOp[addr];
58276   freeP4(db, pOp->p4type, pOp->p4.p);
58277   pOp->p4.p = 0;
58278   if( n==P4_INT32 ){
58279     /* Note: this cast is safe, because the origin data point was an int
58280     ** that was cast to a (const char *). */
58281     pOp->p4.i = SQLITE_PTR_TO_INT(zP4);
58282     pOp->p4type = P4_INT32;
58283   }else if( zP4==0 ){
58284     pOp->p4.p = 0;
58285     pOp->p4type = P4_NOTUSED;
58286   }else if( n==P4_KEYINFO ){
58287     KeyInfo *pKeyInfo;
58288     int nField, nByte;
58289 
58290     nField = ((KeyInfo*)zP4)->nField;
58291     nByte = sizeof(*pKeyInfo) + (nField-1)*sizeof(pKeyInfo->aColl[0]) + nField;
58292     pKeyInfo = sqlite3DbMallocRaw(0, nByte);
58293     pOp->p4.pKeyInfo = pKeyInfo;
58294     if( pKeyInfo ){
58295       u8 *aSortOrder;
58296       memcpy((char*)pKeyInfo, zP4, nByte - nField);
58297       aSortOrder = pKeyInfo->aSortOrder;
58298       if( aSortOrder ){
58299         pKeyInfo->aSortOrder = (unsigned char*)&pKeyInfo->aColl[nField];
58300         memcpy(pKeyInfo->aSortOrder, aSortOrder, nField);
58301       }
58302       pOp->p4type = P4_KEYINFO;
58303     }else{
58304       p->db->mallocFailed = 1;
58305       pOp->p4type = P4_NOTUSED;
58306     }
58307   }else if( n==P4_KEYINFO_HANDOFF ){
58308     pOp->p4.p = (void*)zP4;
58309     pOp->p4type = P4_KEYINFO;
58310   }else if( n==P4_VTAB ){
58311     pOp->p4.p = (void*)zP4;
58312     pOp->p4type = P4_VTAB;
58313     sqlite3VtabLock((VTable *)zP4);
58314     assert( ((VTable *)zP4)->db==p->db );
58315   }else if( n<0 ){
58316     pOp->p4.p = (void*)zP4;
58317     pOp->p4type = (signed char)n;
58318   }else{
58319     if( n==0 ) n = sqlite3Strlen30(zP4);
58320     pOp->p4.z = sqlite3DbStrNDup(p->db, zP4, n);
58321     pOp->p4type = P4_DYNAMIC;
58322   }
58323 }
58324 
58325 #ifndef NDEBUG
58326 /*
58327 ** Change the comment on the the most recently coded instruction.  Or
58328 ** insert a No-op and add the comment to that new instruction.  This
58329 ** makes the code easier to read during debugging.  None of this happens
58330 ** in a production build.
58331 */
58332 SQLITE_PRIVATE void sqlite3VdbeComment(Vdbe *p, const char *zFormat, ...){
58333   va_list ap;
58334   if( !p ) return;
58335   assert( p->nOp>0 || p->aOp==0 );
58336   assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->db->mallocFailed );
58337   if( p->nOp ){
58338     char **pz = &p->aOp[p->nOp-1].zComment;
58339     va_start(ap, zFormat);
58340     sqlite3DbFree(p->db, *pz);
58341     *pz = sqlite3VMPrintf(p->db, zFormat, ap);
58342     va_end(ap);
58343   }
58344 }
58345 SQLITE_PRIVATE void sqlite3VdbeNoopComment(Vdbe *p, const char *zFormat, ...){
58346   va_list ap;
58347   if( !p ) return;
58348   sqlite3VdbeAddOp0(p, OP_Noop);
58349   assert( p->nOp>0 || p->aOp==0 );
58350   assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->db->mallocFailed );
58351   if( p->nOp ){
58352     char **pz = &p->aOp[p->nOp-1].zComment;
58353     va_start(ap, zFormat);
58354     sqlite3DbFree(p->db, *pz);
58355     *pz = sqlite3VMPrintf(p->db, zFormat, ap);
58356     va_end(ap);
58357   }
58358 }
58359 #endif  /* NDEBUG */
58360 
58361 /*
58362 ** Return the opcode for a given address.  If the address is -1, then
58363 ** return the most recently inserted opcode.
58364 **
58365 ** If a memory allocation error has occurred prior to the calling of this
58366 ** routine, then a pointer to a dummy VdbeOp will be returned.  That opcode
58367 ** is readable but not writable, though it is cast to a writable value.
58368 ** The return of a dummy opcode allows the call to continue functioning
58369 ** after a OOM fault without having to check to see if the return from
58370 ** this routine is a valid pointer.  But because the dummy.opcode is 0,
58371 ** dummy will never be written to.  This is verified by code inspection and
58372 ** by running with Valgrind.
58373 **
58374 ** About the #ifdef SQLITE_OMIT_TRACE:  Normally, this routine is never called
58375 ** unless p->nOp>0.  This is because in the absense of SQLITE_OMIT_TRACE,
58376 ** an OP_Trace instruction is always inserted by sqlite3VdbeGet() as soon as
58377 ** a new VDBE is created.  So we are free to set addr to p->nOp-1 without
58378 ** having to double-check to make sure that the result is non-negative. But
58379 ** if SQLITE_OMIT_TRACE is defined, the OP_Trace is omitted and we do need to
58380 ** check the value of p->nOp-1 before continuing.
58381 */
58382 SQLITE_PRIVATE VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){
58383   /* C89 specifies that the constant "dummy" will be initialized to all
58384   ** zeros, which is correct.  MSVC generates a warning, nevertheless. */
58385   static const VdbeOp dummy;  /* Ignore the MSVC warning about no initializer */
58386   assert( p->magic==VDBE_MAGIC_INIT );
58387   if( addr<0 ){
58388 #ifdef SQLITE_OMIT_TRACE
58389     if( p->nOp==0 ) return (VdbeOp*)&dummy;
58390 #endif
58391     addr = p->nOp - 1;
58392   }
58393   assert( (addr>=0 && addr<p->nOp) || p->db->mallocFailed );
58394   if( p->db->mallocFailed ){
58395     return (VdbeOp*)&dummy;
58396   }else{
58397     return &p->aOp[addr];
58398   }
58399 }
58400 
58401 #if !defined(SQLITE_OMIT_EXPLAIN) || !defined(NDEBUG) \
58402      || defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
58403 /*
58404 ** Compute a string that describes the P4 parameter for an opcode.
58405 ** Use zTemp for any required temporary buffer space.
58406 */
58407 static char *displayP4(Op *pOp, char *zTemp, int nTemp){
58408   char *zP4 = zTemp;
58409   assert( nTemp>=20 );
58410   switch( pOp->p4type ){
58411     case P4_KEYINFO_STATIC:
58412     case P4_KEYINFO: {
58413       int i, j;
58414       KeyInfo *pKeyInfo = pOp->p4.pKeyInfo;
58415       sqlite3_snprintf(nTemp, zTemp, "keyinfo(%d", pKeyInfo->nField);
58416       i = sqlite3Strlen30(zTemp);
58417       for(j=0; j<pKeyInfo->nField; j++){
58418         CollSeq *pColl = pKeyInfo->aColl[j];
58419         if( pColl ){
58420           int n = sqlite3Strlen30(pColl->zName);
58421           if( i+n>nTemp-6 ){
58422             memcpy(&zTemp[i],",...",4);
58423             break;
58424           }
58425           zTemp[i++] = ',';
58426           if( pKeyInfo->aSortOrder && pKeyInfo->aSortOrder[j] ){
58427             zTemp[i++] = '-';
58428           }
58429           memcpy(&zTemp[i], pColl->zName,n+1);
58430           i += n;
58431         }else if( i+4<nTemp-6 ){
58432           memcpy(&zTemp[i],",nil",4);
58433           i += 4;
58434         }
58435       }
58436       zTemp[i++] = ')';
58437       zTemp[i] = 0;
58438       assert( i<nTemp );
58439       break;
58440     }
58441     case P4_COLLSEQ: {
58442       CollSeq *pColl = pOp->p4.pColl;
58443       sqlite3_snprintf(nTemp, zTemp, "collseq(%.20s)", pColl->zName);
58444       break;
58445     }
58446     case P4_FUNCDEF: {
58447       FuncDef *pDef = pOp->p4.pFunc;
58448       sqlite3_snprintf(nTemp, zTemp, "%s(%d)", pDef->zName, pDef->nArg);
58449       break;
58450     }
58451     case P4_INT64: {
58452       sqlite3_snprintf(nTemp, zTemp, "%lld", *pOp->p4.pI64);
58453       break;
58454     }
58455     case P4_INT32: {
58456       sqlite3_snprintf(nTemp, zTemp, "%d", pOp->p4.i);
58457       break;
58458     }
58459     case P4_REAL: {
58460       sqlite3_snprintf(nTemp, zTemp, "%.16g", *pOp->p4.pReal);
58461       break;
58462     }
58463     case P4_MEM: {
58464       Mem *pMem = pOp->p4.pMem;
58465       assert( (pMem->flags & MEM_Null)==0 );
58466       if( pMem->flags & MEM_Str ){
58467         zP4 = pMem->z;
58468       }else if( pMem->flags & MEM_Int ){
58469         sqlite3_snprintf(nTemp, zTemp, "%lld", pMem->u.i);
58470       }else if( pMem->flags & MEM_Real ){
58471         sqlite3_snprintf(nTemp, zTemp, "%.16g", pMem->r);
58472       }else{
58473         assert( pMem->flags & MEM_Blob );
58474         zP4 = "(blob)";
58475       }
58476       break;
58477     }
58478 #ifndef SQLITE_OMIT_VIRTUALTABLE
58479     case P4_VTAB: {
58480       sqlite3_vtab *pVtab = pOp->p4.pVtab->pVtab;
58481       sqlite3_snprintf(nTemp, zTemp, "vtab:%p:%p", pVtab, pVtab->pModule);
58482       break;
58483     }
58484 #endif
58485     case P4_INTARRAY: {
58486       sqlite3_snprintf(nTemp, zTemp, "intarray");
58487       break;
58488     }
58489     case P4_SUBPROGRAM: {
58490       sqlite3_snprintf(nTemp, zTemp, "program");
58491       break;
58492     }
58493     default: {
58494       zP4 = pOp->p4.z;
58495       if( zP4==0 ){
58496         zP4 = zTemp;
58497         zTemp[0] = 0;
58498       }
58499     }
58500   }
58501   assert( zP4!=0 );
58502   return zP4;
58503 }
58504 #endif
58505 
58506 /*
58507 ** Declare to the Vdbe that the BTree object at db->aDb[i] is used.
58508 **
58509 ** The prepared statements need to know in advance the complete set of
58510 ** attached databases that they will be using.  A mask of these databases
58511 ** is maintained in p->btreeMask and is used for locking and other purposes.
58512 */
58513 SQLITE_PRIVATE void sqlite3VdbeUsesBtree(Vdbe *p, int i){
58514   assert( i>=0 && i<p->db->nDb && i<(int)sizeof(yDbMask)*8 );
58515   assert( i<(int)sizeof(p->btreeMask)*8 );
58516   p->btreeMask |= ((yDbMask)1)<<i;
58517   if( i!=1 && sqlite3BtreeSharable(p->db->aDb[i].pBt) ){
58518     p->lockMask |= ((yDbMask)1)<<i;
58519   }
58520 }
58521 
58522 #if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
58523 /*
58524 ** If SQLite is compiled to support shared-cache mode and to be threadsafe,
58525 ** this routine obtains the mutex associated with each BtShared structure
58526 ** that may be accessed by the VM passed as an argument. In doing so it also
58527 ** sets the BtShared.db member of each of the BtShared structures, ensuring
58528 ** that the correct busy-handler callback is invoked if required.
58529 **
58530 ** If SQLite is not threadsafe but does support shared-cache mode, then
58531 ** sqlite3BtreeEnter() is invoked to set the BtShared.db variables
58532 ** of all of BtShared structures accessible via the database handle
58533 ** associated with the VM.
58534 **
58535 ** If SQLite is not threadsafe and does not support shared-cache mode, this
58536 ** function is a no-op.
58537 **
58538 ** The p->btreeMask field is a bitmask of all btrees that the prepared
58539 ** statement p will ever use.  Let N be the number of bits in p->btreeMask
58540 ** corresponding to btrees that use shared cache.  Then the runtime of
58541 ** this routine is N*N.  But as N is rarely more than 1, this should not
58542 ** be a problem.
58543 */
58544 SQLITE_PRIVATE void sqlite3VdbeEnter(Vdbe *p){
58545   int i;
58546   yDbMask mask;
58547   sqlite3 *db;
58548   Db *aDb;
58549   int nDb;
58550   if( p->lockMask==0 ) return;  /* The common case */
58551   db = p->db;
58552   aDb = db->aDb;
58553   nDb = db->nDb;
58554   for(i=0, mask=1; i<nDb; i++, mask += mask){
58555     if( i!=1 && (mask & p->lockMask)!=0 && ALWAYS(aDb[i].pBt!=0) ){
58556       sqlite3BtreeEnter(aDb[i].pBt);
58557     }
58558   }
58559 }
58560 #endif
58561 
58562 #if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
58563 /*
58564 ** Unlock all of the btrees previously locked by a call to sqlite3VdbeEnter().
58565 */
58566 SQLITE_PRIVATE void sqlite3VdbeLeave(Vdbe *p){
58567   int i;
58568   yDbMask mask;
58569   sqlite3 *db;
58570   Db *aDb;
58571   int nDb;
58572   if( p->lockMask==0 ) return;  /* The common case */
58573   db = p->db;
58574   aDb = db->aDb;
58575   nDb = db->nDb;
58576   for(i=0, mask=1; i<nDb; i++, mask += mask){
58577     if( i!=1 && (mask & p->lockMask)!=0 && ALWAYS(aDb[i].pBt!=0) ){
58578       sqlite3BtreeLeave(aDb[i].pBt);
58579     }
58580   }
58581 }
58582 #endif
58583 
58584 #if defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
58585 /*
58586 ** Print a single opcode.  This routine is used for debugging only.
58587 */
58588 SQLITE_PRIVATE void sqlite3VdbePrintOp(FILE *pOut, int pc, Op *pOp){
58589   char *zP4;
58590   char zPtr[50];
58591   static const char *zFormat1 = "%4d %-13s %4d %4d %4d %-4s %.2X %s\n";
58592   if( pOut==0 ) pOut = stdout;
58593   zP4 = displayP4(pOp, zPtr, sizeof(zPtr));
58594   fprintf(pOut, zFormat1, pc,
58595       sqlite3OpcodeName(pOp->opcode), pOp->p1, pOp->p2, pOp->p3, zP4, pOp->p5,
58596 #ifdef SQLITE_DEBUG
58597       pOp->zComment ? pOp->zComment : ""
58598 #else
58599       ""
58600 #endif
58601   );
58602   fflush(pOut);
58603 }
58604 #endif
58605 
58606 /*
58607 ** Release an array of N Mem elements
58608 */
58609 static void releaseMemArray(Mem *p, int N){
58610   if( p && N ){
58611     Mem *pEnd;
58612     sqlite3 *db = p->db;
58613     u8 malloc_failed = db->mallocFailed;
58614     if( db->pnBytesFreed ){
58615       for(pEnd=&p[N]; p<pEnd; p++){
58616         sqlite3DbFree(db, p->zMalloc);
58617       }
58618       return;
58619     }
58620     for(pEnd=&p[N]; p<pEnd; p++){
58621       assert( (&p[1])==pEnd || p[0].db==p[1].db );
58622 
58623       /* This block is really an inlined version of sqlite3VdbeMemRelease()
58624       ** that takes advantage of the fact that the memory cell value is
58625       ** being set to NULL after releasing any dynamic resources.
58626       **
58627       ** The justification for duplicating code is that according to
58628       ** callgrind, this causes a certain test case to hit the CPU 4.7
58629       ** percent less (x86 linux, gcc version 4.1.2, -O6) than if
58630       ** sqlite3MemRelease() were called from here. With -O2, this jumps
58631       ** to 6.6 percent. The test case is inserting 1000 rows into a table
58632       ** with no indexes using a single prepared INSERT statement, bind()
58633       ** and reset(). Inserts are grouped into a transaction.
58634       */
58635       if( p->flags&(MEM_Agg|MEM_Dyn|MEM_Frame|MEM_RowSet) ){
58636         sqlite3VdbeMemRelease(p);
58637       }else if( p->zMalloc ){
58638         sqlite3DbFree(db, p->zMalloc);
58639         p->zMalloc = 0;
58640       }
58641 
58642       p->flags = MEM_Null;
58643     }
58644     db->mallocFailed = malloc_failed;
58645   }
58646 }
58647 
58648 /*
58649 ** Delete a VdbeFrame object and its contents. VdbeFrame objects are
58650 ** allocated by the OP_Program opcode in sqlite3VdbeExec().
58651 */
58652 SQLITE_PRIVATE void sqlite3VdbeFrameDelete(VdbeFrame *p){
58653   int i;
58654   Mem *aMem = VdbeFrameMem(p);
58655   VdbeCursor **apCsr = (VdbeCursor **)&aMem[p->nChildMem];
58656   for(i=0; i<p->nChildCsr; i++){
58657     sqlite3VdbeFreeCursor(p->v, apCsr[i]);
58658   }
58659   releaseMemArray(aMem, p->nChildMem);
58660   sqlite3DbFree(p->v->db, p);
58661 }
58662 
58663 #ifndef SQLITE_OMIT_EXPLAIN
58664 /*
58665 ** Give a listing of the program in the virtual machine.
58666 **
58667 ** The interface is the same as sqlite3VdbeExec().  But instead of
58668 ** running the code, it invokes the callback once for each instruction.
58669 ** This feature is used to implement "EXPLAIN".
58670 **
58671 ** When p->explain==1, each instruction is listed.  When
58672 ** p->explain==2, only OP_Explain instructions are listed and these
58673 ** are shown in a different format.  p->explain==2 is used to implement
58674 ** EXPLAIN QUERY PLAN.
58675 **
58676 ** When p->explain==1, first the main program is listed, then each of
58677 ** the trigger subprograms are listed one by one.
58678 */
58679 SQLITE_PRIVATE int sqlite3VdbeList(
58680   Vdbe *p                   /* The VDBE */
58681 ){
58682   int nRow;                            /* Stop when row count reaches this */
58683   int nSub = 0;                        /* Number of sub-vdbes seen so far */
58684   SubProgram **apSub = 0;              /* Array of sub-vdbes */
58685   Mem *pSub = 0;                       /* Memory cell hold array of subprogs */
58686   sqlite3 *db = p->db;                 /* The database connection */
58687   int i;                               /* Loop counter */
58688   int rc = SQLITE_OK;                  /* Return code */
58689   Mem *pMem = p->pResultSet = &p->aMem[1];  /* First Mem of result set */
58690 
58691   assert( p->explain );
58692   assert( p->magic==VDBE_MAGIC_RUN );
58693   assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY || p->rc==SQLITE_NOMEM );
58694 
58695   /* Even though this opcode does not use dynamic strings for
58696   ** the result, result columns may become dynamic if the user calls
58697   ** sqlite3_column_text16(), causing a translation to UTF-16 encoding.
58698   */
58699   releaseMemArray(pMem, 8);
58700 
58701   if( p->rc==SQLITE_NOMEM ){
58702     /* This happens if a malloc() inside a call to sqlite3_column_text() or
58703     ** sqlite3_column_text16() failed.  */
58704     db->mallocFailed = 1;
58705     return SQLITE_ERROR;
58706   }
58707 
58708   /* When the number of output rows reaches nRow, that means the
58709   ** listing has finished and sqlite3_step() should return SQLITE_DONE.
58710   ** nRow is the sum of the number of rows in the main program, plus
58711   ** the sum of the number of rows in all trigger subprograms encountered
58712   ** so far.  The nRow value will increase as new trigger subprograms are
58713   ** encountered, but p->pc will eventually catch up to nRow.
58714   */
58715   nRow = p->nOp;
58716   if( p->explain==1 ){
58717     /* The first 8 memory cells are used for the result set.  So we will
58718     ** commandeer the 9th cell to use as storage for an array of pointers
58719     ** to trigger subprograms.  The VDBE is guaranteed to have at least 9
58720     ** cells.  */
58721     assert( p->nMem>9 );
58722     pSub = &p->aMem[9];
58723     if( pSub->flags&MEM_Blob ){
58724       /* On the first call to sqlite3_step(), pSub will hold a NULL.  It is
58725       ** initialized to a BLOB by the P4_SUBPROGRAM processing logic below */
58726       nSub = pSub->n/sizeof(Vdbe*);
58727       apSub = (SubProgram **)pSub->z;
58728     }
58729     for(i=0; i<nSub; i++){
58730       nRow += apSub[i]->nOp;
58731     }
58732   }
58733 
58734   do{
58735     i = p->pc++;
58736   }while( i<nRow && p->explain==2 && p->aOp[i].opcode!=OP_Explain );
58737   if( i>=nRow ){
58738     p->rc = SQLITE_OK;
58739     rc = SQLITE_DONE;
58740   }else if( db->u1.isInterrupted ){
58741     p->rc = SQLITE_INTERRUPT;
58742     rc = SQLITE_ERROR;
58743     sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(p->rc));
58744   }else{
58745     char *z;
58746     Op *pOp;
58747     if( i<p->nOp ){
58748       /* The output line number is small enough that we are still in the
58749       ** main program. */
58750       pOp = &p->aOp[i];
58751     }else{
58752       /* We are currently listing subprograms.  Figure out which one and
58753       ** pick up the appropriate opcode. */
58754       int j;
58755       i -= p->nOp;
58756       for(j=0; i>=apSub[j]->nOp; j++){
58757         i -= apSub[j]->nOp;
58758       }
58759       pOp = &apSub[j]->aOp[i];
58760     }
58761     if( p->explain==1 ){
58762       pMem->flags = MEM_Int;
58763       pMem->type = SQLITE_INTEGER;
58764       pMem->u.i = i;                                /* Program counter */
58765       pMem++;
58766 
58767       pMem->flags = MEM_Static|MEM_Str|MEM_Term;
58768       pMem->z = (char*)sqlite3OpcodeName(pOp->opcode);  /* Opcode */
58769       assert( pMem->z!=0 );
58770       pMem->n = sqlite3Strlen30(pMem->z);
58771       pMem->type = SQLITE_TEXT;
58772       pMem->enc = SQLITE_UTF8;
58773       pMem++;
58774 
58775       /* When an OP_Program opcode is encounter (the only opcode that has
58776       ** a P4_SUBPROGRAM argument), expand the size of the array of subprograms
58777       ** kept in p->aMem[9].z to hold the new program - assuming this subprogram
58778       ** has not already been seen.
58779       */
58780       if( pOp->p4type==P4_SUBPROGRAM ){
58781         int nByte = (nSub+1)*sizeof(SubProgram*);
58782         int j;
58783         for(j=0; j<nSub; j++){
58784           if( apSub[j]==pOp->p4.pProgram ) break;
58785         }
58786         if( j==nSub && SQLITE_OK==sqlite3VdbeMemGrow(pSub, nByte, 1) ){
58787           apSub = (SubProgram **)pSub->z;
58788           apSub[nSub++] = pOp->p4.pProgram;
58789           pSub->flags |= MEM_Blob;
58790           pSub->n = nSub*sizeof(SubProgram*);
58791         }
58792       }
58793     }
58794 
58795     pMem->flags = MEM_Int;
58796     pMem->u.i = pOp->p1;                          /* P1 */
58797     pMem->type = SQLITE_INTEGER;
58798     pMem++;
58799 
58800     pMem->flags = MEM_Int;
58801     pMem->u.i = pOp->p2;                          /* P2 */
58802     pMem->type = SQLITE_INTEGER;
58803     pMem++;
58804 
58805     pMem->flags = MEM_Int;
58806     pMem->u.i = pOp->p3;                          /* P3 */
58807     pMem->type = SQLITE_INTEGER;
58808     pMem++;
58809 
58810     if( sqlite3VdbeMemGrow(pMem, 32, 0) ){            /* P4 */
58811       assert( p->db->mallocFailed );
58812       return SQLITE_ERROR;
58813     }
58814     pMem->flags = MEM_Dyn|MEM_Str|MEM_Term;
58815     z = displayP4(pOp, pMem->z, 32);
58816     if( z!=pMem->z ){
58817       sqlite3VdbeMemSetStr(pMem, z, -1, SQLITE_UTF8, 0);
58818     }else{
58819       assert( pMem->z!=0 );
58820       pMem->n = sqlite3Strlen30(pMem->z);
58821       pMem->enc = SQLITE_UTF8;
58822     }
58823     pMem->type = SQLITE_TEXT;
58824     pMem++;
58825 
58826     if( p->explain==1 ){
58827       if( sqlite3VdbeMemGrow(pMem, 4, 0) ){
58828         assert( p->db->mallocFailed );
58829         return SQLITE_ERROR;
58830       }
58831       pMem->flags = MEM_Dyn|MEM_Str|MEM_Term;
58832       pMem->n = 2;
58833       sqlite3_snprintf(3, pMem->z, "%.2x", pOp->p5);   /* P5 */
58834       pMem->type = SQLITE_TEXT;
58835       pMem->enc = SQLITE_UTF8;
58836       pMem++;
58837 
58838 #ifdef SQLITE_DEBUG
58839       if( pOp->zComment ){
58840         pMem->flags = MEM_Str|MEM_Term;
58841         pMem->z = pOp->zComment;
58842         pMem->n = sqlite3Strlen30(pMem->z);
58843         pMem->enc = SQLITE_UTF8;
58844         pMem->type = SQLITE_TEXT;
58845       }else
58846 #endif
58847       {
58848         pMem->flags = MEM_Null;                       /* Comment */
58849         pMem->type = SQLITE_NULL;
58850       }
58851     }
58852 
58853     p->nResColumn = 8 - 4*(p->explain-1);
58854     p->rc = SQLITE_OK;
58855     rc = SQLITE_ROW;
58856   }
58857   return rc;
58858 }
58859 #endif /* SQLITE_OMIT_EXPLAIN */
58860 
58861 #ifdef SQLITE_DEBUG
58862 /*
58863 ** Print the SQL that was used to generate a VDBE program.
58864 */
58865 SQLITE_PRIVATE void sqlite3VdbePrintSql(Vdbe *p){
58866   int nOp = p->nOp;
58867   VdbeOp *pOp;
58868   if( nOp<1 ) return;
58869   pOp = &p->aOp[0];
58870   if( pOp->opcode==OP_Trace && pOp->p4.z!=0 ){
58871     const char *z = pOp->p4.z;
58872     while( sqlite3Isspace(*z) ) z++;
58873     printf("SQL: [%s]\n", z);
58874   }
58875 }
58876 #endif
58877 
58878 #if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
58879 /*
58880 ** Print an IOTRACE message showing SQL content.
58881 */
58882 SQLITE_PRIVATE void sqlite3VdbeIOTraceSql(Vdbe *p){
58883   int nOp = p->nOp;
58884   VdbeOp *pOp;
58885   if( sqlite3IoTrace==0 ) return;
58886   if( nOp<1 ) return;
58887   pOp = &p->aOp[0];
58888   if( pOp->opcode==OP_Trace && pOp->p4.z!=0 ){
58889     int i, j;
58890     char z[1000];
58891     sqlite3_snprintf(sizeof(z), z, "%s", pOp->p4.z);
58892     for(i=0; sqlite3Isspace(z[i]); i++){}
58893     for(j=0; z[i]; i++){
58894       if( sqlite3Isspace(z[i]) ){
58895         if( z[i-1]!=' ' ){
58896           z[j++] = ' ';
58897         }
58898       }else{
58899         z[j++] = z[i];
58900       }
58901     }
58902     z[j] = 0;
58903     sqlite3IoTrace("SQL %s\n", z);
58904   }
58905 }
58906 #endif /* !SQLITE_OMIT_TRACE && SQLITE_ENABLE_IOTRACE */
58907 
58908 /*
58909 ** Allocate space from a fixed size buffer and return a pointer to
58910 ** that space.  If insufficient space is available, return NULL.
58911 **
58912 ** The pBuf parameter is the initial value of a pointer which will
58913 ** receive the new memory.  pBuf is normally NULL.  If pBuf is not
58914 ** NULL, it means that memory space has already been allocated and that
58915 ** this routine should not allocate any new memory.  When pBuf is not
58916 ** NULL simply return pBuf.  Only allocate new memory space when pBuf
58917 ** is NULL.
58918 **
58919 ** nByte is the number of bytes of space needed.
58920 **
58921 ** *ppFrom points to available space and pEnd points to the end of the
58922 ** available space.  When space is allocated, *ppFrom is advanced past
58923 ** the end of the allocated space.
58924 **
58925 ** *pnByte is a counter of the number of bytes of space that have failed
58926 ** to allocate.  If there is insufficient space in *ppFrom to satisfy the
58927 ** request, then increment *pnByte by the amount of the request.
58928 */
58929 static void *allocSpace(
58930   void *pBuf,          /* Where return pointer will be stored */
58931   int nByte,           /* Number of bytes to allocate */
58932   u8 **ppFrom,         /* IN/OUT: Allocate from *ppFrom */
58933   u8 *pEnd,            /* Pointer to 1 byte past the end of *ppFrom buffer */
58934   int *pnByte          /* If allocation cannot be made, increment *pnByte */
58935 ){
58936   assert( EIGHT_BYTE_ALIGNMENT(*ppFrom) );
58937   if( pBuf ) return pBuf;
58938   nByte = ROUND8(nByte);
58939   if( &(*ppFrom)[nByte] <= pEnd ){
58940     pBuf = (void*)*ppFrom;
58941     *ppFrom += nByte;
58942   }else{
58943     *pnByte += nByte;
58944   }
58945   return pBuf;
58946 }
58947 
58948 /*
58949 ** Rewind the VDBE back to the beginning in preparation for
58950 ** running it.
58951 */
58952 SQLITE_PRIVATE void sqlite3VdbeRewind(Vdbe *p){
58953 #if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
58954   int i;
58955 #endif
58956   assert( p!=0 );
58957   assert( p->magic==VDBE_MAGIC_INIT );
58958 
58959   /* There should be at least one opcode.
58960   */
58961   assert( p->nOp>0 );
58962 
58963   /* Set the magic to VDBE_MAGIC_RUN sooner rather than later. */
58964   p->magic = VDBE_MAGIC_RUN;
58965 
58966 #ifdef SQLITE_DEBUG
58967   for(i=1; i<p->nMem; i++){
58968     assert( p->aMem[i].db==p->db );
58969   }
58970 #endif
58971   p->pc = -1;
58972   p->rc = SQLITE_OK;
58973   p->errorAction = OE_Abort;
58974   p->magic = VDBE_MAGIC_RUN;
58975   p->nChange = 0;
58976   p->cacheCtr = 1;
58977   p->minWriteFileFormat = 255;
58978   p->iStatement = 0;
58979   p->nFkConstraint = 0;
58980 #ifdef VDBE_PROFILE
58981   for(i=0; i<p->nOp; i++){
58982     p->aOp[i].cnt = 0;
58983     p->aOp[i].cycles = 0;
58984   }
58985 #endif
58986 }
58987 
58988 /*
58989 ** Prepare a virtual machine for execution for the first time after
58990 ** creating the virtual machine.  This involves things such
58991 ** as allocating stack space and initializing the program counter.
58992 ** After the VDBE has be prepped, it can be executed by one or more
58993 ** calls to sqlite3VdbeExec().
58994 **
58995 ** This function may be called exact once on a each virtual machine.
58996 ** After this routine is called the VM has been "packaged" and is ready
58997 ** to run.  After this routine is called, futher calls to
58998 ** sqlite3VdbeAddOp() functions are prohibited.  This routine disconnects
58999 ** the Vdbe from the Parse object that helped generate it so that the
59000 ** the Vdbe becomes an independent entity and the Parse object can be
59001 ** destroyed.
59002 **
59003 ** Use the sqlite3VdbeRewind() procedure to restore a virtual machine back
59004 ** to its initial state after it has been run.
59005 */
59006 SQLITE_PRIVATE void sqlite3VdbeMakeReady(
59007   Vdbe *p,                       /* The VDBE */
59008   Parse *pParse                  /* Parsing context */
59009 ){
59010   sqlite3 *db;                   /* The database connection */
59011   int nVar;                      /* Number of parameters */
59012   int nMem;                      /* Number of VM memory registers */
59013   int nCursor;                   /* Number of cursors required */
59014   int nArg;                      /* Number of arguments in subprograms */
59015   int n;                         /* Loop counter */
59016   u8 *zCsr;                      /* Memory available for allocation */
59017   u8 *zEnd;                      /* First byte past allocated memory */
59018   int nByte;                     /* How much extra memory is needed */
59019 
59020   assert( p!=0 );
59021   assert( p->nOp>0 );
59022   assert( pParse!=0 );
59023   assert( p->magic==VDBE_MAGIC_INIT );
59024   db = p->db;
59025   assert( db->mallocFailed==0 );
59026   nVar = pParse->nVar;
59027   nMem = pParse->nMem;
59028   nCursor = pParse->nTab;
59029   nArg = pParse->nMaxArg;
59030 
59031   /* For each cursor required, also allocate a memory cell. Memory
59032   ** cells (nMem+1-nCursor)..nMem, inclusive, will never be used by
59033   ** the vdbe program. Instead they are used to allocate space for
59034   ** VdbeCursor/BtCursor structures. The blob of memory associated with
59035   ** cursor 0 is stored in memory cell nMem. Memory cell (nMem-1)
59036   ** stores the blob of memory associated with cursor 1, etc.
59037   **
59038   ** See also: allocateCursor().
59039   */
59040   nMem += nCursor;
59041 
59042   /* Allocate space for memory registers, SQL variables, VDBE cursors and
59043   ** an array to marshal SQL function arguments in.
59044   */
59045   zCsr = (u8*)&p->aOp[p->nOp];       /* Memory avaliable for allocation */
59046   zEnd = (u8*)&p->aOp[p->nOpAlloc];  /* First byte past end of zCsr[] */
59047 
59048   resolveP2Values(p, &nArg);
59049   p->usesStmtJournal = (u8)(pParse->isMultiWrite && pParse->mayAbort);
59050   if( pParse->explain && nMem<10 ){
59051     nMem = 10;
59052   }
59053   memset(zCsr, 0, zEnd-zCsr);
59054   zCsr += (zCsr - (u8*)0)&7;
59055   assert( EIGHT_BYTE_ALIGNMENT(zCsr) );
59056   p->expired = 0;
59057 
59058   /* Memory for registers, parameters, cursor, etc, is allocated in two
59059   ** passes.  On the first pass, we try to reuse unused space at the
59060   ** end of the opcode array.  If we are unable to satisfy all memory
59061   ** requirements by reusing the opcode array tail, then the second
59062   ** pass will fill in the rest using a fresh allocation.
59063   **
59064   ** This two-pass approach that reuses as much memory as possible from
59065   ** the leftover space at the end of the opcode array can significantly
59066   ** reduce the amount of memory held by a prepared statement.
59067   */
59068   do {
59069     nByte = 0;
59070     p->aMem = allocSpace(p->aMem, nMem*sizeof(Mem), &zCsr, zEnd, &nByte);
59071     p->aVar = allocSpace(p->aVar, nVar*sizeof(Mem), &zCsr, zEnd, &nByte);
59072     p->apArg = allocSpace(p->apArg, nArg*sizeof(Mem*), &zCsr, zEnd, &nByte);
59073     p->azVar = allocSpace(p->azVar, nVar*sizeof(char*), &zCsr, zEnd, &nByte);
59074     p->apCsr = allocSpace(p->apCsr, nCursor*sizeof(VdbeCursor*),
59075                           &zCsr, zEnd, &nByte);
59076     if( nByte ){
59077       p->pFree = sqlite3DbMallocZero(db, nByte);
59078     }
59079     zCsr = p->pFree;
59080     zEnd = &zCsr[nByte];
59081   }while( nByte && !db->mallocFailed );
59082 
59083   p->nCursor = (u16)nCursor;
59084   if( p->aVar ){
59085     p->nVar = (ynVar)nVar;
59086     for(n=0; n<nVar; n++){
59087       p->aVar[n].flags = MEM_Null;
59088       p->aVar[n].db = db;
59089     }
59090   }
59091   if( p->azVar ){
59092     p->nzVar = pParse->nzVar;
59093     memcpy(p->azVar, pParse->azVar, p->nzVar*sizeof(p->azVar[0]));
59094     memset(pParse->azVar, 0, pParse->nzVar*sizeof(pParse->azVar[0]));
59095   }
59096   if( p->aMem ){
59097     p->aMem--;                      /* aMem[] goes from 1..nMem */
59098     p->nMem = nMem;                 /*       not from 0..nMem-1 */
59099     for(n=1; n<=nMem; n++){
59100       p->aMem[n].flags = MEM_Null;
59101       p->aMem[n].db = db;
59102     }
59103   }
59104   p->explain = pParse->explain;
59105   sqlite3VdbeRewind(p);
59106 }
59107 
59108 /*
59109 ** Close a VDBE cursor and release all the resources that cursor
59110 ** happens to hold.
59111 */
59112 SQLITE_PRIVATE void sqlite3VdbeFreeCursor(Vdbe *p, VdbeCursor *pCx){
59113   if( pCx==0 ){
59114     return;
59115   }
59116   if( pCx->pBt ){
59117     sqlite3BtreeClose(pCx->pBt);
59118     /* The pCx->pCursor will be close automatically, if it exists, by
59119     ** the call above. */
59120   }else if( pCx->pCursor ){
59121     sqlite3BtreeCloseCursor(pCx->pCursor);
59122   }
59123 #ifndef SQLITE_OMIT_VIRTUALTABLE
59124   if( pCx->pVtabCursor ){
59125     sqlite3_vtab_cursor *pVtabCursor = pCx->pVtabCursor;
59126     const sqlite3_module *pModule = pCx->pModule;
59127     p->inVtabMethod = 1;
59128     pModule->xClose(pVtabCursor);
59129     p->inVtabMethod = 0;
59130   }
59131 #endif
59132 }
59133 
59134 /*
59135 ** Copy the values stored in the VdbeFrame structure to its Vdbe. This
59136 ** is used, for example, when a trigger sub-program is halted to restore
59137 ** control to the main program.
59138 */
59139 SQLITE_PRIVATE int sqlite3VdbeFrameRestore(VdbeFrame *pFrame){
59140   Vdbe *v = pFrame->v;
59141   v->aOp = pFrame->aOp;
59142   v->nOp = pFrame->nOp;
59143   v->aMem = pFrame->aMem;
59144   v->nMem = pFrame->nMem;
59145   v->apCsr = pFrame->apCsr;
59146   v->nCursor = pFrame->nCursor;
59147   v->db->lastRowid = pFrame->lastRowid;
59148   v->nChange = pFrame->nChange;
59149   return pFrame->pc;
59150 }
59151 
59152 /*
59153 ** Close all cursors.
59154 **
59155 ** Also release any dynamic memory held by the VM in the Vdbe.aMem memory
59156 ** cell array. This is necessary as the memory cell array may contain
59157 ** pointers to VdbeFrame objects, which may in turn contain pointers to
59158 ** open cursors.
59159 */
59160 static void closeAllCursors(Vdbe *p){
59161   if( p->pFrame ){
59162     VdbeFrame *pFrame;
59163     for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
59164     sqlite3VdbeFrameRestore(pFrame);
59165   }
59166   p->pFrame = 0;
59167   p->nFrame = 0;
59168 
59169   if( p->apCsr ){
59170     int i;
59171     for(i=0; i<p->nCursor; i++){
59172       VdbeCursor *pC = p->apCsr[i];
59173       if( pC ){
59174         sqlite3VdbeFreeCursor(p, pC);
59175         p->apCsr[i] = 0;
59176       }
59177     }
59178   }
59179   if( p->aMem ){
59180     releaseMemArray(&p->aMem[1], p->nMem);
59181   }
59182   while( p->pDelFrame ){
59183     VdbeFrame *pDel = p->pDelFrame;
59184     p->pDelFrame = pDel->pParent;
59185     sqlite3VdbeFrameDelete(pDel);
59186   }
59187 }
59188 
59189 /*
59190 ** Clean up the VM after execution.
59191 **
59192 ** This routine will automatically close any cursors, lists, and/or
59193 ** sorters that were left open.  It also deletes the values of
59194 ** variables in the aVar[] array.
59195 */
59196 static void Cleanup(Vdbe *p){
59197   sqlite3 *db = p->db;
59198 
59199 #ifdef SQLITE_DEBUG
59200   /* Execute assert() statements to ensure that the Vdbe.apCsr[] and
59201   ** Vdbe.aMem[] arrays have already been cleaned up.  */
59202   int i;
59203   for(i=0; i<p->nCursor; i++) assert( p->apCsr==0 || p->apCsr[i]==0 );
59204   for(i=1; i<=p->nMem; i++) assert( p->aMem==0 || p->aMem[i].flags==MEM_Null );
59205 #endif
59206 
59207   sqlite3DbFree(db, p->zErrMsg);
59208   p->zErrMsg = 0;
59209   p->pResultSet = 0;
59210 }
59211 
59212 /*
59213 ** Set the number of result columns that will be returned by this SQL
59214 ** statement. This is now set at compile time, rather than during
59215 ** execution of the vdbe program so that sqlite3_column_count() can
59216 ** be called on an SQL statement before sqlite3_step().
59217 */
59218 SQLITE_PRIVATE void sqlite3VdbeSetNumCols(Vdbe *p, int nResColumn){
59219   Mem *pColName;
59220   int n;
59221   sqlite3 *db = p->db;
59222 
59223   releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
59224   sqlite3DbFree(db, p->aColName);
59225   n = nResColumn*COLNAME_N;
59226   p->nResColumn = (u16)nResColumn;
59227   p->aColName = pColName = (Mem*)sqlite3DbMallocZero(db, sizeof(Mem)*n );
59228   if( p->aColName==0 ) return;
59229   while( n-- > 0 ){
59230     pColName->flags = MEM_Null;
59231     pColName->db = p->db;
59232     pColName++;
59233   }
59234 }
59235 
59236 /*
59237 ** Set the name of the idx'th column to be returned by the SQL statement.
59238 ** zName must be a pointer to a nul terminated string.
59239 **
59240 ** This call must be made after a call to sqlite3VdbeSetNumCols().
59241 **
59242 ** The final parameter, xDel, must be one of SQLITE_DYNAMIC, SQLITE_STATIC
59243 ** or SQLITE_TRANSIENT. If it is SQLITE_DYNAMIC, then the buffer pointed
59244 ** to by zName will be freed by sqlite3DbFree() when the vdbe is destroyed.
59245 */
59246 SQLITE_PRIVATE int sqlite3VdbeSetColName(
59247   Vdbe *p,                         /* Vdbe being configured */
59248   int idx,                         /* Index of column zName applies to */
59249   int var,                         /* One of the COLNAME_* constants */
59250   const char *zName,               /* Pointer to buffer containing name */
59251   void (*xDel)(void*)              /* Memory management strategy for zName */
59252 ){
59253   int rc;
59254   Mem *pColName;
59255   assert( idx<p->nResColumn );
59256   assert( var<COLNAME_N );
59257   if( p->db->mallocFailed ){
59258     assert( !zName || xDel!=SQLITE_DYNAMIC );
59259     return SQLITE_NOMEM;
59260   }
59261   assert( p->aColName!=0 );
59262   pColName = &(p->aColName[idx+var*p->nResColumn]);
59263   rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF8, xDel);
59264   assert( rc!=0 || !zName || (pColName->flags&MEM_Term)!=0 );
59265   return rc;
59266 }
59267 
59268 /*
59269 ** A read or write transaction may or may not be active on database handle
59270 ** db. If a transaction is active, commit it. If there is a
59271 ** write-transaction spanning more than one database file, this routine
59272 ** takes care of the master journal trickery.
59273 */
59274 static int vdbeCommit(sqlite3 *db, Vdbe *p){
59275   int i;
59276   int nTrans = 0;  /* Number of databases with an active write-transaction */
59277   int rc = SQLITE_OK;
59278   int needXcommit = 0;
59279 
59280 #ifdef SQLITE_OMIT_VIRTUALTABLE
59281   /* With this option, sqlite3VtabSync() is defined to be simply
59282   ** SQLITE_OK so p is not used.
59283   */
59284   UNUSED_PARAMETER(p);
59285 #endif
59286 
59287   /* Before doing anything else, call the xSync() callback for any
59288   ** virtual module tables written in this transaction. This has to
59289   ** be done before determining whether a master journal file is
59290   ** required, as an xSync() callback may add an attached database
59291   ** to the transaction.
59292   */
59293   rc = sqlite3VtabSync(db, &p->zErrMsg);
59294 
59295   /* This loop determines (a) if the commit hook should be invoked and
59296   ** (b) how many database files have open write transactions, not
59297   ** including the temp database. (b) is important because if more than
59298   ** one database file has an open write transaction, a master journal
59299   ** file is required for an atomic commit.
59300   */
59301   for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
59302     Btree *pBt = db->aDb[i].pBt;
59303     if( sqlite3BtreeIsInTrans(pBt) ){
59304       needXcommit = 1;
59305       if( i!=1 ) nTrans++;
59306       rc = sqlite3PagerExclusiveLock(sqlite3BtreePager(pBt));
59307     }
59308   }
59309   if( rc!=SQLITE_OK ){
59310     return rc;
59311   }
59312 
59313   /* If there are any write-transactions at all, invoke the commit hook */
59314   if( needXcommit && db->xCommitCallback ){
59315     rc = db->xCommitCallback(db->pCommitArg);
59316     if( rc ){
59317       return SQLITE_CONSTRAINT;
59318     }
59319   }
59320 
59321   /* The simple case - no more than one database file (not counting the
59322   ** TEMP database) has a transaction active.   There is no need for the
59323   ** master-journal.
59324   **
59325   ** If the return value of sqlite3BtreeGetFilename() is a zero length
59326   ** string, it means the main database is :memory: or a temp file.  In
59327   ** that case we do not support atomic multi-file commits, so use the
59328   ** simple case then too.
59329   */
59330   if( 0==sqlite3Strlen30(sqlite3BtreeGetFilename(db->aDb[0].pBt))
59331    || nTrans<=1
59332   ){
59333     for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
59334       Btree *pBt = db->aDb[i].pBt;
59335       if( pBt ){
59336         rc = sqlite3BtreeCommitPhaseOne(pBt, 0);
59337       }
59338     }
59339 
59340     /* Do the commit only if all databases successfully complete phase 1.
59341     ** If one of the BtreeCommitPhaseOne() calls fails, this indicates an
59342     ** IO error while deleting or truncating a journal file. It is unlikely,
59343     ** but could happen. In this case abandon processing and return the error.
59344     */
59345     for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
59346       Btree *pBt = db->aDb[i].pBt;
59347       if( pBt ){
59348         rc = sqlite3BtreeCommitPhaseTwo(pBt, 0);
59349       }
59350     }
59351     if( rc==SQLITE_OK ){
59352       sqlite3VtabCommit(db);
59353     }
59354   }
59355 
59356   /* The complex case - There is a multi-file write-transaction active.
59357   ** This requires a master journal file to ensure the transaction is
59358   ** committed atomicly.
59359   */
59360 #ifndef SQLITE_OMIT_DISKIO
59361   else{
59362     sqlite3_vfs *pVfs = db->pVfs;
59363     int needSync = 0;
59364     char *zMaster = 0;   /* File-name for the master journal */
59365     char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt);
59366     sqlite3_file *pMaster = 0;
59367     i64 offset = 0;
59368     int res;
59369 
59370     /* Select a master journal file name */
59371     do {
59372       u32 iRandom;
59373       sqlite3DbFree(db, zMaster);
59374       sqlite3_randomness(sizeof(iRandom), &iRandom);
59375       zMaster = sqlite3MPrintf(db, "%s-mj%08X", zMainFile, iRandom&0x7fffffff);
59376       if( !zMaster ){
59377         return SQLITE_NOMEM;
59378       }
59379       sqlite3FileSuffix3(zMainFile, zMaster);
59380       rc = sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS, &res);
59381     }while( rc==SQLITE_OK && res );
59382     if( rc==SQLITE_OK ){
59383       /* Open the master journal. */
59384       rc = sqlite3OsOpenMalloc(pVfs, zMaster, &pMaster,
59385           SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|
59386           SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_MASTER_JOURNAL, 0
59387       );
59388     }
59389     if( rc!=SQLITE_OK ){
59390       sqlite3DbFree(db, zMaster);
59391       return rc;
59392     }
59393 
59394     /* Write the name of each database file in the transaction into the new
59395     ** master journal file. If an error occurs at this point close
59396     ** and delete the master journal file. All the individual journal files
59397     ** still have 'null' as the master journal pointer, so they will roll
59398     ** back independently if a failure occurs.
59399     */
59400     for(i=0; i<db->nDb; i++){
59401       Btree *pBt = db->aDb[i].pBt;
59402       if( sqlite3BtreeIsInTrans(pBt) ){
59403         char const *zFile = sqlite3BtreeGetJournalname(pBt);
59404         if( zFile==0 ){
59405           continue;  /* Ignore TEMP and :memory: databases */
59406         }
59407         assert( zFile[0]!=0 );
59408         if( !needSync && !sqlite3BtreeSyncDisabled(pBt) ){
59409           needSync = 1;
59410         }
59411         rc = sqlite3OsWrite(pMaster, zFile, sqlite3Strlen30(zFile)+1, offset);
59412         offset += sqlite3Strlen30(zFile)+1;
59413         if( rc!=SQLITE_OK ){
59414           sqlite3OsCloseFree(pMaster);
59415           sqlite3OsDelete(pVfs, zMaster, 0);
59416           sqlite3DbFree(db, zMaster);
59417           return rc;
59418         }
59419       }
59420     }
59421 
59422     /* Sync the master journal file. If the IOCAP_SEQUENTIAL device
59423     ** flag is set this is not required.
59424     */
59425     if( needSync
59426      && 0==(sqlite3OsDeviceCharacteristics(pMaster)&SQLITE_IOCAP_SEQUENTIAL)
59427      && SQLITE_OK!=(rc = sqlite3OsSync(pMaster, SQLITE_SYNC_NORMAL))
59428     ){
59429       sqlite3OsCloseFree(pMaster);
59430       sqlite3OsDelete(pVfs, zMaster, 0);
59431       sqlite3DbFree(db, zMaster);
59432       return rc;
59433     }
59434 
59435     /* Sync all the db files involved in the transaction. The same call
59436     ** sets the master journal pointer in each individual journal. If
59437     ** an error occurs here, do not delete the master journal file.
59438     **
59439     ** If the error occurs during the first call to
59440     ** sqlite3BtreeCommitPhaseOne(), then there is a chance that the
59441     ** master journal file will be orphaned. But we cannot delete it,
59442     ** in case the master journal file name was written into the journal
59443     ** file before the failure occurred.
59444     */
59445     for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
59446       Btree *pBt = db->aDb[i].pBt;
59447       if( pBt ){
59448         rc = sqlite3BtreeCommitPhaseOne(pBt, zMaster);
59449       }
59450     }
59451     sqlite3OsCloseFree(pMaster);
59452     assert( rc!=SQLITE_BUSY );
59453     if( rc!=SQLITE_OK ){
59454       sqlite3DbFree(db, zMaster);
59455       return rc;
59456     }
59457 
59458     /* Delete the master journal file. This commits the transaction. After
59459     ** doing this the directory is synced again before any individual
59460     ** transaction files are deleted.
59461     */
59462     rc = sqlite3OsDelete(pVfs, zMaster, 1);
59463     sqlite3DbFree(db, zMaster);
59464     zMaster = 0;
59465     if( rc ){
59466       return rc;
59467     }
59468 
59469     /* All files and directories have already been synced, so the following
59470     ** calls to sqlite3BtreeCommitPhaseTwo() are only closing files and
59471     ** deleting or truncating journals. If something goes wrong while
59472     ** this is happening we don't really care. The integrity of the
59473     ** transaction is already guaranteed, but some stray 'cold' journals
59474     ** may be lying around. Returning an error code won't help matters.
59475     */
59476     disable_simulated_io_errors();
59477     sqlite3BeginBenignMalloc();
59478     for(i=0; i<db->nDb; i++){
59479       Btree *pBt = db->aDb[i].pBt;
59480       if( pBt ){
59481         sqlite3BtreeCommitPhaseTwo(pBt, 1);
59482       }
59483     }
59484     sqlite3EndBenignMalloc();
59485     enable_simulated_io_errors();
59486 
59487     sqlite3VtabCommit(db);
59488   }
59489 #endif
59490 
59491   return rc;
59492 }
59493 
59494 /*
59495 ** This routine checks that the sqlite3.activeVdbeCnt count variable
59496 ** matches the number of vdbe's in the list sqlite3.pVdbe that are
59497 ** currently active. An assertion fails if the two counts do not match.
59498 ** This is an internal self-check only - it is not an essential processing
59499 ** step.
59500 **
59501 ** This is a no-op if NDEBUG is defined.
59502 */
59503 #ifndef NDEBUG
59504 static void checkActiveVdbeCnt(sqlite3 *db){
59505   Vdbe *p;
59506   int cnt = 0;
59507   int nWrite = 0;
59508   p = db->pVdbe;
59509   while( p ){
59510     if( p->magic==VDBE_MAGIC_RUN && p->pc>=0 ){
59511       cnt++;
59512       if( p->readOnly==0 ) nWrite++;
59513     }
59514     p = p->pNext;
59515   }
59516   assert( cnt==db->activeVdbeCnt );
59517   assert( nWrite==db->writeVdbeCnt );
59518 }
59519 #else
59520 #define checkActiveVdbeCnt(x)
59521 #endif
59522 
59523 /*
59524 ** For every Btree that in database connection db which
59525 ** has been modified, "trip" or invalidate each cursor in
59526 ** that Btree might have been modified so that the cursor
59527 ** can never be used again.  This happens when a rollback
59528 *** occurs.  We have to trip all the other cursors, even
59529 ** cursor from other VMs in different database connections,
59530 ** so that none of them try to use the data at which they
59531 ** were pointing and which now may have been changed due
59532 ** to the rollback.
59533 **
59534 ** Remember that a rollback can delete tables complete and
59535 ** reorder rootpages.  So it is not sufficient just to save
59536 ** the state of the cursor.  We have to invalidate the cursor
59537 ** so that it is never used again.
59538 */
59539 static void invalidateCursorsOnModifiedBtrees(sqlite3 *db){
59540   int i;
59541   for(i=0; i<db->nDb; i++){
59542     Btree *p = db->aDb[i].pBt;
59543     if( p && sqlite3BtreeIsInTrans(p) ){
59544       sqlite3BtreeTripAllCursors(p, SQLITE_ABORT);
59545     }
59546   }
59547 }
59548 
59549 /*
59550 ** If the Vdbe passed as the first argument opened a statement-transaction,
59551 ** close it now. Argument eOp must be either SAVEPOINT_ROLLBACK or
59552 ** SAVEPOINT_RELEASE. If it is SAVEPOINT_ROLLBACK, then the statement
59553 ** transaction is rolled back. If eOp is SAVEPOINT_RELEASE, then the
59554 ** statement transaction is commtted.
59555 **
59556 ** If an IO error occurs, an SQLITE_IOERR_XXX error code is returned.
59557 ** Otherwise SQLITE_OK.
59558 */
59559 SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *p, int eOp){
59560   sqlite3 *const db = p->db;
59561   int rc = SQLITE_OK;
59562 
59563   /* If p->iStatement is greater than zero, then this Vdbe opened a
59564   ** statement transaction that should be closed here. The only exception
59565   ** is that an IO error may have occured, causing an emergency rollback.
59566   ** In this case (db->nStatement==0), and there is nothing to do.
59567   */
59568   if( db->nStatement && p->iStatement ){
59569     int i;
59570     const int iSavepoint = p->iStatement-1;
59571 
59572     assert( eOp==SAVEPOINT_ROLLBACK || eOp==SAVEPOINT_RELEASE);
59573     assert( db->nStatement>0 );
59574     assert( p->iStatement==(db->nStatement+db->nSavepoint) );
59575 
59576     for(i=0; i<db->nDb; i++){
59577       int rc2 = SQLITE_OK;
59578       Btree *pBt = db->aDb[i].pBt;
59579       if( pBt ){
59580         if( eOp==SAVEPOINT_ROLLBACK ){
59581           rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_ROLLBACK, iSavepoint);
59582         }
59583         if( rc2==SQLITE_OK ){
59584           rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_RELEASE, iSavepoint);
59585         }
59586         if( rc==SQLITE_OK ){
59587           rc = rc2;
59588         }
59589       }
59590     }
59591     db->nStatement--;
59592     p->iStatement = 0;
59593 
59594     if( rc==SQLITE_OK ){
59595       if( eOp==SAVEPOINT_ROLLBACK ){
59596         rc = sqlite3VtabSavepoint(db, SAVEPOINT_ROLLBACK, iSavepoint);
59597       }
59598       if( rc==SQLITE_OK ){
59599         rc = sqlite3VtabSavepoint(db, SAVEPOINT_RELEASE, iSavepoint);
59600       }
59601     }
59602 
59603     /* If the statement transaction is being rolled back, also restore the
59604     ** database handles deferred constraint counter to the value it had when
59605     ** the statement transaction was opened.  */
59606     if( eOp==SAVEPOINT_ROLLBACK ){
59607       db->nDeferredCons = p->nStmtDefCons;
59608     }
59609   }
59610   return rc;
59611 }
59612 
59613 /*
59614 ** This function is called when a transaction opened by the database
59615 ** handle associated with the VM passed as an argument is about to be
59616 ** committed. If there are outstanding deferred foreign key constraint
59617 ** violations, return SQLITE_ERROR. Otherwise, SQLITE_OK.
59618 **
59619 ** If there are outstanding FK violations and this function returns
59620 ** SQLITE_ERROR, set the result of the VM to SQLITE_CONSTRAINT and write
59621 ** an error message to it. Then return SQLITE_ERROR.
59622 */
59623 #ifndef SQLITE_OMIT_FOREIGN_KEY
59624 SQLITE_PRIVATE int sqlite3VdbeCheckFk(Vdbe *p, int deferred){
59625   sqlite3 *db = p->db;
59626   if( (deferred && db->nDeferredCons>0) || (!deferred && p->nFkConstraint>0) ){
59627     p->rc = SQLITE_CONSTRAINT;
59628     p->errorAction = OE_Abort;
59629     sqlite3SetString(&p->zErrMsg, db, "foreign key constraint failed");
59630     return SQLITE_ERROR;
59631   }
59632   return SQLITE_OK;
59633 }
59634 #endif
59635 
59636 /*
59637 ** This routine is called the when a VDBE tries to halt.  If the VDBE
59638 ** has made changes and is in autocommit mode, then commit those
59639 ** changes.  If a rollback is needed, then do the rollback.
59640 **
59641 ** This routine is the only way to move the state of a VM from
59642 ** SQLITE_MAGIC_RUN to SQLITE_MAGIC_HALT.  It is harmless to
59643 ** call this on a VM that is in the SQLITE_MAGIC_HALT state.
59644 **
59645 ** Return an error code.  If the commit could not complete because of
59646 ** lock contention, return SQLITE_BUSY.  If SQLITE_BUSY is returned, it
59647 ** means the close did not happen and needs to be repeated.
59648 */
59649 SQLITE_PRIVATE int sqlite3VdbeHalt(Vdbe *p){
59650   int rc;                         /* Used to store transient return codes */
59651   sqlite3 *db = p->db;
59652 
59653   /* This function contains the logic that determines if a statement or
59654   ** transaction will be committed or rolled back as a result of the
59655   ** execution of this virtual machine.
59656   **
59657   ** If any of the following errors occur:
59658   **
59659   **     SQLITE_NOMEM
59660   **     SQLITE_IOERR
59661   **     SQLITE_FULL
59662   **     SQLITE_INTERRUPT
59663   **
59664   ** Then the internal cache might have been left in an inconsistent
59665   ** state.  We need to rollback the statement transaction, if there is
59666   ** one, or the complete transaction if there is no statement transaction.
59667   */
59668 
59669   if( p->db->mallocFailed ){
59670     p->rc = SQLITE_NOMEM;
59671   }
59672   closeAllCursors(p);
59673   if( p->magic!=VDBE_MAGIC_RUN ){
59674     return SQLITE_OK;
59675   }
59676   checkActiveVdbeCnt(db);
59677 
59678   /* No commit or rollback needed if the program never started */
59679   if( p->pc>=0 ){
59680     int mrc;   /* Primary error code from p->rc */
59681     int eStatementOp = 0;
59682     int isSpecialError;            /* Set to true if a 'special' error */
59683 
59684     /* Lock all btrees used by the statement */
59685     sqlite3VdbeEnter(p);
59686 
59687     /* Check for one of the special errors */
59688     mrc = p->rc & 0xff;
59689     assert( p->rc!=SQLITE_IOERR_BLOCKED );  /* This error no longer exists */
59690     isSpecialError = mrc==SQLITE_NOMEM || mrc==SQLITE_IOERR
59691                      || mrc==SQLITE_INTERRUPT || mrc==SQLITE_FULL;
59692     if( isSpecialError ){
59693       /* If the query was read-only and the error code is SQLITE_INTERRUPT,
59694       ** no rollback is necessary. Otherwise, at least a savepoint
59695       ** transaction must be rolled back to restore the database to a
59696       ** consistent state.
59697       **
59698       ** Even if the statement is read-only, it is important to perform
59699       ** a statement or transaction rollback operation. If the error
59700       ** occured while writing to the journal, sub-journal or database
59701       ** file as part of an effort to free up cache space (see function
59702       ** pagerStress() in pager.c), the rollback is required to restore
59703       ** the pager to a consistent state.
59704       */
59705       if( !p->readOnly || mrc!=SQLITE_INTERRUPT ){
59706         if( (mrc==SQLITE_NOMEM || mrc==SQLITE_FULL) && p->usesStmtJournal ){
59707           eStatementOp = SAVEPOINT_ROLLBACK;
59708         }else{
59709           /* We are forced to roll back the active transaction. Before doing
59710           ** so, abort any other statements this handle currently has active.
59711           */
59712           invalidateCursorsOnModifiedBtrees(db);
59713           sqlite3RollbackAll(db);
59714           sqlite3CloseSavepoints(db);
59715           db->autoCommit = 1;
59716         }
59717       }
59718     }
59719 
59720     /* Check for immediate foreign key violations. */
59721     if( p->rc==SQLITE_OK ){
59722       sqlite3VdbeCheckFk(p, 0);
59723     }
59724 
59725     /* If the auto-commit flag is set and this is the only active writer
59726     ** VM, then we do either a commit or rollback of the current transaction.
59727     **
59728     ** Note: This block also runs if one of the special errors handled
59729     ** above has occurred.
59730     */
59731     if( !sqlite3VtabInSync(db)
59732      && db->autoCommit
59733      && db->writeVdbeCnt==(p->readOnly==0)
59734     ){
59735       if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
59736         rc = sqlite3VdbeCheckFk(p, 1);
59737         if( rc!=SQLITE_OK ){
59738           if( NEVER(p->readOnly) ){
59739             sqlite3VdbeLeave(p);
59740             return SQLITE_ERROR;
59741           }
59742           rc = SQLITE_CONSTRAINT;
59743         }else{
59744           /* The auto-commit flag is true, the vdbe program was successful
59745           ** or hit an 'OR FAIL' constraint and there are no deferred foreign
59746           ** key constraints to hold up the transaction. This means a commit
59747           ** is required. */
59748           rc = vdbeCommit(db, p);
59749         }
59750         if( rc==SQLITE_BUSY && p->readOnly ){
59751           sqlite3VdbeLeave(p);
59752           return SQLITE_BUSY;
59753         }else if( rc!=SQLITE_OK ){
59754           p->rc = rc;
59755           sqlite3RollbackAll(db);
59756         }else{
59757           db->nDeferredCons = 0;
59758           sqlite3CommitInternalChanges(db);
59759         }
59760       }else{
59761         sqlite3RollbackAll(db);
59762       }
59763       db->nStatement = 0;
59764     }else if( eStatementOp==0 ){
59765       if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){
59766         eStatementOp = SAVEPOINT_RELEASE;
59767       }else if( p->errorAction==OE_Abort ){
59768         eStatementOp = SAVEPOINT_ROLLBACK;
59769       }else{
59770         invalidateCursorsOnModifiedBtrees(db);
59771         sqlite3RollbackAll(db);
59772         sqlite3CloseSavepoints(db);
59773         db->autoCommit = 1;
59774       }
59775     }
59776 
59777     /* If eStatementOp is non-zero, then a statement transaction needs to
59778     ** be committed or rolled back. Call sqlite3VdbeCloseStatement() to
59779     ** do so. If this operation returns an error, and the current statement
59780     ** error code is SQLITE_OK or SQLITE_CONSTRAINT, then promote the
59781     ** current statement error code.
59782     */
59783     if( eStatementOp ){
59784       rc = sqlite3VdbeCloseStatement(p, eStatementOp);
59785       if( rc ){
59786         if( p->rc==SQLITE_OK || p->rc==SQLITE_CONSTRAINT ){
59787           p->rc = rc;
59788           sqlite3DbFree(db, p->zErrMsg);
59789           p->zErrMsg = 0;
59790         }
59791         invalidateCursorsOnModifiedBtrees(db);
59792         sqlite3RollbackAll(db);
59793         sqlite3CloseSavepoints(db);
59794         db->autoCommit = 1;
59795       }
59796     }
59797 
59798     /* If this was an INSERT, UPDATE or DELETE and no statement transaction
59799     ** has been rolled back, update the database connection change-counter.
59800     */
59801     if( p->changeCntOn ){
59802       if( eStatementOp!=SAVEPOINT_ROLLBACK ){
59803         sqlite3VdbeSetChanges(db, p->nChange);
59804       }else{
59805         sqlite3VdbeSetChanges(db, 0);
59806       }
59807       p->nChange = 0;
59808     }
59809 
59810     /* Rollback or commit any schema changes that occurred. */
59811     if( p->rc!=SQLITE_OK && db->flags&SQLITE_InternChanges ){
59812       sqlite3ResetInternalSchema(db, -1);
59813       db->flags = (db->flags | SQLITE_InternChanges);
59814     }
59815 
59816     /* Release the locks */
59817     sqlite3VdbeLeave(p);
59818   }
59819 
59820   /* We have successfully halted and closed the VM.  Record this fact. */
59821   if( p->pc>=0 ){
59822     db->activeVdbeCnt--;
59823     if( !p->readOnly ){
59824       db->writeVdbeCnt--;
59825     }
59826     assert( db->activeVdbeCnt>=db->writeVdbeCnt );
59827   }
59828   p->magic = VDBE_MAGIC_HALT;
59829   checkActiveVdbeCnt(db);
59830   if( p->db->mallocFailed ){
59831     p->rc = SQLITE_NOMEM;
59832   }
59833 
59834   /* If the auto-commit flag is set to true, then any locks that were held
59835   ** by connection db have now been released. Call sqlite3ConnectionUnlocked()
59836   ** to invoke any required unlock-notify callbacks.
59837   */
59838   if( db->autoCommit ){
59839     sqlite3ConnectionUnlocked(db);
59840   }
59841 
59842   assert( db->activeVdbeCnt>0 || db->autoCommit==0 || db->nStatement==0 );
59843   return (p->rc==SQLITE_BUSY ? SQLITE_BUSY : SQLITE_OK);
59844 }
59845 
59846 
59847 /*
59848 ** Each VDBE holds the result of the most recent sqlite3_step() call
59849 ** in p->rc.  This routine sets that result back to SQLITE_OK.
59850 */
59851 SQLITE_PRIVATE void sqlite3VdbeResetStepResult(Vdbe *p){
59852   p->rc = SQLITE_OK;
59853 }
59854 
59855 /*
59856 ** Clean up a VDBE after execution but do not delete the VDBE just yet.
59857 ** Write any error messages into *pzErrMsg.  Return the result code.
59858 **
59859 ** After this routine is run, the VDBE should be ready to be executed
59860 ** again.
59861 **
59862 ** To look at it another way, this routine resets the state of the
59863 ** virtual machine from VDBE_MAGIC_RUN or VDBE_MAGIC_HALT back to
59864 ** VDBE_MAGIC_INIT.
59865 */
59866 SQLITE_PRIVATE int sqlite3VdbeReset(Vdbe *p){
59867   sqlite3 *db;
59868   db = p->db;
59869 
59870   /* If the VM did not run to completion or if it encountered an
59871   ** error, then it might not have been halted properly.  So halt
59872   ** it now.
59873   */
59874   sqlite3VdbeHalt(p);
59875 
59876   /* If the VDBE has be run even partially, then transfer the error code
59877   ** and error message from the VDBE into the main database structure.  But
59878   ** if the VDBE has just been set to run but has not actually executed any
59879   ** instructions yet, leave the main database error information unchanged.
59880   */
59881   if( p->pc>=0 ){
59882     if( p->zErrMsg ){
59883       sqlite3BeginBenignMalloc();
59884       sqlite3ValueSetStr(db->pErr,-1,p->zErrMsg,SQLITE_UTF8,SQLITE_TRANSIENT);
59885       sqlite3EndBenignMalloc();
59886       db->errCode = p->rc;
59887       sqlite3DbFree(db, p->zErrMsg);
59888       p->zErrMsg = 0;
59889     }else if( p->rc ){
59890       sqlite3Error(db, p->rc, 0);
59891     }else{
59892       sqlite3Error(db, SQLITE_OK, 0);
59893     }
59894     if( p->runOnlyOnce ) p->expired = 1;
59895   }else if( p->rc && p->expired ){
59896     /* The expired flag was set on the VDBE before the first call
59897     ** to sqlite3_step(). For consistency (since sqlite3_step() was
59898     ** called), set the database error in this case as well.
59899     */
59900     sqlite3Error(db, p->rc, 0);
59901     sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, SQLITE_TRANSIENT);
59902     sqlite3DbFree(db, p->zErrMsg);
59903     p->zErrMsg = 0;
59904   }
59905 
59906   /* Reclaim all memory used by the VDBE
59907   */
59908   Cleanup(p);
59909 
59910   /* Save profiling information from this VDBE run.
59911   */
59912 #ifdef VDBE_PROFILE
59913   {
59914     FILE *out = fopen("vdbe_profile.out", "a");
59915     if( out ){
59916       int i;
59917       fprintf(out, "---- ");
59918       for(i=0; i<p->nOp; i++){
59919         fprintf(out, "%02x", p->aOp[i].opcode);
59920       }
59921       fprintf(out, "\n");
59922       for(i=0; i<p->nOp; i++){
59923         fprintf(out, "%6d %10lld %8lld ",
59924            p->aOp[i].cnt,
59925            p->aOp[i].cycles,
59926            p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0
59927         );
59928         sqlite3VdbePrintOp(out, i, &p->aOp[i]);
59929       }
59930       fclose(out);
59931     }
59932   }
59933 #endif
59934   p->magic = VDBE_MAGIC_INIT;
59935   return p->rc & db->errMask;
59936 }
59937 
59938 /*
59939 ** Clean up and delete a VDBE after execution.  Return an integer which is
59940 ** the result code.  Write any error message text into *pzErrMsg.
59941 */
59942 SQLITE_PRIVATE int sqlite3VdbeFinalize(Vdbe *p){
59943   int rc = SQLITE_OK;
59944   if( p->magic==VDBE_MAGIC_RUN || p->magic==VDBE_MAGIC_HALT ){
59945     rc = sqlite3VdbeReset(p);
59946     assert( (rc & p->db->errMask)==rc );
59947   }
59948   sqlite3VdbeDelete(p);
59949   return rc;
59950 }
59951 
59952 /*
59953 ** Call the destructor for each auxdata entry in pVdbeFunc for which
59954 ** the corresponding bit in mask is clear.  Auxdata entries beyond 31
59955 ** are always destroyed.  To destroy all auxdata entries, call this
59956 ** routine with mask==0.
59957 */
59958 SQLITE_PRIVATE void sqlite3VdbeDeleteAuxData(VdbeFunc *pVdbeFunc, int mask){
59959   int i;
59960   for(i=0; i<pVdbeFunc->nAux; i++){
59961     struct AuxData *pAux = &pVdbeFunc->apAux[i];
59962     if( (i>31 || !(mask&(((u32)1)<<i))) && pAux->pAux ){
59963       if( pAux->xDelete ){
59964         pAux->xDelete(pAux->pAux);
59965       }
59966       pAux->pAux = 0;
59967     }
59968   }
59969 }
59970 
59971 /*
59972 ** Free all memory associated with the Vdbe passed as the second argument.
59973 ** The difference between this function and sqlite3VdbeDelete() is that
59974 ** VdbeDelete() also unlinks the Vdbe from the list of VMs associated with
59975 ** the database connection.
59976 */
59977 SQLITE_PRIVATE void sqlite3VdbeDeleteObject(sqlite3 *db, Vdbe *p){
59978   SubProgram *pSub, *pNext;
59979   int i;
59980   assert( p->db==0 || p->db==db );
59981   releaseMemArray(p->aVar, p->nVar);
59982   releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
59983   for(pSub=p->pProgram; pSub; pSub=pNext){
59984     pNext = pSub->pNext;
59985     vdbeFreeOpArray(db, pSub->aOp, pSub->nOp);
59986     sqlite3DbFree(db, pSub);
59987   }
59988   for(i=p->nzVar-1; i>=0; i--) sqlite3DbFree(db, p->azVar[i]);
59989   vdbeFreeOpArray(db, p->aOp, p->nOp);
59990   sqlite3DbFree(db, p->aLabel);
59991   sqlite3DbFree(db, p->aColName);
59992   sqlite3DbFree(db, p->zSql);
59993   sqlite3DbFree(db, p->pFree);
59994   sqlite3DbFree(db, p);
59995 }
59996 
59997 /*
59998 ** Delete an entire VDBE.
59999 */
60000 SQLITE_PRIVATE void sqlite3VdbeDelete(Vdbe *p){
60001   sqlite3 *db;
60002 
60003   if( NEVER(p==0) ) return;
60004   db = p->db;
60005   if( p->pPrev ){
60006     p->pPrev->pNext = p->pNext;
60007   }else{
60008     assert( db->pVdbe==p );
60009     db->pVdbe = p->pNext;
60010   }
60011   if( p->pNext ){
60012     p->pNext->pPrev = p->pPrev;
60013   }
60014   p->magic = VDBE_MAGIC_DEAD;
60015   p->db = 0;
60016   sqlite3VdbeDeleteObject(db, p);
60017 }
60018 
60019 /*
60020 ** Make sure the cursor p is ready to read or write the row to which it
60021 ** was last positioned.  Return an error code if an OOM fault or I/O error
60022 ** prevents us from positioning the cursor to its correct position.
60023 **
60024 ** If a MoveTo operation is pending on the given cursor, then do that
60025 ** MoveTo now.  If no move is pending, check to see if the row has been
60026 ** deleted out from under the cursor and if it has, mark the row as
60027 ** a NULL row.
60028 **
60029 ** If the cursor is already pointing to the correct row and that row has
60030 ** not been deleted out from under the cursor, then this routine is a no-op.
60031 */
60032 SQLITE_PRIVATE int sqlite3VdbeCursorMoveto(VdbeCursor *p){
60033   if( p->deferredMoveto ){
60034     int res, rc;
60035 #ifdef SQLITE_TEST
60036     extern int sqlite3_search_count;
60037 #endif
60038     assert( p->isTable );
60039     rc = sqlite3BtreeMovetoUnpacked(p->pCursor, 0, p->movetoTarget, 0, &res);
60040     if( rc ) return rc;
60041     p->lastRowid = p->movetoTarget;
60042     if( res!=0 ) return SQLITE_CORRUPT_BKPT;
60043     p->rowidIsValid = 1;
60044 #ifdef SQLITE_TEST
60045     sqlite3_search_count++;
60046 #endif
60047     p->deferredMoveto = 0;
60048     p->cacheStatus = CACHE_STALE;
60049   }else if( ALWAYS(p->pCursor) ){
60050     int hasMoved;
60051     int rc = sqlite3BtreeCursorHasMoved(p->pCursor, &hasMoved);
60052     if( rc ) return rc;
60053     if( hasMoved ){
60054       p->cacheStatus = CACHE_STALE;
60055       p->nullRow = 1;
60056     }
60057   }
60058   return SQLITE_OK;
60059 }
60060 
60061 /*
60062 ** The following functions:
60063 **
60064 ** sqlite3VdbeSerialType()
60065 ** sqlite3VdbeSerialTypeLen()
60066 ** sqlite3VdbeSerialLen()
60067 ** sqlite3VdbeSerialPut()
60068 ** sqlite3VdbeSerialGet()
60069 **
60070 ** encapsulate the code that serializes values for storage in SQLite
60071 ** data and index records. Each serialized value consists of a
60072 ** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned
60073 ** integer, stored as a varint.
60074 **
60075 ** In an SQLite index record, the serial type is stored directly before
60076 ** the blob of data that it corresponds to. In a table record, all serial
60077 ** types are stored at the start of the record, and the blobs of data at
60078 ** the end. Hence these functions allow the caller to handle the
60079 ** serial-type and data blob seperately.
60080 **
60081 ** The following table describes the various storage classes for data:
60082 **
60083 **   serial type        bytes of data      type
60084 **   --------------     ---------------    ---------------
60085 **      0                     0            NULL
60086 **      1                     1            signed integer
60087 **      2                     2            signed integer
60088 **      3                     3            signed integer
60089 **      4                     4            signed integer
60090 **      5                     6            signed integer
60091 **      6                     8            signed integer
60092 **      7                     8            IEEE float
60093 **      8                     0            Integer constant 0
60094 **      9                     0            Integer constant 1
60095 **     10,11                               reserved for expansion
60096 **    N>=12 and even       (N-12)/2        BLOB
60097 **    N>=13 and odd        (N-13)/2        text
60098 **
60099 ** The 8 and 9 types were added in 3.3.0, file format 4.  Prior versions
60100 ** of SQLite will not understand those serial types.
60101 */
60102 
60103 /*
60104 ** Return the serial-type for the value stored in pMem.
60105 */
60106 SQLITE_PRIVATE u32 sqlite3VdbeSerialType(Mem *pMem, int file_format){
60107   int flags = pMem->flags;
60108   int n;
60109 
60110   if( flags&MEM_Null ){
60111     return 0;
60112   }
60113   if( flags&MEM_Int ){
60114     /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */
60115 #   define MAX_6BYTE ((((i64)0x00008000)<<32)-1)
60116     i64 i = pMem->u.i;
60117     u64 u;
60118     if( file_format>=4 && (i&1)==i ){
60119       return 8+(u32)i;
60120     }
60121     if( i<0 ){
60122       if( i<(-MAX_6BYTE) ) return 6;
60123       /* Previous test prevents:  u = -(-9223372036854775808) */
60124       u = -i;
60125     }else{
60126       u = i;
60127     }
60128     if( u<=127 ) return 1;
60129     if( u<=32767 ) return 2;
60130     if( u<=8388607 ) return 3;
60131     if( u<=2147483647 ) return 4;
60132     if( u<=MAX_6BYTE ) return 5;
60133     return 6;
60134   }
60135   if( flags&MEM_Real ){
60136     return 7;
60137   }
60138   assert( pMem->db->mallocFailed || flags&(MEM_Str|MEM_Blob) );
60139   n = pMem->n;
60140   if( flags & MEM_Zero ){
60141     n += pMem->u.nZero;
60142   }
60143   assert( n>=0 );
60144   return ((n*2) + 12 + ((flags&MEM_Str)!=0));
60145 }
60146 
60147 /*
60148 ** Return the length of the data corresponding to the supplied serial-type.
60149 */
60150 SQLITE_PRIVATE u32 sqlite3VdbeSerialTypeLen(u32 serial_type){
60151   if( serial_type>=12 ){
60152     return (serial_type-12)/2;
60153   }else{
60154     static const u8 aSize[] = { 0, 1, 2, 3, 4, 6, 8, 8, 0, 0, 0, 0 };
60155     return aSize[serial_type];
60156   }
60157 }
60158 
60159 /*
60160 ** If we are on an architecture with mixed-endian floating
60161 ** points (ex: ARM7) then swap the lower 4 bytes with the
60162 ** upper 4 bytes.  Return the result.
60163 **
60164 ** For most architectures, this is a no-op.
60165 **
60166 ** (later):  It is reported to me that the mixed-endian problem
60167 ** on ARM7 is an issue with GCC, not with the ARM7 chip.  It seems
60168 ** that early versions of GCC stored the two words of a 64-bit
60169 ** float in the wrong order.  And that error has been propagated
60170 ** ever since.  The blame is not necessarily with GCC, though.
60171 ** GCC might have just copying the problem from a prior compiler.
60172 ** I am also told that newer versions of GCC that follow a different
60173 ** ABI get the byte order right.
60174 **
60175 ** Developers using SQLite on an ARM7 should compile and run their
60176 ** application using -DSQLITE_DEBUG=1 at least once.  With DEBUG
60177 ** enabled, some asserts below will ensure that the byte order of
60178 ** floating point values is correct.
60179 **
60180 ** (2007-08-30)  Frank van Vugt has studied this problem closely
60181 ** and has send his findings to the SQLite developers.  Frank
60182 ** writes that some Linux kernels offer floating point hardware
60183 ** emulation that uses only 32-bit mantissas instead of a full
60184 ** 48-bits as required by the IEEE standard.  (This is the
60185 ** CONFIG_FPE_FASTFPE option.)  On such systems, floating point
60186 ** byte swapping becomes very complicated.  To avoid problems,
60187 ** the necessary byte swapping is carried out using a 64-bit integer
60188 ** rather than a 64-bit float.  Frank assures us that the code here
60189 ** works for him.  We, the developers, have no way to independently
60190 ** verify this, but Frank seems to know what he is talking about
60191 ** so we trust him.
60192 */
60193 #ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
60194 static u64 floatSwap(u64 in){
60195   union {
60196     u64 r;
60197     u32 i[2];
60198   } u;
60199   u32 t;
60200 
60201   u.r = in;
60202   t = u.i[0];
60203   u.i[0] = u.i[1];
60204   u.i[1] = t;
60205   return u.r;
60206 }
60207 # define swapMixedEndianFloat(X)  X = floatSwap(X)
60208 #else
60209 # define swapMixedEndianFloat(X)
60210 #endif
60211 
60212 /*
60213 ** Write the serialized data blob for the value stored in pMem into
60214 ** buf. It is assumed that the caller has allocated sufficient space.
60215 ** Return the number of bytes written.
60216 **
60217 ** nBuf is the amount of space left in buf[].  nBuf must always be
60218 ** large enough to hold the entire field.  Except, if the field is
60219 ** a blob with a zero-filled tail, then buf[] might be just the right
60220 ** size to hold everything except for the zero-filled tail.  If buf[]
60221 ** is only big enough to hold the non-zero prefix, then only write that
60222 ** prefix into buf[].  But if buf[] is large enough to hold both the
60223 ** prefix and the tail then write the prefix and set the tail to all
60224 ** zeros.
60225 **
60226 ** Return the number of bytes actually written into buf[].  The number
60227 ** of bytes in the zero-filled tail is included in the return value only
60228 ** if those bytes were zeroed in buf[].
60229 */
60230 SQLITE_PRIVATE u32 sqlite3VdbeSerialPut(u8 *buf, int nBuf, Mem *pMem, int file_format){
60231   u32 serial_type = sqlite3VdbeSerialType(pMem, file_format);
60232   u32 len;
60233 
60234   /* Integer and Real */
60235   if( serial_type<=7 && serial_type>0 ){
60236     u64 v;
60237     u32 i;
60238     if( serial_type==7 ){
60239       assert( sizeof(v)==sizeof(pMem->r) );
60240       memcpy(&v, &pMem->r, sizeof(v));
60241       swapMixedEndianFloat(v);
60242     }else{
60243       v = pMem->u.i;
60244     }
60245     len = i = sqlite3VdbeSerialTypeLen(serial_type);
60246     assert( len<=(u32)nBuf );
60247     while( i-- ){
60248       buf[i] = (u8)(v&0xFF);
60249       v >>= 8;
60250     }
60251     return len;
60252   }
60253 
60254   /* String or blob */
60255   if( serial_type>=12 ){
60256     assert( pMem->n + ((pMem->flags & MEM_Zero)?pMem->u.nZero:0)
60257              == (int)sqlite3VdbeSerialTypeLen(serial_type) );
60258     assert( pMem->n<=nBuf );
60259     len = pMem->n;
60260     memcpy(buf, pMem->z, len);
60261     if( pMem->flags & MEM_Zero ){
60262       len += pMem->u.nZero;
60263       assert( nBuf>=0 );
60264       if( len > (u32)nBuf ){
60265         len = (u32)nBuf;
60266       }
60267       memset(&buf[pMem->n], 0, len-pMem->n);
60268     }
60269     return len;
60270   }
60271 
60272   /* NULL or constants 0 or 1 */
60273   return 0;
60274 }
60275 
60276 /*
60277 ** Deserialize the data blob pointed to by buf as serial type serial_type
60278 ** and store the result in pMem.  Return the number of bytes read.
60279 */
60280 SQLITE_PRIVATE u32 sqlite3VdbeSerialGet(
60281   const unsigned char *buf,     /* Buffer to deserialize from */
60282   u32 serial_type,              /* Serial type to deserialize */
60283   Mem *pMem                     /* Memory cell to write value into */
60284 ){
60285   switch( serial_type ){
60286     case 10:   /* Reserved for future use */
60287     case 11:   /* Reserved for future use */
60288     case 0: {  /* NULL */
60289       pMem->flags = MEM_Null;
60290       break;
60291     }
60292     case 1: { /* 1-byte signed integer */
60293       pMem->u.i = (signed char)buf[0];
60294       pMem->flags = MEM_Int;
60295       return 1;
60296     }
60297     case 2: { /* 2-byte signed integer */
60298       pMem->u.i = (((signed char)buf[0])<<8) | buf[1];
60299       pMem->flags = MEM_Int;
60300       return 2;
60301     }
60302     case 3: { /* 3-byte signed integer */
60303       pMem->u.i = (((signed char)buf[0])<<16) | (buf[1]<<8) | buf[2];
60304       pMem->flags = MEM_Int;
60305       return 3;
60306     }
60307     case 4: { /* 4-byte signed integer */
60308       pMem->u.i = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3];
60309       pMem->flags = MEM_Int;
60310       return 4;
60311     }
60312     case 5: { /* 6-byte signed integer */
60313       u64 x = (((signed char)buf[0])<<8) | buf[1];
60314       u32 y = (buf[2]<<24) | (buf[3]<<16) | (buf[4]<<8) | buf[5];
60315       x = (x<<32) | y;
60316       pMem->u.i = *(i64*)&x;
60317       pMem->flags = MEM_Int;
60318       return 6;
60319     }
60320     case 6:   /* 8-byte signed integer */
60321     case 7: { /* IEEE floating point */
60322       u64 x;
60323       u32 y;
60324 #if !defined(NDEBUG) && !defined(SQLITE_OMIT_FLOATING_POINT)
60325       /* Verify that integers and floating point values use the same
60326       ** byte order.  Or, that if SQLITE_MIXED_ENDIAN_64BIT_FLOAT is
60327       ** defined that 64-bit floating point values really are mixed
60328       ** endian.
60329       */
60330       static const u64 t1 = ((u64)0x3ff00000)<<32;
60331       static const double r1 = 1.0;
60332       u64 t2 = t1;
60333       swapMixedEndianFloat(t2);
60334       assert( sizeof(r1)==sizeof(t2) && memcmp(&r1, &t2, sizeof(r1))==0 );
60335 #endif
60336 
60337       x = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3];
60338       y = (buf[4]<<24) | (buf[5]<<16) | (buf[6]<<8) | buf[7];
60339       x = (x<<32) | y;
60340       if( serial_type==6 ){
60341         pMem->u.i = *(i64*)&x;
60342         pMem->flags = MEM_Int;
60343       }else{
60344         assert( sizeof(x)==8 && sizeof(pMem->r)==8 );
60345         swapMixedEndianFloat(x);
60346         memcpy(&pMem->r, &x, sizeof(x));
60347         pMem->flags = sqlite3IsNaN(pMem->r) ? MEM_Null : MEM_Real;
60348       }
60349       return 8;
60350     }
60351     case 8:    /* Integer 0 */
60352     case 9: {  /* Integer 1 */
60353       pMem->u.i = serial_type-8;
60354       pMem->flags = MEM_Int;
60355       return 0;
60356     }
60357     default: {
60358       u32 len = (serial_type-12)/2;
60359       pMem->z = (char *)buf;
60360       pMem->n = len;
60361       pMem->xDel = 0;
60362       if( serial_type&0x01 ){
60363         pMem->flags = MEM_Str | MEM_Ephem;
60364       }else{
60365         pMem->flags = MEM_Blob | MEM_Ephem;
60366       }
60367       return len;
60368     }
60369   }
60370   return 0;
60371 }
60372 
60373 
60374 /*
60375 ** Given the nKey-byte encoding of a record in pKey[], parse the
60376 ** record into a UnpackedRecord structure.  Return a pointer to
60377 ** that structure.
60378 **
60379 ** The calling function might provide szSpace bytes of memory
60380 ** space at pSpace.  This space can be used to hold the returned
60381 ** VDbeParsedRecord structure if it is large enough.  If it is
60382 ** not big enough, space is obtained from sqlite3_malloc().
60383 **
60384 ** The returned structure should be closed by a call to
60385 ** sqlite3VdbeDeleteUnpackedRecord().
60386 */
60387 SQLITE_PRIVATE UnpackedRecord *sqlite3VdbeRecordUnpack(
60388   KeyInfo *pKeyInfo,     /* Information about the record format */
60389   int nKey,              /* Size of the binary record */
60390   const void *pKey,      /* The binary record */
60391   char *pSpace,          /* Unaligned space available to hold the object */
60392   int szSpace            /* Size of pSpace[] in bytes */
60393 ){
60394   const unsigned char *aKey = (const unsigned char *)pKey;
60395   UnpackedRecord *p;  /* The unpacked record that we will return */
60396   int nByte;          /* Memory space needed to hold p, in bytes */
60397   int d;
60398   u32 idx;
60399   u16 u;              /* Unsigned loop counter */
60400   u32 szHdr;
60401   Mem *pMem;
60402   int nOff;           /* Increase pSpace by this much to 8-byte align it */
60403 
60404   /*
60405   ** We want to shift the pointer pSpace up such that it is 8-byte aligned.
60406   ** Thus, we need to calculate a value, nOff, between 0 and 7, to shift
60407   ** it by.  If pSpace is already 8-byte aligned, nOff should be zero.
60408   */
60409   nOff = (8 - (SQLITE_PTR_TO_INT(pSpace) & 7)) & 7;
60410   pSpace += nOff;
60411   szSpace -= nOff;
60412   nByte = ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*(pKeyInfo->nField+1);
60413   if( nByte>szSpace ){
60414     p = sqlite3DbMallocRaw(pKeyInfo->db, nByte);
60415     if( p==0 ) return 0;
60416     p->flags = UNPACKED_NEED_FREE | UNPACKED_NEED_DESTROY;
60417   }else{
60418     p = (UnpackedRecord*)pSpace;
60419     p->flags = UNPACKED_NEED_DESTROY;
60420   }
60421   p->pKeyInfo = pKeyInfo;
60422   p->nField = pKeyInfo->nField + 1;
60423   p->aMem = pMem = (Mem*)&((char*)p)[ROUND8(sizeof(UnpackedRecord))];
60424   assert( EIGHT_BYTE_ALIGNMENT(pMem) );
60425   idx = getVarint32(aKey, szHdr);
60426   d = szHdr;
60427   u = 0;
60428   while( idx<szHdr && u<p->nField && d<=nKey ){
60429     u32 serial_type;
60430 
60431     idx += getVarint32(&aKey[idx], serial_type);
60432     pMem->enc = pKeyInfo->enc;
60433     pMem->db = pKeyInfo->db;
60434     /* pMem->flags = 0; // sqlite3VdbeSerialGet() will set this for us */
60435     pMem->zMalloc = 0;
60436     d += sqlite3VdbeSerialGet(&aKey[d], serial_type, pMem);
60437     pMem++;
60438     u++;
60439   }
60440   assert( u<=pKeyInfo->nField + 1 );
60441   p->nField = u;
60442   return (void*)p;
60443 }
60444 
60445 /*
60446 ** This routine destroys a UnpackedRecord object.
60447 */
60448 SQLITE_PRIVATE void sqlite3VdbeDeleteUnpackedRecord(UnpackedRecord *p){
60449 #ifdef SQLITE_DEBUG
60450   int i;
60451   Mem *pMem;
60452 
60453   assert( p!=0 );
60454   assert( p->flags & UNPACKED_NEED_DESTROY );
60455   for(i=0, pMem=p->aMem; i<p->nField; i++, pMem++){
60456     /* The unpacked record is always constructed by the
60457     ** sqlite3VdbeUnpackRecord() function above, which makes all
60458     ** strings and blobs static.  And none of the elements are
60459     ** ever transformed, so there is never anything to delete.
60460     */
60461     if( NEVER(pMem->zMalloc) ) sqlite3VdbeMemRelease(pMem);
60462   }
60463 #endif
60464   if( p->flags & UNPACKED_NEED_FREE ){
60465     sqlite3DbFree(p->pKeyInfo->db, p);
60466   }
60467 }
60468 
60469 /*
60470 ** This function compares the two table rows or index records
60471 ** specified by {nKey1, pKey1} and pPKey2.  It returns a negative, zero
60472 ** or positive integer if key1 is less than, equal to or
60473 ** greater than key2.  The {nKey1, pKey1} key must be a blob
60474 ** created by th OP_MakeRecord opcode of the VDBE.  The pPKey2
60475 ** key must be a parsed key such as obtained from
60476 ** sqlite3VdbeParseRecord.
60477 **
60478 ** Key1 and Key2 do not have to contain the same number of fields.
60479 ** The key with fewer fields is usually compares less than the
60480 ** longer key.  However if the UNPACKED_INCRKEY flags in pPKey2 is set
60481 ** and the common prefixes are equal, then key1 is less than key2.
60482 ** Or if the UNPACKED_MATCH_PREFIX flag is set and the prefixes are
60483 ** equal, then the keys are considered to be equal and
60484 ** the parts beyond the common prefix are ignored.
60485 **
60486 ** If the UNPACKED_IGNORE_ROWID flag is set, then the last byte of
60487 ** the header of pKey1 is ignored.  It is assumed that pKey1 is
60488 ** an index key, and thus ends with a rowid value.  The last byte
60489 ** of the header will therefore be the serial type of the rowid:
60490 ** one of 1, 2, 3, 4, 5, 6, 8, or 9 - the integer serial types.
60491 ** The serial type of the final rowid will always be a single byte.
60492 ** By ignoring this last byte of the header, we force the comparison
60493 ** to ignore the rowid at the end of key1.
60494 */
60495 SQLITE_PRIVATE int sqlite3VdbeRecordCompare(
60496   int nKey1, const void *pKey1, /* Left key */
60497   UnpackedRecord *pPKey2        /* Right key */
60498 ){
60499   int d1;            /* Offset into aKey[] of next data element */
60500   u32 idx1;          /* Offset into aKey[] of next header element */
60501   u32 szHdr1;        /* Number of bytes in header */
60502   int i = 0;
60503   int nField;
60504   int rc = 0;
60505   const unsigned char *aKey1 = (const unsigned char *)pKey1;
60506   KeyInfo *pKeyInfo;
60507   Mem mem1;
60508 
60509   pKeyInfo = pPKey2->pKeyInfo;
60510   mem1.enc = pKeyInfo->enc;
60511   mem1.db = pKeyInfo->db;
60512   /* mem1.flags = 0;  // Will be initialized by sqlite3VdbeSerialGet() */
60513   VVA_ONLY( mem1.zMalloc = 0; ) /* Only needed by assert() statements */
60514 
60515   /* Compilers may complain that mem1.u.i is potentially uninitialized.
60516   ** We could initialize it, as shown here, to silence those complaints.
60517   ** But in fact, mem1.u.i will never actually be used uninitialized, and doing
60518   ** the unnecessary initialization has a measurable negative performance
60519   ** impact, since this routine is a very high runner.  And so, we choose
60520   ** to ignore the compiler warnings and leave this variable uninitialized.
60521   */
60522   /*  mem1.u.i = 0;  // not needed, here to silence compiler warning */
60523 
60524   idx1 = getVarint32(aKey1, szHdr1);
60525   d1 = szHdr1;
60526   if( pPKey2->flags & UNPACKED_IGNORE_ROWID ){
60527     szHdr1--;
60528   }
60529   nField = pKeyInfo->nField;
60530   while( idx1<szHdr1 && i<pPKey2->nField ){
60531     u32 serial_type1;
60532 
60533     /* Read the serial types for the next element in each key. */
60534     idx1 += getVarint32( aKey1+idx1, serial_type1 );
60535     if( d1>=nKey1 && sqlite3VdbeSerialTypeLen(serial_type1)>0 ) break;
60536 
60537     /* Extract the values to be compared.
60538     */
60539     d1 += sqlite3VdbeSerialGet(&aKey1[d1], serial_type1, &mem1);
60540 
60541     /* Do the comparison
60542     */
60543     rc = sqlite3MemCompare(&mem1, &pPKey2->aMem[i],
60544                            i<nField ? pKeyInfo->aColl[i] : 0);
60545     if( rc!=0 ){
60546       assert( mem1.zMalloc==0 );  /* See comment below */
60547 
60548       /* Invert the result if we are using DESC sort order. */
60549       if( pKeyInfo->aSortOrder && i<nField && pKeyInfo->aSortOrder[i] ){
60550         rc = -rc;
60551       }
60552 
60553       /* If the PREFIX_SEARCH flag is set and all fields except the final
60554       ** rowid field were equal, then clear the PREFIX_SEARCH flag and set
60555       ** pPKey2->rowid to the value of the rowid field in (pKey1, nKey1).
60556       ** This is used by the OP_IsUnique opcode.
60557       */
60558       if( (pPKey2->flags & UNPACKED_PREFIX_SEARCH) && i==(pPKey2->nField-1) ){
60559         assert( idx1==szHdr1 && rc );
60560         assert( mem1.flags & MEM_Int );
60561         pPKey2->flags &= ~UNPACKED_PREFIX_SEARCH;
60562         pPKey2->rowid = mem1.u.i;
60563       }
60564 
60565       return rc;
60566     }
60567     i++;
60568   }
60569 
60570   /* No memory allocation is ever used on mem1.  Prove this using
60571   ** the following assert().  If the assert() fails, it indicates a
60572   ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1).
60573   */
60574   assert( mem1.zMalloc==0 );
60575 
60576   /* rc==0 here means that one of the keys ran out of fields and
60577   ** all the fields up to that point were equal. If the UNPACKED_INCRKEY
60578   ** flag is set, then break the tie by treating key2 as larger.
60579   ** If the UPACKED_PREFIX_MATCH flag is set, then keys with common prefixes
60580   ** are considered to be equal.  Otherwise, the longer key is the
60581   ** larger.  As it happens, the pPKey2 will always be the longer
60582   ** if there is a difference.
60583   */
60584   assert( rc==0 );
60585   if( pPKey2->flags & UNPACKED_INCRKEY ){
60586     rc = -1;
60587   }else if( pPKey2->flags & UNPACKED_PREFIX_MATCH ){
60588     /* Leave rc==0 */
60589   }else if( idx1<szHdr1 ){
60590     rc = 1;
60591   }
60592   return rc;
60593 }
60594 
60595 
60596 /*
60597 ** pCur points at an index entry created using the OP_MakeRecord opcode.
60598 ** Read the rowid (the last field in the record) and store it in *rowid.
60599 ** Return SQLITE_OK if everything works, or an error code otherwise.
60600 **
60601 ** pCur might be pointing to text obtained from a corrupt database file.
60602 ** So the content cannot be trusted.  Do appropriate checks on the content.
60603 */
60604 SQLITE_PRIVATE int sqlite3VdbeIdxRowid(sqlite3 *db, BtCursor *pCur, i64 *rowid){
60605   i64 nCellKey = 0;
60606   int rc;
60607   u32 szHdr;        /* Size of the header */
60608   u32 typeRowid;    /* Serial type of the rowid */
60609   u32 lenRowid;     /* Size of the rowid */
60610   Mem m, v;
60611 
60612   UNUSED_PARAMETER(db);
60613 
60614   /* Get the size of the index entry.  Only indices entries of less
60615   ** than 2GiB are support - anything large must be database corruption.
60616   ** Any corruption is detected in sqlite3BtreeParseCellPtr(), though, so
60617   ** this code can safely assume that nCellKey is 32-bits
60618   */
60619   assert( sqlite3BtreeCursorIsValid(pCur) );
60620   rc = sqlite3BtreeKeySize(pCur, &nCellKey);
60621   assert( rc==SQLITE_OK );     /* pCur is always valid so KeySize cannot fail */
60622   assert( (nCellKey & SQLITE_MAX_U32)==(u64)nCellKey );
60623 
60624   /* Read in the complete content of the index entry */
60625   memset(&m, 0, sizeof(m));
60626   rc = sqlite3VdbeMemFromBtree(pCur, 0, (int)nCellKey, 1, &m);
60627   if( rc ){
60628     return rc;
60629   }
60630 
60631   /* The index entry must begin with a header size */
60632   (void)getVarint32((u8*)m.z, szHdr);
60633   testcase( szHdr==3 );
60634   testcase( szHdr==m.n );
60635   if( unlikely(szHdr<3 || (int)szHdr>m.n) ){
60636     goto idx_rowid_corruption;
60637   }
60638 
60639   /* The last field of the index should be an integer - the ROWID.
60640   ** Verify that the last entry really is an integer. */
60641   (void)getVarint32((u8*)&m.z[szHdr-1], typeRowid);
60642   testcase( typeRowid==1 );
60643   testcase( typeRowid==2 );
60644   testcase( typeRowid==3 );
60645   testcase( typeRowid==4 );
60646   testcase( typeRowid==5 );
60647   testcase( typeRowid==6 );
60648   testcase( typeRowid==8 );
60649   testcase( typeRowid==9 );
60650   if( unlikely(typeRowid<1 || typeRowid>9 || typeRowid==7) ){
60651     goto idx_rowid_corruption;
60652   }
60653   lenRowid = sqlite3VdbeSerialTypeLen(typeRowid);
60654   testcase( (u32)m.n==szHdr+lenRowid );
60655   if( unlikely((u32)m.n<szHdr+lenRowid) ){
60656     goto idx_rowid_corruption;
60657   }
60658 
60659   /* Fetch the integer off the end of the index record */
60660   sqlite3VdbeSerialGet((u8*)&m.z[m.n-lenRowid], typeRowid, &v);
60661   *rowid = v.u.i;
60662   sqlite3VdbeMemRelease(&m);
60663   return SQLITE_OK;
60664 
60665   /* Jump here if database corruption is detected after m has been
60666   ** allocated.  Free the m object and return SQLITE_CORRUPT. */
60667 idx_rowid_corruption:
60668   testcase( m.zMalloc!=0 );
60669   sqlite3VdbeMemRelease(&m);
60670   return SQLITE_CORRUPT_BKPT;
60671 }
60672 
60673 /*
60674 ** Compare the key of the index entry that cursor pC is pointing to against
60675 ** the key string in pUnpacked.  Write into *pRes a number
60676 ** that is negative, zero, or positive if pC is less than, equal to,
60677 ** or greater than pUnpacked.  Return SQLITE_OK on success.
60678 **
60679 ** pUnpacked is either created without a rowid or is truncated so that it
60680 ** omits the rowid at the end.  The rowid at the end of the index entry
60681 ** is ignored as well.  Hence, this routine only compares the prefixes
60682 ** of the keys prior to the final rowid, not the entire key.
60683 */
60684 SQLITE_PRIVATE int sqlite3VdbeIdxKeyCompare(
60685   VdbeCursor *pC,             /* The cursor to compare against */
60686   UnpackedRecord *pUnpacked,  /* Unpacked version of key to compare against */
60687   int *res                    /* Write the comparison result here */
60688 ){
60689   i64 nCellKey = 0;
60690   int rc;
60691   BtCursor *pCur = pC->pCursor;
60692   Mem m;
60693 
60694   assert( sqlite3BtreeCursorIsValid(pCur) );
60695   rc = sqlite3BtreeKeySize(pCur, &nCellKey);
60696   assert( rc==SQLITE_OK );    /* pCur is always valid so KeySize cannot fail */
60697   /* nCellKey will always be between 0 and 0xffffffff because of the say
60698   ** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */
60699   if( nCellKey<=0 || nCellKey>0x7fffffff ){
60700     *res = 0;
60701     return SQLITE_CORRUPT_BKPT;
60702   }
60703   memset(&m, 0, sizeof(m));
60704   rc = sqlite3VdbeMemFromBtree(pC->pCursor, 0, (int)nCellKey, 1, &m);
60705   if( rc ){
60706     return rc;
60707   }
60708   assert( pUnpacked->flags & UNPACKED_IGNORE_ROWID );
60709   *res = sqlite3VdbeRecordCompare(m.n, m.z, pUnpacked);
60710   sqlite3VdbeMemRelease(&m);
60711   return SQLITE_OK;
60712 }
60713 
60714 /*
60715 ** This routine sets the value to be returned by subsequent calls to
60716 ** sqlite3_changes() on the database handle 'db'.
60717 */
60718 SQLITE_PRIVATE void sqlite3VdbeSetChanges(sqlite3 *db, int nChange){
60719   assert( sqlite3_mutex_held(db->mutex) );
60720   db->nChange = nChange;
60721   db->nTotalChange += nChange;
60722 }
60723 
60724 /*
60725 ** Set a flag in the vdbe to update the change counter when it is finalised
60726 ** or reset.
60727 */
60728 SQLITE_PRIVATE void sqlite3VdbeCountChanges(Vdbe *v){
60729   v->changeCntOn = 1;
60730 }
60731 
60732 /*
60733 ** Mark every prepared statement associated with a database connection
60734 ** as expired.
60735 **
60736 ** An expired statement means that recompilation of the statement is
60737 ** recommend.  Statements expire when things happen that make their
60738 ** programs obsolete.  Removing user-defined functions or collating
60739 ** sequences, or changing an authorization function are the types of
60740 ** things that make prepared statements obsolete.
60741 */
60742 SQLITE_PRIVATE void sqlite3ExpirePreparedStatements(sqlite3 *db){
60743   Vdbe *p;
60744   for(p = db->pVdbe; p; p=p->pNext){
60745     p->expired = 1;
60746   }
60747 }
60748 
60749 /*
60750 ** Return the database associated with the Vdbe.
60751 */
60752 SQLITE_PRIVATE sqlite3 *sqlite3VdbeDb(Vdbe *v){
60753   return v->db;
60754 }
60755 
60756 /*
60757 ** Return a pointer to an sqlite3_value structure containing the value bound
60758 ** parameter iVar of VM v. Except, if the value is an SQL NULL, return
60759 ** 0 instead. Unless it is NULL, apply affinity aff (one of the SQLITE_AFF_*
60760 ** constants) to the value before returning it.
60761 **
60762 ** The returned value must be freed by the caller using sqlite3ValueFree().
60763 */
60764 SQLITE_PRIVATE sqlite3_value *sqlite3VdbeGetValue(Vdbe *v, int iVar, u8 aff){
60765   assert( iVar>0 );
60766   if( v ){
60767     Mem *pMem = &v->aVar[iVar-1];
60768     if( 0==(pMem->flags & MEM_Null) ){
60769       sqlite3_value *pRet = sqlite3ValueNew(v->db);
60770       if( pRet ){
60771         sqlite3VdbeMemCopy((Mem *)pRet, pMem);
60772         sqlite3ValueApplyAffinity(pRet, aff, SQLITE_UTF8);
60773         sqlite3VdbeMemStoreType((Mem *)pRet);
60774       }
60775       return pRet;
60776     }
60777   }
60778   return 0;
60779 }
60780 
60781 /*
60782 ** Configure SQL variable iVar so that binding a new value to it signals
60783 ** to sqlite3_reoptimize() that re-preparing the statement may result
60784 ** in a better query plan.
60785 */
60786 SQLITE_PRIVATE void sqlite3VdbeSetVarmask(Vdbe *v, int iVar){
60787   assert( iVar>0 );
60788   if( iVar>32 ){
60789     v->expmask = 0xffffffff;
60790   }else{
60791     v->expmask |= ((u32)1 << (iVar-1));
60792   }
60793 }
60794 
60795 /************** End of vdbeaux.c *********************************************/
60796 /************** Begin file vdbeapi.c *****************************************/
60797 /*
60798 ** 2004 May 26
60799 **
60800 ** The author disclaims copyright to this source code.  In place of
60801 ** a legal notice, here is a blessing:
60802 **
60803 **    May you do good and not evil.
60804 **    May you find forgiveness for yourself and forgive others.
60805 **    May you share freely, never taking more than you give.
60806 **
60807 *************************************************************************
60808 **
60809 ** This file contains code use to implement APIs that are part of the
60810 ** VDBE.
60811 */
60812 
60813 #ifndef SQLITE_OMIT_DEPRECATED
60814 /*
60815 ** Return TRUE (non-zero) of the statement supplied as an argument needs
60816 ** to be recompiled.  A statement needs to be recompiled whenever the
60817 ** execution environment changes in a way that would alter the program
60818 ** that sqlite3_prepare() generates.  For example, if new functions or
60819 ** collating sequences are registered or if an authorizer function is
60820 ** added or changed.
60821 */
60822 SQLITE_API int sqlite3_expired(sqlite3_stmt *pStmt){
60823   Vdbe *p = (Vdbe*)pStmt;
60824   return p==0 || p->expired;
60825 }
60826 #endif
60827 
60828 /*
60829 ** Check on a Vdbe to make sure it has not been finalized.  Log
60830 ** an error and return true if it has been finalized (or is otherwise
60831 ** invalid).  Return false if it is ok.
60832 */
60833 static int vdbeSafety(Vdbe *p){
60834   if( p->db==0 ){
60835     sqlite3_log(SQLITE_MISUSE, "API called with finalized prepared statement");
60836     return 1;
60837   }else{
60838     return 0;
60839   }
60840 }
60841 static int vdbeSafetyNotNull(Vdbe *p){
60842   if( p==0 ){
60843     sqlite3_log(SQLITE_MISUSE, "API called with NULL prepared statement");
60844     return 1;
60845   }else{
60846     return vdbeSafety(p);
60847   }
60848 }
60849 
60850 /*
60851 ** The following routine destroys a virtual machine that is created by
60852 ** the sqlite3_compile() routine. The integer returned is an SQLITE_
60853 ** success/failure code that describes the result of executing the virtual
60854 ** machine.
60855 **
60856 ** This routine sets the error code and string returned by
60857 ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
60858 */
60859 SQLITE_API int sqlite3_finalize(sqlite3_stmt *pStmt){
60860   int rc;
60861   if( pStmt==0 ){
60862     /* IMPLEMENTATION-OF: R-57228-12904 Invoking sqlite3_finalize() on a NULL
60863     ** pointer is a harmless no-op. */
60864     rc = SQLITE_OK;
60865   }else{
60866     Vdbe *v = (Vdbe*)pStmt;
60867     sqlite3 *db = v->db;
60868 #if SQLITE_THREADSAFE
60869     sqlite3_mutex *mutex;
60870 #endif
60871     if( vdbeSafety(v) ) return SQLITE_MISUSE_BKPT;
60872 #if SQLITE_THREADSAFE
60873     mutex = v->db->mutex;
60874 #endif
60875     sqlite3_mutex_enter(mutex);
60876     rc = sqlite3VdbeFinalize(v);
60877     rc = sqlite3ApiExit(db, rc);
60878     sqlite3_mutex_leave(mutex);
60879   }
60880   return rc;
60881 }
60882 
60883 /*
60884 ** Terminate the current execution of an SQL statement and reset it
60885 ** back to its starting state so that it can be reused. A success code from
60886 ** the prior execution is returned.
60887 **
60888 ** This routine sets the error code and string returned by
60889 ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
60890 */
60891 SQLITE_API int sqlite3_reset(sqlite3_stmt *pStmt){
60892   int rc;
60893   if( pStmt==0 ){
60894     rc = SQLITE_OK;
60895   }else{
60896     Vdbe *v = (Vdbe*)pStmt;
60897     sqlite3_mutex_enter(v->db->mutex);
60898     rc = sqlite3VdbeReset(v);
60899     sqlite3VdbeRewind(v);
60900     assert( (rc & (v->db->errMask))==rc );
60901     rc = sqlite3ApiExit(v->db, rc);
60902     sqlite3_mutex_leave(v->db->mutex);
60903   }
60904   return rc;
60905 }
60906 
60907 /*
60908 ** Set all the parameters in the compiled SQL statement to NULL.
60909 */
60910 SQLITE_API int sqlite3_clear_bindings(sqlite3_stmt *pStmt){
60911   int i;
60912   int rc = SQLITE_OK;
60913   Vdbe *p = (Vdbe*)pStmt;
60914 #if SQLITE_THREADSAFE
60915   sqlite3_mutex *mutex = ((Vdbe*)pStmt)->db->mutex;
60916 #endif
60917   sqlite3_mutex_enter(mutex);
60918   for(i=0; i<p->nVar; i++){
60919     sqlite3VdbeMemRelease(&p->aVar[i]);
60920     p->aVar[i].flags = MEM_Null;
60921   }
60922   if( p->isPrepareV2 && p->expmask ){
60923     p->expired = 1;
60924   }
60925   sqlite3_mutex_leave(mutex);
60926   return rc;
60927 }
60928 
60929 
60930 /**************************** sqlite3_value_  *******************************
60931 ** The following routines extract information from a Mem or sqlite3_value
60932 ** structure.
60933 */
60934 SQLITE_API const void *sqlite3_value_blob(sqlite3_value *pVal){
60935   Mem *p = (Mem*)pVal;
60936   if( p->flags & (MEM_Blob|MEM_Str) ){
60937     sqlite3VdbeMemExpandBlob(p);
60938     p->flags &= ~MEM_Str;
60939     p->flags |= MEM_Blob;
60940     return p->n ? p->z : 0;
60941   }else{
60942     return sqlite3_value_text(pVal);
60943   }
60944 }
60945 SQLITE_API int sqlite3_value_bytes(sqlite3_value *pVal){
60946   return sqlite3ValueBytes(pVal, SQLITE_UTF8);
60947 }
60948 SQLITE_API int sqlite3_value_bytes16(sqlite3_value *pVal){
60949   return sqlite3ValueBytes(pVal, SQLITE_UTF16NATIVE);
60950 }
60951 SQLITE_API double sqlite3_value_double(sqlite3_value *pVal){
60952   return sqlite3VdbeRealValue((Mem*)pVal);
60953 }
60954 SQLITE_API int sqlite3_value_int(sqlite3_value *pVal){
60955   return (int)sqlite3VdbeIntValue((Mem*)pVal);
60956 }
60957 SQLITE_API sqlite_int64 sqlite3_value_int64(sqlite3_value *pVal){
60958   return sqlite3VdbeIntValue((Mem*)pVal);
60959 }
60960 SQLITE_API const unsigned char *sqlite3_value_text(sqlite3_value *pVal){
60961   return (const unsigned char *)sqlite3ValueText(pVal, SQLITE_UTF8);
60962 }
60963 #ifndef SQLITE_OMIT_UTF16
60964 SQLITE_API const void *sqlite3_value_text16(sqlite3_value* pVal){
60965   return sqlite3ValueText(pVal, SQLITE_UTF16NATIVE);
60966 }
60967 SQLITE_API const void *sqlite3_value_text16be(sqlite3_value *pVal){
60968   return sqlite3ValueText(pVal, SQLITE_UTF16BE);
60969 }
60970 SQLITE_API const void *sqlite3_value_text16le(sqlite3_value *pVal){
60971   return sqlite3ValueText(pVal, SQLITE_UTF16LE);
60972 }
60973 #endif /* SQLITE_OMIT_UTF16 */
60974 SQLITE_API int sqlite3_value_type(sqlite3_value* pVal){
60975   return pVal->type;
60976 }
60977 
60978 /**************************** sqlite3_result_  *******************************
60979 ** The following routines are used by user-defined functions to specify
60980 ** the function result.
60981 **
60982 ** The setStrOrError() funtion calls sqlite3VdbeMemSetStr() to store the
60983 ** result as a string or blob but if the string or blob is too large, it
60984 ** then sets the error code to SQLITE_TOOBIG
60985 */
60986 static void setResultStrOrError(
60987   sqlite3_context *pCtx,  /* Function context */
60988   const char *z,          /* String pointer */
60989   int n,                  /* Bytes in string, or negative */
60990   u8 enc,                 /* Encoding of z.  0 for BLOBs */
60991   void (*xDel)(void*)     /* Destructor function */
60992 ){
60993   if( sqlite3VdbeMemSetStr(&pCtx->s, z, n, enc, xDel)==SQLITE_TOOBIG ){
60994     sqlite3_result_error_toobig(pCtx);
60995   }
60996 }
60997 SQLITE_API void sqlite3_result_blob(
60998   sqlite3_context *pCtx,
60999   const void *z,
61000   int n,
61001   void (*xDel)(void *)
61002 ){
61003   assert( n>=0 );
61004   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
61005   setResultStrOrError(pCtx, z, n, 0, xDel);
61006 }
61007 SQLITE_API void sqlite3_result_double(sqlite3_context *pCtx, double rVal){
61008   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
61009   sqlite3VdbeMemSetDouble(&pCtx->s, rVal);
61010 }
61011 SQLITE_API void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){
61012   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
61013   pCtx->isError = SQLITE_ERROR;
61014   sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, SQLITE_TRANSIENT);
61015 }
61016 #ifndef SQLITE_OMIT_UTF16
61017 SQLITE_API void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){
61018   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
61019   pCtx->isError = SQLITE_ERROR;
61020   sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT);
61021 }
61022 #endif
61023 SQLITE_API void sqlite3_result_int(sqlite3_context *pCtx, int iVal){
61024   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
61025   sqlite3VdbeMemSetInt64(&pCtx->s, (i64)iVal);
61026 }
61027 SQLITE_API void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){
61028   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
61029   sqlite3VdbeMemSetInt64(&pCtx->s, iVal);
61030 }
61031 SQLITE_API void sqlite3_result_null(sqlite3_context *pCtx){
61032   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
61033   sqlite3VdbeMemSetNull(&pCtx->s);
61034 }
61035 SQLITE_API void sqlite3_result_text(
61036   sqlite3_context *pCtx,
61037   const char *z,
61038   int n,
61039   void (*xDel)(void *)
61040 ){
61041   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
61042   setResultStrOrError(pCtx, z, n, SQLITE_UTF8, xDel);
61043 }
61044 #ifndef SQLITE_OMIT_UTF16
61045 SQLITE_API void sqlite3_result_text16(
61046   sqlite3_context *pCtx,
61047   const void *z,
61048   int n,
61049   void (*xDel)(void *)
61050 ){
61051   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
61052   setResultStrOrError(pCtx, z, n, SQLITE_UTF16NATIVE, xDel);
61053 }
61054 SQLITE_API void sqlite3_result_text16be(
61055   sqlite3_context *pCtx,
61056   const void *z,
61057   int n,
61058   void (*xDel)(void *)
61059 ){
61060   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
61061   setResultStrOrError(pCtx, z, n, SQLITE_UTF16BE, xDel);
61062 }
61063 SQLITE_API void sqlite3_result_text16le(
61064   sqlite3_context *pCtx,
61065   const void *z,
61066   int n,
61067   void (*xDel)(void *)
61068 ){
61069   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
61070   setResultStrOrError(pCtx, z, n, SQLITE_UTF16LE, xDel);
61071 }
61072 #endif /* SQLITE_OMIT_UTF16 */
61073 SQLITE_API void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){
61074   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
61075   sqlite3VdbeMemCopy(&pCtx->s, pValue);
61076 }
61077 SQLITE_API void sqlite3_result_zeroblob(sqlite3_context *pCtx, int n){
61078   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
61079   sqlite3VdbeMemSetZeroBlob(&pCtx->s, n);
61080 }
61081 SQLITE_API void sqlite3_result_error_code(sqlite3_context *pCtx, int errCode){
61082   pCtx->isError = errCode;
61083   if( pCtx->s.flags & MEM_Null ){
61084     sqlite3VdbeMemSetStr(&pCtx->s, sqlite3ErrStr(errCode), -1,
61085                          SQLITE_UTF8, SQLITE_STATIC);
61086   }
61087 }
61088 
61089 /* Force an SQLITE_TOOBIG error. */
61090 SQLITE_API void sqlite3_result_error_toobig(sqlite3_context *pCtx){
61091   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
61092   pCtx->isError = SQLITE_TOOBIG;
61093   sqlite3VdbeMemSetStr(&pCtx->s, "string or blob too big", -1,
61094                        SQLITE_UTF8, SQLITE_STATIC);
61095 }
61096 
61097 /* An SQLITE_NOMEM error. */
61098 SQLITE_API void sqlite3_result_error_nomem(sqlite3_context *pCtx){
61099   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
61100   sqlite3VdbeMemSetNull(&pCtx->s);
61101   pCtx->isError = SQLITE_NOMEM;
61102   pCtx->s.db->mallocFailed = 1;
61103 }
61104 
61105 /*
61106 ** This function is called after a transaction has been committed. It
61107 ** invokes callbacks registered with sqlite3_wal_hook() as required.
61108 */
61109 static int doWalCallbacks(sqlite3 *db){
61110   int rc = SQLITE_OK;
61111 #ifndef SQLITE_OMIT_WAL
61112   int i;
61113   for(i=0; i<db->nDb; i++){
61114     Btree *pBt = db->aDb[i].pBt;
61115     if( pBt ){
61116       int nEntry = sqlite3PagerWalCallback(sqlite3BtreePager(pBt));
61117       if( db->xWalCallback && nEntry>0 && rc==SQLITE_OK ){
61118         rc = db->xWalCallback(db->pWalArg, db, db->aDb[i].zName, nEntry);
61119       }
61120     }
61121   }
61122 #endif
61123   return rc;
61124 }
61125 
61126 /*
61127 ** Execute the statement pStmt, either until a row of data is ready, the
61128 ** statement is completely executed or an error occurs.
61129 **
61130 ** This routine implements the bulk of the logic behind the sqlite_step()
61131 ** API.  The only thing omitted is the automatic recompile if a
61132 ** schema change has occurred.  That detail is handled by the
61133 ** outer sqlite3_step() wrapper procedure.
61134 */
61135 static int sqlite3Step(Vdbe *p){
61136   sqlite3 *db;
61137   int rc;
61138 
61139   assert(p);
61140   if( p->magic!=VDBE_MAGIC_RUN ){
61141     /* We used to require that sqlite3_reset() be called before retrying
61142     ** sqlite3_step() after any error or after SQLITE_DONE.  But beginning
61143     ** with version 3.7.0, we changed this so that sqlite3_reset() would
61144     ** be called automatically instead of throwing the SQLITE_MISUSE error.
61145     ** This "automatic-reset" change is not technically an incompatibility,
61146     ** since any application that receives an SQLITE_MISUSE is broken by
61147     ** definition.
61148     **
61149     ** Nevertheless, some published applications that were originally written
61150     ** for version 3.6.23 or earlier do in fact depend on SQLITE_MISUSE
61151     ** returns, and the so were broken by the automatic-reset change.  As a
61152     ** a work-around, the SQLITE_OMIT_AUTORESET compile-time restores the
61153     ** legacy behavior of returning SQLITE_MISUSE for cases where the
61154     ** previous sqlite3_step() returned something other than a SQLITE_LOCKED
61155     ** or SQLITE_BUSY error.
61156     */
61157 #ifdef SQLITE_OMIT_AUTORESET
61158     if( p->rc==SQLITE_BUSY || p->rc==SQLITE_LOCKED ){
61159       sqlite3_reset((sqlite3_stmt*)p);
61160     }else{
61161       return SQLITE_MISUSE_BKPT;
61162     }
61163 #else
61164     sqlite3_reset((sqlite3_stmt*)p);
61165 #endif
61166   }
61167 
61168   /* Check that malloc() has not failed. If it has, return early. */
61169   db = p->db;
61170   if( db->mallocFailed ){
61171     p->rc = SQLITE_NOMEM;
61172     return SQLITE_NOMEM;
61173   }
61174 
61175   if( p->pc<=0 && p->expired ){
61176     p->rc = SQLITE_SCHEMA;
61177     rc = SQLITE_ERROR;
61178     goto end_of_step;
61179   }
61180   if( p->pc<0 ){
61181     /* If there are no other statements currently running, then
61182     ** reset the interrupt flag.  This prevents a call to sqlite3_interrupt
61183     ** from interrupting a statement that has not yet started.
61184     */
61185     if( db->activeVdbeCnt==0 ){
61186       db->u1.isInterrupted = 0;
61187     }
61188 
61189     assert( db->writeVdbeCnt>0 || db->autoCommit==0 || db->nDeferredCons==0 );
61190 
61191 #ifndef SQLITE_OMIT_TRACE
61192     if( db->xProfile && !db->init.busy ){
61193       sqlite3OsCurrentTimeInt64(db->pVfs, &p->startTime);
61194     }
61195 #endif
61196 
61197     db->activeVdbeCnt++;
61198     if( p->readOnly==0 ) db->writeVdbeCnt++;
61199     p->pc = 0;
61200   }
61201 #ifndef SQLITE_OMIT_EXPLAIN
61202   if( p->explain ){
61203     rc = sqlite3VdbeList(p);
61204   }else
61205 #endif /* SQLITE_OMIT_EXPLAIN */
61206   {
61207     db->vdbeExecCnt++;
61208     rc = sqlite3VdbeExec(p);
61209     db->vdbeExecCnt--;
61210   }
61211 
61212 #ifndef SQLITE_OMIT_TRACE
61213   /* Invoke the profile callback if there is one
61214   */
61215   if( rc!=SQLITE_ROW && db->xProfile && !db->init.busy && p->zSql ){
61216     sqlite3_int64 iNow;
61217     sqlite3OsCurrentTimeInt64(db->pVfs, &iNow);
61218     db->xProfile(db->pProfileArg, p->zSql, (iNow - p->startTime)*1000000);
61219   }
61220 #endif
61221 
61222   if( rc==SQLITE_DONE ){
61223     assert( p->rc==SQLITE_OK );
61224     p->rc = doWalCallbacks(db);
61225     if( p->rc!=SQLITE_OK ){
61226       rc = SQLITE_ERROR;
61227     }
61228   }
61229 
61230   db->errCode = rc;
61231   if( SQLITE_NOMEM==sqlite3ApiExit(p->db, p->rc) ){
61232     p->rc = SQLITE_NOMEM;
61233   }
61234 end_of_step:
61235   /* At this point local variable rc holds the value that should be
61236   ** returned if this statement was compiled using the legacy
61237   ** sqlite3_prepare() interface. According to the docs, this can only
61238   ** be one of the values in the first assert() below. Variable p->rc
61239   ** contains the value that would be returned if sqlite3_finalize()
61240   ** were called on statement p.
61241   */
61242   assert( rc==SQLITE_ROW  || rc==SQLITE_DONE   || rc==SQLITE_ERROR
61243        || rc==SQLITE_BUSY || rc==SQLITE_MISUSE
61244   );
61245   assert( p->rc!=SQLITE_ROW && p->rc!=SQLITE_DONE );
61246   if( p->isPrepareV2 && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
61247     /* If this statement was prepared using sqlite3_prepare_v2(), and an
61248     ** error has occured, then return the error code in p->rc to the
61249     ** caller. Set the error code in the database handle to the same value.
61250     */
61251     rc = db->errCode = p->rc;
61252   }
61253   return (rc&db->errMask);
61254 }
61255 
61256 /*
61257 ** The maximum number of times that a statement will try to reparse
61258 ** itself before giving up and returning SQLITE_SCHEMA.
61259 */
61260 #ifndef SQLITE_MAX_SCHEMA_RETRY
61261 # define SQLITE_MAX_SCHEMA_RETRY 5
61262 #endif
61263 
61264 /*
61265 ** This is the top-level implementation of sqlite3_step().  Call
61266 ** sqlite3Step() to do most of the work.  If a schema error occurs,
61267 ** call sqlite3Reprepare() and try again.
61268 */
61269 SQLITE_API int sqlite3_step(sqlite3_stmt *pStmt){
61270   int rc = SQLITE_OK;      /* Result from sqlite3Step() */
61271   int rc2 = SQLITE_OK;     /* Result from sqlite3Reprepare() */
61272   Vdbe *v = (Vdbe*)pStmt;  /* the prepared statement */
61273   int cnt = 0;             /* Counter to prevent infinite loop of reprepares */
61274   sqlite3 *db;             /* The database connection */
61275 
61276   if( vdbeSafetyNotNull(v) ){
61277     return SQLITE_MISUSE_BKPT;
61278   }
61279   db = v->db;
61280   sqlite3_mutex_enter(db->mutex);
61281   while( (rc = sqlite3Step(v))==SQLITE_SCHEMA
61282          && cnt++ < SQLITE_MAX_SCHEMA_RETRY
61283          && (rc2 = rc = sqlite3Reprepare(v))==SQLITE_OK ){
61284     sqlite3_reset(pStmt);
61285     v->expired = 0;
61286   }
61287   if( rc2!=SQLITE_OK && ALWAYS(v->isPrepareV2) && ALWAYS(db->pErr) ){
61288     /* This case occurs after failing to recompile an sql statement.
61289     ** The error message from the SQL compiler has already been loaded
61290     ** into the database handle. This block copies the error message
61291     ** from the database handle into the statement and sets the statement
61292     ** program counter to 0 to ensure that when the statement is
61293     ** finalized or reset the parser error message is available via
61294     ** sqlite3_errmsg() and sqlite3_errcode().
61295     */
61296     const char *zErr = (const char *)sqlite3_value_text(db->pErr);
61297     sqlite3DbFree(db, v->zErrMsg);
61298     if( !db->mallocFailed ){
61299       v->zErrMsg = sqlite3DbStrDup(db, zErr);
61300       v->rc = rc2;
61301     } else {
61302       v->zErrMsg = 0;
61303       v->rc = rc = SQLITE_NOMEM;
61304     }
61305   }
61306   rc = sqlite3ApiExit(db, rc);
61307   sqlite3_mutex_leave(db->mutex);
61308   return rc;
61309 }
61310 
61311 /*
61312 ** Extract the user data from a sqlite3_context structure and return a
61313 ** pointer to it.
61314 */
61315 SQLITE_API void *sqlite3_user_data(sqlite3_context *p){
61316   assert( p && p->pFunc );
61317   return p->pFunc->pUserData;
61318 }
61319 
61320 /*
61321 ** Extract the user data from a sqlite3_context structure and return a
61322 ** pointer to it.
61323 **
61324 ** IMPLEMENTATION-OF: R-46798-50301 The sqlite3_context_db_handle() interface
61325 ** returns a copy of the pointer to the database connection (the 1st
61326 ** parameter) of the sqlite3_create_function() and
61327 ** sqlite3_create_function16() routines that originally registered the
61328 ** application defined function.
61329 */
61330 SQLITE_API sqlite3 *sqlite3_context_db_handle(sqlite3_context *p){
61331   assert( p && p->pFunc );
61332   return p->s.db;
61333 }
61334 
61335 /*
61336 ** The following is the implementation of an SQL function that always
61337 ** fails with an error message stating that the function is used in the
61338 ** wrong context.  The sqlite3_overload_function() API might construct
61339 ** SQL function that use this routine so that the functions will exist
61340 ** for name resolution but are actually overloaded by the xFindFunction
61341 ** method of virtual tables.
61342 */
61343 SQLITE_PRIVATE void sqlite3InvalidFunction(
61344   sqlite3_context *context,  /* The function calling context */
61345   int NotUsed,               /* Number of arguments to the function */
61346   sqlite3_value **NotUsed2   /* Value of each argument */
61347 ){
61348   const char *zName = context->pFunc->zName;
61349   char *zErr;
61350   UNUSED_PARAMETER2(NotUsed, NotUsed2);
61351   zErr = sqlite3_mprintf(
61352       "unable to use function %s in the requested context", zName);
61353   sqlite3_result_error(context, zErr, -1);
61354   sqlite3_free(zErr);
61355 }
61356 
61357 /*
61358 ** Allocate or return the aggregate context for a user function.  A new
61359 ** context is allocated on the first call.  Subsequent calls return the
61360 ** same context that was returned on prior calls.
61361 */
61362 SQLITE_API void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){
61363   Mem *pMem;
61364   assert( p && p->pFunc && p->pFunc->xStep );
61365   assert( sqlite3_mutex_held(p->s.db->mutex) );
61366   pMem = p->pMem;
61367   testcase( nByte<0 );
61368   if( (pMem->flags & MEM_Agg)==0 ){
61369     if( nByte<=0 ){
61370       sqlite3VdbeMemReleaseExternal(pMem);
61371       pMem->flags = MEM_Null;
61372       pMem->z = 0;
61373     }else{
61374       sqlite3VdbeMemGrow(pMem, nByte, 0);
61375       pMem->flags = MEM_Agg;
61376       pMem->u.pDef = p->pFunc;
61377       if( pMem->z ){
61378         memset(pMem->z, 0, nByte);
61379       }
61380     }
61381   }
61382   return (void*)pMem->z;
61383 }
61384 
61385 /*
61386 ** Return the auxilary data pointer, if any, for the iArg'th argument to
61387 ** the user-function defined by pCtx.
61388 */
61389 SQLITE_API void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){
61390   VdbeFunc *pVdbeFunc;
61391 
61392   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
61393   pVdbeFunc = pCtx->pVdbeFunc;
61394   if( !pVdbeFunc || iArg>=pVdbeFunc->nAux || iArg<0 ){
61395     return 0;
61396   }
61397   return pVdbeFunc->apAux[iArg].pAux;
61398 }
61399 
61400 /*
61401 ** Set the auxilary data pointer and delete function, for the iArg'th
61402 ** argument to the user-function defined by pCtx. Any previous value is
61403 ** deleted by calling the delete function specified when it was set.
61404 */
61405 SQLITE_API void sqlite3_set_auxdata(
61406   sqlite3_context *pCtx,
61407   int iArg,
61408   void *pAux,
61409   void (*xDelete)(void*)
61410 ){
61411   struct AuxData *pAuxData;
61412   VdbeFunc *pVdbeFunc;
61413   if( iArg<0 ) goto failed;
61414 
61415   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
61416   pVdbeFunc = pCtx->pVdbeFunc;
61417   if( !pVdbeFunc || pVdbeFunc->nAux<=iArg ){
61418     int nAux = (pVdbeFunc ? pVdbeFunc->nAux : 0);
61419     int nMalloc = sizeof(VdbeFunc) + sizeof(struct AuxData)*iArg;
61420     pVdbeFunc = sqlite3DbRealloc(pCtx->s.db, pVdbeFunc, nMalloc);
61421     if( !pVdbeFunc ){
61422       goto failed;
61423     }
61424     pCtx->pVdbeFunc = pVdbeFunc;
61425     memset(&pVdbeFunc->apAux[nAux], 0, sizeof(struct AuxData)*(iArg+1-nAux));
61426     pVdbeFunc->nAux = iArg+1;
61427     pVdbeFunc->pFunc = pCtx->pFunc;
61428   }
61429 
61430   pAuxData = &pVdbeFunc->apAux[iArg];
61431   if( pAuxData->pAux && pAuxData->xDelete ){
61432     pAuxData->xDelete(pAuxData->pAux);
61433   }
61434   pAuxData->pAux = pAux;
61435   pAuxData->xDelete = xDelete;
61436   return;
61437 
61438 failed:
61439   if( xDelete ){
61440     xDelete(pAux);
61441   }
61442 }
61443 
61444 #ifndef SQLITE_OMIT_DEPRECATED
61445 /*
61446 ** Return the number of times the Step function of a aggregate has been
61447 ** called.
61448 **
61449 ** This function is deprecated.  Do not use it for new code.  It is
61450 ** provide only to avoid breaking legacy code.  New aggregate function
61451 ** implementations should keep their own counts within their aggregate
61452 ** context.
61453 */
61454 SQLITE_API int sqlite3_aggregate_count(sqlite3_context *p){
61455   assert( p && p->pMem && p->pFunc && p->pFunc->xStep );
61456   return p->pMem->n;
61457 }
61458 #endif
61459 
61460 /*
61461 ** Return the number of columns in the result set for the statement pStmt.
61462 */
61463 SQLITE_API int sqlite3_column_count(sqlite3_stmt *pStmt){
61464   Vdbe *pVm = (Vdbe *)pStmt;
61465   return pVm ? pVm->nResColumn : 0;
61466 }
61467 
61468 /*
61469 ** Return the number of values available from the current row of the
61470 ** currently executing statement pStmt.
61471 */
61472 SQLITE_API int sqlite3_data_count(sqlite3_stmt *pStmt){
61473   Vdbe *pVm = (Vdbe *)pStmt;
61474   if( pVm==0 || pVm->pResultSet==0 ) return 0;
61475   return pVm->nResColumn;
61476 }
61477 
61478 
61479 /*
61480 ** Check to see if column iCol of the given statement is valid.  If
61481 ** it is, return a pointer to the Mem for the value of that column.
61482 ** If iCol is not valid, return a pointer to a Mem which has a value
61483 ** of NULL.
61484 */
61485 static Mem *columnMem(sqlite3_stmt *pStmt, int i){
61486   Vdbe *pVm;
61487   Mem *pOut;
61488 
61489   pVm = (Vdbe *)pStmt;
61490   if( pVm && pVm->pResultSet!=0 && i<pVm->nResColumn && i>=0 ){
61491     sqlite3_mutex_enter(pVm->db->mutex);
61492     pOut = &pVm->pResultSet[i];
61493   }else{
61494     /* If the value passed as the second argument is out of range, return
61495     ** a pointer to the following static Mem object which contains the
61496     ** value SQL NULL. Even though the Mem structure contains an element
61497     ** of type i64, on certain architecture (x86) with certain compiler
61498     ** switches (-Os), gcc may align this Mem object on a 4-byte boundary
61499     ** instead of an 8-byte one. This all works fine, except that when
61500     ** running with SQLITE_DEBUG defined the SQLite code sometimes assert()s
61501     ** that a Mem structure is located on an 8-byte boundary. To prevent
61502     ** this assert() from failing, when building with SQLITE_DEBUG defined
61503     ** using gcc, force nullMem to be 8-byte aligned using the magical
61504     ** __attribute__((aligned(8))) macro.  */
61505     static const Mem nullMem
61506 #if defined(SQLITE_DEBUG) && defined(__GNUC__)
61507       __attribute__((aligned(8)))
61508 #endif
61509       = {0, "", (double)0, {0}, 0, MEM_Null, SQLITE_NULL, 0,
61510 #ifdef SQLITE_DEBUG
61511          0, 0,  /* pScopyFrom, pFiller */
61512 #endif
61513          0, 0 };
61514 
61515     if( pVm && ALWAYS(pVm->db) ){
61516       sqlite3_mutex_enter(pVm->db->mutex);
61517       sqlite3Error(pVm->db, SQLITE_RANGE, 0);
61518     }
61519     pOut = (Mem*)&nullMem;
61520   }
61521   return pOut;
61522 }
61523 
61524 /*
61525 ** This function is called after invoking an sqlite3_value_XXX function on a
61526 ** column value (i.e. a value returned by evaluating an SQL expression in the
61527 ** select list of a SELECT statement) that may cause a malloc() failure. If
61528 ** malloc() has failed, the threads mallocFailed flag is cleared and the result
61529 ** code of statement pStmt set to SQLITE_NOMEM.
61530 **
61531 ** Specifically, this is called from within:
61532 **
61533 **     sqlite3_column_int()
61534 **     sqlite3_column_int64()
61535 **     sqlite3_column_text()
61536 **     sqlite3_column_text16()
61537 **     sqlite3_column_real()
61538 **     sqlite3_column_bytes()
61539 **     sqlite3_column_bytes16()
61540 **     sqiite3_column_blob()
61541 */
61542 static void columnMallocFailure(sqlite3_stmt *pStmt)
61543 {
61544   /* If malloc() failed during an encoding conversion within an
61545   ** sqlite3_column_XXX API, then set the return code of the statement to
61546   ** SQLITE_NOMEM. The next call to _step() (if any) will return SQLITE_ERROR
61547   ** and _finalize() will return NOMEM.
61548   */
61549   Vdbe *p = (Vdbe *)pStmt;
61550   if( p ){
61551     p->rc = sqlite3ApiExit(p->db, p->rc);
61552     sqlite3_mutex_leave(p->db->mutex);
61553   }
61554 }
61555 
61556 /**************************** sqlite3_column_  *******************************
61557 ** The following routines are used to access elements of the current row
61558 ** in the result set.
61559 */
61560 SQLITE_API const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){
61561   const void *val;
61562   val = sqlite3_value_blob( columnMem(pStmt,i) );
61563   /* Even though there is no encoding conversion, value_blob() might
61564   ** need to call malloc() to expand the result of a zeroblob()
61565   ** expression.
61566   */
61567   columnMallocFailure(pStmt);
61568   return val;
61569 }
61570 SQLITE_API int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){
61571   int val = sqlite3_value_bytes( columnMem(pStmt,i) );
61572   columnMallocFailure(pStmt);
61573   return val;
61574 }
61575 SQLITE_API int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){
61576   int val = sqlite3_value_bytes16( columnMem(pStmt,i) );
61577   columnMallocFailure(pStmt);
61578   return val;
61579 }
61580 SQLITE_API double sqlite3_column_double(sqlite3_stmt *pStmt, int i){
61581   double val = sqlite3_value_double( columnMem(pStmt,i) );
61582   columnMallocFailure(pStmt);
61583   return val;
61584 }
61585 SQLITE_API int sqlite3_column_int(sqlite3_stmt *pStmt, int i){
61586   int val = sqlite3_value_int( columnMem(pStmt,i) );
61587   columnMallocFailure(pStmt);
61588   return val;
61589 }
61590 SQLITE_API sqlite_int64 sqlite3_column_int64(sqlite3_stmt *pStmt, int i){
61591   sqlite_int64 val = sqlite3_value_int64( columnMem(pStmt,i) );
61592   columnMallocFailure(pStmt);
61593   return val;
61594 }
61595 SQLITE_API const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){
61596   const unsigned char *val = sqlite3_value_text( columnMem(pStmt,i) );
61597   columnMallocFailure(pStmt);
61598   return val;
61599 }
61600 SQLITE_API sqlite3_value *sqlite3_column_value(sqlite3_stmt *pStmt, int i){
61601   Mem *pOut = columnMem(pStmt, i);
61602   if( pOut->flags&MEM_Static ){
61603     pOut->flags &= ~MEM_Static;
61604     pOut->flags |= MEM_Ephem;
61605   }
61606   columnMallocFailure(pStmt);
61607   return (sqlite3_value *)pOut;
61608 }
61609 #ifndef SQLITE_OMIT_UTF16
61610 SQLITE_API const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){
61611   const void *val = sqlite3_value_text16( columnMem(pStmt,i) );
61612   columnMallocFailure(pStmt);
61613   return val;
61614 }
61615 #endif /* SQLITE_OMIT_UTF16 */
61616 SQLITE_API int sqlite3_column_type(sqlite3_stmt *pStmt, int i){
61617   int iType = sqlite3_value_type( columnMem(pStmt,i) );
61618   columnMallocFailure(pStmt);
61619   return iType;
61620 }
61621 
61622 /* The following function is experimental and subject to change or
61623 ** removal */
61624 /*int sqlite3_column_numeric_type(sqlite3_stmt *pStmt, int i){
61625 **  return sqlite3_value_numeric_type( columnMem(pStmt,i) );
61626 **}
61627 */
61628 
61629 /*
61630 ** Convert the N-th element of pStmt->pColName[] into a string using
61631 ** xFunc() then return that string.  If N is out of range, return 0.
61632 **
61633 ** There are up to 5 names for each column.  useType determines which
61634 ** name is returned.  Here are the names:
61635 **
61636 **    0      The column name as it should be displayed for output
61637 **    1      The datatype name for the column
61638 **    2      The name of the database that the column derives from
61639 **    3      The name of the table that the column derives from
61640 **    4      The name of the table column that the result column derives from
61641 **
61642 ** If the result is not a simple column reference (if it is an expression
61643 ** or a constant) then useTypes 2, 3, and 4 return NULL.
61644 */
61645 static const void *columnName(
61646   sqlite3_stmt *pStmt,
61647   int N,
61648   const void *(*xFunc)(Mem*),
61649   int useType
61650 ){
61651   const void *ret = 0;
61652   Vdbe *p = (Vdbe *)pStmt;
61653   int n;
61654   sqlite3 *db = p->db;
61655 
61656   assert( db!=0 );
61657   n = sqlite3_column_count(pStmt);
61658   if( N<n && N>=0 ){
61659     N += useType*n;
61660     sqlite3_mutex_enter(db->mutex);
61661     assert( db->mallocFailed==0 );
61662     ret = xFunc(&p->aColName[N]);
61663      /* A malloc may have failed inside of the xFunc() call. If this
61664     ** is the case, clear the mallocFailed flag and return NULL.
61665     */
61666     if( db->mallocFailed ){
61667       db->mallocFailed = 0;
61668       ret = 0;
61669     }
61670     sqlite3_mutex_leave(db->mutex);
61671   }
61672   return ret;
61673 }
61674 
61675 /*
61676 ** Return the name of the Nth column of the result set returned by SQL
61677 ** statement pStmt.
61678 */
61679 SQLITE_API const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){
61680   return columnName(
61681       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_NAME);
61682 }
61683 #ifndef SQLITE_OMIT_UTF16
61684 SQLITE_API const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){
61685   return columnName(
61686       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_NAME);
61687 }
61688 #endif
61689 
61690 /*
61691 ** Constraint:  If you have ENABLE_COLUMN_METADATA then you must
61692 ** not define OMIT_DECLTYPE.
61693 */
61694 #if defined(SQLITE_OMIT_DECLTYPE) && defined(SQLITE_ENABLE_COLUMN_METADATA)
61695 # error "Must not define both SQLITE_OMIT_DECLTYPE \
61696          and SQLITE_ENABLE_COLUMN_METADATA"
61697 #endif
61698 
61699 #ifndef SQLITE_OMIT_DECLTYPE
61700 /*
61701 ** Return the column declaration type (if applicable) of the 'i'th column
61702 ** of the result set of SQL statement pStmt.
61703 */
61704 SQLITE_API const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){
61705   return columnName(
61706       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DECLTYPE);
61707 }
61708 #ifndef SQLITE_OMIT_UTF16
61709 SQLITE_API const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){
61710   return columnName(
61711       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DECLTYPE);
61712 }
61713 #endif /* SQLITE_OMIT_UTF16 */
61714 #endif /* SQLITE_OMIT_DECLTYPE */
61715 
61716 #ifdef SQLITE_ENABLE_COLUMN_METADATA
61717 /*
61718 ** Return the name of the database from which a result column derives.
61719 ** NULL is returned if the result column is an expression or constant or
61720 ** anything else which is not an unabiguous reference to a database column.
61721 */
61722 SQLITE_API const char *sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){
61723   return columnName(
61724       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DATABASE);
61725 }
61726 #ifndef SQLITE_OMIT_UTF16
61727 SQLITE_API const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){
61728   return columnName(
61729       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DATABASE);
61730 }
61731 #endif /* SQLITE_OMIT_UTF16 */
61732 
61733 /*
61734 ** Return the name of the table from which a result column derives.
61735 ** NULL is returned if the result column is an expression or constant or
61736 ** anything else which is not an unabiguous reference to a database column.
61737 */
61738 SQLITE_API const char *sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){
61739   return columnName(
61740       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_TABLE);
61741 }
61742 #ifndef SQLITE_OMIT_UTF16
61743 SQLITE_API const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){
61744   return columnName(
61745       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_TABLE);
61746 }
61747 #endif /* SQLITE_OMIT_UTF16 */
61748 
61749 /*
61750 ** Return the name of the table column from which a result column derives.
61751 ** NULL is returned if the result column is an expression or constant or
61752 ** anything else which is not an unabiguous reference to a database column.
61753 */
61754 SQLITE_API const char *sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){
61755   return columnName(
61756       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_COLUMN);
61757 }
61758 #ifndef SQLITE_OMIT_UTF16
61759 SQLITE_API const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){
61760   return columnName(
61761       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_COLUMN);
61762 }
61763 #endif /* SQLITE_OMIT_UTF16 */
61764 #endif /* SQLITE_ENABLE_COLUMN_METADATA */
61765 
61766 
61767 /******************************* sqlite3_bind_  ***************************
61768 **
61769 ** Routines used to attach values to wildcards in a compiled SQL statement.
61770 */
61771 /*
61772 ** Unbind the value bound to variable i in virtual machine p. This is the
61773 ** the same as binding a NULL value to the column. If the "i" parameter is
61774 ** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK.
61775 **
61776 ** A successful evaluation of this routine acquires the mutex on p.
61777 ** the mutex is released if any kind of error occurs.
61778 **
61779 ** The error code stored in database p->db is overwritten with the return
61780 ** value in any case.
61781 */
61782 static int vdbeUnbind(Vdbe *p, int i){
61783   Mem *pVar;
61784   if( vdbeSafetyNotNull(p) ){
61785     return SQLITE_MISUSE_BKPT;
61786   }
61787   sqlite3_mutex_enter(p->db->mutex);
61788   if( p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){
61789     sqlite3Error(p->db, SQLITE_MISUSE, 0);
61790     sqlite3_mutex_leave(p->db->mutex);
61791     sqlite3_log(SQLITE_MISUSE,
61792         "bind on a busy prepared statement: [%s]", p->zSql);
61793     return SQLITE_MISUSE_BKPT;
61794   }
61795   if( i<1 || i>p->nVar ){
61796     sqlite3Error(p->db, SQLITE_RANGE, 0);
61797     sqlite3_mutex_leave(p->db->mutex);
61798     return SQLITE_RANGE;
61799   }
61800   i--;
61801   pVar = &p->aVar[i];
61802   sqlite3VdbeMemRelease(pVar);
61803   pVar->flags = MEM_Null;
61804   sqlite3Error(p->db, SQLITE_OK, 0);
61805 
61806   /* If the bit corresponding to this variable in Vdbe.expmask is set, then
61807   ** binding a new value to this variable invalidates the current query plan.
61808   **
61809   ** IMPLEMENTATION-OF: R-48440-37595 If the specific value bound to host
61810   ** parameter in the WHERE clause might influence the choice of query plan
61811   ** for a statement, then the statement will be automatically recompiled,
61812   ** as if there had been a schema change, on the first sqlite3_step() call
61813   ** following any change to the bindings of that parameter.
61814   */
61815   if( p->isPrepareV2 &&
61816      ((i<32 && p->expmask & ((u32)1 << i)) || p->expmask==0xffffffff)
61817   ){
61818     p->expired = 1;
61819   }
61820   return SQLITE_OK;
61821 }
61822 
61823 /*
61824 ** Bind a text or BLOB value.
61825 */
61826 static int bindText(
61827   sqlite3_stmt *pStmt,   /* The statement to bind against */
61828   int i,                 /* Index of the parameter to bind */
61829   const void *zData,     /* Pointer to the data to be bound */
61830   int nData,             /* Number of bytes of data to be bound */
61831   void (*xDel)(void*),   /* Destructor for the data */
61832   u8 encoding            /* Encoding for the data */
61833 ){
61834   Vdbe *p = (Vdbe *)pStmt;
61835   Mem *pVar;
61836   int rc;
61837 
61838   rc = vdbeUnbind(p, i);
61839   if( rc==SQLITE_OK ){
61840     if( zData!=0 ){
61841       pVar = &p->aVar[i-1];
61842       rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel);
61843       if( rc==SQLITE_OK && encoding!=0 ){
61844         rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db));
61845       }
61846       sqlite3Error(p->db, rc, 0);
61847       rc = sqlite3ApiExit(p->db, rc);
61848     }
61849     sqlite3_mutex_leave(p->db->mutex);
61850   }else if( xDel!=SQLITE_STATIC && xDel!=SQLITE_TRANSIENT ){
61851     xDel((void*)zData);
61852   }
61853   return rc;
61854 }
61855 
61856 
61857 /*
61858 ** Bind a blob value to an SQL statement variable.
61859 */
61860 SQLITE_API int sqlite3_bind_blob(
61861   sqlite3_stmt *pStmt,
61862   int i,
61863   const void *zData,
61864   int nData,
61865   void (*xDel)(void*)
61866 ){
61867   return bindText(pStmt, i, zData, nData, xDel, 0);
61868 }
61869 SQLITE_API int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){
61870   int rc;
61871   Vdbe *p = (Vdbe *)pStmt;
61872   rc = vdbeUnbind(p, i);
61873   if( rc==SQLITE_OK ){
61874     sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue);
61875     sqlite3_mutex_leave(p->db->mutex);
61876   }
61877   return rc;
61878 }
61879 SQLITE_API int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){
61880   return sqlite3_bind_int64(p, i, (i64)iValue);
61881 }
61882 SQLITE_API int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){
61883   int rc;
61884   Vdbe *p = (Vdbe *)pStmt;
61885   rc = vdbeUnbind(p, i);
61886   if( rc==SQLITE_OK ){
61887     sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue);
61888     sqlite3_mutex_leave(p->db->mutex);
61889   }
61890   return rc;
61891 }
61892 SQLITE_API int sqlite3_bind_null(sqlite3_stmt *pStmt, int i){
61893   int rc;
61894   Vdbe *p = (Vdbe*)pStmt;
61895   rc = vdbeUnbind(p, i);
61896   if( rc==SQLITE_OK ){
61897     sqlite3_mutex_leave(p->db->mutex);
61898   }
61899   return rc;
61900 }
61901 SQLITE_API int sqlite3_bind_text(
61902   sqlite3_stmt *pStmt,
61903   int i,
61904   const char *zData,
61905   int nData,
61906   void (*xDel)(void*)
61907 ){
61908   return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8);
61909 }
61910 #ifndef SQLITE_OMIT_UTF16
61911 SQLITE_API int sqlite3_bind_text16(
61912   sqlite3_stmt *pStmt,
61913   int i,
61914   const void *zData,
61915   int nData,
61916   void (*xDel)(void*)
61917 ){
61918   return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF16NATIVE);
61919 }
61920 #endif /* SQLITE_OMIT_UTF16 */
61921 SQLITE_API int sqlite3_bind_value(sqlite3_stmt *pStmt, int i, const sqlite3_value *pValue){
61922   int rc;
61923   switch( pValue->type ){
61924     case SQLITE_INTEGER: {
61925       rc = sqlite3_bind_int64(pStmt, i, pValue->u.i);
61926       break;
61927     }
61928     case SQLITE_FLOAT: {
61929       rc = sqlite3_bind_double(pStmt, i, pValue->r);
61930       break;
61931     }
61932     case SQLITE_BLOB: {
61933       if( pValue->flags & MEM_Zero ){
61934         rc = sqlite3_bind_zeroblob(pStmt, i, pValue->u.nZero);
61935       }else{
61936         rc = sqlite3_bind_blob(pStmt, i, pValue->z, pValue->n,SQLITE_TRANSIENT);
61937       }
61938       break;
61939     }
61940     case SQLITE_TEXT: {
61941       rc = bindText(pStmt,i,  pValue->z, pValue->n, SQLITE_TRANSIENT,
61942                               pValue->enc);
61943       break;
61944     }
61945     default: {
61946       rc = sqlite3_bind_null(pStmt, i);
61947       break;
61948     }
61949   }
61950   return rc;
61951 }
61952 SQLITE_API int sqlite3_bind_zeroblob(sqlite3_stmt *pStmt, int i, int n){
61953   int rc;
61954   Vdbe *p = (Vdbe *)pStmt;
61955   rc = vdbeUnbind(p, i);
61956   if( rc==SQLITE_OK ){
61957     sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n);
61958     sqlite3_mutex_leave(p->db->mutex);
61959   }
61960   return rc;
61961 }
61962 
61963 /*
61964 ** Return the number of wildcards that can be potentially bound to.
61965 ** This routine is added to support DBD::SQLite.
61966 */
61967 SQLITE_API int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){
61968   Vdbe *p = (Vdbe*)pStmt;
61969   return p ? p->nVar : 0;
61970 }
61971 
61972 /*
61973 ** Return the name of a wildcard parameter.  Return NULL if the index
61974 ** is out of range or if the wildcard is unnamed.
61975 **
61976 ** The result is always UTF-8.
61977 */
61978 SQLITE_API const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){
61979   Vdbe *p = (Vdbe*)pStmt;
61980   if( p==0 || i<1 || i>p->nzVar ){
61981     return 0;
61982   }
61983   return p->azVar[i-1];
61984 }
61985 
61986 /*
61987 ** Given a wildcard parameter name, return the index of the variable
61988 ** with that name.  If there is no variable with the given name,
61989 ** return 0.
61990 */
61991 SQLITE_PRIVATE int sqlite3VdbeParameterIndex(Vdbe *p, const char *zName, int nName){
61992   int i;
61993   if( p==0 ){
61994     return 0;
61995   }
61996   if( zName ){
61997     for(i=0; i<p->nzVar; i++){
61998       const char *z = p->azVar[i];
61999       if( z && memcmp(z,zName,nName)==0 && z[nName]==0 ){
62000         return i+1;
62001       }
62002     }
62003   }
62004   return 0;
62005 }
62006 SQLITE_API int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){
62007   return sqlite3VdbeParameterIndex((Vdbe*)pStmt, zName, sqlite3Strlen30(zName));
62008 }
62009 
62010 /*
62011 ** Transfer all bindings from the first statement over to the second.
62012 */
62013 SQLITE_PRIVATE int sqlite3TransferBindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
62014   Vdbe *pFrom = (Vdbe*)pFromStmt;
62015   Vdbe *pTo = (Vdbe*)pToStmt;
62016   int i;
62017   assert( pTo->db==pFrom->db );
62018   assert( pTo->nVar==pFrom->nVar );
62019   sqlite3_mutex_enter(pTo->db->mutex);
62020   for(i=0; i<pFrom->nVar; i++){
62021     sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]);
62022   }
62023   sqlite3_mutex_leave(pTo->db->mutex);
62024   return SQLITE_OK;
62025 }
62026 
62027 #ifndef SQLITE_OMIT_DEPRECATED
62028 /*
62029 ** Deprecated external interface.  Internal/core SQLite code
62030 ** should call sqlite3TransferBindings.
62031 **
62032 ** Is is misuse to call this routine with statements from different
62033 ** database connections.  But as this is a deprecated interface, we
62034 ** will not bother to check for that condition.
62035 **
62036 ** If the two statements contain a different number of bindings, then
62037 ** an SQLITE_ERROR is returned.  Nothing else can go wrong, so otherwise
62038 ** SQLITE_OK is returned.
62039 */
62040 SQLITE_API int sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
62041   Vdbe *pFrom = (Vdbe*)pFromStmt;
62042   Vdbe *pTo = (Vdbe*)pToStmt;
62043   if( pFrom->nVar!=pTo->nVar ){
62044     return SQLITE_ERROR;
62045   }
62046   if( pTo->isPrepareV2 && pTo->expmask ){
62047     pTo->expired = 1;
62048   }
62049   if( pFrom->isPrepareV2 && pFrom->expmask ){
62050     pFrom->expired = 1;
62051   }
62052   return sqlite3TransferBindings(pFromStmt, pToStmt);
62053 }
62054 #endif
62055 
62056 /*
62057 ** Return the sqlite3* database handle to which the prepared statement given
62058 ** in the argument belongs.  This is the same database handle that was
62059 ** the first argument to the sqlite3_prepare() that was used to create
62060 ** the statement in the first place.
62061 */
62062 SQLITE_API sqlite3 *sqlite3_db_handle(sqlite3_stmt *pStmt){
62063   return pStmt ? ((Vdbe*)pStmt)->db : 0;
62064 }
62065 
62066 /*
62067 ** Return true if the prepared statement is guaranteed to not modify the
62068 ** database.
62069 */
62070 SQLITE_API int sqlite3_stmt_readonly(sqlite3_stmt *pStmt){
62071   return pStmt ? ((Vdbe*)pStmt)->readOnly : 1;
62072 }
62073 
62074 /*
62075 ** Return a pointer to the next prepared statement after pStmt associated
62076 ** with database connection pDb.  If pStmt is NULL, return the first
62077 ** prepared statement for the database connection.  Return NULL if there
62078 ** are no more.
62079 */
62080 SQLITE_API sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt){
62081   sqlite3_stmt *pNext;
62082   sqlite3_mutex_enter(pDb->mutex);
62083   if( pStmt==0 ){
62084     pNext = (sqlite3_stmt*)pDb->pVdbe;
62085   }else{
62086     pNext = (sqlite3_stmt*)((Vdbe*)pStmt)->pNext;
62087   }
62088   sqlite3_mutex_leave(pDb->mutex);
62089   return pNext;
62090 }
62091 
62092 /*
62093 ** Return the value of a status counter for a prepared statement
62094 */
62095 SQLITE_API int sqlite3_stmt_status(sqlite3_stmt *pStmt, int op, int resetFlag){
62096   Vdbe *pVdbe = (Vdbe*)pStmt;
62097   int v = pVdbe->aCounter[op-1];
62098   if( resetFlag ) pVdbe->aCounter[op-1] = 0;
62099   return v;
62100 }
62101 
62102 /************** End of vdbeapi.c *********************************************/
62103 /************** Begin file vdbetrace.c ***************************************/
62104 /*
62105 ** 2009 November 25
62106 **
62107 ** The author disclaims copyright to this source code.  In place of
62108 ** a legal notice, here is a blessing:
62109 **
62110 **    May you do good and not evil.
62111 **    May you find forgiveness for yourself and forgive others.
62112 **    May you share freely, never taking more than you give.
62113 **
62114 *************************************************************************
62115 **
62116 ** This file contains code used to insert the values of host parameters
62117 ** (aka "wildcards") into the SQL text output by sqlite3_trace().
62118 */
62119 
62120 #ifndef SQLITE_OMIT_TRACE
62121 
62122 /*
62123 ** zSql is a zero-terminated string of UTF-8 SQL text.  Return the number of
62124 ** bytes in this text up to but excluding the first character in
62125 ** a host parameter.  If the text contains no host parameters, return
62126 ** the total number of bytes in the text.
62127 */
62128 static int findNextHostParameter(const char *zSql, int *pnToken){
62129   int tokenType;
62130   int nTotal = 0;
62131   int n;
62132 
62133   *pnToken = 0;
62134   while( zSql[0] ){
62135     n = sqlite3GetToken((u8*)zSql, &tokenType);
62136     assert( n>0 && tokenType!=TK_ILLEGAL );
62137     if( tokenType==TK_VARIABLE ){
62138       *pnToken = n;
62139       break;
62140     }
62141     nTotal += n;
62142     zSql += n;
62143   }
62144   return nTotal;
62145 }
62146 
62147 /*
62148 ** This function returns a pointer to a nul-terminated string in memory
62149 ** obtained from sqlite3DbMalloc(). If sqlite3.vdbeExecCnt is 1, then the
62150 ** string contains a copy of zRawSql but with host parameters expanded to
62151 ** their current bindings. Or, if sqlite3.vdbeExecCnt is greater than 1,
62152 ** then the returned string holds a copy of zRawSql with "-- " prepended
62153 ** to each line of text.
62154 **
62155 ** The calling function is responsible for making sure the memory returned
62156 ** is eventually freed.
62157 **
62158 ** ALGORITHM:  Scan the input string looking for host parameters in any of
62159 ** these forms:  ?, ?N, $A, @A, :A.  Take care to avoid text within
62160 ** string literals, quoted identifier names, and comments.  For text forms,
62161 ** the host parameter index is found by scanning the perpared
62162 ** statement for the corresponding OP_Variable opcode.  Once the host
62163 ** parameter index is known, locate the value in p->aVar[].  Then render
62164 ** the value as a literal in place of the host parameter name.
62165 */
62166 SQLITE_PRIVATE char *sqlite3VdbeExpandSql(
62167   Vdbe *p,                 /* The prepared statement being evaluated */
62168   const char *zRawSql      /* Raw text of the SQL statement */
62169 ){
62170   sqlite3 *db;             /* The database connection */
62171   int idx = 0;             /* Index of a host parameter */
62172   int nextIndex = 1;       /* Index of next ? host parameter */
62173   int n;                   /* Length of a token prefix */
62174   int nToken;              /* Length of the parameter token */
62175   int i;                   /* Loop counter */
62176   Mem *pVar;               /* Value of a host parameter */
62177   StrAccum out;            /* Accumulate the output here */
62178   char zBase[100];         /* Initial working space */
62179 
62180   db = p->db;
62181   sqlite3StrAccumInit(&out, zBase, sizeof(zBase),
62182                       db->aLimit[SQLITE_LIMIT_LENGTH]);
62183   out.db = db;
62184   if( db->vdbeExecCnt>1 ){
62185     while( *zRawSql ){
62186       const char *zStart = zRawSql;
62187       while( *(zRawSql++)!='\n' && *zRawSql );
62188       sqlite3StrAccumAppend(&out, "-- ", 3);
62189       sqlite3StrAccumAppend(&out, zStart, (int)(zRawSql-zStart));
62190     }
62191   }else{
62192     while( zRawSql[0] ){
62193       n = findNextHostParameter(zRawSql, &nToken);
62194       assert( n>0 );
62195       sqlite3StrAccumAppend(&out, zRawSql, n);
62196       zRawSql += n;
62197       assert( zRawSql[0] || nToken==0 );
62198       if( nToken==0 ) break;
62199       if( zRawSql[0]=='?' ){
62200         if( nToken>1 ){
62201           assert( sqlite3Isdigit(zRawSql[1]) );
62202           sqlite3GetInt32(&zRawSql[1], &idx);
62203         }else{
62204           idx = nextIndex;
62205         }
62206       }else{
62207         assert( zRawSql[0]==':' || zRawSql[0]=='$' || zRawSql[0]=='@' );
62208         testcase( zRawSql[0]==':' );
62209         testcase( zRawSql[0]=='$' );
62210         testcase( zRawSql[0]=='@' );
62211         idx = sqlite3VdbeParameterIndex(p, zRawSql, nToken);
62212         assert( idx>0 );
62213       }
62214       zRawSql += nToken;
62215       nextIndex = idx + 1;
62216       assert( idx>0 && idx<=p->nVar );
62217       pVar = &p->aVar[idx-1];
62218       if( pVar->flags & MEM_Null ){
62219         sqlite3StrAccumAppend(&out, "NULL", 4);
62220       }else if( pVar->flags & MEM_Int ){
62221         sqlite3XPrintf(&out, "%lld", pVar->u.i);
62222       }else if( pVar->flags & MEM_Real ){
62223         sqlite3XPrintf(&out, "%!.15g", pVar->r);
62224       }else if( pVar->flags & MEM_Str ){
62225 #ifndef SQLITE_OMIT_UTF16
62226         u8 enc = ENC(db);
62227         if( enc!=SQLITE_UTF8 ){
62228           Mem utf8;
62229           memset(&utf8, 0, sizeof(utf8));
62230           utf8.db = db;
62231           sqlite3VdbeMemSetStr(&utf8, pVar->z, pVar->n, enc, SQLITE_STATIC);
62232           sqlite3VdbeChangeEncoding(&utf8, SQLITE_UTF8);
62233           sqlite3XPrintf(&out, "'%.*q'", utf8.n, utf8.z);
62234           sqlite3VdbeMemRelease(&utf8);
62235         }else
62236 #endif
62237         {
62238           sqlite3XPrintf(&out, "'%.*q'", pVar->n, pVar->z);
62239         }
62240       }else if( pVar->flags & MEM_Zero ){
62241         sqlite3XPrintf(&out, "zeroblob(%d)", pVar->u.nZero);
62242       }else{
62243         assert( pVar->flags & MEM_Blob );
62244         sqlite3StrAccumAppend(&out, "x'", 2);
62245         for(i=0; i<pVar->n; i++){
62246           sqlite3XPrintf(&out, "%02x", pVar->z[i]&0xff);
62247         }
62248         sqlite3StrAccumAppend(&out, "'", 1);
62249       }
62250     }
62251   }
62252   return sqlite3StrAccumFinish(&out);
62253 }
62254 
62255 #endif /* #ifndef SQLITE_OMIT_TRACE */
62256 
62257 /************** End of vdbetrace.c *******************************************/
62258 /************** Begin file vdbe.c ********************************************/
62259 /*
62260 ** 2001 September 15
62261 **
62262 ** The author disclaims copyright to this source code.  In place of
62263 ** a legal notice, here is a blessing:
62264 **
62265 **    May you do good and not evil.
62266 **    May you find forgiveness for yourself and forgive others.
62267 **    May you share freely, never taking more than you give.
62268 **
62269 *************************************************************************
62270 ** The code in this file implements execution method of the
62271 ** Virtual Database Engine (VDBE).  A separate file ("vdbeaux.c")
62272 ** handles housekeeping details such as creating and deleting
62273 ** VDBE instances.  This file is solely interested in executing
62274 ** the VDBE program.
62275 **
62276 ** In the external interface, an "sqlite3_stmt*" is an opaque pointer
62277 ** to a VDBE.
62278 **
62279 ** The SQL parser generates a program which is then executed by
62280 ** the VDBE to do the work of the SQL statement.  VDBE programs are
62281 ** similar in form to assembly language.  The program consists of
62282 ** a linear sequence of operations.  Each operation has an opcode
62283 ** and 5 operands.  Operands P1, P2, and P3 are integers.  Operand P4
62284 ** is a null-terminated string.  Operand P5 is an unsigned character.
62285 ** Few opcodes use all 5 operands.
62286 **
62287 ** Computation results are stored on a set of registers numbered beginning
62288 ** with 1 and going up to Vdbe.nMem.  Each register can store
62289 ** either an integer, a null-terminated string, a floating point
62290 ** number, or the SQL "NULL" value.  An implicit conversion from one
62291 ** type to the other occurs as necessary.
62292 **
62293 ** Most of the code in this file is taken up by the sqlite3VdbeExec()
62294 ** function which does the work of interpreting a VDBE program.
62295 ** But other routines are also provided to help in building up
62296 ** a program instruction by instruction.
62297 **
62298 ** Various scripts scan this source file in order to generate HTML
62299 ** documentation, headers files, or other derived files.  The formatting
62300 ** of the code in this file is, therefore, important.  See other comments
62301 ** in this file for details.  If in doubt, do not deviate from existing
62302 ** commenting and indentation practices when changing or adding code.
62303 */
62304 
62305 /*
62306 ** Invoke this macro on memory cells just prior to changing the
62307 ** value of the cell.  This macro verifies that shallow copies are
62308 ** not misused.
62309 */
62310 #ifdef SQLITE_DEBUG
62311 # define memAboutToChange(P,M) sqlite3VdbeMemPrepareToChange(P,M)
62312 #else
62313 # define memAboutToChange(P,M)
62314 #endif
62315 
62316 /*
62317 ** The following global variable is incremented every time a cursor
62318 ** moves, either by the OP_SeekXX, OP_Next, or OP_Prev opcodes.  The test
62319 ** procedures use this information to make sure that indices are
62320 ** working correctly.  This variable has no function other than to
62321 ** help verify the correct operation of the library.
62322 */
62323 #ifdef SQLITE_TEST
62324 SQLITE_API int sqlite3_search_count = 0;
62325 #endif
62326 
62327 /*
62328 ** When this global variable is positive, it gets decremented once before
62329 ** each instruction in the VDBE.  When reaches zero, the u1.isInterrupted
62330 ** field of the sqlite3 structure is set in order to simulate and interrupt.
62331 **
62332 ** This facility is used for testing purposes only.  It does not function
62333 ** in an ordinary build.
62334 */
62335 #ifdef SQLITE_TEST
62336 SQLITE_API int sqlite3_interrupt_count = 0;
62337 #endif
62338 
62339 /*
62340 ** The next global variable is incremented each type the OP_Sort opcode
62341 ** is executed.  The test procedures use this information to make sure that
62342 ** sorting is occurring or not occurring at appropriate times.   This variable
62343 ** has no function other than to help verify the correct operation of the
62344 ** library.
62345 */
62346 #ifdef SQLITE_TEST
62347 SQLITE_API int sqlite3_sort_count = 0;
62348 #endif
62349 
62350 /*
62351 ** The next global variable records the size of the largest MEM_Blob
62352 ** or MEM_Str that has been used by a VDBE opcode.  The test procedures
62353 ** use this information to make sure that the zero-blob functionality
62354 ** is working correctly.   This variable has no function other than to
62355 ** help verify the correct operation of the library.
62356 */
62357 #ifdef SQLITE_TEST
62358 SQLITE_API int sqlite3_max_blobsize = 0;
62359 static void updateMaxBlobsize(Mem *p){
62360   if( (p->flags & (MEM_Str|MEM_Blob))!=0 && p->n>sqlite3_max_blobsize ){
62361     sqlite3_max_blobsize = p->n;
62362   }
62363 }
62364 #endif
62365 
62366 /*
62367 ** The next global variable is incremented each type the OP_Found opcode
62368 ** is executed. This is used to test whether or not the foreign key
62369 ** operation implemented using OP_FkIsZero is working. This variable
62370 ** has no function other than to help verify the correct operation of the
62371 ** library.
62372 */
62373 #ifdef SQLITE_TEST
62374 SQLITE_API int sqlite3_found_count = 0;
62375 #endif
62376 
62377 /*
62378 ** Test a register to see if it exceeds the current maximum blob size.
62379 ** If it does, record the new maximum blob size.
62380 */
62381 #if defined(SQLITE_TEST) && !defined(SQLITE_OMIT_BUILTIN_TEST)
62382 # define UPDATE_MAX_BLOBSIZE(P)  updateMaxBlobsize(P)
62383 #else
62384 # define UPDATE_MAX_BLOBSIZE(P)
62385 #endif
62386 
62387 /*
62388 ** Convert the given register into a string if it isn't one
62389 ** already. Return non-zero if a malloc() fails.
62390 */
62391 #define Stringify(P, enc) \
62392    if(((P)->flags&(MEM_Str|MEM_Blob))==0 && sqlite3VdbeMemStringify(P,enc)) \
62393      { goto no_mem; }
62394 
62395 /*
62396 ** An ephemeral string value (signified by the MEM_Ephem flag) contains
62397 ** a pointer to a dynamically allocated string where some other entity
62398 ** is responsible for deallocating that string.  Because the register
62399 ** does not control the string, it might be deleted without the register
62400 ** knowing it.
62401 **
62402 ** This routine converts an ephemeral string into a dynamically allocated
62403 ** string that the register itself controls.  In other words, it
62404 ** converts an MEM_Ephem string into an MEM_Dyn string.
62405 */
62406 #define Deephemeralize(P) \
62407    if( ((P)->flags&MEM_Ephem)!=0 \
62408        && sqlite3VdbeMemMakeWriteable(P) ){ goto no_mem;}
62409 
62410 /*
62411 ** Call sqlite3VdbeMemExpandBlob() on the supplied value (type Mem*)
62412 ** P if required.
62413 */
62414 #define ExpandBlob(P) (((P)->flags&MEM_Zero)?sqlite3VdbeMemExpandBlob(P):0)
62415 
62416 /*
62417 ** Argument pMem points at a register that will be passed to a
62418 ** user-defined function or returned to the user as the result of a query.
62419 ** This routine sets the pMem->type variable used by the sqlite3_value_*()
62420 ** routines.
62421 */
62422 SQLITE_PRIVATE void sqlite3VdbeMemStoreType(Mem *pMem){
62423   int flags = pMem->flags;
62424   if( flags & MEM_Null ){
62425     pMem->type = SQLITE_NULL;
62426   }
62427   else if( flags & MEM_Int ){
62428     pMem->type = SQLITE_INTEGER;
62429   }
62430   else if( flags & MEM_Real ){
62431     pMem->type = SQLITE_FLOAT;
62432   }
62433   else if( flags & MEM_Str ){
62434     pMem->type = SQLITE_TEXT;
62435   }else{
62436     pMem->type = SQLITE_BLOB;
62437   }
62438 }
62439 
62440 /*
62441 ** Allocate VdbeCursor number iCur.  Return a pointer to it.  Return NULL
62442 ** if we run out of memory.
62443 */
62444 static VdbeCursor *allocateCursor(
62445   Vdbe *p,              /* The virtual machine */
62446   int iCur,             /* Index of the new VdbeCursor */
62447   int nField,           /* Number of fields in the table or index */
62448   int iDb,              /* When database the cursor belongs to, or -1 */
62449   int isBtreeCursor     /* True for B-Tree.  False for pseudo-table or vtab */
62450 ){
62451   /* Find the memory cell that will be used to store the blob of memory
62452   ** required for this VdbeCursor structure. It is convenient to use a
62453   ** vdbe memory cell to manage the memory allocation required for a
62454   ** VdbeCursor structure for the following reasons:
62455   **
62456   **   * Sometimes cursor numbers are used for a couple of different
62457   **     purposes in a vdbe program. The different uses might require
62458   **     different sized allocations. Memory cells provide growable
62459   **     allocations.
62460   **
62461   **   * When using ENABLE_MEMORY_MANAGEMENT, memory cell buffers can
62462   **     be freed lazily via the sqlite3_release_memory() API. This
62463   **     minimizes the number of malloc calls made by the system.
62464   **
62465   ** Memory cells for cursors are allocated at the top of the address
62466   ** space. Memory cell (p->nMem) corresponds to cursor 0. Space for
62467   ** cursor 1 is managed by memory cell (p->nMem-1), etc.
62468   */
62469   Mem *pMem = &p->aMem[p->nMem-iCur];
62470 
62471   int nByte;
62472   VdbeCursor *pCx = 0;
62473   nByte =
62474       ROUND8(sizeof(VdbeCursor)) +
62475       (isBtreeCursor?sqlite3BtreeCursorSize():0) +
62476       2*nField*sizeof(u32);
62477 
62478   assert( iCur<p->nCursor );
62479   if( p->apCsr[iCur] ){
62480     sqlite3VdbeFreeCursor(p, p->apCsr[iCur]);
62481     p->apCsr[iCur] = 0;
62482   }
62483   if( SQLITE_OK==sqlite3VdbeMemGrow(pMem, nByte, 0) ){
62484     p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->z;
62485     memset(pCx, 0, sizeof(VdbeCursor));
62486     pCx->iDb = iDb;
62487     pCx->nField = nField;
62488     if( nField ){
62489       pCx->aType = (u32 *)&pMem->z[ROUND8(sizeof(VdbeCursor))];
62490     }
62491     if( isBtreeCursor ){
62492       pCx->pCursor = (BtCursor*)
62493           &pMem->z[ROUND8(sizeof(VdbeCursor))+2*nField*sizeof(u32)];
62494       sqlite3BtreeCursorZero(pCx->pCursor);
62495     }
62496   }
62497   return pCx;
62498 }
62499 
62500 /*
62501 ** Try to convert a value into a numeric representation if we can
62502 ** do so without loss of information.  In other words, if the string
62503 ** looks like a number, convert it into a number.  If it does not
62504 ** look like a number, leave it alone.
62505 */
62506 static void applyNumericAffinity(Mem *pRec){
62507   if( (pRec->flags & (MEM_Real|MEM_Int))==0 ){
62508     double rValue;
62509     i64 iValue;
62510     u8 enc = pRec->enc;
62511     if( (pRec->flags&MEM_Str)==0 ) return;
62512     if( sqlite3AtoF(pRec->z, &rValue, pRec->n, enc)==0 ) return;
62513     if( 0==sqlite3Atoi64(pRec->z, &iValue, pRec->n, enc) ){
62514       pRec->u.i = iValue;
62515       pRec->flags |= MEM_Int;
62516     }else{
62517       pRec->r = rValue;
62518       pRec->flags |= MEM_Real;
62519     }
62520   }
62521 }
62522 
62523 /*
62524 ** Processing is determine by the affinity parameter:
62525 **
62526 ** SQLITE_AFF_INTEGER:
62527 ** SQLITE_AFF_REAL:
62528 ** SQLITE_AFF_NUMERIC:
62529 **    Try to convert pRec to an integer representation or a
62530 **    floating-point representation if an integer representation
62531 **    is not possible.  Note that the integer representation is
62532 **    always preferred, even if the affinity is REAL, because
62533 **    an integer representation is more space efficient on disk.
62534 **
62535 ** SQLITE_AFF_TEXT:
62536 **    Convert pRec to a text representation.
62537 **
62538 ** SQLITE_AFF_NONE:
62539 **    No-op.  pRec is unchanged.
62540 */
62541 static void applyAffinity(
62542   Mem *pRec,          /* The value to apply affinity to */
62543   char affinity,      /* The affinity to be applied */
62544   u8 enc              /* Use this text encoding */
62545 ){
62546   if( affinity==SQLITE_AFF_TEXT ){
62547     /* Only attempt the conversion to TEXT if there is an integer or real
62548     ** representation (blob and NULL do not get converted) but no string
62549     ** representation.
62550     */
62551     if( 0==(pRec->flags&MEM_Str) && (pRec->flags&(MEM_Real|MEM_Int)) ){
62552       sqlite3VdbeMemStringify(pRec, enc);
62553     }
62554     pRec->flags &= ~(MEM_Real|MEM_Int);
62555   }else if( affinity!=SQLITE_AFF_NONE ){
62556     assert( affinity==SQLITE_AFF_INTEGER || affinity==SQLITE_AFF_REAL
62557              || affinity==SQLITE_AFF_NUMERIC );
62558     applyNumericAffinity(pRec);
62559     if( pRec->flags & MEM_Real ){
62560       sqlite3VdbeIntegerAffinity(pRec);
62561     }
62562   }
62563 }
62564 
62565 /*
62566 ** Try to convert the type of a function argument or a result column
62567 ** into a numeric representation.  Use either INTEGER or REAL whichever
62568 ** is appropriate.  But only do the conversion if it is possible without
62569 ** loss of information and return the revised type of the argument.
62570 */
62571 SQLITE_API int sqlite3_value_numeric_type(sqlite3_value *pVal){
62572   Mem *pMem = (Mem*)pVal;
62573   if( pMem->type==SQLITE_TEXT ){
62574     applyNumericAffinity(pMem);
62575     sqlite3VdbeMemStoreType(pMem);
62576   }
62577   return pMem->type;
62578 }
62579 
62580 /*
62581 ** Exported version of applyAffinity(). This one works on sqlite3_value*,
62582 ** not the internal Mem* type.
62583 */
62584 SQLITE_PRIVATE void sqlite3ValueApplyAffinity(
62585   sqlite3_value *pVal,
62586   u8 affinity,
62587   u8 enc
62588 ){
62589   applyAffinity((Mem *)pVal, affinity, enc);
62590 }
62591 
62592 #ifdef SQLITE_DEBUG
62593 /*
62594 ** Write a nice string representation of the contents of cell pMem
62595 ** into buffer zBuf, length nBuf.
62596 */
62597 SQLITE_PRIVATE void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf){
62598   char *zCsr = zBuf;
62599   int f = pMem->flags;
62600 
62601   static const char *const encnames[] = {"(X)", "(8)", "(16LE)", "(16BE)"};
62602 
62603   if( f&MEM_Blob ){
62604     int i;
62605     char c;
62606     if( f & MEM_Dyn ){
62607       c = 'z';
62608       assert( (f & (MEM_Static|MEM_Ephem))==0 );
62609     }else if( f & MEM_Static ){
62610       c = 't';
62611       assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
62612     }else if( f & MEM_Ephem ){
62613       c = 'e';
62614       assert( (f & (MEM_Static|MEM_Dyn))==0 );
62615     }else{
62616       c = 's';
62617     }
62618 
62619     sqlite3_snprintf(100, zCsr, "%c", c);
62620     zCsr += sqlite3Strlen30(zCsr);
62621     sqlite3_snprintf(100, zCsr, "%d[", pMem->n);
62622     zCsr += sqlite3Strlen30(zCsr);
62623     for(i=0; i<16 && i<pMem->n; i++){
62624       sqlite3_snprintf(100, zCsr, "%02X", ((int)pMem->z[i] & 0xFF));
62625       zCsr += sqlite3Strlen30(zCsr);
62626     }
62627     for(i=0; i<16 && i<pMem->n; i++){
62628       char z = pMem->z[i];
62629       if( z<32 || z>126 ) *zCsr++ = '.';
62630       else *zCsr++ = z;
62631     }
62632 
62633     sqlite3_snprintf(100, zCsr, "]%s", encnames[pMem->enc]);
62634     zCsr += sqlite3Strlen30(zCsr);
62635     if( f & MEM_Zero ){
62636       sqlite3_snprintf(100, zCsr,"+%dz",pMem->u.nZero);
62637       zCsr += sqlite3Strlen30(zCsr);
62638     }
62639     *zCsr = '\0';
62640   }else if( f & MEM_Str ){
62641     int j, k;
62642     zBuf[0] = ' ';
62643     if( f & MEM_Dyn ){
62644       zBuf[1] = 'z';
62645       assert( (f & (MEM_Static|MEM_Ephem))==0 );
62646     }else if( f & MEM_Static ){
62647       zBuf[1] = 't';
62648       assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
62649     }else if( f & MEM_Ephem ){
62650       zBuf[1] = 'e';
62651       assert( (f & (MEM_Static|MEM_Dyn))==0 );
62652     }else{
62653       zBuf[1] = 's';
62654     }
62655     k = 2;
62656     sqlite3_snprintf(100, &zBuf[k], "%d", pMem->n);
62657     k += sqlite3Strlen30(&zBuf[k]);
62658     zBuf[k++] = '[';
62659     for(j=0; j<15 && j<pMem->n; j++){
62660       u8 c = pMem->z[j];
62661       if( c>=0x20 && c<0x7f ){
62662         zBuf[k++] = c;
62663       }else{
62664         zBuf[k++] = '.';
62665       }
62666     }
62667     zBuf[k++] = ']';
62668     sqlite3_snprintf(100,&zBuf[k], encnames[pMem->enc]);
62669     k += sqlite3Strlen30(&zBuf[k]);
62670     zBuf[k++] = 0;
62671   }
62672 }
62673 #endif
62674 
62675 #ifdef SQLITE_DEBUG
62676 /*
62677 ** Print the value of a register for tracing purposes:
62678 */
62679 static void memTracePrint(FILE *out, Mem *p){
62680   if( p->flags & MEM_Null ){
62681     fprintf(out, " NULL");
62682   }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
62683     fprintf(out, " si:%lld", p->u.i);
62684   }else if( p->flags & MEM_Int ){
62685     fprintf(out, " i:%lld", p->u.i);
62686 #ifndef SQLITE_OMIT_FLOATING_POINT
62687   }else if( p->flags & MEM_Real ){
62688     fprintf(out, " r:%g", p->r);
62689 #endif
62690   }else if( p->flags & MEM_RowSet ){
62691     fprintf(out, " (rowset)");
62692   }else{
62693     char zBuf[200];
62694     sqlite3VdbeMemPrettyPrint(p, zBuf);
62695     fprintf(out, " ");
62696     fprintf(out, "%s", zBuf);
62697   }
62698 }
62699 static void registerTrace(FILE *out, int iReg, Mem *p){
62700   fprintf(out, "REG[%d] = ", iReg);
62701   memTracePrint(out, p);
62702   fprintf(out, "\n");
62703 }
62704 #endif
62705 
62706 #ifdef SQLITE_DEBUG
62707 #  define REGISTER_TRACE(R,M) if(p->trace)registerTrace(p->trace,R,M)
62708 #else
62709 #  define REGISTER_TRACE(R,M)
62710 #endif
62711 
62712 
62713 #ifdef VDBE_PROFILE
62714 
62715 /*
62716 ** hwtime.h contains inline assembler code for implementing
62717 ** high-performance timing routines.
62718 */
62719 /************** Include hwtime.h in the middle of vdbe.c *********************/
62720 /************** Begin file hwtime.h ******************************************/
62721 /*
62722 ** 2008 May 27
62723 **
62724 ** The author disclaims copyright to this source code.  In place of
62725 ** a legal notice, here is a blessing:
62726 **
62727 **    May you do good and not evil.
62728 **    May you find forgiveness for yourself and forgive others.
62729 **    May you share freely, never taking more than you give.
62730 **
62731 ******************************************************************************
62732 **
62733 ** This file contains inline asm code for retrieving "high-performance"
62734 ** counters for x86 class CPUs.
62735 */
62736 #ifndef _HWTIME_H_
62737 #define _HWTIME_H_
62738 
62739 /*
62740 ** The following routine only works on pentium-class (or newer) processors.
62741 ** It uses the RDTSC opcode to read the cycle count value out of the
62742 ** processor and returns that value.  This can be used for high-res
62743 ** profiling.
62744 */
62745 #if (defined(__GNUC__) || defined(_MSC_VER)) && \
62746       (defined(i386) || defined(__i386__) || defined(_M_IX86))
62747 
62748   #if defined(__GNUC__)
62749 
62750   __inline__ sqlite_uint64 sqlite3Hwtime(void){
62751      unsigned int lo, hi;
62752      __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
62753      return (sqlite_uint64)hi << 32 | lo;
62754   }
62755 
62756   #elif defined(_MSC_VER)
62757 
62758   __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
62759      __asm {
62760         rdtsc
62761         ret       ; return value at EDX:EAX
62762      }
62763   }
62764 
62765   #endif
62766 
62767 #elif (defined(__GNUC__) && defined(__x86_64__))
62768 
62769   __inline__ sqlite_uint64 sqlite3Hwtime(void){
62770       unsigned long val;
62771       __asm__ __volatile__ ("rdtsc" : "=A" (val));
62772       return val;
62773   }
62774 
62775 #elif (defined(__GNUC__) && defined(__ppc__))
62776 
62777   __inline__ sqlite_uint64 sqlite3Hwtime(void){
62778       unsigned long long retval;
62779       unsigned long junk;
62780       __asm__ __volatile__ ("\n\
62781           1:      mftbu   %1\n\
62782                   mftb    %L0\n\
62783                   mftbu   %0\n\
62784                   cmpw    %0,%1\n\
62785                   bne     1b"
62786                   : "=r" (retval), "=r" (junk));
62787       return retval;
62788   }
62789 
62790 #else
62791 
62792   #error Need implementation of sqlite3Hwtime() for your platform.
62793 
62794   /*
62795   ** To compile without implementing sqlite3Hwtime() for your platform,
62796   ** you can remove the above #error and use the following
62797   ** stub function.  You will lose timing support for many
62798   ** of the debugging and testing utilities, but it should at
62799   ** least compile and run.
62800   */
62801 SQLITE_PRIVATE   sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
62802 
62803 #endif
62804 
62805 #endif /* !defined(_HWTIME_H_) */
62806 
62807 /************** End of hwtime.h **********************************************/
62808 /************** Continuing where we left off in vdbe.c ***********************/
62809 
62810 #endif
62811 
62812 /*
62813 ** The CHECK_FOR_INTERRUPT macro defined here looks to see if the
62814 ** sqlite3_interrupt() routine has been called.  If it has been, then
62815 ** processing of the VDBE program is interrupted.
62816 **
62817 ** This macro added to every instruction that does a jump in order to
62818 ** implement a loop.  This test used to be on every single instruction,
62819 ** but that meant we more testing that we needed.  By only testing the
62820 ** flag on jump instructions, we get a (small) speed improvement.
62821 */
62822 #define CHECK_FOR_INTERRUPT \
62823    if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
62824 
62825 
62826 #ifndef NDEBUG
62827 /*
62828 ** This function is only called from within an assert() expression. It
62829 ** checks that the sqlite3.nTransaction variable is correctly set to
62830 ** the number of non-transaction savepoints currently in the
62831 ** linked list starting at sqlite3.pSavepoint.
62832 **
62833 ** Usage:
62834 **
62835 **     assert( checkSavepointCount(db) );
62836 */
62837 static int checkSavepointCount(sqlite3 *db){
62838   int n = 0;
62839   Savepoint *p;
62840   for(p=db->pSavepoint; p; p=p->pNext) n++;
62841   assert( n==(db->nSavepoint + db->isTransactionSavepoint) );
62842   return 1;
62843 }
62844 #endif
62845 
62846 /*
62847 ** Transfer error message text from an sqlite3_vtab.zErrMsg (text stored
62848 ** in memory obtained from sqlite3_malloc) into a Vdbe.zErrMsg (text stored
62849 ** in memory obtained from sqlite3DbMalloc).
62850 */
62851 static void importVtabErrMsg(Vdbe *p, sqlite3_vtab *pVtab){
62852   sqlite3 *db = p->db;
62853   sqlite3DbFree(db, p->zErrMsg);
62854   p->zErrMsg = sqlite3DbStrDup(db, pVtab->zErrMsg);
62855   sqlite3_free(pVtab->zErrMsg);
62856   pVtab->zErrMsg = 0;
62857 }
62858 
62859 
62860 /*
62861 ** Execute as much of a VDBE program as we can then return.
62862 **
62863 ** sqlite3VdbeMakeReady() must be called before this routine in order to
62864 ** close the program with a final OP_Halt and to set up the callbacks
62865 ** and the error message pointer.
62866 **
62867 ** Whenever a row or result data is available, this routine will either
62868 ** invoke the result callback (if there is one) or return with
62869 ** SQLITE_ROW.
62870 **
62871 ** If an attempt is made to open a locked database, then this routine
62872 ** will either invoke the busy callback (if there is one) or it will
62873 ** return SQLITE_BUSY.
62874 **
62875 ** If an error occurs, an error message is written to memory obtained
62876 ** from sqlite3_malloc() and p->zErrMsg is made to point to that memory.
62877 ** The error code is stored in p->rc and this routine returns SQLITE_ERROR.
62878 **
62879 ** If the callback ever returns non-zero, then the program exits
62880 ** immediately.  There will be no error message but the p->rc field is
62881 ** set to SQLITE_ABORT and this routine will return SQLITE_ERROR.
62882 **
62883 ** A memory allocation error causes p->rc to be set to SQLITE_NOMEM and this
62884 ** routine to return SQLITE_ERROR.
62885 **
62886 ** Other fatal errors return SQLITE_ERROR.
62887 **
62888 ** After this routine has finished, sqlite3VdbeFinalize() should be
62889 ** used to clean up the mess that was left behind.
62890 */
62891 SQLITE_PRIVATE int sqlite3VdbeExec(
62892   Vdbe *p                    /* The VDBE */
62893 ){
62894   int pc=0;                  /* The program counter */
62895   Op *aOp = p->aOp;          /* Copy of p->aOp */
62896   Op *pOp;                   /* Current operation */
62897   int rc = SQLITE_OK;        /* Value to return */
62898   sqlite3 *db = p->db;       /* The database */
62899   u8 resetSchemaOnFault = 0; /* Reset schema after an error if positive */
62900   u8 encoding = ENC(db);     /* The database encoding */
62901 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
62902   int checkProgress;         /* True if progress callbacks are enabled */
62903   int nProgressOps = 0;      /* Opcodes executed since progress callback. */
62904 #endif
62905   Mem *aMem = p->aMem;       /* Copy of p->aMem */
62906   Mem *pIn1 = 0;             /* 1st input operand */
62907   Mem *pIn2 = 0;             /* 2nd input operand */
62908   Mem *pIn3 = 0;             /* 3rd input operand */
62909   Mem *pOut = 0;             /* Output operand */
62910   int iCompare = 0;          /* Result of last OP_Compare operation */
62911   int *aPermute = 0;         /* Permutation of columns for OP_Compare */
62912   i64 lastRowid = db->lastRowid;  /* Saved value of the last insert ROWID */
62913 #ifdef VDBE_PROFILE
62914   u64 start;                 /* CPU clock count at start of opcode */
62915   int origPc;                /* Program counter at start of opcode */
62916 #endif
62917   /********************************************************************
62918   ** Automatically generated code
62919   **
62920   ** The following union is automatically generated by the
62921   ** vdbe-compress.tcl script.  The purpose of this union is to
62922   ** reduce the amount of stack space required by this function.
62923   ** See comments in the vdbe-compress.tcl script for details.
62924   */
62925   union vdbeExecUnion {
62926     struct OP_Yield_stack_vars {
62927       int pcDest;
62928     } aa;
62929     struct OP_Variable_stack_vars {
62930       Mem *pVar;       /* Value being transferred */
62931     } ab;
62932     struct OP_Move_stack_vars {
62933       char *zMalloc;   /* Holding variable for allocated memory */
62934       int n;           /* Number of registers left to copy */
62935       int p1;          /* Register to copy from */
62936       int p2;          /* Register to copy to */
62937     } ac;
62938     struct OP_ResultRow_stack_vars {
62939       Mem *pMem;
62940       int i;
62941     } ad;
62942     struct OP_Concat_stack_vars {
62943       i64 nByte;
62944     } ae;
62945     struct OP_Remainder_stack_vars {
62946       int flags;      /* Combined MEM_* flags from both inputs */
62947       i64 iA;         /* Integer value of left operand */
62948       i64 iB;         /* Integer value of right operand */
62949       double rA;      /* Real value of left operand */
62950       double rB;      /* Real value of right operand */
62951     } af;
62952     struct OP_Function_stack_vars {
62953       int i;
62954       Mem *pArg;
62955       sqlite3_context ctx;
62956       sqlite3_value **apVal;
62957       int n;
62958     } ag;
62959     struct OP_ShiftRight_stack_vars {
62960       i64 iA;
62961       u64 uA;
62962       i64 iB;
62963       u8 op;
62964     } ah;
62965     struct OP_Ge_stack_vars {
62966       int res;            /* Result of the comparison of pIn1 against pIn3 */
62967       char affinity;      /* Affinity to use for comparison */
62968       u16 flags1;         /* Copy of initial value of pIn1->flags */
62969       u16 flags3;         /* Copy of initial value of pIn3->flags */
62970     } ai;
62971     struct OP_Compare_stack_vars {
62972       int n;
62973       int i;
62974       int p1;
62975       int p2;
62976       const KeyInfo *pKeyInfo;
62977       int idx;
62978       CollSeq *pColl;    /* Collating sequence to use on this term */
62979       int bRev;          /* True for DESCENDING sort order */
62980     } aj;
62981     struct OP_Or_stack_vars {
62982       int v1;    /* Left operand:  0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
62983       int v2;    /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
62984     } ak;
62985     struct OP_IfNot_stack_vars {
62986       int c;
62987     } al;
62988     struct OP_Column_stack_vars {
62989       u32 payloadSize;   /* Number of bytes in the record */
62990       i64 payloadSize64; /* Number of bytes in the record */
62991       int p1;            /* P1 value of the opcode */
62992       int p2;            /* column number to retrieve */
62993       VdbeCursor *pC;    /* The VDBE cursor */
62994       char *zRec;        /* Pointer to complete record-data */
62995       BtCursor *pCrsr;   /* The BTree cursor */
62996       u32 *aType;        /* aType[i] holds the numeric type of the i-th column */
62997       u32 *aOffset;      /* aOffset[i] is offset to start of data for i-th column */
62998       int nField;        /* number of fields in the record */
62999       int len;           /* The length of the serialized data for the column */
63000       int i;             /* Loop counter */
63001       char *zData;       /* Part of the record being decoded */
63002       Mem *pDest;        /* Where to write the extracted value */
63003       Mem sMem;          /* For storing the record being decoded */
63004       u8 *zIdx;          /* Index into header */
63005       u8 *zEndHdr;       /* Pointer to first byte after the header */
63006       u32 offset;        /* Offset into the data */
63007       u32 szField;       /* Number of bytes in the content of a field */
63008       int szHdr;         /* Size of the header size field at start of record */
63009       int avail;         /* Number of bytes of available data */
63010       Mem *pReg;         /* PseudoTable input register */
63011     } am;
63012     struct OP_Affinity_stack_vars {
63013       const char *zAffinity;   /* The affinity to be applied */
63014       char cAff;               /* A single character of affinity */
63015     } an;
63016     struct OP_MakeRecord_stack_vars {
63017       u8 *zNewRecord;        /* A buffer to hold the data for the new record */
63018       Mem *pRec;             /* The new record */
63019       u64 nData;             /* Number of bytes of data space */
63020       int nHdr;              /* Number of bytes of header space */
63021       i64 nByte;             /* Data space required for this record */
63022       int nZero;             /* Number of zero bytes at the end of the record */
63023       int nVarint;           /* Number of bytes in a varint */
63024       u32 serial_type;       /* Type field */
63025       Mem *pData0;           /* First field to be combined into the record */
63026       Mem *pLast;            /* Last field of the record */
63027       int nField;            /* Number of fields in the record */
63028       char *zAffinity;       /* The affinity string for the record */
63029       int file_format;       /* File format to use for encoding */
63030       int i;                 /* Space used in zNewRecord[] */
63031       int len;               /* Length of a field */
63032     } ao;
63033     struct OP_Count_stack_vars {
63034       i64 nEntry;
63035       BtCursor *pCrsr;
63036     } ap;
63037     struct OP_Savepoint_stack_vars {
63038       int p1;                         /* Value of P1 operand */
63039       char *zName;                    /* Name of savepoint */
63040       int nName;
63041       Savepoint *pNew;
63042       Savepoint *pSavepoint;
63043       Savepoint *pTmp;
63044       int iSavepoint;
63045       int ii;
63046     } aq;
63047     struct OP_AutoCommit_stack_vars {
63048       int desiredAutoCommit;
63049       int iRollback;
63050       int turnOnAC;
63051     } ar;
63052     struct OP_Transaction_stack_vars {
63053       Btree *pBt;
63054     } as;
63055     struct OP_ReadCookie_stack_vars {
63056       int iMeta;
63057       int iDb;
63058       int iCookie;
63059     } at;
63060     struct OP_SetCookie_stack_vars {
63061       Db *pDb;
63062     } au;
63063     struct OP_VerifyCookie_stack_vars {
63064       int iMeta;
63065       int iGen;
63066       Btree *pBt;
63067     } av;
63068     struct OP_OpenWrite_stack_vars {
63069       int nField;
63070       KeyInfo *pKeyInfo;
63071       int p2;
63072       int iDb;
63073       int wrFlag;
63074       Btree *pX;
63075       VdbeCursor *pCur;
63076       Db *pDb;
63077     } aw;
63078     struct OP_OpenEphemeral_stack_vars {
63079       VdbeCursor *pCx;
63080     } ax;
63081     struct OP_OpenPseudo_stack_vars {
63082       VdbeCursor *pCx;
63083     } ay;
63084     struct OP_SeekGt_stack_vars {
63085       int res;
63086       int oc;
63087       VdbeCursor *pC;
63088       UnpackedRecord r;
63089       int nField;
63090       i64 iKey;      /* The rowid we are to seek to */
63091     } az;
63092     struct OP_Seek_stack_vars {
63093       VdbeCursor *pC;
63094     } ba;
63095     struct OP_Found_stack_vars {
63096       int alreadyExists;
63097       VdbeCursor *pC;
63098       int res;
63099       UnpackedRecord *pIdxKey;
63100       UnpackedRecord r;
63101       char aTempRec[ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*3 + 7];
63102     } bb;
63103     struct OP_IsUnique_stack_vars {
63104       u16 ii;
63105       VdbeCursor *pCx;
63106       BtCursor *pCrsr;
63107       u16 nField;
63108       Mem *aMx;
63109       UnpackedRecord r;                  /* B-Tree index search key */
63110       i64 R;                             /* Rowid stored in register P3 */
63111     } bc;
63112     struct OP_NotExists_stack_vars {
63113       VdbeCursor *pC;
63114       BtCursor *pCrsr;
63115       int res;
63116       u64 iKey;
63117     } bd;
63118     struct OP_NewRowid_stack_vars {
63119       i64 v;                 /* The new rowid */
63120       VdbeCursor *pC;        /* Cursor of table to get the new rowid */
63121       int res;               /* Result of an sqlite3BtreeLast() */
63122       int cnt;               /* Counter to limit the number of searches */
63123       Mem *pMem;             /* Register holding largest rowid for AUTOINCREMENT */
63124       VdbeFrame *pFrame;     /* Root frame of VDBE */
63125     } be;
63126     struct OP_InsertInt_stack_vars {
63127       Mem *pData;       /* MEM cell holding data for the record to be inserted */
63128       Mem *pKey;        /* MEM cell holding key  for the record */
63129       i64 iKey;         /* The integer ROWID or key for the record to be inserted */
63130       VdbeCursor *pC;   /* Cursor to table into which insert is written */
63131       int nZero;        /* Number of zero-bytes to append */
63132       int seekResult;   /* Result of prior seek or 0 if no USESEEKRESULT flag */
63133       const char *zDb;  /* database name - used by the update hook */
63134       const char *zTbl; /* Table name - used by the opdate hook */
63135       int op;           /* Opcode for update hook: SQLITE_UPDATE or SQLITE_INSERT */
63136     } bf;
63137     struct OP_Delete_stack_vars {
63138       i64 iKey;
63139       VdbeCursor *pC;
63140     } bg;
63141     struct OP_RowData_stack_vars {
63142       VdbeCursor *pC;
63143       BtCursor *pCrsr;
63144       u32 n;
63145       i64 n64;
63146     } bh;
63147     struct OP_Rowid_stack_vars {
63148       VdbeCursor *pC;
63149       i64 v;
63150       sqlite3_vtab *pVtab;
63151       const sqlite3_module *pModule;
63152     } bi;
63153     struct OP_NullRow_stack_vars {
63154       VdbeCursor *pC;
63155     } bj;
63156     struct OP_Last_stack_vars {
63157       VdbeCursor *pC;
63158       BtCursor *pCrsr;
63159       int res;
63160     } bk;
63161     struct OP_Rewind_stack_vars {
63162       VdbeCursor *pC;
63163       BtCursor *pCrsr;
63164       int res;
63165     } bl;
63166     struct OP_Next_stack_vars {
63167       VdbeCursor *pC;
63168       BtCursor *pCrsr;
63169       int res;
63170     } bm;
63171     struct OP_IdxInsert_stack_vars {
63172       VdbeCursor *pC;
63173       BtCursor *pCrsr;
63174       int nKey;
63175       const char *zKey;
63176     } bn;
63177     struct OP_IdxDelete_stack_vars {
63178       VdbeCursor *pC;
63179       BtCursor *pCrsr;
63180       int res;
63181       UnpackedRecord r;
63182     } bo;
63183     struct OP_IdxRowid_stack_vars {
63184       BtCursor *pCrsr;
63185       VdbeCursor *pC;
63186       i64 rowid;
63187     } bp;
63188     struct OP_IdxGE_stack_vars {
63189       VdbeCursor *pC;
63190       int res;
63191       UnpackedRecord r;
63192     } bq;
63193     struct OP_Destroy_stack_vars {
63194       int iMoved;
63195       int iCnt;
63196       Vdbe *pVdbe;
63197       int iDb;
63198     } br;
63199     struct OP_Clear_stack_vars {
63200       int nChange;
63201     } bs;
63202     struct OP_CreateTable_stack_vars {
63203       int pgno;
63204       int flags;
63205       Db *pDb;
63206     } bt;
63207     struct OP_ParseSchema_stack_vars {
63208       int iDb;
63209       const char *zMaster;
63210       char *zSql;
63211       InitData initData;
63212     } bu;
63213     struct OP_IntegrityCk_stack_vars {
63214       int nRoot;      /* Number of tables to check.  (Number of root pages.) */
63215       int *aRoot;     /* Array of rootpage numbers for tables to be checked */
63216       int j;          /* Loop counter */
63217       int nErr;       /* Number of errors reported */
63218       char *z;        /* Text of the error report */
63219       Mem *pnErr;     /* Register keeping track of errors remaining */
63220     } bv;
63221     struct OP_RowSetRead_stack_vars {
63222       i64 val;
63223     } bw;
63224     struct OP_RowSetTest_stack_vars {
63225       int iSet;
63226       int exists;
63227     } bx;
63228     struct OP_Program_stack_vars {
63229       int nMem;               /* Number of memory registers for sub-program */
63230       int nByte;              /* Bytes of runtime space required for sub-program */
63231       Mem *pRt;               /* Register to allocate runtime space */
63232       Mem *pMem;              /* Used to iterate through memory cells */
63233       Mem *pEnd;              /* Last memory cell in new array */
63234       VdbeFrame *pFrame;      /* New vdbe frame to execute in */
63235       SubProgram *pProgram;   /* Sub-program to execute */
63236       void *t;                /* Token identifying trigger */
63237     } by;
63238     struct OP_Param_stack_vars {
63239       VdbeFrame *pFrame;
63240       Mem *pIn;
63241     } bz;
63242     struct OP_MemMax_stack_vars {
63243       Mem *pIn1;
63244       VdbeFrame *pFrame;
63245     } ca;
63246     struct OP_AggStep_stack_vars {
63247       int n;
63248       int i;
63249       Mem *pMem;
63250       Mem *pRec;
63251       sqlite3_context ctx;
63252       sqlite3_value **apVal;
63253     } cb;
63254     struct OP_AggFinal_stack_vars {
63255       Mem *pMem;
63256     } cc;
63257     struct OP_Checkpoint_stack_vars {
63258       int i;                          /* Loop counter */
63259       int aRes[3];                    /* Results */
63260       Mem *pMem;                      /* Write results here */
63261     } cd;
63262     struct OP_JournalMode_stack_vars {
63263       Btree *pBt;                     /* Btree to change journal mode of */
63264       Pager *pPager;                  /* Pager associated with pBt */
63265       int eNew;                       /* New journal mode */
63266       int eOld;                       /* The old journal mode */
63267       const char *zFilename;          /* Name of database file for pPager */
63268     } ce;
63269     struct OP_IncrVacuum_stack_vars {
63270       Btree *pBt;
63271     } cf;
63272     struct OP_VBegin_stack_vars {
63273       VTable *pVTab;
63274     } cg;
63275     struct OP_VOpen_stack_vars {
63276       VdbeCursor *pCur;
63277       sqlite3_vtab_cursor *pVtabCursor;
63278       sqlite3_vtab *pVtab;
63279       sqlite3_module *pModule;
63280     } ch;
63281     struct OP_VFilter_stack_vars {
63282       int nArg;
63283       int iQuery;
63284       const sqlite3_module *pModule;
63285       Mem *pQuery;
63286       Mem *pArgc;
63287       sqlite3_vtab_cursor *pVtabCursor;
63288       sqlite3_vtab *pVtab;
63289       VdbeCursor *pCur;
63290       int res;
63291       int i;
63292       Mem **apArg;
63293     } ci;
63294     struct OP_VColumn_stack_vars {
63295       sqlite3_vtab *pVtab;
63296       const sqlite3_module *pModule;
63297       Mem *pDest;
63298       sqlite3_context sContext;
63299     } cj;
63300     struct OP_VNext_stack_vars {
63301       sqlite3_vtab *pVtab;
63302       const sqlite3_module *pModule;
63303       int res;
63304       VdbeCursor *pCur;
63305     } ck;
63306     struct OP_VRename_stack_vars {
63307       sqlite3_vtab *pVtab;
63308       Mem *pName;
63309     } cl;
63310     struct OP_VUpdate_stack_vars {
63311       sqlite3_vtab *pVtab;
63312       sqlite3_module *pModule;
63313       int nArg;
63314       int i;
63315       sqlite_int64 rowid;
63316       Mem **apArg;
63317       Mem *pX;
63318     } cm;
63319     struct OP_Trace_stack_vars {
63320       char *zTrace;
63321       char *z;
63322     } cn;
63323   } u;
63324   /* End automatically generated code
63325   ********************************************************************/
63326 
63327   assert( p->magic==VDBE_MAGIC_RUN );  /* sqlite3_step() verifies this */
63328   sqlite3VdbeEnter(p);
63329   if( p->rc==SQLITE_NOMEM ){
63330     /* This happens if a malloc() inside a call to sqlite3_column_text() or
63331     ** sqlite3_column_text16() failed.  */
63332     goto no_mem;
63333   }
63334   assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY );
63335   p->rc = SQLITE_OK;
63336   assert( p->explain==0 );
63337   p->pResultSet = 0;
63338   db->busyHandler.nBusy = 0;
63339   CHECK_FOR_INTERRUPT;
63340   sqlite3VdbeIOTraceSql(p);
63341 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
63342   checkProgress = db->xProgress!=0;
63343 #endif
63344 #ifdef SQLITE_DEBUG
63345   sqlite3BeginBenignMalloc();
63346   if( p->pc==0  && (p->db->flags & SQLITE_VdbeListing)!=0 ){
63347     int i;
63348     printf("VDBE Program Listing:\n");
63349     sqlite3VdbePrintSql(p);
63350     for(i=0; i<p->nOp; i++){
63351       sqlite3VdbePrintOp(stdout, i, &aOp[i]);
63352     }
63353   }
63354   sqlite3EndBenignMalloc();
63355 #endif
63356   for(pc=p->pc; rc==SQLITE_OK; pc++){
63357     assert( pc>=0 && pc<p->nOp );
63358     if( db->mallocFailed ) goto no_mem;
63359 #ifdef VDBE_PROFILE
63360     origPc = pc;
63361     start = sqlite3Hwtime();
63362 #endif
63363     pOp = &aOp[pc];
63364 
63365     /* Only allow tracing if SQLITE_DEBUG is defined.
63366     */
63367 #ifdef SQLITE_DEBUG
63368     if( p->trace ){
63369       if( pc==0 ){
63370         printf("VDBE Execution Trace:\n");
63371         sqlite3VdbePrintSql(p);
63372       }
63373       sqlite3VdbePrintOp(p->trace, pc, pOp);
63374     }
63375 #endif
63376 
63377 
63378     /* Check to see if we need to simulate an interrupt.  This only happens
63379     ** if we have a special test build.
63380     */
63381 #ifdef SQLITE_TEST
63382     if( sqlite3_interrupt_count>0 ){
63383       sqlite3_interrupt_count--;
63384       if( sqlite3_interrupt_count==0 ){
63385         sqlite3_interrupt(db);
63386       }
63387     }
63388 #endif
63389 
63390 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
63391     /* Call the progress callback if it is configured and the required number
63392     ** of VDBE ops have been executed (either since this invocation of
63393     ** sqlite3VdbeExec() or since last time the progress callback was called).
63394     ** If the progress callback returns non-zero, exit the virtual machine with
63395     ** a return code SQLITE_ABORT.
63396     */
63397     if( checkProgress ){
63398       if( db->nProgressOps==nProgressOps ){
63399         int prc;
63400         prc = db->xProgress(db->pProgressArg);
63401         if( prc!=0 ){
63402           rc = SQLITE_INTERRUPT;
63403           goto vdbe_error_halt;
63404         }
63405         nProgressOps = 0;
63406       }
63407       nProgressOps++;
63408     }
63409 #endif
63410 
63411     /* On any opcode with the "out2-prerelase" tag, free any
63412     ** external allocations out of mem[p2] and set mem[p2] to be
63413     ** an undefined integer.  Opcodes will either fill in the integer
63414     ** value or convert mem[p2] to a different type.
63415     */
63416     assert( pOp->opflags==sqlite3OpcodeProperty[pOp->opcode] );
63417     if( pOp->opflags & OPFLG_OUT2_PRERELEASE ){
63418       assert( pOp->p2>0 );
63419       assert( pOp->p2<=p->nMem );
63420       pOut = &aMem[pOp->p2];
63421       memAboutToChange(p, pOut);
63422       sqlite3VdbeMemReleaseExternal(pOut);
63423       pOut->flags = MEM_Int;
63424     }
63425 
63426     /* Sanity checking on other operands */
63427 #ifdef SQLITE_DEBUG
63428     if( (pOp->opflags & OPFLG_IN1)!=0 ){
63429       assert( pOp->p1>0 );
63430       assert( pOp->p1<=p->nMem );
63431       assert( memIsValid(&aMem[pOp->p1]) );
63432       REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]);
63433     }
63434     if( (pOp->opflags & OPFLG_IN2)!=0 ){
63435       assert( pOp->p2>0 );
63436       assert( pOp->p2<=p->nMem );
63437       assert( memIsValid(&aMem[pOp->p2]) );
63438       REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]);
63439     }
63440     if( (pOp->opflags & OPFLG_IN3)!=0 ){
63441       assert( pOp->p3>0 );
63442       assert( pOp->p3<=p->nMem );
63443       assert( memIsValid(&aMem[pOp->p3]) );
63444       REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]);
63445     }
63446     if( (pOp->opflags & OPFLG_OUT2)!=0 ){
63447       assert( pOp->p2>0 );
63448       assert( pOp->p2<=p->nMem );
63449       memAboutToChange(p, &aMem[pOp->p2]);
63450     }
63451     if( (pOp->opflags & OPFLG_OUT3)!=0 ){
63452       assert( pOp->p3>0 );
63453       assert( pOp->p3<=p->nMem );
63454       memAboutToChange(p, &aMem[pOp->p3]);
63455     }
63456 #endif
63457 
63458     switch( pOp->opcode ){
63459 
63460 /*****************************************************************************
63461 ** What follows is a massive switch statement where each case implements a
63462 ** separate instruction in the virtual machine.  If we follow the usual
63463 ** indentation conventions, each case should be indented by 6 spaces.  But
63464 ** that is a lot of wasted space on the left margin.  So the code within
63465 ** the switch statement will break with convention and be flush-left. Another
63466 ** big comment (similar to this one) will mark the point in the code where
63467 ** we transition back to normal indentation.
63468 **
63469 ** The formatting of each case is important.  The makefile for SQLite
63470 ** generates two C files "opcodes.h" and "opcodes.c" by scanning this
63471 ** file looking for lines that begin with "case OP_".  The opcodes.h files
63472 ** will be filled with #defines that give unique integer values to each
63473 ** opcode and the opcodes.c file is filled with an array of strings where
63474 ** each string is the symbolic name for the corresponding opcode.  If the
63475 ** case statement is followed by a comment of the form "/# same as ... #/"
63476 ** that comment is used to determine the particular value of the opcode.
63477 **
63478 ** Other keywords in the comment that follows each case are used to
63479 ** construct the OPFLG_INITIALIZER value that initializes opcodeProperty[].
63480 ** Keywords include: in1, in2, in3, out2_prerelease, out2, out3.  See
63481 ** the mkopcodeh.awk script for additional information.
63482 **
63483 ** Documentation about VDBE opcodes is generated by scanning this file
63484 ** for lines of that contain "Opcode:".  That line and all subsequent
63485 ** comment lines are used in the generation of the opcode.html documentation
63486 ** file.
63487 **
63488 ** SUMMARY:
63489 **
63490 **     Formatting is important to scripts that scan this file.
63491 **     Do not deviate from the formatting style currently in use.
63492 **
63493 *****************************************************************************/
63494 
63495 /* Opcode:  Goto * P2 * * *
63496 **
63497 ** An unconditional jump to address P2.
63498 ** The next instruction executed will be
63499 ** the one at index P2 from the beginning of
63500 ** the program.
63501 */
63502 case OP_Goto: {             /* jump */
63503   CHECK_FOR_INTERRUPT;
63504   pc = pOp->p2 - 1;
63505   break;
63506 }
63507 
63508 /* Opcode:  Gosub P1 P2 * * *
63509 **
63510 ** Write the current address onto register P1
63511 ** and then jump to address P2.
63512 */
63513 case OP_Gosub: {            /* jump, in1 */
63514   pIn1 = &aMem[pOp->p1];
63515   assert( (pIn1->flags & MEM_Dyn)==0 );
63516   memAboutToChange(p, pIn1);
63517   pIn1->flags = MEM_Int;
63518   pIn1->u.i = pc;
63519   REGISTER_TRACE(pOp->p1, pIn1);
63520   pc = pOp->p2 - 1;
63521   break;
63522 }
63523 
63524 /* Opcode:  Return P1 * * * *
63525 **
63526 ** Jump to the next instruction after the address in register P1.
63527 */
63528 case OP_Return: {           /* in1 */
63529   pIn1 = &aMem[pOp->p1];
63530   assert( pIn1->flags & MEM_Int );
63531   pc = (int)pIn1->u.i;
63532   break;
63533 }
63534 
63535 /* Opcode:  Yield P1 * * * *
63536 **
63537 ** Swap the program counter with the value in register P1.
63538 */
63539 case OP_Yield: {            /* in1 */
63540 #if 0  /* local variables moved into u.aa */
63541   int pcDest;
63542 #endif /* local variables moved into u.aa */
63543   pIn1 = &aMem[pOp->p1];
63544   assert( (pIn1->flags & MEM_Dyn)==0 );
63545   pIn1->flags = MEM_Int;
63546   u.aa.pcDest = (int)pIn1->u.i;
63547   pIn1->u.i = pc;
63548   REGISTER_TRACE(pOp->p1, pIn1);
63549   pc = u.aa.pcDest;
63550   break;
63551 }
63552 
63553 /* Opcode:  HaltIfNull  P1 P2 P3 P4 *
63554 **
63555 ** Check the value in register P3.  If it is NULL then Halt using
63556 ** parameter P1, P2, and P4 as if this were a Halt instruction.  If the
63557 ** value in register P3 is not NULL, then this routine is a no-op.
63558 */
63559 case OP_HaltIfNull: {      /* in3 */
63560   pIn3 = &aMem[pOp->p3];
63561   if( (pIn3->flags & MEM_Null)==0 ) break;
63562   /* Fall through into OP_Halt */
63563 }
63564 
63565 /* Opcode:  Halt P1 P2 * P4 *
63566 **
63567 ** Exit immediately.  All open cursors, etc are closed
63568 ** automatically.
63569 **
63570 ** P1 is the result code returned by sqlite3_exec(), sqlite3_reset(),
63571 ** or sqlite3_finalize().  For a normal halt, this should be SQLITE_OK (0).
63572 ** For errors, it can be some other value.  If P1!=0 then P2 will determine
63573 ** whether or not to rollback the current transaction.  Do not rollback
63574 ** if P2==OE_Fail. Do the rollback if P2==OE_Rollback.  If P2==OE_Abort,
63575 ** then back out all changes that have occurred during this execution of the
63576 ** VDBE, but do not rollback the transaction.
63577 **
63578 ** If P4 is not null then it is an error message string.
63579 **
63580 ** There is an implied "Halt 0 0 0" instruction inserted at the very end of
63581 ** every program.  So a jump past the last instruction of the program
63582 ** is the same as executing Halt.
63583 */
63584 case OP_Halt: {
63585   if( pOp->p1==SQLITE_OK && p->pFrame ){
63586     /* Halt the sub-program. Return control to the parent frame. */
63587     VdbeFrame *pFrame = p->pFrame;
63588     p->pFrame = pFrame->pParent;
63589     p->nFrame--;
63590     sqlite3VdbeSetChanges(db, p->nChange);
63591     pc = sqlite3VdbeFrameRestore(pFrame);
63592     lastRowid = db->lastRowid;
63593     if( pOp->p2==OE_Ignore ){
63594       /* Instruction pc is the OP_Program that invoked the sub-program
63595       ** currently being halted. If the p2 instruction of this OP_Halt
63596       ** instruction is set to OE_Ignore, then the sub-program is throwing
63597       ** an IGNORE exception. In this case jump to the address specified
63598       ** as the p2 of the calling OP_Program.  */
63599       pc = p->aOp[pc].p2-1;
63600     }
63601     aOp = p->aOp;
63602     aMem = p->aMem;
63603     break;
63604   }
63605 
63606   p->rc = pOp->p1;
63607   p->errorAction = (u8)pOp->p2;
63608   p->pc = pc;
63609   if( pOp->p4.z ){
63610     assert( p->rc!=SQLITE_OK );
63611     sqlite3SetString(&p->zErrMsg, db, "%s", pOp->p4.z);
63612     testcase( sqlite3GlobalConfig.xLog!=0 );
63613     sqlite3_log(pOp->p1, "abort at %d in [%s]: %s", pc, p->zSql, pOp->p4.z);
63614   }else if( p->rc ){
63615     testcase( sqlite3GlobalConfig.xLog!=0 );
63616     sqlite3_log(pOp->p1, "constraint failed at %d in [%s]", pc, p->zSql);
63617   }
63618   rc = sqlite3VdbeHalt(p);
63619   assert( rc==SQLITE_BUSY || rc==SQLITE_OK || rc==SQLITE_ERROR );
63620   if( rc==SQLITE_BUSY ){
63621     p->rc = rc = SQLITE_BUSY;
63622   }else{
63623     assert( rc==SQLITE_OK || p->rc==SQLITE_CONSTRAINT );
63624     assert( rc==SQLITE_OK || db->nDeferredCons>0 );
63625     rc = p->rc ? SQLITE_ERROR : SQLITE_DONE;
63626   }
63627   goto vdbe_return;
63628 }
63629 
63630 /* Opcode: Integer P1 P2 * * *
63631 **
63632 ** The 32-bit integer value P1 is written into register P2.
63633 */
63634 case OP_Integer: {         /* out2-prerelease */
63635   pOut->u.i = pOp->p1;
63636   break;
63637 }
63638 
63639 /* Opcode: Int64 * P2 * P4 *
63640 **
63641 ** P4 is a pointer to a 64-bit integer value.
63642 ** Write that value into register P2.
63643 */
63644 case OP_Int64: {           /* out2-prerelease */
63645   assert( pOp->p4.pI64!=0 );
63646   pOut->u.i = *pOp->p4.pI64;
63647   break;
63648 }
63649 
63650 #ifndef SQLITE_OMIT_FLOATING_POINT
63651 /* Opcode: Real * P2 * P4 *
63652 **
63653 ** P4 is a pointer to a 64-bit floating point value.
63654 ** Write that value into register P2.
63655 */
63656 case OP_Real: {            /* same as TK_FLOAT, out2-prerelease */
63657   pOut->flags = MEM_Real;
63658   assert( !sqlite3IsNaN(*pOp->p4.pReal) );
63659   pOut->r = *pOp->p4.pReal;
63660   break;
63661 }
63662 #endif
63663 
63664 /* Opcode: String8 * P2 * P4 *
63665 **
63666 ** P4 points to a nul terminated UTF-8 string. This opcode is transformed
63667 ** into an OP_String before it is executed for the first time.
63668 */
63669 case OP_String8: {         /* same as TK_STRING, out2-prerelease */
63670   assert( pOp->p4.z!=0 );
63671   pOp->opcode = OP_String;
63672   pOp->p1 = sqlite3Strlen30(pOp->p4.z);
63673 
63674 #ifndef SQLITE_OMIT_UTF16
63675   if( encoding!=SQLITE_UTF8 ){
63676     rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC);
63677     if( rc==SQLITE_TOOBIG ) goto too_big;
63678     if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem;
63679     assert( pOut->zMalloc==pOut->z );
63680     assert( pOut->flags & MEM_Dyn );
63681     pOut->zMalloc = 0;
63682     pOut->flags |= MEM_Static;
63683     pOut->flags &= ~MEM_Dyn;
63684     if( pOp->p4type==P4_DYNAMIC ){
63685       sqlite3DbFree(db, pOp->p4.z);
63686     }
63687     pOp->p4type = P4_DYNAMIC;
63688     pOp->p4.z = pOut->z;
63689     pOp->p1 = pOut->n;
63690   }
63691 #endif
63692   if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
63693     goto too_big;
63694   }
63695   /* Fall through to the next case, OP_String */
63696 }
63697 
63698 /* Opcode: String P1 P2 * P4 *
63699 **
63700 ** The string value P4 of length P1 (bytes) is stored in register P2.
63701 */
63702 case OP_String: {          /* out2-prerelease */
63703   assert( pOp->p4.z!=0 );
63704   pOut->flags = MEM_Str|MEM_Static|MEM_Term;
63705   pOut->z = pOp->p4.z;
63706   pOut->n = pOp->p1;
63707   pOut->enc = encoding;
63708   UPDATE_MAX_BLOBSIZE(pOut);
63709   break;
63710 }
63711 
63712 /* Opcode: Null * P2 * * *
63713 **
63714 ** Write a NULL into register P2.
63715 */
63716 case OP_Null: {           /* out2-prerelease */
63717   pOut->flags = MEM_Null;
63718   break;
63719 }
63720 
63721 
63722 /* Opcode: Blob P1 P2 * P4
63723 **
63724 ** P4 points to a blob of data P1 bytes long.  Store this
63725 ** blob in register P2.
63726 */
63727 case OP_Blob: {                /* out2-prerelease */
63728   assert( pOp->p1 <= SQLITE_MAX_LENGTH );
63729   sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0);
63730   pOut->enc = encoding;
63731   UPDATE_MAX_BLOBSIZE(pOut);
63732   break;
63733 }
63734 
63735 /* Opcode: Variable P1 P2 * P4 *
63736 **
63737 ** Transfer the values of bound parameter P1 into register P2
63738 **
63739 ** If the parameter is named, then its name appears in P4 and P3==1.
63740 ** The P4 value is used by sqlite3_bind_parameter_name().
63741 */
63742 case OP_Variable: {            /* out2-prerelease */
63743 #if 0  /* local variables moved into u.ab */
63744   Mem *pVar;       /* Value being transferred */
63745 #endif /* local variables moved into u.ab */
63746 
63747   assert( pOp->p1>0 && pOp->p1<=p->nVar );
63748   assert( pOp->p4.z==0 || pOp->p4.z==p->azVar[pOp->p1-1] );
63749   u.ab.pVar = &p->aVar[pOp->p1 - 1];
63750   if( sqlite3VdbeMemTooBig(u.ab.pVar) ){
63751     goto too_big;
63752   }
63753   sqlite3VdbeMemShallowCopy(pOut, u.ab.pVar, MEM_Static);
63754   UPDATE_MAX_BLOBSIZE(pOut);
63755   break;
63756 }
63757 
63758 /* Opcode: Move P1 P2 P3 * *
63759 **
63760 ** Move the values in register P1..P1+P3-1 over into
63761 ** registers P2..P2+P3-1.  Registers P1..P1+P1-1 are
63762 ** left holding a NULL.  It is an error for register ranges
63763 ** P1..P1+P3-1 and P2..P2+P3-1 to overlap.
63764 */
63765 case OP_Move: {
63766 #if 0  /* local variables moved into u.ac */
63767   char *zMalloc;   /* Holding variable for allocated memory */
63768   int n;           /* Number of registers left to copy */
63769   int p1;          /* Register to copy from */
63770   int p2;          /* Register to copy to */
63771 #endif /* local variables moved into u.ac */
63772 
63773   u.ac.n = pOp->p3;
63774   u.ac.p1 = pOp->p1;
63775   u.ac.p2 = pOp->p2;
63776   assert( u.ac.n>0 && u.ac.p1>0 && u.ac.p2>0 );
63777   assert( u.ac.p1+u.ac.n<=u.ac.p2 || u.ac.p2+u.ac.n<=u.ac.p1 );
63778 
63779   pIn1 = &aMem[u.ac.p1];
63780   pOut = &aMem[u.ac.p2];
63781   while( u.ac.n-- ){
63782     assert( pOut<=&aMem[p->nMem] );
63783     assert( pIn1<=&aMem[p->nMem] );
63784     assert( memIsValid(pIn1) );
63785     memAboutToChange(p, pOut);
63786     u.ac.zMalloc = pOut->zMalloc;
63787     pOut->zMalloc = 0;
63788     sqlite3VdbeMemMove(pOut, pIn1);
63789     pIn1->zMalloc = u.ac.zMalloc;
63790     REGISTER_TRACE(u.ac.p2++, pOut);
63791     pIn1++;
63792     pOut++;
63793   }
63794   break;
63795 }
63796 
63797 /* Opcode: Copy P1 P2 * * *
63798 **
63799 ** Make a copy of register P1 into register P2.
63800 **
63801 ** This instruction makes a deep copy of the value.  A duplicate
63802 ** is made of any string or blob constant.  See also OP_SCopy.
63803 */
63804 case OP_Copy: {             /* in1, out2 */
63805   pIn1 = &aMem[pOp->p1];
63806   pOut = &aMem[pOp->p2];
63807   assert( pOut!=pIn1 );
63808   sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
63809   Deephemeralize(pOut);
63810   REGISTER_TRACE(pOp->p2, pOut);
63811   break;
63812 }
63813 
63814 /* Opcode: SCopy P1 P2 * * *
63815 **
63816 ** Make a shallow copy of register P1 into register P2.
63817 **
63818 ** This instruction makes a shallow copy of the value.  If the value
63819 ** is a string or blob, then the copy is only a pointer to the
63820 ** original and hence if the original changes so will the copy.
63821 ** Worse, if the original is deallocated, the copy becomes invalid.
63822 ** Thus the program must guarantee that the original will not change
63823 ** during the lifetime of the copy.  Use OP_Copy to make a complete
63824 ** copy.
63825 */
63826 case OP_SCopy: {            /* in1, out2 */
63827   pIn1 = &aMem[pOp->p1];
63828   pOut = &aMem[pOp->p2];
63829   assert( pOut!=pIn1 );
63830   sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
63831 #ifdef SQLITE_DEBUG
63832   if( pOut->pScopyFrom==0 ) pOut->pScopyFrom = pIn1;
63833 #endif
63834   REGISTER_TRACE(pOp->p2, pOut);
63835   break;
63836 }
63837 
63838 /* Opcode: ResultRow P1 P2 * * *
63839 **
63840 ** The registers P1 through P1+P2-1 contain a single row of
63841 ** results. This opcode causes the sqlite3_step() call to terminate
63842 ** with an SQLITE_ROW return code and it sets up the sqlite3_stmt
63843 ** structure to provide access to the top P1 values as the result
63844 ** row.
63845 */
63846 case OP_ResultRow: {
63847 #if 0  /* local variables moved into u.ad */
63848   Mem *pMem;
63849   int i;
63850 #endif /* local variables moved into u.ad */
63851   assert( p->nResColumn==pOp->p2 );
63852   assert( pOp->p1>0 );
63853   assert( pOp->p1+pOp->p2<=p->nMem+1 );
63854 
63855   /* If this statement has violated immediate foreign key constraints, do
63856   ** not return the number of rows modified. And do not RELEASE the statement
63857   ** transaction. It needs to be rolled back.  */
63858   if( SQLITE_OK!=(rc = sqlite3VdbeCheckFk(p, 0)) ){
63859     assert( db->flags&SQLITE_CountRows );
63860     assert( p->usesStmtJournal );
63861     break;
63862   }
63863 
63864   /* If the SQLITE_CountRows flag is set in sqlite3.flags mask, then
63865   ** DML statements invoke this opcode to return the number of rows
63866   ** modified to the user. This is the only way that a VM that
63867   ** opens a statement transaction may invoke this opcode.
63868   **
63869   ** In case this is such a statement, close any statement transaction
63870   ** opened by this VM before returning control to the user. This is to
63871   ** ensure that statement-transactions are always nested, not overlapping.
63872   ** If the open statement-transaction is not closed here, then the user
63873   ** may step another VM that opens its own statement transaction. This
63874   ** may lead to overlapping statement transactions.
63875   **
63876   ** The statement transaction is never a top-level transaction.  Hence
63877   ** the RELEASE call below can never fail.
63878   */
63879   assert( p->iStatement==0 || db->flags&SQLITE_CountRows );
63880   rc = sqlite3VdbeCloseStatement(p, SAVEPOINT_RELEASE);
63881   if( NEVER(rc!=SQLITE_OK) ){
63882     break;
63883   }
63884 
63885   /* Invalidate all ephemeral cursor row caches */
63886   p->cacheCtr = (p->cacheCtr + 2)|1;
63887 
63888   /* Make sure the results of the current row are \000 terminated
63889   ** and have an assigned type.  The results are de-ephemeralized as
63890   ** as side effect.
63891   */
63892   u.ad.pMem = p->pResultSet = &aMem[pOp->p1];
63893   for(u.ad.i=0; u.ad.i<pOp->p2; u.ad.i++){
63894     assert( memIsValid(&u.ad.pMem[u.ad.i]) );
63895     Deephemeralize(&u.ad.pMem[u.ad.i]);
63896     assert( (u.ad.pMem[u.ad.i].flags & MEM_Ephem)==0
63897             || (u.ad.pMem[u.ad.i].flags & (MEM_Str|MEM_Blob))==0 );
63898     sqlite3VdbeMemNulTerminate(&u.ad.pMem[u.ad.i]);
63899     sqlite3VdbeMemStoreType(&u.ad.pMem[u.ad.i]);
63900     REGISTER_TRACE(pOp->p1+u.ad.i, &u.ad.pMem[u.ad.i]);
63901   }
63902   if( db->mallocFailed ) goto no_mem;
63903 
63904   /* Return SQLITE_ROW
63905   */
63906   p->pc = pc + 1;
63907   rc = SQLITE_ROW;
63908   goto vdbe_return;
63909 }
63910 
63911 /* Opcode: Concat P1 P2 P3 * *
63912 **
63913 ** Add the text in register P1 onto the end of the text in
63914 ** register P2 and store the result in register P3.
63915 ** If either the P1 or P2 text are NULL then store NULL in P3.
63916 **
63917 **   P3 = P2 || P1
63918 **
63919 ** It is illegal for P1 and P3 to be the same register. Sometimes,
63920 ** if P3 is the same register as P2, the implementation is able
63921 ** to avoid a memcpy().
63922 */
63923 case OP_Concat: {           /* same as TK_CONCAT, in1, in2, out3 */
63924 #if 0  /* local variables moved into u.ae */
63925   i64 nByte;
63926 #endif /* local variables moved into u.ae */
63927 
63928   pIn1 = &aMem[pOp->p1];
63929   pIn2 = &aMem[pOp->p2];
63930   pOut = &aMem[pOp->p3];
63931   assert( pIn1!=pOut );
63932   if( (pIn1->flags | pIn2->flags) & MEM_Null ){
63933     sqlite3VdbeMemSetNull(pOut);
63934     break;
63935   }
63936   if( ExpandBlob(pIn1) || ExpandBlob(pIn2) ) goto no_mem;
63937   Stringify(pIn1, encoding);
63938   Stringify(pIn2, encoding);
63939   u.ae.nByte = pIn1->n + pIn2->n;
63940   if( u.ae.nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
63941     goto too_big;
63942   }
63943   MemSetTypeFlag(pOut, MEM_Str);
63944   if( sqlite3VdbeMemGrow(pOut, (int)u.ae.nByte+2, pOut==pIn2) ){
63945     goto no_mem;
63946   }
63947   if( pOut!=pIn2 ){
63948     memcpy(pOut->z, pIn2->z, pIn2->n);
63949   }
63950   memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n);
63951   pOut->z[u.ae.nByte] = 0;
63952   pOut->z[u.ae.nByte+1] = 0;
63953   pOut->flags |= MEM_Term;
63954   pOut->n = (int)u.ae.nByte;
63955   pOut->enc = encoding;
63956   UPDATE_MAX_BLOBSIZE(pOut);
63957   break;
63958 }
63959 
63960 /* Opcode: Add P1 P2 P3 * *
63961 **
63962 ** Add the value in register P1 to the value in register P2
63963 ** and store the result in register P3.
63964 ** If either input is NULL, the result is NULL.
63965 */
63966 /* Opcode: Multiply P1 P2 P3 * *
63967 **
63968 **
63969 ** Multiply the value in register P1 by the value in register P2
63970 ** and store the result in register P3.
63971 ** If either input is NULL, the result is NULL.
63972 */
63973 /* Opcode: Subtract P1 P2 P3 * *
63974 **
63975 ** Subtract the value in register P1 from the value in register P2
63976 ** and store the result in register P3.
63977 ** If either input is NULL, the result is NULL.
63978 */
63979 /* Opcode: Divide P1 P2 P3 * *
63980 **
63981 ** Divide the value in register P1 by the value in register P2
63982 ** and store the result in register P3 (P3=P2/P1). If the value in
63983 ** register P1 is zero, then the result is NULL. If either input is
63984 ** NULL, the result is NULL.
63985 */
63986 /* Opcode: Remainder P1 P2 P3 * *
63987 **
63988 ** Compute the remainder after integer division of the value in
63989 ** register P1 by the value in register P2 and store the result in P3.
63990 ** If the value in register P2 is zero the result is NULL.
63991 ** If either operand is NULL, the result is NULL.
63992 */
63993 case OP_Add:                   /* same as TK_PLUS, in1, in2, out3 */
63994 case OP_Subtract:              /* same as TK_MINUS, in1, in2, out3 */
63995 case OP_Multiply:              /* same as TK_STAR, in1, in2, out3 */
63996 case OP_Divide:                /* same as TK_SLASH, in1, in2, out3 */
63997 case OP_Remainder: {           /* same as TK_REM, in1, in2, out3 */
63998 #if 0  /* local variables moved into u.af */
63999   int flags;      /* Combined MEM_* flags from both inputs */
64000   i64 iA;         /* Integer value of left operand */
64001   i64 iB;         /* Integer value of right operand */
64002   double rA;      /* Real value of left operand */
64003   double rB;      /* Real value of right operand */
64004 #endif /* local variables moved into u.af */
64005 
64006   pIn1 = &aMem[pOp->p1];
64007   applyNumericAffinity(pIn1);
64008   pIn2 = &aMem[pOp->p2];
64009   applyNumericAffinity(pIn2);
64010   pOut = &aMem[pOp->p3];
64011   u.af.flags = pIn1->flags | pIn2->flags;
64012   if( (u.af.flags & MEM_Null)!=0 ) goto arithmetic_result_is_null;
64013   if( (pIn1->flags & pIn2->flags & MEM_Int)==MEM_Int ){
64014     u.af.iA = pIn1->u.i;
64015     u.af.iB = pIn2->u.i;
64016     switch( pOp->opcode ){
64017       case OP_Add:       if( sqlite3AddInt64(&u.af.iB,u.af.iA) ) goto fp_math;  break;
64018       case OP_Subtract:  if( sqlite3SubInt64(&u.af.iB,u.af.iA) ) goto fp_math;  break;
64019       case OP_Multiply:  if( sqlite3MulInt64(&u.af.iB,u.af.iA) ) goto fp_math;  break;
64020       case OP_Divide: {
64021         if( u.af.iA==0 ) goto arithmetic_result_is_null;
64022         if( u.af.iA==-1 && u.af.iB==SMALLEST_INT64 ) goto fp_math;
64023         u.af.iB /= u.af.iA;
64024         break;
64025       }
64026       default: {
64027         if( u.af.iA==0 ) goto arithmetic_result_is_null;
64028         if( u.af.iA==-1 ) u.af.iA = 1;
64029         u.af.iB %= u.af.iA;
64030         break;
64031       }
64032     }
64033     pOut->u.i = u.af.iB;
64034     MemSetTypeFlag(pOut, MEM_Int);
64035   }else{
64036 fp_math:
64037     u.af.rA = sqlite3VdbeRealValue(pIn1);
64038     u.af.rB = sqlite3VdbeRealValue(pIn2);
64039     switch( pOp->opcode ){
64040       case OP_Add:         u.af.rB += u.af.rA;       break;
64041       case OP_Subtract:    u.af.rB -= u.af.rA;       break;
64042       case OP_Multiply:    u.af.rB *= u.af.rA;       break;
64043       case OP_Divide: {
64044         /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
64045         if( u.af.rA==(double)0 ) goto arithmetic_result_is_null;
64046         u.af.rB /= u.af.rA;
64047         break;
64048       }
64049       default: {
64050         u.af.iA = (i64)u.af.rA;
64051         u.af.iB = (i64)u.af.rB;
64052         if( u.af.iA==0 ) goto arithmetic_result_is_null;
64053         if( u.af.iA==-1 ) u.af.iA = 1;
64054         u.af.rB = (double)(u.af.iB % u.af.iA);
64055         break;
64056       }
64057     }
64058 #ifdef SQLITE_OMIT_FLOATING_POINT
64059     pOut->u.i = u.af.rB;
64060     MemSetTypeFlag(pOut, MEM_Int);
64061 #else
64062     if( sqlite3IsNaN(u.af.rB) ){
64063       goto arithmetic_result_is_null;
64064     }
64065     pOut->r = u.af.rB;
64066     MemSetTypeFlag(pOut, MEM_Real);
64067     if( (u.af.flags & MEM_Real)==0 ){
64068       sqlite3VdbeIntegerAffinity(pOut);
64069     }
64070 #endif
64071   }
64072   break;
64073 
64074 arithmetic_result_is_null:
64075   sqlite3VdbeMemSetNull(pOut);
64076   break;
64077 }
64078 
64079 /* Opcode: CollSeq * * P4
64080 **
64081 ** P4 is a pointer to a CollSeq struct. If the next call to a user function
64082 ** or aggregate calls sqlite3GetFuncCollSeq(), this collation sequence will
64083 ** be returned. This is used by the built-in min(), max() and nullif()
64084 ** functions.
64085 **
64086 ** The interface used by the implementation of the aforementioned functions
64087 ** to retrieve the collation sequence set by this opcode is not available
64088 ** publicly, only to user functions defined in func.c.
64089 */
64090 case OP_CollSeq: {
64091   assert( pOp->p4type==P4_COLLSEQ );
64092   break;
64093 }
64094 
64095 /* Opcode: Function P1 P2 P3 P4 P5
64096 **
64097 ** Invoke a user function (P4 is a pointer to a Function structure that
64098 ** defines the function) with P5 arguments taken from register P2 and
64099 ** successors.  The result of the function is stored in register P3.
64100 ** Register P3 must not be one of the function inputs.
64101 **
64102 ** P1 is a 32-bit bitmask indicating whether or not each argument to the
64103 ** function was determined to be constant at compile time. If the first
64104 ** argument was constant then bit 0 of P1 is set. This is used to determine
64105 ** whether meta data associated with a user function argument using the
64106 ** sqlite3_set_auxdata() API may be safely retained until the next
64107 ** invocation of this opcode.
64108 **
64109 ** See also: AggStep and AggFinal
64110 */
64111 case OP_Function: {
64112 #if 0  /* local variables moved into u.ag */
64113   int i;
64114   Mem *pArg;
64115   sqlite3_context ctx;
64116   sqlite3_value **apVal;
64117   int n;
64118 #endif /* local variables moved into u.ag */
64119 
64120   u.ag.n = pOp->p5;
64121   u.ag.apVal = p->apArg;
64122   assert( u.ag.apVal || u.ag.n==0 );
64123   assert( pOp->p3>0 && pOp->p3<=p->nMem );
64124   pOut = &aMem[pOp->p3];
64125   memAboutToChange(p, pOut);
64126 
64127   assert( u.ag.n==0 || (pOp->p2>0 && pOp->p2+u.ag.n<=p->nMem+1) );
64128   assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+u.ag.n );
64129   u.ag.pArg = &aMem[pOp->p2];
64130   for(u.ag.i=0; u.ag.i<u.ag.n; u.ag.i++, u.ag.pArg++){
64131     assert( memIsValid(u.ag.pArg) );
64132     u.ag.apVal[u.ag.i] = u.ag.pArg;
64133     Deephemeralize(u.ag.pArg);
64134     sqlite3VdbeMemStoreType(u.ag.pArg);
64135     REGISTER_TRACE(pOp->p2+u.ag.i, u.ag.pArg);
64136   }
64137 
64138   assert( pOp->p4type==P4_FUNCDEF || pOp->p4type==P4_VDBEFUNC );
64139   if( pOp->p4type==P4_FUNCDEF ){
64140     u.ag.ctx.pFunc = pOp->p4.pFunc;
64141     u.ag.ctx.pVdbeFunc = 0;
64142   }else{
64143     u.ag.ctx.pVdbeFunc = (VdbeFunc*)pOp->p4.pVdbeFunc;
64144     u.ag.ctx.pFunc = u.ag.ctx.pVdbeFunc->pFunc;
64145   }
64146 
64147   u.ag.ctx.s.flags = MEM_Null;
64148   u.ag.ctx.s.db = db;
64149   u.ag.ctx.s.xDel = 0;
64150   u.ag.ctx.s.zMalloc = 0;
64151 
64152   /* The output cell may already have a buffer allocated. Move
64153   ** the pointer to u.ag.ctx.s so in case the user-function can use
64154   ** the already allocated buffer instead of allocating a new one.
64155   */
64156   sqlite3VdbeMemMove(&u.ag.ctx.s, pOut);
64157   MemSetTypeFlag(&u.ag.ctx.s, MEM_Null);
64158 
64159   u.ag.ctx.isError = 0;
64160   if( u.ag.ctx.pFunc->flags & SQLITE_FUNC_NEEDCOLL ){
64161     assert( pOp>aOp );
64162     assert( pOp[-1].p4type==P4_COLLSEQ );
64163     assert( pOp[-1].opcode==OP_CollSeq );
64164     u.ag.ctx.pColl = pOp[-1].p4.pColl;
64165   }
64166   db->lastRowid = lastRowid;
64167   (*u.ag.ctx.pFunc->xFunc)(&u.ag.ctx, u.ag.n, u.ag.apVal); /* IMP: R-24505-23230 */
64168   lastRowid = db->lastRowid;
64169 
64170   /* If any auxiliary data functions have been called by this user function,
64171   ** immediately call the destructor for any non-static values.
64172   */
64173   if( u.ag.ctx.pVdbeFunc ){
64174     sqlite3VdbeDeleteAuxData(u.ag.ctx.pVdbeFunc, pOp->p1);
64175     pOp->p4.pVdbeFunc = u.ag.ctx.pVdbeFunc;
64176     pOp->p4type = P4_VDBEFUNC;
64177   }
64178 
64179   if( db->mallocFailed ){
64180     /* Even though a malloc() has failed, the implementation of the
64181     ** user function may have called an sqlite3_result_XXX() function
64182     ** to return a value. The following call releases any resources
64183     ** associated with such a value.
64184     */
64185     sqlite3VdbeMemRelease(&u.ag.ctx.s);
64186     goto no_mem;
64187   }
64188 
64189   /* If the function returned an error, throw an exception */
64190   if( u.ag.ctx.isError ){
64191     sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&u.ag.ctx.s));
64192     rc = u.ag.ctx.isError;
64193   }
64194 
64195   /* Copy the result of the function into register P3 */
64196   sqlite3VdbeChangeEncoding(&u.ag.ctx.s, encoding);
64197   sqlite3VdbeMemMove(pOut, &u.ag.ctx.s);
64198   if( sqlite3VdbeMemTooBig(pOut) ){
64199     goto too_big;
64200   }
64201 
64202 #if 0
64203   /* The app-defined function has done something that as caused this
64204   ** statement to expire.  (Perhaps the function called sqlite3_exec()
64205   ** with a CREATE TABLE statement.)
64206   */
64207   if( p->expired ) rc = SQLITE_ABORT;
64208 #endif
64209 
64210   REGISTER_TRACE(pOp->p3, pOut);
64211   UPDATE_MAX_BLOBSIZE(pOut);
64212   break;
64213 }
64214 
64215 /* Opcode: BitAnd P1 P2 P3 * *
64216 **
64217 ** Take the bit-wise AND of the values in register P1 and P2 and
64218 ** store the result in register P3.
64219 ** If either input is NULL, the result is NULL.
64220 */
64221 /* Opcode: BitOr P1 P2 P3 * *
64222 **
64223 ** Take the bit-wise OR of the values in register P1 and P2 and
64224 ** store the result in register P3.
64225 ** If either input is NULL, the result is NULL.
64226 */
64227 /* Opcode: ShiftLeft P1 P2 P3 * *
64228 **
64229 ** Shift the integer value in register P2 to the left by the
64230 ** number of bits specified by the integer in register P1.
64231 ** Store the result in register P3.
64232 ** If either input is NULL, the result is NULL.
64233 */
64234 /* Opcode: ShiftRight P1 P2 P3 * *
64235 **
64236 ** Shift the integer value in register P2 to the right by the
64237 ** number of bits specified by the integer in register P1.
64238 ** Store the result in register P3.
64239 ** If either input is NULL, the result is NULL.
64240 */
64241 case OP_BitAnd:                 /* same as TK_BITAND, in1, in2, out3 */
64242 case OP_BitOr:                  /* same as TK_BITOR, in1, in2, out3 */
64243 case OP_ShiftLeft:              /* same as TK_LSHIFT, in1, in2, out3 */
64244 case OP_ShiftRight: {           /* same as TK_RSHIFT, in1, in2, out3 */
64245 #if 0  /* local variables moved into u.ah */
64246   i64 iA;
64247   u64 uA;
64248   i64 iB;
64249   u8 op;
64250 #endif /* local variables moved into u.ah */
64251 
64252   pIn1 = &aMem[pOp->p1];
64253   pIn2 = &aMem[pOp->p2];
64254   pOut = &aMem[pOp->p3];
64255   if( (pIn1->flags | pIn2->flags) & MEM_Null ){
64256     sqlite3VdbeMemSetNull(pOut);
64257     break;
64258   }
64259   u.ah.iA = sqlite3VdbeIntValue(pIn2);
64260   u.ah.iB = sqlite3VdbeIntValue(pIn1);
64261   u.ah.op = pOp->opcode;
64262   if( u.ah.op==OP_BitAnd ){
64263     u.ah.iA &= u.ah.iB;
64264   }else if( u.ah.op==OP_BitOr ){
64265     u.ah.iA |= u.ah.iB;
64266   }else if( u.ah.iB!=0 ){
64267     assert( u.ah.op==OP_ShiftRight || u.ah.op==OP_ShiftLeft );
64268 
64269     /* If shifting by a negative amount, shift in the other direction */
64270     if( u.ah.iB<0 ){
64271       assert( OP_ShiftRight==OP_ShiftLeft+1 );
64272       u.ah.op = 2*OP_ShiftLeft + 1 - u.ah.op;
64273       u.ah.iB = u.ah.iB>(-64) ? -u.ah.iB : 64;
64274     }
64275 
64276     if( u.ah.iB>=64 ){
64277       u.ah.iA = (u.ah.iA>=0 || u.ah.op==OP_ShiftLeft) ? 0 : -1;
64278     }else{
64279       memcpy(&u.ah.uA, &u.ah.iA, sizeof(u.ah.uA));
64280       if( u.ah.op==OP_ShiftLeft ){
64281         u.ah.uA <<= u.ah.iB;
64282       }else{
64283         u.ah.uA >>= u.ah.iB;
64284         /* Sign-extend on a right shift of a negative number */
64285         if( u.ah.iA<0 ) u.ah.uA |= ((((u64)0xffffffff)<<32)|0xffffffff) << (64-u.ah.iB);
64286       }
64287       memcpy(&u.ah.iA, &u.ah.uA, sizeof(u.ah.iA));
64288     }
64289   }
64290   pOut->u.i = u.ah.iA;
64291   MemSetTypeFlag(pOut, MEM_Int);
64292   break;
64293 }
64294 
64295 /* Opcode: AddImm  P1 P2 * * *
64296 **
64297 ** Add the constant P2 to the value in register P1.
64298 ** The result is always an integer.
64299 **
64300 ** To force any register to be an integer, just add 0.
64301 */
64302 case OP_AddImm: {            /* in1 */
64303   pIn1 = &aMem[pOp->p1];
64304   memAboutToChange(p, pIn1);
64305   sqlite3VdbeMemIntegerify(pIn1);
64306   pIn1->u.i += pOp->p2;
64307   break;
64308 }
64309 
64310 /* Opcode: MustBeInt P1 P2 * * *
64311 **
64312 ** Force the value in register P1 to be an integer.  If the value
64313 ** in P1 is not an integer and cannot be converted into an integer
64314 ** without data loss, then jump immediately to P2, or if P2==0
64315 ** raise an SQLITE_MISMATCH exception.
64316 */
64317 case OP_MustBeInt: {            /* jump, in1 */
64318   pIn1 = &aMem[pOp->p1];
64319   applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding);
64320   if( (pIn1->flags & MEM_Int)==0 ){
64321     if( pOp->p2==0 ){
64322       rc = SQLITE_MISMATCH;
64323       goto abort_due_to_error;
64324     }else{
64325       pc = pOp->p2 - 1;
64326     }
64327   }else{
64328     MemSetTypeFlag(pIn1, MEM_Int);
64329   }
64330   break;
64331 }
64332 
64333 #ifndef SQLITE_OMIT_FLOATING_POINT
64334 /* Opcode: RealAffinity P1 * * * *
64335 **
64336 ** If register P1 holds an integer convert it to a real value.
64337 **
64338 ** This opcode is used when extracting information from a column that
64339 ** has REAL affinity.  Such column values may still be stored as
64340 ** integers, for space efficiency, but after extraction we want them
64341 ** to have only a real value.
64342 */
64343 case OP_RealAffinity: {                  /* in1 */
64344   pIn1 = &aMem[pOp->p1];
64345   if( pIn1->flags & MEM_Int ){
64346     sqlite3VdbeMemRealify(pIn1);
64347   }
64348   break;
64349 }
64350 #endif
64351 
64352 #ifndef SQLITE_OMIT_CAST
64353 /* Opcode: ToText P1 * * * *
64354 **
64355 ** Force the value in register P1 to be text.
64356 ** If the value is numeric, convert it to a string using the
64357 ** equivalent of printf().  Blob values are unchanged and
64358 ** are afterwards simply interpreted as text.
64359 **
64360 ** A NULL value is not changed by this routine.  It remains NULL.
64361 */
64362 case OP_ToText: {                  /* same as TK_TO_TEXT, in1 */
64363   pIn1 = &aMem[pOp->p1];
64364   memAboutToChange(p, pIn1);
64365   if( pIn1->flags & MEM_Null ) break;
64366   assert( MEM_Str==(MEM_Blob>>3) );
64367   pIn1->flags |= (pIn1->flags&MEM_Blob)>>3;
64368   applyAffinity(pIn1, SQLITE_AFF_TEXT, encoding);
64369   rc = ExpandBlob(pIn1);
64370   assert( pIn1->flags & MEM_Str || db->mallocFailed );
64371   pIn1->flags &= ~(MEM_Int|MEM_Real|MEM_Blob|MEM_Zero);
64372   UPDATE_MAX_BLOBSIZE(pIn1);
64373   break;
64374 }
64375 
64376 /* Opcode: ToBlob P1 * * * *
64377 **
64378 ** Force the value in register P1 to be a BLOB.
64379 ** If the value is numeric, convert it to a string first.
64380 ** Strings are simply reinterpreted as blobs with no change
64381 ** to the underlying data.
64382 **
64383 ** A NULL value is not changed by this routine.  It remains NULL.
64384 */
64385 case OP_ToBlob: {                  /* same as TK_TO_BLOB, in1 */
64386   pIn1 = &aMem[pOp->p1];
64387   if( pIn1->flags & MEM_Null ) break;
64388   if( (pIn1->flags & MEM_Blob)==0 ){
64389     applyAffinity(pIn1, SQLITE_AFF_TEXT, encoding);
64390     assert( pIn1->flags & MEM_Str || db->mallocFailed );
64391     MemSetTypeFlag(pIn1, MEM_Blob);
64392   }else{
64393     pIn1->flags &= ~(MEM_TypeMask&~MEM_Blob);
64394   }
64395   UPDATE_MAX_BLOBSIZE(pIn1);
64396   break;
64397 }
64398 
64399 /* Opcode: ToNumeric P1 * * * *
64400 **
64401 ** Force the value in register P1 to be numeric (either an
64402 ** integer or a floating-point number.)
64403 ** If the value is text or blob, try to convert it to an using the
64404 ** equivalent of atoi() or atof() and store 0 if no such conversion
64405 ** is possible.
64406 **
64407 ** A NULL value is not changed by this routine.  It remains NULL.
64408 */
64409 case OP_ToNumeric: {                  /* same as TK_TO_NUMERIC, in1 */
64410   pIn1 = &aMem[pOp->p1];
64411   sqlite3VdbeMemNumerify(pIn1);
64412   break;
64413 }
64414 #endif /* SQLITE_OMIT_CAST */
64415 
64416 /* Opcode: ToInt P1 * * * *
64417 **
64418 ** Force the value in register P1 to be an integer.  If
64419 ** The value is currently a real number, drop its fractional part.
64420 ** If the value is text or blob, try to convert it to an integer using the
64421 ** equivalent of atoi() and store 0 if no such conversion is possible.
64422 **
64423 ** A NULL value is not changed by this routine.  It remains NULL.
64424 */
64425 case OP_ToInt: {                  /* same as TK_TO_INT, in1 */
64426   pIn1 = &aMem[pOp->p1];
64427   if( (pIn1->flags & MEM_Null)==0 ){
64428     sqlite3VdbeMemIntegerify(pIn1);
64429   }
64430   break;
64431 }
64432 
64433 #if !defined(SQLITE_OMIT_CAST) && !defined(SQLITE_OMIT_FLOATING_POINT)
64434 /* Opcode: ToReal P1 * * * *
64435 **
64436 ** Force the value in register P1 to be a floating point number.
64437 ** If The value is currently an integer, convert it.
64438 ** If the value is text or blob, try to convert it to an integer using the
64439 ** equivalent of atoi() and store 0.0 if no such conversion is possible.
64440 **
64441 ** A NULL value is not changed by this routine.  It remains NULL.
64442 */
64443 case OP_ToReal: {                  /* same as TK_TO_REAL, in1 */
64444   pIn1 = &aMem[pOp->p1];
64445   memAboutToChange(p, pIn1);
64446   if( (pIn1->flags & MEM_Null)==0 ){
64447     sqlite3VdbeMemRealify(pIn1);
64448   }
64449   break;
64450 }
64451 #endif /* !defined(SQLITE_OMIT_CAST) && !defined(SQLITE_OMIT_FLOATING_POINT) */
64452 
64453 /* Opcode: Lt P1 P2 P3 P4 P5
64454 **
64455 ** Compare the values in register P1 and P3.  If reg(P3)<reg(P1) then
64456 ** jump to address P2.
64457 **
64458 ** If the SQLITE_JUMPIFNULL bit of P5 is set and either reg(P1) or
64459 ** reg(P3) is NULL then take the jump.  If the SQLITE_JUMPIFNULL
64460 ** bit is clear then fall through if either operand is NULL.
64461 **
64462 ** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
64463 ** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made
64464 ** to coerce both inputs according to this affinity before the
64465 ** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
64466 ** affinity is used. Note that the affinity conversions are stored
64467 ** back into the input registers P1 and P3.  So this opcode can cause
64468 ** persistent changes to registers P1 and P3.
64469 **
64470 ** Once any conversions have taken place, and neither value is NULL,
64471 ** the values are compared. If both values are blobs then memcmp() is
64472 ** used to determine the results of the comparison.  If both values
64473 ** are text, then the appropriate collating function specified in
64474 ** P4 is  used to do the comparison.  If P4 is not specified then
64475 ** memcmp() is used to compare text string.  If both values are
64476 ** numeric, then a numeric comparison is used. If the two values
64477 ** are of different types, then numbers are considered less than
64478 ** strings and strings are considered less than blobs.
64479 **
64480 ** If the SQLITE_STOREP2 bit of P5 is set, then do not jump.  Instead,
64481 ** store a boolean result (either 0, or 1, or NULL) in register P2.
64482 */
64483 /* Opcode: Ne P1 P2 P3 P4 P5
64484 **
64485 ** This works just like the Lt opcode except that the jump is taken if
64486 ** the operands in registers P1 and P3 are not equal.  See the Lt opcode for
64487 ** additional information.
64488 **
64489 ** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
64490 ** true or false and is never NULL.  If both operands are NULL then the result
64491 ** of comparison is false.  If either operand is NULL then the result is true.
64492 ** If neither operand is NULL the result is the same as it would be if
64493 ** the SQLITE_NULLEQ flag were omitted from P5.
64494 */
64495 /* Opcode: Eq P1 P2 P3 P4 P5
64496 **
64497 ** This works just like the Lt opcode except that the jump is taken if
64498 ** the operands in registers P1 and P3 are equal.
64499 ** See the Lt opcode for additional information.
64500 **
64501 ** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
64502 ** true or false and is never NULL.  If both operands are NULL then the result
64503 ** of comparison is true.  If either operand is NULL then the result is false.
64504 ** If neither operand is NULL the result is the same as it would be if
64505 ** the SQLITE_NULLEQ flag were omitted from P5.
64506 */
64507 /* Opcode: Le P1 P2 P3 P4 P5
64508 **
64509 ** This works just like the Lt opcode except that the jump is taken if
64510 ** the content of register P3 is less than or equal to the content of
64511 ** register P1.  See the Lt opcode for additional information.
64512 */
64513 /* Opcode: Gt P1 P2 P3 P4 P5
64514 **
64515 ** This works just like the Lt opcode except that the jump is taken if
64516 ** the content of register P3 is greater than the content of
64517 ** register P1.  See the Lt opcode for additional information.
64518 */
64519 /* Opcode: Ge P1 P2 P3 P4 P5
64520 **
64521 ** This works just like the Lt opcode except that the jump is taken if
64522 ** the content of register P3 is greater than or equal to the content of
64523 ** register P1.  See the Lt opcode for additional information.
64524 */
64525 case OP_Eq:               /* same as TK_EQ, jump, in1, in3 */
64526 case OP_Ne:               /* same as TK_NE, jump, in1, in3 */
64527 case OP_Lt:               /* same as TK_LT, jump, in1, in3 */
64528 case OP_Le:               /* same as TK_LE, jump, in1, in3 */
64529 case OP_Gt:               /* same as TK_GT, jump, in1, in3 */
64530 case OP_Ge: {             /* same as TK_GE, jump, in1, in3 */
64531 #if 0  /* local variables moved into u.ai */
64532   int res;            /* Result of the comparison of pIn1 against pIn3 */
64533   char affinity;      /* Affinity to use for comparison */
64534   u16 flags1;         /* Copy of initial value of pIn1->flags */
64535   u16 flags3;         /* Copy of initial value of pIn3->flags */
64536 #endif /* local variables moved into u.ai */
64537 
64538   pIn1 = &aMem[pOp->p1];
64539   pIn3 = &aMem[pOp->p3];
64540   u.ai.flags1 = pIn1->flags;
64541   u.ai.flags3 = pIn3->flags;
64542   if( (u.ai.flags1 | u.ai.flags3)&MEM_Null ){
64543     /* One or both operands are NULL */
64544     if( pOp->p5 & SQLITE_NULLEQ ){
64545       /* If SQLITE_NULLEQ is set (which will only happen if the operator is
64546       ** OP_Eq or OP_Ne) then take the jump or not depending on whether
64547       ** or not both operands are null.
64548       */
64549       assert( pOp->opcode==OP_Eq || pOp->opcode==OP_Ne );
64550       u.ai.res = (u.ai.flags1 & u.ai.flags3 & MEM_Null)==0;
64551     }else{
64552       /* SQLITE_NULLEQ is clear and at least one operand is NULL,
64553       ** then the result is always NULL.
64554       ** The jump is taken if the SQLITE_JUMPIFNULL bit is set.
64555       */
64556       if( pOp->p5 & SQLITE_STOREP2 ){
64557         pOut = &aMem[pOp->p2];
64558         MemSetTypeFlag(pOut, MEM_Null);
64559         REGISTER_TRACE(pOp->p2, pOut);
64560       }else if( pOp->p5 & SQLITE_JUMPIFNULL ){
64561         pc = pOp->p2-1;
64562       }
64563       break;
64564     }
64565   }else{
64566     /* Neither operand is NULL.  Do a comparison. */
64567     u.ai.affinity = pOp->p5 & SQLITE_AFF_MASK;
64568     if( u.ai.affinity ){
64569       applyAffinity(pIn1, u.ai.affinity, encoding);
64570       applyAffinity(pIn3, u.ai.affinity, encoding);
64571       if( db->mallocFailed ) goto no_mem;
64572     }
64573 
64574     assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 );
64575     ExpandBlob(pIn1);
64576     ExpandBlob(pIn3);
64577     u.ai.res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl);
64578   }
64579   switch( pOp->opcode ){
64580     case OP_Eq:    u.ai.res = u.ai.res==0;     break;
64581     case OP_Ne:    u.ai.res = u.ai.res!=0;     break;
64582     case OP_Lt:    u.ai.res = u.ai.res<0;      break;
64583     case OP_Le:    u.ai.res = u.ai.res<=0;     break;
64584     case OP_Gt:    u.ai.res = u.ai.res>0;      break;
64585     default:       u.ai.res = u.ai.res>=0;     break;
64586   }
64587 
64588   if( pOp->p5 & SQLITE_STOREP2 ){
64589     pOut = &aMem[pOp->p2];
64590     memAboutToChange(p, pOut);
64591     MemSetTypeFlag(pOut, MEM_Int);
64592     pOut->u.i = u.ai.res;
64593     REGISTER_TRACE(pOp->p2, pOut);
64594   }else if( u.ai.res ){
64595     pc = pOp->p2-1;
64596   }
64597 
64598   /* Undo any changes made by applyAffinity() to the input registers. */
64599   pIn1->flags = (pIn1->flags&~MEM_TypeMask) | (u.ai.flags1&MEM_TypeMask);
64600   pIn3->flags = (pIn3->flags&~MEM_TypeMask) | (u.ai.flags3&MEM_TypeMask);
64601   break;
64602 }
64603 
64604 /* Opcode: Permutation * * * P4 *
64605 **
64606 ** Set the permutation used by the OP_Compare operator to be the array
64607 ** of integers in P4.
64608 **
64609 ** The permutation is only valid until the next OP_Permutation, OP_Compare,
64610 ** OP_Halt, or OP_ResultRow.  Typically the OP_Permutation should occur
64611 ** immediately prior to the OP_Compare.
64612 */
64613 case OP_Permutation: {
64614   assert( pOp->p4type==P4_INTARRAY );
64615   assert( pOp->p4.ai );
64616   aPermute = pOp->p4.ai;
64617   break;
64618 }
64619 
64620 /* Opcode: Compare P1 P2 P3 P4 *
64621 **
64622 ** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this
64623 ** vector "A") and in reg(P2)..reg(P2+P3-1) ("B").  Save the result of
64624 ** the comparison for use by the next OP_Jump instruct.
64625 **
64626 ** P4 is a KeyInfo structure that defines collating sequences and sort
64627 ** orders for the comparison.  The permutation applies to registers
64628 ** only.  The KeyInfo elements are used sequentially.
64629 **
64630 ** The comparison is a sort comparison, so NULLs compare equal,
64631 ** NULLs are less than numbers, numbers are less than strings,
64632 ** and strings are less than blobs.
64633 */
64634 case OP_Compare: {
64635 #if 0  /* local variables moved into u.aj */
64636   int n;
64637   int i;
64638   int p1;
64639   int p2;
64640   const KeyInfo *pKeyInfo;
64641   int idx;
64642   CollSeq *pColl;    /* Collating sequence to use on this term */
64643   int bRev;          /* True for DESCENDING sort order */
64644 #endif /* local variables moved into u.aj */
64645 
64646   u.aj.n = pOp->p3;
64647   u.aj.pKeyInfo = pOp->p4.pKeyInfo;
64648   assert( u.aj.n>0 );
64649   assert( u.aj.pKeyInfo!=0 );
64650   u.aj.p1 = pOp->p1;
64651   u.aj.p2 = pOp->p2;
64652 #if SQLITE_DEBUG
64653   if( aPermute ){
64654     int k, mx = 0;
64655     for(k=0; k<u.aj.n; k++) if( aPermute[k]>mx ) mx = aPermute[k];
64656     assert( u.aj.p1>0 && u.aj.p1+mx<=p->nMem+1 );
64657     assert( u.aj.p2>0 && u.aj.p2+mx<=p->nMem+1 );
64658   }else{
64659     assert( u.aj.p1>0 && u.aj.p1+u.aj.n<=p->nMem+1 );
64660     assert( u.aj.p2>0 && u.aj.p2+u.aj.n<=p->nMem+1 );
64661   }
64662 #endif /* SQLITE_DEBUG */
64663   for(u.aj.i=0; u.aj.i<u.aj.n; u.aj.i++){
64664     u.aj.idx = aPermute ? aPermute[u.aj.i] : u.aj.i;
64665     assert( memIsValid(&aMem[u.aj.p1+u.aj.idx]) );
64666     assert( memIsValid(&aMem[u.aj.p2+u.aj.idx]) );
64667     REGISTER_TRACE(u.aj.p1+u.aj.idx, &aMem[u.aj.p1+u.aj.idx]);
64668     REGISTER_TRACE(u.aj.p2+u.aj.idx, &aMem[u.aj.p2+u.aj.idx]);
64669     assert( u.aj.i<u.aj.pKeyInfo->nField );
64670     u.aj.pColl = u.aj.pKeyInfo->aColl[u.aj.i];
64671     u.aj.bRev = u.aj.pKeyInfo->aSortOrder[u.aj.i];
64672     iCompare = sqlite3MemCompare(&aMem[u.aj.p1+u.aj.idx], &aMem[u.aj.p2+u.aj.idx], u.aj.pColl);
64673     if( iCompare ){
64674       if( u.aj.bRev ) iCompare = -iCompare;
64675       break;
64676     }
64677   }
64678   aPermute = 0;
64679   break;
64680 }
64681 
64682 /* Opcode: Jump P1 P2 P3 * *
64683 **
64684 ** Jump to the instruction at address P1, P2, or P3 depending on whether
64685 ** in the most recent OP_Compare instruction the P1 vector was less than
64686 ** equal to, or greater than the P2 vector, respectively.
64687 */
64688 case OP_Jump: {             /* jump */
64689   if( iCompare<0 ){
64690     pc = pOp->p1 - 1;
64691   }else if( iCompare==0 ){
64692     pc = pOp->p2 - 1;
64693   }else{
64694     pc = pOp->p3 - 1;
64695   }
64696   break;
64697 }
64698 
64699 /* Opcode: And P1 P2 P3 * *
64700 **
64701 ** Take the logical AND of the values in registers P1 and P2 and
64702 ** write the result into register P3.
64703 **
64704 ** If either P1 or P2 is 0 (false) then the result is 0 even if
64705 ** the other input is NULL.  A NULL and true or two NULLs give
64706 ** a NULL output.
64707 */
64708 /* Opcode: Or P1 P2 P3 * *
64709 **
64710 ** Take the logical OR of the values in register P1 and P2 and
64711 ** store the answer in register P3.
64712 **
64713 ** If either P1 or P2 is nonzero (true) then the result is 1 (true)
64714 ** even if the other input is NULL.  A NULL and false or two NULLs
64715 ** give a NULL output.
64716 */
64717 case OP_And:              /* same as TK_AND, in1, in2, out3 */
64718 case OP_Or: {             /* same as TK_OR, in1, in2, out3 */
64719 #if 0  /* local variables moved into u.ak */
64720   int v1;    /* Left operand:  0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
64721   int v2;    /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
64722 #endif /* local variables moved into u.ak */
64723 
64724   pIn1 = &aMem[pOp->p1];
64725   if( pIn1->flags & MEM_Null ){
64726     u.ak.v1 = 2;
64727   }else{
64728     u.ak.v1 = sqlite3VdbeIntValue(pIn1)!=0;
64729   }
64730   pIn2 = &aMem[pOp->p2];
64731   if( pIn2->flags & MEM_Null ){
64732     u.ak.v2 = 2;
64733   }else{
64734     u.ak.v2 = sqlite3VdbeIntValue(pIn2)!=0;
64735   }
64736   if( pOp->opcode==OP_And ){
64737     static const unsigned char and_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 };
64738     u.ak.v1 = and_logic[u.ak.v1*3+u.ak.v2];
64739   }else{
64740     static const unsigned char or_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 };
64741     u.ak.v1 = or_logic[u.ak.v1*3+u.ak.v2];
64742   }
64743   pOut = &aMem[pOp->p3];
64744   if( u.ak.v1==2 ){
64745     MemSetTypeFlag(pOut, MEM_Null);
64746   }else{
64747     pOut->u.i = u.ak.v1;
64748     MemSetTypeFlag(pOut, MEM_Int);
64749   }
64750   break;
64751 }
64752 
64753 /* Opcode: Not P1 P2 * * *
64754 **
64755 ** Interpret the value in register P1 as a boolean value.  Store the
64756 ** boolean complement in register P2.  If the value in register P1 is
64757 ** NULL, then a NULL is stored in P2.
64758 */
64759 case OP_Not: {                /* same as TK_NOT, in1, out2 */
64760   pIn1 = &aMem[pOp->p1];
64761   pOut = &aMem[pOp->p2];
64762   if( pIn1->flags & MEM_Null ){
64763     sqlite3VdbeMemSetNull(pOut);
64764   }else{
64765     sqlite3VdbeMemSetInt64(pOut, !sqlite3VdbeIntValue(pIn1));
64766   }
64767   break;
64768 }
64769 
64770 /* Opcode: BitNot P1 P2 * * *
64771 **
64772 ** Interpret the content of register P1 as an integer.  Store the
64773 ** ones-complement of the P1 value into register P2.  If P1 holds
64774 ** a NULL then store a NULL in P2.
64775 */
64776 case OP_BitNot: {             /* same as TK_BITNOT, in1, out2 */
64777   pIn1 = &aMem[pOp->p1];
64778   pOut = &aMem[pOp->p2];
64779   if( pIn1->flags & MEM_Null ){
64780     sqlite3VdbeMemSetNull(pOut);
64781   }else{
64782     sqlite3VdbeMemSetInt64(pOut, ~sqlite3VdbeIntValue(pIn1));
64783   }
64784   break;
64785 }
64786 
64787 /* Opcode: If P1 P2 P3 * *
64788 **
64789 ** Jump to P2 if the value in register P1 is true.  The value
64790 ** is considered true if it is numeric and non-zero.  If the value
64791 ** in P1 is NULL then take the jump if P3 is true.
64792 */
64793 /* Opcode: IfNot P1 P2 P3 * *
64794 **
64795 ** Jump to P2 if the value in register P1 is False.  The value
64796 ** is considered true if it has a numeric value of zero.  If the value
64797 ** in P1 is NULL then take the jump if P3 is true.
64798 */
64799 case OP_If:                 /* jump, in1 */
64800 case OP_IfNot: {            /* jump, in1 */
64801 #if 0  /* local variables moved into u.al */
64802   int c;
64803 #endif /* local variables moved into u.al */
64804   pIn1 = &aMem[pOp->p1];
64805   if( pIn1->flags & MEM_Null ){
64806     u.al.c = pOp->p3;
64807   }else{
64808 #ifdef SQLITE_OMIT_FLOATING_POINT
64809     u.al.c = sqlite3VdbeIntValue(pIn1)!=0;
64810 #else
64811     u.al.c = sqlite3VdbeRealValue(pIn1)!=0.0;
64812 #endif
64813     if( pOp->opcode==OP_IfNot ) u.al.c = !u.al.c;
64814   }
64815   if( u.al.c ){
64816     pc = pOp->p2-1;
64817   }
64818   break;
64819 }
64820 
64821 /* Opcode: IsNull P1 P2 * * *
64822 **
64823 ** Jump to P2 if the value in register P1 is NULL.
64824 */
64825 case OP_IsNull: {            /* same as TK_ISNULL, jump, in1 */
64826   pIn1 = &aMem[pOp->p1];
64827   if( (pIn1->flags & MEM_Null)!=0 ){
64828     pc = pOp->p2 - 1;
64829   }
64830   break;
64831 }
64832 
64833 /* Opcode: NotNull P1 P2 * * *
64834 **
64835 ** Jump to P2 if the value in register P1 is not NULL.
64836 */
64837 case OP_NotNull: {            /* same as TK_NOTNULL, jump, in1 */
64838   pIn1 = &aMem[pOp->p1];
64839   if( (pIn1->flags & MEM_Null)==0 ){
64840     pc = pOp->p2 - 1;
64841   }
64842   break;
64843 }
64844 
64845 /* Opcode: Column P1 P2 P3 P4 P5
64846 **
64847 ** Interpret the data that cursor P1 points to as a structure built using
64848 ** the MakeRecord instruction.  (See the MakeRecord opcode for additional
64849 ** information about the format of the data.)  Extract the P2-th column
64850 ** from this record.  If there are less that (P2+1)
64851 ** values in the record, extract a NULL.
64852 **
64853 ** The value extracted is stored in register P3.
64854 **
64855 ** If the column contains fewer than P2 fields, then extract a NULL.  Or,
64856 ** if the P4 argument is a P4_MEM use the value of the P4 argument as
64857 ** the result.
64858 **
64859 ** If the OPFLAG_CLEARCACHE bit is set on P5 and P1 is a pseudo-table cursor,
64860 ** then the cache of the cursor is reset prior to extracting the column.
64861 ** The first OP_Column against a pseudo-table after the value of the content
64862 ** register has changed should have this bit set.
64863 */
64864 case OP_Column: {
64865 #if 0  /* local variables moved into u.am */
64866   u32 payloadSize;   /* Number of bytes in the record */
64867   i64 payloadSize64; /* Number of bytes in the record */
64868   int p1;            /* P1 value of the opcode */
64869   int p2;            /* column number to retrieve */
64870   VdbeCursor *pC;    /* The VDBE cursor */
64871   char *zRec;        /* Pointer to complete record-data */
64872   BtCursor *pCrsr;   /* The BTree cursor */
64873   u32 *aType;        /* aType[i] holds the numeric type of the i-th column */
64874   u32 *aOffset;      /* aOffset[i] is offset to start of data for i-th column */
64875   int nField;        /* number of fields in the record */
64876   int len;           /* The length of the serialized data for the column */
64877   int i;             /* Loop counter */
64878   char *zData;       /* Part of the record being decoded */
64879   Mem *pDest;        /* Where to write the extracted value */
64880   Mem sMem;          /* For storing the record being decoded */
64881   u8 *zIdx;          /* Index into header */
64882   u8 *zEndHdr;       /* Pointer to first byte after the header */
64883   u32 offset;        /* Offset into the data */
64884   u32 szField;       /* Number of bytes in the content of a field */
64885   int szHdr;         /* Size of the header size field at start of record */
64886   int avail;         /* Number of bytes of available data */
64887   Mem *pReg;         /* PseudoTable input register */
64888 #endif /* local variables moved into u.am */
64889 
64890 
64891   u.am.p1 = pOp->p1;
64892   u.am.p2 = pOp->p2;
64893   u.am.pC = 0;
64894   memset(&u.am.sMem, 0, sizeof(u.am.sMem));
64895   assert( u.am.p1<p->nCursor );
64896   assert( pOp->p3>0 && pOp->p3<=p->nMem );
64897   u.am.pDest = &aMem[pOp->p3];
64898   memAboutToChange(p, u.am.pDest);
64899   MemSetTypeFlag(u.am.pDest, MEM_Null);
64900   u.am.zRec = 0;
64901 
64902   /* This block sets the variable u.am.payloadSize to be the total number of
64903   ** bytes in the record.
64904   **
64905   ** u.am.zRec is set to be the complete text of the record if it is available.
64906   ** The complete record text is always available for pseudo-tables
64907   ** If the record is stored in a cursor, the complete record text
64908   ** might be available in the  u.am.pC->aRow cache.  Or it might not be.
64909   ** If the data is unavailable,  u.am.zRec is set to NULL.
64910   **
64911   ** We also compute the number of columns in the record.  For cursors,
64912   ** the number of columns is stored in the VdbeCursor.nField element.
64913   */
64914   u.am.pC = p->apCsr[u.am.p1];
64915   assert( u.am.pC!=0 );
64916 #ifndef SQLITE_OMIT_VIRTUALTABLE
64917   assert( u.am.pC->pVtabCursor==0 );
64918 #endif
64919   u.am.pCrsr = u.am.pC->pCursor;
64920   if( u.am.pCrsr!=0 ){
64921     /* The record is stored in a B-Tree */
64922     rc = sqlite3VdbeCursorMoveto(u.am.pC);
64923     if( rc ) goto abort_due_to_error;
64924     if( u.am.pC->nullRow ){
64925       u.am.payloadSize = 0;
64926     }else if( u.am.pC->cacheStatus==p->cacheCtr ){
64927       u.am.payloadSize = u.am.pC->payloadSize;
64928       u.am.zRec = (char*)u.am.pC->aRow;
64929     }else if( u.am.pC->isIndex ){
64930       assert( sqlite3BtreeCursorIsValid(u.am.pCrsr) );
64931       rc = sqlite3BtreeKeySize(u.am.pCrsr, &u.am.payloadSize64);
64932       assert( rc==SQLITE_OK );   /* True because of CursorMoveto() call above */
64933       /* sqlite3BtreeParseCellPtr() uses getVarint32() to extract the
64934       ** payload size, so it is impossible for u.am.payloadSize64 to be
64935       ** larger than 32 bits. */
64936       assert( (u.am.payloadSize64 & SQLITE_MAX_U32)==(u64)u.am.payloadSize64 );
64937       u.am.payloadSize = (u32)u.am.payloadSize64;
64938     }else{
64939       assert( sqlite3BtreeCursorIsValid(u.am.pCrsr) );
64940       rc = sqlite3BtreeDataSize(u.am.pCrsr, &u.am.payloadSize);
64941       assert( rc==SQLITE_OK );   /* DataSize() cannot fail */
64942     }
64943   }else if( u.am.pC->pseudoTableReg>0 ){
64944     u.am.pReg = &aMem[u.am.pC->pseudoTableReg];
64945     assert( u.am.pReg->flags & MEM_Blob );
64946     assert( memIsValid(u.am.pReg) );
64947     u.am.payloadSize = u.am.pReg->n;
64948     u.am.zRec = u.am.pReg->z;
64949     u.am.pC->cacheStatus = (pOp->p5&OPFLAG_CLEARCACHE) ? CACHE_STALE : p->cacheCtr;
64950     assert( u.am.payloadSize==0 || u.am.zRec!=0 );
64951   }else{
64952     /* Consider the row to be NULL */
64953     u.am.payloadSize = 0;
64954   }
64955 
64956   /* If u.am.payloadSize is 0, then just store a NULL */
64957   if( u.am.payloadSize==0 ){
64958     assert( u.am.pDest->flags&MEM_Null );
64959     goto op_column_out;
64960   }
64961   assert( db->aLimit[SQLITE_LIMIT_LENGTH]>=0 );
64962   if( u.am.payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
64963     goto too_big;
64964   }
64965 
64966   u.am.nField = u.am.pC->nField;
64967   assert( u.am.p2<u.am.nField );
64968 
64969   /* Read and parse the table header.  Store the results of the parse
64970   ** into the record header cache fields of the cursor.
64971   */
64972   u.am.aType = u.am.pC->aType;
64973   if( u.am.pC->cacheStatus==p->cacheCtr ){
64974     u.am.aOffset = u.am.pC->aOffset;
64975   }else{
64976     assert(u.am.aType);
64977     u.am.avail = 0;
64978     u.am.pC->aOffset = u.am.aOffset = &u.am.aType[u.am.nField];
64979     u.am.pC->payloadSize = u.am.payloadSize;
64980     u.am.pC->cacheStatus = p->cacheCtr;
64981 
64982     /* Figure out how many bytes are in the header */
64983     if( u.am.zRec ){
64984       u.am.zData = u.am.zRec;
64985     }else{
64986       if( u.am.pC->isIndex ){
64987         u.am.zData = (char*)sqlite3BtreeKeyFetch(u.am.pCrsr, &u.am.avail);
64988       }else{
64989         u.am.zData = (char*)sqlite3BtreeDataFetch(u.am.pCrsr, &u.am.avail);
64990       }
64991       /* If KeyFetch()/DataFetch() managed to get the entire payload,
64992       ** save the payload in the u.am.pC->aRow cache.  That will save us from
64993       ** having to make additional calls to fetch the content portion of
64994       ** the record.
64995       */
64996       assert( u.am.avail>=0 );
64997       if( u.am.payloadSize <= (u32)u.am.avail ){
64998         u.am.zRec = u.am.zData;
64999         u.am.pC->aRow = (u8*)u.am.zData;
65000       }else{
65001         u.am.pC->aRow = 0;
65002       }
65003     }
65004     /* The following assert is true in all cases accept when
65005     ** the database file has been corrupted externally.
65006     **    assert( u.am.zRec!=0 || u.am.avail>=u.am.payloadSize || u.am.avail>=9 ); */
65007     u.am.szHdr = getVarint32((u8*)u.am.zData, u.am.offset);
65008 
65009     /* Make sure a corrupt database has not given us an oversize header.
65010     ** Do this now to avoid an oversize memory allocation.
65011     **
65012     ** Type entries can be between 1 and 5 bytes each.  But 4 and 5 byte
65013     ** types use so much data space that there can only be 4096 and 32 of
65014     ** them, respectively.  So the maximum header length results from a
65015     ** 3-byte type for each of the maximum of 32768 columns plus three
65016     ** extra bytes for the header length itself.  32768*3 + 3 = 98307.
65017     */
65018     if( u.am.offset > 98307 ){
65019       rc = SQLITE_CORRUPT_BKPT;
65020       goto op_column_out;
65021     }
65022 
65023     /* Compute in u.am.len the number of bytes of data we need to read in order
65024     ** to get u.am.nField type values.  u.am.offset is an upper bound on this.  But
65025     ** u.am.nField might be significantly less than the true number of columns
65026     ** in the table, and in that case, 5*u.am.nField+3 might be smaller than u.am.offset.
65027     ** We want to minimize u.am.len in order to limit the size of the memory
65028     ** allocation, especially if a corrupt database file has caused u.am.offset
65029     ** to be oversized. Offset is limited to 98307 above.  But 98307 might
65030     ** still exceed Robson memory allocation limits on some configurations.
65031     ** On systems that cannot tolerate large memory allocations, u.am.nField*5+3
65032     ** will likely be much smaller since u.am.nField will likely be less than
65033     ** 20 or so.  This insures that Robson memory allocation limits are
65034     ** not exceeded even for corrupt database files.
65035     */
65036     u.am.len = u.am.nField*5 + 3;
65037     if( u.am.len > (int)u.am.offset ) u.am.len = (int)u.am.offset;
65038 
65039     /* The KeyFetch() or DataFetch() above are fast and will get the entire
65040     ** record header in most cases.  But they will fail to get the complete
65041     ** record header if the record header does not fit on a single page
65042     ** in the B-Tree.  When that happens, use sqlite3VdbeMemFromBtree() to
65043     ** acquire the complete header text.
65044     */
65045     if( !u.am.zRec && u.am.avail<u.am.len ){
65046       u.am.sMem.flags = 0;
65047       u.am.sMem.db = 0;
65048       rc = sqlite3VdbeMemFromBtree(u.am.pCrsr, 0, u.am.len, u.am.pC->isIndex, &u.am.sMem);
65049       if( rc!=SQLITE_OK ){
65050         goto op_column_out;
65051       }
65052       u.am.zData = u.am.sMem.z;
65053     }
65054     u.am.zEndHdr = (u8 *)&u.am.zData[u.am.len];
65055     u.am.zIdx = (u8 *)&u.am.zData[u.am.szHdr];
65056 
65057     /* Scan the header and use it to fill in the u.am.aType[] and u.am.aOffset[]
65058     ** arrays.  u.am.aType[u.am.i] will contain the type integer for the u.am.i-th
65059     ** column and u.am.aOffset[u.am.i] will contain the u.am.offset from the beginning
65060     ** of the record to the start of the data for the u.am.i-th column
65061     */
65062     for(u.am.i=0; u.am.i<u.am.nField; u.am.i++){
65063       if( u.am.zIdx<u.am.zEndHdr ){
65064         u.am.aOffset[u.am.i] = u.am.offset;
65065         u.am.zIdx += getVarint32(u.am.zIdx, u.am.aType[u.am.i]);
65066         u.am.szField = sqlite3VdbeSerialTypeLen(u.am.aType[u.am.i]);
65067         u.am.offset += u.am.szField;
65068         if( u.am.offset<u.am.szField ){  /* True if u.am.offset overflows */
65069           u.am.zIdx = &u.am.zEndHdr[1];  /* Forces SQLITE_CORRUPT return below */
65070           break;
65071         }
65072       }else{
65073         /* If u.am.i is less that u.am.nField, then there are less fields in this
65074         ** record than SetNumColumns indicated there are columns in the
65075         ** table. Set the u.am.offset for any extra columns not present in
65076         ** the record to 0. This tells code below to store a NULL
65077         ** instead of deserializing a value from the record.
65078         */
65079         u.am.aOffset[u.am.i] = 0;
65080       }
65081     }
65082     sqlite3VdbeMemRelease(&u.am.sMem);
65083     u.am.sMem.flags = MEM_Null;
65084 
65085     /* If we have read more header data than was contained in the header,
65086     ** or if the end of the last field appears to be past the end of the
65087     ** record, or if the end of the last field appears to be before the end
65088     ** of the record (when all fields present), then we must be dealing
65089     ** with a corrupt database.
65090     */
65091     if( (u.am.zIdx > u.am.zEndHdr) || (u.am.offset > u.am.payloadSize)
65092          || (u.am.zIdx==u.am.zEndHdr && u.am.offset!=u.am.payloadSize) ){
65093       rc = SQLITE_CORRUPT_BKPT;
65094       goto op_column_out;
65095     }
65096   }
65097 
65098   /* Get the column information. If u.am.aOffset[u.am.p2] is non-zero, then
65099   ** deserialize the value from the record. If u.am.aOffset[u.am.p2] is zero,
65100   ** then there are not enough fields in the record to satisfy the
65101   ** request.  In this case, set the value NULL or to P4 if P4 is
65102   ** a pointer to a Mem object.
65103   */
65104   if( u.am.aOffset[u.am.p2] ){
65105     assert( rc==SQLITE_OK );
65106     if( u.am.zRec ){
65107       sqlite3VdbeMemReleaseExternal(u.am.pDest);
65108       sqlite3VdbeSerialGet((u8 *)&u.am.zRec[u.am.aOffset[u.am.p2]], u.am.aType[u.am.p2], u.am.pDest);
65109     }else{
65110       u.am.len = sqlite3VdbeSerialTypeLen(u.am.aType[u.am.p2]);
65111       sqlite3VdbeMemMove(&u.am.sMem, u.am.pDest);
65112       rc = sqlite3VdbeMemFromBtree(u.am.pCrsr, u.am.aOffset[u.am.p2], u.am.len, u.am.pC->isIndex, &u.am.sMem);
65113       if( rc!=SQLITE_OK ){
65114         goto op_column_out;
65115       }
65116       u.am.zData = u.am.sMem.z;
65117       sqlite3VdbeSerialGet((u8*)u.am.zData, u.am.aType[u.am.p2], u.am.pDest);
65118     }
65119     u.am.pDest->enc = encoding;
65120   }else{
65121     if( pOp->p4type==P4_MEM ){
65122       sqlite3VdbeMemShallowCopy(u.am.pDest, pOp->p4.pMem, MEM_Static);
65123     }else{
65124       assert( u.am.pDest->flags&MEM_Null );
65125     }
65126   }
65127 
65128   /* If we dynamically allocated space to hold the data (in the
65129   ** sqlite3VdbeMemFromBtree() call above) then transfer control of that
65130   ** dynamically allocated space over to the u.am.pDest structure.
65131   ** This prevents a memory copy.
65132   */
65133   if( u.am.sMem.zMalloc ){
65134     assert( u.am.sMem.z==u.am.sMem.zMalloc );
65135     assert( !(u.am.pDest->flags & MEM_Dyn) );
65136     assert( !(u.am.pDest->flags & (MEM_Blob|MEM_Str)) || u.am.pDest->z==u.am.sMem.z );
65137     u.am.pDest->flags &= ~(MEM_Ephem|MEM_Static);
65138     u.am.pDest->flags |= MEM_Term;
65139     u.am.pDest->z = u.am.sMem.z;
65140     u.am.pDest->zMalloc = u.am.sMem.zMalloc;
65141   }
65142 
65143   rc = sqlite3VdbeMemMakeWriteable(u.am.pDest);
65144 
65145 op_column_out:
65146   UPDATE_MAX_BLOBSIZE(u.am.pDest);
65147   REGISTER_TRACE(pOp->p3, u.am.pDest);
65148   break;
65149 }
65150 
65151 /* Opcode: Affinity P1 P2 * P4 *
65152 **
65153 ** Apply affinities to a range of P2 registers starting with P1.
65154 **
65155 ** P4 is a string that is P2 characters long. The nth character of the
65156 ** string indicates the column affinity that should be used for the nth
65157 ** memory cell in the range.
65158 */
65159 case OP_Affinity: {
65160 #if 0  /* local variables moved into u.an */
65161   const char *zAffinity;   /* The affinity to be applied */
65162   char cAff;               /* A single character of affinity */
65163 #endif /* local variables moved into u.an */
65164 
65165   u.an.zAffinity = pOp->p4.z;
65166   assert( u.an.zAffinity!=0 );
65167   assert( u.an.zAffinity[pOp->p2]==0 );
65168   pIn1 = &aMem[pOp->p1];
65169   while( (u.an.cAff = *(u.an.zAffinity++))!=0 ){
65170     assert( pIn1 <= &p->aMem[p->nMem] );
65171     assert( memIsValid(pIn1) );
65172     ExpandBlob(pIn1);
65173     applyAffinity(pIn1, u.an.cAff, encoding);
65174     pIn1++;
65175   }
65176   break;
65177 }
65178 
65179 /* Opcode: MakeRecord P1 P2 P3 P4 *
65180 **
65181 ** Convert P2 registers beginning with P1 into the [record format]
65182 ** use as a data record in a database table or as a key
65183 ** in an index.  The OP_Column opcode can decode the record later.
65184 **
65185 ** P4 may be a string that is P2 characters long.  The nth character of the
65186 ** string indicates the column affinity that should be used for the nth
65187 ** field of the index key.
65188 **
65189 ** The mapping from character to affinity is given by the SQLITE_AFF_
65190 ** macros defined in sqliteInt.h.
65191 **
65192 ** If P4 is NULL then all index fields have the affinity NONE.
65193 */
65194 case OP_MakeRecord: {
65195 #if 0  /* local variables moved into u.ao */
65196   u8 *zNewRecord;        /* A buffer to hold the data for the new record */
65197   Mem *pRec;             /* The new record */
65198   u64 nData;             /* Number of bytes of data space */
65199   int nHdr;              /* Number of bytes of header space */
65200   i64 nByte;             /* Data space required for this record */
65201   int nZero;             /* Number of zero bytes at the end of the record */
65202   int nVarint;           /* Number of bytes in a varint */
65203   u32 serial_type;       /* Type field */
65204   Mem *pData0;           /* First field to be combined into the record */
65205   Mem *pLast;            /* Last field of the record */
65206   int nField;            /* Number of fields in the record */
65207   char *zAffinity;       /* The affinity string for the record */
65208   int file_format;       /* File format to use for encoding */
65209   int i;                 /* Space used in zNewRecord[] */
65210   int len;               /* Length of a field */
65211 #endif /* local variables moved into u.ao */
65212 
65213   /* Assuming the record contains N fields, the record format looks
65214   ** like this:
65215   **
65216   ** ------------------------------------------------------------------------
65217   ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 |
65218   ** ------------------------------------------------------------------------
65219   **
65220   ** Data(0) is taken from register P1.  Data(1) comes from register P1+1
65221   ** and so froth.
65222   **
65223   ** Each type field is a varint representing the serial type of the
65224   ** corresponding data element (see sqlite3VdbeSerialType()). The
65225   ** hdr-size field is also a varint which is the offset from the beginning
65226   ** of the record to data0.
65227   */
65228   u.ao.nData = 0;         /* Number of bytes of data space */
65229   u.ao.nHdr = 0;          /* Number of bytes of header space */
65230   u.ao.nZero = 0;         /* Number of zero bytes at the end of the record */
65231   u.ao.nField = pOp->p1;
65232   u.ao.zAffinity = pOp->p4.z;
65233   assert( u.ao.nField>0 && pOp->p2>0 && pOp->p2+u.ao.nField<=p->nMem+1 );
65234   u.ao.pData0 = &aMem[u.ao.nField];
65235   u.ao.nField = pOp->p2;
65236   u.ao.pLast = &u.ao.pData0[u.ao.nField-1];
65237   u.ao.file_format = p->minWriteFileFormat;
65238 
65239   /* Identify the output register */
65240   assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 );
65241   pOut = &aMem[pOp->p3];
65242   memAboutToChange(p, pOut);
65243 
65244   /* Loop through the elements that will make up the record to figure
65245   ** out how much space is required for the new record.
65246   */
65247   for(u.ao.pRec=u.ao.pData0; u.ao.pRec<=u.ao.pLast; u.ao.pRec++){
65248     assert( memIsValid(u.ao.pRec) );
65249     if( u.ao.zAffinity ){
65250       applyAffinity(u.ao.pRec, u.ao.zAffinity[u.ao.pRec-u.ao.pData0], encoding);
65251     }
65252     if( u.ao.pRec->flags&MEM_Zero && u.ao.pRec->n>0 ){
65253       sqlite3VdbeMemExpandBlob(u.ao.pRec);
65254     }
65255     u.ao.serial_type = sqlite3VdbeSerialType(u.ao.pRec, u.ao.file_format);
65256     u.ao.len = sqlite3VdbeSerialTypeLen(u.ao.serial_type);
65257     u.ao.nData += u.ao.len;
65258     u.ao.nHdr += sqlite3VarintLen(u.ao.serial_type);
65259     if( u.ao.pRec->flags & MEM_Zero ){
65260       /* Only pure zero-filled BLOBs can be input to this Opcode.
65261       ** We do not allow blobs with a prefix and a zero-filled tail. */
65262       u.ao.nZero += u.ao.pRec->u.nZero;
65263     }else if( u.ao.len ){
65264       u.ao.nZero = 0;
65265     }
65266   }
65267 
65268   /* Add the initial header varint and total the size */
65269   u.ao.nHdr += u.ao.nVarint = sqlite3VarintLen(u.ao.nHdr);
65270   if( u.ao.nVarint<sqlite3VarintLen(u.ao.nHdr) ){
65271     u.ao.nHdr++;
65272   }
65273   u.ao.nByte = u.ao.nHdr+u.ao.nData-u.ao.nZero;
65274   if( u.ao.nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
65275     goto too_big;
65276   }
65277 
65278   /* Make sure the output register has a buffer large enough to store
65279   ** the new record. The output register (pOp->p3) is not allowed to
65280   ** be one of the input registers (because the following call to
65281   ** sqlite3VdbeMemGrow() could clobber the value before it is used).
65282   */
65283   if( sqlite3VdbeMemGrow(pOut, (int)u.ao.nByte, 0) ){
65284     goto no_mem;
65285   }
65286   u.ao.zNewRecord = (u8 *)pOut->z;
65287 
65288   /* Write the record */
65289   u.ao.i = putVarint32(u.ao.zNewRecord, u.ao.nHdr);
65290   for(u.ao.pRec=u.ao.pData0; u.ao.pRec<=u.ao.pLast; u.ao.pRec++){
65291     u.ao.serial_type = sqlite3VdbeSerialType(u.ao.pRec, u.ao.file_format);
65292     u.ao.i += putVarint32(&u.ao.zNewRecord[u.ao.i], u.ao.serial_type);      /* serial type */
65293   }
65294   for(u.ao.pRec=u.ao.pData0; u.ao.pRec<=u.ao.pLast; u.ao.pRec++){  /* serial data */
65295     u.ao.i += sqlite3VdbeSerialPut(&u.ao.zNewRecord[u.ao.i], (int)(u.ao.nByte-u.ao.i), u.ao.pRec,u.ao.file_format);
65296   }
65297   assert( u.ao.i==u.ao.nByte );
65298 
65299   assert( pOp->p3>0 && pOp->p3<=p->nMem );
65300   pOut->n = (int)u.ao.nByte;
65301   pOut->flags = MEM_Blob | MEM_Dyn;
65302   pOut->xDel = 0;
65303   if( u.ao.nZero ){
65304     pOut->u.nZero = u.ao.nZero;
65305     pOut->flags |= MEM_Zero;
65306   }
65307   pOut->enc = SQLITE_UTF8;  /* In case the blob is ever converted to text */
65308   REGISTER_TRACE(pOp->p3, pOut);
65309   UPDATE_MAX_BLOBSIZE(pOut);
65310   break;
65311 }
65312 
65313 /* Opcode: Count P1 P2 * * *
65314 **
65315 ** Store the number of entries (an integer value) in the table or index
65316 ** opened by cursor P1 in register P2
65317 */
65318 #ifndef SQLITE_OMIT_BTREECOUNT
65319 case OP_Count: {         /* out2-prerelease */
65320 #if 0  /* local variables moved into u.ap */
65321   i64 nEntry;
65322   BtCursor *pCrsr;
65323 #endif /* local variables moved into u.ap */
65324 
65325   u.ap.pCrsr = p->apCsr[pOp->p1]->pCursor;
65326   if( u.ap.pCrsr ){
65327     rc = sqlite3BtreeCount(u.ap.pCrsr, &u.ap.nEntry);
65328   }else{
65329     u.ap.nEntry = 0;
65330   }
65331   pOut->u.i = u.ap.nEntry;
65332   break;
65333 }
65334 #endif
65335 
65336 /* Opcode: Savepoint P1 * * P4 *
65337 **
65338 ** Open, release or rollback the savepoint named by parameter P4, depending
65339 ** on the value of P1. To open a new savepoint, P1==0. To release (commit) an
65340 ** existing savepoint, P1==1, or to rollback an existing savepoint P1==2.
65341 */
65342 case OP_Savepoint: {
65343 #if 0  /* local variables moved into u.aq */
65344   int p1;                         /* Value of P1 operand */
65345   char *zName;                    /* Name of savepoint */
65346   int nName;
65347   Savepoint *pNew;
65348   Savepoint *pSavepoint;
65349   Savepoint *pTmp;
65350   int iSavepoint;
65351   int ii;
65352 #endif /* local variables moved into u.aq */
65353 
65354   u.aq.p1 = pOp->p1;
65355   u.aq.zName = pOp->p4.z;
65356 
65357   /* Assert that the u.aq.p1 parameter is valid. Also that if there is no open
65358   ** transaction, then there cannot be any savepoints.
65359   */
65360   assert( db->pSavepoint==0 || db->autoCommit==0 );
65361   assert( u.aq.p1==SAVEPOINT_BEGIN||u.aq.p1==SAVEPOINT_RELEASE||u.aq.p1==SAVEPOINT_ROLLBACK );
65362   assert( db->pSavepoint || db->isTransactionSavepoint==0 );
65363   assert( checkSavepointCount(db) );
65364 
65365   if( u.aq.p1==SAVEPOINT_BEGIN ){
65366     if( db->writeVdbeCnt>0 ){
65367       /* A new savepoint cannot be created if there are active write
65368       ** statements (i.e. open read/write incremental blob handles).
65369       */
65370       sqlite3SetString(&p->zErrMsg, db, "cannot open savepoint - "
65371         "SQL statements in progress");
65372       rc = SQLITE_BUSY;
65373     }else{
65374       u.aq.nName = sqlite3Strlen30(u.aq.zName);
65375 
65376 #ifndef SQLITE_OMIT_VIRTUALTABLE
65377       /* This call is Ok even if this savepoint is actually a transaction
65378       ** savepoint (and therefore should not prompt xSavepoint()) callbacks.
65379       ** If this is a transaction savepoint being opened, it is guaranteed
65380       ** that the db->aVTrans[] array is empty.  */
65381       assert( db->autoCommit==0 || db->nVTrans==0 );
65382       rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN,
65383                                 db->nStatement+db->nSavepoint);
65384       if( rc!=SQLITE_OK ) goto abort_due_to_error;
65385 #endif
65386 
65387       /* Create a new savepoint structure. */
65388       u.aq.pNew = sqlite3DbMallocRaw(db, sizeof(Savepoint)+u.aq.nName+1);
65389       if( u.aq.pNew ){
65390         u.aq.pNew->zName = (char *)&u.aq.pNew[1];
65391         memcpy(u.aq.pNew->zName, u.aq.zName, u.aq.nName+1);
65392 
65393         /* If there is no open transaction, then mark this as a special
65394         ** "transaction savepoint". */
65395         if( db->autoCommit ){
65396           db->autoCommit = 0;
65397           db->isTransactionSavepoint = 1;
65398         }else{
65399           db->nSavepoint++;
65400         }
65401 
65402         /* Link the new savepoint into the database handle's list. */
65403         u.aq.pNew->pNext = db->pSavepoint;
65404         db->pSavepoint = u.aq.pNew;
65405         u.aq.pNew->nDeferredCons = db->nDeferredCons;
65406       }
65407     }
65408   }else{
65409     u.aq.iSavepoint = 0;
65410 
65411     /* Find the named savepoint. If there is no such savepoint, then an
65412     ** an error is returned to the user.  */
65413     for(
65414       u.aq.pSavepoint = db->pSavepoint;
65415       u.aq.pSavepoint && sqlite3StrICmp(u.aq.pSavepoint->zName, u.aq.zName);
65416       u.aq.pSavepoint = u.aq.pSavepoint->pNext
65417     ){
65418       u.aq.iSavepoint++;
65419     }
65420     if( !u.aq.pSavepoint ){
65421       sqlite3SetString(&p->zErrMsg, db, "no such savepoint: %s", u.aq.zName);
65422       rc = SQLITE_ERROR;
65423     }else if(
65424         db->writeVdbeCnt>0 || (u.aq.p1==SAVEPOINT_ROLLBACK && db->activeVdbeCnt>1)
65425     ){
65426       /* It is not possible to release (commit) a savepoint if there are
65427       ** active write statements. It is not possible to rollback a savepoint
65428       ** if there are any active statements at all.
65429       */
65430       sqlite3SetString(&p->zErrMsg, db,
65431         "cannot %s savepoint - SQL statements in progress",
65432         (u.aq.p1==SAVEPOINT_ROLLBACK ? "rollback": "release")
65433       );
65434       rc = SQLITE_BUSY;
65435     }else{
65436 
65437       /* Determine whether or not this is a transaction savepoint. If so,
65438       ** and this is a RELEASE command, then the current transaction
65439       ** is committed.
65440       */
65441       int isTransaction = u.aq.pSavepoint->pNext==0 && db->isTransactionSavepoint;
65442       if( isTransaction && u.aq.p1==SAVEPOINT_RELEASE ){
65443         if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
65444           goto vdbe_return;
65445         }
65446         db->autoCommit = 1;
65447         if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
65448           p->pc = pc;
65449           db->autoCommit = 0;
65450           p->rc = rc = SQLITE_BUSY;
65451           goto vdbe_return;
65452         }
65453         db->isTransactionSavepoint = 0;
65454         rc = p->rc;
65455       }else{
65456         u.aq.iSavepoint = db->nSavepoint - u.aq.iSavepoint - 1;
65457         for(u.aq.ii=0; u.aq.ii<db->nDb; u.aq.ii++){
65458           rc = sqlite3BtreeSavepoint(db->aDb[u.aq.ii].pBt, u.aq.p1, u.aq.iSavepoint);
65459           if( rc!=SQLITE_OK ){
65460             goto abort_due_to_error;
65461           }
65462         }
65463         if( u.aq.p1==SAVEPOINT_ROLLBACK && (db->flags&SQLITE_InternChanges)!=0 ){
65464           sqlite3ExpirePreparedStatements(db);
65465           sqlite3ResetInternalSchema(db, -1);
65466           db->flags = (db->flags | SQLITE_InternChanges);
65467         }
65468       }
65469 
65470       /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all
65471       ** savepoints nested inside of the savepoint being operated on. */
65472       while( db->pSavepoint!=u.aq.pSavepoint ){
65473         u.aq.pTmp = db->pSavepoint;
65474         db->pSavepoint = u.aq.pTmp->pNext;
65475         sqlite3DbFree(db, u.aq.pTmp);
65476         db->nSavepoint--;
65477       }
65478 
65479       /* If it is a RELEASE, then destroy the savepoint being operated on
65480       ** too. If it is a ROLLBACK TO, then set the number of deferred
65481       ** constraint violations present in the database to the value stored
65482       ** when the savepoint was created.  */
65483       if( u.aq.p1==SAVEPOINT_RELEASE ){
65484         assert( u.aq.pSavepoint==db->pSavepoint );
65485         db->pSavepoint = u.aq.pSavepoint->pNext;
65486         sqlite3DbFree(db, u.aq.pSavepoint);
65487         if( !isTransaction ){
65488           db->nSavepoint--;
65489         }
65490       }else{
65491         db->nDeferredCons = u.aq.pSavepoint->nDeferredCons;
65492       }
65493 
65494       if( !isTransaction ){
65495         rc = sqlite3VtabSavepoint(db, u.aq.p1, u.aq.iSavepoint);
65496         if( rc!=SQLITE_OK ) goto abort_due_to_error;
65497       }
65498     }
65499   }
65500 
65501   break;
65502 }
65503 
65504 /* Opcode: AutoCommit P1 P2 * * *
65505 **
65506 ** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll
65507 ** back any currently active btree transactions. If there are any active
65508 ** VMs (apart from this one), then a ROLLBACK fails.  A COMMIT fails if
65509 ** there are active writing VMs or active VMs that use shared cache.
65510 **
65511 ** This instruction causes the VM to halt.
65512 */
65513 case OP_AutoCommit: {
65514 #if 0  /* local variables moved into u.ar */
65515   int desiredAutoCommit;
65516   int iRollback;
65517   int turnOnAC;
65518 #endif /* local variables moved into u.ar */
65519 
65520   u.ar.desiredAutoCommit = pOp->p1;
65521   u.ar.iRollback = pOp->p2;
65522   u.ar.turnOnAC = u.ar.desiredAutoCommit && !db->autoCommit;
65523   assert( u.ar.desiredAutoCommit==1 || u.ar.desiredAutoCommit==0 );
65524   assert( u.ar.desiredAutoCommit==1 || u.ar.iRollback==0 );
65525   assert( db->activeVdbeCnt>0 );  /* At least this one VM is active */
65526 
65527   if( u.ar.turnOnAC && u.ar.iRollback && db->activeVdbeCnt>1 ){
65528     /* If this instruction implements a ROLLBACK and other VMs are
65529     ** still running, and a transaction is active, return an error indicating
65530     ** that the other VMs must complete first.
65531     */
65532     sqlite3SetString(&p->zErrMsg, db, "cannot rollback transaction - "
65533         "SQL statements in progress");
65534     rc = SQLITE_BUSY;
65535   }else if( u.ar.turnOnAC && !u.ar.iRollback && db->writeVdbeCnt>0 ){
65536     /* If this instruction implements a COMMIT and other VMs are writing
65537     ** return an error indicating that the other VMs must complete first.
65538     */
65539     sqlite3SetString(&p->zErrMsg, db, "cannot commit transaction - "
65540         "SQL statements in progress");
65541     rc = SQLITE_BUSY;
65542   }else if( u.ar.desiredAutoCommit!=db->autoCommit ){
65543     if( u.ar.iRollback ){
65544       assert( u.ar.desiredAutoCommit==1 );
65545       sqlite3RollbackAll(db);
65546       db->autoCommit = 1;
65547     }else if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
65548       goto vdbe_return;
65549     }else{
65550       db->autoCommit = (u8)u.ar.desiredAutoCommit;
65551       if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
65552         p->pc = pc;
65553         db->autoCommit = (u8)(1-u.ar.desiredAutoCommit);
65554         p->rc = rc = SQLITE_BUSY;
65555         goto vdbe_return;
65556       }
65557     }
65558     assert( db->nStatement==0 );
65559     sqlite3CloseSavepoints(db);
65560     if( p->rc==SQLITE_OK ){
65561       rc = SQLITE_DONE;
65562     }else{
65563       rc = SQLITE_ERROR;
65564     }
65565     goto vdbe_return;
65566   }else{
65567     sqlite3SetString(&p->zErrMsg, db,
65568         (!u.ar.desiredAutoCommit)?"cannot start a transaction within a transaction":(
65569         (u.ar.iRollback)?"cannot rollback - no transaction is active":
65570                    "cannot commit - no transaction is active"));
65571 
65572     rc = SQLITE_ERROR;
65573   }
65574   break;
65575 }
65576 
65577 /* Opcode: Transaction P1 P2 * * *
65578 **
65579 ** Begin a transaction.  The transaction ends when a Commit or Rollback
65580 ** opcode is encountered.  Depending on the ON CONFLICT setting, the
65581 ** transaction might also be rolled back if an error is encountered.
65582 **
65583 ** P1 is the index of the database file on which the transaction is
65584 ** started.  Index 0 is the main database file and index 1 is the
65585 ** file used for temporary tables.  Indices of 2 or more are used for
65586 ** attached databases.
65587 **
65588 ** If P2 is non-zero, then a write-transaction is started.  A RESERVED lock is
65589 ** obtained on the database file when a write-transaction is started.  No
65590 ** other process can start another write transaction while this transaction is
65591 ** underway.  Starting a write transaction also creates a rollback journal. A
65592 ** write transaction must be started before any changes can be made to the
65593 ** database.  If P2 is 2 or greater then an EXCLUSIVE lock is also obtained
65594 ** on the file.
65595 **
65596 ** If a write-transaction is started and the Vdbe.usesStmtJournal flag is
65597 ** true (this flag is set if the Vdbe may modify more than one row and may
65598 ** throw an ABORT exception), a statement transaction may also be opened.
65599 ** More specifically, a statement transaction is opened iff the database
65600 ** connection is currently not in autocommit mode, or if there are other
65601 ** active statements. A statement transaction allows the affects of this
65602 ** VDBE to be rolled back after an error without having to roll back the
65603 ** entire transaction. If no error is encountered, the statement transaction
65604 ** will automatically commit when the VDBE halts.
65605 **
65606 ** If P2 is zero, then a read-lock is obtained on the database file.
65607 */
65608 case OP_Transaction: {
65609 #if 0  /* local variables moved into u.as */
65610   Btree *pBt;
65611 #endif /* local variables moved into u.as */
65612 
65613   assert( pOp->p1>=0 && pOp->p1<db->nDb );
65614   assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
65615   u.as.pBt = db->aDb[pOp->p1].pBt;
65616 
65617   if( u.as.pBt ){
65618     rc = sqlite3BtreeBeginTrans(u.as.pBt, pOp->p2);
65619     if( rc==SQLITE_BUSY ){
65620       p->pc = pc;
65621       p->rc = rc = SQLITE_BUSY;
65622       goto vdbe_return;
65623     }
65624     if( rc!=SQLITE_OK ){
65625       goto abort_due_to_error;
65626     }
65627 
65628     if( pOp->p2 && p->usesStmtJournal
65629      && (db->autoCommit==0 || db->activeVdbeCnt>1)
65630     ){
65631       assert( sqlite3BtreeIsInTrans(u.as.pBt) );
65632       if( p->iStatement==0 ){
65633         assert( db->nStatement>=0 && db->nSavepoint>=0 );
65634         db->nStatement++;
65635         p->iStatement = db->nSavepoint + db->nStatement;
65636       }
65637 
65638       rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, p->iStatement-1);
65639       if( rc==SQLITE_OK ){
65640         rc = sqlite3BtreeBeginStmt(u.as.pBt, p->iStatement);
65641       }
65642 
65643       /* Store the current value of the database handles deferred constraint
65644       ** counter. If the statement transaction needs to be rolled back,
65645       ** the value of this counter needs to be restored too.  */
65646       p->nStmtDefCons = db->nDeferredCons;
65647     }
65648   }
65649   break;
65650 }
65651 
65652 /* Opcode: ReadCookie P1 P2 P3 * *
65653 **
65654 ** Read cookie number P3 from database P1 and write it into register P2.
65655 ** P3==1 is the schema version.  P3==2 is the database format.
65656 ** P3==3 is the recommended pager cache size, and so forth.  P1==0 is
65657 ** the main database file and P1==1 is the database file used to store
65658 ** temporary tables.
65659 **
65660 ** There must be a read-lock on the database (either a transaction
65661 ** must be started or there must be an open cursor) before
65662 ** executing this instruction.
65663 */
65664 case OP_ReadCookie: {               /* out2-prerelease */
65665 #if 0  /* local variables moved into u.at */
65666   int iMeta;
65667   int iDb;
65668   int iCookie;
65669 #endif /* local variables moved into u.at */
65670 
65671   u.at.iDb = pOp->p1;
65672   u.at.iCookie = pOp->p3;
65673   assert( pOp->p3<SQLITE_N_BTREE_META );
65674   assert( u.at.iDb>=0 && u.at.iDb<db->nDb );
65675   assert( db->aDb[u.at.iDb].pBt!=0 );
65676   assert( (p->btreeMask & (((yDbMask)1)<<u.at.iDb))!=0 );
65677 
65678   sqlite3BtreeGetMeta(db->aDb[u.at.iDb].pBt, u.at.iCookie, (u32 *)&u.at.iMeta);
65679   pOut->u.i = u.at.iMeta;
65680   break;
65681 }
65682 
65683 /* Opcode: SetCookie P1 P2 P3 * *
65684 **
65685 ** Write the content of register P3 (interpreted as an integer)
65686 ** into cookie number P2 of database P1.  P2==1 is the schema version.
65687 ** P2==2 is the database format. P2==3 is the recommended pager cache
65688 ** size, and so forth.  P1==0 is the main database file and P1==1 is the
65689 ** database file used to store temporary tables.
65690 **
65691 ** A transaction must be started before executing this opcode.
65692 */
65693 case OP_SetCookie: {       /* in3 */
65694 #if 0  /* local variables moved into u.au */
65695   Db *pDb;
65696 #endif /* local variables moved into u.au */
65697   assert( pOp->p2<SQLITE_N_BTREE_META );
65698   assert( pOp->p1>=0 && pOp->p1<db->nDb );
65699   assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
65700   u.au.pDb = &db->aDb[pOp->p1];
65701   assert( u.au.pDb->pBt!=0 );
65702   assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
65703   pIn3 = &aMem[pOp->p3];
65704   sqlite3VdbeMemIntegerify(pIn3);
65705   /* See note about index shifting on OP_ReadCookie */
65706   rc = sqlite3BtreeUpdateMeta(u.au.pDb->pBt, pOp->p2, (int)pIn3->u.i);
65707   if( pOp->p2==BTREE_SCHEMA_VERSION ){
65708     /* When the schema cookie changes, record the new cookie internally */
65709     u.au.pDb->pSchema->schema_cookie = (int)pIn3->u.i;
65710     db->flags |= SQLITE_InternChanges;
65711   }else if( pOp->p2==BTREE_FILE_FORMAT ){
65712     /* Record changes in the file format */
65713     u.au.pDb->pSchema->file_format = (u8)pIn3->u.i;
65714   }
65715   if( pOp->p1==1 ){
65716     /* Invalidate all prepared statements whenever the TEMP database
65717     ** schema is changed.  Ticket #1644 */
65718     sqlite3ExpirePreparedStatements(db);
65719     p->expired = 0;
65720   }
65721   break;
65722 }
65723 
65724 /* Opcode: VerifyCookie P1 P2 P3 * *
65725 **
65726 ** Check the value of global database parameter number 0 (the
65727 ** schema version) and make sure it is equal to P2 and that the
65728 ** generation counter on the local schema parse equals P3.
65729 **
65730 ** P1 is the database number which is 0 for the main database file
65731 ** and 1 for the file holding temporary tables and some higher number
65732 ** for auxiliary databases.
65733 **
65734 ** The cookie changes its value whenever the database schema changes.
65735 ** This operation is used to detect when that the cookie has changed
65736 ** and that the current process needs to reread the schema.
65737 **
65738 ** Either a transaction needs to have been started or an OP_Open needs
65739 ** to be executed (to establish a read lock) before this opcode is
65740 ** invoked.
65741 */
65742 case OP_VerifyCookie: {
65743 #if 0  /* local variables moved into u.av */
65744   int iMeta;
65745   int iGen;
65746   Btree *pBt;
65747 #endif /* local variables moved into u.av */
65748 
65749   assert( pOp->p1>=0 && pOp->p1<db->nDb );
65750   assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
65751   assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
65752   u.av.pBt = db->aDb[pOp->p1].pBt;
65753   if( u.av.pBt ){
65754     sqlite3BtreeGetMeta(u.av.pBt, BTREE_SCHEMA_VERSION, (u32 *)&u.av.iMeta);
65755     u.av.iGen = db->aDb[pOp->p1].pSchema->iGeneration;
65756   }else{
65757     u.av.iGen = u.av.iMeta = 0;
65758   }
65759   if( u.av.iMeta!=pOp->p2 || u.av.iGen!=pOp->p3 ){
65760     sqlite3DbFree(db, p->zErrMsg);
65761     p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed");
65762     /* If the schema-cookie from the database file matches the cookie
65763     ** stored with the in-memory representation of the schema, do
65764     ** not reload the schema from the database file.
65765     **
65766     ** If virtual-tables are in use, this is not just an optimization.
65767     ** Often, v-tables store their data in other SQLite tables, which
65768     ** are queried from within xNext() and other v-table methods using
65769     ** prepared queries. If such a query is out-of-date, we do not want to
65770     ** discard the database schema, as the user code implementing the
65771     ** v-table would have to be ready for the sqlite3_vtab structure itself
65772     ** to be invalidated whenever sqlite3_step() is called from within
65773     ** a v-table method.
65774     */
65775     if( db->aDb[pOp->p1].pSchema->schema_cookie!=u.av.iMeta ){
65776       sqlite3ResetInternalSchema(db, pOp->p1);
65777     }
65778 
65779     p->expired = 1;
65780     rc = SQLITE_SCHEMA;
65781   }
65782   break;
65783 }
65784 
65785 /* Opcode: OpenRead P1 P2 P3 P4 P5
65786 **
65787 ** Open a read-only cursor for the database table whose root page is
65788 ** P2 in a database file.  The database file is determined by P3.
65789 ** P3==0 means the main database, P3==1 means the database used for
65790 ** temporary tables, and P3>1 means used the corresponding attached
65791 ** database.  Give the new cursor an identifier of P1.  The P1
65792 ** values need not be contiguous but all P1 values should be small integers.
65793 ** It is an error for P1 to be negative.
65794 **
65795 ** If P5!=0 then use the content of register P2 as the root page, not
65796 ** the value of P2 itself.
65797 **
65798 ** There will be a read lock on the database whenever there is an
65799 ** open cursor.  If the database was unlocked prior to this instruction
65800 ** then a read lock is acquired as part of this instruction.  A read
65801 ** lock allows other processes to read the database but prohibits
65802 ** any other process from modifying the database.  The read lock is
65803 ** released when all cursors are closed.  If this instruction attempts
65804 ** to get a read lock but fails, the script terminates with an
65805 ** SQLITE_BUSY error code.
65806 **
65807 ** The P4 value may be either an integer (P4_INT32) or a pointer to
65808 ** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
65809 ** structure, then said structure defines the content and collating
65810 ** sequence of the index being opened. Otherwise, if P4 is an integer
65811 ** value, it is set to the number of columns in the table.
65812 **
65813 ** See also OpenWrite.
65814 */
65815 /* Opcode: OpenWrite P1 P2 P3 P4 P5
65816 **
65817 ** Open a read/write cursor named P1 on the table or index whose root
65818 ** page is P2.  Or if P5!=0 use the content of register P2 to find the
65819 ** root page.
65820 **
65821 ** The P4 value may be either an integer (P4_INT32) or a pointer to
65822 ** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
65823 ** structure, then said structure defines the content and collating
65824 ** sequence of the index being opened. Otherwise, if P4 is an integer
65825 ** value, it is set to the number of columns in the table, or to the
65826 ** largest index of any column of the table that is actually used.
65827 **
65828 ** This instruction works just like OpenRead except that it opens the cursor
65829 ** in read/write mode.  For a given table, there can be one or more read-only
65830 ** cursors or a single read/write cursor but not both.
65831 **
65832 ** See also OpenRead.
65833 */
65834 case OP_OpenRead:
65835 case OP_OpenWrite: {
65836 #if 0  /* local variables moved into u.aw */
65837   int nField;
65838   KeyInfo *pKeyInfo;
65839   int p2;
65840   int iDb;
65841   int wrFlag;
65842   Btree *pX;
65843   VdbeCursor *pCur;
65844   Db *pDb;
65845 #endif /* local variables moved into u.aw */
65846 
65847   if( p->expired ){
65848     rc = SQLITE_ABORT;
65849     break;
65850   }
65851 
65852   u.aw.nField = 0;
65853   u.aw.pKeyInfo = 0;
65854   u.aw.p2 = pOp->p2;
65855   u.aw.iDb = pOp->p3;
65856   assert( u.aw.iDb>=0 && u.aw.iDb<db->nDb );
65857   assert( (p->btreeMask & (((yDbMask)1)<<u.aw.iDb))!=0 );
65858   u.aw.pDb = &db->aDb[u.aw.iDb];
65859   u.aw.pX = u.aw.pDb->pBt;
65860   assert( u.aw.pX!=0 );
65861   if( pOp->opcode==OP_OpenWrite ){
65862     u.aw.wrFlag = 1;
65863     assert( sqlite3SchemaMutexHeld(db, u.aw.iDb, 0) );
65864     if( u.aw.pDb->pSchema->file_format < p->minWriteFileFormat ){
65865       p->minWriteFileFormat = u.aw.pDb->pSchema->file_format;
65866     }
65867   }else{
65868     u.aw.wrFlag = 0;
65869   }
65870   if( pOp->p5 ){
65871     assert( u.aw.p2>0 );
65872     assert( u.aw.p2<=p->nMem );
65873     pIn2 = &aMem[u.aw.p2];
65874     assert( memIsValid(pIn2) );
65875     assert( (pIn2->flags & MEM_Int)!=0 );
65876     sqlite3VdbeMemIntegerify(pIn2);
65877     u.aw.p2 = (int)pIn2->u.i;
65878     /* The u.aw.p2 value always comes from a prior OP_CreateTable opcode and
65879     ** that opcode will always set the u.aw.p2 value to 2 or more or else fail.
65880     ** If there were a failure, the prepared statement would have halted
65881     ** before reaching this instruction. */
65882     if( NEVER(u.aw.p2<2) ) {
65883       rc = SQLITE_CORRUPT_BKPT;
65884       goto abort_due_to_error;
65885     }
65886   }
65887   if( pOp->p4type==P4_KEYINFO ){
65888     u.aw.pKeyInfo = pOp->p4.pKeyInfo;
65889     u.aw.pKeyInfo->enc = ENC(p->db);
65890     u.aw.nField = u.aw.pKeyInfo->nField+1;
65891   }else if( pOp->p4type==P4_INT32 ){
65892     u.aw.nField = pOp->p4.i;
65893   }
65894   assert( pOp->p1>=0 );
65895   u.aw.pCur = allocateCursor(p, pOp->p1, u.aw.nField, u.aw.iDb, 1);
65896   if( u.aw.pCur==0 ) goto no_mem;
65897   u.aw.pCur->nullRow = 1;
65898   u.aw.pCur->isOrdered = 1;
65899   rc = sqlite3BtreeCursor(u.aw.pX, u.aw.p2, u.aw.wrFlag, u.aw.pKeyInfo, u.aw.pCur->pCursor);
65900   u.aw.pCur->pKeyInfo = u.aw.pKeyInfo;
65901 
65902   /* Since it performs no memory allocation or IO, the only values that
65903   ** sqlite3BtreeCursor() may return are SQLITE_EMPTY and SQLITE_OK.
65904   ** SQLITE_EMPTY is only returned when attempting to open the table
65905   ** rooted at page 1 of a zero-byte database.  */
65906   assert( rc==SQLITE_EMPTY || rc==SQLITE_OK );
65907   if( rc==SQLITE_EMPTY ){
65908     u.aw.pCur->pCursor = 0;
65909     rc = SQLITE_OK;
65910   }
65911 
65912   /* Set the VdbeCursor.isTable and isIndex variables. Previous versions of
65913   ** SQLite used to check if the root-page flags were sane at this point
65914   ** and report database corruption if they were not, but this check has
65915   ** since moved into the btree layer.  */
65916   u.aw.pCur->isTable = pOp->p4type!=P4_KEYINFO;
65917   u.aw.pCur->isIndex = !u.aw.pCur->isTable;
65918   break;
65919 }
65920 
65921 /* Opcode: OpenEphemeral P1 P2 * P4 *
65922 **
65923 ** Open a new cursor P1 to a transient table.
65924 ** The cursor is always opened read/write even if
65925 ** the main database is read-only.  The ephemeral
65926 ** table is deleted automatically when the cursor is closed.
65927 **
65928 ** P2 is the number of columns in the ephemeral table.
65929 ** The cursor points to a BTree table if P4==0 and to a BTree index
65930 ** if P4 is not 0.  If P4 is not NULL, it points to a KeyInfo structure
65931 ** that defines the format of keys in the index.
65932 **
65933 ** This opcode was once called OpenTemp.  But that created
65934 ** confusion because the term "temp table", might refer either
65935 ** to a TEMP table at the SQL level, or to a table opened by
65936 ** this opcode.  Then this opcode was call OpenVirtual.  But
65937 ** that created confusion with the whole virtual-table idea.
65938 */
65939 /* Opcode: OpenAutoindex P1 P2 * P4 *
65940 **
65941 ** This opcode works the same as OP_OpenEphemeral.  It has a
65942 ** different name to distinguish its use.  Tables created using
65943 ** by this opcode will be used for automatically created transient
65944 ** indices in joins.
65945 */
65946 case OP_OpenAutoindex:
65947 case OP_OpenEphemeral: {
65948 #if 0  /* local variables moved into u.ax */
65949   VdbeCursor *pCx;
65950 #endif /* local variables moved into u.ax */
65951   static const int vfsFlags =
65952       SQLITE_OPEN_READWRITE |
65953       SQLITE_OPEN_CREATE |
65954       SQLITE_OPEN_EXCLUSIVE |
65955       SQLITE_OPEN_DELETEONCLOSE |
65956       SQLITE_OPEN_TRANSIENT_DB;
65957 
65958   assert( pOp->p1>=0 );
65959   u.ax.pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1);
65960   if( u.ax.pCx==0 ) goto no_mem;
65961   u.ax.pCx->nullRow = 1;
65962   rc = sqlite3BtreeOpen(db->pVfs, 0, db, &u.ax.pCx->pBt,
65963                         BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5, vfsFlags);
65964   if( rc==SQLITE_OK ){
65965     rc = sqlite3BtreeBeginTrans(u.ax.pCx->pBt, 1);
65966   }
65967   if( rc==SQLITE_OK ){
65968     /* If a transient index is required, create it by calling
65969     ** sqlite3BtreeCreateTable() with the BTREE_BLOBKEY flag before
65970     ** opening it. If a transient table is required, just use the
65971     ** automatically created table with root-page 1 (an BLOB_INTKEY table).
65972     */
65973     if( pOp->p4.pKeyInfo ){
65974       int pgno;
65975       assert( pOp->p4type==P4_KEYINFO );
65976       rc = sqlite3BtreeCreateTable(u.ax.pCx->pBt, &pgno, BTREE_BLOBKEY);
65977       if( rc==SQLITE_OK ){
65978         assert( pgno==MASTER_ROOT+1 );
65979         rc = sqlite3BtreeCursor(u.ax.pCx->pBt, pgno, 1,
65980                                 (KeyInfo*)pOp->p4.z, u.ax.pCx->pCursor);
65981         u.ax.pCx->pKeyInfo = pOp->p4.pKeyInfo;
65982         u.ax.pCx->pKeyInfo->enc = ENC(p->db);
65983       }
65984       u.ax.pCx->isTable = 0;
65985     }else{
65986       rc = sqlite3BtreeCursor(u.ax.pCx->pBt, MASTER_ROOT, 1, 0, u.ax.pCx->pCursor);
65987       u.ax.pCx->isTable = 1;
65988     }
65989   }
65990   u.ax.pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED);
65991   u.ax.pCx->isIndex = !u.ax.pCx->isTable;
65992   break;
65993 }
65994 
65995 /* Opcode: OpenPseudo P1 P2 P3 * *
65996 **
65997 ** Open a new cursor that points to a fake table that contains a single
65998 ** row of data.  The content of that one row in the content of memory
65999 ** register P2.  In other words, cursor P1 becomes an alias for the
66000 ** MEM_Blob content contained in register P2.
66001 **
66002 ** A pseudo-table created by this opcode is used to hold a single
66003 ** row output from the sorter so that the row can be decomposed into
66004 ** individual columns using the OP_Column opcode.  The OP_Column opcode
66005 ** is the only cursor opcode that works with a pseudo-table.
66006 **
66007 ** P3 is the number of fields in the records that will be stored by
66008 ** the pseudo-table.
66009 */
66010 case OP_OpenPseudo: {
66011 #if 0  /* local variables moved into u.ay */
66012   VdbeCursor *pCx;
66013 #endif /* local variables moved into u.ay */
66014 
66015   assert( pOp->p1>=0 );
66016   u.ay.pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, 0);
66017   if( u.ay.pCx==0 ) goto no_mem;
66018   u.ay.pCx->nullRow = 1;
66019   u.ay.pCx->pseudoTableReg = pOp->p2;
66020   u.ay.pCx->isTable = 1;
66021   u.ay.pCx->isIndex = 0;
66022   break;
66023 }
66024 
66025 /* Opcode: Close P1 * * * *
66026 **
66027 ** Close a cursor previously opened as P1.  If P1 is not
66028 ** currently open, this instruction is a no-op.
66029 */
66030 case OP_Close: {
66031   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
66032   sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]);
66033   p->apCsr[pOp->p1] = 0;
66034   break;
66035 }
66036 
66037 /* Opcode: SeekGe P1 P2 P3 P4 *
66038 **
66039 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
66040 ** use the value in register P3 as the key.  If cursor P1 refers
66041 ** to an SQL index, then P3 is the first in an array of P4 registers
66042 ** that are used as an unpacked index key.
66043 **
66044 ** Reposition cursor P1 so that  it points to the smallest entry that
66045 ** is greater than or equal to the key value. If there are no records
66046 ** greater than or equal to the key and P2 is not zero, then jump to P2.
66047 **
66048 ** See also: Found, NotFound, Distinct, SeekLt, SeekGt, SeekLe
66049 */
66050 /* Opcode: SeekGt P1 P2 P3 P4 *
66051 **
66052 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
66053 ** use the value in register P3 as a key. If cursor P1 refers
66054 ** to an SQL index, then P3 is the first in an array of P4 registers
66055 ** that are used as an unpacked index key.
66056 **
66057 ** Reposition cursor P1 so that  it points to the smallest entry that
66058 ** is greater than the key value. If there are no records greater than
66059 ** the key and P2 is not zero, then jump to P2.
66060 **
66061 ** See also: Found, NotFound, Distinct, SeekLt, SeekGe, SeekLe
66062 */
66063 /* Opcode: SeekLt P1 P2 P3 P4 *
66064 **
66065 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
66066 ** use the value in register P3 as a key. If cursor P1 refers
66067 ** to an SQL index, then P3 is the first in an array of P4 registers
66068 ** that are used as an unpacked index key.
66069 **
66070 ** Reposition cursor P1 so that  it points to the largest entry that
66071 ** is less than the key value. If there are no records less than
66072 ** the key and P2 is not zero, then jump to P2.
66073 **
66074 ** See also: Found, NotFound, Distinct, SeekGt, SeekGe, SeekLe
66075 */
66076 /* Opcode: SeekLe P1 P2 P3 P4 *
66077 **
66078 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
66079 ** use the value in register P3 as a key. If cursor P1 refers
66080 ** to an SQL index, then P3 is the first in an array of P4 registers
66081 ** that are used as an unpacked index key.
66082 **
66083 ** Reposition cursor P1 so that it points to the largest entry that
66084 ** is less than or equal to the key value. If there are no records
66085 ** less than or equal to the key and P2 is not zero, then jump to P2.
66086 **
66087 ** See also: Found, NotFound, Distinct, SeekGt, SeekGe, SeekLt
66088 */
66089 case OP_SeekLt:         /* jump, in3 */
66090 case OP_SeekLe:         /* jump, in3 */
66091 case OP_SeekGe:         /* jump, in3 */
66092 case OP_SeekGt: {       /* jump, in3 */
66093 #if 0  /* local variables moved into u.az */
66094   int res;
66095   int oc;
66096   VdbeCursor *pC;
66097   UnpackedRecord r;
66098   int nField;
66099   i64 iKey;      /* The rowid we are to seek to */
66100 #endif /* local variables moved into u.az */
66101 
66102   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
66103   assert( pOp->p2!=0 );
66104   u.az.pC = p->apCsr[pOp->p1];
66105   assert( u.az.pC!=0 );
66106   assert( u.az.pC->pseudoTableReg==0 );
66107   assert( OP_SeekLe == OP_SeekLt+1 );
66108   assert( OP_SeekGe == OP_SeekLt+2 );
66109   assert( OP_SeekGt == OP_SeekLt+3 );
66110   assert( u.az.pC->isOrdered );
66111   if( u.az.pC->pCursor!=0 ){
66112     u.az.oc = pOp->opcode;
66113     u.az.pC->nullRow = 0;
66114     if( u.az.pC->isTable ){
66115       /* The input value in P3 might be of any type: integer, real, string,
66116       ** blob, or NULL.  But it needs to be an integer before we can do
66117       ** the seek, so covert it. */
66118       pIn3 = &aMem[pOp->p3];
66119       applyNumericAffinity(pIn3);
66120       u.az.iKey = sqlite3VdbeIntValue(pIn3);
66121       u.az.pC->rowidIsValid = 0;
66122 
66123       /* If the P3 value could not be converted into an integer without
66124       ** loss of information, then special processing is required... */
66125       if( (pIn3->flags & MEM_Int)==0 ){
66126         if( (pIn3->flags & MEM_Real)==0 ){
66127           /* If the P3 value cannot be converted into any kind of a number,
66128           ** then the seek is not possible, so jump to P2 */
66129           pc = pOp->p2 - 1;
66130           break;
66131         }
66132         /* If we reach this point, then the P3 value must be a floating
66133         ** point number. */
66134         assert( (pIn3->flags & MEM_Real)!=0 );
66135 
66136         if( u.az.iKey==SMALLEST_INT64 && (pIn3->r<(double)u.az.iKey || pIn3->r>0) ){
66137           /* The P3 value is too large in magnitude to be expressed as an
66138           ** integer. */
66139           u.az.res = 1;
66140           if( pIn3->r<0 ){
66141             if( u.az.oc>=OP_SeekGe ){  assert( u.az.oc==OP_SeekGe || u.az.oc==OP_SeekGt );
66142               rc = sqlite3BtreeFirst(u.az.pC->pCursor, &u.az.res);
66143               if( rc!=SQLITE_OK ) goto abort_due_to_error;
66144             }
66145           }else{
66146             if( u.az.oc<=OP_SeekLe ){  assert( u.az.oc==OP_SeekLt || u.az.oc==OP_SeekLe );
66147               rc = sqlite3BtreeLast(u.az.pC->pCursor, &u.az.res);
66148               if( rc!=SQLITE_OK ) goto abort_due_to_error;
66149             }
66150           }
66151           if( u.az.res ){
66152             pc = pOp->p2 - 1;
66153           }
66154           break;
66155         }else if( u.az.oc==OP_SeekLt || u.az.oc==OP_SeekGe ){
66156           /* Use the ceiling() function to convert real->int */
66157           if( pIn3->r > (double)u.az.iKey ) u.az.iKey++;
66158         }else{
66159           /* Use the floor() function to convert real->int */
66160           assert( u.az.oc==OP_SeekLe || u.az.oc==OP_SeekGt );
66161           if( pIn3->r < (double)u.az.iKey ) u.az.iKey--;
66162         }
66163       }
66164       rc = sqlite3BtreeMovetoUnpacked(u.az.pC->pCursor, 0, (u64)u.az.iKey, 0, &u.az.res);
66165       if( rc!=SQLITE_OK ){
66166         goto abort_due_to_error;
66167       }
66168       if( u.az.res==0 ){
66169         u.az.pC->rowidIsValid = 1;
66170         u.az.pC->lastRowid = u.az.iKey;
66171       }
66172     }else{
66173       u.az.nField = pOp->p4.i;
66174       assert( pOp->p4type==P4_INT32 );
66175       assert( u.az.nField>0 );
66176       u.az.r.pKeyInfo = u.az.pC->pKeyInfo;
66177       u.az.r.nField = (u16)u.az.nField;
66178 
66179       /* The next line of code computes as follows, only faster:
66180       **   if( u.az.oc==OP_SeekGt || u.az.oc==OP_SeekLe ){
66181       **     u.az.r.flags = UNPACKED_INCRKEY;
66182       **   }else{
66183       **     u.az.r.flags = 0;
66184       **   }
66185       */
66186       u.az.r.flags = (u16)(UNPACKED_INCRKEY * (1 & (u.az.oc - OP_SeekLt)));
66187       assert( u.az.oc!=OP_SeekGt || u.az.r.flags==UNPACKED_INCRKEY );
66188       assert( u.az.oc!=OP_SeekLe || u.az.r.flags==UNPACKED_INCRKEY );
66189       assert( u.az.oc!=OP_SeekGe || u.az.r.flags==0 );
66190       assert( u.az.oc!=OP_SeekLt || u.az.r.flags==0 );
66191 
66192       u.az.r.aMem = &aMem[pOp->p3];
66193 #ifdef SQLITE_DEBUG
66194       { int i; for(i=0; i<u.az.r.nField; i++) assert( memIsValid(&u.az.r.aMem[i]) ); }
66195 #endif
66196       ExpandBlob(u.az.r.aMem);
66197       rc = sqlite3BtreeMovetoUnpacked(u.az.pC->pCursor, &u.az.r, 0, 0, &u.az.res);
66198       if( rc!=SQLITE_OK ){
66199         goto abort_due_to_error;
66200       }
66201       u.az.pC->rowidIsValid = 0;
66202     }
66203     u.az.pC->deferredMoveto = 0;
66204     u.az.pC->cacheStatus = CACHE_STALE;
66205 #ifdef SQLITE_TEST
66206     sqlite3_search_count++;
66207 #endif
66208     if( u.az.oc>=OP_SeekGe ){  assert( u.az.oc==OP_SeekGe || u.az.oc==OP_SeekGt );
66209       if( u.az.res<0 || (u.az.res==0 && u.az.oc==OP_SeekGt) ){
66210         rc = sqlite3BtreeNext(u.az.pC->pCursor, &u.az.res);
66211         if( rc!=SQLITE_OK ) goto abort_due_to_error;
66212         u.az.pC->rowidIsValid = 0;
66213       }else{
66214         u.az.res = 0;
66215       }
66216     }else{
66217       assert( u.az.oc==OP_SeekLt || u.az.oc==OP_SeekLe );
66218       if( u.az.res>0 || (u.az.res==0 && u.az.oc==OP_SeekLt) ){
66219         rc = sqlite3BtreePrevious(u.az.pC->pCursor, &u.az.res);
66220         if( rc!=SQLITE_OK ) goto abort_due_to_error;
66221         u.az.pC->rowidIsValid = 0;
66222       }else{
66223         /* u.az.res might be negative because the table is empty.  Check to
66224         ** see if this is the case.
66225         */
66226         u.az.res = sqlite3BtreeEof(u.az.pC->pCursor);
66227       }
66228     }
66229     assert( pOp->p2>0 );
66230     if( u.az.res ){
66231       pc = pOp->p2 - 1;
66232     }
66233   }else{
66234     /* This happens when attempting to open the sqlite3_master table
66235     ** for read access returns SQLITE_EMPTY. In this case always
66236     ** take the jump (since there are no records in the table).
66237     */
66238     pc = pOp->p2 - 1;
66239   }
66240   break;
66241 }
66242 
66243 /* Opcode: Seek P1 P2 * * *
66244 **
66245 ** P1 is an open table cursor and P2 is a rowid integer.  Arrange
66246 ** for P1 to move so that it points to the rowid given by P2.
66247 **
66248 ** This is actually a deferred seek.  Nothing actually happens until
66249 ** the cursor is used to read a record.  That way, if no reads
66250 ** occur, no unnecessary I/O happens.
66251 */
66252 case OP_Seek: {    /* in2 */
66253 #if 0  /* local variables moved into u.ba */
66254   VdbeCursor *pC;
66255 #endif /* local variables moved into u.ba */
66256 
66257   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
66258   u.ba.pC = p->apCsr[pOp->p1];
66259   assert( u.ba.pC!=0 );
66260   if( ALWAYS(u.ba.pC->pCursor!=0) ){
66261     assert( u.ba.pC->isTable );
66262     u.ba.pC->nullRow = 0;
66263     pIn2 = &aMem[pOp->p2];
66264     u.ba.pC->movetoTarget = sqlite3VdbeIntValue(pIn2);
66265     u.ba.pC->rowidIsValid = 0;
66266     u.ba.pC->deferredMoveto = 1;
66267   }
66268   break;
66269 }
66270 
66271 
66272 /* Opcode: Found P1 P2 P3 P4 *
66273 **
66274 ** If P4==0 then register P3 holds a blob constructed by MakeRecord.  If
66275 ** P4>0 then register P3 is the first of P4 registers that form an unpacked
66276 ** record.
66277 **
66278 ** Cursor P1 is on an index btree.  If the record identified by P3 and P4
66279 ** is a prefix of any entry in P1 then a jump is made to P2 and
66280 ** P1 is left pointing at the matching entry.
66281 */
66282 /* Opcode: NotFound P1 P2 P3 P4 *
66283 **
66284 ** If P4==0 then register P3 holds a blob constructed by MakeRecord.  If
66285 ** P4>0 then register P3 is the first of P4 registers that form an unpacked
66286 ** record.
66287 **
66288 ** Cursor P1 is on an index btree.  If the record identified by P3 and P4
66289 ** is not the prefix of any entry in P1 then a jump is made to P2.  If P1
66290 ** does contain an entry whose prefix matches the P3/P4 record then control
66291 ** falls through to the next instruction and P1 is left pointing at the
66292 ** matching entry.
66293 **
66294 ** See also: Found, NotExists, IsUnique
66295 */
66296 case OP_NotFound:       /* jump, in3 */
66297 case OP_Found: {        /* jump, in3 */
66298 #if 0  /* local variables moved into u.bb */
66299   int alreadyExists;
66300   VdbeCursor *pC;
66301   int res;
66302   UnpackedRecord *pIdxKey;
66303   UnpackedRecord r;
66304   char aTempRec[ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*3 + 7];
66305 #endif /* local variables moved into u.bb */
66306 
66307 #ifdef SQLITE_TEST
66308   sqlite3_found_count++;
66309 #endif
66310 
66311   u.bb.alreadyExists = 0;
66312   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
66313   assert( pOp->p4type==P4_INT32 );
66314   u.bb.pC = p->apCsr[pOp->p1];
66315   assert( u.bb.pC!=0 );
66316   pIn3 = &aMem[pOp->p3];
66317   if( ALWAYS(u.bb.pC->pCursor!=0) ){
66318 
66319     assert( u.bb.pC->isTable==0 );
66320     if( pOp->p4.i>0 ){
66321       u.bb.r.pKeyInfo = u.bb.pC->pKeyInfo;
66322       u.bb.r.nField = (u16)pOp->p4.i;
66323       u.bb.r.aMem = pIn3;
66324 #ifdef SQLITE_DEBUG
66325       { int i; for(i=0; i<u.bb.r.nField; i++) assert( memIsValid(&u.bb.r.aMem[i]) ); }
66326 #endif
66327       u.bb.r.flags = UNPACKED_PREFIX_MATCH;
66328       u.bb.pIdxKey = &u.bb.r;
66329     }else{
66330       assert( pIn3->flags & MEM_Blob );
66331       assert( (pIn3->flags & MEM_Zero)==0 );  /* zeroblobs already expanded */
66332       u.bb.pIdxKey = sqlite3VdbeRecordUnpack(u.bb.pC->pKeyInfo, pIn3->n, pIn3->z,
66333                                         u.bb.aTempRec, sizeof(u.bb.aTempRec));
66334       if( u.bb.pIdxKey==0 ){
66335         goto no_mem;
66336       }
66337       u.bb.pIdxKey->flags |= UNPACKED_PREFIX_MATCH;
66338     }
66339     rc = sqlite3BtreeMovetoUnpacked(u.bb.pC->pCursor, u.bb.pIdxKey, 0, 0, &u.bb.res);
66340     if( pOp->p4.i==0 ){
66341       sqlite3VdbeDeleteUnpackedRecord(u.bb.pIdxKey);
66342     }
66343     if( rc!=SQLITE_OK ){
66344       break;
66345     }
66346     u.bb.alreadyExists = (u.bb.res==0);
66347     u.bb.pC->deferredMoveto = 0;
66348     u.bb.pC->cacheStatus = CACHE_STALE;
66349   }
66350   if( pOp->opcode==OP_Found ){
66351     if( u.bb.alreadyExists ) pc = pOp->p2 - 1;
66352   }else{
66353     if( !u.bb.alreadyExists ) pc = pOp->p2 - 1;
66354   }
66355   break;
66356 }
66357 
66358 /* Opcode: IsUnique P1 P2 P3 P4 *
66359 **
66360 ** Cursor P1 is open on an index b-tree - that is to say, a btree which
66361 ** no data and where the key are records generated by OP_MakeRecord with
66362 ** the list field being the integer ROWID of the entry that the index
66363 ** entry refers to.
66364 **
66365 ** The P3 register contains an integer record number. Call this record
66366 ** number R. Register P4 is the first in a set of N contiguous registers
66367 ** that make up an unpacked index key that can be used with cursor P1.
66368 ** The value of N can be inferred from the cursor. N includes the rowid
66369 ** value appended to the end of the index record. This rowid value may
66370 ** or may not be the same as R.
66371 **
66372 ** If any of the N registers beginning with register P4 contains a NULL
66373 ** value, jump immediately to P2.
66374 **
66375 ** Otherwise, this instruction checks if cursor P1 contains an entry
66376 ** where the first (N-1) fields match but the rowid value at the end
66377 ** of the index entry is not R. If there is no such entry, control jumps
66378 ** to instruction P2. Otherwise, the rowid of the conflicting index
66379 ** entry is copied to register P3 and control falls through to the next
66380 ** instruction.
66381 **
66382 ** See also: NotFound, NotExists, Found
66383 */
66384 case OP_IsUnique: {        /* jump, in3 */
66385 #if 0  /* local variables moved into u.bc */
66386   u16 ii;
66387   VdbeCursor *pCx;
66388   BtCursor *pCrsr;
66389   u16 nField;
66390   Mem *aMx;
66391   UnpackedRecord r;                  /* B-Tree index search key */
66392   i64 R;                             /* Rowid stored in register P3 */
66393 #endif /* local variables moved into u.bc */
66394 
66395   pIn3 = &aMem[pOp->p3];
66396   u.bc.aMx = &aMem[pOp->p4.i];
66397   /* Assert that the values of parameters P1 and P4 are in range. */
66398   assert( pOp->p4type==P4_INT32 );
66399   assert( pOp->p4.i>0 && pOp->p4.i<=p->nMem );
66400   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
66401 
66402   /* Find the index cursor. */
66403   u.bc.pCx = p->apCsr[pOp->p1];
66404   assert( u.bc.pCx->deferredMoveto==0 );
66405   u.bc.pCx->seekResult = 0;
66406   u.bc.pCx->cacheStatus = CACHE_STALE;
66407   u.bc.pCrsr = u.bc.pCx->pCursor;
66408 
66409   /* If any of the values are NULL, take the jump. */
66410   u.bc.nField = u.bc.pCx->pKeyInfo->nField;
66411   for(u.bc.ii=0; u.bc.ii<u.bc.nField; u.bc.ii++){
66412     if( u.bc.aMx[u.bc.ii].flags & MEM_Null ){
66413       pc = pOp->p2 - 1;
66414       u.bc.pCrsr = 0;
66415       break;
66416     }
66417   }
66418   assert( (u.bc.aMx[u.bc.nField].flags & MEM_Null)==0 );
66419 
66420   if( u.bc.pCrsr!=0 ){
66421     /* Populate the index search key. */
66422     u.bc.r.pKeyInfo = u.bc.pCx->pKeyInfo;
66423     u.bc.r.nField = u.bc.nField + 1;
66424     u.bc.r.flags = UNPACKED_PREFIX_SEARCH;
66425     u.bc.r.aMem = u.bc.aMx;
66426 #ifdef SQLITE_DEBUG
66427     { int i; for(i=0; i<u.bc.r.nField; i++) assert( memIsValid(&u.bc.r.aMem[i]) ); }
66428 #endif
66429 
66430     /* Extract the value of u.bc.R from register P3. */
66431     sqlite3VdbeMemIntegerify(pIn3);
66432     u.bc.R = pIn3->u.i;
66433 
66434     /* Search the B-Tree index. If no conflicting record is found, jump
66435     ** to P2. Otherwise, copy the rowid of the conflicting record to
66436     ** register P3 and fall through to the next instruction.  */
66437     rc = sqlite3BtreeMovetoUnpacked(u.bc.pCrsr, &u.bc.r, 0, 0, &u.bc.pCx->seekResult);
66438     if( (u.bc.r.flags & UNPACKED_PREFIX_SEARCH) || u.bc.r.rowid==u.bc.R ){
66439       pc = pOp->p2 - 1;
66440     }else{
66441       pIn3->u.i = u.bc.r.rowid;
66442     }
66443   }
66444   break;
66445 }
66446 
66447 /* Opcode: NotExists P1 P2 P3 * *
66448 **
66449 ** Use the content of register P3 as an integer key.  If a record
66450 ** with that key does not exist in table of P1, then jump to P2.
66451 ** If the record does exist, then fall through.  The cursor is left
66452 ** pointing to the record if it exists.
66453 **
66454 ** The difference between this operation and NotFound is that this
66455 ** operation assumes the key is an integer and that P1 is a table whereas
66456 ** NotFound assumes key is a blob constructed from MakeRecord and
66457 ** P1 is an index.
66458 **
66459 ** See also: Found, NotFound, IsUnique
66460 */
66461 case OP_NotExists: {        /* jump, in3 */
66462 #if 0  /* local variables moved into u.bd */
66463   VdbeCursor *pC;
66464   BtCursor *pCrsr;
66465   int res;
66466   u64 iKey;
66467 #endif /* local variables moved into u.bd */
66468 
66469   pIn3 = &aMem[pOp->p3];
66470   assert( pIn3->flags & MEM_Int );
66471   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
66472   u.bd.pC = p->apCsr[pOp->p1];
66473   assert( u.bd.pC!=0 );
66474   assert( u.bd.pC->isTable );
66475   assert( u.bd.pC->pseudoTableReg==0 );
66476   u.bd.pCrsr = u.bd.pC->pCursor;
66477   if( u.bd.pCrsr!=0 ){
66478     u.bd.res = 0;
66479     u.bd.iKey = pIn3->u.i;
66480     rc = sqlite3BtreeMovetoUnpacked(u.bd.pCrsr, 0, u.bd.iKey, 0, &u.bd.res);
66481     u.bd.pC->lastRowid = pIn3->u.i;
66482     u.bd.pC->rowidIsValid = u.bd.res==0 ?1:0;
66483     u.bd.pC->nullRow = 0;
66484     u.bd.pC->cacheStatus = CACHE_STALE;
66485     u.bd.pC->deferredMoveto = 0;
66486     if( u.bd.res!=0 ){
66487       pc = pOp->p2 - 1;
66488       assert( u.bd.pC->rowidIsValid==0 );
66489     }
66490     u.bd.pC->seekResult = u.bd.res;
66491   }else{
66492     /* This happens when an attempt to open a read cursor on the
66493     ** sqlite_master table returns SQLITE_EMPTY.
66494     */
66495     pc = pOp->p2 - 1;
66496     assert( u.bd.pC->rowidIsValid==0 );
66497     u.bd.pC->seekResult = 0;
66498   }
66499   break;
66500 }
66501 
66502 /* Opcode: Sequence P1 P2 * * *
66503 **
66504 ** Find the next available sequence number for cursor P1.
66505 ** Write the sequence number into register P2.
66506 ** The sequence number on the cursor is incremented after this
66507 ** instruction.
66508 */
66509 case OP_Sequence: {           /* out2-prerelease */
66510   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
66511   assert( p->apCsr[pOp->p1]!=0 );
66512   pOut->u.i = p->apCsr[pOp->p1]->seqCount++;
66513   break;
66514 }
66515 
66516 
66517 /* Opcode: NewRowid P1 P2 P3 * *
66518 **
66519 ** Get a new integer record number (a.k.a "rowid") used as the key to a table.
66520 ** The record number is not previously used as a key in the database
66521 ** table that cursor P1 points to.  The new record number is written
66522 ** written to register P2.
66523 **
66524 ** If P3>0 then P3 is a register in the root frame of this VDBE that holds
66525 ** the largest previously generated record number. No new record numbers are
66526 ** allowed to be less than this value. When this value reaches its maximum,
66527 ** an SQLITE_FULL error is generated. The P3 register is updated with the '
66528 ** generated record number. This P3 mechanism is used to help implement the
66529 ** AUTOINCREMENT feature.
66530 */
66531 case OP_NewRowid: {           /* out2-prerelease */
66532 #if 0  /* local variables moved into u.be */
66533   i64 v;                 /* The new rowid */
66534   VdbeCursor *pC;        /* Cursor of table to get the new rowid */
66535   int res;               /* Result of an sqlite3BtreeLast() */
66536   int cnt;               /* Counter to limit the number of searches */
66537   Mem *pMem;             /* Register holding largest rowid for AUTOINCREMENT */
66538   VdbeFrame *pFrame;     /* Root frame of VDBE */
66539 #endif /* local variables moved into u.be */
66540 
66541   u.be.v = 0;
66542   u.be.res = 0;
66543   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
66544   u.be.pC = p->apCsr[pOp->p1];
66545   assert( u.be.pC!=0 );
66546   if( NEVER(u.be.pC->pCursor==0) ){
66547     /* The zero initialization above is all that is needed */
66548   }else{
66549     /* The next rowid or record number (different terms for the same
66550     ** thing) is obtained in a two-step algorithm.
66551     **
66552     ** First we attempt to find the largest existing rowid and add one
66553     ** to that.  But if the largest existing rowid is already the maximum
66554     ** positive integer, we have to fall through to the second
66555     ** probabilistic algorithm
66556     **
66557     ** The second algorithm is to select a rowid at random and see if
66558     ** it already exists in the table.  If it does not exist, we have
66559     ** succeeded.  If the random rowid does exist, we select a new one
66560     ** and try again, up to 100 times.
66561     */
66562     assert( u.be.pC->isTable );
66563 
66564 #ifdef SQLITE_32BIT_ROWID
66565 #   define MAX_ROWID 0x7fffffff
66566 #else
66567     /* Some compilers complain about constants of the form 0x7fffffffffffffff.
66568     ** Others complain about 0x7ffffffffffffffffLL.  The following macro seems
66569     ** to provide the constant while making all compilers happy.
66570     */
66571 #   define MAX_ROWID  (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff )
66572 #endif
66573 
66574     if( !u.be.pC->useRandomRowid ){
66575       u.be.v = sqlite3BtreeGetCachedRowid(u.be.pC->pCursor);
66576       if( u.be.v==0 ){
66577         rc = sqlite3BtreeLast(u.be.pC->pCursor, &u.be.res);
66578         if( rc!=SQLITE_OK ){
66579           goto abort_due_to_error;
66580         }
66581         if( u.be.res ){
66582           u.be.v = 1;   /* IMP: R-61914-48074 */
66583         }else{
66584           assert( sqlite3BtreeCursorIsValid(u.be.pC->pCursor) );
66585           rc = sqlite3BtreeKeySize(u.be.pC->pCursor, &u.be.v);
66586           assert( rc==SQLITE_OK );   /* Cannot fail following BtreeLast() */
66587           if( u.be.v==MAX_ROWID ){
66588             u.be.pC->useRandomRowid = 1;
66589           }else{
66590             u.be.v++;   /* IMP: R-29538-34987 */
66591           }
66592         }
66593       }
66594 
66595 #ifndef SQLITE_OMIT_AUTOINCREMENT
66596       if( pOp->p3 ){
66597         /* Assert that P3 is a valid memory cell. */
66598         assert( pOp->p3>0 );
66599         if( p->pFrame ){
66600           for(u.be.pFrame=p->pFrame; u.be.pFrame->pParent; u.be.pFrame=u.be.pFrame->pParent);
66601           /* Assert that P3 is a valid memory cell. */
66602           assert( pOp->p3<=u.be.pFrame->nMem );
66603           u.be.pMem = &u.be.pFrame->aMem[pOp->p3];
66604         }else{
66605           /* Assert that P3 is a valid memory cell. */
66606           assert( pOp->p3<=p->nMem );
66607           u.be.pMem = &aMem[pOp->p3];
66608           memAboutToChange(p, u.be.pMem);
66609         }
66610         assert( memIsValid(u.be.pMem) );
66611 
66612         REGISTER_TRACE(pOp->p3, u.be.pMem);
66613         sqlite3VdbeMemIntegerify(u.be.pMem);
66614         assert( (u.be.pMem->flags & MEM_Int)!=0 );  /* mem(P3) holds an integer */
66615         if( u.be.pMem->u.i==MAX_ROWID || u.be.pC->useRandomRowid ){
66616           rc = SQLITE_FULL;   /* IMP: R-12275-61338 */
66617           goto abort_due_to_error;
66618         }
66619         if( u.be.v<u.be.pMem->u.i+1 ){
66620           u.be.v = u.be.pMem->u.i + 1;
66621         }
66622         u.be.pMem->u.i = u.be.v;
66623       }
66624 #endif
66625 
66626       sqlite3BtreeSetCachedRowid(u.be.pC->pCursor, u.be.v<MAX_ROWID ? u.be.v+1 : 0);
66627     }
66628     if( u.be.pC->useRandomRowid ){
66629       /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the
66630       ** largest possible integer (9223372036854775807) then the database
66631       ** engine starts picking positive candidate ROWIDs at random until
66632       ** it finds one that is not previously used. */
66633       assert( pOp->p3==0 );  /* We cannot be in random rowid mode if this is
66634                              ** an AUTOINCREMENT table. */
66635       /* on the first attempt, simply do one more than previous */
66636       u.be.v = lastRowid;
66637       u.be.v &= (MAX_ROWID>>1); /* ensure doesn't go negative */
66638       u.be.v++; /* ensure non-zero */
66639       u.be.cnt = 0;
66640       while(   ((rc = sqlite3BtreeMovetoUnpacked(u.be.pC->pCursor, 0, (u64)u.be.v,
66641                                                  0, &u.be.res))==SQLITE_OK)
66642             && (u.be.res==0)
66643             && (++u.be.cnt<100)){
66644         /* collision - try another random rowid */
66645         sqlite3_randomness(sizeof(u.be.v), &u.be.v);
66646         if( u.be.cnt<5 ){
66647           /* try "small" random rowids for the initial attempts */
66648           u.be.v &= 0xffffff;
66649         }else{
66650           u.be.v &= (MAX_ROWID>>1); /* ensure doesn't go negative */
66651         }
66652         u.be.v++; /* ensure non-zero */
66653       }
66654       if( rc==SQLITE_OK && u.be.res==0 ){
66655         rc = SQLITE_FULL;   /* IMP: R-38219-53002 */
66656         goto abort_due_to_error;
66657       }
66658       assert( u.be.v>0 );  /* EV: R-40812-03570 */
66659     }
66660     u.be.pC->rowidIsValid = 0;
66661     u.be.pC->deferredMoveto = 0;
66662     u.be.pC->cacheStatus = CACHE_STALE;
66663   }
66664   pOut->u.i = u.be.v;
66665   break;
66666 }
66667 
66668 /* Opcode: Insert P1 P2 P3 P4 P5
66669 **
66670 ** Write an entry into the table of cursor P1.  A new entry is
66671 ** created if it doesn't already exist or the data for an existing
66672 ** entry is overwritten.  The data is the value MEM_Blob stored in register
66673 ** number P2. The key is stored in register P3. The key must
66674 ** be a MEM_Int.
66675 **
66676 ** If the OPFLAG_NCHANGE flag of P5 is set, then the row change count is
66677 ** incremented (otherwise not).  If the OPFLAG_LASTROWID flag of P5 is set,
66678 ** then rowid is stored for subsequent return by the
66679 ** sqlite3_last_insert_rowid() function (otherwise it is unmodified).
66680 **
66681 ** If the OPFLAG_USESEEKRESULT flag of P5 is set and if the result of
66682 ** the last seek operation (OP_NotExists) was a success, then this
66683 ** operation will not attempt to find the appropriate row before doing
66684 ** the insert but will instead overwrite the row that the cursor is
66685 ** currently pointing to.  Presumably, the prior OP_NotExists opcode
66686 ** has already positioned the cursor correctly.  This is an optimization
66687 ** that boosts performance by avoiding redundant seeks.
66688 **
66689 ** If the OPFLAG_ISUPDATE flag is set, then this opcode is part of an
66690 ** UPDATE operation.  Otherwise (if the flag is clear) then this opcode
66691 ** is part of an INSERT operation.  The difference is only important to
66692 ** the update hook.
66693 **
66694 ** Parameter P4 may point to a string containing the table-name, or
66695 ** may be NULL. If it is not NULL, then the update-hook
66696 ** (sqlite3.xUpdateCallback) is invoked following a successful insert.
66697 **
66698 ** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically
66699 ** allocated, then ownership of P2 is transferred to the pseudo-cursor
66700 ** and register P2 becomes ephemeral.  If the cursor is changed, the
66701 ** value of register P2 will then change.  Make sure this does not
66702 ** cause any problems.)
66703 **
66704 ** This instruction only works on tables.  The equivalent instruction
66705 ** for indices is OP_IdxInsert.
66706 */
66707 /* Opcode: InsertInt P1 P2 P3 P4 P5
66708 **
66709 ** This works exactly like OP_Insert except that the key is the
66710 ** integer value P3, not the value of the integer stored in register P3.
66711 */
66712 case OP_Insert:
66713 case OP_InsertInt: {
66714 #if 0  /* local variables moved into u.bf */
66715   Mem *pData;       /* MEM cell holding data for the record to be inserted */
66716   Mem *pKey;        /* MEM cell holding key  for the record */
66717   i64 iKey;         /* The integer ROWID or key for the record to be inserted */
66718   VdbeCursor *pC;   /* Cursor to table into which insert is written */
66719   int nZero;        /* Number of zero-bytes to append */
66720   int seekResult;   /* Result of prior seek or 0 if no USESEEKRESULT flag */
66721   const char *zDb;  /* database name - used by the update hook */
66722   const char *zTbl; /* Table name - used by the opdate hook */
66723   int op;           /* Opcode for update hook: SQLITE_UPDATE or SQLITE_INSERT */
66724 #endif /* local variables moved into u.bf */
66725 
66726   u.bf.pData = &aMem[pOp->p2];
66727   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
66728   assert( memIsValid(u.bf.pData) );
66729   u.bf.pC = p->apCsr[pOp->p1];
66730   assert( u.bf.pC!=0 );
66731   assert( u.bf.pC->pCursor!=0 );
66732   assert( u.bf.pC->pseudoTableReg==0 );
66733   assert( u.bf.pC->isTable );
66734   REGISTER_TRACE(pOp->p2, u.bf.pData);
66735 
66736   if( pOp->opcode==OP_Insert ){
66737     u.bf.pKey = &aMem[pOp->p3];
66738     assert( u.bf.pKey->flags & MEM_Int );
66739     assert( memIsValid(u.bf.pKey) );
66740     REGISTER_TRACE(pOp->p3, u.bf.pKey);
66741     u.bf.iKey = u.bf.pKey->u.i;
66742   }else{
66743     assert( pOp->opcode==OP_InsertInt );
66744     u.bf.iKey = pOp->p3;
66745   }
66746 
66747   if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
66748   if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = lastRowid = u.bf.iKey;
66749   if( u.bf.pData->flags & MEM_Null ){
66750     u.bf.pData->z = 0;
66751     u.bf.pData->n = 0;
66752   }else{
66753     assert( u.bf.pData->flags & (MEM_Blob|MEM_Str) );
66754   }
66755   u.bf.seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? u.bf.pC->seekResult : 0);
66756   if( u.bf.pData->flags & MEM_Zero ){
66757     u.bf.nZero = u.bf.pData->u.nZero;
66758   }else{
66759     u.bf.nZero = 0;
66760   }
66761   sqlite3BtreeSetCachedRowid(u.bf.pC->pCursor, 0);
66762   rc = sqlite3BtreeInsert(u.bf.pC->pCursor, 0, u.bf.iKey,
66763                           u.bf.pData->z, u.bf.pData->n, u.bf.nZero,
66764                           pOp->p5 & OPFLAG_APPEND, u.bf.seekResult
66765   );
66766   u.bf.pC->rowidIsValid = 0;
66767   u.bf.pC->deferredMoveto = 0;
66768   u.bf.pC->cacheStatus = CACHE_STALE;
66769 
66770   /* Invoke the update-hook if required. */
66771   if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){
66772     u.bf.zDb = db->aDb[u.bf.pC->iDb].zName;
66773     u.bf.zTbl = pOp->p4.z;
66774     u.bf.op = ((pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT);
66775     assert( u.bf.pC->isTable );
66776     db->xUpdateCallback(db->pUpdateArg, u.bf.op, u.bf.zDb, u.bf.zTbl, u.bf.iKey);
66777     assert( u.bf.pC->iDb>=0 );
66778   }
66779   break;
66780 }
66781 
66782 /* Opcode: Delete P1 P2 * P4 *
66783 **
66784 ** Delete the record at which the P1 cursor is currently pointing.
66785 **
66786 ** The cursor will be left pointing at either the next or the previous
66787 ** record in the table. If it is left pointing at the next record, then
66788 ** the next Next instruction will be a no-op.  Hence it is OK to delete
66789 ** a record from within an Next loop.
66790 **
66791 ** If the OPFLAG_NCHANGE flag of P2 is set, then the row change count is
66792 ** incremented (otherwise not).
66793 **
66794 ** P1 must not be pseudo-table.  It has to be a real table with
66795 ** multiple rows.
66796 **
66797 ** If P4 is not NULL, then it is the name of the table that P1 is
66798 ** pointing to.  The update hook will be invoked, if it exists.
66799 ** If P4 is not NULL then the P1 cursor must have been positioned
66800 ** using OP_NotFound prior to invoking this opcode.
66801 */
66802 case OP_Delete: {
66803 #if 0  /* local variables moved into u.bg */
66804   i64 iKey;
66805   VdbeCursor *pC;
66806 #endif /* local variables moved into u.bg */
66807 
66808   u.bg.iKey = 0;
66809   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
66810   u.bg.pC = p->apCsr[pOp->p1];
66811   assert( u.bg.pC!=0 );
66812   assert( u.bg.pC->pCursor!=0 );  /* Only valid for real tables, no pseudotables */
66813 
66814   /* If the update-hook will be invoked, set u.bg.iKey to the rowid of the
66815   ** row being deleted.
66816   */
66817   if( db->xUpdateCallback && pOp->p4.z ){
66818     assert( u.bg.pC->isTable );
66819     assert( u.bg.pC->rowidIsValid );  /* lastRowid set by previous OP_NotFound */
66820     u.bg.iKey = u.bg.pC->lastRowid;
66821   }
66822 
66823   /* The OP_Delete opcode always follows an OP_NotExists or OP_Last or
66824   ** OP_Column on the same table without any intervening operations that
66825   ** might move or invalidate the cursor.  Hence cursor u.bg.pC is always pointing
66826   ** to the row to be deleted and the sqlite3VdbeCursorMoveto() operation
66827   ** below is always a no-op and cannot fail.  We will run it anyhow, though,
66828   ** to guard against future changes to the code generator.
66829   **/
66830   assert( u.bg.pC->deferredMoveto==0 );
66831   rc = sqlite3VdbeCursorMoveto(u.bg.pC);
66832   if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
66833 
66834   sqlite3BtreeSetCachedRowid(u.bg.pC->pCursor, 0);
66835   rc = sqlite3BtreeDelete(u.bg.pC->pCursor);
66836   u.bg.pC->cacheStatus = CACHE_STALE;
66837 
66838   /* Invoke the update-hook if required. */
66839   if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){
66840     const char *zDb = db->aDb[u.bg.pC->iDb].zName;
66841     const char *zTbl = pOp->p4.z;
66842     db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, zTbl, u.bg.iKey);
66843     assert( u.bg.pC->iDb>=0 );
66844   }
66845   if( pOp->p2 & OPFLAG_NCHANGE ) p->nChange++;
66846   break;
66847 }
66848 /* Opcode: ResetCount * * * * *
66849 **
66850 ** The value of the change counter is copied to the database handle
66851 ** change counter (returned by subsequent calls to sqlite3_changes()).
66852 ** Then the VMs internal change counter resets to 0.
66853 ** This is used by trigger programs.
66854 */
66855 case OP_ResetCount: {
66856   sqlite3VdbeSetChanges(db, p->nChange);
66857   p->nChange = 0;
66858   break;
66859 }
66860 
66861 /* Opcode: RowData P1 P2 * * *
66862 **
66863 ** Write into register P2 the complete row data for cursor P1.
66864 ** There is no interpretation of the data.
66865 ** It is just copied onto the P2 register exactly as
66866 ** it is found in the database file.
66867 **
66868 ** If the P1 cursor must be pointing to a valid row (not a NULL row)
66869 ** of a real table, not a pseudo-table.
66870 */
66871 /* Opcode: RowKey P1 P2 * * *
66872 **
66873 ** Write into register P2 the complete row key for cursor P1.
66874 ** There is no interpretation of the data.
66875 ** The key is copied onto the P3 register exactly as
66876 ** it is found in the database file.
66877 **
66878 ** If the P1 cursor must be pointing to a valid row (not a NULL row)
66879 ** of a real table, not a pseudo-table.
66880 */
66881 case OP_RowKey:
66882 case OP_RowData: {
66883 #if 0  /* local variables moved into u.bh */
66884   VdbeCursor *pC;
66885   BtCursor *pCrsr;
66886   u32 n;
66887   i64 n64;
66888 #endif /* local variables moved into u.bh */
66889 
66890   pOut = &aMem[pOp->p2];
66891   memAboutToChange(p, pOut);
66892 
66893   /* Note that RowKey and RowData are really exactly the same instruction */
66894   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
66895   u.bh.pC = p->apCsr[pOp->p1];
66896   assert( u.bh.pC->isTable || pOp->opcode==OP_RowKey );
66897   assert( u.bh.pC->isIndex || pOp->opcode==OP_RowData );
66898   assert( u.bh.pC!=0 );
66899   assert( u.bh.pC->nullRow==0 );
66900   assert( u.bh.pC->pseudoTableReg==0 );
66901   assert( u.bh.pC->pCursor!=0 );
66902   u.bh.pCrsr = u.bh.pC->pCursor;
66903   assert( sqlite3BtreeCursorIsValid(u.bh.pCrsr) );
66904 
66905   /* The OP_RowKey and OP_RowData opcodes always follow OP_NotExists or
66906   ** OP_Rewind/Op_Next with no intervening instructions that might invalidate
66907   ** the cursor.  Hence the following sqlite3VdbeCursorMoveto() call is always
66908   ** a no-op and can never fail.  But we leave it in place as a safety.
66909   */
66910   assert( u.bh.pC->deferredMoveto==0 );
66911   rc = sqlite3VdbeCursorMoveto(u.bh.pC);
66912   if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
66913 
66914   if( u.bh.pC->isIndex ){
66915     assert( !u.bh.pC->isTable );
66916     rc = sqlite3BtreeKeySize(u.bh.pCrsr, &u.bh.n64);
66917     assert( rc==SQLITE_OK );    /* True because of CursorMoveto() call above */
66918     if( u.bh.n64>db->aLimit[SQLITE_LIMIT_LENGTH] ){
66919       goto too_big;
66920     }
66921     u.bh.n = (u32)u.bh.n64;
66922   }else{
66923     rc = sqlite3BtreeDataSize(u.bh.pCrsr, &u.bh.n);
66924     assert( rc==SQLITE_OK );    /* DataSize() cannot fail */
66925     if( u.bh.n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
66926       goto too_big;
66927     }
66928   }
66929   if( sqlite3VdbeMemGrow(pOut, u.bh.n, 0) ){
66930     goto no_mem;
66931   }
66932   pOut->n = u.bh.n;
66933   MemSetTypeFlag(pOut, MEM_Blob);
66934   if( u.bh.pC->isIndex ){
66935     rc = sqlite3BtreeKey(u.bh.pCrsr, 0, u.bh.n, pOut->z);
66936   }else{
66937     rc = sqlite3BtreeData(u.bh.pCrsr, 0, u.bh.n, pOut->z);
66938   }
66939   pOut->enc = SQLITE_UTF8;  /* In case the blob is ever cast to text */
66940   UPDATE_MAX_BLOBSIZE(pOut);
66941   break;
66942 }
66943 
66944 /* Opcode: Rowid P1 P2 * * *
66945 **
66946 ** Store in register P2 an integer which is the key of the table entry that
66947 ** P1 is currently point to.
66948 **
66949 ** P1 can be either an ordinary table or a virtual table.  There used to
66950 ** be a separate OP_VRowid opcode for use with virtual tables, but this
66951 ** one opcode now works for both table types.
66952 */
66953 case OP_Rowid: {                 /* out2-prerelease */
66954 #if 0  /* local variables moved into u.bi */
66955   VdbeCursor *pC;
66956   i64 v;
66957   sqlite3_vtab *pVtab;
66958   const sqlite3_module *pModule;
66959 #endif /* local variables moved into u.bi */
66960 
66961   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
66962   u.bi.pC = p->apCsr[pOp->p1];
66963   assert( u.bi.pC!=0 );
66964   assert( u.bi.pC->pseudoTableReg==0 );
66965   if( u.bi.pC->nullRow ){
66966     pOut->flags = MEM_Null;
66967     break;
66968   }else if( u.bi.pC->deferredMoveto ){
66969     u.bi.v = u.bi.pC->movetoTarget;
66970 #ifndef SQLITE_OMIT_VIRTUALTABLE
66971   }else if( u.bi.pC->pVtabCursor ){
66972     u.bi.pVtab = u.bi.pC->pVtabCursor->pVtab;
66973     u.bi.pModule = u.bi.pVtab->pModule;
66974     assert( u.bi.pModule->xRowid );
66975     rc = u.bi.pModule->xRowid(u.bi.pC->pVtabCursor, &u.bi.v);
66976     importVtabErrMsg(p, u.bi.pVtab);
66977 #endif /* SQLITE_OMIT_VIRTUALTABLE */
66978   }else{
66979     assert( u.bi.pC->pCursor!=0 );
66980     rc = sqlite3VdbeCursorMoveto(u.bi.pC);
66981     if( rc ) goto abort_due_to_error;
66982     if( u.bi.pC->rowidIsValid ){
66983       u.bi.v = u.bi.pC->lastRowid;
66984     }else{
66985       rc = sqlite3BtreeKeySize(u.bi.pC->pCursor, &u.bi.v);
66986       assert( rc==SQLITE_OK );  /* Always so because of CursorMoveto() above */
66987     }
66988   }
66989   pOut->u.i = u.bi.v;
66990   break;
66991 }
66992 
66993 /* Opcode: NullRow P1 * * * *
66994 **
66995 ** Move the cursor P1 to a null row.  Any OP_Column operations
66996 ** that occur while the cursor is on the null row will always
66997 ** write a NULL.
66998 */
66999 case OP_NullRow: {
67000 #if 0  /* local variables moved into u.bj */
67001   VdbeCursor *pC;
67002 #endif /* local variables moved into u.bj */
67003 
67004   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67005   u.bj.pC = p->apCsr[pOp->p1];
67006   assert( u.bj.pC!=0 );
67007   u.bj.pC->nullRow = 1;
67008   u.bj.pC->rowidIsValid = 0;
67009   if( u.bj.pC->pCursor ){
67010     sqlite3BtreeClearCursor(u.bj.pC->pCursor);
67011   }
67012   break;
67013 }
67014 
67015 /* Opcode: Last P1 P2 * * *
67016 **
67017 ** The next use of the Rowid or Column or Next instruction for P1
67018 ** will refer to the last entry in the database table or index.
67019 ** If the table or index is empty and P2>0, then jump immediately to P2.
67020 ** If P2 is 0 or if the table or index is not empty, fall through
67021 ** to the following instruction.
67022 */
67023 case OP_Last: {        /* jump */
67024 #if 0  /* local variables moved into u.bk */
67025   VdbeCursor *pC;
67026   BtCursor *pCrsr;
67027   int res;
67028 #endif /* local variables moved into u.bk */
67029 
67030   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67031   u.bk.pC = p->apCsr[pOp->p1];
67032   assert( u.bk.pC!=0 );
67033   u.bk.pCrsr = u.bk.pC->pCursor;
67034   if( u.bk.pCrsr==0 ){
67035     u.bk.res = 1;
67036   }else{
67037     rc = sqlite3BtreeLast(u.bk.pCrsr, &u.bk.res);
67038   }
67039   u.bk.pC->nullRow = (u8)u.bk.res;
67040   u.bk.pC->deferredMoveto = 0;
67041   u.bk.pC->rowidIsValid = 0;
67042   u.bk.pC->cacheStatus = CACHE_STALE;
67043   if( pOp->p2>0 && u.bk.res ){
67044     pc = pOp->p2 - 1;
67045   }
67046   break;
67047 }
67048 
67049 
67050 /* Opcode: Sort P1 P2 * * *
67051 **
67052 ** This opcode does exactly the same thing as OP_Rewind except that
67053 ** it increments an undocumented global variable used for testing.
67054 **
67055 ** Sorting is accomplished by writing records into a sorting index,
67056 ** then rewinding that index and playing it back from beginning to
67057 ** end.  We use the OP_Sort opcode instead of OP_Rewind to do the
67058 ** rewinding so that the global variable will be incremented and
67059 ** regression tests can determine whether or not the optimizer is
67060 ** correctly optimizing out sorts.
67061 */
67062 case OP_Sort: {        /* jump */
67063 #ifdef SQLITE_TEST
67064   sqlite3_sort_count++;
67065   sqlite3_search_count--;
67066 #endif
67067   p->aCounter[SQLITE_STMTSTATUS_SORT-1]++;
67068   /* Fall through into OP_Rewind */
67069 }
67070 /* Opcode: Rewind P1 P2 * * *
67071 **
67072 ** The next use of the Rowid or Column or Next instruction for P1
67073 ** will refer to the first entry in the database table or index.
67074 ** If the table or index is empty and P2>0, then jump immediately to P2.
67075 ** If P2 is 0 or if the table or index is not empty, fall through
67076 ** to the following instruction.
67077 */
67078 case OP_Rewind: {        /* jump */
67079 #if 0  /* local variables moved into u.bl */
67080   VdbeCursor *pC;
67081   BtCursor *pCrsr;
67082   int res;
67083 #endif /* local variables moved into u.bl */
67084 
67085   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67086   u.bl.pC = p->apCsr[pOp->p1];
67087   assert( u.bl.pC!=0 );
67088   u.bl.res = 1;
67089   if( (u.bl.pCrsr = u.bl.pC->pCursor)!=0 ){
67090     rc = sqlite3BtreeFirst(u.bl.pCrsr, &u.bl.res);
67091     u.bl.pC->atFirst = u.bl.res==0 ?1:0;
67092     u.bl.pC->deferredMoveto = 0;
67093     u.bl.pC->cacheStatus = CACHE_STALE;
67094     u.bl.pC->rowidIsValid = 0;
67095   }
67096   u.bl.pC->nullRow = (u8)u.bl.res;
67097   assert( pOp->p2>0 && pOp->p2<p->nOp );
67098   if( u.bl.res ){
67099     pc = pOp->p2 - 1;
67100   }
67101   break;
67102 }
67103 
67104 /* Opcode: Next P1 P2 * * P5
67105 **
67106 ** Advance cursor P1 so that it points to the next key/data pair in its
67107 ** table or index.  If there are no more key/value pairs then fall through
67108 ** to the following instruction.  But if the cursor advance was successful,
67109 ** jump immediately to P2.
67110 **
67111 ** The P1 cursor must be for a real table, not a pseudo-table.
67112 **
67113 ** If P5 is positive and the jump is taken, then event counter
67114 ** number P5-1 in the prepared statement is incremented.
67115 **
67116 ** See also: Prev
67117 */
67118 /* Opcode: Prev P1 P2 * * P5
67119 **
67120 ** Back up cursor P1 so that it points to the previous key/data pair in its
67121 ** table or index.  If there is no previous key/value pairs then fall through
67122 ** to the following instruction.  But if the cursor backup was successful,
67123 ** jump immediately to P2.
67124 **
67125 ** The P1 cursor must be for a real table, not a pseudo-table.
67126 **
67127 ** If P5 is positive and the jump is taken, then event counter
67128 ** number P5-1 in the prepared statement is incremented.
67129 */
67130 case OP_Prev:          /* jump */
67131 case OP_Next: {        /* jump */
67132 #if 0  /* local variables moved into u.bm */
67133   VdbeCursor *pC;
67134   BtCursor *pCrsr;
67135   int res;
67136 #endif /* local variables moved into u.bm */
67137 
67138   CHECK_FOR_INTERRUPT;
67139   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67140   assert( pOp->p5<=ArraySize(p->aCounter) );
67141   u.bm.pC = p->apCsr[pOp->p1];
67142   if( u.bm.pC==0 ){
67143     break;  /* See ticket #2273 */
67144   }
67145   u.bm.pCrsr = u.bm.pC->pCursor;
67146   if( u.bm.pCrsr==0 ){
67147     u.bm.pC->nullRow = 1;
67148     break;
67149   }
67150   u.bm.res = 1;
67151   assert( u.bm.pC->deferredMoveto==0 );
67152   rc = pOp->opcode==OP_Next ? sqlite3BtreeNext(u.bm.pCrsr, &u.bm.res) :
67153                               sqlite3BtreePrevious(u.bm.pCrsr, &u.bm.res);
67154   u.bm.pC->nullRow = (u8)u.bm.res;
67155   u.bm.pC->cacheStatus = CACHE_STALE;
67156   if( u.bm.res==0 ){
67157     pc = pOp->p2 - 1;
67158     if( pOp->p5 ) p->aCounter[pOp->p5-1]++;
67159 #ifdef SQLITE_TEST
67160     sqlite3_search_count++;
67161 #endif
67162   }
67163   u.bm.pC->rowidIsValid = 0;
67164   break;
67165 }
67166 
67167 /* Opcode: IdxInsert P1 P2 P3 * P5
67168 **
67169 ** Register P2 holds an SQL index key made using the
67170 ** MakeRecord instructions.  This opcode writes that key
67171 ** into the index P1.  Data for the entry is nil.
67172 **
67173 ** P3 is a flag that provides a hint to the b-tree layer that this
67174 ** insert is likely to be an append.
67175 **
67176 ** This instruction only works for indices.  The equivalent instruction
67177 ** for tables is OP_Insert.
67178 */
67179 case OP_IdxInsert: {        /* in2 */
67180 #if 0  /* local variables moved into u.bn */
67181   VdbeCursor *pC;
67182   BtCursor *pCrsr;
67183   int nKey;
67184   const char *zKey;
67185 #endif /* local variables moved into u.bn */
67186 
67187   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67188   u.bn.pC = p->apCsr[pOp->p1];
67189   assert( u.bn.pC!=0 );
67190   pIn2 = &aMem[pOp->p2];
67191   assert( pIn2->flags & MEM_Blob );
67192   u.bn.pCrsr = u.bn.pC->pCursor;
67193   if( ALWAYS(u.bn.pCrsr!=0) ){
67194     assert( u.bn.pC->isTable==0 );
67195     rc = ExpandBlob(pIn2);
67196     if( rc==SQLITE_OK ){
67197       u.bn.nKey = pIn2->n;
67198       u.bn.zKey = pIn2->z;
67199       rc = sqlite3BtreeInsert(u.bn.pCrsr, u.bn.zKey, u.bn.nKey, "", 0, 0, pOp->p3,
67200           ((pOp->p5 & OPFLAG_USESEEKRESULT) ? u.bn.pC->seekResult : 0)
67201       );
67202       assert( u.bn.pC->deferredMoveto==0 );
67203       u.bn.pC->cacheStatus = CACHE_STALE;
67204     }
67205   }
67206   break;
67207 }
67208 
67209 /* Opcode: IdxDelete P1 P2 P3 * *
67210 **
67211 ** The content of P3 registers starting at register P2 form
67212 ** an unpacked index key. This opcode removes that entry from the
67213 ** index opened by cursor P1.
67214 */
67215 case OP_IdxDelete: {
67216 #if 0  /* local variables moved into u.bo */
67217   VdbeCursor *pC;
67218   BtCursor *pCrsr;
67219   int res;
67220   UnpackedRecord r;
67221 #endif /* local variables moved into u.bo */
67222 
67223   assert( pOp->p3>0 );
67224   assert( pOp->p2>0 && pOp->p2+pOp->p3<=p->nMem+1 );
67225   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67226   u.bo.pC = p->apCsr[pOp->p1];
67227   assert( u.bo.pC!=0 );
67228   u.bo.pCrsr = u.bo.pC->pCursor;
67229   if( ALWAYS(u.bo.pCrsr!=0) ){
67230     u.bo.r.pKeyInfo = u.bo.pC->pKeyInfo;
67231     u.bo.r.nField = (u16)pOp->p3;
67232     u.bo.r.flags = 0;
67233     u.bo.r.aMem = &aMem[pOp->p2];
67234 #ifdef SQLITE_DEBUG
67235     { int i; for(i=0; i<u.bo.r.nField; i++) assert( memIsValid(&u.bo.r.aMem[i]) ); }
67236 #endif
67237     rc = sqlite3BtreeMovetoUnpacked(u.bo.pCrsr, &u.bo.r, 0, 0, &u.bo.res);
67238     if( rc==SQLITE_OK && u.bo.res==0 ){
67239       rc = sqlite3BtreeDelete(u.bo.pCrsr);
67240     }
67241     assert( u.bo.pC->deferredMoveto==0 );
67242     u.bo.pC->cacheStatus = CACHE_STALE;
67243   }
67244   break;
67245 }
67246 
67247 /* Opcode: IdxRowid P1 P2 * * *
67248 **
67249 ** Write into register P2 an integer which is the last entry in the record at
67250 ** the end of the index key pointed to by cursor P1.  This integer should be
67251 ** the rowid of the table entry to which this index entry points.
67252 **
67253 ** See also: Rowid, MakeRecord.
67254 */
67255 case OP_IdxRowid: {              /* out2-prerelease */
67256 #if 0  /* local variables moved into u.bp */
67257   BtCursor *pCrsr;
67258   VdbeCursor *pC;
67259   i64 rowid;
67260 #endif /* local variables moved into u.bp */
67261 
67262   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67263   u.bp.pC = p->apCsr[pOp->p1];
67264   assert( u.bp.pC!=0 );
67265   u.bp.pCrsr = u.bp.pC->pCursor;
67266   pOut->flags = MEM_Null;
67267   if( ALWAYS(u.bp.pCrsr!=0) ){
67268     rc = sqlite3VdbeCursorMoveto(u.bp.pC);
67269     if( NEVER(rc) ) goto abort_due_to_error;
67270     assert( u.bp.pC->deferredMoveto==0 );
67271     assert( u.bp.pC->isTable==0 );
67272     if( !u.bp.pC->nullRow ){
67273       rc = sqlite3VdbeIdxRowid(db, u.bp.pCrsr, &u.bp.rowid);
67274       if( rc!=SQLITE_OK ){
67275         goto abort_due_to_error;
67276       }
67277       pOut->u.i = u.bp.rowid;
67278       pOut->flags = MEM_Int;
67279     }
67280   }
67281   break;
67282 }
67283 
67284 /* Opcode: IdxGE P1 P2 P3 P4 P5
67285 **
67286 ** The P4 register values beginning with P3 form an unpacked index
67287 ** key that omits the ROWID.  Compare this key value against the index
67288 ** that P1 is currently pointing to, ignoring the ROWID on the P1 index.
67289 **
67290 ** If the P1 index entry is greater than or equal to the key value
67291 ** then jump to P2.  Otherwise fall through to the next instruction.
67292 **
67293 ** If P5 is non-zero then the key value is increased by an epsilon
67294 ** prior to the comparison.  This make the opcode work like IdxGT except
67295 ** that if the key from register P3 is a prefix of the key in the cursor,
67296 ** the result is false whereas it would be true with IdxGT.
67297 */
67298 /* Opcode: IdxLT P1 P2 P3 P4 P5
67299 **
67300 ** The P4 register values beginning with P3 form an unpacked index
67301 ** key that omits the ROWID.  Compare this key value against the index
67302 ** that P1 is currently pointing to, ignoring the ROWID on the P1 index.
67303 **
67304 ** If the P1 index entry is less than the key value then jump to P2.
67305 ** Otherwise fall through to the next instruction.
67306 **
67307 ** If P5 is non-zero then the key value is increased by an epsilon prior
67308 ** to the comparison.  This makes the opcode work like IdxLE.
67309 */
67310 case OP_IdxLT:          /* jump */
67311 case OP_IdxGE: {        /* jump */
67312 #if 0  /* local variables moved into u.bq */
67313   VdbeCursor *pC;
67314   int res;
67315   UnpackedRecord r;
67316 #endif /* local variables moved into u.bq */
67317 
67318   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67319   u.bq.pC = p->apCsr[pOp->p1];
67320   assert( u.bq.pC!=0 );
67321   assert( u.bq.pC->isOrdered );
67322   if( ALWAYS(u.bq.pC->pCursor!=0) ){
67323     assert( u.bq.pC->deferredMoveto==0 );
67324     assert( pOp->p5==0 || pOp->p5==1 );
67325     assert( pOp->p4type==P4_INT32 );
67326     u.bq.r.pKeyInfo = u.bq.pC->pKeyInfo;
67327     u.bq.r.nField = (u16)pOp->p4.i;
67328     if( pOp->p5 ){
67329       u.bq.r.flags = UNPACKED_INCRKEY | UNPACKED_IGNORE_ROWID;
67330     }else{
67331       u.bq.r.flags = UNPACKED_IGNORE_ROWID;
67332     }
67333     u.bq.r.aMem = &aMem[pOp->p3];
67334 #ifdef SQLITE_DEBUG
67335     { int i; for(i=0; i<u.bq.r.nField; i++) assert( memIsValid(&u.bq.r.aMem[i]) ); }
67336 #endif
67337     rc = sqlite3VdbeIdxKeyCompare(u.bq.pC, &u.bq.r, &u.bq.res);
67338     if( pOp->opcode==OP_IdxLT ){
67339       u.bq.res = -u.bq.res;
67340     }else{
67341       assert( pOp->opcode==OP_IdxGE );
67342       u.bq.res++;
67343     }
67344     if( u.bq.res>0 ){
67345       pc = pOp->p2 - 1 ;
67346     }
67347   }
67348   break;
67349 }
67350 
67351 /* Opcode: Destroy P1 P2 P3 * *
67352 **
67353 ** Delete an entire database table or index whose root page in the database
67354 ** file is given by P1.
67355 **
67356 ** The table being destroyed is in the main database file if P3==0.  If
67357 ** P3==1 then the table to be clear is in the auxiliary database file
67358 ** that is used to store tables create using CREATE TEMPORARY TABLE.
67359 **
67360 ** If AUTOVACUUM is enabled then it is possible that another root page
67361 ** might be moved into the newly deleted root page in order to keep all
67362 ** root pages contiguous at the beginning of the database.  The former
67363 ** value of the root page that moved - its value before the move occurred -
67364 ** is stored in register P2.  If no page
67365 ** movement was required (because the table being dropped was already
67366 ** the last one in the database) then a zero is stored in register P2.
67367 ** If AUTOVACUUM is disabled then a zero is stored in register P2.
67368 **
67369 ** See also: Clear
67370 */
67371 case OP_Destroy: {     /* out2-prerelease */
67372 #if 0  /* local variables moved into u.br */
67373   int iMoved;
67374   int iCnt;
67375   Vdbe *pVdbe;
67376   int iDb;
67377 #endif /* local variables moved into u.br */
67378 #ifndef SQLITE_OMIT_VIRTUALTABLE
67379   u.br.iCnt = 0;
67380   for(u.br.pVdbe=db->pVdbe; u.br.pVdbe; u.br.pVdbe = u.br.pVdbe->pNext){
67381     if( u.br.pVdbe->magic==VDBE_MAGIC_RUN && u.br.pVdbe->inVtabMethod<2 && u.br.pVdbe->pc>=0 ){
67382       u.br.iCnt++;
67383     }
67384   }
67385 #else
67386   u.br.iCnt = db->activeVdbeCnt;
67387 #endif
67388   pOut->flags = MEM_Null;
67389   if( u.br.iCnt>1 ){
67390     rc = SQLITE_LOCKED;
67391     p->errorAction = OE_Abort;
67392   }else{
67393     u.br.iDb = pOp->p3;
67394     assert( u.br.iCnt==1 );
67395     assert( (p->btreeMask & (((yDbMask)1)<<u.br.iDb))!=0 );
67396     rc = sqlite3BtreeDropTable(db->aDb[u.br.iDb].pBt, pOp->p1, &u.br.iMoved);
67397     pOut->flags = MEM_Int;
67398     pOut->u.i = u.br.iMoved;
67399 #ifndef SQLITE_OMIT_AUTOVACUUM
67400     if( rc==SQLITE_OK && u.br.iMoved!=0 ){
67401       sqlite3RootPageMoved(db, u.br.iDb, u.br.iMoved, pOp->p1);
67402       /* All OP_Destroy operations occur on the same btree */
67403       assert( resetSchemaOnFault==0 || resetSchemaOnFault==u.br.iDb+1 );
67404       resetSchemaOnFault = u.br.iDb+1;
67405     }
67406 #endif
67407   }
67408   break;
67409 }
67410 
67411 /* Opcode: Clear P1 P2 P3
67412 **
67413 ** Delete all contents of the database table or index whose root page
67414 ** in the database file is given by P1.  But, unlike Destroy, do not
67415 ** remove the table or index from the database file.
67416 **
67417 ** The table being clear is in the main database file if P2==0.  If
67418 ** P2==1 then the table to be clear is in the auxiliary database file
67419 ** that is used to store tables create using CREATE TEMPORARY TABLE.
67420 **
67421 ** If the P3 value is non-zero, then the table referred to must be an
67422 ** intkey table (an SQL table, not an index). In this case the row change
67423 ** count is incremented by the number of rows in the table being cleared.
67424 ** If P3 is greater than zero, then the value stored in register P3 is
67425 ** also incremented by the number of rows in the table being cleared.
67426 **
67427 ** See also: Destroy
67428 */
67429 case OP_Clear: {
67430 #if 0  /* local variables moved into u.bs */
67431   int nChange;
67432 #endif /* local variables moved into u.bs */
67433 
67434   u.bs.nChange = 0;
67435   assert( (p->btreeMask & (((yDbMask)1)<<pOp->p2))!=0 );
67436   rc = sqlite3BtreeClearTable(
67437       db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &u.bs.nChange : 0)
67438   );
67439   if( pOp->p3 ){
67440     p->nChange += u.bs.nChange;
67441     if( pOp->p3>0 ){
67442       assert( memIsValid(&aMem[pOp->p3]) );
67443       memAboutToChange(p, &aMem[pOp->p3]);
67444       aMem[pOp->p3].u.i += u.bs.nChange;
67445     }
67446   }
67447   break;
67448 }
67449 
67450 /* Opcode: CreateTable P1 P2 * * *
67451 **
67452 ** Allocate a new table in the main database file if P1==0 or in the
67453 ** auxiliary database file if P1==1 or in an attached database if
67454 ** P1>1.  Write the root page number of the new table into
67455 ** register P2
67456 **
67457 ** The difference between a table and an index is this:  A table must
67458 ** have a 4-byte integer key and can have arbitrary data.  An index
67459 ** has an arbitrary key but no data.
67460 **
67461 ** See also: CreateIndex
67462 */
67463 /* Opcode: CreateIndex P1 P2 * * *
67464 **
67465 ** Allocate a new index in the main database file if P1==0 or in the
67466 ** auxiliary database file if P1==1 or in an attached database if
67467 ** P1>1.  Write the root page number of the new table into
67468 ** register P2.
67469 **
67470 ** See documentation on OP_CreateTable for additional information.
67471 */
67472 case OP_CreateIndex:            /* out2-prerelease */
67473 case OP_CreateTable: {          /* out2-prerelease */
67474 #if 0  /* local variables moved into u.bt */
67475   int pgno;
67476   int flags;
67477   Db *pDb;
67478 #endif /* local variables moved into u.bt */
67479 
67480   u.bt.pgno = 0;
67481   assert( pOp->p1>=0 && pOp->p1<db->nDb );
67482   assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
67483   u.bt.pDb = &db->aDb[pOp->p1];
67484   assert( u.bt.pDb->pBt!=0 );
67485   if( pOp->opcode==OP_CreateTable ){
67486     /* u.bt.flags = BTREE_INTKEY; */
67487     u.bt.flags = BTREE_INTKEY;
67488   }else{
67489     u.bt.flags = BTREE_BLOBKEY;
67490   }
67491   rc = sqlite3BtreeCreateTable(u.bt.pDb->pBt, &u.bt.pgno, u.bt.flags);
67492   pOut->u.i = u.bt.pgno;
67493   break;
67494 }
67495 
67496 /* Opcode: ParseSchema P1 * * P4 *
67497 **
67498 ** Read and parse all entries from the SQLITE_MASTER table of database P1
67499 ** that match the WHERE clause P4.
67500 **
67501 ** This opcode invokes the parser to create a new virtual machine,
67502 ** then runs the new virtual machine.  It is thus a re-entrant opcode.
67503 */
67504 case OP_ParseSchema: {
67505 #if 0  /* local variables moved into u.bu */
67506   int iDb;
67507   const char *zMaster;
67508   char *zSql;
67509   InitData initData;
67510 #endif /* local variables moved into u.bu */
67511 
67512   /* Any prepared statement that invokes this opcode will hold mutexes
67513   ** on every btree.  This is a prerequisite for invoking
67514   ** sqlite3InitCallback().
67515   */
67516 #ifdef SQLITE_DEBUG
67517   for(u.bu.iDb=0; u.bu.iDb<db->nDb; u.bu.iDb++){
67518     assert( u.bu.iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[u.bu.iDb].pBt) );
67519   }
67520 #endif
67521 
67522   u.bu.iDb = pOp->p1;
67523   assert( u.bu.iDb>=0 && u.bu.iDb<db->nDb );
67524   assert( DbHasProperty(db, u.bu.iDb, DB_SchemaLoaded) );
67525   /* Used to be a conditional */ {
67526     u.bu.zMaster = SCHEMA_TABLE(u.bu.iDb);
67527     u.bu.initData.db = db;
67528     u.bu.initData.iDb = pOp->p1;
67529     u.bu.initData.pzErrMsg = &p->zErrMsg;
67530     u.bu.zSql = sqlite3MPrintf(db,
67531        "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s ORDER BY rowid",
67532        db->aDb[u.bu.iDb].zName, u.bu.zMaster, pOp->p4.z);
67533     if( u.bu.zSql==0 ){
67534       rc = SQLITE_NOMEM;
67535     }else{
67536       assert( db->init.busy==0 );
67537       db->init.busy = 1;
67538       u.bu.initData.rc = SQLITE_OK;
67539       assert( !db->mallocFailed );
67540       rc = sqlite3_exec(db, u.bu.zSql, sqlite3InitCallback, &u.bu.initData, 0);
67541       if( rc==SQLITE_OK ) rc = u.bu.initData.rc;
67542       sqlite3DbFree(db, u.bu.zSql);
67543       db->init.busy = 0;
67544     }
67545   }
67546   if( rc==SQLITE_NOMEM ){
67547     goto no_mem;
67548   }
67549   break;
67550 }
67551 
67552 #if !defined(SQLITE_OMIT_ANALYZE)
67553 /* Opcode: LoadAnalysis P1 * * * *
67554 **
67555 ** Read the sqlite_stat1 table for database P1 and load the content
67556 ** of that table into the internal index hash table.  This will cause
67557 ** the analysis to be used when preparing all subsequent queries.
67558 */
67559 case OP_LoadAnalysis: {
67560   assert( pOp->p1>=0 && pOp->p1<db->nDb );
67561   rc = sqlite3AnalysisLoad(db, pOp->p1);
67562   break;
67563 }
67564 #endif /* !defined(SQLITE_OMIT_ANALYZE) */
67565 
67566 /* Opcode: DropTable P1 * * P4 *
67567 **
67568 ** Remove the internal (in-memory) data structures that describe
67569 ** the table named P4 in database P1.  This is called after a table
67570 ** is dropped in order to keep the internal representation of the
67571 ** schema consistent with what is on disk.
67572 */
67573 case OP_DropTable: {
67574   sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z);
67575   break;
67576 }
67577 
67578 /* Opcode: DropIndex P1 * * P4 *
67579 **
67580 ** Remove the internal (in-memory) data structures that describe
67581 ** the index named P4 in database P1.  This is called after an index
67582 ** is dropped in order to keep the internal representation of the
67583 ** schema consistent with what is on disk.
67584 */
67585 case OP_DropIndex: {
67586   sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z);
67587   break;
67588 }
67589 
67590 /* Opcode: DropTrigger P1 * * P4 *
67591 **
67592 ** Remove the internal (in-memory) data structures that describe
67593 ** the trigger named P4 in database P1.  This is called after a trigger
67594 ** is dropped in order to keep the internal representation of the
67595 ** schema consistent with what is on disk.
67596 */
67597 case OP_DropTrigger: {
67598   sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z);
67599   break;
67600 }
67601 
67602 
67603 #ifndef SQLITE_OMIT_INTEGRITY_CHECK
67604 /* Opcode: IntegrityCk P1 P2 P3 * P5
67605 **
67606 ** Do an analysis of the currently open database.  Store in
67607 ** register P1 the text of an error message describing any problems.
67608 ** If no problems are found, store a NULL in register P1.
67609 **
67610 ** The register P3 contains the maximum number of allowed errors.
67611 ** At most reg(P3) errors will be reported.
67612 ** In other words, the analysis stops as soon as reg(P1) errors are
67613 ** seen.  Reg(P1) is updated with the number of errors remaining.
67614 **
67615 ** The root page numbers of all tables in the database are integer
67616 ** stored in reg(P1), reg(P1+1), reg(P1+2), ....  There are P2 tables
67617 ** total.
67618 **
67619 ** If P5 is not zero, the check is done on the auxiliary database
67620 ** file, not the main database file.
67621 **
67622 ** This opcode is used to implement the integrity_check pragma.
67623 */
67624 case OP_IntegrityCk: {
67625 #if 0  /* local variables moved into u.bv */
67626   int nRoot;      /* Number of tables to check.  (Number of root pages.) */
67627   int *aRoot;     /* Array of rootpage numbers for tables to be checked */
67628   int j;          /* Loop counter */
67629   int nErr;       /* Number of errors reported */
67630   char *z;        /* Text of the error report */
67631   Mem *pnErr;     /* Register keeping track of errors remaining */
67632 #endif /* local variables moved into u.bv */
67633 
67634   u.bv.nRoot = pOp->p2;
67635   assert( u.bv.nRoot>0 );
67636   u.bv.aRoot = sqlite3DbMallocRaw(db, sizeof(int)*(u.bv.nRoot+1) );
67637   if( u.bv.aRoot==0 ) goto no_mem;
67638   assert( pOp->p3>0 && pOp->p3<=p->nMem );
67639   u.bv.pnErr = &aMem[pOp->p3];
67640   assert( (u.bv.pnErr->flags & MEM_Int)!=0 );
67641   assert( (u.bv.pnErr->flags & (MEM_Str|MEM_Blob))==0 );
67642   pIn1 = &aMem[pOp->p1];
67643   for(u.bv.j=0; u.bv.j<u.bv.nRoot; u.bv.j++){
67644     u.bv.aRoot[u.bv.j] = (int)sqlite3VdbeIntValue(&pIn1[u.bv.j]);
67645   }
67646   u.bv.aRoot[u.bv.j] = 0;
67647   assert( pOp->p5<db->nDb );
67648   assert( (p->btreeMask & (((yDbMask)1)<<pOp->p5))!=0 );
67649   u.bv.z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, u.bv.aRoot, u.bv.nRoot,
67650                                  (int)u.bv.pnErr->u.i, &u.bv.nErr);
67651   sqlite3DbFree(db, u.bv.aRoot);
67652   u.bv.pnErr->u.i -= u.bv.nErr;
67653   sqlite3VdbeMemSetNull(pIn1);
67654   if( u.bv.nErr==0 ){
67655     assert( u.bv.z==0 );
67656   }else if( u.bv.z==0 ){
67657     goto no_mem;
67658   }else{
67659     sqlite3VdbeMemSetStr(pIn1, u.bv.z, -1, SQLITE_UTF8, sqlite3_free);
67660   }
67661   UPDATE_MAX_BLOBSIZE(pIn1);
67662   sqlite3VdbeChangeEncoding(pIn1, encoding);
67663   break;
67664 }
67665 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
67666 
67667 /* Opcode: RowSetAdd P1 P2 * * *
67668 **
67669 ** Insert the integer value held by register P2 into a boolean index
67670 ** held in register P1.
67671 **
67672 ** An assertion fails if P2 is not an integer.
67673 */
67674 case OP_RowSetAdd: {       /* in1, in2 */
67675   pIn1 = &aMem[pOp->p1];
67676   pIn2 = &aMem[pOp->p2];
67677   assert( (pIn2->flags & MEM_Int)!=0 );
67678   if( (pIn1->flags & MEM_RowSet)==0 ){
67679     sqlite3VdbeMemSetRowSet(pIn1);
67680     if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem;
67681   }
67682   sqlite3RowSetInsert(pIn1->u.pRowSet, pIn2->u.i);
67683   break;
67684 }
67685 
67686 /* Opcode: RowSetRead P1 P2 P3 * *
67687 **
67688 ** Extract the smallest value from boolean index P1 and put that value into
67689 ** register P3.  Or, if boolean index P1 is initially empty, leave P3
67690 ** unchanged and jump to instruction P2.
67691 */
67692 case OP_RowSetRead: {       /* jump, in1, out3 */
67693 #if 0  /* local variables moved into u.bw */
67694   i64 val;
67695 #endif /* local variables moved into u.bw */
67696   CHECK_FOR_INTERRUPT;
67697   pIn1 = &aMem[pOp->p1];
67698   if( (pIn1->flags & MEM_RowSet)==0
67699    || sqlite3RowSetNext(pIn1->u.pRowSet, &u.bw.val)==0
67700   ){
67701     /* The boolean index is empty */
67702     sqlite3VdbeMemSetNull(pIn1);
67703     pc = pOp->p2 - 1;
67704   }else{
67705     /* A value was pulled from the index */
67706     sqlite3VdbeMemSetInt64(&aMem[pOp->p3], u.bw.val);
67707   }
67708   break;
67709 }
67710 
67711 /* Opcode: RowSetTest P1 P2 P3 P4
67712 **
67713 ** Register P3 is assumed to hold a 64-bit integer value. If register P1
67714 ** contains a RowSet object and that RowSet object contains
67715 ** the value held in P3, jump to register P2. Otherwise, insert the
67716 ** integer in P3 into the RowSet and continue on to the
67717 ** next opcode.
67718 **
67719 ** The RowSet object is optimized for the case where successive sets
67720 ** of integers, where each set contains no duplicates. Each set
67721 ** of values is identified by a unique P4 value. The first set
67722 ** must have P4==0, the final set P4=-1.  P4 must be either -1 or
67723 ** non-negative.  For non-negative values of P4 only the lower 4
67724 ** bits are significant.
67725 **
67726 ** This allows optimizations: (a) when P4==0 there is no need to test
67727 ** the rowset object for P3, as it is guaranteed not to contain it,
67728 ** (b) when P4==-1 there is no need to insert the value, as it will
67729 ** never be tested for, and (c) when a value that is part of set X is
67730 ** inserted, there is no need to search to see if the same value was
67731 ** previously inserted as part of set X (only if it was previously
67732 ** inserted as part of some other set).
67733 */
67734 case OP_RowSetTest: {                     /* jump, in1, in3 */
67735 #if 0  /* local variables moved into u.bx */
67736   int iSet;
67737   int exists;
67738 #endif /* local variables moved into u.bx */
67739 
67740   pIn1 = &aMem[pOp->p1];
67741   pIn3 = &aMem[pOp->p3];
67742   u.bx.iSet = pOp->p4.i;
67743   assert( pIn3->flags&MEM_Int );
67744 
67745   /* If there is anything other than a rowset object in memory cell P1,
67746   ** delete it now and initialize P1 with an empty rowset
67747   */
67748   if( (pIn1->flags & MEM_RowSet)==0 ){
67749     sqlite3VdbeMemSetRowSet(pIn1);
67750     if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem;
67751   }
67752 
67753   assert( pOp->p4type==P4_INT32 );
67754   assert( u.bx.iSet==-1 || u.bx.iSet>=0 );
67755   if( u.bx.iSet ){
67756     u.bx.exists = sqlite3RowSetTest(pIn1->u.pRowSet,
67757                                (u8)(u.bx.iSet>=0 ? u.bx.iSet & 0xf : 0xff),
67758                                pIn3->u.i);
67759     if( u.bx.exists ){
67760       pc = pOp->p2 - 1;
67761       break;
67762     }
67763   }
67764   if( u.bx.iSet>=0 ){
67765     sqlite3RowSetInsert(pIn1->u.pRowSet, pIn3->u.i);
67766   }
67767   break;
67768 }
67769 
67770 
67771 #ifndef SQLITE_OMIT_TRIGGER
67772 
67773 /* Opcode: Program P1 P2 P3 P4 *
67774 **
67775 ** Execute the trigger program passed as P4 (type P4_SUBPROGRAM).
67776 **
67777 ** P1 contains the address of the memory cell that contains the first memory
67778 ** cell in an array of values used as arguments to the sub-program. P2
67779 ** contains the address to jump to if the sub-program throws an IGNORE
67780 ** exception using the RAISE() function. Register P3 contains the address
67781 ** of a memory cell in this (the parent) VM that is used to allocate the
67782 ** memory required by the sub-vdbe at runtime.
67783 **
67784 ** P4 is a pointer to the VM containing the trigger program.
67785 */
67786 case OP_Program: {        /* jump */
67787 #if 0  /* local variables moved into u.by */
67788   int nMem;               /* Number of memory registers for sub-program */
67789   int nByte;              /* Bytes of runtime space required for sub-program */
67790   Mem *pRt;               /* Register to allocate runtime space */
67791   Mem *pMem;              /* Used to iterate through memory cells */
67792   Mem *pEnd;              /* Last memory cell in new array */
67793   VdbeFrame *pFrame;      /* New vdbe frame to execute in */
67794   SubProgram *pProgram;   /* Sub-program to execute */
67795   void *t;                /* Token identifying trigger */
67796 #endif /* local variables moved into u.by */
67797 
67798   u.by.pProgram = pOp->p4.pProgram;
67799   u.by.pRt = &aMem[pOp->p3];
67800   assert( memIsValid(u.by.pRt) );
67801   assert( u.by.pProgram->nOp>0 );
67802 
67803   /* If the p5 flag is clear, then recursive invocation of triggers is
67804   ** disabled for backwards compatibility (p5 is set if this sub-program
67805   ** is really a trigger, not a foreign key action, and the flag set
67806   ** and cleared by the "PRAGMA recursive_triggers" command is clear).
67807   **
67808   ** It is recursive invocation of triggers, at the SQL level, that is
67809   ** disabled. In some cases a single trigger may generate more than one
67810   ** SubProgram (if the trigger may be executed with more than one different
67811   ** ON CONFLICT algorithm). SubProgram structures associated with a
67812   ** single trigger all have the same value for the SubProgram.token
67813   ** variable.  */
67814   if( pOp->p5 ){
67815     u.by.t = u.by.pProgram->token;
67816     for(u.by.pFrame=p->pFrame; u.by.pFrame && u.by.pFrame->token!=u.by.t; u.by.pFrame=u.by.pFrame->pParent);
67817     if( u.by.pFrame ) break;
67818   }
67819 
67820   if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){
67821     rc = SQLITE_ERROR;
67822     sqlite3SetString(&p->zErrMsg, db, "too many levels of trigger recursion");
67823     break;
67824   }
67825 
67826   /* Register u.by.pRt is used to store the memory required to save the state
67827   ** of the current program, and the memory required at runtime to execute
67828   ** the trigger program. If this trigger has been fired before, then u.by.pRt
67829   ** is already allocated. Otherwise, it must be initialized.  */
67830   if( (u.by.pRt->flags&MEM_Frame)==0 ){
67831     /* SubProgram.nMem is set to the number of memory cells used by the
67832     ** program stored in SubProgram.aOp. As well as these, one memory
67833     ** cell is required for each cursor used by the program. Set local
67834     ** variable u.by.nMem (and later, VdbeFrame.nChildMem) to this value.
67835     */
67836     u.by.nMem = u.by.pProgram->nMem + u.by.pProgram->nCsr;
67837     u.by.nByte = ROUND8(sizeof(VdbeFrame))
67838               + u.by.nMem * sizeof(Mem)
67839               + u.by.pProgram->nCsr * sizeof(VdbeCursor *);
67840     u.by.pFrame = sqlite3DbMallocZero(db, u.by.nByte);
67841     if( !u.by.pFrame ){
67842       goto no_mem;
67843     }
67844     sqlite3VdbeMemRelease(u.by.pRt);
67845     u.by.pRt->flags = MEM_Frame;
67846     u.by.pRt->u.pFrame = u.by.pFrame;
67847 
67848     u.by.pFrame->v = p;
67849     u.by.pFrame->nChildMem = u.by.nMem;
67850     u.by.pFrame->nChildCsr = u.by.pProgram->nCsr;
67851     u.by.pFrame->pc = pc;
67852     u.by.pFrame->aMem = p->aMem;
67853     u.by.pFrame->nMem = p->nMem;
67854     u.by.pFrame->apCsr = p->apCsr;
67855     u.by.pFrame->nCursor = p->nCursor;
67856     u.by.pFrame->aOp = p->aOp;
67857     u.by.pFrame->nOp = p->nOp;
67858     u.by.pFrame->token = u.by.pProgram->token;
67859 
67860     u.by.pEnd = &VdbeFrameMem(u.by.pFrame)[u.by.pFrame->nChildMem];
67861     for(u.by.pMem=VdbeFrameMem(u.by.pFrame); u.by.pMem!=u.by.pEnd; u.by.pMem++){
67862       u.by.pMem->flags = MEM_Null;
67863       u.by.pMem->db = db;
67864     }
67865   }else{
67866     u.by.pFrame = u.by.pRt->u.pFrame;
67867     assert( u.by.pProgram->nMem+u.by.pProgram->nCsr==u.by.pFrame->nChildMem );
67868     assert( u.by.pProgram->nCsr==u.by.pFrame->nChildCsr );
67869     assert( pc==u.by.pFrame->pc );
67870   }
67871 
67872   p->nFrame++;
67873   u.by.pFrame->pParent = p->pFrame;
67874   u.by.pFrame->lastRowid = lastRowid;
67875   u.by.pFrame->nChange = p->nChange;
67876   p->nChange = 0;
67877   p->pFrame = u.by.pFrame;
67878   p->aMem = aMem = &VdbeFrameMem(u.by.pFrame)[-1];
67879   p->nMem = u.by.pFrame->nChildMem;
67880   p->nCursor = (u16)u.by.pFrame->nChildCsr;
67881   p->apCsr = (VdbeCursor **)&aMem[p->nMem+1];
67882   p->aOp = aOp = u.by.pProgram->aOp;
67883   p->nOp = u.by.pProgram->nOp;
67884   pc = -1;
67885 
67886   break;
67887 }
67888 
67889 /* Opcode: Param P1 P2 * * *
67890 **
67891 ** This opcode is only ever present in sub-programs called via the
67892 ** OP_Program instruction. Copy a value currently stored in a memory
67893 ** cell of the calling (parent) frame to cell P2 in the current frames
67894 ** address space. This is used by trigger programs to access the new.*
67895 ** and old.* values.
67896 **
67897 ** The address of the cell in the parent frame is determined by adding
67898 ** the value of the P1 argument to the value of the P1 argument to the
67899 ** calling OP_Program instruction.
67900 */
67901 case OP_Param: {           /* out2-prerelease */
67902 #if 0  /* local variables moved into u.bz */
67903   VdbeFrame *pFrame;
67904   Mem *pIn;
67905 #endif /* local variables moved into u.bz */
67906   u.bz.pFrame = p->pFrame;
67907   u.bz.pIn = &u.bz.pFrame->aMem[pOp->p1 + u.bz.pFrame->aOp[u.bz.pFrame->pc].p1];
67908   sqlite3VdbeMemShallowCopy(pOut, u.bz.pIn, MEM_Ephem);
67909   break;
67910 }
67911 
67912 #endif /* #ifndef SQLITE_OMIT_TRIGGER */
67913 
67914 #ifndef SQLITE_OMIT_FOREIGN_KEY
67915 /* Opcode: FkCounter P1 P2 * * *
67916 **
67917 ** Increment a "constraint counter" by P2 (P2 may be negative or positive).
67918 ** If P1 is non-zero, the database constraint counter is incremented
67919 ** (deferred foreign key constraints). Otherwise, if P1 is zero, the
67920 ** statement counter is incremented (immediate foreign key constraints).
67921 */
67922 case OP_FkCounter: {
67923   if( pOp->p1 ){
67924     db->nDeferredCons += pOp->p2;
67925   }else{
67926     p->nFkConstraint += pOp->p2;
67927   }
67928   break;
67929 }
67930 
67931 /* Opcode: FkIfZero P1 P2 * * *
67932 **
67933 ** This opcode tests if a foreign key constraint-counter is currently zero.
67934 ** If so, jump to instruction P2. Otherwise, fall through to the next
67935 ** instruction.
67936 **
67937 ** If P1 is non-zero, then the jump is taken if the database constraint-counter
67938 ** is zero (the one that counts deferred constraint violations). If P1 is
67939 ** zero, the jump is taken if the statement constraint-counter is zero
67940 ** (immediate foreign key constraint violations).
67941 */
67942 case OP_FkIfZero: {         /* jump */
67943   if( pOp->p1 ){
67944     if( db->nDeferredCons==0 ) pc = pOp->p2-1;
67945   }else{
67946     if( p->nFkConstraint==0 ) pc = pOp->p2-1;
67947   }
67948   break;
67949 }
67950 #endif /* #ifndef SQLITE_OMIT_FOREIGN_KEY */
67951 
67952 #ifndef SQLITE_OMIT_AUTOINCREMENT
67953 /* Opcode: MemMax P1 P2 * * *
67954 **
67955 ** P1 is a register in the root frame of this VM (the root frame is
67956 ** different from the current frame if this instruction is being executed
67957 ** within a sub-program). Set the value of register P1 to the maximum of
67958 ** its current value and the value in register P2.
67959 **
67960 ** This instruction throws an error if the memory cell is not initially
67961 ** an integer.
67962 */
67963 case OP_MemMax: {        /* in2 */
67964 #if 0  /* local variables moved into u.ca */
67965   Mem *pIn1;
67966   VdbeFrame *pFrame;
67967 #endif /* local variables moved into u.ca */
67968   if( p->pFrame ){
67969     for(u.ca.pFrame=p->pFrame; u.ca.pFrame->pParent; u.ca.pFrame=u.ca.pFrame->pParent);
67970     u.ca.pIn1 = &u.ca.pFrame->aMem[pOp->p1];
67971   }else{
67972     u.ca.pIn1 = &aMem[pOp->p1];
67973   }
67974   assert( memIsValid(u.ca.pIn1) );
67975   sqlite3VdbeMemIntegerify(u.ca.pIn1);
67976   pIn2 = &aMem[pOp->p2];
67977   sqlite3VdbeMemIntegerify(pIn2);
67978   if( u.ca.pIn1->u.i<pIn2->u.i){
67979     u.ca.pIn1->u.i = pIn2->u.i;
67980   }
67981   break;
67982 }
67983 #endif /* SQLITE_OMIT_AUTOINCREMENT */
67984 
67985 /* Opcode: IfPos P1 P2 * * *
67986 **
67987 ** If the value of register P1 is 1 or greater, jump to P2.
67988 **
67989 ** It is illegal to use this instruction on a register that does
67990 ** not contain an integer.  An assertion fault will result if you try.
67991 */
67992 case OP_IfPos: {        /* jump, in1 */
67993   pIn1 = &aMem[pOp->p1];
67994   assert( pIn1->flags&MEM_Int );
67995   if( pIn1->u.i>0 ){
67996      pc = pOp->p2 - 1;
67997   }
67998   break;
67999 }
68000 
68001 /* Opcode: IfNeg P1 P2 * * *
68002 **
68003 ** If the value of register P1 is less than zero, jump to P2.
68004 **
68005 ** It is illegal to use this instruction on a register that does
68006 ** not contain an integer.  An assertion fault will result if you try.
68007 */
68008 case OP_IfNeg: {        /* jump, in1 */
68009   pIn1 = &aMem[pOp->p1];
68010   assert( pIn1->flags&MEM_Int );
68011   if( pIn1->u.i<0 ){
68012      pc = pOp->p2 - 1;
68013   }
68014   break;
68015 }
68016 
68017 /* Opcode: IfZero P1 P2 P3 * *
68018 **
68019 ** The register P1 must contain an integer.  Add literal P3 to the
68020 ** value in register P1.  If the result is exactly 0, jump to P2.
68021 **
68022 ** It is illegal to use this instruction on a register that does
68023 ** not contain an integer.  An assertion fault will result if you try.
68024 */
68025 case OP_IfZero: {        /* jump, in1 */
68026   pIn1 = &aMem[pOp->p1];
68027   assert( pIn1->flags&MEM_Int );
68028   pIn1->u.i += pOp->p3;
68029   if( pIn1->u.i==0 ){
68030      pc = pOp->p2 - 1;
68031   }
68032   break;
68033 }
68034 
68035 /* Opcode: AggStep * P2 P3 P4 P5
68036 **
68037 ** Execute the step function for an aggregate.  The
68038 ** function has P5 arguments.   P4 is a pointer to the FuncDef
68039 ** structure that specifies the function.  Use register
68040 ** P3 as the accumulator.
68041 **
68042 ** The P5 arguments are taken from register P2 and its
68043 ** successors.
68044 */
68045 case OP_AggStep: {
68046 #if 0  /* local variables moved into u.cb */
68047   int n;
68048   int i;
68049   Mem *pMem;
68050   Mem *pRec;
68051   sqlite3_context ctx;
68052   sqlite3_value **apVal;
68053 #endif /* local variables moved into u.cb */
68054 
68055   u.cb.n = pOp->p5;
68056   assert( u.cb.n>=0 );
68057   u.cb.pRec = &aMem[pOp->p2];
68058   u.cb.apVal = p->apArg;
68059   assert( u.cb.apVal || u.cb.n==0 );
68060   for(u.cb.i=0; u.cb.i<u.cb.n; u.cb.i++, u.cb.pRec++){
68061     assert( memIsValid(u.cb.pRec) );
68062     u.cb.apVal[u.cb.i] = u.cb.pRec;
68063     memAboutToChange(p, u.cb.pRec);
68064     sqlite3VdbeMemStoreType(u.cb.pRec);
68065   }
68066   u.cb.ctx.pFunc = pOp->p4.pFunc;
68067   assert( pOp->p3>0 && pOp->p3<=p->nMem );
68068   u.cb.ctx.pMem = u.cb.pMem = &aMem[pOp->p3];
68069   u.cb.pMem->n++;
68070   u.cb.ctx.s.flags = MEM_Null;
68071   u.cb.ctx.s.z = 0;
68072   u.cb.ctx.s.zMalloc = 0;
68073   u.cb.ctx.s.xDel = 0;
68074   u.cb.ctx.s.db = db;
68075   u.cb.ctx.isError = 0;
68076   u.cb.ctx.pColl = 0;
68077   if( u.cb.ctx.pFunc->flags & SQLITE_FUNC_NEEDCOLL ){
68078     assert( pOp>p->aOp );
68079     assert( pOp[-1].p4type==P4_COLLSEQ );
68080     assert( pOp[-1].opcode==OP_CollSeq );
68081     u.cb.ctx.pColl = pOp[-1].p4.pColl;
68082   }
68083   (u.cb.ctx.pFunc->xStep)(&u.cb.ctx, u.cb.n, u.cb.apVal); /* IMP: R-24505-23230 */
68084   if( u.cb.ctx.isError ){
68085     sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&u.cb.ctx.s));
68086     rc = u.cb.ctx.isError;
68087   }
68088 
68089   sqlite3VdbeMemRelease(&u.cb.ctx.s);
68090 
68091   break;
68092 }
68093 
68094 /* Opcode: AggFinal P1 P2 * P4 *
68095 **
68096 ** Execute the finalizer function for an aggregate.  P1 is
68097 ** the memory location that is the accumulator for the aggregate.
68098 **
68099 ** P2 is the number of arguments that the step function takes and
68100 ** P4 is a pointer to the FuncDef for this function.  The P2
68101 ** argument is not used by this opcode.  It is only there to disambiguate
68102 ** functions that can take varying numbers of arguments.  The
68103 ** P4 argument is only needed for the degenerate case where
68104 ** the step function was not previously called.
68105 */
68106 case OP_AggFinal: {
68107 #if 0  /* local variables moved into u.cc */
68108   Mem *pMem;
68109 #endif /* local variables moved into u.cc */
68110   assert( pOp->p1>0 && pOp->p1<=p->nMem );
68111   u.cc.pMem = &aMem[pOp->p1];
68112   assert( (u.cc.pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
68113   rc = sqlite3VdbeMemFinalize(u.cc.pMem, pOp->p4.pFunc);
68114   if( rc ){
68115     sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(u.cc.pMem));
68116   }
68117   sqlite3VdbeChangeEncoding(u.cc.pMem, encoding);
68118   UPDATE_MAX_BLOBSIZE(u.cc.pMem);
68119   if( sqlite3VdbeMemTooBig(u.cc.pMem) ){
68120     goto too_big;
68121   }
68122   break;
68123 }
68124 
68125 #ifndef SQLITE_OMIT_WAL
68126 /* Opcode: Checkpoint P1 P2 P3 * *
68127 **
68128 ** Checkpoint database P1. This is a no-op if P1 is not currently in
68129 ** WAL mode. Parameter P2 is one of SQLITE_CHECKPOINT_PASSIVE, FULL
68130 ** or RESTART.  Write 1 or 0 into mem[P3] if the checkpoint returns
68131 ** SQLITE_BUSY or not, respectively.  Write the number of pages in the
68132 ** WAL after the checkpoint into mem[P3+1] and the number of pages
68133 ** in the WAL that have been checkpointed after the checkpoint
68134 ** completes into mem[P3+2].  However on an error, mem[P3+1] and
68135 ** mem[P3+2] are initialized to -1.
68136 */
68137 case OP_Checkpoint: {
68138 #if 0  /* local variables moved into u.cd */
68139   int i;                          /* Loop counter */
68140   int aRes[3];                    /* Results */
68141   Mem *pMem;                      /* Write results here */
68142 #endif /* local variables moved into u.cd */
68143 
68144   u.cd.aRes[0] = 0;
68145   u.cd.aRes[1] = u.cd.aRes[2] = -1;
68146   assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE
68147        || pOp->p2==SQLITE_CHECKPOINT_FULL
68148        || pOp->p2==SQLITE_CHECKPOINT_RESTART
68149   );
68150   rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &u.cd.aRes[1], &u.cd.aRes[2]);
68151   if( rc==SQLITE_BUSY ){
68152     rc = SQLITE_OK;
68153     u.cd.aRes[0] = 1;
68154   }
68155   for(u.cd.i=0, u.cd.pMem = &aMem[pOp->p3]; u.cd.i<3; u.cd.i++, u.cd.pMem++){
68156     sqlite3VdbeMemSetInt64(u.cd.pMem, (i64)u.cd.aRes[u.cd.i]);
68157   }
68158   break;
68159 };
68160 #endif
68161 
68162 #ifndef SQLITE_OMIT_PRAGMA
68163 /* Opcode: JournalMode P1 P2 P3 * P5
68164 **
68165 ** Change the journal mode of database P1 to P3. P3 must be one of the
68166 ** PAGER_JOURNALMODE_XXX values. If changing between the various rollback
68167 ** modes (delete, truncate, persist, off and memory), this is a simple
68168 ** operation. No IO is required.
68169 **
68170 ** If changing into or out of WAL mode the procedure is more complicated.
68171 **
68172 ** Write a string containing the final journal-mode to register P2.
68173 */
68174 case OP_JournalMode: {    /* out2-prerelease */
68175 #if 0  /* local variables moved into u.ce */
68176   Btree *pBt;                     /* Btree to change journal mode of */
68177   Pager *pPager;                  /* Pager associated with pBt */
68178   int eNew;                       /* New journal mode */
68179   int eOld;                       /* The old journal mode */
68180   const char *zFilename;          /* Name of database file for pPager */
68181 #endif /* local variables moved into u.ce */
68182 
68183   u.ce.eNew = pOp->p3;
68184   assert( u.ce.eNew==PAGER_JOURNALMODE_DELETE
68185        || u.ce.eNew==PAGER_JOURNALMODE_TRUNCATE
68186        || u.ce.eNew==PAGER_JOURNALMODE_PERSIST
68187        || u.ce.eNew==PAGER_JOURNALMODE_OFF
68188        || u.ce.eNew==PAGER_JOURNALMODE_MEMORY
68189        || u.ce.eNew==PAGER_JOURNALMODE_WAL
68190        || u.ce.eNew==PAGER_JOURNALMODE_QUERY
68191   );
68192   assert( pOp->p1>=0 && pOp->p1<db->nDb );
68193 
68194   u.ce.pBt = db->aDb[pOp->p1].pBt;
68195   u.ce.pPager = sqlite3BtreePager(u.ce.pBt);
68196   u.ce.eOld = sqlite3PagerGetJournalMode(u.ce.pPager);
68197   if( u.ce.eNew==PAGER_JOURNALMODE_QUERY ) u.ce.eNew = u.ce.eOld;
68198   if( !sqlite3PagerOkToChangeJournalMode(u.ce.pPager) ) u.ce.eNew = u.ce.eOld;
68199 
68200 #ifndef SQLITE_OMIT_WAL
68201   u.ce.zFilename = sqlite3PagerFilename(u.ce.pPager);
68202 
68203   /* Do not allow a transition to journal_mode=WAL for a database
68204   ** in temporary storage or if the VFS does not support shared memory
68205   */
68206   if( u.ce.eNew==PAGER_JOURNALMODE_WAL
68207    && (u.ce.zFilename[0]==0                         /* Temp file */
68208        || !sqlite3PagerWalSupported(u.ce.pPager))   /* No shared-memory support */
68209   ){
68210     u.ce.eNew = u.ce.eOld;
68211   }
68212 
68213   if( (u.ce.eNew!=u.ce.eOld)
68214    && (u.ce.eOld==PAGER_JOURNALMODE_WAL || u.ce.eNew==PAGER_JOURNALMODE_WAL)
68215   ){
68216     if( !db->autoCommit || db->activeVdbeCnt>1 ){
68217       rc = SQLITE_ERROR;
68218       sqlite3SetString(&p->zErrMsg, db,
68219           "cannot change %s wal mode from within a transaction",
68220           (u.ce.eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of")
68221       );
68222       break;
68223     }else{
68224 
68225       if( u.ce.eOld==PAGER_JOURNALMODE_WAL ){
68226         /* If leaving WAL mode, close the log file. If successful, the call
68227         ** to PagerCloseWal() checkpoints and deletes the write-ahead-log
68228         ** file. An EXCLUSIVE lock may still be held on the database file
68229         ** after a successful return.
68230         */
68231         rc = sqlite3PagerCloseWal(u.ce.pPager);
68232         if( rc==SQLITE_OK ){
68233           sqlite3PagerSetJournalMode(u.ce.pPager, u.ce.eNew);
68234         }
68235       }else if( u.ce.eOld==PAGER_JOURNALMODE_MEMORY ){
68236         /* Cannot transition directly from MEMORY to WAL.  Use mode OFF
68237         ** as an intermediate */
68238         sqlite3PagerSetJournalMode(u.ce.pPager, PAGER_JOURNALMODE_OFF);
68239       }
68240 
68241       /* Open a transaction on the database file. Regardless of the journal
68242       ** mode, this transaction always uses a rollback journal.
68243       */
68244       assert( sqlite3BtreeIsInTrans(u.ce.pBt)==0 );
68245       if( rc==SQLITE_OK ){
68246         rc = sqlite3BtreeSetVersion(u.ce.pBt, (u.ce.eNew==PAGER_JOURNALMODE_WAL ? 2 : 1));
68247       }
68248     }
68249   }
68250 #endif /* ifndef SQLITE_OMIT_WAL */
68251 
68252   if( rc ){
68253     u.ce.eNew = u.ce.eOld;
68254   }
68255   u.ce.eNew = sqlite3PagerSetJournalMode(u.ce.pPager, u.ce.eNew);
68256 
68257   pOut = &aMem[pOp->p2];
68258   pOut->flags = MEM_Str|MEM_Static|MEM_Term;
68259   pOut->z = (char *)sqlite3JournalModename(u.ce.eNew);
68260   pOut->n = sqlite3Strlen30(pOut->z);
68261   pOut->enc = SQLITE_UTF8;
68262   sqlite3VdbeChangeEncoding(pOut, encoding);
68263   break;
68264 };
68265 #endif /* SQLITE_OMIT_PRAGMA */
68266 
68267 #if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
68268 /* Opcode: Vacuum * * * * *
68269 **
68270 ** Vacuum the entire database.  This opcode will cause other virtual
68271 ** machines to be created and run.  It may not be called from within
68272 ** a transaction.
68273 */
68274 case OP_Vacuum: {
68275   rc = sqlite3RunVacuum(&p->zErrMsg, db);
68276   break;
68277 }
68278 #endif
68279 
68280 #if !defined(SQLITE_OMIT_AUTOVACUUM)
68281 /* Opcode: IncrVacuum P1 P2 * * *
68282 **
68283 ** Perform a single step of the incremental vacuum procedure on
68284 ** the P1 database. If the vacuum has finished, jump to instruction
68285 ** P2. Otherwise, fall through to the next instruction.
68286 */
68287 case OP_IncrVacuum: {        /* jump */
68288 #if 0  /* local variables moved into u.cf */
68289   Btree *pBt;
68290 #endif /* local variables moved into u.cf */
68291 
68292   assert( pOp->p1>=0 && pOp->p1<db->nDb );
68293   assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
68294   u.cf.pBt = db->aDb[pOp->p1].pBt;
68295   rc = sqlite3BtreeIncrVacuum(u.cf.pBt);
68296   if( rc==SQLITE_DONE ){
68297     pc = pOp->p2 - 1;
68298     rc = SQLITE_OK;
68299   }
68300   break;
68301 }
68302 #endif
68303 
68304 /* Opcode: Expire P1 * * * *
68305 **
68306 ** Cause precompiled statements to become expired. An expired statement
68307 ** fails with an error code of SQLITE_SCHEMA if it is ever executed
68308 ** (via sqlite3_step()).
68309 **
68310 ** If P1 is 0, then all SQL statements become expired. If P1 is non-zero,
68311 ** then only the currently executing statement is affected.
68312 */
68313 case OP_Expire: {
68314   if( !pOp->p1 ){
68315     sqlite3ExpirePreparedStatements(db);
68316   }else{
68317     p->expired = 1;
68318   }
68319   break;
68320 }
68321 
68322 #ifndef SQLITE_OMIT_SHARED_CACHE
68323 /* Opcode: TableLock P1 P2 P3 P4 *
68324 **
68325 ** Obtain a lock on a particular table. This instruction is only used when
68326 ** the shared-cache feature is enabled.
68327 **
68328 ** P1 is the index of the database in sqlite3.aDb[] of the database
68329 ** on which the lock is acquired.  A readlock is obtained if P3==0 or
68330 ** a write lock if P3==1.
68331 **
68332 ** P2 contains the root-page of the table to lock.
68333 **
68334 ** P4 contains a pointer to the name of the table being locked. This is only
68335 ** used to generate an error message if the lock cannot be obtained.
68336 */
68337 case OP_TableLock: {
68338   u8 isWriteLock = (u8)pOp->p3;
68339   if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommitted) ){
68340     int p1 = pOp->p1;
68341     assert( p1>=0 && p1<db->nDb );
68342     assert( (p->btreeMask & (((yDbMask)1)<<p1))!=0 );
68343     assert( isWriteLock==0 || isWriteLock==1 );
68344     rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock);
68345     if( (rc&0xFF)==SQLITE_LOCKED ){
68346       const char *z = pOp->p4.z;
68347       sqlite3SetString(&p->zErrMsg, db, "database table is locked: %s", z);
68348     }
68349   }
68350   break;
68351 }
68352 #endif /* SQLITE_OMIT_SHARED_CACHE */
68353 
68354 #ifndef SQLITE_OMIT_VIRTUALTABLE
68355 /* Opcode: VBegin * * * P4 *
68356 **
68357 ** P4 may be a pointer to an sqlite3_vtab structure. If so, call the
68358 ** xBegin method for that table.
68359 **
68360 ** Also, whether or not P4 is set, check that this is not being called from
68361 ** within a callback to a virtual table xSync() method. If it is, the error
68362 ** code will be set to SQLITE_LOCKED.
68363 */
68364 case OP_VBegin: {
68365 #if 0  /* local variables moved into u.cg */
68366   VTable *pVTab;
68367 #endif /* local variables moved into u.cg */
68368   u.cg.pVTab = pOp->p4.pVtab;
68369   rc = sqlite3VtabBegin(db, u.cg.pVTab);
68370   if( u.cg.pVTab ) importVtabErrMsg(p, u.cg.pVTab->pVtab);
68371   break;
68372 }
68373 #endif /* SQLITE_OMIT_VIRTUALTABLE */
68374 
68375 #ifndef SQLITE_OMIT_VIRTUALTABLE
68376 /* Opcode: VCreate P1 * * P4 *
68377 **
68378 ** P4 is the name of a virtual table in database P1. Call the xCreate method
68379 ** for that table.
68380 */
68381 case OP_VCreate: {
68382   rc = sqlite3VtabCallCreate(db, pOp->p1, pOp->p4.z, &p->zErrMsg);
68383   break;
68384 }
68385 #endif /* SQLITE_OMIT_VIRTUALTABLE */
68386 
68387 #ifndef SQLITE_OMIT_VIRTUALTABLE
68388 /* Opcode: VDestroy P1 * * P4 *
68389 **
68390 ** P4 is the name of a virtual table in database P1.  Call the xDestroy method
68391 ** of that table.
68392 */
68393 case OP_VDestroy: {
68394   p->inVtabMethod = 2;
68395   rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z);
68396   p->inVtabMethod = 0;
68397   break;
68398 }
68399 #endif /* SQLITE_OMIT_VIRTUALTABLE */
68400 
68401 #ifndef SQLITE_OMIT_VIRTUALTABLE
68402 /* Opcode: VOpen P1 * * P4 *
68403 **
68404 ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
68405 ** P1 is a cursor number.  This opcode opens a cursor to the virtual
68406 ** table and stores that cursor in P1.
68407 */
68408 case OP_VOpen: {
68409 #if 0  /* local variables moved into u.ch */
68410   VdbeCursor *pCur;
68411   sqlite3_vtab_cursor *pVtabCursor;
68412   sqlite3_vtab *pVtab;
68413   sqlite3_module *pModule;
68414 #endif /* local variables moved into u.ch */
68415 
68416   u.ch.pCur = 0;
68417   u.ch.pVtabCursor = 0;
68418   u.ch.pVtab = pOp->p4.pVtab->pVtab;
68419   u.ch.pModule = (sqlite3_module *)u.ch.pVtab->pModule;
68420   assert(u.ch.pVtab && u.ch.pModule);
68421   rc = u.ch.pModule->xOpen(u.ch.pVtab, &u.ch.pVtabCursor);
68422   importVtabErrMsg(p, u.ch.pVtab);
68423   if( SQLITE_OK==rc ){
68424     /* Initialize sqlite3_vtab_cursor base class */
68425     u.ch.pVtabCursor->pVtab = u.ch.pVtab;
68426 
68427     /* Initialise vdbe cursor object */
68428     u.ch.pCur = allocateCursor(p, pOp->p1, 0, -1, 0);
68429     if( u.ch.pCur ){
68430       u.ch.pCur->pVtabCursor = u.ch.pVtabCursor;
68431       u.ch.pCur->pModule = u.ch.pVtabCursor->pVtab->pModule;
68432     }else{
68433       db->mallocFailed = 1;
68434       u.ch.pModule->xClose(u.ch.pVtabCursor);
68435     }
68436   }
68437   break;
68438 }
68439 #endif /* SQLITE_OMIT_VIRTUALTABLE */
68440 
68441 #ifndef SQLITE_OMIT_VIRTUALTABLE
68442 /* Opcode: VFilter P1 P2 P3 P4 *
68443 **
68444 ** P1 is a cursor opened using VOpen.  P2 is an address to jump to if
68445 ** the filtered result set is empty.
68446 **
68447 ** P4 is either NULL or a string that was generated by the xBestIndex
68448 ** method of the module.  The interpretation of the P4 string is left
68449 ** to the module implementation.
68450 **
68451 ** This opcode invokes the xFilter method on the virtual table specified
68452 ** by P1.  The integer query plan parameter to xFilter is stored in register
68453 ** P3. Register P3+1 stores the argc parameter to be passed to the
68454 ** xFilter method. Registers P3+2..P3+1+argc are the argc
68455 ** additional parameters which are passed to
68456 ** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter.
68457 **
68458 ** A jump is made to P2 if the result set after filtering would be empty.
68459 */
68460 case OP_VFilter: {   /* jump */
68461 #if 0  /* local variables moved into u.ci */
68462   int nArg;
68463   int iQuery;
68464   const sqlite3_module *pModule;
68465   Mem *pQuery;
68466   Mem *pArgc;
68467   sqlite3_vtab_cursor *pVtabCursor;
68468   sqlite3_vtab *pVtab;
68469   VdbeCursor *pCur;
68470   int res;
68471   int i;
68472   Mem **apArg;
68473 #endif /* local variables moved into u.ci */
68474 
68475   u.ci.pQuery = &aMem[pOp->p3];
68476   u.ci.pArgc = &u.ci.pQuery[1];
68477   u.ci.pCur = p->apCsr[pOp->p1];
68478   assert( memIsValid(u.ci.pQuery) );
68479   REGISTER_TRACE(pOp->p3, u.ci.pQuery);
68480   assert( u.ci.pCur->pVtabCursor );
68481   u.ci.pVtabCursor = u.ci.pCur->pVtabCursor;
68482   u.ci.pVtab = u.ci.pVtabCursor->pVtab;
68483   u.ci.pModule = u.ci.pVtab->pModule;
68484 
68485   /* Grab the index number and argc parameters */
68486   assert( (u.ci.pQuery->flags&MEM_Int)!=0 && u.ci.pArgc->flags==MEM_Int );
68487   u.ci.nArg = (int)u.ci.pArgc->u.i;
68488   u.ci.iQuery = (int)u.ci.pQuery->u.i;
68489 
68490   /* Invoke the xFilter method */
68491   {
68492     u.ci.res = 0;
68493     u.ci.apArg = p->apArg;
68494     for(u.ci.i = 0; u.ci.i<u.ci.nArg; u.ci.i++){
68495       u.ci.apArg[u.ci.i] = &u.ci.pArgc[u.ci.i+1];
68496       sqlite3VdbeMemStoreType(u.ci.apArg[u.ci.i]);
68497     }
68498 
68499     p->inVtabMethod = 1;
68500     rc = u.ci.pModule->xFilter(u.ci.pVtabCursor, u.ci.iQuery, pOp->p4.z, u.ci.nArg, u.ci.apArg);
68501     p->inVtabMethod = 0;
68502     importVtabErrMsg(p, u.ci.pVtab);
68503     if( rc==SQLITE_OK ){
68504       u.ci.res = u.ci.pModule->xEof(u.ci.pVtabCursor);
68505     }
68506 
68507     if( u.ci.res ){
68508       pc = pOp->p2 - 1;
68509     }
68510   }
68511   u.ci.pCur->nullRow = 0;
68512 
68513   break;
68514 }
68515 #endif /* SQLITE_OMIT_VIRTUALTABLE */
68516 
68517 #ifndef SQLITE_OMIT_VIRTUALTABLE
68518 /* Opcode: VColumn P1 P2 P3 * *
68519 **
68520 ** Store the value of the P2-th column of
68521 ** the row of the virtual-table that the
68522 ** P1 cursor is pointing to into register P3.
68523 */
68524 case OP_VColumn: {
68525 #if 0  /* local variables moved into u.cj */
68526   sqlite3_vtab *pVtab;
68527   const sqlite3_module *pModule;
68528   Mem *pDest;
68529   sqlite3_context sContext;
68530 #endif /* local variables moved into u.cj */
68531 
68532   VdbeCursor *pCur = p->apCsr[pOp->p1];
68533   assert( pCur->pVtabCursor );
68534   assert( pOp->p3>0 && pOp->p3<=p->nMem );
68535   u.cj.pDest = &aMem[pOp->p3];
68536   memAboutToChange(p, u.cj.pDest);
68537   if( pCur->nullRow ){
68538     sqlite3VdbeMemSetNull(u.cj.pDest);
68539     break;
68540   }
68541   u.cj.pVtab = pCur->pVtabCursor->pVtab;
68542   u.cj.pModule = u.cj.pVtab->pModule;
68543   assert( u.cj.pModule->xColumn );
68544   memset(&u.cj.sContext, 0, sizeof(u.cj.sContext));
68545 
68546   /* The output cell may already have a buffer allocated. Move
68547   ** the current contents to u.cj.sContext.s so in case the user-function
68548   ** can use the already allocated buffer instead of allocating a
68549   ** new one.
68550   */
68551   sqlite3VdbeMemMove(&u.cj.sContext.s, u.cj.pDest);
68552   MemSetTypeFlag(&u.cj.sContext.s, MEM_Null);
68553 
68554   rc = u.cj.pModule->xColumn(pCur->pVtabCursor, &u.cj.sContext, pOp->p2);
68555   importVtabErrMsg(p, u.cj.pVtab);
68556   if( u.cj.sContext.isError ){
68557     rc = u.cj.sContext.isError;
68558   }
68559 
68560   /* Copy the result of the function to the P3 register. We
68561   ** do this regardless of whether or not an error occurred to ensure any
68562   ** dynamic allocation in u.cj.sContext.s (a Mem struct) is  released.
68563   */
68564   sqlite3VdbeChangeEncoding(&u.cj.sContext.s, encoding);
68565   sqlite3VdbeMemMove(u.cj.pDest, &u.cj.sContext.s);
68566   REGISTER_TRACE(pOp->p3, u.cj.pDest);
68567   UPDATE_MAX_BLOBSIZE(u.cj.pDest);
68568 
68569   if( sqlite3VdbeMemTooBig(u.cj.pDest) ){
68570     goto too_big;
68571   }
68572   break;
68573 }
68574 #endif /* SQLITE_OMIT_VIRTUALTABLE */
68575 
68576 #ifndef SQLITE_OMIT_VIRTUALTABLE
68577 /* Opcode: VNext P1 P2 * * *
68578 **
68579 ** Advance virtual table P1 to the next row in its result set and
68580 ** jump to instruction P2.  Or, if the virtual table has reached
68581 ** the end of its result set, then fall through to the next instruction.
68582 */
68583 case OP_VNext: {   /* jump */
68584 #if 0  /* local variables moved into u.ck */
68585   sqlite3_vtab *pVtab;
68586   const sqlite3_module *pModule;
68587   int res;
68588   VdbeCursor *pCur;
68589 #endif /* local variables moved into u.ck */
68590 
68591   u.ck.res = 0;
68592   u.ck.pCur = p->apCsr[pOp->p1];
68593   assert( u.ck.pCur->pVtabCursor );
68594   if( u.ck.pCur->nullRow ){
68595     break;
68596   }
68597   u.ck.pVtab = u.ck.pCur->pVtabCursor->pVtab;
68598   u.ck.pModule = u.ck.pVtab->pModule;
68599   assert( u.ck.pModule->xNext );
68600 
68601   /* Invoke the xNext() method of the module. There is no way for the
68602   ** underlying implementation to return an error if one occurs during
68603   ** xNext(). Instead, if an error occurs, true is returned (indicating that
68604   ** data is available) and the error code returned when xColumn or
68605   ** some other method is next invoked on the save virtual table cursor.
68606   */
68607   p->inVtabMethod = 1;
68608   rc = u.ck.pModule->xNext(u.ck.pCur->pVtabCursor);
68609   p->inVtabMethod = 0;
68610   importVtabErrMsg(p, u.ck.pVtab);
68611   if( rc==SQLITE_OK ){
68612     u.ck.res = u.ck.pModule->xEof(u.ck.pCur->pVtabCursor);
68613   }
68614 
68615   if( !u.ck.res ){
68616     /* If there is data, jump to P2 */
68617     pc = pOp->p2 - 1;
68618   }
68619   break;
68620 }
68621 #endif /* SQLITE_OMIT_VIRTUALTABLE */
68622 
68623 #ifndef SQLITE_OMIT_VIRTUALTABLE
68624 /* Opcode: VRename P1 * * P4 *
68625 **
68626 ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
68627 ** This opcode invokes the corresponding xRename method. The value
68628 ** in register P1 is passed as the zName argument to the xRename method.
68629 */
68630 case OP_VRename: {
68631 #if 0  /* local variables moved into u.cl */
68632   sqlite3_vtab *pVtab;
68633   Mem *pName;
68634 #endif /* local variables moved into u.cl */
68635 
68636   u.cl.pVtab = pOp->p4.pVtab->pVtab;
68637   u.cl.pName = &aMem[pOp->p1];
68638   assert( u.cl.pVtab->pModule->xRename );
68639   assert( memIsValid(u.cl.pName) );
68640   REGISTER_TRACE(pOp->p1, u.cl.pName);
68641   assert( u.cl.pName->flags & MEM_Str );
68642   rc = u.cl.pVtab->pModule->xRename(u.cl.pVtab, u.cl.pName->z);
68643   importVtabErrMsg(p, u.cl.pVtab);
68644   p->expired = 0;
68645 
68646   break;
68647 }
68648 #endif
68649 
68650 #ifndef SQLITE_OMIT_VIRTUALTABLE
68651 /* Opcode: VUpdate P1 P2 P3 P4 *
68652 **
68653 ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
68654 ** This opcode invokes the corresponding xUpdate method. P2 values
68655 ** are contiguous memory cells starting at P3 to pass to the xUpdate
68656 ** invocation. The value in register (P3+P2-1) corresponds to the
68657 ** p2th element of the argv array passed to xUpdate.
68658 **
68659 ** The xUpdate method will do a DELETE or an INSERT or both.
68660 ** The argv[0] element (which corresponds to memory cell P3)
68661 ** is the rowid of a row to delete.  If argv[0] is NULL then no
68662 ** deletion occurs.  The argv[1] element is the rowid of the new
68663 ** row.  This can be NULL to have the virtual table select the new
68664 ** rowid for itself.  The subsequent elements in the array are
68665 ** the values of columns in the new row.
68666 **
68667 ** If P2==1 then no insert is performed.  argv[0] is the rowid of
68668 ** a row to delete.
68669 **
68670 ** P1 is a boolean flag. If it is set to true and the xUpdate call
68671 ** is successful, then the value returned by sqlite3_last_insert_rowid()
68672 ** is set to the value of the rowid for the row just inserted.
68673 */
68674 case OP_VUpdate: {
68675 #if 0  /* local variables moved into u.cm */
68676   sqlite3_vtab *pVtab;
68677   sqlite3_module *pModule;
68678   int nArg;
68679   int i;
68680   sqlite_int64 rowid;
68681   Mem **apArg;
68682   Mem *pX;
68683 #endif /* local variables moved into u.cm */
68684 
68685   assert( pOp->p2==1        || pOp->p5==OE_Fail   || pOp->p5==OE_Rollback
68686        || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace
68687   );
68688   u.cm.pVtab = pOp->p4.pVtab->pVtab;
68689   u.cm.pModule = (sqlite3_module *)u.cm.pVtab->pModule;
68690   u.cm.nArg = pOp->p2;
68691   assert( pOp->p4type==P4_VTAB );
68692   if( ALWAYS(u.cm.pModule->xUpdate) ){
68693     u8 vtabOnConflict = db->vtabOnConflict;
68694     u.cm.apArg = p->apArg;
68695     u.cm.pX = &aMem[pOp->p3];
68696     for(u.cm.i=0; u.cm.i<u.cm.nArg; u.cm.i++){
68697       assert( memIsValid(u.cm.pX) );
68698       memAboutToChange(p, u.cm.pX);
68699       sqlite3VdbeMemStoreType(u.cm.pX);
68700       u.cm.apArg[u.cm.i] = u.cm.pX;
68701       u.cm.pX++;
68702     }
68703     db->vtabOnConflict = pOp->p5;
68704     rc = u.cm.pModule->xUpdate(u.cm.pVtab, u.cm.nArg, u.cm.apArg, &u.cm.rowid);
68705     db->vtabOnConflict = vtabOnConflict;
68706     importVtabErrMsg(p, u.cm.pVtab);
68707     if( rc==SQLITE_OK && pOp->p1 ){
68708       assert( u.cm.nArg>1 && u.cm.apArg[0] && (u.cm.apArg[0]->flags&MEM_Null) );
68709       db->lastRowid = lastRowid = u.cm.rowid;
68710     }
68711     if( rc==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){
68712       if( pOp->p5==OE_Ignore ){
68713         rc = SQLITE_OK;
68714       }else{
68715         p->errorAction = ((pOp->p5==OE_Replace) ? OE_Abort : pOp->p5);
68716       }
68717     }else{
68718       p->nChange++;
68719     }
68720   }
68721   break;
68722 }
68723 #endif /* SQLITE_OMIT_VIRTUALTABLE */
68724 
68725 #ifndef  SQLITE_OMIT_PAGER_PRAGMAS
68726 /* Opcode: Pagecount P1 P2 * * *
68727 **
68728 ** Write the current number of pages in database P1 to memory cell P2.
68729 */
68730 case OP_Pagecount: {            /* out2-prerelease */
68731   pOut->u.i = sqlite3BtreeLastPage(db->aDb[pOp->p1].pBt);
68732   break;
68733 }
68734 #endif
68735 
68736 
68737 #ifndef  SQLITE_OMIT_PAGER_PRAGMAS
68738 /* Opcode: MaxPgcnt P1 P2 P3 * *
68739 **
68740 ** Try to set the maximum page count for database P1 to the value in P3.
68741 ** Do not let the maximum page count fall below the current page count and
68742 ** do not change the maximum page count value if P3==0.
68743 **
68744 ** Store the maximum page count after the change in register P2.
68745 */
68746 case OP_MaxPgcnt: {            /* out2-prerelease */
68747   unsigned int newMax;
68748   Btree *pBt;
68749 
68750   pBt = db->aDb[pOp->p1].pBt;
68751   newMax = 0;
68752   if( pOp->p3 ){
68753     newMax = sqlite3BtreeLastPage(pBt);
68754     if( newMax < (unsigned)pOp->p3 ) newMax = (unsigned)pOp->p3;
68755   }
68756   pOut->u.i = sqlite3BtreeMaxPageCount(pBt, newMax);
68757   break;
68758 }
68759 #endif
68760 
68761 
68762 #ifndef SQLITE_OMIT_TRACE
68763 /* Opcode: Trace * * * P4 *
68764 **
68765 ** If tracing is enabled (by the sqlite3_trace()) interface, then
68766 ** the UTF-8 string contained in P4 is emitted on the trace callback.
68767 */
68768 case OP_Trace: {
68769 #if 0  /* local variables moved into u.cn */
68770   char *zTrace;
68771   char *z;
68772 #endif /* local variables moved into u.cn */
68773 
68774   if( db->xTrace && (u.cn.zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0 ){
68775     u.cn.z = sqlite3VdbeExpandSql(p, u.cn.zTrace);
68776     db->xTrace(db->pTraceArg, u.cn.z);
68777     sqlite3DbFree(db, u.cn.z);
68778   }
68779 #ifdef SQLITE_DEBUG
68780   if( (db->flags & SQLITE_SqlTrace)!=0
68781    && (u.cn.zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
68782   ){
68783     sqlite3DebugPrintf("SQL-trace: %s\n", u.cn.zTrace);
68784   }
68785 #endif /* SQLITE_DEBUG */
68786   break;
68787 }
68788 #endif
68789 
68790 
68791 /* Opcode: Noop * * * * *
68792 **
68793 ** Do nothing.  This instruction is often useful as a jump
68794 ** destination.
68795 */
68796 /*
68797 ** The magic Explain opcode are only inserted when explain==2 (which
68798 ** is to say when the EXPLAIN QUERY PLAN syntax is used.)
68799 ** This opcode records information from the optimizer.  It is the
68800 ** the same as a no-op.  This opcodesnever appears in a real VM program.
68801 */
68802 default: {          /* This is really OP_Noop and OP_Explain */
68803   assert( pOp->opcode==OP_Noop || pOp->opcode==OP_Explain );
68804   break;
68805 }
68806 
68807 /*****************************************************************************
68808 ** The cases of the switch statement above this line should all be indented
68809 ** by 6 spaces.  But the left-most 6 spaces have been removed to improve the
68810 ** readability.  From this point on down, the normal indentation rules are
68811 ** restored.
68812 *****************************************************************************/
68813     }
68814 
68815 #ifdef VDBE_PROFILE
68816     {
68817       u64 elapsed = sqlite3Hwtime() - start;
68818       pOp->cycles += elapsed;
68819       pOp->cnt++;
68820 #if 0
68821         fprintf(stdout, "%10llu ", elapsed);
68822         sqlite3VdbePrintOp(stdout, origPc, &aOp[origPc]);
68823 #endif
68824     }
68825 #endif
68826 
68827     /* The following code adds nothing to the actual functionality
68828     ** of the program.  It is only here for testing and debugging.
68829     ** On the other hand, it does burn CPU cycles every time through
68830     ** the evaluator loop.  So we can leave it out when NDEBUG is defined.
68831     */
68832 #ifndef NDEBUG
68833     assert( pc>=-1 && pc<p->nOp );
68834 
68835 #ifdef SQLITE_DEBUG
68836     if( p->trace ){
68837       if( rc!=0 ) fprintf(p->trace,"rc=%d\n",rc);
68838       if( pOp->opflags & (OPFLG_OUT2_PRERELEASE|OPFLG_OUT2) ){
68839         registerTrace(p->trace, pOp->p2, &aMem[pOp->p2]);
68840       }
68841       if( pOp->opflags & OPFLG_OUT3 ){
68842         registerTrace(p->trace, pOp->p3, &aMem[pOp->p3]);
68843       }
68844     }
68845 #endif  /* SQLITE_DEBUG */
68846 #endif  /* NDEBUG */
68847   }  /* The end of the for(;;) loop the loops through opcodes */
68848 
68849   /* If we reach this point, it means that execution is finished with
68850   ** an error of some kind.
68851   */
68852 vdbe_error_halt:
68853   assert( rc );
68854   p->rc = rc;
68855   testcase( sqlite3GlobalConfig.xLog!=0 );
68856   sqlite3_log(rc, "statement aborts at %d: [%s] %s",
68857                    pc, p->zSql, p->zErrMsg);
68858   sqlite3VdbeHalt(p);
68859   if( rc==SQLITE_IOERR_NOMEM ) db->mallocFailed = 1;
68860   rc = SQLITE_ERROR;
68861   if( resetSchemaOnFault>0 ){
68862     sqlite3ResetInternalSchema(db, resetSchemaOnFault-1);
68863   }
68864 
68865   /* This is the only way out of this procedure.  We have to
68866   ** release the mutexes on btrees that were acquired at the
68867   ** top. */
68868 vdbe_return:
68869   db->lastRowid = lastRowid;
68870   sqlite3VdbeLeave(p);
68871   return rc;
68872 
68873   /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH
68874   ** is encountered.
68875   */
68876 too_big:
68877   sqlite3SetString(&p->zErrMsg, db, "string or blob too big");
68878   rc = SQLITE_TOOBIG;
68879   goto vdbe_error_halt;
68880 
68881   /* Jump to here if a malloc() fails.
68882   */
68883 no_mem:
68884   db->mallocFailed = 1;
68885   sqlite3SetString(&p->zErrMsg, db, "out of memory");
68886   rc = SQLITE_NOMEM;
68887   goto vdbe_error_halt;
68888 
68889   /* Jump to here for any other kind of fatal error.  The "rc" variable
68890   ** should hold the error number.
68891   */
68892 abort_due_to_error:
68893   assert( p->zErrMsg==0 );
68894   if( db->mallocFailed ) rc = SQLITE_NOMEM;
68895   if( rc!=SQLITE_IOERR_NOMEM ){
68896     sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(rc));
68897   }
68898   goto vdbe_error_halt;
68899 
68900   /* Jump to here if the sqlite3_interrupt() API sets the interrupt
68901   ** flag.
68902   */
68903 abort_due_to_interrupt:
68904   assert( db->u1.isInterrupted );
68905   rc = SQLITE_INTERRUPT;
68906   p->rc = rc;
68907   sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(rc));
68908   goto vdbe_error_halt;
68909 }
68910 
68911 /************** End of vdbe.c ************************************************/
68912 /************** Begin file vdbeblob.c ****************************************/
68913 /*
68914 ** 2007 May 1
68915 **
68916 ** The author disclaims copyright to this source code.  In place of
68917 ** a legal notice, here is a blessing:
68918 **
68919 **    May you do good and not evil.
68920 **    May you find forgiveness for yourself and forgive others.
68921 **    May you share freely, never taking more than you give.
68922 **
68923 *************************************************************************
68924 **
68925 ** This file contains code used to implement incremental BLOB I/O.
68926 */
68927 
68928 
68929 #ifndef SQLITE_OMIT_INCRBLOB
68930 
68931 /*
68932 ** Valid sqlite3_blob* handles point to Incrblob structures.
68933 */
68934 typedef struct Incrblob Incrblob;
68935 struct Incrblob {
68936   int flags;              /* Copy of "flags" passed to sqlite3_blob_open() */
68937   int nByte;              /* Size of open blob, in bytes */
68938   int iOffset;            /* Byte offset of blob in cursor data */
68939   int iCol;               /* Table column this handle is open on */
68940   BtCursor *pCsr;         /* Cursor pointing at blob row */
68941   sqlite3_stmt *pStmt;    /* Statement holding cursor open */
68942   sqlite3 *db;            /* The associated database */
68943 };
68944 
68945 
68946 /*
68947 ** This function is used by both blob_open() and blob_reopen(). It seeks
68948 ** the b-tree cursor associated with blob handle p to point to row iRow.
68949 ** If successful, SQLITE_OK is returned and subsequent calls to
68950 ** sqlite3_blob_read() or sqlite3_blob_write() access the specified row.
68951 **
68952 ** If an error occurs, or if the specified row does not exist or does not
68953 ** contain a value of type TEXT or BLOB in the column nominated when the
68954 ** blob handle was opened, then an error code is returned and *pzErr may
68955 ** be set to point to a buffer containing an error message. It is the
68956 ** responsibility of the caller to free the error message buffer using
68957 ** sqlite3DbFree().
68958 **
68959 ** If an error does occur, then the b-tree cursor is closed. All subsequent
68960 ** calls to sqlite3_blob_read(), blob_write() or blob_reopen() will
68961 ** immediately return SQLITE_ABORT.
68962 */
68963 static int blobSeekToRow(Incrblob *p, sqlite3_int64 iRow, char **pzErr){
68964   int rc;                         /* Error code */
68965   char *zErr = 0;                 /* Error message */
68966   Vdbe *v = (Vdbe *)p->pStmt;
68967 
68968   /* Set the value of the SQL statements only variable to integer iRow.
68969   ** This is done directly instead of using sqlite3_bind_int64() to avoid
68970   ** triggering asserts related to mutexes.
68971   */
68972   assert( v->aVar[0].flags&MEM_Int );
68973   v->aVar[0].u.i = iRow;
68974 
68975   rc = sqlite3_step(p->pStmt);
68976   if( rc==SQLITE_ROW ){
68977     u32 type = v->apCsr[0]->aType[p->iCol];
68978     if( type<12 ){
68979       zErr = sqlite3MPrintf(p->db, "cannot open value of type %s",
68980           type==0?"null": type==7?"real": "integer"
68981       );
68982       rc = SQLITE_ERROR;
68983       sqlite3_finalize(p->pStmt);
68984       p->pStmt = 0;
68985     }else{
68986       p->iOffset = v->apCsr[0]->aOffset[p->iCol];
68987       p->nByte = sqlite3VdbeSerialTypeLen(type);
68988       p->pCsr =  v->apCsr[0]->pCursor;
68989       sqlite3BtreeEnterCursor(p->pCsr);
68990       sqlite3BtreeCacheOverflow(p->pCsr);
68991       sqlite3BtreeLeaveCursor(p->pCsr);
68992     }
68993   }
68994 
68995   if( rc==SQLITE_ROW ){
68996     rc = SQLITE_OK;
68997   }else if( p->pStmt ){
68998     rc = sqlite3_finalize(p->pStmt);
68999     p->pStmt = 0;
69000     if( rc==SQLITE_OK ){
69001       zErr = sqlite3MPrintf(p->db, "no such rowid: %lld", iRow);
69002       rc = SQLITE_ERROR;
69003     }else{
69004       zErr = sqlite3MPrintf(p->db, "%s", sqlite3_errmsg(p->db));
69005     }
69006   }
69007 
69008   assert( rc!=SQLITE_OK || zErr==0 );
69009   assert( rc!=SQLITE_ROW && rc!=SQLITE_DONE );
69010 
69011   *pzErr = zErr;
69012   return rc;
69013 }
69014 
69015 /*
69016 ** Open a blob handle.
69017 */
69018 SQLITE_API int sqlite3_blob_open(
69019   sqlite3* db,            /* The database connection */
69020   const char *zDb,        /* The attached database containing the blob */
69021   const char *zTable,     /* The table containing the blob */
69022   const char *zColumn,    /* The column containing the blob */
69023   sqlite_int64 iRow,      /* The row containing the glob */
69024   int flags,              /* True -> read/write access, false -> read-only */
69025   sqlite3_blob **ppBlob   /* Handle for accessing the blob returned here */
69026 ){
69027   int nAttempt = 0;
69028   int iCol;               /* Index of zColumn in row-record */
69029 
69030   /* This VDBE program seeks a btree cursor to the identified
69031   ** db/table/row entry. The reason for using a vdbe program instead
69032   ** of writing code to use the b-tree layer directly is that the
69033   ** vdbe program will take advantage of the various transaction,
69034   ** locking and error handling infrastructure built into the vdbe.
69035   **
69036   ** After seeking the cursor, the vdbe executes an OP_ResultRow.
69037   ** Code external to the Vdbe then "borrows" the b-tree cursor and
69038   ** uses it to implement the blob_read(), blob_write() and
69039   ** blob_bytes() functions.
69040   **
69041   ** The sqlite3_blob_close() function finalizes the vdbe program,
69042   ** which closes the b-tree cursor and (possibly) commits the
69043   ** transaction.
69044   */
69045   static const VdbeOpList openBlob[] = {
69046     {OP_Transaction, 0, 0, 0},     /* 0: Start a transaction */
69047     {OP_VerifyCookie, 0, 0, 0},    /* 1: Check the schema cookie */
69048     {OP_TableLock, 0, 0, 0},       /* 2: Acquire a read or write lock */
69049 
69050     /* One of the following two instructions is replaced by an OP_Noop. */
69051     {OP_OpenRead, 0, 0, 0},        /* 3: Open cursor 0 for reading */
69052     {OP_OpenWrite, 0, 0, 0},       /* 4: Open cursor 0 for read/write */
69053 
69054     {OP_Variable, 1, 1, 1},        /* 5: Push the rowid to the stack */
69055     {OP_NotExists, 0, 10, 1},      /* 6: Seek the cursor */
69056     {OP_Column, 0, 0, 1},          /* 7  */
69057     {OP_ResultRow, 1, 0, 0},       /* 8  */
69058     {OP_Goto, 0, 5, 0},            /* 9  */
69059     {OP_Close, 0, 0, 0},           /* 10 */
69060     {OP_Halt, 0, 0, 0},            /* 11 */
69061   };
69062 
69063   int rc = SQLITE_OK;
69064   char *zErr = 0;
69065   Table *pTab;
69066   Parse *pParse = 0;
69067   Incrblob *pBlob = 0;
69068 
69069   flags = !!flags;                /* flags = (flags ? 1 : 0); */
69070   *ppBlob = 0;
69071 
69072   sqlite3_mutex_enter(db->mutex);
69073 
69074   pBlob = (Incrblob *)sqlite3DbMallocZero(db, sizeof(Incrblob));
69075   if( !pBlob ) goto blob_open_out;
69076   pParse = sqlite3StackAllocRaw(db, sizeof(*pParse));
69077   if( !pParse ) goto blob_open_out;
69078 
69079   do {
69080     memset(pParse, 0, sizeof(Parse));
69081     pParse->db = db;
69082     sqlite3DbFree(db, zErr);
69083     zErr = 0;
69084 
69085     sqlite3BtreeEnterAll(db);
69086     pTab = sqlite3LocateTable(pParse, 0, zTable, zDb);
69087     if( pTab && IsVirtual(pTab) ){
69088       pTab = 0;
69089       sqlite3ErrorMsg(pParse, "cannot open virtual table: %s", zTable);
69090     }
69091 #ifndef SQLITE_OMIT_VIEW
69092     if( pTab && pTab->pSelect ){
69093       pTab = 0;
69094       sqlite3ErrorMsg(pParse, "cannot open view: %s", zTable);
69095     }
69096 #endif
69097     if( !pTab ){
69098       if( pParse->zErrMsg ){
69099         sqlite3DbFree(db, zErr);
69100         zErr = pParse->zErrMsg;
69101         pParse->zErrMsg = 0;
69102       }
69103       rc = SQLITE_ERROR;
69104       sqlite3BtreeLeaveAll(db);
69105       goto blob_open_out;
69106     }
69107 
69108     /* Now search pTab for the exact column. */
69109     for(iCol=0; iCol<pTab->nCol; iCol++) {
69110       if( sqlite3StrICmp(pTab->aCol[iCol].zName, zColumn)==0 ){
69111         break;
69112       }
69113     }
69114     if( iCol==pTab->nCol ){
69115       sqlite3DbFree(db, zErr);
69116       zErr = sqlite3MPrintf(db, "no such column: \"%s\"", zColumn);
69117       rc = SQLITE_ERROR;
69118       sqlite3BtreeLeaveAll(db);
69119       goto blob_open_out;
69120     }
69121 
69122     /* If the value is being opened for writing, check that the
69123     ** column is not indexed, and that it is not part of a foreign key.
69124     ** It is against the rules to open a column to which either of these
69125     ** descriptions applies for writing.  */
69126     if( flags ){
69127       const char *zFault = 0;
69128       Index *pIdx;
69129 #ifndef SQLITE_OMIT_FOREIGN_KEY
69130       if( db->flags&SQLITE_ForeignKeys ){
69131         /* Check that the column is not part of an FK child key definition. It
69132         ** is not necessary to check if it is part of a parent key, as parent
69133         ** key columns must be indexed. The check below will pick up this
69134         ** case.  */
69135         FKey *pFKey;
69136         for(pFKey=pTab->pFKey; pFKey; pFKey=pFKey->pNextFrom){
69137           int j;
69138           for(j=0; j<pFKey->nCol; j++){
69139             if( pFKey->aCol[j].iFrom==iCol ){
69140               zFault = "foreign key";
69141             }
69142           }
69143         }
69144       }
69145 #endif
69146       for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
69147         int j;
69148         for(j=0; j<pIdx->nColumn; j++){
69149           if( pIdx->aiColumn[j]==iCol ){
69150             zFault = "indexed";
69151           }
69152         }
69153       }
69154       if( zFault ){
69155         sqlite3DbFree(db, zErr);
69156         zErr = sqlite3MPrintf(db, "cannot open %s column for writing", zFault);
69157         rc = SQLITE_ERROR;
69158         sqlite3BtreeLeaveAll(db);
69159         goto blob_open_out;
69160       }
69161     }
69162 
69163     pBlob->pStmt = (sqlite3_stmt *)sqlite3VdbeCreate(db);
69164     assert( pBlob->pStmt || db->mallocFailed );
69165     if( pBlob->pStmt ){
69166       Vdbe *v = (Vdbe *)pBlob->pStmt;
69167       int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
69168 
69169       sqlite3VdbeAddOpList(v, sizeof(openBlob)/sizeof(VdbeOpList), openBlob);
69170 
69171 
69172       /* Configure the OP_Transaction */
69173       sqlite3VdbeChangeP1(v, 0, iDb);
69174       sqlite3VdbeChangeP2(v, 0, flags);
69175 
69176       /* Configure the OP_VerifyCookie */
69177       sqlite3VdbeChangeP1(v, 1, iDb);
69178       sqlite3VdbeChangeP2(v, 1, pTab->pSchema->schema_cookie);
69179       sqlite3VdbeChangeP3(v, 1, pTab->pSchema->iGeneration);
69180 
69181       /* Make sure a mutex is held on the table to be accessed */
69182       sqlite3VdbeUsesBtree(v, iDb);
69183 
69184       /* Configure the OP_TableLock instruction */
69185 #ifdef SQLITE_OMIT_SHARED_CACHE
69186       sqlite3VdbeChangeToNoop(v, 2, 1);
69187 #else
69188       sqlite3VdbeChangeP1(v, 2, iDb);
69189       sqlite3VdbeChangeP2(v, 2, pTab->tnum);
69190       sqlite3VdbeChangeP3(v, 2, flags);
69191       sqlite3VdbeChangeP4(v, 2, pTab->zName, P4_TRANSIENT);
69192 #endif
69193 
69194       /* Remove either the OP_OpenWrite or OpenRead. Set the P2
69195       ** parameter of the other to pTab->tnum.  */
69196       sqlite3VdbeChangeToNoop(v, 4 - flags, 1);
69197       sqlite3VdbeChangeP2(v, 3 + flags, pTab->tnum);
69198       sqlite3VdbeChangeP3(v, 3 + flags, iDb);
69199 
69200       /* Configure the number of columns. Configure the cursor to
69201       ** think that the table has one more column than it really
69202       ** does. An OP_Column to retrieve this imaginary column will
69203       ** always return an SQL NULL. This is useful because it means
69204       ** we can invoke OP_Column to fill in the vdbe cursors type
69205       ** and offset cache without causing any IO.
69206       */
69207       sqlite3VdbeChangeP4(v, 3+flags, SQLITE_INT_TO_PTR(pTab->nCol+1),P4_INT32);
69208       sqlite3VdbeChangeP2(v, 7, pTab->nCol);
69209       if( !db->mallocFailed ){
69210         pParse->nVar = 1;
69211         pParse->nMem = 1;
69212         pParse->nTab = 1;
69213         sqlite3VdbeMakeReady(v, pParse);
69214       }
69215     }
69216 
69217     pBlob->flags = flags;
69218     pBlob->iCol = iCol;
69219     pBlob->db = db;
69220     sqlite3BtreeLeaveAll(db);
69221     if( db->mallocFailed ){
69222       goto blob_open_out;
69223     }
69224     sqlite3_bind_int64(pBlob->pStmt, 1, iRow);
69225     rc = blobSeekToRow(pBlob, iRow, &zErr);
69226   } while( (++nAttempt)<5 && rc==SQLITE_SCHEMA );
69227 
69228 blob_open_out:
69229   if( rc==SQLITE_OK && db->mallocFailed==0 ){
69230     *ppBlob = (sqlite3_blob *)pBlob;
69231   }else{
69232     if( pBlob && pBlob->pStmt ) sqlite3VdbeFinalize((Vdbe *)pBlob->pStmt);
69233     sqlite3DbFree(db, pBlob);
69234   }
69235   sqlite3Error(db, rc, (zErr ? "%s" : 0), zErr);
69236   sqlite3DbFree(db, zErr);
69237   sqlite3StackFree(db, pParse);
69238   rc = sqlite3ApiExit(db, rc);
69239   sqlite3_mutex_leave(db->mutex);
69240   return rc;
69241 }
69242 
69243 /*
69244 ** Close a blob handle that was previously created using
69245 ** sqlite3_blob_open().
69246 */
69247 SQLITE_API int sqlite3_blob_close(sqlite3_blob *pBlob){
69248   Incrblob *p = (Incrblob *)pBlob;
69249   int rc;
69250   sqlite3 *db;
69251 
69252   if( p ){
69253     db = p->db;
69254     sqlite3_mutex_enter(db->mutex);
69255     rc = sqlite3_finalize(p->pStmt);
69256     sqlite3DbFree(db, p);
69257     sqlite3_mutex_leave(db->mutex);
69258   }else{
69259     rc = SQLITE_OK;
69260   }
69261   return rc;
69262 }
69263 
69264 /*
69265 ** Perform a read or write operation on a blob
69266 */
69267 static int blobReadWrite(
69268   sqlite3_blob *pBlob,
69269   void *z,
69270   int n,
69271   int iOffset,
69272   int (*xCall)(BtCursor*, u32, u32, void*)
69273 ){
69274   int rc;
69275   Incrblob *p = (Incrblob *)pBlob;
69276   Vdbe *v;
69277   sqlite3 *db;
69278 
69279   if( p==0 ) return SQLITE_MISUSE_BKPT;
69280   db = p->db;
69281   sqlite3_mutex_enter(db->mutex);
69282   v = (Vdbe*)p->pStmt;
69283 
69284   if( n<0 || iOffset<0 || (iOffset+n)>p->nByte ){
69285     /* Request is out of range. Return a transient error. */
69286     rc = SQLITE_ERROR;
69287     sqlite3Error(db, SQLITE_ERROR, 0);
69288   }else if( v==0 ){
69289     /* If there is no statement handle, then the blob-handle has
69290     ** already been invalidated. Return SQLITE_ABORT in this case.
69291     */
69292     rc = SQLITE_ABORT;
69293   }else{
69294     /* Call either BtreeData() or BtreePutData(). If SQLITE_ABORT is
69295     ** returned, clean-up the statement handle.
69296     */
69297     assert( db == v->db );
69298     sqlite3BtreeEnterCursor(p->pCsr);
69299     rc = xCall(p->pCsr, iOffset+p->iOffset, n, z);
69300     sqlite3BtreeLeaveCursor(p->pCsr);
69301     if( rc==SQLITE_ABORT ){
69302       sqlite3VdbeFinalize(v);
69303       p->pStmt = 0;
69304     }else{
69305       db->errCode = rc;
69306       v->rc = rc;
69307     }
69308   }
69309   rc = sqlite3ApiExit(db, rc);
69310   sqlite3_mutex_leave(db->mutex);
69311   return rc;
69312 }
69313 
69314 /*
69315 ** Read data from a blob handle.
69316 */
69317 SQLITE_API int sqlite3_blob_read(sqlite3_blob *pBlob, void *z, int n, int iOffset){
69318   return blobReadWrite(pBlob, z, n, iOffset, sqlite3BtreeData);
69319 }
69320 
69321 /*
69322 ** Write data to a blob handle.
69323 */
69324 SQLITE_API int sqlite3_blob_write(sqlite3_blob *pBlob, const void *z, int n, int iOffset){
69325   return blobReadWrite(pBlob, (void *)z, n, iOffset, sqlite3BtreePutData);
69326 }
69327 
69328 /*
69329 ** Query a blob handle for the size of the data.
69330 **
69331 ** The Incrblob.nByte field is fixed for the lifetime of the Incrblob
69332 ** so no mutex is required for access.
69333 */
69334 SQLITE_API int sqlite3_blob_bytes(sqlite3_blob *pBlob){
69335   Incrblob *p = (Incrblob *)pBlob;
69336   return (p && p->pStmt) ? p->nByte : 0;
69337 }
69338 
69339 /*
69340 ** Move an existing blob handle to point to a different row of the same
69341 ** database table.
69342 **
69343 ** If an error occurs, or if the specified row does not exist or does not
69344 ** contain a blob or text value, then an error code is returned and the
69345 ** database handle error code and message set. If this happens, then all
69346 ** subsequent calls to sqlite3_blob_xxx() functions (except blob_close())
69347 ** immediately return SQLITE_ABORT.
69348 */
69349 SQLITE_API int sqlite3_blob_reopen(sqlite3_blob *pBlob, sqlite3_int64 iRow){
69350   int rc;
69351   Incrblob *p = (Incrblob *)pBlob;
69352   sqlite3 *db;
69353 
69354   if( p==0 ) return SQLITE_MISUSE_BKPT;
69355   db = p->db;
69356   sqlite3_mutex_enter(db->mutex);
69357 
69358   if( p->pStmt==0 ){
69359     /* If there is no statement handle, then the blob-handle has
69360     ** already been invalidated. Return SQLITE_ABORT in this case.
69361     */
69362     rc = SQLITE_ABORT;
69363   }else{
69364     char *zErr;
69365     rc = blobSeekToRow(p, iRow, &zErr);
69366     if( rc!=SQLITE_OK ){
69367       sqlite3Error(db, rc, (zErr ? "%s" : 0), zErr);
69368       sqlite3DbFree(db, zErr);
69369     }
69370     assert( rc!=SQLITE_SCHEMA );
69371   }
69372 
69373   rc = sqlite3ApiExit(db, rc);
69374   assert( rc==SQLITE_OK || p->pStmt==0 );
69375   sqlite3_mutex_leave(db->mutex);
69376   return rc;
69377 }
69378 
69379 #endif /* #ifndef SQLITE_OMIT_INCRBLOB */
69380 
69381 /************** End of vdbeblob.c ********************************************/
69382 /************** Begin file journal.c *****************************************/
69383 /*
69384 ** 2007 August 22
69385 **
69386 ** The author disclaims copyright to this source code.  In place of
69387 ** a legal notice, here is a blessing:
69388 **
69389 **    May you do good and not evil.
69390 **    May you find forgiveness for yourself and forgive others.
69391 **    May you share freely, never taking more than you give.
69392 **
69393 *************************************************************************
69394 **
69395 ** This file implements a special kind of sqlite3_file object used
69396 ** by SQLite to create journal files if the atomic-write optimization
69397 ** is enabled.
69398 **
69399 ** The distinctive characteristic of this sqlite3_file is that the
69400 ** actual on disk file is created lazily. When the file is created,
69401 ** the caller specifies a buffer size for an in-memory buffer to
69402 ** be used to service read() and write() requests. The actual file
69403 ** on disk is not created or populated until either:
69404 **
69405 **   1) The in-memory representation grows too large for the allocated
69406 **      buffer, or
69407 **   2) The sqlite3JournalCreate() function is called.
69408 */
69409 #ifdef SQLITE_ENABLE_ATOMIC_WRITE
69410 
69411 
69412 /*
69413 ** A JournalFile object is a subclass of sqlite3_file used by
69414 ** as an open file handle for journal files.
69415 */
69416 struct JournalFile {
69417   sqlite3_io_methods *pMethod;    /* I/O methods on journal files */
69418   int nBuf;                       /* Size of zBuf[] in bytes */
69419   char *zBuf;                     /* Space to buffer journal writes */
69420   int iSize;                      /* Amount of zBuf[] currently used */
69421   int flags;                      /* xOpen flags */
69422   sqlite3_vfs *pVfs;              /* The "real" underlying VFS */
69423   sqlite3_file *pReal;            /* The "real" underlying file descriptor */
69424   const char *zJournal;           /* Name of the journal file */
69425 };
69426 typedef struct JournalFile JournalFile;
69427 
69428 /*
69429 ** If it does not already exists, create and populate the on-disk file
69430 ** for JournalFile p.
69431 */
69432 static int createFile(JournalFile *p){
69433   int rc = SQLITE_OK;
69434   if( !p->pReal ){
69435     sqlite3_file *pReal = (sqlite3_file *)&p[1];
69436     rc = sqlite3OsOpen(p->pVfs, p->zJournal, pReal, p->flags, 0);
69437     if( rc==SQLITE_OK ){
69438       p->pReal = pReal;
69439       if( p->iSize>0 ){
69440         assert(p->iSize<=p->nBuf);
69441         rc = sqlite3OsWrite(p->pReal, p->zBuf, p->iSize, 0);
69442       }
69443     }
69444   }
69445   return rc;
69446 }
69447 
69448 /*
69449 ** Close the file.
69450 */
69451 static int jrnlClose(sqlite3_file *pJfd){
69452   JournalFile *p = (JournalFile *)pJfd;
69453   if( p->pReal ){
69454     sqlite3OsClose(p->pReal);
69455   }
69456   sqlite3_free(p->zBuf);
69457   return SQLITE_OK;
69458 }
69459 
69460 /*
69461 ** Read data from the file.
69462 */
69463 static int jrnlRead(
69464   sqlite3_file *pJfd,    /* The journal file from which to read */
69465   void *zBuf,            /* Put the results here */
69466   int iAmt,              /* Number of bytes to read */
69467   sqlite_int64 iOfst     /* Begin reading at this offset */
69468 ){
69469   int rc = SQLITE_OK;
69470   JournalFile *p = (JournalFile *)pJfd;
69471   if( p->pReal ){
69472     rc = sqlite3OsRead(p->pReal, zBuf, iAmt, iOfst);
69473   }else if( (iAmt+iOfst)>p->iSize ){
69474     rc = SQLITE_IOERR_SHORT_READ;
69475   }else{
69476     memcpy(zBuf, &p->zBuf[iOfst], iAmt);
69477   }
69478   return rc;
69479 }
69480 
69481 /*
69482 ** Write data to the file.
69483 */
69484 static int jrnlWrite(
69485   sqlite3_file *pJfd,    /* The journal file into which to write */
69486   const void *zBuf,      /* Take data to be written from here */
69487   int iAmt,              /* Number of bytes to write */
69488   sqlite_int64 iOfst     /* Begin writing at this offset into the file */
69489 ){
69490   int rc = SQLITE_OK;
69491   JournalFile *p = (JournalFile *)pJfd;
69492   if( !p->pReal && (iOfst+iAmt)>p->nBuf ){
69493     rc = createFile(p);
69494   }
69495   if( rc==SQLITE_OK ){
69496     if( p->pReal ){
69497       rc = sqlite3OsWrite(p->pReal, zBuf, iAmt, iOfst);
69498     }else{
69499       memcpy(&p->zBuf[iOfst], zBuf, iAmt);
69500       if( p->iSize<(iOfst+iAmt) ){
69501         p->iSize = (iOfst+iAmt);
69502       }
69503     }
69504   }
69505   return rc;
69506 }
69507 
69508 /*
69509 ** Truncate the file.
69510 */
69511 static int jrnlTruncate(sqlite3_file *pJfd, sqlite_int64 size){
69512   int rc = SQLITE_OK;
69513   JournalFile *p = (JournalFile *)pJfd;
69514   if( p->pReal ){
69515     rc = sqlite3OsTruncate(p->pReal, size);
69516   }else if( size<p->iSize ){
69517     p->iSize = size;
69518   }
69519   return rc;
69520 }
69521 
69522 /*
69523 ** Sync the file.
69524 */
69525 static int jrnlSync(sqlite3_file *pJfd, int flags){
69526   int rc;
69527   JournalFile *p = (JournalFile *)pJfd;
69528   if( p->pReal ){
69529     rc = sqlite3OsSync(p->pReal, flags);
69530   }else{
69531     rc = SQLITE_OK;
69532   }
69533   return rc;
69534 }
69535 
69536 /*
69537 ** Query the size of the file in bytes.
69538 */
69539 static int jrnlFileSize(sqlite3_file *pJfd, sqlite_int64 *pSize){
69540   int rc = SQLITE_OK;
69541   JournalFile *p = (JournalFile *)pJfd;
69542   if( p->pReal ){
69543     rc = sqlite3OsFileSize(p->pReal, pSize);
69544   }else{
69545     *pSize = (sqlite_int64) p->iSize;
69546   }
69547   return rc;
69548 }
69549 
69550 /*
69551 ** Table of methods for JournalFile sqlite3_file object.
69552 */
69553 static struct sqlite3_io_methods JournalFileMethods = {
69554   1,             /* iVersion */
69555   jrnlClose,     /* xClose */
69556   jrnlRead,      /* xRead */
69557   jrnlWrite,     /* xWrite */
69558   jrnlTruncate,  /* xTruncate */
69559   jrnlSync,      /* xSync */
69560   jrnlFileSize,  /* xFileSize */
69561   0,             /* xLock */
69562   0,             /* xUnlock */
69563   0,             /* xCheckReservedLock */
69564   0,             /* xFileControl */
69565   0,             /* xSectorSize */
69566   0,             /* xDeviceCharacteristics */
69567   0,             /* xShmMap */
69568   0,             /* xShmLock */
69569   0,             /* xShmBarrier */
69570   0              /* xShmUnmap */
69571 };
69572 
69573 /*
69574 ** Open a journal file.
69575 */
69576 SQLITE_PRIVATE int sqlite3JournalOpen(
69577   sqlite3_vfs *pVfs,         /* The VFS to use for actual file I/O */
69578   const char *zName,         /* Name of the journal file */
69579   sqlite3_file *pJfd,        /* Preallocated, blank file handle */
69580   int flags,                 /* Opening flags */
69581   int nBuf                   /* Bytes buffered before opening the file */
69582 ){
69583   JournalFile *p = (JournalFile *)pJfd;
69584   memset(p, 0, sqlite3JournalSize(pVfs));
69585   if( nBuf>0 ){
69586     p->zBuf = sqlite3MallocZero(nBuf);
69587     if( !p->zBuf ){
69588       return SQLITE_NOMEM;
69589     }
69590   }else{
69591     return sqlite3OsOpen(pVfs, zName, pJfd, flags, 0);
69592   }
69593   p->pMethod = &JournalFileMethods;
69594   p->nBuf = nBuf;
69595   p->flags = flags;
69596   p->zJournal = zName;
69597   p->pVfs = pVfs;
69598   return SQLITE_OK;
69599 }
69600 
69601 /*
69602 ** If the argument p points to a JournalFile structure, and the underlying
69603 ** file has not yet been created, create it now.
69604 */
69605 SQLITE_PRIVATE int sqlite3JournalCreate(sqlite3_file *p){
69606   if( p->pMethods!=&JournalFileMethods ){
69607     return SQLITE_OK;
69608   }
69609   return createFile((JournalFile *)p);
69610 }
69611 
69612 /*
69613 ** Return the number of bytes required to store a JournalFile that uses vfs
69614 ** pVfs to create the underlying on-disk files.
69615 */
69616 SQLITE_PRIVATE int sqlite3JournalSize(sqlite3_vfs *pVfs){
69617   return (pVfs->szOsFile+sizeof(JournalFile));
69618 }
69619 #endif
69620 
69621 /************** End of journal.c *********************************************/
69622 /************** Begin file memjournal.c **************************************/
69623 /*
69624 ** 2008 October 7
69625 **
69626 ** The author disclaims copyright to this source code.  In place of
69627 ** a legal notice, here is a blessing:
69628 **
69629 **    May you do good and not evil.
69630 **    May you find forgiveness for yourself and forgive others.
69631 **    May you share freely, never taking more than you give.
69632 **
69633 *************************************************************************
69634 **
69635 ** This file contains code use to implement an in-memory rollback journal.
69636 ** The in-memory rollback journal is used to journal transactions for
69637 ** ":memory:" databases and when the journal_mode=MEMORY pragma is used.
69638 */
69639 
69640 /* Forward references to internal structures */
69641 typedef struct MemJournal MemJournal;
69642 typedef struct FilePoint FilePoint;
69643 typedef struct FileChunk FileChunk;
69644 
69645 /* Space to hold the rollback journal is allocated in increments of
69646 ** this many bytes.
69647 **
69648 ** The size chosen is a little less than a power of two.  That way,
69649 ** the FileChunk object will have a size that almost exactly fills
69650 ** a power-of-two allocation.  This mimimizes wasted space in power-of-two
69651 ** memory allocators.
69652 */
69653 #define JOURNAL_CHUNKSIZE ((int)(1024-sizeof(FileChunk*)))
69654 
69655 /* Macro to find the minimum of two numeric values.
69656 */
69657 #ifndef MIN
69658 # define MIN(x,y) ((x)<(y)?(x):(y))
69659 #endif
69660 
69661 /*
69662 ** The rollback journal is composed of a linked list of these structures.
69663 */
69664 struct FileChunk {
69665   FileChunk *pNext;               /* Next chunk in the journal */
69666   u8 zChunk[JOURNAL_CHUNKSIZE];   /* Content of this chunk */
69667 };
69668 
69669 /*
69670 ** An instance of this object serves as a cursor into the rollback journal.
69671 ** The cursor can be either for reading or writing.
69672 */
69673 struct FilePoint {
69674   sqlite3_int64 iOffset;          /* Offset from the beginning of the file */
69675   FileChunk *pChunk;              /* Specific chunk into which cursor points */
69676 };
69677 
69678 /*
69679 ** This subclass is a subclass of sqlite3_file.  Each open memory-journal
69680 ** is an instance of this class.
69681 */
69682 struct MemJournal {
69683   sqlite3_io_methods *pMethod;    /* Parent class. MUST BE FIRST */
69684   FileChunk *pFirst;              /* Head of in-memory chunk-list */
69685   FilePoint endpoint;             /* Pointer to the end of the file */
69686   FilePoint readpoint;            /* Pointer to the end of the last xRead() */
69687 };
69688 
69689 /*
69690 ** Read data from the in-memory journal file.  This is the implementation
69691 ** of the sqlite3_vfs.xRead method.
69692 */
69693 static int memjrnlRead(
69694   sqlite3_file *pJfd,    /* The journal file from which to read */
69695   void *zBuf,            /* Put the results here */
69696   int iAmt,              /* Number of bytes to read */
69697   sqlite_int64 iOfst     /* Begin reading at this offset */
69698 ){
69699   MemJournal *p = (MemJournal *)pJfd;
69700   u8 *zOut = zBuf;
69701   int nRead = iAmt;
69702   int iChunkOffset;
69703   FileChunk *pChunk;
69704 
69705   /* SQLite never tries to read past the end of a rollback journal file */
69706   assert( iOfst+iAmt<=p->endpoint.iOffset );
69707 
69708   if( p->readpoint.iOffset!=iOfst || iOfst==0 ){
69709     sqlite3_int64 iOff = 0;
69710     for(pChunk=p->pFirst;
69711         ALWAYS(pChunk) && (iOff+JOURNAL_CHUNKSIZE)<=iOfst;
69712         pChunk=pChunk->pNext
69713     ){
69714       iOff += JOURNAL_CHUNKSIZE;
69715     }
69716   }else{
69717     pChunk = p->readpoint.pChunk;
69718   }
69719 
69720   iChunkOffset = (int)(iOfst%JOURNAL_CHUNKSIZE);
69721   do {
69722     int iSpace = JOURNAL_CHUNKSIZE - iChunkOffset;
69723     int nCopy = MIN(nRead, (JOURNAL_CHUNKSIZE - iChunkOffset));
69724     memcpy(zOut, &pChunk->zChunk[iChunkOffset], nCopy);
69725     zOut += nCopy;
69726     nRead -= iSpace;
69727     iChunkOffset = 0;
69728   } while( nRead>=0 && (pChunk=pChunk->pNext)!=0 && nRead>0 );
69729   p->readpoint.iOffset = iOfst+iAmt;
69730   p->readpoint.pChunk = pChunk;
69731 
69732   return SQLITE_OK;
69733 }
69734 
69735 /*
69736 ** Write data to the file.
69737 */
69738 static int memjrnlWrite(
69739   sqlite3_file *pJfd,    /* The journal file into which to write */
69740   const void *zBuf,      /* Take data to be written from here */
69741   int iAmt,              /* Number of bytes to write */
69742   sqlite_int64 iOfst     /* Begin writing at this offset into the file */
69743 ){
69744   MemJournal *p = (MemJournal *)pJfd;
69745   int nWrite = iAmt;
69746   u8 *zWrite = (u8 *)zBuf;
69747 
69748   /* An in-memory journal file should only ever be appended to. Random
69749   ** access writes are not required by sqlite.
69750   */
69751   assert( iOfst==p->endpoint.iOffset );
69752   UNUSED_PARAMETER(iOfst);
69753 
69754   while( nWrite>0 ){
69755     FileChunk *pChunk = p->endpoint.pChunk;
69756     int iChunkOffset = (int)(p->endpoint.iOffset%JOURNAL_CHUNKSIZE);
69757     int iSpace = MIN(nWrite, JOURNAL_CHUNKSIZE - iChunkOffset);
69758 
69759     if( iChunkOffset==0 ){
69760       /* New chunk is required to extend the file. */
69761       FileChunk *pNew = sqlite3_malloc(sizeof(FileChunk));
69762       if( !pNew ){
69763         return SQLITE_IOERR_NOMEM;
69764       }
69765       pNew->pNext = 0;
69766       if( pChunk ){
69767         assert( p->pFirst );
69768         pChunk->pNext = pNew;
69769       }else{
69770         assert( !p->pFirst );
69771         p->pFirst = pNew;
69772       }
69773       p->endpoint.pChunk = pNew;
69774     }
69775 
69776     memcpy(&p->endpoint.pChunk->zChunk[iChunkOffset], zWrite, iSpace);
69777     zWrite += iSpace;
69778     nWrite -= iSpace;
69779     p->endpoint.iOffset += iSpace;
69780   }
69781 
69782   return SQLITE_OK;
69783 }
69784 
69785 /*
69786 ** Truncate the file.
69787 */
69788 static int memjrnlTruncate(sqlite3_file *pJfd, sqlite_int64 size){
69789   MemJournal *p = (MemJournal *)pJfd;
69790   FileChunk *pChunk;
69791   assert(size==0);
69792   UNUSED_PARAMETER(size);
69793   pChunk = p->pFirst;
69794   while( pChunk ){
69795     FileChunk *pTmp = pChunk;
69796     pChunk = pChunk->pNext;
69797     sqlite3_free(pTmp);
69798   }
69799   sqlite3MemJournalOpen(pJfd);
69800   return SQLITE_OK;
69801 }
69802 
69803 /*
69804 ** Close the file.
69805 */
69806 static int memjrnlClose(sqlite3_file *pJfd){
69807   memjrnlTruncate(pJfd, 0);
69808   return SQLITE_OK;
69809 }
69810 
69811 
69812 /*
69813 ** Sync the file.
69814 **
69815 ** Syncing an in-memory journal is a no-op.  And, in fact, this routine
69816 ** is never called in a working implementation.  This implementation
69817 ** exists purely as a contingency, in case some malfunction in some other
69818 ** part of SQLite causes Sync to be called by mistake.
69819 */
69820 static int memjrnlSync(sqlite3_file *NotUsed, int NotUsed2){
69821   UNUSED_PARAMETER2(NotUsed, NotUsed2);
69822   return SQLITE_OK;
69823 }
69824 
69825 /*
69826 ** Query the size of the file in bytes.
69827 */
69828 static int memjrnlFileSize(sqlite3_file *pJfd, sqlite_int64 *pSize){
69829   MemJournal *p = (MemJournal *)pJfd;
69830   *pSize = (sqlite_int64) p->endpoint.iOffset;
69831   return SQLITE_OK;
69832 }
69833 
69834 /*
69835 ** Table of methods for MemJournal sqlite3_file object.
69836 */
69837 static const struct sqlite3_io_methods MemJournalMethods = {
69838   1,                /* iVersion */
69839   memjrnlClose,     /* xClose */
69840   memjrnlRead,      /* xRead */
69841   memjrnlWrite,     /* xWrite */
69842   memjrnlTruncate,  /* xTruncate */
69843   memjrnlSync,      /* xSync */
69844   memjrnlFileSize,  /* xFileSize */
69845   0,                /* xLock */
69846   0,                /* xUnlock */
69847   0,                /* xCheckReservedLock */
69848   0,                /* xFileControl */
69849   0,                /* xSectorSize */
69850   0,                /* xDeviceCharacteristics */
69851   0,                /* xShmMap */
69852   0,                /* xShmLock */
69853   0,                /* xShmBarrier */
69854   0                 /* xShmUnlock */
69855 };
69856 
69857 /*
69858 ** Open a journal file.
69859 */
69860 SQLITE_PRIVATE void sqlite3MemJournalOpen(sqlite3_file *pJfd){
69861   MemJournal *p = (MemJournal *)pJfd;
69862   assert( EIGHT_BYTE_ALIGNMENT(p) );
69863   memset(p, 0, sqlite3MemJournalSize());
69864   p->pMethod = (sqlite3_io_methods*)&MemJournalMethods;
69865 }
69866 
69867 /*
69868 ** Return true if the file-handle passed as an argument is
69869 ** an in-memory journal
69870 */
69871 SQLITE_PRIVATE int sqlite3IsMemJournal(sqlite3_file *pJfd){
69872   return pJfd->pMethods==&MemJournalMethods;
69873 }
69874 
69875 /*
69876 ** Return the number of bytes required to store a MemJournal file descriptor.
69877 */
69878 SQLITE_PRIVATE int sqlite3MemJournalSize(void){
69879   return sizeof(MemJournal);
69880 }
69881 
69882 /************** End of memjournal.c ******************************************/
69883 /************** Begin file walker.c ******************************************/
69884 /*
69885 ** 2008 August 16
69886 **
69887 ** The author disclaims copyright to this source code.  In place of
69888 ** a legal notice, here is a blessing:
69889 **
69890 **    May you do good and not evil.
69891 **    May you find forgiveness for yourself and forgive others.
69892 **    May you share freely, never taking more than you give.
69893 **
69894 *************************************************************************
69895 ** This file contains routines used for walking the parser tree for
69896 ** an SQL statement.
69897 */
69898 
69899 
69900 /*
69901 ** Walk an expression tree.  Invoke the callback once for each node
69902 ** of the expression, while decending.  (In other words, the callback
69903 ** is invoked before visiting children.)
69904 **
69905 ** The return value from the callback should be one of the WRC_*
69906 ** constants to specify how to proceed with the walk.
69907 **
69908 **    WRC_Continue      Continue descending down the tree.
69909 **
69910 **    WRC_Prune         Do not descend into child nodes.  But allow
69911 **                      the walk to continue with sibling nodes.
69912 **
69913 **    WRC_Abort         Do no more callbacks.  Unwind the stack and
69914 **                      return the top-level walk call.
69915 **
69916 ** The return value from this routine is WRC_Abort to abandon the tree walk
69917 ** and WRC_Continue to continue.
69918 */
69919 SQLITE_PRIVATE int sqlite3WalkExpr(Walker *pWalker, Expr *pExpr){
69920   int rc;
69921   if( pExpr==0 ) return WRC_Continue;
69922   testcase( ExprHasProperty(pExpr, EP_TokenOnly) );
69923   testcase( ExprHasProperty(pExpr, EP_Reduced) );
69924   rc = pWalker->xExprCallback(pWalker, pExpr);
69925   if( rc==WRC_Continue
69926               && !ExprHasAnyProperty(pExpr,EP_TokenOnly) ){
69927     if( sqlite3WalkExpr(pWalker, pExpr->pLeft) ) return WRC_Abort;
69928     if( sqlite3WalkExpr(pWalker, pExpr->pRight) ) return WRC_Abort;
69929     if( ExprHasProperty(pExpr, EP_xIsSelect) ){
69930       if( sqlite3WalkSelect(pWalker, pExpr->x.pSelect) ) return WRC_Abort;
69931     }else{
69932       if( sqlite3WalkExprList(pWalker, pExpr->x.pList) ) return WRC_Abort;
69933     }
69934   }
69935   return rc & WRC_Abort;
69936 }
69937 
69938 /*
69939 ** Call sqlite3WalkExpr() for every expression in list p or until
69940 ** an abort request is seen.
69941 */
69942 SQLITE_PRIVATE int sqlite3WalkExprList(Walker *pWalker, ExprList *p){
69943   int i;
69944   struct ExprList_item *pItem;
69945   if( p ){
69946     for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
69947       if( sqlite3WalkExpr(pWalker, pItem->pExpr) ) return WRC_Abort;
69948     }
69949   }
69950   return WRC_Continue;
69951 }
69952 
69953 /*
69954 ** Walk all expressions associated with SELECT statement p.  Do
69955 ** not invoke the SELECT callback on p, but do (of course) invoke
69956 ** any expr callbacks and SELECT callbacks that come from subqueries.
69957 ** Return WRC_Abort or WRC_Continue.
69958 */
69959 SQLITE_PRIVATE int sqlite3WalkSelectExpr(Walker *pWalker, Select *p){
69960   if( sqlite3WalkExprList(pWalker, p->pEList) ) return WRC_Abort;
69961   if( sqlite3WalkExpr(pWalker, p->pWhere) ) return WRC_Abort;
69962   if( sqlite3WalkExprList(pWalker, p->pGroupBy) ) return WRC_Abort;
69963   if( sqlite3WalkExpr(pWalker, p->pHaving) ) return WRC_Abort;
69964   if( sqlite3WalkExprList(pWalker, p->pOrderBy) ) return WRC_Abort;
69965   if( sqlite3WalkExpr(pWalker, p->pLimit) ) return WRC_Abort;
69966   if( sqlite3WalkExpr(pWalker, p->pOffset) ) return WRC_Abort;
69967   return WRC_Continue;
69968 }
69969 
69970 /*
69971 ** Walk the parse trees associated with all subqueries in the
69972 ** FROM clause of SELECT statement p.  Do not invoke the select
69973 ** callback on p, but do invoke it on each FROM clause subquery
69974 ** and on any subqueries further down in the tree.  Return
69975 ** WRC_Abort or WRC_Continue;
69976 */
69977 SQLITE_PRIVATE int sqlite3WalkSelectFrom(Walker *pWalker, Select *p){
69978   SrcList *pSrc;
69979   int i;
69980   struct SrcList_item *pItem;
69981 
69982   pSrc = p->pSrc;
69983   if( ALWAYS(pSrc) ){
69984     for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
69985       if( sqlite3WalkSelect(pWalker, pItem->pSelect) ){
69986         return WRC_Abort;
69987       }
69988     }
69989   }
69990   return WRC_Continue;
69991 }
69992 
69993 /*
69994 ** Call sqlite3WalkExpr() for every expression in Select statement p.
69995 ** Invoke sqlite3WalkSelect() for subqueries in the FROM clause and
69996 ** on the compound select chain, p->pPrior.
69997 **
69998 ** Return WRC_Continue under normal conditions.  Return WRC_Abort if
69999 ** there is an abort request.
70000 **
70001 ** If the Walker does not have an xSelectCallback() then this routine
70002 ** is a no-op returning WRC_Continue.
70003 */
70004 SQLITE_PRIVATE int sqlite3WalkSelect(Walker *pWalker, Select *p){
70005   int rc;
70006   if( p==0 || pWalker->xSelectCallback==0 ) return WRC_Continue;
70007   rc = WRC_Continue;
70008   while( p  ){
70009     rc = pWalker->xSelectCallback(pWalker, p);
70010     if( rc ) break;
70011     if( sqlite3WalkSelectExpr(pWalker, p) ) return WRC_Abort;
70012     if( sqlite3WalkSelectFrom(pWalker, p) ) return WRC_Abort;
70013     p = p->pPrior;
70014   }
70015   return rc & WRC_Abort;
70016 }
70017 
70018 /************** End of walker.c **********************************************/
70019 /************** Begin file resolve.c *****************************************/
70020 /*
70021 ** 2008 August 18
70022 **
70023 ** The author disclaims copyright to this source code.  In place of
70024 ** a legal notice, here is a blessing:
70025 **
70026 **    May you do good and not evil.
70027 **    May you find forgiveness for yourself and forgive others.
70028 **    May you share freely, never taking more than you give.
70029 **
70030 *************************************************************************
70031 **
70032 ** This file contains routines used for walking the parser tree and
70033 ** resolve all identifiers by associating them with a particular
70034 ** table and column.
70035 */
70036 
70037 /*
70038 ** Turn the pExpr expression into an alias for the iCol-th column of the
70039 ** result set in pEList.
70040 **
70041 ** If the result set column is a simple column reference, then this routine
70042 ** makes an exact copy.  But for any other kind of expression, this
70043 ** routine make a copy of the result set column as the argument to the
70044 ** TK_AS operator.  The TK_AS operator causes the expression to be
70045 ** evaluated just once and then reused for each alias.
70046 **
70047 ** The reason for suppressing the TK_AS term when the expression is a simple
70048 ** column reference is so that the column reference will be recognized as
70049 ** usable by indices within the WHERE clause processing logic.
70050 **
70051 ** Hack:  The TK_AS operator is inhibited if zType[0]=='G'.  This means
70052 ** that in a GROUP BY clause, the expression is evaluated twice.  Hence:
70053 **
70054 **     SELECT random()%5 AS x, count(*) FROM tab GROUP BY x
70055 **
70056 ** Is equivalent to:
70057 **
70058 **     SELECT random()%5 AS x, count(*) FROM tab GROUP BY random()%5
70059 **
70060 ** The result of random()%5 in the GROUP BY clause is probably different
70061 ** from the result in the result-set.  We might fix this someday.  Or
70062 ** then again, we might not...
70063 */
70064 static void resolveAlias(
70065   Parse *pParse,         /* Parsing context */
70066   ExprList *pEList,      /* A result set */
70067   int iCol,              /* A column in the result set.  0..pEList->nExpr-1 */
70068   Expr *pExpr,           /* Transform this into an alias to the result set */
70069   const char *zType      /* "GROUP" or "ORDER" or "" */
70070 ){
70071   Expr *pOrig;           /* The iCol-th column of the result set */
70072   Expr *pDup;            /* Copy of pOrig */
70073   sqlite3 *db;           /* The database connection */
70074 
70075   assert( iCol>=0 && iCol<pEList->nExpr );
70076   pOrig = pEList->a[iCol].pExpr;
70077   assert( pOrig!=0 );
70078   assert( pOrig->flags & EP_Resolved );
70079   db = pParse->db;
70080   if( pOrig->op!=TK_COLUMN && zType[0]!='G' ){
70081     pDup = sqlite3ExprDup(db, pOrig, 0);
70082     pDup = sqlite3PExpr(pParse, TK_AS, pDup, 0, 0);
70083     if( pDup==0 ) return;
70084     if( pEList->a[iCol].iAlias==0 ){
70085       pEList->a[iCol].iAlias = (u16)(++pParse->nAlias);
70086     }
70087     pDup->iTable = pEList->a[iCol].iAlias;
70088   }else if( ExprHasProperty(pOrig, EP_IntValue) || pOrig->u.zToken==0 ){
70089     pDup = sqlite3ExprDup(db, pOrig, 0);
70090     if( pDup==0 ) return;
70091   }else{
70092     char *zToken = pOrig->u.zToken;
70093     assert( zToken!=0 );
70094     pOrig->u.zToken = 0;
70095     pDup = sqlite3ExprDup(db, pOrig, 0);
70096     pOrig->u.zToken = zToken;
70097     if( pDup==0 ) return;
70098     assert( (pDup->flags & (EP_Reduced|EP_TokenOnly))==0 );
70099     pDup->flags2 |= EP2_MallocedToken;
70100     pDup->u.zToken = sqlite3DbStrDup(db, zToken);
70101   }
70102   if( pExpr->flags & EP_ExpCollate ){
70103     pDup->pColl = pExpr->pColl;
70104     pDup->flags |= EP_ExpCollate;
70105   }
70106 
70107   /* Before calling sqlite3ExprDelete(), set the EP_Static flag. This
70108   ** prevents ExprDelete() from deleting the Expr structure itself,
70109   ** allowing it to be repopulated by the memcpy() on the following line.
70110   */
70111   ExprSetProperty(pExpr, EP_Static);
70112   sqlite3ExprDelete(db, pExpr);
70113   memcpy(pExpr, pDup, sizeof(*pExpr));
70114   sqlite3DbFree(db, pDup);
70115 }
70116 
70117 /*
70118 ** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
70119 ** that name in the set of source tables in pSrcList and make the pExpr
70120 ** expression node refer back to that source column.  The following changes
70121 ** are made to pExpr:
70122 **
70123 **    pExpr->iDb           Set the index in db->aDb[] of the database X
70124 **                         (even if X is implied).
70125 **    pExpr->iTable        Set to the cursor number for the table obtained
70126 **                         from pSrcList.
70127 **    pExpr->pTab          Points to the Table structure of X.Y (even if
70128 **                         X and/or Y are implied.)
70129 **    pExpr->iColumn       Set to the column number within the table.
70130 **    pExpr->op            Set to TK_COLUMN.
70131 **    pExpr->pLeft         Any expression this points to is deleted
70132 **    pExpr->pRight        Any expression this points to is deleted.
70133 **
70134 ** The zDb variable is the name of the database (the "X").  This value may be
70135 ** NULL meaning that name is of the form Y.Z or Z.  Any available database
70136 ** can be used.  The zTable variable is the name of the table (the "Y").  This
70137 ** value can be NULL if zDb is also NULL.  If zTable is NULL it
70138 ** means that the form of the name is Z and that columns from any table
70139 ** can be used.
70140 **
70141 ** If the name cannot be resolved unambiguously, leave an error message
70142 ** in pParse and return WRC_Abort.  Return WRC_Prune on success.
70143 */
70144 static int lookupName(
70145   Parse *pParse,       /* The parsing context */
70146   const char *zDb,     /* Name of the database containing table, or NULL */
70147   const char *zTab,    /* Name of table containing column, or NULL */
70148   const char *zCol,    /* Name of the column. */
70149   NameContext *pNC,    /* The name context used to resolve the name */
70150   Expr *pExpr          /* Make this EXPR node point to the selected column */
70151 ){
70152   int i, j;            /* Loop counters */
70153   int cnt = 0;                      /* Number of matching column names */
70154   int cntTab = 0;                   /* Number of matching table names */
70155   sqlite3 *db = pParse->db;         /* The database connection */
70156   struct SrcList_item *pItem;       /* Use for looping over pSrcList items */
70157   struct SrcList_item *pMatch = 0;  /* The matching pSrcList item */
70158   NameContext *pTopNC = pNC;        /* First namecontext in the list */
70159   Schema *pSchema = 0;              /* Schema of the expression */
70160   int isTrigger = 0;
70161 
70162   assert( pNC );     /* the name context cannot be NULL. */
70163   assert( zCol );    /* The Z in X.Y.Z cannot be NULL */
70164   assert( ~ExprHasAnyProperty(pExpr, EP_TokenOnly|EP_Reduced) );
70165 
70166   /* Initialize the node to no-match */
70167   pExpr->iTable = -1;
70168   pExpr->pTab = 0;
70169   ExprSetIrreducible(pExpr);
70170 
70171   /* Start at the inner-most context and move outward until a match is found */
70172   while( pNC && cnt==0 ){
70173     ExprList *pEList;
70174     SrcList *pSrcList = pNC->pSrcList;
70175 
70176     if( pSrcList ){
70177       for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
70178         Table *pTab;
70179         int iDb;
70180         Column *pCol;
70181 
70182         pTab = pItem->pTab;
70183         assert( pTab!=0 && pTab->zName!=0 );
70184         iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
70185         assert( pTab->nCol>0 );
70186         if( zTab ){
70187           if( pItem->zAlias ){
70188             char *zTabName = pItem->zAlias;
70189             if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
70190           }else{
70191             char *zTabName = pTab->zName;
70192             if( NEVER(zTabName==0) || sqlite3StrICmp(zTabName, zTab)!=0 ){
70193               continue;
70194             }
70195             if( zDb!=0 && sqlite3StrICmp(db->aDb[iDb].zName, zDb)!=0 ){
70196               continue;
70197             }
70198           }
70199         }
70200         if( 0==(cntTab++) ){
70201           pExpr->iTable = pItem->iCursor;
70202           pExpr->pTab = pTab;
70203           pSchema = pTab->pSchema;
70204           pMatch = pItem;
70205         }
70206         for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
70207           if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
70208             IdList *pUsing;
70209             cnt++;
70210             pExpr->iTable = pItem->iCursor;
70211             pExpr->pTab = pTab;
70212             pMatch = pItem;
70213             pSchema = pTab->pSchema;
70214             /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
70215             pExpr->iColumn = j==pTab->iPKey ? -1 : (i16)j;
70216             if( i<pSrcList->nSrc-1 ){
70217               if( pItem[1].jointype & JT_NATURAL ){
70218                 /* If this match occurred in the left table of a natural join,
70219                 ** then skip the right table to avoid a duplicate match */
70220                 pItem++;
70221                 i++;
70222               }else if( (pUsing = pItem[1].pUsing)!=0 ){
70223                 /* If this match occurs on a column that is in the USING clause
70224                 ** of a join, skip the search of the right table of the join
70225                 ** to avoid a duplicate match there. */
70226                 int k;
70227                 for(k=0; k<pUsing->nId; k++){
70228                   if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ){
70229                     pItem++;
70230                     i++;
70231                     break;
70232                   }
70233                 }
70234               }
70235             }
70236             break;
70237           }
70238         }
70239       }
70240     }
70241 
70242 #ifndef SQLITE_OMIT_TRIGGER
70243     /* If we have not already resolved the name, then maybe
70244     ** it is a new.* or old.* trigger argument reference
70245     */
70246     if( zDb==0 && zTab!=0 && cnt==0 && pParse->pTriggerTab!=0 ){
70247       int op = pParse->eTriggerOp;
70248       Table *pTab = 0;
70249       assert( op==TK_DELETE || op==TK_UPDATE || op==TK_INSERT );
70250       if( op!=TK_DELETE && sqlite3StrICmp("new",zTab) == 0 ){
70251         pExpr->iTable = 1;
70252         pTab = pParse->pTriggerTab;
70253       }else if( op!=TK_INSERT && sqlite3StrICmp("old",zTab)==0 ){
70254         pExpr->iTable = 0;
70255         pTab = pParse->pTriggerTab;
70256       }
70257 
70258       if( pTab ){
70259         int iCol;
70260         pSchema = pTab->pSchema;
70261         cntTab++;
70262         for(iCol=0; iCol<pTab->nCol; iCol++){
70263           Column *pCol = &pTab->aCol[iCol];
70264           if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
70265             if( iCol==pTab->iPKey ){
70266               iCol = -1;
70267             }
70268             break;
70269           }
70270         }
70271         if( iCol>=pTab->nCol && sqlite3IsRowid(zCol) ){
70272           iCol = -1;        /* IMP: R-44911-55124 */
70273         }
70274         if( iCol<pTab->nCol ){
70275           cnt++;
70276           if( iCol<0 ){
70277             pExpr->affinity = SQLITE_AFF_INTEGER;
70278           }else if( pExpr->iTable==0 ){
70279             testcase( iCol==31 );
70280             testcase( iCol==32 );
70281             pParse->oldmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
70282           }else{
70283             testcase( iCol==31 );
70284             testcase( iCol==32 );
70285             pParse->newmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
70286           }
70287           pExpr->iColumn = (i16)iCol;
70288           pExpr->pTab = pTab;
70289           isTrigger = 1;
70290         }
70291       }
70292     }
70293 #endif /* !defined(SQLITE_OMIT_TRIGGER) */
70294 
70295     /*
70296     ** Perhaps the name is a reference to the ROWID
70297     */
70298     if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){
70299       cnt = 1;
70300       pExpr->iColumn = -1;     /* IMP: R-44911-55124 */
70301       pExpr->affinity = SQLITE_AFF_INTEGER;
70302     }
70303 
70304     /*
70305     ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
70306     ** might refer to an result-set alias.  This happens, for example, when
70307     ** we are resolving names in the WHERE clause of the following command:
70308     **
70309     **     SELECT a+b AS x FROM table WHERE x<10;
70310     **
70311     ** In cases like this, replace pExpr with a copy of the expression that
70312     ** forms the result set entry ("a+b" in the example) and return immediately.
70313     ** Note that the expression in the result set should have already been
70314     ** resolved by the time the WHERE clause is resolved.
70315     */
70316     if( cnt==0 && (pEList = pNC->pEList)!=0 && zTab==0 ){
70317       for(j=0; j<pEList->nExpr; j++){
70318         char *zAs = pEList->a[j].zName;
70319         if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
70320           Expr *pOrig;
70321           assert( pExpr->pLeft==0 && pExpr->pRight==0 );
70322           assert( pExpr->x.pList==0 );
70323           assert( pExpr->x.pSelect==0 );
70324           pOrig = pEList->a[j].pExpr;
70325           if( !pNC->allowAgg && ExprHasProperty(pOrig, EP_Agg) ){
70326             sqlite3ErrorMsg(pParse, "misuse of aliased aggregate %s", zAs);
70327             return WRC_Abort;
70328           }
70329           resolveAlias(pParse, pEList, j, pExpr, "");
70330           cnt = 1;
70331           pMatch = 0;
70332           assert( zTab==0 && zDb==0 );
70333           goto lookupname_end;
70334         }
70335       }
70336     }
70337 
70338     /* Advance to the next name context.  The loop will exit when either
70339     ** we have a match (cnt>0) or when we run out of name contexts.
70340     */
70341     if( cnt==0 ){
70342       pNC = pNC->pNext;
70343     }
70344   }
70345 
70346   /*
70347   ** If X and Y are NULL (in other words if only the column name Z is
70348   ** supplied) and the value of Z is enclosed in double-quotes, then
70349   ** Z is a string literal if it doesn't match any column names.  In that
70350   ** case, we need to return right away and not make any changes to
70351   ** pExpr.
70352   **
70353   ** Because no reference was made to outer contexts, the pNC->nRef
70354   ** fields are not changed in any context.
70355   */
70356   if( cnt==0 && zTab==0 && ExprHasProperty(pExpr,EP_DblQuoted) ){
70357     pExpr->op = TK_STRING;
70358     pExpr->pTab = 0;
70359     return WRC_Prune;
70360   }
70361 
70362   /*
70363   ** cnt==0 means there was not match.  cnt>1 means there were two or
70364   ** more matches.  Either way, we have an error.
70365   */
70366   if( cnt!=1 ){
70367     const char *zErr;
70368     zErr = cnt==0 ? "no such column" : "ambiguous column name";
70369     if( zDb ){
70370       sqlite3ErrorMsg(pParse, "%s: %s.%s.%s", zErr, zDb, zTab, zCol);
70371     }else if( zTab ){
70372       sqlite3ErrorMsg(pParse, "%s: %s.%s", zErr, zTab, zCol);
70373     }else{
70374       sqlite3ErrorMsg(pParse, "%s: %s", zErr, zCol);
70375     }
70376     pParse->checkSchema = 1;
70377     pTopNC->nErr++;
70378   }
70379 
70380   /* If a column from a table in pSrcList is referenced, then record
70381   ** this fact in the pSrcList.a[].colUsed bitmask.  Column 0 causes
70382   ** bit 0 to be set.  Column 1 sets bit 1.  And so forth.  If the
70383   ** column number is greater than the number of bits in the bitmask
70384   ** then set the high-order bit of the bitmask.
70385   */
70386   if( pExpr->iColumn>=0 && pMatch!=0 ){
70387     int n = pExpr->iColumn;
70388     testcase( n==BMS-1 );
70389     if( n>=BMS ){
70390       n = BMS-1;
70391     }
70392     assert( pMatch->iCursor==pExpr->iTable );
70393     pMatch->colUsed |= ((Bitmask)1)<<n;
70394   }
70395 
70396   /* Clean up and return
70397   */
70398   sqlite3ExprDelete(db, pExpr->pLeft);
70399   pExpr->pLeft = 0;
70400   sqlite3ExprDelete(db, pExpr->pRight);
70401   pExpr->pRight = 0;
70402   pExpr->op = (isTrigger ? TK_TRIGGER : TK_COLUMN);
70403 lookupname_end:
70404   if( cnt==1 ){
70405     assert( pNC!=0 );
70406     sqlite3AuthRead(pParse, pExpr, pSchema, pNC->pSrcList);
70407     /* Increment the nRef value on all name contexts from TopNC up to
70408     ** the point where the name matched. */
70409     for(;;){
70410       assert( pTopNC!=0 );
70411       pTopNC->nRef++;
70412       if( pTopNC==pNC ) break;
70413       pTopNC = pTopNC->pNext;
70414     }
70415     return WRC_Prune;
70416   } else {
70417     return WRC_Abort;
70418   }
70419 }
70420 
70421 /*
70422 ** Allocate and return a pointer to an expression to load the column iCol
70423 ** from datasource iSrc in SrcList pSrc.
70424 */
70425 SQLITE_PRIVATE Expr *sqlite3CreateColumnExpr(sqlite3 *db, SrcList *pSrc, int iSrc, int iCol){
70426   Expr *p = sqlite3ExprAlloc(db, TK_COLUMN, 0, 0);
70427   if( p ){
70428     struct SrcList_item *pItem = &pSrc->a[iSrc];
70429     p->pTab = pItem->pTab;
70430     p->iTable = pItem->iCursor;
70431     if( p->pTab->iPKey==iCol ){
70432       p->iColumn = -1;
70433     }else{
70434       p->iColumn = (ynVar)iCol;
70435       testcase( iCol==BMS );
70436       testcase( iCol==BMS-1 );
70437       pItem->colUsed |= ((Bitmask)1)<<(iCol>=BMS ? BMS-1 : iCol);
70438     }
70439     ExprSetProperty(p, EP_Resolved);
70440   }
70441   return p;
70442 }
70443 
70444 /*
70445 ** This routine is callback for sqlite3WalkExpr().
70446 **
70447 ** Resolve symbolic names into TK_COLUMN operators for the current
70448 ** node in the expression tree.  Return 0 to continue the search down
70449 ** the tree or 2 to abort the tree walk.
70450 **
70451 ** This routine also does error checking and name resolution for
70452 ** function names.  The operator for aggregate functions is changed
70453 ** to TK_AGG_FUNCTION.
70454 */
70455 static int resolveExprStep(Walker *pWalker, Expr *pExpr){
70456   NameContext *pNC;
70457   Parse *pParse;
70458 
70459   pNC = pWalker->u.pNC;
70460   assert( pNC!=0 );
70461   pParse = pNC->pParse;
70462   assert( pParse==pWalker->pParse );
70463 
70464   if( ExprHasAnyProperty(pExpr, EP_Resolved) ) return WRC_Prune;
70465   ExprSetProperty(pExpr, EP_Resolved);
70466 #ifndef NDEBUG
70467   if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){
70468     SrcList *pSrcList = pNC->pSrcList;
70469     int i;
70470     for(i=0; i<pNC->pSrcList->nSrc; i++){
70471       assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
70472     }
70473   }
70474 #endif
70475   switch( pExpr->op ){
70476 
70477 #if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
70478     /* The special operator TK_ROW means use the rowid for the first
70479     ** column in the FROM clause.  This is used by the LIMIT and ORDER BY
70480     ** clause processing on UPDATE and DELETE statements.
70481     */
70482     case TK_ROW: {
70483       SrcList *pSrcList = pNC->pSrcList;
70484       struct SrcList_item *pItem;
70485       assert( pSrcList && pSrcList->nSrc==1 );
70486       pItem = pSrcList->a;
70487       pExpr->op = TK_COLUMN;
70488       pExpr->pTab = pItem->pTab;
70489       pExpr->iTable = pItem->iCursor;
70490       pExpr->iColumn = -1;
70491       pExpr->affinity = SQLITE_AFF_INTEGER;
70492       break;
70493     }
70494 #endif /* defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY) */
70495 
70496     /* A lone identifier is the name of a column.
70497     */
70498     case TK_ID: {
70499       return lookupName(pParse, 0, 0, pExpr->u.zToken, pNC, pExpr);
70500     }
70501 
70502     /* A table name and column name:     ID.ID
70503     ** Or a database, table and column:  ID.ID.ID
70504     */
70505     case TK_DOT: {
70506       const char *zColumn;
70507       const char *zTable;
70508       const char *zDb;
70509       Expr *pRight;
70510 
70511       /* if( pSrcList==0 ) break; */
70512       pRight = pExpr->pRight;
70513       if( pRight->op==TK_ID ){
70514         zDb = 0;
70515         zTable = pExpr->pLeft->u.zToken;
70516         zColumn = pRight->u.zToken;
70517       }else{
70518         assert( pRight->op==TK_DOT );
70519         zDb = pExpr->pLeft->u.zToken;
70520         zTable = pRight->pLeft->u.zToken;
70521         zColumn = pRight->pRight->u.zToken;
70522       }
70523       return lookupName(pParse, zDb, zTable, zColumn, pNC, pExpr);
70524     }
70525 
70526     /* Resolve function names
70527     */
70528     case TK_CONST_FUNC:
70529     case TK_FUNCTION: {
70530       ExprList *pList = pExpr->x.pList;    /* The argument list */
70531       int n = pList ? pList->nExpr : 0;    /* Number of arguments */
70532       int no_such_func = 0;       /* True if no such function exists */
70533       int wrong_num_args = 0;     /* True if wrong number of arguments */
70534       int is_agg = 0;             /* True if is an aggregate function */
70535       int auth;                   /* Authorization to use the function */
70536       int nId;                    /* Number of characters in function name */
70537       const char *zId;            /* The function name. */
70538       FuncDef *pDef;              /* Information about the function */
70539       u8 enc = ENC(pParse->db);   /* The database encoding */
70540 
70541       testcase( pExpr->op==TK_CONST_FUNC );
70542       assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
70543       zId = pExpr->u.zToken;
70544       nId = sqlite3Strlen30(zId);
70545       pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
70546       if( pDef==0 ){
70547         pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0);
70548         if( pDef==0 ){
70549           no_such_func = 1;
70550         }else{
70551           wrong_num_args = 1;
70552         }
70553       }else{
70554         is_agg = pDef->xFunc==0;
70555       }
70556 #ifndef SQLITE_OMIT_AUTHORIZATION
70557       if( pDef ){
70558         auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0, pDef->zName, 0);
70559         if( auth!=SQLITE_OK ){
70560           if( auth==SQLITE_DENY ){
70561             sqlite3ErrorMsg(pParse, "not authorized to use function: %s",
70562                                     pDef->zName);
70563             pNC->nErr++;
70564           }
70565           pExpr->op = TK_NULL;
70566           return WRC_Prune;
70567         }
70568       }
70569 #endif
70570       if( is_agg && !pNC->allowAgg ){
70571         sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId);
70572         pNC->nErr++;
70573         is_agg = 0;
70574       }else if( no_such_func ){
70575         sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
70576         pNC->nErr++;
70577       }else if( wrong_num_args ){
70578         sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
70579              nId, zId);
70580         pNC->nErr++;
70581       }
70582       if( is_agg ){
70583         pExpr->op = TK_AGG_FUNCTION;
70584         pNC->hasAgg = 1;
70585       }
70586       if( is_agg ) pNC->allowAgg = 0;
70587       sqlite3WalkExprList(pWalker, pList);
70588       if( is_agg ) pNC->allowAgg = 1;
70589       /* FIX ME:  Compute pExpr->affinity based on the expected return
70590       ** type of the function
70591       */
70592       return WRC_Prune;
70593     }
70594 #ifndef SQLITE_OMIT_SUBQUERY
70595     case TK_SELECT:
70596     case TK_EXISTS:  testcase( pExpr->op==TK_EXISTS );
70597 #endif
70598     case TK_IN: {
70599       testcase( pExpr->op==TK_IN );
70600       if( ExprHasProperty(pExpr, EP_xIsSelect) ){
70601         int nRef = pNC->nRef;
70602 #ifndef SQLITE_OMIT_CHECK
70603         if( pNC->isCheck ){
70604           sqlite3ErrorMsg(pParse,"subqueries prohibited in CHECK constraints");
70605         }
70606 #endif
70607         sqlite3WalkSelect(pWalker, pExpr->x.pSelect);
70608         assert( pNC->nRef>=nRef );
70609         if( nRef!=pNC->nRef ){
70610           ExprSetProperty(pExpr, EP_VarSelect);
70611         }
70612       }
70613       break;
70614     }
70615 #ifndef SQLITE_OMIT_CHECK
70616     case TK_VARIABLE: {
70617       if( pNC->isCheck ){
70618         sqlite3ErrorMsg(pParse,"parameters prohibited in CHECK constraints");
70619       }
70620       break;
70621     }
70622 #endif
70623   }
70624   return (pParse->nErr || pParse->db->mallocFailed) ? WRC_Abort : WRC_Continue;
70625 }
70626 
70627 /*
70628 ** pEList is a list of expressions which are really the result set of the
70629 ** a SELECT statement.  pE is a term in an ORDER BY or GROUP BY clause.
70630 ** This routine checks to see if pE is a simple identifier which corresponds
70631 ** to the AS-name of one of the terms of the expression list.  If it is,
70632 ** this routine return an integer between 1 and N where N is the number of
70633 ** elements in pEList, corresponding to the matching entry.  If there is
70634 ** no match, or if pE is not a simple identifier, then this routine
70635 ** return 0.
70636 **
70637 ** pEList has been resolved.  pE has not.
70638 */
70639 static int resolveAsName(
70640   Parse *pParse,     /* Parsing context for error messages */
70641   ExprList *pEList,  /* List of expressions to scan */
70642   Expr *pE           /* Expression we are trying to match */
70643 ){
70644   int i;             /* Loop counter */
70645 
70646   UNUSED_PARAMETER(pParse);
70647 
70648   if( pE->op==TK_ID ){
70649     char *zCol = pE->u.zToken;
70650     for(i=0; i<pEList->nExpr; i++){
70651       char *zAs = pEList->a[i].zName;
70652       if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
70653         return i+1;
70654       }
70655     }
70656   }
70657   return 0;
70658 }
70659 
70660 /*
70661 ** pE is a pointer to an expression which is a single term in the
70662 ** ORDER BY of a compound SELECT.  The expression has not been
70663 ** name resolved.
70664 **
70665 ** At the point this routine is called, we already know that the
70666 ** ORDER BY term is not an integer index into the result set.  That
70667 ** case is handled by the calling routine.
70668 **
70669 ** Attempt to match pE against result set columns in the left-most
70670 ** SELECT statement.  Return the index i of the matching column,
70671 ** as an indication to the caller that it should sort by the i-th column.
70672 ** The left-most column is 1.  In other words, the value returned is the
70673 ** same integer value that would be used in the SQL statement to indicate
70674 ** the column.
70675 **
70676 ** If there is no match, return 0.  Return -1 if an error occurs.
70677 */
70678 static int resolveOrderByTermToExprList(
70679   Parse *pParse,     /* Parsing context for error messages */
70680   Select *pSelect,   /* The SELECT statement with the ORDER BY clause */
70681   Expr *pE           /* The specific ORDER BY term */
70682 ){
70683   int i;             /* Loop counter */
70684   ExprList *pEList;  /* The columns of the result set */
70685   NameContext nc;    /* Name context for resolving pE */
70686   sqlite3 *db;       /* Database connection */
70687   int rc;            /* Return code from subprocedures */
70688   u8 savedSuppErr;   /* Saved value of db->suppressErr */
70689 
70690   assert( sqlite3ExprIsInteger(pE, &i)==0 );
70691   pEList = pSelect->pEList;
70692 
70693   /* Resolve all names in the ORDER BY term expression
70694   */
70695   memset(&nc, 0, sizeof(nc));
70696   nc.pParse = pParse;
70697   nc.pSrcList = pSelect->pSrc;
70698   nc.pEList = pEList;
70699   nc.allowAgg = 1;
70700   nc.nErr = 0;
70701   db = pParse->db;
70702   savedSuppErr = db->suppressErr;
70703   db->suppressErr = 1;
70704   rc = sqlite3ResolveExprNames(&nc, pE);
70705   db->suppressErr = savedSuppErr;
70706   if( rc ) return 0;
70707 
70708   /* Try to match the ORDER BY expression against an expression
70709   ** in the result set.  Return an 1-based index of the matching
70710   ** result-set entry.
70711   */
70712   for(i=0; i<pEList->nExpr; i++){
70713     if( sqlite3ExprCompare(pEList->a[i].pExpr, pE)<2 ){
70714       return i+1;
70715     }
70716   }
70717 
70718   /* If no match, return 0. */
70719   return 0;
70720 }
70721 
70722 /*
70723 ** Generate an ORDER BY or GROUP BY term out-of-range error.
70724 */
70725 static void resolveOutOfRangeError(
70726   Parse *pParse,         /* The error context into which to write the error */
70727   const char *zType,     /* "ORDER" or "GROUP" */
70728   int i,                 /* The index (1-based) of the term out of range */
70729   int mx                 /* Largest permissible value of i */
70730 ){
70731   sqlite3ErrorMsg(pParse,
70732     "%r %s BY term out of range - should be "
70733     "between 1 and %d", i, zType, mx);
70734 }
70735 
70736 /*
70737 ** Analyze the ORDER BY clause in a compound SELECT statement.   Modify
70738 ** each term of the ORDER BY clause is a constant integer between 1
70739 ** and N where N is the number of columns in the compound SELECT.
70740 **
70741 ** ORDER BY terms that are already an integer between 1 and N are
70742 ** unmodified.  ORDER BY terms that are integers outside the range of
70743 ** 1 through N generate an error.  ORDER BY terms that are expressions
70744 ** are matched against result set expressions of compound SELECT
70745 ** beginning with the left-most SELECT and working toward the right.
70746 ** At the first match, the ORDER BY expression is transformed into
70747 ** the integer column number.
70748 **
70749 ** Return the number of errors seen.
70750 */
70751 static int resolveCompoundOrderBy(
70752   Parse *pParse,        /* Parsing context.  Leave error messages here */
70753   Select *pSelect       /* The SELECT statement containing the ORDER BY */
70754 ){
70755   int i;
70756   ExprList *pOrderBy;
70757   ExprList *pEList;
70758   sqlite3 *db;
70759   int moreToDo = 1;
70760 
70761   pOrderBy = pSelect->pOrderBy;
70762   if( pOrderBy==0 ) return 0;
70763   db = pParse->db;
70764 #if SQLITE_MAX_COLUMN
70765   if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
70766     sqlite3ErrorMsg(pParse, "too many terms in ORDER BY clause");
70767     return 1;
70768   }
70769 #endif
70770   for(i=0; i<pOrderBy->nExpr; i++){
70771     pOrderBy->a[i].done = 0;
70772   }
70773   pSelect->pNext = 0;
70774   while( pSelect->pPrior ){
70775     pSelect->pPrior->pNext = pSelect;
70776     pSelect = pSelect->pPrior;
70777   }
70778   while( pSelect && moreToDo ){
70779     struct ExprList_item *pItem;
70780     moreToDo = 0;
70781     pEList = pSelect->pEList;
70782     assert( pEList!=0 );
70783     for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
70784       int iCol = -1;
70785       Expr *pE, *pDup;
70786       if( pItem->done ) continue;
70787       pE = pItem->pExpr;
70788       if( sqlite3ExprIsInteger(pE, &iCol) ){
70789         if( iCol<=0 || iCol>pEList->nExpr ){
70790           resolveOutOfRangeError(pParse, "ORDER", i+1, pEList->nExpr);
70791           return 1;
70792         }
70793       }else{
70794         iCol = resolveAsName(pParse, pEList, pE);
70795         if( iCol==0 ){
70796           pDup = sqlite3ExprDup(db, pE, 0);
70797           if( !db->mallocFailed ){
70798             assert(pDup);
70799             iCol = resolveOrderByTermToExprList(pParse, pSelect, pDup);
70800           }
70801           sqlite3ExprDelete(db, pDup);
70802         }
70803       }
70804       if( iCol>0 ){
70805         CollSeq *pColl = pE->pColl;
70806         int flags = pE->flags & EP_ExpCollate;
70807         sqlite3ExprDelete(db, pE);
70808         pItem->pExpr = pE = sqlite3Expr(db, TK_INTEGER, 0);
70809         if( pE==0 ) return 1;
70810         pE->pColl = pColl;
70811         pE->flags |= EP_IntValue | flags;
70812         pE->u.iValue = iCol;
70813         pItem->iCol = (u16)iCol;
70814         pItem->done = 1;
70815       }else{
70816         moreToDo = 1;
70817       }
70818     }
70819     pSelect = pSelect->pNext;
70820   }
70821   for(i=0; i<pOrderBy->nExpr; i++){
70822     if( pOrderBy->a[i].done==0 ){
70823       sqlite3ErrorMsg(pParse, "%r ORDER BY term does not match any "
70824             "column in the result set", i+1);
70825       return 1;
70826     }
70827   }
70828   return 0;
70829 }
70830 
70831 /*
70832 ** Check every term in the ORDER BY or GROUP BY clause pOrderBy of
70833 ** the SELECT statement pSelect.  If any term is reference to a
70834 ** result set expression (as determined by the ExprList.a.iCol field)
70835 ** then convert that term into a copy of the corresponding result set
70836 ** column.
70837 **
70838 ** If any errors are detected, add an error message to pParse and
70839 ** return non-zero.  Return zero if no errors are seen.
70840 */
70841 SQLITE_PRIVATE int sqlite3ResolveOrderGroupBy(
70842   Parse *pParse,        /* Parsing context.  Leave error messages here */
70843   Select *pSelect,      /* The SELECT statement containing the clause */
70844   ExprList *pOrderBy,   /* The ORDER BY or GROUP BY clause to be processed */
70845   const char *zType     /* "ORDER" or "GROUP" */
70846 ){
70847   int i;
70848   sqlite3 *db = pParse->db;
70849   ExprList *pEList;
70850   struct ExprList_item *pItem;
70851 
70852   if( pOrderBy==0 || pParse->db->mallocFailed ) return 0;
70853 #if SQLITE_MAX_COLUMN
70854   if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
70855     sqlite3ErrorMsg(pParse, "too many terms in %s BY clause", zType);
70856     return 1;
70857   }
70858 #endif
70859   pEList = pSelect->pEList;
70860   assert( pEList!=0 );  /* sqlite3SelectNew() guarantees this */
70861   for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
70862     if( pItem->iCol ){
70863       if( pItem->iCol>pEList->nExpr ){
70864         resolveOutOfRangeError(pParse, zType, i+1, pEList->nExpr);
70865         return 1;
70866       }
70867       resolveAlias(pParse, pEList, pItem->iCol-1, pItem->pExpr, zType);
70868     }
70869   }
70870   return 0;
70871 }
70872 
70873 /*
70874 ** pOrderBy is an ORDER BY or GROUP BY clause in SELECT statement pSelect.
70875 ** The Name context of the SELECT statement is pNC.  zType is either
70876 ** "ORDER" or "GROUP" depending on which type of clause pOrderBy is.
70877 **
70878 ** This routine resolves each term of the clause into an expression.
70879 ** If the order-by term is an integer I between 1 and N (where N is the
70880 ** number of columns in the result set of the SELECT) then the expression
70881 ** in the resolution is a copy of the I-th result-set expression.  If
70882 ** the order-by term is an identify that corresponds to the AS-name of
70883 ** a result-set expression, then the term resolves to a copy of the
70884 ** result-set expression.  Otherwise, the expression is resolved in
70885 ** the usual way - using sqlite3ResolveExprNames().
70886 **
70887 ** This routine returns the number of errors.  If errors occur, then
70888 ** an appropriate error message might be left in pParse.  (OOM errors
70889 ** excepted.)
70890 */
70891 static int resolveOrderGroupBy(
70892   NameContext *pNC,     /* The name context of the SELECT statement */
70893   Select *pSelect,      /* The SELECT statement holding pOrderBy */
70894   ExprList *pOrderBy,   /* An ORDER BY or GROUP BY clause to resolve */
70895   const char *zType     /* Either "ORDER" or "GROUP", as appropriate */
70896 ){
70897   int i;                         /* Loop counter */
70898   int iCol;                      /* Column number */
70899   struct ExprList_item *pItem;   /* A term of the ORDER BY clause */
70900   Parse *pParse;                 /* Parsing context */
70901   int nResult;                   /* Number of terms in the result set */
70902 
70903   if( pOrderBy==0 ) return 0;
70904   nResult = pSelect->pEList->nExpr;
70905   pParse = pNC->pParse;
70906   for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
70907     Expr *pE = pItem->pExpr;
70908     iCol = resolveAsName(pParse, pSelect->pEList, pE);
70909     if( iCol>0 ){
70910       /* If an AS-name match is found, mark this ORDER BY column as being
70911       ** a copy of the iCol-th result-set column.  The subsequent call to
70912       ** sqlite3ResolveOrderGroupBy() will convert the expression to a
70913       ** copy of the iCol-th result-set expression. */
70914       pItem->iCol = (u16)iCol;
70915       continue;
70916     }
70917     if( sqlite3ExprIsInteger(pE, &iCol) ){
70918       /* The ORDER BY term is an integer constant.  Again, set the column
70919       ** number so that sqlite3ResolveOrderGroupBy() will convert the
70920       ** order-by term to a copy of the result-set expression */
70921       if( iCol<1 ){
70922         resolveOutOfRangeError(pParse, zType, i+1, nResult);
70923         return 1;
70924       }
70925       pItem->iCol = (u16)iCol;
70926       continue;
70927     }
70928 
70929     /* Otherwise, treat the ORDER BY term as an ordinary expression */
70930     pItem->iCol = 0;
70931     if( sqlite3ResolveExprNames(pNC, pE) ){
70932       return 1;
70933     }
70934   }
70935   return sqlite3ResolveOrderGroupBy(pParse, pSelect, pOrderBy, zType);
70936 }
70937 
70938 /*
70939 ** Resolve names in the SELECT statement p and all of its descendents.
70940 */
70941 static int resolveSelectStep(Walker *pWalker, Select *p){
70942   NameContext *pOuterNC;  /* Context that contains this SELECT */
70943   NameContext sNC;        /* Name context of this SELECT */
70944   int isCompound;         /* True if p is a compound select */
70945   int nCompound;          /* Number of compound terms processed so far */
70946   Parse *pParse;          /* Parsing context */
70947   ExprList *pEList;       /* Result set expression list */
70948   int i;                  /* Loop counter */
70949   ExprList *pGroupBy;     /* The GROUP BY clause */
70950   Select *pLeftmost;      /* Left-most of SELECT of a compound */
70951   sqlite3 *db;            /* Database connection */
70952 
70953 
70954   assert( p!=0 );
70955   if( p->selFlags & SF_Resolved ){
70956     return WRC_Prune;
70957   }
70958   pOuterNC = pWalker->u.pNC;
70959   pParse = pWalker->pParse;
70960   db = pParse->db;
70961 
70962   /* Normally sqlite3SelectExpand() will be called first and will have
70963   ** already expanded this SELECT.  However, if this is a subquery within
70964   ** an expression, sqlite3ResolveExprNames() will be called without a
70965   ** prior call to sqlite3SelectExpand().  When that happens, let
70966   ** sqlite3SelectPrep() do all of the processing for this SELECT.
70967   ** sqlite3SelectPrep() will invoke both sqlite3SelectExpand() and
70968   ** this routine in the correct order.
70969   */
70970   if( (p->selFlags & SF_Expanded)==0 ){
70971     sqlite3SelectPrep(pParse, p, pOuterNC);
70972     return (pParse->nErr || db->mallocFailed) ? WRC_Abort : WRC_Prune;
70973   }
70974 
70975   isCompound = p->pPrior!=0;
70976   nCompound = 0;
70977   pLeftmost = p;
70978   while( p ){
70979     assert( (p->selFlags & SF_Expanded)!=0 );
70980     assert( (p->selFlags & SF_Resolved)==0 );
70981     p->selFlags |= SF_Resolved;
70982 
70983     /* Resolve the expressions in the LIMIT and OFFSET clauses. These
70984     ** are not allowed to refer to any names, so pass an empty NameContext.
70985     */
70986     memset(&sNC, 0, sizeof(sNC));
70987     sNC.pParse = pParse;
70988     if( sqlite3ResolveExprNames(&sNC, p->pLimit) ||
70989         sqlite3ResolveExprNames(&sNC, p->pOffset) ){
70990       return WRC_Abort;
70991     }
70992 
70993     /* Set up the local name-context to pass to sqlite3ResolveExprNames() to
70994     ** resolve the result-set expression list.
70995     */
70996     sNC.allowAgg = 1;
70997     sNC.pSrcList = p->pSrc;
70998     sNC.pNext = pOuterNC;
70999 
71000     /* Resolve names in the result set. */
71001     pEList = p->pEList;
71002     assert( pEList!=0 );
71003     for(i=0; i<pEList->nExpr; i++){
71004       Expr *pX = pEList->a[i].pExpr;
71005       if( sqlite3ResolveExprNames(&sNC, pX) ){
71006         return WRC_Abort;
71007       }
71008     }
71009 
71010     /* Recursively resolve names in all subqueries
71011     */
71012     for(i=0; i<p->pSrc->nSrc; i++){
71013       struct SrcList_item *pItem = &p->pSrc->a[i];
71014       if( pItem->pSelect ){
71015         const char *zSavedContext = pParse->zAuthContext;
71016         if( pItem->zName ) pParse->zAuthContext = pItem->zName;
71017         sqlite3ResolveSelectNames(pParse, pItem->pSelect, pOuterNC);
71018         pParse->zAuthContext = zSavedContext;
71019         if( pParse->nErr || db->mallocFailed ) return WRC_Abort;
71020       }
71021     }
71022 
71023     /* If there are no aggregate functions in the result-set, and no GROUP BY
71024     ** expression, do not allow aggregates in any of the other expressions.
71025     */
71026     assert( (p->selFlags & SF_Aggregate)==0 );
71027     pGroupBy = p->pGroupBy;
71028     if( pGroupBy || sNC.hasAgg ){
71029       p->selFlags |= SF_Aggregate;
71030     }else{
71031       sNC.allowAgg = 0;
71032     }
71033 
71034     /* If a HAVING clause is present, then there must be a GROUP BY clause.
71035     */
71036     if( p->pHaving && !pGroupBy ){
71037       sqlite3ErrorMsg(pParse, "a GROUP BY clause is required before HAVING");
71038       return WRC_Abort;
71039     }
71040 
71041     /* Add the expression list to the name-context before parsing the
71042     ** other expressions in the SELECT statement. This is so that
71043     ** expressions in the WHERE clause (etc.) can refer to expressions by
71044     ** aliases in the result set.
71045     **
71046     ** Minor point: If this is the case, then the expression will be
71047     ** re-evaluated for each reference to it.
71048     */
71049     sNC.pEList = p->pEList;
71050     if( sqlite3ResolveExprNames(&sNC, p->pWhere) ||
71051        sqlite3ResolveExprNames(&sNC, p->pHaving)
71052     ){
71053       return WRC_Abort;
71054     }
71055 
71056     /* The ORDER BY and GROUP BY clauses may not refer to terms in
71057     ** outer queries
71058     */
71059     sNC.pNext = 0;
71060     sNC.allowAgg = 1;
71061 
71062     /* Process the ORDER BY clause for singleton SELECT statements.
71063     ** The ORDER BY clause for compounds SELECT statements is handled
71064     ** below, after all of the result-sets for all of the elements of
71065     ** the compound have been resolved.
71066     */
71067     if( !isCompound && resolveOrderGroupBy(&sNC, p, p->pOrderBy, "ORDER") ){
71068       return WRC_Abort;
71069     }
71070     if( db->mallocFailed ){
71071       return WRC_Abort;
71072     }
71073 
71074     /* Resolve the GROUP BY clause.  At the same time, make sure
71075     ** the GROUP BY clause does not contain aggregate functions.
71076     */
71077     if( pGroupBy ){
71078       struct ExprList_item *pItem;
71079 
71080       if( resolveOrderGroupBy(&sNC, p, pGroupBy, "GROUP") || db->mallocFailed ){
71081         return WRC_Abort;
71082       }
71083       for(i=0, pItem=pGroupBy->a; i<pGroupBy->nExpr; i++, pItem++){
71084         if( ExprHasProperty(pItem->pExpr, EP_Agg) ){
71085           sqlite3ErrorMsg(pParse, "aggregate functions are not allowed in "
71086               "the GROUP BY clause");
71087           return WRC_Abort;
71088         }
71089       }
71090     }
71091 
71092     /* Advance to the next term of the compound
71093     */
71094     p = p->pPrior;
71095     nCompound++;
71096   }
71097 
71098   /* Resolve the ORDER BY on a compound SELECT after all terms of
71099   ** the compound have been resolved.
71100   */
71101   if( isCompound && resolveCompoundOrderBy(pParse, pLeftmost) ){
71102     return WRC_Abort;
71103   }
71104 
71105   return WRC_Prune;
71106 }
71107 
71108 /*
71109 ** This routine walks an expression tree and resolves references to
71110 ** table columns and result-set columns.  At the same time, do error
71111 ** checking on function usage and set a flag if any aggregate functions
71112 ** are seen.
71113 **
71114 ** To resolve table columns references we look for nodes (or subtrees) of the
71115 ** form X.Y.Z or Y.Z or just Z where
71116 **
71117 **      X:   The name of a database.  Ex:  "main" or "temp" or
71118 **           the symbolic name assigned to an ATTACH-ed database.
71119 **
71120 **      Y:   The name of a table in a FROM clause.  Or in a trigger
71121 **           one of the special names "old" or "new".
71122 **
71123 **      Z:   The name of a column in table Y.
71124 **
71125 ** The node at the root of the subtree is modified as follows:
71126 **
71127 **    Expr.op        Changed to TK_COLUMN
71128 **    Expr.pTab      Points to the Table object for X.Y
71129 **    Expr.iColumn   The column index in X.Y.  -1 for the rowid.
71130 **    Expr.iTable    The VDBE cursor number for X.Y
71131 **
71132 **
71133 ** To resolve result-set references, look for expression nodes of the
71134 ** form Z (with no X and Y prefix) where the Z matches the right-hand
71135 ** size of an AS clause in the result-set of a SELECT.  The Z expression
71136 ** is replaced by a copy of the left-hand side of the result-set expression.
71137 ** Table-name and function resolution occurs on the substituted expression
71138 ** tree.  For example, in:
71139 **
71140 **      SELECT a+b AS x, c+d AS y FROM t1 ORDER BY x;
71141 **
71142 ** The "x" term of the order by is replaced by "a+b" to render:
71143 **
71144 **      SELECT a+b AS x, c+d AS y FROM t1 ORDER BY a+b;
71145 **
71146 ** Function calls are checked to make sure that the function is
71147 ** defined and that the correct number of arguments are specified.
71148 ** If the function is an aggregate function, then the pNC->hasAgg is
71149 ** set and the opcode is changed from TK_FUNCTION to TK_AGG_FUNCTION.
71150 ** If an expression contains aggregate functions then the EP_Agg
71151 ** property on the expression is set.
71152 **
71153 ** An error message is left in pParse if anything is amiss.  The number
71154 ** if errors is returned.
71155 */
71156 SQLITE_PRIVATE int sqlite3ResolveExprNames(
71157   NameContext *pNC,       /* Namespace to resolve expressions in. */
71158   Expr *pExpr             /* The expression to be analyzed. */
71159 ){
71160   int savedHasAgg;
71161   Walker w;
71162 
71163   if( pExpr==0 ) return 0;
71164 #if SQLITE_MAX_EXPR_DEPTH>0
71165   {
71166     Parse *pParse = pNC->pParse;
71167     if( sqlite3ExprCheckHeight(pParse, pExpr->nHeight+pNC->pParse->nHeight) ){
71168       return 1;
71169     }
71170     pParse->nHeight += pExpr->nHeight;
71171   }
71172 #endif
71173   savedHasAgg = pNC->hasAgg;
71174   pNC->hasAgg = 0;
71175   w.xExprCallback = resolveExprStep;
71176   w.xSelectCallback = resolveSelectStep;
71177   w.pParse = pNC->pParse;
71178   w.u.pNC = pNC;
71179   sqlite3WalkExpr(&w, pExpr);
71180 #if SQLITE_MAX_EXPR_DEPTH>0
71181   pNC->pParse->nHeight -= pExpr->nHeight;
71182 #endif
71183   if( pNC->nErr>0 || w.pParse->nErr>0 ){
71184     ExprSetProperty(pExpr, EP_Error);
71185   }
71186   if( pNC->hasAgg ){
71187     ExprSetProperty(pExpr, EP_Agg);
71188   }else if( savedHasAgg ){
71189     pNC->hasAgg = 1;
71190   }
71191   return ExprHasProperty(pExpr, EP_Error);
71192 }
71193 
71194 
71195 /*
71196 ** Resolve all names in all expressions of a SELECT and in all
71197 ** decendents of the SELECT, including compounds off of p->pPrior,
71198 ** subqueries in expressions, and subqueries used as FROM clause
71199 ** terms.
71200 **
71201 ** See sqlite3ResolveExprNames() for a description of the kinds of
71202 ** transformations that occur.
71203 **
71204 ** All SELECT statements should have been expanded using
71205 ** sqlite3SelectExpand() prior to invoking this routine.
71206 */
71207 SQLITE_PRIVATE void sqlite3ResolveSelectNames(
71208   Parse *pParse,         /* The parser context */
71209   Select *p,             /* The SELECT statement being coded. */
71210   NameContext *pOuterNC  /* Name context for parent SELECT statement */
71211 ){
71212   Walker w;
71213 
71214   assert( p!=0 );
71215   w.xExprCallback = resolveExprStep;
71216   w.xSelectCallback = resolveSelectStep;
71217   w.pParse = pParse;
71218   w.u.pNC = pOuterNC;
71219   sqlite3WalkSelect(&w, p);
71220 }
71221 
71222 /************** End of resolve.c *********************************************/
71223 /************** Begin file expr.c ********************************************/
71224 /*
71225 ** 2001 September 15
71226 **
71227 ** The author disclaims copyright to this source code.  In place of
71228 ** a legal notice, here is a blessing:
71229 **
71230 **    May you do good and not evil.
71231 **    May you find forgiveness for yourself and forgive others.
71232 **    May you share freely, never taking more than you give.
71233 **
71234 *************************************************************************
71235 ** This file contains routines used for analyzing expressions and
71236 ** for generating VDBE code that evaluates expressions in SQLite.
71237 */
71238 
71239 /*
71240 ** Return the 'affinity' of the expression pExpr if any.
71241 **
71242 ** If pExpr is a column, a reference to a column via an 'AS' alias,
71243 ** or a sub-select with a column as the return value, then the
71244 ** affinity of that column is returned. Otherwise, 0x00 is returned,
71245 ** indicating no affinity for the expression.
71246 **
71247 ** i.e. the WHERE clause expresssions in the following statements all
71248 ** have an affinity:
71249 **
71250 ** CREATE TABLE t1(a);
71251 ** SELECT * FROM t1 WHERE a;
71252 ** SELECT a AS b FROM t1 WHERE b;
71253 ** SELECT * FROM t1 WHERE (select a from t1);
71254 */
71255 SQLITE_PRIVATE char sqlite3ExprAffinity(Expr *pExpr){
71256   int op = pExpr->op;
71257   if( op==TK_SELECT ){
71258     assert( pExpr->flags&EP_xIsSelect );
71259     return sqlite3ExprAffinity(pExpr->x.pSelect->pEList->a[0].pExpr);
71260   }
71261 #ifndef SQLITE_OMIT_CAST
71262   if( op==TK_CAST ){
71263     assert( !ExprHasProperty(pExpr, EP_IntValue) );
71264     return sqlite3AffinityType(pExpr->u.zToken);
71265   }
71266 #endif
71267   if( (op==TK_AGG_COLUMN || op==TK_COLUMN || op==TK_REGISTER)
71268    && pExpr->pTab!=0
71269   ){
71270     /* op==TK_REGISTER && pExpr->pTab!=0 happens when pExpr was originally
71271     ** a TK_COLUMN but was previously evaluated and cached in a register */
71272     int j = pExpr->iColumn;
71273     if( j<0 ) return SQLITE_AFF_INTEGER;
71274     assert( pExpr->pTab && j<pExpr->pTab->nCol );
71275     return pExpr->pTab->aCol[j].affinity;
71276   }
71277   return pExpr->affinity;
71278 }
71279 
71280 /*
71281 ** Set the explicit collating sequence for an expression to the
71282 ** collating sequence supplied in the second argument.
71283 */
71284 SQLITE_PRIVATE Expr *sqlite3ExprSetColl(Expr *pExpr, CollSeq *pColl){
71285   if( pExpr && pColl ){
71286     pExpr->pColl = pColl;
71287     pExpr->flags |= EP_ExpCollate;
71288   }
71289   return pExpr;
71290 }
71291 
71292 /*
71293 ** Set the collating sequence for expression pExpr to be the collating
71294 ** sequence named by pToken.   Return a pointer to the revised expression.
71295 ** The collating sequence is marked as "explicit" using the EP_ExpCollate
71296 ** flag.  An explicit collating sequence will override implicit
71297 ** collating sequences.
71298 */
71299 SQLITE_PRIVATE Expr *sqlite3ExprSetCollByToken(Parse *pParse, Expr *pExpr, Token *pCollName){
71300   char *zColl = 0;            /* Dequoted name of collation sequence */
71301   CollSeq *pColl;
71302   sqlite3 *db = pParse->db;
71303   zColl = sqlite3NameFromToken(db, pCollName);
71304   pColl = sqlite3LocateCollSeq(pParse, zColl);
71305   sqlite3ExprSetColl(pExpr, pColl);
71306   sqlite3DbFree(db, zColl);
71307   return pExpr;
71308 }
71309 
71310 /*
71311 ** Return the default collation sequence for the expression pExpr. If
71312 ** there is no default collation type, return 0.
71313 */
71314 SQLITE_PRIVATE CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){
71315   CollSeq *pColl = 0;
71316   Expr *p = pExpr;
71317   while( p ){
71318     int op;
71319     pColl = p->pColl;
71320     if( pColl ) break;
71321     op = p->op;
71322     if( p->pTab!=0 && (
71323         op==TK_AGG_COLUMN || op==TK_COLUMN || op==TK_REGISTER || op==TK_TRIGGER
71324     )){
71325       /* op==TK_REGISTER && p->pTab!=0 happens when pExpr was originally
71326       ** a TK_COLUMN but was previously evaluated and cached in a register */
71327       const char *zColl;
71328       int j = p->iColumn;
71329       if( j>=0 ){
71330         sqlite3 *db = pParse->db;
71331         zColl = p->pTab->aCol[j].zColl;
71332         pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0);
71333         pExpr->pColl = pColl;
71334       }
71335       break;
71336     }
71337     if( op!=TK_CAST && op!=TK_UPLUS ){
71338       break;
71339     }
71340     p = p->pLeft;
71341   }
71342   if( sqlite3CheckCollSeq(pParse, pColl) ){
71343     pColl = 0;
71344   }
71345   return pColl;
71346 }
71347 
71348 /*
71349 ** pExpr is an operand of a comparison operator.  aff2 is the
71350 ** type affinity of the other operand.  This routine returns the
71351 ** type affinity that should be used for the comparison operator.
71352 */
71353 SQLITE_PRIVATE char sqlite3CompareAffinity(Expr *pExpr, char aff2){
71354   char aff1 = sqlite3ExprAffinity(pExpr);
71355   if( aff1 && aff2 ){
71356     /* Both sides of the comparison are columns. If one has numeric
71357     ** affinity, use that. Otherwise use no affinity.
71358     */
71359     if( sqlite3IsNumericAffinity(aff1) || sqlite3IsNumericAffinity(aff2) ){
71360       return SQLITE_AFF_NUMERIC;
71361     }else{
71362       return SQLITE_AFF_NONE;
71363     }
71364   }else if( !aff1 && !aff2 ){
71365     /* Neither side of the comparison is a column.  Compare the
71366     ** results directly.
71367     */
71368     return SQLITE_AFF_NONE;
71369   }else{
71370     /* One side is a column, the other is not. Use the columns affinity. */
71371     assert( aff1==0 || aff2==0 );
71372     return (aff1 + aff2);
71373   }
71374 }
71375 
71376 /*
71377 ** pExpr is a comparison operator.  Return the type affinity that should
71378 ** be applied to both operands prior to doing the comparison.
71379 */
71380 static char comparisonAffinity(Expr *pExpr){
71381   char aff;
71382   assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||
71383           pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||
71384           pExpr->op==TK_NE || pExpr->op==TK_IS || pExpr->op==TK_ISNOT );
71385   assert( pExpr->pLeft );
71386   aff = sqlite3ExprAffinity(pExpr->pLeft);
71387   if( pExpr->pRight ){
71388     aff = sqlite3CompareAffinity(pExpr->pRight, aff);
71389   }else if( ExprHasProperty(pExpr, EP_xIsSelect) ){
71390     aff = sqlite3CompareAffinity(pExpr->x.pSelect->pEList->a[0].pExpr, aff);
71391   }else if( !aff ){
71392     aff = SQLITE_AFF_NONE;
71393   }
71394   return aff;
71395 }
71396 
71397 /*
71398 ** pExpr is a comparison expression, eg. '=', '<', IN(...) etc.
71399 ** idx_affinity is the affinity of an indexed column. Return true
71400 ** if the index with affinity idx_affinity may be used to implement
71401 ** the comparison in pExpr.
71402 */
71403 SQLITE_PRIVATE int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){
71404   char aff = comparisonAffinity(pExpr);
71405   switch( aff ){
71406     case SQLITE_AFF_NONE:
71407       return 1;
71408     case SQLITE_AFF_TEXT:
71409       return idx_affinity==SQLITE_AFF_TEXT;
71410     default:
71411       return sqlite3IsNumericAffinity(idx_affinity);
71412   }
71413 }
71414 
71415 /*
71416 ** Return the P5 value that should be used for a binary comparison
71417 ** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2.
71418 */
71419 static u8 binaryCompareP5(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){
71420   u8 aff = (char)sqlite3ExprAffinity(pExpr2);
71421   aff = (u8)sqlite3CompareAffinity(pExpr1, aff) | (u8)jumpIfNull;
71422   return aff;
71423 }
71424 
71425 /*
71426 ** Return a pointer to the collation sequence that should be used by
71427 ** a binary comparison operator comparing pLeft and pRight.
71428 **
71429 ** If the left hand expression has a collating sequence type, then it is
71430 ** used. Otherwise the collation sequence for the right hand expression
71431 ** is used, or the default (BINARY) if neither expression has a collating
71432 ** type.
71433 **
71434 ** Argument pRight (but not pLeft) may be a null pointer. In this case,
71435 ** it is not considered.
71436 */
71437 SQLITE_PRIVATE CollSeq *sqlite3BinaryCompareCollSeq(
71438   Parse *pParse,
71439   Expr *pLeft,
71440   Expr *pRight
71441 ){
71442   CollSeq *pColl;
71443   assert( pLeft );
71444   if( pLeft->flags & EP_ExpCollate ){
71445     assert( pLeft->pColl );
71446     pColl = pLeft->pColl;
71447   }else if( pRight && pRight->flags & EP_ExpCollate ){
71448     assert( pRight->pColl );
71449     pColl = pRight->pColl;
71450   }else{
71451     pColl = sqlite3ExprCollSeq(pParse, pLeft);
71452     if( !pColl ){
71453       pColl = sqlite3ExprCollSeq(pParse, pRight);
71454     }
71455   }
71456   return pColl;
71457 }
71458 
71459 /*
71460 ** Generate code for a comparison operator.
71461 */
71462 static int codeCompare(
71463   Parse *pParse,    /* The parsing (and code generating) context */
71464   Expr *pLeft,      /* The left operand */
71465   Expr *pRight,     /* The right operand */
71466   int opcode,       /* The comparison opcode */
71467   int in1, int in2, /* Register holding operands */
71468   int dest,         /* Jump here if true.  */
71469   int jumpIfNull    /* If true, jump if either operand is NULL */
71470 ){
71471   int p5;
71472   int addr;
71473   CollSeq *p4;
71474 
71475   p4 = sqlite3BinaryCompareCollSeq(pParse, pLeft, pRight);
71476   p5 = binaryCompareP5(pLeft, pRight, jumpIfNull);
71477   addr = sqlite3VdbeAddOp4(pParse->pVdbe, opcode, in2, dest, in1,
71478                            (void*)p4, P4_COLLSEQ);
71479   sqlite3VdbeChangeP5(pParse->pVdbe, (u8)p5);
71480   return addr;
71481 }
71482 
71483 #if SQLITE_MAX_EXPR_DEPTH>0
71484 /*
71485 ** Check that argument nHeight is less than or equal to the maximum
71486 ** expression depth allowed. If it is not, leave an error message in
71487 ** pParse.
71488 */
71489 SQLITE_PRIVATE int sqlite3ExprCheckHeight(Parse *pParse, int nHeight){
71490   int rc = SQLITE_OK;
71491   int mxHeight = pParse->db->aLimit[SQLITE_LIMIT_EXPR_DEPTH];
71492   if( nHeight>mxHeight ){
71493     sqlite3ErrorMsg(pParse,
71494        "Expression tree is too large (maximum depth %d)", mxHeight
71495     );
71496     rc = SQLITE_ERROR;
71497   }
71498   return rc;
71499 }
71500 
71501 /* The following three functions, heightOfExpr(), heightOfExprList()
71502 ** and heightOfSelect(), are used to determine the maximum height
71503 ** of any expression tree referenced by the structure passed as the
71504 ** first argument.
71505 **
71506 ** If this maximum height is greater than the current value pointed
71507 ** to by pnHeight, the second parameter, then set *pnHeight to that
71508 ** value.
71509 */
71510 static void heightOfExpr(Expr *p, int *pnHeight){
71511   if( p ){
71512     if( p->nHeight>*pnHeight ){
71513       *pnHeight = p->nHeight;
71514     }
71515   }
71516 }
71517 static void heightOfExprList(ExprList *p, int *pnHeight){
71518   if( p ){
71519     int i;
71520     for(i=0; i<p->nExpr; i++){
71521       heightOfExpr(p->a[i].pExpr, pnHeight);
71522     }
71523   }
71524 }
71525 static void heightOfSelect(Select *p, int *pnHeight){
71526   if( p ){
71527     heightOfExpr(p->pWhere, pnHeight);
71528     heightOfExpr(p->pHaving, pnHeight);
71529     heightOfExpr(p->pLimit, pnHeight);
71530     heightOfExpr(p->pOffset, pnHeight);
71531     heightOfExprList(p->pEList, pnHeight);
71532     heightOfExprList(p->pGroupBy, pnHeight);
71533     heightOfExprList(p->pOrderBy, pnHeight);
71534     heightOfSelect(p->pPrior, pnHeight);
71535   }
71536 }
71537 
71538 /*
71539 ** Set the Expr.nHeight variable in the structure passed as an
71540 ** argument. An expression with no children, Expr.pList or
71541 ** Expr.pSelect member has a height of 1. Any other expression
71542 ** has a height equal to the maximum height of any other
71543 ** referenced Expr plus one.
71544 */
71545 static void exprSetHeight(Expr *p){
71546   int nHeight = 0;
71547   heightOfExpr(p->pLeft, &nHeight);
71548   heightOfExpr(p->pRight, &nHeight);
71549   if( ExprHasProperty(p, EP_xIsSelect) ){
71550     heightOfSelect(p->x.pSelect, &nHeight);
71551   }else{
71552     heightOfExprList(p->x.pList, &nHeight);
71553   }
71554   p->nHeight = nHeight + 1;
71555 }
71556 
71557 /*
71558 ** Set the Expr.nHeight variable using the exprSetHeight() function. If
71559 ** the height is greater than the maximum allowed expression depth,
71560 ** leave an error in pParse.
71561 */
71562 SQLITE_PRIVATE void sqlite3ExprSetHeight(Parse *pParse, Expr *p){
71563   exprSetHeight(p);
71564   sqlite3ExprCheckHeight(pParse, p->nHeight);
71565 }
71566 
71567 /*
71568 ** Return the maximum height of any expression tree referenced
71569 ** by the select statement passed as an argument.
71570 */
71571 SQLITE_PRIVATE int sqlite3SelectExprHeight(Select *p){
71572   int nHeight = 0;
71573   heightOfSelect(p, &nHeight);
71574   return nHeight;
71575 }
71576 #else
71577   #define exprSetHeight(y)
71578 #endif /* SQLITE_MAX_EXPR_DEPTH>0 */
71579 
71580 /*
71581 ** This routine is the core allocator for Expr nodes.
71582 **
71583 ** Construct a new expression node and return a pointer to it.  Memory
71584 ** for this node and for the pToken argument is a single allocation
71585 ** obtained from sqlite3DbMalloc().  The calling function
71586 ** is responsible for making sure the node eventually gets freed.
71587 **
71588 ** If dequote is true, then the token (if it exists) is dequoted.
71589 ** If dequote is false, no dequoting is performance.  The deQuote
71590 ** parameter is ignored if pToken is NULL or if the token does not
71591 ** appear to be quoted.  If the quotes were of the form "..." (double-quotes)
71592 ** then the EP_DblQuoted flag is set on the expression node.
71593 **
71594 ** Special case:  If op==TK_INTEGER and pToken points to a string that
71595 ** can be translated into a 32-bit integer, then the token is not
71596 ** stored in u.zToken.  Instead, the integer values is written
71597 ** into u.iValue and the EP_IntValue flag is set.  No extra storage
71598 ** is allocated to hold the integer text and the dequote flag is ignored.
71599 */
71600 SQLITE_PRIVATE Expr *sqlite3ExprAlloc(
71601   sqlite3 *db,            /* Handle for sqlite3DbMallocZero() (may be null) */
71602   int op,                 /* Expression opcode */
71603   const Token *pToken,    /* Token argument.  Might be NULL */
71604   int dequote             /* True to dequote */
71605 ){
71606   Expr *pNew;
71607   int nExtra = 0;
71608   int iValue = 0;
71609 
71610   if( pToken ){
71611     if( op!=TK_INTEGER || pToken->z==0
71612           || sqlite3GetInt32(pToken->z, &iValue)==0 ){
71613       nExtra = pToken->n+1;
71614       assert( iValue>=0 );
71615     }
71616   }
71617   pNew = sqlite3DbMallocZero(db, sizeof(Expr)+nExtra);
71618   if( pNew ){
71619     pNew->op = (u8)op;
71620     pNew->iAgg = -1;
71621     if( pToken ){
71622       if( nExtra==0 ){
71623         pNew->flags |= EP_IntValue;
71624         pNew->u.iValue = iValue;
71625       }else{
71626         int c;
71627         pNew->u.zToken = (char*)&pNew[1];
71628         memcpy(pNew->u.zToken, pToken->z, pToken->n);
71629         pNew->u.zToken[pToken->n] = 0;
71630         if( dequote && nExtra>=3
71631              && ((c = pToken->z[0])=='\'' || c=='"' || c=='[' || c=='`') ){
71632           sqlite3Dequote(pNew->u.zToken);
71633           if( c=='"' ) pNew->flags |= EP_DblQuoted;
71634         }
71635       }
71636     }
71637 #if SQLITE_MAX_EXPR_DEPTH>0
71638     pNew->nHeight = 1;
71639 #endif
71640   }
71641   return pNew;
71642 }
71643 
71644 /*
71645 ** Allocate a new expression node from a zero-terminated token that has
71646 ** already been dequoted.
71647 */
71648 SQLITE_PRIVATE Expr *sqlite3Expr(
71649   sqlite3 *db,            /* Handle for sqlite3DbMallocZero() (may be null) */
71650   int op,                 /* Expression opcode */
71651   const char *zToken      /* Token argument.  Might be NULL */
71652 ){
71653   Token x;
71654   x.z = zToken;
71655   x.n = zToken ? sqlite3Strlen30(zToken) : 0;
71656   return sqlite3ExprAlloc(db, op, &x, 0);
71657 }
71658 
71659 /*
71660 ** Attach subtrees pLeft and pRight to the Expr node pRoot.
71661 **
71662 ** If pRoot==NULL that means that a memory allocation error has occurred.
71663 ** In that case, delete the subtrees pLeft and pRight.
71664 */
71665 SQLITE_PRIVATE void sqlite3ExprAttachSubtrees(
71666   sqlite3 *db,
71667   Expr *pRoot,
71668   Expr *pLeft,
71669   Expr *pRight
71670 ){
71671   if( pRoot==0 ){
71672     assert( db->mallocFailed );
71673     sqlite3ExprDelete(db, pLeft);
71674     sqlite3ExprDelete(db, pRight);
71675   }else{
71676     if( pRight ){
71677       pRoot->pRight = pRight;
71678       if( pRight->flags & EP_ExpCollate ){
71679         pRoot->flags |= EP_ExpCollate;
71680         pRoot->pColl = pRight->pColl;
71681       }
71682     }
71683     if( pLeft ){
71684       pRoot->pLeft = pLeft;
71685       if( pLeft->flags & EP_ExpCollate ){
71686         pRoot->flags |= EP_ExpCollate;
71687         pRoot->pColl = pLeft->pColl;
71688       }
71689     }
71690     exprSetHeight(pRoot);
71691   }
71692 }
71693 
71694 /*
71695 ** Allocate a Expr node which joins as many as two subtrees.
71696 **
71697 ** One or both of the subtrees can be NULL.  Return a pointer to the new
71698 ** Expr node.  Or, if an OOM error occurs, set pParse->db->mallocFailed,
71699 ** free the subtrees and return NULL.
71700 */
71701 SQLITE_PRIVATE Expr *sqlite3PExpr(
71702   Parse *pParse,          /* Parsing context */
71703   int op,                 /* Expression opcode */
71704   Expr *pLeft,            /* Left operand */
71705   Expr *pRight,           /* Right operand */
71706   const Token *pToken     /* Argument token */
71707 ){
71708   Expr *p = sqlite3ExprAlloc(pParse->db, op, pToken, 1);
71709   sqlite3ExprAttachSubtrees(pParse->db, p, pLeft, pRight);
71710   if( p ) {
71711     sqlite3ExprCheckHeight(pParse, p->nHeight);
71712   }
71713   return p;
71714 }
71715 
71716 /*
71717 ** Join two expressions using an AND operator.  If either expression is
71718 ** NULL, then just return the other expression.
71719 */
71720 SQLITE_PRIVATE Expr *sqlite3ExprAnd(sqlite3 *db, Expr *pLeft, Expr *pRight){
71721   if( pLeft==0 ){
71722     return pRight;
71723   }else if( pRight==0 ){
71724     return pLeft;
71725   }else{
71726     Expr *pNew = sqlite3ExprAlloc(db, TK_AND, 0, 0);
71727     sqlite3ExprAttachSubtrees(db, pNew, pLeft, pRight);
71728     return pNew;
71729   }
71730 }
71731 
71732 /*
71733 ** Construct a new expression node for a function with multiple
71734 ** arguments.
71735 */
71736 SQLITE_PRIVATE Expr *sqlite3ExprFunction(Parse *pParse, ExprList *pList, Token *pToken){
71737   Expr *pNew;
71738   sqlite3 *db = pParse->db;
71739   assert( pToken );
71740   pNew = sqlite3ExprAlloc(db, TK_FUNCTION, pToken, 1);
71741   if( pNew==0 ){
71742     sqlite3ExprListDelete(db, pList); /* Avoid memory leak when malloc fails */
71743     return 0;
71744   }
71745   pNew->x.pList = pList;
71746   assert( !ExprHasProperty(pNew, EP_xIsSelect) );
71747   sqlite3ExprSetHeight(pParse, pNew);
71748   return pNew;
71749 }
71750 
71751 /*
71752 ** Assign a variable number to an expression that encodes a wildcard
71753 ** in the original SQL statement.
71754 **
71755 ** Wildcards consisting of a single "?" are assigned the next sequential
71756 ** variable number.
71757 **
71758 ** Wildcards of the form "?nnn" are assigned the number "nnn".  We make
71759 ** sure "nnn" is not too be to avoid a denial of service attack when
71760 ** the SQL statement comes from an external source.
71761 **
71762 ** Wildcards of the form ":aaa", "@aaa", or "$aaa" are assigned the same number
71763 ** as the previous instance of the same wildcard.  Or if this is the first
71764 ** instance of the wildcard, the next sequenial variable number is
71765 ** assigned.
71766 */
71767 SQLITE_PRIVATE void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){
71768   sqlite3 *db = pParse->db;
71769   const char *z;
71770 
71771   if( pExpr==0 ) return;
71772   assert( !ExprHasAnyProperty(pExpr, EP_IntValue|EP_Reduced|EP_TokenOnly) );
71773   z = pExpr->u.zToken;
71774   assert( z!=0 );
71775   assert( z[0]!=0 );
71776   if( z[1]==0 ){
71777     /* Wildcard of the form "?".  Assign the next variable number */
71778     assert( z[0]=='?' );
71779     pExpr->iColumn = (ynVar)(++pParse->nVar);
71780   }else{
71781     ynVar x = 0;
71782     u32 n = sqlite3Strlen30(z);
71783     if( z[0]=='?' ){
71784       /* Wildcard of the form "?nnn".  Convert "nnn" to an integer and
71785       ** use it as the variable number */
71786       i64 i;
71787       int bOk = 0==sqlite3Atoi64(&z[1], &i, n-1, SQLITE_UTF8);
71788       pExpr->iColumn = x = (ynVar)i;
71789       testcase( i==0 );
71790       testcase( i==1 );
71791       testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]-1 );
71792       testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] );
71793       if( bOk==0 || i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
71794         sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
71795             db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]);
71796         x = 0;
71797       }
71798       if( i>pParse->nVar ){
71799         pParse->nVar = (int)i;
71800       }
71801     }else{
71802       /* Wildcards like ":aaa", "$aaa" or "@aaa".  Reuse the same variable
71803       ** number as the prior appearance of the same name, or if the name
71804       ** has never appeared before, reuse the same variable number
71805       */
71806       ynVar i;
71807       for(i=0; i<pParse->nzVar; i++){
71808         if( pParse->azVar[i] && memcmp(pParse->azVar[i],z,n+1)==0 ){
71809           pExpr->iColumn = x = (ynVar)i+1;
71810           break;
71811         }
71812       }
71813       if( x==0 ) x = pExpr->iColumn = (ynVar)(++pParse->nVar);
71814     }
71815     if( x>0 ){
71816       if( x>pParse->nzVar ){
71817         char **a;
71818         a = sqlite3DbRealloc(db, pParse->azVar, x*sizeof(a[0]));
71819         if( a==0 ) return;  /* Error reported through db->mallocFailed */
71820         pParse->azVar = a;
71821         memset(&a[pParse->nzVar], 0, (x-pParse->nzVar)*sizeof(a[0]));
71822         pParse->nzVar = x;
71823       }
71824       if( z[0]!='?' || pParse->azVar[x-1]==0 ){
71825         sqlite3DbFree(db, pParse->azVar[x-1]);
71826         pParse->azVar[x-1] = sqlite3DbStrNDup(db, z, n);
71827       }
71828     }
71829   }
71830   if( !pParse->nErr && pParse->nVar>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
71831     sqlite3ErrorMsg(pParse, "too many SQL variables");
71832   }
71833 }
71834 
71835 /*
71836 ** Recursively delete an expression tree.
71837 */
71838 SQLITE_PRIVATE void sqlite3ExprDelete(sqlite3 *db, Expr *p){
71839   if( p==0 ) return;
71840   /* Sanity check: Assert that the IntValue is non-negative if it exists */
71841   assert( !ExprHasProperty(p, EP_IntValue) || p->u.iValue>=0 );
71842   if( !ExprHasAnyProperty(p, EP_TokenOnly) ){
71843     sqlite3ExprDelete(db, p->pLeft);
71844     sqlite3ExprDelete(db, p->pRight);
71845     if( !ExprHasProperty(p, EP_Reduced) && (p->flags2 & EP2_MallocedToken)!=0 ){
71846       sqlite3DbFree(db, p->u.zToken);
71847     }
71848     if( ExprHasProperty(p, EP_xIsSelect) ){
71849       sqlite3SelectDelete(db, p->x.pSelect);
71850     }else{
71851       sqlite3ExprListDelete(db, p->x.pList);
71852     }
71853   }
71854   if( !ExprHasProperty(p, EP_Static) ){
71855     sqlite3DbFree(db, p);
71856   }
71857 }
71858 
71859 /*
71860 ** Return the number of bytes allocated for the expression structure
71861 ** passed as the first argument. This is always one of EXPR_FULLSIZE,
71862 ** EXPR_REDUCEDSIZE or EXPR_TOKENONLYSIZE.
71863 */
71864 static int exprStructSize(Expr *p){
71865   if( ExprHasProperty(p, EP_TokenOnly) ) return EXPR_TOKENONLYSIZE;
71866   if( ExprHasProperty(p, EP_Reduced) ) return EXPR_REDUCEDSIZE;
71867   return EXPR_FULLSIZE;
71868 }
71869 
71870 /*
71871 ** The dupedExpr*Size() routines each return the number of bytes required
71872 ** to store a copy of an expression or expression tree.  They differ in
71873 ** how much of the tree is measured.
71874 **
71875 **     dupedExprStructSize()     Size of only the Expr structure
71876 **     dupedExprNodeSize()       Size of Expr + space for token
71877 **     dupedExprSize()           Expr + token + subtree components
71878 **
71879 ***************************************************************************
71880 **
71881 ** The dupedExprStructSize() function returns two values OR-ed together:
71882 ** (1) the space required for a copy of the Expr structure only and
71883 ** (2) the EP_xxx flags that indicate what the structure size should be.
71884 ** The return values is always one of:
71885 **
71886 **      EXPR_FULLSIZE
71887 **      EXPR_REDUCEDSIZE   | EP_Reduced
71888 **      EXPR_TOKENONLYSIZE | EP_TokenOnly
71889 **
71890 ** The size of the structure can be found by masking the return value
71891 ** of this routine with 0xfff.  The flags can be found by masking the
71892 ** return value with EP_Reduced|EP_TokenOnly.
71893 **
71894 ** Note that with flags==EXPRDUP_REDUCE, this routines works on full-size
71895 ** (unreduced) Expr objects as they or originally constructed by the parser.
71896 ** During expression analysis, extra information is computed and moved into
71897 ** later parts of teh Expr object and that extra information might get chopped
71898 ** off if the expression is reduced.  Note also that it does not work to
71899 ** make a EXPRDUP_REDUCE copy of a reduced expression.  It is only legal
71900 ** to reduce a pristine expression tree from the parser.  The implementation
71901 ** of dupedExprStructSize() contain multiple assert() statements that attempt
71902 ** to enforce this constraint.
71903 */
71904 static int dupedExprStructSize(Expr *p, int flags){
71905   int nSize;
71906   assert( flags==EXPRDUP_REDUCE || flags==0 ); /* Only one flag value allowed */
71907   if( 0==(flags&EXPRDUP_REDUCE) ){
71908     nSize = EXPR_FULLSIZE;
71909   }else{
71910     assert( !ExprHasAnyProperty(p, EP_TokenOnly|EP_Reduced) );
71911     assert( !ExprHasProperty(p, EP_FromJoin) );
71912     assert( (p->flags2 & EP2_MallocedToken)==0 );
71913     assert( (p->flags2 & EP2_Irreducible)==0 );
71914     if( p->pLeft || p->pRight || p->pColl || p->x.pList ){
71915       nSize = EXPR_REDUCEDSIZE | EP_Reduced;
71916     }else{
71917       nSize = EXPR_TOKENONLYSIZE | EP_TokenOnly;
71918     }
71919   }
71920   return nSize;
71921 }
71922 
71923 /*
71924 ** This function returns the space in bytes required to store the copy
71925 ** of the Expr structure and a copy of the Expr.u.zToken string (if that
71926 ** string is defined.)
71927 */
71928 static int dupedExprNodeSize(Expr *p, int flags){
71929   int nByte = dupedExprStructSize(p, flags) & 0xfff;
71930   if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
71931     nByte += sqlite3Strlen30(p->u.zToken)+1;
71932   }
71933   return ROUND8(nByte);
71934 }
71935 
71936 /*
71937 ** Return the number of bytes required to create a duplicate of the
71938 ** expression passed as the first argument. The second argument is a
71939 ** mask containing EXPRDUP_XXX flags.
71940 **
71941 ** The value returned includes space to create a copy of the Expr struct
71942 ** itself and the buffer referred to by Expr.u.zToken, if any.
71943 **
71944 ** If the EXPRDUP_REDUCE flag is set, then the return value includes
71945 ** space to duplicate all Expr nodes in the tree formed by Expr.pLeft
71946 ** and Expr.pRight variables (but not for any structures pointed to or
71947 ** descended from the Expr.x.pList or Expr.x.pSelect variables).
71948 */
71949 static int dupedExprSize(Expr *p, int flags){
71950   int nByte = 0;
71951   if( p ){
71952     nByte = dupedExprNodeSize(p, flags);
71953     if( flags&EXPRDUP_REDUCE ){
71954       nByte += dupedExprSize(p->pLeft, flags) + dupedExprSize(p->pRight, flags);
71955     }
71956   }
71957   return nByte;
71958 }
71959 
71960 /*
71961 ** This function is similar to sqlite3ExprDup(), except that if pzBuffer
71962 ** is not NULL then *pzBuffer is assumed to point to a buffer large enough
71963 ** to store the copy of expression p, the copies of p->u.zToken
71964 ** (if applicable), and the copies of the p->pLeft and p->pRight expressions,
71965 ** if any. Before returning, *pzBuffer is set to the first byte passed the
71966 ** portion of the buffer copied into by this function.
71967 */
71968 static Expr *exprDup(sqlite3 *db, Expr *p, int flags, u8 **pzBuffer){
71969   Expr *pNew = 0;                      /* Value to return */
71970   if( p ){
71971     const int isReduced = (flags&EXPRDUP_REDUCE);
71972     u8 *zAlloc;
71973     u32 staticFlag = 0;
71974 
71975     assert( pzBuffer==0 || isReduced );
71976 
71977     /* Figure out where to write the new Expr structure. */
71978     if( pzBuffer ){
71979       zAlloc = *pzBuffer;
71980       staticFlag = EP_Static;
71981     }else{
71982       zAlloc = sqlite3DbMallocRaw(db, dupedExprSize(p, flags));
71983     }
71984     pNew = (Expr *)zAlloc;
71985 
71986     if( pNew ){
71987       /* Set nNewSize to the size allocated for the structure pointed to
71988       ** by pNew. This is either EXPR_FULLSIZE, EXPR_REDUCEDSIZE or
71989       ** EXPR_TOKENONLYSIZE. nToken is set to the number of bytes consumed
71990       ** by the copy of the p->u.zToken string (if any).
71991       */
71992       const unsigned nStructSize = dupedExprStructSize(p, flags);
71993       const int nNewSize = nStructSize & 0xfff;
71994       int nToken;
71995       if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
71996         nToken = sqlite3Strlen30(p->u.zToken) + 1;
71997       }else{
71998         nToken = 0;
71999       }
72000       if( isReduced ){
72001         assert( ExprHasProperty(p, EP_Reduced)==0 );
72002         memcpy(zAlloc, p, nNewSize);
72003       }else{
72004         int nSize = exprStructSize(p);
72005         memcpy(zAlloc, p, nSize);
72006         memset(&zAlloc[nSize], 0, EXPR_FULLSIZE-nSize);
72007       }
72008 
72009       /* Set the EP_Reduced, EP_TokenOnly, and EP_Static flags appropriately. */
72010       pNew->flags &= ~(EP_Reduced|EP_TokenOnly|EP_Static);
72011       pNew->flags |= nStructSize & (EP_Reduced|EP_TokenOnly);
72012       pNew->flags |= staticFlag;
72013 
72014       /* Copy the p->u.zToken string, if any. */
72015       if( nToken ){
72016         char *zToken = pNew->u.zToken = (char*)&zAlloc[nNewSize];
72017         memcpy(zToken, p->u.zToken, nToken);
72018       }
72019 
72020       if( 0==((p->flags|pNew->flags) & EP_TokenOnly) ){
72021         /* Fill in the pNew->x.pSelect or pNew->x.pList member. */
72022         if( ExprHasProperty(p, EP_xIsSelect) ){
72023           pNew->x.pSelect = sqlite3SelectDup(db, p->x.pSelect, isReduced);
72024         }else{
72025           pNew->x.pList = sqlite3ExprListDup(db, p->x.pList, isReduced);
72026         }
72027       }
72028 
72029       /* Fill in pNew->pLeft and pNew->pRight. */
72030       if( ExprHasAnyProperty(pNew, EP_Reduced|EP_TokenOnly) ){
72031         zAlloc += dupedExprNodeSize(p, flags);
72032         if( ExprHasProperty(pNew, EP_Reduced) ){
72033           pNew->pLeft = exprDup(db, p->pLeft, EXPRDUP_REDUCE, &zAlloc);
72034           pNew->pRight = exprDup(db, p->pRight, EXPRDUP_REDUCE, &zAlloc);
72035         }
72036         if( pzBuffer ){
72037           *pzBuffer = zAlloc;
72038         }
72039       }else{
72040         pNew->flags2 = 0;
72041         if( !ExprHasAnyProperty(p, EP_TokenOnly) ){
72042           pNew->pLeft = sqlite3ExprDup(db, p->pLeft, 0);
72043           pNew->pRight = sqlite3ExprDup(db, p->pRight, 0);
72044         }
72045       }
72046 
72047     }
72048   }
72049   return pNew;
72050 }
72051 
72052 /*
72053 ** The following group of routines make deep copies of expressions,
72054 ** expression lists, ID lists, and select statements.  The copies can
72055 ** be deleted (by being passed to their respective ...Delete() routines)
72056 ** without effecting the originals.
72057 **
72058 ** The expression list, ID, and source lists return by sqlite3ExprListDup(),
72059 ** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded
72060 ** by subsequent calls to sqlite*ListAppend() routines.
72061 **
72062 ** Any tables that the SrcList might point to are not duplicated.
72063 **
72064 ** The flags parameter contains a combination of the EXPRDUP_XXX flags.
72065 ** If the EXPRDUP_REDUCE flag is set, then the structure returned is a
72066 ** truncated version of the usual Expr structure that will be stored as
72067 ** part of the in-memory representation of the database schema.
72068 */
72069 SQLITE_PRIVATE Expr *sqlite3ExprDup(sqlite3 *db, Expr *p, int flags){
72070   return exprDup(db, p, flags, 0);
72071 }
72072 SQLITE_PRIVATE ExprList *sqlite3ExprListDup(sqlite3 *db, ExprList *p, int flags){
72073   ExprList *pNew;
72074   struct ExprList_item *pItem, *pOldItem;
72075   int i;
72076   if( p==0 ) return 0;
72077   pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) );
72078   if( pNew==0 ) return 0;
72079   pNew->iECursor = 0;
72080   pNew->nExpr = pNew->nAlloc = p->nExpr;
72081   pNew->a = pItem = sqlite3DbMallocRaw(db,  p->nExpr*sizeof(p->a[0]) );
72082   if( pItem==0 ){
72083     sqlite3DbFree(db, pNew);
72084     return 0;
72085   }
72086   pOldItem = p->a;
72087   for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
72088     Expr *pOldExpr = pOldItem->pExpr;
72089     pItem->pExpr = sqlite3ExprDup(db, pOldExpr, flags);
72090     pItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
72091     pItem->zSpan = sqlite3DbStrDup(db, pOldItem->zSpan);
72092     pItem->sortOrder = pOldItem->sortOrder;
72093     pItem->done = 0;
72094     pItem->iCol = pOldItem->iCol;
72095     pItem->iAlias = pOldItem->iAlias;
72096   }
72097   return pNew;
72098 }
72099 
72100 /*
72101 ** If cursors, triggers, views and subqueries are all omitted from
72102 ** the build, then none of the following routines, except for
72103 ** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes
72104 ** called with a NULL argument.
72105 */
72106 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \
72107  || !defined(SQLITE_OMIT_SUBQUERY)
72108 SQLITE_PRIVATE SrcList *sqlite3SrcListDup(sqlite3 *db, SrcList *p, int flags){
72109   SrcList *pNew;
72110   int i;
72111   int nByte;
72112   if( p==0 ) return 0;
72113   nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
72114   pNew = sqlite3DbMallocRaw(db, nByte );
72115   if( pNew==0 ) return 0;
72116   pNew->nSrc = pNew->nAlloc = p->nSrc;
72117   for(i=0; i<p->nSrc; i++){
72118     struct SrcList_item *pNewItem = &pNew->a[i];
72119     struct SrcList_item *pOldItem = &p->a[i];
72120     Table *pTab;
72121     pNewItem->zDatabase = sqlite3DbStrDup(db, pOldItem->zDatabase);
72122     pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
72123     pNewItem->zAlias = sqlite3DbStrDup(db, pOldItem->zAlias);
72124     pNewItem->jointype = pOldItem->jointype;
72125     pNewItem->iCursor = pOldItem->iCursor;
72126     pNewItem->isPopulated = pOldItem->isPopulated;
72127     pNewItem->zIndex = sqlite3DbStrDup(db, pOldItem->zIndex);
72128     pNewItem->notIndexed = pOldItem->notIndexed;
72129     pNewItem->pIndex = pOldItem->pIndex;
72130     pTab = pNewItem->pTab = pOldItem->pTab;
72131     if( pTab ){
72132       pTab->nRef++;
72133     }
72134     pNewItem->pSelect = sqlite3SelectDup(db, pOldItem->pSelect, flags);
72135     pNewItem->pOn = sqlite3ExprDup(db, pOldItem->pOn, flags);
72136     pNewItem->pUsing = sqlite3IdListDup(db, pOldItem->pUsing);
72137     pNewItem->colUsed = pOldItem->colUsed;
72138   }
72139   return pNew;
72140 }
72141 SQLITE_PRIVATE IdList *sqlite3IdListDup(sqlite3 *db, IdList *p){
72142   IdList *pNew;
72143   int i;
72144   if( p==0 ) return 0;
72145   pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) );
72146   if( pNew==0 ) return 0;
72147   pNew->nId = pNew->nAlloc = p->nId;
72148   pNew->a = sqlite3DbMallocRaw(db, p->nId*sizeof(p->a[0]) );
72149   if( pNew->a==0 ){
72150     sqlite3DbFree(db, pNew);
72151     return 0;
72152   }
72153   for(i=0; i<p->nId; i++){
72154     struct IdList_item *pNewItem = &pNew->a[i];
72155     struct IdList_item *pOldItem = &p->a[i];
72156     pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
72157     pNewItem->idx = pOldItem->idx;
72158   }
72159   return pNew;
72160 }
72161 SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){
72162   Select *pNew;
72163   if( p==0 ) return 0;
72164   pNew = sqlite3DbMallocRaw(db, sizeof(*p) );
72165   if( pNew==0 ) return 0;
72166   pNew->pEList = sqlite3ExprListDup(db, p->pEList, flags);
72167   pNew->pSrc = sqlite3SrcListDup(db, p->pSrc, flags);
72168   pNew->pWhere = sqlite3ExprDup(db, p->pWhere, flags);
72169   pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy, flags);
72170   pNew->pHaving = sqlite3ExprDup(db, p->pHaving, flags);
72171   pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, flags);
72172   pNew->op = p->op;
72173   pNew->pPrior = sqlite3SelectDup(db, p->pPrior, flags);
72174   pNew->pLimit = sqlite3ExprDup(db, p->pLimit, flags);
72175   pNew->pOffset = sqlite3ExprDup(db, p->pOffset, flags);
72176   pNew->iLimit = 0;
72177   pNew->iOffset = 0;
72178   pNew->selFlags = p->selFlags & ~SF_UsesEphemeral;
72179   pNew->pRightmost = 0;
72180   pNew->addrOpenEphm[0] = -1;
72181   pNew->addrOpenEphm[1] = -1;
72182   pNew->addrOpenEphm[2] = -1;
72183   return pNew;
72184 }
72185 #else
72186 SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){
72187   assert( p==0 );
72188   return 0;
72189 }
72190 #endif
72191 
72192 
72193 /*
72194 ** Add a new element to the end of an expression list.  If pList is
72195 ** initially NULL, then create a new expression list.
72196 **
72197 ** If a memory allocation error occurs, the entire list is freed and
72198 ** NULL is returned.  If non-NULL is returned, then it is guaranteed
72199 ** that the new entry was successfully appended.
72200 */
72201 SQLITE_PRIVATE ExprList *sqlite3ExprListAppend(
72202   Parse *pParse,          /* Parsing context */
72203   ExprList *pList,        /* List to which to append. Might be NULL */
72204   Expr *pExpr             /* Expression to be appended. Might be NULL */
72205 ){
72206   sqlite3 *db = pParse->db;
72207   if( pList==0 ){
72208     pList = sqlite3DbMallocZero(db, sizeof(ExprList) );
72209     if( pList==0 ){
72210       goto no_mem;
72211     }
72212     assert( pList->nAlloc==0 );
72213   }
72214   if( pList->nAlloc<=pList->nExpr ){
72215     struct ExprList_item *a;
72216     int n = pList->nAlloc*2 + 4;
72217     a = sqlite3DbRealloc(db, pList->a, n*sizeof(pList->a[0]));
72218     if( a==0 ){
72219       goto no_mem;
72220     }
72221     pList->a = a;
72222     pList->nAlloc = sqlite3DbMallocSize(db, a)/sizeof(a[0]);
72223   }
72224   assert( pList->a!=0 );
72225   if( 1 ){
72226     struct ExprList_item *pItem = &pList->a[pList->nExpr++];
72227     memset(pItem, 0, sizeof(*pItem));
72228     pItem->pExpr = pExpr;
72229   }
72230   return pList;
72231 
72232 no_mem:
72233   /* Avoid leaking memory if malloc has failed. */
72234   sqlite3ExprDelete(db, pExpr);
72235   sqlite3ExprListDelete(db, pList);
72236   return 0;
72237 }
72238 
72239 /*
72240 ** Set the ExprList.a[].zName element of the most recently added item
72241 ** on the expression list.
72242 **
72243 ** pList might be NULL following an OOM error.  But pName should never be
72244 ** NULL.  If a memory allocation fails, the pParse->db->mallocFailed flag
72245 ** is set.
72246 */
72247 SQLITE_PRIVATE void sqlite3ExprListSetName(
72248   Parse *pParse,          /* Parsing context */
72249   ExprList *pList,        /* List to which to add the span. */
72250   Token *pName,           /* Name to be added */
72251   int dequote             /* True to cause the name to be dequoted */
72252 ){
72253   assert( pList!=0 || pParse->db->mallocFailed!=0 );
72254   if( pList ){
72255     struct ExprList_item *pItem;
72256     assert( pList->nExpr>0 );
72257     pItem = &pList->a[pList->nExpr-1];
72258     assert( pItem->zName==0 );
72259     pItem->zName = sqlite3DbStrNDup(pParse->db, pName->z, pName->n);
72260     if( dequote && pItem->zName ) sqlite3Dequote(pItem->zName);
72261   }
72262 }
72263 
72264 /*
72265 ** Set the ExprList.a[].zSpan element of the most recently added item
72266 ** on the expression list.
72267 **
72268 ** pList might be NULL following an OOM error.  But pSpan should never be
72269 ** NULL.  If a memory allocation fails, the pParse->db->mallocFailed flag
72270 ** is set.
72271 */
72272 SQLITE_PRIVATE void sqlite3ExprListSetSpan(
72273   Parse *pParse,          /* Parsing context */
72274   ExprList *pList,        /* List to which to add the span. */
72275   ExprSpan *pSpan         /* The span to be added */
72276 ){
72277   sqlite3 *db = pParse->db;
72278   assert( pList!=0 || db->mallocFailed!=0 );
72279   if( pList ){
72280     struct ExprList_item *pItem = &pList->a[pList->nExpr-1];
72281     assert( pList->nExpr>0 );
72282     assert( db->mallocFailed || pItem->pExpr==pSpan->pExpr );
72283     sqlite3DbFree(db, pItem->zSpan);
72284     pItem->zSpan = sqlite3DbStrNDup(db, (char*)pSpan->zStart,
72285                                     (int)(pSpan->zEnd - pSpan->zStart));
72286   }
72287 }
72288 
72289 /*
72290 ** If the expression list pEList contains more than iLimit elements,
72291 ** leave an error message in pParse.
72292 */
72293 SQLITE_PRIVATE void sqlite3ExprListCheckLength(
72294   Parse *pParse,
72295   ExprList *pEList,
72296   const char *zObject
72297 ){
72298   int mx = pParse->db->aLimit[SQLITE_LIMIT_COLUMN];
72299   testcase( pEList && pEList->nExpr==mx );
72300   testcase( pEList && pEList->nExpr==mx+1 );
72301   if( pEList && pEList->nExpr>mx ){
72302     sqlite3ErrorMsg(pParse, "too many columns in %s", zObject);
72303   }
72304 }
72305 
72306 /*
72307 ** Delete an entire expression list.
72308 */
72309 SQLITE_PRIVATE void sqlite3ExprListDelete(sqlite3 *db, ExprList *pList){
72310   int i;
72311   struct ExprList_item *pItem;
72312   if( pList==0 ) return;
72313   assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) );
72314   assert( pList->nExpr<=pList->nAlloc );
72315   for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
72316     sqlite3ExprDelete(db, pItem->pExpr);
72317     sqlite3DbFree(db, pItem->zName);
72318     sqlite3DbFree(db, pItem->zSpan);
72319   }
72320   sqlite3DbFree(db, pList->a);
72321   sqlite3DbFree(db, pList);
72322 }
72323 
72324 /*
72325 ** These routines are Walker callbacks.  Walker.u.pi is a pointer
72326 ** to an integer.  These routines are checking an expression to see
72327 ** if it is a constant.  Set *Walker.u.pi to 0 if the expression is
72328 ** not constant.
72329 **
72330 ** These callback routines are used to implement the following:
72331 **
72332 **     sqlite3ExprIsConstant()
72333 **     sqlite3ExprIsConstantNotJoin()
72334 **     sqlite3ExprIsConstantOrFunction()
72335 **
72336 */
72337 static int exprNodeIsConstant(Walker *pWalker, Expr *pExpr){
72338 
72339   /* If pWalker->u.i is 3 then any term of the expression that comes from
72340   ** the ON or USING clauses of a join disqualifies the expression
72341   ** from being considered constant. */
72342   if( pWalker->u.i==3 && ExprHasAnyProperty(pExpr, EP_FromJoin) ){
72343     pWalker->u.i = 0;
72344     return WRC_Abort;
72345   }
72346 
72347   switch( pExpr->op ){
72348     /* Consider functions to be constant if all their arguments are constant
72349     ** and pWalker->u.i==2 */
72350     case TK_FUNCTION:
72351       if( pWalker->u.i==2 ) return 0;
72352       /* Fall through */
72353     case TK_ID:
72354     case TK_COLUMN:
72355     case TK_AGG_FUNCTION:
72356     case TK_AGG_COLUMN:
72357       testcase( pExpr->op==TK_ID );
72358       testcase( pExpr->op==TK_COLUMN );
72359       testcase( pExpr->op==TK_AGG_FUNCTION );
72360       testcase( pExpr->op==TK_AGG_COLUMN );
72361       pWalker->u.i = 0;
72362       return WRC_Abort;
72363     default:
72364       testcase( pExpr->op==TK_SELECT ); /* selectNodeIsConstant will disallow */
72365       testcase( pExpr->op==TK_EXISTS ); /* selectNodeIsConstant will disallow */
72366       return WRC_Continue;
72367   }
72368 }
72369 static int selectNodeIsConstant(Walker *pWalker, Select *NotUsed){
72370   UNUSED_PARAMETER(NotUsed);
72371   pWalker->u.i = 0;
72372   return WRC_Abort;
72373 }
72374 static int exprIsConst(Expr *p, int initFlag){
72375   Walker w;
72376   w.u.i = initFlag;
72377   w.xExprCallback = exprNodeIsConstant;
72378   w.xSelectCallback = selectNodeIsConstant;
72379   sqlite3WalkExpr(&w, p);
72380   return w.u.i;
72381 }
72382 
72383 /*
72384 ** Walk an expression tree.  Return 1 if the expression is constant
72385 ** and 0 if it involves variables or function calls.
72386 **
72387 ** For the purposes of this function, a double-quoted string (ex: "abc")
72388 ** is considered a variable but a single-quoted string (ex: 'abc') is
72389 ** a constant.
72390 */
72391 SQLITE_PRIVATE int sqlite3ExprIsConstant(Expr *p){
72392   return exprIsConst(p, 1);
72393 }
72394 
72395 /*
72396 ** Walk an expression tree.  Return 1 if the expression is constant
72397 ** that does no originate from the ON or USING clauses of a join.
72398 ** Return 0 if it involves variables or function calls or terms from
72399 ** an ON or USING clause.
72400 */
72401 SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr *p){
72402   return exprIsConst(p, 3);
72403 }
72404 
72405 /*
72406 ** Walk an expression tree.  Return 1 if the expression is constant
72407 ** or a function call with constant arguments.  Return and 0 if there
72408 ** are any variables.
72409 **
72410 ** For the purposes of this function, a double-quoted string (ex: "abc")
72411 ** is considered a variable but a single-quoted string (ex: 'abc') is
72412 ** a constant.
72413 */
72414 SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr *p){
72415   return exprIsConst(p, 2);
72416 }
72417 
72418 /*
72419 ** If the expression p codes a constant integer that is small enough
72420 ** to fit in a 32-bit integer, return 1 and put the value of the integer
72421 ** in *pValue.  If the expression is not an integer or if it is too big
72422 ** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
72423 */
72424 SQLITE_PRIVATE int sqlite3ExprIsInteger(Expr *p, int *pValue){
72425   int rc = 0;
72426 
72427   /* If an expression is an integer literal that fits in a signed 32-bit
72428   ** integer, then the EP_IntValue flag will have already been set */
72429   assert( p->op!=TK_INTEGER || (p->flags & EP_IntValue)!=0
72430            || sqlite3GetInt32(p->u.zToken, &rc)==0 );
72431 
72432   if( p->flags & EP_IntValue ){
72433     *pValue = p->u.iValue;
72434     return 1;
72435   }
72436   switch( p->op ){
72437     case TK_UPLUS: {
72438       rc = sqlite3ExprIsInteger(p->pLeft, pValue);
72439       break;
72440     }
72441     case TK_UMINUS: {
72442       int v;
72443       if( sqlite3ExprIsInteger(p->pLeft, &v) ){
72444         *pValue = -v;
72445         rc = 1;
72446       }
72447       break;
72448     }
72449     default: break;
72450   }
72451   return rc;
72452 }
72453 
72454 /*
72455 ** Return FALSE if there is no chance that the expression can be NULL.
72456 **
72457 ** If the expression might be NULL or if the expression is too complex
72458 ** to tell return TRUE.
72459 **
72460 ** This routine is used as an optimization, to skip OP_IsNull opcodes
72461 ** when we know that a value cannot be NULL.  Hence, a false positive
72462 ** (returning TRUE when in fact the expression can never be NULL) might
72463 ** be a small performance hit but is otherwise harmless.  On the other
72464 ** hand, a false negative (returning FALSE when the result could be NULL)
72465 ** will likely result in an incorrect answer.  So when in doubt, return
72466 ** TRUE.
72467 */
72468 SQLITE_PRIVATE int sqlite3ExprCanBeNull(const Expr *p){
72469   u8 op;
72470   while( p->op==TK_UPLUS || p->op==TK_UMINUS ){ p = p->pLeft; }
72471   op = p->op;
72472   if( op==TK_REGISTER ) op = p->op2;
72473   switch( op ){
72474     case TK_INTEGER:
72475     case TK_STRING:
72476     case TK_FLOAT:
72477     case TK_BLOB:
72478       return 0;
72479     default:
72480       return 1;
72481   }
72482 }
72483 
72484 /*
72485 ** Generate an OP_IsNull instruction that tests register iReg and jumps
72486 ** to location iDest if the value in iReg is NULL.  The value in iReg
72487 ** was computed by pExpr.  If we can look at pExpr at compile-time and
72488 ** determine that it can never generate a NULL, then the OP_IsNull operation
72489 ** can be omitted.
72490 */
72491 SQLITE_PRIVATE void sqlite3ExprCodeIsNullJump(
72492   Vdbe *v,            /* The VDBE under construction */
72493   const Expr *pExpr,  /* Only generate OP_IsNull if this expr can be NULL */
72494   int iReg,           /* Test the value in this register for NULL */
72495   int iDest           /* Jump here if the value is null */
72496 ){
72497   if( sqlite3ExprCanBeNull(pExpr) ){
72498     sqlite3VdbeAddOp2(v, OP_IsNull, iReg, iDest);
72499   }
72500 }
72501 
72502 /*
72503 ** Return TRUE if the given expression is a constant which would be
72504 ** unchanged by OP_Affinity with the affinity given in the second
72505 ** argument.
72506 **
72507 ** This routine is used to determine if the OP_Affinity operation
72508 ** can be omitted.  When in doubt return FALSE.  A false negative
72509 ** is harmless.  A false positive, however, can result in the wrong
72510 ** answer.
72511 */
72512 SQLITE_PRIVATE int sqlite3ExprNeedsNoAffinityChange(const Expr *p, char aff){
72513   u8 op;
72514   if( aff==SQLITE_AFF_NONE ) return 1;
72515   while( p->op==TK_UPLUS || p->op==TK_UMINUS ){ p = p->pLeft; }
72516   op = p->op;
72517   if( op==TK_REGISTER ) op = p->op2;
72518   switch( op ){
72519     case TK_INTEGER: {
72520       return aff==SQLITE_AFF_INTEGER || aff==SQLITE_AFF_NUMERIC;
72521     }
72522     case TK_FLOAT: {
72523       return aff==SQLITE_AFF_REAL || aff==SQLITE_AFF_NUMERIC;
72524     }
72525     case TK_STRING: {
72526       return aff==SQLITE_AFF_TEXT;
72527     }
72528     case TK_BLOB: {
72529       return 1;
72530     }
72531     case TK_COLUMN: {
72532       assert( p->iTable>=0 );  /* p cannot be part of a CHECK constraint */
72533       return p->iColumn<0
72534           && (aff==SQLITE_AFF_INTEGER || aff==SQLITE_AFF_NUMERIC);
72535     }
72536     default: {
72537       return 0;
72538     }
72539   }
72540 }
72541 
72542 /*
72543 ** Return TRUE if the given string is a row-id column name.
72544 */
72545 SQLITE_PRIVATE int sqlite3IsRowid(const char *z){
72546   if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
72547   if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
72548   if( sqlite3StrICmp(z, "OID")==0 ) return 1;
72549   return 0;
72550 }
72551 
72552 /*
72553 ** Return true if we are able to the IN operator optimization on a
72554 ** query of the form
72555 **
72556 **       x IN (SELECT ...)
72557 **
72558 ** Where the SELECT... clause is as specified by the parameter to this
72559 ** routine.
72560 **
72561 ** The Select object passed in has already been preprocessed and no
72562 ** errors have been found.
72563 */
72564 #ifndef SQLITE_OMIT_SUBQUERY
72565 static int isCandidateForInOpt(Select *p){
72566   SrcList *pSrc;
72567   ExprList *pEList;
72568   Table *pTab;
72569   if( p==0 ) return 0;                   /* right-hand side of IN is SELECT */
72570   if( p->pPrior ) return 0;              /* Not a compound SELECT */
72571   if( p->selFlags & (SF_Distinct|SF_Aggregate) ){
72572     testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
72573     testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
72574     return 0; /* No DISTINCT keyword and no aggregate functions */
72575   }
72576   assert( p->pGroupBy==0 );              /* Has no GROUP BY clause */
72577   if( p->pLimit ) return 0;              /* Has no LIMIT clause */
72578   assert( p->pOffset==0 );               /* No LIMIT means no OFFSET */
72579   if( p->pWhere ) return 0;              /* Has no WHERE clause */
72580   pSrc = p->pSrc;
72581   assert( pSrc!=0 );
72582   if( pSrc->nSrc!=1 ) return 0;          /* Single term in FROM clause */
72583   if( pSrc->a[0].pSelect ) return 0;     /* FROM is not a subquery or view */
72584   pTab = pSrc->a[0].pTab;
72585   if( NEVER(pTab==0) ) return 0;
72586   assert( pTab->pSelect==0 );            /* FROM clause is not a view */
72587   if( IsVirtual(pTab) ) return 0;        /* FROM clause not a virtual table */
72588   pEList = p->pEList;
72589   if( pEList->nExpr!=1 ) return 0;       /* One column in the result set */
72590   if( pEList->a[0].pExpr->op!=TK_COLUMN ) return 0; /* Result is a column */
72591   return 1;
72592 }
72593 #endif /* SQLITE_OMIT_SUBQUERY */
72594 
72595 /*
72596 ** This function is used by the implementation of the IN (...) operator.
72597 ** It's job is to find or create a b-tree structure that may be used
72598 ** either to test for membership of the (...) set or to iterate through
72599 ** its members, skipping duplicates.
72600 **
72601 ** The index of the cursor opened on the b-tree (database table, database index
72602 ** or ephermal table) is stored in pX->iTable before this function returns.
72603 ** The returned value of this function indicates the b-tree type, as follows:
72604 **
72605 **   IN_INDEX_ROWID - The cursor was opened on a database table.
72606 **   IN_INDEX_INDEX - The cursor was opened on a database index.
72607 **   IN_INDEX_EPH -   The cursor was opened on a specially created and
72608 **                    populated epheremal table.
72609 **
72610 ** An existing b-tree may only be used if the SELECT is of the simple
72611 ** form:
72612 **
72613 **     SELECT <column> FROM <table>
72614 **
72615 ** If the prNotFound parameter is 0, then the b-tree will be used to iterate
72616 ** through the set members, skipping any duplicates. In this case an
72617 ** epheremal table must be used unless the selected <column> is guaranteed
72618 ** to be unique - either because it is an INTEGER PRIMARY KEY or it
72619 ** has a UNIQUE constraint or UNIQUE index.
72620 **
72621 ** If the prNotFound parameter is not 0, then the b-tree will be used
72622 ** for fast set membership tests. In this case an epheremal table must
72623 ** be used unless <column> is an INTEGER PRIMARY KEY or an index can
72624 ** be found with <column> as its left-most column.
72625 **
72626 ** When the b-tree is being used for membership tests, the calling function
72627 ** needs to know whether or not the structure contains an SQL NULL
72628 ** value in order to correctly evaluate expressions like "X IN (Y, Z)".
72629 ** If there is any chance that the (...) might contain a NULL value at
72630 ** runtime, then a register is allocated and the register number written
72631 ** to *prNotFound. If there is no chance that the (...) contains a
72632 ** NULL value, then *prNotFound is left unchanged.
72633 **
72634 ** If a register is allocated and its location stored in *prNotFound, then
72635 ** its initial value is NULL.  If the (...) does not remain constant
72636 ** for the duration of the query (i.e. the SELECT within the (...)
72637 ** is a correlated subquery) then the value of the allocated register is
72638 ** reset to NULL each time the subquery is rerun. This allows the
72639 ** caller to use vdbe code equivalent to the following:
72640 **
72641 **   if( register==NULL ){
72642 **     has_null = <test if data structure contains null>
72643 **     register = 1
72644 **   }
72645 **
72646 ** in order to avoid running the <test if data structure contains null>
72647 ** test more often than is necessary.
72648 */
72649 #ifndef SQLITE_OMIT_SUBQUERY
72650 SQLITE_PRIVATE int sqlite3FindInIndex(Parse *pParse, Expr *pX, int *prNotFound){
72651   Select *p;                            /* SELECT to the right of IN operator */
72652   int eType = 0;                        /* Type of RHS table. IN_INDEX_* */
72653   int iTab = pParse->nTab++;            /* Cursor of the RHS table */
72654   int mustBeUnique = (prNotFound==0);   /* True if RHS must be unique */
72655 
72656   assert( pX->op==TK_IN );
72657 
72658   /* Check to see if an existing table or index can be used to
72659   ** satisfy the query.  This is preferable to generating a new
72660   ** ephemeral table.
72661   */
72662   p = (ExprHasProperty(pX, EP_xIsSelect) ? pX->x.pSelect : 0);
72663   if( ALWAYS(pParse->nErr==0) && isCandidateForInOpt(p) ){
72664     sqlite3 *db = pParse->db;              /* Database connection */
72665     Expr *pExpr = p->pEList->a[0].pExpr;   /* Expression <column> */
72666     int iCol = pExpr->iColumn;             /* Index of column <column> */
72667     Vdbe *v = sqlite3GetVdbe(pParse);      /* Virtual machine being coded */
72668     Table *pTab = p->pSrc->a[0].pTab;      /* Table <table>. */
72669     int iDb;                               /* Database idx for pTab */
72670 
72671     /* Code an OP_VerifyCookie and OP_TableLock for <table>. */
72672     iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
72673     sqlite3CodeVerifySchema(pParse, iDb);
72674     sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
72675 
72676     /* This function is only called from two places. In both cases the vdbe
72677     ** has already been allocated. So assume sqlite3GetVdbe() is always
72678     ** successful here.
72679     */
72680     assert(v);
72681     if( iCol<0 ){
72682       int iMem = ++pParse->nMem;
72683       int iAddr;
72684 
72685       iAddr = sqlite3VdbeAddOp1(v, OP_If, iMem);
72686       sqlite3VdbeAddOp2(v, OP_Integer, 1, iMem);
72687 
72688       sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
72689       eType = IN_INDEX_ROWID;
72690 
72691       sqlite3VdbeJumpHere(v, iAddr);
72692     }else{
72693       Index *pIdx;                         /* Iterator variable */
72694 
72695       /* The collation sequence used by the comparison. If an index is to
72696       ** be used in place of a temp-table, it must be ordered according
72697       ** to this collation sequence.  */
72698       CollSeq *pReq = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pExpr);
72699 
72700       /* Check that the affinity that will be used to perform the
72701       ** comparison is the same as the affinity of the column. If
72702       ** it is not, it is not possible to use any index.
72703       */
72704       char aff = comparisonAffinity(pX);
72705       int affinity_ok = (pTab->aCol[iCol].affinity==aff||aff==SQLITE_AFF_NONE);
72706 
72707       for(pIdx=pTab->pIndex; pIdx && eType==0 && affinity_ok; pIdx=pIdx->pNext){
72708         if( (pIdx->aiColumn[0]==iCol)
72709          && sqlite3FindCollSeq(db, ENC(db), pIdx->azColl[0], 0)==pReq
72710          && (!mustBeUnique || (pIdx->nColumn==1 && pIdx->onError!=OE_None))
72711         ){
72712           int iMem = ++pParse->nMem;
72713           int iAddr;
72714           char *pKey;
72715 
72716           pKey = (char *)sqlite3IndexKeyinfo(pParse, pIdx);
72717           iAddr = sqlite3VdbeAddOp1(v, OP_If, iMem);
72718           sqlite3VdbeAddOp2(v, OP_Integer, 1, iMem);
72719 
72720           sqlite3VdbeAddOp4(v, OP_OpenRead, iTab, pIdx->tnum, iDb,
72721                                pKey,P4_KEYINFO_HANDOFF);
72722           VdbeComment((v, "%s", pIdx->zName));
72723           eType = IN_INDEX_INDEX;
72724 
72725           sqlite3VdbeJumpHere(v, iAddr);
72726           if( prNotFound && !pTab->aCol[iCol].notNull ){
72727             *prNotFound = ++pParse->nMem;
72728           }
72729         }
72730       }
72731     }
72732   }
72733 
72734   if( eType==0 ){
72735     /* Could not found an existing table or index to use as the RHS b-tree.
72736     ** We will have to generate an ephemeral table to do the job.
72737     */
72738     double savedNQueryLoop = pParse->nQueryLoop;
72739     int rMayHaveNull = 0;
72740     eType = IN_INDEX_EPH;
72741     if( prNotFound ){
72742       *prNotFound = rMayHaveNull = ++pParse->nMem;
72743     }else{
72744       testcase( pParse->nQueryLoop>(double)1 );
72745       pParse->nQueryLoop = (double)1;
72746       if( pX->pLeft->iColumn<0 && !ExprHasAnyProperty(pX, EP_xIsSelect) ){
72747         eType = IN_INDEX_ROWID;
72748       }
72749     }
72750     sqlite3CodeSubselect(pParse, pX, rMayHaveNull, eType==IN_INDEX_ROWID);
72751     pParse->nQueryLoop = savedNQueryLoop;
72752   }else{
72753     pX->iTable = iTab;
72754   }
72755   return eType;
72756 }
72757 #endif
72758 
72759 /*
72760 ** Generate code for scalar subqueries used as a subquery expression, EXISTS,
72761 ** or IN operators.  Examples:
72762 **
72763 **     (SELECT a FROM b)          -- subquery
72764 **     EXISTS (SELECT a FROM b)   -- EXISTS subquery
72765 **     x IN (4,5,11)              -- IN operator with list on right-hand side
72766 **     x IN (SELECT a FROM b)     -- IN operator with subquery on the right
72767 **
72768 ** The pExpr parameter describes the expression that contains the IN
72769 ** operator or subquery.
72770 **
72771 ** If parameter isRowid is non-zero, then expression pExpr is guaranteed
72772 ** to be of the form "<rowid> IN (?, ?, ?)", where <rowid> is a reference
72773 ** to some integer key column of a table B-Tree. In this case, use an
72774 ** intkey B-Tree to store the set of IN(...) values instead of the usual
72775 ** (slower) variable length keys B-Tree.
72776 **
72777 ** If rMayHaveNull is non-zero, that means that the operation is an IN
72778 ** (not a SELECT or EXISTS) and that the RHS might contains NULLs.
72779 ** Furthermore, the IN is in a WHERE clause and that we really want
72780 ** to iterate over the RHS of the IN operator in order to quickly locate
72781 ** all corresponding LHS elements.  All this routine does is initialize
72782 ** the register given by rMayHaveNull to NULL.  Calling routines will take
72783 ** care of changing this register value to non-NULL if the RHS is NULL-free.
72784 **
72785 ** If rMayHaveNull is zero, that means that the subquery is being used
72786 ** for membership testing only.  There is no need to initialize any
72787 ** registers to indicate the presense or absence of NULLs on the RHS.
72788 **
72789 ** For a SELECT or EXISTS operator, return the register that holds the
72790 ** result.  For IN operators or if an error occurs, the return value is 0.
72791 */
72792 #ifndef SQLITE_OMIT_SUBQUERY
72793 SQLITE_PRIVATE int sqlite3CodeSubselect(
72794   Parse *pParse,          /* Parsing context */
72795   Expr *pExpr,            /* The IN, SELECT, or EXISTS operator */
72796   int rMayHaveNull,       /* Register that records whether NULLs exist in RHS */
72797   int isRowid             /* If true, LHS of IN operator is a rowid */
72798 ){
72799   int testAddr = 0;                       /* One-time test address */
72800   int rReg = 0;                           /* Register storing resulting */
72801   Vdbe *v = sqlite3GetVdbe(pParse);
72802   if( NEVER(v==0) ) return 0;
72803   sqlite3ExprCachePush(pParse);
72804 
72805   /* This code must be run in its entirety every time it is encountered
72806   ** if any of the following is true:
72807   **
72808   **    *  The right-hand side is a correlated subquery
72809   **    *  The right-hand side is an expression list containing variables
72810   **    *  We are inside a trigger
72811   **
72812   ** If all of the above are false, then we can run this code just once
72813   ** save the results, and reuse the same result on subsequent invocations.
72814   */
72815   if( !ExprHasAnyProperty(pExpr, EP_VarSelect) && !pParse->pTriggerTab ){
72816     int mem = ++pParse->nMem;
72817     sqlite3VdbeAddOp1(v, OP_If, mem);
72818     testAddr = sqlite3VdbeAddOp2(v, OP_Integer, 1, mem);
72819     assert( testAddr>0 || pParse->db->mallocFailed );
72820   }
72821 
72822 #ifndef SQLITE_OMIT_EXPLAIN
72823   if( pParse->explain==2 ){
72824     char *zMsg = sqlite3MPrintf(
72825         pParse->db, "EXECUTE %s%s SUBQUERY %d", testAddr?"":"CORRELATED ",
72826         pExpr->op==TK_IN?"LIST":"SCALAR", pParse->iNextSelectId
72827     );
72828     sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC);
72829   }
72830 #endif
72831 
72832   switch( pExpr->op ){
72833     case TK_IN: {
72834       char affinity;              /* Affinity of the LHS of the IN */
72835       KeyInfo keyInfo;            /* Keyinfo for the generated table */
72836       int addr;                   /* Address of OP_OpenEphemeral instruction */
72837       Expr *pLeft = pExpr->pLeft; /* the LHS of the IN operator */
72838 
72839       if( rMayHaveNull ){
72840         sqlite3VdbeAddOp2(v, OP_Null, 0, rMayHaveNull);
72841       }
72842 
72843       affinity = sqlite3ExprAffinity(pLeft);
72844 
72845       /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
72846       ** expression it is handled the same way.  An ephemeral table is
72847       ** filled with single-field index keys representing the results
72848       ** from the SELECT or the <exprlist>.
72849       **
72850       ** If the 'x' expression is a column value, or the SELECT...
72851       ** statement returns a column value, then the affinity of that
72852       ** column is used to build the index keys. If both 'x' and the
72853       ** SELECT... statement are columns, then numeric affinity is used
72854       ** if either column has NUMERIC or INTEGER affinity. If neither
72855       ** 'x' nor the SELECT... statement are columns, then numeric affinity
72856       ** is used.
72857       */
72858       pExpr->iTable = pParse->nTab++;
72859       addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pExpr->iTable, !isRowid);
72860       if( rMayHaveNull==0 ) sqlite3VdbeChangeP5(v, BTREE_UNORDERED);
72861       memset(&keyInfo, 0, sizeof(keyInfo));
72862       keyInfo.nField = 1;
72863 
72864       if( ExprHasProperty(pExpr, EP_xIsSelect) ){
72865         /* Case 1:     expr IN (SELECT ...)
72866         **
72867         ** Generate code to write the results of the select into the temporary
72868         ** table allocated and opened above.
72869         */
72870         SelectDest dest;
72871         ExprList *pEList;
72872 
72873         assert( !isRowid );
72874         sqlite3SelectDestInit(&dest, SRT_Set, pExpr->iTable);
72875         dest.affinity = (u8)affinity;
72876         assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
72877         pExpr->x.pSelect->iLimit = 0;
72878         if( sqlite3Select(pParse, pExpr->x.pSelect, &dest) ){
72879           return 0;
72880         }
72881         pEList = pExpr->x.pSelect->pEList;
72882         if( ALWAYS(pEList!=0 && pEList->nExpr>0) ){
72883           keyInfo.aColl[0] = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft,
72884               pEList->a[0].pExpr);
72885         }
72886       }else if( ALWAYS(pExpr->x.pList!=0) ){
72887         /* Case 2:     expr IN (exprlist)
72888         **
72889         ** For each expression, build an index key from the evaluation and
72890         ** store it in the temporary table. If <expr> is a column, then use
72891         ** that columns affinity when building index keys. If <expr> is not
72892         ** a column, use numeric affinity.
72893         */
72894         int i;
72895         ExprList *pList = pExpr->x.pList;
72896         struct ExprList_item *pItem;
72897         int r1, r2, r3;
72898 
72899         if( !affinity ){
72900           affinity = SQLITE_AFF_NONE;
72901         }
72902         keyInfo.aColl[0] = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
72903 
72904         /* Loop through each expression in <exprlist>. */
72905         r1 = sqlite3GetTempReg(pParse);
72906         r2 = sqlite3GetTempReg(pParse);
72907         sqlite3VdbeAddOp2(v, OP_Null, 0, r2);
72908         for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
72909           Expr *pE2 = pItem->pExpr;
72910           int iValToIns;
72911 
72912           /* If the expression is not constant then we will need to
72913           ** disable the test that was generated above that makes sure
72914           ** this code only executes once.  Because for a non-constant
72915           ** expression we need to rerun this code each time.
72916           */
72917           if( testAddr && !sqlite3ExprIsConstant(pE2) ){
72918             sqlite3VdbeChangeToNoop(v, testAddr-1, 2);
72919             testAddr = 0;
72920           }
72921 
72922           /* Evaluate the expression and insert it into the temp table */
72923           if( isRowid && sqlite3ExprIsInteger(pE2, &iValToIns) ){
72924             sqlite3VdbeAddOp3(v, OP_InsertInt, pExpr->iTable, r2, iValToIns);
72925           }else{
72926             r3 = sqlite3ExprCodeTarget(pParse, pE2, r1);
72927             if( isRowid ){
72928               sqlite3VdbeAddOp2(v, OP_MustBeInt, r3,
72929                                 sqlite3VdbeCurrentAddr(v)+2);
72930               sqlite3VdbeAddOp3(v, OP_Insert, pExpr->iTable, r2, r3);
72931             }else{
72932               sqlite3VdbeAddOp4(v, OP_MakeRecord, r3, 1, r2, &affinity, 1);
72933               sqlite3ExprCacheAffinityChange(pParse, r3, 1);
72934               sqlite3VdbeAddOp2(v, OP_IdxInsert, pExpr->iTable, r2);
72935             }
72936           }
72937         }
72938         sqlite3ReleaseTempReg(pParse, r1);
72939         sqlite3ReleaseTempReg(pParse, r2);
72940       }
72941       if( !isRowid ){
72942         sqlite3VdbeChangeP4(v, addr, (void *)&keyInfo, P4_KEYINFO);
72943       }
72944       break;
72945     }
72946 
72947     case TK_EXISTS:
72948     case TK_SELECT:
72949     default: {
72950       /* If this has to be a scalar SELECT.  Generate code to put the
72951       ** value of this select in a memory cell and record the number
72952       ** of the memory cell in iColumn.  If this is an EXISTS, write
72953       ** an integer 0 (not exists) or 1 (exists) into a memory cell
72954       ** and record that memory cell in iColumn.
72955       */
72956       Select *pSel;                         /* SELECT statement to encode */
72957       SelectDest dest;                      /* How to deal with SELECt result */
72958 
72959       testcase( pExpr->op==TK_EXISTS );
72960       testcase( pExpr->op==TK_SELECT );
72961       assert( pExpr->op==TK_EXISTS || pExpr->op==TK_SELECT );
72962 
72963       assert( ExprHasProperty(pExpr, EP_xIsSelect) );
72964       pSel = pExpr->x.pSelect;
72965       sqlite3SelectDestInit(&dest, 0, ++pParse->nMem);
72966       if( pExpr->op==TK_SELECT ){
72967         dest.eDest = SRT_Mem;
72968         sqlite3VdbeAddOp2(v, OP_Null, 0, dest.iParm);
72969         VdbeComment((v, "Init subquery result"));
72970       }else{
72971         dest.eDest = SRT_Exists;
72972         sqlite3VdbeAddOp2(v, OP_Integer, 0, dest.iParm);
72973         VdbeComment((v, "Init EXISTS result"));
72974       }
72975       sqlite3ExprDelete(pParse->db, pSel->pLimit);
72976       pSel->pLimit = sqlite3PExpr(pParse, TK_INTEGER, 0, 0,
72977                                   &sqlite3IntTokens[1]);
72978       pSel->iLimit = 0;
72979       if( sqlite3Select(pParse, pSel, &dest) ){
72980         return 0;
72981       }
72982       rReg = dest.iParm;
72983       ExprSetIrreducible(pExpr);
72984       break;
72985     }
72986   }
72987 
72988   if( testAddr ){
72989     sqlite3VdbeJumpHere(v, testAddr-1);
72990   }
72991   sqlite3ExprCachePop(pParse, 1);
72992 
72993   return rReg;
72994 }
72995 #endif /* SQLITE_OMIT_SUBQUERY */
72996 
72997 #ifndef SQLITE_OMIT_SUBQUERY
72998 /*
72999 ** Generate code for an IN expression.
73000 **
73001 **      x IN (SELECT ...)
73002 **      x IN (value, value, ...)
73003 **
73004 ** The left-hand side (LHS) is a scalar expression.  The right-hand side (RHS)
73005 ** is an array of zero or more values.  The expression is true if the LHS is
73006 ** contained within the RHS.  The value of the expression is unknown (NULL)
73007 ** if the LHS is NULL or if the LHS is not contained within the RHS and the
73008 ** RHS contains one or more NULL values.
73009 **
73010 ** This routine generates code will jump to destIfFalse if the LHS is not
73011 ** contained within the RHS.  If due to NULLs we cannot determine if the LHS
73012 ** is contained in the RHS then jump to destIfNull.  If the LHS is contained
73013 ** within the RHS then fall through.
73014 */
73015 static void sqlite3ExprCodeIN(
73016   Parse *pParse,        /* Parsing and code generating context */
73017   Expr *pExpr,          /* The IN expression */
73018   int destIfFalse,      /* Jump here if LHS is not contained in the RHS */
73019   int destIfNull        /* Jump here if the results are unknown due to NULLs */
73020 ){
73021   int rRhsHasNull = 0;  /* Register that is true if RHS contains NULL values */
73022   char affinity;        /* Comparison affinity to use */
73023   int eType;            /* Type of the RHS */
73024   int r1;               /* Temporary use register */
73025   Vdbe *v;              /* Statement under construction */
73026 
73027   /* Compute the RHS.   After this step, the table with cursor
73028   ** pExpr->iTable will contains the values that make up the RHS.
73029   */
73030   v = pParse->pVdbe;
73031   assert( v!=0 );       /* OOM detected prior to this routine */
73032   VdbeNoopComment((v, "begin IN expr"));
73033   eType = sqlite3FindInIndex(pParse, pExpr, &rRhsHasNull);
73034 
73035   /* Figure out the affinity to use to create a key from the results
73036   ** of the expression. affinityStr stores a static string suitable for
73037   ** P4 of OP_MakeRecord.
73038   */
73039   affinity = comparisonAffinity(pExpr);
73040 
73041   /* Code the LHS, the <expr> from "<expr> IN (...)".
73042   */
73043   sqlite3ExprCachePush(pParse);
73044   r1 = sqlite3GetTempReg(pParse);
73045   sqlite3ExprCode(pParse, pExpr->pLeft, r1);
73046 
73047   /* If the LHS is NULL, then the result is either false or NULL depending
73048   ** on whether the RHS is empty or not, respectively.
73049   */
73050   if( destIfNull==destIfFalse ){
73051     /* Shortcut for the common case where the false and NULL outcomes are
73052     ** the same. */
73053     sqlite3VdbeAddOp2(v, OP_IsNull, r1, destIfNull);
73054   }else{
73055     int addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, r1);
73056     sqlite3VdbeAddOp2(v, OP_Rewind, pExpr->iTable, destIfFalse);
73057     sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfNull);
73058     sqlite3VdbeJumpHere(v, addr1);
73059   }
73060 
73061   if( eType==IN_INDEX_ROWID ){
73062     /* In this case, the RHS is the ROWID of table b-tree
73063     */
73064     sqlite3VdbeAddOp2(v, OP_MustBeInt, r1, destIfFalse);
73065     sqlite3VdbeAddOp3(v, OP_NotExists, pExpr->iTable, destIfFalse, r1);
73066   }else{
73067     /* In this case, the RHS is an index b-tree.
73068     */
73069     sqlite3VdbeAddOp4(v, OP_Affinity, r1, 1, 0, &affinity, 1);
73070 
73071     /* If the set membership test fails, then the result of the
73072     ** "x IN (...)" expression must be either 0 or NULL. If the set
73073     ** contains no NULL values, then the result is 0. If the set
73074     ** contains one or more NULL values, then the result of the
73075     ** expression is also NULL.
73076     */
73077     if( rRhsHasNull==0 || destIfFalse==destIfNull ){
73078       /* This branch runs if it is known at compile time that the RHS
73079       ** cannot contain NULL values. This happens as the result
73080       ** of a "NOT NULL" constraint in the database schema.
73081       **
73082       ** Also run this branch if NULL is equivalent to FALSE
73083       ** for this particular IN operator.
73084       */
73085       sqlite3VdbeAddOp4Int(v, OP_NotFound, pExpr->iTable, destIfFalse, r1, 1);
73086 
73087     }else{
73088       /* In this branch, the RHS of the IN might contain a NULL and
73089       ** the presence of a NULL on the RHS makes a difference in the
73090       ** outcome.
73091       */
73092       int j1, j2, j3;
73093 
73094       /* First check to see if the LHS is contained in the RHS.  If so,
73095       ** then the presence of NULLs in the RHS does not matter, so jump
73096       ** over all of the code that follows.
73097       */
73098       j1 = sqlite3VdbeAddOp4Int(v, OP_Found, pExpr->iTable, 0, r1, 1);
73099 
73100       /* Here we begin generating code that runs if the LHS is not
73101       ** contained within the RHS.  Generate additional code that
73102       ** tests the RHS for NULLs.  If the RHS contains a NULL then
73103       ** jump to destIfNull.  If there are no NULLs in the RHS then
73104       ** jump to destIfFalse.
73105       */
73106       j2 = sqlite3VdbeAddOp1(v, OP_NotNull, rRhsHasNull);
73107       j3 = sqlite3VdbeAddOp4Int(v, OP_Found, pExpr->iTable, 0, rRhsHasNull, 1);
73108       sqlite3VdbeAddOp2(v, OP_Integer, -1, rRhsHasNull);
73109       sqlite3VdbeJumpHere(v, j3);
73110       sqlite3VdbeAddOp2(v, OP_AddImm, rRhsHasNull, 1);
73111       sqlite3VdbeJumpHere(v, j2);
73112 
73113       /* Jump to the appropriate target depending on whether or not
73114       ** the RHS contains a NULL
73115       */
73116       sqlite3VdbeAddOp2(v, OP_If, rRhsHasNull, destIfNull);
73117       sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfFalse);
73118 
73119       /* The OP_Found at the top of this branch jumps here when true,
73120       ** causing the overall IN expression evaluation to fall through.
73121       */
73122       sqlite3VdbeJumpHere(v, j1);
73123     }
73124   }
73125   sqlite3ReleaseTempReg(pParse, r1);
73126   sqlite3ExprCachePop(pParse, 1);
73127   VdbeComment((v, "end IN expr"));
73128 }
73129 #endif /* SQLITE_OMIT_SUBQUERY */
73130 
73131 /*
73132 ** Duplicate an 8-byte value
73133 */
73134 static char *dup8bytes(Vdbe *v, const char *in){
73135   char *out = sqlite3DbMallocRaw(sqlite3VdbeDb(v), 8);
73136   if( out ){
73137     memcpy(out, in, 8);
73138   }
73139   return out;
73140 }
73141 
73142 #ifndef SQLITE_OMIT_FLOATING_POINT
73143 /*
73144 ** Generate an instruction that will put the floating point
73145 ** value described by z[0..n-1] into register iMem.
73146 **
73147 ** The z[] string will probably not be zero-terminated.  But the
73148 ** z[n] character is guaranteed to be something that does not look
73149 ** like the continuation of the number.
73150 */
73151 static void codeReal(Vdbe *v, const char *z, int negateFlag, int iMem){
73152   if( ALWAYS(z!=0) ){
73153     double value;
73154     char *zV;
73155     sqlite3AtoF(z, &value, sqlite3Strlen30(z), SQLITE_UTF8);
73156     assert( !sqlite3IsNaN(value) ); /* The new AtoF never returns NaN */
73157     if( negateFlag ) value = -value;
73158     zV = dup8bytes(v, (char*)&value);
73159     sqlite3VdbeAddOp4(v, OP_Real, 0, iMem, 0, zV, P4_REAL);
73160   }
73161 }
73162 #endif
73163 
73164 
73165 /*
73166 ** Generate an instruction that will put the integer describe by
73167 ** text z[0..n-1] into register iMem.
73168 **
73169 ** Expr.u.zToken is always UTF8 and zero-terminated.
73170 */
73171 static void codeInteger(Parse *pParse, Expr *pExpr, int negFlag, int iMem){
73172   Vdbe *v = pParse->pVdbe;
73173   if( pExpr->flags & EP_IntValue ){
73174     int i = pExpr->u.iValue;
73175     assert( i>=0 );
73176     if( negFlag ) i = -i;
73177     sqlite3VdbeAddOp2(v, OP_Integer, i, iMem);
73178   }else{
73179     int c;
73180     i64 value;
73181     const char *z = pExpr->u.zToken;
73182     assert( z!=0 );
73183     c = sqlite3Atoi64(z, &value, sqlite3Strlen30(z), SQLITE_UTF8);
73184     if( c==0 || (c==2 && negFlag) ){
73185       char *zV;
73186       if( negFlag ){ value = c==2 ? SMALLEST_INT64 : -value; }
73187       zV = dup8bytes(v, (char*)&value);
73188       sqlite3VdbeAddOp4(v, OP_Int64, 0, iMem, 0, zV, P4_INT64);
73189     }else{
73190 #ifdef SQLITE_OMIT_FLOATING_POINT
73191       sqlite3ErrorMsg(pParse, "oversized integer: %s%s", negFlag ? "-" : "", z);
73192 #else
73193       codeReal(v, z, negFlag, iMem);
73194 #endif
73195     }
73196   }
73197 }
73198 
73199 /*
73200 ** Clear a cache entry.
73201 */
73202 static void cacheEntryClear(Parse *pParse, struct yColCache *p){
73203   if( p->tempReg ){
73204     if( pParse->nTempReg<ArraySize(pParse->aTempReg) ){
73205       pParse->aTempReg[pParse->nTempReg++] = p->iReg;
73206     }
73207     p->tempReg = 0;
73208   }
73209 }
73210 
73211 
73212 /*
73213 ** Record in the column cache that a particular column from a
73214 ** particular table is stored in a particular register.
73215 */
73216 SQLITE_PRIVATE void sqlite3ExprCacheStore(Parse *pParse, int iTab, int iCol, int iReg){
73217   int i;
73218   int minLru;
73219   int idxLru;
73220   struct yColCache *p;
73221 
73222   assert( iReg>0 );  /* Register numbers are always positive */
73223   assert( iCol>=-1 && iCol<32768 );  /* Finite column numbers */
73224 
73225   /* The SQLITE_ColumnCache flag disables the column cache.  This is used
73226   ** for testing only - to verify that SQLite always gets the same answer
73227   ** with and without the column cache.
73228   */
73229   if( pParse->db->flags & SQLITE_ColumnCache ) return;
73230 
73231   /* First replace any existing entry.
73232   **
73233   ** Actually, the way the column cache is currently used, we are guaranteed
73234   ** that the object will never already be in cache.  Verify this guarantee.
73235   */
73236 #ifndef NDEBUG
73237   for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
73238 #if 0 /* This code wold remove the entry from the cache if it existed */
73239     if( p->iReg && p->iTable==iTab && p->iColumn==iCol ){
73240       cacheEntryClear(pParse, p);
73241       p->iLevel = pParse->iCacheLevel;
73242       p->iReg = iReg;
73243       p->lru = pParse->iCacheCnt++;
73244       return;
73245     }
73246 #endif
73247     assert( p->iReg==0 || p->iTable!=iTab || p->iColumn!=iCol );
73248   }
73249 #endif
73250 
73251   /* Find an empty slot and replace it */
73252   for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
73253     if( p->iReg==0 ){
73254       p->iLevel = pParse->iCacheLevel;
73255       p->iTable = iTab;
73256       p->iColumn = iCol;
73257       p->iReg = iReg;
73258       p->tempReg = 0;
73259       p->lru = pParse->iCacheCnt++;
73260       return;
73261     }
73262   }
73263 
73264   /* Replace the last recently used */
73265   minLru = 0x7fffffff;
73266   idxLru = -1;
73267   for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
73268     if( p->lru<minLru ){
73269       idxLru = i;
73270       minLru = p->lru;
73271     }
73272   }
73273   if( ALWAYS(idxLru>=0) ){
73274     p = &pParse->aColCache[idxLru];
73275     p->iLevel = pParse->iCacheLevel;
73276     p->iTable = iTab;
73277     p->iColumn = iCol;
73278     p->iReg = iReg;
73279     p->tempReg = 0;
73280     p->lru = pParse->iCacheCnt++;
73281     return;
73282   }
73283 }
73284 
73285 /*
73286 ** Indicate that registers between iReg..iReg+nReg-1 are being overwritten.
73287 ** Purge the range of registers from the column cache.
73288 */
73289 SQLITE_PRIVATE void sqlite3ExprCacheRemove(Parse *pParse, int iReg, int nReg){
73290   int i;
73291   int iLast = iReg + nReg - 1;
73292   struct yColCache *p;
73293   for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
73294     int r = p->iReg;
73295     if( r>=iReg && r<=iLast ){
73296       cacheEntryClear(pParse, p);
73297       p->iReg = 0;
73298     }
73299   }
73300 }
73301 
73302 /*
73303 ** Remember the current column cache context.  Any new entries added
73304 ** added to the column cache after this call are removed when the
73305 ** corresponding pop occurs.
73306 */
73307 SQLITE_PRIVATE void sqlite3ExprCachePush(Parse *pParse){
73308   pParse->iCacheLevel++;
73309 }
73310 
73311 /*
73312 ** Remove from the column cache any entries that were added since the
73313 ** the previous N Push operations.  In other words, restore the cache
73314 ** to the state it was in N Pushes ago.
73315 */
73316 SQLITE_PRIVATE void sqlite3ExprCachePop(Parse *pParse, int N){
73317   int i;
73318   struct yColCache *p;
73319   assert( N>0 );
73320   assert( pParse->iCacheLevel>=N );
73321   pParse->iCacheLevel -= N;
73322   for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
73323     if( p->iReg && p->iLevel>pParse->iCacheLevel ){
73324       cacheEntryClear(pParse, p);
73325       p->iReg = 0;
73326     }
73327   }
73328 }
73329 
73330 /*
73331 ** When a cached column is reused, make sure that its register is
73332 ** no longer available as a temp register.  ticket #3879:  that same
73333 ** register might be in the cache in multiple places, so be sure to
73334 ** get them all.
73335 */
73336 static void sqlite3ExprCachePinRegister(Parse *pParse, int iReg){
73337   int i;
73338   struct yColCache *p;
73339   for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
73340     if( p->iReg==iReg ){
73341       p->tempReg = 0;
73342     }
73343   }
73344 }
73345 
73346 /*
73347 ** Generate code to extract the value of the iCol-th column of a table.
73348 */
73349 SQLITE_PRIVATE void sqlite3ExprCodeGetColumnOfTable(
73350   Vdbe *v,        /* The VDBE under construction */
73351   Table *pTab,    /* The table containing the value */
73352   int iTabCur,    /* The cursor for this table */
73353   int iCol,       /* Index of the column to extract */
73354   int regOut      /* Extract the valud into this register */
73355 ){
73356   if( iCol<0 || iCol==pTab->iPKey ){
73357     sqlite3VdbeAddOp2(v, OP_Rowid, iTabCur, regOut);
73358   }else{
73359     int op = IsVirtual(pTab) ? OP_VColumn : OP_Column;
73360     sqlite3VdbeAddOp3(v, op, iTabCur, iCol, regOut);
73361   }
73362   if( iCol>=0 ){
73363     sqlite3ColumnDefault(v, pTab, iCol, regOut);
73364   }
73365 }
73366 
73367 /*
73368 ** Generate code that will extract the iColumn-th column from
73369 ** table pTab and store the column value in a register.  An effort
73370 ** is made to store the column value in register iReg, but this is
73371 ** not guaranteed.  The location of the column value is returned.
73372 **
73373 ** There must be an open cursor to pTab in iTable when this routine
73374 ** is called.  If iColumn<0 then code is generated that extracts the rowid.
73375 */
73376 SQLITE_PRIVATE int sqlite3ExprCodeGetColumn(
73377   Parse *pParse,   /* Parsing and code generating context */
73378   Table *pTab,     /* Description of the table we are reading from */
73379   int iColumn,     /* Index of the table column */
73380   int iTable,      /* The cursor pointing to the table */
73381   int iReg         /* Store results here */
73382 ){
73383   Vdbe *v = pParse->pVdbe;
73384   int i;
73385   struct yColCache *p;
73386 
73387   for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
73388     if( p->iReg>0 && p->iTable==iTable && p->iColumn==iColumn ){
73389       p->lru = pParse->iCacheCnt++;
73390       sqlite3ExprCachePinRegister(pParse, p->iReg);
73391       return p->iReg;
73392     }
73393   }
73394   assert( v!=0 );
73395   sqlite3ExprCodeGetColumnOfTable(v, pTab, iTable, iColumn, iReg);
73396   sqlite3ExprCacheStore(pParse, iTable, iColumn, iReg);
73397   return iReg;
73398 }
73399 
73400 /*
73401 ** Clear all column cache entries.
73402 */
73403 SQLITE_PRIVATE void sqlite3ExprCacheClear(Parse *pParse){
73404   int i;
73405   struct yColCache *p;
73406 
73407   for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
73408     if( p->iReg ){
73409       cacheEntryClear(pParse, p);
73410       p->iReg = 0;
73411     }
73412   }
73413 }
73414 
73415 /*
73416 ** Record the fact that an affinity change has occurred on iCount
73417 ** registers starting with iStart.
73418 */
73419 SQLITE_PRIVATE void sqlite3ExprCacheAffinityChange(Parse *pParse, int iStart, int iCount){
73420   sqlite3ExprCacheRemove(pParse, iStart, iCount);
73421 }
73422 
73423 /*
73424 ** Generate code to move content from registers iFrom...iFrom+nReg-1
73425 ** over to iTo..iTo+nReg-1. Keep the column cache up-to-date.
73426 */
73427 SQLITE_PRIVATE void sqlite3ExprCodeMove(Parse *pParse, int iFrom, int iTo, int nReg){
73428   int i;
73429   struct yColCache *p;
73430   if( NEVER(iFrom==iTo) ) return;
73431   sqlite3VdbeAddOp3(pParse->pVdbe, OP_Move, iFrom, iTo, nReg);
73432   for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
73433     int x = p->iReg;
73434     if( x>=iFrom && x<iFrom+nReg ){
73435       p->iReg += iTo-iFrom;
73436     }
73437   }
73438 }
73439 
73440 /*
73441 ** Generate code to copy content from registers iFrom...iFrom+nReg-1
73442 ** over to iTo..iTo+nReg-1.
73443 */
73444 SQLITE_PRIVATE void sqlite3ExprCodeCopy(Parse *pParse, int iFrom, int iTo, int nReg){
73445   int i;
73446   if( NEVER(iFrom==iTo) ) return;
73447   for(i=0; i<nReg; i++){
73448     sqlite3VdbeAddOp2(pParse->pVdbe, OP_Copy, iFrom+i, iTo+i);
73449   }
73450 }
73451 
73452 #if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
73453 /*
73454 ** Return true if any register in the range iFrom..iTo (inclusive)
73455 ** is used as part of the column cache.
73456 **
73457 ** This routine is used within assert() and testcase() macros only
73458 ** and does not appear in a normal build.
73459 */
73460 static int usedAsColumnCache(Parse *pParse, int iFrom, int iTo){
73461   int i;
73462   struct yColCache *p;
73463   for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
73464     int r = p->iReg;
73465     if( r>=iFrom && r<=iTo ) return 1;    /*NO_TEST*/
73466   }
73467   return 0;
73468 }
73469 #endif /* SQLITE_DEBUG || SQLITE_COVERAGE_TEST */
73470 
73471 /*
73472 ** Generate code into the current Vdbe to evaluate the given
73473 ** expression.  Attempt to store the results in register "target".
73474 ** Return the register where results are stored.
73475 **
73476 ** With this routine, there is no guarantee that results will
73477 ** be stored in target.  The result might be stored in some other
73478 ** register if it is convenient to do so.  The calling function
73479 ** must check the return code and move the results to the desired
73480 ** register.
73481 */
73482 SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target){
73483   Vdbe *v = pParse->pVdbe;  /* The VM under construction */
73484   int op;                   /* The opcode being coded */
73485   int inReg = target;       /* Results stored in register inReg */
73486   int regFree1 = 0;         /* If non-zero free this temporary register */
73487   int regFree2 = 0;         /* If non-zero free this temporary register */
73488   int r1, r2, r3, r4;       /* Various register numbers */
73489   sqlite3 *db = pParse->db; /* The database connection */
73490 
73491   assert( target>0 && target<=pParse->nMem );
73492   if( v==0 ){
73493     assert( pParse->db->mallocFailed );
73494     return 0;
73495   }
73496 
73497   if( pExpr==0 ){
73498     op = TK_NULL;
73499   }else{
73500     op = pExpr->op;
73501   }
73502   switch( op ){
73503     case TK_AGG_COLUMN: {
73504       AggInfo *pAggInfo = pExpr->pAggInfo;
73505       struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg];
73506       if( !pAggInfo->directMode ){
73507         assert( pCol->iMem>0 );
73508         inReg = pCol->iMem;
73509         break;
73510       }else if( pAggInfo->useSortingIdx ){
73511         sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdx,
73512                               pCol->iSorterColumn, target);
73513         break;
73514       }
73515       /* Otherwise, fall thru into the TK_COLUMN case */
73516     }
73517     case TK_COLUMN: {
73518       if( pExpr->iTable<0 ){
73519         /* This only happens when coding check constraints */
73520         assert( pParse->ckBase>0 );
73521         inReg = pExpr->iColumn + pParse->ckBase;
73522       }else{
73523         inReg = sqlite3ExprCodeGetColumn(pParse, pExpr->pTab,
73524                                  pExpr->iColumn, pExpr->iTable, target);
73525       }
73526       break;
73527     }
73528     case TK_INTEGER: {
73529       codeInteger(pParse, pExpr, 0, target);
73530       break;
73531     }
73532 #ifndef SQLITE_OMIT_FLOATING_POINT
73533     case TK_FLOAT: {
73534       assert( !ExprHasProperty(pExpr, EP_IntValue) );
73535       codeReal(v, pExpr->u.zToken, 0, target);
73536       break;
73537     }
73538 #endif
73539     case TK_STRING: {
73540       assert( !ExprHasProperty(pExpr, EP_IntValue) );
73541       sqlite3VdbeAddOp4(v, OP_String8, 0, target, 0, pExpr->u.zToken, 0);
73542       break;
73543     }
73544     case TK_NULL: {
73545       sqlite3VdbeAddOp2(v, OP_Null, 0, target);
73546       break;
73547     }
73548 #ifndef SQLITE_OMIT_BLOB_LITERAL
73549     case TK_BLOB: {
73550       int n;
73551       const char *z;
73552       char *zBlob;
73553       assert( !ExprHasProperty(pExpr, EP_IntValue) );
73554       assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
73555       assert( pExpr->u.zToken[1]=='\'' );
73556       z = &pExpr->u.zToken[2];
73557       n = sqlite3Strlen30(z) - 1;
73558       assert( z[n]=='\'' );
73559       zBlob = sqlite3HexToBlob(sqlite3VdbeDb(v), z, n);
73560       sqlite3VdbeAddOp4(v, OP_Blob, n/2, target, 0, zBlob, P4_DYNAMIC);
73561       break;
73562     }
73563 #endif
73564     case TK_VARIABLE: {
73565       assert( !ExprHasProperty(pExpr, EP_IntValue) );
73566       assert( pExpr->u.zToken!=0 );
73567       assert( pExpr->u.zToken[0]!=0 );
73568       sqlite3VdbeAddOp2(v, OP_Variable, pExpr->iColumn, target);
73569       if( pExpr->u.zToken[1]!=0 ){
73570         assert( pExpr->u.zToken[0]=='?'
73571              || strcmp(pExpr->u.zToken, pParse->azVar[pExpr->iColumn-1])==0 );
73572         sqlite3VdbeChangeP4(v, -1, pParse->azVar[pExpr->iColumn-1], P4_STATIC);
73573       }
73574       break;
73575     }
73576     case TK_REGISTER: {
73577       inReg = pExpr->iTable;
73578       break;
73579     }
73580     case TK_AS: {
73581       inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
73582       break;
73583     }
73584 #ifndef SQLITE_OMIT_CAST
73585     case TK_CAST: {
73586       /* Expressions of the form:   CAST(pLeft AS token) */
73587       int aff, to_op;
73588       inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
73589       assert( !ExprHasProperty(pExpr, EP_IntValue) );
73590       aff = sqlite3AffinityType(pExpr->u.zToken);
73591       to_op = aff - SQLITE_AFF_TEXT + OP_ToText;
73592       assert( to_op==OP_ToText    || aff!=SQLITE_AFF_TEXT    );
73593       assert( to_op==OP_ToBlob    || aff!=SQLITE_AFF_NONE    );
73594       assert( to_op==OP_ToNumeric || aff!=SQLITE_AFF_NUMERIC );
73595       assert( to_op==OP_ToInt     || aff!=SQLITE_AFF_INTEGER );
73596       assert( to_op==OP_ToReal    || aff!=SQLITE_AFF_REAL    );
73597       testcase( to_op==OP_ToText );
73598       testcase( to_op==OP_ToBlob );
73599       testcase( to_op==OP_ToNumeric );
73600       testcase( to_op==OP_ToInt );
73601       testcase( to_op==OP_ToReal );
73602       if( inReg!=target ){
73603         sqlite3VdbeAddOp2(v, OP_SCopy, inReg, target);
73604         inReg = target;
73605       }
73606       sqlite3VdbeAddOp1(v, to_op, inReg);
73607       testcase( usedAsColumnCache(pParse, inReg, inReg) );
73608       sqlite3ExprCacheAffinityChange(pParse, inReg, 1);
73609       break;
73610     }
73611 #endif /* SQLITE_OMIT_CAST */
73612     case TK_LT:
73613     case TK_LE:
73614     case TK_GT:
73615     case TK_GE:
73616     case TK_NE:
73617     case TK_EQ: {
73618       assert( TK_LT==OP_Lt );
73619       assert( TK_LE==OP_Le );
73620       assert( TK_GT==OP_Gt );
73621       assert( TK_GE==OP_Ge );
73622       assert( TK_EQ==OP_Eq );
73623       assert( TK_NE==OP_Ne );
73624       testcase( op==TK_LT );
73625       testcase( op==TK_LE );
73626       testcase( op==TK_GT );
73627       testcase( op==TK_GE );
73628       testcase( op==TK_EQ );
73629       testcase( op==TK_NE );
73630       r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
73631       r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
73632       codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
73633                   r1, r2, inReg, SQLITE_STOREP2);
73634       testcase( regFree1==0 );
73635       testcase( regFree2==0 );
73636       break;
73637     }
73638     case TK_IS:
73639     case TK_ISNOT: {
73640       testcase( op==TK_IS );
73641       testcase( op==TK_ISNOT );
73642       r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
73643       r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
73644       op = (op==TK_IS) ? TK_EQ : TK_NE;
73645       codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
73646                   r1, r2, inReg, SQLITE_STOREP2 | SQLITE_NULLEQ);
73647       testcase( regFree1==0 );
73648       testcase( regFree2==0 );
73649       break;
73650     }
73651     case TK_AND:
73652     case TK_OR:
73653     case TK_PLUS:
73654     case TK_STAR:
73655     case TK_MINUS:
73656     case TK_REM:
73657     case TK_BITAND:
73658     case TK_BITOR:
73659     case TK_SLASH:
73660     case TK_LSHIFT:
73661     case TK_RSHIFT:
73662     case TK_CONCAT: {
73663       assert( TK_AND==OP_And );
73664       assert( TK_OR==OP_Or );
73665       assert( TK_PLUS==OP_Add );
73666       assert( TK_MINUS==OP_Subtract );
73667       assert( TK_REM==OP_Remainder );
73668       assert( TK_BITAND==OP_BitAnd );
73669       assert( TK_BITOR==OP_BitOr );
73670       assert( TK_SLASH==OP_Divide );
73671       assert( TK_LSHIFT==OP_ShiftLeft );
73672       assert( TK_RSHIFT==OP_ShiftRight );
73673       assert( TK_CONCAT==OP_Concat );
73674       testcase( op==TK_AND );
73675       testcase( op==TK_OR );
73676       testcase( op==TK_PLUS );
73677       testcase( op==TK_MINUS );
73678       testcase( op==TK_REM );
73679       testcase( op==TK_BITAND );
73680       testcase( op==TK_BITOR );
73681       testcase( op==TK_SLASH );
73682       testcase( op==TK_LSHIFT );
73683       testcase( op==TK_RSHIFT );
73684       testcase( op==TK_CONCAT );
73685       r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
73686       r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
73687       sqlite3VdbeAddOp3(v, op, r2, r1, target);
73688       testcase( regFree1==0 );
73689       testcase( regFree2==0 );
73690       break;
73691     }
73692     case TK_UMINUS: {
73693       Expr *pLeft = pExpr->pLeft;
73694       assert( pLeft );
73695       if( pLeft->op==TK_INTEGER ){
73696         codeInteger(pParse, pLeft, 1, target);
73697 #ifndef SQLITE_OMIT_FLOATING_POINT
73698       }else if( pLeft->op==TK_FLOAT ){
73699         assert( !ExprHasProperty(pExpr, EP_IntValue) );
73700         codeReal(v, pLeft->u.zToken, 1, target);
73701 #endif
73702       }else{
73703         regFree1 = r1 = sqlite3GetTempReg(pParse);
73704         sqlite3VdbeAddOp2(v, OP_Integer, 0, r1);
73705         r2 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree2);
73706         sqlite3VdbeAddOp3(v, OP_Subtract, r2, r1, target);
73707         testcase( regFree2==0 );
73708       }
73709       inReg = target;
73710       break;
73711     }
73712     case TK_BITNOT:
73713     case TK_NOT: {
73714       assert( TK_BITNOT==OP_BitNot );
73715       assert( TK_NOT==OP_Not );
73716       testcase( op==TK_BITNOT );
73717       testcase( op==TK_NOT );
73718       r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
73719       testcase( regFree1==0 );
73720       inReg = target;
73721       sqlite3VdbeAddOp2(v, op, r1, inReg);
73722       break;
73723     }
73724     case TK_ISNULL:
73725     case TK_NOTNULL: {
73726       int addr;
73727       assert( TK_ISNULL==OP_IsNull );
73728       assert( TK_NOTNULL==OP_NotNull );
73729       testcase( op==TK_ISNULL );
73730       testcase( op==TK_NOTNULL );
73731       sqlite3VdbeAddOp2(v, OP_Integer, 1, target);
73732       r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
73733       testcase( regFree1==0 );
73734       addr = sqlite3VdbeAddOp1(v, op, r1);
73735       sqlite3VdbeAddOp2(v, OP_AddImm, target, -1);
73736       sqlite3VdbeJumpHere(v, addr);
73737       break;
73738     }
73739     case TK_AGG_FUNCTION: {
73740       AggInfo *pInfo = pExpr->pAggInfo;
73741       if( pInfo==0 ){
73742         assert( !ExprHasProperty(pExpr, EP_IntValue) );
73743         sqlite3ErrorMsg(pParse, "misuse of aggregate: %s()", pExpr->u.zToken);
73744       }else{
73745         inReg = pInfo->aFunc[pExpr->iAgg].iMem;
73746       }
73747       break;
73748     }
73749     case TK_CONST_FUNC:
73750     case TK_FUNCTION: {
73751       ExprList *pFarg;       /* List of function arguments */
73752       int nFarg;             /* Number of function arguments */
73753       FuncDef *pDef;         /* The function definition object */
73754       int nId;               /* Length of the function name in bytes */
73755       const char *zId;       /* The function name */
73756       int constMask = 0;     /* Mask of function arguments that are constant */
73757       int i;                 /* Loop counter */
73758       u8 enc = ENC(db);      /* The text encoding used by this database */
73759       CollSeq *pColl = 0;    /* A collating sequence */
73760 
73761       assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
73762       testcase( op==TK_CONST_FUNC );
73763       testcase( op==TK_FUNCTION );
73764       if( ExprHasAnyProperty(pExpr, EP_TokenOnly) ){
73765         pFarg = 0;
73766       }else{
73767         pFarg = pExpr->x.pList;
73768       }
73769       nFarg = pFarg ? pFarg->nExpr : 0;
73770       assert( !ExprHasProperty(pExpr, EP_IntValue) );
73771       zId = pExpr->u.zToken;
73772       nId = sqlite3Strlen30(zId);
73773       pDef = sqlite3FindFunction(db, zId, nId, nFarg, enc, 0);
73774       if( pDef==0 ){
73775         sqlite3ErrorMsg(pParse, "unknown function: %.*s()", nId, zId);
73776         break;
73777       }
73778 
73779       /* Attempt a direct implementation of the built-in COALESCE() and
73780       ** IFNULL() functions.  This avoids unnecessary evalation of
73781       ** arguments past the first non-NULL argument.
73782       */
73783       if( pDef->flags & SQLITE_FUNC_COALESCE ){
73784         int endCoalesce = sqlite3VdbeMakeLabel(v);
73785         assert( nFarg>=2 );
73786         sqlite3ExprCode(pParse, pFarg->a[0].pExpr, target);
73787         for(i=1; i<nFarg; i++){
73788           sqlite3VdbeAddOp2(v, OP_NotNull, target, endCoalesce);
73789           sqlite3ExprCacheRemove(pParse, target, 1);
73790           sqlite3ExprCachePush(pParse);
73791           sqlite3ExprCode(pParse, pFarg->a[i].pExpr, target);
73792           sqlite3ExprCachePop(pParse, 1);
73793         }
73794         sqlite3VdbeResolveLabel(v, endCoalesce);
73795         break;
73796       }
73797 
73798 
73799       if( pFarg ){
73800         r1 = sqlite3GetTempRange(pParse, nFarg);
73801         sqlite3ExprCachePush(pParse);     /* Ticket 2ea2425d34be */
73802         sqlite3ExprCodeExprList(pParse, pFarg, r1, 1);
73803         sqlite3ExprCachePop(pParse, 1);   /* Ticket 2ea2425d34be */
73804       }else{
73805         r1 = 0;
73806       }
73807 #ifndef SQLITE_OMIT_VIRTUALTABLE
73808       /* Possibly overload the function if the first argument is
73809       ** a virtual table column.
73810       **
73811       ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the
73812       ** second argument, not the first, as the argument to test to
73813       ** see if it is a column in a virtual table.  This is done because
73814       ** the left operand of infix functions (the operand we want to
73815       ** control overloading) ends up as the second argument to the
73816       ** function.  The expression "A glob B" is equivalent to
73817       ** "glob(B,A).  We want to use the A in "A glob B" to test
73818       ** for function overloading.  But we use the B term in "glob(B,A)".
73819       */
73820       if( nFarg>=2 && (pExpr->flags & EP_InfixFunc) ){
73821         pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[1].pExpr);
73822       }else if( nFarg>0 ){
73823         pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[0].pExpr);
73824       }
73825 #endif
73826       for(i=0; i<nFarg; i++){
73827         if( i<32 && sqlite3ExprIsConstant(pFarg->a[i].pExpr) ){
73828           constMask |= (1<<i);
73829         }
73830         if( (pDef->flags & SQLITE_FUNC_NEEDCOLL)!=0 && !pColl ){
73831           pColl = sqlite3ExprCollSeq(pParse, pFarg->a[i].pExpr);
73832         }
73833       }
73834       if( pDef->flags & SQLITE_FUNC_NEEDCOLL ){
73835         if( !pColl ) pColl = db->pDfltColl;
73836         sqlite3VdbeAddOp4(v, OP_CollSeq, 0, 0, 0, (char *)pColl, P4_COLLSEQ);
73837       }
73838       sqlite3VdbeAddOp4(v, OP_Function, constMask, r1, target,
73839                         (char*)pDef, P4_FUNCDEF);
73840       sqlite3VdbeChangeP5(v, (u8)nFarg);
73841       if( nFarg ){
73842         sqlite3ReleaseTempRange(pParse, r1, nFarg);
73843       }
73844       break;
73845     }
73846 #ifndef SQLITE_OMIT_SUBQUERY
73847     case TK_EXISTS:
73848     case TK_SELECT: {
73849       testcase( op==TK_EXISTS );
73850       testcase( op==TK_SELECT );
73851       inReg = sqlite3CodeSubselect(pParse, pExpr, 0, 0);
73852       break;
73853     }
73854     case TK_IN: {
73855       int destIfFalse = sqlite3VdbeMakeLabel(v);
73856       int destIfNull = sqlite3VdbeMakeLabel(v);
73857       sqlite3VdbeAddOp2(v, OP_Null, 0, target);
73858       sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull);
73859       sqlite3VdbeAddOp2(v, OP_Integer, 1, target);
73860       sqlite3VdbeResolveLabel(v, destIfFalse);
73861       sqlite3VdbeAddOp2(v, OP_AddImm, target, 0);
73862       sqlite3VdbeResolveLabel(v, destIfNull);
73863       break;
73864     }
73865 #endif /* SQLITE_OMIT_SUBQUERY */
73866 
73867 
73868     /*
73869     **    x BETWEEN y AND z
73870     **
73871     ** This is equivalent to
73872     **
73873     **    x>=y AND x<=z
73874     **
73875     ** X is stored in pExpr->pLeft.
73876     ** Y is stored in pExpr->pList->a[0].pExpr.
73877     ** Z is stored in pExpr->pList->a[1].pExpr.
73878     */
73879     case TK_BETWEEN: {
73880       Expr *pLeft = pExpr->pLeft;
73881       struct ExprList_item *pLItem = pExpr->x.pList->a;
73882       Expr *pRight = pLItem->pExpr;
73883 
73884       r1 = sqlite3ExprCodeTemp(pParse, pLeft, &regFree1);
73885       r2 = sqlite3ExprCodeTemp(pParse, pRight, &regFree2);
73886       testcase( regFree1==0 );
73887       testcase( regFree2==0 );
73888       r3 = sqlite3GetTempReg(pParse);
73889       r4 = sqlite3GetTempReg(pParse);
73890       codeCompare(pParse, pLeft, pRight, OP_Ge,
73891                   r1, r2, r3, SQLITE_STOREP2);
73892       pLItem++;
73893       pRight = pLItem->pExpr;
73894       sqlite3ReleaseTempReg(pParse, regFree2);
73895       r2 = sqlite3ExprCodeTemp(pParse, pRight, &regFree2);
73896       testcase( regFree2==0 );
73897       codeCompare(pParse, pLeft, pRight, OP_Le, r1, r2, r4, SQLITE_STOREP2);
73898       sqlite3VdbeAddOp3(v, OP_And, r3, r4, target);
73899       sqlite3ReleaseTempReg(pParse, r3);
73900       sqlite3ReleaseTempReg(pParse, r4);
73901       break;
73902     }
73903     case TK_UPLUS: {
73904       inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
73905       break;
73906     }
73907 
73908     case TK_TRIGGER: {
73909       /* If the opcode is TK_TRIGGER, then the expression is a reference
73910       ** to a column in the new.* or old.* pseudo-tables available to
73911       ** trigger programs. In this case Expr.iTable is set to 1 for the
73912       ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
73913       ** is set to the column of the pseudo-table to read, or to -1 to
73914       ** read the rowid field.
73915       **
73916       ** The expression is implemented using an OP_Param opcode. The p1
73917       ** parameter is set to 0 for an old.rowid reference, or to (i+1)
73918       ** to reference another column of the old.* pseudo-table, where
73919       ** i is the index of the column. For a new.rowid reference, p1 is
73920       ** set to (n+1), where n is the number of columns in each pseudo-table.
73921       ** For a reference to any other column in the new.* pseudo-table, p1
73922       ** is set to (n+2+i), where n and i are as defined previously. For
73923       ** example, if the table on which triggers are being fired is
73924       ** declared as:
73925       **
73926       **   CREATE TABLE t1(a, b);
73927       **
73928       ** Then p1 is interpreted as follows:
73929       **
73930       **   p1==0   ->    old.rowid     p1==3   ->    new.rowid
73931       **   p1==1   ->    old.a         p1==4   ->    new.a
73932       **   p1==2   ->    old.b         p1==5   ->    new.b
73933       */
73934       Table *pTab = pExpr->pTab;
73935       int p1 = pExpr->iTable * (pTab->nCol+1) + 1 + pExpr->iColumn;
73936 
73937       assert( pExpr->iTable==0 || pExpr->iTable==1 );
73938       assert( pExpr->iColumn>=-1 && pExpr->iColumn<pTab->nCol );
73939       assert( pTab->iPKey<0 || pExpr->iColumn!=pTab->iPKey );
73940       assert( p1>=0 && p1<(pTab->nCol*2+2) );
73941 
73942       sqlite3VdbeAddOp2(v, OP_Param, p1, target);
73943       VdbeComment((v, "%s.%s -> $%d",
73944         (pExpr->iTable ? "new" : "old"),
73945         (pExpr->iColumn<0 ? "rowid" : pExpr->pTab->aCol[pExpr->iColumn].zName),
73946         target
73947       ));
73948 
73949 #ifndef SQLITE_OMIT_FLOATING_POINT
73950       /* If the column has REAL affinity, it may currently be stored as an
73951       ** integer. Use OP_RealAffinity to make sure it is really real.  */
73952       if( pExpr->iColumn>=0
73953        && pTab->aCol[pExpr->iColumn].affinity==SQLITE_AFF_REAL
73954       ){
73955         sqlite3VdbeAddOp1(v, OP_RealAffinity, target);
73956       }
73957 #endif
73958       break;
73959     }
73960 
73961 
73962     /*
73963     ** Form A:
73964     **   CASE x WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END
73965     **
73966     ** Form B:
73967     **   CASE WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END
73968     **
73969     ** Form A is can be transformed into the equivalent form B as follows:
73970     **   CASE WHEN x=e1 THEN r1 WHEN x=e2 THEN r2 ...
73971     **        WHEN x=eN THEN rN ELSE y END
73972     **
73973     ** X (if it exists) is in pExpr->pLeft.
73974     ** Y is in pExpr->pRight.  The Y is also optional.  If there is no
73975     ** ELSE clause and no other term matches, then the result of the
73976     ** exprssion is NULL.
73977     ** Ei is in pExpr->pList->a[i*2] and Ri is pExpr->pList->a[i*2+1].
73978     **
73979     ** The result of the expression is the Ri for the first matching Ei,
73980     ** or if there is no matching Ei, the ELSE term Y, or if there is
73981     ** no ELSE term, NULL.
73982     */
73983     default: assert( op==TK_CASE ); {
73984       int endLabel;                     /* GOTO label for end of CASE stmt */
73985       int nextCase;                     /* GOTO label for next WHEN clause */
73986       int nExpr;                        /* 2x number of WHEN terms */
73987       int i;                            /* Loop counter */
73988       ExprList *pEList;                 /* List of WHEN terms */
73989       struct ExprList_item *aListelem;  /* Array of WHEN terms */
73990       Expr opCompare;                   /* The X==Ei expression */
73991       Expr cacheX;                      /* Cached expression X */
73992       Expr *pX;                         /* The X expression */
73993       Expr *pTest = 0;                  /* X==Ei (form A) or just Ei (form B) */
73994       VVA_ONLY( int iCacheLevel = pParse->iCacheLevel; )
73995 
73996       assert( !ExprHasProperty(pExpr, EP_xIsSelect) && pExpr->x.pList );
73997       assert((pExpr->x.pList->nExpr % 2) == 0);
73998       assert(pExpr->x.pList->nExpr > 0);
73999       pEList = pExpr->x.pList;
74000       aListelem = pEList->a;
74001       nExpr = pEList->nExpr;
74002       endLabel = sqlite3VdbeMakeLabel(v);
74003       if( (pX = pExpr->pLeft)!=0 ){
74004         cacheX = *pX;
74005         testcase( pX->op==TK_COLUMN );
74006         testcase( pX->op==TK_REGISTER );
74007         cacheX.iTable = sqlite3ExprCodeTemp(pParse, pX, &regFree1);
74008         testcase( regFree1==0 );
74009         cacheX.op = TK_REGISTER;
74010         opCompare.op = TK_EQ;
74011         opCompare.pLeft = &cacheX;
74012         pTest = &opCompare;
74013         /* Ticket b351d95f9cd5ef17e9d9dbae18f5ca8611190001:
74014         ** The value in regFree1 might get SCopy-ed into the file result.
74015         ** So make sure that the regFree1 register is not reused for other
74016         ** purposes and possibly overwritten.  */
74017         regFree1 = 0;
74018       }
74019       for(i=0; i<nExpr; i=i+2){
74020         sqlite3ExprCachePush(pParse);
74021         if( pX ){
74022           assert( pTest!=0 );
74023           opCompare.pRight = aListelem[i].pExpr;
74024         }else{
74025           pTest = aListelem[i].pExpr;
74026         }
74027         nextCase = sqlite3VdbeMakeLabel(v);
74028         testcase( pTest->op==TK_COLUMN );
74029         sqlite3ExprIfFalse(pParse, pTest, nextCase, SQLITE_JUMPIFNULL);
74030         testcase( aListelem[i+1].pExpr->op==TK_COLUMN );
74031         testcase( aListelem[i+1].pExpr->op==TK_REGISTER );
74032         sqlite3ExprCode(pParse, aListelem[i+1].pExpr, target);
74033         sqlite3VdbeAddOp2(v, OP_Goto, 0, endLabel);
74034         sqlite3ExprCachePop(pParse, 1);
74035         sqlite3VdbeResolveLabel(v, nextCase);
74036       }
74037       if( pExpr->pRight ){
74038         sqlite3ExprCachePush(pParse);
74039         sqlite3ExprCode(pParse, pExpr->pRight, target);
74040         sqlite3ExprCachePop(pParse, 1);
74041       }else{
74042         sqlite3VdbeAddOp2(v, OP_Null, 0, target);
74043       }
74044       assert( db->mallocFailed || pParse->nErr>0
74045            || pParse->iCacheLevel==iCacheLevel );
74046       sqlite3VdbeResolveLabel(v, endLabel);
74047       break;
74048     }
74049 #ifndef SQLITE_OMIT_TRIGGER
74050     case TK_RAISE: {
74051       assert( pExpr->affinity==OE_Rollback
74052            || pExpr->affinity==OE_Abort
74053            || pExpr->affinity==OE_Fail
74054            || pExpr->affinity==OE_Ignore
74055       );
74056       if( !pParse->pTriggerTab ){
74057         sqlite3ErrorMsg(pParse,
74058                        "RAISE() may only be used within a trigger-program");
74059         return 0;
74060       }
74061       if( pExpr->affinity==OE_Abort ){
74062         sqlite3MayAbort(pParse);
74063       }
74064       assert( !ExprHasProperty(pExpr, EP_IntValue) );
74065       if( pExpr->affinity==OE_Ignore ){
74066         sqlite3VdbeAddOp4(
74067             v, OP_Halt, SQLITE_OK, OE_Ignore, 0, pExpr->u.zToken,0);
74068       }else{
74069         sqlite3HaltConstraint(pParse, pExpr->affinity, pExpr->u.zToken, 0);
74070       }
74071 
74072       break;
74073     }
74074 #endif
74075   }
74076   sqlite3ReleaseTempReg(pParse, regFree1);
74077   sqlite3ReleaseTempReg(pParse, regFree2);
74078   return inReg;
74079 }
74080 
74081 /*
74082 ** Generate code to evaluate an expression and store the results
74083 ** into a register.  Return the register number where the results
74084 ** are stored.
74085 **
74086 ** If the register is a temporary register that can be deallocated,
74087 ** then write its number into *pReg.  If the result register is not
74088 ** a temporary, then set *pReg to zero.
74089 */
74090 SQLITE_PRIVATE int sqlite3ExprCodeTemp(Parse *pParse, Expr *pExpr, int *pReg){
74091   int r1 = sqlite3GetTempReg(pParse);
74092   int r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1);
74093   if( r2==r1 ){
74094     *pReg = r1;
74095   }else{
74096     sqlite3ReleaseTempReg(pParse, r1);
74097     *pReg = 0;
74098   }
74099   return r2;
74100 }
74101 
74102 /*
74103 ** Generate code that will evaluate expression pExpr and store the
74104 ** results in register target.  The results are guaranteed to appear
74105 ** in register target.
74106 */
74107 SQLITE_PRIVATE int sqlite3ExprCode(Parse *pParse, Expr *pExpr, int target){
74108   int inReg;
74109 
74110   assert( target>0 && target<=pParse->nMem );
74111   if( pExpr && pExpr->op==TK_REGISTER ){
74112     sqlite3VdbeAddOp2(pParse->pVdbe, OP_Copy, pExpr->iTable, target);
74113   }else{
74114     inReg = sqlite3ExprCodeTarget(pParse, pExpr, target);
74115     assert( pParse->pVdbe || pParse->db->mallocFailed );
74116     if( inReg!=target && pParse->pVdbe ){
74117       sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, inReg, target);
74118     }
74119   }
74120   return target;
74121 }
74122 
74123 /*
74124 ** Generate code that evalutes the given expression and puts the result
74125 ** in register target.
74126 **
74127 ** Also make a copy of the expression results into another "cache" register
74128 ** and modify the expression so that the next time it is evaluated,
74129 ** the result is a copy of the cache register.
74130 **
74131 ** This routine is used for expressions that are used multiple
74132 ** times.  They are evaluated once and the results of the expression
74133 ** are reused.
74134 */
74135 SQLITE_PRIVATE int sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr, int target){
74136   Vdbe *v = pParse->pVdbe;
74137   int inReg;
74138   inReg = sqlite3ExprCode(pParse, pExpr, target);
74139   assert( target>0 );
74140   /* This routine is called for terms to INSERT or UPDATE.  And the only
74141   ** other place where expressions can be converted into TK_REGISTER is
74142   ** in WHERE clause processing.  So as currently implemented, there is
74143   ** no way for a TK_REGISTER to exist here.  But it seems prudent to
74144   ** keep the ALWAYS() in case the conditions above change with future
74145   ** modifications or enhancements. */
74146   if( ALWAYS(pExpr->op!=TK_REGISTER) ){
74147     int iMem;
74148     iMem = ++pParse->nMem;
74149     sqlite3VdbeAddOp2(v, OP_Copy, inReg, iMem);
74150     pExpr->iTable = iMem;
74151     pExpr->op2 = pExpr->op;
74152     pExpr->op = TK_REGISTER;
74153   }
74154   return inReg;
74155 }
74156 
74157 /*
74158 ** Return TRUE if pExpr is an constant expression that is appropriate
74159 ** for factoring out of a loop.  Appropriate expressions are:
74160 **
74161 **    *  Any expression that evaluates to two or more opcodes.
74162 **
74163 **    *  Any OP_Integer, OP_Real, OP_String, OP_Blob, OP_Null,
74164 **       or OP_Variable that does not need to be placed in a
74165 **       specific register.
74166 **
74167 ** There is no point in factoring out single-instruction constant
74168 ** expressions that need to be placed in a particular register.
74169 ** We could factor them out, but then we would end up adding an
74170 ** OP_SCopy instruction to move the value into the correct register
74171 ** later.  We might as well just use the original instruction and
74172 ** avoid the OP_SCopy.
74173 */
74174 static int isAppropriateForFactoring(Expr *p){
74175   if( !sqlite3ExprIsConstantNotJoin(p) ){
74176     return 0;  /* Only constant expressions are appropriate for factoring */
74177   }
74178   if( (p->flags & EP_FixedDest)==0 ){
74179     return 1;  /* Any constant without a fixed destination is appropriate */
74180   }
74181   while( p->op==TK_UPLUS ) p = p->pLeft;
74182   switch( p->op ){
74183 #ifndef SQLITE_OMIT_BLOB_LITERAL
74184     case TK_BLOB:
74185 #endif
74186     case TK_VARIABLE:
74187     case TK_INTEGER:
74188     case TK_FLOAT:
74189     case TK_NULL:
74190     case TK_STRING: {
74191       testcase( p->op==TK_BLOB );
74192       testcase( p->op==TK_VARIABLE );
74193       testcase( p->op==TK_INTEGER );
74194       testcase( p->op==TK_FLOAT );
74195       testcase( p->op==TK_NULL );
74196       testcase( p->op==TK_STRING );
74197       /* Single-instruction constants with a fixed destination are
74198       ** better done in-line.  If we factor them, they will just end
74199       ** up generating an OP_SCopy to move the value to the destination
74200       ** register. */
74201       return 0;
74202     }
74203     case TK_UMINUS: {
74204       if( p->pLeft->op==TK_FLOAT || p->pLeft->op==TK_INTEGER ){
74205         return 0;
74206       }
74207       break;
74208     }
74209     default: {
74210       break;
74211     }
74212   }
74213   return 1;
74214 }
74215 
74216 /*
74217 ** If pExpr is a constant expression that is appropriate for
74218 ** factoring out of a loop, then evaluate the expression
74219 ** into a register and convert the expression into a TK_REGISTER
74220 ** expression.
74221 */
74222 static int evalConstExpr(Walker *pWalker, Expr *pExpr){
74223   Parse *pParse = pWalker->pParse;
74224   switch( pExpr->op ){
74225     case TK_IN:
74226     case TK_REGISTER: {
74227       return WRC_Prune;
74228     }
74229     case TK_FUNCTION:
74230     case TK_AGG_FUNCTION:
74231     case TK_CONST_FUNC: {
74232       /* The arguments to a function have a fixed destination.
74233       ** Mark them this way to avoid generated unneeded OP_SCopy
74234       ** instructions.
74235       */
74236       ExprList *pList = pExpr->x.pList;
74237       assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
74238       if( pList ){
74239         int i = pList->nExpr;
74240         struct ExprList_item *pItem = pList->a;
74241         for(; i>0; i--, pItem++){
74242           if( ALWAYS(pItem->pExpr) ) pItem->pExpr->flags |= EP_FixedDest;
74243         }
74244       }
74245       break;
74246     }
74247   }
74248   if( isAppropriateForFactoring(pExpr) ){
74249     int r1 = ++pParse->nMem;
74250     int r2;
74251     r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1);
74252     if( NEVER(r1!=r2) ) sqlite3ReleaseTempReg(pParse, r1);
74253     pExpr->op2 = pExpr->op;
74254     pExpr->op = TK_REGISTER;
74255     pExpr->iTable = r2;
74256     return WRC_Prune;
74257   }
74258   return WRC_Continue;
74259 }
74260 
74261 /*
74262 ** Preevaluate constant subexpressions within pExpr and store the
74263 ** results in registers.  Modify pExpr so that the constant subexpresions
74264 ** are TK_REGISTER opcodes that refer to the precomputed values.
74265 **
74266 ** This routine is a no-op if the jump to the cookie-check code has
74267 ** already occur.  Since the cookie-check jump is generated prior to
74268 ** any other serious processing, this check ensures that there is no
74269 ** way to accidently bypass the constant initializations.
74270 **
74271 ** This routine is also a no-op if the SQLITE_FactorOutConst optimization
74272 ** is disabled via the sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS)
74273 ** interface.  This allows test logic to verify that the same answer is
74274 ** obtained for queries regardless of whether or not constants are
74275 ** precomputed into registers or if they are inserted in-line.
74276 */
74277 SQLITE_PRIVATE void sqlite3ExprCodeConstants(Parse *pParse, Expr *pExpr){
74278   Walker w;
74279   if( pParse->cookieGoto ) return;
74280   if( (pParse->db->flags & SQLITE_FactorOutConst)!=0 ) return;
74281   w.xExprCallback = evalConstExpr;
74282   w.xSelectCallback = 0;
74283   w.pParse = pParse;
74284   sqlite3WalkExpr(&w, pExpr);
74285 }
74286 
74287 
74288 /*
74289 ** Generate code that pushes the value of every element of the given
74290 ** expression list into a sequence of registers beginning at target.
74291 **
74292 ** Return the number of elements evaluated.
74293 */
74294 SQLITE_PRIVATE int sqlite3ExprCodeExprList(
74295   Parse *pParse,     /* Parsing context */
74296   ExprList *pList,   /* The expression list to be coded */
74297   int target,        /* Where to write results */
74298   int doHardCopy     /* Make a hard copy of every element */
74299 ){
74300   struct ExprList_item *pItem;
74301   int i, n;
74302   assert( pList!=0 );
74303   assert( target>0 );
74304   assert( pParse->pVdbe!=0 );  /* Never gets this far otherwise */
74305   n = pList->nExpr;
74306   for(pItem=pList->a, i=0; i<n; i++, pItem++){
74307     Expr *pExpr = pItem->pExpr;
74308     int inReg = sqlite3ExprCodeTarget(pParse, pExpr, target+i);
74309     if( inReg!=target+i ){
74310       sqlite3VdbeAddOp2(pParse->pVdbe, doHardCopy ? OP_Copy : OP_SCopy,
74311                         inReg, target+i);
74312     }
74313   }
74314   return n;
74315 }
74316 
74317 /*
74318 ** Generate code for a BETWEEN operator.
74319 **
74320 **    x BETWEEN y AND z
74321 **
74322 ** The above is equivalent to
74323 **
74324 **    x>=y AND x<=z
74325 **
74326 ** Code it as such, taking care to do the common subexpression
74327 ** elementation of x.
74328 */
74329 static void exprCodeBetween(
74330   Parse *pParse,    /* Parsing and code generating context */
74331   Expr *pExpr,      /* The BETWEEN expression */
74332   int dest,         /* Jump here if the jump is taken */
74333   int jumpIfTrue,   /* Take the jump if the BETWEEN is true */
74334   int jumpIfNull    /* Take the jump if the BETWEEN is NULL */
74335 ){
74336   Expr exprAnd;     /* The AND operator in  x>=y AND x<=z  */
74337   Expr compLeft;    /* The  x>=y  term */
74338   Expr compRight;   /* The  x<=z  term */
74339   Expr exprX;       /* The  x  subexpression */
74340   int regFree1 = 0; /* Temporary use register */
74341 
74342   assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
74343   exprX = *pExpr->pLeft;
74344   exprAnd.op = TK_AND;
74345   exprAnd.pLeft = &compLeft;
74346   exprAnd.pRight = &compRight;
74347   compLeft.op = TK_GE;
74348   compLeft.pLeft = &exprX;
74349   compLeft.pRight = pExpr->x.pList->a[0].pExpr;
74350   compRight.op = TK_LE;
74351   compRight.pLeft = &exprX;
74352   compRight.pRight = pExpr->x.pList->a[1].pExpr;
74353   exprX.iTable = sqlite3ExprCodeTemp(pParse, &exprX, &regFree1);
74354   exprX.op = TK_REGISTER;
74355   if( jumpIfTrue ){
74356     sqlite3ExprIfTrue(pParse, &exprAnd, dest, jumpIfNull);
74357   }else{
74358     sqlite3ExprIfFalse(pParse, &exprAnd, dest, jumpIfNull);
74359   }
74360   sqlite3ReleaseTempReg(pParse, regFree1);
74361 
74362   /* Ensure adequate test coverage */
74363   testcase( jumpIfTrue==0 && jumpIfNull==0 && regFree1==0 );
74364   testcase( jumpIfTrue==0 && jumpIfNull==0 && regFree1!=0 );
74365   testcase( jumpIfTrue==0 && jumpIfNull!=0 && regFree1==0 );
74366   testcase( jumpIfTrue==0 && jumpIfNull!=0 && regFree1!=0 );
74367   testcase( jumpIfTrue!=0 && jumpIfNull==0 && regFree1==0 );
74368   testcase( jumpIfTrue!=0 && jumpIfNull==0 && regFree1!=0 );
74369   testcase( jumpIfTrue!=0 && jumpIfNull!=0 && regFree1==0 );
74370   testcase( jumpIfTrue!=0 && jumpIfNull!=0 && regFree1!=0 );
74371 }
74372 
74373 /*
74374 ** Generate code for a boolean expression such that a jump is made
74375 ** to the label "dest" if the expression is true but execution
74376 ** continues straight thru if the expression is false.
74377 **
74378 ** If the expression evaluates to NULL (neither true nor false), then
74379 ** take the jump if the jumpIfNull flag is SQLITE_JUMPIFNULL.
74380 **
74381 ** This code depends on the fact that certain token values (ex: TK_EQ)
74382 ** are the same as opcode values (ex: OP_Eq) that implement the corresponding
74383 ** operation.  Special comments in vdbe.c and the mkopcodeh.awk script in
74384 ** the make process cause these values to align.  Assert()s in the code
74385 ** below verify that the numbers are aligned correctly.
74386 */
74387 SQLITE_PRIVATE void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
74388   Vdbe *v = pParse->pVdbe;
74389   int op = 0;
74390   int regFree1 = 0;
74391   int regFree2 = 0;
74392   int r1, r2;
74393 
74394   assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 );
74395   if( NEVER(v==0) )     return;  /* Existance of VDBE checked by caller */
74396   if( NEVER(pExpr==0) ) return;  /* No way this can happen */
74397   op = pExpr->op;
74398   switch( op ){
74399     case TK_AND: {
74400       int d2 = sqlite3VdbeMakeLabel(v);
74401       testcase( jumpIfNull==0 );
74402       sqlite3ExprCachePush(pParse);
74403       sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2,jumpIfNull^SQLITE_JUMPIFNULL);
74404       sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
74405       sqlite3VdbeResolveLabel(v, d2);
74406       sqlite3ExprCachePop(pParse, 1);
74407       break;
74408     }
74409     case TK_OR: {
74410       testcase( jumpIfNull==0 );
74411       sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
74412       sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
74413       break;
74414     }
74415     case TK_NOT: {
74416       testcase( jumpIfNull==0 );
74417       sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
74418       break;
74419     }
74420     case TK_LT:
74421     case TK_LE:
74422     case TK_GT:
74423     case TK_GE:
74424     case TK_NE:
74425     case TK_EQ: {
74426       assert( TK_LT==OP_Lt );
74427       assert( TK_LE==OP_Le );
74428       assert( TK_GT==OP_Gt );
74429       assert( TK_GE==OP_Ge );
74430       assert( TK_EQ==OP_Eq );
74431       assert( TK_NE==OP_Ne );
74432       testcase( op==TK_LT );
74433       testcase( op==TK_LE );
74434       testcase( op==TK_GT );
74435       testcase( op==TK_GE );
74436       testcase( op==TK_EQ );
74437       testcase( op==TK_NE );
74438       testcase( jumpIfNull==0 );
74439       r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
74440       r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
74441       codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
74442                   r1, r2, dest, jumpIfNull);
74443       testcase( regFree1==0 );
74444       testcase( regFree2==0 );
74445       break;
74446     }
74447     case TK_IS:
74448     case TK_ISNOT: {
74449       testcase( op==TK_IS );
74450       testcase( op==TK_ISNOT );
74451       r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
74452       r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
74453       op = (op==TK_IS) ? TK_EQ : TK_NE;
74454       codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
74455                   r1, r2, dest, SQLITE_NULLEQ);
74456       testcase( regFree1==0 );
74457       testcase( regFree2==0 );
74458       break;
74459     }
74460     case TK_ISNULL:
74461     case TK_NOTNULL: {
74462       assert( TK_ISNULL==OP_IsNull );
74463       assert( TK_NOTNULL==OP_NotNull );
74464       testcase( op==TK_ISNULL );
74465       testcase( op==TK_NOTNULL );
74466       r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
74467       sqlite3VdbeAddOp2(v, op, r1, dest);
74468       testcase( regFree1==0 );
74469       break;
74470     }
74471     case TK_BETWEEN: {
74472       testcase( jumpIfNull==0 );
74473       exprCodeBetween(pParse, pExpr, dest, 1, jumpIfNull);
74474       break;
74475     }
74476 #ifndef SQLITE_OMIT_SUBQUERY
74477     case TK_IN: {
74478       int destIfFalse = sqlite3VdbeMakeLabel(v);
74479       int destIfNull = jumpIfNull ? dest : destIfFalse;
74480       sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull);
74481       sqlite3VdbeAddOp2(v, OP_Goto, 0, dest);
74482       sqlite3VdbeResolveLabel(v, destIfFalse);
74483       break;
74484     }
74485 #endif
74486     default: {
74487       r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
74488       sqlite3VdbeAddOp3(v, OP_If, r1, dest, jumpIfNull!=0);
74489       testcase( regFree1==0 );
74490       testcase( jumpIfNull==0 );
74491       break;
74492     }
74493   }
74494   sqlite3ReleaseTempReg(pParse, regFree1);
74495   sqlite3ReleaseTempReg(pParse, regFree2);
74496 }
74497 
74498 /*
74499 ** Generate code for a boolean expression such that a jump is made
74500 ** to the label "dest" if the expression is false but execution
74501 ** continues straight thru if the expression is true.
74502 **
74503 ** If the expression evaluates to NULL (neither true nor false) then
74504 ** jump if jumpIfNull is SQLITE_JUMPIFNULL or fall through if jumpIfNull
74505 ** is 0.
74506 */
74507 SQLITE_PRIVATE void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
74508   Vdbe *v = pParse->pVdbe;
74509   int op = 0;
74510   int regFree1 = 0;
74511   int regFree2 = 0;
74512   int r1, r2;
74513 
74514   assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 );
74515   if( NEVER(v==0) ) return; /* Existance of VDBE checked by caller */
74516   if( pExpr==0 )    return;
74517 
74518   /* The value of pExpr->op and op are related as follows:
74519   **
74520   **       pExpr->op            op
74521   **       ---------          ----------
74522   **       TK_ISNULL          OP_NotNull
74523   **       TK_NOTNULL         OP_IsNull
74524   **       TK_NE              OP_Eq
74525   **       TK_EQ              OP_Ne
74526   **       TK_GT              OP_Le
74527   **       TK_LE              OP_Gt
74528   **       TK_GE              OP_Lt
74529   **       TK_LT              OP_Ge
74530   **
74531   ** For other values of pExpr->op, op is undefined and unused.
74532   ** The value of TK_ and OP_ constants are arranged such that we
74533   ** can compute the mapping above using the following expression.
74534   ** Assert()s verify that the computation is correct.
74535   */
74536   op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
74537 
74538   /* Verify correct alignment of TK_ and OP_ constants
74539   */
74540   assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
74541   assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
74542   assert( pExpr->op!=TK_NE || op==OP_Eq );
74543   assert( pExpr->op!=TK_EQ || op==OP_Ne );
74544   assert( pExpr->op!=TK_LT || op==OP_Ge );
74545   assert( pExpr->op!=TK_LE || op==OP_Gt );
74546   assert( pExpr->op!=TK_GT || op==OP_Le );
74547   assert( pExpr->op!=TK_GE || op==OP_Lt );
74548 
74549   switch( pExpr->op ){
74550     case TK_AND: {
74551       testcase( jumpIfNull==0 );
74552       sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
74553       sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
74554       break;
74555     }
74556     case TK_OR: {
74557       int d2 = sqlite3VdbeMakeLabel(v);
74558       testcase( jumpIfNull==0 );
74559       sqlite3ExprCachePush(pParse);
74560       sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, jumpIfNull^SQLITE_JUMPIFNULL);
74561       sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
74562       sqlite3VdbeResolveLabel(v, d2);
74563       sqlite3ExprCachePop(pParse, 1);
74564       break;
74565     }
74566     case TK_NOT: {
74567       testcase( jumpIfNull==0 );
74568       sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
74569       break;
74570     }
74571     case TK_LT:
74572     case TK_LE:
74573     case TK_GT:
74574     case TK_GE:
74575     case TK_NE:
74576     case TK_EQ: {
74577       testcase( op==TK_LT );
74578       testcase( op==TK_LE );
74579       testcase( op==TK_GT );
74580       testcase( op==TK_GE );
74581       testcase( op==TK_EQ );
74582       testcase( op==TK_NE );
74583       testcase( jumpIfNull==0 );
74584       r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
74585       r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
74586       codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
74587                   r1, r2, dest, jumpIfNull);
74588       testcase( regFree1==0 );
74589       testcase( regFree2==0 );
74590       break;
74591     }
74592     case TK_IS:
74593     case TK_ISNOT: {
74594       testcase( pExpr->op==TK_IS );
74595       testcase( pExpr->op==TK_ISNOT );
74596       r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
74597       r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
74598       op = (pExpr->op==TK_IS) ? TK_NE : TK_EQ;
74599       codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
74600                   r1, r2, dest, SQLITE_NULLEQ);
74601       testcase( regFree1==0 );
74602       testcase( regFree2==0 );
74603       break;
74604     }
74605     case TK_ISNULL:
74606     case TK_NOTNULL: {
74607       testcase( op==TK_ISNULL );
74608       testcase( op==TK_NOTNULL );
74609       r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
74610       sqlite3VdbeAddOp2(v, op, r1, dest);
74611       testcase( regFree1==0 );
74612       break;
74613     }
74614     case TK_BETWEEN: {
74615       testcase( jumpIfNull==0 );
74616       exprCodeBetween(pParse, pExpr, dest, 0, jumpIfNull);
74617       break;
74618     }
74619 #ifndef SQLITE_OMIT_SUBQUERY
74620     case TK_IN: {
74621       if( jumpIfNull ){
74622         sqlite3ExprCodeIN(pParse, pExpr, dest, dest);
74623       }else{
74624         int destIfNull = sqlite3VdbeMakeLabel(v);
74625         sqlite3ExprCodeIN(pParse, pExpr, dest, destIfNull);
74626         sqlite3VdbeResolveLabel(v, destIfNull);
74627       }
74628       break;
74629     }
74630 #endif
74631     default: {
74632       r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
74633       sqlite3VdbeAddOp3(v, OP_IfNot, r1, dest, jumpIfNull!=0);
74634       testcase( regFree1==0 );
74635       testcase( jumpIfNull==0 );
74636       break;
74637     }
74638   }
74639   sqlite3ReleaseTempReg(pParse, regFree1);
74640   sqlite3ReleaseTempReg(pParse, regFree2);
74641 }
74642 
74643 /*
74644 ** Do a deep comparison of two expression trees.  Return 0 if the two
74645 ** expressions are completely identical.  Return 1 if they differ only
74646 ** by a COLLATE operator at the top level.  Return 2 if there are differences
74647 ** other than the top-level COLLATE operator.
74648 **
74649 ** Sometimes this routine will return 2 even if the two expressions
74650 ** really are equivalent.  If we cannot prove that the expressions are
74651 ** identical, we return 2 just to be safe.  So if this routine
74652 ** returns 2, then you do not really know for certain if the two
74653 ** expressions are the same.  But if you get a 0 or 1 return, then you
74654 ** can be sure the expressions are the same.  In the places where
74655 ** this routine is used, it does not hurt to get an extra 2 - that
74656 ** just might result in some slightly slower code.  But returning
74657 ** an incorrect 0 or 1 could lead to a malfunction.
74658 */
74659 SQLITE_PRIVATE int sqlite3ExprCompare(Expr *pA, Expr *pB){
74660   if( pA==0||pB==0 ){
74661     return pB==pA ? 0 : 2;
74662   }
74663   assert( !ExprHasAnyProperty(pA, EP_TokenOnly|EP_Reduced) );
74664   assert( !ExprHasAnyProperty(pB, EP_TokenOnly|EP_Reduced) );
74665   if( ExprHasProperty(pA, EP_xIsSelect) || ExprHasProperty(pB, EP_xIsSelect) ){
74666     return 2;
74667   }
74668   if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 2;
74669   if( pA->op!=pB->op ) return 2;
74670   if( sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 2;
74671   if( sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 2;
74672   if( sqlite3ExprListCompare(pA->x.pList, pB->x.pList) ) return 2;
74673   if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 2;
74674   if( ExprHasProperty(pA, EP_IntValue) ){
74675     if( !ExprHasProperty(pB, EP_IntValue) || pA->u.iValue!=pB->u.iValue ){
74676       return 2;
74677     }
74678   }else if( pA->op!=TK_COLUMN && pA->u.zToken ){
74679     if( ExprHasProperty(pB, EP_IntValue) || NEVER(pB->u.zToken==0) ) return 2;
74680     if( sqlite3StrICmp(pA->u.zToken,pB->u.zToken)!=0 ){
74681       return 2;
74682     }
74683   }
74684   if( (pA->flags & EP_ExpCollate)!=(pB->flags & EP_ExpCollate) ) return 1;
74685   if( (pA->flags & EP_ExpCollate)!=0 && pA->pColl!=pB->pColl ) return 2;
74686   return 0;
74687 }
74688 
74689 /*
74690 ** Compare two ExprList objects.  Return 0 if they are identical and
74691 ** non-zero if they differ in any way.
74692 **
74693 ** This routine might return non-zero for equivalent ExprLists.  The
74694 ** only consequence will be disabled optimizations.  But this routine
74695 ** must never return 0 if the two ExprList objects are different, or
74696 ** a malfunction will result.
74697 **
74698 ** Two NULL pointers are considered to be the same.  But a NULL pointer
74699 ** always differs from a non-NULL pointer.
74700 */
74701 SQLITE_PRIVATE int sqlite3ExprListCompare(ExprList *pA, ExprList *pB){
74702   int i;
74703   if( pA==0 && pB==0 ) return 0;
74704   if( pA==0 || pB==0 ) return 1;
74705   if( pA->nExpr!=pB->nExpr ) return 1;
74706   for(i=0; i<pA->nExpr; i++){
74707     Expr *pExprA = pA->a[i].pExpr;
74708     Expr *pExprB = pB->a[i].pExpr;
74709     if( pA->a[i].sortOrder!=pB->a[i].sortOrder ) return 1;
74710     if( sqlite3ExprCompare(pExprA, pExprB) ) return 1;
74711   }
74712   return 0;
74713 }
74714 
74715 /*
74716 ** Add a new element to the pAggInfo->aCol[] array.  Return the index of
74717 ** the new element.  Return a negative number if malloc fails.
74718 */
74719 static int addAggInfoColumn(sqlite3 *db, AggInfo *pInfo){
74720   int i;
74721   pInfo->aCol = sqlite3ArrayAllocate(
74722        db,
74723        pInfo->aCol,
74724        sizeof(pInfo->aCol[0]),
74725        3,
74726        &pInfo->nColumn,
74727        &pInfo->nColumnAlloc,
74728        &i
74729   );
74730   return i;
74731 }
74732 
74733 /*
74734 ** Add a new element to the pAggInfo->aFunc[] array.  Return the index of
74735 ** the new element.  Return a negative number if malloc fails.
74736 */
74737 static int addAggInfoFunc(sqlite3 *db, AggInfo *pInfo){
74738   int i;
74739   pInfo->aFunc = sqlite3ArrayAllocate(
74740        db,
74741        pInfo->aFunc,
74742        sizeof(pInfo->aFunc[0]),
74743        3,
74744        &pInfo->nFunc,
74745        &pInfo->nFuncAlloc,
74746        &i
74747   );
74748   return i;
74749 }
74750 
74751 /*
74752 ** This is the xExprCallback for a tree walker.  It is used to
74753 ** implement sqlite3ExprAnalyzeAggregates().  See sqlite3ExprAnalyzeAggregates
74754 ** for additional information.
74755 */
74756 static int analyzeAggregate(Walker *pWalker, Expr *pExpr){
74757   int i;
74758   NameContext *pNC = pWalker->u.pNC;
74759   Parse *pParse = pNC->pParse;
74760   SrcList *pSrcList = pNC->pSrcList;
74761   AggInfo *pAggInfo = pNC->pAggInfo;
74762 
74763   switch( pExpr->op ){
74764     case TK_AGG_COLUMN:
74765     case TK_COLUMN: {
74766       testcase( pExpr->op==TK_AGG_COLUMN );
74767       testcase( pExpr->op==TK_COLUMN );
74768       /* Check to see if the column is in one of the tables in the FROM
74769       ** clause of the aggregate query */
74770       if( ALWAYS(pSrcList!=0) ){
74771         struct SrcList_item *pItem = pSrcList->a;
74772         for(i=0; i<pSrcList->nSrc; i++, pItem++){
74773           struct AggInfo_col *pCol;
74774           assert( !ExprHasAnyProperty(pExpr, EP_TokenOnly|EP_Reduced) );
74775           if( pExpr->iTable==pItem->iCursor ){
74776             /* If we reach this point, it means that pExpr refers to a table
74777             ** that is in the FROM clause of the aggregate query.
74778             **
74779             ** Make an entry for the column in pAggInfo->aCol[] if there
74780             ** is not an entry there already.
74781             */
74782             int k;
74783             pCol = pAggInfo->aCol;
74784             for(k=0; k<pAggInfo->nColumn; k++, pCol++){
74785               if( pCol->iTable==pExpr->iTable &&
74786                   pCol->iColumn==pExpr->iColumn ){
74787                 break;
74788               }
74789             }
74790             if( (k>=pAggInfo->nColumn)
74791              && (k = addAggInfoColumn(pParse->db, pAggInfo))>=0
74792             ){
74793               pCol = &pAggInfo->aCol[k];
74794               pCol->pTab = pExpr->pTab;
74795               pCol->iTable = pExpr->iTable;
74796               pCol->iColumn = pExpr->iColumn;
74797               pCol->iMem = ++pParse->nMem;
74798               pCol->iSorterColumn = -1;
74799               pCol->pExpr = pExpr;
74800               if( pAggInfo->pGroupBy ){
74801                 int j, n;
74802                 ExprList *pGB = pAggInfo->pGroupBy;
74803                 struct ExprList_item *pTerm = pGB->a;
74804                 n = pGB->nExpr;
74805                 for(j=0; j<n; j++, pTerm++){
74806                   Expr *pE = pTerm->pExpr;
74807                   if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable &&
74808                       pE->iColumn==pExpr->iColumn ){
74809                     pCol->iSorterColumn = j;
74810                     break;
74811                   }
74812                 }
74813               }
74814               if( pCol->iSorterColumn<0 ){
74815                 pCol->iSorterColumn = pAggInfo->nSortingColumn++;
74816               }
74817             }
74818             /* There is now an entry for pExpr in pAggInfo->aCol[] (either
74819             ** because it was there before or because we just created it).
74820             ** Convert the pExpr to be a TK_AGG_COLUMN referring to that
74821             ** pAggInfo->aCol[] entry.
74822             */
74823             ExprSetIrreducible(pExpr);
74824             pExpr->pAggInfo = pAggInfo;
74825             pExpr->op = TK_AGG_COLUMN;
74826             pExpr->iAgg = (i16)k;
74827             break;
74828           } /* endif pExpr->iTable==pItem->iCursor */
74829         } /* end loop over pSrcList */
74830       }
74831       return WRC_Prune;
74832     }
74833     case TK_AGG_FUNCTION: {
74834       /* The pNC->nDepth==0 test causes aggregate functions in subqueries
74835       ** to be ignored */
74836       if( pNC->nDepth==0 ){
74837         /* Check to see if pExpr is a duplicate of another aggregate
74838         ** function that is already in the pAggInfo structure
74839         */
74840         struct AggInfo_func *pItem = pAggInfo->aFunc;
74841         for(i=0; i<pAggInfo->nFunc; i++, pItem++){
74842           if( sqlite3ExprCompare(pItem->pExpr, pExpr)==0 ){
74843             break;
74844           }
74845         }
74846         if( i>=pAggInfo->nFunc ){
74847           /* pExpr is original.  Make a new entry in pAggInfo->aFunc[]
74848           */
74849           u8 enc = ENC(pParse->db);
74850           i = addAggInfoFunc(pParse->db, pAggInfo);
74851           if( i>=0 ){
74852             assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
74853             pItem = &pAggInfo->aFunc[i];
74854             pItem->pExpr = pExpr;
74855             pItem->iMem = ++pParse->nMem;
74856             assert( !ExprHasProperty(pExpr, EP_IntValue) );
74857             pItem->pFunc = sqlite3FindFunction(pParse->db,
74858                    pExpr->u.zToken, sqlite3Strlen30(pExpr->u.zToken),
74859                    pExpr->x.pList ? pExpr->x.pList->nExpr : 0, enc, 0);
74860             if( pExpr->flags & EP_Distinct ){
74861               pItem->iDistinct = pParse->nTab++;
74862             }else{
74863               pItem->iDistinct = -1;
74864             }
74865           }
74866         }
74867         /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry
74868         */
74869         assert( !ExprHasAnyProperty(pExpr, EP_TokenOnly|EP_Reduced) );
74870         ExprSetIrreducible(pExpr);
74871         pExpr->iAgg = (i16)i;
74872         pExpr->pAggInfo = pAggInfo;
74873         return WRC_Prune;
74874       }
74875     }
74876   }
74877   return WRC_Continue;
74878 }
74879 static int analyzeAggregatesInSelect(Walker *pWalker, Select *pSelect){
74880   NameContext *pNC = pWalker->u.pNC;
74881   if( pNC->nDepth==0 ){
74882     pNC->nDepth++;
74883     sqlite3WalkSelect(pWalker, pSelect);
74884     pNC->nDepth--;
74885     return WRC_Prune;
74886   }else{
74887     return WRC_Continue;
74888   }
74889 }
74890 
74891 /*
74892 ** Analyze the given expression looking for aggregate functions and
74893 ** for variables that need to be added to the pParse->aAgg[] array.
74894 ** Make additional entries to the pParse->aAgg[] array as necessary.
74895 **
74896 ** This routine should only be called after the expression has been
74897 ** analyzed by sqlite3ResolveExprNames().
74898 */
74899 SQLITE_PRIVATE void sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){
74900   Walker w;
74901   w.xExprCallback = analyzeAggregate;
74902   w.xSelectCallback = analyzeAggregatesInSelect;
74903   w.u.pNC = pNC;
74904   assert( pNC->pSrcList!=0 );
74905   sqlite3WalkExpr(&w, pExpr);
74906 }
74907 
74908 /*
74909 ** Call sqlite3ExprAnalyzeAggregates() for every expression in an
74910 ** expression list.  Return the number of errors.
74911 **
74912 ** If an error is found, the analysis is cut short.
74913 */
74914 SQLITE_PRIVATE void sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){
74915   struct ExprList_item *pItem;
74916   int i;
74917   if( pList ){
74918     for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
74919       sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr);
74920     }
74921   }
74922 }
74923 
74924 /*
74925 ** Allocate a single new register for use to hold some intermediate result.
74926 */
74927 SQLITE_PRIVATE int sqlite3GetTempReg(Parse *pParse){
74928   if( pParse->nTempReg==0 ){
74929     return ++pParse->nMem;
74930   }
74931   return pParse->aTempReg[--pParse->nTempReg];
74932 }
74933 
74934 /*
74935 ** Deallocate a register, making available for reuse for some other
74936 ** purpose.
74937 **
74938 ** If a register is currently being used by the column cache, then
74939 ** the dallocation is deferred until the column cache line that uses
74940 ** the register becomes stale.
74941 */
74942 SQLITE_PRIVATE void sqlite3ReleaseTempReg(Parse *pParse, int iReg){
74943   if( iReg && pParse->nTempReg<ArraySize(pParse->aTempReg) ){
74944     int i;
74945     struct yColCache *p;
74946     for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
74947       if( p->iReg==iReg ){
74948         p->tempReg = 1;
74949         return;
74950       }
74951     }
74952     pParse->aTempReg[pParse->nTempReg++] = iReg;
74953   }
74954 }
74955 
74956 /*
74957 ** Allocate or deallocate a block of nReg consecutive registers
74958 */
74959 SQLITE_PRIVATE int sqlite3GetTempRange(Parse *pParse, int nReg){
74960   int i, n;
74961   i = pParse->iRangeReg;
74962   n = pParse->nRangeReg;
74963   if( nReg<=n ){
74964     assert( !usedAsColumnCache(pParse, i, i+n-1) );
74965     pParse->iRangeReg += nReg;
74966     pParse->nRangeReg -= nReg;
74967   }else{
74968     i = pParse->nMem+1;
74969     pParse->nMem += nReg;
74970   }
74971   return i;
74972 }
74973 SQLITE_PRIVATE void sqlite3ReleaseTempRange(Parse *pParse, int iReg, int nReg){
74974   sqlite3ExprCacheRemove(pParse, iReg, nReg);
74975   if( nReg>pParse->nRangeReg ){
74976     pParse->nRangeReg = nReg;
74977     pParse->iRangeReg = iReg;
74978   }
74979 }
74980 
74981 /************** End of expr.c ************************************************/
74982 /************** Begin file alter.c *******************************************/
74983 /*
74984 ** 2005 February 15
74985 **
74986 ** The author disclaims copyright to this source code.  In place of
74987 ** a legal notice, here is a blessing:
74988 **
74989 **    May you do good and not evil.
74990 **    May you find forgiveness for yourself and forgive others.
74991 **    May you share freely, never taking more than you give.
74992 **
74993 *************************************************************************
74994 ** This file contains C code routines that used to generate VDBE code
74995 ** that implements the ALTER TABLE command.
74996 */
74997 
74998 /*
74999 ** The code in this file only exists if we are not omitting the
75000 ** ALTER TABLE logic from the build.
75001 */
75002 #ifndef SQLITE_OMIT_ALTERTABLE
75003 
75004 
75005 /*
75006 ** This function is used by SQL generated to implement the
75007 ** ALTER TABLE command. The first argument is the text of a CREATE TABLE or
75008 ** CREATE INDEX command. The second is a table name. The table name in
75009 ** the CREATE TABLE or CREATE INDEX statement is replaced with the third
75010 ** argument and the result returned. Examples:
75011 **
75012 ** sqlite_rename_table('CREATE TABLE abc(a, b, c)', 'def')
75013 **     -> 'CREATE TABLE def(a, b, c)'
75014 **
75015 ** sqlite_rename_table('CREATE INDEX i ON abc(a)', 'def')
75016 **     -> 'CREATE INDEX i ON def(a, b, c)'
75017 */
75018 static void renameTableFunc(
75019   sqlite3_context *context,
75020   int NotUsed,
75021   sqlite3_value **argv
75022 ){
75023   unsigned char const *zSql = sqlite3_value_text(argv[0]);
75024   unsigned char const *zTableName = sqlite3_value_text(argv[1]);
75025 
75026   int token;
75027   Token tname;
75028   unsigned char const *zCsr = zSql;
75029   int len = 0;
75030   char *zRet;
75031 
75032   sqlite3 *db = sqlite3_context_db_handle(context);
75033 
75034   UNUSED_PARAMETER(NotUsed);
75035 
75036   /* The principle used to locate the table name in the CREATE TABLE
75037   ** statement is that the table name is the first non-space token that
75038   ** is immediately followed by a TK_LP or TK_USING token.
75039   */
75040   if( zSql ){
75041     do {
75042       if( !*zCsr ){
75043         /* Ran out of input before finding an opening bracket. Return NULL. */
75044         return;
75045       }
75046 
75047       /* Store the token that zCsr points to in tname. */
75048       tname.z = (char*)zCsr;
75049       tname.n = len;
75050 
75051       /* Advance zCsr to the next token. Store that token type in 'token',
75052       ** and its length in 'len' (to be used next iteration of this loop).
75053       */
75054       do {
75055         zCsr += len;
75056         len = sqlite3GetToken(zCsr, &token);
75057       } while( token==TK_SPACE );
75058       assert( len>0 );
75059     } while( token!=TK_LP && token!=TK_USING );
75060 
75061     zRet = sqlite3MPrintf(db, "%.*s\"%w\"%s", ((u8*)tname.z) - zSql, zSql,
75062        zTableName, tname.z+tname.n);
75063     sqlite3_result_text(context, zRet, -1, SQLITE_DYNAMIC);
75064   }
75065 }
75066 
75067 /*
75068 ** This C function implements an SQL user function that is used by SQL code
75069 ** generated by the ALTER TABLE ... RENAME command to modify the definition
75070 ** of any foreign key constraints that use the table being renamed as the
75071 ** parent table. It is passed three arguments:
75072 **
75073 **   1) The complete text of the CREATE TABLE statement being modified,
75074 **   2) The old name of the table being renamed, and
75075 **   3) The new name of the table being renamed.
75076 **
75077 ** It returns the new CREATE TABLE statement. For example:
75078 **
75079 **   sqlite_rename_parent('CREATE TABLE t1(a REFERENCES t2)', 't2', 't3')
75080 **       -> 'CREATE TABLE t1(a REFERENCES t3)'
75081 */
75082 #ifndef SQLITE_OMIT_FOREIGN_KEY
75083 static void renameParentFunc(
75084   sqlite3_context *context,
75085   int NotUsed,
75086   sqlite3_value **argv
75087 ){
75088   sqlite3 *db = sqlite3_context_db_handle(context);
75089   char *zOutput = 0;
75090   char *zResult;
75091   unsigned char const *zInput = sqlite3_value_text(argv[0]);
75092   unsigned char const *zOld = sqlite3_value_text(argv[1]);
75093   unsigned char const *zNew = sqlite3_value_text(argv[2]);
75094 
75095   unsigned const char *z;         /* Pointer to token */
75096   int n;                          /* Length of token z */
75097   int token;                      /* Type of token */
75098 
75099   UNUSED_PARAMETER(NotUsed);
75100   for(z=zInput; *z; z=z+n){
75101     n = sqlite3GetToken(z, &token);
75102     if( token==TK_REFERENCES ){
75103       char *zParent;
75104       do {
75105         z += n;
75106         n = sqlite3GetToken(z, &token);
75107       }while( token==TK_SPACE );
75108 
75109       zParent = sqlite3DbStrNDup(db, (const char *)z, n);
75110       if( zParent==0 ) break;
75111       sqlite3Dequote(zParent);
75112       if( 0==sqlite3StrICmp((const char *)zOld, zParent) ){
75113         char *zOut = sqlite3MPrintf(db, "%s%.*s\"%w\"",
75114             (zOutput?zOutput:""), z-zInput, zInput, (const char *)zNew
75115         );
75116         sqlite3DbFree(db, zOutput);
75117         zOutput = zOut;
75118         zInput = &z[n];
75119       }
75120       sqlite3DbFree(db, zParent);
75121     }
75122   }
75123 
75124   zResult = sqlite3MPrintf(db, "%s%s", (zOutput?zOutput:""), zInput),
75125   sqlite3_result_text(context, zResult, -1, SQLITE_DYNAMIC);
75126   sqlite3DbFree(db, zOutput);
75127 }
75128 #endif
75129 
75130 #ifndef SQLITE_OMIT_TRIGGER
75131 /* This function is used by SQL generated to implement the
75132 ** ALTER TABLE command. The first argument is the text of a CREATE TRIGGER
75133 ** statement. The second is a table name. The table name in the CREATE
75134 ** TRIGGER statement is replaced with the third argument and the result
75135 ** returned. This is analagous to renameTableFunc() above, except for CREATE
75136 ** TRIGGER, not CREATE INDEX and CREATE TABLE.
75137 */
75138 static void renameTriggerFunc(
75139   sqlite3_context *context,
75140   int NotUsed,
75141   sqlite3_value **argv
75142 ){
75143   unsigned char const *zSql = sqlite3_value_text(argv[0]);
75144   unsigned char const *zTableName = sqlite3_value_text(argv[1]);
75145 
75146   int token;
75147   Token tname;
75148   int dist = 3;
75149   unsigned char const *zCsr = zSql;
75150   int len = 0;
75151   char *zRet;
75152   sqlite3 *db = sqlite3_context_db_handle(context);
75153 
75154   UNUSED_PARAMETER(NotUsed);
75155 
75156   /* The principle used to locate the table name in the CREATE TRIGGER
75157   ** statement is that the table name is the first token that is immediatedly
75158   ** preceded by either TK_ON or TK_DOT and immediatedly followed by one
75159   ** of TK_WHEN, TK_BEGIN or TK_FOR.
75160   */
75161   if( zSql ){
75162     do {
75163 
75164       if( !*zCsr ){
75165         /* Ran out of input before finding the table name. Return NULL. */
75166         return;
75167       }
75168 
75169       /* Store the token that zCsr points to in tname. */
75170       tname.z = (char*)zCsr;
75171       tname.n = len;
75172 
75173       /* Advance zCsr to the next token. Store that token type in 'token',
75174       ** and its length in 'len' (to be used next iteration of this loop).
75175       */
75176       do {
75177         zCsr += len;
75178         len = sqlite3GetToken(zCsr, &token);
75179       }while( token==TK_SPACE );
75180       assert( len>0 );
75181 
75182       /* Variable 'dist' stores the number of tokens read since the most
75183       ** recent TK_DOT or TK_ON. This means that when a WHEN, FOR or BEGIN
75184       ** token is read and 'dist' equals 2, the condition stated above
75185       ** to be met.
75186       **
75187       ** Note that ON cannot be a database, table or column name, so
75188       ** there is no need to worry about syntax like
75189       ** "CREATE TRIGGER ... ON ON.ON BEGIN ..." etc.
75190       */
75191       dist++;
75192       if( token==TK_DOT || token==TK_ON ){
75193         dist = 0;
75194       }
75195     } while( dist!=2 || (token!=TK_WHEN && token!=TK_FOR && token!=TK_BEGIN) );
75196 
75197     /* Variable tname now contains the token that is the old table-name
75198     ** in the CREATE TRIGGER statement.
75199     */
75200     zRet = sqlite3MPrintf(db, "%.*s\"%w\"%s", ((u8*)tname.z) - zSql, zSql,
75201        zTableName, tname.z+tname.n);
75202     sqlite3_result_text(context, zRet, -1, SQLITE_DYNAMIC);
75203   }
75204 }
75205 #endif   /* !SQLITE_OMIT_TRIGGER */
75206 
75207 /*
75208 ** Register built-in functions used to help implement ALTER TABLE
75209 */
75210 SQLITE_PRIVATE void sqlite3AlterFunctions(void){
75211   static SQLITE_WSD FuncDef aAlterTableFuncs[] = {
75212     FUNCTION(sqlite_rename_table,   2, 0, 0, renameTableFunc),
75213 #ifndef SQLITE_OMIT_TRIGGER
75214     FUNCTION(sqlite_rename_trigger, 2, 0, 0, renameTriggerFunc),
75215 #endif
75216 #ifndef SQLITE_OMIT_FOREIGN_KEY
75217     FUNCTION(sqlite_rename_parent,  3, 0, 0, renameParentFunc),
75218 #endif
75219   };
75220   int i;
75221   FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
75222   FuncDef *aFunc = (FuncDef*)&GLOBAL(FuncDef, aAlterTableFuncs);
75223 
75224   for(i=0; i<ArraySize(aAlterTableFuncs); i++){
75225     sqlite3FuncDefInsert(pHash, &aFunc[i]);
75226   }
75227 }
75228 
75229 /*
75230 ** This function is used to create the text of expressions of the form:
75231 **
75232 **   name=<constant1> OR name=<constant2> OR ...
75233 **
75234 ** If argument zWhere is NULL, then a pointer string containing the text
75235 ** "name=<constant>" is returned, where <constant> is the quoted version
75236 ** of the string passed as argument zConstant. The returned buffer is
75237 ** allocated using sqlite3DbMalloc(). It is the responsibility of the
75238 ** caller to ensure that it is eventually freed.
75239 **
75240 ** If argument zWhere is not NULL, then the string returned is
75241 ** "<where> OR name=<constant>", where <where> is the contents of zWhere.
75242 ** In this case zWhere is passed to sqlite3DbFree() before returning.
75243 **
75244 */
75245 static char *whereOrName(sqlite3 *db, char *zWhere, char *zConstant){
75246   char *zNew;
75247   if( !zWhere ){
75248     zNew = sqlite3MPrintf(db, "name=%Q", zConstant);
75249   }else{
75250     zNew = sqlite3MPrintf(db, "%s OR name=%Q", zWhere, zConstant);
75251     sqlite3DbFree(db, zWhere);
75252   }
75253   return zNew;
75254 }
75255 
75256 #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
75257 /*
75258 ** Generate the text of a WHERE expression which can be used to select all
75259 ** tables that have foreign key constraints that refer to table pTab (i.e.
75260 ** constraints for which pTab is the parent table) from the sqlite_master
75261 ** table.
75262 */
75263 static char *whereForeignKeys(Parse *pParse, Table *pTab){
75264   FKey *p;
75265   char *zWhere = 0;
75266   for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
75267     zWhere = whereOrName(pParse->db, zWhere, p->pFrom->zName);
75268   }
75269   return zWhere;
75270 }
75271 #endif
75272 
75273 /*
75274 ** Generate the text of a WHERE expression which can be used to select all
75275 ** temporary triggers on table pTab from the sqlite_temp_master table. If
75276 ** table pTab has no temporary triggers, or is itself stored in the
75277 ** temporary database, NULL is returned.
75278 */
75279 static char *whereTempTriggers(Parse *pParse, Table *pTab){
75280   Trigger *pTrig;
75281   char *zWhere = 0;
75282   const Schema *pTempSchema = pParse->db->aDb[1].pSchema; /* Temp db schema */
75283 
75284   /* If the table is not located in the temp-db (in which case NULL is
75285   ** returned, loop through the tables list of triggers. For each trigger
75286   ** that is not part of the temp-db schema, add a clause to the WHERE
75287   ** expression being built up in zWhere.
75288   */
75289   if( pTab->pSchema!=pTempSchema ){
75290     sqlite3 *db = pParse->db;
75291     for(pTrig=sqlite3TriggerList(pParse, pTab); pTrig; pTrig=pTrig->pNext){
75292       if( pTrig->pSchema==pTempSchema ){
75293         zWhere = whereOrName(db, zWhere, pTrig->zName);
75294       }
75295     }
75296   }
75297   if( zWhere ){
75298     char *zNew = sqlite3MPrintf(pParse->db, "type='trigger' AND (%s)", zWhere);
75299     sqlite3DbFree(pParse->db, zWhere);
75300     zWhere = zNew;
75301   }
75302   return zWhere;
75303 }
75304 
75305 /*
75306 ** Generate code to drop and reload the internal representation of table
75307 ** pTab from the database, including triggers and temporary triggers.
75308 ** Argument zName is the name of the table in the database schema at
75309 ** the time the generated code is executed. This can be different from
75310 ** pTab->zName if this function is being called to code part of an
75311 ** "ALTER TABLE RENAME TO" statement.
75312 */
75313 static void reloadTableSchema(Parse *pParse, Table *pTab, const char *zName){
75314   Vdbe *v;
75315   char *zWhere;
75316   int iDb;                   /* Index of database containing pTab */
75317 #ifndef SQLITE_OMIT_TRIGGER
75318   Trigger *pTrig;
75319 #endif
75320 
75321   v = sqlite3GetVdbe(pParse);
75322   if( NEVER(v==0) ) return;
75323   assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
75324   iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
75325   assert( iDb>=0 );
75326 
75327 #ifndef SQLITE_OMIT_TRIGGER
75328   /* Drop any table triggers from the internal schema. */
75329   for(pTrig=sqlite3TriggerList(pParse, pTab); pTrig; pTrig=pTrig->pNext){
75330     int iTrigDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
75331     assert( iTrigDb==iDb || iTrigDb==1 );
75332     sqlite3VdbeAddOp4(v, OP_DropTrigger, iTrigDb, 0, 0, pTrig->zName, 0);
75333   }
75334 #endif
75335 
75336   /* Drop the table and index from the internal schema.  */
75337   sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0);
75338 
75339   /* Reload the table, index and permanent trigger schemas. */
75340   zWhere = sqlite3MPrintf(pParse->db, "tbl_name=%Q", zName);
75341   if( !zWhere ) return;
75342   sqlite3VdbeAddParseSchemaOp(v, iDb, zWhere);
75343 
75344 #ifndef SQLITE_OMIT_TRIGGER
75345   /* Now, if the table is not stored in the temp database, reload any temp
75346   ** triggers. Don't use IN(...) in case SQLITE_OMIT_SUBQUERY is defined.
75347   */
75348   if( (zWhere=whereTempTriggers(pParse, pTab))!=0 ){
75349     sqlite3VdbeAddParseSchemaOp(v, 1, zWhere);
75350   }
75351 #endif
75352 }
75353 
75354 /*
75355 ** Parameter zName is the name of a table that is about to be altered
75356 ** (either with ALTER TABLE ... RENAME TO or ALTER TABLE ... ADD COLUMN).
75357 ** If the table is a system table, this function leaves an error message
75358 ** in pParse->zErr (system tables may not be altered) and returns non-zero.
75359 **
75360 ** Or, if zName is not a system table, zero is returned.
75361 */
75362 static int isSystemTable(Parse *pParse, const char *zName){
75363   if( sqlite3Strlen30(zName)>6 && 0==sqlite3StrNICmp(zName, "sqlite_", 7) ){
75364     sqlite3ErrorMsg(pParse, "table %s may not be altered", zName);
75365     return 1;
75366   }
75367   return 0;
75368 }
75369 
75370 /*
75371 ** Generate code to implement the "ALTER TABLE xxx RENAME TO yyy"
75372 ** command.
75373 */
75374 SQLITE_PRIVATE void sqlite3AlterRenameTable(
75375   Parse *pParse,            /* Parser context. */
75376   SrcList *pSrc,            /* The table to rename. */
75377   Token *pName              /* The new table name. */
75378 ){
75379   int iDb;                  /* Database that contains the table */
75380   char *zDb;                /* Name of database iDb */
75381   Table *pTab;              /* Table being renamed */
75382   char *zName = 0;          /* NULL-terminated version of pName */
75383   sqlite3 *db = pParse->db; /* Database connection */
75384   int nTabName;             /* Number of UTF-8 characters in zTabName */
75385   const char *zTabName;     /* Original name of the table */
75386   Vdbe *v;
75387 #ifndef SQLITE_OMIT_TRIGGER
75388   char *zWhere = 0;         /* Where clause to locate temp triggers */
75389 #endif
75390   VTable *pVTab = 0;        /* Non-zero if this is a v-tab with an xRename() */
75391   int savedDbFlags;         /* Saved value of db->flags */
75392 
75393   savedDbFlags = db->flags;
75394   if( NEVER(db->mallocFailed) ) goto exit_rename_table;
75395   assert( pSrc->nSrc==1 );
75396   assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
75397 
75398   pTab = sqlite3LocateTable(pParse, 0, pSrc->a[0].zName, pSrc->a[0].zDatabase);
75399   if( !pTab ) goto exit_rename_table;
75400   iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
75401   zDb = db->aDb[iDb].zName;
75402   db->flags |= SQLITE_PreferBuiltin;
75403 
75404   /* Get a NULL terminated version of the new table name. */
75405   zName = sqlite3NameFromToken(db, pName);
75406   if( !zName ) goto exit_rename_table;
75407 
75408   /* Check that a table or index named 'zName' does not already exist
75409   ** in database iDb. If so, this is an error.
75410   */
75411   if( sqlite3FindTable(db, zName, zDb) || sqlite3FindIndex(db, zName, zDb) ){
75412     sqlite3ErrorMsg(pParse,
75413         "there is already another table or index with this name: %s", zName);
75414     goto exit_rename_table;
75415   }
75416 
75417   /* Make sure it is not a system table being altered, or a reserved name
75418   ** that the table is being renamed to.
75419   */
75420   if( SQLITE_OK!=isSystemTable(pParse, pTab->zName) ){
75421     goto exit_rename_table;
75422   }
75423   if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){ goto
75424     exit_rename_table;
75425   }
75426 
75427 #ifndef SQLITE_OMIT_VIEW
75428   if( pTab->pSelect ){
75429     sqlite3ErrorMsg(pParse, "view %s may not be altered", pTab->zName);
75430     goto exit_rename_table;
75431   }
75432 #endif
75433 
75434 #ifndef SQLITE_OMIT_AUTHORIZATION
75435   /* Invoke the authorization callback. */
75436   if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
75437     goto exit_rename_table;
75438   }
75439 #endif
75440 
75441 #ifndef SQLITE_OMIT_VIRTUALTABLE
75442   if( sqlite3ViewGetColumnNames(pParse, pTab) ){
75443     goto exit_rename_table;
75444   }
75445   if( IsVirtual(pTab) ){
75446     pVTab = sqlite3GetVTable(db, pTab);
75447     if( pVTab->pVtab->pModule->xRename==0 ){
75448       pVTab = 0;
75449     }
75450   }
75451 #endif
75452 
75453   /* Begin a transaction and code the VerifyCookie for database iDb.
75454   ** Then modify the schema cookie (since the ALTER TABLE modifies the
75455   ** schema). Open a statement transaction if the table is a virtual
75456   ** table.
75457   */
75458   v = sqlite3GetVdbe(pParse);
75459   if( v==0 ){
75460     goto exit_rename_table;
75461   }
75462   sqlite3BeginWriteOperation(pParse, pVTab!=0, iDb);
75463   sqlite3ChangeCookie(pParse, iDb);
75464 
75465   /* If this is a virtual table, invoke the xRename() function if
75466   ** one is defined. The xRename() callback will modify the names
75467   ** of any resources used by the v-table implementation (including other
75468   ** SQLite tables) that are identified by the name of the virtual table.
75469   */
75470 #ifndef SQLITE_OMIT_VIRTUALTABLE
75471   if( pVTab ){
75472     int i = ++pParse->nMem;
75473     sqlite3VdbeAddOp4(v, OP_String8, 0, i, 0, zName, 0);
75474     sqlite3VdbeAddOp4(v, OP_VRename, i, 0, 0,(const char*)pVTab, P4_VTAB);
75475     sqlite3MayAbort(pParse);
75476   }
75477 #endif
75478 
75479   /* figure out how many UTF-8 characters are in zName */
75480   zTabName = pTab->zName;
75481   nTabName = sqlite3Utf8CharLen(zTabName, -1);
75482 
75483 #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
75484   if( db->flags&SQLITE_ForeignKeys ){
75485     /* If foreign-key support is enabled, rewrite the CREATE TABLE
75486     ** statements corresponding to all child tables of foreign key constraints
75487     ** for which the renamed table is the parent table.  */
75488     if( (zWhere=whereForeignKeys(pParse, pTab))!=0 ){
75489       sqlite3NestedParse(pParse,
75490           "UPDATE \"%w\".%s SET "
75491               "sql = sqlite_rename_parent(sql, %Q, %Q) "
75492               "WHERE %s;", zDb, SCHEMA_TABLE(iDb), zTabName, zName, zWhere);
75493       sqlite3DbFree(db, zWhere);
75494     }
75495   }
75496 #endif
75497 
75498   /* Modify the sqlite_master table to use the new table name. */
75499   sqlite3NestedParse(pParse,
75500       "UPDATE %Q.%s SET "
75501 #ifdef SQLITE_OMIT_TRIGGER
75502           "sql = sqlite_rename_table(sql, %Q), "
75503 #else
75504           "sql = CASE "
75505             "WHEN type = 'trigger' THEN sqlite_rename_trigger(sql, %Q)"
75506             "ELSE sqlite_rename_table(sql, %Q) END, "
75507 #endif
75508           "tbl_name = %Q, "
75509           "name = CASE "
75510             "WHEN type='table' THEN %Q "
75511             "WHEN name LIKE 'sqlite_autoindex%%' AND type='index' THEN "
75512              "'sqlite_autoindex_' || %Q || substr(name,%d+18) "
75513             "ELSE name END "
75514       "WHERE tbl_name=%Q AND "
75515           "(type='table' OR type='index' OR type='trigger');",
75516       zDb, SCHEMA_TABLE(iDb), zName, zName, zName,
75517 #ifndef SQLITE_OMIT_TRIGGER
75518       zName,
75519 #endif
75520       zName, nTabName, zTabName
75521   );
75522 
75523 #ifndef SQLITE_OMIT_AUTOINCREMENT
75524   /* If the sqlite_sequence table exists in this database, then update
75525   ** it with the new table name.
75526   */
75527   if( sqlite3FindTable(db, "sqlite_sequence", zDb) ){
75528     sqlite3NestedParse(pParse,
75529         "UPDATE \"%w\".sqlite_sequence set name = %Q WHERE name = %Q",
75530         zDb, zName, pTab->zName);
75531   }
75532 #endif
75533 
75534 #ifndef SQLITE_OMIT_TRIGGER
75535   /* If there are TEMP triggers on this table, modify the sqlite_temp_master
75536   ** table. Don't do this if the table being ALTERed is itself located in
75537   ** the temp database.
75538   */
75539   if( (zWhere=whereTempTriggers(pParse, pTab))!=0 ){
75540     sqlite3NestedParse(pParse,
75541         "UPDATE sqlite_temp_master SET "
75542             "sql = sqlite_rename_trigger(sql, %Q), "
75543             "tbl_name = %Q "
75544             "WHERE %s;", zName, zName, zWhere);
75545     sqlite3DbFree(db, zWhere);
75546   }
75547 #endif
75548 
75549 #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
75550   if( db->flags&SQLITE_ForeignKeys ){
75551     FKey *p;
75552     for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
75553       Table *pFrom = p->pFrom;
75554       if( pFrom!=pTab ){
75555         reloadTableSchema(pParse, p->pFrom, pFrom->zName);
75556       }
75557     }
75558   }
75559 #endif
75560 
75561   /* Drop and reload the internal table schema. */
75562   reloadTableSchema(pParse, pTab, zName);
75563 
75564 exit_rename_table:
75565   sqlite3SrcListDelete(db, pSrc);
75566   sqlite3DbFree(db, zName);
75567   db->flags = savedDbFlags;
75568 }
75569 
75570 
75571 /*
75572 ** Generate code to make sure the file format number is at least minFormat.
75573 ** The generated code will increase the file format number if necessary.
75574 */
75575 SQLITE_PRIVATE void sqlite3MinimumFileFormat(Parse *pParse, int iDb, int minFormat){
75576   Vdbe *v;
75577   v = sqlite3GetVdbe(pParse);
75578   /* The VDBE should have been allocated before this routine is called.
75579   ** If that allocation failed, we would have quit before reaching this
75580   ** point */
75581   if( ALWAYS(v) ){
75582     int r1 = sqlite3GetTempReg(pParse);
75583     int r2 = sqlite3GetTempReg(pParse);
75584     int j1;
75585     sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, r1, BTREE_FILE_FORMAT);
75586     sqlite3VdbeUsesBtree(v, iDb);
75587     sqlite3VdbeAddOp2(v, OP_Integer, minFormat, r2);
75588     j1 = sqlite3VdbeAddOp3(v, OP_Ge, r2, 0, r1);
75589     sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, r2);
75590     sqlite3VdbeJumpHere(v, j1);
75591     sqlite3ReleaseTempReg(pParse, r1);
75592     sqlite3ReleaseTempReg(pParse, r2);
75593   }
75594 }
75595 
75596 /*
75597 ** This function is called after an "ALTER TABLE ... ADD" statement
75598 ** has been parsed. Argument pColDef contains the text of the new
75599 ** column definition.
75600 **
75601 ** The Table structure pParse->pNewTable was extended to include
75602 ** the new column during parsing.
75603 */
75604 SQLITE_PRIVATE void sqlite3AlterFinishAddColumn(Parse *pParse, Token *pColDef){
75605   Table *pNew;              /* Copy of pParse->pNewTable */
75606   Table *pTab;              /* Table being altered */
75607   int iDb;                  /* Database number */
75608   const char *zDb;          /* Database name */
75609   const char *zTab;         /* Table name */
75610   char *zCol;               /* Null-terminated column definition */
75611   Column *pCol;             /* The new column */
75612   Expr *pDflt;              /* Default value for the new column */
75613   sqlite3 *db;              /* The database connection; */
75614 
75615   db = pParse->db;
75616   if( pParse->nErr || db->mallocFailed ) return;
75617   pNew = pParse->pNewTable;
75618   assert( pNew );
75619 
75620   assert( sqlite3BtreeHoldsAllMutexes(db) );
75621   iDb = sqlite3SchemaToIndex(db, pNew->pSchema);
75622   zDb = db->aDb[iDb].zName;
75623   zTab = &pNew->zName[16];  /* Skip the "sqlite_altertab_" prefix on the name */
75624   pCol = &pNew->aCol[pNew->nCol-1];
75625   pDflt = pCol->pDflt;
75626   pTab = sqlite3FindTable(db, zTab, zDb);
75627   assert( pTab );
75628 
75629 #ifndef SQLITE_OMIT_AUTHORIZATION
75630   /* Invoke the authorization callback. */
75631   if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
75632     return;
75633   }
75634 #endif
75635 
75636   /* If the default value for the new column was specified with a
75637   ** literal NULL, then set pDflt to 0. This simplifies checking
75638   ** for an SQL NULL default below.
75639   */
75640   if( pDflt && pDflt->op==TK_NULL ){
75641     pDflt = 0;
75642   }
75643 
75644   /* Check that the new column is not specified as PRIMARY KEY or UNIQUE.
75645   ** If there is a NOT NULL constraint, then the default value for the
75646   ** column must not be NULL.
75647   */
75648   if( pCol->isPrimKey ){
75649     sqlite3ErrorMsg(pParse, "Cannot add a PRIMARY KEY column");
75650     return;
75651   }
75652   if( pNew->pIndex ){
75653     sqlite3ErrorMsg(pParse, "Cannot add a UNIQUE column");
75654     return;
75655   }
75656   if( (db->flags&SQLITE_ForeignKeys) && pNew->pFKey && pDflt ){
75657     sqlite3ErrorMsg(pParse,
75658         "Cannot add a REFERENCES column with non-NULL default value");
75659     return;
75660   }
75661   if( pCol->notNull && !pDflt ){
75662     sqlite3ErrorMsg(pParse,
75663         "Cannot add a NOT NULL column with default value NULL");
75664     return;
75665   }
75666 
75667   /* Ensure the default expression is something that sqlite3ValueFromExpr()
75668   ** can handle (i.e. not CURRENT_TIME etc.)
75669   */
75670   if( pDflt ){
75671     sqlite3_value *pVal;
75672     if( sqlite3ValueFromExpr(db, pDflt, SQLITE_UTF8, SQLITE_AFF_NONE, &pVal) ){
75673       db->mallocFailed = 1;
75674       return;
75675     }
75676     if( !pVal ){
75677       sqlite3ErrorMsg(pParse, "Cannot add a column with non-constant default");
75678       return;
75679     }
75680     sqlite3ValueFree(pVal);
75681   }
75682 
75683   /* Modify the CREATE TABLE statement. */
75684   zCol = sqlite3DbStrNDup(db, (char*)pColDef->z, pColDef->n);
75685   if( zCol ){
75686     char *zEnd = &zCol[pColDef->n-1];
75687     int savedDbFlags = db->flags;
75688     while( zEnd>zCol && (*zEnd==';' || sqlite3Isspace(*zEnd)) ){
75689       *zEnd-- = '\0';
75690     }
75691     db->flags |= SQLITE_PreferBuiltin;
75692     sqlite3NestedParse(pParse,
75693         "UPDATE \"%w\".%s SET "
75694           "sql = substr(sql,1,%d) || ', ' || %Q || substr(sql,%d) "
75695         "WHERE type = 'table' AND name = %Q",
75696       zDb, SCHEMA_TABLE(iDb), pNew->addColOffset, zCol, pNew->addColOffset+1,
75697       zTab
75698     );
75699     sqlite3DbFree(db, zCol);
75700     db->flags = savedDbFlags;
75701   }
75702 
75703   /* If the default value of the new column is NULL, then set the file
75704   ** format to 2. If the default value of the new column is not NULL,
75705   ** the file format becomes 3.
75706   */
75707   sqlite3MinimumFileFormat(pParse, iDb, pDflt ? 3 : 2);
75708 
75709   /* Reload the schema of the modified table. */
75710   reloadTableSchema(pParse, pTab, pTab->zName);
75711 }
75712 
75713 /*
75714 ** This function is called by the parser after the table-name in
75715 ** an "ALTER TABLE <table-name> ADD" statement is parsed. Argument
75716 ** pSrc is the full-name of the table being altered.
75717 **
75718 ** This routine makes a (partial) copy of the Table structure
75719 ** for the table being altered and sets Parse.pNewTable to point
75720 ** to it. Routines called by the parser as the column definition
75721 ** is parsed (i.e. sqlite3AddColumn()) add the new Column data to
75722 ** the copy. The copy of the Table structure is deleted by tokenize.c
75723 ** after parsing is finished.
75724 **
75725 ** Routine sqlite3AlterFinishAddColumn() will be called to complete
75726 ** coding the "ALTER TABLE ... ADD" statement.
75727 */
75728 SQLITE_PRIVATE void sqlite3AlterBeginAddColumn(Parse *pParse, SrcList *pSrc){
75729   Table *pNew;
75730   Table *pTab;
75731   Vdbe *v;
75732   int iDb;
75733   int i;
75734   int nAlloc;
75735   sqlite3 *db = pParse->db;
75736 
75737   /* Look up the table being altered. */
75738   assert( pParse->pNewTable==0 );
75739   assert( sqlite3BtreeHoldsAllMutexes(db) );
75740   if( db->mallocFailed ) goto exit_begin_add_column;
75741   pTab = sqlite3LocateTable(pParse, 0, pSrc->a[0].zName, pSrc->a[0].zDatabase);
75742   if( !pTab ) goto exit_begin_add_column;
75743 
75744 #ifndef SQLITE_OMIT_VIRTUALTABLE
75745   if( IsVirtual(pTab) ){
75746     sqlite3ErrorMsg(pParse, "virtual tables may not be altered");
75747     goto exit_begin_add_column;
75748   }
75749 #endif
75750 
75751   /* Make sure this is not an attempt to ALTER a view. */
75752   if( pTab->pSelect ){
75753     sqlite3ErrorMsg(pParse, "Cannot add a column to a view");
75754     goto exit_begin_add_column;
75755   }
75756   if( SQLITE_OK!=isSystemTable(pParse, pTab->zName) ){
75757     goto exit_begin_add_column;
75758   }
75759 
75760   assert( pTab->addColOffset>0 );
75761   iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
75762 
75763   /* Put a copy of the Table struct in Parse.pNewTable for the
75764   ** sqlite3AddColumn() function and friends to modify.  But modify
75765   ** the name by adding an "sqlite_altertab_" prefix.  By adding this
75766   ** prefix, we insure that the name will not collide with an existing
75767   ** table because user table are not allowed to have the "sqlite_"
75768   ** prefix on their name.
75769   */
75770   pNew = (Table*)sqlite3DbMallocZero(db, sizeof(Table));
75771   if( !pNew ) goto exit_begin_add_column;
75772   pParse->pNewTable = pNew;
75773   pNew->nRef = 1;
75774   pNew->nCol = pTab->nCol;
75775   assert( pNew->nCol>0 );
75776   nAlloc = (((pNew->nCol-1)/8)*8)+8;
75777   assert( nAlloc>=pNew->nCol && nAlloc%8==0 && nAlloc-pNew->nCol<8 );
75778   pNew->aCol = (Column*)sqlite3DbMallocZero(db, sizeof(Column)*nAlloc);
75779   pNew->zName = sqlite3MPrintf(db, "sqlite_altertab_%s", pTab->zName);
75780   if( !pNew->aCol || !pNew->zName ){
75781     db->mallocFailed = 1;
75782     goto exit_begin_add_column;
75783   }
75784   memcpy(pNew->aCol, pTab->aCol, sizeof(Column)*pNew->nCol);
75785   for(i=0; i<pNew->nCol; i++){
75786     Column *pCol = &pNew->aCol[i];
75787     pCol->zName = sqlite3DbStrDup(db, pCol->zName);
75788     pCol->zColl = 0;
75789     pCol->zType = 0;
75790     pCol->pDflt = 0;
75791     pCol->zDflt = 0;
75792   }
75793   pNew->pSchema = db->aDb[iDb].pSchema;
75794   pNew->addColOffset = pTab->addColOffset;
75795   pNew->nRef = 1;
75796 
75797   /* Begin a transaction and increment the schema cookie.  */
75798   sqlite3BeginWriteOperation(pParse, 0, iDb);
75799   v = sqlite3GetVdbe(pParse);
75800   if( !v ) goto exit_begin_add_column;
75801   sqlite3ChangeCookie(pParse, iDb);
75802 
75803 exit_begin_add_column:
75804   sqlite3SrcListDelete(db, pSrc);
75805   return;
75806 }
75807 #endif  /* SQLITE_ALTER_TABLE */
75808 
75809 /************** End of alter.c ***********************************************/
75810 /************** Begin file analyze.c *****************************************/
75811 /*
75812 ** 2005 July 8
75813 **
75814 ** The author disclaims copyright to this source code.  In place of
75815 ** a legal notice, here is a blessing:
75816 **
75817 **    May you do good and not evil.
75818 **    May you find forgiveness for yourself and forgive others.
75819 **    May you share freely, never taking more than you give.
75820 **
75821 *************************************************************************
75822 ** This file contains code associated with the ANALYZE command.
75823 */
75824 #ifndef SQLITE_OMIT_ANALYZE
75825 
75826 /*
75827 ** This routine generates code that opens the sqlite_stat1 table for
75828 ** writing with cursor iStatCur. If the library was built with the
75829 ** SQLITE_ENABLE_STAT2 macro defined, then the sqlite_stat2 table is
75830 ** opened for writing using cursor (iStatCur+1)
75831 **
75832 ** If the sqlite_stat1 tables does not previously exist, it is created.
75833 ** Similarly, if the sqlite_stat2 table does not exist and the library
75834 ** is compiled with SQLITE_ENABLE_STAT2 defined, it is created.
75835 **
75836 ** Argument zWhere may be a pointer to a buffer containing a table name,
75837 ** or it may be a NULL pointer. If it is not NULL, then all entries in
75838 ** the sqlite_stat1 and (if applicable) sqlite_stat2 tables associated
75839 ** with the named table are deleted. If zWhere==0, then code is generated
75840 ** to delete all stat table entries.
75841 */
75842 static void openStatTable(
75843   Parse *pParse,          /* Parsing context */
75844   int iDb,                /* The database we are looking in */
75845   int iStatCur,           /* Open the sqlite_stat1 table on this cursor */
75846   const char *zWhere,     /* Delete entries for this table or index */
75847   const char *zWhereType  /* Either "tbl" or "idx" */
75848 ){
75849   static const struct {
75850     const char *zName;
75851     const char *zCols;
75852   } aTable[] = {
75853     { "sqlite_stat1", "tbl,idx,stat" },
75854 #ifdef SQLITE_ENABLE_STAT2
75855     { "sqlite_stat2", "tbl,idx,sampleno,sample" },
75856 #endif
75857   };
75858 
75859   int aRoot[] = {0, 0};
75860   u8 aCreateTbl[] = {0, 0};
75861 
75862   int i;
75863   sqlite3 *db = pParse->db;
75864   Db *pDb;
75865   Vdbe *v = sqlite3GetVdbe(pParse);
75866   if( v==0 ) return;
75867   assert( sqlite3BtreeHoldsAllMutexes(db) );
75868   assert( sqlite3VdbeDb(v)==db );
75869   pDb = &db->aDb[iDb];
75870 
75871   for(i=0; i<ArraySize(aTable); i++){
75872     const char *zTab = aTable[i].zName;
75873     Table *pStat;
75874     if( (pStat = sqlite3FindTable(db, zTab, pDb->zName))==0 ){
75875       /* The sqlite_stat[12] table does not exist. Create it. Note that a
75876       ** side-effect of the CREATE TABLE statement is to leave the rootpage
75877       ** of the new table in register pParse->regRoot. This is important
75878       ** because the OpenWrite opcode below will be needing it. */
75879       sqlite3NestedParse(pParse,
75880           "CREATE TABLE %Q.%s(%s)", pDb->zName, zTab, aTable[i].zCols
75881       );
75882       aRoot[i] = pParse->regRoot;
75883       aCreateTbl[i] = 1;
75884     }else{
75885       /* The table already exists. If zWhere is not NULL, delete all entries
75886       ** associated with the table zWhere. If zWhere is NULL, delete the
75887       ** entire contents of the table. */
75888       aRoot[i] = pStat->tnum;
75889       sqlite3TableLock(pParse, iDb, aRoot[i], 1, zTab);
75890       if( zWhere ){
75891         sqlite3NestedParse(pParse,
75892            "DELETE FROM %Q.%s WHERE %s=%Q", pDb->zName, zTab, zWhereType, zWhere
75893         );
75894       }else{
75895         /* The sqlite_stat[12] table already exists.  Delete all rows. */
75896         sqlite3VdbeAddOp2(v, OP_Clear, aRoot[i], iDb);
75897       }
75898     }
75899   }
75900 
75901   /* Open the sqlite_stat[12] tables for writing. */
75902   for(i=0; i<ArraySize(aTable); i++){
75903     sqlite3VdbeAddOp3(v, OP_OpenWrite, iStatCur+i, aRoot[i], iDb);
75904     sqlite3VdbeChangeP4(v, -1, (char *)3, P4_INT32);
75905     sqlite3VdbeChangeP5(v, aCreateTbl[i]);
75906   }
75907 }
75908 
75909 /*
75910 ** Generate code to do an analysis of all indices associated with
75911 ** a single table.
75912 */
75913 static void analyzeOneTable(
75914   Parse *pParse,   /* Parser context */
75915   Table *pTab,     /* Table whose indices are to be analyzed */
75916   Index *pOnlyIdx, /* If not NULL, only analyze this one index */
75917   int iStatCur,    /* Index of VdbeCursor that writes the sqlite_stat1 table */
75918   int iMem         /* Available memory locations begin here */
75919 ){
75920   sqlite3 *db = pParse->db;    /* Database handle */
75921   Index *pIdx;                 /* An index to being analyzed */
75922   int iIdxCur;                 /* Cursor open on index being analyzed */
75923   Vdbe *v;                     /* The virtual machine being built up */
75924   int i;                       /* Loop counter */
75925   int topOfLoop;               /* The top of the loop */
75926   int endOfLoop;               /* The end of the loop */
75927   int jZeroRows = -1;          /* Jump from here if number of rows is zero */
75928   int iDb;                     /* Index of database containing pTab */
75929   int regTabname = iMem++;     /* Register containing table name */
75930   int regIdxname = iMem++;     /* Register containing index name */
75931   int regSampleno = iMem++;    /* Register containing next sample number */
75932   int regCol = iMem++;         /* Content of a column analyzed table */
75933   int regRec = iMem++;         /* Register holding completed record */
75934   int regTemp = iMem++;        /* Temporary use register */
75935   int regRowid = iMem++;       /* Rowid for the inserted record */
75936 
75937 #ifdef SQLITE_ENABLE_STAT2
75938   int addr = 0;                /* Instruction address */
75939   int regTemp2 = iMem++;       /* Temporary use register */
75940   int regSamplerecno = iMem++; /* Index of next sample to record */
75941   int regRecno = iMem++;       /* Current sample index */
75942   int regLast = iMem++;        /* Index of last sample to record */
75943   int regFirst = iMem++;       /* Index of first sample to record */
75944 #endif
75945 
75946   v = sqlite3GetVdbe(pParse);
75947   if( v==0 || NEVER(pTab==0) ){
75948     return;
75949   }
75950   if( pTab->tnum==0 ){
75951     /* Do not gather statistics on views or virtual tables */
75952     return;
75953   }
75954   if( memcmp(pTab->zName, "sqlite_", 7)==0 ){
75955     /* Do not gather statistics on system tables */
75956     return;
75957   }
75958   assert( sqlite3BtreeHoldsAllMutexes(db) );
75959   iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
75960   assert( iDb>=0 );
75961   assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
75962 #ifndef SQLITE_OMIT_AUTHORIZATION
75963   if( sqlite3AuthCheck(pParse, SQLITE_ANALYZE, pTab->zName, 0,
75964       db->aDb[iDb].zName ) ){
75965     return;
75966   }
75967 #endif
75968 
75969   /* Establish a read-lock on the table at the shared-cache level. */
75970   sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
75971 
75972   iIdxCur = pParse->nTab++;
75973   sqlite3VdbeAddOp4(v, OP_String8, 0, regTabname, 0, pTab->zName, 0);
75974   for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
75975     int nCol;
75976     KeyInfo *pKey;
75977 
75978     if( pOnlyIdx && pOnlyIdx!=pIdx ) continue;
75979     nCol = pIdx->nColumn;
75980     pKey = sqlite3IndexKeyinfo(pParse, pIdx);
75981     if( iMem+1+(nCol*2)>pParse->nMem ){
75982       pParse->nMem = iMem+1+(nCol*2);
75983     }
75984 
75985     /* Open a cursor to the index to be analyzed. */
75986     assert( iDb==sqlite3SchemaToIndex(db, pIdx->pSchema) );
75987     sqlite3VdbeAddOp4(v, OP_OpenRead, iIdxCur, pIdx->tnum, iDb,
75988         (char *)pKey, P4_KEYINFO_HANDOFF);
75989     VdbeComment((v, "%s", pIdx->zName));
75990 
75991     /* Populate the register containing the index name. */
75992     sqlite3VdbeAddOp4(v, OP_String8, 0, regIdxname, 0, pIdx->zName, 0);
75993 
75994 #ifdef SQLITE_ENABLE_STAT2
75995 
75996     /* If this iteration of the loop is generating code to analyze the
75997     ** first index in the pTab->pIndex list, then register regLast has
75998     ** not been populated. In this case populate it now.  */
75999     if( pTab->pIndex==pIdx ){
76000       sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES, regSamplerecno);
76001       sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES*2-1, regTemp);
76002       sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES*2, regTemp2);
76003 
76004       sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regLast);
76005       sqlite3VdbeAddOp2(v, OP_Null, 0, regFirst);
76006       addr = sqlite3VdbeAddOp3(v, OP_Lt, regSamplerecno, 0, regLast);
76007       sqlite3VdbeAddOp3(v, OP_Divide, regTemp2, regLast, regFirst);
76008       sqlite3VdbeAddOp3(v, OP_Multiply, regLast, regTemp, regLast);
76009       sqlite3VdbeAddOp2(v, OP_AddImm, regLast, SQLITE_INDEX_SAMPLES*2-2);
76010       sqlite3VdbeAddOp3(v, OP_Divide,  regTemp2, regLast, regLast);
76011       sqlite3VdbeJumpHere(v, addr);
76012     }
76013 
76014     /* Zero the regSampleno and regRecno registers. */
76015     sqlite3VdbeAddOp2(v, OP_Integer, 0, regSampleno);
76016     sqlite3VdbeAddOp2(v, OP_Integer, 0, regRecno);
76017     sqlite3VdbeAddOp2(v, OP_Copy, regFirst, regSamplerecno);
76018 #endif
76019 
76020     /* The block of memory cells initialized here is used as follows.
76021     **
76022     **    iMem:
76023     **        The total number of rows in the table.
76024     **
76025     **    iMem+1 .. iMem+nCol:
76026     **        Number of distinct entries in index considering the
76027     **        left-most N columns only, where N is between 1 and nCol,
76028     **        inclusive.
76029     **
76030     **    iMem+nCol+1 .. Mem+2*nCol:
76031     **        Previous value of indexed columns, from left to right.
76032     **
76033     ** Cells iMem through iMem+nCol are initialized to 0. The others are
76034     ** initialized to contain an SQL NULL.
76035     */
76036     for(i=0; i<=nCol; i++){
76037       sqlite3VdbeAddOp2(v, OP_Integer, 0, iMem+i);
76038     }
76039     for(i=0; i<nCol; i++){
76040       sqlite3VdbeAddOp2(v, OP_Null, 0, iMem+nCol+i+1);
76041     }
76042 
76043     /* Start the analysis loop. This loop runs through all the entries in
76044     ** the index b-tree.  */
76045     endOfLoop = sqlite3VdbeMakeLabel(v);
76046     sqlite3VdbeAddOp2(v, OP_Rewind, iIdxCur, endOfLoop);
76047     topOfLoop = sqlite3VdbeCurrentAddr(v);
76048     sqlite3VdbeAddOp2(v, OP_AddImm, iMem, 1);
76049 
76050     for(i=0; i<nCol; i++){
76051       CollSeq *pColl;
76052       sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regCol);
76053       if( i==0 ){
76054 #ifdef SQLITE_ENABLE_STAT2
76055         /* Check if the record that cursor iIdxCur points to contains a
76056         ** value that should be stored in the sqlite_stat2 table. If so,
76057         ** store it.  */
76058         int ne = sqlite3VdbeAddOp3(v, OP_Ne, regRecno, 0, regSamplerecno);
76059         assert( regTabname+1==regIdxname
76060              && regTabname+2==regSampleno
76061              && regTabname+3==regCol
76062         );
76063         sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL);
76064         sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 4, regRec, "aaab", 0);
76065         sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur+1, regRowid);
76066         sqlite3VdbeAddOp3(v, OP_Insert, iStatCur+1, regRec, regRowid);
76067 
76068         /* Calculate new values for regSamplerecno and regSampleno.
76069         **
76070         **   sampleno = sampleno + 1
76071         **   samplerecno = samplerecno+(remaining records)/(remaining samples)
76072         */
76073         sqlite3VdbeAddOp2(v, OP_AddImm, regSampleno, 1);
76074         sqlite3VdbeAddOp3(v, OP_Subtract, regRecno, regLast, regTemp);
76075         sqlite3VdbeAddOp2(v, OP_AddImm, regTemp, -1);
76076         sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES, regTemp2);
76077         sqlite3VdbeAddOp3(v, OP_Subtract, regSampleno, regTemp2, regTemp2);
76078         sqlite3VdbeAddOp3(v, OP_Divide, regTemp2, regTemp, regTemp);
76079         sqlite3VdbeAddOp3(v, OP_Add, regSamplerecno, regTemp, regSamplerecno);
76080 
76081         sqlite3VdbeJumpHere(v, ne);
76082         sqlite3VdbeAddOp2(v, OP_AddImm, regRecno, 1);
76083 #endif
76084 
76085         /* Always record the very first row */
76086         sqlite3VdbeAddOp1(v, OP_IfNot, iMem+1);
76087       }
76088       assert( pIdx->azColl!=0 );
76089       assert( pIdx->azColl[i]!=0 );
76090       pColl = sqlite3LocateCollSeq(pParse, pIdx->azColl[i]);
76091       sqlite3VdbeAddOp4(v, OP_Ne, regCol, 0, iMem+nCol+i+1,
76092                        (char*)pColl, P4_COLLSEQ);
76093       sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
76094     }
76095     if( db->mallocFailed ){
76096       /* If a malloc failure has occurred, then the result of the expression
76097       ** passed as the second argument to the call to sqlite3VdbeJumpHere()
76098       ** below may be negative. Which causes an assert() to fail (or an
76099       ** out-of-bounds write if SQLITE_DEBUG is not defined).  */
76100       return;
76101     }
76102     sqlite3VdbeAddOp2(v, OP_Goto, 0, endOfLoop);
76103     for(i=0; i<nCol; i++){
76104       int addr2 = sqlite3VdbeCurrentAddr(v) - (nCol*2);
76105       if( i==0 ){
76106         sqlite3VdbeJumpHere(v, addr2-1);  /* Set jump dest for the OP_IfNot */
76107       }
76108       sqlite3VdbeJumpHere(v, addr2);      /* Set jump dest for the OP_Ne */
76109       sqlite3VdbeAddOp2(v, OP_AddImm, iMem+i+1, 1);
76110       sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, iMem+nCol+i+1);
76111     }
76112 
76113     /* End of the analysis loop. */
76114     sqlite3VdbeResolveLabel(v, endOfLoop);
76115     sqlite3VdbeAddOp2(v, OP_Next, iIdxCur, topOfLoop);
76116     sqlite3VdbeAddOp1(v, OP_Close, iIdxCur);
76117 
76118     /* Store the results in sqlite_stat1.
76119     **
76120     ** The result is a single row of the sqlite_stat1 table.  The first
76121     ** two columns are the names of the table and index.  The third column
76122     ** is a string composed of a list of integer statistics about the
76123     ** index.  The first integer in the list is the total number of entries
76124     ** in the index.  There is one additional integer in the list for each
76125     ** column of the table.  This additional integer is a guess of how many
76126     ** rows of the table the index will select.  If D is the count of distinct
76127     ** values and K is the total number of rows, then the integer is computed
76128     ** as:
76129     **
76130     **        I = (K+D-1)/D
76131     **
76132     ** If K==0 then no entry is made into the sqlite_stat1 table.
76133     ** If K>0 then it is always the case the D>0 so division by zero
76134     ** is never possible.
76135     */
76136     sqlite3VdbeAddOp2(v, OP_SCopy, iMem, regSampleno);
76137     if( jZeroRows<0 ){
76138       jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, iMem);
76139     }
76140     for(i=0; i<nCol; i++){
76141       sqlite3VdbeAddOp4(v, OP_String8, 0, regTemp, 0, " ", 0);
76142       sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regSampleno, regSampleno);
76143       sqlite3VdbeAddOp3(v, OP_Add, iMem, iMem+i+1, regTemp);
76144       sqlite3VdbeAddOp2(v, OP_AddImm, regTemp, -1);
76145       sqlite3VdbeAddOp3(v, OP_Divide, iMem+i+1, regTemp, regTemp);
76146       sqlite3VdbeAddOp1(v, OP_ToInt, regTemp);
76147       sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regSampleno, regSampleno);
76148     }
76149     sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regRec, "aaa", 0);
76150     sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regRowid);
76151     sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regRowid);
76152     sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
76153   }
76154 
76155   /* If the table has no indices, create a single sqlite_stat1 entry
76156   ** containing NULL as the index name and the row count as the content.
76157   */
76158   if( pTab->pIndex==0 ){
76159     sqlite3VdbeAddOp3(v, OP_OpenRead, iIdxCur, pTab->tnum, iDb);
76160     VdbeComment((v, "%s", pTab->zName));
76161     sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regSampleno);
76162     sqlite3VdbeAddOp1(v, OP_Close, iIdxCur);
76163     jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, regSampleno);
76164   }else{
76165     sqlite3VdbeJumpHere(v, jZeroRows);
76166     jZeroRows = sqlite3VdbeAddOp0(v, OP_Goto);
76167   }
76168   sqlite3VdbeAddOp2(v, OP_Null, 0, regIdxname);
76169   sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regRec, "aaa", 0);
76170   sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regRowid);
76171   sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regRowid);
76172   sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
76173   if( pParse->nMem<regRec ) pParse->nMem = regRec;
76174   sqlite3VdbeJumpHere(v, jZeroRows);
76175 }
76176 
76177 /*
76178 ** Generate code that will cause the most recent index analysis to
76179 ** be loaded into internal hash tables where is can be used.
76180 */
76181 static void loadAnalysis(Parse *pParse, int iDb){
76182   Vdbe *v = sqlite3GetVdbe(pParse);
76183   if( v ){
76184     sqlite3VdbeAddOp1(v, OP_LoadAnalysis, iDb);
76185   }
76186 }
76187 
76188 /*
76189 ** Generate code that will do an analysis of an entire database
76190 */
76191 static void analyzeDatabase(Parse *pParse, int iDb){
76192   sqlite3 *db = pParse->db;
76193   Schema *pSchema = db->aDb[iDb].pSchema;    /* Schema of database iDb */
76194   HashElem *k;
76195   int iStatCur;
76196   int iMem;
76197 
76198   sqlite3BeginWriteOperation(pParse, 0, iDb);
76199   iStatCur = pParse->nTab;
76200   pParse->nTab += 2;
76201   openStatTable(pParse, iDb, iStatCur, 0, 0);
76202   iMem = pParse->nMem+1;
76203   assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
76204   for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
76205     Table *pTab = (Table*)sqliteHashData(k);
76206     analyzeOneTable(pParse, pTab, 0, iStatCur, iMem);
76207   }
76208   loadAnalysis(pParse, iDb);
76209 }
76210 
76211 /*
76212 ** Generate code that will do an analysis of a single table in
76213 ** a database.  If pOnlyIdx is not NULL then it is a single index
76214 ** in pTab that should be analyzed.
76215 */
76216 static void analyzeTable(Parse *pParse, Table *pTab, Index *pOnlyIdx){
76217   int iDb;
76218   int iStatCur;
76219 
76220   assert( pTab!=0 );
76221   assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
76222   iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
76223   sqlite3BeginWriteOperation(pParse, 0, iDb);
76224   iStatCur = pParse->nTab;
76225   pParse->nTab += 2;
76226   if( pOnlyIdx ){
76227     openStatTable(pParse, iDb, iStatCur, pOnlyIdx->zName, "idx");
76228   }else{
76229     openStatTable(pParse, iDb, iStatCur, pTab->zName, "tbl");
76230   }
76231   analyzeOneTable(pParse, pTab, pOnlyIdx, iStatCur, pParse->nMem+1);
76232   loadAnalysis(pParse, iDb);
76233 }
76234 
76235 /*
76236 ** Generate code for the ANALYZE command.  The parser calls this routine
76237 ** when it recognizes an ANALYZE command.
76238 **
76239 **        ANALYZE                            -- 1
76240 **        ANALYZE  <database>                -- 2
76241 **        ANALYZE  ?<database>.?<tablename>  -- 3
76242 **
76243 ** Form 1 causes all indices in all attached databases to be analyzed.
76244 ** Form 2 analyzes all indices the single database named.
76245 ** Form 3 analyzes all indices associated with the named table.
76246 */
76247 SQLITE_PRIVATE void sqlite3Analyze(Parse *pParse, Token *pName1, Token *pName2){
76248   sqlite3 *db = pParse->db;
76249   int iDb;
76250   int i;
76251   char *z, *zDb;
76252   Table *pTab;
76253   Index *pIdx;
76254   Token *pTableName;
76255 
76256   /* Read the database schema. If an error occurs, leave an error message
76257   ** and code in pParse and return NULL. */
76258   assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
76259   if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
76260     return;
76261   }
76262 
76263   assert( pName2!=0 || pName1==0 );
76264   if( pName1==0 ){
76265     /* Form 1:  Analyze everything */
76266     for(i=0; i<db->nDb; i++){
76267       if( i==1 ) continue;  /* Do not analyze the TEMP database */
76268       analyzeDatabase(pParse, i);
76269     }
76270   }else if( pName2->n==0 ){
76271     /* Form 2:  Analyze the database or table named */
76272     iDb = sqlite3FindDb(db, pName1);
76273     if( iDb>=0 ){
76274       analyzeDatabase(pParse, iDb);
76275     }else{
76276       z = sqlite3NameFromToken(db, pName1);
76277       if( z ){
76278         if( (pIdx = sqlite3FindIndex(db, z, 0))!=0 ){
76279           analyzeTable(pParse, pIdx->pTable, pIdx);
76280         }else if( (pTab = sqlite3LocateTable(pParse, 0, z, 0))!=0 ){
76281           analyzeTable(pParse, pTab, 0);
76282         }
76283         sqlite3DbFree(db, z);
76284       }
76285     }
76286   }else{
76287     /* Form 3: Analyze the fully qualified table name */
76288     iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pTableName);
76289     if( iDb>=0 ){
76290       zDb = db->aDb[iDb].zName;
76291       z = sqlite3NameFromToken(db, pTableName);
76292       if( z ){
76293         if( (pIdx = sqlite3FindIndex(db, z, zDb))!=0 ){
76294           analyzeTable(pParse, pIdx->pTable, pIdx);
76295         }else if( (pTab = sqlite3LocateTable(pParse, 0, z, zDb))!=0 ){
76296           analyzeTable(pParse, pTab, 0);
76297         }
76298         sqlite3DbFree(db, z);
76299       }
76300     }
76301   }
76302 }
76303 
76304 /*
76305 ** Used to pass information from the analyzer reader through to the
76306 ** callback routine.
76307 */
76308 typedef struct analysisInfo analysisInfo;
76309 struct analysisInfo {
76310   sqlite3 *db;
76311   const char *zDatabase;
76312 };
76313 
76314 /*
76315 ** This callback is invoked once for each index when reading the
76316 ** sqlite_stat1 table.
76317 **
76318 **     argv[0] = name of the table
76319 **     argv[1] = name of the index (might be NULL)
76320 **     argv[2] = results of analysis - on integer for each column
76321 **
76322 ** Entries for which argv[1]==NULL simply record the number of rows in
76323 ** the table.
76324 */
76325 static int analysisLoader(void *pData, int argc, char **argv, char **NotUsed){
76326   analysisInfo *pInfo = (analysisInfo*)pData;
76327   Index *pIndex;
76328   Table *pTable;
76329   int i, c, n;
76330   unsigned int v;
76331   const char *z;
76332 
76333   assert( argc==3 );
76334   UNUSED_PARAMETER2(NotUsed, argc);
76335 
76336   if( argv==0 || argv[0]==0 || argv[2]==0 ){
76337     return 0;
76338   }
76339   pTable = sqlite3FindTable(pInfo->db, argv[0], pInfo->zDatabase);
76340   if( pTable==0 ){
76341     return 0;
76342   }
76343   if( argv[1] ){
76344     pIndex = sqlite3FindIndex(pInfo->db, argv[1], pInfo->zDatabase);
76345   }else{
76346     pIndex = 0;
76347   }
76348   n = pIndex ? pIndex->nColumn : 0;
76349   z = argv[2];
76350   for(i=0; *z && i<=n; i++){
76351     v = 0;
76352     while( (c=z[0])>='0' && c<='9' ){
76353       v = v*10 + c - '0';
76354       z++;
76355     }
76356     if( i==0 ) pTable->nRowEst = v;
76357     if( pIndex==0 ) break;
76358     pIndex->aiRowEst[i] = v;
76359     if( *z==' ' ) z++;
76360     if( memcmp(z, "unordered", 10)==0 ){
76361       pIndex->bUnordered = 1;
76362       break;
76363     }
76364   }
76365   return 0;
76366 }
76367 
76368 /*
76369 ** If the Index.aSample variable is not NULL, delete the aSample[] array
76370 ** and its contents.
76371 */
76372 SQLITE_PRIVATE void sqlite3DeleteIndexSamples(sqlite3 *db, Index *pIdx){
76373 #ifdef SQLITE_ENABLE_STAT2
76374   if( pIdx->aSample ){
76375     int j;
76376     for(j=0; j<SQLITE_INDEX_SAMPLES; j++){
76377       IndexSample *p = &pIdx->aSample[j];
76378       if( p->eType==SQLITE_TEXT || p->eType==SQLITE_BLOB ){
76379         sqlite3DbFree(db, p->u.z);
76380       }
76381     }
76382     sqlite3DbFree(db, pIdx->aSample);
76383   }
76384 #else
76385   UNUSED_PARAMETER(db);
76386   UNUSED_PARAMETER(pIdx);
76387 #endif
76388 }
76389 
76390 /*
76391 ** Load the content of the sqlite_stat1 and sqlite_stat2 tables. The
76392 ** contents of sqlite_stat1 are used to populate the Index.aiRowEst[]
76393 ** arrays. The contents of sqlite_stat2 are used to populate the
76394 ** Index.aSample[] arrays.
76395 **
76396 ** If the sqlite_stat1 table is not present in the database, SQLITE_ERROR
76397 ** is returned. In this case, even if SQLITE_ENABLE_STAT2 was defined
76398 ** during compilation and the sqlite_stat2 table is present, no data is
76399 ** read from it.
76400 **
76401 ** If SQLITE_ENABLE_STAT2 was defined during compilation and the
76402 ** sqlite_stat2 table is not present in the database, SQLITE_ERROR is
76403 ** returned. However, in this case, data is read from the sqlite_stat1
76404 ** table (if it is present) before returning.
76405 **
76406 ** If an OOM error occurs, this function always sets db->mallocFailed.
76407 ** This means if the caller does not care about other errors, the return
76408 ** code may be ignored.
76409 */
76410 SQLITE_PRIVATE int sqlite3AnalysisLoad(sqlite3 *db, int iDb){
76411   analysisInfo sInfo;
76412   HashElem *i;
76413   char *zSql;
76414   int rc;
76415 
76416   assert( iDb>=0 && iDb<db->nDb );
76417   assert( db->aDb[iDb].pBt!=0 );
76418 
76419   /* Clear any prior statistics */
76420   assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
76421   for(i=sqliteHashFirst(&db->aDb[iDb].pSchema->idxHash);i;i=sqliteHashNext(i)){
76422     Index *pIdx = sqliteHashData(i);
76423     sqlite3DefaultRowEst(pIdx);
76424     sqlite3DeleteIndexSamples(db, pIdx);
76425     pIdx->aSample = 0;
76426   }
76427 
76428   /* Check to make sure the sqlite_stat1 table exists */
76429   sInfo.db = db;
76430   sInfo.zDatabase = db->aDb[iDb].zName;
76431   if( sqlite3FindTable(db, "sqlite_stat1", sInfo.zDatabase)==0 ){
76432     return SQLITE_ERROR;
76433   }
76434 
76435   /* Load new statistics out of the sqlite_stat1 table */
76436   zSql = sqlite3MPrintf(db,
76437       "SELECT tbl, idx, stat FROM %Q.sqlite_stat1", sInfo.zDatabase);
76438   if( zSql==0 ){
76439     rc = SQLITE_NOMEM;
76440   }else{
76441     rc = sqlite3_exec(db, zSql, analysisLoader, &sInfo, 0);
76442     sqlite3DbFree(db, zSql);
76443   }
76444 
76445 
76446   /* Load the statistics from the sqlite_stat2 table. */
76447 #ifdef SQLITE_ENABLE_STAT2
76448   if( rc==SQLITE_OK && !sqlite3FindTable(db, "sqlite_stat2", sInfo.zDatabase) ){
76449     rc = SQLITE_ERROR;
76450   }
76451   if( rc==SQLITE_OK ){
76452     sqlite3_stmt *pStmt = 0;
76453 
76454     zSql = sqlite3MPrintf(db,
76455         "SELECT idx,sampleno,sample FROM %Q.sqlite_stat2", sInfo.zDatabase);
76456     if( !zSql ){
76457       rc = SQLITE_NOMEM;
76458     }else{
76459       rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
76460       sqlite3DbFree(db, zSql);
76461     }
76462 
76463     if( rc==SQLITE_OK ){
76464       while( sqlite3_step(pStmt)==SQLITE_ROW ){
76465         char *zIndex;   /* Index name */
76466         Index *pIdx;    /* Pointer to the index object */
76467 
76468         zIndex = (char *)sqlite3_column_text(pStmt, 0);
76469         pIdx = zIndex ? sqlite3FindIndex(db, zIndex, sInfo.zDatabase) : 0;
76470         if( pIdx ){
76471           int iSample = sqlite3_column_int(pStmt, 1);
76472           if( iSample<SQLITE_INDEX_SAMPLES && iSample>=0 ){
76473             int eType = sqlite3_column_type(pStmt, 2);
76474 
76475             if( pIdx->aSample==0 ){
76476               static const int sz = sizeof(IndexSample)*SQLITE_INDEX_SAMPLES;
76477               pIdx->aSample = (IndexSample *)sqlite3DbMallocRaw(0, sz);
76478               if( pIdx->aSample==0 ){
76479                 db->mallocFailed = 1;
76480                 break;
76481               }
76482 	      memset(pIdx->aSample, 0, sz);
76483             }
76484 
76485             assert( pIdx->aSample );
76486             {
76487               IndexSample *pSample = &pIdx->aSample[iSample];
76488               pSample->eType = (u8)eType;
76489               if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
76490                 pSample->u.r = sqlite3_column_double(pStmt, 2);
76491               }else if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){
76492                 const char *z = (const char *)(
76493                     (eType==SQLITE_BLOB) ?
76494                     sqlite3_column_blob(pStmt, 2):
76495                     sqlite3_column_text(pStmt, 2)
76496                 );
76497                 int n = sqlite3_column_bytes(pStmt, 2);
76498                 if( n>24 ){
76499                   n = 24;
76500                 }
76501                 pSample->nByte = (u8)n;
76502                 if( n < 1){
76503                   pSample->u.z = 0;
76504                 }else{
76505                   pSample->u.z = sqlite3DbStrNDup(0, z, n);
76506                   if( pSample->u.z==0 ){
76507                     db->mallocFailed = 1;
76508                     break;
76509                   }
76510                 }
76511               }
76512             }
76513           }
76514         }
76515       }
76516       rc = sqlite3_finalize(pStmt);
76517     }
76518   }
76519 #endif
76520 
76521   if( rc==SQLITE_NOMEM ){
76522     db->mallocFailed = 1;
76523   }
76524   return rc;
76525 }
76526 
76527 
76528 #endif /* SQLITE_OMIT_ANALYZE */
76529 
76530 /************** End of analyze.c *********************************************/
76531 /************** Begin file attach.c ******************************************/
76532 /*
76533 ** 2003 April 6
76534 **
76535 ** The author disclaims copyright to this source code.  In place of
76536 ** a legal notice, here is a blessing:
76537 **
76538 **    May you do good and not evil.
76539 **    May you find forgiveness for yourself and forgive others.
76540 **    May you share freely, never taking more than you give.
76541 **
76542 *************************************************************************
76543 ** This file contains code used to implement the ATTACH and DETACH commands.
76544 */
76545 
76546 #ifndef SQLITE_OMIT_ATTACH
76547 /*
76548 ** Resolve an expression that was part of an ATTACH or DETACH statement. This
76549 ** is slightly different from resolving a normal SQL expression, because simple
76550 ** identifiers are treated as strings, not possible column names or aliases.
76551 **
76552 ** i.e. if the parser sees:
76553 **
76554 **     ATTACH DATABASE abc AS def
76555 **
76556 ** it treats the two expressions as literal strings 'abc' and 'def' instead of
76557 ** looking for columns of the same name.
76558 **
76559 ** This only applies to the root node of pExpr, so the statement:
76560 **
76561 **     ATTACH DATABASE abc||def AS 'db2'
76562 **
76563 ** will fail because neither abc or def can be resolved.
76564 */
76565 static int resolveAttachExpr(NameContext *pName, Expr *pExpr)
76566 {
76567   int rc = SQLITE_OK;
76568   if( pExpr ){
76569     if( pExpr->op!=TK_ID ){
76570       rc = sqlite3ResolveExprNames(pName, pExpr);
76571       if( rc==SQLITE_OK && !sqlite3ExprIsConstant(pExpr) ){
76572         sqlite3ErrorMsg(pName->pParse, "invalid name: \"%s\"", pExpr->u.zToken);
76573         return SQLITE_ERROR;
76574       }
76575     }else{
76576       pExpr->op = TK_STRING;
76577     }
76578   }
76579   return rc;
76580 }
76581 
76582 /*
76583 ** An SQL user-function registered to do the work of an ATTACH statement. The
76584 ** three arguments to the function come directly from an attach statement:
76585 **
76586 **     ATTACH DATABASE x AS y KEY z
76587 **
76588 **     SELECT sqlite_attach(x, y, z)
76589 **
76590 ** If the optional "KEY z" syntax is omitted, an SQL NULL is passed as the
76591 ** third argument.
76592 */
76593 static void attachFunc(
76594   sqlite3_context *context,
76595   int NotUsed,
76596   sqlite3_value **argv
76597 ){
76598   int i;
76599   int rc = 0;
76600   sqlite3 *db = sqlite3_context_db_handle(context);
76601   const char *zName;
76602   const char *zFile;
76603   char *zPath = 0;
76604   char *zErr = 0;
76605   unsigned int flags;
76606   Db *aNew;
76607   char *zErrDyn = 0;
76608   sqlite3_vfs *pVfs;
76609 
76610   UNUSED_PARAMETER(NotUsed);
76611 
76612   zFile = (const char *)sqlite3_value_text(argv[0]);
76613   zName = (const char *)sqlite3_value_text(argv[1]);
76614   if( zFile==0 ) zFile = "";
76615   if( zName==0 ) zName = "";
76616 
76617   /* Check for the following errors:
76618   **
76619   **     * Too many attached databases,
76620   **     * Transaction currently open
76621   **     * Specified database name already being used.
76622   */
76623   if( db->nDb>=db->aLimit[SQLITE_LIMIT_ATTACHED]+2 ){
76624     zErrDyn = sqlite3MPrintf(db, "too many attached databases - max %d",
76625       db->aLimit[SQLITE_LIMIT_ATTACHED]
76626     );
76627     goto attach_error;
76628   }
76629   if( !db->autoCommit ){
76630     zErrDyn = sqlite3MPrintf(db, "cannot ATTACH database within transaction");
76631     goto attach_error;
76632   }
76633   for(i=0; i<db->nDb; i++){
76634     char *z = db->aDb[i].zName;
76635     assert( z && zName );
76636     if( sqlite3StrICmp(z, zName)==0 ){
76637       zErrDyn = sqlite3MPrintf(db, "database %s is already in use", zName);
76638       goto attach_error;
76639     }
76640   }
76641 
76642   /* Allocate the new entry in the db->aDb[] array and initialise the schema
76643   ** hash tables.
76644   */
76645   if( db->aDb==db->aDbStatic ){
76646     aNew = sqlite3DbMallocRaw(db, sizeof(db->aDb[0])*3 );
76647     if( aNew==0 ) return;
76648     memcpy(aNew, db->aDb, sizeof(db->aDb[0])*2);
76649   }else{
76650     aNew = sqlite3DbRealloc(db, db->aDb, sizeof(db->aDb[0])*(db->nDb+1) );
76651     if( aNew==0 ) return;
76652   }
76653   db->aDb = aNew;
76654   aNew = &db->aDb[db->nDb];
76655   memset(aNew, 0, sizeof(*aNew));
76656 
76657   /* Open the database file. If the btree is successfully opened, use
76658   ** it to obtain the database schema. At this point the schema may
76659   ** or may not be initialised.
76660   */
76661   flags = db->openFlags;
76662   rc = sqlite3ParseUri(db->pVfs->zName, zFile, &flags, &pVfs, &zPath, &zErr);
76663   if( rc!=SQLITE_OK ){
76664     if( rc==SQLITE_NOMEM ) db->mallocFailed = 1;
76665     sqlite3_result_error(context, zErr, -1);
76666     sqlite3_free(zErr);
76667     return;
76668   }
76669   assert( pVfs );
76670   flags |= SQLITE_OPEN_MAIN_DB;
76671   rc = sqlite3BtreeOpen(pVfs, zPath, db, &aNew->pBt, 0, flags);
76672   sqlite3_free( zPath );
76673   db->nDb++;
76674   if( rc==SQLITE_CONSTRAINT ){
76675     rc = SQLITE_ERROR;
76676     zErrDyn = sqlite3MPrintf(db, "database is already attached");
76677   }else if( rc==SQLITE_OK ){
76678     Pager *pPager;
76679     aNew->pSchema = sqlite3SchemaGet(db, aNew->pBt);
76680     if( !aNew->pSchema ){
76681       rc = SQLITE_NOMEM;
76682     }else if( aNew->pSchema->file_format && aNew->pSchema->enc!=ENC(db) ){
76683       zErrDyn = sqlite3MPrintf(db,
76684         "attached databases must use the same text encoding as main database");
76685       rc = SQLITE_ERROR;
76686     }
76687     pPager = sqlite3BtreePager(aNew->pBt);
76688     sqlite3PagerLockingMode(pPager, db->dfltLockMode);
76689     sqlite3BtreeSecureDelete(aNew->pBt,
76690                              sqlite3BtreeSecureDelete(db->aDb[0].pBt,-1) );
76691   }
76692   aNew->safety_level = 3;
76693   aNew->zName = sqlite3DbStrDup(db, zName);
76694   if( rc==SQLITE_OK && aNew->zName==0 ){
76695     rc = SQLITE_NOMEM;
76696   }
76697 
76698 
76699 #ifdef SQLITE_HAS_CODEC
76700   if( rc==SQLITE_OK ){
76701     extern int sqlite3CodecAttach(sqlite3*, int, const void*, int);
76702     extern void sqlite3CodecGetKey(sqlite3*, int, void**, int*);
76703     int nKey;
76704     char *zKey;
76705     int t = sqlite3_value_type(argv[2]);
76706     switch( t ){
76707       case SQLITE_INTEGER:
76708       case SQLITE_FLOAT:
76709         zErrDyn = sqlite3DbStrDup(db, "Invalid key value");
76710         rc = SQLITE_ERROR;
76711         break;
76712 
76713       case SQLITE_TEXT:
76714       case SQLITE_BLOB:
76715         nKey = sqlite3_value_bytes(argv[2]);
76716         zKey = (char *)sqlite3_value_blob(argv[2]);
76717         rc = sqlite3CodecAttach(db, db->nDb-1, zKey, nKey);
76718         break;
76719 
76720       case SQLITE_NULL:
76721         /* No key specified.  Use the key from the main database */
76722         sqlite3CodecGetKey(db, 0, (void**)&zKey, &nKey);
76723         if( nKey>0 || sqlite3BtreeGetReserve(db->aDb[0].pBt)>0 ){
76724           rc = sqlite3CodecAttach(db, db->nDb-1, zKey, nKey);
76725         }
76726         break;
76727     }
76728   }
76729 #endif
76730 
76731   /* If the file was opened successfully, read the schema for the new database.
76732   ** If this fails, or if opening the file failed, then close the file and
76733   ** remove the entry from the db->aDb[] array. i.e. put everything back the way
76734   ** we found it.
76735   */
76736   if( rc==SQLITE_OK ){
76737     sqlite3BtreeEnterAll(db);
76738     rc = sqlite3Init(db, &zErrDyn);
76739     sqlite3BtreeLeaveAll(db);
76740   }
76741   if( rc ){
76742     int iDb = db->nDb - 1;
76743     assert( iDb>=2 );
76744     if( db->aDb[iDb].pBt ){
76745       sqlite3BtreeClose(db->aDb[iDb].pBt);
76746       db->aDb[iDb].pBt = 0;
76747       db->aDb[iDb].pSchema = 0;
76748     }
76749     sqlite3ResetInternalSchema(db, -1);
76750     db->nDb = iDb;
76751     if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
76752       db->mallocFailed = 1;
76753       sqlite3DbFree(db, zErrDyn);
76754       zErrDyn = sqlite3MPrintf(db, "out of memory");
76755     }else if( zErrDyn==0 ){
76756       zErrDyn = sqlite3MPrintf(db, "unable to open database: %s", zFile);
76757     }
76758     goto attach_error;
76759   }
76760 
76761   return;
76762 
76763 attach_error:
76764   /* Return an error if we get here */
76765   if( zErrDyn ){
76766     sqlite3_result_error(context, zErrDyn, -1);
76767     sqlite3DbFree(db, zErrDyn);
76768   }
76769   if( rc ) sqlite3_result_error_code(context, rc);
76770 }
76771 
76772 /*
76773 ** An SQL user-function registered to do the work of an DETACH statement. The
76774 ** three arguments to the function come directly from a detach statement:
76775 **
76776 **     DETACH DATABASE x
76777 **
76778 **     SELECT sqlite_detach(x)
76779 */
76780 static void detachFunc(
76781   sqlite3_context *context,
76782   int NotUsed,
76783   sqlite3_value **argv
76784 ){
76785   const char *zName = (const char *)sqlite3_value_text(argv[0]);
76786   sqlite3 *db = sqlite3_context_db_handle(context);
76787   int i;
76788   Db *pDb = 0;
76789   char zErr[128];
76790 
76791   UNUSED_PARAMETER(NotUsed);
76792 
76793   if( zName==0 ) zName = "";
76794   for(i=0; i<db->nDb; i++){
76795     pDb = &db->aDb[i];
76796     if( pDb->pBt==0 ) continue;
76797     if( sqlite3StrICmp(pDb->zName, zName)==0 ) break;
76798   }
76799 
76800   if( i>=db->nDb ){
76801     sqlite3_snprintf(sizeof(zErr),zErr, "no such database: %s", zName);
76802     goto detach_error;
76803   }
76804   if( i<2 ){
76805     sqlite3_snprintf(sizeof(zErr),zErr, "cannot detach database %s", zName);
76806     goto detach_error;
76807   }
76808   if( !db->autoCommit ){
76809     sqlite3_snprintf(sizeof(zErr), zErr,
76810                      "cannot DETACH database within transaction");
76811     goto detach_error;
76812   }
76813   if( sqlite3BtreeIsInReadTrans(pDb->pBt) || sqlite3BtreeIsInBackup(pDb->pBt) ){
76814     sqlite3_snprintf(sizeof(zErr),zErr, "database %s is locked", zName);
76815     goto detach_error;
76816   }
76817 
76818   sqlite3BtreeClose(pDb->pBt);
76819   pDb->pBt = 0;
76820   pDb->pSchema = 0;
76821   sqlite3ResetInternalSchema(db, -1);
76822   return;
76823 
76824 detach_error:
76825   sqlite3_result_error(context, zErr, -1);
76826 }
76827 
76828 /*
76829 ** This procedure generates VDBE code for a single invocation of either the
76830 ** sqlite_detach() or sqlite_attach() SQL user functions.
76831 */
76832 static void codeAttach(
76833   Parse *pParse,       /* The parser context */
76834   int type,            /* Either SQLITE_ATTACH or SQLITE_DETACH */
76835   FuncDef const *pFunc,/* FuncDef wrapper for detachFunc() or attachFunc() */
76836   Expr *pAuthArg,      /* Expression to pass to authorization callback */
76837   Expr *pFilename,     /* Name of database file */
76838   Expr *pDbname,       /* Name of the database to use internally */
76839   Expr *pKey           /* Database key for encryption extension */
76840 ){
76841   int rc;
76842   NameContext sName;
76843   Vdbe *v;
76844   sqlite3* db = pParse->db;
76845   int regArgs;
76846 
76847   memset(&sName, 0, sizeof(NameContext));
76848   sName.pParse = pParse;
76849 
76850   if(
76851       SQLITE_OK!=(rc = resolveAttachExpr(&sName, pFilename)) ||
76852       SQLITE_OK!=(rc = resolveAttachExpr(&sName, pDbname)) ||
76853       SQLITE_OK!=(rc = resolveAttachExpr(&sName, pKey))
76854   ){
76855     pParse->nErr++;
76856     goto attach_end;
76857   }
76858 
76859 #ifndef SQLITE_OMIT_AUTHORIZATION
76860   if( pAuthArg ){
76861     char *zAuthArg;
76862     if( pAuthArg->op==TK_STRING ){
76863       zAuthArg = pAuthArg->u.zToken;
76864     }else{
76865       zAuthArg = 0;
76866     }
76867     rc = sqlite3AuthCheck(pParse, type, zAuthArg, 0, 0);
76868     if(rc!=SQLITE_OK ){
76869       goto attach_end;
76870     }
76871   }
76872 #endif /* SQLITE_OMIT_AUTHORIZATION */
76873 
76874 
76875   v = sqlite3GetVdbe(pParse);
76876   regArgs = sqlite3GetTempRange(pParse, 4);
76877   sqlite3ExprCode(pParse, pFilename, regArgs);
76878   sqlite3ExprCode(pParse, pDbname, regArgs+1);
76879   sqlite3ExprCode(pParse, pKey, regArgs+2);
76880 
76881   assert( v || db->mallocFailed );
76882   if( v ){
76883     sqlite3VdbeAddOp3(v, OP_Function, 0, regArgs+3-pFunc->nArg, regArgs+3);
76884     assert( pFunc->nArg==-1 || (pFunc->nArg&0xff)==pFunc->nArg );
76885     sqlite3VdbeChangeP5(v, (u8)(pFunc->nArg));
76886     sqlite3VdbeChangeP4(v, -1, (char *)pFunc, P4_FUNCDEF);
76887 
76888     /* Code an OP_Expire. For an ATTACH statement, set P1 to true (expire this
76889     ** statement only). For DETACH, set it to false (expire all existing
76890     ** statements).
76891     */
76892     sqlite3VdbeAddOp1(v, OP_Expire, (type==SQLITE_ATTACH));
76893   }
76894 
76895 attach_end:
76896   sqlite3ExprDelete(db, pFilename);
76897   sqlite3ExprDelete(db, pDbname);
76898   sqlite3ExprDelete(db, pKey);
76899 }
76900 
76901 /*
76902 ** Called by the parser to compile a DETACH statement.
76903 **
76904 **     DETACH pDbname
76905 */
76906 SQLITE_PRIVATE void sqlite3Detach(Parse *pParse, Expr *pDbname){
76907   static const FuncDef detach_func = {
76908     1,                /* nArg */
76909     SQLITE_UTF8,      /* iPrefEnc */
76910     0,                /* flags */
76911     0,                /* pUserData */
76912     0,                /* pNext */
76913     detachFunc,       /* xFunc */
76914     0,                /* xStep */
76915     0,                /* xFinalize */
76916     "sqlite_detach",  /* zName */
76917     0,                /* pHash */
76918     0                 /* pDestructor */
76919   };
76920   codeAttach(pParse, SQLITE_DETACH, &detach_func, pDbname, 0, 0, pDbname);
76921 }
76922 
76923 /*
76924 ** Called by the parser to compile an ATTACH statement.
76925 **
76926 **     ATTACH p AS pDbname KEY pKey
76927 */
76928 SQLITE_PRIVATE void sqlite3Attach(Parse *pParse, Expr *p, Expr *pDbname, Expr *pKey){
76929   static const FuncDef attach_func = {
76930     3,                /* nArg */
76931     SQLITE_UTF8,      /* iPrefEnc */
76932     0,                /* flags */
76933     0,                /* pUserData */
76934     0,                /* pNext */
76935     attachFunc,       /* xFunc */
76936     0,                /* xStep */
76937     0,                /* xFinalize */
76938     "sqlite_attach",  /* zName */
76939     0,                /* pHash */
76940     0                 /* pDestructor */
76941   };
76942   codeAttach(pParse, SQLITE_ATTACH, &attach_func, p, p, pDbname, pKey);
76943 }
76944 #endif /* SQLITE_OMIT_ATTACH */
76945 
76946 /*
76947 ** Initialize a DbFixer structure.  This routine must be called prior
76948 ** to passing the structure to one of the sqliteFixAAAA() routines below.
76949 **
76950 ** The return value indicates whether or not fixation is required.  TRUE
76951 ** means we do need to fix the database references, FALSE means we do not.
76952 */
76953 SQLITE_PRIVATE int sqlite3FixInit(
76954   DbFixer *pFix,      /* The fixer to be initialized */
76955   Parse *pParse,      /* Error messages will be written here */
76956   int iDb,            /* This is the database that must be used */
76957   const char *zType,  /* "view", "trigger", or "index" */
76958   const Token *pName  /* Name of the view, trigger, or index */
76959 ){
76960   sqlite3 *db;
76961 
76962   if( NEVER(iDb<0) || iDb==1 ) return 0;
76963   db = pParse->db;
76964   assert( db->nDb>iDb );
76965   pFix->pParse = pParse;
76966   pFix->zDb = db->aDb[iDb].zName;
76967   pFix->zType = zType;
76968   pFix->pName = pName;
76969   return 1;
76970 }
76971 
76972 /*
76973 ** The following set of routines walk through the parse tree and assign
76974 ** a specific database to all table references where the database name
76975 ** was left unspecified in the original SQL statement.  The pFix structure
76976 ** must have been initialized by a prior call to sqlite3FixInit().
76977 **
76978 ** These routines are used to make sure that an index, trigger, or
76979 ** view in one database does not refer to objects in a different database.
76980 ** (Exception: indices, triggers, and views in the TEMP database are
76981 ** allowed to refer to anything.)  If a reference is explicitly made
76982 ** to an object in a different database, an error message is added to
76983 ** pParse->zErrMsg and these routines return non-zero.  If everything
76984 ** checks out, these routines return 0.
76985 */
76986 SQLITE_PRIVATE int sqlite3FixSrcList(
76987   DbFixer *pFix,       /* Context of the fixation */
76988   SrcList *pList       /* The Source list to check and modify */
76989 ){
76990   int i;
76991   const char *zDb;
76992   struct SrcList_item *pItem;
76993 
76994   if( NEVER(pList==0) ) return 0;
76995   zDb = pFix->zDb;
76996   for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
76997     if( pItem->zDatabase==0 ){
76998       pItem->zDatabase = sqlite3DbStrDup(pFix->pParse->db, zDb);
76999     }else if( sqlite3StrICmp(pItem->zDatabase,zDb)!=0 ){
77000       sqlite3ErrorMsg(pFix->pParse,
77001          "%s %T cannot reference objects in database %s",
77002          pFix->zType, pFix->pName, pItem->zDatabase);
77003       return 1;
77004     }
77005 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER)
77006     if( sqlite3FixSelect(pFix, pItem->pSelect) ) return 1;
77007     if( sqlite3FixExpr(pFix, pItem->pOn) ) return 1;
77008 #endif
77009   }
77010   return 0;
77011 }
77012 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER)
77013 SQLITE_PRIVATE int sqlite3FixSelect(
77014   DbFixer *pFix,       /* Context of the fixation */
77015   Select *pSelect      /* The SELECT statement to be fixed to one database */
77016 ){
77017   while( pSelect ){
77018     if( sqlite3FixExprList(pFix, pSelect->pEList) ){
77019       return 1;
77020     }
77021     if( sqlite3FixSrcList(pFix, pSelect->pSrc) ){
77022       return 1;
77023     }
77024     if( sqlite3FixExpr(pFix, pSelect->pWhere) ){
77025       return 1;
77026     }
77027     if( sqlite3FixExpr(pFix, pSelect->pHaving) ){
77028       return 1;
77029     }
77030     pSelect = pSelect->pPrior;
77031   }
77032   return 0;
77033 }
77034 SQLITE_PRIVATE int sqlite3FixExpr(
77035   DbFixer *pFix,     /* Context of the fixation */
77036   Expr *pExpr        /* The expression to be fixed to one database */
77037 ){
77038   while( pExpr ){
77039     if( ExprHasAnyProperty(pExpr, EP_TokenOnly) ) break;
77040     if( ExprHasProperty(pExpr, EP_xIsSelect) ){
77041       if( sqlite3FixSelect(pFix, pExpr->x.pSelect) ) return 1;
77042     }else{
77043       if( sqlite3FixExprList(pFix, pExpr->x.pList) ) return 1;
77044     }
77045     if( sqlite3FixExpr(pFix, pExpr->pRight) ){
77046       return 1;
77047     }
77048     pExpr = pExpr->pLeft;
77049   }
77050   return 0;
77051 }
77052 SQLITE_PRIVATE int sqlite3FixExprList(
77053   DbFixer *pFix,     /* Context of the fixation */
77054   ExprList *pList    /* The expression to be fixed to one database */
77055 ){
77056   int i;
77057   struct ExprList_item *pItem;
77058   if( pList==0 ) return 0;
77059   for(i=0, pItem=pList->a; i<pList->nExpr; i++, pItem++){
77060     if( sqlite3FixExpr(pFix, pItem->pExpr) ){
77061       return 1;
77062     }
77063   }
77064   return 0;
77065 }
77066 #endif
77067 
77068 #ifndef SQLITE_OMIT_TRIGGER
77069 SQLITE_PRIVATE int sqlite3FixTriggerStep(
77070   DbFixer *pFix,     /* Context of the fixation */
77071   TriggerStep *pStep /* The trigger step be fixed to one database */
77072 ){
77073   while( pStep ){
77074     if( sqlite3FixSelect(pFix, pStep->pSelect) ){
77075       return 1;
77076     }
77077     if( sqlite3FixExpr(pFix, pStep->pWhere) ){
77078       return 1;
77079     }
77080     if( sqlite3FixExprList(pFix, pStep->pExprList) ){
77081       return 1;
77082     }
77083     pStep = pStep->pNext;
77084   }
77085   return 0;
77086 }
77087 #endif
77088 
77089 /************** End of attach.c **********************************************/
77090 /************** Begin file auth.c ********************************************/
77091 /*
77092 ** 2003 January 11
77093 **
77094 ** The author disclaims copyright to this source code.  In place of
77095 ** a legal notice, here is a blessing:
77096 **
77097 **    May you do good and not evil.
77098 **    May you find forgiveness for yourself and forgive others.
77099 **    May you share freely, never taking more than you give.
77100 **
77101 *************************************************************************
77102 ** This file contains code used to implement the sqlite3_set_authorizer()
77103 ** API.  This facility is an optional feature of the library.  Embedded
77104 ** systems that do not need this facility may omit it by recompiling
77105 ** the library with -DSQLITE_OMIT_AUTHORIZATION=1
77106 */
77107 
77108 /*
77109 ** All of the code in this file may be omitted by defining a single
77110 ** macro.
77111 */
77112 #ifndef SQLITE_OMIT_AUTHORIZATION
77113 
77114 /*
77115 ** Set or clear the access authorization function.
77116 **
77117 ** The access authorization function is be called during the compilation
77118 ** phase to verify that the user has read and/or write access permission on
77119 ** various fields of the database.  The first argument to the auth function
77120 ** is a copy of the 3rd argument to this routine.  The second argument
77121 ** to the auth function is one of these constants:
77122 **
77123 **       SQLITE_CREATE_INDEX
77124 **       SQLITE_CREATE_TABLE
77125 **       SQLITE_CREATE_TEMP_INDEX
77126 **       SQLITE_CREATE_TEMP_TABLE
77127 **       SQLITE_CREATE_TEMP_TRIGGER
77128 **       SQLITE_CREATE_TEMP_VIEW
77129 **       SQLITE_CREATE_TRIGGER
77130 **       SQLITE_CREATE_VIEW
77131 **       SQLITE_DELETE
77132 **       SQLITE_DROP_INDEX
77133 **       SQLITE_DROP_TABLE
77134 **       SQLITE_DROP_TEMP_INDEX
77135 **       SQLITE_DROP_TEMP_TABLE
77136 **       SQLITE_DROP_TEMP_TRIGGER
77137 **       SQLITE_DROP_TEMP_VIEW
77138 **       SQLITE_DROP_TRIGGER
77139 **       SQLITE_DROP_VIEW
77140 **       SQLITE_INSERT
77141 **       SQLITE_PRAGMA
77142 **       SQLITE_READ
77143 **       SQLITE_SELECT
77144 **       SQLITE_TRANSACTION
77145 **       SQLITE_UPDATE
77146 **
77147 ** The third and fourth arguments to the auth function are the name of
77148 ** the table and the column that are being accessed.  The auth function
77149 ** should return either SQLITE_OK, SQLITE_DENY, or SQLITE_IGNORE.  If
77150 ** SQLITE_OK is returned, it means that access is allowed.  SQLITE_DENY
77151 ** means that the SQL statement will never-run - the sqlite3_exec() call
77152 ** will return with an error.  SQLITE_IGNORE means that the SQL statement
77153 ** should run but attempts to read the specified column will return NULL
77154 ** and attempts to write the column will be ignored.
77155 **
77156 ** Setting the auth function to NULL disables this hook.  The default
77157 ** setting of the auth function is NULL.
77158 */
77159 SQLITE_API int sqlite3_set_authorizer(
77160   sqlite3 *db,
77161   int (*xAuth)(void*,int,const char*,const char*,const char*,const char*),
77162   void *pArg
77163 ){
77164   sqlite3_mutex_enter(db->mutex);
77165   db->xAuth = xAuth;
77166   db->pAuthArg = pArg;
77167   sqlite3ExpirePreparedStatements(db);
77168   sqlite3_mutex_leave(db->mutex);
77169   return SQLITE_OK;
77170 }
77171 
77172 /*
77173 ** Write an error message into pParse->zErrMsg that explains that the
77174 ** user-supplied authorization function returned an illegal value.
77175 */
77176 static void sqliteAuthBadReturnCode(Parse *pParse){
77177   sqlite3ErrorMsg(pParse, "authorizer malfunction");
77178   pParse->rc = SQLITE_ERROR;
77179 }
77180 
77181 /*
77182 ** Invoke the authorization callback for permission to read column zCol from
77183 ** table zTab in database zDb. This function assumes that an authorization
77184 ** callback has been registered (i.e. that sqlite3.xAuth is not NULL).
77185 **
77186 ** If SQLITE_IGNORE is returned and pExpr is not NULL, then pExpr is changed
77187 ** to an SQL NULL expression. Otherwise, if pExpr is NULL, then SQLITE_IGNORE
77188 ** is treated as SQLITE_DENY. In this case an error is left in pParse.
77189 */
77190 SQLITE_PRIVATE int sqlite3AuthReadCol(
77191   Parse *pParse,                  /* The parser context */
77192   const char *zTab,               /* Table name */
77193   const char *zCol,               /* Column name */
77194   int iDb                         /* Index of containing database. */
77195 ){
77196   sqlite3 *db = pParse->db;       /* Database handle */
77197   char *zDb = db->aDb[iDb].zName; /* Name of attached database */
77198   int rc;                         /* Auth callback return code */
77199 
77200   rc = db->xAuth(db->pAuthArg, SQLITE_READ, zTab,zCol,zDb,pParse->zAuthContext);
77201   if( rc==SQLITE_DENY ){
77202     if( db->nDb>2 || iDb!=0 ){
77203       sqlite3ErrorMsg(pParse, "access to %s.%s.%s is prohibited",zDb,zTab,zCol);
77204     }else{
77205       sqlite3ErrorMsg(pParse, "access to %s.%s is prohibited", zTab, zCol);
77206     }
77207     pParse->rc = SQLITE_AUTH;
77208   }else if( rc!=SQLITE_IGNORE && rc!=SQLITE_OK ){
77209     sqliteAuthBadReturnCode(pParse);
77210   }
77211   return rc;
77212 }
77213 
77214 /*
77215 ** The pExpr should be a TK_COLUMN expression.  The table referred to
77216 ** is in pTabList or else it is the NEW or OLD table of a trigger.
77217 ** Check to see if it is OK to read this particular column.
77218 **
77219 ** If the auth function returns SQLITE_IGNORE, change the TK_COLUMN
77220 ** instruction into a TK_NULL.  If the auth function returns SQLITE_DENY,
77221 ** then generate an error.
77222 */
77223 SQLITE_PRIVATE void sqlite3AuthRead(
77224   Parse *pParse,        /* The parser context */
77225   Expr *pExpr,          /* The expression to check authorization on */
77226   Schema *pSchema,      /* The schema of the expression */
77227   SrcList *pTabList     /* All table that pExpr might refer to */
77228 ){
77229   sqlite3 *db = pParse->db;
77230   Table *pTab = 0;      /* The table being read */
77231   const char *zCol;     /* Name of the column of the table */
77232   int iSrc;             /* Index in pTabList->a[] of table being read */
77233   int iDb;              /* The index of the database the expression refers to */
77234   int iCol;             /* Index of column in table */
77235 
77236   if( db->xAuth==0 ) return;
77237   iDb = sqlite3SchemaToIndex(pParse->db, pSchema);
77238   if( iDb<0 ){
77239     /* An attempt to read a column out of a subquery or other
77240     ** temporary table. */
77241     return;
77242   }
77243 
77244   assert( pExpr->op==TK_COLUMN || pExpr->op==TK_TRIGGER );
77245   if( pExpr->op==TK_TRIGGER ){
77246     pTab = pParse->pTriggerTab;
77247   }else{
77248     assert( pTabList );
77249     for(iSrc=0; ALWAYS(iSrc<pTabList->nSrc); iSrc++){
77250       if( pExpr->iTable==pTabList->a[iSrc].iCursor ){
77251         pTab = pTabList->a[iSrc].pTab;
77252         break;
77253       }
77254     }
77255   }
77256   iCol = pExpr->iColumn;
77257   if( NEVER(pTab==0) ) return;
77258 
77259   if( iCol>=0 ){
77260     assert( iCol<pTab->nCol );
77261     zCol = pTab->aCol[iCol].zName;
77262   }else if( pTab->iPKey>=0 ){
77263     assert( pTab->iPKey<pTab->nCol );
77264     zCol = pTab->aCol[pTab->iPKey].zName;
77265   }else{
77266     zCol = "ROWID";
77267   }
77268   assert( iDb>=0 && iDb<db->nDb );
77269   if( SQLITE_IGNORE==sqlite3AuthReadCol(pParse, pTab->zName, zCol, iDb) ){
77270     pExpr->op = TK_NULL;
77271   }
77272 }
77273 
77274 /*
77275 ** Do an authorization check using the code and arguments given.  Return
77276 ** either SQLITE_OK (zero) or SQLITE_IGNORE or SQLITE_DENY.  If SQLITE_DENY
77277 ** is returned, then the error count and error message in pParse are
77278 ** modified appropriately.
77279 */
77280 SQLITE_PRIVATE int sqlite3AuthCheck(
77281   Parse *pParse,
77282   int code,
77283   const char *zArg1,
77284   const char *zArg2,
77285   const char *zArg3
77286 ){
77287   sqlite3 *db = pParse->db;
77288   int rc;
77289 
77290   /* Don't do any authorization checks if the database is initialising
77291   ** or if the parser is being invoked from within sqlite3_declare_vtab.
77292   */
77293   if( db->init.busy || IN_DECLARE_VTAB ){
77294     return SQLITE_OK;
77295   }
77296 
77297   if( db->xAuth==0 ){
77298     return SQLITE_OK;
77299   }
77300   rc = db->xAuth(db->pAuthArg, code, zArg1, zArg2, zArg3, pParse->zAuthContext);
77301   if( rc==SQLITE_DENY ){
77302     sqlite3ErrorMsg(pParse, "not authorized");
77303     pParse->rc = SQLITE_AUTH;
77304   }else if( rc!=SQLITE_OK && rc!=SQLITE_IGNORE ){
77305     rc = SQLITE_DENY;
77306     sqliteAuthBadReturnCode(pParse);
77307   }
77308   return rc;
77309 }
77310 
77311 /*
77312 ** Push an authorization context.  After this routine is called, the
77313 ** zArg3 argument to authorization callbacks will be zContext until
77314 ** popped.  Or if pParse==0, this routine is a no-op.
77315 */
77316 SQLITE_PRIVATE void sqlite3AuthContextPush(
77317   Parse *pParse,
77318   AuthContext *pContext,
77319   const char *zContext
77320 ){
77321   assert( pParse );
77322   pContext->pParse = pParse;
77323   pContext->zAuthContext = pParse->zAuthContext;
77324   pParse->zAuthContext = zContext;
77325 }
77326 
77327 /*
77328 ** Pop an authorization context that was previously pushed
77329 ** by sqlite3AuthContextPush
77330 */
77331 SQLITE_PRIVATE void sqlite3AuthContextPop(AuthContext *pContext){
77332   if( pContext->pParse ){
77333     pContext->pParse->zAuthContext = pContext->zAuthContext;
77334     pContext->pParse = 0;
77335   }
77336 }
77337 
77338 #endif /* SQLITE_OMIT_AUTHORIZATION */
77339 
77340 /************** End of auth.c ************************************************/
77341 /************** Begin file build.c *******************************************/
77342 /*
77343 ** 2001 September 15
77344 **
77345 ** The author disclaims copyright to this source code.  In place of
77346 ** a legal notice, here is a blessing:
77347 **
77348 **    May you do good and not evil.
77349 **    May you find forgiveness for yourself and forgive others.
77350 **    May you share freely, never taking more than you give.
77351 **
77352 *************************************************************************
77353 ** This file contains C code routines that are called by the SQLite parser
77354 ** when syntax rules are reduced.  The routines in this file handle the
77355 ** following kinds of SQL syntax:
77356 **
77357 **     CREATE TABLE
77358 **     DROP TABLE
77359 **     CREATE INDEX
77360 **     DROP INDEX
77361 **     creating ID lists
77362 **     BEGIN TRANSACTION
77363 **     COMMIT
77364 **     ROLLBACK
77365 */
77366 
77367 /*
77368 ** This routine is called when a new SQL statement is beginning to
77369 ** be parsed.  Initialize the pParse structure as needed.
77370 */
77371 SQLITE_PRIVATE void sqlite3BeginParse(Parse *pParse, int explainFlag){
77372   pParse->explain = (u8)explainFlag;
77373   pParse->nVar = 0;
77374 }
77375 
77376 #ifndef SQLITE_OMIT_SHARED_CACHE
77377 /*
77378 ** The TableLock structure is only used by the sqlite3TableLock() and
77379 ** codeTableLocks() functions.
77380 */
77381 struct TableLock {
77382   int iDb;             /* The database containing the table to be locked */
77383   int iTab;            /* The root page of the table to be locked */
77384   u8 isWriteLock;      /* True for write lock.  False for a read lock */
77385   const char *zName;   /* Name of the table */
77386 };
77387 
77388 /*
77389 ** Record the fact that we want to lock a table at run-time.
77390 **
77391 ** The table to be locked has root page iTab and is found in database iDb.
77392 ** A read or a write lock can be taken depending on isWritelock.
77393 **
77394 ** This routine just records the fact that the lock is desired.  The
77395 ** code to make the lock occur is generated by a later call to
77396 ** codeTableLocks() which occurs during sqlite3FinishCoding().
77397 */
77398 SQLITE_PRIVATE void sqlite3TableLock(
77399   Parse *pParse,     /* Parsing context */
77400   int iDb,           /* Index of the database containing the table to lock */
77401   int iTab,          /* Root page number of the table to be locked */
77402   u8 isWriteLock,    /* True for a write lock */
77403   const char *zName  /* Name of the table to be locked */
77404 ){
77405   Parse *pToplevel = sqlite3ParseToplevel(pParse);
77406   int i;
77407   int nBytes;
77408   TableLock *p;
77409   assert( iDb>=0 );
77410 
77411   for(i=0; i<pToplevel->nTableLock; i++){
77412     p = &pToplevel->aTableLock[i];
77413     if( p->iDb==iDb && p->iTab==iTab ){
77414       p->isWriteLock = (p->isWriteLock || isWriteLock);
77415       return;
77416     }
77417   }
77418 
77419   nBytes = sizeof(TableLock) * (pToplevel->nTableLock+1);
77420   pToplevel->aTableLock =
77421       sqlite3DbReallocOrFree(pToplevel->db, pToplevel->aTableLock, nBytes);
77422   if( pToplevel->aTableLock ){
77423     p = &pToplevel->aTableLock[pToplevel->nTableLock++];
77424     p->iDb = iDb;
77425     p->iTab = iTab;
77426     p->isWriteLock = isWriteLock;
77427     p->zName = zName;
77428   }else{
77429     pToplevel->nTableLock = 0;
77430     pToplevel->db->mallocFailed = 1;
77431   }
77432 }
77433 
77434 /*
77435 ** Code an OP_TableLock instruction for each table locked by the
77436 ** statement (configured by calls to sqlite3TableLock()).
77437 */
77438 static void codeTableLocks(Parse *pParse){
77439   int i;
77440   Vdbe *pVdbe;
77441 
77442   pVdbe = sqlite3GetVdbe(pParse);
77443   assert( pVdbe!=0 ); /* sqlite3GetVdbe cannot fail: VDBE already allocated */
77444 
77445   for(i=0; i<pParse->nTableLock; i++){
77446     TableLock *p = &pParse->aTableLock[i];
77447     int p1 = p->iDb;
77448     sqlite3VdbeAddOp4(pVdbe, OP_TableLock, p1, p->iTab, p->isWriteLock,
77449                       p->zName, P4_STATIC);
77450   }
77451 }
77452 #else
77453   #define codeTableLocks(x)
77454 #endif
77455 
77456 /*
77457 ** This routine is called after a single SQL statement has been
77458 ** parsed and a VDBE program to execute that statement has been
77459 ** prepared.  This routine puts the finishing touches on the
77460 ** VDBE program and resets the pParse structure for the next
77461 ** parse.
77462 **
77463 ** Note that if an error occurred, it might be the case that
77464 ** no VDBE code was generated.
77465 */
77466 SQLITE_PRIVATE void sqlite3FinishCoding(Parse *pParse){
77467   sqlite3 *db;
77468   Vdbe *v;
77469 
77470   db = pParse->db;
77471   if( db->mallocFailed ) return;
77472   if( pParse->nested ) return;
77473   if( pParse->nErr ) return;
77474 
77475   /* Begin by generating some termination code at the end of the
77476   ** vdbe program
77477   */
77478   v = sqlite3GetVdbe(pParse);
77479   assert( !pParse->isMultiWrite
77480        || sqlite3VdbeAssertMayAbort(v, pParse->mayAbort));
77481   if( v ){
77482     sqlite3VdbeAddOp0(v, OP_Halt);
77483 
77484     /* The cookie mask contains one bit for each database file open.
77485     ** (Bit 0 is for main, bit 1 is for temp, and so forth.)  Bits are
77486     ** set for each database that is used.  Generate code to start a
77487     ** transaction on each used database and to verify the schema cookie
77488     ** on each used database.
77489     */
77490     if( pParse->cookieGoto>0 ){
77491       yDbMask mask;
77492       int iDb;
77493       sqlite3VdbeJumpHere(v, pParse->cookieGoto-1);
77494       for(iDb=0, mask=1; iDb<db->nDb; mask<<=1, iDb++){
77495         if( (mask & pParse->cookieMask)==0 ) continue;
77496         sqlite3VdbeUsesBtree(v, iDb);
77497         sqlite3VdbeAddOp2(v,OP_Transaction, iDb, (mask & pParse->writeMask)!=0);
77498         if( db->init.busy==0 ){
77499           assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
77500           sqlite3VdbeAddOp3(v, OP_VerifyCookie,
77501                             iDb, pParse->cookieValue[iDb],
77502                             db->aDb[iDb].pSchema->iGeneration);
77503         }
77504       }
77505 #ifndef SQLITE_OMIT_VIRTUALTABLE
77506       {
77507         int i;
77508         for(i=0; i<pParse->nVtabLock; i++){
77509           char *vtab = (char *)sqlite3GetVTable(db, pParse->apVtabLock[i]);
77510           sqlite3VdbeAddOp4(v, OP_VBegin, 0, 0, 0, vtab, P4_VTAB);
77511         }
77512         pParse->nVtabLock = 0;
77513       }
77514 #endif
77515 
77516       /* Once all the cookies have been verified and transactions opened,
77517       ** obtain the required table-locks. This is a no-op unless the
77518       ** shared-cache feature is enabled.
77519       */
77520       codeTableLocks(pParse);
77521 
77522       /* Initialize any AUTOINCREMENT data structures required.
77523       */
77524       sqlite3AutoincrementBegin(pParse);
77525 
77526       /* Finally, jump back to the beginning of the executable code. */
77527       sqlite3VdbeAddOp2(v, OP_Goto, 0, pParse->cookieGoto);
77528     }
77529   }
77530 
77531 
77532   /* Get the VDBE program ready for execution
77533   */
77534   if( v && ALWAYS(pParse->nErr==0) && !db->mallocFailed ){
77535 #ifdef SQLITE_DEBUG
77536     FILE *trace = (db->flags & SQLITE_VdbeTrace)!=0 ? stdout : 0;
77537     sqlite3VdbeTrace(v, trace);
77538 #endif
77539     assert( pParse->iCacheLevel==0 );  /* Disables and re-enables match */
77540     /* A minimum of one cursor is required if autoincrement is used
77541     *  See ticket [a696379c1f08866] */
77542     if( pParse->pAinc!=0 && pParse->nTab==0 ) pParse->nTab = 1;
77543     sqlite3VdbeMakeReady(v, pParse);
77544     pParse->rc = SQLITE_DONE;
77545     pParse->colNamesSet = 0;
77546   }else{
77547     pParse->rc = SQLITE_ERROR;
77548   }
77549   pParse->nTab = 0;
77550   pParse->nMem = 0;
77551   pParse->nSet = 0;
77552   pParse->nVar = 0;
77553   pParse->cookieMask = 0;
77554   pParse->cookieGoto = 0;
77555 }
77556 
77557 /*
77558 ** Run the parser and code generator recursively in order to generate
77559 ** code for the SQL statement given onto the end of the pParse context
77560 ** currently under construction.  When the parser is run recursively
77561 ** this way, the final OP_Halt is not appended and other initialization
77562 ** and finalization steps are omitted because those are handling by the
77563 ** outermost parser.
77564 **
77565 ** Not everything is nestable.  This facility is designed to permit
77566 ** INSERT, UPDATE, and DELETE operations against SQLITE_MASTER.  Use
77567 ** care if you decide to try to use this routine for some other purposes.
77568 */
77569 SQLITE_PRIVATE void sqlite3NestedParse(Parse *pParse, const char *zFormat, ...){
77570   va_list ap;
77571   char *zSql;
77572   char *zErrMsg = 0;
77573   sqlite3 *db = pParse->db;
77574 # define SAVE_SZ  (sizeof(Parse) - offsetof(Parse,nVar))
77575   char saveBuf[SAVE_SZ];
77576 
77577   if( pParse->nErr ) return;
77578   assert( pParse->nested<10 );  /* Nesting should only be of limited depth */
77579   va_start(ap, zFormat);
77580   zSql = sqlite3VMPrintf(db, zFormat, ap);
77581   va_end(ap);
77582   if( zSql==0 ){
77583     return;   /* A malloc must have failed */
77584   }
77585   pParse->nested++;
77586   memcpy(saveBuf, &pParse->nVar, SAVE_SZ);
77587   memset(&pParse->nVar, 0, SAVE_SZ);
77588   sqlite3RunParser(pParse, zSql, &zErrMsg);
77589   sqlite3DbFree(db, zErrMsg);
77590   sqlite3DbFree(db, zSql);
77591   memcpy(&pParse->nVar, saveBuf, SAVE_SZ);
77592   pParse->nested--;
77593 }
77594 
77595 /*
77596 ** Locate the in-memory structure that describes a particular database
77597 ** table given the name of that table and (optionally) the name of the
77598 ** database containing the table.  Return NULL if not found.
77599 **
77600 ** If zDatabase is 0, all databases are searched for the table and the
77601 ** first matching table is returned.  (No checking for duplicate table
77602 ** names is done.)  The search order is TEMP first, then MAIN, then any
77603 ** auxiliary databases added using the ATTACH command.
77604 **
77605 ** See also sqlite3LocateTable().
77606 */
77607 SQLITE_PRIVATE Table *sqlite3FindTable(sqlite3 *db, const char *zName, const char *zDatabase){
77608   Table *p = 0;
77609   int i;
77610   int nName;
77611   assert( zName!=0 );
77612   nName = sqlite3Strlen30(zName);
77613   /* All mutexes are required for schema access.  Make sure we hold them. */
77614   assert( zDatabase!=0 || sqlite3BtreeHoldsAllMutexes(db) );
77615   for(i=OMIT_TEMPDB; i<db->nDb; i++){
77616     int j = (i<2) ? i^1 : i;   /* Search TEMP before MAIN */
77617     if( zDatabase!=0 && sqlite3StrICmp(zDatabase, db->aDb[j].zName) ) continue;
77618     assert( sqlite3SchemaMutexHeld(db, j, 0) );
77619     p = sqlite3HashFind(&db->aDb[j].pSchema->tblHash, zName, nName);
77620     if( p ) break;
77621   }
77622   return p;
77623 }
77624 
77625 /*
77626 ** Locate the in-memory structure that describes a particular database
77627 ** table given the name of that table and (optionally) the name of the
77628 ** database containing the table.  Return NULL if not found.  Also leave an
77629 ** error message in pParse->zErrMsg.
77630 **
77631 ** The difference between this routine and sqlite3FindTable() is that this
77632 ** routine leaves an error message in pParse->zErrMsg where
77633 ** sqlite3FindTable() does not.
77634 */
77635 SQLITE_PRIVATE Table *sqlite3LocateTable(
77636   Parse *pParse,         /* context in which to report errors */
77637   int isView,            /* True if looking for a VIEW rather than a TABLE */
77638   const char *zName,     /* Name of the table we are looking for */
77639   const char *zDbase     /* Name of the database.  Might be NULL */
77640 ){
77641   Table *p;
77642 
77643   /* Read the database schema. If an error occurs, leave an error message
77644   ** and code in pParse and return NULL. */
77645   if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
77646     return 0;
77647   }
77648 
77649   p = sqlite3FindTable(pParse->db, zName, zDbase);
77650   if( p==0 ){
77651     const char *zMsg = isView ? "no such view" : "no such table";
77652     if( zDbase ){
77653       sqlite3ErrorMsg(pParse, "%s: %s.%s", zMsg, zDbase, zName);
77654     }else{
77655       sqlite3ErrorMsg(pParse, "%s: %s", zMsg, zName);
77656     }
77657     pParse->checkSchema = 1;
77658   }
77659   return p;
77660 }
77661 
77662 /*
77663 ** Locate the in-memory structure that describes
77664 ** a particular index given the name of that index
77665 ** and the name of the database that contains the index.
77666 ** Return NULL if not found.
77667 **
77668 ** If zDatabase is 0, all databases are searched for the
77669 ** table and the first matching index is returned.  (No checking
77670 ** for duplicate index names is done.)  The search order is
77671 ** TEMP first, then MAIN, then any auxiliary databases added
77672 ** using the ATTACH command.
77673 */
77674 SQLITE_PRIVATE Index *sqlite3FindIndex(sqlite3 *db, const char *zName, const char *zDb){
77675   Index *p = 0;
77676   int i;
77677   int nName = sqlite3Strlen30(zName);
77678   /* All mutexes are required for schema access.  Make sure we hold them. */
77679   assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) );
77680   for(i=OMIT_TEMPDB; i<db->nDb; i++){
77681     int j = (i<2) ? i^1 : i;  /* Search TEMP before MAIN */
77682     Schema *pSchema = db->aDb[j].pSchema;
77683     assert( pSchema );
77684     if( zDb && sqlite3StrICmp(zDb, db->aDb[j].zName) ) continue;
77685     assert( sqlite3SchemaMutexHeld(db, j, 0) );
77686     p = sqlite3HashFind(&pSchema->idxHash, zName, nName);
77687     if( p ) break;
77688   }
77689   return p;
77690 }
77691 
77692 /*
77693 ** Reclaim the memory used by an index
77694 */
77695 static void freeIndex(sqlite3 *db, Index *p){
77696 #ifndef SQLITE_OMIT_ANALYZE
77697   sqlite3DeleteIndexSamples(db, p);
77698 #endif
77699   sqlite3DbFree(db, p->zColAff);
77700   sqlite3DbFree(db, p);
77701 }
77702 
77703 /*
77704 ** For the index called zIdxName which is found in the database iDb,
77705 ** unlike that index from its Table then remove the index from
77706 ** the index hash table and free all memory structures associated
77707 ** with the index.
77708 */
77709 SQLITE_PRIVATE void sqlite3UnlinkAndDeleteIndex(sqlite3 *db, int iDb, const char *zIdxName){
77710   Index *pIndex;
77711   int len;
77712   Hash *pHash;
77713 
77714   assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
77715   pHash = &db->aDb[iDb].pSchema->idxHash;
77716   len = sqlite3Strlen30(zIdxName);
77717   pIndex = sqlite3HashInsert(pHash, zIdxName, len, 0);
77718   if( ALWAYS(pIndex) ){
77719     if( pIndex->pTable->pIndex==pIndex ){
77720       pIndex->pTable->pIndex = pIndex->pNext;
77721     }else{
77722       Index *p;
77723       /* Justification of ALWAYS();  The index must be on the list of
77724       ** indices. */
77725       p = pIndex->pTable->pIndex;
77726       while( ALWAYS(p) && p->pNext!=pIndex ){ p = p->pNext; }
77727       if( ALWAYS(p && p->pNext==pIndex) ){
77728         p->pNext = pIndex->pNext;
77729       }
77730     }
77731     freeIndex(db, pIndex);
77732   }
77733   db->flags |= SQLITE_InternChanges;
77734 }
77735 
77736 /*
77737 ** Erase all schema information from the in-memory hash tables of
77738 ** a single database.  This routine is called to reclaim memory
77739 ** before the database closes.  It is also called during a rollback
77740 ** if there were schema changes during the transaction or if a
77741 ** schema-cookie mismatch occurs.
77742 **
77743 ** If iDb<0 then reset the internal schema tables for all database
77744 ** files.  If iDb>=0 then reset the internal schema for only the
77745 ** single file indicated.
77746 */
77747 SQLITE_PRIVATE void sqlite3ResetInternalSchema(sqlite3 *db, int iDb){
77748   int i, j;
77749   assert( iDb<db->nDb );
77750 
77751   if( iDb>=0 ){
77752     /* Case 1:  Reset the single schema identified by iDb */
77753     Db *pDb = &db->aDb[iDb];
77754     assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
77755     assert( pDb->pSchema!=0 );
77756     sqlite3SchemaClear(pDb->pSchema);
77757 
77758     /* If any database other than TEMP is reset, then also reset TEMP
77759     ** since TEMP might be holding triggers that reference tables in the
77760     ** other database.
77761     */
77762     if( iDb!=1 ){
77763       pDb = &db->aDb[1];
77764       assert( pDb->pSchema!=0 );
77765       sqlite3SchemaClear(pDb->pSchema);
77766     }
77767     return;
77768   }
77769   /* Case 2 (from here to the end): Reset all schemas for all attached
77770   ** databases. */
77771   assert( iDb<0 );
77772   sqlite3BtreeEnterAll(db);
77773   for(i=0; i<db->nDb; i++){
77774     Db *pDb = &db->aDb[i];
77775     if( pDb->pSchema ){
77776       sqlite3SchemaClear(pDb->pSchema);
77777     }
77778   }
77779   db->flags &= ~SQLITE_InternChanges;
77780   sqlite3VtabUnlockList(db);
77781   sqlite3BtreeLeaveAll(db);
77782 
77783   /* If one or more of the auxiliary database files has been closed,
77784   ** then remove them from the auxiliary database list.  We take the
77785   ** opportunity to do this here since we have just deleted all of the
77786   ** schema hash tables and therefore do not have to make any changes
77787   ** to any of those tables.
77788   */
77789   for(i=j=2; i<db->nDb; i++){
77790     struct Db *pDb = &db->aDb[i];
77791     if( pDb->pBt==0 ){
77792       sqlite3DbFree(db, pDb->zName);
77793       pDb->zName = 0;
77794       continue;
77795     }
77796     if( j<i ){
77797       db->aDb[j] = db->aDb[i];
77798     }
77799     j++;
77800   }
77801   memset(&db->aDb[j], 0, (db->nDb-j)*sizeof(db->aDb[j]));
77802   db->nDb = j;
77803   if( db->nDb<=2 && db->aDb!=db->aDbStatic ){
77804     memcpy(db->aDbStatic, db->aDb, 2*sizeof(db->aDb[0]));
77805     sqlite3DbFree(db, db->aDb);
77806     db->aDb = db->aDbStatic;
77807   }
77808 }
77809 
77810 /*
77811 ** This routine is called when a commit occurs.
77812 */
77813 SQLITE_PRIVATE void sqlite3CommitInternalChanges(sqlite3 *db){
77814   db->flags &= ~SQLITE_InternChanges;
77815 }
77816 
77817 /*
77818 ** Delete memory allocated for the column names of a table or view (the
77819 ** Table.aCol[] array).
77820 */
77821 static void sqliteDeleteColumnNames(sqlite3 *db, Table *pTable){
77822   int i;
77823   Column *pCol;
77824   assert( pTable!=0 );
77825   if( (pCol = pTable->aCol)!=0 ){
77826     for(i=0; i<pTable->nCol; i++, pCol++){
77827       sqlite3DbFree(db, pCol->zName);
77828       sqlite3ExprDelete(db, pCol->pDflt);
77829       sqlite3DbFree(db, pCol->zDflt);
77830       sqlite3DbFree(db, pCol->zType);
77831       sqlite3DbFree(db, pCol->zColl);
77832     }
77833     sqlite3DbFree(db, pTable->aCol);
77834   }
77835 }
77836 
77837 /*
77838 ** Remove the memory data structures associated with the given
77839 ** Table.  No changes are made to disk by this routine.
77840 **
77841 ** This routine just deletes the data structure.  It does not unlink
77842 ** the table data structure from the hash table.  But it does destroy
77843 ** memory structures of the indices and foreign keys associated with
77844 ** the table.
77845 */
77846 SQLITE_PRIVATE void sqlite3DeleteTable(sqlite3 *db, Table *pTable){
77847   Index *pIndex, *pNext;
77848 
77849   assert( !pTable || pTable->nRef>0 );
77850 
77851   /* Do not delete the table until the reference count reaches zero. */
77852   if( !pTable ) return;
77853   if( ((!db || db->pnBytesFreed==0) && (--pTable->nRef)>0) ) return;
77854 
77855   /* Delete all indices associated with this table. */
77856   for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){
77857     pNext = pIndex->pNext;
77858     assert( pIndex->pSchema==pTable->pSchema );
77859     if( !db || db->pnBytesFreed==0 ){
77860       char *zName = pIndex->zName;
77861       TESTONLY ( Index *pOld = ) sqlite3HashInsert(
77862 	  &pIndex->pSchema->idxHash, zName, sqlite3Strlen30(zName), 0
77863       );
77864       assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
77865       assert( pOld==pIndex || pOld==0 );
77866     }
77867     freeIndex(db, pIndex);
77868   }
77869 
77870   /* Delete any foreign keys attached to this table. */
77871   sqlite3FkDelete(db, pTable);
77872 
77873   /* Delete the Table structure itself.
77874   */
77875   sqliteDeleteColumnNames(db, pTable);
77876   sqlite3DbFree(db, pTable->zName);
77877   sqlite3DbFree(db, pTable->zColAff);
77878   sqlite3SelectDelete(db, pTable->pSelect);
77879 #ifndef SQLITE_OMIT_CHECK
77880   sqlite3ExprDelete(db, pTable->pCheck);
77881 #endif
77882 #ifndef SQLITE_OMIT_VIRTUALTABLE
77883   sqlite3VtabClear(db, pTable);
77884 #endif
77885   sqlite3DbFree(db, pTable);
77886 }
77887 
77888 /*
77889 ** Unlink the given table from the hash tables and the delete the
77890 ** table structure with all its indices and foreign keys.
77891 */
77892 SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTable(sqlite3 *db, int iDb, const char *zTabName){
77893   Table *p;
77894   Db *pDb;
77895 
77896   assert( db!=0 );
77897   assert( iDb>=0 && iDb<db->nDb );
77898   assert( zTabName );
77899   assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
77900   testcase( zTabName[0]==0 );  /* Zero-length table names are allowed */
77901   pDb = &db->aDb[iDb];
77902   p = sqlite3HashInsert(&pDb->pSchema->tblHash, zTabName,
77903                         sqlite3Strlen30(zTabName),0);
77904   sqlite3DeleteTable(db, p);
77905   db->flags |= SQLITE_InternChanges;
77906 }
77907 
77908 /*
77909 ** Given a token, return a string that consists of the text of that
77910 ** token.  Space to hold the returned string
77911 ** is obtained from sqliteMalloc() and must be freed by the calling
77912 ** function.
77913 **
77914 ** Any quotation marks (ex:  "name", 'name', [name], or `name`) that
77915 ** surround the body of the token are removed.
77916 **
77917 ** Tokens are often just pointers into the original SQL text and so
77918 ** are not \000 terminated and are not persistent.  The returned string
77919 ** is \000 terminated and is persistent.
77920 */
77921 SQLITE_PRIVATE char *sqlite3NameFromToken(sqlite3 *db, Token *pName){
77922   char *zName;
77923   if( pName ){
77924     zName = sqlite3DbStrNDup(db, (char*)pName->z, pName->n);
77925     sqlite3Dequote(zName);
77926   }else{
77927     zName = 0;
77928   }
77929   return zName;
77930 }
77931 
77932 /*
77933 ** Open the sqlite_master table stored in database number iDb for
77934 ** writing. The table is opened using cursor 0.
77935 */
77936 SQLITE_PRIVATE void sqlite3OpenMasterTable(Parse *p, int iDb){
77937   Vdbe *v = sqlite3GetVdbe(p);
77938   sqlite3TableLock(p, iDb, MASTER_ROOT, 1, SCHEMA_TABLE(iDb));
77939   sqlite3VdbeAddOp3(v, OP_OpenWrite, 0, MASTER_ROOT, iDb);
77940   sqlite3VdbeChangeP4(v, -1, (char *)5, P4_INT32);  /* 5 column table */
77941   if( p->nTab==0 ){
77942     p->nTab = 1;
77943   }
77944 }
77945 
77946 /*
77947 ** Parameter zName points to a nul-terminated buffer containing the name
77948 ** of a database ("main", "temp" or the name of an attached db). This
77949 ** function returns the index of the named database in db->aDb[], or
77950 ** -1 if the named db cannot be found.
77951 */
77952 SQLITE_PRIVATE int sqlite3FindDbName(sqlite3 *db, const char *zName){
77953   int i = -1;         /* Database number */
77954   if( zName ){
77955     Db *pDb;
77956     int n = sqlite3Strlen30(zName);
77957     for(i=(db->nDb-1), pDb=&db->aDb[i]; i>=0; i--, pDb--){
77958       if( (!OMIT_TEMPDB || i!=1 ) && n==sqlite3Strlen30(pDb->zName) &&
77959           0==sqlite3StrICmp(pDb->zName, zName) ){
77960         break;
77961       }
77962     }
77963   }
77964   return i;
77965 }
77966 
77967 /*
77968 ** The token *pName contains the name of a database (either "main" or
77969 ** "temp" or the name of an attached db). This routine returns the
77970 ** index of the named database in db->aDb[], or -1 if the named db
77971 ** does not exist.
77972 */
77973 SQLITE_PRIVATE int sqlite3FindDb(sqlite3 *db, Token *pName){
77974   int i;                               /* Database number */
77975   char *zName;                         /* Name we are searching for */
77976   zName = sqlite3NameFromToken(db, pName);
77977   i = sqlite3FindDbName(db, zName);
77978   sqlite3DbFree(db, zName);
77979   return i;
77980 }
77981 
77982 /* The table or view or trigger name is passed to this routine via tokens
77983 ** pName1 and pName2. If the table name was fully qualified, for example:
77984 **
77985 ** CREATE TABLE xxx.yyy (...);
77986 **
77987 ** Then pName1 is set to "xxx" and pName2 "yyy". On the other hand if
77988 ** the table name is not fully qualified, i.e.:
77989 **
77990 ** CREATE TABLE yyy(...);
77991 **
77992 ** Then pName1 is set to "yyy" and pName2 is "".
77993 **
77994 ** This routine sets the *ppUnqual pointer to point at the token (pName1 or
77995 ** pName2) that stores the unqualified table name.  The index of the
77996 ** database "xxx" is returned.
77997 */
77998 SQLITE_PRIVATE int sqlite3TwoPartName(
77999   Parse *pParse,      /* Parsing and code generating context */
78000   Token *pName1,      /* The "xxx" in the name "xxx.yyy" or "xxx" */
78001   Token *pName2,      /* The "yyy" in the name "xxx.yyy" */
78002   Token **pUnqual     /* Write the unqualified object name here */
78003 ){
78004   int iDb;                    /* Database holding the object */
78005   sqlite3 *db = pParse->db;
78006 
78007   if( ALWAYS(pName2!=0) && pName2->n>0 ){
78008     if( db->init.busy ) {
78009       sqlite3ErrorMsg(pParse, "corrupt database");
78010       pParse->nErr++;
78011       return -1;
78012     }
78013     *pUnqual = pName2;
78014     iDb = sqlite3FindDb(db, pName1);
78015     if( iDb<0 ){
78016       sqlite3ErrorMsg(pParse, "unknown database %T", pName1);
78017       pParse->nErr++;
78018       return -1;
78019     }
78020   }else{
78021     assert( db->init.iDb==0 || db->init.busy );
78022     iDb = db->init.iDb;
78023     *pUnqual = pName1;
78024   }
78025   return iDb;
78026 }
78027 
78028 /*
78029 ** This routine is used to check if the UTF-8 string zName is a legal
78030 ** unqualified name for a new schema object (table, index, view or
78031 ** trigger). All names are legal except those that begin with the string
78032 ** "sqlite_" (in upper, lower or mixed case). This portion of the namespace
78033 ** is reserved for internal use.
78034 */
78035 SQLITE_PRIVATE int sqlite3CheckObjectName(Parse *pParse, const char *zName){
78036   if( !pParse->db->init.busy && pParse->nested==0
78037           && (pParse->db->flags & SQLITE_WriteSchema)==0
78038           && 0==sqlite3StrNICmp(zName, "sqlite_", 7) ){
78039     sqlite3ErrorMsg(pParse, "object name reserved for internal use: %s", zName);
78040     return SQLITE_ERROR;
78041   }
78042   return SQLITE_OK;
78043 }
78044 
78045 /*
78046 ** Begin constructing a new table representation in memory.  This is
78047 ** the first of several action routines that get called in response
78048 ** to a CREATE TABLE statement.  In particular, this routine is called
78049 ** after seeing tokens "CREATE" and "TABLE" and the table name. The isTemp
78050 ** flag is true if the table should be stored in the auxiliary database
78051 ** file instead of in the main database file.  This is normally the case
78052 ** when the "TEMP" or "TEMPORARY" keyword occurs in between
78053 ** CREATE and TABLE.
78054 **
78055 ** The new table record is initialized and put in pParse->pNewTable.
78056 ** As more of the CREATE TABLE statement is parsed, additional action
78057 ** routines will be called to add more information to this record.
78058 ** At the end of the CREATE TABLE statement, the sqlite3EndTable() routine
78059 ** is called to complete the construction of the new table record.
78060 */
78061 SQLITE_PRIVATE void sqlite3StartTable(
78062   Parse *pParse,   /* Parser context */
78063   Token *pName1,   /* First part of the name of the table or view */
78064   Token *pName2,   /* Second part of the name of the table or view */
78065   int isTemp,      /* True if this is a TEMP table */
78066   int isView,      /* True if this is a VIEW */
78067   int isVirtual,   /* True if this is a VIRTUAL table */
78068   int noErr        /* Do nothing if table already exists */
78069 ){
78070   Table *pTable;
78071   char *zName = 0; /* The name of the new table */
78072   sqlite3 *db = pParse->db;
78073   Vdbe *v;
78074   int iDb;         /* Database number to create the table in */
78075   Token *pName;    /* Unqualified name of the table to create */
78076 
78077   /* The table or view name to create is passed to this routine via tokens
78078   ** pName1 and pName2. If the table name was fully qualified, for example:
78079   **
78080   ** CREATE TABLE xxx.yyy (...);
78081   **
78082   ** Then pName1 is set to "xxx" and pName2 "yyy". On the other hand if
78083   ** the table name is not fully qualified, i.e.:
78084   **
78085   ** CREATE TABLE yyy(...);
78086   **
78087   ** Then pName1 is set to "yyy" and pName2 is "".
78088   **
78089   ** The call below sets the pName pointer to point at the token (pName1 or
78090   ** pName2) that stores the unqualified table name. The variable iDb is
78091   ** set to the index of the database that the table or view is to be
78092   ** created in.
78093   */
78094   iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
78095   if( iDb<0 ) return;
78096   if( !OMIT_TEMPDB && isTemp && pName2->n>0 && iDb!=1 ){
78097     /* If creating a temp table, the name may not be qualified. Unless
78098     ** the database name is "temp" anyway.  */
78099     sqlite3ErrorMsg(pParse, "temporary table name must be unqualified");
78100     return;
78101   }
78102   if( !OMIT_TEMPDB && isTemp ) iDb = 1;
78103 
78104   pParse->sNameToken = *pName;
78105   zName = sqlite3NameFromToken(db, pName);
78106   if( zName==0 ) return;
78107   if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
78108     goto begin_table_error;
78109   }
78110   if( db->init.iDb==1 ) isTemp = 1;
78111 #ifndef SQLITE_OMIT_AUTHORIZATION
78112   assert( (isTemp & 1)==isTemp );
78113   {
78114     int code;
78115     char *zDb = db->aDb[iDb].zName;
78116     if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0, zDb) ){
78117       goto begin_table_error;
78118     }
78119     if( isView ){
78120       if( !OMIT_TEMPDB && isTemp ){
78121         code = SQLITE_CREATE_TEMP_VIEW;
78122       }else{
78123         code = SQLITE_CREATE_VIEW;
78124       }
78125     }else{
78126       if( !OMIT_TEMPDB && isTemp ){
78127         code = SQLITE_CREATE_TEMP_TABLE;
78128       }else{
78129         code = SQLITE_CREATE_TABLE;
78130       }
78131     }
78132     if( !isVirtual && sqlite3AuthCheck(pParse, code, zName, 0, zDb) ){
78133       goto begin_table_error;
78134     }
78135   }
78136 #endif
78137 
78138   /* Make sure the new table name does not collide with an existing
78139   ** index or table name in the same database.  Issue an error message if
78140   ** it does. The exception is if the statement being parsed was passed
78141   ** to an sqlite3_declare_vtab() call. In that case only the column names
78142   ** and types will be used, so there is no need to test for namespace
78143   ** collisions.
78144   */
78145   if( !IN_DECLARE_VTAB ){
78146     char *zDb = db->aDb[iDb].zName;
78147     if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
78148       goto begin_table_error;
78149     }
78150     pTable = sqlite3FindTable(db, zName, zDb);
78151     if( pTable ){
78152       if( !noErr ){
78153         sqlite3ErrorMsg(pParse, "table %T already exists", pName);
78154       }else{
78155         assert( !db->init.busy );
78156         sqlite3CodeVerifySchema(pParse, iDb);
78157       }
78158       goto begin_table_error;
78159     }
78160     if( sqlite3FindIndex(db, zName, zDb)!=0 ){
78161       sqlite3ErrorMsg(pParse, "there is already an index named %s", zName);
78162       goto begin_table_error;
78163     }
78164   }
78165 
78166   pTable = sqlite3DbMallocZero(db, sizeof(Table));
78167   if( pTable==0 ){
78168     db->mallocFailed = 1;
78169     pParse->rc = SQLITE_NOMEM;
78170     pParse->nErr++;
78171     goto begin_table_error;
78172   }
78173   pTable->zName = zName;
78174   pTable->iPKey = -1;
78175   pTable->pSchema = db->aDb[iDb].pSchema;
78176   pTable->nRef = 1;
78177   pTable->nRowEst = 1000000;
78178   assert( pParse->pNewTable==0 );
78179   pParse->pNewTable = pTable;
78180 
78181   /* If this is the magic sqlite_sequence table used by autoincrement,
78182   ** then record a pointer to this table in the main database structure
78183   ** so that INSERT can find the table easily.
78184   */
78185 #ifndef SQLITE_OMIT_AUTOINCREMENT
78186   if( !pParse->nested && strcmp(zName, "sqlite_sequence")==0 ){
78187     assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
78188     pTable->pSchema->pSeqTab = pTable;
78189   }
78190 #endif
78191 
78192   /* Begin generating the code that will insert the table record into
78193   ** the SQLITE_MASTER table.  Note in particular that we must go ahead
78194   ** and allocate the record number for the table entry now.  Before any
78195   ** PRIMARY KEY or UNIQUE keywords are parsed.  Those keywords will cause
78196   ** indices to be created and the table record must come before the
78197   ** indices.  Hence, the record number for the table must be allocated
78198   ** now.
78199   */
78200   if( !db->init.busy && (v = sqlite3GetVdbe(pParse))!=0 ){
78201     int j1;
78202     int fileFormat;
78203     int reg1, reg2, reg3;
78204     sqlite3BeginWriteOperation(pParse, 0, iDb);
78205 
78206 #ifndef SQLITE_OMIT_VIRTUALTABLE
78207     if( isVirtual ){
78208       sqlite3VdbeAddOp0(v, OP_VBegin);
78209     }
78210 #endif
78211 
78212     /* If the file format and encoding in the database have not been set,
78213     ** set them now.
78214     */
78215     reg1 = pParse->regRowid = ++pParse->nMem;
78216     reg2 = pParse->regRoot = ++pParse->nMem;
78217     reg3 = ++pParse->nMem;
78218     sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, reg3, BTREE_FILE_FORMAT);
78219     sqlite3VdbeUsesBtree(v, iDb);
78220     j1 = sqlite3VdbeAddOp1(v, OP_If, reg3);
78221     fileFormat = (db->flags & SQLITE_LegacyFileFmt)!=0 ?
78222                   1 : SQLITE_MAX_FILE_FORMAT;
78223     sqlite3VdbeAddOp2(v, OP_Integer, fileFormat, reg3);
78224     sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, reg3);
78225     sqlite3VdbeAddOp2(v, OP_Integer, ENC(db), reg3);
78226     sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_TEXT_ENCODING, reg3);
78227     sqlite3VdbeJumpHere(v, j1);
78228 
78229     /* This just creates a place-holder record in the sqlite_master table.
78230     ** The record created does not contain anything yet.  It will be replaced
78231     ** by the real entry in code generated at sqlite3EndTable().
78232     **
78233     ** The rowid for the new entry is left in register pParse->regRowid.
78234     ** The root page number of the new table is left in reg pParse->regRoot.
78235     ** The rowid and root page number values are needed by the code that
78236     ** sqlite3EndTable will generate.
78237     */
78238 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
78239     if( isView || isVirtual ){
78240       sqlite3VdbeAddOp2(v, OP_Integer, 0, reg2);
78241     }else
78242 #endif
78243     {
78244       sqlite3VdbeAddOp2(v, OP_CreateTable, iDb, reg2);
78245     }
78246     sqlite3OpenMasterTable(pParse, iDb);
78247     sqlite3VdbeAddOp2(v, OP_NewRowid, 0, reg1);
78248     sqlite3VdbeAddOp2(v, OP_Null, 0, reg3);
78249     sqlite3VdbeAddOp3(v, OP_Insert, 0, reg3, reg1);
78250     sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
78251     sqlite3VdbeAddOp0(v, OP_Close);
78252   }
78253 
78254   /* Normal (non-error) return. */
78255   return;
78256 
78257   /* If an error occurs, we jump here */
78258 begin_table_error:
78259   sqlite3DbFree(db, zName);
78260   return;
78261 }
78262 
78263 /*
78264 ** This macro is used to compare two strings in a case-insensitive manner.
78265 ** It is slightly faster than calling sqlite3StrICmp() directly, but
78266 ** produces larger code.
78267 **
78268 ** WARNING: This macro is not compatible with the strcmp() family. It
78269 ** returns true if the two strings are equal, otherwise false.
78270 */
78271 #define STRICMP(x, y) (\
78272 sqlite3UpperToLower[*(unsigned char *)(x)]==   \
78273 sqlite3UpperToLower[*(unsigned char *)(y)]     \
78274 && sqlite3StrICmp((x)+1,(y)+1)==0 )
78275 
78276 /*
78277 ** Add a new column to the table currently being constructed.
78278 **
78279 ** The parser calls this routine once for each column declaration
78280 ** in a CREATE TABLE statement.  sqlite3StartTable() gets called
78281 ** first to get things going.  Then this routine is called for each
78282 ** column.
78283 */
78284 SQLITE_PRIVATE void sqlite3AddColumn(Parse *pParse, Token *pName){
78285   Table *p;
78286   int i;
78287   char *z;
78288   Column *pCol;
78289   sqlite3 *db = pParse->db;
78290   if( (p = pParse->pNewTable)==0 ) return;
78291 #if SQLITE_MAX_COLUMN
78292   if( p->nCol+1>db->aLimit[SQLITE_LIMIT_COLUMN] ){
78293     sqlite3ErrorMsg(pParse, "too many columns on %s", p->zName);
78294     return;
78295   }
78296 #endif
78297   z = sqlite3NameFromToken(db, pName);
78298   if( z==0 ) return;
78299   for(i=0; i<p->nCol; i++){
78300     if( STRICMP(z, p->aCol[i].zName) ){
78301       sqlite3ErrorMsg(pParse, "duplicate column name: %s", z);
78302       sqlite3DbFree(db, z);
78303       return;
78304     }
78305   }
78306   if( (p->nCol & 0x7)==0 ){
78307     Column *aNew;
78308     aNew = sqlite3DbRealloc(db,p->aCol,(p->nCol+8)*sizeof(p->aCol[0]));
78309     if( aNew==0 ){
78310       sqlite3DbFree(db, z);
78311       return;
78312     }
78313     p->aCol = aNew;
78314   }
78315   pCol = &p->aCol[p->nCol];
78316   memset(pCol, 0, sizeof(p->aCol[0]));
78317   pCol->zName = z;
78318 
78319   /* If there is no type specified, columns have the default affinity
78320   ** 'NONE'. If there is a type specified, then sqlite3AddColumnType() will
78321   ** be called next to set pCol->affinity correctly.
78322   */
78323   pCol->affinity = SQLITE_AFF_NONE;
78324   p->nCol++;
78325 }
78326 
78327 /*
78328 ** This routine is called by the parser while in the middle of
78329 ** parsing a CREATE TABLE statement.  A "NOT NULL" constraint has
78330 ** been seen on a column.  This routine sets the notNull flag on
78331 ** the column currently under construction.
78332 */
78333 SQLITE_PRIVATE void sqlite3AddNotNull(Parse *pParse, int onError){
78334   Table *p;
78335   p = pParse->pNewTable;
78336   if( p==0 || NEVER(p->nCol<1) ) return;
78337   p->aCol[p->nCol-1].notNull = (u8)onError;
78338 }
78339 
78340 /*
78341 ** Scan the column type name zType (length nType) and return the
78342 ** associated affinity type.
78343 **
78344 ** This routine does a case-independent search of zType for the
78345 ** substrings in the following table. If one of the substrings is
78346 ** found, the corresponding affinity is returned. If zType contains
78347 ** more than one of the substrings, entries toward the top of
78348 ** the table take priority. For example, if zType is 'BLOBINT',
78349 ** SQLITE_AFF_INTEGER is returned.
78350 **
78351 ** Substring     | Affinity
78352 ** --------------------------------
78353 ** 'INT'         | SQLITE_AFF_INTEGER
78354 ** 'CHAR'        | SQLITE_AFF_TEXT
78355 ** 'CLOB'        | SQLITE_AFF_TEXT
78356 ** 'TEXT'        | SQLITE_AFF_TEXT
78357 ** 'BLOB'        | SQLITE_AFF_NONE
78358 ** 'REAL'        | SQLITE_AFF_REAL
78359 ** 'FLOA'        | SQLITE_AFF_REAL
78360 ** 'DOUB'        | SQLITE_AFF_REAL
78361 **
78362 ** If none of the substrings in the above table are found,
78363 ** SQLITE_AFF_NUMERIC is returned.
78364 */
78365 SQLITE_PRIVATE char sqlite3AffinityType(const char *zIn){
78366   u32 h = 0;
78367   char aff = SQLITE_AFF_NUMERIC;
78368 
78369   if( zIn ) while( zIn[0] ){
78370     h = (h<<8) + sqlite3UpperToLower[(*zIn)&0xff];
78371     zIn++;
78372     if( h==(('c'<<24)+('h'<<16)+('a'<<8)+'r') ){             /* CHAR */
78373       aff = SQLITE_AFF_TEXT;
78374     }else if( h==(('c'<<24)+('l'<<16)+('o'<<8)+'b') ){       /* CLOB */
78375       aff = SQLITE_AFF_TEXT;
78376     }else if( h==(('t'<<24)+('e'<<16)+('x'<<8)+'t') ){       /* TEXT */
78377       aff = SQLITE_AFF_TEXT;
78378     }else if( h==(('b'<<24)+('l'<<16)+('o'<<8)+'b')          /* BLOB */
78379         && (aff==SQLITE_AFF_NUMERIC || aff==SQLITE_AFF_REAL) ){
78380       aff = SQLITE_AFF_NONE;
78381 #ifndef SQLITE_OMIT_FLOATING_POINT
78382     }else if( h==(('r'<<24)+('e'<<16)+('a'<<8)+'l')          /* REAL */
78383         && aff==SQLITE_AFF_NUMERIC ){
78384       aff = SQLITE_AFF_REAL;
78385     }else if( h==(('f'<<24)+('l'<<16)+('o'<<8)+'a')          /* FLOA */
78386         && aff==SQLITE_AFF_NUMERIC ){
78387       aff = SQLITE_AFF_REAL;
78388     }else if( h==(('d'<<24)+('o'<<16)+('u'<<8)+'b')          /* DOUB */
78389         && aff==SQLITE_AFF_NUMERIC ){
78390       aff = SQLITE_AFF_REAL;
78391 #endif
78392     }else if( (h&0x00FFFFFF)==(('i'<<16)+('n'<<8)+'t') ){    /* INT */
78393       aff = SQLITE_AFF_INTEGER;
78394       break;
78395     }
78396   }
78397 
78398   return aff;
78399 }
78400 
78401 /*
78402 ** This routine is called by the parser while in the middle of
78403 ** parsing a CREATE TABLE statement.  The pFirst token is the first
78404 ** token in the sequence of tokens that describe the type of the
78405 ** column currently under construction.   pLast is the last token
78406 ** in the sequence.  Use this information to construct a string
78407 ** that contains the typename of the column and store that string
78408 ** in zType.
78409 */
78410 SQLITE_PRIVATE void sqlite3AddColumnType(Parse *pParse, Token *pType){
78411   Table *p;
78412   Column *pCol;
78413 
78414   p = pParse->pNewTable;
78415   if( p==0 || NEVER(p->nCol<1) ) return;
78416   pCol = &p->aCol[p->nCol-1];
78417   assert( pCol->zType==0 );
78418   pCol->zType = sqlite3NameFromToken(pParse->db, pType);
78419   pCol->affinity = sqlite3AffinityType(pCol->zType);
78420 }
78421 
78422 /*
78423 ** The expression is the default value for the most recently added column
78424 ** of the table currently under construction.
78425 **
78426 ** Default value expressions must be constant.  Raise an exception if this
78427 ** is not the case.
78428 **
78429 ** This routine is called by the parser while in the middle of
78430 ** parsing a CREATE TABLE statement.
78431 */
78432 SQLITE_PRIVATE void sqlite3AddDefaultValue(Parse *pParse, ExprSpan *pSpan){
78433   Table *p;
78434   Column *pCol;
78435   sqlite3 *db = pParse->db;
78436   p = pParse->pNewTable;
78437   if( p!=0 ){
78438     pCol = &(p->aCol[p->nCol-1]);
78439     if( !sqlite3ExprIsConstantOrFunction(pSpan->pExpr) ){
78440       sqlite3ErrorMsg(pParse, "default value of column [%s] is not constant",
78441           pCol->zName);
78442     }else{
78443       /* A copy of pExpr is used instead of the original, as pExpr contains
78444       ** tokens that point to volatile memory. The 'span' of the expression
78445       ** is required by pragma table_info.
78446       */
78447       sqlite3ExprDelete(db, pCol->pDflt);
78448       pCol->pDflt = sqlite3ExprDup(db, pSpan->pExpr, EXPRDUP_REDUCE);
78449       sqlite3DbFree(db, pCol->zDflt);
78450       pCol->zDflt = sqlite3DbStrNDup(db, (char*)pSpan->zStart,
78451                                      (int)(pSpan->zEnd - pSpan->zStart));
78452     }
78453   }
78454   sqlite3ExprDelete(db, pSpan->pExpr);
78455 }
78456 
78457 /*
78458 ** Designate the PRIMARY KEY for the table.  pList is a list of names
78459 ** of columns that form the primary key.  If pList is NULL, then the
78460 ** most recently added column of the table is the primary key.
78461 **
78462 ** A table can have at most one primary key.  If the table already has
78463 ** a primary key (and this is the second primary key) then create an
78464 ** error.
78465 **
78466 ** If the PRIMARY KEY is on a single column whose datatype is INTEGER,
78467 ** then we will try to use that column as the rowid.  Set the Table.iPKey
78468 ** field of the table under construction to be the index of the
78469 ** INTEGER PRIMARY KEY column.  Table.iPKey is set to -1 if there is
78470 ** no INTEGER PRIMARY KEY.
78471 **
78472 ** If the key is not an INTEGER PRIMARY KEY, then create a unique
78473 ** index for the key.  No index is created for INTEGER PRIMARY KEYs.
78474 */
78475 SQLITE_PRIVATE void sqlite3AddPrimaryKey(
78476   Parse *pParse,    /* Parsing context */
78477   ExprList *pList,  /* List of field names to be indexed */
78478   int onError,      /* What to do with a uniqueness conflict */
78479   int autoInc,      /* True if the AUTOINCREMENT keyword is present */
78480   int sortOrder     /* SQLITE_SO_ASC or SQLITE_SO_DESC */
78481 ){
78482   Table *pTab = pParse->pNewTable;
78483   char *zType = 0;
78484   int iCol = -1, i;
78485   if( pTab==0 || IN_DECLARE_VTAB ) goto primary_key_exit;
78486   if( pTab->tabFlags & TF_HasPrimaryKey ){
78487     sqlite3ErrorMsg(pParse,
78488       "table \"%s\" has more than one primary key", pTab->zName);
78489     goto primary_key_exit;
78490   }
78491   pTab->tabFlags |= TF_HasPrimaryKey;
78492   if( pList==0 ){
78493     iCol = pTab->nCol - 1;
78494     pTab->aCol[iCol].isPrimKey = 1;
78495   }else{
78496     for(i=0; i<pList->nExpr; i++){
78497       for(iCol=0; iCol<pTab->nCol; iCol++){
78498         if( sqlite3StrICmp(pList->a[i].zName, pTab->aCol[iCol].zName)==0 ){
78499           break;
78500         }
78501       }
78502       if( iCol<pTab->nCol ){
78503         pTab->aCol[iCol].isPrimKey = 1;
78504       }
78505     }
78506     if( pList->nExpr>1 ) iCol = -1;
78507   }
78508   if( iCol>=0 && iCol<pTab->nCol ){
78509     zType = pTab->aCol[iCol].zType;
78510   }
78511   if( zType && sqlite3StrICmp(zType, "INTEGER")==0
78512         && sortOrder==SQLITE_SO_ASC ){
78513     pTab->iPKey = iCol;
78514     pTab->keyConf = (u8)onError;
78515     assert( autoInc==0 || autoInc==1 );
78516     pTab->tabFlags |= autoInc*TF_Autoincrement;
78517   }else if( autoInc ){
78518 #ifndef SQLITE_OMIT_AUTOINCREMENT
78519     sqlite3ErrorMsg(pParse, "AUTOINCREMENT is only allowed on an "
78520        "INTEGER PRIMARY KEY");
78521 #endif
78522   }else{
78523     Index *p;
78524     p = sqlite3CreateIndex(pParse, 0, 0, 0, pList, onError, 0, 0, sortOrder, 0);
78525     if( p ){
78526       p->autoIndex = 2;
78527     }
78528     pList = 0;
78529   }
78530 
78531 primary_key_exit:
78532   sqlite3ExprListDelete(pParse->db, pList);
78533   return;
78534 }
78535 
78536 /*
78537 ** Add a new CHECK constraint to the table currently under construction.
78538 */
78539 SQLITE_PRIVATE void sqlite3AddCheckConstraint(
78540   Parse *pParse,    /* Parsing context */
78541   Expr *pCheckExpr  /* The check expression */
78542 ){
78543   sqlite3 *db = pParse->db;
78544 #ifndef SQLITE_OMIT_CHECK
78545   Table *pTab = pParse->pNewTable;
78546   if( pTab && !IN_DECLARE_VTAB ){
78547     pTab->pCheck = sqlite3ExprAnd(db, pTab->pCheck, pCheckExpr);
78548   }else
78549 #endif
78550   {
78551     sqlite3ExprDelete(db, pCheckExpr);
78552   }
78553 }
78554 
78555 /*
78556 ** Set the collation function of the most recently parsed table column
78557 ** to the CollSeq given.
78558 */
78559 SQLITE_PRIVATE void sqlite3AddCollateType(Parse *pParse, Token *pToken){
78560   Table *p;
78561   int i;
78562   char *zColl;              /* Dequoted name of collation sequence */
78563   sqlite3 *db;
78564 
78565   if( (p = pParse->pNewTable)==0 ) return;
78566   i = p->nCol-1;
78567   db = pParse->db;
78568   zColl = sqlite3NameFromToken(db, pToken);
78569   if( !zColl ) return;
78570 
78571   if( sqlite3LocateCollSeq(pParse, zColl) ){
78572     Index *pIdx;
78573     p->aCol[i].zColl = zColl;
78574 
78575     /* If the column is declared as "<name> PRIMARY KEY COLLATE <type>",
78576     ** then an index may have been created on this column before the
78577     ** collation type was added. Correct this if it is the case.
78578     */
78579     for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
78580       assert( pIdx->nColumn==1 );
78581       if( pIdx->aiColumn[0]==i ){
78582         pIdx->azColl[0] = p->aCol[i].zColl;
78583       }
78584     }
78585   }else{
78586     sqlite3DbFree(db, zColl);
78587   }
78588 }
78589 
78590 /*
78591 ** This function returns the collation sequence for database native text
78592 ** encoding identified by the string zName, length nName.
78593 **
78594 ** If the requested collation sequence is not available, or not available
78595 ** in the database native encoding, the collation factory is invoked to
78596 ** request it. If the collation factory does not supply such a sequence,
78597 ** and the sequence is available in another text encoding, then that is
78598 ** returned instead.
78599 **
78600 ** If no versions of the requested collations sequence are available, or
78601 ** another error occurs, NULL is returned and an error message written into
78602 ** pParse.
78603 **
78604 ** This routine is a wrapper around sqlite3FindCollSeq().  This routine
78605 ** invokes the collation factory if the named collation cannot be found
78606 ** and generates an error message.
78607 **
78608 ** See also: sqlite3FindCollSeq(), sqlite3GetCollSeq()
78609 */
78610 SQLITE_PRIVATE CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char *zName){
78611   sqlite3 *db = pParse->db;
78612   u8 enc = ENC(db);
78613   u8 initbusy = db->init.busy;
78614   CollSeq *pColl;
78615 
78616   pColl = sqlite3FindCollSeq(db, enc, zName, initbusy);
78617   if( !initbusy && (!pColl || !pColl->xCmp) ){
78618     pColl = sqlite3GetCollSeq(db, enc, pColl, zName);
78619     if( !pColl ){
78620       sqlite3ErrorMsg(pParse, "no such collation sequence: %s", zName);
78621     }
78622   }
78623 
78624   return pColl;
78625 }
78626 
78627 
78628 /*
78629 ** Generate code that will increment the schema cookie.
78630 **
78631 ** The schema cookie is used to determine when the schema for the
78632 ** database changes.  After each schema change, the cookie value
78633 ** changes.  When a process first reads the schema it records the
78634 ** cookie.  Thereafter, whenever it goes to access the database,
78635 ** it checks the cookie to make sure the schema has not changed
78636 ** since it was last read.
78637 **
78638 ** This plan is not completely bullet-proof.  It is possible for
78639 ** the schema to change multiple times and for the cookie to be
78640 ** set back to prior value.  But schema changes are infrequent
78641 ** and the probability of hitting the same cookie value is only
78642 ** 1 chance in 2^32.  So we're safe enough.
78643 */
78644 SQLITE_PRIVATE void sqlite3ChangeCookie(Parse *pParse, int iDb){
78645   int r1 = sqlite3GetTempReg(pParse);
78646   sqlite3 *db = pParse->db;
78647   Vdbe *v = pParse->pVdbe;
78648   assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
78649   sqlite3VdbeAddOp2(v, OP_Integer, db->aDb[iDb].pSchema->schema_cookie+1, r1);
78650   sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_SCHEMA_VERSION, r1);
78651   sqlite3ReleaseTempReg(pParse, r1);
78652 }
78653 
78654 /*
78655 ** Measure the number of characters needed to output the given
78656 ** identifier.  The number returned includes any quotes used
78657 ** but does not include the null terminator.
78658 **
78659 ** The estimate is conservative.  It might be larger that what is
78660 ** really needed.
78661 */
78662 static int identLength(const char *z){
78663   int n;
78664   for(n=0; *z; n++, z++){
78665     if( *z=='"' ){ n++; }
78666   }
78667   return n + 2;
78668 }
78669 
78670 /*
78671 ** The first parameter is a pointer to an output buffer. The second
78672 ** parameter is a pointer to an integer that contains the offset at
78673 ** which to write into the output buffer. This function copies the
78674 ** nul-terminated string pointed to by the third parameter, zSignedIdent,
78675 ** to the specified offset in the buffer and updates *pIdx to refer
78676 ** to the first byte after the last byte written before returning.
78677 **
78678 ** If the string zSignedIdent consists entirely of alpha-numeric
78679 ** characters, does not begin with a digit and is not an SQL keyword,
78680 ** then it is copied to the output buffer exactly as it is. Otherwise,
78681 ** it is quoted using double-quotes.
78682 */
78683 static void identPut(char *z, int *pIdx, char *zSignedIdent){
78684   unsigned char *zIdent = (unsigned char*)zSignedIdent;
78685   int i, j, needQuote;
78686   i = *pIdx;
78687 
78688   for(j=0; zIdent[j]; j++){
78689     if( !sqlite3Isalnum(zIdent[j]) && zIdent[j]!='_' ) break;
78690   }
78691   needQuote = sqlite3Isdigit(zIdent[0]) || sqlite3KeywordCode(zIdent, j)!=TK_ID;
78692   if( !needQuote ){
78693     needQuote = zIdent[j];
78694   }
78695 
78696   if( needQuote ) z[i++] = '"';
78697   for(j=0; zIdent[j]; j++){
78698     z[i++] = zIdent[j];
78699     if( zIdent[j]=='"' ) z[i++] = '"';
78700   }
78701   if( needQuote ) z[i++] = '"';
78702   z[i] = 0;
78703   *pIdx = i;
78704 }
78705 
78706 /*
78707 ** Generate a CREATE TABLE statement appropriate for the given
78708 ** table.  Memory to hold the text of the statement is obtained
78709 ** from sqliteMalloc() and must be freed by the calling function.
78710 */
78711 static char *createTableStmt(sqlite3 *db, Table *p){
78712   int i, k, n;
78713   char *zStmt;
78714   char *zSep, *zSep2, *zEnd;
78715   Column *pCol;
78716   n = 0;
78717   for(pCol = p->aCol, i=0; i<p->nCol; i++, pCol++){
78718     n += identLength(pCol->zName) + 5;
78719   }
78720   n += identLength(p->zName);
78721   if( n<50 ){
78722     zSep = "";
78723     zSep2 = ",";
78724     zEnd = ")";
78725   }else{
78726     zSep = "\n  ";
78727     zSep2 = ",\n  ";
78728     zEnd = "\n)";
78729   }
78730   n += 35 + 6*p->nCol;
78731   zStmt = sqlite3DbMallocRaw(0, n);
78732   if( zStmt==0 ){
78733     db->mallocFailed = 1;
78734     return 0;
78735   }
78736   sqlite3_snprintf(n, zStmt, "CREATE TABLE ");
78737   k = sqlite3Strlen30(zStmt);
78738   identPut(zStmt, &k, p->zName);
78739   zStmt[k++] = '(';
78740   for(pCol=p->aCol, i=0; i<p->nCol; i++, pCol++){
78741     static const char * const azType[] = {
78742         /* SQLITE_AFF_TEXT    */ " TEXT",
78743         /* SQLITE_AFF_NONE    */ "",
78744         /* SQLITE_AFF_NUMERIC */ " NUM",
78745         /* SQLITE_AFF_INTEGER */ " INT",
78746         /* SQLITE_AFF_REAL    */ " REAL"
78747     };
78748     int len;
78749     const char *zType;
78750 
78751     sqlite3_snprintf(n-k, &zStmt[k], zSep);
78752     k += sqlite3Strlen30(&zStmt[k]);
78753     zSep = zSep2;
78754     identPut(zStmt, &k, pCol->zName);
78755     assert( pCol->affinity-SQLITE_AFF_TEXT >= 0 );
78756     assert( pCol->affinity-SQLITE_AFF_TEXT < ArraySize(azType) );
78757     testcase( pCol->affinity==SQLITE_AFF_TEXT );
78758     testcase( pCol->affinity==SQLITE_AFF_NONE );
78759     testcase( pCol->affinity==SQLITE_AFF_NUMERIC );
78760     testcase( pCol->affinity==SQLITE_AFF_INTEGER );
78761     testcase( pCol->affinity==SQLITE_AFF_REAL );
78762 
78763     zType = azType[pCol->affinity - SQLITE_AFF_TEXT];
78764     len = sqlite3Strlen30(zType);
78765     assert( pCol->affinity==SQLITE_AFF_NONE
78766             || pCol->affinity==sqlite3AffinityType(zType) );
78767     memcpy(&zStmt[k], zType, len);
78768     k += len;
78769     assert( k<=n );
78770   }
78771   sqlite3_snprintf(n-k, &zStmt[k], "%s", zEnd);
78772   return zStmt;
78773 }
78774 
78775 /*
78776 ** This routine is called to report the final ")" that terminates
78777 ** a CREATE TABLE statement.
78778 **
78779 ** The table structure that other action routines have been building
78780 ** is added to the internal hash tables, assuming no errors have
78781 ** occurred.
78782 **
78783 ** An entry for the table is made in the master table on disk, unless
78784 ** this is a temporary table or db->init.busy==1.  When db->init.busy==1
78785 ** it means we are reading the sqlite_master table because we just
78786 ** connected to the database or because the sqlite_master table has
78787 ** recently changed, so the entry for this table already exists in
78788 ** the sqlite_master table.  We do not want to create it again.
78789 **
78790 ** If the pSelect argument is not NULL, it means that this routine
78791 ** was called to create a table generated from a
78792 ** "CREATE TABLE ... AS SELECT ..." statement.  The column names of
78793 ** the new table will match the result set of the SELECT.
78794 */
78795 SQLITE_PRIVATE void sqlite3EndTable(
78796   Parse *pParse,          /* Parse context */
78797   Token *pCons,           /* The ',' token after the last column defn. */
78798   Token *pEnd,            /* The final ')' token in the CREATE TABLE */
78799   Select *pSelect         /* Select from a "CREATE ... AS SELECT" */
78800 ){
78801   Table *p;
78802   sqlite3 *db = pParse->db;
78803   int iDb;
78804 
78805   if( (pEnd==0 && pSelect==0) || db->mallocFailed ){
78806     return;
78807   }
78808   p = pParse->pNewTable;
78809   if( p==0 ) return;
78810 
78811   assert( !db->init.busy || !pSelect );
78812 
78813   iDb = sqlite3SchemaToIndex(db, p->pSchema);
78814 
78815 #ifndef SQLITE_OMIT_CHECK
78816   /* Resolve names in all CHECK constraint expressions.
78817   */
78818   if( p->pCheck ){
78819     SrcList sSrc;                   /* Fake SrcList for pParse->pNewTable */
78820     NameContext sNC;                /* Name context for pParse->pNewTable */
78821 
78822     memset(&sNC, 0, sizeof(sNC));
78823     memset(&sSrc, 0, sizeof(sSrc));
78824     sSrc.nSrc = 1;
78825     sSrc.a[0].zName = p->zName;
78826     sSrc.a[0].pTab = p;
78827     sSrc.a[0].iCursor = -1;
78828     sNC.pParse = pParse;
78829     sNC.pSrcList = &sSrc;
78830     sNC.isCheck = 1;
78831     if( sqlite3ResolveExprNames(&sNC, p->pCheck) ){
78832       return;
78833     }
78834   }
78835 #endif /* !defined(SQLITE_OMIT_CHECK) */
78836 
78837   /* If the db->init.busy is 1 it means we are reading the SQL off the
78838   ** "sqlite_master" or "sqlite_temp_master" table on the disk.
78839   ** So do not write to the disk again.  Extract the root page number
78840   ** for the table from the db->init.newTnum field.  (The page number
78841   ** should have been put there by the sqliteOpenCb routine.)
78842   */
78843   if( db->init.busy ){
78844     p->tnum = db->init.newTnum;
78845   }
78846 
78847   /* If not initializing, then create a record for the new table
78848   ** in the SQLITE_MASTER table of the database.
78849   **
78850   ** If this is a TEMPORARY table, write the entry into the auxiliary
78851   ** file instead of into the main database file.
78852   */
78853   if( !db->init.busy ){
78854     int n;
78855     Vdbe *v;
78856     char *zType;    /* "view" or "table" */
78857     char *zType2;   /* "VIEW" or "TABLE" */
78858     char *zStmt;    /* Text of the CREATE TABLE or CREATE VIEW statement */
78859 
78860     v = sqlite3GetVdbe(pParse);
78861     if( NEVER(v==0) ) return;
78862 
78863     sqlite3VdbeAddOp1(v, OP_Close, 0);
78864 
78865     /*
78866     ** Initialize zType for the new view or table.
78867     */
78868     if( p->pSelect==0 ){
78869       /* A regular table */
78870       zType = "table";
78871       zType2 = "TABLE";
78872 #ifndef SQLITE_OMIT_VIEW
78873     }else{
78874       /* A view */
78875       zType = "view";
78876       zType2 = "VIEW";
78877 #endif
78878     }
78879 
78880     /* If this is a CREATE TABLE xx AS SELECT ..., execute the SELECT
78881     ** statement to populate the new table. The root-page number for the
78882     ** new table is in register pParse->regRoot.
78883     **
78884     ** Once the SELECT has been coded by sqlite3Select(), it is in a
78885     ** suitable state to query for the column names and types to be used
78886     ** by the new table.
78887     **
78888     ** A shared-cache write-lock is not required to write to the new table,
78889     ** as a schema-lock must have already been obtained to create it. Since
78890     ** a schema-lock excludes all other database users, the write-lock would
78891     ** be redundant.
78892     */
78893     if( pSelect ){
78894       SelectDest dest;
78895       Table *pSelTab;
78896 
78897       assert(pParse->nTab==1);
78898       sqlite3VdbeAddOp3(v, OP_OpenWrite, 1, pParse->regRoot, iDb);
78899       sqlite3VdbeChangeP5(v, 1);
78900       pParse->nTab = 2;
78901       sqlite3SelectDestInit(&dest, SRT_Table, 1);
78902       sqlite3Select(pParse, pSelect, &dest);
78903       sqlite3VdbeAddOp1(v, OP_Close, 1);
78904       if( pParse->nErr==0 ){
78905         pSelTab = sqlite3ResultSetOfSelect(pParse, pSelect);
78906         if( pSelTab==0 ) return;
78907         assert( p->aCol==0 );
78908         p->nCol = pSelTab->nCol;
78909         p->aCol = pSelTab->aCol;
78910         pSelTab->nCol = 0;
78911         pSelTab->aCol = 0;
78912         sqlite3DeleteTable(db, pSelTab);
78913       }
78914     }
78915 
78916     /* Compute the complete text of the CREATE statement */
78917     if( pSelect ){
78918       zStmt = createTableStmt(db, p);
78919     }else{
78920       n = (int)(pEnd->z - pParse->sNameToken.z) + 1;
78921       zStmt = sqlite3MPrintf(db,
78922           "CREATE %s %.*s", zType2, n, pParse->sNameToken.z
78923       );
78924     }
78925 
78926     /* A slot for the record has already been allocated in the
78927     ** SQLITE_MASTER table.  We just need to update that slot with all
78928     ** the information we've collected.
78929     */
78930     sqlite3NestedParse(pParse,
78931       "UPDATE %Q.%s "
78932          "SET type='%s', name=%Q, tbl_name=%Q, rootpage=#%d, sql=%Q "
78933        "WHERE rowid=#%d",
78934       db->aDb[iDb].zName, SCHEMA_TABLE(iDb),
78935       zType,
78936       p->zName,
78937       p->zName,
78938       pParse->regRoot,
78939       zStmt,
78940       pParse->regRowid
78941     );
78942     sqlite3DbFree(db, zStmt);
78943     sqlite3ChangeCookie(pParse, iDb);
78944 
78945 #ifndef SQLITE_OMIT_AUTOINCREMENT
78946     /* Check to see if we need to create an sqlite_sequence table for
78947     ** keeping track of autoincrement keys.
78948     */
78949     if( p->tabFlags & TF_Autoincrement ){
78950       Db *pDb = &db->aDb[iDb];
78951       assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
78952       if( pDb->pSchema->pSeqTab==0 ){
78953         sqlite3NestedParse(pParse,
78954           "CREATE TABLE %Q.sqlite_sequence(name,seq)",
78955           pDb->zName
78956         );
78957       }
78958     }
78959 #endif
78960 
78961     /* Reparse everything to update our internal data structures */
78962     sqlite3VdbeAddParseSchemaOp(v, iDb,
78963                sqlite3MPrintf(db, "tbl_name='%q'", p->zName));
78964   }
78965 
78966 
78967   /* Add the table to the in-memory representation of the database.
78968   */
78969   if( db->init.busy ){
78970     Table *pOld;
78971     Schema *pSchema = p->pSchema;
78972     assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
78973     pOld = sqlite3HashInsert(&pSchema->tblHash, p->zName,
78974                              sqlite3Strlen30(p->zName),p);
78975     if( pOld ){
78976       assert( p==pOld );  /* Malloc must have failed inside HashInsert() */
78977       db->mallocFailed = 1;
78978       return;
78979     }
78980     pParse->pNewTable = 0;
78981     db->nTable++;
78982     db->flags |= SQLITE_InternChanges;
78983 
78984 #ifndef SQLITE_OMIT_ALTERTABLE
78985     if( !p->pSelect ){
78986       const char *zName = (const char *)pParse->sNameToken.z;
78987       int nName;
78988       assert( !pSelect && pCons && pEnd );
78989       if( pCons->z==0 ){
78990         pCons = pEnd;
78991       }
78992       nName = (int)((const char *)pCons->z - zName);
78993       p->addColOffset = 13 + sqlite3Utf8CharLen(zName, nName);
78994     }
78995 #endif
78996   }
78997 }
78998 
78999 #ifndef SQLITE_OMIT_VIEW
79000 /*
79001 ** The parser calls this routine in order to create a new VIEW
79002 */
79003 SQLITE_PRIVATE void sqlite3CreateView(
79004   Parse *pParse,     /* The parsing context */
79005   Token *pBegin,     /* The CREATE token that begins the statement */
79006   Token *pName1,     /* The token that holds the name of the view */
79007   Token *pName2,     /* The token that holds the name of the view */
79008   Select *pSelect,   /* A SELECT statement that will become the new view */
79009   int isTemp,        /* TRUE for a TEMPORARY view */
79010   int noErr          /* Suppress error messages if VIEW already exists */
79011 ){
79012   Table *p;
79013   int n;
79014   const char *z;
79015   Token sEnd;
79016   DbFixer sFix;
79017   Token *pName;
79018   int iDb;
79019   sqlite3 *db = pParse->db;
79020 
79021   if( pParse->nVar>0 ){
79022     sqlite3ErrorMsg(pParse, "parameters are not allowed in views");
79023     sqlite3SelectDelete(db, pSelect);
79024     return;
79025   }
79026   sqlite3StartTable(pParse, pName1, pName2, isTemp, 1, 0, noErr);
79027   p = pParse->pNewTable;
79028   if( p==0 || pParse->nErr ){
79029     sqlite3SelectDelete(db, pSelect);
79030     return;
79031   }
79032   sqlite3TwoPartName(pParse, pName1, pName2, &pName);
79033   iDb = sqlite3SchemaToIndex(db, p->pSchema);
79034   if( sqlite3FixInit(&sFix, pParse, iDb, "view", pName)
79035     && sqlite3FixSelect(&sFix, pSelect)
79036   ){
79037     sqlite3SelectDelete(db, pSelect);
79038     return;
79039   }
79040 
79041   /* Make a copy of the entire SELECT statement that defines the view.
79042   ** This will force all the Expr.token.z values to be dynamically
79043   ** allocated rather than point to the input string - which means that
79044   ** they will persist after the current sqlite3_exec() call returns.
79045   */
79046   p->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
79047   sqlite3SelectDelete(db, pSelect);
79048   if( db->mallocFailed ){
79049     return;
79050   }
79051   if( !db->init.busy ){
79052     sqlite3ViewGetColumnNames(pParse, p);
79053   }
79054 
79055   /* Locate the end of the CREATE VIEW statement.  Make sEnd point to
79056   ** the end.
79057   */
79058   sEnd = pParse->sLastToken;
79059   if( ALWAYS(sEnd.z[0]!=0) && sEnd.z[0]!=';' ){
79060     sEnd.z += sEnd.n;
79061   }
79062   sEnd.n = 0;
79063   n = (int)(sEnd.z - pBegin->z);
79064   z = pBegin->z;
79065   while( ALWAYS(n>0) && sqlite3Isspace(z[n-1]) ){ n--; }
79066   sEnd.z = &z[n-1];
79067   sEnd.n = 1;
79068 
79069   /* Use sqlite3EndTable() to add the view to the SQLITE_MASTER table */
79070   sqlite3EndTable(pParse, 0, &sEnd, 0);
79071   return;
79072 }
79073 #endif /* SQLITE_OMIT_VIEW */
79074 
79075 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
79076 /*
79077 ** The Table structure pTable is really a VIEW.  Fill in the names of
79078 ** the columns of the view in the pTable structure.  Return the number
79079 ** of errors.  If an error is seen leave an error message in pParse->zErrMsg.
79080 */
79081 SQLITE_PRIVATE int sqlite3ViewGetColumnNames(Parse *pParse, Table *pTable){
79082   Table *pSelTab;   /* A fake table from which we get the result set */
79083   Select *pSel;     /* Copy of the SELECT that implements the view */
79084   int nErr = 0;     /* Number of errors encountered */
79085   int n;            /* Temporarily holds the number of cursors assigned */
79086   sqlite3 *db = pParse->db;  /* Database connection for malloc errors */
79087   int (*xAuth)(void*,int,const char*,const char*,const char*,const char*);
79088 
79089   assert( pTable );
79090 
79091 #ifndef SQLITE_OMIT_VIRTUALTABLE
79092   if( sqlite3VtabCallConnect(pParse, pTable) ){
79093     return SQLITE_ERROR;
79094   }
79095   if( IsVirtual(pTable) ) return 0;
79096 #endif
79097 
79098 #ifndef SQLITE_OMIT_VIEW
79099   /* A positive nCol means the columns names for this view are
79100   ** already known.
79101   */
79102   if( pTable->nCol>0 ) return 0;
79103 
79104   /* A negative nCol is a special marker meaning that we are currently
79105   ** trying to compute the column names.  If we enter this routine with
79106   ** a negative nCol, it means two or more views form a loop, like this:
79107   **
79108   **     CREATE VIEW one AS SELECT * FROM two;
79109   **     CREATE VIEW two AS SELECT * FROM one;
79110   **
79111   ** Actually, the error above is now caught prior to reaching this point.
79112   ** But the following test is still important as it does come up
79113   ** in the following:
79114   **
79115   **     CREATE TABLE main.ex1(a);
79116   **     CREATE TEMP VIEW ex1 AS SELECT a FROM ex1;
79117   **     SELECT * FROM temp.ex1;
79118   */
79119   if( pTable->nCol<0 ){
79120     sqlite3ErrorMsg(pParse, "view %s is circularly defined", pTable->zName);
79121     return 1;
79122   }
79123   assert( pTable->nCol>=0 );
79124 
79125   /* If we get this far, it means we need to compute the table names.
79126   ** Note that the call to sqlite3ResultSetOfSelect() will expand any
79127   ** "*" elements in the results set of the view and will assign cursors
79128   ** to the elements of the FROM clause.  But we do not want these changes
79129   ** to be permanent.  So the computation is done on a copy of the SELECT
79130   ** statement that defines the view.
79131   */
79132   assert( pTable->pSelect );
79133   pSel = sqlite3SelectDup(db, pTable->pSelect, 0);
79134   if( pSel ){
79135     u8 enableLookaside = db->lookaside.bEnabled;
79136     n = pParse->nTab;
79137     sqlite3SrcListAssignCursors(pParse, pSel->pSrc);
79138     pTable->nCol = -1;
79139     db->lookaside.bEnabled = 0;
79140 #ifndef SQLITE_OMIT_AUTHORIZATION
79141     xAuth = db->xAuth;
79142     db->xAuth = 0;
79143     pSelTab = sqlite3ResultSetOfSelect(pParse, pSel);
79144     db->xAuth = xAuth;
79145 #else
79146     pSelTab = sqlite3ResultSetOfSelect(pParse, pSel);
79147 #endif
79148     db->lookaside.bEnabled = enableLookaside;
79149     pParse->nTab = n;
79150     if( pSelTab ){
79151       assert( pTable->aCol==0 );
79152       pTable->nCol = pSelTab->nCol;
79153       pTable->aCol = pSelTab->aCol;
79154       pSelTab->nCol = 0;
79155       pSelTab->aCol = 0;
79156       sqlite3DeleteTable(db, pSelTab);
79157       assert( sqlite3SchemaMutexHeld(db, 0, pTable->pSchema) );
79158       pTable->pSchema->flags |= DB_UnresetViews;
79159     }else{
79160       pTable->nCol = 0;
79161       nErr++;
79162     }
79163     sqlite3SelectDelete(db, pSel);
79164   } else {
79165     nErr++;
79166   }
79167 #endif /* SQLITE_OMIT_VIEW */
79168   return nErr;
79169 }
79170 #endif /* !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) */
79171 
79172 #ifndef SQLITE_OMIT_VIEW
79173 /*
79174 ** Clear the column names from every VIEW in database idx.
79175 */
79176 static void sqliteViewResetAll(sqlite3 *db, int idx){
79177   HashElem *i;
79178   assert( sqlite3SchemaMutexHeld(db, idx, 0) );
79179   if( !DbHasProperty(db, idx, DB_UnresetViews) ) return;
79180   for(i=sqliteHashFirst(&db->aDb[idx].pSchema->tblHash); i;i=sqliteHashNext(i)){
79181     Table *pTab = sqliteHashData(i);
79182     if( pTab->pSelect ){
79183       sqliteDeleteColumnNames(db, pTab);
79184       pTab->aCol = 0;
79185       pTab->nCol = 0;
79186     }
79187   }
79188   DbClearProperty(db, idx, DB_UnresetViews);
79189 }
79190 #else
79191 # define sqliteViewResetAll(A,B)
79192 #endif /* SQLITE_OMIT_VIEW */
79193 
79194 /*
79195 ** This function is called by the VDBE to adjust the internal schema
79196 ** used by SQLite when the btree layer moves a table root page. The
79197 ** root-page of a table or index in database iDb has changed from iFrom
79198 ** to iTo.
79199 **
79200 ** Ticket #1728:  The symbol table might still contain information
79201 ** on tables and/or indices that are the process of being deleted.
79202 ** If you are unlucky, one of those deleted indices or tables might
79203 ** have the same rootpage number as the real table or index that is
79204 ** being moved.  So we cannot stop searching after the first match
79205 ** because the first match might be for one of the deleted indices
79206 ** or tables and not the table/index that is actually being moved.
79207 ** We must continue looping until all tables and indices with
79208 ** rootpage==iFrom have been converted to have a rootpage of iTo
79209 ** in order to be certain that we got the right one.
79210 */
79211 #ifndef SQLITE_OMIT_AUTOVACUUM
79212 SQLITE_PRIVATE void sqlite3RootPageMoved(sqlite3 *db, int iDb, int iFrom, int iTo){
79213   HashElem *pElem;
79214   Hash *pHash;
79215   Db *pDb;
79216 
79217   assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
79218   pDb = &db->aDb[iDb];
79219   pHash = &pDb->pSchema->tblHash;
79220   for(pElem=sqliteHashFirst(pHash); pElem; pElem=sqliteHashNext(pElem)){
79221     Table *pTab = sqliteHashData(pElem);
79222     if( pTab->tnum==iFrom ){
79223       pTab->tnum = iTo;
79224     }
79225   }
79226   pHash = &pDb->pSchema->idxHash;
79227   for(pElem=sqliteHashFirst(pHash); pElem; pElem=sqliteHashNext(pElem)){
79228     Index *pIdx = sqliteHashData(pElem);
79229     if( pIdx->tnum==iFrom ){
79230       pIdx->tnum = iTo;
79231     }
79232   }
79233 }
79234 #endif
79235 
79236 /*
79237 ** Write code to erase the table with root-page iTable from database iDb.
79238 ** Also write code to modify the sqlite_master table and internal schema
79239 ** if a root-page of another table is moved by the btree-layer whilst
79240 ** erasing iTable (this can happen with an auto-vacuum database).
79241 */
79242 static void destroyRootPage(Parse *pParse, int iTable, int iDb){
79243   Vdbe *v = sqlite3GetVdbe(pParse);
79244   int r1 = sqlite3GetTempReg(pParse);
79245   sqlite3VdbeAddOp3(v, OP_Destroy, iTable, r1, iDb);
79246   sqlite3MayAbort(pParse);
79247 #ifndef SQLITE_OMIT_AUTOVACUUM
79248   /* OP_Destroy stores an in integer r1. If this integer
79249   ** is non-zero, then it is the root page number of a table moved to
79250   ** location iTable. The following code modifies the sqlite_master table to
79251   ** reflect this.
79252   **
79253   ** The "#NNN" in the SQL is a special constant that means whatever value
79254   ** is in register NNN.  See grammar rules associated with the TK_REGISTER
79255   ** token for additional information.
79256   */
79257   sqlite3NestedParse(pParse,
79258      "UPDATE %Q.%s SET rootpage=%d WHERE #%d AND rootpage=#%d",
79259      pParse->db->aDb[iDb].zName, SCHEMA_TABLE(iDb), iTable, r1, r1);
79260 #endif
79261   sqlite3ReleaseTempReg(pParse, r1);
79262 }
79263 
79264 /*
79265 ** Write VDBE code to erase table pTab and all associated indices on disk.
79266 ** Code to update the sqlite_master tables and internal schema definitions
79267 ** in case a root-page belonging to another table is moved by the btree layer
79268 ** is also added (this can happen with an auto-vacuum database).
79269 */
79270 static void destroyTable(Parse *pParse, Table *pTab){
79271 #ifdef SQLITE_OMIT_AUTOVACUUM
79272   Index *pIdx;
79273   int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
79274   destroyRootPage(pParse, pTab->tnum, iDb);
79275   for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
79276     destroyRootPage(pParse, pIdx->tnum, iDb);
79277   }
79278 #else
79279   /* If the database may be auto-vacuum capable (if SQLITE_OMIT_AUTOVACUUM
79280   ** is not defined), then it is important to call OP_Destroy on the
79281   ** table and index root-pages in order, starting with the numerically
79282   ** largest root-page number. This guarantees that none of the root-pages
79283   ** to be destroyed is relocated by an earlier OP_Destroy. i.e. if the
79284   ** following were coded:
79285   **
79286   ** OP_Destroy 4 0
79287   ** ...
79288   ** OP_Destroy 5 0
79289   **
79290   ** and root page 5 happened to be the largest root-page number in the
79291   ** database, then root page 5 would be moved to page 4 by the
79292   ** "OP_Destroy 4 0" opcode. The subsequent "OP_Destroy 5 0" would hit
79293   ** a free-list page.
79294   */
79295   int iTab = pTab->tnum;
79296   int iDestroyed = 0;
79297 
79298   while( 1 ){
79299     Index *pIdx;
79300     int iLargest = 0;
79301 
79302     if( iDestroyed==0 || iTab<iDestroyed ){
79303       iLargest = iTab;
79304     }
79305     for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
79306       int iIdx = pIdx->tnum;
79307       assert( pIdx->pSchema==pTab->pSchema );
79308       if( (iDestroyed==0 || (iIdx<iDestroyed)) && iIdx>iLargest ){
79309         iLargest = iIdx;
79310       }
79311     }
79312     if( iLargest==0 ){
79313       return;
79314     }else{
79315       int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
79316       destroyRootPage(pParse, iLargest, iDb);
79317       iDestroyed = iLargest;
79318     }
79319   }
79320 #endif
79321 }
79322 
79323 /*
79324 ** This routine is called to do the work of a DROP TABLE statement.
79325 ** pName is the name of the table to be dropped.
79326 */
79327 SQLITE_PRIVATE void sqlite3DropTable(Parse *pParse, SrcList *pName, int isView, int noErr){
79328   Table *pTab;
79329   Vdbe *v;
79330   sqlite3 *db = pParse->db;
79331   int iDb;
79332 
79333   if( db->mallocFailed ){
79334     goto exit_drop_table;
79335   }
79336   assert( pParse->nErr==0 );
79337   assert( pName->nSrc==1 );
79338   if( noErr ) db->suppressErr++;
79339   pTab = sqlite3LocateTable(pParse, isView,
79340                             pName->a[0].zName, pName->a[0].zDatabase);
79341   if( noErr ) db->suppressErr--;
79342 
79343   if( pTab==0 ){
79344     if( noErr ) sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase);
79345     goto exit_drop_table;
79346   }
79347   iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
79348   assert( iDb>=0 && iDb<db->nDb );
79349 
79350   /* If pTab is a virtual table, call ViewGetColumnNames() to ensure
79351   ** it is initialized.
79352   */
79353   if( IsVirtual(pTab) && sqlite3ViewGetColumnNames(pParse, pTab) ){
79354     goto exit_drop_table;
79355   }
79356 #ifndef SQLITE_OMIT_AUTHORIZATION
79357   {
79358     int code;
79359     const char *zTab = SCHEMA_TABLE(iDb);
79360     const char *zDb = db->aDb[iDb].zName;
79361     const char *zArg2 = 0;
79362     if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb)){
79363       goto exit_drop_table;
79364     }
79365     if( isView ){
79366       if( !OMIT_TEMPDB && iDb==1 ){
79367         code = SQLITE_DROP_TEMP_VIEW;
79368       }else{
79369         code = SQLITE_DROP_VIEW;
79370       }
79371 #ifndef SQLITE_OMIT_VIRTUALTABLE
79372     }else if( IsVirtual(pTab) ){
79373       code = SQLITE_DROP_VTABLE;
79374       zArg2 = sqlite3GetVTable(db, pTab)->pMod->zName;
79375 #endif
79376     }else{
79377       if( !OMIT_TEMPDB && iDb==1 ){
79378         code = SQLITE_DROP_TEMP_TABLE;
79379       }else{
79380         code = SQLITE_DROP_TABLE;
79381       }
79382     }
79383     if( sqlite3AuthCheck(pParse, code, pTab->zName, zArg2, zDb) ){
79384       goto exit_drop_table;
79385     }
79386     if( sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){
79387       goto exit_drop_table;
79388     }
79389   }
79390 #endif
79391   if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
79392     sqlite3ErrorMsg(pParse, "table %s may not be dropped", pTab->zName);
79393     goto exit_drop_table;
79394   }
79395 
79396 #ifndef SQLITE_OMIT_VIEW
79397   /* Ensure DROP TABLE is not used on a view, and DROP VIEW is not used
79398   ** on a table.
79399   */
79400   if( isView && pTab->pSelect==0 ){
79401     sqlite3ErrorMsg(pParse, "use DROP TABLE to delete table %s", pTab->zName);
79402     goto exit_drop_table;
79403   }
79404   if( !isView && pTab->pSelect ){
79405     sqlite3ErrorMsg(pParse, "use DROP VIEW to delete view %s", pTab->zName);
79406     goto exit_drop_table;
79407   }
79408 #endif
79409 
79410   /* Generate code to remove the table from the master table
79411   ** on disk.
79412   */
79413   v = sqlite3GetVdbe(pParse);
79414   if( v ){
79415     Trigger *pTrigger;
79416     Db *pDb = &db->aDb[iDb];
79417     sqlite3BeginWriteOperation(pParse, 1, iDb);
79418 
79419 #ifndef SQLITE_OMIT_VIRTUALTABLE
79420     if( IsVirtual(pTab) ){
79421       sqlite3VdbeAddOp0(v, OP_VBegin);
79422     }
79423 #endif
79424     sqlite3FkDropTable(pParse, pName, pTab);
79425 
79426     /* Drop all triggers associated with the table being dropped. Code
79427     ** is generated to remove entries from sqlite_master and/or
79428     ** sqlite_temp_master if required.
79429     */
79430     pTrigger = sqlite3TriggerList(pParse, pTab);
79431     while( pTrigger ){
79432       assert( pTrigger->pSchema==pTab->pSchema ||
79433           pTrigger->pSchema==db->aDb[1].pSchema );
79434       sqlite3DropTriggerPtr(pParse, pTrigger);
79435       pTrigger = pTrigger->pNext;
79436     }
79437 
79438 #ifndef SQLITE_OMIT_AUTOINCREMENT
79439     /* Remove any entries of the sqlite_sequence table associated with
79440     ** the table being dropped. This is done before the table is dropped
79441     ** at the btree level, in case the sqlite_sequence table needs to
79442     ** move as a result of the drop (can happen in auto-vacuum mode).
79443     */
79444     if( pTab->tabFlags & TF_Autoincrement ){
79445       sqlite3NestedParse(pParse,
79446         "DELETE FROM %s.sqlite_sequence WHERE name=%Q",
79447         pDb->zName, pTab->zName
79448       );
79449     }
79450 #endif
79451 
79452     /* Drop all SQLITE_MASTER table and index entries that refer to the
79453     ** table. The program name loops through the master table and deletes
79454     ** every row that refers to a table of the same name as the one being
79455     ** dropped. Triggers are handled seperately because a trigger can be
79456     ** created in the temp database that refers to a table in another
79457     ** database.
79458     */
79459     sqlite3NestedParse(pParse,
79460         "DELETE FROM %Q.%s WHERE tbl_name=%Q and type!='trigger'",
79461         pDb->zName, SCHEMA_TABLE(iDb), pTab->zName);
79462 
79463     /* Drop any statistics from the sqlite_stat1 table, if it exists */
79464     if( sqlite3FindTable(db, "sqlite_stat1", db->aDb[iDb].zName) ){
79465       sqlite3NestedParse(pParse,
79466         "DELETE FROM %Q.sqlite_stat1 WHERE tbl=%Q", pDb->zName, pTab->zName
79467       );
79468     }
79469 
79470     if( !isView && !IsVirtual(pTab) ){
79471       destroyTable(pParse, pTab);
79472     }
79473 
79474     /* Remove the table entry from SQLite's internal schema and modify
79475     ** the schema cookie.
79476     */
79477     if( IsVirtual(pTab) ){
79478       sqlite3VdbeAddOp4(v, OP_VDestroy, iDb, 0, 0, pTab->zName, 0);
79479     }
79480     sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0);
79481     sqlite3ChangeCookie(pParse, iDb);
79482   }
79483   sqliteViewResetAll(db, iDb);
79484 
79485 exit_drop_table:
79486   sqlite3SrcListDelete(db, pName);
79487 }
79488 
79489 /*
79490 ** This routine is called to create a new foreign key on the table
79491 ** currently under construction.  pFromCol determines which columns
79492 ** in the current table point to the foreign key.  If pFromCol==0 then
79493 ** connect the key to the last column inserted.  pTo is the name of
79494 ** the table referred to.  pToCol is a list of tables in the other
79495 ** pTo table that the foreign key points to.  flags contains all
79496 ** information about the conflict resolution algorithms specified
79497 ** in the ON DELETE, ON UPDATE and ON INSERT clauses.
79498 **
79499 ** An FKey structure is created and added to the table currently
79500 ** under construction in the pParse->pNewTable field.
79501 **
79502 ** The foreign key is set for IMMEDIATE processing.  A subsequent call
79503 ** to sqlite3DeferForeignKey() might change this to DEFERRED.
79504 */
79505 SQLITE_PRIVATE void sqlite3CreateForeignKey(
79506   Parse *pParse,       /* Parsing context */
79507   ExprList *pFromCol,  /* Columns in this table that point to other table */
79508   Token *pTo,          /* Name of the other table */
79509   ExprList *pToCol,    /* Columns in the other table */
79510   int flags            /* Conflict resolution algorithms. */
79511 ){
79512   sqlite3 *db = pParse->db;
79513 #ifndef SQLITE_OMIT_FOREIGN_KEY
79514   FKey *pFKey = 0;
79515   FKey *pNextTo;
79516   Table *p = pParse->pNewTable;
79517   int nByte;
79518   int i;
79519   int nCol;
79520   char *z;
79521 
79522   assert( pTo!=0 );
79523   if( p==0 || IN_DECLARE_VTAB ) goto fk_end;
79524   if( pFromCol==0 ){
79525     int iCol = p->nCol-1;
79526     if( NEVER(iCol<0) ) goto fk_end;
79527     if( pToCol && pToCol->nExpr!=1 ){
79528       sqlite3ErrorMsg(pParse, "foreign key on %s"
79529          " should reference only one column of table %T",
79530          p->aCol[iCol].zName, pTo);
79531       goto fk_end;
79532     }
79533     nCol = 1;
79534   }else if( pToCol && pToCol->nExpr!=pFromCol->nExpr ){
79535     sqlite3ErrorMsg(pParse,
79536         "number of columns in foreign key does not match the number of "
79537         "columns in the referenced table");
79538     goto fk_end;
79539   }else{
79540     nCol = pFromCol->nExpr;
79541   }
79542   nByte = sizeof(*pFKey) + (nCol-1)*sizeof(pFKey->aCol[0]) + pTo->n + 1;
79543   if( pToCol ){
79544     for(i=0; i<pToCol->nExpr; i++){
79545       nByte += sqlite3Strlen30(pToCol->a[i].zName) + 1;
79546     }
79547   }
79548   pFKey = sqlite3DbMallocZero(db, nByte );
79549   if( pFKey==0 ){
79550     goto fk_end;
79551   }
79552   pFKey->pFrom = p;
79553   pFKey->pNextFrom = p->pFKey;
79554   z = (char*)&pFKey->aCol[nCol];
79555   pFKey->zTo = z;
79556   memcpy(z, pTo->z, pTo->n);
79557   z[pTo->n] = 0;
79558   sqlite3Dequote(z);
79559   z += pTo->n+1;
79560   pFKey->nCol = nCol;
79561   if( pFromCol==0 ){
79562     pFKey->aCol[0].iFrom = p->nCol-1;
79563   }else{
79564     for(i=0; i<nCol; i++){
79565       int j;
79566       for(j=0; j<p->nCol; j++){
79567         if( sqlite3StrICmp(p->aCol[j].zName, pFromCol->a[i].zName)==0 ){
79568           pFKey->aCol[i].iFrom = j;
79569           break;
79570         }
79571       }
79572       if( j>=p->nCol ){
79573         sqlite3ErrorMsg(pParse,
79574           "unknown column \"%s\" in foreign key definition",
79575           pFromCol->a[i].zName);
79576         goto fk_end;
79577       }
79578     }
79579   }
79580   if( pToCol ){
79581     for(i=0; i<nCol; i++){
79582       int n = sqlite3Strlen30(pToCol->a[i].zName);
79583       pFKey->aCol[i].zCol = z;
79584       memcpy(z, pToCol->a[i].zName, n);
79585       z[n] = 0;
79586       z += n+1;
79587     }
79588   }
79589   pFKey->isDeferred = 0;
79590   pFKey->aAction[0] = (u8)(flags & 0xff);            /* ON DELETE action */
79591   pFKey->aAction[1] = (u8)((flags >> 8 ) & 0xff);    /* ON UPDATE action */
79592 
79593   assert( sqlite3SchemaMutexHeld(db, 0, p->pSchema) );
79594   pNextTo = (FKey *)sqlite3HashInsert(&p->pSchema->fkeyHash,
79595       pFKey->zTo, sqlite3Strlen30(pFKey->zTo), (void *)pFKey
79596   );
79597   if( pNextTo==pFKey ){
79598     db->mallocFailed = 1;
79599     goto fk_end;
79600   }
79601   if( pNextTo ){
79602     assert( pNextTo->pPrevTo==0 );
79603     pFKey->pNextTo = pNextTo;
79604     pNextTo->pPrevTo = pFKey;
79605   }
79606 
79607   /* Link the foreign key to the table as the last step.
79608   */
79609   p->pFKey = pFKey;
79610   pFKey = 0;
79611 
79612 fk_end:
79613   sqlite3DbFree(db, pFKey);
79614 #endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
79615   sqlite3ExprListDelete(db, pFromCol);
79616   sqlite3ExprListDelete(db, pToCol);
79617 }
79618 
79619 /*
79620 ** This routine is called when an INITIALLY IMMEDIATE or INITIALLY DEFERRED
79621 ** clause is seen as part of a foreign key definition.  The isDeferred
79622 ** parameter is 1 for INITIALLY DEFERRED and 0 for INITIALLY IMMEDIATE.
79623 ** The behavior of the most recently created foreign key is adjusted
79624 ** accordingly.
79625 */
79626 SQLITE_PRIVATE void sqlite3DeferForeignKey(Parse *pParse, int isDeferred){
79627 #ifndef SQLITE_OMIT_FOREIGN_KEY
79628   Table *pTab;
79629   FKey *pFKey;
79630   if( (pTab = pParse->pNewTable)==0 || (pFKey = pTab->pFKey)==0 ) return;
79631   assert( isDeferred==0 || isDeferred==1 ); /* EV: R-30323-21917 */
79632   pFKey->isDeferred = (u8)isDeferred;
79633 #endif
79634 }
79635 
79636 /*
79637 ** Generate code that will erase and refill index *pIdx.  This is
79638 ** used to initialize a newly created index or to recompute the
79639 ** content of an index in response to a REINDEX command.
79640 **
79641 ** if memRootPage is not negative, it means that the index is newly
79642 ** created.  The register specified by memRootPage contains the
79643 ** root page number of the index.  If memRootPage is negative, then
79644 ** the index already exists and must be cleared before being refilled and
79645 ** the root page number of the index is taken from pIndex->tnum.
79646 */
79647 static void sqlite3RefillIndex(Parse *pParse, Index *pIndex, int memRootPage){
79648   Table *pTab = pIndex->pTable;  /* The table that is indexed */
79649   int iTab = pParse->nTab++;     /* Btree cursor used for pTab */
79650   int iIdx = pParse->nTab++;     /* Btree cursor used for pIndex */
79651   int addr1;                     /* Address of top of loop */
79652   int tnum;                      /* Root page of index */
79653   Vdbe *v;                       /* Generate code into this virtual machine */
79654   KeyInfo *pKey;                 /* KeyInfo for index */
79655   int regIdxKey;                 /* Registers containing the index key */
79656   int regRecord;                 /* Register holding assemblied index record */
79657   sqlite3 *db = pParse->db;      /* The database connection */
79658   int iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
79659 
79660 #ifndef SQLITE_OMIT_AUTHORIZATION
79661   if( sqlite3AuthCheck(pParse, SQLITE_REINDEX, pIndex->zName, 0,
79662       db->aDb[iDb].zName ) ){
79663     return;
79664   }
79665 #endif
79666 
79667   /* Require a write-lock on the table to perform this operation */
79668   sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName);
79669 
79670   v = sqlite3GetVdbe(pParse);
79671   if( v==0 ) return;
79672   if( memRootPage>=0 ){
79673     tnum = memRootPage;
79674   }else{
79675     tnum = pIndex->tnum;
79676     sqlite3VdbeAddOp2(v, OP_Clear, tnum, iDb);
79677   }
79678   pKey = sqlite3IndexKeyinfo(pParse, pIndex);
79679   sqlite3VdbeAddOp4(v, OP_OpenWrite, iIdx, tnum, iDb,
79680                     (char *)pKey, P4_KEYINFO_HANDOFF);
79681   if( memRootPage>=0 ){
79682     sqlite3VdbeChangeP5(v, 1);
79683   }
79684   sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
79685   addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0);
79686   regRecord = sqlite3GetTempReg(pParse);
79687   regIdxKey = sqlite3GenerateIndexKey(pParse, pIndex, iTab, regRecord, 1);
79688   if( pIndex->onError!=OE_None ){
79689     const int regRowid = regIdxKey + pIndex->nColumn;
79690     const int j2 = sqlite3VdbeCurrentAddr(v) + 2;
79691     void * const pRegKey = SQLITE_INT_TO_PTR(regIdxKey);
79692 
79693     /* The registers accessed by the OP_IsUnique opcode were allocated
79694     ** using sqlite3GetTempRange() inside of the sqlite3GenerateIndexKey()
79695     ** call above. Just before that function was freed they were released
79696     ** (made available to the compiler for reuse) using
79697     ** sqlite3ReleaseTempRange(). So in some ways having the OP_IsUnique
79698     ** opcode use the values stored within seems dangerous. However, since
79699     ** we can be sure that no other temp registers have been allocated
79700     ** since sqlite3ReleaseTempRange() was called, it is safe to do so.
79701     */
79702     sqlite3VdbeAddOp4(v, OP_IsUnique, iIdx, j2, regRowid, pRegKey, P4_INT32);
79703     sqlite3HaltConstraint(
79704         pParse, OE_Abort, "indexed columns are not unique", P4_STATIC);
79705   }
79706   sqlite3VdbeAddOp2(v, OP_IdxInsert, iIdx, regRecord);
79707   sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
79708   sqlite3ReleaseTempReg(pParse, regRecord);
79709   sqlite3VdbeAddOp2(v, OP_Next, iTab, addr1+1);
79710   sqlite3VdbeJumpHere(v, addr1);
79711   sqlite3VdbeAddOp1(v, OP_Close, iTab);
79712   sqlite3VdbeAddOp1(v, OP_Close, iIdx);
79713 }
79714 
79715 /*
79716 ** Create a new index for an SQL table.  pName1.pName2 is the name of the index
79717 ** and pTblList is the name of the table that is to be indexed.  Both will
79718 ** be NULL for a primary key or an index that is created to satisfy a
79719 ** UNIQUE constraint.  If pTable and pIndex are NULL, use pParse->pNewTable
79720 ** as the table to be indexed.  pParse->pNewTable is a table that is
79721 ** currently being constructed by a CREATE TABLE statement.
79722 **
79723 ** pList is a list of columns to be indexed.  pList will be NULL if this
79724 ** is a primary key or unique-constraint on the most recent column added
79725 ** to the table currently under construction.
79726 **
79727 ** If the index is created successfully, return a pointer to the new Index
79728 ** structure. This is used by sqlite3AddPrimaryKey() to mark the index
79729 ** as the tables primary key (Index.autoIndex==2).
79730 */
79731 SQLITE_PRIVATE Index *sqlite3CreateIndex(
79732   Parse *pParse,     /* All information about this parse */
79733   Token *pName1,     /* First part of index name. May be NULL */
79734   Token *pName2,     /* Second part of index name. May be NULL */
79735   SrcList *pTblName, /* Table to index. Use pParse->pNewTable if 0 */
79736   ExprList *pList,   /* A list of columns to be indexed */
79737   int onError,       /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
79738   Token *pStart,     /* The CREATE token that begins this statement */
79739   Token *pEnd,       /* The ")" that closes the CREATE INDEX statement */
79740   int sortOrder,     /* Sort order of primary key when pList==NULL */
79741   int ifNotExist     /* Omit error if index already exists */
79742 ){
79743   Index *pRet = 0;     /* Pointer to return */
79744   Table *pTab = 0;     /* Table to be indexed */
79745   Index *pIndex = 0;   /* The index to be created */
79746   char *zName = 0;     /* Name of the index */
79747   int nName;           /* Number of characters in zName */
79748   int i, j;
79749   Token nullId;        /* Fake token for an empty ID list */
79750   DbFixer sFix;        /* For assigning database names to pTable */
79751   int sortOrderMask;   /* 1 to honor DESC in index.  0 to ignore. */
79752   sqlite3 *db = pParse->db;
79753   Db *pDb;             /* The specific table containing the indexed database */
79754   int iDb;             /* Index of the database that is being written */
79755   Token *pName = 0;    /* Unqualified name of the index to create */
79756   struct ExprList_item *pListItem; /* For looping over pList */
79757   int nCol;
79758   int nExtra = 0;
79759   char *zExtra;
79760 
79761   assert( pStart==0 || pEnd!=0 ); /* pEnd must be non-NULL if pStart is */
79762   assert( pParse->nErr==0 );      /* Never called with prior errors */
79763   if( db->mallocFailed || IN_DECLARE_VTAB ){
79764     goto exit_create_index;
79765   }
79766   if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
79767     goto exit_create_index;
79768   }
79769 
79770   /*
79771   ** Find the table that is to be indexed.  Return early if not found.
79772   */
79773   if( pTblName!=0 ){
79774 
79775     /* Use the two-part index name to determine the database
79776     ** to search for the table. 'Fix' the table name to this db
79777     ** before looking up the table.
79778     */
79779     assert( pName1 && pName2 );
79780     iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
79781     if( iDb<0 ) goto exit_create_index;
79782 
79783 #ifndef SQLITE_OMIT_TEMPDB
79784     /* If the index name was unqualified, check if the the table
79785     ** is a temp table. If so, set the database to 1. Do not do this
79786     ** if initialising a database schema.
79787     */
79788     if( !db->init.busy ){
79789       pTab = sqlite3SrcListLookup(pParse, pTblName);
79790       if( pName2->n==0 && pTab && pTab->pSchema==db->aDb[1].pSchema ){
79791         iDb = 1;
79792       }
79793     }
79794 #endif
79795 
79796     if( sqlite3FixInit(&sFix, pParse, iDb, "index", pName) &&
79797         sqlite3FixSrcList(&sFix, pTblName)
79798     ){
79799       /* Because the parser constructs pTblName from a single identifier,
79800       ** sqlite3FixSrcList can never fail. */
79801       assert(0);
79802     }
79803     pTab = sqlite3LocateTable(pParse, 0, pTblName->a[0].zName,
79804         pTblName->a[0].zDatabase);
79805     if( !pTab || db->mallocFailed ) goto exit_create_index;
79806     assert( db->aDb[iDb].pSchema==pTab->pSchema );
79807   }else{
79808     assert( pName==0 );
79809     pTab = pParse->pNewTable;
79810     if( !pTab ) goto exit_create_index;
79811     iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
79812   }
79813   pDb = &db->aDb[iDb];
79814 
79815   assert( pTab!=0 );
79816   assert( pParse->nErr==0 );
79817   if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0
79818        && memcmp(&pTab->zName[7],"altertab_",9)!=0 ){
79819     sqlite3ErrorMsg(pParse, "table %s may not be indexed", pTab->zName);
79820     goto exit_create_index;
79821   }
79822 #ifndef SQLITE_OMIT_VIEW
79823   if( pTab->pSelect ){
79824     sqlite3ErrorMsg(pParse, "views may not be indexed");
79825     goto exit_create_index;
79826   }
79827 #endif
79828 #ifndef SQLITE_OMIT_VIRTUALTABLE
79829   if( IsVirtual(pTab) ){
79830     sqlite3ErrorMsg(pParse, "virtual tables may not be indexed");
79831     goto exit_create_index;
79832   }
79833 #endif
79834 
79835   /*
79836   ** Find the name of the index.  Make sure there is not already another
79837   ** index or table with the same name.
79838   **
79839   ** Exception:  If we are reading the names of permanent indices from the
79840   ** sqlite_master table (because some other process changed the schema) and
79841   ** one of the index names collides with the name of a temporary table or
79842   ** index, then we will continue to process this index.
79843   **
79844   ** If pName==0 it means that we are
79845   ** dealing with a primary key or UNIQUE constraint.  We have to invent our
79846   ** own name.
79847   */
79848   if( pName ){
79849     zName = sqlite3NameFromToken(db, pName);
79850     if( zName==0 ) goto exit_create_index;
79851     if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
79852       goto exit_create_index;
79853     }
79854     if( !db->init.busy ){
79855       if( sqlite3FindTable(db, zName, 0)!=0 ){
79856         sqlite3ErrorMsg(pParse, "there is already a table named %s", zName);
79857         goto exit_create_index;
79858       }
79859     }
79860     if( sqlite3FindIndex(db, zName, pDb->zName)!=0 ){
79861       if( !ifNotExist ){
79862         sqlite3ErrorMsg(pParse, "index %s already exists", zName);
79863       }else{
79864         assert( !db->init.busy );
79865         sqlite3CodeVerifySchema(pParse, iDb);
79866       }
79867       goto exit_create_index;
79868     }
79869   }else{
79870     int n;
79871     Index *pLoop;
79872     for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){}
79873     zName = sqlite3MPrintf(db, "sqlite_autoindex_%s_%d", pTab->zName, n);
79874     if( zName==0 ){
79875       goto exit_create_index;
79876     }
79877   }
79878 
79879   /* Check for authorization to create an index.
79880   */
79881 #ifndef SQLITE_OMIT_AUTHORIZATION
79882   {
79883     const char *zDb = pDb->zName;
79884     if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iDb), 0, zDb) ){
79885       goto exit_create_index;
79886     }
79887     i = SQLITE_CREATE_INDEX;
79888     if( !OMIT_TEMPDB && iDb==1 ) i = SQLITE_CREATE_TEMP_INDEX;
79889     if( sqlite3AuthCheck(pParse, i, zName, pTab->zName, zDb) ){
79890       goto exit_create_index;
79891     }
79892   }
79893 #endif
79894 
79895   /* If pList==0, it means this routine was called to make a primary
79896   ** key out of the last column added to the table under construction.
79897   ** So create a fake list to simulate this.
79898   */
79899   if( pList==0 ){
79900     nullId.z = pTab->aCol[pTab->nCol-1].zName;
79901     nullId.n = sqlite3Strlen30((char*)nullId.z);
79902     pList = sqlite3ExprListAppend(pParse, 0, 0);
79903     if( pList==0 ) goto exit_create_index;
79904     sqlite3ExprListSetName(pParse, pList, &nullId, 0);
79905     pList->a[0].sortOrder = (u8)sortOrder;
79906   }
79907 
79908   /* Figure out how many bytes of space are required to store explicitly
79909   ** specified collation sequence names.
79910   */
79911   for(i=0; i<pList->nExpr; i++){
79912     Expr *pExpr = pList->a[i].pExpr;
79913     if( pExpr ){
79914       CollSeq *pColl = pExpr->pColl;
79915       /* Either pColl!=0 or there was an OOM failure.  But if an OOM
79916       ** failure we have quit before reaching this point. */
79917       if( ALWAYS(pColl) ){
79918         nExtra += (1 + sqlite3Strlen30(pColl->zName));
79919       }
79920     }
79921   }
79922 
79923   /*
79924   ** Allocate the index structure.
79925   */
79926   nName = sqlite3Strlen30(zName);
79927   nCol = pList->nExpr;
79928   pIndex = sqlite3DbMallocZero(db,
79929       sizeof(Index) +              /* Index structure  */
79930       sizeof(int)*nCol +           /* Index.aiColumn   */
79931       sizeof(int)*(nCol+1) +       /* Index.aiRowEst   */
79932       sizeof(char *)*nCol +        /* Index.azColl     */
79933       sizeof(u8)*nCol +            /* Index.aSortOrder */
79934       nName + 1 +                  /* Index.zName      */
79935       nExtra                       /* Collation sequence names */
79936   );
79937   if( db->mallocFailed ){
79938     goto exit_create_index;
79939   }
79940   pIndex->azColl = (char**)(&pIndex[1]);
79941   pIndex->aiColumn = (int *)(&pIndex->azColl[nCol]);
79942   pIndex->aiRowEst = (unsigned *)(&pIndex->aiColumn[nCol]);
79943   pIndex->aSortOrder = (u8 *)(&pIndex->aiRowEst[nCol+1]);
79944   pIndex->zName = (char *)(&pIndex->aSortOrder[nCol]);
79945   zExtra = (char *)(&pIndex->zName[nName+1]);
79946   memcpy(pIndex->zName, zName, nName+1);
79947   pIndex->pTable = pTab;
79948   pIndex->nColumn = pList->nExpr;
79949   pIndex->onError = (u8)onError;
79950   pIndex->autoIndex = (u8)(pName==0);
79951   pIndex->pSchema = db->aDb[iDb].pSchema;
79952   assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
79953 
79954   /* Check to see if we should honor DESC requests on index columns
79955   */
79956   if( pDb->pSchema->file_format>=4 ){
79957     sortOrderMask = -1;   /* Honor DESC */
79958   }else{
79959     sortOrderMask = 0;    /* Ignore DESC */
79960   }
79961 
79962   /* Scan the names of the columns of the table to be indexed and
79963   ** load the column indices into the Index structure.  Report an error
79964   ** if any column is not found.
79965   **
79966   ** TODO:  Add a test to make sure that the same column is not named
79967   ** more than once within the same index.  Only the first instance of
79968   ** the column will ever be used by the optimizer.  Note that using the
79969   ** same column more than once cannot be an error because that would
79970   ** break backwards compatibility - it needs to be a warning.
79971   */
79972   for(i=0, pListItem=pList->a; i<pList->nExpr; i++, pListItem++){
79973     const char *zColName = pListItem->zName;
79974     Column *pTabCol;
79975     int requestedSortOrder;
79976     char *zColl;                   /* Collation sequence name */
79977 
79978     for(j=0, pTabCol=pTab->aCol; j<pTab->nCol; j++, pTabCol++){
79979       if( sqlite3StrICmp(zColName, pTabCol->zName)==0 ) break;
79980     }
79981     if( j>=pTab->nCol ){
79982       sqlite3ErrorMsg(pParse, "table %s has no column named %s",
79983         pTab->zName, zColName);
79984       pParse->checkSchema = 1;
79985       goto exit_create_index;
79986     }
79987     pIndex->aiColumn[i] = j;
79988     /* Justification of the ALWAYS(pListItem->pExpr->pColl):  Because of
79989     ** the way the "idxlist" non-terminal is constructed by the parser,
79990     ** if pListItem->pExpr is not null then either pListItem->pExpr->pColl
79991     ** must exist or else there must have been an OOM error.  But if there
79992     ** was an OOM error, we would never reach this point. */
79993     if( pListItem->pExpr && ALWAYS(pListItem->pExpr->pColl) ){
79994       int nColl;
79995       zColl = pListItem->pExpr->pColl->zName;
79996       nColl = sqlite3Strlen30(zColl) + 1;
79997       assert( nExtra>=nColl );
79998       memcpy(zExtra, zColl, nColl);
79999       zColl = zExtra;
80000       zExtra += nColl;
80001       nExtra -= nColl;
80002     }else{
80003       zColl = pTab->aCol[j].zColl;
80004       if( !zColl ){
80005         zColl = db->pDfltColl->zName;
80006       }
80007     }
80008     if( !db->init.busy && !sqlite3LocateCollSeq(pParse, zColl) ){
80009       goto exit_create_index;
80010     }
80011     pIndex->azColl[i] = zColl;
80012     requestedSortOrder = pListItem->sortOrder & sortOrderMask;
80013     pIndex->aSortOrder[i] = (u8)requestedSortOrder;
80014   }
80015   sqlite3DefaultRowEst(pIndex);
80016 
80017   if( pTab==pParse->pNewTable ){
80018     /* This routine has been called to create an automatic index as a
80019     ** result of a PRIMARY KEY or UNIQUE clause on a column definition, or
80020     ** a PRIMARY KEY or UNIQUE clause following the column definitions.
80021     ** i.e. one of:
80022     **
80023     ** CREATE TABLE t(x PRIMARY KEY, y);
80024     ** CREATE TABLE t(x, y, UNIQUE(x, y));
80025     **
80026     ** Either way, check to see if the table already has such an index. If
80027     ** so, don't bother creating this one. This only applies to
80028     ** automatically created indices. Users can do as they wish with
80029     ** explicit indices.
80030     **
80031     ** Two UNIQUE or PRIMARY KEY constraints are considered equivalent
80032     ** (and thus suppressing the second one) even if they have different
80033     ** sort orders.
80034     **
80035     ** If there are different collating sequences or if the columns of
80036     ** the constraint occur in different orders, then the constraints are
80037     ** considered distinct and both result in separate indices.
80038     */
80039     Index *pIdx;
80040     for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
80041       int k;
80042       assert( pIdx->onError!=OE_None );
80043       assert( pIdx->autoIndex );
80044       assert( pIndex->onError!=OE_None );
80045 
80046       if( pIdx->nColumn!=pIndex->nColumn ) continue;
80047       for(k=0; k<pIdx->nColumn; k++){
80048         const char *z1;
80049         const char *z2;
80050         if( pIdx->aiColumn[k]!=pIndex->aiColumn[k] ) break;
80051         z1 = pIdx->azColl[k];
80052         z2 = pIndex->azColl[k];
80053         if( z1!=z2 && sqlite3StrICmp(z1, z2) ) break;
80054       }
80055       if( k==pIdx->nColumn ){
80056         if( pIdx->onError!=pIndex->onError ){
80057           /* This constraint creates the same index as a previous
80058           ** constraint specified somewhere in the CREATE TABLE statement.
80059           ** However the ON CONFLICT clauses are different. If both this
80060           ** constraint and the previous equivalent constraint have explicit
80061           ** ON CONFLICT clauses this is an error. Otherwise, use the
80062           ** explicitly specified behaviour for the index.
80063           */
80064           if( !(pIdx->onError==OE_Default || pIndex->onError==OE_Default) ){
80065             sqlite3ErrorMsg(pParse,
80066                 "conflicting ON CONFLICT clauses specified", 0);
80067           }
80068           if( pIdx->onError==OE_Default ){
80069             pIdx->onError = pIndex->onError;
80070           }
80071         }
80072         goto exit_create_index;
80073       }
80074     }
80075   }
80076 
80077   /* Link the new Index structure to its table and to the other
80078   ** in-memory database structures.
80079   */
80080   if( db->init.busy ){
80081     Index *p;
80082     assert( sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
80083     p = sqlite3HashInsert(&pIndex->pSchema->idxHash,
80084                           pIndex->zName, sqlite3Strlen30(pIndex->zName),
80085                           pIndex);
80086     if( p ){
80087       assert( p==pIndex );  /* Malloc must have failed */
80088       db->mallocFailed = 1;
80089       goto exit_create_index;
80090     }
80091     db->flags |= SQLITE_InternChanges;
80092     if( pTblName!=0 ){
80093       pIndex->tnum = db->init.newTnum;
80094     }
80095   }
80096 
80097   /* If the db->init.busy is 0 then create the index on disk.  This
80098   ** involves writing the index into the master table and filling in the
80099   ** index with the current table contents.
80100   **
80101   ** The db->init.busy is 0 when the user first enters a CREATE INDEX
80102   ** command.  db->init.busy is 1 when a database is opened and
80103   ** CREATE INDEX statements are read out of the master table.  In
80104   ** the latter case the index already exists on disk, which is why
80105   ** we don't want to recreate it.
80106   **
80107   ** If pTblName==0 it means this index is generated as a primary key
80108   ** or UNIQUE constraint of a CREATE TABLE statement.  Since the table
80109   ** has just been created, it contains no data and the index initialization
80110   ** step can be skipped.
80111   */
80112   else{ /* if( db->init.busy==0 ) */
80113     Vdbe *v;
80114     char *zStmt;
80115     int iMem = ++pParse->nMem;
80116 
80117     v = sqlite3GetVdbe(pParse);
80118     if( v==0 ) goto exit_create_index;
80119 
80120 
80121     /* Create the rootpage for the index
80122     */
80123     sqlite3BeginWriteOperation(pParse, 1, iDb);
80124     sqlite3VdbeAddOp2(v, OP_CreateIndex, iDb, iMem);
80125 
80126     /* Gather the complete text of the CREATE INDEX statement into
80127     ** the zStmt variable
80128     */
80129     if( pStart ){
80130       assert( pEnd!=0 );
80131       /* A named index with an explicit CREATE INDEX statement */
80132       zStmt = sqlite3MPrintf(db, "CREATE%s INDEX %.*s",
80133         onError==OE_None ? "" : " UNIQUE",
80134         pEnd->z - pName->z + 1,
80135         pName->z);
80136     }else{
80137       /* An automatic index created by a PRIMARY KEY or UNIQUE constraint */
80138       /* zStmt = sqlite3MPrintf(""); */
80139       zStmt = 0;
80140     }
80141 
80142     /* Add an entry in sqlite_master for this index
80143     */
80144     sqlite3NestedParse(pParse,
80145         "INSERT INTO %Q.%s VALUES('index',%Q,%Q,#%d,%Q);",
80146         db->aDb[iDb].zName, SCHEMA_TABLE(iDb),
80147         pIndex->zName,
80148         pTab->zName,
80149         iMem,
80150         zStmt
80151     );
80152     sqlite3DbFree(db, zStmt);
80153 
80154     /* Fill the index with data and reparse the schema. Code an OP_Expire
80155     ** to invalidate all pre-compiled statements.
80156     */
80157     if( pTblName ){
80158       sqlite3RefillIndex(pParse, pIndex, iMem);
80159       sqlite3ChangeCookie(pParse, iDb);
80160       sqlite3VdbeAddParseSchemaOp(v, iDb,
80161          sqlite3MPrintf(db, "name='%q' AND type='index'", pIndex->zName));
80162       sqlite3VdbeAddOp1(v, OP_Expire, 0);
80163     }
80164   }
80165 
80166   /* When adding an index to the list of indices for a table, make
80167   ** sure all indices labeled OE_Replace come after all those labeled
80168   ** OE_Ignore.  This is necessary for the correct constraint check
80169   ** processing (in sqlite3GenerateConstraintChecks()) as part of
80170   ** UPDATE and INSERT statements.
80171   */
80172   if( db->init.busy || pTblName==0 ){
80173     if( onError!=OE_Replace || pTab->pIndex==0
80174          || pTab->pIndex->onError==OE_Replace){
80175       pIndex->pNext = pTab->pIndex;
80176       pTab->pIndex = pIndex;
80177     }else{
80178       Index *pOther = pTab->pIndex;
80179       while( pOther->pNext && pOther->pNext->onError!=OE_Replace ){
80180         pOther = pOther->pNext;
80181       }
80182       pIndex->pNext = pOther->pNext;
80183       pOther->pNext = pIndex;
80184     }
80185     pRet = pIndex;
80186     pIndex = 0;
80187   }
80188 
80189   /* Clean up before exiting */
80190 exit_create_index:
80191   if( pIndex ){
80192     sqlite3DbFree(db, pIndex->zColAff);
80193     sqlite3DbFree(db, pIndex);
80194   }
80195   sqlite3ExprListDelete(db, pList);
80196   sqlite3SrcListDelete(db, pTblName);
80197   sqlite3DbFree(db, zName);
80198   return pRet;
80199 }
80200 
80201 /*
80202 ** Fill the Index.aiRowEst[] array with default information - information
80203 ** to be used when we have not run the ANALYZE command.
80204 **
80205 ** aiRowEst[0] is suppose to contain the number of elements in the index.
80206 ** Since we do not know, guess 1 million.  aiRowEst[1] is an estimate of the
80207 ** number of rows in the table that match any particular value of the
80208 ** first column of the index.  aiRowEst[2] is an estimate of the number
80209 ** of rows that match any particular combiniation of the first 2 columns
80210 ** of the index.  And so forth.  It must always be the case that
80211 *
80212 **           aiRowEst[N]<=aiRowEst[N-1]
80213 **           aiRowEst[N]>=1
80214 **
80215 ** Apart from that, we have little to go on besides intuition as to
80216 ** how aiRowEst[] should be initialized.  The numbers generated here
80217 ** are based on typical values found in actual indices.
80218 */
80219 SQLITE_PRIVATE void sqlite3DefaultRowEst(Index *pIdx){
80220   unsigned *a = pIdx->aiRowEst;
80221   int i;
80222   unsigned n;
80223   assert( a!=0 );
80224   a[0] = pIdx->pTable->nRowEst;
80225   if( a[0]<10 ) a[0] = 10;
80226   n = 10;
80227   for(i=1; i<=pIdx->nColumn; i++){
80228     a[i] = n;
80229     if( n>5 ) n--;
80230   }
80231   if( pIdx->onError!=OE_None ){
80232     a[pIdx->nColumn] = 1;
80233   }
80234 }
80235 
80236 /*
80237 ** This routine will drop an existing named index.  This routine
80238 ** implements the DROP INDEX statement.
80239 */
80240 SQLITE_PRIVATE void sqlite3DropIndex(Parse *pParse, SrcList *pName, int ifExists){
80241   Index *pIndex;
80242   Vdbe *v;
80243   sqlite3 *db = pParse->db;
80244   int iDb;
80245 
80246   assert( pParse->nErr==0 );   /* Never called with prior errors */
80247   if( db->mallocFailed ){
80248     goto exit_drop_index;
80249   }
80250   assert( pName->nSrc==1 );
80251   if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
80252     goto exit_drop_index;
80253   }
80254   pIndex = sqlite3FindIndex(db, pName->a[0].zName, pName->a[0].zDatabase);
80255   if( pIndex==0 ){
80256     if( !ifExists ){
80257       sqlite3ErrorMsg(pParse, "no such index: %S", pName, 0);
80258     }else{
80259       sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase);
80260     }
80261     pParse->checkSchema = 1;
80262     goto exit_drop_index;
80263   }
80264   if( pIndex->autoIndex ){
80265     sqlite3ErrorMsg(pParse, "index associated with UNIQUE "
80266       "or PRIMARY KEY constraint cannot be dropped", 0);
80267     goto exit_drop_index;
80268   }
80269   iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
80270 #ifndef SQLITE_OMIT_AUTHORIZATION
80271   {
80272     int code = SQLITE_DROP_INDEX;
80273     Table *pTab = pIndex->pTable;
80274     const char *zDb = db->aDb[iDb].zName;
80275     const char *zTab = SCHEMA_TABLE(iDb);
80276     if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
80277       goto exit_drop_index;
80278     }
80279     if( !OMIT_TEMPDB && iDb ) code = SQLITE_DROP_TEMP_INDEX;
80280     if( sqlite3AuthCheck(pParse, code, pIndex->zName, pTab->zName, zDb) ){
80281       goto exit_drop_index;
80282     }
80283   }
80284 #endif
80285 
80286   /* Generate code to remove the index and from the master table */
80287   v = sqlite3GetVdbe(pParse);
80288   if( v ){
80289     sqlite3BeginWriteOperation(pParse, 1, iDb);
80290     sqlite3NestedParse(pParse,
80291        "DELETE FROM %Q.%s WHERE name=%Q AND type='index'",
80292        db->aDb[iDb].zName, SCHEMA_TABLE(iDb),
80293        pIndex->zName
80294     );
80295     if( sqlite3FindTable(db, "sqlite_stat1", db->aDb[iDb].zName) ){
80296       sqlite3NestedParse(pParse,
80297         "DELETE FROM %Q.sqlite_stat1 WHERE idx=%Q",
80298         db->aDb[iDb].zName, pIndex->zName
80299       );
80300     }
80301     sqlite3ChangeCookie(pParse, iDb);
80302     destroyRootPage(pParse, pIndex->tnum, iDb);
80303     sqlite3VdbeAddOp4(v, OP_DropIndex, iDb, 0, 0, pIndex->zName, 0);
80304   }
80305 
80306 exit_drop_index:
80307   sqlite3SrcListDelete(db, pName);
80308 }
80309 
80310 /*
80311 ** pArray is a pointer to an array of objects.  Each object in the
80312 ** array is szEntry bytes in size.  This routine allocates a new
80313 ** object on the end of the array.
80314 **
80315 ** *pnEntry is the number of entries already in use.  *pnAlloc is
80316 ** the previously allocated size of the array.  initSize is the
80317 ** suggested initial array size allocation.
80318 **
80319 ** The index of the new entry is returned in *pIdx.
80320 **
80321 ** This routine returns a pointer to the array of objects.  This
80322 ** might be the same as the pArray parameter or it might be a different
80323 ** pointer if the array was resized.
80324 */
80325 SQLITE_PRIVATE void *sqlite3ArrayAllocate(
80326   sqlite3 *db,      /* Connection to notify of malloc failures */
80327   void *pArray,     /* Array of objects.  Might be reallocated */
80328   int szEntry,      /* Size of each object in the array */
80329   int initSize,     /* Suggested initial allocation, in elements */
80330   int *pnEntry,     /* Number of objects currently in use */
80331   int *pnAlloc,     /* Current size of the allocation, in elements */
80332   int *pIdx         /* Write the index of a new slot here */
80333 ){
80334   char *z;
80335   if( *pnEntry >= *pnAlloc ){
80336     void *pNew;
80337     int newSize;
80338     newSize = (*pnAlloc)*2 + initSize;
80339     pNew = sqlite3DbRealloc(db, pArray, newSize*szEntry);
80340     if( pNew==0 ){
80341       *pIdx = -1;
80342       return pArray;
80343     }
80344     *pnAlloc = sqlite3DbMallocSize(db, pNew)/szEntry;
80345     pArray = pNew;
80346   }
80347   z = (char*)pArray;
80348   memset(&z[*pnEntry * szEntry], 0, szEntry);
80349   *pIdx = *pnEntry;
80350   ++*pnEntry;
80351   return pArray;
80352 }
80353 
80354 /*
80355 ** Append a new element to the given IdList.  Create a new IdList if
80356 ** need be.
80357 **
80358 ** A new IdList is returned, or NULL if malloc() fails.
80359 */
80360 SQLITE_PRIVATE IdList *sqlite3IdListAppend(sqlite3 *db, IdList *pList, Token *pToken){
80361   int i;
80362   if( pList==0 ){
80363     pList = sqlite3DbMallocZero(db, sizeof(IdList) );
80364     if( pList==0 ) return 0;
80365     pList->nAlloc = 0;
80366   }
80367   pList->a = sqlite3ArrayAllocate(
80368       db,
80369       pList->a,
80370       sizeof(pList->a[0]),
80371       5,
80372       &pList->nId,
80373       &pList->nAlloc,
80374       &i
80375   );
80376   if( i<0 ){
80377     sqlite3IdListDelete(db, pList);
80378     return 0;
80379   }
80380   pList->a[i].zName = sqlite3NameFromToken(db, pToken);
80381   return pList;
80382 }
80383 
80384 /*
80385 ** Delete an IdList.
80386 */
80387 SQLITE_PRIVATE void sqlite3IdListDelete(sqlite3 *db, IdList *pList){
80388   int i;
80389   if( pList==0 ) return;
80390   for(i=0; i<pList->nId; i++){
80391     sqlite3DbFree(db, pList->a[i].zName);
80392   }
80393   sqlite3DbFree(db, pList->a);
80394   sqlite3DbFree(db, pList);
80395 }
80396 
80397 /*
80398 ** Return the index in pList of the identifier named zId.  Return -1
80399 ** if not found.
80400 */
80401 SQLITE_PRIVATE int sqlite3IdListIndex(IdList *pList, const char *zName){
80402   int i;
80403   if( pList==0 ) return -1;
80404   for(i=0; i<pList->nId; i++){
80405     if( sqlite3StrICmp(pList->a[i].zName, zName)==0 ) return i;
80406   }
80407   return -1;
80408 }
80409 
80410 /*
80411 ** Expand the space allocated for the given SrcList object by
80412 ** creating nExtra new slots beginning at iStart.  iStart is zero based.
80413 ** New slots are zeroed.
80414 **
80415 ** For example, suppose a SrcList initially contains two entries: A,B.
80416 ** To append 3 new entries onto the end, do this:
80417 **
80418 **    sqlite3SrcListEnlarge(db, pSrclist, 3, 2);
80419 **
80420 ** After the call above it would contain:  A, B, nil, nil, nil.
80421 ** If the iStart argument had been 1 instead of 2, then the result
80422 ** would have been:  A, nil, nil, nil, B.  To prepend the new slots,
80423 ** the iStart value would be 0.  The result then would
80424 ** be: nil, nil, nil, A, B.
80425 **
80426 ** If a memory allocation fails the SrcList is unchanged.  The
80427 ** db->mallocFailed flag will be set to true.
80428 */
80429 SQLITE_PRIVATE SrcList *sqlite3SrcListEnlarge(
80430   sqlite3 *db,       /* Database connection to notify of OOM errors */
80431   SrcList *pSrc,     /* The SrcList to be enlarged */
80432   int nExtra,        /* Number of new slots to add to pSrc->a[] */
80433   int iStart         /* Index in pSrc->a[] of first new slot */
80434 ){
80435   int i;
80436 
80437   /* Sanity checking on calling parameters */
80438   assert( iStart>=0 );
80439   assert( nExtra>=1 );
80440   assert( pSrc!=0 );
80441   assert( iStart<=pSrc->nSrc );
80442 
80443   /* Allocate additional space if needed */
80444   if( pSrc->nSrc+nExtra>pSrc->nAlloc ){
80445     SrcList *pNew;
80446     int nAlloc = pSrc->nSrc+nExtra;
80447     int nGot;
80448     pNew = sqlite3DbRealloc(db, pSrc,
80449                sizeof(*pSrc) + (nAlloc-1)*sizeof(pSrc->a[0]) );
80450     if( pNew==0 ){
80451       assert( db->mallocFailed );
80452       return pSrc;
80453     }
80454     pSrc = pNew;
80455     nGot = (sqlite3DbMallocSize(db, pNew) - sizeof(*pSrc))/sizeof(pSrc->a[0])+1;
80456     pSrc->nAlloc = (u16)nGot;
80457   }
80458 
80459   /* Move existing slots that come after the newly inserted slots
80460   ** out of the way */
80461   for(i=pSrc->nSrc-1; i>=iStart; i--){
80462     pSrc->a[i+nExtra] = pSrc->a[i];
80463   }
80464   pSrc->nSrc += (i16)nExtra;
80465 
80466   /* Zero the newly allocated slots */
80467   memset(&pSrc->a[iStart], 0, sizeof(pSrc->a[0])*nExtra);
80468   for(i=iStart; i<iStart+nExtra; i++){
80469     pSrc->a[i].iCursor = -1;
80470   }
80471 
80472   /* Return a pointer to the enlarged SrcList */
80473   return pSrc;
80474 }
80475 
80476 
80477 /*
80478 ** Append a new table name to the given SrcList.  Create a new SrcList if
80479 ** need be.  A new entry is created in the SrcList even if pTable is NULL.
80480 **
80481 ** A SrcList is returned, or NULL if there is an OOM error.  The returned
80482 ** SrcList might be the same as the SrcList that was input or it might be
80483 ** a new one.  If an OOM error does occurs, then the prior value of pList
80484 ** that is input to this routine is automatically freed.
80485 **
80486 ** If pDatabase is not null, it means that the table has an optional
80487 ** database name prefix.  Like this:  "database.table".  The pDatabase
80488 ** points to the table name and the pTable points to the database name.
80489 ** The SrcList.a[].zName field is filled with the table name which might
80490 ** come from pTable (if pDatabase is NULL) or from pDatabase.
80491 ** SrcList.a[].zDatabase is filled with the database name from pTable,
80492 ** or with NULL if no database is specified.
80493 **
80494 ** In other words, if call like this:
80495 **
80496 **         sqlite3SrcListAppend(D,A,B,0);
80497 **
80498 ** Then B is a table name and the database name is unspecified.  If called
80499 ** like this:
80500 **
80501 **         sqlite3SrcListAppend(D,A,B,C);
80502 **
80503 ** Then C is the table name and B is the database name.  If C is defined
80504 ** then so is B.  In other words, we never have a case where:
80505 **
80506 **         sqlite3SrcListAppend(D,A,0,C);
80507 **
80508 ** Both pTable and pDatabase are assumed to be quoted.  They are dequoted
80509 ** before being added to the SrcList.
80510 */
80511 SQLITE_PRIVATE SrcList *sqlite3SrcListAppend(
80512   sqlite3 *db,        /* Connection to notify of malloc failures */
80513   SrcList *pList,     /* Append to this SrcList. NULL creates a new SrcList */
80514   Token *pTable,      /* Table to append */
80515   Token *pDatabase    /* Database of the table */
80516 ){
80517   struct SrcList_item *pItem;
80518   assert( pDatabase==0 || pTable!=0 );  /* Cannot have C without B */
80519   if( pList==0 ){
80520     pList = sqlite3DbMallocZero(db, sizeof(SrcList) );
80521     if( pList==0 ) return 0;
80522     pList->nAlloc = 1;
80523   }
80524   pList = sqlite3SrcListEnlarge(db, pList, 1, pList->nSrc);
80525   if( db->mallocFailed ){
80526     sqlite3SrcListDelete(db, pList);
80527     return 0;
80528   }
80529   pItem = &pList->a[pList->nSrc-1];
80530   if( pDatabase && pDatabase->z==0 ){
80531     pDatabase = 0;
80532   }
80533   if( pDatabase ){
80534     Token *pTemp = pDatabase;
80535     pDatabase = pTable;
80536     pTable = pTemp;
80537   }
80538   pItem->zName = sqlite3NameFromToken(db, pTable);
80539   pItem->zDatabase = sqlite3NameFromToken(db, pDatabase);
80540   return pList;
80541 }
80542 
80543 /*
80544 ** Assign VdbeCursor index numbers to all tables in a SrcList
80545 */
80546 SQLITE_PRIVATE void sqlite3SrcListAssignCursors(Parse *pParse, SrcList *pList){
80547   int i;
80548   struct SrcList_item *pItem;
80549   assert(pList || pParse->db->mallocFailed );
80550   if( pList ){
80551     for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
80552       if( pItem->iCursor>=0 ) break;
80553       pItem->iCursor = pParse->nTab++;
80554       if( pItem->pSelect ){
80555         sqlite3SrcListAssignCursors(pParse, pItem->pSelect->pSrc);
80556       }
80557     }
80558   }
80559 }
80560 
80561 /*
80562 ** Delete an entire SrcList including all its substructure.
80563 */
80564 SQLITE_PRIVATE void sqlite3SrcListDelete(sqlite3 *db, SrcList *pList){
80565   int i;
80566   struct SrcList_item *pItem;
80567   if( pList==0 ) return;
80568   for(pItem=pList->a, i=0; i<pList->nSrc; i++, pItem++){
80569     sqlite3DbFree(db, pItem->zDatabase);
80570     sqlite3DbFree(db, pItem->zName);
80571     sqlite3DbFree(db, pItem->zAlias);
80572     sqlite3DbFree(db, pItem->zIndex);
80573     sqlite3DeleteTable(db, pItem->pTab);
80574     sqlite3SelectDelete(db, pItem->pSelect);
80575     sqlite3ExprDelete(db, pItem->pOn);
80576     sqlite3IdListDelete(db, pItem->pUsing);
80577   }
80578   sqlite3DbFree(db, pList);
80579 }
80580 
80581 /*
80582 ** This routine is called by the parser to add a new term to the
80583 ** end of a growing FROM clause.  The "p" parameter is the part of
80584 ** the FROM clause that has already been constructed.  "p" is NULL
80585 ** if this is the first term of the FROM clause.  pTable and pDatabase
80586 ** are the name of the table and database named in the FROM clause term.
80587 ** pDatabase is NULL if the database name qualifier is missing - the
80588 ** usual case.  If the term has a alias, then pAlias points to the
80589 ** alias token.  If the term is a subquery, then pSubquery is the
80590 ** SELECT statement that the subquery encodes.  The pTable and
80591 ** pDatabase parameters are NULL for subqueries.  The pOn and pUsing
80592 ** parameters are the content of the ON and USING clauses.
80593 **
80594 ** Return a new SrcList which encodes is the FROM with the new
80595 ** term added.
80596 */
80597 SQLITE_PRIVATE SrcList *sqlite3SrcListAppendFromTerm(
80598   Parse *pParse,          /* Parsing context */
80599   SrcList *p,             /* The left part of the FROM clause already seen */
80600   Token *pTable,          /* Name of the table to add to the FROM clause */
80601   Token *pDatabase,       /* Name of the database containing pTable */
80602   Token *pAlias,          /* The right-hand side of the AS subexpression */
80603   Select *pSubquery,      /* A subquery used in place of a table name */
80604   Expr *pOn,              /* The ON clause of a join */
80605   IdList *pUsing          /* The USING clause of a join */
80606 ){
80607   struct SrcList_item *pItem;
80608   sqlite3 *db = pParse->db;
80609   if( !p && (pOn || pUsing) ){
80610     sqlite3ErrorMsg(pParse, "a JOIN clause is required before %s",
80611       (pOn ? "ON" : "USING")
80612     );
80613     goto append_from_error;
80614   }
80615   p = sqlite3SrcListAppend(db, p, pTable, pDatabase);
80616   if( p==0 || NEVER(p->nSrc==0) ){
80617     goto append_from_error;
80618   }
80619   pItem = &p->a[p->nSrc-1];
80620   assert( pAlias!=0 );
80621   if( pAlias->n ){
80622     pItem->zAlias = sqlite3NameFromToken(db, pAlias);
80623   }
80624   pItem->pSelect = pSubquery;
80625   pItem->pOn = pOn;
80626   pItem->pUsing = pUsing;
80627   return p;
80628 
80629  append_from_error:
80630   assert( p==0 );
80631   sqlite3ExprDelete(db, pOn);
80632   sqlite3IdListDelete(db, pUsing);
80633   sqlite3SelectDelete(db, pSubquery);
80634   return 0;
80635 }
80636 
80637 /*
80638 ** Add an INDEXED BY or NOT INDEXED clause to the most recently added
80639 ** element of the source-list passed as the second argument.
80640 */
80641 SQLITE_PRIVATE void sqlite3SrcListIndexedBy(Parse *pParse, SrcList *p, Token *pIndexedBy){
80642   assert( pIndexedBy!=0 );
80643   if( p && ALWAYS(p->nSrc>0) ){
80644     struct SrcList_item *pItem = &p->a[p->nSrc-1];
80645     assert( pItem->notIndexed==0 && pItem->zIndex==0 );
80646     if( pIndexedBy->n==1 && !pIndexedBy->z ){
80647       /* A "NOT INDEXED" clause was supplied. See parse.y
80648       ** construct "indexed_opt" for details. */
80649       pItem->notIndexed = 1;
80650     }else{
80651       pItem->zIndex = sqlite3NameFromToken(pParse->db, pIndexedBy);
80652     }
80653   }
80654 }
80655 
80656 /*
80657 ** When building up a FROM clause in the parser, the join operator
80658 ** is initially attached to the left operand.  But the code generator
80659 ** expects the join operator to be on the right operand.  This routine
80660 ** Shifts all join operators from left to right for an entire FROM
80661 ** clause.
80662 **
80663 ** Example: Suppose the join is like this:
80664 **
80665 **           A natural cross join B
80666 **
80667 ** The operator is "natural cross join".  The A and B operands are stored
80668 ** in p->a[0] and p->a[1], respectively.  The parser initially stores the
80669 ** operator with A.  This routine shifts that operator over to B.
80670 */
80671 SQLITE_PRIVATE void sqlite3SrcListShiftJoinType(SrcList *p){
80672   if( p && p->a ){
80673     int i;
80674     for(i=p->nSrc-1; i>0; i--){
80675       p->a[i].jointype = p->a[i-1].jointype;
80676     }
80677     p->a[0].jointype = 0;
80678   }
80679 }
80680 
80681 /*
80682 ** Begin a transaction
80683 */
80684 SQLITE_PRIVATE void sqlite3BeginTransaction(Parse *pParse, int type){
80685   sqlite3 *db;
80686   Vdbe *v;
80687   int i;
80688 
80689   assert( pParse!=0 );
80690   db = pParse->db;
80691   assert( db!=0 );
80692 /*  if( db->aDb[0].pBt==0 ) return; */
80693   if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "BEGIN", 0, 0) ){
80694     return;
80695   }
80696   v = sqlite3GetVdbe(pParse);
80697   if( !v ) return;
80698   if( type!=TK_DEFERRED ){
80699     for(i=0; i<db->nDb; i++){
80700       sqlite3VdbeAddOp2(v, OP_Transaction, i, (type==TK_EXCLUSIVE)+1);
80701       sqlite3VdbeUsesBtree(v, i);
80702     }
80703   }
80704   sqlite3VdbeAddOp2(v, OP_AutoCommit, 0, 0);
80705 }
80706 
80707 /*
80708 ** Commit a transaction
80709 */
80710 SQLITE_PRIVATE void sqlite3CommitTransaction(Parse *pParse){
80711   sqlite3 *db;
80712   Vdbe *v;
80713 
80714   assert( pParse!=0 );
80715   db = pParse->db;
80716   assert( db!=0 );
80717 /*  if( db->aDb[0].pBt==0 ) return; */
80718   if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "COMMIT", 0, 0) ){
80719     return;
80720   }
80721   v = sqlite3GetVdbe(pParse);
80722   if( v ){
80723     sqlite3VdbeAddOp2(v, OP_AutoCommit, 1, 0);
80724   }
80725 }
80726 
80727 /*
80728 ** Rollback a transaction
80729 */
80730 SQLITE_PRIVATE void sqlite3RollbackTransaction(Parse *pParse){
80731   sqlite3 *db;
80732   Vdbe *v;
80733 
80734   assert( pParse!=0 );
80735   db = pParse->db;
80736   assert( db!=0 );
80737 /*  if( db->aDb[0].pBt==0 ) return; */
80738   if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "ROLLBACK", 0, 0) ){
80739     return;
80740   }
80741   v = sqlite3GetVdbe(pParse);
80742   if( v ){
80743     sqlite3VdbeAddOp2(v, OP_AutoCommit, 1, 1);
80744   }
80745 }
80746 
80747 /*
80748 ** This function is called by the parser when it parses a command to create,
80749 ** release or rollback an SQL savepoint.
80750 */
80751 SQLITE_PRIVATE void sqlite3Savepoint(Parse *pParse, int op, Token *pName){
80752   char *zName = sqlite3NameFromToken(pParse->db, pName);
80753   if( zName ){
80754     Vdbe *v = sqlite3GetVdbe(pParse);
80755 #ifndef SQLITE_OMIT_AUTHORIZATION
80756     static const char * const az[] = { "BEGIN", "RELEASE", "ROLLBACK" };
80757     assert( !SAVEPOINT_BEGIN && SAVEPOINT_RELEASE==1 && SAVEPOINT_ROLLBACK==2 );
80758 #endif
80759     if( !v || sqlite3AuthCheck(pParse, SQLITE_SAVEPOINT, az[op], zName, 0) ){
80760       sqlite3DbFree(pParse->db, zName);
80761       return;
80762     }
80763     sqlite3VdbeAddOp4(v, OP_Savepoint, op, 0, 0, zName, P4_DYNAMIC);
80764   }
80765 }
80766 
80767 /*
80768 ** Make sure the TEMP database is open and available for use.  Return
80769 ** the number of errors.  Leave any error messages in the pParse structure.
80770 */
80771 SQLITE_PRIVATE int sqlite3OpenTempDatabase(Parse *pParse){
80772   sqlite3 *db = pParse->db;
80773   if( db->aDb[1].pBt==0 && !pParse->explain ){
80774     int rc;
80775     Btree *pBt;
80776     static const int flags =
80777           SQLITE_OPEN_READWRITE |
80778           SQLITE_OPEN_CREATE |
80779           SQLITE_OPEN_EXCLUSIVE |
80780           SQLITE_OPEN_DELETEONCLOSE |
80781           SQLITE_OPEN_TEMP_DB;
80782 
80783     rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pBt, 0, flags);
80784     if( rc!=SQLITE_OK ){
80785       sqlite3ErrorMsg(pParse, "unable to open a temporary database "
80786         "file for storing temporary tables");
80787       pParse->rc = rc;
80788       return 1;
80789     }
80790     db->aDb[1].pBt = pBt;
80791     assert( db->aDb[1].pSchema );
80792     if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize, -1, 0) ){
80793       db->mallocFailed = 1;
80794       return 1;
80795     }
80796   }
80797   return 0;
80798 }
80799 
80800 /*
80801 ** Generate VDBE code that will verify the schema cookie and start
80802 ** a read-transaction for all named database files.
80803 **
80804 ** It is important that all schema cookies be verified and all
80805 ** read transactions be started before anything else happens in
80806 ** the VDBE program.  But this routine can be called after much other
80807 ** code has been generated.  So here is what we do:
80808 **
80809 ** The first time this routine is called, we code an OP_Goto that
80810 ** will jump to a subroutine at the end of the program.  Then we
80811 ** record every database that needs its schema verified in the
80812 ** pParse->cookieMask field.  Later, after all other code has been
80813 ** generated, the subroutine that does the cookie verifications and
80814 ** starts the transactions will be coded and the OP_Goto P2 value
80815 ** will be made to point to that subroutine.  The generation of the
80816 ** cookie verification subroutine code happens in sqlite3FinishCoding().
80817 **
80818 ** If iDb<0 then code the OP_Goto only - don't set flag to verify the
80819 ** schema on any databases.  This can be used to position the OP_Goto
80820 ** early in the code, before we know if any database tables will be used.
80821 */
80822 SQLITE_PRIVATE void sqlite3CodeVerifySchema(Parse *pParse, int iDb){
80823   Parse *pToplevel = sqlite3ParseToplevel(pParse);
80824 
80825   if( pToplevel->cookieGoto==0 ){
80826     Vdbe *v = sqlite3GetVdbe(pToplevel);
80827     if( v==0 ) return;  /* This only happens if there was a prior error */
80828     pToplevel->cookieGoto = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0)+1;
80829   }
80830   if( iDb>=0 ){
80831     sqlite3 *db = pToplevel->db;
80832     yDbMask mask;
80833 
80834     assert( iDb<db->nDb );
80835     assert( db->aDb[iDb].pBt!=0 || iDb==1 );
80836     assert( iDb<SQLITE_MAX_ATTACHED+2 );
80837     assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
80838     mask = ((yDbMask)1)<<iDb;
80839     if( (pToplevel->cookieMask & mask)==0 ){
80840       pToplevel->cookieMask |= mask;
80841       pToplevel->cookieValue[iDb] = db->aDb[iDb].pSchema->schema_cookie;
80842       if( !OMIT_TEMPDB && iDb==1 ){
80843         sqlite3OpenTempDatabase(pToplevel);
80844       }
80845     }
80846   }
80847 }
80848 
80849 /*
80850 ** If argument zDb is NULL, then call sqlite3CodeVerifySchema() for each
80851 ** attached database. Otherwise, invoke it for the database named zDb only.
80852 */
80853 SQLITE_PRIVATE void sqlite3CodeVerifyNamedSchema(Parse *pParse, const char *zDb){
80854   sqlite3 *db = pParse->db;
80855   int i;
80856   for(i=0; i<db->nDb; i++){
80857     Db *pDb = &db->aDb[i];
80858     if( pDb->pBt && (!zDb || 0==sqlite3StrICmp(zDb, pDb->zName)) ){
80859       sqlite3CodeVerifySchema(pParse, i);
80860     }
80861   }
80862 }
80863 
80864 /*
80865 ** Generate VDBE code that prepares for doing an operation that
80866 ** might change the database.
80867 **
80868 ** This routine starts a new transaction if we are not already within
80869 ** a transaction.  If we are already within a transaction, then a checkpoint
80870 ** is set if the setStatement parameter is true.  A checkpoint should
80871 ** be set for operations that might fail (due to a constraint) part of
80872 ** the way through and which will need to undo some writes without having to
80873 ** rollback the whole transaction.  For operations where all constraints
80874 ** can be checked before any changes are made to the database, it is never
80875 ** necessary to undo a write and the checkpoint should not be set.
80876 */
80877 SQLITE_PRIVATE void sqlite3BeginWriteOperation(Parse *pParse, int setStatement, int iDb){
80878   Parse *pToplevel = sqlite3ParseToplevel(pParse);
80879   sqlite3CodeVerifySchema(pParse, iDb);
80880   pToplevel->writeMask |= ((yDbMask)1)<<iDb;
80881   pToplevel->isMultiWrite |= setStatement;
80882 }
80883 
80884 /*
80885 ** Indicate that the statement currently under construction might write
80886 ** more than one entry (example: deleting one row then inserting another,
80887 ** inserting multiple rows in a table, or inserting a row and index entries.)
80888 ** If an abort occurs after some of these writes have completed, then it will
80889 ** be necessary to undo the completed writes.
80890 */
80891 SQLITE_PRIVATE void sqlite3MultiWrite(Parse *pParse){
80892   Parse *pToplevel = sqlite3ParseToplevel(pParse);
80893   pToplevel->isMultiWrite = 1;
80894 }
80895 
80896 /*
80897 ** The code generator calls this routine if is discovers that it is
80898 ** possible to abort a statement prior to completion.  In order to
80899 ** perform this abort without corrupting the database, we need to make
80900 ** sure that the statement is protected by a statement transaction.
80901 **
80902 ** Technically, we only need to set the mayAbort flag if the
80903 ** isMultiWrite flag was previously set.  There is a time dependency
80904 ** such that the abort must occur after the multiwrite.  This makes
80905 ** some statements involving the REPLACE conflict resolution algorithm
80906 ** go a little faster.  But taking advantage of this time dependency
80907 ** makes it more difficult to prove that the code is correct (in
80908 ** particular, it prevents us from writing an effective
80909 ** implementation of sqlite3AssertMayAbort()) and so we have chosen
80910 ** to take the safe route and skip the optimization.
80911 */
80912 SQLITE_PRIVATE void sqlite3MayAbort(Parse *pParse){
80913   Parse *pToplevel = sqlite3ParseToplevel(pParse);
80914   pToplevel->mayAbort = 1;
80915 }
80916 
80917 /*
80918 ** Code an OP_Halt that causes the vdbe to return an SQLITE_CONSTRAINT
80919 ** error. The onError parameter determines which (if any) of the statement
80920 ** and/or current transaction is rolled back.
80921 */
80922 SQLITE_PRIVATE void sqlite3HaltConstraint(Parse *pParse, int onError, char *p4, int p4type){
80923   Vdbe *v = sqlite3GetVdbe(pParse);
80924   if( onError==OE_Abort ){
80925     sqlite3MayAbort(pParse);
80926   }
80927   sqlite3VdbeAddOp4(v, OP_Halt, SQLITE_CONSTRAINT, onError, 0, p4, p4type);
80928 }
80929 
80930 /*
80931 ** Check to see if pIndex uses the collating sequence pColl.  Return
80932 ** true if it does and false if it does not.
80933 */
80934 #ifndef SQLITE_OMIT_REINDEX
80935 static int collationMatch(const char *zColl, Index *pIndex){
80936   int i;
80937   assert( zColl!=0 );
80938   for(i=0; i<pIndex->nColumn; i++){
80939     const char *z = pIndex->azColl[i];
80940     assert( z!=0 );
80941     if( 0==sqlite3StrICmp(z, zColl) ){
80942       return 1;
80943     }
80944   }
80945   return 0;
80946 }
80947 #endif
80948 
80949 /*
80950 ** Recompute all indices of pTab that use the collating sequence pColl.
80951 ** If pColl==0 then recompute all indices of pTab.
80952 */
80953 #ifndef SQLITE_OMIT_REINDEX
80954 static void reindexTable(Parse *pParse, Table *pTab, char const *zColl){
80955   Index *pIndex;              /* An index associated with pTab */
80956 
80957   for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){
80958     if( zColl==0 || collationMatch(zColl, pIndex) ){
80959       int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
80960       sqlite3BeginWriteOperation(pParse, 0, iDb);
80961       sqlite3RefillIndex(pParse, pIndex, -1);
80962     }
80963   }
80964 }
80965 #endif
80966 
80967 /*
80968 ** Recompute all indices of all tables in all databases where the
80969 ** indices use the collating sequence pColl.  If pColl==0 then recompute
80970 ** all indices everywhere.
80971 */
80972 #ifndef SQLITE_OMIT_REINDEX
80973 static void reindexDatabases(Parse *pParse, char const *zColl){
80974   Db *pDb;                    /* A single database */
80975   int iDb;                    /* The database index number */
80976   sqlite3 *db = pParse->db;   /* The database connection */
80977   HashElem *k;                /* For looping over tables in pDb */
80978   Table *pTab;                /* A table in the database */
80979 
80980   assert( sqlite3BtreeHoldsAllMutexes(db) );  /* Needed for schema access */
80981   for(iDb=0, pDb=db->aDb; iDb<db->nDb; iDb++, pDb++){
80982     assert( pDb!=0 );
80983     for(k=sqliteHashFirst(&pDb->pSchema->tblHash);  k; k=sqliteHashNext(k)){
80984       pTab = (Table*)sqliteHashData(k);
80985       reindexTable(pParse, pTab, zColl);
80986     }
80987   }
80988 }
80989 #endif
80990 
80991 /*
80992 ** Generate code for the REINDEX command.
80993 **
80994 **        REINDEX                            -- 1
80995 **        REINDEX  <collation>               -- 2
80996 **        REINDEX  ?<database>.?<tablename>  -- 3
80997 **        REINDEX  ?<database>.?<indexname>  -- 4
80998 **
80999 ** Form 1 causes all indices in all attached databases to be rebuilt.
81000 ** Form 2 rebuilds all indices in all databases that use the named
81001 ** collating function.  Forms 3 and 4 rebuild the named index or all
81002 ** indices associated with the named table.
81003 */
81004 #ifndef SQLITE_OMIT_REINDEX
81005 SQLITE_PRIVATE void sqlite3Reindex(Parse *pParse, Token *pName1, Token *pName2){
81006   CollSeq *pColl;             /* Collating sequence to be reindexed, or NULL */
81007   char *z;                    /* Name of a table or index */
81008   const char *zDb;            /* Name of the database */
81009   Table *pTab;                /* A table in the database */
81010   Index *pIndex;              /* An index associated with pTab */
81011   int iDb;                    /* The database index number */
81012   sqlite3 *db = pParse->db;   /* The database connection */
81013   Token *pObjName;            /* Name of the table or index to be reindexed */
81014 
81015   /* Read the database schema. If an error occurs, leave an error message
81016   ** and code in pParse and return NULL. */
81017   if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
81018     return;
81019   }
81020 
81021   if( pName1==0 ){
81022     reindexDatabases(pParse, 0);
81023     return;
81024   }else if( NEVER(pName2==0) || pName2->z==0 ){
81025     char *zColl;
81026     assert( pName1->z );
81027     zColl = sqlite3NameFromToken(pParse->db, pName1);
81028     if( !zColl ) return;
81029     pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0);
81030     if( pColl ){
81031       reindexDatabases(pParse, zColl);
81032       sqlite3DbFree(db, zColl);
81033       return;
81034     }
81035     sqlite3DbFree(db, zColl);
81036   }
81037   iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pObjName);
81038   if( iDb<0 ) return;
81039   z = sqlite3NameFromToken(db, pObjName);
81040   if( z==0 ) return;
81041   zDb = db->aDb[iDb].zName;
81042   pTab = sqlite3FindTable(db, z, zDb);
81043   if( pTab ){
81044     reindexTable(pParse, pTab, 0);
81045     sqlite3DbFree(db, z);
81046     return;
81047   }
81048   pIndex = sqlite3FindIndex(db, z, zDb);
81049   sqlite3DbFree(db, z);
81050   if( pIndex ){
81051     sqlite3BeginWriteOperation(pParse, 0, iDb);
81052     sqlite3RefillIndex(pParse, pIndex, -1);
81053     return;
81054   }
81055   sqlite3ErrorMsg(pParse, "unable to identify the object to be reindexed");
81056 }
81057 #endif
81058 
81059 /*
81060 ** Return a dynamicly allocated KeyInfo structure that can be used
81061 ** with OP_OpenRead or OP_OpenWrite to access database index pIdx.
81062 **
81063 ** If successful, a pointer to the new structure is returned. In this case
81064 ** the caller is responsible for calling sqlite3DbFree(db, ) on the returned
81065 ** pointer. If an error occurs (out of memory or missing collation
81066 ** sequence), NULL is returned and the state of pParse updated to reflect
81067 ** the error.
81068 */
81069 SQLITE_PRIVATE KeyInfo *sqlite3IndexKeyinfo(Parse *pParse, Index *pIdx){
81070   int i;
81071   int nCol = pIdx->nColumn;
81072   int nBytes = sizeof(KeyInfo) + (nCol-1)*sizeof(CollSeq*) + nCol;
81073   sqlite3 *db = pParse->db;
81074   KeyInfo *pKey = (KeyInfo *)sqlite3DbMallocZero(db, nBytes);
81075 
81076   if( pKey ){
81077     pKey->db = pParse->db;
81078     pKey->aSortOrder = (u8 *)&(pKey->aColl[nCol]);
81079     assert( &pKey->aSortOrder[nCol]==&(((u8 *)pKey)[nBytes]) );
81080     for(i=0; i<nCol; i++){
81081       char *zColl = pIdx->azColl[i];
81082       assert( zColl );
81083       pKey->aColl[i] = sqlite3LocateCollSeq(pParse, zColl);
81084       pKey->aSortOrder[i] = pIdx->aSortOrder[i];
81085     }
81086     pKey->nField = (u16)nCol;
81087   }
81088 
81089   if( pParse->nErr ){
81090     sqlite3DbFree(db, pKey);
81091     pKey = 0;
81092   }
81093   return pKey;
81094 }
81095 
81096 /************** End of build.c ***********************************************/
81097 /************** Begin file callback.c ****************************************/
81098 /*
81099 ** 2005 May 23
81100 **
81101 ** The author disclaims copyright to this source code.  In place of
81102 ** a legal notice, here is a blessing:
81103 **
81104 **    May you do good and not evil.
81105 **    May you find forgiveness for yourself and forgive others.
81106 **    May you share freely, never taking more than you give.
81107 **
81108 *************************************************************************
81109 **
81110 ** This file contains functions used to access the internal hash tables
81111 ** of user defined functions and collation sequences.
81112 */
81113 
81114 
81115 /*
81116 ** Invoke the 'collation needed' callback to request a collation sequence
81117 ** in the encoding enc of name zName, length nName.
81118 */
81119 static void callCollNeeded(sqlite3 *db, int enc, const char *zName){
81120   assert( !db->xCollNeeded || !db->xCollNeeded16 );
81121   if( db->xCollNeeded ){
81122     char *zExternal = sqlite3DbStrDup(db, zName);
81123     if( !zExternal ) return;
81124     db->xCollNeeded(db->pCollNeededArg, db, enc, zExternal);
81125     sqlite3DbFree(db, zExternal);
81126   }
81127 #ifndef SQLITE_OMIT_UTF16
81128   if( db->xCollNeeded16 ){
81129     char const *zExternal;
81130     sqlite3_value *pTmp = sqlite3ValueNew(db);
81131     sqlite3ValueSetStr(pTmp, -1, zName, SQLITE_UTF8, SQLITE_STATIC);
81132     zExternal = sqlite3ValueText(pTmp, SQLITE_UTF16NATIVE);
81133     if( zExternal ){
81134       db->xCollNeeded16(db->pCollNeededArg, db, (int)ENC(db), zExternal);
81135     }
81136     sqlite3ValueFree(pTmp);
81137   }
81138 #endif
81139 }
81140 
81141 /*
81142 ** This routine is called if the collation factory fails to deliver a
81143 ** collation function in the best encoding but there may be other versions
81144 ** of this collation function (for other text encodings) available. Use one
81145 ** of these instead if they exist. Avoid a UTF-8 <-> UTF-16 conversion if
81146 ** possible.
81147 */
81148 static int synthCollSeq(sqlite3 *db, CollSeq *pColl){
81149   CollSeq *pColl2;
81150   char *z = pColl->zName;
81151   int i;
81152   static const u8 aEnc[] = { SQLITE_UTF16BE, SQLITE_UTF16LE, SQLITE_UTF8 };
81153   for(i=0; i<3; i++){
81154     pColl2 = sqlite3FindCollSeq(db, aEnc[i], z, 0);
81155     if( pColl2->xCmp!=0 ){
81156       memcpy(pColl, pColl2, sizeof(CollSeq));
81157       pColl->xDel = 0;         /* Do not copy the destructor */
81158       return SQLITE_OK;
81159     }
81160   }
81161   return SQLITE_ERROR;
81162 }
81163 
81164 /*
81165 ** This function is responsible for invoking the collation factory callback
81166 ** or substituting a collation sequence of a different encoding when the
81167 ** requested collation sequence is not available in the desired encoding.
81168 **
81169 ** If it is not NULL, then pColl must point to the database native encoding
81170 ** collation sequence with name zName, length nName.
81171 **
81172 ** The return value is either the collation sequence to be used in database
81173 ** db for collation type name zName, length nName, or NULL, if no collation
81174 ** sequence can be found.
81175 **
81176 ** See also: sqlite3LocateCollSeq(), sqlite3FindCollSeq()
81177 */
81178 SQLITE_PRIVATE CollSeq *sqlite3GetCollSeq(
81179   sqlite3* db,          /* The database connection */
81180   u8 enc,               /* The desired encoding for the collating sequence */
81181   CollSeq *pColl,       /* Collating sequence with native encoding, or NULL */
81182   const char *zName     /* Collating sequence name */
81183 ){
81184   CollSeq *p;
81185 
81186   p = pColl;
81187   if( !p ){
81188     p = sqlite3FindCollSeq(db, enc, zName, 0);
81189   }
81190   if( !p || !p->xCmp ){
81191     /* No collation sequence of this type for this encoding is registered.
81192     ** Call the collation factory to see if it can supply us with one.
81193     */
81194     callCollNeeded(db, enc, zName);
81195     p = sqlite3FindCollSeq(db, enc, zName, 0);
81196   }
81197   if( p && !p->xCmp && synthCollSeq(db, p) ){
81198     p = 0;
81199   }
81200   assert( !p || p->xCmp );
81201   return p;
81202 }
81203 
81204 /*
81205 ** This routine is called on a collation sequence before it is used to
81206 ** check that it is defined. An undefined collation sequence exists when
81207 ** a database is loaded that contains references to collation sequences
81208 ** that have not been defined by sqlite3_create_collation() etc.
81209 **
81210 ** If required, this routine calls the 'collation needed' callback to
81211 ** request a definition of the collating sequence. If this doesn't work,
81212 ** an equivalent collating sequence that uses a text encoding different
81213 ** from the main database is substituted, if one is available.
81214 */
81215 SQLITE_PRIVATE int sqlite3CheckCollSeq(Parse *pParse, CollSeq *pColl){
81216   if( pColl ){
81217     const char *zName = pColl->zName;
81218     sqlite3 *db = pParse->db;
81219     CollSeq *p = sqlite3GetCollSeq(db, ENC(db), pColl, zName);
81220     if( !p ){
81221       sqlite3ErrorMsg(pParse, "no such collation sequence: %s", zName);
81222       pParse->nErr++;
81223       return SQLITE_ERROR;
81224     }
81225     assert( p==pColl );
81226   }
81227   return SQLITE_OK;
81228 }
81229 
81230 
81231 
81232 /*
81233 ** Locate and return an entry from the db.aCollSeq hash table. If the entry
81234 ** specified by zName and nName is not found and parameter 'create' is
81235 ** true, then create a new entry. Otherwise return NULL.
81236 **
81237 ** Each pointer stored in the sqlite3.aCollSeq hash table contains an
81238 ** array of three CollSeq structures. The first is the collation sequence
81239 ** prefferred for UTF-8, the second UTF-16le, and the third UTF-16be.
81240 **
81241 ** Stored immediately after the three collation sequences is a copy of
81242 ** the collation sequence name. A pointer to this string is stored in
81243 ** each collation sequence structure.
81244 */
81245 static CollSeq *findCollSeqEntry(
81246   sqlite3 *db,          /* Database connection */
81247   const char *zName,    /* Name of the collating sequence */
81248   int create            /* Create a new entry if true */
81249 ){
81250   CollSeq *pColl;
81251   int nName = sqlite3Strlen30(zName);
81252   pColl = sqlite3HashFind(&db->aCollSeq, zName, nName);
81253 
81254   if( 0==pColl && create ){
81255     pColl = sqlite3DbMallocZero(db, 3*sizeof(*pColl) + nName + 1 );
81256     if( pColl ){
81257       CollSeq *pDel = 0;
81258       pColl[0].zName = (char*)&pColl[3];
81259       pColl[0].enc = SQLITE_UTF8;
81260       pColl[1].zName = (char*)&pColl[3];
81261       pColl[1].enc = SQLITE_UTF16LE;
81262       pColl[2].zName = (char*)&pColl[3];
81263       pColl[2].enc = SQLITE_UTF16BE;
81264       memcpy(pColl[0].zName, zName, nName);
81265       pColl[0].zName[nName] = 0;
81266       pDel = sqlite3HashInsert(&db->aCollSeq, pColl[0].zName, nName, pColl);
81267 
81268       /* If a malloc() failure occurred in sqlite3HashInsert(), it will
81269       ** return the pColl pointer to be deleted (because it wasn't added
81270       ** to the hash table).
81271       */
81272       assert( pDel==0 || pDel==pColl );
81273       if( pDel!=0 ){
81274         db->mallocFailed = 1;
81275         sqlite3DbFree(db, pDel);
81276         pColl = 0;
81277       }
81278     }
81279   }
81280   return pColl;
81281 }
81282 
81283 /*
81284 ** Parameter zName points to a UTF-8 encoded string nName bytes long.
81285 ** Return the CollSeq* pointer for the collation sequence named zName
81286 ** for the encoding 'enc' from the database 'db'.
81287 **
81288 ** If the entry specified is not found and 'create' is true, then create a
81289 ** new entry.  Otherwise return NULL.
81290 **
81291 ** A separate function sqlite3LocateCollSeq() is a wrapper around
81292 ** this routine.  sqlite3LocateCollSeq() invokes the collation factory
81293 ** if necessary and generates an error message if the collating sequence
81294 ** cannot be found.
81295 **
81296 ** See also: sqlite3LocateCollSeq(), sqlite3GetCollSeq()
81297 */
81298 SQLITE_PRIVATE CollSeq *sqlite3FindCollSeq(
81299   sqlite3 *db,
81300   u8 enc,
81301   const char *zName,
81302   int create
81303 ){
81304   CollSeq *pColl;
81305   if( zName ){
81306     pColl = findCollSeqEntry(db, zName, create);
81307   }else{
81308     pColl = db->pDfltColl;
81309   }
81310   assert( SQLITE_UTF8==1 && SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 );
81311   assert( enc>=SQLITE_UTF8 && enc<=SQLITE_UTF16BE );
81312   if( pColl ) pColl += enc-1;
81313   return pColl;
81314 }
81315 
81316 /* During the search for the best function definition, this procedure
81317 ** is called to test how well the function passed as the first argument
81318 ** matches the request for a function with nArg arguments in a system
81319 ** that uses encoding enc. The value returned indicates how well the
81320 ** request is matched. A higher value indicates a better match.
81321 **
81322 ** The returned value is always between 0 and 6, as follows:
81323 **
81324 ** 0: Not a match, or if nArg<0 and the function is has no implementation.
81325 ** 1: A variable arguments function that prefers UTF-8 when a UTF-16
81326 **    encoding is requested, or vice versa.
81327 ** 2: A variable arguments function that uses UTF-16BE when UTF-16LE is
81328 **    requested, or vice versa.
81329 ** 3: A variable arguments function using the same text encoding.
81330 ** 4: A function with the exact number of arguments requested that
81331 **    prefers UTF-8 when a UTF-16 encoding is requested, or vice versa.
81332 ** 5: A function with the exact number of arguments requested that
81333 **    prefers UTF-16LE when UTF-16BE is requested, or vice versa.
81334 ** 6: An exact match.
81335 **
81336 */
81337 static int matchQuality(FuncDef *p, int nArg, u8 enc){
81338   int match = 0;
81339   if( p->nArg==-1 || p->nArg==nArg
81340    || (nArg==-1 && (p->xFunc!=0 || p->xStep!=0))
81341   ){
81342     match = 1;
81343     if( p->nArg==nArg || nArg==-1 ){
81344       match = 4;
81345     }
81346     if( enc==p->iPrefEnc ){
81347       match += 2;
81348     }
81349     else if( (enc==SQLITE_UTF16LE && p->iPrefEnc==SQLITE_UTF16BE) ||
81350              (enc==SQLITE_UTF16BE && p->iPrefEnc==SQLITE_UTF16LE) ){
81351       match += 1;
81352     }
81353   }
81354   return match;
81355 }
81356 
81357 /*
81358 ** Search a FuncDefHash for a function with the given name.  Return
81359 ** a pointer to the matching FuncDef if found, or 0 if there is no match.
81360 */
81361 static FuncDef *functionSearch(
81362   FuncDefHash *pHash,  /* Hash table to search */
81363   int h,               /* Hash of the name */
81364   const char *zFunc,   /* Name of function */
81365   int nFunc            /* Number of bytes in zFunc */
81366 ){
81367   FuncDef *p;
81368   for(p=pHash->a[h]; p; p=p->pHash){
81369     if( sqlite3StrNICmp(p->zName, zFunc, nFunc)==0 && p->zName[nFunc]==0 ){
81370       return p;
81371     }
81372   }
81373   return 0;
81374 }
81375 
81376 /*
81377 ** Insert a new FuncDef into a FuncDefHash hash table.
81378 */
81379 SQLITE_PRIVATE void sqlite3FuncDefInsert(
81380   FuncDefHash *pHash,  /* The hash table into which to insert */
81381   FuncDef *pDef        /* The function definition to insert */
81382 ){
81383   FuncDef *pOther;
81384   int nName = sqlite3Strlen30(pDef->zName);
81385   u8 c1 = (u8)pDef->zName[0];
81386   int h = (sqlite3UpperToLower[c1] + nName) % ArraySize(pHash->a);
81387   pOther = functionSearch(pHash, h, pDef->zName, nName);
81388   if( pOther ){
81389     assert( pOther!=pDef && pOther->pNext!=pDef );
81390     pDef->pNext = pOther->pNext;
81391     pOther->pNext = pDef;
81392   }else{
81393     pDef->pNext = 0;
81394     pDef->pHash = pHash->a[h];
81395     pHash->a[h] = pDef;
81396   }
81397 }
81398 
81399 
81400 
81401 /*
81402 ** Locate a user function given a name, a number of arguments and a flag
81403 ** indicating whether the function prefers UTF-16 over UTF-8.  Return a
81404 ** pointer to the FuncDef structure that defines that function, or return
81405 ** NULL if the function does not exist.
81406 **
81407 ** If the createFlag argument is true, then a new (blank) FuncDef
81408 ** structure is created and liked into the "db" structure if a
81409 ** no matching function previously existed.  When createFlag is true
81410 ** and the nArg parameter is -1, then only a function that accepts
81411 ** any number of arguments will be returned.
81412 **
81413 ** If createFlag is false and nArg is -1, then the first valid
81414 ** function found is returned.  A function is valid if either xFunc
81415 ** or xStep is non-zero.
81416 **
81417 ** If createFlag is false, then a function with the required name and
81418 ** number of arguments may be returned even if the eTextRep flag does not
81419 ** match that requested.
81420 */
81421 SQLITE_PRIVATE FuncDef *sqlite3FindFunction(
81422   sqlite3 *db,       /* An open database */
81423   const char *zName, /* Name of the function.  Not null-terminated */
81424   int nName,         /* Number of characters in the name */
81425   int nArg,          /* Number of arguments.  -1 means any number */
81426   u8 enc,            /* Preferred text encoding */
81427   int createFlag     /* Create new entry if true and does not otherwise exist */
81428 ){
81429   FuncDef *p;         /* Iterator variable */
81430   FuncDef *pBest = 0; /* Best match found so far */
81431   int bestScore = 0;  /* Score of best match */
81432   int h;              /* Hash value */
81433 
81434 
81435   assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
81436   h = (sqlite3UpperToLower[(u8)zName[0]] + nName) % ArraySize(db->aFunc.a);
81437 
81438   /* First search for a match amongst the application-defined functions.
81439   */
81440   p = functionSearch(&db->aFunc, h, zName, nName);
81441   while( p ){
81442     int score = matchQuality(p, nArg, enc);
81443     if( score>bestScore ){
81444       pBest = p;
81445       bestScore = score;
81446     }
81447     p = p->pNext;
81448   }
81449 
81450   /* If no match is found, search the built-in functions.
81451   **
81452   ** If the SQLITE_PreferBuiltin flag is set, then search the built-in
81453   ** functions even if a prior app-defined function was found.  And give
81454   ** priority to built-in functions.
81455   **
81456   ** Except, if createFlag is true, that means that we are trying to
81457   ** install a new function.  Whatever FuncDef structure is returned it will
81458   ** have fields overwritten with new information appropriate for the
81459   ** new function.  But the FuncDefs for built-in functions are read-only.
81460   ** So we must not search for built-ins when creating a new function.
81461   */
81462   if( !createFlag && (pBest==0 || (db->flags & SQLITE_PreferBuiltin)!=0) ){
81463     FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
81464     bestScore = 0;
81465     p = functionSearch(pHash, h, zName, nName);
81466     while( p ){
81467       int score = matchQuality(p, nArg, enc);
81468       if( score>bestScore ){
81469         pBest = p;
81470         bestScore = score;
81471       }
81472       p = p->pNext;
81473     }
81474   }
81475 
81476   /* If the createFlag parameter is true and the search did not reveal an
81477   ** exact match for the name, number of arguments and encoding, then add a
81478   ** new entry to the hash table and return it.
81479   */
81480   if( createFlag && (bestScore<6 || pBest->nArg!=nArg) &&
81481       (pBest = sqlite3DbMallocZero(db, sizeof(*pBest)+nName+1))!=0 ){
81482     pBest->zName = (char *)&pBest[1];
81483     pBest->nArg = (u16)nArg;
81484     pBest->iPrefEnc = enc;
81485     memcpy(pBest->zName, zName, nName);
81486     pBest->zName[nName] = 0;
81487     sqlite3FuncDefInsert(&db->aFunc, pBest);
81488   }
81489 
81490   if( pBest && (pBest->xStep || pBest->xFunc || createFlag) ){
81491     return pBest;
81492   }
81493   return 0;
81494 }
81495 
81496 /*
81497 ** Free all resources held by the schema structure. The void* argument points
81498 ** at a Schema struct. This function does not call sqlite3DbFree(db, ) on the
81499 ** pointer itself, it just cleans up subsidiary resources (i.e. the contents
81500 ** of the schema hash tables).
81501 **
81502 ** The Schema.cache_size variable is not cleared.
81503 */
81504 SQLITE_PRIVATE void sqlite3SchemaClear(void *p){
81505   Hash temp1;
81506   Hash temp2;
81507   HashElem *pElem;
81508   Schema *pSchema = (Schema *)p;
81509 
81510   temp1 = pSchema->tblHash;
81511   temp2 = pSchema->trigHash;
81512   sqlite3HashInit(&pSchema->trigHash);
81513   sqlite3HashClear(&pSchema->idxHash);
81514   for(pElem=sqliteHashFirst(&temp2); pElem; pElem=sqliteHashNext(pElem)){
81515     sqlite3DeleteTrigger(0, (Trigger*)sqliteHashData(pElem));
81516   }
81517   sqlite3HashClear(&temp2);
81518   sqlite3HashInit(&pSchema->tblHash);
81519   for(pElem=sqliteHashFirst(&temp1); pElem; pElem=sqliteHashNext(pElem)){
81520     Table *pTab = sqliteHashData(pElem);
81521     sqlite3DeleteTable(0, pTab);
81522   }
81523   sqlite3HashClear(&temp1);
81524   sqlite3HashClear(&pSchema->fkeyHash);
81525   pSchema->pSeqTab = 0;
81526   if( pSchema->flags & DB_SchemaLoaded ){
81527     pSchema->iGeneration++;
81528     pSchema->flags &= ~DB_SchemaLoaded;
81529   }
81530 }
81531 
81532 /*
81533 ** Find and return the schema associated with a BTree.  Create
81534 ** a new one if necessary.
81535 */
81536 SQLITE_PRIVATE Schema *sqlite3SchemaGet(sqlite3 *db, Btree *pBt){
81537   Schema * p;
81538   if( pBt ){
81539     p = (Schema *)sqlite3BtreeSchema(pBt, sizeof(Schema), sqlite3SchemaClear);
81540   }else{
81541     p = (Schema *)sqlite3DbMallocZero(0, sizeof(Schema));
81542   }
81543   if( !p ){
81544     db->mallocFailed = 1;
81545   }else if ( 0==p->file_format ){
81546     sqlite3HashInit(&p->tblHash);
81547     sqlite3HashInit(&p->idxHash);
81548     sqlite3HashInit(&p->trigHash);
81549     sqlite3HashInit(&p->fkeyHash);
81550     p->enc = SQLITE_UTF8;
81551   }
81552   return p;
81553 }
81554 
81555 /************** End of callback.c ********************************************/
81556 /************** Begin file delete.c ******************************************/
81557 /*
81558 ** 2001 September 15
81559 **
81560 ** The author disclaims copyright to this source code.  In place of
81561 ** a legal notice, here is a blessing:
81562 **
81563 **    May you do good and not evil.
81564 **    May you find forgiveness for yourself and forgive others.
81565 **    May you share freely, never taking more than you give.
81566 **
81567 *************************************************************************
81568 ** This file contains C code routines that are called by the parser
81569 ** in order to generate code for DELETE FROM statements.
81570 */
81571 
81572 /*
81573 ** While a SrcList can in general represent multiple tables and subqueries
81574 ** (as in the FROM clause of a SELECT statement) in this case it contains
81575 ** the name of a single table, as one might find in an INSERT, DELETE,
81576 ** or UPDATE statement.  Look up that table in the symbol table and
81577 ** return a pointer.  Set an error message and return NULL if the table
81578 ** name is not found or if any other error occurs.
81579 **
81580 ** The following fields are initialized appropriate in pSrc:
81581 **
81582 **    pSrc->a[0].pTab       Pointer to the Table object
81583 **    pSrc->a[0].pIndex     Pointer to the INDEXED BY index, if there is one
81584 **
81585 */
81586 SQLITE_PRIVATE Table *sqlite3SrcListLookup(Parse *pParse, SrcList *pSrc){
81587   struct SrcList_item *pItem = pSrc->a;
81588   Table *pTab;
81589   assert( pItem && pSrc->nSrc==1 );
81590   pTab = sqlite3LocateTable(pParse, 0, pItem->zName, pItem->zDatabase);
81591   sqlite3DeleteTable(pParse->db, pItem->pTab);
81592   pItem->pTab = pTab;
81593   if( pTab ){
81594     pTab->nRef++;
81595   }
81596   if( sqlite3IndexedByLookup(pParse, pItem) ){
81597     pTab = 0;
81598   }
81599   return pTab;
81600 }
81601 
81602 /*
81603 ** Check to make sure the given table is writable.  If it is not
81604 ** writable, generate an error message and return 1.  If it is
81605 ** writable return 0;
81606 */
81607 SQLITE_PRIVATE int sqlite3IsReadOnly(Parse *pParse, Table *pTab, int viewOk){
81608   /* A table is not writable under the following circumstances:
81609   **
81610   **   1) It is a virtual table and no implementation of the xUpdate method
81611   **      has been provided, or
81612   **   2) It is a system table (i.e. sqlite_master), this call is not
81613   **      part of a nested parse and writable_schema pragma has not
81614   **      been specified.
81615   **
81616   ** In either case leave an error message in pParse and return non-zero.
81617   */
81618   if( ( IsVirtual(pTab)
81619      && sqlite3GetVTable(pParse->db, pTab)->pMod->pModule->xUpdate==0 )
81620    || ( (pTab->tabFlags & TF_Readonly)!=0
81621      && (pParse->db->flags & SQLITE_WriteSchema)==0
81622      && pParse->nested==0 )
81623   ){
81624     sqlite3ErrorMsg(pParse, "table %s may not be modified", pTab->zName);
81625     return 1;
81626   }
81627 
81628 #ifndef SQLITE_OMIT_VIEW
81629   if( !viewOk && pTab->pSelect ){
81630     sqlite3ErrorMsg(pParse,"cannot modify %s because it is a view",pTab->zName);
81631     return 1;
81632   }
81633 #endif
81634   return 0;
81635 }
81636 
81637 
81638 #if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
81639 /*
81640 ** Evaluate a view and store its result in an ephemeral table.  The
81641 ** pWhere argument is an optional WHERE clause that restricts the
81642 ** set of rows in the view that are to be added to the ephemeral table.
81643 */
81644 SQLITE_PRIVATE void sqlite3MaterializeView(
81645   Parse *pParse,       /* Parsing context */
81646   Table *pView,        /* View definition */
81647   Expr *pWhere,        /* Optional WHERE clause to be added */
81648   int iCur             /* Cursor number for ephemerial table */
81649 ){
81650   SelectDest dest;
81651   Select *pDup;
81652   sqlite3 *db = pParse->db;
81653 
81654   pDup = sqlite3SelectDup(db, pView->pSelect, 0);
81655   if( pWhere ){
81656     SrcList *pFrom;
81657 
81658     pWhere = sqlite3ExprDup(db, pWhere, 0);
81659     pFrom = sqlite3SrcListAppend(db, 0, 0, 0);
81660     if( pFrom ){
81661       assert( pFrom->nSrc==1 );
81662       pFrom->a[0].zAlias = sqlite3DbStrDup(db, pView->zName);
81663       pFrom->a[0].pSelect = pDup;
81664       assert( pFrom->a[0].pOn==0 );
81665       assert( pFrom->a[0].pUsing==0 );
81666     }else{
81667       sqlite3SelectDelete(db, pDup);
81668     }
81669     pDup = sqlite3SelectNew(pParse, 0, pFrom, pWhere, 0, 0, 0, 0, 0, 0);
81670   }
81671   sqlite3SelectDestInit(&dest, SRT_EphemTab, iCur);
81672   sqlite3Select(pParse, pDup, &dest);
81673   sqlite3SelectDelete(db, pDup);
81674 }
81675 #endif /* !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER) */
81676 
81677 #if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
81678 /*
81679 ** Generate an expression tree to implement the WHERE, ORDER BY,
81680 ** and LIMIT/OFFSET portion of DELETE and UPDATE statements.
81681 **
81682 **     DELETE FROM table_wxyz WHERE a<5 ORDER BY a LIMIT 1;
81683 **                            \__________________________/
81684 **                               pLimitWhere (pInClause)
81685 */
81686 SQLITE_PRIVATE Expr *sqlite3LimitWhere(
81687   Parse *pParse,               /* The parser context */
81688   SrcList *pSrc,               /* the FROM clause -- which tables to scan */
81689   Expr *pWhere,                /* The WHERE clause.  May be null */
81690   ExprList *pOrderBy,          /* The ORDER BY clause.  May be null */
81691   Expr *pLimit,                /* The LIMIT clause.  May be null */
81692   Expr *pOffset,               /* The OFFSET clause.  May be null */
81693   char *zStmtType              /* Either DELETE or UPDATE.  For error messages. */
81694 ){
81695   Expr *pWhereRowid = NULL;    /* WHERE rowid .. */
81696   Expr *pInClause = NULL;      /* WHERE rowid IN ( select ) */
81697   Expr *pSelectRowid = NULL;   /* SELECT rowid ... */
81698   ExprList *pEList = NULL;     /* Expression list contaning only pSelectRowid */
81699   SrcList *pSelectSrc = NULL;  /* SELECT rowid FROM x ... (dup of pSrc) */
81700   Select *pSelect = NULL;      /* Complete SELECT tree */
81701 
81702   /* Check that there isn't an ORDER BY without a LIMIT clause.
81703   */
81704   if( pOrderBy && (pLimit == 0) ) {
81705     sqlite3ErrorMsg(pParse, "ORDER BY without LIMIT on %s", zStmtType);
81706     pParse->parseError = 1;
81707     goto limit_where_cleanup_2;
81708   }
81709 
81710   /* We only need to generate a select expression if there
81711   ** is a limit/offset term to enforce.
81712   */
81713   if( pLimit == 0 ) {
81714     /* if pLimit is null, pOffset will always be null as well. */
81715     assert( pOffset == 0 );
81716     return pWhere;
81717   }
81718 
81719   /* Generate a select expression tree to enforce the limit/offset
81720   ** term for the DELETE or UPDATE statement.  For example:
81721   **   DELETE FROM table_a WHERE col1=1 ORDER BY col2 LIMIT 1 OFFSET 1
81722   ** becomes:
81723   **   DELETE FROM table_a WHERE rowid IN (
81724   **     SELECT rowid FROM table_a WHERE col1=1 ORDER BY col2 LIMIT 1 OFFSET 1
81725   **   );
81726   */
81727 
81728   pSelectRowid = sqlite3PExpr(pParse, TK_ROW, 0, 0, 0);
81729   if( pSelectRowid == 0 ) goto limit_where_cleanup_2;
81730   pEList = sqlite3ExprListAppend(pParse, 0, pSelectRowid);
81731   if( pEList == 0 ) goto limit_where_cleanup_2;
81732 
81733   /* duplicate the FROM clause as it is needed by both the DELETE/UPDATE tree
81734   ** and the SELECT subtree. */
81735   pSelectSrc = sqlite3SrcListDup(pParse->db, pSrc, 0);
81736   if( pSelectSrc == 0 ) {
81737     sqlite3ExprListDelete(pParse->db, pEList);
81738     goto limit_where_cleanup_2;
81739   }
81740 
81741   /* generate the SELECT expression tree. */
81742   pSelect = sqlite3SelectNew(pParse,pEList,pSelectSrc,pWhere,0,0,
81743                              pOrderBy,0,pLimit,pOffset);
81744   if( pSelect == 0 ) return 0;
81745 
81746   /* now generate the new WHERE rowid IN clause for the DELETE/UDPATE */
81747   pWhereRowid = sqlite3PExpr(pParse, TK_ROW, 0, 0, 0);
81748   if( pWhereRowid == 0 ) goto limit_where_cleanup_1;
81749   pInClause = sqlite3PExpr(pParse, TK_IN, pWhereRowid, 0, 0);
81750   if( pInClause == 0 ) goto limit_where_cleanup_1;
81751 
81752   pInClause->x.pSelect = pSelect;
81753   pInClause->flags |= EP_xIsSelect;
81754   sqlite3ExprSetHeight(pParse, pInClause);
81755   return pInClause;
81756 
81757   /* something went wrong. clean up anything allocated. */
81758 limit_where_cleanup_1:
81759   sqlite3SelectDelete(pParse->db, pSelect);
81760   return 0;
81761 
81762 limit_where_cleanup_2:
81763   sqlite3ExprDelete(pParse->db, pWhere);
81764   sqlite3ExprListDelete(pParse->db, pOrderBy);
81765   sqlite3ExprDelete(pParse->db, pLimit);
81766   sqlite3ExprDelete(pParse->db, pOffset);
81767   return 0;
81768 }
81769 #endif /* defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY) */
81770 
81771 /*
81772 ** Generate code for a DELETE FROM statement.
81773 **
81774 **     DELETE FROM table_wxyz WHERE a<5 AND b NOT NULL;
81775 **                 \________/       \________________/
81776 **                  pTabList              pWhere
81777 */
81778 SQLITE_PRIVATE void sqlite3DeleteFrom(
81779   Parse *pParse,         /* The parser context */
81780   SrcList *pTabList,     /* The table from which we should delete things */
81781   Expr *pWhere           /* The WHERE clause.  May be null */
81782 ){
81783   Vdbe *v;               /* The virtual database engine */
81784   Table *pTab;           /* The table from which records will be deleted */
81785   const char *zDb;       /* Name of database holding pTab */
81786   int end, addr = 0;     /* A couple addresses of generated code */
81787   int i;                 /* Loop counter */
81788   WhereInfo *pWInfo;     /* Information about the WHERE clause */
81789   Index *pIdx;           /* For looping over indices of the table */
81790   int iCur;              /* VDBE Cursor number for pTab */
81791   sqlite3 *db;           /* Main database structure */
81792   AuthContext sContext;  /* Authorization context */
81793   NameContext sNC;       /* Name context to resolve expressions in */
81794   int iDb;               /* Database number */
81795   int memCnt = -1;       /* Memory cell used for change counting */
81796   int rcauth;            /* Value returned by authorization callback */
81797 
81798 #ifndef SQLITE_OMIT_TRIGGER
81799   int isView;                  /* True if attempting to delete from a view */
81800   Trigger *pTrigger;           /* List of table triggers, if required */
81801 #endif
81802 
81803   memset(&sContext, 0, sizeof(sContext));
81804   db = pParse->db;
81805   if( pParse->nErr || db->mallocFailed ){
81806     goto delete_from_cleanup;
81807   }
81808   assert( pTabList->nSrc==1 );
81809 
81810   /* Locate the table which we want to delete.  This table has to be
81811   ** put in an SrcList structure because some of the subroutines we
81812   ** will be calling are designed to work with multiple tables and expect
81813   ** an SrcList* parameter instead of just a Table* parameter.
81814   */
81815   pTab = sqlite3SrcListLookup(pParse, pTabList);
81816   if( pTab==0 )  goto delete_from_cleanup;
81817 
81818   /* Figure out if we have any triggers and if the table being
81819   ** deleted from is a view
81820   */
81821 #ifndef SQLITE_OMIT_TRIGGER
81822   pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
81823   isView = pTab->pSelect!=0;
81824 #else
81825 # define pTrigger 0
81826 # define isView 0
81827 #endif
81828 #ifdef SQLITE_OMIT_VIEW
81829 # undef isView
81830 # define isView 0
81831 #endif
81832 
81833   /* If pTab is really a view, make sure it has been initialized.
81834   */
81835   if( sqlite3ViewGetColumnNames(pParse, pTab) ){
81836     goto delete_from_cleanup;
81837   }
81838 
81839   if( sqlite3IsReadOnly(pParse, pTab, (pTrigger?1:0)) ){
81840     goto delete_from_cleanup;
81841   }
81842   iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
81843   assert( iDb<db->nDb );
81844   zDb = db->aDb[iDb].zName;
81845   rcauth = sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb);
81846   assert( rcauth==SQLITE_OK || rcauth==SQLITE_DENY || rcauth==SQLITE_IGNORE );
81847   if( rcauth==SQLITE_DENY ){
81848     goto delete_from_cleanup;
81849   }
81850   assert(!isView || pTrigger);
81851 
81852   /* Assign  cursor number to the table and all its indices.
81853   */
81854   assert( pTabList->nSrc==1 );
81855   iCur = pTabList->a[0].iCursor = pParse->nTab++;
81856   for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
81857     pParse->nTab++;
81858   }
81859 
81860   /* Start the view context
81861   */
81862   if( isView ){
81863     sqlite3AuthContextPush(pParse, &sContext, pTab->zName);
81864   }
81865 
81866   /* Begin generating code.
81867   */
81868   v = sqlite3GetVdbe(pParse);
81869   if( v==0 ){
81870     goto delete_from_cleanup;
81871   }
81872   if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
81873   sqlite3BeginWriteOperation(pParse, 1, iDb);
81874 
81875   /* If we are trying to delete from a view, realize that view into
81876   ** a ephemeral table.
81877   */
81878 #if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
81879   if( isView ){
81880     sqlite3MaterializeView(pParse, pTab, pWhere, iCur);
81881   }
81882 #endif
81883 
81884   /* Resolve the column names in the WHERE clause.
81885   */
81886   memset(&sNC, 0, sizeof(sNC));
81887   sNC.pParse = pParse;
81888   sNC.pSrcList = pTabList;
81889   if( sqlite3ResolveExprNames(&sNC, pWhere) ){
81890     goto delete_from_cleanup;
81891   }
81892 
81893   /* Initialize the counter of the number of rows deleted, if
81894   ** we are counting rows.
81895   */
81896   if( db->flags & SQLITE_CountRows ){
81897     memCnt = ++pParse->nMem;
81898     sqlite3VdbeAddOp2(v, OP_Integer, 0, memCnt);
81899   }
81900 
81901 #ifndef SQLITE_OMIT_TRUNCATE_OPTIMIZATION
81902   /* Special case: A DELETE without a WHERE clause deletes everything.
81903   ** It is easier just to erase the whole table. Prior to version 3.6.5,
81904   ** this optimization caused the row change count (the value returned by
81905   ** API function sqlite3_count_changes) to be set incorrectly.  */
81906   if( rcauth==SQLITE_OK && pWhere==0 && !pTrigger && !IsVirtual(pTab)
81907    && 0==sqlite3FkRequired(pParse, pTab, 0, 0)
81908   ){
81909     assert( !isView );
81910     sqlite3VdbeAddOp4(v, OP_Clear, pTab->tnum, iDb, memCnt,
81911                       pTab->zName, P4_STATIC);
81912     for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
81913       assert( pIdx->pSchema==pTab->pSchema );
81914       sqlite3VdbeAddOp2(v, OP_Clear, pIdx->tnum, iDb);
81915     }
81916   }else
81917 #endif /* SQLITE_OMIT_TRUNCATE_OPTIMIZATION */
81918   /* The usual case: There is a WHERE clause so we have to scan through
81919   ** the table and pick which records to delete.
81920   */
81921   {
81922     int iRowSet = ++pParse->nMem;   /* Register for rowset of rows to delete */
81923     int iRowid = ++pParse->nMem;    /* Used for storing rowid values. */
81924     int regRowid;                   /* Actual register containing rowids */
81925 
81926     /* Collect rowids of every row to be deleted.
81927     */
81928     sqlite3VdbeAddOp2(v, OP_Null, 0, iRowSet);
81929     pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere,0,WHERE_DUPLICATES_OK);
81930     if( pWInfo==0 ) goto delete_from_cleanup;
81931     regRowid = sqlite3ExprCodeGetColumn(pParse, pTab, -1, iCur, iRowid);
81932     sqlite3VdbeAddOp2(v, OP_RowSetAdd, iRowSet, regRowid);
81933     if( db->flags & SQLITE_CountRows ){
81934       sqlite3VdbeAddOp2(v, OP_AddImm, memCnt, 1);
81935     }
81936     sqlite3WhereEnd(pWInfo);
81937 
81938     /* Delete every item whose key was written to the list during the
81939     ** database scan.  We have to delete items after the scan is complete
81940     ** because deleting an item can change the scan order.  */
81941     end = sqlite3VdbeMakeLabel(v);
81942 
81943     /* Unless this is a view, open cursors for the table we are
81944     ** deleting from and all its indices. If this is a view, then the
81945     ** only effect this statement has is to fire the INSTEAD OF
81946     ** triggers.  */
81947     if( !isView ){
81948       sqlite3OpenTableAndIndices(pParse, pTab, iCur, OP_OpenWrite);
81949     }
81950 
81951     addr = sqlite3VdbeAddOp3(v, OP_RowSetRead, iRowSet, end, iRowid);
81952 
81953     /* Delete the row */
81954 #ifndef SQLITE_OMIT_VIRTUALTABLE
81955     if( IsVirtual(pTab) ){
81956       const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
81957       sqlite3VtabMakeWritable(pParse, pTab);
81958       sqlite3VdbeAddOp4(v, OP_VUpdate, 0, 1, iRowid, pVTab, P4_VTAB);
81959       sqlite3VdbeChangeP5(v, OE_Abort);
81960       sqlite3MayAbort(pParse);
81961     }else
81962 #endif
81963     {
81964       int count = (pParse->nested==0);    /* True to count changes */
81965       sqlite3GenerateRowDelete(pParse, pTab, iCur, iRowid, count, pTrigger, OE_Default);
81966     }
81967 
81968     /* End of the delete loop */
81969     sqlite3VdbeAddOp2(v, OP_Goto, 0, addr);
81970     sqlite3VdbeResolveLabel(v, end);
81971 
81972     /* Close the cursors open on the table and its indexes. */
81973     if( !isView && !IsVirtual(pTab) ){
81974       for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
81975         sqlite3VdbeAddOp2(v, OP_Close, iCur + i, pIdx->tnum);
81976       }
81977       sqlite3VdbeAddOp1(v, OP_Close, iCur);
81978     }
81979   }
81980 
81981   /* Update the sqlite_sequence table by storing the content of the
81982   ** maximum rowid counter values recorded while inserting into
81983   ** autoincrement tables.
81984   */
81985   if( pParse->nested==0 && pParse->pTriggerTab==0 ){
81986     sqlite3AutoincrementEnd(pParse);
81987   }
81988 
81989   /* Return the number of rows that were deleted. If this routine is
81990   ** generating code because of a call to sqlite3NestedParse(), do not
81991   ** invoke the callback function.
81992   */
81993   if( (db->flags&SQLITE_CountRows) && !pParse->nested && !pParse->pTriggerTab ){
81994     sqlite3VdbeAddOp2(v, OP_ResultRow, memCnt, 1);
81995     sqlite3VdbeSetNumCols(v, 1);
81996     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows deleted", SQLITE_STATIC);
81997   }
81998 
81999 delete_from_cleanup:
82000   sqlite3AuthContextPop(&sContext);
82001   sqlite3SrcListDelete(db, pTabList);
82002   sqlite3ExprDelete(db, pWhere);
82003   return;
82004 }
82005 /* Make sure "isView" and other macros defined above are undefined. Otherwise
82006 ** thely may interfere with compilation of other functions in this file
82007 ** (or in another file, if this file becomes part of the amalgamation).  */
82008 #ifdef isView
82009  #undef isView
82010 #endif
82011 #ifdef pTrigger
82012  #undef pTrigger
82013 #endif
82014 
82015 /*
82016 ** This routine generates VDBE code that causes a single row of a
82017 ** single table to be deleted.
82018 **
82019 ** The VDBE must be in a particular state when this routine is called.
82020 ** These are the requirements:
82021 **
82022 **   1.  A read/write cursor pointing to pTab, the table containing the row
82023 **       to be deleted, must be opened as cursor number $iCur.
82024 **
82025 **   2.  Read/write cursors for all indices of pTab must be open as
82026 **       cursor number base+i for the i-th index.
82027 **
82028 **   3.  The record number of the row to be deleted must be stored in
82029 **       memory cell iRowid.
82030 **
82031 ** This routine generates code to remove both the table record and all
82032 ** index entries that point to that record.
82033 */
82034 SQLITE_PRIVATE void sqlite3GenerateRowDelete(
82035   Parse *pParse,     /* Parsing context */
82036   Table *pTab,       /* Table containing the row to be deleted */
82037   int iCur,          /* Cursor number for the table */
82038   int iRowid,        /* Memory cell that contains the rowid to delete */
82039   int count,         /* If non-zero, increment the row change counter */
82040   Trigger *pTrigger, /* List of triggers to (potentially) fire */
82041   int onconf         /* Default ON CONFLICT policy for triggers */
82042 ){
82043   Vdbe *v = pParse->pVdbe;        /* Vdbe */
82044   int iOld = 0;                   /* First register in OLD.* array */
82045   int iLabel;                     /* Label resolved to end of generated code */
82046 
82047   /* Vdbe is guaranteed to have been allocated by this stage. */
82048   assert( v );
82049 
82050   /* Seek cursor iCur to the row to delete. If this row no longer exists
82051   ** (this can happen if a trigger program has already deleted it), do
82052   ** not attempt to delete it or fire any DELETE triggers.  */
82053   iLabel = sqlite3VdbeMakeLabel(v);
82054   sqlite3VdbeAddOp3(v, OP_NotExists, iCur, iLabel, iRowid);
82055 
82056   /* If there are any triggers to fire, allocate a range of registers to
82057   ** use for the old.* references in the triggers.  */
82058   if( sqlite3FkRequired(pParse, pTab, 0, 0) || pTrigger ){
82059     u32 mask;                     /* Mask of OLD.* columns in use */
82060     int iCol;                     /* Iterator used while populating OLD.* */
82061 
82062     /* TODO: Could use temporary registers here. Also could attempt to
82063     ** avoid copying the contents of the rowid register.  */
82064     mask = sqlite3TriggerColmask(
82065         pParse, pTrigger, 0, 0, TRIGGER_BEFORE|TRIGGER_AFTER, pTab, onconf
82066     );
82067     mask |= sqlite3FkOldmask(pParse, pTab);
82068     iOld = pParse->nMem+1;
82069     pParse->nMem += (1 + pTab->nCol);
82070 
82071     /* Populate the OLD.* pseudo-table register array. These values will be
82072     ** used by any BEFORE and AFTER triggers that exist.  */
82073     sqlite3VdbeAddOp2(v, OP_Copy, iRowid, iOld);
82074     for(iCol=0; iCol<pTab->nCol; iCol++){
82075       if( mask==0xffffffff || mask&(1<<iCol) ){
82076         sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, iCol, iOld+iCol+1);
82077       }
82078     }
82079 
82080     /* Invoke BEFORE DELETE trigger programs. */
82081     sqlite3CodeRowTrigger(pParse, pTrigger,
82082         TK_DELETE, 0, TRIGGER_BEFORE, pTab, iOld, onconf, iLabel
82083     );
82084 
82085     /* Seek the cursor to the row to be deleted again. It may be that
82086     ** the BEFORE triggers coded above have already removed the row
82087     ** being deleted. Do not attempt to delete the row a second time, and
82088     ** do not fire AFTER triggers.  */
82089     sqlite3VdbeAddOp3(v, OP_NotExists, iCur, iLabel, iRowid);
82090 
82091     /* Do FK processing. This call checks that any FK constraints that
82092     ** refer to this table (i.e. constraints attached to other tables)
82093     ** are not violated by deleting this row.  */
82094     sqlite3FkCheck(pParse, pTab, iOld, 0);
82095   }
82096 
82097   /* Delete the index and table entries. Skip this step if pTab is really
82098   ** a view (in which case the only effect of the DELETE statement is to
82099   ** fire the INSTEAD OF triggers).  */
82100   if( pTab->pSelect==0 ){
82101     sqlite3GenerateRowIndexDelete(pParse, pTab, iCur, 0);
82102     sqlite3VdbeAddOp2(v, OP_Delete, iCur, (count?OPFLAG_NCHANGE:0));
82103     if( count ){
82104       sqlite3VdbeChangeP4(v, -1, pTab->zName, P4_TRANSIENT);
82105     }
82106   }
82107 
82108   /* Do any ON CASCADE, SET NULL or SET DEFAULT operations required to
82109   ** handle rows (possibly in other tables) that refer via a foreign key
82110   ** to the row just deleted. */
82111   sqlite3FkActions(pParse, pTab, 0, iOld);
82112 
82113   /* Invoke AFTER DELETE trigger programs. */
82114   sqlite3CodeRowTrigger(pParse, pTrigger,
82115       TK_DELETE, 0, TRIGGER_AFTER, pTab, iOld, onconf, iLabel
82116   );
82117 
82118   /* Jump here if the row had already been deleted before any BEFORE
82119   ** trigger programs were invoked. Or if a trigger program throws a
82120   ** RAISE(IGNORE) exception.  */
82121   sqlite3VdbeResolveLabel(v, iLabel);
82122 }
82123 
82124 /*
82125 ** This routine generates VDBE code that causes the deletion of all
82126 ** index entries associated with a single row of a single table.
82127 **
82128 ** The VDBE must be in a particular state when this routine is called.
82129 ** These are the requirements:
82130 **
82131 **   1.  A read/write cursor pointing to pTab, the table containing the row
82132 **       to be deleted, must be opened as cursor number "iCur".
82133 **
82134 **   2.  Read/write cursors for all indices of pTab must be open as
82135 **       cursor number iCur+i for the i-th index.
82136 **
82137 **   3.  The "iCur" cursor must be pointing to the row that is to be
82138 **       deleted.
82139 */
82140 SQLITE_PRIVATE void sqlite3GenerateRowIndexDelete(
82141   Parse *pParse,     /* Parsing and code generating context */
82142   Table *pTab,       /* Table containing the row to be deleted */
82143   int iCur,          /* Cursor number for the table */
82144   int *aRegIdx       /* Only delete if aRegIdx!=0 && aRegIdx[i]>0 */
82145 ){
82146   int i;
82147   Index *pIdx;
82148   int r1;
82149 
82150   for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
82151     if( aRegIdx!=0 && aRegIdx[i-1]==0 ) continue;
82152     r1 = sqlite3GenerateIndexKey(pParse, pIdx, iCur, 0, 0);
82153     sqlite3VdbeAddOp3(pParse->pVdbe, OP_IdxDelete, iCur+i, r1,pIdx->nColumn+1);
82154   }
82155 }
82156 
82157 /*
82158 ** Generate code that will assemble an index key and put it in register
82159 ** regOut.  The key with be for index pIdx which is an index on pTab.
82160 ** iCur is the index of a cursor open on the pTab table and pointing to
82161 ** the entry that needs indexing.
82162 **
82163 ** Return a register number which is the first in a block of
82164 ** registers that holds the elements of the index key.  The
82165 ** block of registers has already been deallocated by the time
82166 ** this routine returns.
82167 */
82168 SQLITE_PRIVATE int sqlite3GenerateIndexKey(
82169   Parse *pParse,     /* Parsing context */
82170   Index *pIdx,       /* The index for which to generate a key */
82171   int iCur,          /* Cursor number for the pIdx->pTable table */
82172   int regOut,        /* Write the new index key to this register */
82173   int doMakeRec      /* Run the OP_MakeRecord instruction if true */
82174 ){
82175   Vdbe *v = pParse->pVdbe;
82176   int j;
82177   Table *pTab = pIdx->pTable;
82178   int regBase;
82179   int nCol;
82180 
82181   nCol = pIdx->nColumn;
82182   regBase = sqlite3GetTempRange(pParse, nCol+1);
82183   sqlite3VdbeAddOp2(v, OP_Rowid, iCur, regBase+nCol);
82184   for(j=0; j<nCol; j++){
82185     int idx = pIdx->aiColumn[j];
82186     if( idx==pTab->iPKey ){
82187       sqlite3VdbeAddOp2(v, OP_SCopy, regBase+nCol, regBase+j);
82188     }else{
82189       sqlite3VdbeAddOp3(v, OP_Column, iCur, idx, regBase+j);
82190       sqlite3ColumnDefault(v, pTab, idx, -1);
82191     }
82192   }
82193   if( doMakeRec ){
82194     const char *zAff;
82195     if( pTab->pSelect || (pParse->db->flags & SQLITE_IdxRealAsInt)!=0 ){
82196       zAff = 0;
82197     }else{
82198       zAff = sqlite3IndexAffinityStr(v, pIdx);
82199     }
82200     sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol+1, regOut);
82201     sqlite3VdbeChangeP4(v, -1, zAff, P4_TRANSIENT);
82202   }
82203   sqlite3ReleaseTempRange(pParse, regBase, nCol+1);
82204   return regBase;
82205 }
82206 
82207 /************** End of delete.c **********************************************/
82208 /************** Begin file func.c ********************************************/
82209 /*
82210 ** 2002 February 23
82211 **
82212 ** The author disclaims copyright to this source code.  In place of
82213 ** a legal notice, here is a blessing:
82214 **
82215 **    May you do good and not evil.
82216 **    May you find forgiveness for yourself and forgive others.
82217 **    May you share freely, never taking more than you give.
82218 **
82219 *************************************************************************
82220 ** This file contains the C functions that implement various SQL
82221 ** functions of SQLite.
82222 **
82223 ** There is only one exported symbol in this file - the function
82224 ** sqliteRegisterBuildinFunctions() found at the bottom of the file.
82225 ** All other code has file scope.
82226 */
82227 
82228 /*
82229 ** Return the collating function associated with a function.
82230 */
82231 static CollSeq *sqlite3GetFuncCollSeq(sqlite3_context *context){
82232   return context->pColl;
82233 }
82234 
82235 /*
82236 ** Implementation of the non-aggregate min() and max() functions
82237 */
82238 static void minmaxFunc(
82239   sqlite3_context *context,
82240   int argc,
82241   sqlite3_value **argv
82242 ){
82243   int i;
82244   int mask;    /* 0 for min() or 0xffffffff for max() */
82245   int iBest;
82246   CollSeq *pColl;
82247 
82248   assert( argc>1 );
82249   mask = sqlite3_user_data(context)==0 ? 0 : -1;
82250   pColl = sqlite3GetFuncCollSeq(context);
82251   assert( pColl );
82252   assert( mask==-1 || mask==0 );
82253   iBest = 0;
82254   if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
82255   for(i=1; i<argc; i++){
82256     if( sqlite3_value_type(argv[i])==SQLITE_NULL ) return;
82257     if( (sqlite3MemCompare(argv[iBest], argv[i], pColl)^mask)>=0 ){
82258       testcase( mask==0 );
82259       iBest = i;
82260     }
82261   }
82262   sqlite3_result_value(context, argv[iBest]);
82263 }
82264 
82265 /*
82266 ** Return the type of the argument.
82267 */
82268 static void typeofFunc(
82269   sqlite3_context *context,
82270   int NotUsed,
82271   sqlite3_value **argv
82272 ){
82273   const char *z = 0;
82274   UNUSED_PARAMETER(NotUsed);
82275   switch( sqlite3_value_type(argv[0]) ){
82276     case SQLITE_INTEGER: z = "integer"; break;
82277     case SQLITE_TEXT:    z = "text";    break;
82278     case SQLITE_FLOAT:   z = "real";    break;
82279     case SQLITE_BLOB:    z = "blob";    break;
82280     default:             z = "null";    break;
82281   }
82282   sqlite3_result_text(context, z, -1, SQLITE_STATIC);
82283 }
82284 
82285 
82286 /*
82287 ** Implementation of the length() function
82288 */
82289 static void lengthFunc(
82290   sqlite3_context *context,
82291   int argc,
82292   sqlite3_value **argv
82293 ){
82294   int len;
82295 
82296   assert( argc==1 );
82297   UNUSED_PARAMETER(argc);
82298   switch( sqlite3_value_type(argv[0]) ){
82299     case SQLITE_BLOB:
82300     case SQLITE_INTEGER:
82301     case SQLITE_FLOAT: {
82302       sqlite3_result_int(context, sqlite3_value_bytes(argv[0]));
82303       break;
82304     }
82305     case SQLITE_TEXT: {
82306       const unsigned char *z = sqlite3_value_text(argv[0]);
82307       if( z==0 ) return;
82308       len = 0;
82309       while( *z ){
82310         len++;
82311         SQLITE_SKIP_UTF8(z);
82312       }
82313       sqlite3_result_int(context, len);
82314       break;
82315     }
82316     default: {
82317       sqlite3_result_null(context);
82318       break;
82319     }
82320   }
82321 }
82322 
82323 /*
82324 ** Implementation of the abs() function.
82325 **
82326 ** IMP: R-23979-26855 The abs(X) function returns the absolute value of
82327 ** the numeric argument X.
82328 */
82329 static void absFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
82330   assert( argc==1 );
82331   UNUSED_PARAMETER(argc);
82332   switch( sqlite3_value_type(argv[0]) ){
82333     case SQLITE_INTEGER: {
82334       i64 iVal = sqlite3_value_int64(argv[0]);
82335       if( iVal<0 ){
82336         if( (iVal<<1)==0 ){
82337           /* IMP: R-35460-15084 If X is the integer -9223372036854775807 then
82338           ** abs(X) throws an integer overflow error since there is no
82339           ** equivalent positive 64-bit two complement value. */
82340           sqlite3_result_error(context, "integer overflow", -1);
82341           return;
82342         }
82343         iVal = -iVal;
82344       }
82345       sqlite3_result_int64(context, iVal);
82346       break;
82347     }
82348     case SQLITE_NULL: {
82349       /* IMP: R-37434-19929 Abs(X) returns NULL if X is NULL. */
82350       sqlite3_result_null(context);
82351       break;
82352     }
82353     default: {
82354       /* Because sqlite3_value_double() returns 0.0 if the argument is not
82355       ** something that can be converted into a number, we have:
82356       ** IMP: R-57326-31541 Abs(X) return 0.0 if X is a string or blob that
82357       ** cannot be converted to a numeric value.
82358       */
82359       double rVal = sqlite3_value_double(argv[0]);
82360       if( rVal<0 ) rVal = -rVal;
82361       sqlite3_result_double(context, rVal);
82362       break;
82363     }
82364   }
82365 }
82366 
82367 /*
82368 ** Implementation of the substr() function.
82369 **
82370 ** substr(x,p1,p2)  returns p2 characters of x[] beginning with p1.
82371 ** p1 is 1-indexed.  So substr(x,1,1) returns the first character
82372 ** of x.  If x is text, then we actually count UTF-8 characters.
82373 ** If x is a blob, then we count bytes.
82374 **
82375 ** If p1 is negative, then we begin abs(p1) from the end of x[].
82376 **
82377 ** If p2 is negative, return the p2 characters preceeding p1.
82378 */
82379 static void substrFunc(
82380   sqlite3_context *context,
82381   int argc,
82382   sqlite3_value **argv
82383 ){
82384   const unsigned char *z;
82385   const unsigned char *z2;
82386   int len;
82387   int p0type;
82388   i64 p1, p2;
82389   int negP2 = 0;
82390 
82391   assert( argc==3 || argc==2 );
82392   if( sqlite3_value_type(argv[1])==SQLITE_NULL
82393    || (argc==3 && sqlite3_value_type(argv[2])==SQLITE_NULL)
82394   ){
82395     return;
82396   }
82397   p0type = sqlite3_value_type(argv[0]);
82398   p1 = sqlite3_value_int(argv[1]);
82399   if( p0type==SQLITE_BLOB ){
82400     len = sqlite3_value_bytes(argv[0]);
82401     z = sqlite3_value_blob(argv[0]);
82402     if( z==0 ) return;
82403     assert( len==sqlite3_value_bytes(argv[0]) );
82404   }else{
82405     z = sqlite3_value_text(argv[0]);
82406     if( z==0 ) return;
82407     len = 0;
82408     if( p1<0 ){
82409       for(z2=z; *z2; len++){
82410         SQLITE_SKIP_UTF8(z2);
82411       }
82412     }
82413   }
82414   if( argc==3 ){
82415     p2 = sqlite3_value_int(argv[2]);
82416     if( p2<0 ){
82417       p2 = -p2;
82418       negP2 = 1;
82419     }
82420   }else{
82421     p2 = sqlite3_context_db_handle(context)->aLimit[SQLITE_LIMIT_LENGTH];
82422   }
82423   if( p1<0 ){
82424     p1 += len;
82425     if( p1<0 ){
82426       p2 += p1;
82427       if( p2<0 ) p2 = 0;
82428       p1 = 0;
82429     }
82430   }else if( p1>0 ){
82431     p1--;
82432   }else if( p2>0 ){
82433     p2--;
82434   }
82435   if( negP2 ){
82436     p1 -= p2;
82437     if( p1<0 ){
82438       p2 += p1;
82439       p1 = 0;
82440     }
82441   }
82442   assert( p1>=0 && p2>=0 );
82443   if( p0type!=SQLITE_BLOB ){
82444     while( *z && p1 ){
82445       SQLITE_SKIP_UTF8(z);
82446       p1--;
82447     }
82448     for(z2=z; *z2 && p2; p2--){
82449       SQLITE_SKIP_UTF8(z2);
82450     }
82451     sqlite3_result_text(context, (char*)z, (int)(z2-z), SQLITE_TRANSIENT);
82452   }else{
82453     if( p1+p2>len ){
82454       p2 = len-p1;
82455       if( p2<0 ) p2 = 0;
82456     }
82457     sqlite3_result_blob(context, (char*)&z[p1], (int)p2, SQLITE_TRANSIENT);
82458   }
82459 }
82460 
82461 /*
82462 ** Implementation of the round() function
82463 */
82464 #ifndef SQLITE_OMIT_FLOATING_POINT
82465 static void roundFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
82466   int n = 0;
82467   double r;
82468   char *zBuf;
82469   assert( argc==1 || argc==2 );
82470   if( argc==2 ){
82471     if( SQLITE_NULL==sqlite3_value_type(argv[1]) ) return;
82472     n = sqlite3_value_int(argv[1]);
82473     if( n>30 ) n = 30;
82474     if( n<0 ) n = 0;
82475   }
82476   if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
82477   r = sqlite3_value_double(argv[0]);
82478   /* If Y==0 and X will fit in a 64-bit int,
82479   ** handle the rounding directly,
82480   ** otherwise use printf.
82481   */
82482   if( n==0 && r>=0 && r<LARGEST_INT64-1 ){
82483     r = (double)((sqlite_int64)(r+0.5));
82484   }else if( n==0 && r<0 && (-r)<LARGEST_INT64-1 ){
82485     r = -(double)((sqlite_int64)((-r)+0.5));
82486   }else{
82487     zBuf = sqlite3_mprintf("%.*f",n,r);
82488     if( zBuf==0 ){
82489       sqlite3_result_error_nomem(context);
82490       return;
82491     }
82492     sqlite3AtoF(zBuf, &r, sqlite3Strlen30(zBuf), SQLITE_UTF8);
82493     sqlite3_free(zBuf);
82494   }
82495   sqlite3_result_double(context, r);
82496 }
82497 #endif
82498 
82499 /*
82500 ** Allocate nByte bytes of space using sqlite3_malloc(). If the
82501 ** allocation fails, call sqlite3_result_error_nomem() to notify
82502 ** the database handle that malloc() has failed and return NULL.
82503 ** If nByte is larger than the maximum string or blob length, then
82504 ** raise an SQLITE_TOOBIG exception and return NULL.
82505 */
82506 static void *contextMalloc(sqlite3_context *context, i64 nByte){
82507   char *z;
82508   sqlite3 *db = sqlite3_context_db_handle(context);
82509   assert( nByte>0 );
82510   testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH] );
82511   testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH]+1 );
82512   if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
82513     sqlite3_result_error_toobig(context);
82514     z = 0;
82515   }else{
82516     z = sqlite3Malloc((int)nByte);
82517     if( !z ){
82518       sqlite3_result_error_nomem(context);
82519     }
82520   }
82521   return z;
82522 }
82523 
82524 /*
82525 ** Implementation of the upper() and lower() SQL functions.
82526 */
82527 static void upperFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
82528   char *z1;
82529   const char *z2;
82530   int i, n;
82531   UNUSED_PARAMETER(argc);
82532   z2 = (char*)sqlite3_value_text(argv[0]);
82533   n = sqlite3_value_bytes(argv[0]);
82534   /* Verify that the call to _bytes() does not invalidate the _text() pointer */
82535   assert( z2==(char*)sqlite3_value_text(argv[0]) );
82536   if( z2 ){
82537     z1 = contextMalloc(context, ((i64)n)+1);
82538     if( z1 ){
82539       memcpy(z1, z2, n+1);
82540       for(i=0; z1[i]; i++){
82541         z1[i] = (char)sqlite3Toupper(z1[i]);
82542       }
82543       sqlite3_result_text(context, z1, -1, sqlite3_free);
82544     }
82545   }
82546 }
82547 static void lowerFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
82548   u8 *z1;
82549   const char *z2;
82550   int i, n;
82551   UNUSED_PARAMETER(argc);
82552   z2 = (char*)sqlite3_value_text(argv[0]);
82553   n = sqlite3_value_bytes(argv[0]);
82554   /* Verify that the call to _bytes() does not invalidate the _text() pointer */
82555   assert( z2==(char*)sqlite3_value_text(argv[0]) );
82556   if( z2 ){
82557     z1 = contextMalloc(context, ((i64)n)+1);
82558     if( z1 ){
82559       memcpy(z1, z2, n+1);
82560       for(i=0; z1[i]; i++){
82561         z1[i] = sqlite3Tolower(z1[i]);
82562       }
82563       sqlite3_result_text(context, (char *)z1, -1, sqlite3_free);
82564     }
82565   }
82566 }
82567 
82568 
82569 #if 0  /* This function is never used. */
82570 /*
82571 ** The COALESCE() and IFNULL() functions used to be implemented as shown
82572 ** here.  But now they are implemented as VDBE code so that unused arguments
82573 ** do not have to be computed.  This legacy implementation is retained as
82574 ** comment.
82575 */
82576 /*
82577 ** Implementation of the IFNULL(), NVL(), and COALESCE() functions.
82578 ** All three do the same thing.  They return the first non-NULL
82579 ** argument.
82580 */
82581 static void ifnullFunc(
82582   sqlite3_context *context,
82583   int argc,
82584   sqlite3_value **argv
82585 ){
82586   int i;
82587   for(i=0; i<argc; i++){
82588     if( SQLITE_NULL!=sqlite3_value_type(argv[i]) ){
82589       sqlite3_result_value(context, argv[i]);
82590       break;
82591     }
82592   }
82593 }
82594 #endif /* NOT USED */
82595 #define ifnullFunc versionFunc   /* Substitute function - never called */
82596 
82597 /*
82598 ** Implementation of random().  Return a random integer.
82599 */
82600 static void randomFunc(
82601   sqlite3_context *context,
82602   int NotUsed,
82603   sqlite3_value **NotUsed2
82604 ){
82605   sqlite_int64 r;
82606   UNUSED_PARAMETER2(NotUsed, NotUsed2);
82607   sqlite3_randomness(sizeof(r), &r);
82608   if( r<0 ){
82609     /* We need to prevent a random number of 0x8000000000000000
82610     ** (or -9223372036854775808) since when you do abs() of that
82611     ** number of you get the same value back again.  To do this
82612     ** in a way that is testable, mask the sign bit off of negative
82613     ** values, resulting in a positive value.  Then take the
82614     ** 2s complement of that positive value.  The end result can
82615     ** therefore be no less than -9223372036854775807.
82616     */
82617     r = -(r ^ (((sqlite3_int64)1)<<63));
82618   }
82619   sqlite3_result_int64(context, r);
82620 }
82621 
82622 /*
82623 ** Implementation of randomblob(N).  Return a random blob
82624 ** that is N bytes long.
82625 */
82626 static void randomBlob(
82627   sqlite3_context *context,
82628   int argc,
82629   sqlite3_value **argv
82630 ){
82631   int n;
82632   unsigned char *p;
82633   assert( argc==1 );
82634   UNUSED_PARAMETER(argc);
82635   n = sqlite3_value_int(argv[0]);
82636   if( n<1 ){
82637     n = 1;
82638   }
82639   p = contextMalloc(context, n);
82640   if( p ){
82641     sqlite3_randomness(n, p);
82642     sqlite3_result_blob(context, (char*)p, n, sqlite3_free);
82643   }
82644 }
82645 
82646 /*
82647 ** Implementation of the last_insert_rowid() SQL function.  The return
82648 ** value is the same as the sqlite3_last_insert_rowid() API function.
82649 */
82650 static void last_insert_rowid(
82651   sqlite3_context *context,
82652   int NotUsed,
82653   sqlite3_value **NotUsed2
82654 ){
82655   sqlite3 *db = sqlite3_context_db_handle(context);
82656   UNUSED_PARAMETER2(NotUsed, NotUsed2);
82657   /* IMP: R-51513-12026 The last_insert_rowid() SQL function is a
82658   ** wrapper around the sqlite3_last_insert_rowid() C/C++ interface
82659   ** function. */
82660   sqlite3_result_int64(context, sqlite3_last_insert_rowid(db));
82661 }
82662 
82663 /*
82664 ** Implementation of the changes() SQL function.
82665 **
82666 ** IMP: R-62073-11209 The changes() SQL function is a wrapper
82667 ** around the sqlite3_changes() C/C++ function and hence follows the same
82668 ** rules for counting changes.
82669 */
82670 static void changes(
82671   sqlite3_context *context,
82672   int NotUsed,
82673   sqlite3_value **NotUsed2
82674 ){
82675   sqlite3 *db = sqlite3_context_db_handle(context);
82676   UNUSED_PARAMETER2(NotUsed, NotUsed2);
82677   sqlite3_result_int(context, sqlite3_changes(db));
82678 }
82679 
82680 /*
82681 ** Implementation of the total_changes() SQL function.  The return value is
82682 ** the same as the sqlite3_total_changes() API function.
82683 */
82684 static void total_changes(
82685   sqlite3_context *context,
82686   int NotUsed,
82687   sqlite3_value **NotUsed2
82688 ){
82689   sqlite3 *db = sqlite3_context_db_handle(context);
82690   UNUSED_PARAMETER2(NotUsed, NotUsed2);
82691   /* IMP: R-52756-41993 This function is a wrapper around the
82692   ** sqlite3_total_changes() C/C++ interface. */
82693   sqlite3_result_int(context, sqlite3_total_changes(db));
82694 }
82695 
82696 /*
82697 ** A structure defining how to do GLOB-style comparisons.
82698 */
82699 struct compareInfo {
82700   u8 matchAll;
82701   u8 matchOne;
82702   u8 matchSet;
82703   u8 noCase;
82704 };
82705 
82706 /*
82707 ** For LIKE and GLOB matching on EBCDIC machines, assume that every
82708 ** character is exactly one byte in size.  Also, all characters are
82709 ** able to participate in upper-case-to-lower-case mappings in EBCDIC
82710 ** whereas only characters less than 0x80 do in ASCII.
82711 */
82712 #if defined(SQLITE_EBCDIC)
82713 # define sqlite3Utf8Read(A,C)  (*(A++))
82714 # define GlogUpperToLower(A)   A = sqlite3UpperToLower[A]
82715 #else
82716 # define GlogUpperToLower(A)   if( !((A)&~0x7f) ){ A = sqlite3UpperToLower[A]; }
82717 #endif
82718 
82719 static const struct compareInfo globInfo = { '*', '?', '[', 0 };
82720 /* The correct SQL-92 behavior is for the LIKE operator to ignore
82721 ** case.  Thus  'a' LIKE 'A' would be true. */
82722 static const struct compareInfo likeInfoNorm = { '%', '_',   0, 1 };
82723 /* If SQLITE_CASE_SENSITIVE_LIKE is defined, then the LIKE operator
82724 ** is case sensitive causing 'a' LIKE 'A' to be false */
82725 static const struct compareInfo likeInfoAlt = { '%', '_',   0, 0 };
82726 
82727 /*
82728 ** Compare two UTF-8 strings for equality where the first string can
82729 ** potentially be a "glob" expression.  Return true (1) if they
82730 ** are the same and false (0) if they are different.
82731 **
82732 ** Globbing rules:
82733 **
82734 **      '*'       Matches any sequence of zero or more characters.
82735 **
82736 **      '?'       Matches exactly one character.
82737 **
82738 **     [...]      Matches one character from the enclosed list of
82739 **                characters.
82740 **
82741 **     [^...]     Matches one character not in the enclosed list.
82742 **
82743 ** With the [...] and [^...] matching, a ']' character can be included
82744 ** in the list by making it the first character after '[' or '^'.  A
82745 ** range of characters can be specified using '-'.  Example:
82746 ** "[a-z]" matches any single lower-case letter.  To match a '-', make
82747 ** it the last character in the list.
82748 **
82749 ** This routine is usually quick, but can be N**2 in the worst case.
82750 **
82751 ** Hints: to match '*' or '?', put them in "[]".  Like this:
82752 **
82753 **         abc[*]xyz        Matches "abc*xyz" only
82754 */
82755 static int patternCompare(
82756   const u8 *zPattern,              /* The glob pattern */
82757   const u8 *zString,               /* The string to compare against the glob */
82758   const struct compareInfo *pInfo, /* Information about how to do the compare */
82759   u32 esc                          /* The escape character */
82760 ){
82761   u32 c, c2;
82762   int invert;
82763   int seen;
82764   u8 matchOne = pInfo->matchOne;
82765   u8 matchAll = pInfo->matchAll;
82766   u8 matchSet = pInfo->matchSet;
82767   u8 noCase = pInfo->noCase;
82768   int prevEscape = 0;     /* True if the previous character was 'escape' */
82769 
82770   while( (c = sqlite3Utf8Read(zPattern,&zPattern))!=0 ){
82771     if( !prevEscape && c==matchAll ){
82772       while( (c=sqlite3Utf8Read(zPattern,&zPattern)) == matchAll
82773                || c == matchOne ){
82774         if( c==matchOne && sqlite3Utf8Read(zString, &zString)==0 ){
82775           return 0;
82776         }
82777       }
82778       if( c==0 ){
82779         return 1;
82780       }else if( c==esc ){
82781         c = sqlite3Utf8Read(zPattern, &zPattern);
82782         if( c==0 ){
82783           return 0;
82784         }
82785       }else if( c==matchSet ){
82786         assert( esc==0 );         /* This is GLOB, not LIKE */
82787         assert( matchSet<0x80 );  /* '[' is a single-byte character */
82788         while( *zString && patternCompare(&zPattern[-1],zString,pInfo,esc)==0 ){
82789           SQLITE_SKIP_UTF8(zString);
82790         }
82791         return *zString!=0;
82792       }
82793       while( (c2 = sqlite3Utf8Read(zString,&zString))!=0 ){
82794         if( noCase ){
82795           GlogUpperToLower(c2);
82796           GlogUpperToLower(c);
82797           while( c2 != 0 && c2 != c ){
82798             c2 = sqlite3Utf8Read(zString, &zString);
82799             GlogUpperToLower(c2);
82800           }
82801         }else{
82802           while( c2 != 0 && c2 != c ){
82803             c2 = sqlite3Utf8Read(zString, &zString);
82804           }
82805         }
82806         if( c2==0 ) return 0;
82807         if( patternCompare(zPattern,zString,pInfo,esc) ) return 1;
82808       }
82809       return 0;
82810     }else if( !prevEscape && c==matchOne ){
82811       if( sqlite3Utf8Read(zString, &zString)==0 ){
82812         return 0;
82813       }
82814     }else if( c==matchSet ){
82815       u32 prior_c = 0;
82816       assert( esc==0 );    /* This only occurs for GLOB, not LIKE */
82817       seen = 0;
82818       invert = 0;
82819       c = sqlite3Utf8Read(zString, &zString);
82820       if( c==0 ) return 0;
82821       c2 = sqlite3Utf8Read(zPattern, &zPattern);
82822       if( c2=='^' ){
82823         invert = 1;
82824         c2 = sqlite3Utf8Read(zPattern, &zPattern);
82825       }
82826       if( c2==']' ){
82827         if( c==']' ) seen = 1;
82828         c2 = sqlite3Utf8Read(zPattern, &zPattern);
82829       }
82830       while( c2 && c2!=']' ){
82831         if( c2=='-' && zPattern[0]!=']' && zPattern[0]!=0 && prior_c>0 ){
82832           c2 = sqlite3Utf8Read(zPattern, &zPattern);
82833           if( c>=prior_c && c<=c2 ) seen = 1;
82834           prior_c = 0;
82835         }else{
82836           if( c==c2 ){
82837             seen = 1;
82838           }
82839           prior_c = c2;
82840         }
82841         c2 = sqlite3Utf8Read(zPattern, &zPattern);
82842       }
82843       if( c2==0 || (seen ^ invert)==0 ){
82844         return 0;
82845       }
82846     }else if( esc==c && !prevEscape ){
82847       prevEscape = 1;
82848     }else{
82849       c2 = sqlite3Utf8Read(zString, &zString);
82850       if( noCase ){
82851         GlogUpperToLower(c);
82852         GlogUpperToLower(c2);
82853       }
82854       if( c!=c2 ){
82855         return 0;
82856       }
82857       prevEscape = 0;
82858     }
82859   }
82860   return *zString==0;
82861 }
82862 
82863 /*
82864 ** Count the number of times that the LIKE operator (or GLOB which is
82865 ** just a variation of LIKE) gets called.  This is used for testing
82866 ** only.
82867 */
82868 #ifdef SQLITE_TEST
82869 SQLITE_API int sqlite3_like_count = 0;
82870 #endif
82871 
82872 
82873 /*
82874 ** Implementation of the like() SQL function.  This function implements
82875 ** the build-in LIKE operator.  The first argument to the function is the
82876 ** pattern and the second argument is the string.  So, the SQL statements:
82877 **
82878 **       A LIKE B
82879 **
82880 ** is implemented as like(B,A).
82881 **
82882 ** This same function (with a different compareInfo structure) computes
82883 ** the GLOB operator.
82884 */
82885 static void likeFunc(
82886   sqlite3_context *context,
82887   int argc,
82888   sqlite3_value **argv
82889 ){
82890   const unsigned char *zA, *zB;
82891   u32 escape = 0;
82892   int nPat;
82893   sqlite3 *db = sqlite3_context_db_handle(context);
82894 
82895   zB = sqlite3_value_text(argv[0]);
82896   zA = sqlite3_value_text(argv[1]);
82897 
82898   /* Limit the length of the LIKE or GLOB pattern to avoid problems
82899   ** of deep recursion and N*N behavior in patternCompare().
82900   */
82901   nPat = sqlite3_value_bytes(argv[0]);
82902   testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] );
82903   testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]+1 );
82904   if( nPat > db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] ){
82905     sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1);
82906     return;
82907   }
82908   assert( zB==sqlite3_value_text(argv[0]) );  /* Encoding did not change */
82909 
82910   if( argc==3 ){
82911     /* The escape character string must consist of a single UTF-8 character.
82912     ** Otherwise, return an error.
82913     */
82914     const unsigned char *zEsc = sqlite3_value_text(argv[2]);
82915     if( zEsc==0 ) return;
82916     if( sqlite3Utf8CharLen((char*)zEsc, -1)!=1 ){
82917       sqlite3_result_error(context,
82918           "ESCAPE expression must be a single character", -1);
82919       return;
82920     }
82921     escape = sqlite3Utf8Read(zEsc, &zEsc);
82922   }
82923   if( zA && zB ){
82924     struct compareInfo *pInfo = sqlite3_user_data(context);
82925 #ifdef SQLITE_TEST
82926     sqlite3_like_count++;
82927 #endif
82928 
82929     sqlite3_result_int(context, patternCompare(zB, zA, pInfo, escape));
82930   }
82931 }
82932 
82933 /*
82934 ** Implementation of the NULLIF(x,y) function.  The result is the first
82935 ** argument if the arguments are different.  The result is NULL if the
82936 ** arguments are equal to each other.
82937 */
82938 static void nullifFunc(
82939   sqlite3_context *context,
82940   int NotUsed,
82941   sqlite3_value **argv
82942 ){
82943   CollSeq *pColl = sqlite3GetFuncCollSeq(context);
82944   UNUSED_PARAMETER(NotUsed);
82945   if( sqlite3MemCompare(argv[0], argv[1], pColl)!=0 ){
82946     sqlite3_result_value(context, argv[0]);
82947   }
82948 }
82949 
82950 /*
82951 ** Implementation of the sqlite_version() function.  The result is the version
82952 ** of the SQLite library that is running.
82953 */
82954 static void versionFunc(
82955   sqlite3_context *context,
82956   int NotUsed,
82957   sqlite3_value **NotUsed2
82958 ){
82959   UNUSED_PARAMETER2(NotUsed, NotUsed2);
82960   /* IMP: R-48699-48617 This function is an SQL wrapper around the
82961   ** sqlite3_libversion() C-interface. */
82962   sqlite3_result_text(context, sqlite3_libversion(), -1, SQLITE_STATIC);
82963 }
82964 
82965 /*
82966 ** Implementation of the sqlite_source_id() function. The result is a string
82967 ** that identifies the particular version of the source code used to build
82968 ** SQLite.
82969 */
82970 static void sourceidFunc(
82971   sqlite3_context *context,
82972   int NotUsed,
82973   sqlite3_value **NotUsed2
82974 ){
82975   UNUSED_PARAMETER2(NotUsed, NotUsed2);
82976   /* IMP: R-24470-31136 This function is an SQL wrapper around the
82977   ** sqlite3_sourceid() C interface. */
82978   sqlite3_result_text(context, sqlite3_sourceid(), -1, SQLITE_STATIC);
82979 }
82980 
82981 /*
82982 ** Implementation of the sqlite_log() function.  This is a wrapper around
82983 ** sqlite3_log().  The return value is NULL.  The function exists purely for
82984 ** its side-effects.
82985 */
82986 static void errlogFunc(
82987   sqlite3_context *context,
82988   int argc,
82989   sqlite3_value **argv
82990 ){
82991   UNUSED_PARAMETER(argc);
82992   UNUSED_PARAMETER(context);
82993   sqlite3_log(sqlite3_value_int(argv[0]), "%s", sqlite3_value_text(argv[1]));
82994 }
82995 
82996 /*
82997 ** Implementation of the sqlite_compileoption_used() function.
82998 ** The result is an integer that identifies if the compiler option
82999 ** was used to build SQLite.
83000 */
83001 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
83002 static void compileoptionusedFunc(
83003   sqlite3_context *context,
83004   int argc,
83005   sqlite3_value **argv
83006 ){
83007   const char *zOptName;
83008   assert( argc==1 );
83009   UNUSED_PARAMETER(argc);
83010   /* IMP: R-39564-36305 The sqlite_compileoption_used() SQL
83011   ** function is a wrapper around the sqlite3_compileoption_used() C/C++
83012   ** function.
83013   */
83014   if( (zOptName = (const char*)sqlite3_value_text(argv[0]))!=0 ){
83015     sqlite3_result_int(context, sqlite3_compileoption_used(zOptName));
83016   }
83017 }
83018 #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
83019 
83020 /*
83021 ** Implementation of the sqlite_compileoption_get() function.
83022 ** The result is a string that identifies the compiler options
83023 ** used to build SQLite.
83024 */
83025 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
83026 static void compileoptiongetFunc(
83027   sqlite3_context *context,
83028   int argc,
83029   sqlite3_value **argv
83030 ){
83031   int n;
83032   assert( argc==1 );
83033   UNUSED_PARAMETER(argc);
83034   /* IMP: R-04922-24076 The sqlite_compileoption_get() SQL function
83035   ** is a wrapper around the sqlite3_compileoption_get() C/C++ function.
83036   */
83037   n = sqlite3_value_int(argv[0]);
83038   sqlite3_result_text(context, sqlite3_compileoption_get(n), -1, SQLITE_STATIC);
83039 }
83040 #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
83041 
83042 /* Array for converting from half-bytes (nybbles) into ASCII hex
83043 ** digits. */
83044 static const char hexdigits[] = {
83045   '0', '1', '2', '3', '4', '5', '6', '7',
83046   '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
83047 };
83048 
83049 /*
83050 ** EXPERIMENTAL - This is not an official function.  The interface may
83051 ** change.  This function may disappear.  Do not write code that depends
83052 ** on this function.
83053 **
83054 ** Implementation of the QUOTE() function.  This function takes a single
83055 ** argument.  If the argument is numeric, the return value is the same as
83056 ** the argument.  If the argument is NULL, the return value is the string
83057 ** "NULL".  Otherwise, the argument is enclosed in single quotes with
83058 ** single-quote escapes.
83059 */
83060 static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
83061   assert( argc==1 );
83062   UNUSED_PARAMETER(argc);
83063   switch( sqlite3_value_type(argv[0]) ){
83064     case SQLITE_INTEGER:
83065     case SQLITE_FLOAT: {
83066       sqlite3_result_value(context, argv[0]);
83067       break;
83068     }
83069     case SQLITE_BLOB: {
83070       char *zText = 0;
83071       char const *zBlob = sqlite3_value_blob(argv[0]);
83072       int nBlob = sqlite3_value_bytes(argv[0]);
83073       assert( zBlob==sqlite3_value_blob(argv[0]) ); /* No encoding change */
83074       zText = (char *)contextMalloc(context, (2*(i64)nBlob)+4);
83075       if( zText ){
83076         int i;
83077         for(i=0; i<nBlob; i++){
83078           zText[(i*2)+2] = hexdigits[(zBlob[i]>>4)&0x0F];
83079           zText[(i*2)+3] = hexdigits[(zBlob[i])&0x0F];
83080         }
83081         zText[(nBlob*2)+2] = '\'';
83082         zText[(nBlob*2)+3] = '\0';
83083         zText[0] = 'X';
83084         zText[1] = '\'';
83085         sqlite3_result_text(context, zText, -1, SQLITE_TRANSIENT);
83086         sqlite3_free(zText);
83087       }
83088       break;
83089     }
83090     case SQLITE_TEXT: {
83091       int i,j;
83092       u64 n;
83093       const unsigned char *zArg = sqlite3_value_text(argv[0]);
83094       char *z;
83095 
83096       if( zArg==0 ) return;
83097       for(i=0, n=0; zArg[i]; i++){ if( zArg[i]=='\'' ) n++; }
83098       z = contextMalloc(context, ((i64)i)+((i64)n)+3);
83099       if( z ){
83100         z[0] = '\'';
83101         for(i=0, j=1; zArg[i]; i++){
83102           z[j++] = zArg[i];
83103           if( zArg[i]=='\'' ){
83104             z[j++] = '\'';
83105           }
83106         }
83107         z[j++] = '\'';
83108         z[j] = 0;
83109         sqlite3_result_text(context, z, j, sqlite3_free);
83110       }
83111       break;
83112     }
83113     default: {
83114       assert( sqlite3_value_type(argv[0])==SQLITE_NULL );
83115       sqlite3_result_text(context, "NULL", 4, SQLITE_STATIC);
83116       break;
83117     }
83118   }
83119 }
83120 
83121 /*
83122 ** The hex() function.  Interpret the argument as a blob.  Return
83123 ** a hexadecimal rendering as text.
83124 */
83125 static void hexFunc(
83126   sqlite3_context *context,
83127   int argc,
83128   sqlite3_value **argv
83129 ){
83130   int i, n;
83131   const unsigned char *pBlob;
83132   char *zHex, *z;
83133   assert( argc==1 );
83134   UNUSED_PARAMETER(argc);
83135   pBlob = sqlite3_value_blob(argv[0]);
83136   n = sqlite3_value_bytes(argv[0]);
83137   assert( pBlob==sqlite3_value_blob(argv[0]) );  /* No encoding change */
83138   z = zHex = contextMalloc(context, ((i64)n)*2 + 1);
83139   if( zHex ){
83140     for(i=0; i<n; i++, pBlob++){
83141       unsigned char c = *pBlob;
83142       *(z++) = hexdigits[(c>>4)&0xf];
83143       *(z++) = hexdigits[c&0xf];
83144     }
83145     *z = 0;
83146     sqlite3_result_text(context, zHex, n*2, sqlite3_free);
83147   }
83148 }
83149 
83150 /*
83151 ** The zeroblob(N) function returns a zero-filled blob of size N bytes.
83152 */
83153 static void zeroblobFunc(
83154   sqlite3_context *context,
83155   int argc,
83156   sqlite3_value **argv
83157 ){
83158   i64 n;
83159   sqlite3 *db = sqlite3_context_db_handle(context);
83160   assert( argc==1 );
83161   UNUSED_PARAMETER(argc);
83162   n = sqlite3_value_int64(argv[0]);
83163   testcase( n==db->aLimit[SQLITE_LIMIT_LENGTH] );
83164   testcase( n==db->aLimit[SQLITE_LIMIT_LENGTH]+1 );
83165   if( n>db->aLimit[SQLITE_LIMIT_LENGTH] ){
83166     sqlite3_result_error_toobig(context);
83167   }else{
83168     sqlite3_result_zeroblob(context, (int)n); /* IMP: R-00293-64994 */
83169   }
83170 }
83171 
83172 /*
83173 ** The replace() function.  Three arguments are all strings: call
83174 ** them A, B, and C. The result is also a string which is derived
83175 ** from A by replacing every occurance of B with C.  The match
83176 ** must be exact.  Collating sequences are not used.
83177 */
83178 static void replaceFunc(
83179   sqlite3_context *context,
83180   int argc,
83181   sqlite3_value **argv
83182 ){
83183   const unsigned char *zStr;        /* The input string A */
83184   const unsigned char *zPattern;    /* The pattern string B */
83185   const unsigned char *zRep;        /* The replacement string C */
83186   unsigned char *zOut;              /* The output */
83187   int nStr;                /* Size of zStr */
83188   int nPattern;            /* Size of zPattern */
83189   int nRep;                /* Size of zRep */
83190   i64 nOut;                /* Maximum size of zOut */
83191   int loopLimit;           /* Last zStr[] that might match zPattern[] */
83192   int i, j;                /* Loop counters */
83193 
83194   assert( argc==3 );
83195   UNUSED_PARAMETER(argc);
83196   zStr = sqlite3_value_text(argv[0]);
83197   if( zStr==0 ) return;
83198   nStr = sqlite3_value_bytes(argv[0]);
83199   assert( zStr==sqlite3_value_text(argv[0]) );  /* No encoding change */
83200   zPattern = sqlite3_value_text(argv[1]);
83201   if( zPattern==0 ){
83202     assert( sqlite3_value_type(argv[1])==SQLITE_NULL
83203             || sqlite3_context_db_handle(context)->mallocFailed );
83204     return;
83205   }
83206   if( zPattern[0]==0 ){
83207     assert( sqlite3_value_type(argv[1])!=SQLITE_NULL );
83208     sqlite3_result_value(context, argv[0]);
83209     return;
83210   }
83211   nPattern = sqlite3_value_bytes(argv[1]);
83212   assert( zPattern==sqlite3_value_text(argv[1]) );  /* No encoding change */
83213   zRep = sqlite3_value_text(argv[2]);
83214   if( zRep==0 ) return;
83215   nRep = sqlite3_value_bytes(argv[2]);
83216   assert( zRep==sqlite3_value_text(argv[2]) );
83217   nOut = nStr + 1;
83218   assert( nOut<SQLITE_MAX_LENGTH );
83219   zOut = contextMalloc(context, (i64)nOut);
83220   if( zOut==0 ){
83221     return;
83222   }
83223   loopLimit = nStr - nPattern;
83224   for(i=j=0; i<=loopLimit; i++){
83225     if( zStr[i]!=zPattern[0] || memcmp(&zStr[i], zPattern, nPattern) ){
83226       zOut[j++] = zStr[i];
83227     }else{
83228       u8 *zOld;
83229       sqlite3 *db = sqlite3_context_db_handle(context);
83230       nOut += nRep - nPattern;
83231       testcase( nOut-1==db->aLimit[SQLITE_LIMIT_LENGTH] );
83232       testcase( nOut-2==db->aLimit[SQLITE_LIMIT_LENGTH] );
83233       if( nOut-1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
83234         sqlite3_result_error_toobig(context);
83235         sqlite3_free(zOut);
83236         return;
83237       }
83238       zOld = zOut;
83239       zOut = sqlite3_realloc(zOut, (int)nOut);
83240       if( zOut==0 ){
83241         sqlite3_result_error_nomem(context);
83242         sqlite3_free(zOld);
83243         return;
83244       }
83245       memcpy(&zOut[j], zRep, nRep);
83246       j += nRep;
83247       i += nPattern-1;
83248     }
83249   }
83250   assert( j+nStr-i+1==nOut );
83251   memcpy(&zOut[j], &zStr[i], nStr-i);
83252   j += nStr - i;
83253   assert( j<=nOut );
83254   zOut[j] = 0;
83255   sqlite3_result_text(context, (char*)zOut, j, sqlite3_free);
83256 }
83257 
83258 /*
83259 ** Implementation of the TRIM(), LTRIM(), and RTRIM() functions.
83260 ** The userdata is 0x1 for left trim, 0x2 for right trim, 0x3 for both.
83261 */
83262 static void trimFunc(
83263   sqlite3_context *context,
83264   int argc,
83265   sqlite3_value **argv
83266 ){
83267   const unsigned char *zIn;         /* Input string */
83268   const unsigned char *zCharSet;    /* Set of characters to trim */
83269   int nIn;                          /* Number of bytes in input */
83270   int flags;                        /* 1: trimleft  2: trimright  3: trim */
83271   int i;                            /* Loop counter */
83272   unsigned char *aLen = 0;          /* Length of each character in zCharSet */
83273   unsigned char **azChar = 0;       /* Individual characters in zCharSet */
83274   int nChar;                        /* Number of characters in zCharSet */
83275 
83276   if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
83277     return;
83278   }
83279   zIn = sqlite3_value_text(argv[0]);
83280   if( zIn==0 ) return;
83281   nIn = sqlite3_value_bytes(argv[0]);
83282   assert( zIn==sqlite3_value_text(argv[0]) );
83283   if( argc==1 ){
83284     static const unsigned char lenOne[] = { 1 };
83285     static unsigned char * const azOne[] = { (u8*)" " };
83286     nChar = 1;
83287     aLen = (u8*)lenOne;
83288     azChar = (unsigned char **)azOne;
83289     zCharSet = 0;
83290   }else if( (zCharSet = sqlite3_value_text(argv[1]))==0 ){
83291     return;
83292   }else{
83293     const unsigned char *z;
83294     for(z=zCharSet, nChar=0; *z; nChar++){
83295       SQLITE_SKIP_UTF8(z);
83296     }
83297     if( nChar>0 ){
83298       azChar = contextMalloc(context, ((i64)nChar)*(sizeof(char*)+1));
83299       if( azChar==0 ){
83300         return;
83301       }
83302       aLen = (unsigned char*)&azChar[nChar];
83303       for(z=zCharSet, nChar=0; *z; nChar++){
83304         azChar[nChar] = (unsigned char *)z;
83305         SQLITE_SKIP_UTF8(z);
83306         aLen[nChar] = (u8)(z - azChar[nChar]);
83307       }
83308     }
83309   }
83310   if( nChar>0 ){
83311     flags = SQLITE_PTR_TO_INT(sqlite3_user_data(context));
83312     if( flags & 1 ){
83313       while( nIn>0 ){
83314         int len = 0;
83315         for(i=0; i<nChar; i++){
83316           len = aLen[i];
83317           if( len<=nIn && memcmp(zIn, azChar[i], len)==0 ) break;
83318         }
83319         if( i>=nChar ) break;
83320         zIn += len;
83321         nIn -= len;
83322       }
83323     }
83324     if( flags & 2 ){
83325       while( nIn>0 ){
83326         int len = 0;
83327         for(i=0; i<nChar; i++){
83328           len = aLen[i];
83329           if( len<=nIn && memcmp(&zIn[nIn-len],azChar[i],len)==0 ) break;
83330         }
83331         if( i>=nChar ) break;
83332         nIn -= len;
83333       }
83334     }
83335     if( zCharSet ){
83336       sqlite3_free(azChar);
83337     }
83338   }
83339   sqlite3_result_text(context, (char*)zIn, nIn, SQLITE_TRANSIENT);
83340 }
83341 
83342 
83343 /* IMP: R-25361-16150 This function is omitted from SQLite by default. It
83344 ** is only available if the SQLITE_SOUNDEX compile-time option is used
83345 ** when SQLite is built.
83346 */
83347 #ifdef SQLITE_SOUNDEX
83348 /*
83349 ** Compute the soundex encoding of a word.
83350 **
83351 ** IMP: R-59782-00072 The soundex(X) function returns a string that is the
83352 ** soundex encoding of the string X.
83353 */
83354 static void soundexFunc(
83355   sqlite3_context *context,
83356   int argc,
83357   sqlite3_value **argv
83358 ){
83359   char zResult[8];
83360   const u8 *zIn;
83361   int i, j;
83362   static const unsigned char iCode[] = {
83363     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
83364     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
83365     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
83366     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
83367     0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
83368     1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
83369     0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
83370     1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
83371   };
83372   assert( argc==1 );
83373   zIn = (u8*)sqlite3_value_text(argv[0]);
83374   if( zIn==0 ) zIn = (u8*)"";
83375   for(i=0; zIn[i] && !sqlite3Isalpha(zIn[i]); i++){}
83376   if( zIn[i] ){
83377     u8 prevcode = iCode[zIn[i]&0x7f];
83378     zResult[0] = sqlite3Toupper(zIn[i]);
83379     for(j=1; j<4 && zIn[i]; i++){
83380       int code = iCode[zIn[i]&0x7f];
83381       if( code>0 ){
83382         if( code!=prevcode ){
83383           prevcode = code;
83384           zResult[j++] = code + '0';
83385         }
83386       }else{
83387         prevcode = 0;
83388       }
83389     }
83390     while( j<4 ){
83391       zResult[j++] = '0';
83392     }
83393     zResult[j] = 0;
83394     sqlite3_result_text(context, zResult, 4, SQLITE_TRANSIENT);
83395   }else{
83396     /* IMP: R-64894-50321 The string "?000" is returned if the argument
83397     ** is NULL or contains no ASCII alphabetic characters. */
83398     sqlite3_result_text(context, "?000", 4, SQLITE_STATIC);
83399   }
83400 }
83401 #endif /* SQLITE_SOUNDEX */
83402 
83403 #ifndef SQLITE_OMIT_LOAD_EXTENSION
83404 /*
83405 ** A function that loads a shared-library extension then returns NULL.
83406 */
83407 static void loadExt(sqlite3_context *context, int argc, sqlite3_value **argv){
83408   const char *zFile = (const char *)sqlite3_value_text(argv[0]);
83409   const char *zProc;
83410   sqlite3 *db = sqlite3_context_db_handle(context);
83411   char *zErrMsg = 0;
83412 
83413   if( argc==2 ){
83414     zProc = (const char *)sqlite3_value_text(argv[1]);
83415   }else{
83416     zProc = 0;
83417   }
83418   if( zFile && sqlite3_load_extension(db, zFile, zProc, &zErrMsg) ){
83419     sqlite3_result_error(context, zErrMsg, -1);
83420     sqlite3_free(zErrMsg);
83421   }
83422 }
83423 #endif
83424 
83425 
83426 /*
83427 ** An instance of the following structure holds the context of a
83428 ** sum() or avg() aggregate computation.
83429 */
83430 typedef struct SumCtx SumCtx;
83431 struct SumCtx {
83432   double rSum;      /* Floating point sum */
83433   i64 iSum;         /* Integer sum */
83434   i64 cnt;          /* Number of elements summed */
83435   u8 overflow;      /* True if integer overflow seen */
83436   u8 approx;        /* True if non-integer value was input to the sum */
83437 };
83438 
83439 /*
83440 ** Routines used to compute the sum, average, and total.
83441 **
83442 ** The SUM() function follows the (broken) SQL standard which means
83443 ** that it returns NULL if it sums over no inputs.  TOTAL returns
83444 ** 0.0 in that case.  In addition, TOTAL always returns a float where
83445 ** SUM might return an integer if it never encounters a floating point
83446 ** value.  TOTAL never fails, but SUM might through an exception if
83447 ** it overflows an integer.
83448 */
83449 static void sumStep(sqlite3_context *context, int argc, sqlite3_value **argv){
83450   SumCtx *p;
83451   int type;
83452   assert( argc==1 );
83453   UNUSED_PARAMETER(argc);
83454   p = sqlite3_aggregate_context(context, sizeof(*p));
83455   type = sqlite3_value_numeric_type(argv[0]);
83456   if( p && type!=SQLITE_NULL ){
83457     p->cnt++;
83458     if( type==SQLITE_INTEGER ){
83459       i64 v = sqlite3_value_int64(argv[0]);
83460       p->rSum += v;
83461       if( (p->approx|p->overflow)==0 && sqlite3AddInt64(&p->iSum, v) ){
83462         p->overflow = 1;
83463       }
83464     }else{
83465       p->rSum += sqlite3_value_double(argv[0]);
83466       p->approx = 1;
83467     }
83468   }
83469 }
83470 static void sumFinalize(sqlite3_context *context){
83471   SumCtx *p;
83472   p = sqlite3_aggregate_context(context, 0);
83473   if( p && p->cnt>0 ){
83474     if( p->overflow ){
83475       sqlite3_result_error(context,"integer overflow",-1);
83476     }else if( p->approx ){
83477       sqlite3_result_double(context, p->rSum);
83478     }else{
83479       sqlite3_result_int64(context, p->iSum);
83480     }
83481   }
83482 }
83483 static void avgFinalize(sqlite3_context *context){
83484   SumCtx *p;
83485   p = sqlite3_aggregate_context(context, 0);
83486   if( p && p->cnt>0 ){
83487     sqlite3_result_double(context, p->rSum/(double)p->cnt);
83488   }
83489 }
83490 static void totalFinalize(sqlite3_context *context){
83491   SumCtx *p;
83492   p = sqlite3_aggregate_context(context, 0);
83493   /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
83494   sqlite3_result_double(context, p ? p->rSum : (double)0);
83495 }
83496 
83497 /*
83498 ** The following structure keeps track of state information for the
83499 ** count() aggregate function.
83500 */
83501 typedef struct CountCtx CountCtx;
83502 struct CountCtx {
83503   i64 n;
83504 };
83505 
83506 /*
83507 ** Routines to implement the count() aggregate function.
83508 */
83509 static void countStep(sqlite3_context *context, int argc, sqlite3_value **argv){
83510   CountCtx *p;
83511   p = sqlite3_aggregate_context(context, sizeof(*p));
83512   if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(argv[0])) && p ){
83513     p->n++;
83514   }
83515 
83516 #ifndef SQLITE_OMIT_DEPRECATED
83517   /* The sqlite3_aggregate_count() function is deprecated.  But just to make
83518   ** sure it still operates correctly, verify that its count agrees with our
83519   ** internal count when using count(*) and when the total count can be
83520   ** expressed as a 32-bit integer. */
83521   assert( argc==1 || p==0 || p->n>0x7fffffff
83522           || p->n==sqlite3_aggregate_count(context) );
83523 #endif
83524 }
83525 static void countFinalize(sqlite3_context *context){
83526   CountCtx *p;
83527   p = sqlite3_aggregate_context(context, 0);
83528   sqlite3_result_int64(context, p ? p->n : 0);
83529 }
83530 
83531 /*
83532 ** Routines to implement min() and max() aggregate functions.
83533 */
83534 static void minmaxStep(
83535   sqlite3_context *context,
83536   int NotUsed,
83537   sqlite3_value **argv
83538 ){
83539   Mem *pArg  = (Mem *)argv[0];
83540   Mem *pBest;
83541   UNUSED_PARAMETER(NotUsed);
83542 
83543   if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
83544   pBest = (Mem *)sqlite3_aggregate_context(context, sizeof(*pBest));
83545   if( !pBest ) return;
83546 
83547   if( pBest->flags ){
83548     int max;
83549     int cmp;
83550     CollSeq *pColl = sqlite3GetFuncCollSeq(context);
83551     /* This step function is used for both the min() and max() aggregates,
83552     ** the only difference between the two being that the sense of the
83553     ** comparison is inverted. For the max() aggregate, the
83554     ** sqlite3_user_data() function returns (void *)-1. For min() it
83555     ** returns (void *)db, where db is the sqlite3* database pointer.
83556     ** Therefore the next statement sets variable 'max' to 1 for the max()
83557     ** aggregate, or 0 for min().
83558     */
83559     max = sqlite3_user_data(context)!=0;
83560     cmp = sqlite3MemCompare(pBest, pArg, pColl);
83561     if( (max && cmp<0) || (!max && cmp>0) ){
83562       sqlite3VdbeMemCopy(pBest, pArg);
83563     }
83564   }else{
83565     sqlite3VdbeMemCopy(pBest, pArg);
83566   }
83567 }
83568 static void minMaxFinalize(sqlite3_context *context){
83569   sqlite3_value *pRes;
83570   pRes = (sqlite3_value *)sqlite3_aggregate_context(context, 0);
83571   if( pRes ){
83572     if( ALWAYS(pRes->flags) ){
83573       sqlite3_result_value(context, pRes);
83574     }
83575     sqlite3VdbeMemRelease(pRes);
83576   }
83577 }
83578 
83579 /*
83580 ** group_concat(EXPR, ?SEPARATOR?)
83581 */
83582 static void groupConcatStep(
83583   sqlite3_context *context,
83584   int argc,
83585   sqlite3_value **argv
83586 ){
83587   const char *zVal;
83588   StrAccum *pAccum;
83589   const char *zSep;
83590   int nVal, nSep;
83591   assert( argc==1 || argc==2 );
83592   if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
83593   pAccum = (StrAccum*)sqlite3_aggregate_context(context, sizeof(*pAccum));
83594 
83595   if( pAccum ){
83596     sqlite3 *db = sqlite3_context_db_handle(context);
83597     int firstTerm = pAccum->useMalloc==0;
83598     pAccum->useMalloc = 2;
83599     pAccum->mxAlloc = db->aLimit[SQLITE_LIMIT_LENGTH];
83600     if( !firstTerm ){
83601       if( argc==2 ){
83602         zSep = (char*)sqlite3_value_text(argv[1]);
83603         nSep = sqlite3_value_bytes(argv[1]);
83604       }else{
83605         zSep = ",";
83606         nSep = 1;
83607       }
83608       sqlite3StrAccumAppend(pAccum, zSep, nSep);
83609     }
83610     zVal = (char*)sqlite3_value_text(argv[0]);
83611     nVal = sqlite3_value_bytes(argv[0]);
83612     sqlite3StrAccumAppend(pAccum, zVal, nVal);
83613   }
83614 }
83615 static void groupConcatFinalize(sqlite3_context *context){
83616   StrAccum *pAccum;
83617   pAccum = sqlite3_aggregate_context(context, 0);
83618   if( pAccum ){
83619     if( pAccum->tooBig ){
83620       sqlite3_result_error_toobig(context);
83621     }else if( pAccum->mallocFailed ){
83622       sqlite3_result_error_nomem(context);
83623     }else{
83624       sqlite3_result_text(context, sqlite3StrAccumFinish(pAccum), -1,
83625                           sqlite3_free);
83626     }
83627   }
83628 }
83629 
83630 /*
83631 ** This routine does per-connection function registration.  Most
83632 ** of the built-in functions above are part of the global function set.
83633 ** This routine only deals with those that are not global.
83634 */
83635 SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(sqlite3 *db){
83636   int rc = sqlite3_overload_function(db, "MATCH", 2);
83637   assert( rc==SQLITE_NOMEM || rc==SQLITE_OK );
83638   if( rc==SQLITE_NOMEM ){
83639     db->mallocFailed = 1;
83640   }
83641 }
83642 
83643 /*
83644 ** Set the LIKEOPT flag on the 2-argument function with the given name.
83645 */
83646 static void setLikeOptFlag(sqlite3 *db, const char *zName, u8 flagVal){
83647   FuncDef *pDef;
83648   pDef = sqlite3FindFunction(db, zName, sqlite3Strlen30(zName),
83649                              2, SQLITE_UTF8, 0);
83650   if( ALWAYS(pDef) ){
83651     pDef->flags = flagVal;
83652   }
83653 }
83654 
83655 /*
83656 ** Register the built-in LIKE and GLOB functions.  The caseSensitive
83657 ** parameter determines whether or not the LIKE operator is case
83658 ** sensitive.  GLOB is always case sensitive.
83659 */
83660 SQLITE_PRIVATE void sqlite3RegisterLikeFunctions(sqlite3 *db, int caseSensitive){
83661   struct compareInfo *pInfo;
83662   if( caseSensitive ){
83663     pInfo = (struct compareInfo*)&likeInfoAlt;
83664   }else{
83665     pInfo = (struct compareInfo*)&likeInfoNorm;
83666   }
83667   sqlite3CreateFunc(db, "like", 2, SQLITE_UTF8, pInfo, likeFunc, 0, 0, 0);
83668   sqlite3CreateFunc(db, "like", 3, SQLITE_UTF8, pInfo, likeFunc, 0, 0, 0);
83669   sqlite3CreateFunc(db, "glob", 2, SQLITE_UTF8,
83670       (struct compareInfo*)&globInfo, likeFunc, 0, 0, 0);
83671   setLikeOptFlag(db, "glob", SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE);
83672   setLikeOptFlag(db, "like",
83673       caseSensitive ? (SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE) : SQLITE_FUNC_LIKE);
83674 }
83675 
83676 /*
83677 ** pExpr points to an expression which implements a function.  If
83678 ** it is appropriate to apply the LIKE optimization to that function
83679 ** then set aWc[0] through aWc[2] to the wildcard characters and
83680 ** return TRUE.  If the function is not a LIKE-style function then
83681 ** return FALSE.
83682 */
83683 SQLITE_PRIVATE int sqlite3IsLikeFunction(sqlite3 *db, Expr *pExpr, int *pIsNocase, char *aWc){
83684   FuncDef *pDef;
83685   if( pExpr->op!=TK_FUNCTION
83686    || !pExpr->x.pList
83687    || pExpr->x.pList->nExpr!=2
83688   ){
83689     return 0;
83690   }
83691   assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
83692   pDef = sqlite3FindFunction(db, pExpr->u.zToken,
83693                              sqlite3Strlen30(pExpr->u.zToken),
83694                              2, SQLITE_UTF8, 0);
83695   if( NEVER(pDef==0) || (pDef->flags & SQLITE_FUNC_LIKE)==0 ){
83696     return 0;
83697   }
83698 
83699   /* The memcpy() statement assumes that the wildcard characters are
83700   ** the first three statements in the compareInfo structure.  The
83701   ** asserts() that follow verify that assumption
83702   */
83703   memcpy(aWc, pDef->pUserData, 3);
83704   assert( (char*)&likeInfoAlt == (char*)&likeInfoAlt.matchAll );
83705   assert( &((char*)&likeInfoAlt)[1] == (char*)&likeInfoAlt.matchOne );
83706   assert( &((char*)&likeInfoAlt)[2] == (char*)&likeInfoAlt.matchSet );
83707   *pIsNocase = (pDef->flags & SQLITE_FUNC_CASE)==0;
83708   return 1;
83709 }
83710 
83711 /*
83712 ** All all of the FuncDef structures in the aBuiltinFunc[] array above
83713 ** to the global function hash table.  This occurs at start-time (as
83714 ** a consequence of calling sqlite3_initialize()).
83715 **
83716 ** After this routine runs
83717 */
83718 SQLITE_PRIVATE void sqlite3RegisterGlobalFunctions(void){
83719   /*
83720   ** The following array holds FuncDef structures for all of the functions
83721   ** defined in this file.
83722   **
83723   ** The array cannot be constant since changes are made to the
83724   ** FuncDef.pHash elements at start-time.  The elements of this array
83725   ** are read-only after initialization is complete.
83726   */
83727   static SQLITE_WSD FuncDef aBuiltinFunc[] = {
83728     FUNCTION(ltrim,              1, 1, 0, trimFunc         ),
83729     FUNCTION(ltrim,              2, 1, 0, trimFunc         ),
83730     FUNCTION(rtrim,              1, 2, 0, trimFunc         ),
83731     FUNCTION(rtrim,              2, 2, 0, trimFunc         ),
83732     FUNCTION(trim,               1, 3, 0, trimFunc         ),
83733     FUNCTION(trim,               2, 3, 0, trimFunc         ),
83734     FUNCTION(min,               -1, 0, 1, minmaxFunc       ),
83735     FUNCTION(min,                0, 0, 1, 0                ),
83736     AGGREGATE(min,               1, 0, 1, minmaxStep,      minMaxFinalize ),
83737     FUNCTION(max,               -1, 1, 1, minmaxFunc       ),
83738     FUNCTION(max,                0, 1, 1, 0                ),
83739     AGGREGATE(max,               1, 1, 1, minmaxStep,      minMaxFinalize ),
83740     FUNCTION(typeof,             1, 0, 0, typeofFunc       ),
83741     FUNCTION(length,             1, 0, 0, lengthFunc       ),
83742     FUNCTION(substr,             2, 0, 0, substrFunc       ),
83743     FUNCTION(substr,             3, 0, 0, substrFunc       ),
83744     FUNCTION(abs,                1, 0, 0, absFunc          ),
83745 #ifndef SQLITE_OMIT_FLOATING_POINT
83746     FUNCTION(round,              1, 0, 0, roundFunc        ),
83747     FUNCTION(round,              2, 0, 0, roundFunc        ),
83748 #endif
83749     FUNCTION(upper,              1, 0, 0, upperFunc        ),
83750     FUNCTION(lower,              1, 0, 0, lowerFunc        ),
83751     FUNCTION(coalesce,           1, 0, 0, 0                ),
83752     FUNCTION(coalesce,           0, 0, 0, 0                ),
83753 /*  FUNCTION(coalesce,          -1, 0, 0, ifnullFunc       ), */
83754     {-1,SQLITE_UTF8,SQLITE_FUNC_COALESCE,0,0,ifnullFunc,0,0,"coalesce",0,0},
83755     FUNCTION(hex,                1, 0, 0, hexFunc          ),
83756 /*  FUNCTION(ifnull,             2, 0, 0, ifnullFunc       ), */
83757     {2,SQLITE_UTF8,SQLITE_FUNC_COALESCE,0,0,ifnullFunc,0,0,"ifnull",0,0},
83758     FUNCTION(random,             0, 0, 0, randomFunc       ),
83759     FUNCTION(randomblob,         1, 0, 0, randomBlob       ),
83760     FUNCTION(nullif,             2, 0, 1, nullifFunc       ),
83761     FUNCTION(sqlite_version,     0, 0, 0, versionFunc      ),
83762     FUNCTION(sqlite_source_id,   0, 0, 0, sourceidFunc     ),
83763     FUNCTION(sqlite_log,         2, 0, 0, errlogFunc       ),
83764 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
83765     FUNCTION(sqlite_compileoption_used,1, 0, 0, compileoptionusedFunc  ),
83766     FUNCTION(sqlite_compileoption_get, 1, 0, 0, compileoptiongetFunc  ),
83767 #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
83768     FUNCTION(quote,              1, 0, 0, quoteFunc        ),
83769     FUNCTION(last_insert_rowid,  0, 0, 0, last_insert_rowid),
83770     FUNCTION(changes,            0, 0, 0, changes          ),
83771     FUNCTION(total_changes,      0, 0, 0, total_changes    ),
83772     FUNCTION(replace,            3, 0, 0, replaceFunc      ),
83773     FUNCTION(zeroblob,           1, 0, 0, zeroblobFunc     ),
83774   #ifdef SQLITE_SOUNDEX
83775     FUNCTION(soundex,            1, 0, 0, soundexFunc      ),
83776   #endif
83777   #ifndef SQLITE_OMIT_LOAD_EXTENSION
83778     FUNCTION(load_extension,     1, 0, 0, loadExt          ),
83779     FUNCTION(load_extension,     2, 0, 0, loadExt          ),
83780   #endif
83781     AGGREGATE(sum,               1, 0, 0, sumStep,         sumFinalize    ),
83782     AGGREGATE(total,             1, 0, 0, sumStep,         totalFinalize    ),
83783     AGGREGATE(avg,               1, 0, 0, sumStep,         avgFinalize    ),
83784  /* AGGREGATE(count,             0, 0, 0, countStep,       countFinalize  ), */
83785     {0,SQLITE_UTF8,SQLITE_FUNC_COUNT,0,0,0,countStep,countFinalize,"count",0,0},
83786     AGGREGATE(count,             1, 0, 0, countStep,       countFinalize  ),
83787     AGGREGATE(group_concat,      1, 0, 0, groupConcatStep, groupConcatFinalize),
83788     AGGREGATE(group_concat,      2, 0, 0, groupConcatStep, groupConcatFinalize),
83789 
83790     LIKEFUNC(glob, 2, &globInfo, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE),
83791   #ifdef SQLITE_CASE_SENSITIVE_LIKE
83792     LIKEFUNC(like, 2, &likeInfoAlt, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE),
83793     LIKEFUNC(like, 3, &likeInfoAlt, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE),
83794   #else
83795     LIKEFUNC(like, 2, &likeInfoNorm, SQLITE_FUNC_LIKE),
83796     LIKEFUNC(like, 3, &likeInfoNorm, SQLITE_FUNC_LIKE),
83797   #endif
83798   };
83799 
83800   int i;
83801   FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
83802   FuncDef *aFunc = (FuncDef*)&GLOBAL(FuncDef, aBuiltinFunc);
83803 
83804   for(i=0; i<ArraySize(aBuiltinFunc); i++){
83805     sqlite3FuncDefInsert(pHash, &aFunc[i]);
83806   }
83807   sqlite3RegisterDateTimeFunctions();
83808 #ifndef SQLITE_OMIT_ALTERTABLE
83809   sqlite3AlterFunctions();
83810 #endif
83811 }
83812 
83813 /************** End of func.c ************************************************/
83814 /************** Begin file fkey.c ********************************************/
83815 /*
83816 **
83817 ** The author disclaims copyright to this source code.  In place of
83818 ** a legal notice, here is a blessing:
83819 **
83820 **    May you do good and not evil.
83821 **    May you find forgiveness for yourself and forgive others.
83822 **    May you share freely, never taking more than you give.
83823 **
83824 *************************************************************************
83825 ** This file contains code used by the compiler to add foreign key
83826 ** support to compiled SQL statements.
83827 */
83828 
83829 #ifndef SQLITE_OMIT_FOREIGN_KEY
83830 #ifndef SQLITE_OMIT_TRIGGER
83831 
83832 /*
83833 ** Deferred and Immediate FKs
83834 ** --------------------------
83835 **
83836 ** Foreign keys in SQLite come in two flavours: deferred and immediate.
83837 ** If an immediate foreign key constraint is violated, SQLITE_CONSTRAINT
83838 ** is returned and the current statement transaction rolled back. If a
83839 ** deferred foreign key constraint is violated, no action is taken
83840 ** immediately. However if the application attempts to commit the
83841 ** transaction before fixing the constraint violation, the attempt fails.
83842 **
83843 ** Deferred constraints are implemented using a simple counter associated
83844 ** with the database handle. The counter is set to zero each time a
83845 ** database transaction is opened. Each time a statement is executed
83846 ** that causes a foreign key violation, the counter is incremented. Each
83847 ** time a statement is executed that removes an existing violation from
83848 ** the database, the counter is decremented. When the transaction is
83849 ** committed, the commit fails if the current value of the counter is
83850 ** greater than zero. This scheme has two big drawbacks:
83851 **
83852 **   * When a commit fails due to a deferred foreign key constraint,
83853 **     there is no way to tell which foreign constraint is not satisfied,
83854 **     or which row it is not satisfied for.
83855 **
83856 **   * If the database contains foreign key violations when the
83857 **     transaction is opened, this may cause the mechanism to malfunction.
83858 **
83859 ** Despite these problems, this approach is adopted as it seems simpler
83860 ** than the alternatives.
83861 **
83862 ** INSERT operations:
83863 **
83864 **   I.1) For each FK for which the table is the child table, search
83865 **        the parent table for a match. If none is found increment the
83866 **        constraint counter.
83867 **
83868 **   I.2) For each FK for which the table is the parent table,
83869 **        search the child table for rows that correspond to the new
83870 **        row in the parent table. Decrement the counter for each row
83871 **        found (as the constraint is now satisfied).
83872 **
83873 ** DELETE operations:
83874 **
83875 **   D.1) For each FK for which the table is the child table,
83876 **        search the parent table for a row that corresponds to the
83877 **        deleted row in the child table. If such a row is not found,
83878 **        decrement the counter.
83879 **
83880 **   D.2) For each FK for which the table is the parent table, search
83881 **        the child table for rows that correspond to the deleted row
83882 **        in the parent table. For each found increment the counter.
83883 **
83884 ** UPDATE operations:
83885 **
83886 **   An UPDATE command requires that all 4 steps above are taken, but only
83887 **   for FK constraints for which the affected columns are actually
83888 **   modified (values must be compared at runtime).
83889 **
83890 ** Note that I.1 and D.1 are very similar operations, as are I.2 and D.2.
83891 ** This simplifies the implementation a bit.
83892 **
83893 ** For the purposes of immediate FK constraints, the OR REPLACE conflict
83894 ** resolution is considered to delete rows before the new row is inserted.
83895 ** If a delete caused by OR REPLACE violates an FK constraint, an exception
83896 ** is thrown, even if the FK constraint would be satisfied after the new
83897 ** row is inserted.
83898 **
83899 ** Immediate constraints are usually handled similarly. The only difference
83900 ** is that the counter used is stored as part of each individual statement
83901 ** object (struct Vdbe). If, after the statement has run, its immediate
83902 ** constraint counter is greater than zero, it returns SQLITE_CONSTRAINT
83903 ** and the statement transaction is rolled back. An exception is an INSERT
83904 ** statement that inserts a single row only (no triggers). In this case,
83905 ** instead of using a counter, an exception is thrown immediately if the
83906 ** INSERT violates a foreign key constraint. This is necessary as such
83907 ** an INSERT does not open a statement transaction.
83908 **
83909 ** TODO: How should dropping a table be handled? How should renaming a
83910 ** table be handled?
83911 **
83912 **
83913 ** Query API Notes
83914 ** ---------------
83915 **
83916 ** Before coding an UPDATE or DELETE row operation, the code-generator
83917 ** for those two operations needs to know whether or not the operation
83918 ** requires any FK processing and, if so, which columns of the original
83919 ** row are required by the FK processing VDBE code (i.e. if FKs were
83920 ** implemented using triggers, which of the old.* columns would be
83921 ** accessed). No information is required by the code-generator before
83922 ** coding an INSERT operation. The functions used by the UPDATE/DELETE
83923 ** generation code to query for this information are:
83924 **
83925 **   sqlite3FkRequired() - Test to see if FK processing is required.
83926 **   sqlite3FkOldmask()  - Query for the set of required old.* columns.
83927 **
83928 **
83929 ** Externally accessible module functions
83930 ** --------------------------------------
83931 **
83932 **   sqlite3FkCheck()    - Check for foreign key violations.
83933 **   sqlite3FkActions()  - Code triggers for ON UPDATE/ON DELETE actions.
83934 **   sqlite3FkDelete()   - Delete an FKey structure.
83935 */
83936 
83937 /*
83938 ** VDBE Calling Convention
83939 ** -----------------------
83940 **
83941 ** Example:
83942 **
83943 **   For the following INSERT statement:
83944 **
83945 **     CREATE TABLE t1(a, b INTEGER PRIMARY KEY, c);
83946 **     INSERT INTO t1 VALUES(1, 2, 3.1);
83947 **
83948 **   Register (x):        2    (type integer)
83949 **   Register (x+1):      1    (type integer)
83950 **   Register (x+2):      NULL (type NULL)
83951 **   Register (x+3):      3.1  (type real)
83952 */
83953 
83954 /*
83955 ** A foreign key constraint requires that the key columns in the parent
83956 ** table are collectively subject to a UNIQUE or PRIMARY KEY constraint.
83957 ** Given that pParent is the parent table for foreign key constraint pFKey,
83958 ** search the schema a unique index on the parent key columns.
83959 **
83960 ** If successful, zero is returned. If the parent key is an INTEGER PRIMARY
83961 ** KEY column, then output variable *ppIdx is set to NULL. Otherwise, *ppIdx
83962 ** is set to point to the unique index.
83963 **
83964 ** If the parent key consists of a single column (the foreign key constraint
83965 ** is not a composite foreign key), output variable *paiCol is set to NULL.
83966 ** Otherwise, it is set to point to an allocated array of size N, where
83967 ** N is the number of columns in the parent key. The first element of the
83968 ** array is the index of the child table column that is mapped by the FK
83969 ** constraint to the parent table column stored in the left-most column
83970 ** of index *ppIdx. The second element of the array is the index of the
83971 ** child table column that corresponds to the second left-most column of
83972 ** *ppIdx, and so on.
83973 **
83974 ** If the required index cannot be found, either because:
83975 **
83976 **   1) The named parent key columns do not exist, or
83977 **
83978 **   2) The named parent key columns do exist, but are not subject to a
83979 **      UNIQUE or PRIMARY KEY constraint, or
83980 **
83981 **   3) No parent key columns were provided explicitly as part of the
83982 **      foreign key definition, and the parent table does not have a
83983 **      PRIMARY KEY, or
83984 **
83985 **   4) No parent key columns were provided explicitly as part of the
83986 **      foreign key definition, and the PRIMARY KEY of the parent table
83987 **      consists of a a different number of columns to the child key in
83988 **      the child table.
83989 **
83990 ** then non-zero is returned, and a "foreign key mismatch" error loaded
83991 ** into pParse. If an OOM error occurs, non-zero is returned and the
83992 ** pParse->db->mallocFailed flag is set.
83993 */
83994 static int locateFkeyIndex(
83995   Parse *pParse,                  /* Parse context to store any error in */
83996   Table *pParent,                 /* Parent table of FK constraint pFKey */
83997   FKey *pFKey,                    /* Foreign key to find index for */
83998   Index **ppIdx,                  /* OUT: Unique index on parent table */
83999   int **paiCol                    /* OUT: Map of index columns in pFKey */
84000 ){
84001   Index *pIdx = 0;                    /* Value to return via *ppIdx */
84002   int *aiCol = 0;                     /* Value to return via *paiCol */
84003   int nCol = pFKey->nCol;             /* Number of columns in parent key */
84004   char *zKey = pFKey->aCol[0].zCol;   /* Name of left-most parent key column */
84005 
84006   /* The caller is responsible for zeroing output parameters. */
84007   assert( ppIdx && *ppIdx==0 );
84008   assert( !paiCol || *paiCol==0 );
84009   assert( pParse );
84010 
84011   /* If this is a non-composite (single column) foreign key, check if it
84012   ** maps to the INTEGER PRIMARY KEY of table pParent. If so, leave *ppIdx
84013   ** and *paiCol set to zero and return early.
84014   **
84015   ** Otherwise, for a composite foreign key (more than one column), allocate
84016   ** space for the aiCol array (returned via output parameter *paiCol).
84017   ** Non-composite foreign keys do not require the aiCol array.
84018   */
84019   if( nCol==1 ){
84020     /* The FK maps to the IPK if any of the following are true:
84021     **
84022     **   1) There is an INTEGER PRIMARY KEY column and the FK is implicitly
84023     **      mapped to the primary key of table pParent, or
84024     **   2) The FK is explicitly mapped to a column declared as INTEGER
84025     **      PRIMARY KEY.
84026     */
84027     if( pParent->iPKey>=0 ){
84028       if( !zKey ) return 0;
84029       if( !sqlite3StrICmp(pParent->aCol[pParent->iPKey].zName, zKey) ) return 0;
84030     }
84031   }else if( paiCol ){
84032     assert( nCol>1 );
84033     aiCol = (int *)sqlite3DbMallocRaw(pParse->db, nCol*sizeof(int));
84034     if( !aiCol ) return 1;
84035     *paiCol = aiCol;
84036   }
84037 
84038   for(pIdx=pParent->pIndex; pIdx; pIdx=pIdx->pNext){
84039     if( pIdx->nColumn==nCol && pIdx->onError!=OE_None ){
84040       /* pIdx is a UNIQUE index (or a PRIMARY KEY) and has the right number
84041       ** of columns. If each indexed column corresponds to a foreign key
84042       ** column of pFKey, then this index is a winner.  */
84043 
84044       if( zKey==0 ){
84045         /* If zKey is NULL, then this foreign key is implicitly mapped to
84046         ** the PRIMARY KEY of table pParent. The PRIMARY KEY index may be
84047         ** identified by the test (Index.autoIndex==2).  */
84048         if( pIdx->autoIndex==2 ){
84049           if( aiCol ){
84050             int i;
84051             for(i=0; i<nCol; i++) aiCol[i] = pFKey->aCol[i].iFrom;
84052           }
84053           break;
84054         }
84055       }else{
84056         /* If zKey is non-NULL, then this foreign key was declared to
84057         ** map to an explicit list of columns in table pParent. Check if this
84058         ** index matches those columns. Also, check that the index uses
84059         ** the default collation sequences for each column. */
84060         int i, j;
84061         for(i=0; i<nCol; i++){
84062           int iCol = pIdx->aiColumn[i];     /* Index of column in parent tbl */
84063           char *zDfltColl;                  /* Def. collation for column */
84064           char *zIdxCol;                    /* Name of indexed column */
84065 
84066           /* If the index uses a collation sequence that is different from
84067           ** the default collation sequence for the column, this index is
84068           ** unusable. Bail out early in this case.  */
84069           zDfltColl = pParent->aCol[iCol].zColl;
84070           if( !zDfltColl ){
84071             zDfltColl = "BINARY";
84072           }
84073           if( sqlite3StrICmp(pIdx->azColl[i], zDfltColl) ) break;
84074 
84075           zIdxCol = pParent->aCol[iCol].zName;
84076           for(j=0; j<nCol; j++){
84077             if( sqlite3StrICmp(pFKey->aCol[j].zCol, zIdxCol)==0 ){
84078               if( aiCol ) aiCol[i] = pFKey->aCol[j].iFrom;
84079               break;
84080             }
84081           }
84082           if( j==nCol ) break;
84083         }
84084         if( i==nCol ) break;      /* pIdx is usable */
84085       }
84086     }
84087   }
84088 
84089   if( !pIdx ){
84090     if( !pParse->disableTriggers ){
84091       sqlite3ErrorMsg(pParse, "foreign key mismatch");
84092     }
84093     sqlite3DbFree(pParse->db, aiCol);
84094     return 1;
84095   }
84096 
84097   *ppIdx = pIdx;
84098   return 0;
84099 }
84100 
84101 /*
84102 ** This function is called when a row is inserted into or deleted from the
84103 ** child table of foreign key constraint pFKey. If an SQL UPDATE is executed
84104 ** on the child table of pFKey, this function is invoked twice for each row
84105 ** affected - once to "delete" the old row, and then again to "insert" the
84106 ** new row.
84107 **
84108 ** Each time it is called, this function generates VDBE code to locate the
84109 ** row in the parent table that corresponds to the row being inserted into
84110 ** or deleted from the child table. If the parent row can be found, no
84111 ** special action is taken. Otherwise, if the parent row can *not* be
84112 ** found in the parent table:
84113 **
84114 **   Operation | FK type   | Action taken
84115 **   --------------------------------------------------------------------------
84116 **   INSERT      immediate   Increment the "immediate constraint counter".
84117 **
84118 **   DELETE      immediate   Decrement the "immediate constraint counter".
84119 **
84120 **   INSERT      deferred    Increment the "deferred constraint counter".
84121 **
84122 **   DELETE      deferred    Decrement the "deferred constraint counter".
84123 **
84124 ** These operations are identified in the comment at the top of this file
84125 ** (fkey.c) as "I.1" and "D.1".
84126 */
84127 static void fkLookupParent(
84128   Parse *pParse,        /* Parse context */
84129   int iDb,              /* Index of database housing pTab */
84130   Table *pTab,          /* Parent table of FK pFKey */
84131   Index *pIdx,          /* Unique index on parent key columns in pTab */
84132   FKey *pFKey,          /* Foreign key constraint */
84133   int *aiCol,           /* Map from parent key columns to child table columns */
84134   int regData,          /* Address of array containing child table row */
84135   int nIncr,            /* Increment constraint counter by this */
84136   int isIgnore          /* If true, pretend pTab contains all NULL values */
84137 ){
84138   int i;                                    /* Iterator variable */
84139   Vdbe *v = sqlite3GetVdbe(pParse);         /* Vdbe to add code to */
84140   int iCur = pParse->nTab - 1;              /* Cursor number to use */
84141   int iOk = sqlite3VdbeMakeLabel(v);        /* jump here if parent key found */
84142 
84143   /* If nIncr is less than zero, then check at runtime if there are any
84144   ** outstanding constraints to resolve. If there are not, there is no need
84145   ** to check if deleting this row resolves any outstanding violations.
84146   **
84147   ** Check if any of the key columns in the child table row are NULL. If
84148   ** any are, then the constraint is considered satisfied. No need to
84149   ** search for a matching row in the parent table.  */
84150   if( nIncr<0 ){
84151     sqlite3VdbeAddOp2(v, OP_FkIfZero, pFKey->isDeferred, iOk);
84152   }
84153   for(i=0; i<pFKey->nCol; i++){
84154     int iReg = aiCol[i] + regData + 1;
84155     sqlite3VdbeAddOp2(v, OP_IsNull, iReg, iOk);
84156   }
84157 
84158   if( isIgnore==0 ){
84159     if( pIdx==0 ){
84160       /* If pIdx is NULL, then the parent key is the INTEGER PRIMARY KEY
84161       ** column of the parent table (table pTab).  */
84162       int iMustBeInt;               /* Address of MustBeInt instruction */
84163       int regTemp = sqlite3GetTempReg(pParse);
84164 
84165       /* Invoke MustBeInt to coerce the child key value to an integer (i.e.
84166       ** apply the affinity of the parent key). If this fails, then there
84167       ** is no matching parent key. Before using MustBeInt, make a copy of
84168       ** the value. Otherwise, the value inserted into the child key column
84169       ** will have INTEGER affinity applied to it, which may not be correct.  */
84170       sqlite3VdbeAddOp2(v, OP_SCopy, aiCol[0]+1+regData, regTemp);
84171       iMustBeInt = sqlite3VdbeAddOp2(v, OP_MustBeInt, regTemp, 0);
84172 
84173       /* If the parent table is the same as the child table, and we are about
84174       ** to increment the constraint-counter (i.e. this is an INSERT operation),
84175       ** then check if the row being inserted matches itself. If so, do not
84176       ** increment the constraint-counter.  */
84177       if( pTab==pFKey->pFrom && nIncr==1 ){
84178         sqlite3VdbeAddOp3(v, OP_Eq, regData, iOk, regTemp);
84179       }
84180 
84181       sqlite3OpenTable(pParse, iCur, iDb, pTab, OP_OpenRead);
84182       sqlite3VdbeAddOp3(v, OP_NotExists, iCur, 0, regTemp);
84183       sqlite3VdbeAddOp2(v, OP_Goto, 0, iOk);
84184       sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
84185       sqlite3VdbeJumpHere(v, iMustBeInt);
84186       sqlite3ReleaseTempReg(pParse, regTemp);
84187     }else{
84188       int nCol = pFKey->nCol;
84189       int regTemp = sqlite3GetTempRange(pParse, nCol);
84190       int regRec = sqlite3GetTempReg(pParse);
84191       KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx);
84192 
84193       sqlite3VdbeAddOp3(v, OP_OpenRead, iCur, pIdx->tnum, iDb);
84194       sqlite3VdbeChangeP4(v, -1, (char*)pKey, P4_KEYINFO_HANDOFF);
84195       for(i=0; i<nCol; i++){
84196         sqlite3VdbeAddOp2(v, OP_Copy, aiCol[i]+1+regData, regTemp+i);
84197       }
84198 
84199       /* If the parent table is the same as the child table, and we are about
84200       ** to increment the constraint-counter (i.e. this is an INSERT operation),
84201       ** then check if the row being inserted matches itself. If so, do not
84202       ** increment the constraint-counter.
84203       **
84204       ** If any of the parent-key values are NULL, then the row cannot match
84205       ** itself. So set JUMPIFNULL to make sure we do the OP_Found if any
84206       ** of the parent-key values are NULL (at this point it is known that
84207       ** none of the child key values are).
84208       */
84209       if( pTab==pFKey->pFrom && nIncr==1 ){
84210         int iJump = sqlite3VdbeCurrentAddr(v) + nCol + 1;
84211         for(i=0; i<nCol; i++){
84212           int iChild = aiCol[i]+1+regData;
84213           int iParent = pIdx->aiColumn[i]+1+regData;
84214           assert( aiCol[i]!=pTab->iPKey );
84215           if( pIdx->aiColumn[i]==pTab->iPKey ){
84216             /* The parent key is a composite key that includes the IPK column */
84217             iParent = regData;
84218           }
84219           sqlite3VdbeAddOp3(v, OP_Ne, iChild, iJump, iParent);
84220           sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL);
84221         }
84222         sqlite3VdbeAddOp2(v, OP_Goto, 0, iOk);
84223       }
84224 
84225       sqlite3VdbeAddOp3(v, OP_MakeRecord, regTemp, nCol, regRec);
84226       sqlite3VdbeChangeP4(v, -1, sqlite3IndexAffinityStr(v,pIdx), P4_TRANSIENT);
84227       sqlite3VdbeAddOp4Int(v, OP_Found, iCur, iOk, regRec, 0);
84228 
84229       sqlite3ReleaseTempReg(pParse, regRec);
84230       sqlite3ReleaseTempRange(pParse, regTemp, nCol);
84231     }
84232   }
84233 
84234   if( !pFKey->isDeferred && !pParse->pToplevel && !pParse->isMultiWrite ){
84235     /* Special case: If this is an INSERT statement that will insert exactly
84236     ** one row into the table, raise a constraint immediately instead of
84237     ** incrementing a counter. This is necessary as the VM code is being
84238     ** generated for will not open a statement transaction.  */
84239     assert( nIncr==1 );
84240     sqlite3HaltConstraint(
84241         pParse, OE_Abort, "foreign key constraint failed", P4_STATIC
84242     );
84243   }else{
84244     if( nIncr>0 && pFKey->isDeferred==0 ){
84245       sqlite3ParseToplevel(pParse)->mayAbort = 1;
84246     }
84247     sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, nIncr);
84248   }
84249 
84250   sqlite3VdbeResolveLabel(v, iOk);
84251   sqlite3VdbeAddOp1(v, OP_Close, iCur);
84252 }
84253 
84254 /*
84255 ** This function is called to generate code executed when a row is deleted
84256 ** from the parent table of foreign key constraint pFKey and, if pFKey is
84257 ** deferred, when a row is inserted into the same table. When generating
84258 ** code for an SQL UPDATE operation, this function may be called twice -
84259 ** once to "delete" the old row and once to "insert" the new row.
84260 **
84261 ** The code generated by this function scans through the rows in the child
84262 ** table that correspond to the parent table row being deleted or inserted.
84263 ** For each child row found, one of the following actions is taken:
84264 **
84265 **   Operation | FK type   | Action taken
84266 **   --------------------------------------------------------------------------
84267 **   DELETE      immediate   Increment the "immediate constraint counter".
84268 **                           Or, if the ON (UPDATE|DELETE) action is RESTRICT,
84269 **                           throw a "foreign key constraint failed" exception.
84270 **
84271 **   INSERT      immediate   Decrement the "immediate constraint counter".
84272 **
84273 **   DELETE      deferred    Increment the "deferred constraint counter".
84274 **                           Or, if the ON (UPDATE|DELETE) action is RESTRICT,
84275 **                           throw a "foreign key constraint failed" exception.
84276 **
84277 **   INSERT      deferred    Decrement the "deferred constraint counter".
84278 **
84279 ** These operations are identified in the comment at the top of this file
84280 ** (fkey.c) as "I.2" and "D.2".
84281 */
84282 static void fkScanChildren(
84283   Parse *pParse,                  /* Parse context */
84284   SrcList *pSrc,                  /* SrcList containing the table to scan */
84285   Table *pTab,
84286   Index *pIdx,                    /* Foreign key index */
84287   FKey *pFKey,                    /* Foreign key relationship */
84288   int *aiCol,                     /* Map from pIdx cols to child table cols */
84289   int regData,                    /* Referenced table data starts here */
84290   int nIncr                       /* Amount to increment deferred counter by */
84291 ){
84292   sqlite3 *db = pParse->db;       /* Database handle */
84293   int i;                          /* Iterator variable */
84294   Expr *pWhere = 0;               /* WHERE clause to scan with */
84295   NameContext sNameContext;       /* Context used to resolve WHERE clause */
84296   WhereInfo *pWInfo;              /* Context used by sqlite3WhereXXX() */
84297   int iFkIfZero = 0;              /* Address of OP_FkIfZero */
84298   Vdbe *v = sqlite3GetVdbe(pParse);
84299 
84300   assert( !pIdx || pIdx->pTable==pTab );
84301 
84302   if( nIncr<0 ){
84303     iFkIfZero = sqlite3VdbeAddOp2(v, OP_FkIfZero, pFKey->isDeferred, 0);
84304   }
84305 
84306   /* Create an Expr object representing an SQL expression like:
84307   **
84308   **   <parent-key1> = <child-key1> AND <parent-key2> = <child-key2> ...
84309   **
84310   ** The collation sequence used for the comparison should be that of
84311   ** the parent key columns. The affinity of the parent key column should
84312   ** be applied to each child key value before the comparison takes place.
84313   */
84314   for(i=0; i<pFKey->nCol; i++){
84315     Expr *pLeft;                  /* Value from parent table row */
84316     Expr *pRight;                 /* Column ref to child table */
84317     Expr *pEq;                    /* Expression (pLeft = pRight) */
84318     int iCol;                     /* Index of column in child table */
84319     const char *zCol;             /* Name of column in child table */
84320 
84321     pLeft = sqlite3Expr(db, TK_REGISTER, 0);
84322     if( pLeft ){
84323       /* Set the collation sequence and affinity of the LHS of each TK_EQ
84324       ** expression to the parent key column defaults.  */
84325       if( pIdx ){
84326         Column *pCol;
84327         iCol = pIdx->aiColumn[i];
84328         pCol = &pTab->aCol[iCol];
84329         if( pTab->iPKey==iCol ) iCol = -1;
84330         pLeft->iTable = regData+iCol+1;
84331         pLeft->affinity = pCol->affinity;
84332         pLeft->pColl = sqlite3LocateCollSeq(pParse, pCol->zColl);
84333       }else{
84334         pLeft->iTable = regData;
84335         pLeft->affinity = SQLITE_AFF_INTEGER;
84336       }
84337     }
84338     iCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
84339     assert( iCol>=0 );
84340     zCol = pFKey->pFrom->aCol[iCol].zName;
84341     pRight = sqlite3Expr(db, TK_ID, zCol);
84342     pEq = sqlite3PExpr(pParse, TK_EQ, pLeft, pRight, 0);
84343     pWhere = sqlite3ExprAnd(db, pWhere, pEq);
84344   }
84345 
84346   /* If the child table is the same as the parent table, and this scan
84347   ** is taking place as part of a DELETE operation (operation D.2), omit the
84348   ** row being deleted from the scan by adding ($rowid != rowid) to the WHERE
84349   ** clause, where $rowid is the rowid of the row being deleted.  */
84350   if( pTab==pFKey->pFrom && nIncr>0 ){
84351     Expr *pEq;                    /* Expression (pLeft = pRight) */
84352     Expr *pLeft;                  /* Value from parent table row */
84353     Expr *pRight;                 /* Column ref to child table */
84354     pLeft = sqlite3Expr(db, TK_REGISTER, 0);
84355     pRight = sqlite3Expr(db, TK_COLUMN, 0);
84356     if( pLeft && pRight ){
84357       pLeft->iTable = regData;
84358       pLeft->affinity = SQLITE_AFF_INTEGER;
84359       pRight->iTable = pSrc->a[0].iCursor;
84360       pRight->iColumn = -1;
84361     }
84362     pEq = sqlite3PExpr(pParse, TK_NE, pLeft, pRight, 0);
84363     pWhere = sqlite3ExprAnd(db, pWhere, pEq);
84364   }
84365 
84366   /* Resolve the references in the WHERE clause. */
84367   memset(&sNameContext, 0, sizeof(NameContext));
84368   sNameContext.pSrcList = pSrc;
84369   sNameContext.pParse = pParse;
84370   sqlite3ResolveExprNames(&sNameContext, pWhere);
84371 
84372   /* Create VDBE to loop through the entries in pSrc that match the WHERE
84373   ** clause. If the constraint is not deferred, throw an exception for
84374   ** each row found. Otherwise, for deferred constraints, increment the
84375   ** deferred constraint counter by nIncr for each row selected.  */
84376   pWInfo = sqlite3WhereBegin(pParse, pSrc, pWhere, 0, 0);
84377   if( nIncr>0 && pFKey->isDeferred==0 ){
84378     sqlite3ParseToplevel(pParse)->mayAbort = 1;
84379   }
84380   sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, nIncr);
84381   if( pWInfo ){
84382     sqlite3WhereEnd(pWInfo);
84383   }
84384 
84385   /* Clean up the WHERE clause constructed above. */
84386   sqlite3ExprDelete(db, pWhere);
84387   if( iFkIfZero ){
84388     sqlite3VdbeJumpHere(v, iFkIfZero);
84389   }
84390 }
84391 
84392 /*
84393 ** This function returns a pointer to the head of a linked list of FK
84394 ** constraints for which table pTab is the parent table. For example,
84395 ** given the following schema:
84396 **
84397 **   CREATE TABLE t1(a PRIMARY KEY);
84398 **   CREATE TABLE t2(b REFERENCES t1(a);
84399 **
84400 ** Calling this function with table "t1" as an argument returns a pointer
84401 ** to the FKey structure representing the foreign key constraint on table
84402 ** "t2". Calling this function with "t2" as the argument would return a
84403 ** NULL pointer (as there are no FK constraints for which t2 is the parent
84404 ** table).
84405 */
84406 SQLITE_PRIVATE FKey *sqlite3FkReferences(Table *pTab){
84407   int nName = sqlite3Strlen30(pTab->zName);
84408   return (FKey *)sqlite3HashFind(&pTab->pSchema->fkeyHash, pTab->zName, nName);
84409 }
84410 
84411 /*
84412 ** The second argument is a Trigger structure allocated by the
84413 ** fkActionTrigger() routine. This function deletes the Trigger structure
84414 ** and all of its sub-components.
84415 **
84416 ** The Trigger structure or any of its sub-components may be allocated from
84417 ** the lookaside buffer belonging to database handle dbMem.
84418 */
84419 static void fkTriggerDelete(sqlite3 *dbMem, Trigger *p){
84420   if( p ){
84421     TriggerStep *pStep = p->step_list;
84422     sqlite3ExprDelete(dbMem, pStep->pWhere);
84423     sqlite3ExprListDelete(dbMem, pStep->pExprList);
84424     sqlite3SelectDelete(dbMem, pStep->pSelect);
84425     sqlite3ExprDelete(dbMem, p->pWhen);
84426     sqlite3DbFree(dbMem, p);
84427   }
84428 }
84429 
84430 /*
84431 ** This function is called to generate code that runs when table pTab is
84432 ** being dropped from the database. The SrcList passed as the second argument
84433 ** to this function contains a single entry guaranteed to resolve to
84434 ** table pTab.
84435 **
84436 ** Normally, no code is required. However, if either
84437 **
84438 **   (a) The table is the parent table of a FK constraint, or
84439 **   (b) The table is the child table of a deferred FK constraint and it is
84440 **       determined at runtime that there are outstanding deferred FK
84441 **       constraint violations in the database,
84442 **
84443 ** then the equivalent of "DELETE FROM <tbl>" is executed before dropping
84444 ** the table from the database. Triggers are disabled while running this
84445 ** DELETE, but foreign key actions are not.
84446 */
84447 SQLITE_PRIVATE void sqlite3FkDropTable(Parse *pParse, SrcList *pName, Table *pTab){
84448   sqlite3 *db = pParse->db;
84449   if( (db->flags&SQLITE_ForeignKeys) && !IsVirtual(pTab) && !pTab->pSelect ){
84450     int iSkip = 0;
84451     Vdbe *v = sqlite3GetVdbe(pParse);
84452 
84453     assert( v );                  /* VDBE has already been allocated */
84454     if( sqlite3FkReferences(pTab)==0 ){
84455       /* Search for a deferred foreign key constraint for which this table
84456       ** is the child table. If one cannot be found, return without
84457       ** generating any VDBE code. If one can be found, then jump over
84458       ** the entire DELETE if there are no outstanding deferred constraints
84459       ** when this statement is run.  */
84460       FKey *p;
84461       for(p=pTab->pFKey; p; p=p->pNextFrom){
84462         if( p->isDeferred ) break;
84463       }
84464       if( !p ) return;
84465       iSkip = sqlite3VdbeMakeLabel(v);
84466       sqlite3VdbeAddOp2(v, OP_FkIfZero, 1, iSkip);
84467     }
84468 
84469     pParse->disableTriggers = 1;
84470     sqlite3DeleteFrom(pParse, sqlite3SrcListDup(db, pName, 0), 0);
84471     pParse->disableTriggers = 0;
84472 
84473     /* If the DELETE has generated immediate foreign key constraint
84474     ** violations, halt the VDBE and return an error at this point, before
84475     ** any modifications to the schema are made. This is because statement
84476     ** transactions are not able to rollback schema changes.  */
84477     sqlite3VdbeAddOp2(v, OP_FkIfZero, 0, sqlite3VdbeCurrentAddr(v)+2);
84478     sqlite3HaltConstraint(
84479         pParse, OE_Abort, "foreign key constraint failed", P4_STATIC
84480     );
84481 
84482     if( iSkip ){
84483       sqlite3VdbeResolveLabel(v, iSkip);
84484     }
84485   }
84486 }
84487 
84488 /*
84489 ** This function is called when inserting, deleting or updating a row of
84490 ** table pTab to generate VDBE code to perform foreign key constraint
84491 ** processing for the operation.
84492 **
84493 ** For a DELETE operation, parameter regOld is passed the index of the
84494 ** first register in an array of (pTab->nCol+1) registers containing the
84495 ** rowid of the row being deleted, followed by each of the column values
84496 ** of the row being deleted, from left to right. Parameter regNew is passed
84497 ** zero in this case.
84498 **
84499 ** For an INSERT operation, regOld is passed zero and regNew is passed the
84500 ** first register of an array of (pTab->nCol+1) registers containing the new
84501 ** row data.
84502 **
84503 ** For an UPDATE operation, this function is called twice. Once before
84504 ** the original record is deleted from the table using the calling convention
84505 ** described for DELETE. Then again after the original record is deleted
84506 ** but before the new record is inserted using the INSERT convention.
84507 */
84508 SQLITE_PRIVATE void sqlite3FkCheck(
84509   Parse *pParse,                  /* Parse context */
84510   Table *pTab,                    /* Row is being deleted from this table */
84511   int regOld,                     /* Previous row data is stored here */
84512   int regNew                      /* New row data is stored here */
84513 ){
84514   sqlite3 *db = pParse->db;       /* Database handle */
84515   FKey *pFKey;                    /* Used to iterate through FKs */
84516   int iDb;                        /* Index of database containing pTab */
84517   const char *zDb;                /* Name of database containing pTab */
84518   int isIgnoreErrors = pParse->disableTriggers;
84519 
84520   /* Exactly one of regOld and regNew should be non-zero. */
84521   assert( (regOld==0)!=(regNew==0) );
84522 
84523   /* If foreign-keys are disabled, this function is a no-op. */
84524   if( (db->flags&SQLITE_ForeignKeys)==0 ) return;
84525 
84526   iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
84527   zDb = db->aDb[iDb].zName;
84528 
84529   /* Loop through all the foreign key constraints for which pTab is the
84530   ** child table (the table that the foreign key definition is part of).  */
84531   for(pFKey=pTab->pFKey; pFKey; pFKey=pFKey->pNextFrom){
84532     Table *pTo;                   /* Parent table of foreign key pFKey */
84533     Index *pIdx = 0;              /* Index on key columns in pTo */
84534     int *aiFree = 0;
84535     int *aiCol;
84536     int iCol;
84537     int i;
84538     int isIgnore = 0;
84539 
84540     /* Find the parent table of this foreign key. Also find a unique index
84541     ** on the parent key columns in the parent table. If either of these
84542     ** schema items cannot be located, set an error in pParse and return
84543     ** early.  */
84544     if( pParse->disableTriggers ){
84545       pTo = sqlite3FindTable(db, pFKey->zTo, zDb);
84546     }else{
84547       pTo = sqlite3LocateTable(pParse, 0, pFKey->zTo, zDb);
84548     }
84549     if( !pTo || locateFkeyIndex(pParse, pTo, pFKey, &pIdx, &aiFree) ){
84550       if( !isIgnoreErrors || db->mallocFailed ) return;
84551       continue;
84552     }
84553     assert( pFKey->nCol==1 || (aiFree && pIdx) );
84554 
84555     if( aiFree ){
84556       aiCol = aiFree;
84557     }else{
84558       iCol = pFKey->aCol[0].iFrom;
84559       aiCol = &iCol;
84560     }
84561     for(i=0; i<pFKey->nCol; i++){
84562       if( aiCol[i]==pTab->iPKey ){
84563         aiCol[i] = -1;
84564       }
84565 #ifndef SQLITE_OMIT_AUTHORIZATION
84566       /* Request permission to read the parent key columns. If the
84567       ** authorization callback returns SQLITE_IGNORE, behave as if any
84568       ** values read from the parent table are NULL. */
84569       if( db->xAuth ){
84570         int rcauth;
84571         char *zCol = pTo->aCol[pIdx ? pIdx->aiColumn[i] : pTo->iPKey].zName;
84572         rcauth = sqlite3AuthReadCol(pParse, pTo->zName, zCol, iDb);
84573         isIgnore = (rcauth==SQLITE_IGNORE);
84574       }
84575 #endif
84576     }
84577 
84578     /* Take a shared-cache advisory read-lock on the parent table. Allocate
84579     ** a cursor to use to search the unique index on the parent key columns
84580     ** in the parent table.  */
84581     sqlite3TableLock(pParse, iDb, pTo->tnum, 0, pTo->zName);
84582     pParse->nTab++;
84583 
84584     if( regOld!=0 ){
84585       /* A row is being removed from the child table. Search for the parent.
84586       ** If the parent does not exist, removing the child row resolves an
84587       ** outstanding foreign key constraint violation. */
84588       fkLookupParent(pParse, iDb, pTo, pIdx, pFKey, aiCol, regOld, -1,isIgnore);
84589     }
84590     if( regNew!=0 ){
84591       /* A row is being added to the child table. If a parent row cannot
84592       ** be found, adding the child row has violated the FK constraint. */
84593       fkLookupParent(pParse, iDb, pTo, pIdx, pFKey, aiCol, regNew, +1,isIgnore);
84594     }
84595 
84596     sqlite3DbFree(db, aiFree);
84597   }
84598 
84599   /* Loop through all the foreign key constraints that refer to this table */
84600   for(pFKey = sqlite3FkReferences(pTab); pFKey; pFKey=pFKey->pNextTo){
84601     Index *pIdx = 0;              /* Foreign key index for pFKey */
84602     SrcList *pSrc;
84603     int *aiCol = 0;
84604 
84605     if( !pFKey->isDeferred && !pParse->pToplevel && !pParse->isMultiWrite ){
84606       assert( regOld==0 && regNew!=0 );
84607       /* Inserting a single row into a parent table cannot cause an immediate
84608       ** foreign key violation. So do nothing in this case.  */
84609       continue;
84610     }
84611 
84612     if( locateFkeyIndex(pParse, pTab, pFKey, &pIdx, &aiCol) ){
84613       if( !isIgnoreErrors || db->mallocFailed ) return;
84614       continue;
84615     }
84616     assert( aiCol || pFKey->nCol==1 );
84617 
84618     /* Create a SrcList structure containing a single table (the table
84619     ** the foreign key that refers to this table is attached to). This
84620     ** is required for the sqlite3WhereXXX() interface.  */
84621     pSrc = sqlite3SrcListAppend(db, 0, 0, 0);
84622     if( pSrc ){
84623       struct SrcList_item *pItem = pSrc->a;
84624       pItem->pTab = pFKey->pFrom;
84625       pItem->zName = pFKey->pFrom->zName;
84626       pItem->pTab->nRef++;
84627       pItem->iCursor = pParse->nTab++;
84628 
84629       if( regNew!=0 ){
84630         fkScanChildren(pParse, pSrc, pTab, pIdx, pFKey, aiCol, regNew, -1);
84631       }
84632       if( regOld!=0 ){
84633         /* If there is a RESTRICT action configured for the current operation
84634         ** on the parent table of this FK, then throw an exception
84635         ** immediately if the FK constraint is violated, even if this is a
84636         ** deferred trigger. That's what RESTRICT means. To defer checking
84637         ** the constraint, the FK should specify NO ACTION (represented
84638         ** using OE_None). NO ACTION is the default.  */
84639         fkScanChildren(pParse, pSrc, pTab, pIdx, pFKey, aiCol, regOld, 1);
84640       }
84641       pItem->zName = 0;
84642       sqlite3SrcListDelete(db, pSrc);
84643     }
84644     sqlite3DbFree(db, aiCol);
84645   }
84646 }
84647 
84648 #define COLUMN_MASK(x) (((x)>31) ? 0xffffffff : ((u32)1<<(x)))
84649 
84650 /*
84651 ** This function is called before generating code to update or delete a
84652 ** row contained in table pTab.
84653 */
84654 SQLITE_PRIVATE u32 sqlite3FkOldmask(
84655   Parse *pParse,                  /* Parse context */
84656   Table *pTab                     /* Table being modified */
84657 ){
84658   u32 mask = 0;
84659   if( pParse->db->flags&SQLITE_ForeignKeys ){
84660     FKey *p;
84661     int i;
84662     for(p=pTab->pFKey; p; p=p->pNextFrom){
84663       for(i=0; i<p->nCol; i++) mask |= COLUMN_MASK(p->aCol[i].iFrom);
84664     }
84665     for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
84666       Index *pIdx = 0;
84667       locateFkeyIndex(pParse, pTab, p, &pIdx, 0);
84668       if( pIdx ){
84669         for(i=0; i<pIdx->nColumn; i++) mask |= COLUMN_MASK(pIdx->aiColumn[i]);
84670       }
84671     }
84672   }
84673   return mask;
84674 }
84675 
84676 /*
84677 ** This function is called before generating code to update or delete a
84678 ** row contained in table pTab. If the operation is a DELETE, then
84679 ** parameter aChange is passed a NULL value. For an UPDATE, aChange points
84680 ** to an array of size N, where N is the number of columns in table pTab.
84681 ** If the i'th column is not modified by the UPDATE, then the corresponding
84682 ** entry in the aChange[] array is set to -1. If the column is modified,
84683 ** the value is 0 or greater. Parameter chngRowid is set to true if the
84684 ** UPDATE statement modifies the rowid fields of the table.
84685 **
84686 ** If any foreign key processing will be required, this function returns
84687 ** true. If there is no foreign key related processing, this function
84688 ** returns false.
84689 */
84690 SQLITE_PRIVATE int sqlite3FkRequired(
84691   Parse *pParse,                  /* Parse context */
84692   Table *pTab,                    /* Table being modified */
84693   int *aChange,                   /* Non-NULL for UPDATE operations */
84694   int chngRowid                   /* True for UPDATE that affects rowid */
84695 ){
84696   if( pParse->db->flags&SQLITE_ForeignKeys ){
84697     if( !aChange ){
84698       /* A DELETE operation. Foreign key processing is required if the
84699       ** table in question is either the child or parent table for any
84700       ** foreign key constraint.  */
84701       return (sqlite3FkReferences(pTab) || pTab->pFKey);
84702     }else{
84703       /* This is an UPDATE. Foreign key processing is only required if the
84704       ** operation modifies one or more child or parent key columns. */
84705       int i;
84706       FKey *p;
84707 
84708       /* Check if any child key columns are being modified. */
84709       for(p=pTab->pFKey; p; p=p->pNextFrom){
84710         for(i=0; i<p->nCol; i++){
84711           int iChildKey = p->aCol[i].iFrom;
84712           if( aChange[iChildKey]>=0 ) return 1;
84713           if( iChildKey==pTab->iPKey && chngRowid ) return 1;
84714         }
84715       }
84716 
84717       /* Check if any parent key columns are being modified. */
84718       for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
84719         for(i=0; i<p->nCol; i++){
84720           char *zKey = p->aCol[i].zCol;
84721           int iKey;
84722           for(iKey=0; iKey<pTab->nCol; iKey++){
84723             Column *pCol = &pTab->aCol[iKey];
84724             if( (zKey ? !sqlite3StrICmp(pCol->zName, zKey) : pCol->isPrimKey) ){
84725               if( aChange[iKey]>=0 ) return 1;
84726               if( iKey==pTab->iPKey && chngRowid ) return 1;
84727             }
84728           }
84729         }
84730       }
84731     }
84732   }
84733   return 0;
84734 }
84735 
84736 /*
84737 ** This function is called when an UPDATE or DELETE operation is being
84738 ** compiled on table pTab, which is the parent table of foreign-key pFKey.
84739 ** If the current operation is an UPDATE, then the pChanges parameter is
84740 ** passed a pointer to the list of columns being modified. If it is a
84741 ** DELETE, pChanges is passed a NULL pointer.
84742 **
84743 ** It returns a pointer to a Trigger structure containing a trigger
84744 ** equivalent to the ON UPDATE or ON DELETE action specified by pFKey.
84745 ** If the action is "NO ACTION" or "RESTRICT", then a NULL pointer is
84746 ** returned (these actions require no special handling by the triggers
84747 ** sub-system, code for them is created by fkScanChildren()).
84748 **
84749 ** For example, if pFKey is the foreign key and pTab is table "p" in
84750 ** the following schema:
84751 **
84752 **   CREATE TABLE p(pk PRIMARY KEY);
84753 **   CREATE TABLE c(ck REFERENCES p ON DELETE CASCADE);
84754 **
84755 ** then the returned trigger structure is equivalent to:
84756 **
84757 **   CREATE TRIGGER ... DELETE ON p BEGIN
84758 **     DELETE FROM c WHERE ck = old.pk;
84759 **   END;
84760 **
84761 ** The returned pointer is cached as part of the foreign key object. It
84762 ** is eventually freed along with the rest of the foreign key object by
84763 ** sqlite3FkDelete().
84764 */
84765 static Trigger *fkActionTrigger(
84766   Parse *pParse,                  /* Parse context */
84767   Table *pTab,                    /* Table being updated or deleted from */
84768   FKey *pFKey,                    /* Foreign key to get action for */
84769   ExprList *pChanges              /* Change-list for UPDATE, NULL for DELETE */
84770 ){
84771   sqlite3 *db = pParse->db;       /* Database handle */
84772   int action;                     /* One of OE_None, OE_Cascade etc. */
84773   Trigger *pTrigger;              /* Trigger definition to return */
84774   int iAction = (pChanges!=0);    /* 1 for UPDATE, 0 for DELETE */
84775 
84776   action = pFKey->aAction[iAction];
84777   pTrigger = pFKey->apTrigger[iAction];
84778 
84779   if( action!=OE_None && !pTrigger ){
84780     u8 enableLookaside;           /* Copy of db->lookaside.bEnabled */
84781     char const *zFrom;            /* Name of child table */
84782     int nFrom;                    /* Length in bytes of zFrom */
84783     Index *pIdx = 0;              /* Parent key index for this FK */
84784     int *aiCol = 0;               /* child table cols -> parent key cols */
84785     TriggerStep *pStep = 0;        /* First (only) step of trigger program */
84786     Expr *pWhere = 0;             /* WHERE clause of trigger step */
84787     ExprList *pList = 0;          /* Changes list if ON UPDATE CASCADE */
84788     Select *pSelect = 0;          /* If RESTRICT, "SELECT RAISE(...)" */
84789     int i;                        /* Iterator variable */
84790     Expr *pWhen = 0;              /* WHEN clause for the trigger */
84791 
84792     if( locateFkeyIndex(pParse, pTab, pFKey, &pIdx, &aiCol) ) return 0;
84793     assert( aiCol || pFKey->nCol==1 );
84794 
84795     for(i=0; i<pFKey->nCol; i++){
84796       Token tOld = { "old", 3 };  /* Literal "old" token */
84797       Token tNew = { "new", 3 };  /* Literal "new" token */
84798       Token tFromCol;             /* Name of column in child table */
84799       Token tToCol;               /* Name of column in parent table */
84800       int iFromCol;               /* Idx of column in child table */
84801       Expr *pEq;                  /* tFromCol = OLD.tToCol */
84802 
84803       iFromCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
84804       assert( iFromCol>=0 );
84805       tToCol.z = pIdx ? pTab->aCol[pIdx->aiColumn[i]].zName : "oid";
84806       tFromCol.z = pFKey->pFrom->aCol[iFromCol].zName;
84807 
84808       tToCol.n = sqlite3Strlen30(tToCol.z);
84809       tFromCol.n = sqlite3Strlen30(tFromCol.z);
84810 
84811       /* Create the expression "OLD.zToCol = zFromCol". It is important
84812       ** that the "OLD.zToCol" term is on the LHS of the = operator, so
84813       ** that the affinity and collation sequence associated with the
84814       ** parent table are used for the comparison. */
84815       pEq = sqlite3PExpr(pParse, TK_EQ,
84816           sqlite3PExpr(pParse, TK_DOT,
84817             sqlite3PExpr(pParse, TK_ID, 0, 0, &tOld),
84818             sqlite3PExpr(pParse, TK_ID, 0, 0, &tToCol)
84819           , 0),
84820           sqlite3PExpr(pParse, TK_ID, 0, 0, &tFromCol)
84821       , 0);
84822       pWhere = sqlite3ExprAnd(db, pWhere, pEq);
84823 
84824       /* For ON UPDATE, construct the next term of the WHEN clause.
84825       ** The final WHEN clause will be like this:
84826       **
84827       **    WHEN NOT(old.col1 IS new.col1 AND ... AND old.colN IS new.colN)
84828       */
84829       if( pChanges ){
84830         pEq = sqlite3PExpr(pParse, TK_IS,
84831             sqlite3PExpr(pParse, TK_DOT,
84832               sqlite3PExpr(pParse, TK_ID, 0, 0, &tOld),
84833               sqlite3PExpr(pParse, TK_ID, 0, 0, &tToCol),
84834               0),
84835             sqlite3PExpr(pParse, TK_DOT,
84836               sqlite3PExpr(pParse, TK_ID, 0, 0, &tNew),
84837               sqlite3PExpr(pParse, TK_ID, 0, 0, &tToCol),
84838               0),
84839             0);
84840         pWhen = sqlite3ExprAnd(db, pWhen, pEq);
84841       }
84842 
84843       if( action!=OE_Restrict && (action!=OE_Cascade || pChanges) ){
84844         Expr *pNew;
84845         if( action==OE_Cascade ){
84846           pNew = sqlite3PExpr(pParse, TK_DOT,
84847             sqlite3PExpr(pParse, TK_ID, 0, 0, &tNew),
84848             sqlite3PExpr(pParse, TK_ID, 0, 0, &tToCol)
84849           , 0);
84850         }else if( action==OE_SetDflt ){
84851           Expr *pDflt = pFKey->pFrom->aCol[iFromCol].pDflt;
84852           if( pDflt ){
84853             pNew = sqlite3ExprDup(db, pDflt, 0);
84854           }else{
84855             pNew = sqlite3PExpr(pParse, TK_NULL, 0, 0, 0);
84856           }
84857         }else{
84858           pNew = sqlite3PExpr(pParse, TK_NULL, 0, 0, 0);
84859         }
84860         pList = sqlite3ExprListAppend(pParse, pList, pNew);
84861         sqlite3ExprListSetName(pParse, pList, &tFromCol, 0);
84862       }
84863     }
84864     sqlite3DbFree(db, aiCol);
84865 
84866     zFrom = pFKey->pFrom->zName;
84867     nFrom = sqlite3Strlen30(zFrom);
84868 
84869     if( action==OE_Restrict ){
84870       Token tFrom;
84871       Expr *pRaise;
84872 
84873       tFrom.z = zFrom;
84874       tFrom.n = nFrom;
84875       pRaise = sqlite3Expr(db, TK_RAISE, "foreign key constraint failed");
84876       if( pRaise ){
84877         pRaise->affinity = OE_Abort;
84878       }
84879       pSelect = sqlite3SelectNew(pParse,
84880           sqlite3ExprListAppend(pParse, 0, pRaise),
84881           sqlite3SrcListAppend(db, 0, &tFrom, 0),
84882           pWhere,
84883           0, 0, 0, 0, 0, 0
84884       );
84885       pWhere = 0;
84886     }
84887 
84888     /* Disable lookaside memory allocation */
84889     enableLookaside = db->lookaside.bEnabled;
84890     db->lookaside.bEnabled = 0;
84891 
84892     pTrigger = (Trigger *)sqlite3DbMallocZero(db,
84893         sizeof(Trigger) +         /* struct Trigger */
84894         sizeof(TriggerStep) +     /* Single step in trigger program */
84895         nFrom + 1                 /* Space for pStep->target.z */
84896     );
84897     if( pTrigger ){
84898       pStep = pTrigger->step_list = (TriggerStep *)&pTrigger[1];
84899       pStep->target.z = (char *)&pStep[1];
84900       pStep->target.n = nFrom;
84901       memcpy((char *)pStep->target.z, zFrom, nFrom);
84902 
84903       pStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
84904       pStep->pExprList = sqlite3ExprListDup(db, pList, EXPRDUP_REDUCE);
84905       pStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
84906       if( pWhen ){
84907         pWhen = sqlite3PExpr(pParse, TK_NOT, pWhen, 0, 0);
84908         pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
84909       }
84910     }
84911 
84912     /* Re-enable the lookaside buffer, if it was disabled earlier. */
84913     db->lookaside.bEnabled = enableLookaside;
84914 
84915     sqlite3ExprDelete(db, pWhere);
84916     sqlite3ExprDelete(db, pWhen);
84917     sqlite3ExprListDelete(db, pList);
84918     sqlite3SelectDelete(db, pSelect);
84919     if( db->mallocFailed==1 ){
84920       fkTriggerDelete(db, pTrigger);
84921       return 0;
84922     }
84923 
84924     switch( action ){
84925       case OE_Restrict:
84926         pStep->op = TK_SELECT;
84927         break;
84928       case OE_Cascade:
84929         if( !pChanges ){
84930           pStep->op = TK_DELETE;
84931           break;
84932         }
84933       default:
84934         pStep->op = TK_UPDATE;
84935     }
84936     pStep->pTrig = pTrigger;
84937     pTrigger->pSchema = pTab->pSchema;
84938     pTrigger->pTabSchema = pTab->pSchema;
84939     pFKey->apTrigger[iAction] = pTrigger;
84940     pTrigger->op = (pChanges ? TK_UPDATE : TK_DELETE);
84941   }
84942 
84943   return pTrigger;
84944 }
84945 
84946 /*
84947 ** This function is called when deleting or updating a row to implement
84948 ** any required CASCADE, SET NULL or SET DEFAULT actions.
84949 */
84950 SQLITE_PRIVATE void sqlite3FkActions(
84951   Parse *pParse,                  /* Parse context */
84952   Table *pTab,                    /* Table being updated or deleted from */
84953   ExprList *pChanges,             /* Change-list for UPDATE, NULL for DELETE */
84954   int regOld                      /* Address of array containing old row */
84955 ){
84956   /* If foreign-key support is enabled, iterate through all FKs that
84957   ** refer to table pTab. If there is an action associated with the FK
84958   ** for this operation (either update or delete), invoke the associated
84959   ** trigger sub-program.  */
84960   if( pParse->db->flags&SQLITE_ForeignKeys ){
84961     FKey *pFKey;                  /* Iterator variable */
84962     for(pFKey = sqlite3FkReferences(pTab); pFKey; pFKey=pFKey->pNextTo){
84963       Trigger *pAction = fkActionTrigger(pParse, pTab, pFKey, pChanges);
84964       if( pAction ){
84965         sqlite3CodeRowTriggerDirect(pParse, pAction, pTab, regOld, OE_Abort, 0);
84966       }
84967     }
84968   }
84969 }
84970 
84971 #endif /* ifndef SQLITE_OMIT_TRIGGER */
84972 
84973 /*
84974 ** Free all memory associated with foreign key definitions attached to
84975 ** table pTab. Remove the deleted foreign keys from the Schema.fkeyHash
84976 ** hash table.
84977 */
84978 SQLITE_PRIVATE void sqlite3FkDelete(sqlite3 *db, Table *pTab){
84979   FKey *pFKey;                    /* Iterator variable */
84980   FKey *pNext;                    /* Copy of pFKey->pNextFrom */
84981 
84982   assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pTab->pSchema) );
84983   for(pFKey=pTab->pFKey; pFKey; pFKey=pNext){
84984 
84985     /* Remove the FK from the fkeyHash hash table. */
84986     if( !db || db->pnBytesFreed==0 ){
84987       if( pFKey->pPrevTo ){
84988         pFKey->pPrevTo->pNextTo = pFKey->pNextTo;
84989       }else{
84990         void *p = (void *)pFKey->pNextTo;
84991         const char *z = (p ? pFKey->pNextTo->zTo : pFKey->zTo);
84992         sqlite3HashInsert(&pTab->pSchema->fkeyHash, z, sqlite3Strlen30(z), p);
84993       }
84994       if( pFKey->pNextTo ){
84995         pFKey->pNextTo->pPrevTo = pFKey->pPrevTo;
84996       }
84997     }
84998 
84999     /* EV: R-30323-21917 Each foreign key constraint in SQLite is
85000     ** classified as either immediate or deferred.
85001     */
85002     assert( pFKey->isDeferred==0 || pFKey->isDeferred==1 );
85003 
85004     /* Delete any triggers created to implement actions for this FK. */
85005 #ifndef SQLITE_OMIT_TRIGGER
85006     fkTriggerDelete(db, pFKey->apTrigger[0]);
85007     fkTriggerDelete(db, pFKey->apTrigger[1]);
85008 #endif
85009 
85010     pNext = pFKey->pNextFrom;
85011     sqlite3DbFree(db, pFKey);
85012   }
85013 }
85014 #endif /* ifndef SQLITE_OMIT_FOREIGN_KEY */
85015 
85016 /************** End of fkey.c ************************************************/
85017 /************** Begin file insert.c ******************************************/
85018 /*
85019 ** 2001 September 15
85020 **
85021 ** The author disclaims copyright to this source code.  In place of
85022 ** a legal notice, here is a blessing:
85023 **
85024 **    May you do good and not evil.
85025 **    May you find forgiveness for yourself and forgive others.
85026 **    May you share freely, never taking more than you give.
85027 **
85028 *************************************************************************
85029 ** This file contains C code routines that are called by the parser
85030 ** to handle INSERT statements in SQLite.
85031 */
85032 
85033 /*
85034 ** Generate code that will open a table for reading.
85035 */
85036 SQLITE_PRIVATE void sqlite3OpenTable(
85037   Parse *p,       /* Generate code into this VDBE */
85038   int iCur,       /* The cursor number of the table */
85039   int iDb,        /* The database index in sqlite3.aDb[] */
85040   Table *pTab,    /* The table to be opened */
85041   int opcode      /* OP_OpenRead or OP_OpenWrite */
85042 ){
85043   Vdbe *v;
85044   if( IsVirtual(pTab) ) return;
85045   v = sqlite3GetVdbe(p);
85046   assert( opcode==OP_OpenWrite || opcode==OP_OpenRead );
85047   sqlite3TableLock(p, iDb, pTab->tnum, (opcode==OP_OpenWrite)?1:0, pTab->zName);
85048   sqlite3VdbeAddOp3(v, opcode, iCur, pTab->tnum, iDb);
85049   sqlite3VdbeChangeP4(v, -1, SQLITE_INT_TO_PTR(pTab->nCol), P4_INT32);
85050   VdbeComment((v, "%s", pTab->zName));
85051 }
85052 
85053 /*
85054 ** Return a pointer to the column affinity string associated with index
85055 ** pIdx. A column affinity string has one character for each column in
85056 ** the table, according to the affinity of the column:
85057 **
85058 **  Character      Column affinity
85059 **  ------------------------------
85060 **  'a'            TEXT
85061 **  'b'            NONE
85062 **  'c'            NUMERIC
85063 **  'd'            INTEGER
85064 **  'e'            REAL
85065 **
85066 ** An extra 'b' is appended to the end of the string to cover the
85067 ** rowid that appears as the last column in every index.
85068 **
85069 ** Memory for the buffer containing the column index affinity string
85070 ** is managed along with the rest of the Index structure. It will be
85071 ** released when sqlite3DeleteIndex() is called.
85072 */
85073 SQLITE_PRIVATE const char *sqlite3IndexAffinityStr(Vdbe *v, Index *pIdx){
85074   if( !pIdx->zColAff ){
85075     /* The first time a column affinity string for a particular index is
85076     ** required, it is allocated and populated here. It is then stored as
85077     ** a member of the Index structure for subsequent use.
85078     **
85079     ** The column affinity string will eventually be deleted by
85080     ** sqliteDeleteIndex() when the Index structure itself is cleaned
85081     ** up.
85082     */
85083     int n;
85084     Table *pTab = pIdx->pTable;
85085     sqlite3 *db = sqlite3VdbeDb(v);
85086     pIdx->zColAff = (char *)sqlite3DbMallocRaw(0, pIdx->nColumn+2);
85087     if( !pIdx->zColAff ){
85088       db->mallocFailed = 1;
85089       return 0;
85090     }
85091     for(n=0; n<pIdx->nColumn; n++){
85092       pIdx->zColAff[n] = pTab->aCol[pIdx->aiColumn[n]].affinity;
85093     }
85094     pIdx->zColAff[n++] = SQLITE_AFF_NONE;
85095     pIdx->zColAff[n] = 0;
85096   }
85097 
85098   return pIdx->zColAff;
85099 }
85100 
85101 /*
85102 ** Set P4 of the most recently inserted opcode to a column affinity
85103 ** string for table pTab. A column affinity string has one character
85104 ** for each column indexed by the index, according to the affinity of the
85105 ** column:
85106 **
85107 **  Character      Column affinity
85108 **  ------------------------------
85109 **  'a'            TEXT
85110 **  'b'            NONE
85111 **  'c'            NUMERIC
85112 **  'd'            INTEGER
85113 **  'e'            REAL
85114 */
85115 SQLITE_PRIVATE void sqlite3TableAffinityStr(Vdbe *v, Table *pTab){
85116   /* The first time a column affinity string for a particular table
85117   ** is required, it is allocated and populated here. It is then
85118   ** stored as a member of the Table structure for subsequent use.
85119   **
85120   ** The column affinity string will eventually be deleted by
85121   ** sqlite3DeleteTable() when the Table structure itself is cleaned up.
85122   */
85123   if( !pTab->zColAff ){
85124     char *zColAff;
85125     int i;
85126     sqlite3 *db = sqlite3VdbeDb(v);
85127 
85128     zColAff = (char *)sqlite3DbMallocRaw(0, pTab->nCol+1);
85129     if( !zColAff ){
85130       db->mallocFailed = 1;
85131       return;
85132     }
85133 
85134     for(i=0; i<pTab->nCol; i++){
85135       zColAff[i] = pTab->aCol[i].affinity;
85136     }
85137     zColAff[pTab->nCol] = '\0';
85138 
85139     pTab->zColAff = zColAff;
85140   }
85141 
85142   sqlite3VdbeChangeP4(v, -1, pTab->zColAff, P4_TRANSIENT);
85143 }
85144 
85145 /*
85146 ** Return non-zero if the table pTab in database iDb or any of its indices
85147 ** have been opened at any point in the VDBE program beginning at location
85148 ** iStartAddr throught the end of the program.  This is used to see if
85149 ** a statement of the form  "INSERT INTO <iDb, pTab> SELECT ..." can
85150 ** run without using temporary table for the results of the SELECT.
85151 */
85152 static int readsTable(Parse *p, int iStartAddr, int iDb, Table *pTab){
85153   Vdbe *v = sqlite3GetVdbe(p);
85154   int i;
85155   int iEnd = sqlite3VdbeCurrentAddr(v);
85156 #ifndef SQLITE_OMIT_VIRTUALTABLE
85157   VTable *pVTab = IsVirtual(pTab) ? sqlite3GetVTable(p->db, pTab) : 0;
85158 #endif
85159 
85160   for(i=iStartAddr; i<iEnd; i++){
85161     VdbeOp *pOp = sqlite3VdbeGetOp(v, i);
85162     assert( pOp!=0 );
85163     if( pOp->opcode==OP_OpenRead && pOp->p3==iDb ){
85164       Index *pIndex;
85165       int tnum = pOp->p2;
85166       if( tnum==pTab->tnum ){
85167         return 1;
85168       }
85169       for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){
85170         if( tnum==pIndex->tnum ){
85171           return 1;
85172         }
85173       }
85174     }
85175 #ifndef SQLITE_OMIT_VIRTUALTABLE
85176     if( pOp->opcode==OP_VOpen && pOp->p4.pVtab==pVTab ){
85177       assert( pOp->p4.pVtab!=0 );
85178       assert( pOp->p4type==P4_VTAB );
85179       return 1;
85180     }
85181 #endif
85182   }
85183   return 0;
85184 }
85185 
85186 #ifndef SQLITE_OMIT_AUTOINCREMENT
85187 /*
85188 ** Locate or create an AutoincInfo structure associated with table pTab
85189 ** which is in database iDb.  Return the register number for the register
85190 ** that holds the maximum rowid.
85191 **
85192 ** There is at most one AutoincInfo structure per table even if the
85193 ** same table is autoincremented multiple times due to inserts within
85194 ** triggers.  A new AutoincInfo structure is created if this is the
85195 ** first use of table pTab.  On 2nd and subsequent uses, the original
85196 ** AutoincInfo structure is used.
85197 **
85198 ** Three memory locations are allocated:
85199 **
85200 **   (1)  Register to hold the name of the pTab table.
85201 **   (2)  Register to hold the maximum ROWID of pTab.
85202 **   (3)  Register to hold the rowid in sqlite_sequence of pTab
85203 **
85204 ** The 2nd register is the one that is returned.  That is all the
85205 ** insert routine needs to know about.
85206 */
85207 static int autoIncBegin(
85208   Parse *pParse,      /* Parsing context */
85209   int iDb,            /* Index of the database holding pTab */
85210   Table *pTab         /* The table we are writing to */
85211 ){
85212   int memId = 0;      /* Register holding maximum rowid */
85213   if( pTab->tabFlags & TF_Autoincrement ){
85214     Parse *pToplevel = sqlite3ParseToplevel(pParse);
85215     AutoincInfo *pInfo;
85216 
85217     pInfo = pToplevel->pAinc;
85218     while( pInfo && pInfo->pTab!=pTab ){ pInfo = pInfo->pNext; }
85219     if( pInfo==0 ){
85220       pInfo = sqlite3DbMallocRaw(pParse->db, sizeof(*pInfo));
85221       if( pInfo==0 ) return 0;
85222       pInfo->pNext = pToplevel->pAinc;
85223       pToplevel->pAinc = pInfo;
85224       pInfo->pTab = pTab;
85225       pInfo->iDb = iDb;
85226       pToplevel->nMem++;                  /* Register to hold name of table */
85227       pInfo->regCtr = ++pToplevel->nMem;  /* Max rowid register */
85228       pToplevel->nMem++;                  /* Rowid in sqlite_sequence */
85229     }
85230     memId = pInfo->regCtr;
85231   }
85232   return memId;
85233 }
85234 
85235 /*
85236 ** This routine generates code that will initialize all of the
85237 ** register used by the autoincrement tracker.
85238 */
85239 SQLITE_PRIVATE void sqlite3AutoincrementBegin(Parse *pParse){
85240   AutoincInfo *p;            /* Information about an AUTOINCREMENT */
85241   sqlite3 *db = pParse->db;  /* The database connection */
85242   Db *pDb;                   /* Database only autoinc table */
85243   int memId;                 /* Register holding max rowid */
85244   int addr;                  /* A VDBE address */
85245   Vdbe *v = pParse->pVdbe;   /* VDBE under construction */
85246 
85247   /* This routine is never called during trigger-generation.  It is
85248   ** only called from the top-level */
85249   assert( pParse->pTriggerTab==0 );
85250   assert( pParse==sqlite3ParseToplevel(pParse) );
85251 
85252   assert( v );   /* We failed long ago if this is not so */
85253   for(p = pParse->pAinc; p; p = p->pNext){
85254     pDb = &db->aDb[p->iDb];
85255     memId = p->regCtr;
85256     assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
85257     sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenRead);
85258     addr = sqlite3VdbeCurrentAddr(v);
85259     sqlite3VdbeAddOp4(v, OP_String8, 0, memId-1, 0, p->pTab->zName, 0);
85260     sqlite3VdbeAddOp2(v, OP_Rewind, 0, addr+9);
85261     sqlite3VdbeAddOp3(v, OP_Column, 0, 0, memId);
85262     sqlite3VdbeAddOp3(v, OP_Ne, memId-1, addr+7, memId);
85263     sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL);
85264     sqlite3VdbeAddOp2(v, OP_Rowid, 0, memId+1);
85265     sqlite3VdbeAddOp3(v, OP_Column, 0, 1, memId);
85266     sqlite3VdbeAddOp2(v, OP_Goto, 0, addr+9);
85267     sqlite3VdbeAddOp2(v, OP_Next, 0, addr+2);
85268     sqlite3VdbeAddOp2(v, OP_Integer, 0, memId);
85269     sqlite3VdbeAddOp0(v, OP_Close);
85270   }
85271 }
85272 
85273 /*
85274 ** Update the maximum rowid for an autoincrement calculation.
85275 **
85276 ** This routine should be called when the top of the stack holds a
85277 ** new rowid that is about to be inserted.  If that new rowid is
85278 ** larger than the maximum rowid in the memId memory cell, then the
85279 ** memory cell is updated.  The stack is unchanged.
85280 */
85281 static void autoIncStep(Parse *pParse, int memId, int regRowid){
85282   if( memId>0 ){
85283     sqlite3VdbeAddOp2(pParse->pVdbe, OP_MemMax, memId, regRowid);
85284   }
85285 }
85286 
85287 /*
85288 ** This routine generates the code needed to write autoincrement
85289 ** maximum rowid values back into the sqlite_sequence register.
85290 ** Every statement that might do an INSERT into an autoincrement
85291 ** table (either directly or through triggers) needs to call this
85292 ** routine just before the "exit" code.
85293 */
85294 SQLITE_PRIVATE void sqlite3AutoincrementEnd(Parse *pParse){
85295   AutoincInfo *p;
85296   Vdbe *v = pParse->pVdbe;
85297   sqlite3 *db = pParse->db;
85298 
85299   assert( v );
85300   for(p = pParse->pAinc; p; p = p->pNext){
85301     Db *pDb = &db->aDb[p->iDb];
85302     int j1, j2, j3, j4, j5;
85303     int iRec;
85304     int memId = p->regCtr;
85305 
85306     iRec = sqlite3GetTempReg(pParse);
85307     assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
85308     sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenWrite);
85309     j1 = sqlite3VdbeAddOp1(v, OP_NotNull, memId+1);
85310     j2 = sqlite3VdbeAddOp0(v, OP_Rewind);
85311     j3 = sqlite3VdbeAddOp3(v, OP_Column, 0, 0, iRec);
85312     j4 = sqlite3VdbeAddOp3(v, OP_Eq, memId-1, 0, iRec);
85313     sqlite3VdbeAddOp2(v, OP_Next, 0, j3);
85314     sqlite3VdbeJumpHere(v, j2);
85315     sqlite3VdbeAddOp2(v, OP_NewRowid, 0, memId+1);
85316     j5 = sqlite3VdbeAddOp0(v, OP_Goto);
85317     sqlite3VdbeJumpHere(v, j4);
85318     sqlite3VdbeAddOp2(v, OP_Rowid, 0, memId+1);
85319     sqlite3VdbeJumpHere(v, j1);
85320     sqlite3VdbeJumpHere(v, j5);
85321     sqlite3VdbeAddOp3(v, OP_MakeRecord, memId-1, 2, iRec);
85322     sqlite3VdbeAddOp3(v, OP_Insert, 0, iRec, memId+1);
85323     sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
85324     sqlite3VdbeAddOp0(v, OP_Close);
85325     sqlite3ReleaseTempReg(pParse, iRec);
85326   }
85327 }
85328 #else
85329 /*
85330 ** If SQLITE_OMIT_AUTOINCREMENT is defined, then the three routines
85331 ** above are all no-ops
85332 */
85333 # define autoIncBegin(A,B,C) (0)
85334 # define autoIncStep(A,B,C)
85335 #endif /* SQLITE_OMIT_AUTOINCREMENT */
85336 
85337 
85338 /* Forward declaration */
85339 static int xferOptimization(
85340   Parse *pParse,        /* Parser context */
85341   Table *pDest,         /* The table we are inserting into */
85342   Select *pSelect,      /* A SELECT statement to use as the data source */
85343   int onError,          /* How to handle constraint errors */
85344   int iDbDest           /* The database of pDest */
85345 );
85346 
85347 /*
85348 ** This routine is call to handle SQL of the following forms:
85349 **
85350 **    insert into TABLE (IDLIST) values(EXPRLIST)
85351 **    insert into TABLE (IDLIST) select
85352 **
85353 ** The IDLIST following the table name is always optional.  If omitted,
85354 ** then a list of all columns for the table is substituted.  The IDLIST
85355 ** appears in the pColumn parameter.  pColumn is NULL if IDLIST is omitted.
85356 **
85357 ** The pList parameter holds EXPRLIST in the first form of the INSERT
85358 ** statement above, and pSelect is NULL.  For the second form, pList is
85359 ** NULL and pSelect is a pointer to the select statement used to generate
85360 ** data for the insert.
85361 **
85362 ** The code generated follows one of four templates.  For a simple
85363 ** select with data coming from a VALUES clause, the code executes
85364 ** once straight down through.  Pseudo-code follows (we call this
85365 ** the "1st template"):
85366 **
85367 **         open write cursor to <table> and its indices
85368 **         puts VALUES clause expressions onto the stack
85369 **         write the resulting record into <table>
85370 **         cleanup
85371 **
85372 ** The three remaining templates assume the statement is of the form
85373 **
85374 **   INSERT INTO <table> SELECT ...
85375 **
85376 ** If the SELECT clause is of the restricted form "SELECT * FROM <table2>" -
85377 ** in other words if the SELECT pulls all columns from a single table
85378 ** and there is no WHERE or LIMIT or GROUP BY or ORDER BY clauses, and
85379 ** if <table2> and <table1> are distinct tables but have identical
85380 ** schemas, including all the same indices, then a special optimization
85381 ** is invoked that copies raw records from <table2> over to <table1>.
85382 ** See the xferOptimization() function for the implementation of this
85383 ** template.  This is the 2nd template.
85384 **
85385 **         open a write cursor to <table>
85386 **         open read cursor on <table2>
85387 **         transfer all records in <table2> over to <table>
85388 **         close cursors
85389 **         foreach index on <table>
85390 **           open a write cursor on the <table> index
85391 **           open a read cursor on the corresponding <table2> index
85392 **           transfer all records from the read to the write cursors
85393 **           close cursors
85394 **         end foreach
85395 **
85396 ** The 3rd template is for when the second template does not apply
85397 ** and the SELECT clause does not read from <table> at any time.
85398 ** The generated code follows this template:
85399 **
85400 **         EOF <- 0
85401 **         X <- A
85402 **         goto B
85403 **      A: setup for the SELECT
85404 **         loop over the rows in the SELECT
85405 **           load values into registers R..R+n
85406 **           yield X
85407 **         end loop
85408 **         cleanup after the SELECT
85409 **         EOF <- 1
85410 **         yield X
85411 **         goto A
85412 **      B: open write cursor to <table> and its indices
85413 **      C: yield X
85414 **         if EOF goto D
85415 **         insert the select result into <table> from R..R+n
85416 **         goto C
85417 **      D: cleanup
85418 **
85419 ** The 4th template is used if the insert statement takes its
85420 ** values from a SELECT but the data is being inserted into a table
85421 ** that is also read as part of the SELECT.  In the third form,
85422 ** we have to use a intermediate table to store the results of
85423 ** the select.  The template is like this:
85424 **
85425 **         EOF <- 0
85426 **         X <- A
85427 **         goto B
85428 **      A: setup for the SELECT
85429 **         loop over the tables in the SELECT
85430 **           load value into register R..R+n
85431 **           yield X
85432 **         end loop
85433 **         cleanup after the SELECT
85434 **         EOF <- 1
85435 **         yield X
85436 **         halt-error
85437 **      B: open temp table
85438 **      L: yield X
85439 **         if EOF goto M
85440 **         insert row from R..R+n into temp table
85441 **         goto L
85442 **      M: open write cursor to <table> and its indices
85443 **         rewind temp table
85444 **      C: loop over rows of intermediate table
85445 **           transfer values form intermediate table into <table>
85446 **         end loop
85447 **      D: cleanup
85448 */
85449 SQLITE_PRIVATE void sqlite3Insert(
85450   Parse *pParse,        /* Parser context */
85451   SrcList *pTabList,    /* Name of table into which we are inserting */
85452   ExprList *pList,      /* List of values to be inserted */
85453   Select *pSelect,      /* A SELECT statement to use as the data source */
85454   IdList *pColumn,      /* Column names corresponding to IDLIST. */
85455   int onError           /* How to handle constraint errors */
85456 ){
85457   sqlite3 *db;          /* The main database structure */
85458   Table *pTab;          /* The table to insert into.  aka TABLE */
85459   char *zTab;           /* Name of the table into which we are inserting */
85460   const char *zDb;      /* Name of the database holding this table */
85461   int i, j, idx;        /* Loop counters */
85462   Vdbe *v;              /* Generate code into this virtual machine */
85463   Index *pIdx;          /* For looping over indices of the table */
85464   int nColumn;          /* Number of columns in the data */
85465   int nHidden = 0;      /* Number of hidden columns if TABLE is virtual */
85466   int baseCur = 0;      /* VDBE Cursor number for pTab */
85467   int keyColumn = -1;   /* Column that is the INTEGER PRIMARY KEY */
85468   int endOfLoop;        /* Label for the end of the insertion loop */
85469   int useTempTable = 0; /* Store SELECT results in intermediate table */
85470   int srcTab = 0;       /* Data comes from this temporary cursor if >=0 */
85471   int addrInsTop = 0;   /* Jump to label "D" */
85472   int addrCont = 0;     /* Top of insert loop. Label "C" in templates 3 and 4 */
85473   int addrSelect = 0;   /* Address of coroutine that implements the SELECT */
85474   SelectDest dest;      /* Destination for SELECT on rhs of INSERT */
85475   int iDb;              /* Index of database holding TABLE */
85476   Db *pDb;              /* The database containing table being inserted into */
85477   int appendFlag = 0;   /* True if the insert is likely to be an append */
85478 
85479   /* Register allocations */
85480   int regFromSelect = 0;/* Base register for data coming from SELECT */
85481   int regAutoinc = 0;   /* Register holding the AUTOINCREMENT counter */
85482   int regRowCount = 0;  /* Memory cell used for the row counter */
85483   int regIns;           /* Block of regs holding rowid+data being inserted */
85484   int regRowid;         /* registers holding insert rowid */
85485   int regData;          /* register holding first column to insert */
85486   int regEof = 0;       /* Register recording end of SELECT data */
85487   int *aRegIdx = 0;     /* One register allocated to each index */
85488 
85489 #ifndef SQLITE_OMIT_TRIGGER
85490   int isView;                 /* True if attempting to insert into a view */
85491   Trigger *pTrigger;          /* List of triggers on pTab, if required */
85492   int tmask;                  /* Mask of trigger times */
85493 #endif
85494 
85495   db = pParse->db;
85496   memset(&dest, 0, sizeof(dest));
85497   if( pParse->nErr || db->mallocFailed ){
85498     goto insert_cleanup;
85499   }
85500 
85501   /* Locate the table into which we will be inserting new information.
85502   */
85503   assert( pTabList->nSrc==1 );
85504   zTab = pTabList->a[0].zName;
85505   if( NEVER(zTab==0) ) goto insert_cleanup;
85506   pTab = sqlite3SrcListLookup(pParse, pTabList);
85507   if( pTab==0 ){
85508     goto insert_cleanup;
85509   }
85510   iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
85511   assert( iDb<db->nDb );
85512   pDb = &db->aDb[iDb];
85513   zDb = pDb->zName;
85514   if( sqlite3AuthCheck(pParse, SQLITE_INSERT, pTab->zName, 0, zDb) ){
85515     goto insert_cleanup;
85516   }
85517 
85518   /* Figure out if we have any triggers and if the table being
85519   ** inserted into is a view
85520   */
85521 #ifndef SQLITE_OMIT_TRIGGER
85522   pTrigger = sqlite3TriggersExist(pParse, pTab, TK_INSERT, 0, &tmask);
85523   isView = pTab->pSelect!=0;
85524 #else
85525 # define pTrigger 0
85526 # define tmask 0
85527 # define isView 0
85528 #endif
85529 #ifdef SQLITE_OMIT_VIEW
85530 # undef isView
85531 # define isView 0
85532 #endif
85533   assert( (pTrigger && tmask) || (pTrigger==0 && tmask==0) );
85534 
85535   /* If pTab is really a view, make sure it has been initialized.
85536   ** ViewGetColumnNames() is a no-op if pTab is not a view (or virtual
85537   ** module table).
85538   */
85539   if( sqlite3ViewGetColumnNames(pParse, pTab) ){
85540     goto insert_cleanup;
85541   }
85542 
85543   /* Ensure that:
85544   *  (a) the table is not read-only,
85545   *  (b) that if it is a view then ON INSERT triggers exist
85546   */
85547   if( sqlite3IsReadOnly(pParse, pTab, tmask) ){
85548     goto insert_cleanup;
85549   }
85550 
85551   /* Allocate a VDBE
85552   */
85553   v = sqlite3GetVdbe(pParse);
85554   if( v==0 ) goto insert_cleanup;
85555   if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
85556   sqlite3BeginWriteOperation(pParse, pSelect || pTrigger, iDb);
85557 
85558 #ifndef SQLITE_OMIT_XFER_OPT
85559   /* If the statement is of the form
85560   **
85561   **       INSERT INTO <table1> SELECT * FROM <table2>;
85562   **
85563   ** Then special optimizations can be applied that make the transfer
85564   ** very fast and which reduce fragmentation of indices.
85565   **
85566   ** This is the 2nd template.
85567   */
85568   if( pColumn==0 && xferOptimization(pParse, pTab, pSelect, onError, iDb) ){
85569     assert( !pTrigger );
85570     assert( pList==0 );
85571     goto insert_end;
85572   }
85573 #endif /* SQLITE_OMIT_XFER_OPT */
85574 
85575   /* If this is an AUTOINCREMENT table, look up the sequence number in the
85576   ** sqlite_sequence table and store it in memory cell regAutoinc.
85577   */
85578   regAutoinc = autoIncBegin(pParse, iDb, pTab);
85579 
85580   /* Figure out how many columns of data are supplied.  If the data
85581   ** is coming from a SELECT statement, then generate a co-routine that
85582   ** produces a single row of the SELECT on each invocation.  The
85583   ** co-routine is the common header to the 3rd and 4th templates.
85584   */
85585   if( pSelect ){
85586     /* Data is coming from a SELECT.  Generate code to implement that SELECT
85587     ** as a co-routine.  The code is common to both the 3rd and 4th
85588     ** templates:
85589     **
85590     **         EOF <- 0
85591     **         X <- A
85592     **         goto B
85593     **      A: setup for the SELECT
85594     **         loop over the tables in the SELECT
85595     **           load value into register R..R+n
85596     **           yield X
85597     **         end loop
85598     **         cleanup after the SELECT
85599     **         EOF <- 1
85600     **         yield X
85601     **         halt-error
85602     **
85603     ** On each invocation of the co-routine, it puts a single row of the
85604     ** SELECT result into registers dest.iMem...dest.iMem+dest.nMem-1.
85605     ** (These output registers are allocated by sqlite3Select().)  When
85606     ** the SELECT completes, it sets the EOF flag stored in regEof.
85607     */
85608     int rc, j1;
85609 
85610     regEof = ++pParse->nMem;
85611     sqlite3VdbeAddOp2(v, OP_Integer, 0, regEof);      /* EOF <- 0 */
85612     VdbeComment((v, "SELECT eof flag"));
85613     sqlite3SelectDestInit(&dest, SRT_Coroutine, ++pParse->nMem);
85614     addrSelect = sqlite3VdbeCurrentAddr(v)+2;
85615     sqlite3VdbeAddOp2(v, OP_Integer, addrSelect-1, dest.iParm);
85616     j1 = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0);
85617     VdbeComment((v, "Jump over SELECT coroutine"));
85618 
85619     /* Resolve the expressions in the SELECT statement and execute it. */
85620     rc = sqlite3Select(pParse, pSelect, &dest);
85621     assert( pParse->nErr==0 || rc );
85622     if( rc || NEVER(pParse->nErr) || db->mallocFailed ){
85623       goto insert_cleanup;
85624     }
85625     sqlite3VdbeAddOp2(v, OP_Integer, 1, regEof);         /* EOF <- 1 */
85626     sqlite3VdbeAddOp1(v, OP_Yield, dest.iParm);   /* yield X */
85627     sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_INTERNAL, OE_Abort);
85628     VdbeComment((v, "End of SELECT coroutine"));
85629     sqlite3VdbeJumpHere(v, j1);                          /* label B: */
85630 
85631     regFromSelect = dest.iMem;
85632     assert( pSelect->pEList );
85633     nColumn = pSelect->pEList->nExpr;
85634     assert( dest.nMem==nColumn );
85635 
85636     /* Set useTempTable to TRUE if the result of the SELECT statement
85637     ** should be written into a temporary table (template 4).  Set to
85638     ** FALSE if each* row of the SELECT can be written directly into
85639     ** the destination table (template 3).
85640     **
85641     ** A temp table must be used if the table being updated is also one
85642     ** of the tables being read by the SELECT statement.  Also use a
85643     ** temp table in the case of row triggers.
85644     */
85645     if( pTrigger || readsTable(pParse, addrSelect, iDb, pTab) ){
85646       useTempTable = 1;
85647     }
85648 
85649     if( useTempTable ){
85650       /* Invoke the coroutine to extract information from the SELECT
85651       ** and add it to a transient table srcTab.  The code generated
85652       ** here is from the 4th template:
85653       **
85654       **      B: open temp table
85655       **      L: yield X
85656       **         if EOF goto M
85657       **         insert row from R..R+n into temp table
85658       **         goto L
85659       **      M: ...
85660       */
85661       int regRec;          /* Register to hold packed record */
85662       int regTempRowid;    /* Register to hold temp table ROWID */
85663       int addrTop;         /* Label "L" */
85664       int addrIf;          /* Address of jump to M */
85665 
85666       srcTab = pParse->nTab++;
85667       regRec = sqlite3GetTempReg(pParse);
85668       regTempRowid = sqlite3GetTempReg(pParse);
85669       sqlite3VdbeAddOp2(v, OP_OpenEphemeral, srcTab, nColumn);
85670       addrTop = sqlite3VdbeAddOp1(v, OP_Yield, dest.iParm);
85671       addrIf = sqlite3VdbeAddOp1(v, OP_If, regEof);
85672       sqlite3VdbeAddOp3(v, OP_MakeRecord, regFromSelect, nColumn, regRec);
85673       sqlite3VdbeAddOp2(v, OP_NewRowid, srcTab, regTempRowid);
85674       sqlite3VdbeAddOp3(v, OP_Insert, srcTab, regRec, regTempRowid);
85675       sqlite3VdbeAddOp2(v, OP_Goto, 0, addrTop);
85676       sqlite3VdbeJumpHere(v, addrIf);
85677       sqlite3ReleaseTempReg(pParse, regRec);
85678       sqlite3ReleaseTempReg(pParse, regTempRowid);
85679     }
85680   }else{
85681     /* This is the case if the data for the INSERT is coming from a VALUES
85682     ** clause
85683     */
85684     NameContext sNC;
85685     memset(&sNC, 0, sizeof(sNC));
85686     sNC.pParse = pParse;
85687     srcTab = -1;
85688     assert( useTempTable==0 );
85689     nColumn = pList ? pList->nExpr : 0;
85690     for(i=0; i<nColumn; i++){
85691       if( sqlite3ResolveExprNames(&sNC, pList->a[i].pExpr) ){
85692         goto insert_cleanup;
85693       }
85694     }
85695   }
85696 
85697   /* Make sure the number of columns in the source data matches the number
85698   ** of columns to be inserted into the table.
85699   */
85700   if( IsVirtual(pTab) ){
85701     for(i=0; i<pTab->nCol; i++){
85702       nHidden += (IsHiddenColumn(&pTab->aCol[i]) ? 1 : 0);
85703     }
85704   }
85705   if( pColumn==0 && nColumn && nColumn!=(pTab->nCol-nHidden) ){
85706     sqlite3ErrorMsg(pParse,
85707        "table %S has %d columns but %d values were supplied",
85708        pTabList, 0, pTab->nCol-nHidden, nColumn);
85709     goto insert_cleanup;
85710   }
85711   if( pColumn!=0 && nColumn!=pColumn->nId ){
85712     sqlite3ErrorMsg(pParse, "%d values for %d columns", nColumn, pColumn->nId);
85713     goto insert_cleanup;
85714   }
85715 
85716   /* If the INSERT statement included an IDLIST term, then make sure
85717   ** all elements of the IDLIST really are columns of the table and
85718   ** remember the column indices.
85719   **
85720   ** If the table has an INTEGER PRIMARY KEY column and that column
85721   ** is named in the IDLIST, then record in the keyColumn variable
85722   ** the index into IDLIST of the primary key column.  keyColumn is
85723   ** the index of the primary key as it appears in IDLIST, not as
85724   ** is appears in the original table.  (The index of the primary
85725   ** key in the original table is pTab->iPKey.)
85726   */
85727   if( pColumn ){
85728     for(i=0; i<pColumn->nId; i++){
85729       pColumn->a[i].idx = -1;
85730     }
85731     for(i=0; i<pColumn->nId; i++){
85732       for(j=0; j<pTab->nCol; j++){
85733         if( sqlite3StrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){
85734           pColumn->a[i].idx = j;
85735           if( j==pTab->iPKey ){
85736             keyColumn = i;
85737           }
85738           break;
85739         }
85740       }
85741       if( j>=pTab->nCol ){
85742         if( sqlite3IsRowid(pColumn->a[i].zName) ){
85743           keyColumn = i;
85744         }else{
85745           sqlite3ErrorMsg(pParse, "table %S has no column named %s",
85746               pTabList, 0, pColumn->a[i].zName);
85747           pParse->checkSchema = 1;
85748           goto insert_cleanup;
85749         }
85750       }
85751     }
85752   }
85753 
85754   /* If there is no IDLIST term but the table has an integer primary
85755   ** key, the set the keyColumn variable to the primary key column index
85756   ** in the original table definition.
85757   */
85758   if( pColumn==0 && nColumn>0 ){
85759     keyColumn = pTab->iPKey;
85760   }
85761 
85762   /* Initialize the count of rows to be inserted
85763   */
85764   if( db->flags & SQLITE_CountRows ){
85765     regRowCount = ++pParse->nMem;
85766     sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount);
85767   }
85768 
85769   /* If this is not a view, open the table and and all indices */
85770   if( !isView ){
85771     int nIdx;
85772 
85773     baseCur = pParse->nTab;
85774     nIdx = sqlite3OpenTableAndIndices(pParse, pTab, baseCur, OP_OpenWrite);
85775     aRegIdx = sqlite3DbMallocRaw(db, sizeof(int)*(nIdx+1));
85776     if( aRegIdx==0 ){
85777       goto insert_cleanup;
85778     }
85779     for(i=0; i<nIdx; i++){
85780       aRegIdx[i] = ++pParse->nMem;
85781     }
85782   }
85783 
85784   /* This is the top of the main insertion loop */
85785   if( useTempTable ){
85786     /* This block codes the top of loop only.  The complete loop is the
85787     ** following pseudocode (template 4):
85788     **
85789     **         rewind temp table
85790     **      C: loop over rows of intermediate table
85791     **           transfer values form intermediate table into <table>
85792     **         end loop
85793     **      D: ...
85794     */
85795     addrInsTop = sqlite3VdbeAddOp1(v, OP_Rewind, srcTab);
85796     addrCont = sqlite3VdbeCurrentAddr(v);
85797   }else if( pSelect ){
85798     /* This block codes the top of loop only.  The complete loop is the
85799     ** following pseudocode (template 3):
85800     **
85801     **      C: yield X
85802     **         if EOF goto D
85803     **         insert the select result into <table> from R..R+n
85804     **         goto C
85805     **      D: ...
85806     */
85807     addrCont = sqlite3VdbeAddOp1(v, OP_Yield, dest.iParm);
85808     addrInsTop = sqlite3VdbeAddOp1(v, OP_If, regEof);
85809   }
85810 
85811   /* Allocate registers for holding the rowid of the new row,
85812   ** the content of the new row, and the assemblied row record.
85813   */
85814   regRowid = regIns = pParse->nMem+1;
85815   pParse->nMem += pTab->nCol + 1;
85816   if( IsVirtual(pTab) ){
85817     regRowid++;
85818     pParse->nMem++;
85819   }
85820   regData = regRowid+1;
85821 
85822   /* Run the BEFORE and INSTEAD OF triggers, if there are any
85823   */
85824   endOfLoop = sqlite3VdbeMakeLabel(v);
85825   if( tmask & TRIGGER_BEFORE ){
85826     int regCols = sqlite3GetTempRange(pParse, pTab->nCol+1);
85827 
85828     /* build the NEW.* reference row.  Note that if there is an INTEGER
85829     ** PRIMARY KEY into which a NULL is being inserted, that NULL will be
85830     ** translated into a unique ID for the row.  But on a BEFORE trigger,
85831     ** we do not know what the unique ID will be (because the insert has
85832     ** not happened yet) so we substitute a rowid of -1
85833     */
85834     if( keyColumn<0 ){
85835       sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols);
85836     }else{
85837       int j1;
85838       if( useTempTable ){
85839         sqlite3VdbeAddOp3(v, OP_Column, srcTab, keyColumn, regCols);
85840       }else{
85841         assert( pSelect==0 );  /* Otherwise useTempTable is true */
85842         sqlite3ExprCode(pParse, pList->a[keyColumn].pExpr, regCols);
85843       }
85844       j1 = sqlite3VdbeAddOp1(v, OP_NotNull, regCols);
85845       sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols);
85846       sqlite3VdbeJumpHere(v, j1);
85847       sqlite3VdbeAddOp1(v, OP_MustBeInt, regCols);
85848     }
85849 
85850     /* Cannot have triggers on a virtual table. If it were possible,
85851     ** this block would have to account for hidden column.
85852     */
85853     assert( !IsVirtual(pTab) );
85854 
85855     /* Create the new column data
85856     */
85857     for(i=0; i<pTab->nCol; i++){
85858       if( pColumn==0 ){
85859         j = i;
85860       }else{
85861         for(j=0; j<pColumn->nId; j++){
85862           if( pColumn->a[j].idx==i ) break;
85863         }
85864       }
85865       if( (!useTempTable && !pList) || (pColumn && j>=pColumn->nId) ){
85866         sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, regCols+i+1);
85867       }else if( useTempTable ){
85868         sqlite3VdbeAddOp3(v, OP_Column, srcTab, j, regCols+i+1);
85869       }else{
85870         assert( pSelect==0 ); /* Otherwise useTempTable is true */
85871         sqlite3ExprCodeAndCache(pParse, pList->a[j].pExpr, regCols+i+1);
85872       }
85873     }
85874 
85875     /* If this is an INSERT on a view with an INSTEAD OF INSERT trigger,
85876     ** do not attempt any conversions before assembling the record.
85877     ** If this is a real table, attempt conversions as required by the
85878     ** table column affinities.
85879     */
85880     if( !isView ){
85881       sqlite3VdbeAddOp2(v, OP_Affinity, regCols+1, pTab->nCol);
85882       sqlite3TableAffinityStr(v, pTab);
85883     }
85884 
85885     /* Fire BEFORE or INSTEAD OF triggers */
85886     sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_BEFORE,
85887         pTab, regCols-pTab->nCol-1, onError, endOfLoop);
85888 
85889     sqlite3ReleaseTempRange(pParse, regCols, pTab->nCol+1);
85890   }
85891 
85892   /* Push the record number for the new entry onto the stack.  The
85893   ** record number is a randomly generate integer created by NewRowid
85894   ** except when the table has an INTEGER PRIMARY KEY column, in which
85895   ** case the record number is the same as that column.
85896   */
85897   if( !isView ){
85898     if( IsVirtual(pTab) ){
85899       /* The row that the VUpdate opcode will delete: none */
85900       sqlite3VdbeAddOp2(v, OP_Null, 0, regIns);
85901     }
85902     if( keyColumn>=0 ){
85903       if( useTempTable ){
85904         sqlite3VdbeAddOp3(v, OP_Column, srcTab, keyColumn, regRowid);
85905       }else if( pSelect ){
85906         sqlite3VdbeAddOp2(v, OP_SCopy, regFromSelect+keyColumn, regRowid);
85907       }else{
85908         VdbeOp *pOp;
85909         sqlite3ExprCode(pParse, pList->a[keyColumn].pExpr, regRowid);
85910         pOp = sqlite3VdbeGetOp(v, -1);
85911         if( ALWAYS(pOp) && pOp->opcode==OP_Null && !IsVirtual(pTab) ){
85912           appendFlag = 1;
85913           pOp->opcode = OP_NewRowid;
85914           pOp->p1 = baseCur;
85915           pOp->p2 = regRowid;
85916           pOp->p3 = regAutoinc;
85917         }
85918       }
85919       /* If the PRIMARY KEY expression is NULL, then use OP_NewRowid
85920       ** to generate a unique primary key value.
85921       */
85922       if( !appendFlag ){
85923         int j1;
85924         if( !IsVirtual(pTab) ){
85925           j1 = sqlite3VdbeAddOp1(v, OP_NotNull, regRowid);
85926           sqlite3VdbeAddOp3(v, OP_NewRowid, baseCur, regRowid, regAutoinc);
85927           sqlite3VdbeJumpHere(v, j1);
85928         }else{
85929           j1 = sqlite3VdbeCurrentAddr(v);
85930           sqlite3VdbeAddOp2(v, OP_IsNull, regRowid, j1+2);
85931         }
85932         sqlite3VdbeAddOp1(v, OP_MustBeInt, regRowid);
85933       }
85934     }else if( IsVirtual(pTab) ){
85935       sqlite3VdbeAddOp2(v, OP_Null, 0, regRowid);
85936     }else{
85937       sqlite3VdbeAddOp3(v, OP_NewRowid, baseCur, regRowid, regAutoinc);
85938       appendFlag = 1;
85939     }
85940     autoIncStep(pParse, regAutoinc, regRowid);
85941 
85942     /* Push onto the stack, data for all columns of the new entry, beginning
85943     ** with the first column.
85944     */
85945     nHidden = 0;
85946     for(i=0; i<pTab->nCol; i++){
85947       int iRegStore = regRowid+1+i;
85948       if( i==pTab->iPKey ){
85949         /* The value of the INTEGER PRIMARY KEY column is always a NULL.
85950         ** Whenever this column is read, the record number will be substituted
85951         ** in its place.  So will fill this column with a NULL to avoid
85952         ** taking up data space with information that will never be used. */
85953         sqlite3VdbeAddOp2(v, OP_Null, 0, iRegStore);
85954         continue;
85955       }
85956       if( pColumn==0 ){
85957         if( IsHiddenColumn(&pTab->aCol[i]) ){
85958           assert( IsVirtual(pTab) );
85959           j = -1;
85960           nHidden++;
85961         }else{
85962           j = i - nHidden;
85963         }
85964       }else{
85965         for(j=0; j<pColumn->nId; j++){
85966           if( pColumn->a[j].idx==i ) break;
85967         }
85968       }
85969       if( j<0 || nColumn==0 || (pColumn && j>=pColumn->nId) ){
85970         sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, iRegStore);
85971       }else if( useTempTable ){
85972         sqlite3VdbeAddOp3(v, OP_Column, srcTab, j, iRegStore);
85973       }else if( pSelect ){
85974         sqlite3VdbeAddOp2(v, OP_SCopy, regFromSelect+j, iRegStore);
85975       }else{
85976         sqlite3ExprCode(pParse, pList->a[j].pExpr, iRegStore);
85977       }
85978     }
85979 
85980     /* Generate code to check constraints and generate index keys and
85981     ** do the insertion.
85982     */
85983 #ifndef SQLITE_OMIT_VIRTUALTABLE
85984     if( IsVirtual(pTab) ){
85985       const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
85986       sqlite3VtabMakeWritable(pParse, pTab);
85987       sqlite3VdbeAddOp4(v, OP_VUpdate, 1, pTab->nCol+2, regIns, pVTab, P4_VTAB);
85988       sqlite3VdbeChangeP5(v, onError==OE_Default ? OE_Abort : onError);
85989       sqlite3MayAbort(pParse);
85990     }else
85991 #endif
85992     {
85993       int isReplace;    /* Set to true if constraints may cause a replace */
85994       sqlite3GenerateConstraintChecks(pParse, pTab, baseCur, regIns, aRegIdx,
85995           keyColumn>=0, 0, onError, endOfLoop, &isReplace
85996       );
85997       sqlite3FkCheck(pParse, pTab, 0, regIns);
85998       sqlite3CompleteInsertion(
85999           pParse, pTab, baseCur, regIns, aRegIdx, 0, appendFlag, isReplace==0
86000       );
86001     }
86002   }
86003 
86004   /* Update the count of rows that are inserted
86005   */
86006   if( (db->flags & SQLITE_CountRows)!=0 ){
86007     sqlite3VdbeAddOp2(v, OP_AddImm, regRowCount, 1);
86008   }
86009 
86010   if( pTrigger ){
86011     /* Code AFTER triggers */
86012     sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_AFTER,
86013         pTab, regData-2-pTab->nCol, onError, endOfLoop);
86014   }
86015 
86016   /* The bottom of the main insertion loop, if the data source
86017   ** is a SELECT statement.
86018   */
86019   sqlite3VdbeResolveLabel(v, endOfLoop);
86020   if( useTempTable ){
86021     sqlite3VdbeAddOp2(v, OP_Next, srcTab, addrCont);
86022     sqlite3VdbeJumpHere(v, addrInsTop);
86023     sqlite3VdbeAddOp1(v, OP_Close, srcTab);
86024   }else if( pSelect ){
86025     sqlite3VdbeAddOp2(v, OP_Goto, 0, addrCont);
86026     sqlite3VdbeJumpHere(v, addrInsTop);
86027   }
86028 
86029   if( !IsVirtual(pTab) && !isView ){
86030     /* Close all tables opened */
86031     sqlite3VdbeAddOp1(v, OP_Close, baseCur);
86032     for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
86033       sqlite3VdbeAddOp1(v, OP_Close, idx+baseCur);
86034     }
86035   }
86036 
86037 insert_end:
86038   /* Update the sqlite_sequence table by storing the content of the
86039   ** maximum rowid counter values recorded while inserting into
86040   ** autoincrement tables.
86041   */
86042   if( pParse->nested==0 && pParse->pTriggerTab==0 ){
86043     sqlite3AutoincrementEnd(pParse);
86044   }
86045 
86046   /*
86047   ** Return the number of rows inserted. If this routine is
86048   ** generating code because of a call to sqlite3NestedParse(), do not
86049   ** invoke the callback function.
86050   */
86051   if( (db->flags&SQLITE_CountRows) && !pParse->nested && !pParse->pTriggerTab ){
86052     sqlite3VdbeAddOp2(v, OP_ResultRow, regRowCount, 1);
86053     sqlite3VdbeSetNumCols(v, 1);
86054     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows inserted", SQLITE_STATIC);
86055   }
86056 
86057 insert_cleanup:
86058   sqlite3SrcListDelete(db, pTabList);
86059   sqlite3ExprListDelete(db, pList);
86060   sqlite3SelectDelete(db, pSelect);
86061   sqlite3IdListDelete(db, pColumn);
86062   sqlite3DbFree(db, aRegIdx);
86063 }
86064 
86065 /* Make sure "isView" and other macros defined above are undefined. Otherwise
86066 ** thely may interfere with compilation of other functions in this file
86067 ** (or in another file, if this file becomes part of the amalgamation).  */
86068 #ifdef isView
86069  #undef isView
86070 #endif
86071 #ifdef pTrigger
86072  #undef pTrigger
86073 #endif
86074 #ifdef tmask
86075  #undef tmask
86076 #endif
86077 
86078 
86079 /*
86080 ** Generate code to do constraint checks prior to an INSERT or an UPDATE.
86081 **
86082 ** The input is a range of consecutive registers as follows:
86083 **
86084 **    1.  The rowid of the row after the update.
86085 **
86086 **    2.  The data in the first column of the entry after the update.
86087 **
86088 **    i.  Data from middle columns...
86089 **
86090 **    N.  The data in the last column of the entry after the update.
86091 **
86092 ** The regRowid parameter is the index of the register containing (1).
86093 **
86094 ** If isUpdate is true and rowidChng is non-zero, then rowidChng contains
86095 ** the address of a register containing the rowid before the update takes
86096 ** place. isUpdate is true for UPDATEs and false for INSERTs. If isUpdate
86097 ** is false, indicating an INSERT statement, then a non-zero rowidChng
86098 ** indicates that the rowid was explicitly specified as part of the
86099 ** INSERT statement. If rowidChng is false, it means that  the rowid is
86100 ** computed automatically in an insert or that the rowid value is not
86101 ** modified by an update.
86102 **
86103 ** The code generated by this routine store new index entries into
86104 ** registers identified by aRegIdx[].  No index entry is created for
86105 ** indices where aRegIdx[i]==0.  The order of indices in aRegIdx[] is
86106 ** the same as the order of indices on the linked list of indices
86107 ** attached to the table.
86108 **
86109 ** This routine also generates code to check constraints.  NOT NULL,
86110 ** CHECK, and UNIQUE constraints are all checked.  If a constraint fails,
86111 ** then the appropriate action is performed.  There are five possible
86112 ** actions: ROLLBACK, ABORT, FAIL, REPLACE, and IGNORE.
86113 **
86114 **  Constraint type  Action       What Happens
86115 **  ---------------  ----------   ----------------------------------------
86116 **  any              ROLLBACK     The current transaction is rolled back and
86117 **                                sqlite3_exec() returns immediately with a
86118 **                                return code of SQLITE_CONSTRAINT.
86119 **
86120 **  any              ABORT        Back out changes from the current command
86121 **                                only (do not do a complete rollback) then
86122 **                                cause sqlite3_exec() to return immediately
86123 **                                with SQLITE_CONSTRAINT.
86124 **
86125 **  any              FAIL         Sqlite_exec() returns immediately with a
86126 **                                return code of SQLITE_CONSTRAINT.  The
86127 **                                transaction is not rolled back and any
86128 **                                prior changes are retained.
86129 **
86130 **  any              IGNORE       The record number and data is popped from
86131 **                                the stack and there is an immediate jump
86132 **                                to label ignoreDest.
86133 **
86134 **  NOT NULL         REPLACE      The NULL value is replace by the default
86135 **                                value for that column.  If the default value
86136 **                                is NULL, the action is the same as ABORT.
86137 **
86138 **  UNIQUE           REPLACE      The other row that conflicts with the row
86139 **                                being inserted is removed.
86140 **
86141 **  CHECK            REPLACE      Illegal.  The results in an exception.
86142 **
86143 ** Which action to take is determined by the overrideError parameter.
86144 ** Or if overrideError==OE_Default, then the pParse->onError parameter
86145 ** is used.  Or if pParse->onError==OE_Default then the onError value
86146 ** for the constraint is used.
86147 **
86148 ** The calling routine must open a read/write cursor for pTab with
86149 ** cursor number "baseCur".  All indices of pTab must also have open
86150 ** read/write cursors with cursor number baseCur+i for the i-th cursor.
86151 ** Except, if there is no possibility of a REPLACE action then
86152 ** cursors do not need to be open for indices where aRegIdx[i]==0.
86153 */
86154 SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(
86155   Parse *pParse,      /* The parser context */
86156   Table *pTab,        /* the table into which we are inserting */
86157   int baseCur,        /* Index of a read/write cursor pointing at pTab */
86158   int regRowid,       /* Index of the range of input registers */
86159   int *aRegIdx,       /* Register used by each index.  0 for unused indices */
86160   int rowidChng,      /* True if the rowid might collide with existing entry */
86161   int isUpdate,       /* True for UPDATE, False for INSERT */
86162   int overrideError,  /* Override onError to this if not OE_Default */
86163   int ignoreDest,     /* Jump to this label on an OE_Ignore resolution */
86164   int *pbMayReplace   /* OUT: Set to true if constraint may cause a replace */
86165 ){
86166   int i;              /* loop counter */
86167   Vdbe *v;            /* VDBE under constrution */
86168   int nCol;           /* Number of columns */
86169   int onError;        /* Conflict resolution strategy */
86170   int j1;             /* Addresss of jump instruction */
86171   int j2 = 0, j3;     /* Addresses of jump instructions */
86172   int regData;        /* Register containing first data column */
86173   int iCur;           /* Table cursor number */
86174   Index *pIdx;         /* Pointer to one of the indices */
86175   int seenReplace = 0; /* True if REPLACE is used to resolve INT PK conflict */
86176   int regOldRowid = (rowidChng && isUpdate) ? rowidChng : regRowid;
86177 
86178   v = sqlite3GetVdbe(pParse);
86179   assert( v!=0 );
86180   assert( pTab->pSelect==0 );  /* This table is not a VIEW */
86181   nCol = pTab->nCol;
86182   regData = regRowid + 1;
86183 
86184   /* Test all NOT NULL constraints.
86185   */
86186   for(i=0; i<nCol; i++){
86187     if( i==pTab->iPKey ){
86188       continue;
86189     }
86190     onError = pTab->aCol[i].notNull;
86191     if( onError==OE_None ) continue;
86192     if( overrideError!=OE_Default ){
86193       onError = overrideError;
86194     }else if( onError==OE_Default ){
86195       onError = OE_Abort;
86196     }
86197     if( onError==OE_Replace && pTab->aCol[i].pDflt==0 ){
86198       onError = OE_Abort;
86199     }
86200     assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail
86201         || onError==OE_Ignore || onError==OE_Replace );
86202     switch( onError ){
86203       case OE_Abort:
86204         sqlite3MayAbort(pParse);
86205       case OE_Rollback:
86206       case OE_Fail: {
86207         char *zMsg;
86208         sqlite3VdbeAddOp3(v, OP_HaltIfNull,
86209                                   SQLITE_CONSTRAINT, onError, regData+i);
86210         zMsg = sqlite3MPrintf(pParse->db, "%s.%s may not be NULL",
86211                               pTab->zName, pTab->aCol[i].zName);
86212         sqlite3VdbeChangeP4(v, -1, zMsg, P4_DYNAMIC);
86213         break;
86214       }
86215       case OE_Ignore: {
86216         sqlite3VdbeAddOp2(v, OP_IsNull, regData+i, ignoreDest);
86217         break;
86218       }
86219       default: {
86220         assert( onError==OE_Replace );
86221         j1 = sqlite3VdbeAddOp1(v, OP_NotNull, regData+i);
86222         sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, regData+i);
86223         sqlite3VdbeJumpHere(v, j1);
86224         break;
86225       }
86226     }
86227   }
86228 
86229   /* Test all CHECK constraints
86230   */
86231 #ifndef SQLITE_OMIT_CHECK
86232   if( pTab->pCheck && (pParse->db->flags & SQLITE_IgnoreChecks)==0 ){
86233     int allOk = sqlite3VdbeMakeLabel(v);
86234     pParse->ckBase = regData;
86235     sqlite3ExprIfTrue(pParse, pTab->pCheck, allOk, SQLITE_JUMPIFNULL);
86236     onError = overrideError!=OE_Default ? overrideError : OE_Abort;
86237     if( onError==OE_Ignore ){
86238       sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest);
86239     }else{
86240       if( onError==OE_Replace ) onError = OE_Abort; /* IMP: R-15569-63625 */
86241       sqlite3HaltConstraint(pParse, onError, 0, 0);
86242     }
86243     sqlite3VdbeResolveLabel(v, allOk);
86244   }
86245 #endif /* !defined(SQLITE_OMIT_CHECK) */
86246 
86247   /* If we have an INTEGER PRIMARY KEY, make sure the primary key
86248   ** of the new record does not previously exist.  Except, if this
86249   ** is an UPDATE and the primary key is not changing, that is OK.
86250   */
86251   if( rowidChng ){
86252     onError = pTab->keyConf;
86253     if( overrideError!=OE_Default ){
86254       onError = overrideError;
86255     }else if( onError==OE_Default ){
86256       onError = OE_Abort;
86257     }
86258 
86259     if( isUpdate ){
86260       j2 = sqlite3VdbeAddOp3(v, OP_Eq, regRowid, 0, rowidChng);
86261     }
86262     j3 = sqlite3VdbeAddOp3(v, OP_NotExists, baseCur, 0, regRowid);
86263     switch( onError ){
86264       default: {
86265         onError = OE_Abort;
86266         /* Fall thru into the next case */
86267       }
86268       case OE_Rollback:
86269       case OE_Abort:
86270       case OE_Fail: {
86271         sqlite3HaltConstraint(
86272           pParse, onError, "PRIMARY KEY must be unique", P4_STATIC);
86273         break;
86274       }
86275       case OE_Replace: {
86276         /* If there are DELETE triggers on this table and the
86277         ** recursive-triggers flag is set, call GenerateRowDelete() to
86278         ** remove the conflicting row from the the table. This will fire
86279         ** the triggers and remove both the table and index b-tree entries.
86280         **
86281         ** Otherwise, if there are no triggers or the recursive-triggers
86282         ** flag is not set, but the table has one or more indexes, call
86283         ** GenerateRowIndexDelete(). This removes the index b-tree entries
86284         ** only. The table b-tree entry will be replaced by the new entry
86285         ** when it is inserted.
86286         **
86287         ** If either GenerateRowDelete() or GenerateRowIndexDelete() is called,
86288         ** also invoke MultiWrite() to indicate that this VDBE may require
86289         ** statement rollback (if the statement is aborted after the delete
86290         ** takes place). Earlier versions called sqlite3MultiWrite() regardless,
86291         ** but being more selective here allows statements like:
86292         **
86293         **   REPLACE INTO t(rowid) VALUES($newrowid)
86294         **
86295         ** to run without a statement journal if there are no indexes on the
86296         ** table.
86297         */
86298         Trigger *pTrigger = 0;
86299         if( pParse->db->flags&SQLITE_RecTriggers ){
86300           pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
86301         }
86302         if( pTrigger || sqlite3FkRequired(pParse, pTab, 0, 0) ){
86303           sqlite3MultiWrite(pParse);
86304           sqlite3GenerateRowDelete(
86305               pParse, pTab, baseCur, regRowid, 0, pTrigger, OE_Replace
86306           );
86307         }else if( pTab->pIndex ){
86308           sqlite3MultiWrite(pParse);
86309           sqlite3GenerateRowIndexDelete(pParse, pTab, baseCur, 0);
86310         }
86311         seenReplace = 1;
86312         break;
86313       }
86314       case OE_Ignore: {
86315         assert( seenReplace==0 );
86316         sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest);
86317         break;
86318       }
86319     }
86320     sqlite3VdbeJumpHere(v, j3);
86321     if( isUpdate ){
86322       sqlite3VdbeJumpHere(v, j2);
86323     }
86324   }
86325 
86326   /* Test all UNIQUE constraints by creating entries for each UNIQUE
86327   ** index and making sure that duplicate entries do not already exist.
86328   ** Add the new records to the indices as we go.
86329   */
86330   for(iCur=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, iCur++){
86331     int regIdx;
86332     int regR;
86333 
86334     if( aRegIdx[iCur]==0 ) continue;  /* Skip unused indices */
86335 
86336     /* Create a key for accessing the index entry */
86337     regIdx = sqlite3GetTempRange(pParse, pIdx->nColumn+1);
86338     for(i=0; i<pIdx->nColumn; i++){
86339       int idx = pIdx->aiColumn[i];
86340       if( idx==pTab->iPKey ){
86341         sqlite3VdbeAddOp2(v, OP_SCopy, regRowid, regIdx+i);
86342       }else{
86343         sqlite3VdbeAddOp2(v, OP_SCopy, regData+idx, regIdx+i);
86344       }
86345     }
86346     sqlite3VdbeAddOp2(v, OP_SCopy, regRowid, regIdx+i);
86347     sqlite3VdbeAddOp3(v, OP_MakeRecord, regIdx, pIdx->nColumn+1, aRegIdx[iCur]);
86348     sqlite3VdbeChangeP4(v, -1, sqlite3IndexAffinityStr(v, pIdx), P4_TRANSIENT);
86349     sqlite3ExprCacheAffinityChange(pParse, regIdx, pIdx->nColumn+1);
86350 
86351     /* Find out what action to take in case there is an indexing conflict */
86352     onError = pIdx->onError;
86353     if( onError==OE_None ){
86354       sqlite3ReleaseTempRange(pParse, regIdx, pIdx->nColumn+1);
86355       continue;  /* pIdx is not a UNIQUE index */
86356     }
86357     if( overrideError!=OE_Default ){
86358       onError = overrideError;
86359     }else if( onError==OE_Default ){
86360       onError = OE_Abort;
86361     }
86362     if( seenReplace ){
86363       if( onError==OE_Ignore ) onError = OE_Replace;
86364       else if( onError==OE_Fail ) onError = OE_Abort;
86365     }
86366 
86367     /* Check to see if the new index entry will be unique */
86368     regR = sqlite3GetTempReg(pParse);
86369     sqlite3VdbeAddOp2(v, OP_SCopy, regOldRowid, regR);
86370     j3 = sqlite3VdbeAddOp4(v, OP_IsUnique, baseCur+iCur+1, 0,
86371                            regR, SQLITE_INT_TO_PTR(regIdx),
86372                            P4_INT32);
86373     sqlite3ReleaseTempRange(pParse, regIdx, pIdx->nColumn+1);
86374 
86375     /* Generate code that executes if the new index entry is not unique */
86376     assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail
86377         || onError==OE_Ignore || onError==OE_Replace );
86378     switch( onError ){
86379       case OE_Rollback:
86380       case OE_Abort:
86381       case OE_Fail: {
86382         int j;
86383         StrAccum errMsg;
86384         const char *zSep;
86385         char *zErr;
86386 
86387         sqlite3StrAccumInit(&errMsg, 0, 0, 200);
86388         errMsg.db = pParse->db;
86389         zSep = pIdx->nColumn>1 ? "columns " : "column ";
86390         for(j=0; j<pIdx->nColumn; j++){
86391           char *zCol = pTab->aCol[pIdx->aiColumn[j]].zName;
86392           sqlite3StrAccumAppend(&errMsg, zSep, -1);
86393           zSep = ", ";
86394           sqlite3StrAccumAppend(&errMsg, zCol, -1);
86395         }
86396         sqlite3StrAccumAppend(&errMsg,
86397             pIdx->nColumn>1 ? " are not unique" : " is not unique", -1);
86398         zErr = sqlite3StrAccumFinish(&errMsg);
86399         sqlite3HaltConstraint(pParse, onError, zErr, 0);
86400         sqlite3DbFree(errMsg.db, zErr);
86401         break;
86402       }
86403       case OE_Ignore: {
86404         assert( seenReplace==0 );
86405         sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest);
86406         break;
86407       }
86408       default: {
86409         Trigger *pTrigger = 0;
86410         assert( onError==OE_Replace );
86411         sqlite3MultiWrite(pParse);
86412         if( pParse->db->flags&SQLITE_RecTriggers ){
86413           pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
86414         }
86415         sqlite3GenerateRowDelete(
86416             pParse, pTab, baseCur, regR, 0, pTrigger, OE_Replace
86417         );
86418         seenReplace = 1;
86419         break;
86420       }
86421     }
86422     sqlite3VdbeJumpHere(v, j3);
86423     sqlite3ReleaseTempReg(pParse, regR);
86424   }
86425 
86426   if( pbMayReplace ){
86427     *pbMayReplace = seenReplace;
86428   }
86429 }
86430 
86431 /*
86432 ** This routine generates code to finish the INSERT or UPDATE operation
86433 ** that was started by a prior call to sqlite3GenerateConstraintChecks.
86434 ** A consecutive range of registers starting at regRowid contains the
86435 ** rowid and the content to be inserted.
86436 **
86437 ** The arguments to this routine should be the same as the first six
86438 ** arguments to sqlite3GenerateConstraintChecks.
86439 */
86440 SQLITE_PRIVATE void sqlite3CompleteInsertion(
86441   Parse *pParse,      /* The parser context */
86442   Table *pTab,        /* the table into which we are inserting */
86443   int baseCur,        /* Index of a read/write cursor pointing at pTab */
86444   int regRowid,       /* Range of content */
86445   int *aRegIdx,       /* Register used by each index.  0 for unused indices */
86446   int isUpdate,       /* True for UPDATE, False for INSERT */
86447   int appendBias,     /* True if this is likely to be an append */
86448   int useSeekResult   /* True to set the USESEEKRESULT flag on OP_[Idx]Insert */
86449 ){
86450   int i;
86451   Vdbe *v;
86452   int nIdx;
86453   Index *pIdx;
86454   u8 pik_flags;
86455   int regData;
86456   int regRec;
86457 
86458   v = sqlite3GetVdbe(pParse);
86459   assert( v!=0 );
86460   assert( pTab->pSelect==0 );  /* This table is not a VIEW */
86461   for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){}
86462   for(i=nIdx-1; i>=0; i--){
86463     if( aRegIdx[i]==0 ) continue;
86464     sqlite3VdbeAddOp2(v, OP_IdxInsert, baseCur+i+1, aRegIdx[i]);
86465     if( useSeekResult ){
86466       sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
86467     }
86468   }
86469   regData = regRowid + 1;
86470   regRec = sqlite3GetTempReg(pParse);
86471   sqlite3VdbeAddOp3(v, OP_MakeRecord, regData, pTab->nCol, regRec);
86472   sqlite3TableAffinityStr(v, pTab);
86473   sqlite3ExprCacheAffinityChange(pParse, regData, pTab->nCol);
86474   if( pParse->nested ){
86475     pik_flags = 0;
86476   }else{
86477     pik_flags = OPFLAG_NCHANGE;
86478     pik_flags |= (isUpdate?OPFLAG_ISUPDATE:OPFLAG_LASTROWID);
86479   }
86480   if( appendBias ){
86481     pik_flags |= OPFLAG_APPEND;
86482   }
86483   if( useSeekResult ){
86484     pik_flags |= OPFLAG_USESEEKRESULT;
86485   }
86486   sqlite3VdbeAddOp3(v, OP_Insert, baseCur, regRec, regRowid);
86487   if( !pParse->nested ){
86488     sqlite3VdbeChangeP4(v, -1, pTab->zName, P4_TRANSIENT);
86489   }
86490   sqlite3VdbeChangeP5(v, pik_flags);
86491 }
86492 
86493 /*
86494 ** Generate code that will open cursors for a table and for all
86495 ** indices of that table.  The "baseCur" parameter is the cursor number used
86496 ** for the table.  Indices are opened on subsequent cursors.
86497 **
86498 ** Return the number of indices on the table.
86499 */
86500 SQLITE_PRIVATE int sqlite3OpenTableAndIndices(
86501   Parse *pParse,   /* Parsing context */
86502   Table *pTab,     /* Table to be opened */
86503   int baseCur,     /* Cursor number assigned to the table */
86504   int op           /* OP_OpenRead or OP_OpenWrite */
86505 ){
86506   int i;
86507   int iDb;
86508   Index *pIdx;
86509   Vdbe *v;
86510 
86511   if( IsVirtual(pTab) ) return 0;
86512   iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
86513   v = sqlite3GetVdbe(pParse);
86514   assert( v!=0 );
86515   sqlite3OpenTable(pParse, baseCur, iDb, pTab, op);
86516   for(i=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
86517     KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx);
86518     assert( pIdx->pSchema==pTab->pSchema );
86519     sqlite3VdbeAddOp4(v, op, i+baseCur, pIdx->tnum, iDb,
86520                       (char*)pKey, P4_KEYINFO_HANDOFF);
86521     VdbeComment((v, "%s", pIdx->zName));
86522   }
86523   if( pParse->nTab<baseCur+i ){
86524     pParse->nTab = baseCur+i;
86525   }
86526   return i-1;
86527 }
86528 
86529 
86530 #ifdef SQLITE_TEST
86531 /*
86532 ** The following global variable is incremented whenever the
86533 ** transfer optimization is used.  This is used for testing
86534 ** purposes only - to make sure the transfer optimization really
86535 ** is happening when it is suppose to.
86536 */
86537 SQLITE_API int sqlite3_xferopt_count;
86538 #endif /* SQLITE_TEST */
86539 
86540 
86541 #ifndef SQLITE_OMIT_XFER_OPT
86542 /*
86543 ** Check to collation names to see if they are compatible.
86544 */
86545 static int xferCompatibleCollation(const char *z1, const char *z2){
86546   if( z1==0 ){
86547     return z2==0;
86548   }
86549   if( z2==0 ){
86550     return 0;
86551   }
86552   return sqlite3StrICmp(z1, z2)==0;
86553 }
86554 
86555 
86556 /*
86557 ** Check to see if index pSrc is compatible as a source of data
86558 ** for index pDest in an insert transfer optimization.  The rules
86559 ** for a compatible index:
86560 **
86561 **    *   The index is over the same set of columns
86562 **    *   The same DESC and ASC markings occurs on all columns
86563 **    *   The same onError processing (OE_Abort, OE_Ignore, etc)
86564 **    *   The same collating sequence on each column
86565 */
86566 static int xferCompatibleIndex(Index *pDest, Index *pSrc){
86567   int i;
86568   assert( pDest && pSrc );
86569   assert( pDest->pTable!=pSrc->pTable );
86570   if( pDest->nColumn!=pSrc->nColumn ){
86571     return 0;   /* Different number of columns */
86572   }
86573   if( pDest->onError!=pSrc->onError ){
86574     return 0;   /* Different conflict resolution strategies */
86575   }
86576   for(i=0; i<pSrc->nColumn; i++){
86577     if( pSrc->aiColumn[i]!=pDest->aiColumn[i] ){
86578       return 0;   /* Different columns indexed */
86579     }
86580     if( pSrc->aSortOrder[i]!=pDest->aSortOrder[i] ){
86581       return 0;   /* Different sort orders */
86582     }
86583     if( !xferCompatibleCollation(pSrc->azColl[i],pDest->azColl[i]) ){
86584       return 0;   /* Different collating sequences */
86585     }
86586   }
86587 
86588   /* If no test above fails then the indices must be compatible */
86589   return 1;
86590 }
86591 
86592 /*
86593 ** Attempt the transfer optimization on INSERTs of the form
86594 **
86595 **     INSERT INTO tab1 SELECT * FROM tab2;
86596 **
86597 ** This optimization is only attempted if
86598 **
86599 **    (1)  tab1 and tab2 have identical schemas including all the
86600 **         same indices and constraints
86601 **
86602 **    (2)  tab1 and tab2 are different tables
86603 **
86604 **    (3)  There must be no triggers on tab1
86605 **
86606 **    (4)  The result set of the SELECT statement is "*"
86607 **
86608 **    (5)  The SELECT statement has no WHERE, HAVING, ORDER BY, GROUP BY,
86609 **         or LIMIT clause.
86610 **
86611 **    (6)  The SELECT statement is a simple (not a compound) select that
86612 **         contains only tab2 in its FROM clause
86613 **
86614 ** This method for implementing the INSERT transfers raw records from
86615 ** tab2 over to tab1.  The columns are not decoded.  Raw records from
86616 ** the indices of tab2 are transfered to tab1 as well.  In so doing,
86617 ** the resulting tab1 has much less fragmentation.
86618 **
86619 ** This routine returns TRUE if the optimization is attempted.  If any
86620 ** of the conditions above fail so that the optimization should not
86621 ** be attempted, then this routine returns FALSE.
86622 */
86623 static int xferOptimization(
86624   Parse *pParse,        /* Parser context */
86625   Table *pDest,         /* The table we are inserting into */
86626   Select *pSelect,      /* A SELECT statement to use as the data source */
86627   int onError,          /* How to handle constraint errors */
86628   int iDbDest           /* The database of pDest */
86629 ){
86630   ExprList *pEList;                /* The result set of the SELECT */
86631   Table *pSrc;                     /* The table in the FROM clause of SELECT */
86632   Index *pSrcIdx, *pDestIdx;       /* Source and destination indices */
86633   struct SrcList_item *pItem;      /* An element of pSelect->pSrc */
86634   int i;                           /* Loop counter */
86635   int iDbSrc;                      /* The database of pSrc */
86636   int iSrc, iDest;                 /* Cursors from source and destination */
86637   int addr1, addr2;                /* Loop addresses */
86638   int emptyDestTest;               /* Address of test for empty pDest */
86639   int emptySrcTest;                /* Address of test for empty pSrc */
86640   Vdbe *v;                         /* The VDBE we are building */
86641   KeyInfo *pKey;                   /* Key information for an index */
86642   int regAutoinc;                  /* Memory register used by AUTOINC */
86643   int destHasUniqueIdx = 0;        /* True if pDest has a UNIQUE index */
86644   int regData, regRowid;           /* Registers holding data and rowid */
86645 
86646   if( pSelect==0 ){
86647     return 0;   /* Must be of the form  INSERT INTO ... SELECT ... */
86648   }
86649   if( sqlite3TriggerList(pParse, pDest) ){
86650     return 0;   /* tab1 must not have triggers */
86651   }
86652 #ifndef SQLITE_OMIT_VIRTUALTABLE
86653   if( pDest->tabFlags & TF_Virtual ){
86654     return 0;   /* tab1 must not be a virtual table */
86655   }
86656 #endif
86657   if( onError==OE_Default ){
86658     onError = OE_Abort;
86659   }
86660   if( onError!=OE_Abort && onError!=OE_Rollback ){
86661     return 0;   /* Cannot do OR REPLACE or OR IGNORE or OR FAIL */
86662   }
86663   assert(pSelect->pSrc);   /* allocated even if there is no FROM clause */
86664   if( pSelect->pSrc->nSrc!=1 ){
86665     return 0;   /* FROM clause must have exactly one term */
86666   }
86667   if( pSelect->pSrc->a[0].pSelect ){
86668     return 0;   /* FROM clause cannot contain a subquery */
86669   }
86670   if( pSelect->pWhere ){
86671     return 0;   /* SELECT may not have a WHERE clause */
86672   }
86673   if( pSelect->pOrderBy ){
86674     return 0;   /* SELECT may not have an ORDER BY clause */
86675   }
86676   /* Do not need to test for a HAVING clause.  If HAVING is present but
86677   ** there is no ORDER BY, we will get an error. */
86678   if( pSelect->pGroupBy ){
86679     return 0;   /* SELECT may not have a GROUP BY clause */
86680   }
86681   if( pSelect->pLimit ){
86682     return 0;   /* SELECT may not have a LIMIT clause */
86683   }
86684   assert( pSelect->pOffset==0 );  /* Must be so if pLimit==0 */
86685   if( pSelect->pPrior ){
86686     return 0;   /* SELECT may not be a compound query */
86687   }
86688   if( pSelect->selFlags & SF_Distinct ){
86689     return 0;   /* SELECT may not be DISTINCT */
86690   }
86691   pEList = pSelect->pEList;
86692   assert( pEList!=0 );
86693   if( pEList->nExpr!=1 ){
86694     return 0;   /* The result set must have exactly one column */
86695   }
86696   assert( pEList->a[0].pExpr );
86697   if( pEList->a[0].pExpr->op!=TK_ALL ){
86698     return 0;   /* The result set must be the special operator "*" */
86699   }
86700 
86701   /* At this point we have established that the statement is of the
86702   ** correct syntactic form to participate in this optimization.  Now
86703   ** we have to check the semantics.
86704   */
86705   pItem = pSelect->pSrc->a;
86706   pSrc = sqlite3LocateTable(pParse, 0, pItem->zName, pItem->zDatabase);
86707   if( pSrc==0 ){
86708     return 0;   /* FROM clause does not contain a real table */
86709   }
86710   if( pSrc==pDest ){
86711     return 0;   /* tab1 and tab2 may not be the same table */
86712   }
86713 #ifndef SQLITE_OMIT_VIRTUALTABLE
86714   if( pSrc->tabFlags & TF_Virtual ){
86715     return 0;   /* tab2 must not be a virtual table */
86716   }
86717 #endif
86718   if( pSrc->pSelect ){
86719     return 0;   /* tab2 may not be a view */
86720   }
86721   if( pDest->nCol!=pSrc->nCol ){
86722     return 0;   /* Number of columns must be the same in tab1 and tab2 */
86723   }
86724   if( pDest->iPKey!=pSrc->iPKey ){
86725     return 0;   /* Both tables must have the same INTEGER PRIMARY KEY */
86726   }
86727   for(i=0; i<pDest->nCol; i++){
86728     if( pDest->aCol[i].affinity!=pSrc->aCol[i].affinity ){
86729       return 0;    /* Affinity must be the same on all columns */
86730     }
86731     if( !xferCompatibleCollation(pDest->aCol[i].zColl, pSrc->aCol[i].zColl) ){
86732       return 0;    /* Collating sequence must be the same on all columns */
86733     }
86734     if( pDest->aCol[i].notNull && !pSrc->aCol[i].notNull ){
86735       return 0;    /* tab2 must be NOT NULL if tab1 is */
86736     }
86737   }
86738   for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
86739     if( pDestIdx->onError!=OE_None ){
86740       destHasUniqueIdx = 1;
86741     }
86742     for(pSrcIdx=pSrc->pIndex; pSrcIdx; pSrcIdx=pSrcIdx->pNext){
86743       if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break;
86744     }
86745     if( pSrcIdx==0 ){
86746       return 0;    /* pDestIdx has no corresponding index in pSrc */
86747     }
86748   }
86749 #ifndef SQLITE_OMIT_CHECK
86750   if( pDest->pCheck && sqlite3ExprCompare(pSrc->pCheck, pDest->pCheck) ){
86751     return 0;   /* Tables have different CHECK constraints.  Ticket #2252 */
86752   }
86753 #endif
86754 #ifndef SQLITE_OMIT_FOREIGN_KEY
86755   /* Disallow the transfer optimization if the destination table constains
86756   ** any foreign key constraints.  This is more restrictive than necessary.
86757   ** But the main beneficiary of the transfer optimization is the VACUUM
86758   ** command, and the VACUUM command disables foreign key constraints.  So
86759   ** the extra complication to make this rule less restrictive is probably
86760   ** not worth the effort.  Ticket [6284df89debdfa61db8073e062908af0c9b6118e]
86761   */
86762   if( (pParse->db->flags & SQLITE_ForeignKeys)!=0 && pDest->pFKey!=0 ){
86763     return 0;
86764   }
86765 #endif
86766 
86767   /* If we get this far, it means either:
86768   **
86769   **    *   We can always do the transfer if the table contains an
86770   **        an integer primary key
86771   **
86772   **    *   We can conditionally do the transfer if the destination
86773   **        table is empty.
86774   */
86775 #ifdef SQLITE_TEST
86776   sqlite3_xferopt_count++;
86777 #endif
86778   iDbSrc = sqlite3SchemaToIndex(pParse->db, pSrc->pSchema);
86779   v = sqlite3GetVdbe(pParse);
86780   sqlite3CodeVerifySchema(pParse, iDbSrc);
86781   iSrc = pParse->nTab++;
86782   iDest = pParse->nTab++;
86783   regAutoinc = autoIncBegin(pParse, iDbDest, pDest);
86784   sqlite3OpenTable(pParse, iDest, iDbDest, pDest, OP_OpenWrite);
86785   if( (pDest->iPKey<0 && pDest->pIndex!=0) || destHasUniqueIdx ){
86786     /* If tables do not have an INTEGER PRIMARY KEY and there
86787     ** are indices to be copied and the destination is not empty,
86788     ** we have to disallow the transfer optimization because the
86789     ** the rowids might change which will mess up indexing.
86790     **
86791     ** Or if the destination has a UNIQUE index and is not empty,
86792     ** we also disallow the transfer optimization because we cannot
86793     ** insure that all entries in the union of DEST and SRC will be
86794     ** unique.
86795     */
86796     addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iDest, 0);
86797     emptyDestTest = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0);
86798     sqlite3VdbeJumpHere(v, addr1);
86799   }else{
86800     emptyDestTest = 0;
86801   }
86802   sqlite3OpenTable(pParse, iSrc, iDbSrc, pSrc, OP_OpenRead);
86803   emptySrcTest = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0);
86804   regData = sqlite3GetTempReg(pParse);
86805   regRowid = sqlite3GetTempReg(pParse);
86806   if( pDest->iPKey>=0 ){
86807     addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid);
86808     addr2 = sqlite3VdbeAddOp3(v, OP_NotExists, iDest, 0, regRowid);
86809     sqlite3HaltConstraint(
86810         pParse, onError, "PRIMARY KEY must be unique", P4_STATIC);
86811     sqlite3VdbeJumpHere(v, addr2);
86812     autoIncStep(pParse, regAutoinc, regRowid);
86813   }else if( pDest->pIndex==0 ){
86814     addr1 = sqlite3VdbeAddOp2(v, OP_NewRowid, iDest, regRowid);
86815   }else{
86816     addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid);
86817     assert( (pDest->tabFlags & TF_Autoincrement)==0 );
86818   }
86819   sqlite3VdbeAddOp2(v, OP_RowData, iSrc, regData);
86820   sqlite3VdbeAddOp3(v, OP_Insert, iDest, regData, regRowid);
86821   sqlite3VdbeChangeP5(v, OPFLAG_NCHANGE|OPFLAG_LASTROWID|OPFLAG_APPEND);
86822   sqlite3VdbeChangeP4(v, -1, pDest->zName, 0);
86823   sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1);
86824   for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
86825     for(pSrcIdx=pSrc->pIndex; ALWAYS(pSrcIdx); pSrcIdx=pSrcIdx->pNext){
86826       if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break;
86827     }
86828     assert( pSrcIdx );
86829     sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0);
86830     sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
86831     pKey = sqlite3IndexKeyinfo(pParse, pSrcIdx);
86832     sqlite3VdbeAddOp4(v, OP_OpenRead, iSrc, pSrcIdx->tnum, iDbSrc,
86833                       (char*)pKey, P4_KEYINFO_HANDOFF);
86834     VdbeComment((v, "%s", pSrcIdx->zName));
86835     pKey = sqlite3IndexKeyinfo(pParse, pDestIdx);
86836     sqlite3VdbeAddOp4(v, OP_OpenWrite, iDest, pDestIdx->tnum, iDbDest,
86837                       (char*)pKey, P4_KEYINFO_HANDOFF);
86838     VdbeComment((v, "%s", pDestIdx->zName));
86839     addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0);
86840     sqlite3VdbeAddOp2(v, OP_RowKey, iSrc, regData);
86841     sqlite3VdbeAddOp3(v, OP_IdxInsert, iDest, regData, 1);
86842     sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1+1);
86843     sqlite3VdbeJumpHere(v, addr1);
86844   }
86845   sqlite3VdbeJumpHere(v, emptySrcTest);
86846   sqlite3ReleaseTempReg(pParse, regRowid);
86847   sqlite3ReleaseTempReg(pParse, regData);
86848   sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0);
86849   sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
86850   if( emptyDestTest ){
86851     sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_OK, 0);
86852     sqlite3VdbeJumpHere(v, emptyDestTest);
86853     sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
86854     return 0;
86855   }else{
86856     return 1;
86857   }
86858 }
86859 #endif /* SQLITE_OMIT_XFER_OPT */
86860 
86861 /************** End of insert.c **********************************************/
86862 /************** Begin file legacy.c ******************************************/
86863 /*
86864 ** 2001 September 15
86865 **
86866 ** The author disclaims copyright to this source code.  In place of
86867 ** a legal notice, here is a blessing:
86868 **
86869 **    May you do good and not evil.
86870 **    May you find forgiveness for yourself and forgive others.
86871 **    May you share freely, never taking more than you give.
86872 **
86873 *************************************************************************
86874 ** Main file for the SQLite library.  The routines in this file
86875 ** implement the programmer interface to the library.  Routines in
86876 ** other files are for internal use by SQLite and should not be
86877 ** accessed by users of the library.
86878 */
86879 
86880 
86881 /*
86882 ** Execute SQL code.  Return one of the SQLITE_ success/failure
86883 ** codes.  Also write an error message into memory obtained from
86884 ** malloc() and make *pzErrMsg point to that message.
86885 **
86886 ** If the SQL is a query, then for each row in the query result
86887 ** the xCallback() function is called.  pArg becomes the first
86888 ** argument to xCallback().  If xCallback=NULL then no callback
86889 ** is invoked, even for queries.
86890 */
86891 SQLITE_API int sqlite3_exec(
86892   sqlite3 *db,                /* The database on which the SQL executes */
86893   const char *zSql,           /* The SQL to be executed */
86894   sqlite3_callback xCallback, /* Invoke this callback routine */
86895   void *pArg,                 /* First argument to xCallback() */
86896   char **pzErrMsg             /* Write error messages here */
86897 ){
86898   int rc = SQLITE_OK;         /* Return code */
86899   const char *zLeftover;      /* Tail of unprocessed SQL */
86900   sqlite3_stmt *pStmt = 0;    /* The current SQL statement */
86901   char **azCols = 0;          /* Names of result columns */
86902   int nRetry = 0;             /* Number of retry attempts */
86903   int callbackIsInit;         /* True if callback data is initialized */
86904 
86905   if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
86906   if( zSql==0 ) zSql = "";
86907 
86908   sqlite3_mutex_enter(db->mutex);
86909   sqlite3Error(db, SQLITE_OK, 0);
86910   while( (rc==SQLITE_OK || (rc==SQLITE_SCHEMA && (++nRetry)<2)) && zSql[0] ){
86911     int nCol;
86912     char **azVals = 0;
86913 
86914     pStmt = 0;
86915     rc = sqlite3_prepare(db, zSql, -1, &pStmt, &zLeftover);
86916     assert( rc==SQLITE_OK || pStmt==0 );
86917     if( rc!=SQLITE_OK ){
86918       continue;
86919     }
86920     if( !pStmt ){
86921       /* this happens for a comment or white-space */
86922       zSql = zLeftover;
86923       continue;
86924     }
86925 
86926     callbackIsInit = 0;
86927     nCol = sqlite3_column_count(pStmt);
86928 
86929     while( 1 ){
86930       int i;
86931       rc = sqlite3_step(pStmt);
86932 
86933       /* Invoke the callback function if required */
86934       if( xCallback && (SQLITE_ROW==rc ||
86935           (SQLITE_DONE==rc && !callbackIsInit
86936                            && db->flags&SQLITE_NullCallback)) ){
86937         if( !callbackIsInit ){
86938           azCols = sqlite3DbMallocZero(db, 2*nCol*sizeof(const char*) + 1);
86939           if( azCols==0 ){
86940             goto exec_out;
86941           }
86942           for(i=0; i<nCol; i++){
86943             azCols[i] = (char *)sqlite3_column_name(pStmt, i);
86944             /* sqlite3VdbeSetColName() installs column names as UTF8
86945             ** strings so there is no way for sqlite3_column_name() to fail. */
86946             assert( azCols[i]!=0 );
86947           }
86948           callbackIsInit = 1;
86949         }
86950         if( rc==SQLITE_ROW ){
86951           azVals = &azCols[nCol];
86952           for(i=0; i<nCol; i++){
86953             azVals[i] = (char *)sqlite3_column_text(pStmt, i);
86954             if( !azVals[i] && sqlite3_column_type(pStmt, i)!=SQLITE_NULL ){
86955               db->mallocFailed = 1;
86956               goto exec_out;
86957             }
86958           }
86959         }
86960         if( xCallback(pArg, nCol, azVals, azCols) ){
86961           rc = SQLITE_ABORT;
86962           sqlite3VdbeFinalize((Vdbe *)pStmt);
86963           pStmt = 0;
86964           sqlite3Error(db, SQLITE_ABORT, 0);
86965           goto exec_out;
86966         }
86967       }
86968 
86969       if( rc!=SQLITE_ROW ){
86970         rc = sqlite3VdbeFinalize((Vdbe *)pStmt);
86971         pStmt = 0;
86972         if( rc!=SQLITE_SCHEMA ){
86973           nRetry = 0;
86974           zSql = zLeftover;
86975           while( sqlite3Isspace(zSql[0]) ) zSql++;
86976         }
86977         break;
86978       }
86979     }
86980 
86981     sqlite3DbFree(db, azCols);
86982     azCols = 0;
86983   }
86984 
86985 exec_out:
86986   if( pStmt ) sqlite3VdbeFinalize((Vdbe *)pStmt);
86987   sqlite3DbFree(db, azCols);
86988 
86989   rc = sqlite3ApiExit(db, rc);
86990   if( rc!=SQLITE_OK && ALWAYS(rc==sqlite3_errcode(db)) && pzErrMsg ){
86991     int nErrMsg = 1 + sqlite3Strlen30(sqlite3_errmsg(db));
86992     *pzErrMsg = sqlite3Malloc(nErrMsg);
86993     if( *pzErrMsg ){
86994       memcpy(*pzErrMsg, sqlite3_errmsg(db), nErrMsg);
86995     }else{
86996       rc = SQLITE_NOMEM;
86997       sqlite3Error(db, SQLITE_NOMEM, 0);
86998     }
86999   }else if( pzErrMsg ){
87000     *pzErrMsg = 0;
87001   }
87002 
87003   assert( (rc&db->errMask)==rc );
87004   sqlite3_mutex_leave(db->mutex);
87005   return rc;
87006 }
87007 
87008 /************** End of legacy.c **********************************************/
87009 /************** Begin file loadext.c *****************************************/
87010 /*
87011 ** 2006 June 7
87012 **
87013 ** The author disclaims copyright to this source code.  In place of
87014 ** a legal notice, here is a blessing:
87015 **
87016 **    May you do good and not evil.
87017 **    May you find forgiveness for yourself and forgive others.
87018 **    May you share freely, never taking more than you give.
87019 **
87020 *************************************************************************
87021 ** This file contains code used to dynamically load extensions into
87022 ** the SQLite library.
87023 */
87024 
87025 #ifndef SQLITE_CORE
87026   #define SQLITE_CORE 1  /* Disable the API redefinition in sqlite3ext.h */
87027 #endif
87028 /************** Include sqlite3ext.h in the middle of loadext.c **************/
87029 /************** Begin file sqlite3ext.h **************************************/
87030 /*
87031 ** 2006 June 7
87032 **
87033 ** The author disclaims copyright to this source code.  In place of
87034 ** a legal notice, here is a blessing:
87035 **
87036 **    May you do good and not evil.
87037 **    May you find forgiveness for yourself and forgive others.
87038 **    May you share freely, never taking more than you give.
87039 **
87040 *************************************************************************
87041 ** This header file defines the SQLite interface for use by
87042 ** shared libraries that want to be imported as extensions into
87043 ** an SQLite instance.  Shared libraries that intend to be loaded
87044 ** as extensions by SQLite should #include this file instead of
87045 ** sqlite3.h.
87046 */
87047 #ifndef _SQLITE3EXT_H_
87048 #define _SQLITE3EXT_H_
87049 
87050 typedef struct sqlite3_api_routines sqlite3_api_routines;
87051 
87052 /*
87053 ** The following structure holds pointers to all of the SQLite API
87054 ** routines.
87055 **
87056 ** WARNING:  In order to maintain backwards compatibility, add new
87057 ** interfaces to the end of this structure only.  If you insert new
87058 ** interfaces in the middle of this structure, then older different
87059 ** versions of SQLite will not be able to load each others' shared
87060 ** libraries!
87061 */
87062 struct sqlite3_api_routines {
87063   void * (*aggregate_context)(sqlite3_context*,int nBytes);
87064   int  (*aggregate_count)(sqlite3_context*);
87065   int  (*bind_blob)(sqlite3_stmt*,int,const void*,int n,void(*)(void*));
87066   int  (*bind_double)(sqlite3_stmt*,int,double);
87067   int  (*bind_int)(sqlite3_stmt*,int,int);
87068   int  (*bind_int64)(sqlite3_stmt*,int,sqlite_int64);
87069   int  (*bind_null)(sqlite3_stmt*,int);
87070   int  (*bind_parameter_count)(sqlite3_stmt*);
87071   int  (*bind_parameter_index)(sqlite3_stmt*,const char*zName);
87072   const char * (*bind_parameter_name)(sqlite3_stmt*,int);
87073   int  (*bind_text)(sqlite3_stmt*,int,const char*,int n,void(*)(void*));
87074   int  (*bind_text16)(sqlite3_stmt*,int,const void*,int,void(*)(void*));
87075   int  (*bind_value)(sqlite3_stmt*,int,const sqlite3_value*);
87076   int  (*busy_handler)(sqlite3*,int(*)(void*,int),void*);
87077   int  (*busy_timeout)(sqlite3*,int ms);
87078   int  (*changes)(sqlite3*);
87079   int  (*close)(sqlite3*);
87080   int  (*collation_needed)(sqlite3*,void*,void(*)(void*,sqlite3*,int eTextRep,const char*));
87081   int  (*collation_needed16)(sqlite3*,void*,void(*)(void*,sqlite3*,int eTextRep,const void*));
87082   const void * (*column_blob)(sqlite3_stmt*,int iCol);
87083   int  (*column_bytes)(sqlite3_stmt*,int iCol);
87084   int  (*column_bytes16)(sqlite3_stmt*,int iCol);
87085   int  (*column_count)(sqlite3_stmt*pStmt);
87086   const char * (*column_database_name)(sqlite3_stmt*,int);
87087   const void * (*column_database_name16)(sqlite3_stmt*,int);
87088   const char * (*column_decltype)(sqlite3_stmt*,int i);
87089   const void * (*column_decltype16)(sqlite3_stmt*,int);
87090   double  (*column_double)(sqlite3_stmt*,int iCol);
87091   int  (*column_int)(sqlite3_stmt*,int iCol);
87092   sqlite_int64  (*column_int64)(sqlite3_stmt*,int iCol);
87093   const char * (*column_name)(sqlite3_stmt*,int);
87094   const void * (*column_name16)(sqlite3_stmt*,int);
87095   const char * (*column_origin_name)(sqlite3_stmt*,int);
87096   const void * (*column_origin_name16)(sqlite3_stmt*,int);
87097   const char * (*column_table_name)(sqlite3_stmt*,int);
87098   const void * (*column_table_name16)(sqlite3_stmt*,int);
87099   const unsigned char * (*column_text)(sqlite3_stmt*,int iCol);
87100   const void * (*column_text16)(sqlite3_stmt*,int iCol);
87101   int  (*column_type)(sqlite3_stmt*,int iCol);
87102   sqlite3_value* (*column_value)(sqlite3_stmt*,int iCol);
87103   void * (*commit_hook)(sqlite3*,int(*)(void*),void*);
87104   int  (*complete)(const char*sql);
87105   int  (*complete16)(const void*sql);
87106   int  (*create_collation)(sqlite3*,const char*,int,void*,int(*)(void*,int,const void*,int,const void*));
87107   int  (*create_collation16)(sqlite3*,const void*,int,void*,int(*)(void*,int,const void*,int,const void*));
87108   int  (*create_function)(sqlite3*,const char*,int,int,void*,void (*xFunc)(sqlite3_context*,int,sqlite3_value**),void (*xStep)(sqlite3_context*,int,sqlite3_value**),void (*xFinal)(sqlite3_context*));
87109   int  (*create_function16)(sqlite3*,const void*,int,int,void*,void (*xFunc)(sqlite3_context*,int,sqlite3_value**),void (*xStep)(sqlite3_context*,int,sqlite3_value**),void (*xFinal)(sqlite3_context*));
87110   int (*create_module)(sqlite3*,const char*,const sqlite3_module*,void*);
87111   int  (*data_count)(sqlite3_stmt*pStmt);
87112   sqlite3 * (*db_handle)(sqlite3_stmt*);
87113   int (*declare_vtab)(sqlite3*,const char*);
87114   int  (*enable_shared_cache)(int);
87115   int  (*errcode)(sqlite3*db);
87116   const char * (*errmsg)(sqlite3*);
87117   const void * (*errmsg16)(sqlite3*);
87118   int  (*exec)(sqlite3*,const char*,sqlite3_callback,void*,char**);
87119   int  (*expired)(sqlite3_stmt*);
87120   int  (*finalize)(sqlite3_stmt*pStmt);
87121   void  (*free)(void*);
87122   void  (*free_table)(char**result);
87123   int  (*get_autocommit)(sqlite3*);
87124   void * (*get_auxdata)(sqlite3_context*,int);
87125   int  (*get_table)(sqlite3*,const char*,char***,int*,int*,char**);
87126   int  (*global_recover)(void);
87127   void  (*interruptx)(sqlite3*);
87128   sqlite_int64  (*last_insert_rowid)(sqlite3*);
87129   const char * (*libversion)(void);
87130   int  (*libversion_number)(void);
87131   void *(*malloc)(int);
87132   char * (*mprintf)(const char*,...);
87133   int  (*open)(const char*,sqlite3**);
87134   int  (*open16)(const void*,sqlite3**);
87135   int  (*prepare)(sqlite3*,const char*,int,sqlite3_stmt**,const char**);
87136   int  (*prepare16)(sqlite3*,const void*,int,sqlite3_stmt**,const void**);
87137   void * (*profile)(sqlite3*,void(*)(void*,const char*,sqlite_uint64),void*);
87138   void  (*progress_handler)(sqlite3*,int,int(*)(void*),void*);
87139   void *(*realloc)(void*,int);
87140   int  (*reset)(sqlite3_stmt*pStmt);
87141   void  (*result_blob)(sqlite3_context*,const void*,int,void(*)(void*));
87142   void  (*result_double)(sqlite3_context*,double);
87143   void  (*result_error)(sqlite3_context*,const char*,int);
87144   void  (*result_error16)(sqlite3_context*,const void*,int);
87145   void  (*result_int)(sqlite3_context*,int);
87146   void  (*result_int64)(sqlite3_context*,sqlite_int64);
87147   void  (*result_null)(sqlite3_context*);
87148   void  (*result_text)(sqlite3_context*,const char*,int,void(*)(void*));
87149   void  (*result_text16)(sqlite3_context*,const void*,int,void(*)(void*));
87150   void  (*result_text16be)(sqlite3_context*,const void*,int,void(*)(void*));
87151   void  (*result_text16le)(sqlite3_context*,const void*,int,void(*)(void*));
87152   void  (*result_value)(sqlite3_context*,sqlite3_value*);
87153   void * (*rollback_hook)(sqlite3*,void(*)(void*),void*);
87154   int  (*set_authorizer)(sqlite3*,int(*)(void*,int,const char*,const char*,const char*,const char*),void*);
87155   void  (*set_auxdata)(sqlite3_context*,int,void*,void (*)(void*));
87156   char * (*snprintf)(int,char*,const char*,...);
87157   int  (*step)(sqlite3_stmt*);
87158   int  (*table_column_metadata)(sqlite3*,const char*,const char*,const char*,char const**,char const**,int*,int*,int*);
87159   void  (*thread_cleanup)(void);
87160   int  (*total_changes)(sqlite3*);
87161   void * (*trace)(sqlite3*,void(*xTrace)(void*,const char*),void*);
87162   int  (*transfer_bindings)(sqlite3_stmt*,sqlite3_stmt*);
87163   void * (*update_hook)(sqlite3*,void(*)(void*,int ,char const*,char const*,sqlite_int64),void*);
87164   void * (*user_data)(sqlite3_context*);
87165   const void * (*value_blob)(sqlite3_value*);
87166   int  (*value_bytes)(sqlite3_value*);
87167   int  (*value_bytes16)(sqlite3_value*);
87168   double  (*value_double)(sqlite3_value*);
87169   int  (*value_int)(sqlite3_value*);
87170   sqlite_int64  (*value_int64)(sqlite3_value*);
87171   int  (*value_numeric_type)(sqlite3_value*);
87172   const unsigned char * (*value_text)(sqlite3_value*);
87173   const void * (*value_text16)(sqlite3_value*);
87174   const void * (*value_text16be)(sqlite3_value*);
87175   const void * (*value_text16le)(sqlite3_value*);
87176   int  (*value_type)(sqlite3_value*);
87177   char *(*vmprintf)(const char*,va_list);
87178   /* Added ??? */
87179   int (*overload_function)(sqlite3*, const char *zFuncName, int nArg);
87180   /* Added by 3.3.13 */
87181   int (*prepare_v2)(sqlite3*,const char*,int,sqlite3_stmt**,const char**);
87182   int (*prepare16_v2)(sqlite3*,const void*,int,sqlite3_stmt**,const void**);
87183   int (*clear_bindings)(sqlite3_stmt*);
87184   /* Added by 3.4.1 */
87185   int (*create_module_v2)(sqlite3*,const char*,const sqlite3_module*,void*,void (*xDestroy)(void *));
87186   /* Added by 3.5.0 */
87187   int (*bind_zeroblob)(sqlite3_stmt*,int,int);
87188   int (*blob_bytes)(sqlite3_blob*);
87189   int (*blob_close)(sqlite3_blob*);
87190   int (*blob_open)(sqlite3*,const char*,const char*,const char*,sqlite3_int64,int,sqlite3_blob**);
87191   int (*blob_read)(sqlite3_blob*,void*,int,int);
87192   int (*blob_write)(sqlite3_blob*,const void*,int,int);
87193   int (*create_collation_v2)(sqlite3*,const char*,int,void*,int(*)(void*,int,const void*,int,const void*),void(*)(void*));
87194   int (*file_control)(sqlite3*,const char*,int,void*);
87195   sqlite3_int64 (*memory_highwater)(int);
87196   sqlite3_int64 (*memory_used)(void);
87197   sqlite3_mutex *(*mutex_alloc)(int);
87198   void (*mutex_enter)(sqlite3_mutex*);
87199   void (*mutex_free)(sqlite3_mutex*);
87200   void (*mutex_leave)(sqlite3_mutex*);
87201   int (*mutex_try)(sqlite3_mutex*);
87202   int (*open_v2)(const char*,sqlite3**,int,const char*);
87203   int (*release_memory)(int);
87204   void (*result_error_nomem)(sqlite3_context*);
87205   void (*result_error_toobig)(sqlite3_context*);
87206   int (*sleep)(int);
87207   void (*soft_heap_limit)(int);
87208   sqlite3_vfs *(*vfs_find)(const char*);
87209   int (*vfs_register)(sqlite3_vfs*,int);
87210   int (*vfs_unregister)(sqlite3_vfs*);
87211   int (*xthreadsafe)(void);
87212   void (*result_zeroblob)(sqlite3_context*,int);
87213   void (*result_error_code)(sqlite3_context*,int);
87214   int (*test_control)(int, ...);
87215   void (*randomness)(int,void*);
87216   sqlite3 *(*context_db_handle)(sqlite3_context*);
87217   int (*extended_result_codes)(sqlite3*,int);
87218   int (*limit)(sqlite3*,int,int);
87219   sqlite3_stmt *(*next_stmt)(sqlite3*,sqlite3_stmt*);
87220   const char *(*sql)(sqlite3_stmt*);
87221   int (*status)(int,int*,int*,int);
87222   int (*backup_finish)(sqlite3_backup*);
87223   sqlite3_backup *(*backup_init)(sqlite3*,const char*,sqlite3*,const char*);
87224   int (*backup_pagecount)(sqlite3_backup*);
87225   int (*backup_remaining)(sqlite3_backup*);
87226   int (*backup_step)(sqlite3_backup*,int);
87227   const char *(*compileoption_get)(int);
87228   int (*compileoption_used)(const char*);
87229   int (*create_function_v2)(sqlite3*,const char*,int,int,void*,void (*xFunc)(sqlite3_context*,int,sqlite3_value**),void (*xStep)(sqlite3_context*,int,sqlite3_value**),void (*xFinal)(sqlite3_context*),void(*xDestroy)(void*));
87230   int (*db_config)(sqlite3*,int,...);
87231   sqlite3_mutex *(*db_mutex)(sqlite3*);
87232   int (*db_status)(sqlite3*,int,int*,int*,int);
87233   int (*extended_errcode)(sqlite3*);
87234   void (*log)(int,const char*,...);
87235   sqlite3_int64 (*soft_heap_limit64)(sqlite3_int64);
87236   const char *(*sourceid)(void);
87237   int (*stmt_status)(sqlite3_stmt*,int,int);
87238   int (*strnicmp)(const char*,const char*,int);
87239   int (*unlock_notify)(sqlite3*,void(*)(void**,int),void*);
87240   int (*wal_autocheckpoint)(sqlite3*,int);
87241   int (*wal_checkpoint)(sqlite3*,const char*);
87242   void *(*wal_hook)(sqlite3*,int(*)(void*,sqlite3*,const char*,int),void*);
87243 };
87244 
87245 /*
87246 ** The following macros redefine the API routines so that they are
87247 ** redirected throught the global sqlite3_api structure.
87248 **
87249 ** This header file is also used by the loadext.c source file
87250 ** (part of the main SQLite library - not an extension) so that
87251 ** it can get access to the sqlite3_api_routines structure
87252 ** definition.  But the main library does not want to redefine
87253 ** the API.  So the redefinition macros are only valid if the
87254 ** SQLITE_CORE macros is undefined.
87255 */
87256 #ifndef SQLITE_CORE
87257 #define sqlite3_aggregate_context      sqlite3_api->aggregate_context
87258 #ifndef SQLITE_OMIT_DEPRECATED
87259 #define sqlite3_aggregate_count        sqlite3_api->aggregate_count
87260 #endif
87261 #define sqlite3_bind_blob              sqlite3_api->bind_blob
87262 #define sqlite3_bind_double            sqlite3_api->bind_double
87263 #define sqlite3_bind_int               sqlite3_api->bind_int
87264 #define sqlite3_bind_int64             sqlite3_api->bind_int64
87265 #define sqlite3_bind_null              sqlite3_api->bind_null
87266 #define sqlite3_bind_parameter_count   sqlite3_api->bind_parameter_count
87267 #define sqlite3_bind_parameter_index   sqlite3_api->bind_parameter_index
87268 #define sqlite3_bind_parameter_name    sqlite3_api->bind_parameter_name
87269 #define sqlite3_bind_text              sqlite3_api->bind_text
87270 #define sqlite3_bind_text16            sqlite3_api->bind_text16
87271 #define sqlite3_bind_value             sqlite3_api->bind_value
87272 #define sqlite3_busy_handler           sqlite3_api->busy_handler
87273 #define sqlite3_busy_timeout           sqlite3_api->busy_timeout
87274 #define sqlite3_changes                sqlite3_api->changes
87275 #define sqlite3_close                  sqlite3_api->close
87276 #define sqlite3_collation_needed       sqlite3_api->collation_needed
87277 #define sqlite3_collation_needed16     sqlite3_api->collation_needed16
87278 #define sqlite3_column_blob            sqlite3_api->column_blob
87279 #define sqlite3_column_bytes           sqlite3_api->column_bytes
87280 #define sqlite3_column_bytes16         sqlite3_api->column_bytes16
87281 #define sqlite3_column_count           sqlite3_api->column_count
87282 #define sqlite3_column_database_name   sqlite3_api->column_database_name
87283 #define sqlite3_column_database_name16 sqlite3_api->column_database_name16
87284 #define sqlite3_column_decltype        sqlite3_api->column_decltype
87285 #define sqlite3_column_decltype16      sqlite3_api->column_decltype16
87286 #define sqlite3_column_double          sqlite3_api->column_double
87287 #define sqlite3_column_int             sqlite3_api->column_int
87288 #define sqlite3_column_int64           sqlite3_api->column_int64
87289 #define sqlite3_column_name            sqlite3_api->column_name
87290 #define sqlite3_column_name16          sqlite3_api->column_name16
87291 #define sqlite3_column_origin_name     sqlite3_api->column_origin_name
87292 #define sqlite3_column_origin_name16   sqlite3_api->column_origin_name16
87293 #define sqlite3_column_table_name      sqlite3_api->column_table_name
87294 #define sqlite3_column_table_name16    sqlite3_api->column_table_name16
87295 #define sqlite3_column_text            sqlite3_api->column_text
87296 #define sqlite3_column_text16          sqlite3_api->column_text16
87297 #define sqlite3_column_type            sqlite3_api->column_type
87298 #define sqlite3_column_value           sqlite3_api->column_value
87299 #define sqlite3_commit_hook            sqlite3_api->commit_hook
87300 #define sqlite3_complete               sqlite3_api->complete
87301 #define sqlite3_complete16             sqlite3_api->complete16
87302 #define sqlite3_create_collation       sqlite3_api->create_collation
87303 #define sqlite3_create_collation16     sqlite3_api->create_collation16
87304 #define sqlite3_create_function        sqlite3_api->create_function
87305 #define sqlite3_create_function16      sqlite3_api->create_function16
87306 #define sqlite3_create_module          sqlite3_api->create_module
87307 #define sqlite3_create_module_v2       sqlite3_api->create_module_v2
87308 #define sqlite3_data_count             sqlite3_api->data_count
87309 #define sqlite3_db_handle              sqlite3_api->db_handle
87310 #define sqlite3_declare_vtab           sqlite3_api->declare_vtab
87311 #define sqlite3_enable_shared_cache    sqlite3_api->enable_shared_cache
87312 #define sqlite3_errcode                sqlite3_api->errcode
87313 #define sqlite3_errmsg                 sqlite3_api->errmsg
87314 #define sqlite3_errmsg16               sqlite3_api->errmsg16
87315 #define sqlite3_exec                   sqlite3_api->exec
87316 #ifndef SQLITE_OMIT_DEPRECATED
87317 #define sqlite3_expired                sqlite3_api->expired
87318 #endif
87319 #define sqlite3_finalize               sqlite3_api->finalize
87320 #define sqlite3_free                   sqlite3_api->free
87321 #define sqlite3_free_table             sqlite3_api->free_table
87322 #define sqlite3_get_autocommit         sqlite3_api->get_autocommit
87323 #define sqlite3_get_auxdata            sqlite3_api->get_auxdata
87324 #define sqlite3_get_table              sqlite3_api->get_table
87325 #ifndef SQLITE_OMIT_DEPRECATED
87326 #define sqlite3_global_recover         sqlite3_api->global_recover
87327 #endif
87328 #define sqlite3_interrupt              sqlite3_api->interruptx
87329 #define sqlite3_last_insert_rowid      sqlite3_api->last_insert_rowid
87330 #define sqlite3_libversion             sqlite3_api->libversion
87331 #define sqlite3_libversion_number      sqlite3_api->libversion_number
87332 #define sqlite3_malloc                 sqlite3_api->malloc
87333 #define sqlite3_mprintf                sqlite3_api->mprintf
87334 #define sqlite3_open                   sqlite3_api->open
87335 #define sqlite3_open16                 sqlite3_api->open16
87336 #define sqlite3_prepare                sqlite3_api->prepare
87337 #define sqlite3_prepare16              sqlite3_api->prepare16
87338 #define sqlite3_prepare_v2             sqlite3_api->prepare_v2
87339 #define sqlite3_prepare16_v2           sqlite3_api->prepare16_v2
87340 #define sqlite3_profile                sqlite3_api->profile
87341 #define sqlite3_progress_handler       sqlite3_api->progress_handler
87342 #define sqlite3_realloc                sqlite3_api->realloc
87343 #define sqlite3_reset                  sqlite3_api->reset
87344 #define sqlite3_result_blob            sqlite3_api->result_blob
87345 #define sqlite3_result_double          sqlite3_api->result_double
87346 #define sqlite3_result_error           sqlite3_api->result_error
87347 #define sqlite3_result_error16         sqlite3_api->result_error16
87348 #define sqlite3_result_int             sqlite3_api->result_int
87349 #define sqlite3_result_int64           sqlite3_api->result_int64
87350 #define sqlite3_result_null            sqlite3_api->result_null
87351 #define sqlite3_result_text            sqlite3_api->result_text
87352 #define sqlite3_result_text16          sqlite3_api->result_text16
87353 #define sqlite3_result_text16be        sqlite3_api->result_text16be
87354 #define sqlite3_result_text16le        sqlite3_api->result_text16le
87355 #define sqlite3_result_value           sqlite3_api->result_value
87356 #define sqlite3_rollback_hook          sqlite3_api->rollback_hook
87357 #define sqlite3_set_authorizer         sqlite3_api->set_authorizer
87358 #define sqlite3_set_auxdata            sqlite3_api->set_auxdata
87359 #define sqlite3_snprintf               sqlite3_api->snprintf
87360 #define sqlite3_step                   sqlite3_api->step
87361 #define sqlite3_table_column_metadata  sqlite3_api->table_column_metadata
87362 #define sqlite3_thread_cleanup         sqlite3_api->thread_cleanup
87363 #define sqlite3_total_changes          sqlite3_api->total_changes
87364 #define sqlite3_trace                  sqlite3_api->trace
87365 #ifndef SQLITE_OMIT_DEPRECATED
87366 #define sqlite3_transfer_bindings      sqlite3_api->transfer_bindings
87367 #endif
87368 #define sqlite3_update_hook            sqlite3_api->update_hook
87369 #define sqlite3_user_data              sqlite3_api->user_data
87370 #define sqlite3_value_blob             sqlite3_api->value_blob
87371 #define sqlite3_value_bytes            sqlite3_api->value_bytes
87372 #define sqlite3_value_bytes16          sqlite3_api->value_bytes16
87373 #define sqlite3_value_double           sqlite3_api->value_double
87374 #define sqlite3_value_int              sqlite3_api->value_int
87375 #define sqlite3_value_int64            sqlite3_api->value_int64
87376 #define sqlite3_value_numeric_type     sqlite3_api->value_numeric_type
87377 #define sqlite3_value_text             sqlite3_api->value_text
87378 #define sqlite3_value_text16           sqlite3_api->value_text16
87379 #define sqlite3_value_text16be         sqlite3_api->value_text16be
87380 #define sqlite3_value_text16le         sqlite3_api->value_text16le
87381 #define sqlite3_value_type             sqlite3_api->value_type
87382 #define sqlite3_vmprintf               sqlite3_api->vmprintf
87383 #define sqlite3_overload_function      sqlite3_api->overload_function
87384 #define sqlite3_prepare_v2             sqlite3_api->prepare_v2
87385 #define sqlite3_prepare16_v2           sqlite3_api->prepare16_v2
87386 #define sqlite3_clear_bindings         sqlite3_api->clear_bindings
87387 #define sqlite3_bind_zeroblob          sqlite3_api->bind_zeroblob
87388 #define sqlite3_blob_bytes             sqlite3_api->blob_bytes
87389 #define sqlite3_blob_close             sqlite3_api->blob_close
87390 #define sqlite3_blob_open              sqlite3_api->blob_open
87391 #define sqlite3_blob_read              sqlite3_api->blob_read
87392 #define sqlite3_blob_write             sqlite3_api->blob_write
87393 #define sqlite3_create_collation_v2    sqlite3_api->create_collation_v2
87394 #define sqlite3_file_control           sqlite3_api->file_control
87395 #define sqlite3_memory_highwater       sqlite3_api->memory_highwater
87396 #define sqlite3_memory_used            sqlite3_api->memory_used
87397 #define sqlite3_mutex_alloc            sqlite3_api->mutex_alloc
87398 #define sqlite3_mutex_enter            sqlite3_api->mutex_enter
87399 #define sqlite3_mutex_free             sqlite3_api->mutex_free
87400 #define sqlite3_mutex_leave            sqlite3_api->mutex_leave
87401 #define sqlite3_mutex_try              sqlite3_api->mutex_try
87402 #define sqlite3_open_v2                sqlite3_api->open_v2
87403 #define sqlite3_release_memory         sqlite3_api->release_memory
87404 #define sqlite3_result_error_nomem     sqlite3_api->result_error_nomem
87405 #define sqlite3_result_error_toobig    sqlite3_api->result_error_toobig
87406 #define sqlite3_sleep                  sqlite3_api->sleep
87407 #define sqlite3_soft_heap_limit        sqlite3_api->soft_heap_limit
87408 #define sqlite3_vfs_find               sqlite3_api->vfs_find
87409 #define sqlite3_vfs_register           sqlite3_api->vfs_register
87410 #define sqlite3_vfs_unregister         sqlite3_api->vfs_unregister
87411 #define sqlite3_threadsafe             sqlite3_api->xthreadsafe
87412 #define sqlite3_result_zeroblob        sqlite3_api->result_zeroblob
87413 #define sqlite3_result_error_code      sqlite3_api->result_error_code
87414 #define sqlite3_test_control           sqlite3_api->test_control
87415 #define sqlite3_randomness             sqlite3_api->randomness
87416 #define sqlite3_context_db_handle      sqlite3_api->context_db_handle
87417 #define sqlite3_extended_result_codes  sqlite3_api->extended_result_codes
87418 #define sqlite3_limit                  sqlite3_api->limit
87419 #define sqlite3_next_stmt              sqlite3_api->next_stmt
87420 #define sqlite3_sql                    sqlite3_api->sql
87421 #define sqlite3_status                 sqlite3_api->status
87422 #define sqlite3_backup_finish          sqlite3_api->backup_finish
87423 #define sqlite3_backup_init            sqlite3_api->backup_init
87424 #define sqlite3_backup_pagecount       sqlite3_api->backup_pagecount
87425 #define sqlite3_backup_remaining       sqlite3_api->backup_remaining
87426 #define sqlite3_backup_step            sqlite3_api->backup_step
87427 #define sqlite3_compileoption_get      sqlite3_api->compileoption_get
87428 #define sqlite3_compileoption_used     sqlite3_api->compileoption_used
87429 #define sqlite3_create_function_v2     sqlite3_api->create_function_v2
87430 #define sqlite3_db_config              sqlite3_api->db_config
87431 #define sqlite3_db_mutex               sqlite3_api->db_mutex
87432 #define sqlite3_db_status              sqlite3_api->db_status
87433 #define sqlite3_extended_errcode       sqlite3_api->extended_errcode
87434 #define sqlite3_log                    sqlite3_api->log
87435 #define sqlite3_soft_heap_limit64      sqlite3_api->soft_heap_limit64
87436 #define sqlite3_sourceid               sqlite3_api->sourceid
87437 #define sqlite3_stmt_status            sqlite3_api->stmt_status
87438 #define sqlite3_strnicmp               sqlite3_api->strnicmp
87439 #define sqlite3_unlock_notify          sqlite3_api->unlock_notify
87440 #define sqlite3_wal_autocheckpoint     sqlite3_api->wal_autocheckpoint
87441 #define sqlite3_wal_checkpoint         sqlite3_api->wal_checkpoint
87442 #define sqlite3_wal_hook               sqlite3_api->wal_hook
87443 #endif /* SQLITE_CORE */
87444 
87445 #define SQLITE_EXTENSION_INIT1     const sqlite3_api_routines *sqlite3_api = 0;
87446 #define SQLITE_EXTENSION_INIT2(v)  sqlite3_api = v;
87447 
87448 #endif /* _SQLITE3EXT_H_ */
87449 
87450 /************** End of sqlite3ext.h ******************************************/
87451 /************** Continuing where we left off in loadext.c ********************/
87452 
87453 #ifndef SQLITE_OMIT_LOAD_EXTENSION
87454 
87455 /*
87456 ** Some API routines are omitted when various features are
87457 ** excluded from a build of SQLite.  Substitute a NULL pointer
87458 ** for any missing APIs.
87459 */
87460 #ifndef SQLITE_ENABLE_COLUMN_METADATA
87461 # define sqlite3_column_database_name   0
87462 # define sqlite3_column_database_name16 0
87463 # define sqlite3_column_table_name      0
87464 # define sqlite3_column_table_name16    0
87465 # define sqlite3_column_origin_name     0
87466 # define sqlite3_column_origin_name16   0
87467 # define sqlite3_table_column_metadata  0
87468 #endif
87469 
87470 #ifdef SQLITE_OMIT_AUTHORIZATION
87471 # define sqlite3_set_authorizer         0
87472 #endif
87473 
87474 #ifdef SQLITE_OMIT_UTF16
87475 # define sqlite3_bind_text16            0
87476 # define sqlite3_collation_needed16     0
87477 # define sqlite3_column_decltype16      0
87478 # define sqlite3_column_name16          0
87479 # define sqlite3_column_text16          0
87480 # define sqlite3_complete16             0
87481 # define sqlite3_create_collation16     0
87482 # define sqlite3_create_function16      0
87483 # define sqlite3_errmsg16               0
87484 # define sqlite3_open16                 0
87485 # define sqlite3_prepare16              0
87486 # define sqlite3_prepare16_v2           0
87487 # define sqlite3_result_error16         0
87488 # define sqlite3_result_text16          0
87489 # define sqlite3_result_text16be        0
87490 # define sqlite3_result_text16le        0
87491 # define sqlite3_value_text16           0
87492 # define sqlite3_value_text16be         0
87493 # define sqlite3_value_text16le         0
87494 # define sqlite3_column_database_name16 0
87495 # define sqlite3_column_table_name16    0
87496 # define sqlite3_column_origin_name16   0
87497 #endif
87498 
87499 #ifdef SQLITE_OMIT_COMPLETE
87500 # define sqlite3_complete 0
87501 # define sqlite3_complete16 0
87502 #endif
87503 
87504 #ifdef SQLITE_OMIT_DECLTYPE
87505 # define sqlite3_column_decltype16      0
87506 # define sqlite3_column_decltype        0
87507 #endif
87508 
87509 #ifdef SQLITE_OMIT_PROGRESS_CALLBACK
87510 # define sqlite3_progress_handler 0
87511 #endif
87512 
87513 #ifdef SQLITE_OMIT_VIRTUALTABLE
87514 # define sqlite3_create_module 0
87515 # define sqlite3_create_module_v2 0
87516 # define sqlite3_declare_vtab 0
87517 #endif
87518 
87519 #ifdef SQLITE_OMIT_SHARED_CACHE
87520 # define sqlite3_enable_shared_cache 0
87521 #endif
87522 
87523 #ifdef SQLITE_OMIT_TRACE
87524 # define sqlite3_profile       0
87525 # define sqlite3_trace         0
87526 #endif
87527 
87528 #ifdef SQLITE_OMIT_GET_TABLE
87529 # define sqlite3_free_table    0
87530 # define sqlite3_get_table     0
87531 #endif
87532 
87533 #ifdef SQLITE_OMIT_INCRBLOB
87534 #define sqlite3_bind_zeroblob  0
87535 #define sqlite3_blob_bytes     0
87536 #define sqlite3_blob_close     0
87537 #define sqlite3_blob_open      0
87538 #define sqlite3_blob_read      0
87539 #define sqlite3_blob_write     0
87540 #endif
87541 
87542 /*
87543 ** The following structure contains pointers to all SQLite API routines.
87544 ** A pointer to this structure is passed into extensions when they are
87545 ** loaded so that the extension can make calls back into the SQLite
87546 ** library.
87547 **
87548 ** When adding new APIs, add them to the bottom of this structure
87549 ** in order to preserve backwards compatibility.
87550 **
87551 ** Extensions that use newer APIs should first call the
87552 ** sqlite3_libversion_number() to make sure that the API they
87553 ** intend to use is supported by the library.  Extensions should
87554 ** also check to make sure that the pointer to the function is
87555 ** not NULL before calling it.
87556 */
87557 static const sqlite3_api_routines sqlite3Apis = {
87558   sqlite3_aggregate_context,
87559 #ifndef SQLITE_OMIT_DEPRECATED
87560   sqlite3_aggregate_count,
87561 #else
87562   0,
87563 #endif
87564   sqlite3_bind_blob,
87565   sqlite3_bind_double,
87566   sqlite3_bind_int,
87567   sqlite3_bind_int64,
87568   sqlite3_bind_null,
87569   sqlite3_bind_parameter_count,
87570   sqlite3_bind_parameter_index,
87571   sqlite3_bind_parameter_name,
87572   sqlite3_bind_text,
87573   sqlite3_bind_text16,
87574   sqlite3_bind_value,
87575   sqlite3_busy_handler,
87576   sqlite3_busy_timeout,
87577   sqlite3_changes,
87578   sqlite3_close,
87579   sqlite3_collation_needed,
87580   sqlite3_collation_needed16,
87581   sqlite3_column_blob,
87582   sqlite3_column_bytes,
87583   sqlite3_column_bytes16,
87584   sqlite3_column_count,
87585   sqlite3_column_database_name,
87586   sqlite3_column_database_name16,
87587   sqlite3_column_decltype,
87588   sqlite3_column_decltype16,
87589   sqlite3_column_double,
87590   sqlite3_column_int,
87591   sqlite3_column_int64,
87592   sqlite3_column_name,
87593   sqlite3_column_name16,
87594   sqlite3_column_origin_name,
87595   sqlite3_column_origin_name16,
87596   sqlite3_column_table_name,
87597   sqlite3_column_table_name16,
87598   sqlite3_column_text,
87599   sqlite3_column_text16,
87600   sqlite3_column_type,
87601   sqlite3_column_value,
87602   sqlite3_commit_hook,
87603   sqlite3_complete,
87604   sqlite3_complete16,
87605   sqlite3_create_collation,
87606   sqlite3_create_collation16,
87607   sqlite3_create_function,
87608   sqlite3_create_function16,
87609   sqlite3_create_module,
87610   sqlite3_data_count,
87611   sqlite3_db_handle,
87612   sqlite3_declare_vtab,
87613   sqlite3_enable_shared_cache,
87614   sqlite3_errcode,
87615   sqlite3_errmsg,
87616   sqlite3_errmsg16,
87617   sqlite3_exec,
87618 #ifndef SQLITE_OMIT_DEPRECATED
87619   sqlite3_expired,
87620 #else
87621   0,
87622 #endif
87623   sqlite3_finalize,
87624   sqlite3_free,
87625   sqlite3_free_table,
87626   sqlite3_get_autocommit,
87627   sqlite3_get_auxdata,
87628   sqlite3_get_table,
87629   0,     /* Was sqlite3_global_recover(), but that function is deprecated */
87630   sqlite3_interrupt,
87631   sqlite3_last_insert_rowid,
87632   sqlite3_libversion,
87633   sqlite3_libversion_number,
87634   sqlite3_malloc,
87635   sqlite3_mprintf,
87636   sqlite3_open,
87637   sqlite3_open16,
87638   sqlite3_prepare,
87639   sqlite3_prepare16,
87640   sqlite3_profile,
87641   sqlite3_progress_handler,
87642   sqlite3_realloc,
87643   sqlite3_reset,
87644   sqlite3_result_blob,
87645   sqlite3_result_double,
87646   sqlite3_result_error,
87647   sqlite3_result_error16,
87648   sqlite3_result_int,
87649   sqlite3_result_int64,
87650   sqlite3_result_null,
87651   sqlite3_result_text,
87652   sqlite3_result_text16,
87653   sqlite3_result_text16be,
87654   sqlite3_result_text16le,
87655   sqlite3_result_value,
87656   sqlite3_rollback_hook,
87657   sqlite3_set_authorizer,
87658   sqlite3_set_auxdata,
87659   sqlite3_snprintf,
87660   sqlite3_step,
87661   sqlite3_table_column_metadata,
87662 #ifndef SQLITE_OMIT_DEPRECATED
87663   sqlite3_thread_cleanup,
87664 #else
87665   0,
87666 #endif
87667   sqlite3_total_changes,
87668   sqlite3_trace,
87669 #ifndef SQLITE_OMIT_DEPRECATED
87670   sqlite3_transfer_bindings,
87671 #else
87672   0,
87673 #endif
87674   sqlite3_update_hook,
87675   sqlite3_user_data,
87676   sqlite3_value_blob,
87677   sqlite3_value_bytes,
87678   sqlite3_value_bytes16,
87679   sqlite3_value_double,
87680   sqlite3_value_int,
87681   sqlite3_value_int64,
87682   sqlite3_value_numeric_type,
87683   sqlite3_value_text,
87684   sqlite3_value_text16,
87685   sqlite3_value_text16be,
87686   sqlite3_value_text16le,
87687   sqlite3_value_type,
87688   sqlite3_vmprintf,
87689   /*
87690   ** The original API set ends here.  All extensions can call any
87691   ** of the APIs above provided that the pointer is not NULL.  But
87692   ** before calling APIs that follow, extension should check the
87693   ** sqlite3_libversion_number() to make sure they are dealing with
87694   ** a library that is new enough to support that API.
87695   *************************************************************************
87696   */
87697   sqlite3_overload_function,
87698 
87699   /*
87700   ** Added after 3.3.13
87701   */
87702   sqlite3_prepare_v2,
87703   sqlite3_prepare16_v2,
87704   sqlite3_clear_bindings,
87705 
87706   /*
87707   ** Added for 3.4.1
87708   */
87709   sqlite3_create_module_v2,
87710 
87711   /*
87712   ** Added for 3.5.0
87713   */
87714   sqlite3_bind_zeroblob,
87715   sqlite3_blob_bytes,
87716   sqlite3_blob_close,
87717   sqlite3_blob_open,
87718   sqlite3_blob_read,
87719   sqlite3_blob_write,
87720   sqlite3_create_collation_v2,
87721   sqlite3_file_control,
87722   sqlite3_memory_highwater,
87723   sqlite3_memory_used,
87724 #ifdef SQLITE_MUTEX_OMIT
87725   0,
87726   0,
87727   0,
87728   0,
87729   0,
87730 #else
87731   sqlite3_mutex_alloc,
87732   sqlite3_mutex_enter,
87733   sqlite3_mutex_free,
87734   sqlite3_mutex_leave,
87735   sqlite3_mutex_try,
87736 #endif
87737   sqlite3_open_v2,
87738   sqlite3_release_memory,
87739   sqlite3_result_error_nomem,
87740   sqlite3_result_error_toobig,
87741   sqlite3_sleep,
87742   sqlite3_soft_heap_limit,
87743   sqlite3_vfs_find,
87744   sqlite3_vfs_register,
87745   sqlite3_vfs_unregister,
87746 
87747   /*
87748   ** Added for 3.5.8
87749   */
87750   sqlite3_threadsafe,
87751   sqlite3_result_zeroblob,
87752   sqlite3_result_error_code,
87753   sqlite3_test_control,
87754   sqlite3_randomness,
87755   sqlite3_context_db_handle,
87756 
87757   /*
87758   ** Added for 3.6.0
87759   */
87760   sqlite3_extended_result_codes,
87761   sqlite3_limit,
87762   sqlite3_next_stmt,
87763   sqlite3_sql,
87764   sqlite3_status,
87765 
87766   /*
87767   ** Added for 3.7.4
87768   */
87769   sqlite3_backup_finish,
87770   sqlite3_backup_init,
87771   sqlite3_backup_pagecount,
87772   sqlite3_backup_remaining,
87773   sqlite3_backup_step,
87774 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
87775   sqlite3_compileoption_get,
87776   sqlite3_compileoption_used,
87777 #else
87778   0,
87779   0,
87780 #endif
87781   sqlite3_create_function_v2,
87782   sqlite3_db_config,
87783   sqlite3_db_mutex,
87784   sqlite3_db_status,
87785   sqlite3_extended_errcode,
87786   sqlite3_log,
87787   sqlite3_soft_heap_limit64,
87788   sqlite3_sourceid,
87789   sqlite3_stmt_status,
87790   sqlite3_strnicmp,
87791 #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
87792   sqlite3_unlock_notify,
87793 #else
87794   0,
87795 #endif
87796 #ifndef SQLITE_OMIT_WAL
87797   sqlite3_wal_autocheckpoint,
87798   sqlite3_wal_checkpoint,
87799   sqlite3_wal_hook,
87800 #else
87801   0,
87802   0,
87803   0,
87804 #endif
87805 };
87806 
87807 /*
87808 ** Attempt to load an SQLite extension library contained in the file
87809 ** zFile.  The entry point is zProc.  zProc may be 0 in which case a
87810 ** default entry point name (sqlite3_extension_init) is used.  Use
87811 ** of the default name is recommended.
87812 **
87813 ** Return SQLITE_OK on success and SQLITE_ERROR if something goes wrong.
87814 **
87815 ** If an error occurs and pzErrMsg is not 0, then fill *pzErrMsg with
87816 ** error message text.  The calling function should free this memory
87817 ** by calling sqlite3DbFree(db, ).
87818 */
87819 static int sqlite3LoadExtension(
87820   sqlite3 *db,          /* Load the extension into this database connection */
87821   const char *zFile,    /* Name of the shared library containing extension */
87822   const char *zProc,    /* Entry point.  Use "sqlite3_extension_init" if 0 */
87823   char **pzErrMsg       /* Put error message here if not 0 */
87824 ){
87825   sqlite3_vfs *pVfs = db->pVfs;
87826   void *handle;
87827   int (*xInit)(sqlite3*,char**,const sqlite3_api_routines*);
87828   char *zErrmsg = 0;
87829   void **aHandle;
87830   const int nMsg = 300;
87831 
87832   if( pzErrMsg ) *pzErrMsg = 0;
87833 
87834   /* Ticket #1863.  To avoid a creating security problems for older
87835   ** applications that relink against newer versions of SQLite, the
87836   ** ability to run load_extension is turned off by default.  One
87837   ** must call sqlite3_enable_load_extension() to turn on extension
87838   ** loading.  Otherwise you get the following error.
87839   */
87840   if( (db->flags & SQLITE_LoadExtension)==0 ){
87841     if( pzErrMsg ){
87842       *pzErrMsg = sqlite3_mprintf("not authorized");
87843     }
87844     return SQLITE_ERROR;
87845   }
87846 
87847   if( zProc==0 ){
87848     zProc = "sqlite3_extension_init";
87849   }
87850 
87851   handle = sqlite3OsDlOpen(pVfs, zFile);
87852   if( handle==0 ){
87853     if( pzErrMsg ){
87854       *pzErrMsg = zErrmsg = sqlite3_malloc(nMsg);
87855       if( zErrmsg ){
87856         sqlite3_snprintf(nMsg, zErrmsg,
87857             "unable to open shared library [%s]", zFile);
87858         sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
87859       }
87860     }
87861     return SQLITE_ERROR;
87862   }
87863   xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*))
87864                    sqlite3OsDlSym(pVfs, handle, zProc);
87865   if( xInit==0 ){
87866     if( pzErrMsg ){
87867       *pzErrMsg = zErrmsg = sqlite3_malloc(nMsg);
87868       if( zErrmsg ){
87869         sqlite3_snprintf(nMsg, zErrmsg,
87870             "no entry point [%s] in shared library [%s]", zProc,zFile);
87871         sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
87872       }
87873       sqlite3OsDlClose(pVfs, handle);
87874     }
87875     return SQLITE_ERROR;
87876   }else if( xInit(db, &zErrmsg, &sqlite3Apis) ){
87877     if( pzErrMsg ){
87878       *pzErrMsg = sqlite3_mprintf("error during initialization: %s", zErrmsg);
87879     }
87880     sqlite3_free(zErrmsg);
87881     sqlite3OsDlClose(pVfs, handle);
87882     return SQLITE_ERROR;
87883   }
87884 
87885   /* Append the new shared library handle to the db->aExtension array. */
87886   aHandle = sqlite3DbMallocZero(db, sizeof(handle)*(db->nExtension+1));
87887   if( aHandle==0 ){
87888     return SQLITE_NOMEM;
87889   }
87890   if( db->nExtension>0 ){
87891     memcpy(aHandle, db->aExtension, sizeof(handle)*db->nExtension);
87892   }
87893   sqlite3DbFree(db, db->aExtension);
87894   db->aExtension = aHandle;
87895 
87896   db->aExtension[db->nExtension++] = handle;
87897   return SQLITE_OK;
87898 }
87899 SQLITE_API int sqlite3_load_extension(
87900   sqlite3 *db,          /* Load the extension into this database connection */
87901   const char *zFile,    /* Name of the shared library containing extension */
87902   const char *zProc,    /* Entry point.  Use "sqlite3_extension_init" if 0 */
87903   char **pzErrMsg       /* Put error message here if not 0 */
87904 ){
87905   int rc;
87906   sqlite3_mutex_enter(db->mutex);
87907   rc = sqlite3LoadExtension(db, zFile, zProc, pzErrMsg);
87908   rc = sqlite3ApiExit(db, rc);
87909   sqlite3_mutex_leave(db->mutex);
87910   return rc;
87911 }
87912 
87913 /*
87914 ** Call this routine when the database connection is closing in order
87915 ** to clean up loaded extensions
87916 */
87917 SQLITE_PRIVATE void sqlite3CloseExtensions(sqlite3 *db){
87918   int i;
87919   assert( sqlite3_mutex_held(db->mutex) );
87920   for(i=0; i<db->nExtension; i++){
87921     sqlite3OsDlClose(db->pVfs, db->aExtension[i]);
87922   }
87923   sqlite3DbFree(db, db->aExtension);
87924 }
87925 
87926 /*
87927 ** Enable or disable extension loading.  Extension loading is disabled by
87928 ** default so as not to open security holes in older applications.
87929 */
87930 SQLITE_API int sqlite3_enable_load_extension(sqlite3 *db, int onoff){
87931   sqlite3_mutex_enter(db->mutex);
87932   if( onoff ){
87933     db->flags |= SQLITE_LoadExtension;
87934   }else{
87935     db->flags &= ~SQLITE_LoadExtension;
87936   }
87937   sqlite3_mutex_leave(db->mutex);
87938   return SQLITE_OK;
87939 }
87940 
87941 #endif /* SQLITE_OMIT_LOAD_EXTENSION */
87942 
87943 /*
87944 ** The auto-extension code added regardless of whether or not extension
87945 ** loading is supported.  We need a dummy sqlite3Apis pointer for that
87946 ** code if regular extension loading is not available.  This is that
87947 ** dummy pointer.
87948 */
87949 #ifdef SQLITE_OMIT_LOAD_EXTENSION
87950 static const sqlite3_api_routines sqlite3Apis = { 0 };
87951 #endif
87952 
87953 
87954 /*
87955 ** The following object holds the list of automatically loaded
87956 ** extensions.
87957 **
87958 ** This list is shared across threads.  The SQLITE_MUTEX_STATIC_MASTER
87959 ** mutex must be held while accessing this list.
87960 */
87961 typedef struct sqlite3AutoExtList sqlite3AutoExtList;
87962 static SQLITE_WSD struct sqlite3AutoExtList {
87963   int nExt;              /* Number of entries in aExt[] */
87964   void (**aExt)(void);   /* Pointers to the extension init functions */
87965 } sqlite3Autoext = { 0, 0 };
87966 
87967 /* The "wsdAutoext" macro will resolve to the autoextension
87968 ** state vector.  If writable static data is unsupported on the target,
87969 ** we have to locate the state vector at run-time.  In the more common
87970 ** case where writable static data is supported, wsdStat can refer directly
87971 ** to the "sqlite3Autoext" state vector declared above.
87972 */
87973 #ifdef SQLITE_OMIT_WSD
87974 # define wsdAutoextInit \
87975   sqlite3AutoExtList *x = &GLOBAL(sqlite3AutoExtList,sqlite3Autoext)
87976 # define wsdAutoext x[0]
87977 #else
87978 # define wsdAutoextInit
87979 # define wsdAutoext sqlite3Autoext
87980 #endif
87981 
87982 
87983 /*
87984 ** Register a statically linked extension that is automatically
87985 ** loaded by every new database connection.
87986 */
87987 SQLITE_API int sqlite3_auto_extension(void (*xInit)(void)){
87988   int rc = SQLITE_OK;
87989 #ifndef SQLITE_OMIT_AUTOINIT
87990   rc = sqlite3_initialize();
87991   if( rc ){
87992     return rc;
87993   }else
87994 #endif
87995   {
87996     int i;
87997 #if SQLITE_THREADSAFE
87998     sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
87999 #endif
88000     wsdAutoextInit;
88001     sqlite3_mutex_enter(mutex);
88002     for(i=0; i<wsdAutoext.nExt; i++){
88003       if( wsdAutoext.aExt[i]==xInit ) break;
88004     }
88005     if( i==wsdAutoext.nExt ){
88006       int nByte = (wsdAutoext.nExt+1)*sizeof(wsdAutoext.aExt[0]);
88007       void (**aNew)(void);
88008       aNew = sqlite3_realloc(wsdAutoext.aExt, nByte);
88009       if( aNew==0 ){
88010         rc = SQLITE_NOMEM;
88011       }else{
88012         wsdAutoext.aExt = aNew;
88013         wsdAutoext.aExt[wsdAutoext.nExt] = xInit;
88014         wsdAutoext.nExt++;
88015       }
88016     }
88017     sqlite3_mutex_leave(mutex);
88018     assert( (rc&0xff)==rc );
88019     return rc;
88020   }
88021 }
88022 
88023 /*
88024 ** Reset the automatic extension loading mechanism.
88025 */
88026 SQLITE_API void sqlite3_reset_auto_extension(void){
88027 #ifndef SQLITE_OMIT_AUTOINIT
88028   if( sqlite3_initialize()==SQLITE_OK )
88029 #endif
88030   {
88031 #if SQLITE_THREADSAFE
88032     sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
88033 #endif
88034     wsdAutoextInit;
88035     sqlite3_mutex_enter(mutex);
88036     sqlite3_free(wsdAutoext.aExt);
88037     wsdAutoext.aExt = 0;
88038     wsdAutoext.nExt = 0;
88039     sqlite3_mutex_leave(mutex);
88040   }
88041 }
88042 
88043 /*
88044 ** Load all automatic extensions.
88045 **
88046 ** If anything goes wrong, set an error in the database connection.
88047 */
88048 SQLITE_PRIVATE void sqlite3AutoLoadExtensions(sqlite3 *db){
88049   int i;
88050   int go = 1;
88051   int (*xInit)(sqlite3*,char**,const sqlite3_api_routines*);
88052 
88053   wsdAutoextInit;
88054   if( wsdAutoext.nExt==0 ){
88055     /* Common case: early out without every having to acquire a mutex */
88056     return;
88057   }
88058   for(i=0; go; i++){
88059     char *zErrmsg;
88060 #if SQLITE_THREADSAFE
88061     sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
88062 #endif
88063     sqlite3_mutex_enter(mutex);
88064     if( i>=wsdAutoext.nExt ){
88065       xInit = 0;
88066       go = 0;
88067     }else{
88068       xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*))
88069               wsdAutoext.aExt[i];
88070     }
88071     sqlite3_mutex_leave(mutex);
88072     zErrmsg = 0;
88073     if( xInit && xInit(db, &zErrmsg, &sqlite3Apis) ){
88074       sqlite3Error(db, SQLITE_ERROR,
88075             "automatic extension loading failed: %s", zErrmsg);
88076       go = 0;
88077     }
88078     sqlite3_free(zErrmsg);
88079   }
88080 }
88081 
88082 /************** End of loadext.c *********************************************/
88083 /************** Begin file pragma.c ******************************************/
88084 /*
88085 ** 2003 April 6
88086 **
88087 ** The author disclaims copyright to this source code.  In place of
88088 ** a legal notice, here is a blessing:
88089 **
88090 **    May you do good and not evil.
88091 **    May you find forgiveness for yourself and forgive others.
88092 **    May you share freely, never taking more than you give.
88093 **
88094 *************************************************************************
88095 ** This file contains code used to implement the PRAGMA command.
88096 */
88097 
88098 /*
88099 ** Interpret the given string as a safety level.  Return 0 for OFF,
88100 ** 1 for ON or NORMAL and 2 for FULL.  Return 1 for an empty or
88101 ** unrecognized string argument.
88102 **
88103 ** Note that the values returned are one less that the values that
88104 ** should be passed into sqlite3BtreeSetSafetyLevel().  The is done
88105 ** to support legacy SQL code.  The safety level used to be boolean
88106 ** and older scripts may have used numbers 0 for OFF and 1 for ON.
88107 */
88108 static u8 getSafetyLevel(const char *z){
88109                              /* 123456789 123456789 */
88110   static const char zText[] = "onoffalseyestruefull";
88111   static const u8 iOffset[] = {0, 1, 2, 4, 9, 12, 16};
88112   static const u8 iLength[] = {2, 2, 3, 5, 3, 4, 4};
88113   static const u8 iValue[] =  {1, 0, 0, 0, 1, 1, 2};
88114   int i, n;
88115   if( sqlite3Isdigit(*z) ){
88116     return (u8)sqlite3Atoi(z);
88117   }
88118   n = sqlite3Strlen30(z);
88119   for(i=0; i<ArraySize(iLength); i++){
88120     if( iLength[i]==n && sqlite3StrNICmp(&zText[iOffset[i]],z,n)==0 ){
88121       return iValue[i];
88122     }
88123   }
88124   return 1;
88125 }
88126 
88127 /*
88128 ** Interpret the given string as a boolean value.
88129 */
88130 SQLITE_PRIVATE u8 sqlite3GetBoolean(const char *z){
88131   return getSafetyLevel(z)&1;
88132 }
88133 
88134 /* The sqlite3GetBoolean() function is used by other modules but the
88135 ** remainder of this file is specific to PRAGMA processing.  So omit
88136 ** the rest of the file if PRAGMAs are omitted from the build.
88137 */
88138 #if !defined(SQLITE_OMIT_PRAGMA)
88139 
88140 /*
88141 ** Interpret the given string as a locking mode value.
88142 */
88143 static int getLockingMode(const char *z){
88144   if( z ){
88145     if( 0==sqlite3StrICmp(z, "exclusive") ) return PAGER_LOCKINGMODE_EXCLUSIVE;
88146     if( 0==sqlite3StrICmp(z, "normal") ) return PAGER_LOCKINGMODE_NORMAL;
88147   }
88148   return PAGER_LOCKINGMODE_QUERY;
88149 }
88150 
88151 #ifndef SQLITE_OMIT_AUTOVACUUM
88152 /*
88153 ** Interpret the given string as an auto-vacuum mode value.
88154 **
88155 ** The following strings, "none", "full" and "incremental" are
88156 ** acceptable, as are their numeric equivalents: 0, 1 and 2 respectively.
88157 */
88158 static int getAutoVacuum(const char *z){
88159   int i;
88160   if( 0==sqlite3StrICmp(z, "none") ) return BTREE_AUTOVACUUM_NONE;
88161   if( 0==sqlite3StrICmp(z, "full") ) return BTREE_AUTOVACUUM_FULL;
88162   if( 0==sqlite3StrICmp(z, "incremental") ) return BTREE_AUTOVACUUM_INCR;
88163   i = sqlite3Atoi(z);
88164   return (u8)((i>=0&&i<=2)?i:0);
88165 }
88166 #endif /* ifndef SQLITE_OMIT_AUTOVACUUM */
88167 
88168 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
88169 /*
88170 ** Interpret the given string as a temp db location. Return 1 for file
88171 ** backed temporary databases, 2 for the Red-Black tree in memory database
88172 ** and 0 to use the compile-time default.
88173 */
88174 static int getTempStore(const char *z){
88175   if( z[0]>='0' && z[0]<='2' ){
88176     return z[0] - '0';
88177   }else if( sqlite3StrICmp(z, "file")==0 ){
88178     return 1;
88179   }else if( sqlite3StrICmp(z, "memory")==0 ){
88180     return 2;
88181   }else{
88182     return 0;
88183   }
88184 }
88185 #endif /* SQLITE_PAGER_PRAGMAS */
88186 
88187 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
88188 /*
88189 ** Invalidate temp storage, either when the temp storage is changed
88190 ** from default, or when 'file' and the temp_store_directory has changed
88191 */
88192 static int invalidateTempStorage(Parse *pParse){
88193   sqlite3 *db = pParse->db;
88194   if( db->aDb[1].pBt!=0 ){
88195     if( !db->autoCommit || sqlite3BtreeIsInReadTrans(db->aDb[1].pBt) ){
88196       sqlite3ErrorMsg(pParse, "temporary storage cannot be changed "
88197         "from within a transaction");
88198       return SQLITE_ERROR;
88199     }
88200     sqlite3BtreeClose(db->aDb[1].pBt);
88201     db->aDb[1].pBt = 0;
88202     sqlite3ResetInternalSchema(db, -1);
88203   }
88204   return SQLITE_OK;
88205 }
88206 #endif /* SQLITE_PAGER_PRAGMAS */
88207 
88208 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
88209 /*
88210 ** If the TEMP database is open, close it and mark the database schema
88211 ** as needing reloading.  This must be done when using the SQLITE_TEMP_STORE
88212 ** or DEFAULT_TEMP_STORE pragmas.
88213 */
88214 static int changeTempStorage(Parse *pParse, const char *zStorageType){
88215   int ts = getTempStore(zStorageType);
88216   sqlite3 *db = pParse->db;
88217   if( db->temp_store==ts ) return SQLITE_OK;
88218   if( invalidateTempStorage( pParse ) != SQLITE_OK ){
88219     return SQLITE_ERROR;
88220   }
88221   db->temp_store = (u8)ts;
88222   return SQLITE_OK;
88223 }
88224 #endif /* SQLITE_PAGER_PRAGMAS */
88225 
88226 /*
88227 ** Generate code to return a single integer value.
88228 */
88229 static void returnSingleInt(Parse *pParse, const char *zLabel, i64 value){
88230   Vdbe *v = sqlite3GetVdbe(pParse);
88231   int mem = ++pParse->nMem;
88232   i64 *pI64 = sqlite3DbMallocRaw(pParse->db, sizeof(value));
88233   if( pI64 ){
88234     memcpy(pI64, &value, sizeof(value));
88235   }
88236   sqlite3VdbeAddOp4(v, OP_Int64, 0, mem, 0, (char*)pI64, P4_INT64);
88237   sqlite3VdbeSetNumCols(v, 1);
88238   sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLabel, SQLITE_STATIC);
88239   sqlite3VdbeAddOp2(v, OP_ResultRow, mem, 1);
88240 }
88241 
88242 #ifndef SQLITE_OMIT_FLAG_PRAGMAS
88243 /*
88244 ** Check to see if zRight and zLeft refer to a pragma that queries
88245 ** or changes one of the flags in db->flags.  Return 1 if so and 0 if not.
88246 ** Also, implement the pragma.
88247 */
88248 static int flagPragma(Parse *pParse, const char *zLeft, const char *zRight){
88249   static const struct sPragmaType {
88250     const char *zName;  /* Name of the pragma */
88251     int mask;           /* Mask for the db->flags value */
88252   } aPragma[] = {
88253     { "full_column_names",        SQLITE_FullColNames  },
88254     { "short_column_names",       SQLITE_ShortColNames },
88255     { "count_changes",            SQLITE_CountRows     },
88256     { "empty_result_callbacks",   SQLITE_NullCallback  },
88257     { "legacy_file_format",       SQLITE_LegacyFileFmt },
88258     { "fullfsync",                SQLITE_FullFSync     },
88259     { "checkpoint_fullfsync",     SQLITE_CkptFullFSync },
88260     { "reverse_unordered_selects", SQLITE_ReverseOrder  },
88261 #ifndef SQLITE_OMIT_AUTOMATIC_INDEX
88262     { "automatic_index",          SQLITE_AutoIndex     },
88263 #endif
88264 #ifdef SQLITE_DEBUG
88265     { "sql_trace",                SQLITE_SqlTrace      },
88266     { "vdbe_listing",             SQLITE_VdbeListing   },
88267     { "vdbe_trace",               SQLITE_VdbeTrace     },
88268 #endif
88269 #ifndef SQLITE_OMIT_CHECK
88270     { "ignore_check_constraints", SQLITE_IgnoreChecks  },
88271 #endif
88272     /* The following is VERY experimental */
88273     { "writable_schema",          SQLITE_WriteSchema|SQLITE_RecoveryMode },
88274     { "omit_readlock",            SQLITE_NoReadlock    },
88275 
88276     /* TODO: Maybe it shouldn't be possible to change the ReadUncommitted
88277     ** flag if there are any active statements. */
88278     { "read_uncommitted",         SQLITE_ReadUncommitted },
88279     { "recursive_triggers",       SQLITE_RecTriggers },
88280 
88281     /* This flag may only be set if both foreign-key and trigger support
88282     ** are present in the build.  */
88283 #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
88284     { "foreign_keys",             SQLITE_ForeignKeys },
88285 #endif
88286   };
88287   int i;
88288   const struct sPragmaType *p;
88289   for(i=0, p=aPragma; i<ArraySize(aPragma); i++, p++){
88290     if( sqlite3StrICmp(zLeft, p->zName)==0 ){
88291       sqlite3 *db = pParse->db;
88292       Vdbe *v;
88293       v = sqlite3GetVdbe(pParse);
88294       assert( v!=0 );  /* Already allocated by sqlite3Pragma() */
88295       if( ALWAYS(v) ){
88296         if( zRight==0 ){
88297           returnSingleInt(pParse, p->zName, (db->flags & p->mask)!=0 );
88298         }else{
88299           int mask = p->mask;          /* Mask of bits to set or clear. */
88300           if( db->autoCommit==0 ){
88301             /* Foreign key support may not be enabled or disabled while not
88302             ** in auto-commit mode.  */
88303             mask &= ~(SQLITE_ForeignKeys);
88304           }
88305 
88306           if( sqlite3GetBoolean(zRight) ){
88307             db->flags |= mask;
88308           }else{
88309             db->flags &= ~mask;
88310           }
88311 
88312           /* Many of the flag-pragmas modify the code generated by the SQL
88313           ** compiler (eg. count_changes). So add an opcode to expire all
88314           ** compiled SQL statements after modifying a pragma value.
88315           */
88316           sqlite3VdbeAddOp2(v, OP_Expire, 0, 0);
88317         }
88318       }
88319 
88320       return 1;
88321     }
88322   }
88323   return 0;
88324 }
88325 #endif /* SQLITE_OMIT_FLAG_PRAGMAS */
88326 
88327 /*
88328 ** Return a human-readable name for a constraint resolution action.
88329 */
88330 #ifndef SQLITE_OMIT_FOREIGN_KEY
88331 static const char *actionName(u8 action){
88332   const char *zName;
88333   switch( action ){
88334     case OE_SetNull:  zName = "SET NULL";        break;
88335     case OE_SetDflt:  zName = "SET DEFAULT";     break;
88336     case OE_Cascade:  zName = "CASCADE";         break;
88337     case OE_Restrict: zName = "RESTRICT";        break;
88338     default:          zName = "NO ACTION";
88339                       assert( action==OE_None ); break;
88340   }
88341   return zName;
88342 }
88343 #endif
88344 
88345 
88346 /*
88347 ** Parameter eMode must be one of the PAGER_JOURNALMODE_XXX constants
88348 ** defined in pager.h. This function returns the associated lowercase
88349 ** journal-mode name.
88350 */
88351 SQLITE_PRIVATE const char *sqlite3JournalModename(int eMode){
88352   static char * const azModeName[] = {
88353     "delete", "persist", "off", "truncate", "memory"
88354 #ifndef SQLITE_OMIT_WAL
88355      , "wal"
88356 #endif
88357   };
88358   assert( PAGER_JOURNALMODE_DELETE==0 );
88359   assert( PAGER_JOURNALMODE_PERSIST==1 );
88360   assert( PAGER_JOURNALMODE_OFF==2 );
88361   assert( PAGER_JOURNALMODE_TRUNCATE==3 );
88362   assert( PAGER_JOURNALMODE_MEMORY==4 );
88363   assert( PAGER_JOURNALMODE_WAL==5 );
88364   assert( eMode>=0 && eMode<=ArraySize(azModeName) );
88365 
88366   if( eMode==ArraySize(azModeName) ) return 0;
88367   return azModeName[eMode];
88368 }
88369 
88370 /*
88371 ** Process a pragma statement.
88372 **
88373 ** Pragmas are of this form:
88374 **
88375 **      PRAGMA [database.]id [= value]
88376 **
88377 ** The identifier might also be a string.  The value is a string, and
88378 ** identifier, or a number.  If minusFlag is true, then the value is
88379 ** a number that was preceded by a minus sign.
88380 **
88381 ** If the left side is "database.id" then pId1 is the database name
88382 ** and pId2 is the id.  If the left side is just "id" then pId1 is the
88383 ** id and pId2 is any empty string.
88384 */
88385 SQLITE_PRIVATE void sqlite3Pragma(
88386   Parse *pParse,
88387   Token *pId1,        /* First part of [database.]id field */
88388   Token *pId2,        /* Second part of [database.]id field, or NULL */
88389   Token *pValue,      /* Token for <value>, or NULL */
88390   int minusFlag       /* True if a '-' sign preceded <value> */
88391 ){
88392   char *zLeft = 0;       /* Nul-terminated UTF-8 string <id> */
88393   char *zRight = 0;      /* Nul-terminated UTF-8 string <value>, or NULL */
88394   const char *zDb = 0;   /* The database name */
88395   Token *pId;            /* Pointer to <id> token */
88396   int iDb;               /* Database index for <database> */
88397   sqlite3 *db = pParse->db;
88398   Db *pDb;
88399   Vdbe *v = pParse->pVdbe = sqlite3VdbeCreate(db);
88400   if( v==0 ) return;
88401   sqlite3VdbeRunOnlyOnce(v);
88402   pParse->nMem = 2;
88403 
88404   /* Interpret the [database.] part of the pragma statement. iDb is the
88405   ** index of the database this pragma is being applied to in db.aDb[]. */
88406   iDb = sqlite3TwoPartName(pParse, pId1, pId2, &pId);
88407   if( iDb<0 ) return;
88408   pDb = &db->aDb[iDb];
88409 
88410   /* If the temp database has been explicitly named as part of the
88411   ** pragma, make sure it is open.
88412   */
88413   if( iDb==1 && sqlite3OpenTempDatabase(pParse) ){
88414     return;
88415   }
88416 
88417   zLeft = sqlite3NameFromToken(db, pId);
88418   if( !zLeft ) return;
88419   if( minusFlag ){
88420     zRight = sqlite3MPrintf(db, "-%T", pValue);
88421   }else{
88422     zRight = sqlite3NameFromToken(db, pValue);
88423   }
88424 
88425   assert( pId2 );
88426   zDb = pId2->n>0 ? pDb->zName : 0;
88427   if( sqlite3AuthCheck(pParse, SQLITE_PRAGMA, zLeft, zRight, zDb) ){
88428     goto pragma_out;
88429   }
88430 
88431 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
88432   /*
88433   **  PRAGMA [database.]default_cache_size
88434   **  PRAGMA [database.]default_cache_size=N
88435   **
88436   ** The first form reports the current persistent setting for the
88437   ** page cache size.  The value returned is the maximum number of
88438   ** pages in the page cache.  The second form sets both the current
88439   ** page cache size value and the persistent page cache size value
88440   ** stored in the database file.
88441   **
88442   ** Older versions of SQLite would set the default cache size to a
88443   ** negative number to indicate synchronous=OFF.  These days, synchronous
88444   ** is always on by default regardless of the sign of the default cache
88445   ** size.  But continue to take the absolute value of the default cache
88446   ** size of historical compatibility.
88447   */
88448   if( sqlite3StrICmp(zLeft,"default_cache_size")==0 ){
88449     static const VdbeOpList getCacheSize[] = {
88450       { OP_Transaction, 0, 0,        0},                         /* 0 */
88451       { OP_ReadCookie,  0, 1,        BTREE_DEFAULT_CACHE_SIZE},  /* 1 */
88452       { OP_IfPos,       1, 7,        0},
88453       { OP_Integer,     0, 2,        0},
88454       { OP_Subtract,    1, 2,        1},
88455       { OP_IfPos,       1, 7,        0},
88456       { OP_Integer,     0, 1,        0},                         /* 6 */
88457       { OP_ResultRow,   1, 1,        0},
88458     };
88459     int addr;
88460     if( sqlite3ReadSchema(pParse) ) goto pragma_out;
88461     sqlite3VdbeUsesBtree(v, iDb);
88462     if( !zRight ){
88463       sqlite3VdbeSetNumCols(v, 1);
88464       sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cache_size", SQLITE_STATIC);
88465       pParse->nMem += 2;
88466       addr = sqlite3VdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize);
88467       sqlite3VdbeChangeP1(v, addr, iDb);
88468       sqlite3VdbeChangeP1(v, addr+1, iDb);
88469       sqlite3VdbeChangeP1(v, addr+6, SQLITE_DEFAULT_CACHE_SIZE);
88470     }else{
88471       int size = sqlite3AbsInt32(sqlite3Atoi(zRight));
88472       sqlite3BeginWriteOperation(pParse, 0, iDb);
88473       sqlite3VdbeAddOp2(v, OP_Integer, size, 1);
88474       sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_DEFAULT_CACHE_SIZE, 1);
88475       assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
88476       pDb->pSchema->cache_size = size;
88477       sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
88478     }
88479   }else
88480 
88481   /*
88482   **  PRAGMA [database.]page_size
88483   **  PRAGMA [database.]page_size=N
88484   **
88485   ** The first form reports the current setting for the
88486   ** database page size in bytes.  The second form sets the
88487   ** database page size value.  The value can only be set if
88488   ** the database has not yet been created.
88489   */
88490   if( sqlite3StrICmp(zLeft,"page_size")==0 ){
88491     Btree *pBt = pDb->pBt;
88492     assert( pBt!=0 );
88493     if( !zRight ){
88494       int size = ALWAYS(pBt) ? sqlite3BtreeGetPageSize(pBt) : 0;
88495       returnSingleInt(pParse, "page_size", size);
88496     }else{
88497       /* Malloc may fail when setting the page-size, as there is an internal
88498       ** buffer that the pager module resizes using sqlite3_realloc().
88499       */
88500       db->nextPagesize = sqlite3Atoi(zRight);
88501       if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize, -1, 0) ){
88502         db->mallocFailed = 1;
88503       }
88504     }
88505   }else
88506 
88507   /*
88508   **  PRAGMA [database.]secure_delete
88509   **  PRAGMA [database.]secure_delete=ON/OFF
88510   **
88511   ** The first form reports the current setting for the
88512   ** secure_delete flag.  The second form changes the secure_delete
88513   ** flag setting and reports thenew value.
88514   */
88515   if( sqlite3StrICmp(zLeft,"secure_delete")==0 ){
88516     Btree *pBt = pDb->pBt;
88517     int b = -1;
88518     assert( pBt!=0 );
88519     if( zRight ){
88520       b = sqlite3GetBoolean(zRight);
88521     }
88522     if( pId2->n==0 && b>=0 ){
88523       int ii;
88524       for(ii=0; ii<db->nDb; ii++){
88525         sqlite3BtreeSecureDelete(db->aDb[ii].pBt, b);
88526       }
88527     }
88528     b = sqlite3BtreeSecureDelete(pBt, b);
88529     returnSingleInt(pParse, "secure_delete", b);
88530   }else
88531 
88532   /*
88533   **  PRAGMA [database.]max_page_count
88534   **  PRAGMA [database.]max_page_count=N
88535   **
88536   ** The first form reports the current setting for the
88537   ** maximum number of pages in the database file.  The
88538   ** second form attempts to change this setting.  Both
88539   ** forms return the current setting.
88540   **
88541   **  PRAGMA [database.]page_count
88542   **
88543   ** Return the number of pages in the specified database.
88544   */
88545   if( sqlite3StrICmp(zLeft,"page_count")==0
88546    || sqlite3StrICmp(zLeft,"max_page_count")==0
88547   ){
88548     int iReg;
88549     if( sqlite3ReadSchema(pParse) ) goto pragma_out;
88550     sqlite3CodeVerifySchema(pParse, iDb);
88551     iReg = ++pParse->nMem;
88552     if( zLeft[0]=='p' ){
88553       sqlite3VdbeAddOp2(v, OP_Pagecount, iDb, iReg);
88554     }else{
88555       sqlite3VdbeAddOp3(v, OP_MaxPgcnt, iDb, iReg, sqlite3Atoi(zRight));
88556     }
88557     sqlite3VdbeAddOp2(v, OP_ResultRow, iReg, 1);
88558     sqlite3VdbeSetNumCols(v, 1);
88559     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, SQLITE_TRANSIENT);
88560   }else
88561 
88562   /*
88563   **  PRAGMA [database.]locking_mode
88564   **  PRAGMA [database.]locking_mode = (normal|exclusive)
88565   */
88566   if( sqlite3StrICmp(zLeft,"locking_mode")==0 ){
88567     const char *zRet = "normal";
88568     int eMode = getLockingMode(zRight);
88569 
88570     if( pId2->n==0 && eMode==PAGER_LOCKINGMODE_QUERY ){
88571       /* Simple "PRAGMA locking_mode;" statement. This is a query for
88572       ** the current default locking mode (which may be different to
88573       ** the locking-mode of the main database).
88574       */
88575       eMode = db->dfltLockMode;
88576     }else{
88577       Pager *pPager;
88578       if( pId2->n==0 ){
88579         /* This indicates that no database name was specified as part
88580         ** of the PRAGMA command. In this case the locking-mode must be
88581         ** set on all attached databases, as well as the main db file.
88582         **
88583         ** Also, the sqlite3.dfltLockMode variable is set so that
88584         ** any subsequently attached databases also use the specified
88585         ** locking mode.
88586         */
88587         int ii;
88588         assert(pDb==&db->aDb[0]);
88589         for(ii=2; ii<db->nDb; ii++){
88590           pPager = sqlite3BtreePager(db->aDb[ii].pBt);
88591           sqlite3PagerLockingMode(pPager, eMode);
88592         }
88593         db->dfltLockMode = (u8)eMode;
88594       }
88595       pPager = sqlite3BtreePager(pDb->pBt);
88596       eMode = sqlite3PagerLockingMode(pPager, eMode);
88597     }
88598 
88599     assert(eMode==PAGER_LOCKINGMODE_NORMAL||eMode==PAGER_LOCKINGMODE_EXCLUSIVE);
88600     if( eMode==PAGER_LOCKINGMODE_EXCLUSIVE ){
88601       zRet = "exclusive";
88602     }
88603     sqlite3VdbeSetNumCols(v, 1);
88604     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "locking_mode", SQLITE_STATIC);
88605     sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, zRet, 0);
88606     sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
88607   }else
88608 
88609   /*
88610   **  PRAGMA [database.]journal_mode
88611   **  PRAGMA [database.]journal_mode =
88612   **                      (delete|persist|off|truncate|memory|wal|off)
88613   */
88614   if( sqlite3StrICmp(zLeft,"journal_mode")==0 ){
88615     int eMode;        /* One of the PAGER_JOURNALMODE_XXX symbols */
88616     int ii;           /* Loop counter */
88617 
88618     /* Force the schema to be loaded on all databases.  This cases all
88619     ** database files to be opened and the journal_modes set. */
88620     if( sqlite3ReadSchema(pParse) ){
88621       goto pragma_out;
88622     }
88623 
88624     sqlite3VdbeSetNumCols(v, 1);
88625     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "journal_mode", SQLITE_STATIC);
88626 
88627     if( zRight==0 ){
88628       /* If there is no "=MODE" part of the pragma, do a query for the
88629       ** current mode */
88630       eMode = PAGER_JOURNALMODE_QUERY;
88631     }else{
88632       const char *zMode;
88633       int n = sqlite3Strlen30(zRight);
88634       for(eMode=0; (zMode = sqlite3JournalModename(eMode))!=0; eMode++){
88635         if( sqlite3StrNICmp(zRight, zMode, n)==0 ) break;
88636       }
88637       if( !zMode ){
88638         /* If the "=MODE" part does not match any known journal mode,
88639         ** then do a query */
88640         eMode = PAGER_JOURNALMODE_QUERY;
88641       }
88642     }
88643     if( eMode==PAGER_JOURNALMODE_QUERY && pId2->n==0 ){
88644       /* Convert "PRAGMA journal_mode" into "PRAGMA main.journal_mode" */
88645       iDb = 0;
88646       pId2->n = 1;
88647     }
88648     for(ii=db->nDb-1; ii>=0; ii--){
88649       if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
88650         sqlite3VdbeUsesBtree(v, ii);
88651         sqlite3VdbeAddOp3(v, OP_JournalMode, ii, 1, eMode);
88652       }
88653     }
88654     sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
88655   }else
88656 
88657   /*
88658   **  PRAGMA [database.]journal_size_limit
88659   **  PRAGMA [database.]journal_size_limit=N
88660   **
88661   ** Get or set the size limit on rollback journal files.
88662   */
88663   if( sqlite3StrICmp(zLeft,"journal_size_limit")==0 ){
88664     Pager *pPager = sqlite3BtreePager(pDb->pBt);
88665     i64 iLimit = -2;
88666     if( zRight ){
88667       sqlite3Atoi64(zRight, &iLimit, 1000000, SQLITE_UTF8);
88668       if( iLimit<-1 ) iLimit = -1;
88669     }
88670     iLimit = sqlite3PagerJournalSizeLimit(pPager, iLimit);
88671     returnSingleInt(pParse, "journal_size_limit", iLimit);
88672   }else
88673 
88674 #endif /* SQLITE_OMIT_PAGER_PRAGMAS */
88675 
88676   /*
88677   **  PRAGMA [database.]auto_vacuum
88678   **  PRAGMA [database.]auto_vacuum=N
88679   **
88680   ** Get or set the value of the database 'auto-vacuum' parameter.
88681   ** The value is one of:  0 NONE 1 FULL 2 INCREMENTAL
88682   */
88683 #ifndef SQLITE_OMIT_AUTOVACUUM
88684   if( sqlite3StrICmp(zLeft,"auto_vacuum")==0 ){
88685     Btree *pBt = pDb->pBt;
88686     assert( pBt!=0 );
88687     if( sqlite3ReadSchema(pParse) ){
88688       goto pragma_out;
88689     }
88690     if( !zRight ){
88691       int auto_vacuum;
88692       if( ALWAYS(pBt) ){
88693          auto_vacuum = sqlite3BtreeGetAutoVacuum(pBt);
88694       }else{
88695          auto_vacuum = SQLITE_DEFAULT_AUTOVACUUM;
88696       }
88697       returnSingleInt(pParse, "auto_vacuum", auto_vacuum);
88698     }else{
88699       int eAuto = getAutoVacuum(zRight);
88700       assert( eAuto>=0 && eAuto<=2 );
88701       db->nextAutovac = (u8)eAuto;
88702       if( ALWAYS(eAuto>=0) ){
88703         /* Call SetAutoVacuum() to set initialize the internal auto and
88704         ** incr-vacuum flags. This is required in case this connection
88705         ** creates the database file. It is important that it is created
88706         ** as an auto-vacuum capable db.
88707         */
88708         int rc = sqlite3BtreeSetAutoVacuum(pBt, eAuto);
88709         if( rc==SQLITE_OK && (eAuto==1 || eAuto==2) ){
88710           /* When setting the auto_vacuum mode to either "full" or
88711           ** "incremental", write the value of meta[6] in the database
88712           ** file. Before writing to meta[6], check that meta[3] indicates
88713           ** that this really is an auto-vacuum capable database.
88714           */
88715           static const VdbeOpList setMeta6[] = {
88716             { OP_Transaction,    0,         1,                 0},    /* 0 */
88717             { OP_ReadCookie,     0,         1,         BTREE_LARGEST_ROOT_PAGE},
88718             { OP_If,             1,         0,                 0},    /* 2 */
88719             { OP_Halt,           SQLITE_OK, OE_Abort,          0},    /* 3 */
88720             { OP_Integer,        0,         1,                 0},    /* 4 */
88721             { OP_SetCookie,      0,         BTREE_INCR_VACUUM, 1},    /* 5 */
88722           };
88723           int iAddr;
88724           iAddr = sqlite3VdbeAddOpList(v, ArraySize(setMeta6), setMeta6);
88725           sqlite3VdbeChangeP1(v, iAddr, iDb);
88726           sqlite3VdbeChangeP1(v, iAddr+1, iDb);
88727           sqlite3VdbeChangeP2(v, iAddr+2, iAddr+4);
88728           sqlite3VdbeChangeP1(v, iAddr+4, eAuto-1);
88729           sqlite3VdbeChangeP1(v, iAddr+5, iDb);
88730           sqlite3VdbeUsesBtree(v, iDb);
88731         }
88732       }
88733     }
88734   }else
88735 #endif
88736 
88737   /*
88738   **  PRAGMA [database.]incremental_vacuum(N)
88739   **
88740   ** Do N steps of incremental vacuuming on a database.
88741   */
88742 #ifndef SQLITE_OMIT_AUTOVACUUM
88743   if( sqlite3StrICmp(zLeft,"incremental_vacuum")==0 ){
88744     int iLimit, addr;
88745     if( sqlite3ReadSchema(pParse) ){
88746       goto pragma_out;
88747     }
88748     if( zRight==0 || !sqlite3GetInt32(zRight, &iLimit) || iLimit<=0 ){
88749       iLimit = 0x7fffffff;
88750     }
88751     sqlite3BeginWriteOperation(pParse, 0, iDb);
88752     sqlite3VdbeAddOp2(v, OP_Integer, iLimit, 1);
88753     addr = sqlite3VdbeAddOp1(v, OP_IncrVacuum, iDb);
88754     sqlite3VdbeAddOp1(v, OP_ResultRow, 1);
88755     sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
88756     sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr);
88757     sqlite3VdbeJumpHere(v, addr);
88758   }else
88759 #endif
88760 
88761 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
88762   /*
88763   **  PRAGMA [database.]cache_size
88764   **  PRAGMA [database.]cache_size=N
88765   **
88766   ** The first form reports the current local setting for the
88767   ** page cache size.  The local setting can be different from
88768   ** the persistent cache size value that is stored in the database
88769   ** file itself.  The value returned is the maximum number of
88770   ** pages in the page cache.  The second form sets the local
88771   ** page cache size value.  It does not change the persistent
88772   ** cache size stored on the disk so the cache size will revert
88773   ** to its default value when the database is closed and reopened.
88774   ** N should be a positive integer.
88775   */
88776   if( sqlite3StrICmp(zLeft,"cache_size")==0 ){
88777     if( sqlite3ReadSchema(pParse) ) goto pragma_out;
88778     assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
88779     if( !zRight ){
88780       returnSingleInt(pParse, "cache_size", pDb->pSchema->cache_size);
88781     }else{
88782       int size = sqlite3AbsInt32(sqlite3Atoi(zRight));
88783       pDb->pSchema->cache_size = size;
88784       sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
88785     }
88786   }else
88787 
88788   /*
88789   **   PRAGMA temp_store
88790   **   PRAGMA temp_store = "default"|"memory"|"file"
88791   **
88792   ** Return or set the local value of the temp_store flag.  Changing
88793   ** the local value does not make changes to the disk file and the default
88794   ** value will be restored the next time the database is opened.
88795   **
88796   ** Note that it is possible for the library compile-time options to
88797   ** override this setting
88798   */
88799   if( sqlite3StrICmp(zLeft, "temp_store")==0 ){
88800     if( !zRight ){
88801       returnSingleInt(pParse, "temp_store", db->temp_store);
88802     }else{
88803       changeTempStorage(pParse, zRight);
88804     }
88805   }else
88806 
88807   /*
88808   **   PRAGMA temp_store_directory
88809   **   PRAGMA temp_store_directory = ""|"directory_name"
88810   **
88811   ** Return or set the local value of the temp_store_directory flag.  Changing
88812   ** the value sets a specific directory to be used for temporary files.
88813   ** Setting to a null string reverts to the default temporary directory search.
88814   ** If temporary directory is changed, then invalidateTempStorage.
88815   **
88816   */
88817   if( sqlite3StrICmp(zLeft, "temp_store_directory")==0 ){
88818     if( !zRight ){
88819       if( sqlite3_temp_directory ){
88820         sqlite3VdbeSetNumCols(v, 1);
88821         sqlite3VdbeSetColName(v, 0, COLNAME_NAME,
88822             "temp_store_directory", SQLITE_STATIC);
88823         sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, sqlite3_temp_directory, 0);
88824         sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
88825       }
88826     }else{
88827 #ifndef SQLITE_OMIT_WSD
88828       if( zRight[0] ){
88829         int rc;
88830         int res;
88831         rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
88832         if( rc!=SQLITE_OK || res==0 ){
88833           sqlite3ErrorMsg(pParse, "not a writable directory");
88834           goto pragma_out;
88835         }
88836       }
88837       if( SQLITE_TEMP_STORE==0
88838        || (SQLITE_TEMP_STORE==1 && db->temp_store<=1)
88839        || (SQLITE_TEMP_STORE==2 && db->temp_store==1)
88840       ){
88841         invalidateTempStorage(pParse);
88842       }
88843       sqlite3_free(sqlite3_temp_directory);
88844       if( zRight[0] ){
88845         sqlite3_temp_directory = sqlite3_mprintf("%s", zRight);
88846       }else{
88847         sqlite3_temp_directory = 0;
88848       }
88849 #endif /* SQLITE_OMIT_WSD */
88850     }
88851   }else
88852 
88853 #if !defined(SQLITE_ENABLE_LOCKING_STYLE)
88854 #  if defined(__APPLE__)
88855 #    define SQLITE_ENABLE_LOCKING_STYLE 1
88856 #  else
88857 #    define SQLITE_ENABLE_LOCKING_STYLE 0
88858 #  endif
88859 #endif
88860 #if SQLITE_ENABLE_LOCKING_STYLE
88861   /*
88862    **   PRAGMA [database.]lock_proxy_file
88863    **   PRAGMA [database.]lock_proxy_file = ":auto:"|"lock_file_path"
88864    **
88865    ** Return or set the value of the lock_proxy_file flag.  Changing
88866    ** the value sets a specific file to be used for database access locks.
88867    **
88868    */
88869   if( sqlite3StrICmp(zLeft, "lock_proxy_file")==0 ){
88870     if( !zRight ){
88871       Pager *pPager = sqlite3BtreePager(pDb->pBt);
88872       char *proxy_file_path = NULL;
88873       sqlite3_file *pFile = sqlite3PagerFile(pPager);
88874       sqlite3OsFileControl(pFile, SQLITE_GET_LOCKPROXYFILE,
88875                            &proxy_file_path);
88876 
88877       if( proxy_file_path ){
88878         sqlite3VdbeSetNumCols(v, 1);
88879         sqlite3VdbeSetColName(v, 0, COLNAME_NAME,
88880                               "lock_proxy_file", SQLITE_STATIC);
88881         sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, proxy_file_path, 0);
88882         sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
88883       }
88884     }else{
88885       Pager *pPager = sqlite3BtreePager(pDb->pBt);
88886       sqlite3_file *pFile = sqlite3PagerFile(pPager);
88887       int res;
88888       if( zRight[0] ){
88889         res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
88890                                      zRight);
88891       } else {
88892         res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
88893                                      NULL);
88894       }
88895       if( res!=SQLITE_OK ){
88896         sqlite3ErrorMsg(pParse, "failed to set lock proxy file");
88897         goto pragma_out;
88898       }
88899     }
88900   }else
88901 #endif /* SQLITE_ENABLE_LOCKING_STYLE */
88902 
88903   /*
88904   **   PRAGMA [database.]synchronous
88905   **   PRAGMA [database.]synchronous=OFF|ON|NORMAL|FULL
88906   **
88907   ** Return or set the local value of the synchronous flag.  Changing
88908   ** the local value does not make changes to the disk file and the
88909   ** default value will be restored the next time the database is
88910   ** opened.
88911   */
88912   if( sqlite3StrICmp(zLeft,"synchronous")==0 ){
88913     if( sqlite3ReadSchema(pParse) ) goto pragma_out;
88914     if( !zRight ){
88915       returnSingleInt(pParse, "synchronous", pDb->safety_level-1);
88916     }else{
88917       if( !db->autoCommit ){
88918         sqlite3ErrorMsg(pParse,
88919             "Safety level may not be changed inside a transaction");
88920       }else{
88921         pDb->safety_level = getSafetyLevel(zRight)+1;
88922       }
88923     }
88924   }else
88925 #endif /* SQLITE_OMIT_PAGER_PRAGMAS */
88926 
88927 #ifndef SQLITE_OMIT_FLAG_PRAGMAS
88928   if( flagPragma(pParse, zLeft, zRight) ){
88929     /* The flagPragma() subroutine also generates any necessary code
88930     ** there is nothing more to do here */
88931   }else
88932 #endif /* SQLITE_OMIT_FLAG_PRAGMAS */
88933 
88934 #ifndef SQLITE_OMIT_SCHEMA_PRAGMAS
88935   /*
88936   **   PRAGMA table_info(<table>)
88937   **
88938   ** Return a single row for each column of the named table. The columns of
88939   ** the returned data set are:
88940   **
88941   ** cid:        Column id (numbered from left to right, starting at 0)
88942   ** name:       Column name
88943   ** type:       Column declaration type.
88944   ** notnull:    True if 'NOT NULL' is part of column declaration
88945   ** dflt_value: The default value for the column, if any.
88946   */
88947   if( sqlite3StrICmp(zLeft, "table_info")==0 && zRight ){
88948     Table *pTab;
88949     if( sqlite3ReadSchema(pParse) ) goto pragma_out;
88950     pTab = sqlite3FindTable(db, zRight, zDb);
88951     if( pTab ){
88952       int i;
88953       int nHidden = 0;
88954       Column *pCol;
88955       sqlite3VdbeSetNumCols(v, 6);
88956       pParse->nMem = 6;
88957       sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cid", SQLITE_STATIC);
88958       sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
88959       sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "type", SQLITE_STATIC);
88960       sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "notnull", SQLITE_STATIC);
88961       sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "dflt_value", SQLITE_STATIC);
88962       sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "pk", SQLITE_STATIC);
88963       sqlite3ViewGetColumnNames(pParse, pTab);
88964       for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
88965         if( IsHiddenColumn(pCol) ){
88966           nHidden++;
88967           continue;
88968         }
88969         sqlite3VdbeAddOp2(v, OP_Integer, i-nHidden, 1);
88970         sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pCol->zName, 0);
88971         sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
88972            pCol->zType ? pCol->zType : "", 0);
88973         sqlite3VdbeAddOp2(v, OP_Integer, (pCol->notNull ? 1 : 0), 4);
88974         if( pCol->zDflt ){
88975           sqlite3VdbeAddOp4(v, OP_String8, 0, 5, 0, (char*)pCol->zDflt, 0);
88976         }else{
88977           sqlite3VdbeAddOp2(v, OP_Null, 0, 5);
88978         }
88979         sqlite3VdbeAddOp2(v, OP_Integer, pCol->isPrimKey, 6);
88980         sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 6);
88981       }
88982     }
88983   }else
88984 
88985   if( sqlite3StrICmp(zLeft, "index_info")==0 && zRight ){
88986     Index *pIdx;
88987     Table *pTab;
88988     if( sqlite3ReadSchema(pParse) ) goto pragma_out;
88989     pIdx = sqlite3FindIndex(db, zRight, zDb);
88990     if( pIdx ){
88991       int i;
88992       pTab = pIdx->pTable;
88993       sqlite3VdbeSetNumCols(v, 3);
88994       pParse->nMem = 3;
88995       sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seqno", SQLITE_STATIC);
88996       sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "cid", SQLITE_STATIC);
88997       sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "name", SQLITE_STATIC);
88998       for(i=0; i<pIdx->nColumn; i++){
88999         int cnum = pIdx->aiColumn[i];
89000         sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
89001         sqlite3VdbeAddOp2(v, OP_Integer, cnum, 2);
89002         assert( pTab->nCol>cnum );
89003         sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pTab->aCol[cnum].zName, 0);
89004         sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
89005       }
89006     }
89007   }else
89008 
89009   if( sqlite3StrICmp(zLeft, "index_list")==0 && zRight ){
89010     Index *pIdx;
89011     Table *pTab;
89012     if( sqlite3ReadSchema(pParse) ) goto pragma_out;
89013     pTab = sqlite3FindTable(db, zRight, zDb);
89014     if( pTab ){
89015       v = sqlite3GetVdbe(pParse);
89016       pIdx = pTab->pIndex;
89017       if( pIdx ){
89018         int i = 0;
89019         sqlite3VdbeSetNumCols(v, 3);
89020         pParse->nMem = 3;
89021         sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
89022         sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
89023         sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "unique", SQLITE_STATIC);
89024         while(pIdx){
89025           sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
89026           sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pIdx->zName, 0);
89027           sqlite3VdbeAddOp2(v, OP_Integer, pIdx->onError!=OE_None, 3);
89028           sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
89029           ++i;
89030           pIdx = pIdx->pNext;
89031         }
89032       }
89033     }
89034   }else
89035 
89036   if( sqlite3StrICmp(zLeft, "database_list")==0 ){
89037     int i;
89038     if( sqlite3ReadSchema(pParse) ) goto pragma_out;
89039     sqlite3VdbeSetNumCols(v, 3);
89040     pParse->nMem = 3;
89041     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
89042     sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
89043     sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "file", SQLITE_STATIC);
89044     for(i=0; i<db->nDb; i++){
89045       if( db->aDb[i].pBt==0 ) continue;
89046       assert( db->aDb[i].zName!=0 );
89047       sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
89048       sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, db->aDb[i].zName, 0);
89049       sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
89050            sqlite3BtreeGetFilename(db->aDb[i].pBt), 0);
89051       sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
89052     }
89053   }else
89054 
89055   if( sqlite3StrICmp(zLeft, "collation_list")==0 ){
89056     int i = 0;
89057     HashElem *p;
89058     sqlite3VdbeSetNumCols(v, 2);
89059     pParse->nMem = 2;
89060     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
89061     sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
89062     for(p=sqliteHashFirst(&db->aCollSeq); p; p=sqliteHashNext(p)){
89063       CollSeq *pColl = (CollSeq *)sqliteHashData(p);
89064       sqlite3VdbeAddOp2(v, OP_Integer, i++, 1);
89065       sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pColl->zName, 0);
89066       sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
89067     }
89068   }else
89069 #endif /* SQLITE_OMIT_SCHEMA_PRAGMAS */
89070 
89071 #ifndef SQLITE_OMIT_FOREIGN_KEY
89072   if( sqlite3StrICmp(zLeft, "foreign_key_list")==0 && zRight ){
89073     FKey *pFK;
89074     Table *pTab;
89075     if( sqlite3ReadSchema(pParse) ) goto pragma_out;
89076     pTab = sqlite3FindTable(db, zRight, zDb);
89077     if( pTab ){
89078       v = sqlite3GetVdbe(pParse);
89079       pFK = pTab->pFKey;
89080       if( pFK ){
89081         int i = 0;
89082         sqlite3VdbeSetNumCols(v, 8);
89083         pParse->nMem = 8;
89084         sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "id", SQLITE_STATIC);
89085         sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "seq", SQLITE_STATIC);
89086         sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "table", SQLITE_STATIC);
89087         sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "from", SQLITE_STATIC);
89088         sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "to", SQLITE_STATIC);
89089         sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "on_update", SQLITE_STATIC);
89090         sqlite3VdbeSetColName(v, 6, COLNAME_NAME, "on_delete", SQLITE_STATIC);
89091         sqlite3VdbeSetColName(v, 7, COLNAME_NAME, "match", SQLITE_STATIC);
89092         while(pFK){
89093           int j;
89094           for(j=0; j<pFK->nCol; j++){
89095             char *zCol = pFK->aCol[j].zCol;
89096             char *zOnDelete = (char *)actionName(pFK->aAction[0]);
89097             char *zOnUpdate = (char *)actionName(pFK->aAction[1]);
89098             sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
89099             sqlite3VdbeAddOp2(v, OP_Integer, j, 2);
89100             sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pFK->zTo, 0);
89101             sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0,
89102                               pTab->aCol[pFK->aCol[j].iFrom].zName, 0);
89103             sqlite3VdbeAddOp4(v, zCol ? OP_String8 : OP_Null, 0, 5, 0, zCol, 0);
89104             sqlite3VdbeAddOp4(v, OP_String8, 0, 6, 0, zOnUpdate, 0);
89105             sqlite3VdbeAddOp4(v, OP_String8, 0, 7, 0, zOnDelete, 0);
89106             sqlite3VdbeAddOp4(v, OP_String8, 0, 8, 0, "NONE", 0);
89107             sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 8);
89108           }
89109           ++i;
89110           pFK = pFK->pNextFrom;
89111         }
89112       }
89113     }
89114   }else
89115 #endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
89116 
89117 #ifndef NDEBUG
89118   if( sqlite3StrICmp(zLeft, "parser_trace")==0 ){
89119     if( zRight ){
89120       if( sqlite3GetBoolean(zRight) ){
89121         sqlite3ParserTrace(stderr, "parser: ");
89122       }else{
89123         sqlite3ParserTrace(0, 0);
89124       }
89125     }
89126   }else
89127 #endif
89128 
89129   /* Reinstall the LIKE and GLOB functions.  The variant of LIKE
89130   ** used will be case sensitive or not depending on the RHS.
89131   */
89132   if( sqlite3StrICmp(zLeft, "case_sensitive_like")==0 ){
89133     if( zRight ){
89134       sqlite3RegisterLikeFunctions(db, sqlite3GetBoolean(zRight));
89135     }
89136   }else
89137 
89138 #ifndef SQLITE_INTEGRITY_CHECK_ERROR_MAX
89139 # define SQLITE_INTEGRITY_CHECK_ERROR_MAX 100
89140 #endif
89141 
89142 #ifndef SQLITE_OMIT_INTEGRITY_CHECK
89143   /* Pragma "quick_check" is an experimental reduced version of
89144   ** integrity_check designed to detect most database corruption
89145   ** without most of the overhead of a full integrity-check.
89146   */
89147   if( sqlite3StrICmp(zLeft, "integrity_check")==0
89148    || sqlite3StrICmp(zLeft, "quick_check")==0
89149   ){
89150     int i, j, addr, mxErr;
89151 
89152     /* Code that appears at the end of the integrity check.  If no error
89153     ** messages have been generated, output OK.  Otherwise output the
89154     ** error message
89155     */
89156     static const VdbeOpList endCode[] = {
89157       { OP_AddImm,      1, 0,        0},    /* 0 */
89158       { OP_IfNeg,       1, 0,        0},    /* 1 */
89159       { OP_String8,     0, 3,        0},    /* 2 */
89160       { OP_ResultRow,   3, 1,        0},
89161     };
89162 
89163     int isQuick = (zLeft[0]=='q');
89164 
89165     /* Initialize the VDBE program */
89166     if( sqlite3ReadSchema(pParse) ) goto pragma_out;
89167     pParse->nMem = 6;
89168     sqlite3VdbeSetNumCols(v, 1);
89169     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "integrity_check", SQLITE_STATIC);
89170 
89171     /* Set the maximum error count */
89172     mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
89173     if( zRight ){
89174       sqlite3GetInt32(zRight, &mxErr);
89175       if( mxErr<=0 ){
89176         mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
89177       }
89178     }
89179     sqlite3VdbeAddOp2(v, OP_Integer, mxErr, 1);  /* reg[1] holds errors left */
89180 
89181     /* Do an integrity check on each database file */
89182     for(i=0; i<db->nDb; i++){
89183       HashElem *x;
89184       Hash *pTbls;
89185       int cnt = 0;
89186 
89187       if( OMIT_TEMPDB && i==1 ) continue;
89188 
89189       sqlite3CodeVerifySchema(pParse, i);
89190       addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Halt if out of errors */
89191       sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
89192       sqlite3VdbeJumpHere(v, addr);
89193 
89194       /* Do an integrity check of the B-Tree
89195       **
89196       ** Begin by filling registers 2, 3, ... with the root pages numbers
89197       ** for all tables and indices in the database.
89198       */
89199       assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
89200       pTbls = &db->aDb[i].pSchema->tblHash;
89201       for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
89202         Table *pTab = sqliteHashData(x);
89203         Index *pIdx;
89204         sqlite3VdbeAddOp2(v, OP_Integer, pTab->tnum, 2+cnt);
89205         cnt++;
89206         for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
89207           sqlite3VdbeAddOp2(v, OP_Integer, pIdx->tnum, 2+cnt);
89208           cnt++;
89209         }
89210       }
89211 
89212       /* Make sure sufficient number of registers have been allocated */
89213       if( pParse->nMem < cnt+4 ){
89214         pParse->nMem = cnt+4;
89215       }
89216 
89217       /* Do the b-tree integrity checks */
89218       sqlite3VdbeAddOp3(v, OP_IntegrityCk, 2, cnt, 1);
89219       sqlite3VdbeChangeP5(v, (u8)i);
89220       addr = sqlite3VdbeAddOp1(v, OP_IsNull, 2);
89221       sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
89222          sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zName),
89223          P4_DYNAMIC);
89224       sqlite3VdbeAddOp3(v, OP_Move, 2, 4, 1);
89225       sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 2);
89226       sqlite3VdbeAddOp2(v, OP_ResultRow, 2, 1);
89227       sqlite3VdbeJumpHere(v, addr);
89228 
89229       /* Make sure all the indices are constructed correctly.
89230       */
89231       for(x=sqliteHashFirst(pTbls); x && !isQuick; x=sqliteHashNext(x)){
89232         Table *pTab = sqliteHashData(x);
89233         Index *pIdx;
89234         int loopTop;
89235 
89236         if( pTab->pIndex==0 ) continue;
89237         addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1);  /* Stop if out of errors */
89238         sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
89239         sqlite3VdbeJumpHere(v, addr);
89240         sqlite3OpenTableAndIndices(pParse, pTab, 1, OP_OpenRead);
89241         sqlite3VdbeAddOp2(v, OP_Integer, 0, 2);  /* reg(2) will count entries */
89242         loopTop = sqlite3VdbeAddOp2(v, OP_Rewind, 1, 0);
89243         sqlite3VdbeAddOp2(v, OP_AddImm, 2, 1);   /* increment entry count */
89244         for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
89245           int jmp2;
89246           int r1;
89247           static const VdbeOpList idxErr[] = {
89248             { OP_AddImm,      1, -1,  0},
89249             { OP_String8,     0,  3,  0},    /* 1 */
89250             { OP_Rowid,       1,  4,  0},
89251             { OP_String8,     0,  5,  0},    /* 3 */
89252             { OP_String8,     0,  6,  0},    /* 4 */
89253             { OP_Concat,      4,  3,  3},
89254             { OP_Concat,      5,  3,  3},
89255             { OP_Concat,      6,  3,  3},
89256             { OP_ResultRow,   3,  1,  0},
89257             { OP_IfPos,       1,  0,  0},    /* 9 */
89258             { OP_Halt,        0,  0,  0},
89259           };
89260           r1 = sqlite3GenerateIndexKey(pParse, pIdx, 1, 3, 0);
89261           jmp2 = sqlite3VdbeAddOp4Int(v, OP_Found, j+2, 0, r1, pIdx->nColumn+1);
89262           addr = sqlite3VdbeAddOpList(v, ArraySize(idxErr), idxErr);
89263           sqlite3VdbeChangeP4(v, addr+1, "rowid ", P4_STATIC);
89264           sqlite3VdbeChangeP4(v, addr+3, " missing from index ", P4_STATIC);
89265           sqlite3VdbeChangeP4(v, addr+4, pIdx->zName, P4_TRANSIENT);
89266           sqlite3VdbeJumpHere(v, addr+9);
89267           sqlite3VdbeJumpHere(v, jmp2);
89268         }
89269         sqlite3VdbeAddOp2(v, OP_Next, 1, loopTop+1);
89270         sqlite3VdbeJumpHere(v, loopTop);
89271         for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
89272           static const VdbeOpList cntIdx[] = {
89273              { OP_Integer,      0,  3,  0},
89274              { OP_Rewind,       0,  0,  0},  /* 1 */
89275              { OP_AddImm,       3,  1,  0},
89276              { OP_Next,         0,  0,  0},  /* 3 */
89277              { OP_Eq,           2,  0,  3},  /* 4 */
89278              { OP_AddImm,       1, -1,  0},
89279              { OP_String8,      0,  2,  0},  /* 6 */
89280              { OP_String8,      0,  3,  0},  /* 7 */
89281              { OP_Concat,       3,  2,  2},
89282              { OP_ResultRow,    2,  1,  0},
89283           };
89284           addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1);
89285           sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
89286           sqlite3VdbeJumpHere(v, addr);
89287           addr = sqlite3VdbeAddOpList(v, ArraySize(cntIdx), cntIdx);
89288           sqlite3VdbeChangeP1(v, addr+1, j+2);
89289           sqlite3VdbeChangeP2(v, addr+1, addr+4);
89290           sqlite3VdbeChangeP1(v, addr+3, j+2);
89291           sqlite3VdbeChangeP2(v, addr+3, addr+2);
89292           sqlite3VdbeJumpHere(v, addr+4);
89293           sqlite3VdbeChangeP4(v, addr+6,
89294                      "wrong # of entries in index ", P4_STATIC);
89295           sqlite3VdbeChangeP4(v, addr+7, pIdx->zName, P4_TRANSIENT);
89296         }
89297       }
89298     }
89299     addr = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode);
89300     sqlite3VdbeChangeP2(v, addr, -mxErr);
89301     sqlite3VdbeJumpHere(v, addr+1);
89302     sqlite3VdbeChangeP4(v, addr+2, "ok", P4_STATIC);
89303   }else
89304 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
89305 
89306 #ifndef SQLITE_OMIT_UTF16
89307   /*
89308   **   PRAGMA encoding
89309   **   PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be"
89310   **
89311   ** In its first form, this pragma returns the encoding of the main
89312   ** database. If the database is not initialized, it is initialized now.
89313   **
89314   ** The second form of this pragma is a no-op if the main database file
89315   ** has not already been initialized. In this case it sets the default
89316   ** encoding that will be used for the main database file if a new file
89317   ** is created. If an existing main database file is opened, then the
89318   ** default text encoding for the existing database is used.
89319   **
89320   ** In all cases new databases created using the ATTACH command are
89321   ** created to use the same default text encoding as the main database. If
89322   ** the main database has not been initialized and/or created when ATTACH
89323   ** is executed, this is done before the ATTACH operation.
89324   **
89325   ** In the second form this pragma sets the text encoding to be used in
89326   ** new database files created using this database handle. It is only
89327   ** useful if invoked immediately after the main database i
89328   */
89329   if( sqlite3StrICmp(zLeft, "encoding")==0 ){
89330     static const struct EncName {
89331       char *zName;
89332       u8 enc;
89333     } encnames[] = {
89334       { "UTF8",     SQLITE_UTF8        },
89335       { "UTF-8",    SQLITE_UTF8        },  /* Must be element [1] */
89336       { "UTF-16le", SQLITE_UTF16LE     },  /* Must be element [2] */
89337       { "UTF-16be", SQLITE_UTF16BE     },  /* Must be element [3] */
89338       { "UTF16le",  SQLITE_UTF16LE     },
89339       { "UTF16be",  SQLITE_UTF16BE     },
89340       { "UTF-16",   0                  }, /* SQLITE_UTF16NATIVE */
89341       { "UTF16",    0                  }, /* SQLITE_UTF16NATIVE */
89342       { 0, 0 }
89343     };
89344     const struct EncName *pEnc;
89345     if( !zRight ){    /* "PRAGMA encoding" */
89346       if( sqlite3ReadSchema(pParse) ) goto pragma_out;
89347       sqlite3VdbeSetNumCols(v, 1);
89348       sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "encoding", SQLITE_STATIC);
89349       sqlite3VdbeAddOp2(v, OP_String8, 0, 1);
89350       assert( encnames[SQLITE_UTF8].enc==SQLITE_UTF8 );
89351       assert( encnames[SQLITE_UTF16LE].enc==SQLITE_UTF16LE );
89352       assert( encnames[SQLITE_UTF16BE].enc==SQLITE_UTF16BE );
89353       sqlite3VdbeChangeP4(v, -1, encnames[ENC(pParse->db)].zName, P4_STATIC);
89354       sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
89355     }else{                        /* "PRAGMA encoding = XXX" */
89356       /* Only change the value of sqlite.enc if the database handle is not
89357       ** initialized. If the main database exists, the new sqlite.enc value
89358       ** will be overwritten when the schema is next loaded. If it does not
89359       ** already exists, it will be created to use the new encoding value.
89360       */
89361       if(
89362         !(DbHasProperty(db, 0, DB_SchemaLoaded)) ||
89363         DbHasProperty(db, 0, DB_Empty)
89364       ){
89365         for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
89366           if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){
89367             ENC(pParse->db) = pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE;
89368             break;
89369           }
89370         }
89371         if( !pEnc->zName ){
89372           sqlite3ErrorMsg(pParse, "unsupported encoding: %s", zRight);
89373         }
89374       }
89375     }
89376   }else
89377 #endif /* SQLITE_OMIT_UTF16 */
89378 
89379 #ifndef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
89380   /*
89381   **   PRAGMA [database.]schema_version
89382   **   PRAGMA [database.]schema_version = <integer>
89383   **
89384   **   PRAGMA [database.]user_version
89385   **   PRAGMA [database.]user_version = <integer>
89386   **
89387   ** The pragma's schema_version and user_version are used to set or get
89388   ** the value of the schema-version and user-version, respectively. Both
89389   ** the schema-version and the user-version are 32-bit signed integers
89390   ** stored in the database header.
89391   **
89392   ** The schema-cookie is usually only manipulated internally by SQLite. It
89393   ** is incremented by SQLite whenever the database schema is modified (by
89394   ** creating or dropping a table or index). The schema version is used by
89395   ** SQLite each time a query is executed to ensure that the internal cache
89396   ** of the schema used when compiling the SQL query matches the schema of
89397   ** the database against which the compiled query is actually executed.
89398   ** Subverting this mechanism by using "PRAGMA schema_version" to modify
89399   ** the schema-version is potentially dangerous and may lead to program
89400   ** crashes or database corruption. Use with caution!
89401   **
89402   ** The user-version is not used internally by SQLite. It may be used by
89403   ** applications for any purpose.
89404   */
89405   if( sqlite3StrICmp(zLeft, "schema_version")==0
89406    || sqlite3StrICmp(zLeft, "user_version")==0
89407    || sqlite3StrICmp(zLeft, "freelist_count")==0
89408   ){
89409     int iCookie;   /* Cookie index. 1 for schema-cookie, 6 for user-cookie. */
89410     sqlite3VdbeUsesBtree(v, iDb);
89411     switch( zLeft[0] ){
89412       case 'f': case 'F':
89413         iCookie = BTREE_FREE_PAGE_COUNT;
89414         break;
89415       case 's': case 'S':
89416         iCookie = BTREE_SCHEMA_VERSION;
89417         break;
89418       default:
89419         iCookie = BTREE_USER_VERSION;
89420         break;
89421     }
89422 
89423     if( zRight && iCookie!=BTREE_FREE_PAGE_COUNT ){
89424       /* Write the specified cookie value */
89425       static const VdbeOpList setCookie[] = {
89426         { OP_Transaction,    0,  1,  0},    /* 0 */
89427         { OP_Integer,        0,  1,  0},    /* 1 */
89428         { OP_SetCookie,      0,  0,  1},    /* 2 */
89429       };
89430       int addr = sqlite3VdbeAddOpList(v, ArraySize(setCookie), setCookie);
89431       sqlite3VdbeChangeP1(v, addr, iDb);
89432       sqlite3VdbeChangeP1(v, addr+1, sqlite3Atoi(zRight));
89433       sqlite3VdbeChangeP1(v, addr+2, iDb);
89434       sqlite3VdbeChangeP2(v, addr+2, iCookie);
89435     }else{
89436       /* Read the specified cookie value */
89437       static const VdbeOpList readCookie[] = {
89438         { OP_Transaction,     0,  0,  0},    /* 0 */
89439         { OP_ReadCookie,      0,  1,  0},    /* 1 */
89440         { OP_ResultRow,       1,  1,  0}
89441       };
89442       int addr = sqlite3VdbeAddOpList(v, ArraySize(readCookie), readCookie);
89443       sqlite3VdbeChangeP1(v, addr, iDb);
89444       sqlite3VdbeChangeP1(v, addr+1, iDb);
89445       sqlite3VdbeChangeP3(v, addr+1, iCookie);
89446       sqlite3VdbeSetNumCols(v, 1);
89447       sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, SQLITE_TRANSIENT);
89448     }
89449   }else
89450 #endif /* SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS */
89451 
89452 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
89453   /*
89454   **   PRAGMA compile_options
89455   **
89456   ** Return the names of all compile-time options used in this build,
89457   ** one option per row.
89458   */
89459   if( sqlite3StrICmp(zLeft, "compile_options")==0 ){
89460     int i = 0;
89461     const char *zOpt;
89462     sqlite3VdbeSetNumCols(v, 1);
89463     pParse->nMem = 1;
89464     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "compile_option", SQLITE_STATIC);
89465     while( (zOpt = sqlite3_compileoption_get(i++))!=0 ){
89466       sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, zOpt, 0);
89467       sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
89468     }
89469   }else
89470 #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
89471 
89472 #ifndef SQLITE_OMIT_WAL
89473   /*
89474   **   PRAGMA [database.]wal_checkpoint = passive|full|restart
89475   **
89476   ** Checkpoint the database.
89477   */
89478   if( sqlite3StrICmp(zLeft, "wal_checkpoint")==0 ){
89479     int iBt = (pId2->z?iDb:SQLITE_MAX_ATTACHED);
89480     int eMode = SQLITE_CHECKPOINT_PASSIVE;
89481     if( zRight ){
89482       if( sqlite3StrICmp(zRight, "full")==0 ){
89483         eMode = SQLITE_CHECKPOINT_FULL;
89484       }else if( sqlite3StrICmp(zRight, "restart")==0 ){
89485         eMode = SQLITE_CHECKPOINT_RESTART;
89486       }
89487     }
89488     if( sqlite3ReadSchema(pParse) ) goto pragma_out;
89489     sqlite3VdbeSetNumCols(v, 3);
89490     pParse->nMem = 3;
89491     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "busy", SQLITE_STATIC);
89492     sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "log", SQLITE_STATIC);
89493     sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "checkpointed", SQLITE_STATIC);
89494 
89495     sqlite3VdbeAddOp3(v, OP_Checkpoint, iBt, eMode, 1);
89496     sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
89497   }else
89498 
89499   /*
89500   **   PRAGMA wal_autocheckpoint
89501   **   PRAGMA wal_autocheckpoint = N
89502   **
89503   ** Configure a database connection to automatically checkpoint a database
89504   ** after accumulating N frames in the log. Or query for the current value
89505   ** of N.
89506   */
89507   if( sqlite3StrICmp(zLeft, "wal_autocheckpoint")==0 ){
89508     if( zRight ){
89509       sqlite3_wal_autocheckpoint(db, sqlite3Atoi(zRight));
89510     }
89511     returnSingleInt(pParse, "wal_autocheckpoint",
89512        db->xWalCallback==sqlite3WalDefaultHook ?
89513            SQLITE_PTR_TO_INT(db->pWalArg) : 0);
89514   }else
89515 #endif
89516 
89517 #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
89518   /*
89519   ** Report the current state of file logs for all databases
89520   */
89521   if( sqlite3StrICmp(zLeft, "lock_status")==0 ){
89522     static const char *const azLockName[] = {
89523       "unlocked", "shared", "reserved", "pending", "exclusive"
89524     };
89525     int i;
89526     sqlite3VdbeSetNumCols(v, 2);
89527     pParse->nMem = 2;
89528     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "database", SQLITE_STATIC);
89529     sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "status", SQLITE_STATIC);
89530     for(i=0; i<db->nDb; i++){
89531       Btree *pBt;
89532       Pager *pPager;
89533       const char *zState = "unknown";
89534       int j;
89535       if( db->aDb[i].zName==0 ) continue;
89536       sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, db->aDb[i].zName, P4_STATIC);
89537       pBt = db->aDb[i].pBt;
89538       if( pBt==0 || (pPager = sqlite3BtreePager(pBt))==0 ){
89539         zState = "closed";
89540       }else if( sqlite3_file_control(db, i ? db->aDb[i].zName : 0,
89541                                      SQLITE_FCNTL_LOCKSTATE, &j)==SQLITE_OK ){
89542          zState = azLockName[j];
89543       }
89544       sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, zState, P4_STATIC);
89545       sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
89546     }
89547 
89548   }else
89549 #endif
89550 
89551 #ifdef SQLITE_HAS_CODEC
89552   if( sqlite3StrICmp(zLeft, "key")==0 && zRight ){
89553     sqlite3_key(db, zRight, sqlite3Strlen30(zRight));
89554   }else
89555   if( sqlite3StrICmp(zLeft, "rekey")==0 && zRight ){
89556     sqlite3_rekey(db, zRight, sqlite3Strlen30(zRight));
89557   }else
89558   if( zRight && (sqlite3StrICmp(zLeft, "hexkey")==0 ||
89559                  sqlite3StrICmp(zLeft, "hexrekey")==0) ){
89560     int i, h1, h2;
89561     char zKey[40];
89562     for(i=0; (h1 = zRight[i])!=0 && (h2 = zRight[i+1])!=0; i+=2){
89563       h1 += 9*(1&(h1>>6));
89564       h2 += 9*(1&(h2>>6));
89565       zKey[i/2] = (h2 & 0x0f) | ((h1 & 0xf)<<4);
89566     }
89567     if( (zLeft[3] & 0xf)==0xb ){
89568       sqlite3_key(db, zKey, i/2);
89569     }else{
89570       sqlite3_rekey(db, zKey, i/2);
89571     }
89572   }else
89573 #endif
89574 #if defined(SQLITE_HAS_CODEC) || defined(SQLITE_ENABLE_CEROD)
89575   if( sqlite3StrICmp(zLeft, "activate_extensions")==0 ){
89576 #ifdef SQLITE_HAS_CODEC
89577     if( sqlite3StrNICmp(zRight, "see-", 4)==0 ){
89578       sqlite3_activate_see(&zRight[4]);
89579     }
89580 #endif
89581 #ifdef SQLITE_ENABLE_CEROD
89582     if( sqlite3StrNICmp(zRight, "cerod-", 6)==0 ){
89583       sqlite3_activate_cerod(&zRight[6]);
89584     }
89585 #endif
89586   }else
89587 #endif
89588 
89589 
89590   {/* Empty ELSE clause */}
89591 
89592   /*
89593   ** Reset the safety level, in case the fullfsync flag or synchronous
89594   ** setting changed.
89595   */
89596 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
89597   if( db->autoCommit ){
89598     sqlite3BtreeSetSafetyLevel(pDb->pBt, pDb->safety_level,
89599                (db->flags&SQLITE_FullFSync)!=0,
89600                (db->flags&SQLITE_CkptFullFSync)!=0);
89601   }
89602 #endif
89603 pragma_out:
89604   sqlite3DbFree(db, zLeft);
89605   sqlite3DbFree(db, zRight);
89606 }
89607 
89608 #endif /* SQLITE_OMIT_PRAGMA */
89609 
89610 /************** End of pragma.c **********************************************/
89611 /************** Begin file prepare.c *****************************************/
89612 /*
89613 ** 2005 May 25
89614 **
89615 ** The author disclaims copyright to this source code.  In place of
89616 ** a legal notice, here is a blessing:
89617 **
89618 **    May you do good and not evil.
89619 **    May you find forgiveness for yourself and forgive others.
89620 **    May you share freely, never taking more than you give.
89621 **
89622 *************************************************************************
89623 ** This file contains the implementation of the sqlite3_prepare()
89624 ** interface, and routines that contribute to loading the database schema
89625 ** from disk.
89626 */
89627 
89628 /*
89629 ** Fill the InitData structure with an error message that indicates
89630 ** that the database is corrupt.
89631 */
89632 static void corruptSchema(
89633   InitData *pData,     /* Initialization context */
89634   const char *zObj,    /* Object being parsed at the point of error */
89635   const char *zExtra   /* Error information */
89636 ){
89637   sqlite3 *db = pData->db;
89638   if( !db->mallocFailed && (db->flags & SQLITE_RecoveryMode)==0 ){
89639     if( zObj==0 ) zObj = "?";
89640     sqlite3SetString(pData->pzErrMsg, db,
89641       "malformed database schema (%s)", zObj);
89642     if( zExtra ){
89643       *pData->pzErrMsg = sqlite3MAppendf(db, *pData->pzErrMsg,
89644                                  "%s - %s", *pData->pzErrMsg, zExtra);
89645     }
89646   }
89647   pData->rc = db->mallocFailed ? SQLITE_NOMEM : SQLITE_CORRUPT_BKPT;
89648 }
89649 
89650 /*
89651 ** This is the callback routine for the code that initializes the
89652 ** database.  See sqlite3Init() below for additional information.
89653 ** This routine is also called from the OP_ParseSchema opcode of the VDBE.
89654 **
89655 ** Each callback contains the following information:
89656 **
89657 **     argv[0] = name of thing being created
89658 **     argv[1] = root page number for table or index. 0 for trigger or view.
89659 **     argv[2] = SQL text for the CREATE statement.
89660 **
89661 */
89662 SQLITE_PRIVATE int sqlite3InitCallback(void *pInit, int argc, char **argv, char **NotUsed){
89663   InitData *pData = (InitData*)pInit;
89664   sqlite3 *db = pData->db;
89665   int iDb = pData->iDb;
89666 
89667   assert( argc==3 );
89668   UNUSED_PARAMETER2(NotUsed, argc);
89669   assert( sqlite3_mutex_held(db->mutex) );
89670   DbClearProperty(db, iDb, DB_Empty);
89671   if( db->mallocFailed ){
89672     corruptSchema(pData, argv[0], 0);
89673     return 1;
89674   }
89675 
89676   assert( iDb>=0 && iDb<db->nDb );
89677   if( argv==0 ) return 0;   /* Might happen if EMPTY_RESULT_CALLBACKS are on */
89678   if( argv[1]==0 ){
89679     corruptSchema(pData, argv[0], 0);
89680   }else if( argv[2] && argv[2][0] ){
89681     /* Call the parser to process a CREATE TABLE, INDEX or VIEW.
89682     ** But because db->init.busy is set to 1, no VDBE code is generated
89683     ** or executed.  All the parser does is build the internal data
89684     ** structures that describe the table, index, or view.
89685     */
89686     int rc;
89687     sqlite3_stmt *pStmt;
89688     TESTONLY(int rcp);            /* Return code from sqlite3_prepare() */
89689 
89690     assert( db->init.busy );
89691     db->init.iDb = iDb;
89692     db->init.newTnum = sqlite3Atoi(argv[1]);
89693     db->init.orphanTrigger = 0;
89694     TESTONLY(rcp = ) sqlite3_prepare(db, argv[2], -1, &pStmt, 0);
89695     rc = db->errCode;
89696     assert( (rc&0xFF)==(rcp&0xFF) );
89697     db->init.iDb = 0;
89698     if( SQLITE_OK!=rc ){
89699       if( db->init.orphanTrigger ){
89700         assert( iDb==1 );
89701       }else{
89702         pData->rc = rc;
89703         if( rc==SQLITE_NOMEM ){
89704           db->mallocFailed = 1;
89705         }else if( rc!=SQLITE_INTERRUPT && (rc&0xFF)!=SQLITE_LOCKED ){
89706           corruptSchema(pData, argv[0], sqlite3_errmsg(db));
89707         }
89708       }
89709     }
89710     sqlite3_finalize(pStmt);
89711   }else if( argv[0]==0 ){
89712     corruptSchema(pData, 0, 0);
89713   }else{
89714     /* If the SQL column is blank it means this is an index that
89715     ** was created to be the PRIMARY KEY or to fulfill a UNIQUE
89716     ** constraint for a CREATE TABLE.  The index should have already
89717     ** been created when we processed the CREATE TABLE.  All we have
89718     ** to do here is record the root page number for that index.
89719     */
89720     Index *pIndex;
89721     pIndex = sqlite3FindIndex(db, argv[0], db->aDb[iDb].zName);
89722     if( pIndex==0 ){
89723       /* This can occur if there exists an index on a TEMP table which
89724       ** has the same name as another index on a permanent index.  Since
89725       ** the permanent table is hidden by the TEMP table, we can also
89726       ** safely ignore the index on the permanent table.
89727       */
89728       /* Do Nothing */;
89729     }else if( sqlite3GetInt32(argv[1], &pIndex->tnum)==0 ){
89730       corruptSchema(pData, argv[0], "invalid rootpage");
89731     }
89732   }
89733   return 0;
89734 }
89735 
89736 /*
89737 ** Attempt to read the database schema and initialize internal
89738 ** data structures for a single database file.  The index of the
89739 ** database file is given by iDb.  iDb==0 is used for the main
89740 ** database.  iDb==1 should never be used.  iDb>=2 is used for
89741 ** auxiliary databases.  Return one of the SQLITE_ error codes to
89742 ** indicate success or failure.
89743 */
89744 static int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg){
89745   int rc;
89746   int i;
89747   int size;
89748   Table *pTab;
89749   Db *pDb;
89750   char const *azArg[4];
89751   int meta[5];
89752   InitData initData;
89753   char const *zMasterSchema;
89754   char const *zMasterName;
89755   int openedTransaction = 0;
89756 
89757   /*
89758   ** The master database table has a structure like this
89759   */
89760   static const char master_schema[] =
89761      "CREATE TABLE sqlite_master(\n"
89762      "  type text,\n"
89763      "  name text,\n"
89764      "  tbl_name text,\n"
89765      "  rootpage integer,\n"
89766      "  sql text\n"
89767      ")"
89768   ;
89769 #ifndef SQLITE_OMIT_TEMPDB
89770   static const char temp_master_schema[] =
89771      "CREATE TEMP TABLE sqlite_temp_master(\n"
89772      "  type text,\n"
89773      "  name text,\n"
89774      "  tbl_name text,\n"
89775      "  rootpage integer,\n"
89776      "  sql text\n"
89777      ")"
89778   ;
89779 #else
89780   #define temp_master_schema 0
89781 #endif
89782 
89783   assert( iDb>=0 && iDb<db->nDb );
89784   assert( db->aDb[iDb].pSchema );
89785   assert( sqlite3_mutex_held(db->mutex) );
89786   assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
89787 
89788   /* zMasterSchema and zInitScript are set to point at the master schema
89789   ** and initialisation script appropriate for the database being
89790   ** initialised. zMasterName is the name of the master table.
89791   */
89792   if( !OMIT_TEMPDB && iDb==1 ){
89793     zMasterSchema = temp_master_schema;
89794   }else{
89795     zMasterSchema = master_schema;
89796   }
89797   zMasterName = SCHEMA_TABLE(iDb);
89798 
89799   /* Construct the schema tables.  */
89800   azArg[0] = zMasterName;
89801   azArg[1] = "1";
89802   azArg[2] = zMasterSchema;
89803   azArg[3] = 0;
89804   initData.db = db;
89805   initData.iDb = iDb;
89806   initData.rc = SQLITE_OK;
89807   initData.pzErrMsg = pzErrMsg;
89808   sqlite3InitCallback(&initData, 3, (char **)azArg, 0);
89809   if( initData.rc ){
89810     rc = initData.rc;
89811     goto error_out;
89812   }
89813   pTab = sqlite3FindTable(db, zMasterName, db->aDb[iDb].zName);
89814   if( ALWAYS(pTab) ){
89815     pTab->tabFlags |= TF_Readonly;
89816   }
89817 
89818   /* Create a cursor to hold the database open
89819   */
89820   pDb = &db->aDb[iDb];
89821   if( pDb->pBt==0 ){
89822     if( !OMIT_TEMPDB && ALWAYS(iDb==1) ){
89823       DbSetProperty(db, 1, DB_SchemaLoaded);
89824     }
89825     return SQLITE_OK;
89826   }
89827 
89828   /* If there is not already a read-only (or read-write) transaction opened
89829   ** on the b-tree database, open one now. If a transaction is opened, it
89830   ** will be closed before this function returns.  */
89831   sqlite3BtreeEnter(pDb->pBt);
89832   if( !sqlite3BtreeIsInReadTrans(pDb->pBt) ){
89833     rc = sqlite3BtreeBeginTrans(pDb->pBt, 0);
89834     if( rc!=SQLITE_OK ){
89835       sqlite3SetString(pzErrMsg, db, "%s", sqlite3ErrStr(rc));
89836       goto initone_error_out;
89837     }
89838     openedTransaction = 1;
89839   }
89840 
89841   /* Get the database meta information.
89842   **
89843   ** Meta values are as follows:
89844   **    meta[0]   Schema cookie.  Changes with each schema change.
89845   **    meta[1]   File format of schema layer.
89846   **    meta[2]   Size of the page cache.
89847   **    meta[3]   Largest rootpage (auto/incr_vacuum mode)
89848   **    meta[4]   Db text encoding. 1:UTF-8 2:UTF-16LE 3:UTF-16BE
89849   **    meta[5]   User version
89850   **    meta[6]   Incremental vacuum mode
89851   **    meta[7]   unused
89852   **    meta[8]   unused
89853   **    meta[9]   unused
89854   **
89855   ** Note: The #defined SQLITE_UTF* symbols in sqliteInt.h correspond to
89856   ** the possible values of meta[4].
89857   */
89858   for(i=0; i<ArraySize(meta); i++){
89859     sqlite3BtreeGetMeta(pDb->pBt, i+1, (u32 *)&meta[i]);
89860   }
89861   pDb->pSchema->schema_cookie = meta[BTREE_SCHEMA_VERSION-1];
89862 
89863   /* If opening a non-empty database, check the text encoding. For the
89864   ** main database, set sqlite3.enc to the encoding of the main database.
89865   ** For an attached db, it is an error if the encoding is not the same
89866   ** as sqlite3.enc.
89867   */
89868   if( meta[BTREE_TEXT_ENCODING-1] ){  /* text encoding */
89869     if( iDb==0 ){
89870       u8 encoding;
89871       /* If opening the main database, set ENC(db). */
89872       encoding = (u8)meta[BTREE_TEXT_ENCODING-1] & 3;
89873       if( encoding==0 ) encoding = SQLITE_UTF8;
89874       ENC(db) = encoding;
89875       db->pDfltColl = sqlite3FindCollSeq(db, SQLITE_UTF8, "BINARY", 0);
89876     }else{
89877       /* If opening an attached database, the encoding much match ENC(db) */
89878       if( meta[BTREE_TEXT_ENCODING-1]!=ENC(db) ){
89879         sqlite3SetString(pzErrMsg, db, "attached databases must use the same"
89880             " text encoding as main database");
89881         rc = SQLITE_ERROR;
89882         goto initone_error_out;
89883       }
89884     }
89885   }else{
89886     DbSetProperty(db, iDb, DB_Empty);
89887   }
89888   pDb->pSchema->enc = ENC(db);
89889 
89890   if( pDb->pSchema->cache_size==0 ){
89891     size = sqlite3AbsInt32(meta[BTREE_DEFAULT_CACHE_SIZE-1]);
89892     if( size==0 ){ size = SQLITE_DEFAULT_CACHE_SIZE; }
89893     pDb->pSchema->cache_size = size;
89894     sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
89895   }
89896 
89897   /*
89898   ** file_format==1    Version 3.0.0.
89899   ** file_format==2    Version 3.1.3.  // ALTER TABLE ADD COLUMN
89900   ** file_format==3    Version 3.1.4.  // ditto but with non-NULL defaults
89901   ** file_format==4    Version 3.3.0.  // DESC indices.  Boolean constants
89902   */
89903   pDb->pSchema->file_format = (u8)meta[BTREE_FILE_FORMAT-1];
89904   if( pDb->pSchema->file_format==0 ){
89905     pDb->pSchema->file_format = 1;
89906   }
89907   if( pDb->pSchema->file_format>SQLITE_MAX_FILE_FORMAT ){
89908     sqlite3SetString(pzErrMsg, db, "unsupported file format");
89909     rc = SQLITE_ERROR;
89910     goto initone_error_out;
89911   }
89912 
89913   /* Ticket #2804:  When we open a database in the newer file format,
89914   ** clear the legacy_file_format pragma flag so that a VACUUM will
89915   ** not downgrade the database and thus invalidate any descending
89916   ** indices that the user might have created.
89917   */
89918   if( iDb==0 && meta[BTREE_FILE_FORMAT-1]>=4 ){
89919     db->flags &= ~SQLITE_LegacyFileFmt;
89920   }
89921 
89922   /* Read the schema information out of the schema tables
89923   */
89924   assert( db->init.busy );
89925   {
89926     char *zSql;
89927     zSql = sqlite3MPrintf(db,
89928         "SELECT name, rootpage, sql FROM '%q'.%s ORDER BY rowid",
89929         db->aDb[iDb].zName, zMasterName);
89930 #ifndef SQLITE_OMIT_AUTHORIZATION
89931     {
89932       int (*xAuth)(void*,int,const char*,const char*,const char*,const char*);
89933       xAuth = db->xAuth;
89934       db->xAuth = 0;
89935 #endif
89936       rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
89937 #ifndef SQLITE_OMIT_AUTHORIZATION
89938       db->xAuth = xAuth;
89939     }
89940 #endif
89941     if( rc==SQLITE_OK ) rc = initData.rc;
89942     sqlite3DbFree(db, zSql);
89943 #ifndef SQLITE_OMIT_ANALYZE
89944     if( rc==SQLITE_OK ){
89945       sqlite3AnalysisLoad(db, iDb);
89946     }
89947 #endif
89948   }
89949   if( db->mallocFailed ){
89950     rc = SQLITE_NOMEM;
89951     sqlite3ResetInternalSchema(db, -1);
89952   }
89953   if( rc==SQLITE_OK || (db->flags&SQLITE_RecoveryMode)){
89954     /* Black magic: If the SQLITE_RecoveryMode flag is set, then consider
89955     ** the schema loaded, even if errors occurred. In this situation the
89956     ** current sqlite3_prepare() operation will fail, but the following one
89957     ** will attempt to compile the supplied statement against whatever subset
89958     ** of the schema was loaded before the error occurred. The primary
89959     ** purpose of this is to allow access to the sqlite_master table
89960     ** even when its contents have been corrupted.
89961     */
89962     DbSetProperty(db, iDb, DB_SchemaLoaded);
89963     rc = SQLITE_OK;
89964   }
89965 
89966   /* Jump here for an error that occurs after successfully allocating
89967   ** curMain and calling sqlite3BtreeEnter(). For an error that occurs
89968   ** before that point, jump to error_out.
89969   */
89970 initone_error_out:
89971   if( openedTransaction ){
89972     sqlite3BtreeCommit(pDb->pBt);
89973   }
89974   sqlite3BtreeLeave(pDb->pBt);
89975 
89976 error_out:
89977   if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
89978     db->mallocFailed = 1;
89979   }
89980   return rc;
89981 }
89982 
89983 /*
89984 ** Initialize all database files - the main database file, the file
89985 ** used to store temporary tables, and any additional database files
89986 ** created using ATTACH statements.  Return a success code.  If an
89987 ** error occurs, write an error message into *pzErrMsg.
89988 **
89989 ** After a database is initialized, the DB_SchemaLoaded bit is set
89990 ** bit is set in the flags field of the Db structure. If the database
89991 ** file was of zero-length, then the DB_Empty flag is also set.
89992 */
89993 SQLITE_PRIVATE int sqlite3Init(sqlite3 *db, char **pzErrMsg){
89994   int i, rc;
89995   int commit_internal = !(db->flags&SQLITE_InternChanges);
89996 
89997   assert( sqlite3_mutex_held(db->mutex) );
89998   rc = SQLITE_OK;
89999   db->init.busy = 1;
90000   for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
90001     if( DbHasProperty(db, i, DB_SchemaLoaded) || i==1 ) continue;
90002     rc = sqlite3InitOne(db, i, pzErrMsg);
90003     if( rc ){
90004       sqlite3ResetInternalSchema(db, i);
90005     }
90006   }
90007 
90008   /* Once all the other databases have been initialised, load the schema
90009   ** for the TEMP database. This is loaded last, as the TEMP database
90010   ** schema may contain references to objects in other databases.
90011   */
90012 #ifndef SQLITE_OMIT_TEMPDB
90013   if( rc==SQLITE_OK && ALWAYS(db->nDb>1)
90014                     && !DbHasProperty(db, 1, DB_SchemaLoaded) ){
90015     rc = sqlite3InitOne(db, 1, pzErrMsg);
90016     if( rc ){
90017       sqlite3ResetInternalSchema(db, 1);
90018     }
90019   }
90020 #endif
90021 
90022   db->init.busy = 0;
90023   if( rc==SQLITE_OK && commit_internal ){
90024     sqlite3CommitInternalChanges(db);
90025   }
90026 
90027   return rc;
90028 }
90029 
90030 /*
90031 ** This routine is a no-op if the database schema is already initialised.
90032 ** Otherwise, the schema is loaded. An error code is returned.
90033 */
90034 SQLITE_PRIVATE int sqlite3ReadSchema(Parse *pParse){
90035   int rc = SQLITE_OK;
90036   sqlite3 *db = pParse->db;
90037   assert( sqlite3_mutex_held(db->mutex) );
90038   if( !db->init.busy ){
90039     rc = sqlite3Init(db, &pParse->zErrMsg);
90040   }
90041   if( rc!=SQLITE_OK ){
90042     pParse->rc = rc;
90043     pParse->nErr++;
90044   }
90045   return rc;
90046 }
90047 
90048 
90049 /*
90050 ** Check schema cookies in all databases.  If any cookie is out
90051 ** of date set pParse->rc to SQLITE_SCHEMA.  If all schema cookies
90052 ** make no changes to pParse->rc.
90053 */
90054 static void schemaIsValid(Parse *pParse){
90055   sqlite3 *db = pParse->db;
90056   int iDb;
90057   int rc;
90058   int cookie;
90059 
90060   assert( pParse->checkSchema );
90061   assert( sqlite3_mutex_held(db->mutex) );
90062   for(iDb=0; iDb<db->nDb; iDb++){
90063     int openedTransaction = 0;         /* True if a transaction is opened */
90064     Btree *pBt = db->aDb[iDb].pBt;     /* Btree database to read cookie from */
90065     if( pBt==0 ) continue;
90066 
90067     /* If there is not already a read-only (or read-write) transaction opened
90068     ** on the b-tree database, open one now. If a transaction is opened, it
90069     ** will be closed immediately after reading the meta-value. */
90070     if( !sqlite3BtreeIsInReadTrans(pBt) ){
90071       rc = sqlite3BtreeBeginTrans(pBt, 0);
90072       if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
90073         db->mallocFailed = 1;
90074       }
90075       if( rc!=SQLITE_OK ) return;
90076       openedTransaction = 1;
90077     }
90078 
90079     /* Read the schema cookie from the database. If it does not match the
90080     ** value stored as part of the in-memory schema representation,
90081     ** set Parse.rc to SQLITE_SCHEMA. */
90082     sqlite3BtreeGetMeta(pBt, BTREE_SCHEMA_VERSION, (u32 *)&cookie);
90083     assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
90084     if( cookie!=db->aDb[iDb].pSchema->schema_cookie ){
90085       sqlite3ResetInternalSchema(db, iDb);
90086       pParse->rc = SQLITE_SCHEMA;
90087     }
90088 
90089     /* Close the transaction, if one was opened. */
90090     if( openedTransaction ){
90091       sqlite3BtreeCommit(pBt);
90092     }
90093   }
90094 }
90095 
90096 /*
90097 ** Convert a schema pointer into the iDb index that indicates
90098 ** which database file in db->aDb[] the schema refers to.
90099 **
90100 ** If the same database is attached more than once, the first
90101 ** attached database is returned.
90102 */
90103 SQLITE_PRIVATE int sqlite3SchemaToIndex(sqlite3 *db, Schema *pSchema){
90104   int i = -1000000;
90105 
90106   /* If pSchema is NULL, then return -1000000. This happens when code in
90107   ** expr.c is trying to resolve a reference to a transient table (i.e. one
90108   ** created by a sub-select). In this case the return value of this
90109   ** function should never be used.
90110   **
90111   ** We return -1000000 instead of the more usual -1 simply because using
90112   ** -1000000 as the incorrect index into db->aDb[] is much
90113   ** more likely to cause a segfault than -1 (of course there are assert()
90114   ** statements too, but it never hurts to play the odds).
90115   */
90116   assert( sqlite3_mutex_held(db->mutex) );
90117   if( pSchema ){
90118     for(i=0; ALWAYS(i<db->nDb); i++){
90119       if( db->aDb[i].pSchema==pSchema ){
90120         break;
90121       }
90122     }
90123     assert( i>=0 && i<db->nDb );
90124   }
90125   return i;
90126 }
90127 
90128 /*
90129 ** Compile the UTF-8 encoded SQL statement zSql into a statement handle.
90130 */
90131 static int sqlite3Prepare(
90132   sqlite3 *db,              /* Database handle. */
90133   const char *zSql,         /* UTF-8 encoded SQL statement. */
90134   int nBytes,               /* Length of zSql in bytes. */
90135   int saveSqlFlag,          /* True to copy SQL text into the sqlite3_stmt */
90136   Vdbe *pReprepare,         /* VM being reprepared */
90137   sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
90138   const char **pzTail       /* OUT: End of parsed string */
90139 ){
90140   Parse *pParse;            /* Parsing context */
90141   char *zErrMsg = 0;        /* Error message */
90142   int rc = SQLITE_OK;       /* Result code */
90143   int i;                    /* Loop counter */
90144 
90145   /* Allocate the parsing context */
90146   pParse = sqlite3StackAllocZero(db, sizeof(*pParse));
90147   if( pParse==0 ){
90148     rc = SQLITE_NOMEM;
90149     goto end_prepare;
90150   }
90151   pParse->pReprepare = pReprepare;
90152   assert( ppStmt && *ppStmt==0 );
90153   assert( !db->mallocFailed );
90154   assert( sqlite3_mutex_held(db->mutex) );
90155 
90156   /* Check to verify that it is possible to get a read lock on all
90157   ** database schemas.  The inability to get a read lock indicates that
90158   ** some other database connection is holding a write-lock, which in
90159   ** turn means that the other connection has made uncommitted changes
90160   ** to the schema.
90161   **
90162   ** Were we to proceed and prepare the statement against the uncommitted
90163   ** schema changes and if those schema changes are subsequently rolled
90164   ** back and different changes are made in their place, then when this
90165   ** prepared statement goes to run the schema cookie would fail to detect
90166   ** the schema change.  Disaster would follow.
90167   **
90168   ** This thread is currently holding mutexes on all Btrees (because
90169   ** of the sqlite3BtreeEnterAll() in sqlite3LockAndPrepare()) so it
90170   ** is not possible for another thread to start a new schema change
90171   ** while this routine is running.  Hence, we do not need to hold
90172   ** locks on the schema, we just need to make sure nobody else is
90173   ** holding them.
90174   **
90175   ** Note that setting READ_UNCOMMITTED overrides most lock detection,
90176   ** but it does *not* override schema lock detection, so this all still
90177   ** works even if READ_UNCOMMITTED is set.
90178   */
90179   for(i=0; i<db->nDb; i++) {
90180     Btree *pBt = db->aDb[i].pBt;
90181     if( pBt ){
90182       assert( sqlite3BtreeHoldsMutex(pBt) );
90183       rc = sqlite3BtreeSchemaLocked(pBt);
90184       if( rc ){
90185         const char *zDb = db->aDb[i].zName;
90186         sqlite3Error(db, rc, "database schema is locked: %s", zDb);
90187         testcase( db->flags & SQLITE_ReadUncommitted );
90188         goto end_prepare;
90189       }
90190     }
90191   }
90192 
90193   sqlite3VtabUnlockList(db);
90194 
90195   pParse->db = db;
90196   pParse->nQueryLoop = (double)1;
90197   if( nBytes>=0 && (nBytes==0 || zSql[nBytes-1]!=0) ){
90198     char *zSqlCopy;
90199     int mxLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
90200     testcase( nBytes==mxLen );
90201     testcase( nBytes==mxLen+1 );
90202     if( nBytes>mxLen ){
90203       sqlite3Error(db, SQLITE_TOOBIG, "statement too long");
90204       rc = sqlite3ApiExit(db, SQLITE_TOOBIG);
90205       goto end_prepare;
90206     }
90207     zSqlCopy = sqlite3DbStrNDup(db, zSql, nBytes);
90208     if( zSqlCopy ){
90209       sqlite3RunParser(pParse, zSqlCopy, &zErrMsg);
90210       sqlite3DbFree(db, zSqlCopy);
90211       pParse->zTail = &zSql[pParse->zTail-zSqlCopy];
90212     }else{
90213       pParse->zTail = &zSql[nBytes];
90214     }
90215   }else{
90216     sqlite3RunParser(pParse, zSql, &zErrMsg);
90217   }
90218   assert( 1==(int)pParse->nQueryLoop );
90219 
90220   if( db->mallocFailed ){
90221     pParse->rc = SQLITE_NOMEM;
90222   }
90223   if( pParse->rc==SQLITE_DONE ) pParse->rc = SQLITE_OK;
90224   if( pParse->checkSchema ){
90225     schemaIsValid(pParse);
90226   }
90227   if( db->mallocFailed ){
90228     pParse->rc = SQLITE_NOMEM;
90229   }
90230   if( pzTail ){
90231     *pzTail = pParse->zTail;
90232   }
90233   rc = pParse->rc;
90234 
90235 #ifndef SQLITE_OMIT_EXPLAIN
90236   if( rc==SQLITE_OK && pParse->pVdbe && pParse->explain ){
90237     static const char * const azColName[] = {
90238        "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment",
90239        "selectid", "order", "from", "detail"
90240     };
90241     int iFirst, mx;
90242     if( pParse->explain==2 ){
90243       sqlite3VdbeSetNumCols(pParse->pVdbe, 4);
90244       iFirst = 8;
90245       mx = 12;
90246     }else{
90247       sqlite3VdbeSetNumCols(pParse->pVdbe, 8);
90248       iFirst = 0;
90249       mx = 8;
90250     }
90251     for(i=iFirst; i<mx; i++){
90252       sqlite3VdbeSetColName(pParse->pVdbe, i-iFirst, COLNAME_NAME,
90253                             azColName[i], SQLITE_STATIC);
90254     }
90255   }
90256 #endif
90257 
90258   assert( db->init.busy==0 || saveSqlFlag==0 );
90259   if( db->init.busy==0 ){
90260     Vdbe *pVdbe = pParse->pVdbe;
90261     sqlite3VdbeSetSql(pVdbe, zSql, (int)(pParse->zTail-zSql), saveSqlFlag);
90262   }
90263   if( pParse->pVdbe && (rc!=SQLITE_OK || db->mallocFailed) ){
90264     sqlite3VdbeFinalize(pParse->pVdbe);
90265     assert(!(*ppStmt));
90266   }else{
90267     *ppStmt = (sqlite3_stmt*)pParse->pVdbe;
90268   }
90269 
90270   if( zErrMsg ){
90271     sqlite3Error(db, rc, "%s", zErrMsg);
90272     sqlite3DbFree(db, zErrMsg);
90273   }else{
90274     sqlite3Error(db, rc, 0);
90275   }
90276 
90277   /* Delete any TriggerPrg structures allocated while parsing this statement. */
90278   while( pParse->pTriggerPrg ){
90279     TriggerPrg *pT = pParse->pTriggerPrg;
90280     pParse->pTriggerPrg = pT->pNext;
90281     sqlite3DbFree(db, pT);
90282   }
90283 
90284 end_prepare:
90285 
90286   sqlite3StackFree(db, pParse);
90287   rc = sqlite3ApiExit(db, rc);
90288   assert( (rc&db->errMask)==rc );
90289   return rc;
90290 }
90291 static int sqlite3LockAndPrepare(
90292   sqlite3 *db,              /* Database handle. */
90293   const char *zSql,         /* UTF-8 encoded SQL statement. */
90294   int nBytes,               /* Length of zSql in bytes. */
90295   int saveSqlFlag,          /* True to copy SQL text into the sqlite3_stmt */
90296   Vdbe *pOld,               /* VM being reprepared */
90297   sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
90298   const char **pzTail       /* OUT: End of parsed string */
90299 ){
90300   int rc;
90301   assert( ppStmt!=0 );
90302   *ppStmt = 0;
90303   if( !sqlite3SafetyCheckOk(db) ){
90304     return SQLITE_MISUSE_BKPT;
90305   }
90306   sqlite3_mutex_enter(db->mutex);
90307   sqlite3BtreeEnterAll(db);
90308   rc = sqlite3Prepare(db, zSql, nBytes, saveSqlFlag, pOld, ppStmt, pzTail);
90309   if( rc==SQLITE_SCHEMA ){
90310     sqlite3_finalize(*ppStmt);
90311     rc = sqlite3Prepare(db, zSql, nBytes, saveSqlFlag, pOld, ppStmt, pzTail);
90312   }
90313   sqlite3BtreeLeaveAll(db);
90314   sqlite3_mutex_leave(db->mutex);
90315   return rc;
90316 }
90317 
90318 /*
90319 ** Rerun the compilation of a statement after a schema change.
90320 **
90321 ** If the statement is successfully recompiled, return SQLITE_OK. Otherwise,
90322 ** if the statement cannot be recompiled because another connection has
90323 ** locked the sqlite3_master table, return SQLITE_LOCKED. If any other error
90324 ** occurs, return SQLITE_SCHEMA.
90325 */
90326 SQLITE_PRIVATE int sqlite3Reprepare(Vdbe *p){
90327   int rc;
90328   sqlite3_stmt *pNew;
90329   const char *zSql;
90330   sqlite3 *db;
90331 
90332   assert( sqlite3_mutex_held(sqlite3VdbeDb(p)->mutex) );
90333   zSql = sqlite3_sql((sqlite3_stmt *)p);
90334   assert( zSql!=0 );  /* Reprepare only called for prepare_v2() statements */
90335   db = sqlite3VdbeDb(p);
90336   assert( sqlite3_mutex_held(db->mutex) );
90337   rc = sqlite3LockAndPrepare(db, zSql, -1, 0, p, &pNew, 0);
90338   if( rc ){
90339     if( rc==SQLITE_NOMEM ){
90340       db->mallocFailed = 1;
90341     }
90342     assert( pNew==0 );
90343     return rc;
90344   }else{
90345     assert( pNew!=0 );
90346   }
90347   sqlite3VdbeSwap((Vdbe*)pNew, p);
90348   sqlite3TransferBindings(pNew, (sqlite3_stmt*)p);
90349   sqlite3VdbeResetStepResult((Vdbe*)pNew);
90350   sqlite3VdbeFinalize((Vdbe*)pNew);
90351   return SQLITE_OK;
90352 }
90353 
90354 
90355 /*
90356 ** Two versions of the official API.  Legacy and new use.  In the legacy
90357 ** version, the original SQL text is not saved in the prepared statement
90358 ** and so if a schema change occurs, SQLITE_SCHEMA is returned by
90359 ** sqlite3_step().  In the new version, the original SQL text is retained
90360 ** and the statement is automatically recompiled if an schema change
90361 ** occurs.
90362 */
90363 SQLITE_API int sqlite3_prepare(
90364   sqlite3 *db,              /* Database handle. */
90365   const char *zSql,         /* UTF-8 encoded SQL statement. */
90366   int nBytes,               /* Length of zSql in bytes. */
90367   sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
90368   const char **pzTail       /* OUT: End of parsed string */
90369 ){
90370   int rc;
90371   rc = sqlite3LockAndPrepare(db,zSql,nBytes,0,0,ppStmt,pzTail);
90372   assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 );  /* VERIFY: F13021 */
90373   return rc;
90374 }
90375 SQLITE_API int sqlite3_prepare_v2(
90376   sqlite3 *db,              /* Database handle. */
90377   const char *zSql,         /* UTF-8 encoded SQL statement. */
90378   int nBytes,               /* Length of zSql in bytes. */
90379   sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
90380   const char **pzTail       /* OUT: End of parsed string */
90381 ){
90382   int rc;
90383   rc = sqlite3LockAndPrepare(db,zSql,nBytes,1,0,ppStmt,pzTail);
90384   assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 );  /* VERIFY: F13021 */
90385   return rc;
90386 }
90387 
90388 
90389 #ifndef SQLITE_OMIT_UTF16
90390 /*
90391 ** Compile the UTF-16 encoded SQL statement zSql into a statement handle.
90392 */
90393 static int sqlite3Prepare16(
90394   sqlite3 *db,              /* Database handle. */
90395   const void *zSql,         /* UTF-16 encoded SQL statement. */
90396   int nBytes,               /* Length of zSql in bytes. */
90397   int saveSqlFlag,          /* True to save SQL text into the sqlite3_stmt */
90398   sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
90399   const void **pzTail       /* OUT: End of parsed string */
90400 ){
90401   /* This function currently works by first transforming the UTF-16
90402   ** encoded string to UTF-8, then invoking sqlite3_prepare(). The
90403   ** tricky bit is figuring out the pointer to return in *pzTail.
90404   */
90405   char *zSql8;
90406   const char *zTail8 = 0;
90407   int rc = SQLITE_OK;
90408 
90409   assert( ppStmt );
90410   *ppStmt = 0;
90411   if( !sqlite3SafetyCheckOk(db) ){
90412     return SQLITE_MISUSE_BKPT;
90413   }
90414   sqlite3_mutex_enter(db->mutex);
90415   zSql8 = sqlite3Utf16to8(db, zSql, nBytes, SQLITE_UTF16NATIVE);
90416   if( zSql8 ){
90417     rc = sqlite3LockAndPrepare(db, zSql8, -1, saveSqlFlag, 0, ppStmt, &zTail8);
90418   }
90419 
90420   if( zTail8 && pzTail ){
90421     /* If sqlite3_prepare returns a tail pointer, we calculate the
90422     ** equivalent pointer into the UTF-16 string by counting the unicode
90423     ** characters between zSql8 and zTail8, and then returning a pointer
90424     ** the same number of characters into the UTF-16 string.
90425     */
90426     int chars_parsed = sqlite3Utf8CharLen(zSql8, (int)(zTail8-zSql8));
90427     *pzTail = (u8 *)zSql + sqlite3Utf16ByteLen(zSql, chars_parsed);
90428   }
90429   sqlite3DbFree(db, zSql8);
90430   rc = sqlite3ApiExit(db, rc);
90431   sqlite3_mutex_leave(db->mutex);
90432   return rc;
90433 }
90434 
90435 /*
90436 ** Two versions of the official API.  Legacy and new use.  In the legacy
90437 ** version, the original SQL text is not saved in the prepared statement
90438 ** and so if a schema change occurs, SQLITE_SCHEMA is returned by
90439 ** sqlite3_step().  In the new version, the original SQL text is retained
90440 ** and the statement is automatically recompiled if an schema change
90441 ** occurs.
90442 */
90443 SQLITE_API int sqlite3_prepare16(
90444   sqlite3 *db,              /* Database handle. */
90445   const void *zSql,         /* UTF-16 encoded SQL statement. */
90446   int nBytes,               /* Length of zSql in bytes. */
90447   sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
90448   const void **pzTail       /* OUT: End of parsed string */
90449 ){
90450   int rc;
90451   rc = sqlite3Prepare16(db,zSql,nBytes,0,ppStmt,pzTail);
90452   assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 );  /* VERIFY: F13021 */
90453   return rc;
90454 }
90455 SQLITE_API int sqlite3_prepare16_v2(
90456   sqlite3 *db,              /* Database handle. */
90457   const void *zSql,         /* UTF-16 encoded SQL statement. */
90458   int nBytes,               /* Length of zSql in bytes. */
90459   sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
90460   const void **pzTail       /* OUT: End of parsed string */
90461 ){
90462   int rc;
90463   rc = sqlite3Prepare16(db,zSql,nBytes,1,ppStmt,pzTail);
90464   assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 );  /* VERIFY: F13021 */
90465   return rc;
90466 }
90467 
90468 #endif /* SQLITE_OMIT_UTF16 */
90469 
90470 /************** End of prepare.c *********************************************/
90471 /************** Begin file select.c ******************************************/
90472 /*
90473 ** 2001 September 15
90474 **
90475 ** The author disclaims copyright to this source code.  In place of
90476 ** a legal notice, here is a blessing:
90477 **
90478 **    May you do good and not evil.
90479 **    May you find forgiveness for yourself and forgive others.
90480 **    May you share freely, never taking more than you give.
90481 **
90482 *************************************************************************
90483 ** This file contains C code routines that are called by the parser
90484 ** to handle SELECT statements in SQLite.
90485 */
90486 
90487 
90488 /*
90489 ** Delete all the content of a Select structure but do not deallocate
90490 ** the select structure itself.
90491 */
90492 static void clearSelect(sqlite3 *db, Select *p){
90493   sqlite3ExprListDelete(db, p->pEList);
90494   sqlite3SrcListDelete(db, p->pSrc);
90495   sqlite3ExprDelete(db, p->pWhere);
90496   sqlite3ExprListDelete(db, p->pGroupBy);
90497   sqlite3ExprDelete(db, p->pHaving);
90498   sqlite3ExprListDelete(db, p->pOrderBy);
90499   sqlite3SelectDelete(db, p->pPrior);
90500   sqlite3ExprDelete(db, p->pLimit);
90501   sqlite3ExprDelete(db, p->pOffset);
90502 }
90503 
90504 /*
90505 ** Initialize a SelectDest structure.
90506 */
90507 SQLITE_PRIVATE void sqlite3SelectDestInit(SelectDest *pDest, int eDest, int iParm){
90508   pDest->eDest = (u8)eDest;
90509   pDest->iParm = iParm;
90510   pDest->affinity = 0;
90511   pDest->iMem = 0;
90512   pDest->nMem = 0;
90513 }
90514 
90515 
90516 /*
90517 ** Allocate a new Select structure and return a pointer to that
90518 ** structure.
90519 */
90520 SQLITE_PRIVATE Select *sqlite3SelectNew(
90521   Parse *pParse,        /* Parsing context */
90522   ExprList *pEList,     /* which columns to include in the result */
90523   SrcList *pSrc,        /* the FROM clause -- which tables to scan */
90524   Expr *pWhere,         /* the WHERE clause */
90525   ExprList *pGroupBy,   /* the GROUP BY clause */
90526   Expr *pHaving,        /* the HAVING clause */
90527   ExprList *pOrderBy,   /* the ORDER BY clause */
90528   int isDistinct,       /* true if the DISTINCT keyword is present */
90529   Expr *pLimit,         /* LIMIT value.  NULL means not used */
90530   Expr *pOffset         /* OFFSET value.  NULL means no offset */
90531 ){
90532   Select *pNew;
90533   Select standin;
90534   sqlite3 *db = pParse->db;
90535   pNew = sqlite3DbMallocZero(db, sizeof(*pNew) );
90536   assert( db->mallocFailed || !pOffset || pLimit ); /* OFFSET implies LIMIT */
90537   if( pNew==0 ){
90538     pNew = &standin;
90539     memset(pNew, 0, sizeof(*pNew));
90540   }
90541   if( pEList==0 ){
90542     pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db,TK_ALL,0));
90543   }
90544   pNew->pEList = pEList;
90545   pNew->pSrc = pSrc;
90546   pNew->pWhere = pWhere;
90547   pNew->pGroupBy = pGroupBy;
90548   pNew->pHaving = pHaving;
90549   pNew->pOrderBy = pOrderBy;
90550   pNew->selFlags = isDistinct ? SF_Distinct : 0;
90551   pNew->op = TK_SELECT;
90552   pNew->pLimit = pLimit;
90553   pNew->pOffset = pOffset;
90554   assert( pOffset==0 || pLimit!=0 );
90555   pNew->addrOpenEphm[0] = -1;
90556   pNew->addrOpenEphm[1] = -1;
90557   pNew->addrOpenEphm[2] = -1;
90558   if( db->mallocFailed ) {
90559     clearSelect(db, pNew);
90560     if( pNew!=&standin ) sqlite3DbFree(db, pNew);
90561     pNew = 0;
90562   }
90563   return pNew;
90564 }
90565 
90566 /*
90567 ** Delete the given Select structure and all of its substructures.
90568 */
90569 SQLITE_PRIVATE void sqlite3SelectDelete(sqlite3 *db, Select *p){
90570   if( p ){
90571     clearSelect(db, p);
90572     sqlite3DbFree(db, p);
90573   }
90574 }
90575 
90576 /*
90577 ** Given 1 to 3 identifiers preceeding the JOIN keyword, determine the
90578 ** type of join.  Return an integer constant that expresses that type
90579 ** in terms of the following bit values:
90580 **
90581 **     JT_INNER
90582 **     JT_CROSS
90583 **     JT_OUTER
90584 **     JT_NATURAL
90585 **     JT_LEFT
90586 **     JT_RIGHT
90587 **
90588 ** A full outer join is the combination of JT_LEFT and JT_RIGHT.
90589 **
90590 ** If an illegal or unsupported join type is seen, then still return
90591 ** a join type, but put an error in the pParse structure.
90592 */
90593 SQLITE_PRIVATE int sqlite3JoinType(Parse *pParse, Token *pA, Token *pB, Token *pC){
90594   int jointype = 0;
90595   Token *apAll[3];
90596   Token *p;
90597                              /*   0123456789 123456789 123456789 123 */
90598   static const char zKeyText[] = "naturaleftouterightfullinnercross";
90599   static const struct {
90600     u8 i;        /* Beginning of keyword text in zKeyText[] */
90601     u8 nChar;    /* Length of the keyword in characters */
90602     u8 code;     /* Join type mask */
90603   } aKeyword[] = {
90604     /* natural */ { 0,  7, JT_NATURAL                },
90605     /* left    */ { 6,  4, JT_LEFT|JT_OUTER          },
90606     /* outer   */ { 10, 5, JT_OUTER                  },
90607     /* right   */ { 14, 5, JT_RIGHT|JT_OUTER         },
90608     /* full    */ { 19, 4, JT_LEFT|JT_RIGHT|JT_OUTER },
90609     /* inner   */ { 23, 5, JT_INNER                  },
90610     /* cross   */ { 28, 5, JT_INNER|JT_CROSS         },
90611   };
90612   int i, j;
90613   apAll[0] = pA;
90614   apAll[1] = pB;
90615   apAll[2] = pC;
90616   for(i=0; i<3 && apAll[i]; i++){
90617     p = apAll[i];
90618     for(j=0; j<ArraySize(aKeyword); j++){
90619       if( p->n==aKeyword[j].nChar
90620           && sqlite3StrNICmp((char*)p->z, &zKeyText[aKeyword[j].i], p->n)==0 ){
90621         jointype |= aKeyword[j].code;
90622         break;
90623       }
90624     }
90625     testcase( j==0 || j==1 || j==2 || j==3 || j==4 || j==5 || j==6 );
90626     if( j>=ArraySize(aKeyword) ){
90627       jointype |= JT_ERROR;
90628       break;
90629     }
90630   }
90631   if(
90632      (jointype & (JT_INNER|JT_OUTER))==(JT_INNER|JT_OUTER) ||
90633      (jointype & JT_ERROR)!=0
90634   ){
90635     const char *zSp = " ";
90636     assert( pB!=0 );
90637     if( pC==0 ){ zSp++; }
90638     sqlite3ErrorMsg(pParse, "unknown or unsupported join type: "
90639        "%T %T%s%T", pA, pB, zSp, pC);
90640     jointype = JT_INNER;
90641   }else if( (jointype & JT_OUTER)!=0
90642          && (jointype & (JT_LEFT|JT_RIGHT))!=JT_LEFT ){
90643     sqlite3ErrorMsg(pParse,
90644       "RIGHT and FULL OUTER JOINs are not currently supported");
90645     jointype = JT_INNER;
90646   }
90647   return jointype;
90648 }
90649 
90650 /*
90651 ** Return the index of a column in a table.  Return -1 if the column
90652 ** is not contained in the table.
90653 */
90654 static int columnIndex(Table *pTab, const char *zCol){
90655   int i;
90656   for(i=0; i<pTab->nCol; i++){
90657     if( sqlite3StrICmp(pTab->aCol[i].zName, zCol)==0 ) return i;
90658   }
90659   return -1;
90660 }
90661 
90662 /*
90663 ** Search the first N tables in pSrc, from left to right, looking for a
90664 ** table that has a column named zCol.
90665 **
90666 ** When found, set *piTab and *piCol to the table index and column index
90667 ** of the matching column and return TRUE.
90668 **
90669 ** If not found, return FALSE.
90670 */
90671 static int tableAndColumnIndex(
90672   SrcList *pSrc,       /* Array of tables to search */
90673   int N,               /* Number of tables in pSrc->a[] to search */
90674   const char *zCol,    /* Name of the column we are looking for */
90675   int *piTab,          /* Write index of pSrc->a[] here */
90676   int *piCol           /* Write index of pSrc->a[*piTab].pTab->aCol[] here */
90677 ){
90678   int i;               /* For looping over tables in pSrc */
90679   int iCol;            /* Index of column matching zCol */
90680 
90681   assert( (piTab==0)==(piCol==0) );  /* Both or neither are NULL */
90682   for(i=0; i<N; i++){
90683     iCol = columnIndex(pSrc->a[i].pTab, zCol);
90684     if( iCol>=0 ){
90685       if( piTab ){
90686         *piTab = i;
90687         *piCol = iCol;
90688       }
90689       return 1;
90690     }
90691   }
90692   return 0;
90693 }
90694 
90695 /*
90696 ** This function is used to add terms implied by JOIN syntax to the
90697 ** WHERE clause expression of a SELECT statement. The new term, which
90698 ** is ANDed with the existing WHERE clause, is of the form:
90699 **
90700 **    (tab1.col1 = tab2.col2)
90701 **
90702 ** where tab1 is the iSrc'th table in SrcList pSrc and tab2 is the
90703 ** (iSrc+1)'th. Column col1 is column iColLeft of tab1, and col2 is
90704 ** column iColRight of tab2.
90705 */
90706 static void addWhereTerm(
90707   Parse *pParse,                  /* Parsing context */
90708   SrcList *pSrc,                  /* List of tables in FROM clause */
90709   int iLeft,                      /* Index of first table to join in pSrc */
90710   int iColLeft,                   /* Index of column in first table */
90711   int iRight,                     /* Index of second table in pSrc */
90712   int iColRight,                  /* Index of column in second table */
90713   int isOuterJoin,                /* True if this is an OUTER join */
90714   Expr **ppWhere                  /* IN/OUT: The WHERE clause to add to */
90715 ){
90716   sqlite3 *db = pParse->db;
90717   Expr *pE1;
90718   Expr *pE2;
90719   Expr *pEq;
90720 
90721   assert( iLeft<iRight );
90722   assert( pSrc->nSrc>iRight );
90723   assert( pSrc->a[iLeft].pTab );
90724   assert( pSrc->a[iRight].pTab );
90725 
90726   pE1 = sqlite3CreateColumnExpr(db, pSrc, iLeft, iColLeft);
90727   pE2 = sqlite3CreateColumnExpr(db, pSrc, iRight, iColRight);
90728 
90729   pEq = sqlite3PExpr(pParse, TK_EQ, pE1, pE2, 0);
90730   if( pEq && isOuterJoin ){
90731     ExprSetProperty(pEq, EP_FromJoin);
90732     assert( !ExprHasAnyProperty(pEq, EP_TokenOnly|EP_Reduced) );
90733     ExprSetIrreducible(pEq);
90734     pEq->iRightJoinTable = (i16)pE2->iTable;
90735   }
90736   *ppWhere = sqlite3ExprAnd(db, *ppWhere, pEq);
90737 }
90738 
90739 /*
90740 ** Set the EP_FromJoin property on all terms of the given expression.
90741 ** And set the Expr.iRightJoinTable to iTable for every term in the
90742 ** expression.
90743 **
90744 ** The EP_FromJoin property is used on terms of an expression to tell
90745 ** the LEFT OUTER JOIN processing logic that this term is part of the
90746 ** join restriction specified in the ON or USING clause and not a part
90747 ** of the more general WHERE clause.  These terms are moved over to the
90748 ** WHERE clause during join processing but we need to remember that they
90749 ** originated in the ON or USING clause.
90750 **
90751 ** The Expr.iRightJoinTable tells the WHERE clause processing that the
90752 ** expression depends on table iRightJoinTable even if that table is not
90753 ** explicitly mentioned in the expression.  That information is needed
90754 ** for cases like this:
90755 **
90756 **    SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.b AND t1.x=5
90757 **
90758 ** The where clause needs to defer the handling of the t1.x=5
90759 ** term until after the t2 loop of the join.  In that way, a
90760 ** NULL t2 row will be inserted whenever t1.x!=5.  If we do not
90761 ** defer the handling of t1.x=5, it will be processed immediately
90762 ** after the t1 loop and rows with t1.x!=5 will never appear in
90763 ** the output, which is incorrect.
90764 */
90765 static void setJoinExpr(Expr *p, int iTable){
90766   while( p ){
90767     ExprSetProperty(p, EP_FromJoin);
90768     assert( !ExprHasAnyProperty(p, EP_TokenOnly|EP_Reduced) );
90769     ExprSetIrreducible(p);
90770     p->iRightJoinTable = (i16)iTable;
90771     setJoinExpr(p->pLeft, iTable);
90772     p = p->pRight;
90773   }
90774 }
90775 
90776 /*
90777 ** This routine processes the join information for a SELECT statement.
90778 ** ON and USING clauses are converted into extra terms of the WHERE clause.
90779 ** NATURAL joins also create extra WHERE clause terms.
90780 **
90781 ** The terms of a FROM clause are contained in the Select.pSrc structure.
90782 ** The left most table is the first entry in Select.pSrc.  The right-most
90783 ** table is the last entry.  The join operator is held in the entry to
90784 ** the left.  Thus entry 0 contains the join operator for the join between
90785 ** entries 0 and 1.  Any ON or USING clauses associated with the join are
90786 ** also attached to the left entry.
90787 **
90788 ** This routine returns the number of errors encountered.
90789 */
90790 static int sqliteProcessJoin(Parse *pParse, Select *p){
90791   SrcList *pSrc;                  /* All tables in the FROM clause */
90792   int i, j;                       /* Loop counters */
90793   struct SrcList_item *pLeft;     /* Left table being joined */
90794   struct SrcList_item *pRight;    /* Right table being joined */
90795 
90796   pSrc = p->pSrc;
90797   pLeft = &pSrc->a[0];
90798   pRight = &pLeft[1];
90799   for(i=0; i<pSrc->nSrc-1; i++, pRight++, pLeft++){
90800     Table *pLeftTab = pLeft->pTab;
90801     Table *pRightTab = pRight->pTab;
90802     int isOuter;
90803 
90804     if( NEVER(pLeftTab==0 || pRightTab==0) ) continue;
90805     isOuter = (pRight->jointype & JT_OUTER)!=0;
90806 
90807     /* When the NATURAL keyword is present, add WHERE clause terms for
90808     ** every column that the two tables have in common.
90809     */
90810     if( pRight->jointype & JT_NATURAL ){
90811       if( pRight->pOn || pRight->pUsing ){
90812         sqlite3ErrorMsg(pParse, "a NATURAL join may not have "
90813            "an ON or USING clause", 0);
90814         return 1;
90815       }
90816       for(j=0; j<pRightTab->nCol; j++){
90817         char *zName;   /* Name of column in the right table */
90818         int iLeft;     /* Matching left table */
90819         int iLeftCol;  /* Matching column in the left table */
90820 
90821         zName = pRightTab->aCol[j].zName;
90822         if( tableAndColumnIndex(pSrc, i+1, zName, &iLeft, &iLeftCol) ){
90823           addWhereTerm(pParse, pSrc, iLeft, iLeftCol, i+1, j,
90824                        isOuter, &p->pWhere);
90825         }
90826       }
90827     }
90828 
90829     /* Disallow both ON and USING clauses in the same join
90830     */
90831     if( pRight->pOn && pRight->pUsing ){
90832       sqlite3ErrorMsg(pParse, "cannot have both ON and USING "
90833         "clauses in the same join");
90834       return 1;
90835     }
90836 
90837     /* Add the ON clause to the end of the WHERE clause, connected by
90838     ** an AND operator.
90839     */
90840     if( pRight->pOn ){
90841       if( isOuter ) setJoinExpr(pRight->pOn, pRight->iCursor);
90842       p->pWhere = sqlite3ExprAnd(pParse->db, p->pWhere, pRight->pOn);
90843       pRight->pOn = 0;
90844     }
90845 
90846     /* Create extra terms on the WHERE clause for each column named
90847     ** in the USING clause.  Example: If the two tables to be joined are
90848     ** A and B and the USING clause names X, Y, and Z, then add this
90849     ** to the WHERE clause:    A.X=B.X AND A.Y=B.Y AND A.Z=B.Z
90850     ** Report an error if any column mentioned in the USING clause is
90851     ** not contained in both tables to be joined.
90852     */
90853     if( pRight->pUsing ){
90854       IdList *pList = pRight->pUsing;
90855       for(j=0; j<pList->nId; j++){
90856         char *zName;     /* Name of the term in the USING clause */
90857         int iLeft;       /* Table on the left with matching column name */
90858         int iLeftCol;    /* Column number of matching column on the left */
90859         int iRightCol;   /* Column number of matching column on the right */
90860 
90861         zName = pList->a[j].zName;
90862         iRightCol = columnIndex(pRightTab, zName);
90863         if( iRightCol<0
90864          || !tableAndColumnIndex(pSrc, i+1, zName, &iLeft, &iLeftCol)
90865         ){
90866           sqlite3ErrorMsg(pParse, "cannot join using column %s - column "
90867             "not present in both tables", zName);
90868           return 1;
90869         }
90870         addWhereTerm(pParse, pSrc, iLeft, iLeftCol, i+1, iRightCol,
90871                      isOuter, &p->pWhere);
90872       }
90873     }
90874   }
90875   return 0;
90876 }
90877 
90878 /*
90879 ** Insert code into "v" that will push the record on the top of the
90880 ** stack into the sorter.
90881 */
90882 static void pushOntoSorter(
90883   Parse *pParse,         /* Parser context */
90884   ExprList *pOrderBy,    /* The ORDER BY clause */
90885   Select *pSelect,       /* The whole SELECT statement */
90886   int regData            /* Register holding data to be sorted */
90887 ){
90888   Vdbe *v = pParse->pVdbe;
90889   int nExpr = pOrderBy->nExpr;
90890   int regBase = sqlite3GetTempRange(pParse, nExpr+2);
90891   int regRecord = sqlite3GetTempReg(pParse);
90892   sqlite3ExprCacheClear(pParse);
90893   sqlite3ExprCodeExprList(pParse, pOrderBy, regBase, 0);
90894   sqlite3VdbeAddOp2(v, OP_Sequence, pOrderBy->iECursor, regBase+nExpr);
90895   sqlite3ExprCodeMove(pParse, regData, regBase+nExpr+1, 1);
90896   sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nExpr + 2, regRecord);
90897   sqlite3VdbeAddOp2(v, OP_IdxInsert, pOrderBy->iECursor, regRecord);
90898   sqlite3ReleaseTempReg(pParse, regRecord);
90899   sqlite3ReleaseTempRange(pParse, regBase, nExpr+2);
90900   if( pSelect->iLimit ){
90901     int addr1, addr2;
90902     int iLimit;
90903     if( pSelect->iOffset ){
90904       iLimit = pSelect->iOffset+1;
90905     }else{
90906       iLimit = pSelect->iLimit;
90907     }
90908     addr1 = sqlite3VdbeAddOp1(v, OP_IfZero, iLimit);
90909     sqlite3VdbeAddOp2(v, OP_AddImm, iLimit, -1);
90910     addr2 = sqlite3VdbeAddOp0(v, OP_Goto);
90911     sqlite3VdbeJumpHere(v, addr1);
90912     sqlite3VdbeAddOp1(v, OP_Last, pOrderBy->iECursor);
90913     sqlite3VdbeAddOp1(v, OP_Delete, pOrderBy->iECursor);
90914     sqlite3VdbeJumpHere(v, addr2);
90915   }
90916 }
90917 
90918 /*
90919 ** Add code to implement the OFFSET
90920 */
90921 static void codeOffset(
90922   Vdbe *v,          /* Generate code into this VM */
90923   Select *p,        /* The SELECT statement being coded */
90924   int iContinue     /* Jump here to skip the current record */
90925 ){
90926   if( p->iOffset && iContinue!=0 ){
90927     int addr;
90928     sqlite3VdbeAddOp2(v, OP_AddImm, p->iOffset, -1);
90929     addr = sqlite3VdbeAddOp1(v, OP_IfNeg, p->iOffset);
90930     sqlite3VdbeAddOp2(v, OP_Goto, 0, iContinue);
90931     VdbeComment((v, "skip OFFSET records"));
90932     sqlite3VdbeJumpHere(v, addr);
90933   }
90934 }
90935 
90936 /*
90937 ** Add code that will check to make sure the N registers starting at iMem
90938 ** form a distinct entry.  iTab is a sorting index that holds previously
90939 ** seen combinations of the N values.  A new entry is made in iTab
90940 ** if the current N values are new.
90941 **
90942 ** A jump to addrRepeat is made and the N+1 values are popped from the
90943 ** stack if the top N elements are not distinct.
90944 */
90945 static void codeDistinct(
90946   Parse *pParse,     /* Parsing and code generating context */
90947   int iTab,          /* A sorting index used to test for distinctness */
90948   int addrRepeat,    /* Jump to here if not distinct */
90949   int N,             /* Number of elements */
90950   int iMem           /* First element */
90951 ){
90952   Vdbe *v;
90953   int r1;
90954 
90955   v = pParse->pVdbe;
90956   r1 = sqlite3GetTempReg(pParse);
90957   sqlite3VdbeAddOp4Int(v, OP_Found, iTab, addrRepeat, iMem, N);
90958   sqlite3VdbeAddOp3(v, OP_MakeRecord, iMem, N, r1);
90959   sqlite3VdbeAddOp2(v, OP_IdxInsert, iTab, r1);
90960   sqlite3ReleaseTempReg(pParse, r1);
90961 }
90962 
90963 #ifndef SQLITE_OMIT_SUBQUERY
90964 /*
90965 ** Generate an error message when a SELECT is used within a subexpression
90966 ** (example:  "a IN (SELECT * FROM table)") but it has more than 1 result
90967 ** column.  We do this in a subroutine because the error used to occur
90968 ** in multiple places.  (The error only occurs in one place now, but we
90969 ** retain the subroutine to minimize code disruption.)
90970 */
90971 static int checkForMultiColumnSelectError(
90972   Parse *pParse,       /* Parse context. */
90973   SelectDest *pDest,   /* Destination of SELECT results */
90974   int nExpr            /* Number of result columns returned by SELECT */
90975 ){
90976   int eDest = pDest->eDest;
90977   if( nExpr>1 && (eDest==SRT_Mem || eDest==SRT_Set) ){
90978     sqlite3ErrorMsg(pParse, "only a single result allowed for "
90979        "a SELECT that is part of an expression");
90980     return 1;
90981   }else{
90982     return 0;
90983   }
90984 }
90985 #endif
90986 
90987 /*
90988 ** This routine generates the code for the inside of the inner loop
90989 ** of a SELECT.
90990 **
90991 ** If srcTab and nColumn are both zero, then the pEList expressions
90992 ** are evaluated in order to get the data for this row.  If nColumn>0
90993 ** then data is pulled from srcTab and pEList is used only to get the
90994 ** datatypes for each column.
90995 */
90996 static void selectInnerLoop(
90997   Parse *pParse,          /* The parser context */
90998   Select *p,              /* The complete select statement being coded */
90999   ExprList *pEList,       /* List of values being extracted */
91000   int srcTab,             /* Pull data from this table */
91001   int nColumn,            /* Number of columns in the source table */
91002   ExprList *pOrderBy,     /* If not NULL, sort results using this key */
91003   int distinct,           /* If >=0, make sure results are distinct */
91004   SelectDest *pDest,      /* How to dispose of the results */
91005   int iContinue,          /* Jump here to continue with next row */
91006   int iBreak              /* Jump here to break out of the inner loop */
91007 ){
91008   Vdbe *v = pParse->pVdbe;
91009   int i;
91010   int hasDistinct;        /* True if the DISTINCT keyword is present */
91011   int regResult;              /* Start of memory holding result set */
91012   int eDest = pDest->eDest;   /* How to dispose of results */
91013   int iParm = pDest->iParm;   /* First argument to disposal method */
91014   int nResultCol;             /* Number of result columns */
91015 
91016   assert( v );
91017   if( NEVER(v==0) ) return;
91018   assert( pEList!=0 );
91019   hasDistinct = distinct>=0;
91020   if( pOrderBy==0 && !hasDistinct ){
91021     codeOffset(v, p, iContinue);
91022   }
91023 
91024   /* Pull the requested columns.
91025   */
91026   if( nColumn>0 ){
91027     nResultCol = nColumn;
91028   }else{
91029     nResultCol = pEList->nExpr;
91030   }
91031   if( pDest->iMem==0 ){
91032     pDest->iMem = pParse->nMem+1;
91033     pDest->nMem = nResultCol;
91034     pParse->nMem += nResultCol;
91035   }else{
91036     assert( pDest->nMem==nResultCol );
91037   }
91038   regResult = pDest->iMem;
91039   if( nColumn>0 ){
91040     for(i=0; i<nColumn; i++){
91041       sqlite3VdbeAddOp3(v, OP_Column, srcTab, i, regResult+i);
91042     }
91043   }else if( eDest!=SRT_Exists ){
91044     /* If the destination is an EXISTS(...) expression, the actual
91045     ** values returned by the SELECT are not required.
91046     */
91047     sqlite3ExprCacheClear(pParse);
91048     sqlite3ExprCodeExprList(pParse, pEList, regResult, eDest==SRT_Output);
91049   }
91050   nColumn = nResultCol;
91051 
91052   /* If the DISTINCT keyword was present on the SELECT statement
91053   ** and this row has been seen before, then do not make this row
91054   ** part of the result.
91055   */
91056   if( hasDistinct ){
91057     assert( pEList!=0 );
91058     assert( pEList->nExpr==nColumn );
91059     codeDistinct(pParse, distinct, iContinue, nColumn, regResult);
91060     if( pOrderBy==0 ){
91061       codeOffset(v, p, iContinue);
91062     }
91063   }
91064 
91065   switch( eDest ){
91066     /* In this mode, write each query result to the key of the temporary
91067     ** table iParm.
91068     */
91069 #ifndef SQLITE_OMIT_COMPOUND_SELECT
91070     case SRT_Union: {
91071       int r1;
91072       r1 = sqlite3GetTempReg(pParse);
91073       sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nColumn, r1);
91074       sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, r1);
91075       sqlite3ReleaseTempReg(pParse, r1);
91076       break;
91077     }
91078 
91079     /* Construct a record from the query result, but instead of
91080     ** saving that record, use it as a key to delete elements from
91081     ** the temporary table iParm.
91082     */
91083     case SRT_Except: {
91084       sqlite3VdbeAddOp3(v, OP_IdxDelete, iParm, regResult, nColumn);
91085       break;
91086     }
91087 #endif
91088 
91089     /* Store the result as data using a unique key.
91090     */
91091     case SRT_Table:
91092     case SRT_EphemTab: {
91093       int r1 = sqlite3GetTempReg(pParse);
91094       testcase( eDest==SRT_Table );
91095       testcase( eDest==SRT_EphemTab );
91096       sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nColumn, r1);
91097       if( pOrderBy ){
91098         pushOntoSorter(pParse, pOrderBy, p, r1);
91099       }else{
91100         int r2 = sqlite3GetTempReg(pParse);
91101         sqlite3VdbeAddOp2(v, OP_NewRowid, iParm, r2);
91102         sqlite3VdbeAddOp3(v, OP_Insert, iParm, r1, r2);
91103         sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
91104         sqlite3ReleaseTempReg(pParse, r2);
91105       }
91106       sqlite3ReleaseTempReg(pParse, r1);
91107       break;
91108     }
91109 
91110 #ifndef SQLITE_OMIT_SUBQUERY
91111     /* If we are creating a set for an "expr IN (SELECT ...)" construct,
91112     ** then there should be a single item on the stack.  Write this
91113     ** item into the set table with bogus data.
91114     */
91115     case SRT_Set: {
91116       assert( nColumn==1 );
91117       p->affinity = sqlite3CompareAffinity(pEList->a[0].pExpr, pDest->affinity);
91118       if( pOrderBy ){
91119         /* At first glance you would think we could optimize out the
91120         ** ORDER BY in this case since the order of entries in the set
91121         ** does not matter.  But there might be a LIMIT clause, in which
91122         ** case the order does matter */
91123         pushOntoSorter(pParse, pOrderBy, p, regResult);
91124       }else{
91125         int r1 = sqlite3GetTempReg(pParse);
91126         sqlite3VdbeAddOp4(v, OP_MakeRecord, regResult, 1, r1, &p->affinity, 1);
91127         sqlite3ExprCacheAffinityChange(pParse, regResult, 1);
91128         sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, r1);
91129         sqlite3ReleaseTempReg(pParse, r1);
91130       }
91131       break;
91132     }
91133 
91134     /* If any row exist in the result set, record that fact and abort.
91135     */
91136     case SRT_Exists: {
91137       sqlite3VdbeAddOp2(v, OP_Integer, 1, iParm);
91138       /* The LIMIT clause will terminate the loop for us */
91139       break;
91140     }
91141 
91142     /* If this is a scalar select that is part of an expression, then
91143     ** store the results in the appropriate memory cell and break out
91144     ** of the scan loop.
91145     */
91146     case SRT_Mem: {
91147       assert( nColumn==1 );
91148       if( pOrderBy ){
91149         pushOntoSorter(pParse, pOrderBy, p, regResult);
91150       }else{
91151         sqlite3ExprCodeMove(pParse, regResult, iParm, 1);
91152         /* The LIMIT clause will jump out of the loop for us */
91153       }
91154       break;
91155     }
91156 #endif /* #ifndef SQLITE_OMIT_SUBQUERY */
91157 
91158     /* Send the data to the callback function or to a subroutine.  In the
91159     ** case of a subroutine, the subroutine itself is responsible for
91160     ** popping the data from the stack.
91161     */
91162     case SRT_Coroutine:
91163     case SRT_Output: {
91164       testcase( eDest==SRT_Coroutine );
91165       testcase( eDest==SRT_Output );
91166       if( pOrderBy ){
91167         int r1 = sqlite3GetTempReg(pParse);
91168         sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nColumn, r1);
91169         pushOntoSorter(pParse, pOrderBy, p, r1);
91170         sqlite3ReleaseTempReg(pParse, r1);
91171       }else if( eDest==SRT_Coroutine ){
91172         sqlite3VdbeAddOp1(v, OP_Yield, pDest->iParm);
91173       }else{
91174         sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, nColumn);
91175         sqlite3ExprCacheAffinityChange(pParse, regResult, nColumn);
91176       }
91177       break;
91178     }
91179 
91180 #if !defined(SQLITE_OMIT_TRIGGER)
91181     /* Discard the results.  This is used for SELECT statements inside
91182     ** the body of a TRIGGER.  The purpose of such selects is to call
91183     ** user-defined functions that have side effects.  We do not care
91184     ** about the actual results of the select.
91185     */
91186     default: {
91187       assert( eDest==SRT_Discard );
91188       break;
91189     }
91190 #endif
91191   }
91192 
91193   /* Jump to the end of the loop if the LIMIT is reached.  Except, if
91194   ** there is a sorter, in which case the sorter has already limited
91195   ** the output for us.
91196   */
91197   if( pOrderBy==0 && p->iLimit ){
91198     sqlite3VdbeAddOp3(v, OP_IfZero, p->iLimit, iBreak, -1);
91199   }
91200 }
91201 
91202 /*
91203 ** Given an expression list, generate a KeyInfo structure that records
91204 ** the collating sequence for each expression in that expression list.
91205 **
91206 ** If the ExprList is an ORDER BY or GROUP BY clause then the resulting
91207 ** KeyInfo structure is appropriate for initializing a virtual index to
91208 ** implement that clause.  If the ExprList is the result set of a SELECT
91209 ** then the KeyInfo structure is appropriate for initializing a virtual
91210 ** index to implement a DISTINCT test.
91211 **
91212 ** Space to hold the KeyInfo structure is obtain from malloc.  The calling
91213 ** function is responsible for seeing that this structure is eventually
91214 ** freed.  Add the KeyInfo structure to the P4 field of an opcode using
91215 ** P4_KEYINFO_HANDOFF is the usual way of dealing with this.
91216 */
91217 static KeyInfo *keyInfoFromExprList(Parse *pParse, ExprList *pList){
91218   sqlite3 *db = pParse->db;
91219   int nExpr;
91220   KeyInfo *pInfo;
91221   struct ExprList_item *pItem;
91222   int i;
91223 
91224   nExpr = pList->nExpr;
91225   pInfo = sqlite3DbMallocZero(db, sizeof(*pInfo) + nExpr*(sizeof(CollSeq*)+1) );
91226   if( pInfo ){
91227     pInfo->aSortOrder = (u8*)&pInfo->aColl[nExpr];
91228     pInfo->nField = (u16)nExpr;
91229     pInfo->enc = ENC(db);
91230     pInfo->db = db;
91231     for(i=0, pItem=pList->a; i<nExpr; i++, pItem++){
91232       CollSeq *pColl;
91233       pColl = sqlite3ExprCollSeq(pParse, pItem->pExpr);
91234       if( !pColl ){
91235         pColl = db->pDfltColl;
91236       }
91237       pInfo->aColl[i] = pColl;
91238       pInfo->aSortOrder[i] = pItem->sortOrder;
91239     }
91240   }
91241   return pInfo;
91242 }
91243 
91244 #ifndef SQLITE_OMIT_COMPOUND_SELECT
91245 /*
91246 ** Name of the connection operator, used for error messages.
91247 */
91248 static const char *selectOpName(int id){
91249   char *z;
91250   switch( id ){
91251     case TK_ALL:       z = "UNION ALL";   break;
91252     case TK_INTERSECT: z = "INTERSECT";   break;
91253     case TK_EXCEPT:    z = "EXCEPT";      break;
91254     default:           z = "UNION";       break;
91255   }
91256   return z;
91257 }
91258 #endif /* SQLITE_OMIT_COMPOUND_SELECT */
91259 
91260 #ifndef SQLITE_OMIT_EXPLAIN
91261 /*
91262 ** Unless an "EXPLAIN QUERY PLAN" command is being processed, this function
91263 ** is a no-op. Otherwise, it adds a single row of output to the EQP result,
91264 ** where the caption is of the form:
91265 **
91266 **   "USE TEMP B-TREE FOR xxx"
91267 **
91268 ** where xxx is one of "DISTINCT", "ORDER BY" or "GROUP BY". Exactly which
91269 ** is determined by the zUsage argument.
91270 */
91271 static void explainTempTable(Parse *pParse, const char *zUsage){
91272   if( pParse->explain==2 ){
91273     Vdbe *v = pParse->pVdbe;
91274     char *zMsg = sqlite3MPrintf(pParse->db, "USE TEMP B-TREE FOR %s", zUsage);
91275     sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC);
91276   }
91277 }
91278 
91279 /*
91280 ** Assign expression b to lvalue a. A second, no-op, version of this macro
91281 ** is provided when SQLITE_OMIT_EXPLAIN is defined. This allows the code
91282 ** in sqlite3Select() to assign values to structure member variables that
91283 ** only exist if SQLITE_OMIT_EXPLAIN is not defined without polluting the
91284 ** code with #ifndef directives.
91285 */
91286 # define explainSetInteger(a, b) a = b
91287 
91288 #else
91289 /* No-op versions of the explainXXX() functions and macros. */
91290 # define explainTempTable(y,z)
91291 # define explainSetInteger(y,z)
91292 #endif
91293 
91294 #if !defined(SQLITE_OMIT_EXPLAIN) && !defined(SQLITE_OMIT_COMPOUND_SELECT)
91295 /*
91296 ** Unless an "EXPLAIN QUERY PLAN" command is being processed, this function
91297 ** is a no-op. Otherwise, it adds a single row of output to the EQP result,
91298 ** where the caption is of one of the two forms:
91299 **
91300 **   "COMPOSITE SUBQUERIES iSub1 and iSub2 (op)"
91301 **   "COMPOSITE SUBQUERIES iSub1 and iSub2 USING TEMP B-TREE (op)"
91302 **
91303 ** where iSub1 and iSub2 are the integers passed as the corresponding
91304 ** function parameters, and op is the text representation of the parameter
91305 ** of the same name. The parameter "op" must be one of TK_UNION, TK_EXCEPT,
91306 ** TK_INTERSECT or TK_ALL. The first form is used if argument bUseTmp is
91307 ** false, or the second form if it is true.
91308 */
91309 static void explainComposite(
91310   Parse *pParse,                  /* Parse context */
91311   int op,                         /* One of TK_UNION, TK_EXCEPT etc. */
91312   int iSub1,                      /* Subquery id 1 */
91313   int iSub2,                      /* Subquery id 2 */
91314   int bUseTmp                     /* True if a temp table was used */
91315 ){
91316   assert( op==TK_UNION || op==TK_EXCEPT || op==TK_INTERSECT || op==TK_ALL );
91317   if( pParse->explain==2 ){
91318     Vdbe *v = pParse->pVdbe;
91319     char *zMsg = sqlite3MPrintf(
91320         pParse->db, "COMPOUND SUBQUERIES %d AND %d %s(%s)", iSub1, iSub2,
91321         bUseTmp?"USING TEMP B-TREE ":"", selectOpName(op)
91322     );
91323     sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC);
91324   }
91325 }
91326 #else
91327 /* No-op versions of the explainXXX() functions and macros. */
91328 # define explainComposite(v,w,x,y,z)
91329 #endif
91330 
91331 /*
91332 ** If the inner loop was generated using a non-null pOrderBy argument,
91333 ** then the results were placed in a sorter.  After the loop is terminated
91334 ** we need to run the sorter and output the results.  The following
91335 ** routine generates the code needed to do that.
91336 */
91337 static void generateSortTail(
91338   Parse *pParse,    /* Parsing context */
91339   Select *p,        /* The SELECT statement */
91340   Vdbe *v,          /* Generate code into this VDBE */
91341   int nColumn,      /* Number of columns of data */
91342   SelectDest *pDest /* Write the sorted results here */
91343 ){
91344   int addrBreak = sqlite3VdbeMakeLabel(v);     /* Jump here to exit loop */
91345   int addrContinue = sqlite3VdbeMakeLabel(v);  /* Jump here for next cycle */
91346   int addr;
91347   int iTab;
91348   int pseudoTab = 0;
91349   ExprList *pOrderBy = p->pOrderBy;
91350 
91351   int eDest = pDest->eDest;
91352   int iParm = pDest->iParm;
91353 
91354   int regRow;
91355   int regRowid;
91356 
91357   iTab = pOrderBy->iECursor;
91358   regRow = sqlite3GetTempReg(pParse);
91359   if( eDest==SRT_Output || eDest==SRT_Coroutine ){
91360     pseudoTab = pParse->nTab++;
91361     sqlite3VdbeAddOp3(v, OP_OpenPseudo, pseudoTab, regRow, nColumn);
91362     regRowid = 0;
91363   }else{
91364     regRowid = sqlite3GetTempReg(pParse);
91365   }
91366   addr = 1 + sqlite3VdbeAddOp2(v, OP_Sort, iTab, addrBreak);
91367   codeOffset(v, p, addrContinue);
91368   sqlite3VdbeAddOp3(v, OP_Column, iTab, pOrderBy->nExpr + 1, regRow);
91369   switch( eDest ){
91370     case SRT_Table:
91371     case SRT_EphemTab: {
91372       testcase( eDest==SRT_Table );
91373       testcase( eDest==SRT_EphemTab );
91374       sqlite3VdbeAddOp2(v, OP_NewRowid, iParm, regRowid);
91375       sqlite3VdbeAddOp3(v, OP_Insert, iParm, regRow, regRowid);
91376       sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
91377       break;
91378     }
91379 #ifndef SQLITE_OMIT_SUBQUERY
91380     case SRT_Set: {
91381       assert( nColumn==1 );
91382       sqlite3VdbeAddOp4(v, OP_MakeRecord, regRow, 1, regRowid, &p->affinity, 1);
91383       sqlite3ExprCacheAffinityChange(pParse, regRow, 1);
91384       sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, regRowid);
91385       break;
91386     }
91387     case SRT_Mem: {
91388       assert( nColumn==1 );
91389       sqlite3ExprCodeMove(pParse, regRow, iParm, 1);
91390       /* The LIMIT clause will terminate the loop for us */
91391       break;
91392     }
91393 #endif
91394     default: {
91395       int i;
91396       assert( eDest==SRT_Output || eDest==SRT_Coroutine );
91397       testcase( eDest==SRT_Output );
91398       testcase( eDest==SRT_Coroutine );
91399       for(i=0; i<nColumn; i++){
91400         assert( regRow!=pDest->iMem+i );
91401         sqlite3VdbeAddOp3(v, OP_Column, pseudoTab, i, pDest->iMem+i);
91402         if( i==0 ){
91403           sqlite3VdbeChangeP5(v, OPFLAG_CLEARCACHE);
91404         }
91405       }
91406       if( eDest==SRT_Output ){
91407         sqlite3VdbeAddOp2(v, OP_ResultRow, pDest->iMem, nColumn);
91408         sqlite3ExprCacheAffinityChange(pParse, pDest->iMem, nColumn);
91409       }else{
91410         sqlite3VdbeAddOp1(v, OP_Yield, pDest->iParm);
91411       }
91412       break;
91413     }
91414   }
91415   sqlite3ReleaseTempReg(pParse, regRow);
91416   sqlite3ReleaseTempReg(pParse, regRowid);
91417 
91418   /* The bottom of the loop
91419   */
91420   sqlite3VdbeResolveLabel(v, addrContinue);
91421   sqlite3VdbeAddOp2(v, OP_Next, iTab, addr);
91422   sqlite3VdbeResolveLabel(v, addrBreak);
91423   if( eDest==SRT_Output || eDest==SRT_Coroutine ){
91424     sqlite3VdbeAddOp2(v, OP_Close, pseudoTab, 0);
91425   }
91426 }
91427 
91428 /*
91429 ** Return a pointer to a string containing the 'declaration type' of the
91430 ** expression pExpr. The string may be treated as static by the caller.
91431 **
91432 ** The declaration type is the exact datatype definition extracted from the
91433 ** original CREATE TABLE statement if the expression is a column. The
91434 ** declaration type for a ROWID field is INTEGER. Exactly when an expression
91435 ** is considered a column can be complex in the presence of subqueries. The
91436 ** result-set expression in all of the following SELECT statements is
91437 ** considered a column by this function.
91438 **
91439 **   SELECT col FROM tbl;
91440 **   SELECT (SELECT col FROM tbl;
91441 **   SELECT (SELECT col FROM tbl);
91442 **   SELECT abc FROM (SELECT col AS abc FROM tbl);
91443 **
91444 ** The declaration type for any expression other than a column is NULL.
91445 */
91446 static const char *columnType(
91447   NameContext *pNC,
91448   Expr *pExpr,
91449   const char **pzOriginDb,
91450   const char **pzOriginTab,
91451   const char **pzOriginCol
91452 ){
91453   char const *zType = 0;
91454   char const *zOriginDb = 0;
91455   char const *zOriginTab = 0;
91456   char const *zOriginCol = 0;
91457   int j;
91458   if( NEVER(pExpr==0) || pNC->pSrcList==0 ) return 0;
91459 
91460   switch( pExpr->op ){
91461     case TK_AGG_COLUMN:
91462     case TK_COLUMN: {
91463       /* The expression is a column. Locate the table the column is being
91464       ** extracted from in NameContext.pSrcList. This table may be real
91465       ** database table or a subquery.
91466       */
91467       Table *pTab = 0;            /* Table structure column is extracted from */
91468       Select *pS = 0;             /* Select the column is extracted from */
91469       int iCol = pExpr->iColumn;  /* Index of column in pTab */
91470       testcase( pExpr->op==TK_AGG_COLUMN );
91471       testcase( pExpr->op==TK_COLUMN );
91472       while( pNC && !pTab ){
91473         SrcList *pTabList = pNC->pSrcList;
91474         for(j=0;j<pTabList->nSrc && pTabList->a[j].iCursor!=pExpr->iTable;j++);
91475         if( j<pTabList->nSrc ){
91476           pTab = pTabList->a[j].pTab;
91477           pS = pTabList->a[j].pSelect;
91478         }else{
91479           pNC = pNC->pNext;
91480         }
91481       }
91482 
91483       if( pTab==0 ){
91484         /* At one time, code such as "SELECT new.x" within a trigger would
91485         ** cause this condition to run.  Since then, we have restructured how
91486         ** trigger code is generated and so this condition is no longer
91487         ** possible. However, it can still be true for statements like
91488         ** the following:
91489         **
91490         **   CREATE TABLE t1(col INTEGER);
91491         **   SELECT (SELECT t1.col) FROM FROM t1;
91492         **
91493         ** when columnType() is called on the expression "t1.col" in the
91494         ** sub-select. In this case, set the column type to NULL, even
91495         ** though it should really be "INTEGER".
91496         **
91497         ** This is not a problem, as the column type of "t1.col" is never
91498         ** used. When columnType() is called on the expression
91499         ** "(SELECT t1.col)", the correct type is returned (see the TK_SELECT
91500         ** branch below.  */
91501         break;
91502       }
91503 
91504       assert( pTab && pExpr->pTab==pTab );
91505       if( pS ){
91506         /* The "table" is actually a sub-select or a view in the FROM clause
91507         ** of the SELECT statement. Return the declaration type and origin
91508         ** data for the result-set column of the sub-select.
91509         */
91510         if( iCol>=0 && ALWAYS(iCol<pS->pEList->nExpr) ){
91511           /* If iCol is less than zero, then the expression requests the
91512           ** rowid of the sub-select or view. This expression is legal (see
91513           ** test case misc2.2.2) - it always evaluates to NULL.
91514           */
91515           NameContext sNC;
91516           Expr *p = pS->pEList->a[iCol].pExpr;
91517           sNC.pSrcList = pS->pSrc;
91518           sNC.pNext = pNC;
91519           sNC.pParse = pNC->pParse;
91520           zType = columnType(&sNC, p, &zOriginDb, &zOriginTab, &zOriginCol);
91521         }
91522       }else if( ALWAYS(pTab->pSchema) ){
91523         /* A real table */
91524         assert( !pS );
91525         if( iCol<0 ) iCol = pTab->iPKey;
91526         assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) );
91527         if( iCol<0 ){
91528           zType = "INTEGER";
91529           zOriginCol = "rowid";
91530         }else{
91531           zType = pTab->aCol[iCol].zType;
91532           zOriginCol = pTab->aCol[iCol].zName;
91533         }
91534         zOriginTab = pTab->zName;
91535         if( pNC->pParse ){
91536           int iDb = sqlite3SchemaToIndex(pNC->pParse->db, pTab->pSchema);
91537           zOriginDb = pNC->pParse->db->aDb[iDb].zName;
91538         }
91539       }
91540       break;
91541     }
91542 #ifndef SQLITE_OMIT_SUBQUERY
91543     case TK_SELECT: {
91544       /* The expression is a sub-select. Return the declaration type and
91545       ** origin info for the single column in the result set of the SELECT
91546       ** statement.
91547       */
91548       NameContext sNC;
91549       Select *pS = pExpr->x.pSelect;
91550       Expr *p = pS->pEList->a[0].pExpr;
91551       assert( ExprHasProperty(pExpr, EP_xIsSelect) );
91552       sNC.pSrcList = pS->pSrc;
91553       sNC.pNext = pNC;
91554       sNC.pParse = pNC->pParse;
91555       zType = columnType(&sNC, p, &zOriginDb, &zOriginTab, &zOriginCol);
91556       break;
91557     }
91558 #endif
91559   }
91560 
91561   if( pzOriginDb ){
91562     assert( pzOriginTab && pzOriginCol );
91563     *pzOriginDb = zOriginDb;
91564     *pzOriginTab = zOriginTab;
91565     *pzOriginCol = zOriginCol;
91566   }
91567   return zType;
91568 }
91569 
91570 /*
91571 ** Generate code that will tell the VDBE the declaration types of columns
91572 ** in the result set.
91573 */
91574 static void generateColumnTypes(
91575   Parse *pParse,      /* Parser context */
91576   SrcList *pTabList,  /* List of tables */
91577   ExprList *pEList    /* Expressions defining the result set */
91578 ){
91579 #ifndef SQLITE_OMIT_DECLTYPE
91580   Vdbe *v = pParse->pVdbe;
91581   int i;
91582   NameContext sNC;
91583   sNC.pSrcList = pTabList;
91584   sNC.pParse = pParse;
91585   for(i=0; i<pEList->nExpr; i++){
91586     Expr *p = pEList->a[i].pExpr;
91587     const char *zType;
91588 #ifdef SQLITE_ENABLE_COLUMN_METADATA
91589     const char *zOrigDb = 0;
91590     const char *zOrigTab = 0;
91591     const char *zOrigCol = 0;
91592     zType = columnType(&sNC, p, &zOrigDb, &zOrigTab, &zOrigCol);
91593 
91594     /* The vdbe must make its own copy of the column-type and other
91595     ** column specific strings, in case the schema is reset before this
91596     ** virtual machine is deleted.
91597     */
91598     sqlite3VdbeSetColName(v, i, COLNAME_DATABASE, zOrigDb, SQLITE_TRANSIENT);
91599     sqlite3VdbeSetColName(v, i, COLNAME_TABLE, zOrigTab, SQLITE_TRANSIENT);
91600     sqlite3VdbeSetColName(v, i, COLNAME_COLUMN, zOrigCol, SQLITE_TRANSIENT);
91601 #else
91602     zType = columnType(&sNC, p, 0, 0, 0);
91603 #endif
91604     sqlite3VdbeSetColName(v, i, COLNAME_DECLTYPE, zType, SQLITE_TRANSIENT);
91605   }
91606 #endif /* SQLITE_OMIT_DECLTYPE */
91607 }
91608 
91609 /*
91610 ** Generate code that will tell the VDBE the names of columns
91611 ** in the result set.  This information is used to provide the
91612 ** azCol[] values in the callback.
91613 */
91614 static void generateColumnNames(
91615   Parse *pParse,      /* Parser context */
91616   SrcList *pTabList,  /* List of tables */
91617   ExprList *pEList    /* Expressions defining the result set */
91618 ){
91619   Vdbe *v = pParse->pVdbe;
91620   int i, j;
91621   sqlite3 *db = pParse->db;
91622   int fullNames, shortNames;
91623 
91624 #ifndef SQLITE_OMIT_EXPLAIN
91625   /* If this is an EXPLAIN, skip this step */
91626   if( pParse->explain ){
91627     return;
91628   }
91629 #endif
91630 
91631   if( pParse->colNamesSet || NEVER(v==0) || db->mallocFailed ) return;
91632   pParse->colNamesSet = 1;
91633   fullNames = (db->flags & SQLITE_FullColNames)!=0;
91634   shortNames = (db->flags & SQLITE_ShortColNames)!=0;
91635   sqlite3VdbeSetNumCols(v, pEList->nExpr);
91636   for(i=0; i<pEList->nExpr; i++){
91637     Expr *p;
91638     p = pEList->a[i].pExpr;
91639     if( NEVER(p==0) ) continue;
91640     if( pEList->a[i].zName ){
91641       char *zName = pEList->a[i].zName;
91642       sqlite3VdbeSetColName(v, i, COLNAME_NAME, zName, SQLITE_TRANSIENT);
91643     }else if( (p->op==TK_COLUMN || p->op==TK_AGG_COLUMN) && pTabList ){
91644       Table *pTab;
91645       char *zCol;
91646       int iCol = p->iColumn;
91647       for(j=0; ALWAYS(j<pTabList->nSrc); j++){
91648         if( pTabList->a[j].iCursor==p->iTable ) break;
91649       }
91650       assert( j<pTabList->nSrc );
91651       pTab = pTabList->a[j].pTab;
91652       if( iCol<0 ) iCol = pTab->iPKey;
91653       assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) );
91654       if( iCol<0 ){
91655         zCol = "rowid";
91656       }else{
91657         zCol = pTab->aCol[iCol].zName;
91658       }
91659       if( !shortNames && !fullNames ){
91660         sqlite3VdbeSetColName(v, i, COLNAME_NAME,
91661             sqlite3DbStrDup(db, pEList->a[i].zSpan), SQLITE_DYNAMIC);
91662       }else if( fullNames ){
91663         char *zName = 0;
91664         zName = sqlite3MPrintf(db, "%s.%s", pTab->zName, zCol);
91665         sqlite3VdbeSetColName(v, i, COLNAME_NAME, zName, SQLITE_DYNAMIC);
91666       }else{
91667         sqlite3VdbeSetColName(v, i, COLNAME_NAME, zCol, SQLITE_TRANSIENT);
91668       }
91669     }else{
91670       sqlite3VdbeSetColName(v, i, COLNAME_NAME,
91671           sqlite3DbStrDup(db, pEList->a[i].zSpan), SQLITE_DYNAMIC);
91672     }
91673   }
91674   generateColumnTypes(pParse, pTabList, pEList);
91675 }
91676 
91677 /*
91678 ** Given a an expression list (which is really the list of expressions
91679 ** that form the result set of a SELECT statement) compute appropriate
91680 ** column names for a table that would hold the expression list.
91681 **
91682 ** All column names will be unique.
91683 **
91684 ** Only the column names are computed.  Column.zType, Column.zColl,
91685 ** and other fields of Column are zeroed.
91686 **
91687 ** Return SQLITE_OK on success.  If a memory allocation error occurs,
91688 ** store NULL in *paCol and 0 in *pnCol and return SQLITE_NOMEM.
91689 */
91690 static int selectColumnsFromExprList(
91691   Parse *pParse,          /* Parsing context */
91692   ExprList *pEList,       /* Expr list from which to derive column names */
91693   int *pnCol,             /* Write the number of columns here */
91694   Column **paCol          /* Write the new column list here */
91695 ){
91696   sqlite3 *db = pParse->db;   /* Database connection */
91697   int i, j;                   /* Loop counters */
91698   int cnt;                    /* Index added to make the name unique */
91699   Column *aCol, *pCol;        /* For looping over result columns */
91700   int nCol;                   /* Number of columns in the result set */
91701   Expr *p;                    /* Expression for a single result column */
91702   char *zName;                /* Column name */
91703   int nName;                  /* Size of name in zName[] */
91704 
91705   *pnCol = nCol = pEList->nExpr;
91706   aCol = *paCol = sqlite3DbMallocZero(db, sizeof(aCol[0])*nCol);
91707   if( aCol==0 ) return SQLITE_NOMEM;
91708   for(i=0, pCol=aCol; i<nCol; i++, pCol++){
91709     /* Get an appropriate name for the column
91710     */
91711     p = pEList->a[i].pExpr;
91712     assert( p->pRight==0 || ExprHasProperty(p->pRight, EP_IntValue)
91713                || p->pRight->u.zToken==0 || p->pRight->u.zToken[0]!=0 );
91714     if( (zName = pEList->a[i].zName)!=0 ){
91715       /* If the column contains an "AS <name>" phrase, use <name> as the name */
91716       zName = sqlite3DbStrDup(db, zName);
91717     }else{
91718       Expr *pColExpr = p;  /* The expression that is the result column name */
91719       Table *pTab;         /* Table associated with this expression */
91720       while( pColExpr->op==TK_DOT ) pColExpr = pColExpr->pRight;
91721       if( pColExpr->op==TK_COLUMN && ALWAYS(pColExpr->pTab!=0) ){
91722         /* For columns use the column name name */
91723         int iCol = pColExpr->iColumn;
91724         pTab = pColExpr->pTab;
91725         if( iCol<0 ) iCol = pTab->iPKey;
91726         zName = sqlite3MPrintf(db, "%s",
91727                  iCol>=0 ? pTab->aCol[iCol].zName : "rowid");
91728       }else if( pColExpr->op==TK_ID ){
91729         assert( !ExprHasProperty(pColExpr, EP_IntValue) );
91730         zName = sqlite3MPrintf(db, "%s", pColExpr->u.zToken);
91731       }else{
91732         /* Use the original text of the column expression as its name */
91733         zName = sqlite3MPrintf(db, "%s", pEList->a[i].zSpan);
91734       }
91735     }
91736     if( db->mallocFailed ){
91737       sqlite3DbFree(db, zName);
91738       break;
91739     }
91740 
91741     /* Make sure the column name is unique.  If the name is not unique,
91742     ** append a integer to the name so that it becomes unique.
91743     */
91744     nName = sqlite3Strlen30(zName);
91745     for(j=cnt=0; j<i; j++){
91746       if( sqlite3StrICmp(aCol[j].zName, zName)==0 ){
91747         char *zNewName;
91748         zName[nName] = 0;
91749         zNewName = sqlite3MPrintf(db, "%s:%d", zName, ++cnt);
91750         sqlite3DbFree(db, zName);
91751         zName = zNewName;
91752         j = -1;
91753         if( zName==0 ) break;
91754       }
91755     }
91756     pCol->zName = zName;
91757   }
91758   if( db->mallocFailed ){
91759     for(j=0; j<i; j++){
91760       sqlite3DbFree(db, aCol[j].zName);
91761     }
91762     sqlite3DbFree(db, aCol);
91763     *paCol = 0;
91764     *pnCol = 0;
91765     return SQLITE_NOMEM;
91766   }
91767   return SQLITE_OK;
91768 }
91769 
91770 /*
91771 ** Add type and collation information to a column list based on
91772 ** a SELECT statement.
91773 **
91774 ** The column list presumably came from selectColumnNamesFromExprList().
91775 ** The column list has only names, not types or collations.  This
91776 ** routine goes through and adds the types and collations.
91777 **
91778 ** This routine requires that all identifiers in the SELECT
91779 ** statement be resolved.
91780 */
91781 static void selectAddColumnTypeAndCollation(
91782   Parse *pParse,        /* Parsing contexts */
91783   int nCol,             /* Number of columns */
91784   Column *aCol,         /* List of columns */
91785   Select *pSelect       /* SELECT used to determine types and collations */
91786 ){
91787   sqlite3 *db = pParse->db;
91788   NameContext sNC;
91789   Column *pCol;
91790   CollSeq *pColl;
91791   int i;
91792   Expr *p;
91793   struct ExprList_item *a;
91794 
91795   assert( pSelect!=0 );
91796   assert( (pSelect->selFlags & SF_Resolved)!=0 );
91797   assert( nCol==pSelect->pEList->nExpr || db->mallocFailed );
91798   if( db->mallocFailed ) return;
91799   memset(&sNC, 0, sizeof(sNC));
91800   sNC.pSrcList = pSelect->pSrc;
91801   a = pSelect->pEList->a;
91802   for(i=0, pCol=aCol; i<nCol; i++, pCol++){
91803     p = a[i].pExpr;
91804     pCol->zType = sqlite3DbStrDup(db, columnType(&sNC, p, 0, 0, 0));
91805     pCol->affinity = sqlite3ExprAffinity(p);
91806     if( pCol->affinity==0 ) pCol->affinity = SQLITE_AFF_NONE;
91807     pColl = sqlite3ExprCollSeq(pParse, p);
91808     if( pColl ){
91809       pCol->zColl = sqlite3DbStrDup(db, pColl->zName);
91810     }
91811   }
91812 }
91813 
91814 /*
91815 ** Given a SELECT statement, generate a Table structure that describes
91816 ** the result set of that SELECT.
91817 */
91818 SQLITE_PRIVATE Table *sqlite3ResultSetOfSelect(Parse *pParse, Select *pSelect){
91819   Table *pTab;
91820   sqlite3 *db = pParse->db;
91821   int savedFlags;
91822 
91823   savedFlags = db->flags;
91824   db->flags &= ~SQLITE_FullColNames;
91825   db->flags |= SQLITE_ShortColNames;
91826   sqlite3SelectPrep(pParse, pSelect, 0);
91827   if( pParse->nErr ) return 0;
91828   while( pSelect->pPrior ) pSelect = pSelect->pPrior;
91829   db->flags = savedFlags;
91830   pTab = sqlite3DbMallocZero(db, sizeof(Table) );
91831   if( pTab==0 ){
91832     return 0;
91833   }
91834   /* The sqlite3ResultSetOfSelect() is only used n contexts where lookaside
91835   ** is disabled */
91836   assert( db->lookaside.bEnabled==0 );
91837   pTab->nRef = 1;
91838   pTab->zName = 0;
91839   pTab->nRowEst = 1000000;
91840   selectColumnsFromExprList(pParse, pSelect->pEList, &pTab->nCol, &pTab->aCol);
91841   selectAddColumnTypeAndCollation(pParse, pTab->nCol, pTab->aCol, pSelect);
91842   pTab->iPKey = -1;
91843   if( db->mallocFailed ){
91844     sqlite3DeleteTable(db, pTab);
91845     return 0;
91846   }
91847   return pTab;
91848 }
91849 
91850 /*
91851 ** Get a VDBE for the given parser context.  Create a new one if necessary.
91852 ** If an error occurs, return NULL and leave a message in pParse.
91853 */
91854 SQLITE_PRIVATE Vdbe *sqlite3GetVdbe(Parse *pParse){
91855   Vdbe *v = pParse->pVdbe;
91856   if( v==0 ){
91857     v = pParse->pVdbe = sqlite3VdbeCreate(pParse->db);
91858 #ifndef SQLITE_OMIT_TRACE
91859     if( v ){
91860       sqlite3VdbeAddOp0(v, OP_Trace);
91861     }
91862 #endif
91863   }
91864   return v;
91865 }
91866 
91867 
91868 /*
91869 ** Compute the iLimit and iOffset fields of the SELECT based on the
91870 ** pLimit and pOffset expressions.  pLimit and pOffset hold the expressions
91871 ** that appear in the original SQL statement after the LIMIT and OFFSET
91872 ** keywords.  Or NULL if those keywords are omitted. iLimit and iOffset
91873 ** are the integer memory register numbers for counters used to compute
91874 ** the limit and offset.  If there is no limit and/or offset, then
91875 ** iLimit and iOffset are negative.
91876 **
91877 ** This routine changes the values of iLimit and iOffset only if
91878 ** a limit or offset is defined by pLimit and pOffset.  iLimit and
91879 ** iOffset should have been preset to appropriate default values
91880 ** (usually but not always -1) prior to calling this routine.
91881 ** Only if pLimit!=0 or pOffset!=0 do the limit registers get
91882 ** redefined.  The UNION ALL operator uses this property to force
91883 ** the reuse of the same limit and offset registers across multiple
91884 ** SELECT statements.
91885 */
91886 static void computeLimitRegisters(Parse *pParse, Select *p, int iBreak){
91887   Vdbe *v = 0;
91888   int iLimit = 0;
91889   int iOffset;
91890   int addr1, n;
91891   if( p->iLimit ) return;
91892 
91893   /*
91894   ** "LIMIT -1" always shows all rows.  There is some
91895   ** contraversy about what the correct behavior should be.
91896   ** The current implementation interprets "LIMIT 0" to mean
91897   ** no rows.
91898   */
91899   sqlite3ExprCacheClear(pParse);
91900   assert( p->pOffset==0 || p->pLimit!=0 );
91901   if( p->pLimit ){
91902     p->iLimit = iLimit = ++pParse->nMem;
91903     v = sqlite3GetVdbe(pParse);
91904     if( NEVER(v==0) ) return;  /* VDBE should have already been allocated */
91905     if( sqlite3ExprIsInteger(p->pLimit, &n) ){
91906       sqlite3VdbeAddOp2(v, OP_Integer, n, iLimit);
91907       VdbeComment((v, "LIMIT counter"));
91908       if( n==0 ){
91909         sqlite3VdbeAddOp2(v, OP_Goto, 0, iBreak);
91910       }else{
91911         if( p->nSelectRow > (double)n ) p->nSelectRow = (double)n;
91912       }
91913     }else{
91914       sqlite3ExprCode(pParse, p->pLimit, iLimit);
91915       sqlite3VdbeAddOp1(v, OP_MustBeInt, iLimit);
91916       VdbeComment((v, "LIMIT counter"));
91917       sqlite3VdbeAddOp2(v, OP_IfZero, iLimit, iBreak);
91918     }
91919     if( p->pOffset ){
91920       p->iOffset = iOffset = ++pParse->nMem;
91921       pParse->nMem++;   /* Allocate an extra register for limit+offset */
91922       sqlite3ExprCode(pParse, p->pOffset, iOffset);
91923       sqlite3VdbeAddOp1(v, OP_MustBeInt, iOffset);
91924       VdbeComment((v, "OFFSET counter"));
91925       addr1 = sqlite3VdbeAddOp1(v, OP_IfPos, iOffset);
91926       sqlite3VdbeAddOp2(v, OP_Integer, 0, iOffset);
91927       sqlite3VdbeJumpHere(v, addr1);
91928       sqlite3VdbeAddOp3(v, OP_Add, iLimit, iOffset, iOffset+1);
91929       VdbeComment((v, "LIMIT+OFFSET"));
91930       addr1 = sqlite3VdbeAddOp1(v, OP_IfPos, iLimit);
91931       sqlite3VdbeAddOp2(v, OP_Integer, -1, iOffset+1);
91932       sqlite3VdbeJumpHere(v, addr1);
91933     }
91934   }
91935 }
91936 
91937 #ifndef SQLITE_OMIT_COMPOUND_SELECT
91938 /*
91939 ** Return the appropriate collating sequence for the iCol-th column of
91940 ** the result set for the compound-select statement "p".  Return NULL if
91941 ** the column has no default collating sequence.
91942 **
91943 ** The collating sequence for the compound select is taken from the
91944 ** left-most term of the select that has a collating sequence.
91945 */
91946 static CollSeq *multiSelectCollSeq(Parse *pParse, Select *p, int iCol){
91947   CollSeq *pRet;
91948   if( p->pPrior ){
91949     pRet = multiSelectCollSeq(pParse, p->pPrior, iCol);
91950   }else{
91951     pRet = 0;
91952   }
91953   assert( iCol>=0 );
91954   if( pRet==0 && iCol<p->pEList->nExpr ){
91955     pRet = sqlite3ExprCollSeq(pParse, p->pEList->a[iCol].pExpr);
91956   }
91957   return pRet;
91958 }
91959 #endif /* SQLITE_OMIT_COMPOUND_SELECT */
91960 
91961 /* Forward reference */
91962 static int multiSelectOrderBy(
91963   Parse *pParse,        /* Parsing context */
91964   Select *p,            /* The right-most of SELECTs to be coded */
91965   SelectDest *pDest     /* What to do with query results */
91966 );
91967 
91968 
91969 #ifndef SQLITE_OMIT_COMPOUND_SELECT
91970 /*
91971 ** This routine is called to process a compound query form from
91972 ** two or more separate queries using UNION, UNION ALL, EXCEPT, or
91973 ** INTERSECT
91974 **
91975 ** "p" points to the right-most of the two queries.  the query on the
91976 ** left is p->pPrior.  The left query could also be a compound query
91977 ** in which case this routine will be called recursively.
91978 **
91979 ** The results of the total query are to be written into a destination
91980 ** of type eDest with parameter iParm.
91981 **
91982 ** Example 1:  Consider a three-way compound SQL statement.
91983 **
91984 **     SELECT a FROM t1 UNION SELECT b FROM t2 UNION SELECT c FROM t3
91985 **
91986 ** This statement is parsed up as follows:
91987 **
91988 **     SELECT c FROM t3
91989 **      |
91990 **      `----->  SELECT b FROM t2
91991 **                |
91992 **                `------>  SELECT a FROM t1
91993 **
91994 ** The arrows in the diagram above represent the Select.pPrior pointer.
91995 ** So if this routine is called with p equal to the t3 query, then
91996 ** pPrior will be the t2 query.  p->op will be TK_UNION in this case.
91997 **
91998 ** Notice that because of the way SQLite parses compound SELECTs, the
91999 ** individual selects always group from left to right.
92000 */
92001 static int multiSelect(
92002   Parse *pParse,        /* Parsing context */
92003   Select *p,            /* The right-most of SELECTs to be coded */
92004   SelectDest *pDest     /* What to do with query results */
92005 ){
92006   int rc = SQLITE_OK;   /* Success code from a subroutine */
92007   Select *pPrior;       /* Another SELECT immediately to our left */
92008   Vdbe *v;              /* Generate code to this VDBE */
92009   SelectDest dest;      /* Alternative data destination */
92010   Select *pDelete = 0;  /* Chain of simple selects to delete */
92011   sqlite3 *db;          /* Database connection */
92012 #ifndef SQLITE_OMIT_EXPLAIN
92013   int iSub1;            /* EQP id of left-hand query */
92014   int iSub2;            /* EQP id of right-hand query */
92015 #endif
92016 
92017   /* Make sure there is no ORDER BY or LIMIT clause on prior SELECTs.  Only
92018   ** the last (right-most) SELECT in the series may have an ORDER BY or LIMIT.
92019   */
92020   assert( p && p->pPrior );  /* Calling function guarantees this much */
92021   db = pParse->db;
92022   pPrior = p->pPrior;
92023   assert( pPrior->pRightmost!=pPrior );
92024   assert( pPrior->pRightmost==p->pRightmost );
92025   dest = *pDest;
92026   if( pPrior->pOrderBy ){
92027     sqlite3ErrorMsg(pParse,"ORDER BY clause should come after %s not before",
92028       selectOpName(p->op));
92029     rc = 1;
92030     goto multi_select_end;
92031   }
92032   if( pPrior->pLimit ){
92033     sqlite3ErrorMsg(pParse,"LIMIT clause should come after %s not before",
92034       selectOpName(p->op));
92035     rc = 1;
92036     goto multi_select_end;
92037   }
92038 
92039   v = sqlite3GetVdbe(pParse);
92040   assert( v!=0 );  /* The VDBE already created by calling function */
92041 
92042   /* Create the destination temporary table if necessary
92043   */
92044   if( dest.eDest==SRT_EphemTab ){
92045     assert( p->pEList );
92046     sqlite3VdbeAddOp2(v, OP_OpenEphemeral, dest.iParm, p->pEList->nExpr);
92047     sqlite3VdbeChangeP5(v, BTREE_UNORDERED);
92048     dest.eDest = SRT_Table;
92049   }
92050 
92051   /* Make sure all SELECTs in the statement have the same number of elements
92052   ** in their result sets.
92053   */
92054   assert( p->pEList && pPrior->pEList );
92055   if( p->pEList->nExpr!=pPrior->pEList->nExpr ){
92056     sqlite3ErrorMsg(pParse, "SELECTs to the left and right of %s"
92057       " do not have the same number of result columns", selectOpName(p->op));
92058     rc = 1;
92059     goto multi_select_end;
92060   }
92061 
92062   /* Compound SELECTs that have an ORDER BY clause are handled separately.
92063   */
92064   if( p->pOrderBy ){
92065     return multiSelectOrderBy(pParse, p, pDest);
92066   }
92067 
92068   /* Generate code for the left and right SELECT statements.
92069   */
92070   switch( p->op ){
92071     case TK_ALL: {
92072       int addr = 0;
92073       int nLimit;
92074       assert( !pPrior->pLimit );
92075       pPrior->pLimit = p->pLimit;
92076       pPrior->pOffset = p->pOffset;
92077       explainSetInteger(iSub1, pParse->iNextSelectId);
92078       rc = sqlite3Select(pParse, pPrior, &dest);
92079       p->pLimit = 0;
92080       p->pOffset = 0;
92081       if( rc ){
92082         goto multi_select_end;
92083       }
92084       p->pPrior = 0;
92085       p->iLimit = pPrior->iLimit;
92086       p->iOffset = pPrior->iOffset;
92087       if( p->iLimit ){
92088         addr = sqlite3VdbeAddOp1(v, OP_IfZero, p->iLimit);
92089         VdbeComment((v, "Jump ahead if LIMIT reached"));
92090       }
92091       explainSetInteger(iSub2, pParse->iNextSelectId);
92092       rc = sqlite3Select(pParse, p, &dest);
92093       testcase( rc!=SQLITE_OK );
92094       pDelete = p->pPrior;
92095       p->pPrior = pPrior;
92096       p->nSelectRow += pPrior->nSelectRow;
92097       if( pPrior->pLimit
92098        && sqlite3ExprIsInteger(pPrior->pLimit, &nLimit)
92099        && p->nSelectRow > (double)nLimit
92100       ){
92101         p->nSelectRow = (double)nLimit;
92102       }
92103       if( addr ){
92104         sqlite3VdbeJumpHere(v, addr);
92105       }
92106       break;
92107     }
92108     case TK_EXCEPT:
92109     case TK_UNION: {
92110       int unionTab;    /* Cursor number of the temporary table holding result */
92111       u8 op = 0;       /* One of the SRT_ operations to apply to self */
92112       int priorOp;     /* The SRT_ operation to apply to prior selects */
92113       Expr *pLimit, *pOffset; /* Saved values of p->nLimit and p->nOffset */
92114       int addr;
92115       SelectDest uniondest;
92116 
92117       testcase( p->op==TK_EXCEPT );
92118       testcase( p->op==TK_UNION );
92119       priorOp = SRT_Union;
92120       if( dest.eDest==priorOp && ALWAYS(!p->pLimit &&!p->pOffset) ){
92121         /* We can reuse a temporary table generated by a SELECT to our
92122         ** right.
92123         */
92124         assert( p->pRightmost!=p );  /* Can only happen for leftward elements
92125                                      ** of a 3-way or more compound */
92126         assert( p->pLimit==0 );      /* Not allowed on leftward elements */
92127         assert( p->pOffset==0 );     /* Not allowed on leftward elements */
92128         unionTab = dest.iParm;
92129       }else{
92130         /* We will need to create our own temporary table to hold the
92131         ** intermediate results.
92132         */
92133         unionTab = pParse->nTab++;
92134         assert( p->pOrderBy==0 );
92135         addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, unionTab, 0);
92136         assert( p->addrOpenEphm[0] == -1 );
92137         p->addrOpenEphm[0] = addr;
92138         p->pRightmost->selFlags |= SF_UsesEphemeral;
92139         assert( p->pEList );
92140       }
92141 
92142       /* Code the SELECT statements to our left
92143       */
92144       assert( !pPrior->pOrderBy );
92145       sqlite3SelectDestInit(&uniondest, priorOp, unionTab);
92146       explainSetInteger(iSub1, pParse->iNextSelectId);
92147       rc = sqlite3Select(pParse, pPrior, &uniondest);
92148       if( rc ){
92149         goto multi_select_end;
92150       }
92151 
92152       /* Code the current SELECT statement
92153       */
92154       if( p->op==TK_EXCEPT ){
92155         op = SRT_Except;
92156       }else{
92157         assert( p->op==TK_UNION );
92158         op = SRT_Union;
92159       }
92160       p->pPrior = 0;
92161       pLimit = p->pLimit;
92162       p->pLimit = 0;
92163       pOffset = p->pOffset;
92164       p->pOffset = 0;
92165       uniondest.eDest = op;
92166       explainSetInteger(iSub2, pParse->iNextSelectId);
92167       rc = sqlite3Select(pParse, p, &uniondest);
92168       testcase( rc!=SQLITE_OK );
92169       /* Query flattening in sqlite3Select() might refill p->pOrderBy.
92170       ** Be sure to delete p->pOrderBy, therefore, to avoid a memory leak. */
92171       sqlite3ExprListDelete(db, p->pOrderBy);
92172       pDelete = p->pPrior;
92173       p->pPrior = pPrior;
92174       p->pOrderBy = 0;
92175       if( p->op==TK_UNION ) p->nSelectRow += pPrior->nSelectRow;
92176       sqlite3ExprDelete(db, p->pLimit);
92177       p->pLimit = pLimit;
92178       p->pOffset = pOffset;
92179       p->iLimit = 0;
92180       p->iOffset = 0;
92181 
92182       /* Convert the data in the temporary table into whatever form
92183       ** it is that we currently need.
92184       */
92185       assert( unionTab==dest.iParm || dest.eDest!=priorOp );
92186       if( dest.eDest!=priorOp ){
92187         int iCont, iBreak, iStart;
92188         assert( p->pEList );
92189         if( dest.eDest==SRT_Output ){
92190           Select *pFirst = p;
92191           while( pFirst->pPrior ) pFirst = pFirst->pPrior;
92192           generateColumnNames(pParse, 0, pFirst->pEList);
92193         }
92194         iBreak = sqlite3VdbeMakeLabel(v);
92195         iCont = sqlite3VdbeMakeLabel(v);
92196         computeLimitRegisters(pParse, p, iBreak);
92197         sqlite3VdbeAddOp2(v, OP_Rewind, unionTab, iBreak);
92198         iStart = sqlite3VdbeCurrentAddr(v);
92199         selectInnerLoop(pParse, p, p->pEList, unionTab, p->pEList->nExpr,
92200                         0, -1, &dest, iCont, iBreak);
92201         sqlite3VdbeResolveLabel(v, iCont);
92202         sqlite3VdbeAddOp2(v, OP_Next, unionTab, iStart);
92203         sqlite3VdbeResolveLabel(v, iBreak);
92204         sqlite3VdbeAddOp2(v, OP_Close, unionTab, 0);
92205       }
92206       break;
92207     }
92208     default: assert( p->op==TK_INTERSECT ); {
92209       int tab1, tab2;
92210       int iCont, iBreak, iStart;
92211       Expr *pLimit, *pOffset;
92212       int addr;
92213       SelectDest intersectdest;
92214       int r1;
92215 
92216       /* INTERSECT is different from the others since it requires
92217       ** two temporary tables.  Hence it has its own case.  Begin
92218       ** by allocating the tables we will need.
92219       */
92220       tab1 = pParse->nTab++;
92221       tab2 = pParse->nTab++;
92222       assert( p->pOrderBy==0 );
92223 
92224       addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, tab1, 0);
92225       assert( p->addrOpenEphm[0] == -1 );
92226       p->addrOpenEphm[0] = addr;
92227       p->pRightmost->selFlags |= SF_UsesEphemeral;
92228       assert( p->pEList );
92229 
92230       /* Code the SELECTs to our left into temporary table "tab1".
92231       */
92232       sqlite3SelectDestInit(&intersectdest, SRT_Union, tab1);
92233       explainSetInteger(iSub1, pParse->iNextSelectId);
92234       rc = sqlite3Select(pParse, pPrior, &intersectdest);
92235       if( rc ){
92236         goto multi_select_end;
92237       }
92238 
92239       /* Code the current SELECT into temporary table "tab2"
92240       */
92241       addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, tab2, 0);
92242       assert( p->addrOpenEphm[1] == -1 );
92243       p->addrOpenEphm[1] = addr;
92244       p->pPrior = 0;
92245       pLimit = p->pLimit;
92246       p->pLimit = 0;
92247       pOffset = p->pOffset;
92248       p->pOffset = 0;
92249       intersectdest.iParm = tab2;
92250       explainSetInteger(iSub2, pParse->iNextSelectId);
92251       rc = sqlite3Select(pParse, p, &intersectdest);
92252       testcase( rc!=SQLITE_OK );
92253       pDelete = p->pPrior;
92254       p->pPrior = pPrior;
92255       if( p->nSelectRow>pPrior->nSelectRow ) p->nSelectRow = pPrior->nSelectRow;
92256       sqlite3ExprDelete(db, p->pLimit);
92257       p->pLimit = pLimit;
92258       p->pOffset = pOffset;
92259 
92260       /* Generate code to take the intersection of the two temporary
92261       ** tables.
92262       */
92263       assert( p->pEList );
92264       if( dest.eDest==SRT_Output ){
92265         Select *pFirst = p;
92266         while( pFirst->pPrior ) pFirst = pFirst->pPrior;
92267         generateColumnNames(pParse, 0, pFirst->pEList);
92268       }
92269       iBreak = sqlite3VdbeMakeLabel(v);
92270       iCont = sqlite3VdbeMakeLabel(v);
92271       computeLimitRegisters(pParse, p, iBreak);
92272       sqlite3VdbeAddOp2(v, OP_Rewind, tab1, iBreak);
92273       r1 = sqlite3GetTempReg(pParse);
92274       iStart = sqlite3VdbeAddOp2(v, OP_RowKey, tab1, r1);
92275       sqlite3VdbeAddOp4Int(v, OP_NotFound, tab2, iCont, r1, 0);
92276       sqlite3ReleaseTempReg(pParse, r1);
92277       selectInnerLoop(pParse, p, p->pEList, tab1, p->pEList->nExpr,
92278                       0, -1, &dest, iCont, iBreak);
92279       sqlite3VdbeResolveLabel(v, iCont);
92280       sqlite3VdbeAddOp2(v, OP_Next, tab1, iStart);
92281       sqlite3VdbeResolveLabel(v, iBreak);
92282       sqlite3VdbeAddOp2(v, OP_Close, tab2, 0);
92283       sqlite3VdbeAddOp2(v, OP_Close, tab1, 0);
92284       break;
92285     }
92286   }
92287 
92288   explainComposite(pParse, p->op, iSub1, iSub2, p->op!=TK_ALL);
92289 
92290   /* Compute collating sequences used by
92291   ** temporary tables needed to implement the compound select.
92292   ** Attach the KeyInfo structure to all temporary tables.
92293   **
92294   ** This section is run by the right-most SELECT statement only.
92295   ** SELECT statements to the left always skip this part.  The right-most
92296   ** SELECT might also skip this part if it has no ORDER BY clause and
92297   ** no temp tables are required.
92298   */
92299   if( p->selFlags & SF_UsesEphemeral ){
92300     int i;                        /* Loop counter */
92301     KeyInfo *pKeyInfo;            /* Collating sequence for the result set */
92302     Select *pLoop;                /* For looping through SELECT statements */
92303     CollSeq **apColl;             /* For looping through pKeyInfo->aColl[] */
92304     int nCol;                     /* Number of columns in result set */
92305 
92306     assert( p->pRightmost==p );
92307     nCol = p->pEList->nExpr;
92308     pKeyInfo = sqlite3DbMallocZero(db,
92309                        sizeof(*pKeyInfo)+nCol*(sizeof(CollSeq*) + 1));
92310     if( !pKeyInfo ){
92311       rc = SQLITE_NOMEM;
92312       goto multi_select_end;
92313     }
92314 
92315     pKeyInfo->enc = ENC(db);
92316     pKeyInfo->nField = (u16)nCol;
92317 
92318     for(i=0, apColl=pKeyInfo->aColl; i<nCol; i++, apColl++){
92319       *apColl = multiSelectCollSeq(pParse, p, i);
92320       if( 0==*apColl ){
92321         *apColl = db->pDfltColl;
92322       }
92323     }
92324 
92325     for(pLoop=p; pLoop; pLoop=pLoop->pPrior){
92326       for(i=0; i<2; i++){
92327         int addr = pLoop->addrOpenEphm[i];
92328         if( addr<0 ){
92329           /* If [0] is unused then [1] is also unused.  So we can
92330           ** always safely abort as soon as the first unused slot is found */
92331           assert( pLoop->addrOpenEphm[1]<0 );
92332           break;
92333         }
92334         sqlite3VdbeChangeP2(v, addr, nCol);
92335         sqlite3VdbeChangeP4(v, addr, (char*)pKeyInfo, P4_KEYINFO);
92336         pLoop->addrOpenEphm[i] = -1;
92337       }
92338     }
92339     sqlite3DbFree(db, pKeyInfo);
92340   }
92341 
92342 multi_select_end:
92343   pDest->iMem = dest.iMem;
92344   pDest->nMem = dest.nMem;
92345   sqlite3SelectDelete(db, pDelete);
92346   return rc;
92347 }
92348 #endif /* SQLITE_OMIT_COMPOUND_SELECT */
92349 
92350 /*
92351 ** Code an output subroutine for a coroutine implementation of a
92352 ** SELECT statment.
92353 **
92354 ** The data to be output is contained in pIn->iMem.  There are
92355 ** pIn->nMem columns to be output.  pDest is where the output should
92356 ** be sent.
92357 **
92358 ** regReturn is the number of the register holding the subroutine
92359 ** return address.
92360 **
92361 ** If regPrev>0 then it is the first register in a vector that
92362 ** records the previous output.  mem[regPrev] is a flag that is false
92363 ** if there has been no previous output.  If regPrev>0 then code is
92364 ** generated to suppress duplicates.  pKeyInfo is used for comparing
92365 ** keys.
92366 **
92367 ** If the LIMIT found in p->iLimit is reached, jump immediately to
92368 ** iBreak.
92369 */
92370 static int generateOutputSubroutine(
92371   Parse *pParse,          /* Parsing context */
92372   Select *p,              /* The SELECT statement */
92373   SelectDest *pIn,        /* Coroutine supplying data */
92374   SelectDest *pDest,      /* Where to send the data */
92375   int regReturn,          /* The return address register */
92376   int regPrev,            /* Previous result register.  No uniqueness if 0 */
92377   KeyInfo *pKeyInfo,      /* For comparing with previous entry */
92378   int p4type,             /* The p4 type for pKeyInfo */
92379   int iBreak              /* Jump here if we hit the LIMIT */
92380 ){
92381   Vdbe *v = pParse->pVdbe;
92382   int iContinue;
92383   int addr;
92384 
92385   addr = sqlite3VdbeCurrentAddr(v);
92386   iContinue = sqlite3VdbeMakeLabel(v);
92387 
92388   /* Suppress duplicates for UNION, EXCEPT, and INTERSECT
92389   */
92390   if( regPrev ){
92391     int j1, j2;
92392     j1 = sqlite3VdbeAddOp1(v, OP_IfNot, regPrev);
92393     j2 = sqlite3VdbeAddOp4(v, OP_Compare, pIn->iMem, regPrev+1, pIn->nMem,
92394                               (char*)pKeyInfo, p4type);
92395     sqlite3VdbeAddOp3(v, OP_Jump, j2+2, iContinue, j2+2);
92396     sqlite3VdbeJumpHere(v, j1);
92397     sqlite3ExprCodeCopy(pParse, pIn->iMem, regPrev+1, pIn->nMem);
92398     sqlite3VdbeAddOp2(v, OP_Integer, 1, regPrev);
92399   }
92400   if( pParse->db->mallocFailed ) return 0;
92401 
92402   /* Suppress the the first OFFSET entries if there is an OFFSET clause
92403   */
92404   codeOffset(v, p, iContinue);
92405 
92406   switch( pDest->eDest ){
92407     /* Store the result as data using a unique key.
92408     */
92409     case SRT_Table:
92410     case SRT_EphemTab: {
92411       int r1 = sqlite3GetTempReg(pParse);
92412       int r2 = sqlite3GetTempReg(pParse);
92413       testcase( pDest->eDest==SRT_Table );
92414       testcase( pDest->eDest==SRT_EphemTab );
92415       sqlite3VdbeAddOp3(v, OP_MakeRecord, pIn->iMem, pIn->nMem, r1);
92416       sqlite3VdbeAddOp2(v, OP_NewRowid, pDest->iParm, r2);
92417       sqlite3VdbeAddOp3(v, OP_Insert, pDest->iParm, r1, r2);
92418       sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
92419       sqlite3ReleaseTempReg(pParse, r2);
92420       sqlite3ReleaseTempReg(pParse, r1);
92421       break;
92422     }
92423 
92424 #ifndef SQLITE_OMIT_SUBQUERY
92425     /* If we are creating a set for an "expr IN (SELECT ...)" construct,
92426     ** then there should be a single item on the stack.  Write this
92427     ** item into the set table with bogus data.
92428     */
92429     case SRT_Set: {
92430       int r1;
92431       assert( pIn->nMem==1 );
92432       p->affinity =
92433          sqlite3CompareAffinity(p->pEList->a[0].pExpr, pDest->affinity);
92434       r1 = sqlite3GetTempReg(pParse);
92435       sqlite3VdbeAddOp4(v, OP_MakeRecord, pIn->iMem, 1, r1, &p->affinity, 1);
92436       sqlite3ExprCacheAffinityChange(pParse, pIn->iMem, 1);
92437       sqlite3VdbeAddOp2(v, OP_IdxInsert, pDest->iParm, r1);
92438       sqlite3ReleaseTempReg(pParse, r1);
92439       break;
92440     }
92441 
92442 #if 0  /* Never occurs on an ORDER BY query */
92443     /* If any row exist in the result set, record that fact and abort.
92444     */
92445     case SRT_Exists: {
92446       sqlite3VdbeAddOp2(v, OP_Integer, 1, pDest->iParm);
92447       /* The LIMIT clause will terminate the loop for us */
92448       break;
92449     }
92450 #endif
92451 
92452     /* If this is a scalar select that is part of an expression, then
92453     ** store the results in the appropriate memory cell and break out
92454     ** of the scan loop.
92455     */
92456     case SRT_Mem: {
92457       assert( pIn->nMem==1 );
92458       sqlite3ExprCodeMove(pParse, pIn->iMem, pDest->iParm, 1);
92459       /* The LIMIT clause will jump out of the loop for us */
92460       break;
92461     }
92462 #endif /* #ifndef SQLITE_OMIT_SUBQUERY */
92463 
92464     /* The results are stored in a sequence of registers
92465     ** starting at pDest->iMem.  Then the co-routine yields.
92466     */
92467     case SRT_Coroutine: {
92468       if( pDest->iMem==0 ){
92469         pDest->iMem = sqlite3GetTempRange(pParse, pIn->nMem);
92470         pDest->nMem = pIn->nMem;
92471       }
92472       sqlite3ExprCodeMove(pParse, pIn->iMem, pDest->iMem, pDest->nMem);
92473       sqlite3VdbeAddOp1(v, OP_Yield, pDest->iParm);
92474       break;
92475     }
92476 
92477     /* If none of the above, then the result destination must be
92478     ** SRT_Output.  This routine is never called with any other
92479     ** destination other than the ones handled above or SRT_Output.
92480     **
92481     ** For SRT_Output, results are stored in a sequence of registers.
92482     ** Then the OP_ResultRow opcode is used to cause sqlite3_step() to
92483     ** return the next row of result.
92484     */
92485     default: {
92486       assert( pDest->eDest==SRT_Output );
92487       sqlite3VdbeAddOp2(v, OP_ResultRow, pIn->iMem, pIn->nMem);
92488       sqlite3ExprCacheAffinityChange(pParse, pIn->iMem, pIn->nMem);
92489       break;
92490     }
92491   }
92492 
92493   /* Jump to the end of the loop if the LIMIT is reached.
92494   */
92495   if( p->iLimit ){
92496     sqlite3VdbeAddOp3(v, OP_IfZero, p->iLimit, iBreak, -1);
92497   }
92498 
92499   /* Generate the subroutine return
92500   */
92501   sqlite3VdbeResolveLabel(v, iContinue);
92502   sqlite3VdbeAddOp1(v, OP_Return, regReturn);
92503 
92504   return addr;
92505 }
92506 
92507 /*
92508 ** Alternative compound select code generator for cases when there
92509 ** is an ORDER BY clause.
92510 **
92511 ** We assume a query of the following form:
92512 **
92513 **      <selectA>  <operator>  <selectB>  ORDER BY <orderbylist>
92514 **
92515 ** <operator> is one of UNION ALL, UNION, EXCEPT, or INTERSECT.  The idea
92516 ** is to code both <selectA> and <selectB> with the ORDER BY clause as
92517 ** co-routines.  Then run the co-routines in parallel and merge the results
92518 ** into the output.  In addition to the two coroutines (called selectA and
92519 ** selectB) there are 7 subroutines:
92520 **
92521 **    outA:    Move the output of the selectA coroutine into the output
92522 **             of the compound query.
92523 **
92524 **    outB:    Move the output of the selectB coroutine into the output
92525 **             of the compound query.  (Only generated for UNION and
92526 **             UNION ALL.  EXCEPT and INSERTSECT never output a row that
92527 **             appears only in B.)
92528 **
92529 **    AltB:    Called when there is data from both coroutines and A<B.
92530 **
92531 **    AeqB:    Called when there is data from both coroutines and A==B.
92532 **
92533 **    AgtB:    Called when there is data from both coroutines and A>B.
92534 **
92535 **    EofA:    Called when data is exhausted from selectA.
92536 **
92537 **    EofB:    Called when data is exhausted from selectB.
92538 **
92539 ** The implementation of the latter five subroutines depend on which
92540 ** <operator> is used:
92541 **
92542 **
92543 **             UNION ALL         UNION            EXCEPT          INTERSECT
92544 **          -------------  -----------------  --------------  -----------------
92545 **   AltB:   outA, nextA      outA, nextA       outA, nextA         nextA
92546 **
92547 **   AeqB:   outA, nextA         nextA             nextA         outA, nextA
92548 **
92549 **   AgtB:   outB, nextB      outB, nextB          nextB            nextB
92550 **
92551 **   EofA:   outB, nextB      outB, nextB          halt             halt
92552 **
92553 **   EofB:   outA, nextA      outA, nextA       outA, nextA         halt
92554 **
92555 ** In the AltB, AeqB, and AgtB subroutines, an EOF on A following nextA
92556 ** causes an immediate jump to EofA and an EOF on B following nextB causes
92557 ** an immediate jump to EofB.  Within EofA and EofB, and EOF on entry or
92558 ** following nextX causes a jump to the end of the select processing.
92559 **
92560 ** Duplicate removal in the UNION, EXCEPT, and INTERSECT cases is handled
92561 ** within the output subroutine.  The regPrev register set holds the previously
92562 ** output value.  A comparison is made against this value and the output
92563 ** is skipped if the next results would be the same as the previous.
92564 **
92565 ** The implementation plan is to implement the two coroutines and seven
92566 ** subroutines first, then put the control logic at the bottom.  Like this:
92567 **
92568 **          goto Init
92569 **     coA: coroutine for left query (A)
92570 **     coB: coroutine for right query (B)
92571 **    outA: output one row of A
92572 **    outB: output one row of B (UNION and UNION ALL only)
92573 **    EofA: ...
92574 **    EofB: ...
92575 **    AltB: ...
92576 **    AeqB: ...
92577 **    AgtB: ...
92578 **    Init: initialize coroutine registers
92579 **          yield coA
92580 **          if eof(A) goto EofA
92581 **          yield coB
92582 **          if eof(B) goto EofB
92583 **    Cmpr: Compare A, B
92584 **          Jump AltB, AeqB, AgtB
92585 **     End: ...
92586 **
92587 ** We call AltB, AeqB, AgtB, EofA, and EofB "subroutines" but they are not
92588 ** actually called using Gosub and they do not Return.  EofA and EofB loop
92589 ** until all data is exhausted then jump to the "end" labe.  AltB, AeqB,
92590 ** and AgtB jump to either L2 or to one of EofA or EofB.
92591 */
92592 #ifndef SQLITE_OMIT_COMPOUND_SELECT
92593 static int multiSelectOrderBy(
92594   Parse *pParse,        /* Parsing context */
92595   Select *p,            /* The right-most of SELECTs to be coded */
92596   SelectDest *pDest     /* What to do with query results */
92597 ){
92598   int i, j;             /* Loop counters */
92599   Select *pPrior;       /* Another SELECT immediately to our left */
92600   Vdbe *v;              /* Generate code to this VDBE */
92601   SelectDest destA;     /* Destination for coroutine A */
92602   SelectDest destB;     /* Destination for coroutine B */
92603   int regAddrA;         /* Address register for select-A coroutine */
92604   int regEofA;          /* Flag to indicate when select-A is complete */
92605   int regAddrB;         /* Address register for select-B coroutine */
92606   int regEofB;          /* Flag to indicate when select-B is complete */
92607   int addrSelectA;      /* Address of the select-A coroutine */
92608   int addrSelectB;      /* Address of the select-B coroutine */
92609   int regOutA;          /* Address register for the output-A subroutine */
92610   int regOutB;          /* Address register for the output-B subroutine */
92611   int addrOutA;         /* Address of the output-A subroutine */
92612   int addrOutB = 0;     /* Address of the output-B subroutine */
92613   int addrEofA;         /* Address of the select-A-exhausted subroutine */
92614   int addrEofB;         /* Address of the select-B-exhausted subroutine */
92615   int addrAltB;         /* Address of the A<B subroutine */
92616   int addrAeqB;         /* Address of the A==B subroutine */
92617   int addrAgtB;         /* Address of the A>B subroutine */
92618   int regLimitA;        /* Limit register for select-A */
92619   int regLimitB;        /* Limit register for select-A */
92620   int regPrev;          /* A range of registers to hold previous output */
92621   int savedLimit;       /* Saved value of p->iLimit */
92622   int savedOffset;      /* Saved value of p->iOffset */
92623   int labelCmpr;        /* Label for the start of the merge algorithm */
92624   int labelEnd;         /* Label for the end of the overall SELECT stmt */
92625   int j1;               /* Jump instructions that get retargetted */
92626   int op;               /* One of TK_ALL, TK_UNION, TK_EXCEPT, TK_INTERSECT */
92627   KeyInfo *pKeyDup = 0; /* Comparison information for duplicate removal */
92628   KeyInfo *pKeyMerge;   /* Comparison information for merging rows */
92629   sqlite3 *db;          /* Database connection */
92630   ExprList *pOrderBy;   /* The ORDER BY clause */
92631   int nOrderBy;         /* Number of terms in the ORDER BY clause */
92632   int *aPermute;        /* Mapping from ORDER BY terms to result set columns */
92633 #ifndef SQLITE_OMIT_EXPLAIN
92634   int iSub1;            /* EQP id of left-hand query */
92635   int iSub2;            /* EQP id of right-hand query */
92636 #endif
92637 
92638   assert( p->pOrderBy!=0 );
92639   assert( pKeyDup==0 ); /* "Managed" code needs this.  Ticket #3382. */
92640   db = pParse->db;
92641   v = pParse->pVdbe;
92642   assert( v!=0 );       /* Already thrown the error if VDBE alloc failed */
92643   labelEnd = sqlite3VdbeMakeLabel(v);
92644   labelCmpr = sqlite3VdbeMakeLabel(v);
92645 
92646 
92647   /* Patch up the ORDER BY clause
92648   */
92649   op = p->op;
92650   pPrior = p->pPrior;
92651   assert( pPrior->pOrderBy==0 );
92652   pOrderBy = p->pOrderBy;
92653   assert( pOrderBy );
92654   nOrderBy = pOrderBy->nExpr;
92655 
92656   /* For operators other than UNION ALL we have to make sure that
92657   ** the ORDER BY clause covers every term of the result set.  Add
92658   ** terms to the ORDER BY clause as necessary.
92659   */
92660   if( op!=TK_ALL ){
92661     for(i=1; db->mallocFailed==0 && i<=p->pEList->nExpr; i++){
92662       struct ExprList_item *pItem;
92663       for(j=0, pItem=pOrderBy->a; j<nOrderBy; j++, pItem++){
92664         assert( pItem->iCol>0 );
92665         if( pItem->iCol==i ) break;
92666       }
92667       if( j==nOrderBy ){
92668         Expr *pNew = sqlite3Expr(db, TK_INTEGER, 0);
92669         if( pNew==0 ) return SQLITE_NOMEM;
92670         pNew->flags |= EP_IntValue;
92671         pNew->u.iValue = i;
92672         pOrderBy = sqlite3ExprListAppend(pParse, pOrderBy, pNew);
92673         pOrderBy->a[nOrderBy++].iCol = (u16)i;
92674       }
92675     }
92676   }
92677 
92678   /* Compute the comparison permutation and keyinfo that is used with
92679   ** the permutation used to determine if the next
92680   ** row of results comes from selectA or selectB.  Also add explicit
92681   ** collations to the ORDER BY clause terms so that when the subqueries
92682   ** to the right and the left are evaluated, they use the correct
92683   ** collation.
92684   */
92685   aPermute = sqlite3DbMallocRaw(db, sizeof(int)*nOrderBy);
92686   if( aPermute ){
92687     struct ExprList_item *pItem;
92688     for(i=0, pItem=pOrderBy->a; i<nOrderBy; i++, pItem++){
92689       assert( pItem->iCol>0  && pItem->iCol<=p->pEList->nExpr );
92690       aPermute[i] = pItem->iCol - 1;
92691     }
92692     pKeyMerge =
92693       sqlite3DbMallocRaw(db, sizeof(*pKeyMerge)+nOrderBy*(sizeof(CollSeq*)+1));
92694     if( pKeyMerge ){
92695       pKeyMerge->aSortOrder = (u8*)&pKeyMerge->aColl[nOrderBy];
92696       pKeyMerge->nField = (u16)nOrderBy;
92697       pKeyMerge->enc = ENC(db);
92698       for(i=0; i<nOrderBy; i++){
92699         CollSeq *pColl;
92700         Expr *pTerm = pOrderBy->a[i].pExpr;
92701         if( pTerm->flags & EP_ExpCollate ){
92702           pColl = pTerm->pColl;
92703         }else{
92704           pColl = multiSelectCollSeq(pParse, p, aPermute[i]);
92705           pTerm->flags |= EP_ExpCollate;
92706           pTerm->pColl = pColl;
92707         }
92708         pKeyMerge->aColl[i] = pColl;
92709         pKeyMerge->aSortOrder[i] = pOrderBy->a[i].sortOrder;
92710       }
92711     }
92712   }else{
92713     pKeyMerge = 0;
92714   }
92715 
92716   /* Reattach the ORDER BY clause to the query.
92717   */
92718   p->pOrderBy = pOrderBy;
92719   pPrior->pOrderBy = sqlite3ExprListDup(pParse->db, pOrderBy, 0);
92720 
92721   /* Allocate a range of temporary registers and the KeyInfo needed
92722   ** for the logic that removes duplicate result rows when the
92723   ** operator is UNION, EXCEPT, or INTERSECT (but not UNION ALL).
92724   */
92725   if( op==TK_ALL ){
92726     regPrev = 0;
92727   }else{
92728     int nExpr = p->pEList->nExpr;
92729     assert( nOrderBy>=nExpr || db->mallocFailed );
92730     regPrev = sqlite3GetTempRange(pParse, nExpr+1);
92731     sqlite3VdbeAddOp2(v, OP_Integer, 0, regPrev);
92732     pKeyDup = sqlite3DbMallocZero(db,
92733                   sizeof(*pKeyDup) + nExpr*(sizeof(CollSeq*)+1) );
92734     if( pKeyDup ){
92735       pKeyDup->aSortOrder = (u8*)&pKeyDup->aColl[nExpr];
92736       pKeyDup->nField = (u16)nExpr;
92737       pKeyDup->enc = ENC(db);
92738       for(i=0; i<nExpr; i++){
92739         pKeyDup->aColl[i] = multiSelectCollSeq(pParse, p, i);
92740         pKeyDup->aSortOrder[i] = 0;
92741       }
92742     }
92743   }
92744 
92745   /* Separate the left and the right query from one another
92746   */
92747   p->pPrior = 0;
92748   sqlite3ResolveOrderGroupBy(pParse, p, p->pOrderBy, "ORDER");
92749   if( pPrior->pPrior==0 ){
92750     sqlite3ResolveOrderGroupBy(pParse, pPrior, pPrior->pOrderBy, "ORDER");
92751   }
92752 
92753   /* Compute the limit registers */
92754   computeLimitRegisters(pParse, p, labelEnd);
92755   if( p->iLimit && op==TK_ALL ){
92756     regLimitA = ++pParse->nMem;
92757     regLimitB = ++pParse->nMem;
92758     sqlite3VdbeAddOp2(v, OP_Copy, p->iOffset ? p->iOffset+1 : p->iLimit,
92759                                   regLimitA);
92760     sqlite3VdbeAddOp2(v, OP_Copy, regLimitA, regLimitB);
92761   }else{
92762     regLimitA = regLimitB = 0;
92763   }
92764   sqlite3ExprDelete(db, p->pLimit);
92765   p->pLimit = 0;
92766   sqlite3ExprDelete(db, p->pOffset);
92767   p->pOffset = 0;
92768 
92769   regAddrA = ++pParse->nMem;
92770   regEofA = ++pParse->nMem;
92771   regAddrB = ++pParse->nMem;
92772   regEofB = ++pParse->nMem;
92773   regOutA = ++pParse->nMem;
92774   regOutB = ++pParse->nMem;
92775   sqlite3SelectDestInit(&destA, SRT_Coroutine, regAddrA);
92776   sqlite3SelectDestInit(&destB, SRT_Coroutine, regAddrB);
92777 
92778   /* Jump past the various subroutines and coroutines to the main
92779   ** merge loop
92780   */
92781   j1 = sqlite3VdbeAddOp0(v, OP_Goto);
92782   addrSelectA = sqlite3VdbeCurrentAddr(v);
92783 
92784 
92785   /* Generate a coroutine to evaluate the SELECT statement to the
92786   ** left of the compound operator - the "A" select.
92787   */
92788   VdbeNoopComment((v, "Begin coroutine for left SELECT"));
92789   pPrior->iLimit = regLimitA;
92790   explainSetInteger(iSub1, pParse->iNextSelectId);
92791   sqlite3Select(pParse, pPrior, &destA);
92792   sqlite3VdbeAddOp2(v, OP_Integer, 1, regEofA);
92793   sqlite3VdbeAddOp1(v, OP_Yield, regAddrA);
92794   VdbeNoopComment((v, "End coroutine for left SELECT"));
92795 
92796   /* Generate a coroutine to evaluate the SELECT statement on
92797   ** the right - the "B" select
92798   */
92799   addrSelectB = sqlite3VdbeCurrentAddr(v);
92800   VdbeNoopComment((v, "Begin coroutine for right SELECT"));
92801   savedLimit = p->iLimit;
92802   savedOffset = p->iOffset;
92803   p->iLimit = regLimitB;
92804   p->iOffset = 0;
92805   explainSetInteger(iSub2, pParse->iNextSelectId);
92806   sqlite3Select(pParse, p, &destB);
92807   p->iLimit = savedLimit;
92808   p->iOffset = savedOffset;
92809   sqlite3VdbeAddOp2(v, OP_Integer, 1, regEofB);
92810   sqlite3VdbeAddOp1(v, OP_Yield, regAddrB);
92811   VdbeNoopComment((v, "End coroutine for right SELECT"));
92812 
92813   /* Generate a subroutine that outputs the current row of the A
92814   ** select as the next output row of the compound select.
92815   */
92816   VdbeNoopComment((v, "Output routine for A"));
92817   addrOutA = generateOutputSubroutine(pParse,
92818                  p, &destA, pDest, regOutA,
92819                  regPrev, pKeyDup, P4_KEYINFO_HANDOFF, labelEnd);
92820 
92821   /* Generate a subroutine that outputs the current row of the B
92822   ** select as the next output row of the compound select.
92823   */
92824   if( op==TK_ALL || op==TK_UNION ){
92825     VdbeNoopComment((v, "Output routine for B"));
92826     addrOutB = generateOutputSubroutine(pParse,
92827                  p, &destB, pDest, regOutB,
92828                  regPrev, pKeyDup, P4_KEYINFO_STATIC, labelEnd);
92829   }
92830 
92831   /* Generate a subroutine to run when the results from select A
92832   ** are exhausted and only data in select B remains.
92833   */
92834   VdbeNoopComment((v, "eof-A subroutine"));
92835   if( op==TK_EXCEPT || op==TK_INTERSECT ){
92836     addrEofA = sqlite3VdbeAddOp2(v, OP_Goto, 0, labelEnd);
92837   }else{
92838     addrEofA = sqlite3VdbeAddOp2(v, OP_If, regEofB, labelEnd);
92839     sqlite3VdbeAddOp2(v, OP_Gosub, regOutB, addrOutB);
92840     sqlite3VdbeAddOp1(v, OP_Yield, regAddrB);
92841     sqlite3VdbeAddOp2(v, OP_Goto, 0, addrEofA);
92842     p->nSelectRow += pPrior->nSelectRow;
92843   }
92844 
92845   /* Generate a subroutine to run when the results from select B
92846   ** are exhausted and only data in select A remains.
92847   */
92848   if( op==TK_INTERSECT ){
92849     addrEofB = addrEofA;
92850     if( p->nSelectRow > pPrior->nSelectRow ) p->nSelectRow = pPrior->nSelectRow;
92851   }else{
92852     VdbeNoopComment((v, "eof-B subroutine"));
92853     addrEofB = sqlite3VdbeAddOp2(v, OP_If, regEofA, labelEnd);
92854     sqlite3VdbeAddOp2(v, OP_Gosub, regOutA, addrOutA);
92855     sqlite3VdbeAddOp1(v, OP_Yield, regAddrA);
92856     sqlite3VdbeAddOp2(v, OP_Goto, 0, addrEofB);
92857   }
92858 
92859   /* Generate code to handle the case of A<B
92860   */
92861   VdbeNoopComment((v, "A-lt-B subroutine"));
92862   addrAltB = sqlite3VdbeAddOp2(v, OP_Gosub, regOutA, addrOutA);
92863   sqlite3VdbeAddOp1(v, OP_Yield, regAddrA);
92864   sqlite3VdbeAddOp2(v, OP_If, regEofA, addrEofA);
92865   sqlite3VdbeAddOp2(v, OP_Goto, 0, labelCmpr);
92866 
92867   /* Generate code to handle the case of A==B
92868   */
92869   if( op==TK_ALL ){
92870     addrAeqB = addrAltB;
92871   }else if( op==TK_INTERSECT ){
92872     addrAeqB = addrAltB;
92873     addrAltB++;
92874   }else{
92875     VdbeNoopComment((v, "A-eq-B subroutine"));
92876     addrAeqB =
92877     sqlite3VdbeAddOp1(v, OP_Yield, regAddrA);
92878     sqlite3VdbeAddOp2(v, OP_If, regEofA, addrEofA);
92879     sqlite3VdbeAddOp2(v, OP_Goto, 0, labelCmpr);
92880   }
92881 
92882   /* Generate code to handle the case of A>B
92883   */
92884   VdbeNoopComment((v, "A-gt-B subroutine"));
92885   addrAgtB = sqlite3VdbeCurrentAddr(v);
92886   if( op==TK_ALL || op==TK_UNION ){
92887     sqlite3VdbeAddOp2(v, OP_Gosub, regOutB, addrOutB);
92888   }
92889   sqlite3VdbeAddOp1(v, OP_Yield, regAddrB);
92890   sqlite3VdbeAddOp2(v, OP_If, regEofB, addrEofB);
92891   sqlite3VdbeAddOp2(v, OP_Goto, 0, labelCmpr);
92892 
92893   /* This code runs once to initialize everything.
92894   */
92895   sqlite3VdbeJumpHere(v, j1);
92896   sqlite3VdbeAddOp2(v, OP_Integer, 0, regEofA);
92897   sqlite3VdbeAddOp2(v, OP_Integer, 0, regEofB);
92898   sqlite3VdbeAddOp2(v, OP_Gosub, regAddrA, addrSelectA);
92899   sqlite3VdbeAddOp2(v, OP_Gosub, regAddrB, addrSelectB);
92900   sqlite3VdbeAddOp2(v, OP_If, regEofA, addrEofA);
92901   sqlite3VdbeAddOp2(v, OP_If, regEofB, addrEofB);
92902 
92903   /* Implement the main merge loop
92904   */
92905   sqlite3VdbeResolveLabel(v, labelCmpr);
92906   sqlite3VdbeAddOp4(v, OP_Permutation, 0, 0, 0, (char*)aPermute, P4_INTARRAY);
92907   sqlite3VdbeAddOp4(v, OP_Compare, destA.iMem, destB.iMem, nOrderBy,
92908                          (char*)pKeyMerge, P4_KEYINFO_HANDOFF);
92909   sqlite3VdbeAddOp3(v, OP_Jump, addrAltB, addrAeqB, addrAgtB);
92910 
92911   /* Release temporary registers
92912   */
92913   if( regPrev ){
92914     sqlite3ReleaseTempRange(pParse, regPrev, nOrderBy+1);
92915   }
92916 
92917   /* Jump to the this point in order to terminate the query.
92918   */
92919   sqlite3VdbeResolveLabel(v, labelEnd);
92920 
92921   /* Set the number of output columns
92922   */
92923   if( pDest->eDest==SRT_Output ){
92924     Select *pFirst = pPrior;
92925     while( pFirst->pPrior ) pFirst = pFirst->pPrior;
92926     generateColumnNames(pParse, 0, pFirst->pEList);
92927   }
92928 
92929   /* Reassembly the compound query so that it will be freed correctly
92930   ** by the calling function */
92931   if( p->pPrior ){
92932     sqlite3SelectDelete(db, p->pPrior);
92933   }
92934   p->pPrior = pPrior;
92935 
92936   /*** TBD:  Insert subroutine calls to close cursors on incomplete
92937   **** subqueries ****/
92938   explainComposite(pParse, p->op, iSub1, iSub2, 0);
92939   return SQLITE_OK;
92940 }
92941 #endif
92942 
92943 #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
92944 /* Forward Declarations */
92945 static void substExprList(sqlite3*, ExprList*, int, ExprList*);
92946 static void substSelect(sqlite3*, Select *, int, ExprList *);
92947 
92948 /*
92949 ** Scan through the expression pExpr.  Replace every reference to
92950 ** a column in table number iTable with a copy of the iColumn-th
92951 ** entry in pEList.  (But leave references to the ROWID column
92952 ** unchanged.)
92953 **
92954 ** This routine is part of the flattening procedure.  A subquery
92955 ** whose result set is defined by pEList appears as entry in the
92956 ** FROM clause of a SELECT such that the VDBE cursor assigned to that
92957 ** FORM clause entry is iTable.  This routine make the necessary
92958 ** changes to pExpr so that it refers directly to the source table
92959 ** of the subquery rather the result set of the subquery.
92960 */
92961 static Expr *substExpr(
92962   sqlite3 *db,        /* Report malloc errors to this connection */
92963   Expr *pExpr,        /* Expr in which substitution occurs */
92964   int iTable,         /* Table to be substituted */
92965   ExprList *pEList    /* Substitute expressions */
92966 ){
92967   if( pExpr==0 ) return 0;
92968   if( pExpr->op==TK_COLUMN && pExpr->iTable==iTable ){
92969     if( pExpr->iColumn<0 ){
92970       pExpr->op = TK_NULL;
92971     }else{
92972       Expr *pNew;
92973       assert( pEList!=0 && pExpr->iColumn<pEList->nExpr );
92974       assert( pExpr->pLeft==0 && pExpr->pRight==0 );
92975       pNew = sqlite3ExprDup(db, pEList->a[pExpr->iColumn].pExpr, 0);
92976       if( pNew && pExpr->pColl ){
92977         pNew->pColl = pExpr->pColl;
92978       }
92979       sqlite3ExprDelete(db, pExpr);
92980       pExpr = pNew;
92981     }
92982   }else{
92983     pExpr->pLeft = substExpr(db, pExpr->pLeft, iTable, pEList);
92984     pExpr->pRight = substExpr(db, pExpr->pRight, iTable, pEList);
92985     if( ExprHasProperty(pExpr, EP_xIsSelect) ){
92986       substSelect(db, pExpr->x.pSelect, iTable, pEList);
92987     }else{
92988       substExprList(db, pExpr->x.pList, iTable, pEList);
92989     }
92990   }
92991   return pExpr;
92992 }
92993 static void substExprList(
92994   sqlite3 *db,         /* Report malloc errors here */
92995   ExprList *pList,     /* List to scan and in which to make substitutes */
92996   int iTable,          /* Table to be substituted */
92997   ExprList *pEList     /* Substitute values */
92998 ){
92999   int i;
93000   if( pList==0 ) return;
93001   for(i=0; i<pList->nExpr; i++){
93002     pList->a[i].pExpr = substExpr(db, pList->a[i].pExpr, iTable, pEList);
93003   }
93004 }
93005 static void substSelect(
93006   sqlite3 *db,         /* Report malloc errors here */
93007   Select *p,           /* SELECT statement in which to make substitutions */
93008   int iTable,          /* Table to be replaced */
93009   ExprList *pEList     /* Substitute values */
93010 ){
93011   SrcList *pSrc;
93012   struct SrcList_item *pItem;
93013   int i;
93014   if( !p ) return;
93015   substExprList(db, p->pEList, iTable, pEList);
93016   substExprList(db, p->pGroupBy, iTable, pEList);
93017   substExprList(db, p->pOrderBy, iTable, pEList);
93018   p->pHaving = substExpr(db, p->pHaving, iTable, pEList);
93019   p->pWhere = substExpr(db, p->pWhere, iTable, pEList);
93020   substSelect(db, p->pPrior, iTable, pEList);
93021   pSrc = p->pSrc;
93022   assert( pSrc );  /* Even for (SELECT 1) we have: pSrc!=0 but pSrc->nSrc==0 */
93023   if( ALWAYS(pSrc) ){
93024     for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
93025       substSelect(db, pItem->pSelect, iTable, pEList);
93026     }
93027   }
93028 }
93029 #endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
93030 
93031 #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
93032 /*
93033 ** This routine attempts to flatten subqueries in order to speed
93034 ** execution.  It returns 1 if it makes changes and 0 if no flattening
93035 ** occurs.
93036 **
93037 ** To understand the concept of flattening, consider the following
93038 ** query:
93039 **
93040 **     SELECT a FROM (SELECT x+y AS a FROM t1 WHERE z<100) WHERE a>5
93041 **
93042 ** The default way of implementing this query is to execute the
93043 ** subquery first and store the results in a temporary table, then
93044 ** run the outer query on that temporary table.  This requires two
93045 ** passes over the data.  Furthermore, because the temporary table
93046 ** has no indices, the WHERE clause on the outer query cannot be
93047 ** optimized.
93048 **
93049 ** This routine attempts to rewrite queries such as the above into
93050 ** a single flat select, like this:
93051 **
93052 **     SELECT x+y AS a FROM t1 WHERE z<100 AND a>5
93053 **
93054 ** The code generated for this simpification gives the same result
93055 ** but only has to scan the data once.  And because indices might
93056 ** exist on the table t1, a complete scan of the data might be
93057 ** avoided.
93058 **
93059 ** Flattening is only attempted if all of the following are true:
93060 **
93061 **   (1)  The subquery and the outer query do not both use aggregates.
93062 **
93063 **   (2)  The subquery is not an aggregate or the outer query is not a join.
93064 **
93065 **   (3)  The subquery is not the right operand of a left outer join
93066 **        (Originally ticket #306.  Strengthened by ticket #3300)
93067 **
93068 **   (4)  The subquery is not DISTINCT.
93069 **
93070 **  (**)  At one point restrictions (4) and (5) defined a subset of DISTINCT
93071 **        sub-queries that were excluded from this optimization. Restriction
93072 **        (4) has since been expanded to exclude all DISTINCT subqueries.
93073 **
93074 **   (6)  The subquery does not use aggregates or the outer query is not
93075 **        DISTINCT.
93076 **
93077 **   (7)  The subquery has a FROM clause.
93078 **
93079 **   (8)  The subquery does not use LIMIT or the outer query is not a join.
93080 **
93081 **   (9)  The subquery does not use LIMIT or the outer query does not use
93082 **        aggregates.
93083 **
93084 **  (10)  The subquery does not use aggregates or the outer query does not
93085 **        use LIMIT.
93086 **
93087 **  (11)  The subquery and the outer query do not both have ORDER BY clauses.
93088 **
93089 **  (**)  Not implemented.  Subsumed into restriction (3).  Was previously
93090 **        a separate restriction deriving from ticket #350.
93091 **
93092 **  (13)  The subquery and outer query do not both use LIMIT.
93093 **
93094 **  (14)  The subquery does not use OFFSET.
93095 **
93096 **  (15)  The outer query is not part of a compound select or the
93097 **        subquery does not have a LIMIT clause.
93098 **        (See ticket #2339 and ticket [02a8e81d44]).
93099 **
93100 **  (16)  The outer query is not an aggregate or the subquery does
93101 **        not contain ORDER BY.  (Ticket #2942)  This used to not matter
93102 **        until we introduced the group_concat() function.
93103 **
93104 **  (17)  The sub-query is not a compound select, or it is a UNION ALL
93105 **        compound clause made up entirely of non-aggregate queries, and
93106 **        the parent query:
93107 **
93108 **          * is not itself part of a compound select,
93109 **          * is not an aggregate or DISTINCT query, and
93110 **          * has no other tables or sub-selects in the FROM clause.
93111 **
93112 **        The parent and sub-query may contain WHERE clauses. Subject to
93113 **        rules (11), (13) and (14), they may also contain ORDER BY,
93114 **        LIMIT and OFFSET clauses.
93115 **
93116 **  (18)  If the sub-query is a compound select, then all terms of the
93117 **        ORDER by clause of the parent must be simple references to
93118 **        columns of the sub-query.
93119 **
93120 **  (19)  The subquery does not use LIMIT or the outer query does not
93121 **        have a WHERE clause.
93122 **
93123 **  (20)  If the sub-query is a compound select, then it must not use
93124 **        an ORDER BY clause.  Ticket #3773.  We could relax this constraint
93125 **        somewhat by saying that the terms of the ORDER BY clause must
93126 **        appear as unmodified result columns in the outer query.  But
93127 **        have other optimizations in mind to deal with that case.
93128 **
93129 **  (21)  The subquery does not use LIMIT or the outer query is not
93130 **        DISTINCT.  (See ticket [752e1646fc]).
93131 **
93132 ** In this routine, the "p" parameter is a pointer to the outer query.
93133 ** The subquery is p->pSrc->a[iFrom].  isAgg is true if the outer query
93134 ** uses aggregates and subqueryIsAgg is true if the subquery uses aggregates.
93135 **
93136 ** If flattening is not attempted, this routine is a no-op and returns 0.
93137 ** If flattening is attempted this routine returns 1.
93138 **
93139 ** All of the expression analysis must occur on both the outer query and
93140 ** the subquery before this routine runs.
93141 */
93142 static int flattenSubquery(
93143   Parse *pParse,       /* Parsing context */
93144   Select *p,           /* The parent or outer SELECT statement */
93145   int iFrom,           /* Index in p->pSrc->a[] of the inner subquery */
93146   int isAgg,           /* True if outer SELECT uses aggregate functions */
93147   int subqueryIsAgg    /* True if the subquery uses aggregate functions */
93148 ){
93149   const char *zSavedAuthContext = pParse->zAuthContext;
93150   Select *pParent;
93151   Select *pSub;       /* The inner query or "subquery" */
93152   Select *pSub1;      /* Pointer to the rightmost select in sub-query */
93153   SrcList *pSrc;      /* The FROM clause of the outer query */
93154   SrcList *pSubSrc;   /* The FROM clause of the subquery */
93155   ExprList *pList;    /* The result set of the outer query */
93156   int iParent;        /* VDBE cursor number of the pSub result set temp table */
93157   int i;              /* Loop counter */
93158   Expr *pWhere;                    /* The WHERE clause */
93159   struct SrcList_item *pSubitem;   /* The subquery */
93160   sqlite3 *db = pParse->db;
93161 
93162   /* Check to see if flattening is permitted.  Return 0 if not.
93163   */
93164   assert( p!=0 );
93165   assert( p->pPrior==0 );  /* Unable to flatten compound queries */
93166   if( db->flags & SQLITE_QueryFlattener ) return 0;
93167   pSrc = p->pSrc;
93168   assert( pSrc && iFrom>=0 && iFrom<pSrc->nSrc );
93169   pSubitem = &pSrc->a[iFrom];
93170   iParent = pSubitem->iCursor;
93171   pSub = pSubitem->pSelect;
93172   assert( pSub!=0 );
93173   if( isAgg && subqueryIsAgg ) return 0;                 /* Restriction (1)  */
93174   if( subqueryIsAgg && pSrc->nSrc>1 ) return 0;          /* Restriction (2)  */
93175   pSubSrc = pSub->pSrc;
93176   assert( pSubSrc );
93177   /* Prior to version 3.1.2, when LIMIT and OFFSET had to be simple constants,
93178   ** not arbitrary expresssions, we allowed some combining of LIMIT and OFFSET
93179   ** because they could be computed at compile-time.  But when LIMIT and OFFSET
93180   ** became arbitrary expressions, we were forced to add restrictions (13)
93181   ** and (14). */
93182   if( pSub->pLimit && p->pLimit ) return 0;              /* Restriction (13) */
93183   if( pSub->pOffset ) return 0;                          /* Restriction (14) */
93184   if( p->pRightmost && pSub->pLimit ){
93185     return 0;                                            /* Restriction (15) */
93186   }
93187   if( pSubSrc->nSrc==0 ) return 0;                       /* Restriction (7)  */
93188   if( pSub->selFlags & SF_Distinct ) return 0;           /* Restriction (5)  */
93189   if( pSub->pLimit && (pSrc->nSrc>1 || isAgg) ){
93190      return 0;         /* Restrictions (8)(9) */
93191   }
93192   if( (p->selFlags & SF_Distinct)!=0 && subqueryIsAgg ){
93193      return 0;         /* Restriction (6)  */
93194   }
93195   if( p->pOrderBy && pSub->pOrderBy ){
93196      return 0;                                           /* Restriction (11) */
93197   }
93198   if( isAgg && pSub->pOrderBy ) return 0;                /* Restriction (16) */
93199   if( pSub->pLimit && p->pWhere ) return 0;              /* Restriction (19) */
93200   if( pSub->pLimit && (p->selFlags & SF_Distinct)!=0 ){
93201      return 0;         /* Restriction (21) */
93202   }
93203 
93204   /* OBSOLETE COMMENT 1:
93205   ** Restriction 3:  If the subquery is a join, make sure the subquery is
93206   ** not used as the right operand of an outer join.  Examples of why this
93207   ** is not allowed:
93208   **
93209   **         t1 LEFT OUTER JOIN (t2 JOIN t3)
93210   **
93211   ** If we flatten the above, we would get
93212   **
93213   **         (t1 LEFT OUTER JOIN t2) JOIN t3
93214   **
93215   ** which is not at all the same thing.
93216   **
93217   ** OBSOLETE COMMENT 2:
93218   ** Restriction 12:  If the subquery is the right operand of a left outer
93219   ** join, make sure the subquery has no WHERE clause.
93220   ** An examples of why this is not allowed:
93221   **
93222   **         t1 LEFT OUTER JOIN (SELECT * FROM t2 WHERE t2.x>0)
93223   **
93224   ** If we flatten the above, we would get
93225   **
93226   **         (t1 LEFT OUTER JOIN t2) WHERE t2.x>0
93227   **
93228   ** But the t2.x>0 test will always fail on a NULL row of t2, which
93229   ** effectively converts the OUTER JOIN into an INNER JOIN.
93230   **
93231   ** THIS OVERRIDES OBSOLETE COMMENTS 1 AND 2 ABOVE:
93232   ** Ticket #3300 shows that flattening the right term of a LEFT JOIN
93233   ** is fraught with danger.  Best to avoid the whole thing.  If the
93234   ** subquery is the right term of a LEFT JOIN, then do not flatten.
93235   */
93236   if( (pSubitem->jointype & JT_OUTER)!=0 ){
93237     return 0;
93238   }
93239 
93240   /* Restriction 17: If the sub-query is a compound SELECT, then it must
93241   ** use only the UNION ALL operator. And none of the simple select queries
93242   ** that make up the compound SELECT are allowed to be aggregate or distinct
93243   ** queries.
93244   */
93245   if( pSub->pPrior ){
93246     if( pSub->pOrderBy ){
93247       return 0;  /* Restriction 20 */
93248     }
93249     if( isAgg || (p->selFlags & SF_Distinct)!=0 || pSrc->nSrc!=1 ){
93250       return 0;
93251     }
93252     for(pSub1=pSub; pSub1; pSub1=pSub1->pPrior){
93253       testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
93254       testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
93255       if( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))!=0
93256        || (pSub1->pPrior && pSub1->op!=TK_ALL)
93257        || NEVER(pSub1->pSrc==0) || pSub1->pSrc->nSrc!=1
93258       ){
93259         return 0;
93260       }
93261     }
93262 
93263     /* Restriction 18. */
93264     if( p->pOrderBy ){
93265       int ii;
93266       for(ii=0; ii<p->pOrderBy->nExpr; ii++){
93267         if( p->pOrderBy->a[ii].iCol==0 ) return 0;
93268       }
93269     }
93270   }
93271 
93272   /***** If we reach this point, flattening is permitted. *****/
93273 
93274   /* Authorize the subquery */
93275   pParse->zAuthContext = pSubitem->zName;
93276   sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0);
93277   pParse->zAuthContext = zSavedAuthContext;
93278 
93279   /* If the sub-query is a compound SELECT statement, then (by restrictions
93280   ** 17 and 18 above) it must be a UNION ALL and the parent query must
93281   ** be of the form:
93282   **
93283   **     SELECT <expr-list> FROM (<sub-query>) <where-clause>
93284   **
93285   ** followed by any ORDER BY, LIMIT and/or OFFSET clauses. This block
93286   ** creates N-1 copies of the parent query without any ORDER BY, LIMIT or
93287   ** OFFSET clauses and joins them to the left-hand-side of the original
93288   ** using UNION ALL operators. In this case N is the number of simple
93289   ** select statements in the compound sub-query.
93290   **
93291   ** Example:
93292   **
93293   **     SELECT a+1 FROM (
93294   **        SELECT x FROM tab
93295   **        UNION ALL
93296   **        SELECT y FROM tab
93297   **        UNION ALL
93298   **        SELECT abs(z*2) FROM tab2
93299   **     ) WHERE a!=5 ORDER BY 1
93300   **
93301   ** Transformed into:
93302   **
93303   **     SELECT x+1 FROM tab WHERE x+1!=5
93304   **     UNION ALL
93305   **     SELECT y+1 FROM tab WHERE y+1!=5
93306   **     UNION ALL
93307   **     SELECT abs(z*2)+1 FROM tab2 WHERE abs(z*2)+1!=5
93308   **     ORDER BY 1
93309   **
93310   ** We call this the "compound-subquery flattening".
93311   */
93312   for(pSub=pSub->pPrior; pSub; pSub=pSub->pPrior){
93313     Select *pNew;
93314     ExprList *pOrderBy = p->pOrderBy;
93315     Expr *pLimit = p->pLimit;
93316     Select *pPrior = p->pPrior;
93317     p->pOrderBy = 0;
93318     p->pSrc = 0;
93319     p->pPrior = 0;
93320     p->pLimit = 0;
93321     pNew = sqlite3SelectDup(db, p, 0);
93322     p->pLimit = pLimit;
93323     p->pOrderBy = pOrderBy;
93324     p->pSrc = pSrc;
93325     p->op = TK_ALL;
93326     p->pRightmost = 0;
93327     if( pNew==0 ){
93328       pNew = pPrior;
93329     }else{
93330       pNew->pPrior = pPrior;
93331       pNew->pRightmost = 0;
93332     }
93333     p->pPrior = pNew;
93334     if( db->mallocFailed ) return 1;
93335   }
93336 
93337   /* Begin flattening the iFrom-th entry of the FROM clause
93338   ** in the outer query.
93339   */
93340   pSub = pSub1 = pSubitem->pSelect;
93341 
93342   /* Delete the transient table structure associated with the
93343   ** subquery
93344   */
93345   sqlite3DbFree(db, pSubitem->zDatabase);
93346   sqlite3DbFree(db, pSubitem->zName);
93347   sqlite3DbFree(db, pSubitem->zAlias);
93348   pSubitem->zDatabase = 0;
93349   pSubitem->zName = 0;
93350   pSubitem->zAlias = 0;
93351   pSubitem->pSelect = 0;
93352 
93353   /* Defer deleting the Table object associated with the
93354   ** subquery until code generation is
93355   ** complete, since there may still exist Expr.pTab entries that
93356   ** refer to the subquery even after flattening.  Ticket #3346.
93357   **
93358   ** pSubitem->pTab is always non-NULL by test restrictions and tests above.
93359   */
93360   if( ALWAYS(pSubitem->pTab!=0) ){
93361     Table *pTabToDel = pSubitem->pTab;
93362     if( pTabToDel->nRef==1 ){
93363       Parse *pToplevel = sqlite3ParseToplevel(pParse);
93364       pTabToDel->pNextZombie = pToplevel->pZombieTab;
93365       pToplevel->pZombieTab = pTabToDel;
93366     }else{
93367       pTabToDel->nRef--;
93368     }
93369     pSubitem->pTab = 0;
93370   }
93371 
93372   /* The following loop runs once for each term in a compound-subquery
93373   ** flattening (as described above).  If we are doing a different kind
93374   ** of flattening - a flattening other than a compound-subquery flattening -
93375   ** then this loop only runs once.
93376   **
93377   ** This loop moves all of the FROM elements of the subquery into the
93378   ** the FROM clause of the outer query.  Before doing this, remember
93379   ** the cursor number for the original outer query FROM element in
93380   ** iParent.  The iParent cursor will never be used.  Subsequent code
93381   ** will scan expressions looking for iParent references and replace
93382   ** those references with expressions that resolve to the subquery FROM
93383   ** elements we are now copying in.
93384   */
93385   for(pParent=p; pParent; pParent=pParent->pPrior, pSub=pSub->pPrior){
93386     int nSubSrc;
93387     u8 jointype = 0;
93388     pSubSrc = pSub->pSrc;     /* FROM clause of subquery */
93389     nSubSrc = pSubSrc->nSrc;  /* Number of terms in subquery FROM clause */
93390     pSrc = pParent->pSrc;     /* FROM clause of the outer query */
93391 
93392     if( pSrc ){
93393       assert( pParent==p );  /* First time through the loop */
93394       jointype = pSubitem->jointype;
93395     }else{
93396       assert( pParent!=p );  /* 2nd and subsequent times through the loop */
93397       pSrc = pParent->pSrc = sqlite3SrcListAppend(db, 0, 0, 0);
93398       if( pSrc==0 ){
93399         assert( db->mallocFailed );
93400         break;
93401       }
93402     }
93403 
93404     /* The subquery uses a single slot of the FROM clause of the outer
93405     ** query.  If the subquery has more than one element in its FROM clause,
93406     ** then expand the outer query to make space for it to hold all elements
93407     ** of the subquery.
93408     **
93409     ** Example:
93410     **
93411     **    SELECT * FROM tabA, (SELECT * FROM sub1, sub2), tabB;
93412     **
93413     ** The outer query has 3 slots in its FROM clause.  One slot of the
93414     ** outer query (the middle slot) is used by the subquery.  The next
93415     ** block of code will expand the out query to 4 slots.  The middle
93416     ** slot is expanded to two slots in order to make space for the
93417     ** two elements in the FROM clause of the subquery.
93418     */
93419     if( nSubSrc>1 ){
93420       pParent->pSrc = pSrc = sqlite3SrcListEnlarge(db, pSrc, nSubSrc-1,iFrom+1);
93421       if( db->mallocFailed ){
93422         break;
93423       }
93424     }
93425 
93426     /* Transfer the FROM clause terms from the subquery into the
93427     ** outer query.
93428     */
93429     for(i=0; i<nSubSrc; i++){
93430       sqlite3IdListDelete(db, pSrc->a[i+iFrom].pUsing);
93431       pSrc->a[i+iFrom] = pSubSrc->a[i];
93432       memset(&pSubSrc->a[i], 0, sizeof(pSubSrc->a[i]));
93433     }
93434     pSrc->a[iFrom].jointype = jointype;
93435 
93436     /* Now begin substituting subquery result set expressions for
93437     ** references to the iParent in the outer query.
93438     **
93439     ** Example:
93440     **
93441     **   SELECT a+5, b*10 FROM (SELECT x*3 AS a, y+10 AS b FROM t1) WHERE a>b;
93442     **   \                     \_____________ subquery __________/          /
93443     **    \_____________________ outer query ______________________________/
93444     **
93445     ** We look at every expression in the outer query and every place we see
93446     ** "a" we substitute "x*3" and every place we see "b" we substitute "y+10".
93447     */
93448     pList = pParent->pEList;
93449     for(i=0; i<pList->nExpr; i++){
93450       if( pList->a[i].zName==0 ){
93451         const char *zSpan = pList->a[i].zSpan;
93452         if( ALWAYS(zSpan) ){
93453           pList->a[i].zName = sqlite3DbStrDup(db, zSpan);
93454         }
93455       }
93456     }
93457     substExprList(db, pParent->pEList, iParent, pSub->pEList);
93458     if( isAgg ){
93459       substExprList(db, pParent->pGroupBy, iParent, pSub->pEList);
93460       pParent->pHaving = substExpr(db, pParent->pHaving, iParent, pSub->pEList);
93461     }
93462     if( pSub->pOrderBy ){
93463       assert( pParent->pOrderBy==0 );
93464       pParent->pOrderBy = pSub->pOrderBy;
93465       pSub->pOrderBy = 0;
93466     }else if( pParent->pOrderBy ){
93467       substExprList(db, pParent->pOrderBy, iParent, pSub->pEList);
93468     }
93469     if( pSub->pWhere ){
93470       pWhere = sqlite3ExprDup(db, pSub->pWhere, 0);
93471     }else{
93472       pWhere = 0;
93473     }
93474     if( subqueryIsAgg ){
93475       assert( pParent->pHaving==0 );
93476       pParent->pHaving = pParent->pWhere;
93477       pParent->pWhere = pWhere;
93478       pParent->pHaving = substExpr(db, pParent->pHaving, iParent, pSub->pEList);
93479       pParent->pHaving = sqlite3ExprAnd(db, pParent->pHaving,
93480                                   sqlite3ExprDup(db, pSub->pHaving, 0));
93481       assert( pParent->pGroupBy==0 );
93482       pParent->pGroupBy = sqlite3ExprListDup(db, pSub->pGroupBy, 0);
93483     }else{
93484       pParent->pWhere = substExpr(db, pParent->pWhere, iParent, pSub->pEList);
93485       pParent->pWhere = sqlite3ExprAnd(db, pParent->pWhere, pWhere);
93486     }
93487 
93488     /* The flattened query is distinct if either the inner or the
93489     ** outer query is distinct.
93490     */
93491     pParent->selFlags |= pSub->selFlags & SF_Distinct;
93492 
93493     /*
93494     ** SELECT ... FROM (SELECT ... LIMIT a OFFSET b) LIMIT x OFFSET y;
93495     **
93496     ** One is tempted to try to add a and b to combine the limits.  But this
93497     ** does not work if either limit is negative.
93498     */
93499     if( pSub->pLimit ){
93500       pParent->pLimit = pSub->pLimit;
93501       pSub->pLimit = 0;
93502     }
93503   }
93504 
93505   /* Finially, delete what is left of the subquery and return
93506   ** success.
93507   */
93508   sqlite3SelectDelete(db, pSub1);
93509 
93510   return 1;
93511 }
93512 #endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
93513 
93514 /*
93515 ** Analyze the SELECT statement passed as an argument to see if it
93516 ** is a min() or max() query. Return WHERE_ORDERBY_MIN or WHERE_ORDERBY_MAX if
93517 ** it is, or 0 otherwise. At present, a query is considered to be
93518 ** a min()/max() query if:
93519 **
93520 **   1. There is a single object in the FROM clause.
93521 **
93522 **   2. There is a single expression in the result set, and it is
93523 **      either min(x) or max(x), where x is a column reference.
93524 */
93525 static u8 minMaxQuery(Select *p){
93526   Expr *pExpr;
93527   ExprList *pEList = p->pEList;
93528 
93529   if( pEList->nExpr!=1 ) return WHERE_ORDERBY_NORMAL;
93530   pExpr = pEList->a[0].pExpr;
93531   if( pExpr->op!=TK_AGG_FUNCTION ) return 0;
93532   if( NEVER(ExprHasProperty(pExpr, EP_xIsSelect)) ) return 0;
93533   pEList = pExpr->x.pList;
93534   if( pEList==0 || pEList->nExpr!=1 ) return 0;
93535   if( pEList->a[0].pExpr->op!=TK_AGG_COLUMN ) return WHERE_ORDERBY_NORMAL;
93536   assert( !ExprHasProperty(pExpr, EP_IntValue) );
93537   if( sqlite3StrICmp(pExpr->u.zToken,"min")==0 ){
93538     return WHERE_ORDERBY_MIN;
93539   }else if( sqlite3StrICmp(pExpr->u.zToken,"max")==0 ){
93540     return WHERE_ORDERBY_MAX;
93541   }
93542   return WHERE_ORDERBY_NORMAL;
93543 }
93544 
93545 /*
93546 ** The select statement passed as the first argument is an aggregate query.
93547 ** The second argment is the associated aggregate-info object. This
93548 ** function tests if the SELECT is of the form:
93549 **
93550 **   SELECT count(*) FROM <tbl>
93551 **
93552 ** where table is a database table, not a sub-select or view. If the query
93553 ** does match this pattern, then a pointer to the Table object representing
93554 ** <tbl> is returned. Otherwise, 0 is returned.
93555 */
93556 static Table *isSimpleCount(Select *p, AggInfo *pAggInfo){
93557   Table *pTab;
93558   Expr *pExpr;
93559 
93560   assert( !p->pGroupBy );
93561 
93562   if( p->pWhere || p->pEList->nExpr!=1
93563    || p->pSrc->nSrc!=1 || p->pSrc->a[0].pSelect
93564   ){
93565     return 0;
93566   }
93567   pTab = p->pSrc->a[0].pTab;
93568   pExpr = p->pEList->a[0].pExpr;
93569   assert( pTab && !pTab->pSelect && pExpr );
93570 
93571   if( IsVirtual(pTab) ) return 0;
93572   if( pExpr->op!=TK_AGG_FUNCTION ) return 0;
93573   if( (pAggInfo->aFunc[0].pFunc->flags&SQLITE_FUNC_COUNT)==0 ) return 0;
93574   if( pExpr->flags&EP_Distinct ) return 0;
93575 
93576   return pTab;
93577 }
93578 
93579 /*
93580 ** If the source-list item passed as an argument was augmented with an
93581 ** INDEXED BY clause, then try to locate the specified index. If there
93582 ** was such a clause and the named index cannot be found, return
93583 ** SQLITE_ERROR and leave an error in pParse. Otherwise, populate
93584 ** pFrom->pIndex and return SQLITE_OK.
93585 */
93586 SQLITE_PRIVATE int sqlite3IndexedByLookup(Parse *pParse, struct SrcList_item *pFrom){
93587   if( pFrom->pTab && pFrom->zIndex ){
93588     Table *pTab = pFrom->pTab;
93589     char *zIndex = pFrom->zIndex;
93590     Index *pIdx;
93591     for(pIdx=pTab->pIndex;
93592         pIdx && sqlite3StrICmp(pIdx->zName, zIndex);
93593         pIdx=pIdx->pNext
93594     );
93595     if( !pIdx ){
93596       sqlite3ErrorMsg(pParse, "no such index: %s", zIndex, 0);
93597       pParse->checkSchema = 1;
93598       return SQLITE_ERROR;
93599     }
93600     pFrom->pIndex = pIdx;
93601   }
93602   return SQLITE_OK;
93603 }
93604 
93605 /*
93606 ** This routine is a Walker callback for "expanding" a SELECT statement.
93607 ** "Expanding" means to do the following:
93608 **
93609 **    (1)  Make sure VDBE cursor numbers have been assigned to every
93610 **         element of the FROM clause.
93611 **
93612 **    (2)  Fill in the pTabList->a[].pTab fields in the SrcList that
93613 **         defines FROM clause.  When views appear in the FROM clause,
93614 **         fill pTabList->a[].pSelect with a copy of the SELECT statement
93615 **         that implements the view.  A copy is made of the view's SELECT
93616 **         statement so that we can freely modify or delete that statement
93617 **         without worrying about messing up the presistent representation
93618 **         of the view.
93619 **
93620 **    (3)  Add terms to the WHERE clause to accomodate the NATURAL keyword
93621 **         on joins and the ON and USING clause of joins.
93622 **
93623 **    (4)  Scan the list of columns in the result set (pEList) looking
93624 **         for instances of the "*" operator or the TABLE.* operator.
93625 **         If found, expand each "*" to be every column in every table
93626 **         and TABLE.* to be every column in TABLE.
93627 **
93628 */
93629 static int selectExpander(Walker *pWalker, Select *p){
93630   Parse *pParse = pWalker->pParse;
93631   int i, j, k;
93632   SrcList *pTabList;
93633   ExprList *pEList;
93634   struct SrcList_item *pFrom;
93635   sqlite3 *db = pParse->db;
93636 
93637   if( db->mallocFailed  ){
93638     return WRC_Abort;
93639   }
93640   if( NEVER(p->pSrc==0) || (p->selFlags & SF_Expanded)!=0 ){
93641     return WRC_Prune;
93642   }
93643   p->selFlags |= SF_Expanded;
93644   pTabList = p->pSrc;
93645   pEList = p->pEList;
93646 
93647   /* Make sure cursor numbers have been assigned to all entries in
93648   ** the FROM clause of the SELECT statement.
93649   */
93650   sqlite3SrcListAssignCursors(pParse, pTabList);
93651 
93652   /* Look up every table named in the FROM clause of the select.  If
93653   ** an entry of the FROM clause is a subquery instead of a table or view,
93654   ** then create a transient table structure to describe the subquery.
93655   */
93656   for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
93657     Table *pTab;
93658     if( pFrom->pTab!=0 ){
93659       /* This statement has already been prepared.  There is no need
93660       ** to go further. */
93661       assert( i==0 );
93662       return WRC_Prune;
93663     }
93664     if( pFrom->zName==0 ){
93665 #ifndef SQLITE_OMIT_SUBQUERY
93666       Select *pSel = pFrom->pSelect;
93667       /* A sub-query in the FROM clause of a SELECT */
93668       assert( pSel!=0 );
93669       assert( pFrom->pTab==0 );
93670       sqlite3WalkSelect(pWalker, pSel);
93671       pFrom->pTab = pTab = sqlite3DbMallocZero(db, sizeof(Table));
93672       if( pTab==0 ) return WRC_Abort;
93673       pTab->nRef = 1;
93674       pTab->zName = sqlite3MPrintf(db, "sqlite_subquery_%p_", (void*)pTab);
93675       while( pSel->pPrior ){ pSel = pSel->pPrior; }
93676       selectColumnsFromExprList(pParse, pSel->pEList, &pTab->nCol, &pTab->aCol);
93677       pTab->iPKey = -1;
93678       pTab->nRowEst = 1000000;
93679       pTab->tabFlags |= TF_Ephemeral;
93680 #endif
93681     }else{
93682       /* An ordinary table or view name in the FROM clause */
93683       assert( pFrom->pTab==0 );
93684       pFrom->pTab = pTab =
93685         sqlite3LocateTable(pParse,0,pFrom->zName,pFrom->zDatabase);
93686       if( pTab==0 ) return WRC_Abort;
93687       pTab->nRef++;
93688 #if !defined(SQLITE_OMIT_VIEW) || !defined (SQLITE_OMIT_VIRTUALTABLE)
93689       if( pTab->pSelect || IsVirtual(pTab) ){
93690         /* We reach here if the named table is a really a view */
93691         if( sqlite3ViewGetColumnNames(pParse, pTab) ) return WRC_Abort;
93692         assert( pFrom->pSelect==0 );
93693         pFrom->pSelect = sqlite3SelectDup(db, pTab->pSelect, 0);
93694         sqlite3WalkSelect(pWalker, pFrom->pSelect);
93695       }
93696 #endif
93697     }
93698 
93699     /* Locate the index named by the INDEXED BY clause, if any. */
93700     if( sqlite3IndexedByLookup(pParse, pFrom) ){
93701       return WRC_Abort;
93702     }
93703   }
93704 
93705   /* Process NATURAL keywords, and ON and USING clauses of joins.
93706   */
93707   if( db->mallocFailed || sqliteProcessJoin(pParse, p) ){
93708     return WRC_Abort;
93709   }
93710 
93711   /* For every "*" that occurs in the column list, insert the names of
93712   ** all columns in all tables.  And for every TABLE.* insert the names
93713   ** of all columns in TABLE.  The parser inserted a special expression
93714   ** with the TK_ALL operator for each "*" that it found in the column list.
93715   ** The following code just has to locate the TK_ALL expressions and expand
93716   ** each one to the list of all columns in all tables.
93717   **
93718   ** The first loop just checks to see if there are any "*" operators
93719   ** that need expanding.
93720   */
93721   for(k=0; k<pEList->nExpr; k++){
93722     Expr *pE = pEList->a[k].pExpr;
93723     if( pE->op==TK_ALL ) break;
93724     assert( pE->op!=TK_DOT || pE->pRight!=0 );
93725     assert( pE->op!=TK_DOT || (pE->pLeft!=0 && pE->pLeft->op==TK_ID) );
93726     if( pE->op==TK_DOT && pE->pRight->op==TK_ALL ) break;
93727   }
93728   if( k<pEList->nExpr ){
93729     /*
93730     ** If we get here it means the result set contains one or more "*"
93731     ** operators that need to be expanded.  Loop through each expression
93732     ** in the result set and expand them one by one.
93733     */
93734     struct ExprList_item *a = pEList->a;
93735     ExprList *pNew = 0;
93736     int flags = pParse->db->flags;
93737     int longNames = (flags & SQLITE_FullColNames)!=0
93738                       && (flags & SQLITE_ShortColNames)==0;
93739 
93740     for(k=0; k<pEList->nExpr; k++){
93741       Expr *pE = a[k].pExpr;
93742       assert( pE->op!=TK_DOT || pE->pRight!=0 );
93743       if( pE->op!=TK_ALL && (pE->op!=TK_DOT || pE->pRight->op!=TK_ALL) ){
93744         /* This particular expression does not need to be expanded.
93745         */
93746         pNew = sqlite3ExprListAppend(pParse, pNew, a[k].pExpr);
93747         if( pNew ){
93748           pNew->a[pNew->nExpr-1].zName = a[k].zName;
93749           pNew->a[pNew->nExpr-1].zSpan = a[k].zSpan;
93750           a[k].zName = 0;
93751           a[k].zSpan = 0;
93752         }
93753         a[k].pExpr = 0;
93754       }else{
93755         /* This expression is a "*" or a "TABLE.*" and needs to be
93756         ** expanded. */
93757         int tableSeen = 0;      /* Set to 1 when TABLE matches */
93758         char *zTName;            /* text of name of TABLE */
93759         if( pE->op==TK_DOT ){
93760           assert( pE->pLeft!=0 );
93761           assert( !ExprHasProperty(pE->pLeft, EP_IntValue) );
93762           zTName = pE->pLeft->u.zToken;
93763         }else{
93764           zTName = 0;
93765         }
93766         for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
93767           Table *pTab = pFrom->pTab;
93768           char *zTabName = pFrom->zAlias;
93769           if( zTabName==0 ){
93770             zTabName = pTab->zName;
93771           }
93772           if( db->mallocFailed ) break;
93773           if( zTName && sqlite3StrICmp(zTName, zTabName)!=0 ){
93774             continue;
93775           }
93776           tableSeen = 1;
93777           for(j=0; j<pTab->nCol; j++){
93778             Expr *pExpr, *pRight;
93779             char *zName = pTab->aCol[j].zName;
93780             char *zColname;  /* The computed column name */
93781             char *zToFree;   /* Malloced string that needs to be freed */
93782             Token sColname;  /* Computed column name as a token */
93783 
93784             /* If a column is marked as 'hidden' (currently only possible
93785             ** for virtual tables), do not include it in the expanded
93786             ** result-set list.
93787             */
93788             if( IsHiddenColumn(&pTab->aCol[j]) ){
93789               assert(IsVirtual(pTab));
93790               continue;
93791             }
93792 
93793             if( i>0 && zTName==0 ){
93794               if( (pFrom->jointype & JT_NATURAL)!=0
93795                 && tableAndColumnIndex(pTabList, i, zName, 0, 0)
93796               ){
93797                 /* In a NATURAL join, omit the join columns from the
93798                 ** table to the right of the join */
93799                 continue;
93800               }
93801               if( sqlite3IdListIndex(pFrom->pUsing, zName)>=0 ){
93802                 /* In a join with a USING clause, omit columns in the
93803                 ** using clause from the table on the right. */
93804                 continue;
93805               }
93806             }
93807             pRight = sqlite3Expr(db, TK_ID, zName);
93808             zColname = zName;
93809             zToFree = 0;
93810             if( longNames || pTabList->nSrc>1 ){
93811               Expr *pLeft;
93812               pLeft = sqlite3Expr(db, TK_ID, zTabName);
93813               pExpr = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight, 0);
93814               if( longNames ){
93815                 zColname = sqlite3MPrintf(db, "%s.%s", zTabName, zName);
93816                 zToFree = zColname;
93817               }
93818             }else{
93819               pExpr = pRight;
93820             }
93821             pNew = sqlite3ExprListAppend(pParse, pNew, pExpr);
93822             sColname.z = zColname;
93823             sColname.n = sqlite3Strlen30(zColname);
93824             sqlite3ExprListSetName(pParse, pNew, &sColname, 0);
93825             sqlite3DbFree(db, zToFree);
93826           }
93827         }
93828         if( !tableSeen ){
93829           if( zTName ){
93830             sqlite3ErrorMsg(pParse, "no such table: %s", zTName);
93831           }else{
93832             sqlite3ErrorMsg(pParse, "no tables specified");
93833           }
93834         }
93835       }
93836     }
93837     sqlite3ExprListDelete(db, pEList);
93838     p->pEList = pNew;
93839   }
93840 #if SQLITE_MAX_COLUMN
93841   if( p->pEList && p->pEList->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
93842     sqlite3ErrorMsg(pParse, "too many columns in result set");
93843   }
93844 #endif
93845   return WRC_Continue;
93846 }
93847 
93848 /*
93849 ** No-op routine for the parse-tree walker.
93850 **
93851 ** When this routine is the Walker.xExprCallback then expression trees
93852 ** are walked without any actions being taken at each node.  Presumably,
93853 ** when this routine is used for Walker.xExprCallback then
93854 ** Walker.xSelectCallback is set to do something useful for every
93855 ** subquery in the parser tree.
93856 */
93857 static int exprWalkNoop(Walker *NotUsed, Expr *NotUsed2){
93858   UNUSED_PARAMETER2(NotUsed, NotUsed2);
93859   return WRC_Continue;
93860 }
93861 
93862 /*
93863 ** This routine "expands" a SELECT statement and all of its subqueries.
93864 ** For additional information on what it means to "expand" a SELECT
93865 ** statement, see the comment on the selectExpand worker callback above.
93866 **
93867 ** Expanding a SELECT statement is the first step in processing a
93868 ** SELECT statement.  The SELECT statement must be expanded before
93869 ** name resolution is performed.
93870 **
93871 ** If anything goes wrong, an error message is written into pParse.
93872 ** The calling function can detect the problem by looking at pParse->nErr
93873 ** and/or pParse->db->mallocFailed.
93874 */
93875 static void sqlite3SelectExpand(Parse *pParse, Select *pSelect){
93876   Walker w;
93877   w.xSelectCallback = selectExpander;
93878   w.xExprCallback = exprWalkNoop;
93879   w.pParse = pParse;
93880   sqlite3WalkSelect(&w, pSelect);
93881 }
93882 
93883 
93884 #ifndef SQLITE_OMIT_SUBQUERY
93885 /*
93886 ** This is a Walker.xSelectCallback callback for the sqlite3SelectTypeInfo()
93887 ** interface.
93888 **
93889 ** For each FROM-clause subquery, add Column.zType and Column.zColl
93890 ** information to the Table structure that represents the result set
93891 ** of that subquery.
93892 **
93893 ** The Table structure that represents the result set was constructed
93894 ** by selectExpander() but the type and collation information was omitted
93895 ** at that point because identifiers had not yet been resolved.  This
93896 ** routine is called after identifier resolution.
93897 */
93898 static int selectAddSubqueryTypeInfo(Walker *pWalker, Select *p){
93899   Parse *pParse;
93900   int i;
93901   SrcList *pTabList;
93902   struct SrcList_item *pFrom;
93903 
93904   assert( p->selFlags & SF_Resolved );
93905   if( (p->selFlags & SF_HasTypeInfo)==0 ){
93906     p->selFlags |= SF_HasTypeInfo;
93907     pParse = pWalker->pParse;
93908     pTabList = p->pSrc;
93909     for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
93910       Table *pTab = pFrom->pTab;
93911       if( ALWAYS(pTab!=0) && (pTab->tabFlags & TF_Ephemeral)!=0 ){
93912         /* A sub-query in the FROM clause of a SELECT */
93913         Select *pSel = pFrom->pSelect;
93914         assert( pSel );
93915         while( pSel->pPrior ) pSel = pSel->pPrior;
93916         selectAddColumnTypeAndCollation(pParse, pTab->nCol, pTab->aCol, pSel);
93917       }
93918     }
93919   }
93920   return WRC_Continue;
93921 }
93922 #endif
93923 
93924 
93925 /*
93926 ** This routine adds datatype and collating sequence information to
93927 ** the Table structures of all FROM-clause subqueries in a
93928 ** SELECT statement.
93929 **
93930 ** Use this routine after name resolution.
93931 */
93932 static void sqlite3SelectAddTypeInfo(Parse *pParse, Select *pSelect){
93933 #ifndef SQLITE_OMIT_SUBQUERY
93934   Walker w;
93935   w.xSelectCallback = selectAddSubqueryTypeInfo;
93936   w.xExprCallback = exprWalkNoop;
93937   w.pParse = pParse;
93938   sqlite3WalkSelect(&w, pSelect);
93939 #endif
93940 }
93941 
93942 
93943 /*
93944 ** This routine sets of a SELECT statement for processing.  The
93945 ** following is accomplished:
93946 **
93947 **     *  VDBE Cursor numbers are assigned to all FROM-clause terms.
93948 **     *  Ephemeral Table objects are created for all FROM-clause subqueries.
93949 **     *  ON and USING clauses are shifted into WHERE statements
93950 **     *  Wildcards "*" and "TABLE.*" in result sets are expanded.
93951 **     *  Identifiers in expression are matched to tables.
93952 **
93953 ** This routine acts recursively on all subqueries within the SELECT.
93954 */
93955 SQLITE_PRIVATE void sqlite3SelectPrep(
93956   Parse *pParse,         /* The parser context */
93957   Select *p,             /* The SELECT statement being coded. */
93958   NameContext *pOuterNC  /* Name context for container */
93959 ){
93960   sqlite3 *db;
93961   if( NEVER(p==0) ) return;
93962   db = pParse->db;
93963   if( p->selFlags & SF_HasTypeInfo ) return;
93964   sqlite3SelectExpand(pParse, p);
93965   if( pParse->nErr || db->mallocFailed ) return;
93966   sqlite3ResolveSelectNames(pParse, p, pOuterNC);
93967   if( pParse->nErr || db->mallocFailed ) return;
93968   sqlite3SelectAddTypeInfo(pParse, p);
93969 }
93970 
93971 /*
93972 ** Reset the aggregate accumulator.
93973 **
93974 ** The aggregate accumulator is a set of memory cells that hold
93975 ** intermediate results while calculating an aggregate.  This
93976 ** routine simply stores NULLs in all of those memory cells.
93977 */
93978 static void resetAccumulator(Parse *pParse, AggInfo *pAggInfo){
93979   Vdbe *v = pParse->pVdbe;
93980   int i;
93981   struct AggInfo_func *pFunc;
93982   if( pAggInfo->nFunc+pAggInfo->nColumn==0 ){
93983     return;
93984   }
93985   for(i=0; i<pAggInfo->nColumn; i++){
93986     sqlite3VdbeAddOp2(v, OP_Null, 0, pAggInfo->aCol[i].iMem);
93987   }
93988   for(pFunc=pAggInfo->aFunc, i=0; i<pAggInfo->nFunc; i++, pFunc++){
93989     sqlite3VdbeAddOp2(v, OP_Null, 0, pFunc->iMem);
93990     if( pFunc->iDistinct>=0 ){
93991       Expr *pE = pFunc->pExpr;
93992       assert( !ExprHasProperty(pE, EP_xIsSelect) );
93993       if( pE->x.pList==0 || pE->x.pList->nExpr!=1 ){
93994         sqlite3ErrorMsg(pParse, "DISTINCT aggregates must have exactly one "
93995            "argument");
93996         pFunc->iDistinct = -1;
93997       }else{
93998         KeyInfo *pKeyInfo = keyInfoFromExprList(pParse, pE->x.pList);
93999         sqlite3VdbeAddOp4(v, OP_OpenEphemeral, pFunc->iDistinct, 0, 0,
94000                           (char*)pKeyInfo, P4_KEYINFO_HANDOFF);
94001       }
94002     }
94003   }
94004 }
94005 
94006 /*
94007 ** Invoke the OP_AggFinalize opcode for every aggregate function
94008 ** in the AggInfo structure.
94009 */
94010 static void finalizeAggFunctions(Parse *pParse, AggInfo *pAggInfo){
94011   Vdbe *v = pParse->pVdbe;
94012   int i;
94013   struct AggInfo_func *pF;
94014   for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
94015     ExprList *pList = pF->pExpr->x.pList;
94016     assert( !ExprHasProperty(pF->pExpr, EP_xIsSelect) );
94017     sqlite3VdbeAddOp4(v, OP_AggFinal, pF->iMem, pList ? pList->nExpr : 0, 0,
94018                       (void*)pF->pFunc, P4_FUNCDEF);
94019   }
94020 }
94021 
94022 /*
94023 ** Update the accumulator memory cells for an aggregate based on
94024 ** the current cursor position.
94025 */
94026 static void updateAccumulator(Parse *pParse, AggInfo *pAggInfo){
94027   Vdbe *v = pParse->pVdbe;
94028   int i;
94029   struct AggInfo_func *pF;
94030   struct AggInfo_col *pC;
94031 
94032   pAggInfo->directMode = 1;
94033   sqlite3ExprCacheClear(pParse);
94034   for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
94035     int nArg;
94036     int addrNext = 0;
94037     int regAgg;
94038     ExprList *pList = pF->pExpr->x.pList;
94039     assert( !ExprHasProperty(pF->pExpr, EP_xIsSelect) );
94040     if( pList ){
94041       nArg = pList->nExpr;
94042       regAgg = sqlite3GetTempRange(pParse, nArg);
94043       sqlite3ExprCodeExprList(pParse, pList, regAgg, 1);
94044     }else{
94045       nArg = 0;
94046       regAgg = 0;
94047     }
94048     if( pF->iDistinct>=0 ){
94049       addrNext = sqlite3VdbeMakeLabel(v);
94050       assert( nArg==1 );
94051       codeDistinct(pParse, pF->iDistinct, addrNext, 1, regAgg);
94052     }
94053     if( pF->pFunc->flags & SQLITE_FUNC_NEEDCOLL ){
94054       CollSeq *pColl = 0;
94055       struct ExprList_item *pItem;
94056       int j;
94057       assert( pList!=0 );  /* pList!=0 if pF->pFunc has NEEDCOLL */
94058       for(j=0, pItem=pList->a; !pColl && j<nArg; j++, pItem++){
94059         pColl = sqlite3ExprCollSeq(pParse, pItem->pExpr);
94060       }
94061       if( !pColl ){
94062         pColl = pParse->db->pDfltColl;
94063       }
94064       sqlite3VdbeAddOp4(v, OP_CollSeq, 0, 0, 0, (char *)pColl, P4_COLLSEQ);
94065     }
94066     sqlite3VdbeAddOp4(v, OP_AggStep, 0, regAgg, pF->iMem,
94067                       (void*)pF->pFunc, P4_FUNCDEF);
94068     sqlite3VdbeChangeP5(v, (u8)nArg);
94069     sqlite3ExprCacheAffinityChange(pParse, regAgg, nArg);
94070     sqlite3ReleaseTempRange(pParse, regAgg, nArg);
94071     if( addrNext ){
94072       sqlite3VdbeResolveLabel(v, addrNext);
94073       sqlite3ExprCacheClear(pParse);
94074     }
94075   }
94076 
94077   /* Before populating the accumulator registers, clear the column cache.
94078   ** Otherwise, if any of the required column values are already present
94079   ** in registers, sqlite3ExprCode() may use OP_SCopy to copy the value
94080   ** to pC->iMem. But by the time the value is used, the original register
94081   ** may have been used, invalidating the underlying buffer holding the
94082   ** text or blob value. See ticket [883034dcb5].
94083   **
94084   ** Another solution would be to change the OP_SCopy used to copy cached
94085   ** values to an OP_Copy.
94086   */
94087   sqlite3ExprCacheClear(pParse);
94088   for(i=0, pC=pAggInfo->aCol; i<pAggInfo->nAccumulator; i++, pC++){
94089     sqlite3ExprCode(pParse, pC->pExpr, pC->iMem);
94090   }
94091   pAggInfo->directMode = 0;
94092   sqlite3ExprCacheClear(pParse);
94093 }
94094 
94095 /*
94096 ** Add a single OP_Explain instruction to the VDBE to explain a simple
94097 ** count(*) query ("SELECT count(*) FROM pTab").
94098 */
94099 #ifndef SQLITE_OMIT_EXPLAIN
94100 static void explainSimpleCount(
94101   Parse *pParse,                  /* Parse context */
94102   Table *pTab,                    /* Table being queried */
94103   Index *pIdx                     /* Index used to optimize scan, or NULL */
94104 ){
94105   if( pParse->explain==2 ){
94106     char *zEqp = sqlite3MPrintf(pParse->db, "SCAN TABLE %s %s%s(~%d rows)",
94107         pTab->zName,
94108         pIdx ? "USING COVERING INDEX " : "",
94109         pIdx ? pIdx->zName : "",
94110         pTab->nRowEst
94111     );
94112     sqlite3VdbeAddOp4(
94113         pParse->pVdbe, OP_Explain, pParse->iSelectId, 0, 0, zEqp, P4_DYNAMIC
94114     );
94115   }
94116 }
94117 #else
94118 # define explainSimpleCount(a,b,c)
94119 #endif
94120 
94121 /*
94122 ** Generate code for the SELECT statement given in the p argument.
94123 **
94124 ** The results are distributed in various ways depending on the
94125 ** contents of the SelectDest structure pointed to by argument pDest
94126 ** as follows:
94127 **
94128 **     pDest->eDest    Result
94129 **     ------------    -------------------------------------------
94130 **     SRT_Output      Generate a row of output (using the OP_ResultRow
94131 **                     opcode) for each row in the result set.
94132 **
94133 **     SRT_Mem         Only valid if the result is a single column.
94134 **                     Store the first column of the first result row
94135 **                     in register pDest->iParm then abandon the rest
94136 **                     of the query.  This destination implies "LIMIT 1".
94137 **
94138 **     SRT_Set         The result must be a single column.  Store each
94139 **                     row of result as the key in table pDest->iParm.
94140 **                     Apply the affinity pDest->affinity before storing
94141 **                     results.  Used to implement "IN (SELECT ...)".
94142 **
94143 **     SRT_Union       Store results as a key in a temporary table pDest->iParm.
94144 **
94145 **     SRT_Except      Remove results from the temporary table pDest->iParm.
94146 **
94147 **     SRT_Table       Store results in temporary table pDest->iParm.
94148 **                     This is like SRT_EphemTab except that the table
94149 **                     is assumed to already be open.
94150 **
94151 **     SRT_EphemTab    Create an temporary table pDest->iParm and store
94152 **                     the result there. The cursor is left open after
94153 **                     returning.  This is like SRT_Table except that
94154 **                     this destination uses OP_OpenEphemeral to create
94155 **                     the table first.
94156 **
94157 **     SRT_Coroutine   Generate a co-routine that returns a new row of
94158 **                     results each time it is invoked.  The entry point
94159 **                     of the co-routine is stored in register pDest->iParm.
94160 **
94161 **     SRT_Exists      Store a 1 in memory cell pDest->iParm if the result
94162 **                     set is not empty.
94163 **
94164 **     SRT_Discard     Throw the results away.  This is used by SELECT
94165 **                     statements within triggers whose only purpose is
94166 **                     the side-effects of functions.
94167 **
94168 ** This routine returns the number of errors.  If any errors are
94169 ** encountered, then an appropriate error message is left in
94170 ** pParse->zErrMsg.
94171 **
94172 ** This routine does NOT free the Select structure passed in.  The
94173 ** calling function needs to do that.
94174 */
94175 SQLITE_PRIVATE int sqlite3Select(
94176   Parse *pParse,         /* The parser context */
94177   Select *p,             /* The SELECT statement being coded. */
94178   SelectDest *pDest      /* What to do with the query results */
94179 ){
94180   int i, j;              /* Loop counters */
94181   WhereInfo *pWInfo;     /* Return from sqlite3WhereBegin() */
94182   Vdbe *v;               /* The virtual machine under construction */
94183   int isAgg;             /* True for select lists like "count(*)" */
94184   ExprList *pEList;      /* List of columns to extract. */
94185   SrcList *pTabList;     /* List of tables to select from */
94186   Expr *pWhere;          /* The WHERE clause.  May be NULL */
94187   ExprList *pOrderBy;    /* The ORDER BY clause.  May be NULL */
94188   ExprList *pGroupBy;    /* The GROUP BY clause.  May be NULL */
94189   Expr *pHaving;         /* The HAVING clause.  May be NULL */
94190   int isDistinct;        /* True if the DISTINCT keyword is present */
94191   int distinct;          /* Table to use for the distinct set */
94192   int rc = 1;            /* Value to return from this function */
94193   int addrSortIndex;     /* Address of an OP_OpenEphemeral instruction */
94194   AggInfo sAggInfo;      /* Information used by aggregate queries */
94195   int iEnd;              /* Address of the end of the query */
94196   sqlite3 *db;           /* The database connection */
94197 
94198 #ifndef SQLITE_OMIT_EXPLAIN
94199   int iRestoreSelectId = pParse->iSelectId;
94200   pParse->iSelectId = pParse->iNextSelectId++;
94201 #endif
94202 
94203   db = pParse->db;
94204   if( p==0 || db->mallocFailed || pParse->nErr ){
94205     return 1;
94206   }
94207   if( sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0) ) return 1;
94208   memset(&sAggInfo, 0, sizeof(sAggInfo));
94209 
94210   if( IgnorableOrderby(pDest) ){
94211     assert(pDest->eDest==SRT_Exists || pDest->eDest==SRT_Union ||
94212            pDest->eDest==SRT_Except || pDest->eDest==SRT_Discard);
94213     /* If ORDER BY makes no difference in the output then neither does
94214     ** DISTINCT so it can be removed too. */
94215     sqlite3ExprListDelete(db, p->pOrderBy);
94216     p->pOrderBy = 0;
94217     p->selFlags &= ~SF_Distinct;
94218   }
94219   sqlite3SelectPrep(pParse, p, 0);
94220   pOrderBy = p->pOrderBy;
94221   pTabList = p->pSrc;
94222   pEList = p->pEList;
94223   if( pParse->nErr || db->mallocFailed ){
94224     goto select_end;
94225   }
94226   isAgg = (p->selFlags & SF_Aggregate)!=0;
94227   assert( pEList!=0 );
94228 
94229   /* Begin generating code.
94230   */
94231   v = sqlite3GetVdbe(pParse);
94232   if( v==0 ) goto select_end;
94233 
94234   /* If writing to memory or generating a set
94235   ** only a single column may be output.
94236   */
94237 #ifndef SQLITE_OMIT_SUBQUERY
94238   if( checkForMultiColumnSelectError(pParse, pDest, pEList->nExpr) ){
94239     goto select_end;
94240   }
94241 #endif
94242 
94243   /* Generate code for all sub-queries in the FROM clause
94244   */
94245 #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
94246   for(i=0; !p->pPrior && i<pTabList->nSrc; i++){
94247     struct SrcList_item *pItem = &pTabList->a[i];
94248     SelectDest dest;
94249     Select *pSub = pItem->pSelect;
94250     int isAggSub;
94251 
94252     if( pSub==0 || pItem->isPopulated ) continue;
94253 
94254     /* Increment Parse.nHeight by the height of the largest expression
94255     ** tree refered to by this, the parent select. The child select
94256     ** may contain expression trees of at most
94257     ** (SQLITE_MAX_EXPR_DEPTH-Parse.nHeight) height. This is a bit
94258     ** more conservative than necessary, but much easier than enforcing
94259     ** an exact limit.
94260     */
94261     pParse->nHeight += sqlite3SelectExprHeight(p);
94262 
94263     /* Check to see if the subquery can be absorbed into the parent. */
94264     isAggSub = (pSub->selFlags & SF_Aggregate)!=0;
94265     if( flattenSubquery(pParse, p, i, isAgg, isAggSub) ){
94266       if( isAggSub ){
94267         isAgg = 1;
94268         p->selFlags |= SF_Aggregate;
94269       }
94270       i = -1;
94271     }else{
94272       sqlite3SelectDestInit(&dest, SRT_EphemTab, pItem->iCursor);
94273       assert( pItem->isPopulated==0 );
94274       explainSetInteger(pItem->iSelectId, (u8)pParse->iNextSelectId);
94275       sqlite3Select(pParse, pSub, &dest);
94276       pItem->isPopulated = 1;
94277       pItem->pTab->nRowEst = (unsigned)pSub->nSelectRow;
94278     }
94279     if( /*pParse->nErr ||*/ db->mallocFailed ){
94280       goto select_end;
94281     }
94282     pParse->nHeight -= sqlite3SelectExprHeight(p);
94283     pTabList = p->pSrc;
94284     if( !IgnorableOrderby(pDest) ){
94285       pOrderBy = p->pOrderBy;
94286     }
94287   }
94288   pEList = p->pEList;
94289 #endif
94290   pWhere = p->pWhere;
94291   pGroupBy = p->pGroupBy;
94292   pHaving = p->pHaving;
94293   isDistinct = (p->selFlags & SF_Distinct)!=0;
94294 
94295 #ifndef SQLITE_OMIT_COMPOUND_SELECT
94296   /* If there is are a sequence of queries, do the earlier ones first.
94297   */
94298   if( p->pPrior ){
94299     if( p->pRightmost==0 ){
94300       Select *pLoop, *pRight = 0;
94301       int cnt = 0;
94302       int mxSelect;
94303       for(pLoop=p; pLoop; pLoop=pLoop->pPrior, cnt++){
94304         pLoop->pRightmost = p;
94305         pLoop->pNext = pRight;
94306         pRight = pLoop;
94307       }
94308       mxSelect = db->aLimit[SQLITE_LIMIT_COMPOUND_SELECT];
94309       if( mxSelect && cnt>mxSelect ){
94310         sqlite3ErrorMsg(pParse, "too many terms in compound SELECT");
94311         goto select_end;
94312       }
94313     }
94314     rc = multiSelect(pParse, p, pDest);
94315     explainSetInteger(pParse->iSelectId, iRestoreSelectId);
94316     return rc;
94317   }
94318 #endif
94319 
94320   /* If possible, rewrite the query to use GROUP BY instead of DISTINCT.
94321   ** GROUP BY might use an index, DISTINCT never does.
94322   */
94323   assert( p->pGroupBy==0 || (p->selFlags & SF_Aggregate)!=0 );
94324   if( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct ){
94325     p->pGroupBy = sqlite3ExprListDup(db, p->pEList, 0);
94326     pGroupBy = p->pGroupBy;
94327     p->selFlags &= ~SF_Distinct;
94328   }
94329 
94330   /* If there is both a GROUP BY and an ORDER BY clause and they are
94331   ** identical, then disable the ORDER BY clause since the GROUP BY
94332   ** will cause elements to come out in the correct order.  This is
94333   ** an optimization - the correct answer should result regardless.
94334   ** Use the SQLITE_GroupByOrder flag with SQLITE_TESTCTRL_OPTIMIZER
94335   ** to disable this optimization for testing purposes.
94336   */
94337   if( sqlite3ExprListCompare(p->pGroupBy, pOrderBy)==0
94338          && (db->flags & SQLITE_GroupByOrder)==0 ){
94339     pOrderBy = 0;
94340   }
94341 
94342   /* If there is an ORDER BY clause, then this sorting
94343   ** index might end up being unused if the data can be
94344   ** extracted in pre-sorted order.  If that is the case, then the
94345   ** OP_OpenEphemeral instruction will be changed to an OP_Noop once
94346   ** we figure out that the sorting index is not needed.  The addrSortIndex
94347   ** variable is used to facilitate that change.
94348   */
94349   if( pOrderBy ){
94350     KeyInfo *pKeyInfo;
94351     pKeyInfo = keyInfoFromExprList(pParse, pOrderBy);
94352     pOrderBy->iECursor = pParse->nTab++;
94353     p->addrOpenEphm[2] = addrSortIndex =
94354       sqlite3VdbeAddOp4(v, OP_OpenEphemeral,
94355                            pOrderBy->iECursor, pOrderBy->nExpr+2, 0,
94356                            (char*)pKeyInfo, P4_KEYINFO_HANDOFF);
94357   }else{
94358     addrSortIndex = -1;
94359   }
94360 
94361   /* If the output is destined for a temporary table, open that table.
94362   */
94363   if( pDest->eDest==SRT_EphemTab ){
94364     sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pDest->iParm, pEList->nExpr);
94365   }
94366 
94367   /* Set the limiter.
94368   */
94369   iEnd = sqlite3VdbeMakeLabel(v);
94370   p->nSelectRow = (double)LARGEST_INT64;
94371   computeLimitRegisters(pParse, p, iEnd);
94372 
94373   /* Open a virtual index to use for the distinct set.
94374   */
94375   if( p->selFlags & SF_Distinct ){
94376     KeyInfo *pKeyInfo;
94377     assert( isAgg || pGroupBy );
94378     distinct = pParse->nTab++;
94379     pKeyInfo = keyInfoFromExprList(pParse, p->pEList);
94380     sqlite3VdbeAddOp4(v, OP_OpenEphemeral, distinct, 0, 0,
94381                         (char*)pKeyInfo, P4_KEYINFO_HANDOFF);
94382     sqlite3VdbeChangeP5(v, BTREE_UNORDERED);
94383   }else{
94384     distinct = -1;
94385   }
94386 
94387   /* Aggregate and non-aggregate queries are handled differently */
94388   if( !isAgg && pGroupBy==0 ){
94389     /* This case is for non-aggregate queries
94390     ** Begin the database scan
94391     */
94392     pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, &pOrderBy, 0);
94393     if( pWInfo==0 ) goto select_end;
94394     if( pWInfo->nRowOut < p->nSelectRow ) p->nSelectRow = pWInfo->nRowOut;
94395 
94396     /* If sorting index that was created by a prior OP_OpenEphemeral
94397     ** instruction ended up not being needed, then change the OP_OpenEphemeral
94398     ** into an OP_Noop.
94399     */
94400     if( addrSortIndex>=0 && pOrderBy==0 ){
94401       sqlite3VdbeChangeToNoop(v, addrSortIndex, 1);
94402       p->addrOpenEphm[2] = -1;
94403     }
94404 
94405     /* Use the standard inner loop
94406     */
94407     assert(!isDistinct);
94408     selectInnerLoop(pParse, p, pEList, 0, 0, pOrderBy, -1, pDest,
94409                     pWInfo->iContinue, pWInfo->iBreak);
94410 
94411     /* End the database scan loop.
94412     */
94413     sqlite3WhereEnd(pWInfo);
94414   }else{
94415     /* This is the processing for aggregate queries */
94416     NameContext sNC;    /* Name context for processing aggregate information */
94417     int iAMem;          /* First Mem address for storing current GROUP BY */
94418     int iBMem;          /* First Mem address for previous GROUP BY */
94419     int iUseFlag;       /* Mem address holding flag indicating that at least
94420                         ** one row of the input to the aggregator has been
94421                         ** processed */
94422     int iAbortFlag;     /* Mem address which causes query abort if positive */
94423     int groupBySort;    /* Rows come from source in GROUP BY order */
94424     int addrEnd;        /* End of processing for this SELECT */
94425 
94426     /* Remove any and all aliases between the result set and the
94427     ** GROUP BY clause.
94428     */
94429     if( pGroupBy ){
94430       int k;                        /* Loop counter */
94431       struct ExprList_item *pItem;  /* For looping over expression in a list */
94432 
94433       for(k=p->pEList->nExpr, pItem=p->pEList->a; k>0; k--, pItem++){
94434         pItem->iAlias = 0;
94435       }
94436       for(k=pGroupBy->nExpr, pItem=pGroupBy->a; k>0; k--, pItem++){
94437         pItem->iAlias = 0;
94438       }
94439       if( p->nSelectRow>(double)100 ) p->nSelectRow = (double)100;
94440     }else{
94441       p->nSelectRow = (double)1;
94442     }
94443 
94444 
94445     /* Create a label to jump to when we want to abort the query */
94446     addrEnd = sqlite3VdbeMakeLabel(v);
94447 
94448     /* Convert TK_COLUMN nodes into TK_AGG_COLUMN and make entries in
94449     ** sAggInfo for all TK_AGG_FUNCTION nodes in expressions of the
94450     ** SELECT statement.
94451     */
94452     memset(&sNC, 0, sizeof(sNC));
94453     sNC.pParse = pParse;
94454     sNC.pSrcList = pTabList;
94455     sNC.pAggInfo = &sAggInfo;
94456     sAggInfo.nSortingColumn = pGroupBy ? pGroupBy->nExpr+1 : 0;
94457     sAggInfo.pGroupBy = pGroupBy;
94458     sqlite3ExprAnalyzeAggList(&sNC, pEList);
94459     sqlite3ExprAnalyzeAggList(&sNC, pOrderBy);
94460     if( pHaving ){
94461       sqlite3ExprAnalyzeAggregates(&sNC, pHaving);
94462     }
94463     sAggInfo.nAccumulator = sAggInfo.nColumn;
94464     for(i=0; i<sAggInfo.nFunc; i++){
94465       assert( !ExprHasProperty(sAggInfo.aFunc[i].pExpr, EP_xIsSelect) );
94466       sqlite3ExprAnalyzeAggList(&sNC, sAggInfo.aFunc[i].pExpr->x.pList);
94467     }
94468     if( db->mallocFailed ) goto select_end;
94469 
94470     /* Processing for aggregates with GROUP BY is very different and
94471     ** much more complex than aggregates without a GROUP BY.
94472     */
94473     if( pGroupBy ){
94474       KeyInfo *pKeyInfo;  /* Keying information for the group by clause */
94475       int j1;             /* A-vs-B comparision jump */
94476       int addrOutputRow;  /* Start of subroutine that outputs a result row */
94477       int regOutputRow;   /* Return address register for output subroutine */
94478       int addrSetAbort;   /* Set the abort flag and return */
94479       int addrTopOfLoop;  /* Top of the input loop */
94480       int addrSortingIdx; /* The OP_OpenEphemeral for the sorting index */
94481       int addrReset;      /* Subroutine for resetting the accumulator */
94482       int regReset;       /* Return address register for reset subroutine */
94483 
94484       /* If there is a GROUP BY clause we might need a sorting index to
94485       ** implement it.  Allocate that sorting index now.  If it turns out
94486       ** that we do not need it after all, the OpenEphemeral instruction
94487       ** will be converted into a Noop.
94488       */
94489       sAggInfo.sortingIdx = pParse->nTab++;
94490       pKeyInfo = keyInfoFromExprList(pParse, pGroupBy);
94491       addrSortingIdx = sqlite3VdbeAddOp4(v, OP_OpenEphemeral,
94492           sAggInfo.sortingIdx, sAggInfo.nSortingColumn,
94493           0, (char*)pKeyInfo, P4_KEYINFO_HANDOFF);
94494 
94495       /* Initialize memory locations used by GROUP BY aggregate processing
94496       */
94497       iUseFlag = ++pParse->nMem;
94498       iAbortFlag = ++pParse->nMem;
94499       regOutputRow = ++pParse->nMem;
94500       addrOutputRow = sqlite3VdbeMakeLabel(v);
94501       regReset = ++pParse->nMem;
94502       addrReset = sqlite3VdbeMakeLabel(v);
94503       iAMem = pParse->nMem + 1;
94504       pParse->nMem += pGroupBy->nExpr;
94505       iBMem = pParse->nMem + 1;
94506       pParse->nMem += pGroupBy->nExpr;
94507       sqlite3VdbeAddOp2(v, OP_Integer, 0, iAbortFlag);
94508       VdbeComment((v, "clear abort flag"));
94509       sqlite3VdbeAddOp2(v, OP_Integer, 0, iUseFlag);
94510       VdbeComment((v, "indicate accumulator empty"));
94511 
94512       /* Begin a loop that will extract all source rows in GROUP BY order.
94513       ** This might involve two separate loops with an OP_Sort in between, or
94514       ** it might be a single loop that uses an index to extract information
94515       ** in the right order to begin with.
94516       */
94517       sqlite3VdbeAddOp2(v, OP_Gosub, regReset, addrReset);
94518       pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, &pGroupBy, 0);
94519       if( pWInfo==0 ) goto select_end;
94520       if( pGroupBy==0 ){
94521         /* The optimizer is able to deliver rows in group by order so
94522         ** we do not have to sort.  The OP_OpenEphemeral table will be
94523         ** cancelled later because we still need to use the pKeyInfo
94524         */
94525         pGroupBy = p->pGroupBy;
94526         groupBySort = 0;
94527       }else{
94528         /* Rows are coming out in undetermined order.  We have to push
94529         ** each row into a sorting index, terminate the first loop,
94530         ** then loop over the sorting index in order to get the output
94531         ** in sorted order
94532         */
94533         int regBase;
94534         int regRecord;
94535         int nCol;
94536         int nGroupBy;
94537 
94538         explainTempTable(pParse,
94539             isDistinct && !(p->selFlags&SF_Distinct)?"DISTINCT":"GROUP BY");
94540 
94541         groupBySort = 1;
94542         nGroupBy = pGroupBy->nExpr;
94543         nCol = nGroupBy + 1;
94544         j = nGroupBy+1;
94545         for(i=0; i<sAggInfo.nColumn; i++){
94546           if( sAggInfo.aCol[i].iSorterColumn>=j ){
94547             nCol++;
94548             j++;
94549           }
94550         }
94551         regBase = sqlite3GetTempRange(pParse, nCol);
94552         sqlite3ExprCacheClear(pParse);
94553         sqlite3ExprCodeExprList(pParse, pGroupBy, regBase, 0);
94554         sqlite3VdbeAddOp2(v, OP_Sequence, sAggInfo.sortingIdx,regBase+nGroupBy);
94555         j = nGroupBy+1;
94556         for(i=0; i<sAggInfo.nColumn; i++){
94557           struct AggInfo_col *pCol = &sAggInfo.aCol[i];
94558           if( pCol->iSorterColumn>=j ){
94559             int r1 = j + regBase;
94560             int r2;
94561 
94562             r2 = sqlite3ExprCodeGetColumn(pParse,
94563                                pCol->pTab, pCol->iColumn, pCol->iTable, r1);
94564             if( r1!=r2 ){
94565               sqlite3VdbeAddOp2(v, OP_SCopy, r2, r1);
94566             }
94567             j++;
94568           }
94569         }
94570         regRecord = sqlite3GetTempReg(pParse);
94571         sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol, regRecord);
94572         sqlite3VdbeAddOp2(v, OP_IdxInsert, sAggInfo.sortingIdx, regRecord);
94573         sqlite3ReleaseTempReg(pParse, regRecord);
94574         sqlite3ReleaseTempRange(pParse, regBase, nCol);
94575         sqlite3WhereEnd(pWInfo);
94576         sqlite3VdbeAddOp2(v, OP_Sort, sAggInfo.sortingIdx, addrEnd);
94577         VdbeComment((v, "GROUP BY sort"));
94578         sAggInfo.useSortingIdx = 1;
94579         sqlite3ExprCacheClear(pParse);
94580       }
94581 
94582       /* Evaluate the current GROUP BY terms and store in b0, b1, b2...
94583       ** (b0 is memory location iBMem+0, b1 is iBMem+1, and so forth)
94584       ** Then compare the current GROUP BY terms against the GROUP BY terms
94585       ** from the previous row currently stored in a0, a1, a2...
94586       */
94587       addrTopOfLoop = sqlite3VdbeCurrentAddr(v);
94588       sqlite3ExprCacheClear(pParse);
94589       for(j=0; j<pGroupBy->nExpr; j++){
94590         if( groupBySort ){
94591           sqlite3VdbeAddOp3(v, OP_Column, sAggInfo.sortingIdx, j, iBMem+j);
94592         }else{
94593           sAggInfo.directMode = 1;
94594           sqlite3ExprCode(pParse, pGroupBy->a[j].pExpr, iBMem+j);
94595         }
94596       }
94597       sqlite3VdbeAddOp4(v, OP_Compare, iAMem, iBMem, pGroupBy->nExpr,
94598                           (char*)pKeyInfo, P4_KEYINFO);
94599       j1 = sqlite3VdbeCurrentAddr(v);
94600       sqlite3VdbeAddOp3(v, OP_Jump, j1+1, 0, j1+1);
94601 
94602       /* Generate code that runs whenever the GROUP BY changes.
94603       ** Changes in the GROUP BY are detected by the previous code
94604       ** block.  If there were no changes, this block is skipped.
94605       **
94606       ** This code copies current group by terms in b0,b1,b2,...
94607       ** over to a0,a1,a2.  It then calls the output subroutine
94608       ** and resets the aggregate accumulator registers in preparation
94609       ** for the next GROUP BY batch.
94610       */
94611       sqlite3ExprCodeMove(pParse, iBMem, iAMem, pGroupBy->nExpr);
94612       sqlite3VdbeAddOp2(v, OP_Gosub, regOutputRow, addrOutputRow);
94613       VdbeComment((v, "output one row"));
94614       sqlite3VdbeAddOp2(v, OP_IfPos, iAbortFlag, addrEnd);
94615       VdbeComment((v, "check abort flag"));
94616       sqlite3VdbeAddOp2(v, OP_Gosub, regReset, addrReset);
94617       VdbeComment((v, "reset accumulator"));
94618 
94619       /* Update the aggregate accumulators based on the content of
94620       ** the current row
94621       */
94622       sqlite3VdbeJumpHere(v, j1);
94623       updateAccumulator(pParse, &sAggInfo);
94624       sqlite3VdbeAddOp2(v, OP_Integer, 1, iUseFlag);
94625       VdbeComment((v, "indicate data in accumulator"));
94626 
94627       /* End of the loop
94628       */
94629       if( groupBySort ){
94630         sqlite3VdbeAddOp2(v, OP_Next, sAggInfo.sortingIdx, addrTopOfLoop);
94631       }else{
94632         sqlite3WhereEnd(pWInfo);
94633         sqlite3VdbeChangeToNoop(v, addrSortingIdx, 1);
94634       }
94635 
94636       /* Output the final row of result
94637       */
94638       sqlite3VdbeAddOp2(v, OP_Gosub, regOutputRow, addrOutputRow);
94639       VdbeComment((v, "output final row"));
94640 
94641       /* Jump over the subroutines
94642       */
94643       sqlite3VdbeAddOp2(v, OP_Goto, 0, addrEnd);
94644 
94645       /* Generate a subroutine that outputs a single row of the result
94646       ** set.  This subroutine first looks at the iUseFlag.  If iUseFlag
94647       ** is less than or equal to zero, the subroutine is a no-op.  If
94648       ** the processing calls for the query to abort, this subroutine
94649       ** increments the iAbortFlag memory location before returning in
94650       ** order to signal the caller to abort.
94651       */
94652       addrSetAbort = sqlite3VdbeCurrentAddr(v);
94653       sqlite3VdbeAddOp2(v, OP_Integer, 1, iAbortFlag);
94654       VdbeComment((v, "set abort flag"));
94655       sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
94656       sqlite3VdbeResolveLabel(v, addrOutputRow);
94657       addrOutputRow = sqlite3VdbeCurrentAddr(v);
94658       sqlite3VdbeAddOp2(v, OP_IfPos, iUseFlag, addrOutputRow+2);
94659       VdbeComment((v, "Groupby result generator entry point"));
94660       sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
94661       finalizeAggFunctions(pParse, &sAggInfo);
94662       sqlite3ExprIfFalse(pParse, pHaving, addrOutputRow+1, SQLITE_JUMPIFNULL);
94663       selectInnerLoop(pParse, p, p->pEList, 0, 0, pOrderBy,
94664                       distinct, pDest,
94665                       addrOutputRow+1, addrSetAbort);
94666       sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
94667       VdbeComment((v, "end groupby result generator"));
94668 
94669       /* Generate a subroutine that will reset the group-by accumulator
94670       */
94671       sqlite3VdbeResolveLabel(v, addrReset);
94672       resetAccumulator(pParse, &sAggInfo);
94673       sqlite3VdbeAddOp1(v, OP_Return, regReset);
94674 
94675     } /* endif pGroupBy.  Begin aggregate queries without GROUP BY: */
94676     else {
94677       ExprList *pDel = 0;
94678 #ifndef SQLITE_OMIT_BTREECOUNT
94679       Table *pTab;
94680       if( (pTab = isSimpleCount(p, &sAggInfo))!=0 ){
94681         /* If isSimpleCount() returns a pointer to a Table structure, then
94682         ** the SQL statement is of the form:
94683         **
94684         **   SELECT count(*) FROM <tbl>
94685         **
94686         ** where the Table structure returned represents table <tbl>.
94687         **
94688         ** This statement is so common that it is optimized specially. The
94689         ** OP_Count instruction is executed either on the intkey table that
94690         ** contains the data for table <tbl> or on one of its indexes. It
94691         ** is better to execute the op on an index, as indexes are almost
94692         ** always spread across less pages than their corresponding tables.
94693         */
94694         const int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
94695         const int iCsr = pParse->nTab++;     /* Cursor to scan b-tree */
94696         Index *pIdx;                         /* Iterator variable */
94697         KeyInfo *pKeyInfo = 0;               /* Keyinfo for scanned index */
94698         Index *pBest = 0;                    /* Best index found so far */
94699         int iRoot = pTab->tnum;              /* Root page of scanned b-tree */
94700 
94701         sqlite3CodeVerifySchema(pParse, iDb);
94702         sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
94703 
94704         /* Search for the index that has the least amount of columns. If
94705         ** there is such an index, and it has less columns than the table
94706         ** does, then we can assume that it consumes less space on disk and
94707         ** will therefore be cheaper to scan to determine the query result.
94708         ** In this case set iRoot to the root page number of the index b-tree
94709         ** and pKeyInfo to the KeyInfo structure required to navigate the
94710         ** index.
94711         **
94712         ** (2011-04-15) Do not do a full scan of an unordered index.
94713         **
94714         ** In practice the KeyInfo structure will not be used. It is only
94715         ** passed to keep OP_OpenRead happy.
94716         */
94717         for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
94718           if( pIdx->bUnordered==0 && (!pBest || pIdx->nColumn<pBest->nColumn) ){
94719             pBest = pIdx;
94720           }
94721         }
94722         if( pBest && pBest->nColumn<pTab->nCol ){
94723           iRoot = pBest->tnum;
94724           pKeyInfo = sqlite3IndexKeyinfo(pParse, pBest);
94725         }
94726 
94727         /* Open a read-only cursor, execute the OP_Count, close the cursor. */
94728         sqlite3VdbeAddOp3(v, OP_OpenRead, iCsr, iRoot, iDb);
94729         if( pKeyInfo ){
94730           sqlite3VdbeChangeP4(v, -1, (char *)pKeyInfo, P4_KEYINFO_HANDOFF);
94731         }
94732         sqlite3VdbeAddOp2(v, OP_Count, iCsr, sAggInfo.aFunc[0].iMem);
94733         sqlite3VdbeAddOp1(v, OP_Close, iCsr);
94734         explainSimpleCount(pParse, pTab, pBest);
94735       }else
94736 #endif /* SQLITE_OMIT_BTREECOUNT */
94737       {
94738         /* Check if the query is of one of the following forms:
94739         **
94740         **   SELECT min(x) FROM ...
94741         **   SELECT max(x) FROM ...
94742         **
94743         ** If it is, then ask the code in where.c to attempt to sort results
94744         ** as if there was an "ORDER ON x" or "ORDER ON x DESC" clause.
94745         ** If where.c is able to produce results sorted in this order, then
94746         ** add vdbe code to break out of the processing loop after the
94747         ** first iteration (since the first iteration of the loop is
94748         ** guaranteed to operate on the row with the minimum or maximum
94749         ** value of x, the only row required).
94750         **
94751         ** A special flag must be passed to sqlite3WhereBegin() to slightly
94752         ** modify behaviour as follows:
94753         **
94754         **   + If the query is a "SELECT min(x)", then the loop coded by
94755         **     where.c should not iterate over any values with a NULL value
94756         **     for x.
94757         **
94758         **   + The optimizer code in where.c (the thing that decides which
94759         **     index or indices to use) should place a different priority on
94760         **     satisfying the 'ORDER BY' clause than it does in other cases.
94761         **     Refer to code and comments in where.c for details.
94762         */
94763         ExprList *pMinMax = 0;
94764         u8 flag = minMaxQuery(p);
94765         if( flag ){
94766           assert( !ExprHasProperty(p->pEList->a[0].pExpr, EP_xIsSelect) );
94767           pMinMax = sqlite3ExprListDup(db, p->pEList->a[0].pExpr->x.pList,0);
94768           pDel = pMinMax;
94769           if( pMinMax && !db->mallocFailed ){
94770             pMinMax->a[0].sortOrder = flag!=WHERE_ORDERBY_MIN ?1:0;
94771             pMinMax->a[0].pExpr->op = TK_COLUMN;
94772           }
94773         }
94774 
94775         /* This case runs if the aggregate has no GROUP BY clause.  The
94776         ** processing is much simpler since there is only a single row
94777         ** of output.
94778         */
94779         resetAccumulator(pParse, &sAggInfo);
94780         pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, &pMinMax, flag);
94781         if( pWInfo==0 ){
94782           sqlite3ExprListDelete(db, pDel);
94783           goto select_end;
94784         }
94785         updateAccumulator(pParse, &sAggInfo);
94786         if( !pMinMax && flag ){
94787           sqlite3VdbeAddOp2(v, OP_Goto, 0, pWInfo->iBreak);
94788           VdbeComment((v, "%s() by index",
94789                 (flag==WHERE_ORDERBY_MIN?"min":"max")));
94790         }
94791         sqlite3WhereEnd(pWInfo);
94792         finalizeAggFunctions(pParse, &sAggInfo);
94793       }
94794 
94795       pOrderBy = 0;
94796       sqlite3ExprIfFalse(pParse, pHaving, addrEnd, SQLITE_JUMPIFNULL);
94797       selectInnerLoop(pParse, p, p->pEList, 0, 0, 0, -1,
94798                       pDest, addrEnd, addrEnd);
94799       sqlite3ExprListDelete(db, pDel);
94800     }
94801     sqlite3VdbeResolveLabel(v, addrEnd);
94802 
94803   } /* endif aggregate query */
94804 
94805   if( distinct>=0 ){
94806     explainTempTable(pParse, "DISTINCT");
94807   }
94808 
94809   /* If there is an ORDER BY clause, then we need to sort the results
94810   ** and send them to the callback one by one.
94811   */
94812   if( pOrderBy ){
94813     explainTempTable(pParse, "ORDER BY");
94814     generateSortTail(pParse, p, v, pEList->nExpr, pDest);
94815   }
94816 
94817   /* Jump here to skip this query
94818   */
94819   sqlite3VdbeResolveLabel(v, iEnd);
94820 
94821   /* The SELECT was successfully coded.   Set the return code to 0
94822   ** to indicate no errors.
94823   */
94824   rc = 0;
94825 
94826   /* Control jumps to here if an error is encountered above, or upon
94827   ** successful coding of the SELECT.
94828   */
94829 select_end:
94830   explainSetInteger(pParse->iSelectId, iRestoreSelectId);
94831 
94832   /* Identify column names if results of the SELECT are to be output.
94833   */
94834   if( rc==SQLITE_OK && pDest->eDest==SRT_Output ){
94835     generateColumnNames(pParse, pTabList, pEList);
94836   }
94837 
94838   sqlite3DbFree(db, sAggInfo.aCol);
94839   sqlite3DbFree(db, sAggInfo.aFunc);
94840   return rc;
94841 }
94842 
94843 #if defined(SQLITE_DEBUG)
94844 /*
94845 *******************************************************************************
94846 ** The following code is used for testing and debugging only.  The code
94847 ** that follows does not appear in normal builds.
94848 **
94849 ** These routines are used to print out the content of all or part of a
94850 ** parse structures such as Select or Expr.  Such printouts are useful
94851 ** for helping to understand what is happening inside the code generator
94852 ** during the execution of complex SELECT statements.
94853 **
94854 ** These routine are not called anywhere from within the normal
94855 ** code base.  Then are intended to be called from within the debugger
94856 ** or from temporary "printf" statements inserted for debugging.
94857 */
94858 SQLITE_PRIVATE void sqlite3PrintExpr(Expr *p){
94859   if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
94860     sqlite3DebugPrintf("(%s", p->u.zToken);
94861   }else{
94862     sqlite3DebugPrintf("(%d", p->op);
94863   }
94864   if( p->pLeft ){
94865     sqlite3DebugPrintf(" ");
94866     sqlite3PrintExpr(p->pLeft);
94867   }
94868   if( p->pRight ){
94869     sqlite3DebugPrintf(" ");
94870     sqlite3PrintExpr(p->pRight);
94871   }
94872   sqlite3DebugPrintf(")");
94873 }
94874 SQLITE_PRIVATE void sqlite3PrintExprList(ExprList *pList){
94875   int i;
94876   for(i=0; i<pList->nExpr; i++){
94877     sqlite3PrintExpr(pList->a[i].pExpr);
94878     if( i<pList->nExpr-1 ){
94879       sqlite3DebugPrintf(", ");
94880     }
94881   }
94882 }
94883 SQLITE_PRIVATE void sqlite3PrintSelect(Select *p, int indent){
94884   sqlite3DebugPrintf("%*sSELECT(%p) ", indent, "", p);
94885   sqlite3PrintExprList(p->pEList);
94886   sqlite3DebugPrintf("\n");
94887   if( p->pSrc ){
94888     char *zPrefix;
94889     int i;
94890     zPrefix = "FROM";
94891     for(i=0; i<p->pSrc->nSrc; i++){
94892       struct SrcList_item *pItem = &p->pSrc->a[i];
94893       sqlite3DebugPrintf("%*s ", indent+6, zPrefix);
94894       zPrefix = "";
94895       if( pItem->pSelect ){
94896         sqlite3DebugPrintf("(\n");
94897         sqlite3PrintSelect(pItem->pSelect, indent+10);
94898         sqlite3DebugPrintf("%*s)", indent+8, "");
94899       }else if( pItem->zName ){
94900         sqlite3DebugPrintf("%s", pItem->zName);
94901       }
94902       if( pItem->pTab ){
94903         sqlite3DebugPrintf("(table: %s)", pItem->pTab->zName);
94904       }
94905       if( pItem->zAlias ){
94906         sqlite3DebugPrintf(" AS %s", pItem->zAlias);
94907       }
94908       if( i<p->pSrc->nSrc-1 ){
94909         sqlite3DebugPrintf(",");
94910       }
94911       sqlite3DebugPrintf("\n");
94912     }
94913   }
94914   if( p->pWhere ){
94915     sqlite3DebugPrintf("%*s WHERE ", indent, "");
94916     sqlite3PrintExpr(p->pWhere);
94917     sqlite3DebugPrintf("\n");
94918   }
94919   if( p->pGroupBy ){
94920     sqlite3DebugPrintf("%*s GROUP BY ", indent, "");
94921     sqlite3PrintExprList(p->pGroupBy);
94922     sqlite3DebugPrintf("\n");
94923   }
94924   if( p->pHaving ){
94925     sqlite3DebugPrintf("%*s HAVING ", indent, "");
94926     sqlite3PrintExpr(p->pHaving);
94927     sqlite3DebugPrintf("\n");
94928   }
94929   if( p->pOrderBy ){
94930     sqlite3DebugPrintf("%*s ORDER BY ", indent, "");
94931     sqlite3PrintExprList(p->pOrderBy);
94932     sqlite3DebugPrintf("\n");
94933   }
94934 }
94935 /* End of the structure debug printing code
94936 *****************************************************************************/
94937 #endif /* defined(SQLITE_TEST) || defined(SQLITE_DEBUG) */
94938 
94939 /************** End of select.c **********************************************/
94940 /************** Begin file table.c *******************************************/
94941 /*
94942 ** 2001 September 15
94943 **
94944 ** The author disclaims copyright to this source code.  In place of
94945 ** a legal notice, here is a blessing:
94946 **
94947 **    May you do good and not evil.
94948 **    May you find forgiveness for yourself and forgive others.
94949 **    May you share freely, never taking more than you give.
94950 **
94951 *************************************************************************
94952 ** This file contains the sqlite3_get_table() and sqlite3_free_table()
94953 ** interface routines.  These are just wrappers around the main
94954 ** interface routine of sqlite3_exec().
94955 **
94956 ** These routines are in a separate files so that they will not be linked
94957 ** if they are not used.
94958 */
94959 
94960 #ifndef SQLITE_OMIT_GET_TABLE
94961 
94962 /*
94963 ** This structure is used to pass data from sqlite3_get_table() through
94964 ** to the callback function is uses to build the result.
94965 */
94966 typedef struct TabResult {
94967   char **azResult;   /* Accumulated output */
94968   char *zErrMsg;     /* Error message text, if an error occurs */
94969   int nAlloc;        /* Slots allocated for azResult[] */
94970   int nRow;          /* Number of rows in the result */
94971   int nColumn;       /* Number of columns in the result */
94972   int nData;         /* Slots used in azResult[].  (nRow+1)*nColumn */
94973   int rc;            /* Return code from sqlite3_exec() */
94974 } TabResult;
94975 
94976 /*
94977 ** This routine is called once for each row in the result table.  Its job
94978 ** is to fill in the TabResult structure appropriately, allocating new
94979 ** memory as necessary.
94980 */
94981 static int sqlite3_get_table_cb(void *pArg, int nCol, char **argv, char **colv){
94982   TabResult *p = (TabResult*)pArg;  /* Result accumulator */
94983   int need;                         /* Slots needed in p->azResult[] */
94984   int i;                            /* Loop counter */
94985   char *z;                          /* A single column of result */
94986 
94987   /* Make sure there is enough space in p->azResult to hold everything
94988   ** we need to remember from this invocation of the callback.
94989   */
94990   if( p->nRow==0 && argv!=0 ){
94991     need = nCol*2;
94992   }else{
94993     need = nCol;
94994   }
94995   if( p->nData + need > p->nAlloc ){
94996     char **azNew;
94997     p->nAlloc = p->nAlloc*2 + need;
94998     azNew = sqlite3_realloc( p->azResult, sizeof(char*)*p->nAlloc );
94999     if( azNew==0 ) goto malloc_failed;
95000     p->azResult = azNew;
95001   }
95002 
95003   /* If this is the first row, then generate an extra row containing
95004   ** the names of all columns.
95005   */
95006   if( p->nRow==0 ){
95007     p->nColumn = nCol;
95008     for(i=0; i<nCol; i++){
95009       z = sqlite3_mprintf("%s", colv[i]);
95010       if( z==0 ) goto malloc_failed;
95011       p->azResult[p->nData++] = z;
95012     }
95013   }else if( p->nColumn!=nCol ){
95014     sqlite3_free(p->zErrMsg);
95015     p->zErrMsg = sqlite3_mprintf(
95016        "sqlite3_get_table() called with two or more incompatible queries"
95017     );
95018     p->rc = SQLITE_ERROR;
95019     return 1;
95020   }
95021 
95022   /* Copy over the row data
95023   */
95024   if( argv!=0 ){
95025     for(i=0; i<nCol; i++){
95026       if( argv[i]==0 ){
95027         z = 0;
95028       }else{
95029         int n = sqlite3Strlen30(argv[i])+1;
95030         z = sqlite3_malloc( n );
95031         if( z==0 ) goto malloc_failed;
95032         memcpy(z, argv[i], n);
95033       }
95034       p->azResult[p->nData++] = z;
95035     }
95036     p->nRow++;
95037   }
95038   return 0;
95039 
95040 malloc_failed:
95041   p->rc = SQLITE_NOMEM;
95042   return 1;
95043 }
95044 
95045 /*
95046 ** Query the database.  But instead of invoking a callback for each row,
95047 ** malloc() for space to hold the result and return the entire results
95048 ** at the conclusion of the call.
95049 **
95050 ** The result that is written to ***pazResult is held in memory obtained
95051 ** from malloc().  But the caller cannot free this memory directly.
95052 ** Instead, the entire table should be passed to sqlite3_free_table() when
95053 ** the calling procedure is finished using it.
95054 */
95055 SQLITE_API int sqlite3_get_table(
95056   sqlite3 *db,                /* The database on which the SQL executes */
95057   const char *zSql,           /* The SQL to be executed */
95058   char ***pazResult,          /* Write the result table here */
95059   int *pnRow,                 /* Write the number of rows in the result here */
95060   int *pnColumn,              /* Write the number of columns of result here */
95061   char **pzErrMsg             /* Write error messages here */
95062 ){
95063   int rc;
95064   TabResult res;
95065 
95066   *pazResult = 0;
95067   if( pnColumn ) *pnColumn = 0;
95068   if( pnRow ) *pnRow = 0;
95069   if( pzErrMsg ) *pzErrMsg = 0;
95070   res.zErrMsg = 0;
95071   res.nRow = 0;
95072   res.nColumn = 0;
95073   res.nData = 1;
95074   res.nAlloc = 20;
95075   res.rc = SQLITE_OK;
95076   res.azResult = sqlite3_malloc(sizeof(char*)*res.nAlloc );
95077   if( res.azResult==0 ){
95078      db->errCode = SQLITE_NOMEM;
95079      return SQLITE_NOMEM;
95080   }
95081   res.azResult[0] = 0;
95082   rc = sqlite3_exec(db, zSql, sqlite3_get_table_cb, &res, pzErrMsg);
95083   assert( sizeof(res.azResult[0])>= sizeof(res.nData) );
95084   res.azResult[0] = SQLITE_INT_TO_PTR(res.nData);
95085   if( (rc&0xff)==SQLITE_ABORT ){
95086     sqlite3_free_table(&res.azResult[1]);
95087     if( res.zErrMsg ){
95088       if( pzErrMsg ){
95089         sqlite3_free(*pzErrMsg);
95090         *pzErrMsg = sqlite3_mprintf("%s",res.zErrMsg);
95091       }
95092       sqlite3_free(res.zErrMsg);
95093     }
95094     db->errCode = res.rc;  /* Assume 32-bit assignment is atomic */
95095     return res.rc;
95096   }
95097   sqlite3_free(res.zErrMsg);
95098   if( rc!=SQLITE_OK ){
95099     sqlite3_free_table(&res.azResult[1]);
95100     return rc;
95101   }
95102   if( res.nAlloc>res.nData ){
95103     char **azNew;
95104     azNew = sqlite3_realloc( res.azResult, sizeof(char*)*res.nData );
95105     if( azNew==0 ){
95106       sqlite3_free_table(&res.azResult[1]);
95107       db->errCode = SQLITE_NOMEM;
95108       return SQLITE_NOMEM;
95109     }
95110     res.azResult = azNew;
95111   }
95112   *pazResult = &res.azResult[1];
95113   if( pnColumn ) *pnColumn = res.nColumn;
95114   if( pnRow ) *pnRow = res.nRow;
95115   return rc;
95116 }
95117 
95118 /*
95119 ** This routine frees the space the sqlite3_get_table() malloced.
95120 */
95121 SQLITE_API void sqlite3_free_table(
95122   char **azResult            /* Result returned from from sqlite3_get_table() */
95123 ){
95124   if( azResult ){
95125     int i, n;
95126     azResult--;
95127     assert( azResult!=0 );
95128     n = SQLITE_PTR_TO_INT(azResult[0]);
95129     for(i=1; i<n; i++){ if( azResult[i] ) sqlite3_free(azResult[i]); }
95130     sqlite3_free(azResult);
95131   }
95132 }
95133 
95134 #endif /* SQLITE_OMIT_GET_TABLE */
95135 
95136 /************** End of table.c ***********************************************/
95137 /************** Begin file trigger.c *****************************************/
95138 /*
95139 **
95140 ** The author disclaims copyright to this source code.  In place of
95141 ** a legal notice, here is a blessing:
95142 **
95143 **    May you do good and not evil.
95144 **    May you find forgiveness for yourself and forgive others.
95145 **    May you share freely, never taking more than you give.
95146 **
95147 *************************************************************************
95148 ** This file contains the implementation for TRIGGERs
95149 */
95150 
95151 #ifndef SQLITE_OMIT_TRIGGER
95152 /*
95153 ** Delete a linked list of TriggerStep structures.
95154 */
95155 SQLITE_PRIVATE void sqlite3DeleteTriggerStep(sqlite3 *db, TriggerStep *pTriggerStep){
95156   while( pTriggerStep ){
95157     TriggerStep * pTmp = pTriggerStep;
95158     pTriggerStep = pTriggerStep->pNext;
95159 
95160     sqlite3ExprDelete(db, pTmp->pWhere);
95161     sqlite3ExprListDelete(db, pTmp->pExprList);
95162     sqlite3SelectDelete(db, pTmp->pSelect);
95163     sqlite3IdListDelete(db, pTmp->pIdList);
95164 
95165     sqlite3DbFree(db, pTmp);
95166   }
95167 }
95168 
95169 /*
95170 ** Given table pTab, return a list of all the triggers attached to
95171 ** the table. The list is connected by Trigger.pNext pointers.
95172 **
95173 ** All of the triggers on pTab that are in the same database as pTab
95174 ** are already attached to pTab->pTrigger.  But there might be additional
95175 ** triggers on pTab in the TEMP schema.  This routine prepends all
95176 ** TEMP triggers on pTab to the beginning of the pTab->pTrigger list
95177 ** and returns the combined list.
95178 **
95179 ** To state it another way:  This routine returns a list of all triggers
95180 ** that fire off of pTab.  The list will include any TEMP triggers on
95181 ** pTab as well as the triggers lised in pTab->pTrigger.
95182 */
95183 SQLITE_PRIVATE Trigger *sqlite3TriggerList(Parse *pParse, Table *pTab){
95184   Schema * const pTmpSchema = pParse->db->aDb[1].pSchema;
95185   Trigger *pList = 0;                  /* List of triggers to return */
95186 
95187   if( pParse->disableTriggers ){
95188     return 0;
95189   }
95190 
95191   if( pTmpSchema!=pTab->pSchema ){
95192     HashElem *p;
95193     assert( sqlite3SchemaMutexHeld(pParse->db, 0, pTmpSchema) );
95194     for(p=sqliteHashFirst(&pTmpSchema->trigHash); p; p=sqliteHashNext(p)){
95195       Trigger *pTrig = (Trigger *)sqliteHashData(p);
95196       if( pTrig->pTabSchema==pTab->pSchema
95197        && 0==sqlite3StrICmp(pTrig->table, pTab->zName)
95198       ){
95199         pTrig->pNext = (pList ? pList : pTab->pTrigger);
95200         pList = pTrig;
95201       }
95202     }
95203   }
95204 
95205   return (pList ? pList : pTab->pTrigger);
95206 }
95207 
95208 /*
95209 ** This is called by the parser when it sees a CREATE TRIGGER statement
95210 ** up to the point of the BEGIN before the trigger actions.  A Trigger
95211 ** structure is generated based on the information available and stored
95212 ** in pParse->pNewTrigger.  After the trigger actions have been parsed, the
95213 ** sqlite3FinishTrigger() function is called to complete the trigger
95214 ** construction process.
95215 */
95216 SQLITE_PRIVATE void sqlite3BeginTrigger(
95217   Parse *pParse,      /* The parse context of the CREATE TRIGGER statement */
95218   Token *pName1,      /* The name of the trigger */
95219   Token *pName2,      /* The name of the trigger */
95220   int tr_tm,          /* One of TK_BEFORE, TK_AFTER, TK_INSTEAD */
95221   int op,             /* One of TK_INSERT, TK_UPDATE, TK_DELETE */
95222   IdList *pColumns,   /* column list if this is an UPDATE OF trigger */
95223   SrcList *pTableName,/* The name of the table/view the trigger applies to */
95224   Expr *pWhen,        /* WHEN clause */
95225   int isTemp,         /* True if the TEMPORARY keyword is present */
95226   int noErr           /* Suppress errors if the trigger already exists */
95227 ){
95228   Trigger *pTrigger = 0;  /* The new trigger */
95229   Table *pTab;            /* Table that the trigger fires off of */
95230   char *zName = 0;        /* Name of the trigger */
95231   sqlite3 *db = pParse->db;  /* The database connection */
95232   int iDb;                /* The database to store the trigger in */
95233   Token *pName;           /* The unqualified db name */
95234   DbFixer sFix;           /* State vector for the DB fixer */
95235   int iTabDb;             /* Index of the database holding pTab */
95236 
95237   assert( pName1!=0 );   /* pName1->z might be NULL, but not pName1 itself */
95238   assert( pName2!=0 );
95239   assert( op==TK_INSERT || op==TK_UPDATE || op==TK_DELETE );
95240   assert( op>0 && op<0xff );
95241   if( isTemp ){
95242     /* If TEMP was specified, then the trigger name may not be qualified. */
95243     if( pName2->n>0 ){
95244       sqlite3ErrorMsg(pParse, "temporary trigger may not have qualified name");
95245       goto trigger_cleanup;
95246     }
95247     iDb = 1;
95248     pName = pName1;
95249   }else{
95250     /* Figure out the db that the the trigger will be created in */
95251     iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
95252     if( iDb<0 ){
95253       goto trigger_cleanup;
95254     }
95255   }
95256 
95257   /* If the trigger name was unqualified, and the table is a temp table,
95258   ** then set iDb to 1 to create the trigger in the temporary database.
95259   ** If sqlite3SrcListLookup() returns 0, indicating the table does not
95260   ** exist, the error is caught by the block below.
95261   */
95262   if( !pTableName || db->mallocFailed ){
95263     goto trigger_cleanup;
95264   }
95265   pTab = sqlite3SrcListLookup(pParse, pTableName);
95266   if( db->init.busy==0 && pName2->n==0 && pTab
95267         && pTab->pSchema==db->aDb[1].pSchema ){
95268     iDb = 1;
95269   }
95270 
95271   /* Ensure the table name matches database name and that the table exists */
95272   if( db->mallocFailed ) goto trigger_cleanup;
95273   assert( pTableName->nSrc==1 );
95274   if( sqlite3FixInit(&sFix, pParse, iDb, "trigger", pName) &&
95275       sqlite3FixSrcList(&sFix, pTableName) ){
95276     goto trigger_cleanup;
95277   }
95278   pTab = sqlite3SrcListLookup(pParse, pTableName);
95279   if( !pTab ){
95280     /* The table does not exist. */
95281     if( db->init.iDb==1 ){
95282       /* Ticket #3810.
95283       ** Normally, whenever a table is dropped, all associated triggers are
95284       ** dropped too.  But if a TEMP trigger is created on a non-TEMP table
95285       ** and the table is dropped by a different database connection, the
95286       ** trigger is not visible to the database connection that does the
95287       ** drop so the trigger cannot be dropped.  This results in an
95288       ** "orphaned trigger" - a trigger whose associated table is missing.
95289       */
95290       db->init.orphanTrigger = 1;
95291     }
95292     goto trigger_cleanup;
95293   }
95294   if( IsVirtual(pTab) ){
95295     sqlite3ErrorMsg(pParse, "cannot create triggers on virtual tables");
95296     goto trigger_cleanup;
95297   }
95298 
95299   /* Check that the trigger name is not reserved and that no trigger of the
95300   ** specified name exists */
95301   zName = sqlite3NameFromToken(db, pName);
95302   if( !zName || SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
95303     goto trigger_cleanup;
95304   }
95305   assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
95306   if( sqlite3HashFind(&(db->aDb[iDb].pSchema->trigHash),
95307                       zName, sqlite3Strlen30(zName)) ){
95308     if( !noErr ){
95309       sqlite3ErrorMsg(pParse, "trigger %T already exists", pName);
95310     }else{
95311       assert( !db->init.busy );
95312       sqlite3CodeVerifySchema(pParse, iDb);
95313     }
95314     goto trigger_cleanup;
95315   }
95316 
95317   /* Do not create a trigger on a system table */
95318   if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
95319     sqlite3ErrorMsg(pParse, "cannot create trigger on system table");
95320     pParse->nErr++;
95321     goto trigger_cleanup;
95322   }
95323 
95324   /* INSTEAD of triggers are only for views and views only support INSTEAD
95325   ** of triggers.
95326   */
95327   if( pTab->pSelect && tr_tm!=TK_INSTEAD ){
95328     sqlite3ErrorMsg(pParse, "cannot create %s trigger on view: %S",
95329         (tr_tm == TK_BEFORE)?"BEFORE":"AFTER", pTableName, 0);
95330     goto trigger_cleanup;
95331   }
95332   if( !pTab->pSelect && tr_tm==TK_INSTEAD ){
95333     sqlite3ErrorMsg(pParse, "cannot create INSTEAD OF"
95334         " trigger on table: %S", pTableName, 0);
95335     goto trigger_cleanup;
95336   }
95337   iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
95338 
95339 #ifndef SQLITE_OMIT_AUTHORIZATION
95340   {
95341     int code = SQLITE_CREATE_TRIGGER;
95342     const char *zDb = db->aDb[iTabDb].zName;
95343     const char *zDbTrig = isTemp ? db->aDb[1].zName : zDb;
95344     if( iTabDb==1 || isTemp ) code = SQLITE_CREATE_TEMP_TRIGGER;
95345     if( sqlite3AuthCheck(pParse, code, zName, pTab->zName, zDbTrig) ){
95346       goto trigger_cleanup;
95347     }
95348     if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iTabDb),0,zDb)){
95349       goto trigger_cleanup;
95350     }
95351   }
95352 #endif
95353 
95354   /* INSTEAD OF triggers can only appear on views and BEFORE triggers
95355   ** cannot appear on views.  So we might as well translate every
95356   ** INSTEAD OF trigger into a BEFORE trigger.  It simplifies code
95357   ** elsewhere.
95358   */
95359   if (tr_tm == TK_INSTEAD){
95360     tr_tm = TK_BEFORE;
95361   }
95362 
95363   /* Build the Trigger object */
95364   pTrigger = (Trigger*)sqlite3DbMallocZero(db, sizeof(Trigger));
95365   if( pTrigger==0 ) goto trigger_cleanup;
95366   pTrigger->zName = zName;
95367   zName = 0;
95368   pTrigger->table = sqlite3DbStrDup(db, pTableName->a[0].zName);
95369   pTrigger->pSchema = db->aDb[iDb].pSchema;
95370   pTrigger->pTabSchema = pTab->pSchema;
95371   pTrigger->op = (u8)op;
95372   pTrigger->tr_tm = tr_tm==TK_BEFORE ? TRIGGER_BEFORE : TRIGGER_AFTER;
95373   pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
95374   pTrigger->pColumns = sqlite3IdListDup(db, pColumns);
95375   assert( pParse->pNewTrigger==0 );
95376   pParse->pNewTrigger = pTrigger;
95377 
95378 trigger_cleanup:
95379   sqlite3DbFree(db, zName);
95380   sqlite3SrcListDelete(db, pTableName);
95381   sqlite3IdListDelete(db, pColumns);
95382   sqlite3ExprDelete(db, pWhen);
95383   if( !pParse->pNewTrigger ){
95384     sqlite3DeleteTrigger(db, pTrigger);
95385   }else{
95386     assert( pParse->pNewTrigger==pTrigger );
95387   }
95388 }
95389 
95390 /*
95391 ** This routine is called after all of the trigger actions have been parsed
95392 ** in order to complete the process of building the trigger.
95393 */
95394 SQLITE_PRIVATE void sqlite3FinishTrigger(
95395   Parse *pParse,          /* Parser context */
95396   TriggerStep *pStepList, /* The triggered program */
95397   Token *pAll             /* Token that describes the complete CREATE TRIGGER */
95398 ){
95399   Trigger *pTrig = pParse->pNewTrigger;   /* Trigger being finished */
95400   char *zName;                            /* Name of trigger */
95401   sqlite3 *db = pParse->db;               /* The database */
95402   DbFixer sFix;                           /* Fixer object */
95403   int iDb;                                /* Database containing the trigger */
95404   Token nameToken;                        /* Trigger name for error reporting */
95405 
95406   pParse->pNewTrigger = 0;
95407   if( NEVER(pParse->nErr) || !pTrig ) goto triggerfinish_cleanup;
95408   zName = pTrig->zName;
95409   iDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
95410   pTrig->step_list = pStepList;
95411   while( pStepList ){
95412     pStepList->pTrig = pTrig;
95413     pStepList = pStepList->pNext;
95414   }
95415   nameToken.z = pTrig->zName;
95416   nameToken.n = sqlite3Strlen30(nameToken.z);
95417   if( sqlite3FixInit(&sFix, pParse, iDb, "trigger", &nameToken)
95418           && sqlite3FixTriggerStep(&sFix, pTrig->step_list) ){
95419     goto triggerfinish_cleanup;
95420   }
95421 
95422   /* if we are not initializing,
95423   ** build the sqlite_master entry
95424   */
95425   if( !db->init.busy ){
95426     Vdbe *v;
95427     char *z;
95428 
95429     /* Make an entry in the sqlite_master table */
95430     v = sqlite3GetVdbe(pParse);
95431     if( v==0 ) goto triggerfinish_cleanup;
95432     sqlite3BeginWriteOperation(pParse, 0, iDb);
95433     z = sqlite3DbStrNDup(db, (char*)pAll->z, pAll->n);
95434     sqlite3NestedParse(pParse,
95435        "INSERT INTO %Q.%s VALUES('trigger',%Q,%Q,0,'CREATE TRIGGER %q')",
95436        db->aDb[iDb].zName, SCHEMA_TABLE(iDb), zName,
95437        pTrig->table, z);
95438     sqlite3DbFree(db, z);
95439     sqlite3ChangeCookie(pParse, iDb);
95440     sqlite3VdbeAddParseSchemaOp(v, iDb,
95441         sqlite3MPrintf(db, "type='trigger' AND name='%q'", zName));
95442   }
95443 
95444   if( db->init.busy ){
95445     Trigger *pLink = pTrig;
95446     Hash *pHash = &db->aDb[iDb].pSchema->trigHash;
95447     assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
95448     pTrig = sqlite3HashInsert(pHash, zName, sqlite3Strlen30(zName), pTrig);
95449     if( pTrig ){
95450       db->mallocFailed = 1;
95451     }else if( pLink->pSchema==pLink->pTabSchema ){
95452       Table *pTab;
95453       int n = sqlite3Strlen30(pLink->table);
95454       pTab = sqlite3HashFind(&pLink->pTabSchema->tblHash, pLink->table, n);
95455       assert( pTab!=0 );
95456       pLink->pNext = pTab->pTrigger;
95457       pTab->pTrigger = pLink;
95458     }
95459   }
95460 
95461 triggerfinish_cleanup:
95462   sqlite3DeleteTrigger(db, pTrig);
95463   assert( !pParse->pNewTrigger );
95464   sqlite3DeleteTriggerStep(db, pStepList);
95465 }
95466 
95467 /*
95468 ** Turn a SELECT statement (that the pSelect parameter points to) into
95469 ** a trigger step.  Return a pointer to a TriggerStep structure.
95470 **
95471 ** The parser calls this routine when it finds a SELECT statement in
95472 ** body of a TRIGGER.
95473 */
95474 SQLITE_PRIVATE TriggerStep *sqlite3TriggerSelectStep(sqlite3 *db, Select *pSelect){
95475   TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
95476   if( pTriggerStep==0 ) {
95477     sqlite3SelectDelete(db, pSelect);
95478     return 0;
95479   }
95480   pTriggerStep->op = TK_SELECT;
95481   pTriggerStep->pSelect = pSelect;
95482   pTriggerStep->orconf = OE_Default;
95483   return pTriggerStep;
95484 }
95485 
95486 /*
95487 ** Allocate space to hold a new trigger step.  The allocated space
95488 ** holds both the TriggerStep object and the TriggerStep.target.z string.
95489 **
95490 ** If an OOM error occurs, NULL is returned and db->mallocFailed is set.
95491 */
95492 static TriggerStep *triggerStepAllocate(
95493   sqlite3 *db,                /* Database connection */
95494   u8 op,                      /* Trigger opcode */
95495   Token *pName                /* The target name */
95496 ){
95497   TriggerStep *pTriggerStep;
95498 
95499   pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep) + pName->n);
95500   if( pTriggerStep ){
95501     char *z = (char*)&pTriggerStep[1];
95502     memcpy(z, pName->z, pName->n);
95503     pTriggerStep->target.z = z;
95504     pTriggerStep->target.n = pName->n;
95505     pTriggerStep->op = op;
95506   }
95507   return pTriggerStep;
95508 }
95509 
95510 /*
95511 ** Build a trigger step out of an INSERT statement.  Return a pointer
95512 ** to the new trigger step.
95513 **
95514 ** The parser calls this routine when it sees an INSERT inside the
95515 ** body of a trigger.
95516 */
95517 SQLITE_PRIVATE TriggerStep *sqlite3TriggerInsertStep(
95518   sqlite3 *db,        /* The database connection */
95519   Token *pTableName,  /* Name of the table into which we insert */
95520   IdList *pColumn,    /* List of columns in pTableName to insert into */
95521   ExprList *pEList,   /* The VALUE clause: a list of values to be inserted */
95522   Select *pSelect,    /* A SELECT statement that supplies values */
95523   u8 orconf           /* The conflict algorithm (OE_Abort, OE_Replace, etc.) */
95524 ){
95525   TriggerStep *pTriggerStep;
95526 
95527   assert(pEList == 0 || pSelect == 0);
95528   assert(pEList != 0 || pSelect != 0 || db->mallocFailed);
95529 
95530   pTriggerStep = triggerStepAllocate(db, TK_INSERT, pTableName);
95531   if( pTriggerStep ){
95532     pTriggerStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
95533     pTriggerStep->pIdList = pColumn;
95534     pTriggerStep->pExprList = sqlite3ExprListDup(db, pEList, EXPRDUP_REDUCE);
95535     pTriggerStep->orconf = orconf;
95536   }else{
95537     sqlite3IdListDelete(db, pColumn);
95538   }
95539   sqlite3ExprListDelete(db, pEList);
95540   sqlite3SelectDelete(db, pSelect);
95541 
95542   return pTriggerStep;
95543 }
95544 
95545 /*
95546 ** Construct a trigger step that implements an UPDATE statement and return
95547 ** a pointer to that trigger step.  The parser calls this routine when it
95548 ** sees an UPDATE statement inside the body of a CREATE TRIGGER.
95549 */
95550 SQLITE_PRIVATE TriggerStep *sqlite3TriggerUpdateStep(
95551   sqlite3 *db,         /* The database connection */
95552   Token *pTableName,   /* Name of the table to be updated */
95553   ExprList *pEList,    /* The SET clause: list of column and new values */
95554   Expr *pWhere,        /* The WHERE clause */
95555   u8 orconf            /* The conflict algorithm. (OE_Abort, OE_Ignore, etc) */
95556 ){
95557   TriggerStep *pTriggerStep;
95558 
95559   pTriggerStep = triggerStepAllocate(db, TK_UPDATE, pTableName);
95560   if( pTriggerStep ){
95561     pTriggerStep->pExprList = sqlite3ExprListDup(db, pEList, EXPRDUP_REDUCE);
95562     pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
95563     pTriggerStep->orconf = orconf;
95564   }
95565   sqlite3ExprListDelete(db, pEList);
95566   sqlite3ExprDelete(db, pWhere);
95567   return pTriggerStep;
95568 }
95569 
95570 /*
95571 ** Construct a trigger step that implements a DELETE statement and return
95572 ** a pointer to that trigger step.  The parser calls this routine when it
95573 ** sees a DELETE statement inside the body of a CREATE TRIGGER.
95574 */
95575 SQLITE_PRIVATE TriggerStep *sqlite3TriggerDeleteStep(
95576   sqlite3 *db,            /* Database connection */
95577   Token *pTableName,      /* The table from which rows are deleted */
95578   Expr *pWhere            /* The WHERE clause */
95579 ){
95580   TriggerStep *pTriggerStep;
95581 
95582   pTriggerStep = triggerStepAllocate(db, TK_DELETE, pTableName);
95583   if( pTriggerStep ){
95584     pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
95585     pTriggerStep->orconf = OE_Default;
95586   }
95587   sqlite3ExprDelete(db, pWhere);
95588   return pTriggerStep;
95589 }
95590 
95591 /*
95592 ** Recursively delete a Trigger structure
95593 */
95594 SQLITE_PRIVATE void sqlite3DeleteTrigger(sqlite3 *db, Trigger *pTrigger){
95595   if( pTrigger==0 ) return;
95596   sqlite3DeleteTriggerStep(db, pTrigger->step_list);
95597   sqlite3DbFree(db, pTrigger->zName);
95598   sqlite3DbFree(db, pTrigger->table);
95599   sqlite3ExprDelete(db, pTrigger->pWhen);
95600   sqlite3IdListDelete(db, pTrigger->pColumns);
95601   sqlite3DbFree(db, pTrigger);
95602 }
95603 
95604 /*
95605 ** This function is called to drop a trigger from the database schema.
95606 **
95607 ** This may be called directly from the parser and therefore identifies
95608 ** the trigger by name.  The sqlite3DropTriggerPtr() routine does the
95609 ** same job as this routine except it takes a pointer to the trigger
95610 ** instead of the trigger name.
95611 **/
95612 SQLITE_PRIVATE void sqlite3DropTrigger(Parse *pParse, SrcList *pName, int noErr){
95613   Trigger *pTrigger = 0;
95614   int i;
95615   const char *zDb;
95616   const char *zName;
95617   int nName;
95618   sqlite3 *db = pParse->db;
95619 
95620   if( db->mallocFailed ) goto drop_trigger_cleanup;
95621   if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
95622     goto drop_trigger_cleanup;
95623   }
95624 
95625   assert( pName->nSrc==1 );
95626   zDb = pName->a[0].zDatabase;
95627   zName = pName->a[0].zName;
95628   nName = sqlite3Strlen30(zName);
95629   assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) );
95630   for(i=OMIT_TEMPDB; i<db->nDb; i++){
95631     int j = (i<2) ? i^1 : i;  /* Search TEMP before MAIN */
95632     if( zDb && sqlite3StrICmp(db->aDb[j].zName, zDb) ) continue;
95633     assert( sqlite3SchemaMutexHeld(db, j, 0) );
95634     pTrigger = sqlite3HashFind(&(db->aDb[j].pSchema->trigHash), zName, nName);
95635     if( pTrigger ) break;
95636   }
95637   if( !pTrigger ){
95638     if( !noErr ){
95639       sqlite3ErrorMsg(pParse, "no such trigger: %S", pName, 0);
95640     }else{
95641       sqlite3CodeVerifyNamedSchema(pParse, zDb);
95642     }
95643     pParse->checkSchema = 1;
95644     goto drop_trigger_cleanup;
95645   }
95646   sqlite3DropTriggerPtr(pParse, pTrigger);
95647 
95648 drop_trigger_cleanup:
95649   sqlite3SrcListDelete(db, pName);
95650 }
95651 
95652 /*
95653 ** Return a pointer to the Table structure for the table that a trigger
95654 ** is set on.
95655 */
95656 static Table *tableOfTrigger(Trigger *pTrigger){
95657   int n = sqlite3Strlen30(pTrigger->table);
95658   return sqlite3HashFind(&pTrigger->pTabSchema->tblHash, pTrigger->table, n);
95659 }
95660 
95661 
95662 /*
95663 ** Drop a trigger given a pointer to that trigger.
95664 */
95665 SQLITE_PRIVATE void sqlite3DropTriggerPtr(Parse *pParse, Trigger *pTrigger){
95666   Table   *pTable;
95667   Vdbe *v;
95668   sqlite3 *db = pParse->db;
95669   int iDb;
95670 
95671   iDb = sqlite3SchemaToIndex(pParse->db, pTrigger->pSchema);
95672   assert( iDb>=0 && iDb<db->nDb );
95673   pTable = tableOfTrigger(pTrigger);
95674   assert( pTable );
95675   assert( pTable->pSchema==pTrigger->pSchema || iDb==1 );
95676 #ifndef SQLITE_OMIT_AUTHORIZATION
95677   {
95678     int code = SQLITE_DROP_TRIGGER;
95679     const char *zDb = db->aDb[iDb].zName;
95680     const char *zTab = SCHEMA_TABLE(iDb);
95681     if( iDb==1 ) code = SQLITE_DROP_TEMP_TRIGGER;
95682     if( sqlite3AuthCheck(pParse, code, pTrigger->zName, pTable->zName, zDb) ||
95683       sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
95684       return;
95685     }
95686   }
95687 #endif
95688 
95689   /* Generate code to destroy the database record of the trigger.
95690   */
95691   assert( pTable!=0 );
95692   if( (v = sqlite3GetVdbe(pParse))!=0 ){
95693     int base;
95694     static const VdbeOpList dropTrigger[] = {
95695       { OP_Rewind,     0, ADDR(9),  0},
95696       { OP_String8,    0, 1,        0}, /* 1 */
95697       { OP_Column,     0, 1,        2},
95698       { OP_Ne,         2, ADDR(8),  1},
95699       { OP_String8,    0, 1,        0}, /* 4: "trigger" */
95700       { OP_Column,     0, 0,        2},
95701       { OP_Ne,         2, ADDR(8),  1},
95702       { OP_Delete,     0, 0,        0},
95703       { OP_Next,       0, ADDR(1),  0}, /* 8 */
95704     };
95705 
95706     sqlite3BeginWriteOperation(pParse, 0, iDb);
95707     sqlite3OpenMasterTable(pParse, iDb);
95708     base = sqlite3VdbeAddOpList(v,  ArraySize(dropTrigger), dropTrigger);
95709     sqlite3VdbeChangeP4(v, base+1, pTrigger->zName, P4_TRANSIENT);
95710     sqlite3VdbeChangeP4(v, base+4, "trigger", P4_STATIC);
95711     sqlite3ChangeCookie(pParse, iDb);
95712     sqlite3VdbeAddOp2(v, OP_Close, 0, 0);
95713     sqlite3VdbeAddOp4(v, OP_DropTrigger, iDb, 0, 0, pTrigger->zName, 0);
95714     if( pParse->nMem<3 ){
95715       pParse->nMem = 3;
95716     }
95717   }
95718 }
95719 
95720 /*
95721 ** Remove a trigger from the hash tables of the sqlite* pointer.
95722 */
95723 SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTrigger(sqlite3 *db, int iDb, const char *zName){
95724   Trigger *pTrigger;
95725   Hash *pHash;
95726 
95727   assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
95728   pHash = &(db->aDb[iDb].pSchema->trigHash);
95729   pTrigger = sqlite3HashInsert(pHash, zName, sqlite3Strlen30(zName), 0);
95730   if( ALWAYS(pTrigger) ){
95731     if( pTrigger->pSchema==pTrigger->pTabSchema ){
95732       Table *pTab = tableOfTrigger(pTrigger);
95733       Trigger **pp;
95734       for(pp=&pTab->pTrigger; *pp!=pTrigger; pp=&((*pp)->pNext));
95735       *pp = (*pp)->pNext;
95736     }
95737     sqlite3DeleteTrigger(db, pTrigger);
95738     db->flags |= SQLITE_InternChanges;
95739   }
95740 }
95741 
95742 /*
95743 ** pEList is the SET clause of an UPDATE statement.  Each entry
95744 ** in pEList is of the format <id>=<expr>.  If any of the entries
95745 ** in pEList have an <id> which matches an identifier in pIdList,
95746 ** then return TRUE.  If pIdList==NULL, then it is considered a
95747 ** wildcard that matches anything.  Likewise if pEList==NULL then
95748 ** it matches anything so always return true.  Return false only
95749 ** if there is no match.
95750 */
95751 static int checkColumnOverlap(IdList *pIdList, ExprList *pEList){
95752   int e;
95753   if( pIdList==0 || NEVER(pEList==0) ) return 1;
95754   for(e=0; e<pEList->nExpr; e++){
95755     if( sqlite3IdListIndex(pIdList, pEList->a[e].zName)>=0 ) return 1;
95756   }
95757   return 0;
95758 }
95759 
95760 /*
95761 ** Return a list of all triggers on table pTab if there exists at least
95762 ** one trigger that must be fired when an operation of type 'op' is
95763 ** performed on the table, and, if that operation is an UPDATE, if at
95764 ** least one of the columns in pChanges is being modified.
95765 */
95766 SQLITE_PRIVATE Trigger *sqlite3TriggersExist(
95767   Parse *pParse,          /* Parse context */
95768   Table *pTab,            /* The table the contains the triggers */
95769   int op,                 /* one of TK_DELETE, TK_INSERT, TK_UPDATE */
95770   ExprList *pChanges,     /* Columns that change in an UPDATE statement */
95771   int *pMask              /* OUT: Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
95772 ){
95773   int mask = 0;
95774   Trigger *pList = 0;
95775   Trigger *p;
95776 
95777   if( (pParse->db->flags & SQLITE_EnableTrigger)!=0 ){
95778     pList = sqlite3TriggerList(pParse, pTab);
95779   }
95780   assert( pList==0 || IsVirtual(pTab)==0 );
95781   for(p=pList; p; p=p->pNext){
95782     if( p->op==op && checkColumnOverlap(p->pColumns, pChanges) ){
95783       mask |= p->tr_tm;
95784     }
95785   }
95786   if( pMask ){
95787     *pMask = mask;
95788   }
95789   return (mask ? pList : 0);
95790 }
95791 
95792 /*
95793 ** Convert the pStep->target token into a SrcList and return a pointer
95794 ** to that SrcList.
95795 **
95796 ** This routine adds a specific database name, if needed, to the target when
95797 ** forming the SrcList.  This prevents a trigger in one database from
95798 ** referring to a target in another database.  An exception is when the
95799 ** trigger is in TEMP in which case it can refer to any other database it
95800 ** wants.
95801 */
95802 static SrcList *targetSrcList(
95803   Parse *pParse,       /* The parsing context */
95804   TriggerStep *pStep   /* The trigger containing the target token */
95805 ){
95806   int iDb;             /* Index of the database to use */
95807   SrcList *pSrc;       /* SrcList to be returned */
95808 
95809   pSrc = sqlite3SrcListAppend(pParse->db, 0, &pStep->target, 0);
95810   if( pSrc ){
95811     assert( pSrc->nSrc>0 );
95812     assert( pSrc->a!=0 );
95813     iDb = sqlite3SchemaToIndex(pParse->db, pStep->pTrig->pSchema);
95814     if( iDb==0 || iDb>=2 ){
95815       sqlite3 *db = pParse->db;
95816       assert( iDb<pParse->db->nDb );
95817       pSrc->a[pSrc->nSrc-1].zDatabase = sqlite3DbStrDup(db, db->aDb[iDb].zName);
95818     }
95819   }
95820   return pSrc;
95821 }
95822 
95823 /*
95824 ** Generate VDBE code for the statements inside the body of a single
95825 ** trigger.
95826 */
95827 static int codeTriggerProgram(
95828   Parse *pParse,            /* The parser context */
95829   TriggerStep *pStepList,   /* List of statements inside the trigger body */
95830   int orconf                /* Conflict algorithm. (OE_Abort, etc) */
95831 ){
95832   TriggerStep *pStep;
95833   Vdbe *v = pParse->pVdbe;
95834   sqlite3 *db = pParse->db;
95835 
95836   assert( pParse->pTriggerTab && pParse->pToplevel );
95837   assert( pStepList );
95838   assert( v!=0 );
95839   for(pStep=pStepList; pStep; pStep=pStep->pNext){
95840     /* Figure out the ON CONFLICT policy that will be used for this step
95841     ** of the trigger program. If the statement that caused this trigger
95842     ** to fire had an explicit ON CONFLICT, then use it. Otherwise, use
95843     ** the ON CONFLICT policy that was specified as part of the trigger
95844     ** step statement. Example:
95845     **
95846     **   CREATE TRIGGER AFTER INSERT ON t1 BEGIN;
95847     **     INSERT OR REPLACE INTO t2 VALUES(new.a, new.b);
95848     **   END;
95849     **
95850     **   INSERT INTO t1 ... ;            -- insert into t2 uses REPLACE policy
95851     **   INSERT OR IGNORE INTO t1 ... ;  -- insert into t2 uses IGNORE policy
95852     */
95853     pParse->eOrconf = (orconf==OE_Default)?pStep->orconf:(u8)orconf;
95854 
95855     switch( pStep->op ){
95856       case TK_UPDATE: {
95857         sqlite3Update(pParse,
95858           targetSrcList(pParse, pStep),
95859           sqlite3ExprListDup(db, pStep->pExprList, 0),
95860           sqlite3ExprDup(db, pStep->pWhere, 0),
95861           pParse->eOrconf
95862         );
95863         break;
95864       }
95865       case TK_INSERT: {
95866         sqlite3Insert(pParse,
95867           targetSrcList(pParse, pStep),
95868           sqlite3ExprListDup(db, pStep->pExprList, 0),
95869           sqlite3SelectDup(db, pStep->pSelect, 0),
95870           sqlite3IdListDup(db, pStep->pIdList),
95871           pParse->eOrconf
95872         );
95873         break;
95874       }
95875       case TK_DELETE: {
95876         sqlite3DeleteFrom(pParse,
95877           targetSrcList(pParse, pStep),
95878           sqlite3ExprDup(db, pStep->pWhere, 0)
95879         );
95880         break;
95881       }
95882       default: assert( pStep->op==TK_SELECT ); {
95883         SelectDest sDest;
95884         Select *pSelect = sqlite3SelectDup(db, pStep->pSelect, 0);
95885         sqlite3SelectDestInit(&sDest, SRT_Discard, 0);
95886         sqlite3Select(pParse, pSelect, &sDest);
95887         sqlite3SelectDelete(db, pSelect);
95888         break;
95889       }
95890     }
95891     if( pStep->op!=TK_SELECT ){
95892       sqlite3VdbeAddOp0(v, OP_ResetCount);
95893     }
95894   }
95895 
95896   return 0;
95897 }
95898 
95899 #ifdef SQLITE_DEBUG
95900 /*
95901 ** This function is used to add VdbeComment() annotations to a VDBE
95902 ** program. It is not used in production code, only for debugging.
95903 */
95904 static const char *onErrorText(int onError){
95905   switch( onError ){
95906     case OE_Abort:    return "abort";
95907     case OE_Rollback: return "rollback";
95908     case OE_Fail:     return "fail";
95909     case OE_Replace:  return "replace";
95910     case OE_Ignore:   return "ignore";
95911     case OE_Default:  return "default";
95912   }
95913   return "n/a";
95914 }
95915 #endif
95916 
95917 /*
95918 ** Parse context structure pFrom has just been used to create a sub-vdbe
95919 ** (trigger program). If an error has occurred, transfer error information
95920 ** from pFrom to pTo.
95921 */
95922 static void transferParseError(Parse *pTo, Parse *pFrom){
95923   assert( pFrom->zErrMsg==0 || pFrom->nErr );
95924   assert( pTo->zErrMsg==0 || pTo->nErr );
95925   if( pTo->nErr==0 ){
95926     pTo->zErrMsg = pFrom->zErrMsg;
95927     pTo->nErr = pFrom->nErr;
95928   }else{
95929     sqlite3DbFree(pFrom->db, pFrom->zErrMsg);
95930   }
95931 }
95932 
95933 /*
95934 ** Create and populate a new TriggerPrg object with a sub-program
95935 ** implementing trigger pTrigger with ON CONFLICT policy orconf.
95936 */
95937 static TriggerPrg *codeRowTrigger(
95938   Parse *pParse,       /* Current parse context */
95939   Trigger *pTrigger,   /* Trigger to code */
95940   Table *pTab,         /* The table pTrigger is attached to */
95941   int orconf           /* ON CONFLICT policy to code trigger program with */
95942 ){
95943   Parse *pTop = sqlite3ParseToplevel(pParse);
95944   sqlite3 *db = pParse->db;   /* Database handle */
95945   TriggerPrg *pPrg;           /* Value to return */
95946   Expr *pWhen = 0;            /* Duplicate of trigger WHEN expression */
95947   Vdbe *v;                    /* Temporary VM */
95948   NameContext sNC;            /* Name context for sub-vdbe */
95949   SubProgram *pProgram = 0;   /* Sub-vdbe for trigger program */
95950   Parse *pSubParse;           /* Parse context for sub-vdbe */
95951   int iEndTrigger = 0;        /* Label to jump to if WHEN is false */
95952 
95953   assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
95954   assert( pTop->pVdbe );
95955 
95956   /* Allocate the TriggerPrg and SubProgram objects. To ensure that they
95957   ** are freed if an error occurs, link them into the Parse.pTriggerPrg
95958   ** list of the top-level Parse object sooner rather than later.  */
95959   pPrg = sqlite3DbMallocZero(db, sizeof(TriggerPrg));
95960   if( !pPrg ) return 0;
95961   pPrg->pNext = pTop->pTriggerPrg;
95962   pTop->pTriggerPrg = pPrg;
95963   pPrg->pProgram = pProgram = sqlite3DbMallocZero(db, sizeof(SubProgram));
95964   if( !pProgram ) return 0;
95965   sqlite3VdbeLinkSubProgram(pTop->pVdbe, pProgram);
95966   pPrg->pTrigger = pTrigger;
95967   pPrg->orconf = orconf;
95968   pPrg->aColmask[0] = 0xffffffff;
95969   pPrg->aColmask[1] = 0xffffffff;
95970 
95971   /* Allocate and populate a new Parse context to use for coding the
95972   ** trigger sub-program.  */
95973   pSubParse = sqlite3StackAllocZero(db, sizeof(Parse));
95974   if( !pSubParse ) return 0;
95975   memset(&sNC, 0, sizeof(sNC));
95976   sNC.pParse = pSubParse;
95977   pSubParse->db = db;
95978   pSubParse->pTriggerTab = pTab;
95979   pSubParse->pToplevel = pTop;
95980   pSubParse->zAuthContext = pTrigger->zName;
95981   pSubParse->eTriggerOp = pTrigger->op;
95982   pSubParse->nQueryLoop = pParse->nQueryLoop;
95983 
95984   v = sqlite3GetVdbe(pSubParse);
95985   if( v ){
95986     VdbeComment((v, "Start: %s.%s (%s %s%s%s ON %s)",
95987       pTrigger->zName, onErrorText(orconf),
95988       (pTrigger->tr_tm==TRIGGER_BEFORE ? "BEFORE" : "AFTER"),
95989         (pTrigger->op==TK_UPDATE ? "UPDATE" : ""),
95990         (pTrigger->op==TK_INSERT ? "INSERT" : ""),
95991         (pTrigger->op==TK_DELETE ? "DELETE" : ""),
95992       pTab->zName
95993     ));
95994 #ifndef SQLITE_OMIT_TRACE
95995     sqlite3VdbeChangeP4(v, -1,
95996       sqlite3MPrintf(db, "-- TRIGGER %s", pTrigger->zName), P4_DYNAMIC
95997     );
95998 #endif
95999 
96000     /* If one was specified, code the WHEN clause. If it evaluates to false
96001     ** (or NULL) the sub-vdbe is immediately halted by jumping to the
96002     ** OP_Halt inserted at the end of the program.  */
96003     if( pTrigger->pWhen ){
96004       pWhen = sqlite3ExprDup(db, pTrigger->pWhen, 0);
96005       if( SQLITE_OK==sqlite3ResolveExprNames(&sNC, pWhen)
96006        && db->mallocFailed==0
96007       ){
96008         iEndTrigger = sqlite3VdbeMakeLabel(v);
96009         sqlite3ExprIfFalse(pSubParse, pWhen, iEndTrigger, SQLITE_JUMPIFNULL);
96010       }
96011       sqlite3ExprDelete(db, pWhen);
96012     }
96013 
96014     /* Code the trigger program into the sub-vdbe. */
96015     codeTriggerProgram(pSubParse, pTrigger->step_list, orconf);
96016 
96017     /* Insert an OP_Halt at the end of the sub-program. */
96018     if( iEndTrigger ){
96019       sqlite3VdbeResolveLabel(v, iEndTrigger);
96020     }
96021     sqlite3VdbeAddOp0(v, OP_Halt);
96022     VdbeComment((v, "End: %s.%s", pTrigger->zName, onErrorText(orconf)));
96023 
96024     transferParseError(pParse, pSubParse);
96025     if( db->mallocFailed==0 ){
96026       pProgram->aOp = sqlite3VdbeTakeOpArray(v, &pProgram->nOp, &pTop->nMaxArg);
96027     }
96028     pProgram->nMem = pSubParse->nMem;
96029     pProgram->nCsr = pSubParse->nTab;
96030     pProgram->token = (void *)pTrigger;
96031     pPrg->aColmask[0] = pSubParse->oldmask;
96032     pPrg->aColmask[1] = pSubParse->newmask;
96033     sqlite3VdbeDelete(v);
96034   }
96035 
96036   assert( !pSubParse->pAinc       && !pSubParse->pZombieTab );
96037   assert( !pSubParse->pTriggerPrg && !pSubParse->nMaxArg );
96038   sqlite3StackFree(db, pSubParse);
96039 
96040   return pPrg;
96041 }
96042 
96043 /*
96044 ** Return a pointer to a TriggerPrg object containing the sub-program for
96045 ** trigger pTrigger with default ON CONFLICT algorithm orconf. If no such
96046 ** TriggerPrg object exists, a new object is allocated and populated before
96047 ** being returned.
96048 */
96049 static TriggerPrg *getRowTrigger(
96050   Parse *pParse,       /* Current parse context */
96051   Trigger *pTrigger,   /* Trigger to code */
96052   Table *pTab,         /* The table trigger pTrigger is attached to */
96053   int orconf           /* ON CONFLICT algorithm. */
96054 ){
96055   Parse *pRoot = sqlite3ParseToplevel(pParse);
96056   TriggerPrg *pPrg;
96057 
96058   assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
96059 
96060   /* It may be that this trigger has already been coded (or is in the
96061   ** process of being coded). If this is the case, then an entry with
96062   ** a matching TriggerPrg.pTrigger field will be present somewhere
96063   ** in the Parse.pTriggerPrg list. Search for such an entry.  */
96064   for(pPrg=pRoot->pTriggerPrg;
96065       pPrg && (pPrg->pTrigger!=pTrigger || pPrg->orconf!=orconf);
96066       pPrg=pPrg->pNext
96067   );
96068 
96069   /* If an existing TriggerPrg could not be located, create a new one. */
96070   if( !pPrg ){
96071     pPrg = codeRowTrigger(pParse, pTrigger, pTab, orconf);
96072   }
96073 
96074   return pPrg;
96075 }
96076 
96077 /*
96078 ** Generate code for the trigger program associated with trigger p on
96079 ** table pTab. The reg, orconf and ignoreJump parameters passed to this
96080 ** function are the same as those described in the header function for
96081 ** sqlite3CodeRowTrigger()
96082 */
96083 SQLITE_PRIVATE void sqlite3CodeRowTriggerDirect(
96084   Parse *pParse,       /* Parse context */
96085   Trigger *p,          /* Trigger to code */
96086   Table *pTab,         /* The table to code triggers from */
96087   int reg,             /* Reg array containing OLD.* and NEW.* values */
96088   int orconf,          /* ON CONFLICT policy */
96089   int ignoreJump       /* Instruction to jump to for RAISE(IGNORE) */
96090 ){
96091   Vdbe *v = sqlite3GetVdbe(pParse); /* Main VM */
96092   TriggerPrg *pPrg;
96093   pPrg = getRowTrigger(pParse, p, pTab, orconf);
96094   assert( pPrg || pParse->nErr || pParse->db->mallocFailed );
96095 
96096   /* Code the OP_Program opcode in the parent VDBE. P4 of the OP_Program
96097   ** is a pointer to the sub-vdbe containing the trigger program.  */
96098   if( pPrg ){
96099     int bRecursive = (p->zName && 0==(pParse->db->flags&SQLITE_RecTriggers));
96100 
96101     sqlite3VdbeAddOp3(v, OP_Program, reg, ignoreJump, ++pParse->nMem);
96102     sqlite3VdbeChangeP4(v, -1, (const char *)pPrg->pProgram, P4_SUBPROGRAM);
96103     VdbeComment(
96104         (v, "Call: %s.%s", (p->zName?p->zName:"fkey"), onErrorText(orconf)));
96105 
96106     /* Set the P5 operand of the OP_Program instruction to non-zero if
96107     ** recursive invocation of this trigger program is disallowed. Recursive
96108     ** invocation is disallowed if (a) the sub-program is really a trigger,
96109     ** not a foreign key action, and (b) the flag to enable recursive triggers
96110     ** is clear.  */
96111     sqlite3VdbeChangeP5(v, (u8)bRecursive);
96112   }
96113 }
96114 
96115 /*
96116 ** This is called to code the required FOR EACH ROW triggers for an operation
96117 ** on table pTab. The operation to code triggers for (INSERT, UPDATE or DELETE)
96118 ** is given by the op paramater. The tr_tm parameter determines whether the
96119 ** BEFORE or AFTER triggers are coded. If the operation is an UPDATE, then
96120 ** parameter pChanges is passed the list of columns being modified.
96121 **
96122 ** If there are no triggers that fire at the specified time for the specified
96123 ** operation on pTab, this function is a no-op.
96124 **
96125 ** The reg argument is the address of the first in an array of registers
96126 ** that contain the values substituted for the new.* and old.* references
96127 ** in the trigger program. If N is the number of columns in table pTab
96128 ** (a copy of pTab->nCol), then registers are populated as follows:
96129 **
96130 **   Register       Contains
96131 **   ------------------------------------------------------
96132 **   reg+0          OLD.rowid
96133 **   reg+1          OLD.* value of left-most column of pTab
96134 **   ...            ...
96135 **   reg+N          OLD.* value of right-most column of pTab
96136 **   reg+N+1        NEW.rowid
96137 **   reg+N+2        OLD.* value of left-most column of pTab
96138 **   ...            ...
96139 **   reg+N+N+1      NEW.* value of right-most column of pTab
96140 **
96141 ** For ON DELETE triggers, the registers containing the NEW.* values will
96142 ** never be accessed by the trigger program, so they are not allocated or
96143 ** populated by the caller (there is no data to populate them with anyway).
96144 ** Similarly, for ON INSERT triggers the values stored in the OLD.* registers
96145 ** are never accessed, and so are not allocated by the caller. So, for an
96146 ** ON INSERT trigger, the value passed to this function as parameter reg
96147 ** is not a readable register, although registers (reg+N) through
96148 ** (reg+N+N+1) are.
96149 **
96150 ** Parameter orconf is the default conflict resolution algorithm for the
96151 ** trigger program to use (REPLACE, IGNORE etc.). Parameter ignoreJump
96152 ** is the instruction that control should jump to if a trigger program
96153 ** raises an IGNORE exception.
96154 */
96155 SQLITE_PRIVATE void sqlite3CodeRowTrigger(
96156   Parse *pParse,       /* Parse context */
96157   Trigger *pTrigger,   /* List of triggers on table pTab */
96158   int op,              /* One of TK_UPDATE, TK_INSERT, TK_DELETE */
96159   ExprList *pChanges,  /* Changes list for any UPDATE OF triggers */
96160   int tr_tm,           /* One of TRIGGER_BEFORE, TRIGGER_AFTER */
96161   Table *pTab,         /* The table to code triggers from */
96162   int reg,             /* The first in an array of registers (see above) */
96163   int orconf,          /* ON CONFLICT policy */
96164   int ignoreJump       /* Instruction to jump to for RAISE(IGNORE) */
96165 ){
96166   Trigger *p;          /* Used to iterate through pTrigger list */
96167 
96168   assert( op==TK_UPDATE || op==TK_INSERT || op==TK_DELETE );
96169   assert( tr_tm==TRIGGER_BEFORE || tr_tm==TRIGGER_AFTER );
96170   assert( (op==TK_UPDATE)==(pChanges!=0) );
96171 
96172   for(p=pTrigger; p; p=p->pNext){
96173 
96174     /* Sanity checking:  The schema for the trigger and for the table are
96175     ** always defined.  The trigger must be in the same schema as the table
96176     ** or else it must be a TEMP trigger. */
96177     assert( p->pSchema!=0 );
96178     assert( p->pTabSchema!=0 );
96179     assert( p->pSchema==p->pTabSchema
96180          || p->pSchema==pParse->db->aDb[1].pSchema );
96181 
96182     /* Determine whether we should code this trigger */
96183     if( p->op==op
96184      && p->tr_tm==tr_tm
96185      && checkColumnOverlap(p->pColumns, pChanges)
96186     ){
96187       sqlite3CodeRowTriggerDirect(pParse, p, pTab, reg, orconf, ignoreJump);
96188     }
96189   }
96190 }
96191 
96192 /*
96193 ** Triggers may access values stored in the old.* or new.* pseudo-table.
96194 ** This function returns a 32-bit bitmask indicating which columns of the
96195 ** old.* or new.* tables actually are used by triggers. This information
96196 ** may be used by the caller, for example, to avoid having to load the entire
96197 ** old.* record into memory when executing an UPDATE or DELETE command.
96198 **
96199 ** Bit 0 of the returned mask is set if the left-most column of the
96200 ** table may be accessed using an [old|new].<col> reference. Bit 1 is set if
96201 ** the second leftmost column value is required, and so on. If there
96202 ** are more than 32 columns in the table, and at least one of the columns
96203 ** with an index greater than 32 may be accessed, 0xffffffff is returned.
96204 **
96205 ** It is not possible to determine if the old.rowid or new.rowid column is
96206 ** accessed by triggers. The caller must always assume that it is.
96207 **
96208 ** Parameter isNew must be either 1 or 0. If it is 0, then the mask returned
96209 ** applies to the old.* table. If 1, the new.* table.
96210 **
96211 ** Parameter tr_tm must be a mask with one or both of the TRIGGER_BEFORE
96212 ** and TRIGGER_AFTER bits set. Values accessed by BEFORE triggers are only
96213 ** included in the returned mask if the TRIGGER_BEFORE bit is set in the
96214 ** tr_tm parameter. Similarly, values accessed by AFTER triggers are only
96215 ** included in the returned mask if the TRIGGER_AFTER bit is set in tr_tm.
96216 */
96217 SQLITE_PRIVATE u32 sqlite3TriggerColmask(
96218   Parse *pParse,       /* Parse context */
96219   Trigger *pTrigger,   /* List of triggers on table pTab */
96220   ExprList *pChanges,  /* Changes list for any UPDATE OF triggers */
96221   int isNew,           /* 1 for new.* ref mask, 0 for old.* ref mask */
96222   int tr_tm,           /* Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
96223   Table *pTab,         /* The table to code triggers from */
96224   int orconf           /* Default ON CONFLICT policy for trigger steps */
96225 ){
96226   const int op = pChanges ? TK_UPDATE : TK_DELETE;
96227   u32 mask = 0;
96228   Trigger *p;
96229 
96230   assert( isNew==1 || isNew==0 );
96231   for(p=pTrigger; p; p=p->pNext){
96232     if( p->op==op && (tr_tm&p->tr_tm)
96233      && checkColumnOverlap(p->pColumns,pChanges)
96234     ){
96235       TriggerPrg *pPrg;
96236       pPrg = getRowTrigger(pParse, p, pTab, orconf);
96237       if( pPrg ){
96238         mask |= pPrg->aColmask[isNew];
96239       }
96240     }
96241   }
96242 
96243   return mask;
96244 }
96245 
96246 #endif /* !defined(SQLITE_OMIT_TRIGGER) */
96247 
96248 /************** End of trigger.c *********************************************/
96249 /************** Begin file update.c ******************************************/
96250 /*
96251 ** 2001 September 15
96252 **
96253 ** The author disclaims copyright to this source code.  In place of
96254 ** a legal notice, here is a blessing:
96255 **
96256 **    May you do good and not evil.
96257 **    May you find forgiveness for yourself and forgive others.
96258 **    May you share freely, never taking more than you give.
96259 **
96260 *************************************************************************
96261 ** This file contains C code routines that are called by the parser
96262 ** to handle UPDATE statements.
96263 */
96264 
96265 #ifndef SQLITE_OMIT_VIRTUALTABLE
96266 /* Forward declaration */
96267 static void updateVirtualTable(
96268   Parse *pParse,       /* The parsing context */
96269   SrcList *pSrc,       /* The virtual table to be modified */
96270   Table *pTab,         /* The virtual table */
96271   ExprList *pChanges,  /* The columns to change in the UPDATE statement */
96272   Expr *pRowidExpr,    /* Expression used to recompute the rowid */
96273   int *aXRef,          /* Mapping from columns of pTab to entries in pChanges */
96274   Expr *pWhere,        /* WHERE clause of the UPDATE statement */
96275   int onError          /* ON CONFLICT strategy */
96276 );
96277 #endif /* SQLITE_OMIT_VIRTUALTABLE */
96278 
96279 /*
96280 ** The most recently coded instruction was an OP_Column to retrieve the
96281 ** i-th column of table pTab. This routine sets the P4 parameter of the
96282 ** OP_Column to the default value, if any.
96283 **
96284 ** The default value of a column is specified by a DEFAULT clause in the
96285 ** column definition. This was either supplied by the user when the table
96286 ** was created, or added later to the table definition by an ALTER TABLE
96287 ** command. If the latter, then the row-records in the table btree on disk
96288 ** may not contain a value for the column and the default value, taken
96289 ** from the P4 parameter of the OP_Column instruction, is returned instead.
96290 ** If the former, then all row-records are guaranteed to include a value
96291 ** for the column and the P4 value is not required.
96292 **
96293 ** Column definitions created by an ALTER TABLE command may only have
96294 ** literal default values specified: a number, null or a string. (If a more
96295 ** complicated default expression value was provided, it is evaluated
96296 ** when the ALTER TABLE is executed and one of the literal values written
96297 ** into the sqlite_master table.)
96298 **
96299 ** Therefore, the P4 parameter is only required if the default value for
96300 ** the column is a literal number, string or null. The sqlite3ValueFromExpr()
96301 ** function is capable of transforming these types of expressions into
96302 ** sqlite3_value objects.
96303 **
96304 ** If parameter iReg is not negative, code an OP_RealAffinity instruction
96305 ** on register iReg. This is used when an equivalent integer value is
96306 ** stored in place of an 8-byte floating point value in order to save
96307 ** space.
96308 */
96309 SQLITE_PRIVATE void sqlite3ColumnDefault(Vdbe *v, Table *pTab, int i, int iReg){
96310   assert( pTab!=0 );
96311   if( !pTab->pSelect ){
96312     sqlite3_value *pValue;
96313     u8 enc = ENC(sqlite3VdbeDb(v));
96314     Column *pCol = &pTab->aCol[i];
96315     VdbeComment((v, "%s.%s", pTab->zName, pCol->zName));
96316     assert( i<pTab->nCol );
96317     sqlite3ValueFromExpr(sqlite3VdbeDb(v), pCol->pDflt, enc,
96318                          pCol->affinity, &pValue);
96319     if( pValue ){
96320       sqlite3VdbeChangeP4(v, -1, (const char *)pValue, P4_MEM);
96321     }
96322 #ifndef SQLITE_OMIT_FLOATING_POINT
96323     if( iReg>=0 && pTab->aCol[i].affinity==SQLITE_AFF_REAL ){
96324       sqlite3VdbeAddOp1(v, OP_RealAffinity, iReg);
96325     }
96326 #endif
96327   }
96328 }
96329 
96330 /*
96331 ** Process an UPDATE statement.
96332 **
96333 **   UPDATE OR IGNORE table_wxyz SET a=b, c=d WHERE e<5 AND f NOT NULL;
96334 **          \_______/ \________/     \______/       \________________/
96335 *            onError   pTabList      pChanges             pWhere
96336 */
96337 SQLITE_PRIVATE void sqlite3Update(
96338   Parse *pParse,         /* The parser context */
96339   SrcList *pTabList,     /* The table in which we should change things */
96340   ExprList *pChanges,    /* Things to be changed */
96341   Expr *pWhere,          /* The WHERE clause.  May be null */
96342   int onError            /* How to handle constraint errors */
96343 ){
96344   int i, j;              /* Loop counters */
96345   Table *pTab;           /* The table to be updated */
96346   int addr = 0;          /* VDBE instruction address of the start of the loop */
96347   WhereInfo *pWInfo;     /* Information about the WHERE clause */
96348   Vdbe *v;               /* The virtual database engine */
96349   Index *pIdx;           /* For looping over indices */
96350   int nIdx;              /* Number of indices that need updating */
96351   int iCur;              /* VDBE Cursor number of pTab */
96352   sqlite3 *db;           /* The database structure */
96353   int *aRegIdx = 0;      /* One register assigned to each index to be updated */
96354   int *aXRef = 0;        /* aXRef[i] is the index in pChanges->a[] of the
96355                          ** an expression for the i-th column of the table.
96356                          ** aXRef[i]==-1 if the i-th column is not changed. */
96357   int chngRowid;         /* True if the record number is being changed */
96358   Expr *pRowidExpr = 0;  /* Expression defining the new record number */
96359   int openAll = 0;       /* True if all indices need to be opened */
96360   AuthContext sContext;  /* The authorization context */
96361   NameContext sNC;       /* The name-context to resolve expressions in */
96362   int iDb;               /* Database containing the table being updated */
96363   int okOnePass;         /* True for one-pass algorithm without the FIFO */
96364   int hasFK;             /* True if foreign key processing is required */
96365 
96366 #ifndef SQLITE_OMIT_TRIGGER
96367   int isView;            /* True when updating a view (INSTEAD OF trigger) */
96368   Trigger *pTrigger;     /* List of triggers on pTab, if required */
96369   int tmask;             /* Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
96370 #endif
96371   int newmask;           /* Mask of NEW.* columns accessed by BEFORE triggers */
96372 
96373   /* Register Allocations */
96374   int regRowCount = 0;   /* A count of rows changed */
96375   int regOldRowid;       /* The old rowid */
96376   int regNewRowid;       /* The new rowid */
96377   int regNew;
96378   int regOld = 0;
96379   int regRowSet = 0;     /* Rowset of rows to be updated */
96380 
96381   memset(&sContext, 0, sizeof(sContext));
96382   db = pParse->db;
96383   if( pParse->nErr || db->mallocFailed ){
96384     goto update_cleanup;
96385   }
96386   assert( pTabList->nSrc==1 );
96387 
96388   /* Locate the table which we want to update.
96389   */
96390   pTab = sqlite3SrcListLookup(pParse, pTabList);
96391   if( pTab==0 ) goto update_cleanup;
96392   iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
96393 
96394   /* Figure out if we have any triggers and if the table being
96395   ** updated is a view.
96396   */
96397 #ifndef SQLITE_OMIT_TRIGGER
96398   pTrigger = sqlite3TriggersExist(pParse, pTab, TK_UPDATE, pChanges, &tmask);
96399   isView = pTab->pSelect!=0;
96400   assert( pTrigger || tmask==0 );
96401 #else
96402 # define pTrigger 0
96403 # define isView 0
96404 # define tmask 0
96405 #endif
96406 #ifdef SQLITE_OMIT_VIEW
96407 # undef isView
96408 # define isView 0
96409 #endif
96410 
96411   if( sqlite3ViewGetColumnNames(pParse, pTab) ){
96412     goto update_cleanup;
96413   }
96414   if( sqlite3IsReadOnly(pParse, pTab, tmask) ){
96415     goto update_cleanup;
96416   }
96417   aXRef = sqlite3DbMallocRaw(db, sizeof(int) * pTab->nCol );
96418   if( aXRef==0 ) goto update_cleanup;
96419   for(i=0; i<pTab->nCol; i++) aXRef[i] = -1;
96420 
96421   /* Allocate a cursors for the main database table and for all indices.
96422   ** The index cursors might not be used, but if they are used they
96423   ** need to occur right after the database cursor.  So go ahead and
96424   ** allocate enough space, just in case.
96425   */
96426   pTabList->a[0].iCursor = iCur = pParse->nTab++;
96427   for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
96428     pParse->nTab++;
96429   }
96430 
96431   /* Initialize the name-context */
96432   memset(&sNC, 0, sizeof(sNC));
96433   sNC.pParse = pParse;
96434   sNC.pSrcList = pTabList;
96435 
96436   /* Resolve the column names in all the expressions of the
96437   ** of the UPDATE statement.  Also find the column index
96438   ** for each column to be updated in the pChanges array.  For each
96439   ** column to be updated, make sure we have authorization to change
96440   ** that column.
96441   */
96442   chngRowid = 0;
96443   for(i=0; i<pChanges->nExpr; i++){
96444     if( sqlite3ResolveExprNames(&sNC, pChanges->a[i].pExpr) ){
96445       goto update_cleanup;
96446     }
96447     for(j=0; j<pTab->nCol; j++){
96448       if( sqlite3StrICmp(pTab->aCol[j].zName, pChanges->a[i].zName)==0 ){
96449         if( j==pTab->iPKey ){
96450           chngRowid = 1;
96451           pRowidExpr = pChanges->a[i].pExpr;
96452         }
96453         aXRef[j] = i;
96454         break;
96455       }
96456     }
96457     if( j>=pTab->nCol ){
96458       if( sqlite3IsRowid(pChanges->a[i].zName) ){
96459         chngRowid = 1;
96460         pRowidExpr = pChanges->a[i].pExpr;
96461       }else{
96462         sqlite3ErrorMsg(pParse, "no such column: %s", pChanges->a[i].zName);
96463         pParse->checkSchema = 1;
96464         goto update_cleanup;
96465       }
96466     }
96467 #ifndef SQLITE_OMIT_AUTHORIZATION
96468     {
96469       int rc;
96470       rc = sqlite3AuthCheck(pParse, SQLITE_UPDATE, pTab->zName,
96471                            pTab->aCol[j].zName, db->aDb[iDb].zName);
96472       if( rc==SQLITE_DENY ){
96473         goto update_cleanup;
96474       }else if( rc==SQLITE_IGNORE ){
96475         aXRef[j] = -1;
96476       }
96477     }
96478 #endif
96479   }
96480 
96481   hasFK = sqlite3FkRequired(pParse, pTab, aXRef, chngRowid);
96482 
96483   /* Allocate memory for the array aRegIdx[].  There is one entry in the
96484   ** array for each index associated with table being updated.  Fill in
96485   ** the value with a register number for indices that are to be used
96486   ** and with zero for unused indices.
96487   */
96488   for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){}
96489   if( nIdx>0 ){
96490     aRegIdx = sqlite3DbMallocRaw(db, sizeof(Index*) * nIdx );
96491     if( aRegIdx==0 ) goto update_cleanup;
96492   }
96493   for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
96494     int reg;
96495     if( hasFK || chngRowid ){
96496       reg = ++pParse->nMem;
96497     }else{
96498       reg = 0;
96499       for(i=0; i<pIdx->nColumn; i++){
96500         if( aXRef[pIdx->aiColumn[i]]>=0 ){
96501           reg = ++pParse->nMem;
96502           break;
96503         }
96504       }
96505     }
96506     aRegIdx[j] = reg;
96507   }
96508 
96509   /* Begin generating code. */
96510   v = sqlite3GetVdbe(pParse);
96511   if( v==0 ) goto update_cleanup;
96512   if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
96513   sqlite3BeginWriteOperation(pParse, 1, iDb);
96514 
96515 #ifndef SQLITE_OMIT_VIRTUALTABLE
96516   /* Virtual tables must be handled separately */
96517   if( IsVirtual(pTab) ){
96518     updateVirtualTable(pParse, pTabList, pTab, pChanges, pRowidExpr, aXRef,
96519                        pWhere, onError);
96520     pWhere = 0;
96521     pTabList = 0;
96522     goto update_cleanup;
96523   }
96524 #endif
96525 
96526   /* Allocate required registers. */
96527   regOldRowid = regNewRowid = ++pParse->nMem;
96528   if( pTrigger || hasFK ){
96529     regOld = pParse->nMem + 1;
96530     pParse->nMem += pTab->nCol;
96531   }
96532   if( chngRowid || pTrigger || hasFK ){
96533     regNewRowid = ++pParse->nMem;
96534   }
96535   regNew = pParse->nMem + 1;
96536   pParse->nMem += pTab->nCol;
96537 
96538   /* Start the view context. */
96539   if( isView ){
96540     sqlite3AuthContextPush(pParse, &sContext, pTab->zName);
96541   }
96542 
96543   /* If we are trying to update a view, realize that view into
96544   ** a ephemeral table.
96545   */
96546 #if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
96547   if( isView ){
96548     sqlite3MaterializeView(pParse, pTab, pWhere, iCur);
96549   }
96550 #endif
96551 
96552   /* Resolve the column names in all the expressions in the
96553   ** WHERE clause.
96554   */
96555   if( sqlite3ResolveExprNames(&sNC, pWhere) ){
96556     goto update_cleanup;
96557   }
96558 
96559   /* Begin the database scan
96560   */
96561   sqlite3VdbeAddOp2(v, OP_Null, 0, regOldRowid);
96562   pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere,0, WHERE_ONEPASS_DESIRED);
96563   if( pWInfo==0 ) goto update_cleanup;
96564   okOnePass = pWInfo->okOnePass;
96565 
96566   /* Remember the rowid of every item to be updated.
96567   */
96568   sqlite3VdbeAddOp2(v, OP_Rowid, iCur, regOldRowid);
96569   if( !okOnePass ){
96570     regRowSet = ++pParse->nMem;
96571     sqlite3VdbeAddOp2(v, OP_RowSetAdd, regRowSet, regOldRowid);
96572   }
96573 
96574   /* End the database scan loop.
96575   */
96576   sqlite3WhereEnd(pWInfo);
96577 
96578   /* Initialize the count of updated rows
96579   */
96580   if( (db->flags & SQLITE_CountRows) && !pParse->pTriggerTab ){
96581     regRowCount = ++pParse->nMem;
96582     sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount);
96583   }
96584 
96585   if( !isView ){
96586     /*
96587     ** Open every index that needs updating.  Note that if any
96588     ** index could potentially invoke a REPLACE conflict resolution
96589     ** action, then we need to open all indices because we might need
96590     ** to be deleting some records.
96591     */
96592     if( !okOnePass ) sqlite3OpenTable(pParse, iCur, iDb, pTab, OP_OpenWrite);
96593     if( onError==OE_Replace ){
96594       openAll = 1;
96595     }else{
96596       openAll = 0;
96597       for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
96598         if( pIdx->onError==OE_Replace ){
96599           openAll = 1;
96600           break;
96601         }
96602       }
96603     }
96604     for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
96605       if( openAll || aRegIdx[i]>0 ){
96606         KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx);
96607         sqlite3VdbeAddOp4(v, OP_OpenWrite, iCur+i+1, pIdx->tnum, iDb,
96608                        (char*)pKey, P4_KEYINFO_HANDOFF);
96609         assert( pParse->nTab>iCur+i+1 );
96610       }
96611     }
96612   }
96613 
96614   /* Top of the update loop */
96615   if( okOnePass ){
96616     int a1 = sqlite3VdbeAddOp1(v, OP_NotNull, regOldRowid);
96617     addr = sqlite3VdbeAddOp0(v, OP_Goto);
96618     sqlite3VdbeJumpHere(v, a1);
96619   }else{
96620     addr = sqlite3VdbeAddOp3(v, OP_RowSetRead, regRowSet, 0, regOldRowid);
96621   }
96622 
96623   /* Make cursor iCur point to the record that is being updated. If
96624   ** this record does not exist for some reason (deleted by a trigger,
96625   ** for example, then jump to the next iteration of the RowSet loop.  */
96626   sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addr, regOldRowid);
96627 
96628   /* If the record number will change, set register regNewRowid to
96629   ** contain the new value. If the record number is not being modified,
96630   ** then regNewRowid is the same register as regOldRowid, which is
96631   ** already populated.  */
96632   assert( chngRowid || pTrigger || hasFK || regOldRowid==regNewRowid );
96633   if( chngRowid ){
96634     sqlite3ExprCode(pParse, pRowidExpr, regNewRowid);
96635     sqlite3VdbeAddOp1(v, OP_MustBeInt, regNewRowid);
96636   }
96637 
96638   /* If there are triggers on this table, populate an array of registers
96639   ** with the required old.* column data.  */
96640   if( hasFK || pTrigger ){
96641     u32 oldmask = (hasFK ? sqlite3FkOldmask(pParse, pTab) : 0);
96642     oldmask |= sqlite3TriggerColmask(pParse,
96643         pTrigger, pChanges, 0, TRIGGER_BEFORE|TRIGGER_AFTER, pTab, onError
96644     );
96645     for(i=0; i<pTab->nCol; i++){
96646       if( aXRef[i]<0 || oldmask==0xffffffff || (i<32 && (oldmask & (1<<i))) ){
96647         sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, i, regOld+i);
96648       }else{
96649         sqlite3VdbeAddOp2(v, OP_Null, 0, regOld+i);
96650       }
96651     }
96652     if( chngRowid==0 ){
96653       sqlite3VdbeAddOp2(v, OP_Copy, regOldRowid, regNewRowid);
96654     }
96655   }
96656 
96657   /* Populate the array of registers beginning at regNew with the new
96658   ** row data. This array is used to check constaints, create the new
96659   ** table and index records, and as the values for any new.* references
96660   ** made by triggers.
96661   **
96662   ** If there are one or more BEFORE triggers, then do not populate the
96663   ** registers associated with columns that are (a) not modified by
96664   ** this UPDATE statement and (b) not accessed by new.* references. The
96665   ** values for registers not modified by the UPDATE must be reloaded from
96666   ** the database after the BEFORE triggers are fired anyway (as the trigger
96667   ** may have modified them). So not loading those that are not going to
96668   ** be used eliminates some redundant opcodes.
96669   */
96670   newmask = sqlite3TriggerColmask(
96671       pParse, pTrigger, pChanges, 1, TRIGGER_BEFORE, pTab, onError
96672   );
96673   for(i=0; i<pTab->nCol; i++){
96674     if( i==pTab->iPKey ){
96675       sqlite3VdbeAddOp2(v, OP_Null, 0, regNew+i);
96676     }else{
96677       j = aXRef[i];
96678       if( j>=0 ){
96679         sqlite3ExprCode(pParse, pChanges->a[j].pExpr, regNew+i);
96680       }else if( 0==(tmask&TRIGGER_BEFORE) || i>31 || (newmask&(1<<i)) ){
96681         /* This branch loads the value of a column that will not be changed
96682         ** into a register. This is done if there are no BEFORE triggers, or
96683         ** if there are one or more BEFORE triggers that use this value via
96684         ** a new.* reference in a trigger program.
96685         */
96686         testcase( i==31 );
96687         testcase( i==32 );
96688         sqlite3VdbeAddOp3(v, OP_Column, iCur, i, regNew+i);
96689         sqlite3ColumnDefault(v, pTab, i, regNew+i);
96690       }
96691     }
96692   }
96693 
96694   /* Fire any BEFORE UPDATE triggers. This happens before constraints are
96695   ** verified. One could argue that this is wrong.
96696   */
96697   if( tmask&TRIGGER_BEFORE ){
96698     sqlite3VdbeAddOp2(v, OP_Affinity, regNew, pTab->nCol);
96699     sqlite3TableAffinityStr(v, pTab);
96700     sqlite3CodeRowTrigger(pParse, pTrigger, TK_UPDATE, pChanges,
96701         TRIGGER_BEFORE, pTab, regOldRowid, onError, addr);
96702 
96703     /* The row-trigger may have deleted the row being updated. In this
96704     ** case, jump to the next row. No updates or AFTER triggers are
96705     ** required. This behaviour - what happens when the row being updated
96706     ** is deleted or renamed by a BEFORE trigger - is left undefined in the
96707     ** documentation.
96708     */
96709     sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addr, regOldRowid);
96710 
96711     /* If it did not delete it, the row-trigger may still have modified
96712     ** some of the columns of the row being updated. Load the values for
96713     ** all columns not modified by the update statement into their
96714     ** registers in case this has happened.
96715     */
96716     for(i=0; i<pTab->nCol; i++){
96717       if( aXRef[i]<0 && i!=pTab->iPKey ){
96718         sqlite3VdbeAddOp3(v, OP_Column, iCur, i, regNew+i);
96719         sqlite3ColumnDefault(v, pTab, i, regNew+i);
96720       }
96721     }
96722   }
96723 
96724   if( !isView ){
96725     int j1;                       /* Address of jump instruction */
96726 
96727     /* Do constraint checks. */
96728     sqlite3GenerateConstraintChecks(pParse, pTab, iCur, regNewRowid,
96729         aRegIdx, (chngRowid?regOldRowid:0), 1, onError, addr, 0);
96730 
96731     /* Do FK constraint checks. */
96732     if( hasFK ){
96733       sqlite3FkCheck(pParse, pTab, regOldRowid, 0);
96734     }
96735 
96736     /* Delete the index entries associated with the current record.  */
96737     j1 = sqlite3VdbeAddOp3(v, OP_NotExists, iCur, 0, regOldRowid);
96738     sqlite3GenerateRowIndexDelete(pParse, pTab, iCur, aRegIdx);
96739 
96740     /* If changing the record number, delete the old record.  */
96741     if( hasFK || chngRowid ){
96742       sqlite3VdbeAddOp2(v, OP_Delete, iCur, 0);
96743     }
96744     sqlite3VdbeJumpHere(v, j1);
96745 
96746     if( hasFK ){
96747       sqlite3FkCheck(pParse, pTab, 0, regNewRowid);
96748     }
96749 
96750     /* Insert the new index entries and the new record. */
96751     sqlite3CompleteInsertion(pParse, pTab, iCur, regNewRowid, aRegIdx, 1, 0, 0);
96752 
96753     /* Do any ON CASCADE, SET NULL or SET DEFAULT operations required to
96754     ** handle rows (possibly in other tables) that refer via a foreign key
96755     ** to the row just updated. */
96756     if( hasFK ){
96757       sqlite3FkActions(pParse, pTab, pChanges, regOldRowid);
96758     }
96759   }
96760 
96761   /* Increment the row counter
96762   */
96763   if( (db->flags & SQLITE_CountRows) && !pParse->pTriggerTab){
96764     sqlite3VdbeAddOp2(v, OP_AddImm, regRowCount, 1);
96765   }
96766 
96767   sqlite3CodeRowTrigger(pParse, pTrigger, TK_UPDATE, pChanges,
96768       TRIGGER_AFTER, pTab, regOldRowid, onError, addr);
96769 
96770   /* Repeat the above with the next record to be updated, until
96771   ** all record selected by the WHERE clause have been updated.
96772   */
96773   sqlite3VdbeAddOp2(v, OP_Goto, 0, addr);
96774   sqlite3VdbeJumpHere(v, addr);
96775 
96776   /* Close all tables */
96777   for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
96778     if( openAll || aRegIdx[i]>0 ){
96779       sqlite3VdbeAddOp2(v, OP_Close, iCur+i+1, 0);
96780     }
96781   }
96782   sqlite3VdbeAddOp2(v, OP_Close, iCur, 0);
96783 
96784   /* Update the sqlite_sequence table by storing the content of the
96785   ** maximum rowid counter values recorded while inserting into
96786   ** autoincrement tables.
96787   */
96788   if( pParse->nested==0 && pParse->pTriggerTab==0 ){
96789     sqlite3AutoincrementEnd(pParse);
96790   }
96791 
96792   /*
96793   ** Return the number of rows that were changed. If this routine is
96794   ** generating code because of a call to sqlite3NestedParse(), do not
96795   ** invoke the callback function.
96796   */
96797   if( (db->flags&SQLITE_CountRows) && !pParse->pTriggerTab && !pParse->nested ){
96798     sqlite3VdbeAddOp2(v, OP_ResultRow, regRowCount, 1);
96799     sqlite3VdbeSetNumCols(v, 1);
96800     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows updated", SQLITE_STATIC);
96801   }
96802 
96803 update_cleanup:
96804   sqlite3AuthContextPop(&sContext);
96805   sqlite3DbFree(db, aRegIdx);
96806   sqlite3DbFree(db, aXRef);
96807   sqlite3SrcListDelete(db, pTabList);
96808   sqlite3ExprListDelete(db, pChanges);
96809   sqlite3ExprDelete(db, pWhere);
96810   return;
96811 }
96812 /* Make sure "isView" and other macros defined above are undefined. Otherwise
96813 ** thely may interfere with compilation of other functions in this file
96814 ** (or in another file, if this file becomes part of the amalgamation).  */
96815 #ifdef isView
96816  #undef isView
96817 #endif
96818 #ifdef pTrigger
96819  #undef pTrigger
96820 #endif
96821 
96822 #ifndef SQLITE_OMIT_VIRTUALTABLE
96823 /*
96824 ** Generate code for an UPDATE of a virtual table.
96825 **
96826 ** The strategy is that we create an ephemerial table that contains
96827 ** for each row to be changed:
96828 **
96829 **   (A)  The original rowid of that row.
96830 **   (B)  The revised rowid for the row. (note1)
96831 **   (C)  The content of every column in the row.
96832 **
96833 ** Then we loop over this ephemeral table and for each row in
96834 ** the ephermeral table call VUpdate.
96835 **
96836 ** When finished, drop the ephemeral table.
96837 **
96838 ** (note1) Actually, if we know in advance that (A) is always the same
96839 ** as (B) we only store (A), then duplicate (A) when pulling
96840 ** it out of the ephemeral table before calling VUpdate.
96841 */
96842 static void updateVirtualTable(
96843   Parse *pParse,       /* The parsing context */
96844   SrcList *pSrc,       /* The virtual table to be modified */
96845   Table *pTab,         /* The virtual table */
96846   ExprList *pChanges,  /* The columns to change in the UPDATE statement */
96847   Expr *pRowid,        /* Expression used to recompute the rowid */
96848   int *aXRef,          /* Mapping from columns of pTab to entries in pChanges */
96849   Expr *pWhere,        /* WHERE clause of the UPDATE statement */
96850   int onError          /* ON CONFLICT strategy */
96851 ){
96852   Vdbe *v = pParse->pVdbe;  /* Virtual machine under construction */
96853   ExprList *pEList = 0;     /* The result set of the SELECT statement */
96854   Select *pSelect = 0;      /* The SELECT statement */
96855   Expr *pExpr;              /* Temporary expression */
96856   int ephemTab;             /* Table holding the result of the SELECT */
96857   int i;                    /* Loop counter */
96858   int addr;                 /* Address of top of loop */
96859   int iReg;                 /* First register in set passed to OP_VUpdate */
96860   sqlite3 *db = pParse->db; /* Database connection */
96861   const char *pVTab = (const char*)sqlite3GetVTable(db, pTab);
96862   SelectDest dest;
96863 
96864   /* Construct the SELECT statement that will find the new values for
96865   ** all updated rows.
96866   */
96867   pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db, TK_ID, "_rowid_"));
96868   if( pRowid ){
96869     pEList = sqlite3ExprListAppend(pParse, pEList,
96870                                    sqlite3ExprDup(db, pRowid, 0));
96871   }
96872   assert( pTab->iPKey<0 );
96873   for(i=0; i<pTab->nCol; i++){
96874     if( aXRef[i]>=0 ){
96875       pExpr = sqlite3ExprDup(db, pChanges->a[aXRef[i]].pExpr, 0);
96876     }else{
96877       pExpr = sqlite3Expr(db, TK_ID, pTab->aCol[i].zName);
96878     }
96879     pEList = sqlite3ExprListAppend(pParse, pEList, pExpr);
96880   }
96881   pSelect = sqlite3SelectNew(pParse, pEList, pSrc, pWhere, 0, 0, 0, 0, 0, 0);
96882 
96883   /* Create the ephemeral table into which the update results will
96884   ** be stored.
96885   */
96886   assert( v );
96887   ephemTab = pParse->nTab++;
96888   sqlite3VdbeAddOp2(v, OP_OpenEphemeral, ephemTab, pTab->nCol+1+(pRowid!=0));
96889   sqlite3VdbeChangeP5(v, BTREE_UNORDERED);
96890 
96891   /* fill the ephemeral table
96892   */
96893   sqlite3SelectDestInit(&dest, SRT_Table, ephemTab);
96894   sqlite3Select(pParse, pSelect, &dest);
96895 
96896   /* Generate code to scan the ephemeral table and call VUpdate. */
96897   iReg = ++pParse->nMem;
96898   pParse->nMem += pTab->nCol+1;
96899   addr = sqlite3VdbeAddOp2(v, OP_Rewind, ephemTab, 0);
96900   sqlite3VdbeAddOp3(v, OP_Column,  ephemTab, 0, iReg);
96901   sqlite3VdbeAddOp3(v, OP_Column, ephemTab, (pRowid?1:0), iReg+1);
96902   for(i=0; i<pTab->nCol; i++){
96903     sqlite3VdbeAddOp3(v, OP_Column, ephemTab, i+1+(pRowid!=0), iReg+2+i);
96904   }
96905   sqlite3VtabMakeWritable(pParse, pTab);
96906   sqlite3VdbeAddOp4(v, OP_VUpdate, 0, pTab->nCol+2, iReg, pVTab, P4_VTAB);
96907   sqlite3VdbeChangeP5(v, onError==OE_Default ? OE_Abort : onError);
96908   sqlite3MayAbort(pParse);
96909   sqlite3VdbeAddOp2(v, OP_Next, ephemTab, addr+1);
96910   sqlite3VdbeJumpHere(v, addr);
96911   sqlite3VdbeAddOp2(v, OP_Close, ephemTab, 0);
96912 
96913   /* Cleanup */
96914   sqlite3SelectDelete(db, pSelect);
96915 }
96916 #endif /* SQLITE_OMIT_VIRTUALTABLE */
96917 
96918 /************** End of update.c **********************************************/
96919 /************** Begin file vacuum.c ******************************************/
96920 /*
96921 ** 2003 April 6
96922 **
96923 ** The author disclaims copyright to this source code.  In place of
96924 ** a legal notice, here is a blessing:
96925 **
96926 **    May you do good and not evil.
96927 **    May you find forgiveness for yourself and forgive others.
96928 **    May you share freely, never taking more than you give.
96929 **
96930 *************************************************************************
96931 ** This file contains code used to implement the VACUUM command.
96932 **
96933 ** Most of the code in this file may be omitted by defining the
96934 ** SQLITE_OMIT_VACUUM macro.
96935 */
96936 
96937 #if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
96938 /*
96939 ** Finalize a prepared statement.  If there was an error, store the
96940 ** text of the error message in *pzErrMsg.  Return the result code.
96941 */
96942 static int vacuumFinalize(sqlite3 *db, sqlite3_stmt *pStmt, char **pzErrMsg){
96943   int rc;
96944   rc = sqlite3VdbeFinalize((Vdbe*)pStmt);
96945   if( rc ){
96946     sqlite3SetString(pzErrMsg, db, sqlite3_errmsg(db));
96947   }
96948   return rc;
96949 }
96950 
96951 /*
96952 ** Execute zSql on database db. Return an error code.
96953 */
96954 static int execSql(sqlite3 *db, char **pzErrMsg, const char *zSql){
96955   sqlite3_stmt *pStmt;
96956   VVA_ONLY( int rc; )
96957   if( !zSql ){
96958     return SQLITE_NOMEM;
96959   }
96960   if( SQLITE_OK!=sqlite3_prepare(db, zSql, -1, &pStmt, 0) ){
96961     sqlite3SetString(pzErrMsg, db, sqlite3_errmsg(db));
96962     return sqlite3_errcode(db);
96963   }
96964   VVA_ONLY( rc = ) sqlite3_step(pStmt);
96965   assert( rc!=SQLITE_ROW );
96966   return vacuumFinalize(db, pStmt, pzErrMsg);
96967 }
96968 
96969 /*
96970 ** Execute zSql on database db. The statement returns exactly
96971 ** one column. Execute this as SQL on the same database.
96972 */
96973 static int execExecSql(sqlite3 *db, char **pzErrMsg, const char *zSql){
96974   sqlite3_stmt *pStmt;
96975   int rc;
96976 
96977   rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
96978   if( rc!=SQLITE_OK ) return rc;
96979 
96980   while( SQLITE_ROW==sqlite3_step(pStmt) ){
96981     rc = execSql(db, pzErrMsg, (char*)sqlite3_column_text(pStmt, 0));
96982     if( rc!=SQLITE_OK ){
96983       vacuumFinalize(db, pStmt, pzErrMsg);
96984       return rc;
96985     }
96986   }
96987 
96988   return vacuumFinalize(db, pStmt, pzErrMsg);
96989 }
96990 
96991 /*
96992 ** The non-standard VACUUM command is used to clean up the database,
96993 ** collapse free space, etc.  It is modelled after the VACUUM command
96994 ** in PostgreSQL.
96995 **
96996 ** In version 1.0.x of SQLite, the VACUUM command would call
96997 ** gdbm_reorganize() on all the database tables.  But beginning
96998 ** with 2.0.0, SQLite no longer uses GDBM so this command has
96999 ** become a no-op.
97000 */
97001 SQLITE_PRIVATE void sqlite3Vacuum(Parse *pParse){
97002   Vdbe *v = sqlite3GetVdbe(pParse);
97003   if( v ){
97004     sqlite3VdbeAddOp2(v, OP_Vacuum, 0, 0);
97005   }
97006   return;
97007 }
97008 
97009 /*
97010 ** This routine implements the OP_Vacuum opcode of the VDBE.
97011 */
97012 SQLITE_PRIVATE int sqlite3RunVacuum(char **pzErrMsg, sqlite3 *db){
97013   int rc = SQLITE_OK;     /* Return code from service routines */
97014   Btree *pMain;           /* The database being vacuumed */
97015   Btree *pTemp;           /* The temporary database we vacuum into */
97016   char *zSql = 0;         /* SQL statements */
97017   int saved_flags;        /* Saved value of the db->flags */
97018   int saved_nChange;      /* Saved value of db->nChange */
97019   int saved_nTotalChange; /* Saved value of db->nTotalChange */
97020   void (*saved_xTrace)(void*,const char*);  /* Saved db->xTrace */
97021   Db *pDb = 0;            /* Database to detach at end of vacuum */
97022   int isMemDb;            /* True if vacuuming a :memory: database */
97023   int nRes;               /* Bytes of reserved space at the end of each page */
97024   int nDb;                /* Number of attached databases */
97025 
97026   if( !db->autoCommit ){
97027     sqlite3SetString(pzErrMsg, db, "cannot VACUUM from within a transaction");
97028     return SQLITE_ERROR;
97029   }
97030   if( db->activeVdbeCnt>1 ){
97031     sqlite3SetString(pzErrMsg, db,"cannot VACUUM - SQL statements in progress");
97032     return SQLITE_ERROR;
97033   }
97034 
97035   /* Save the current value of the database flags so that it can be
97036   ** restored before returning. Then set the writable-schema flag, and
97037   ** disable CHECK and foreign key constraints.  */
97038   saved_flags = db->flags;
97039   saved_nChange = db->nChange;
97040   saved_nTotalChange = db->nTotalChange;
97041   saved_xTrace = db->xTrace;
97042   db->flags |= SQLITE_WriteSchema | SQLITE_IgnoreChecks | SQLITE_PreferBuiltin;
97043   db->flags &= ~(SQLITE_ForeignKeys | SQLITE_ReverseOrder);
97044   db->xTrace = 0;
97045 
97046   pMain = db->aDb[0].pBt;
97047   isMemDb = sqlite3PagerIsMemdb(sqlite3BtreePager(pMain));
97048 
97049   /* Attach the temporary database as 'vacuum_db'. The synchronous pragma
97050   ** can be set to 'off' for this file, as it is not recovered if a crash
97051   ** occurs anyway. The integrity of the database is maintained by a
97052   ** (possibly synchronous) transaction opened on the main database before
97053   ** sqlite3BtreeCopyFile() is called.
97054   **
97055   ** An optimisation would be to use a non-journaled pager.
97056   ** (Later:) I tried setting "PRAGMA vacuum_db.journal_mode=OFF" but
97057   ** that actually made the VACUUM run slower.  Very little journalling
97058   ** actually occurs when doing a vacuum since the vacuum_db is initially
97059   ** empty.  Only the journal header is written.  Apparently it takes more
97060   ** time to parse and run the PRAGMA to turn journalling off than it does
97061   ** to write the journal header file.
97062   */
97063   nDb = db->nDb;
97064   if( sqlite3TempInMemory(db) ){
97065     zSql = "ATTACH ':memory:' AS vacuum_db;";
97066   }else{
97067     zSql = "ATTACH '' AS vacuum_db;";
97068   }
97069   rc = execSql(db, pzErrMsg, zSql);
97070   if( db->nDb>nDb ){
97071     pDb = &db->aDb[db->nDb-1];
97072     assert( strcmp(pDb->zName,"vacuum_db")==0 );
97073   }
97074   if( rc!=SQLITE_OK ) goto end_of_vacuum;
97075   pTemp = db->aDb[db->nDb-1].pBt;
97076 
97077   /* The call to execSql() to attach the temp database has left the file
97078   ** locked (as there was more than one active statement when the transaction
97079   ** to read the schema was concluded. Unlock it here so that this doesn't
97080   ** cause problems for the call to BtreeSetPageSize() below.  */
97081   sqlite3BtreeCommit(pTemp);
97082 
97083   nRes = sqlite3BtreeGetReserve(pMain);
97084 
97085   /* A VACUUM cannot change the pagesize of an encrypted database. */
97086 #ifdef SQLITE_HAS_CODEC
97087   if( db->nextPagesize ){
97088     extern void sqlite3CodecGetKey(sqlite3*, int, void**, int*);
97089     int nKey;
97090     char *zKey;
97091     sqlite3CodecGetKey(db, 0, (void**)&zKey, &nKey);
97092     if( nKey ) db->nextPagesize = 0;
97093   }
97094 #endif
97095 
97096   /* Do not attempt to change the page size for a WAL database */
97097   if( sqlite3PagerGetJournalMode(sqlite3BtreePager(pMain))
97098                                                ==PAGER_JOURNALMODE_WAL ){
97099     db->nextPagesize = 0;
97100   }
97101 
97102   if( sqlite3BtreeSetPageSize(pTemp, sqlite3BtreeGetPageSize(pMain), nRes, 0)
97103    || (!isMemDb && sqlite3BtreeSetPageSize(pTemp, db->nextPagesize, nRes, 0))
97104    || NEVER(db->mallocFailed)
97105   ){
97106     rc = SQLITE_NOMEM;
97107     goto end_of_vacuum;
97108   }
97109   rc = execSql(db, pzErrMsg, "PRAGMA vacuum_db.synchronous=OFF");
97110   if( rc!=SQLITE_OK ){
97111     goto end_of_vacuum;
97112   }
97113 
97114 #ifndef SQLITE_OMIT_AUTOVACUUM
97115   sqlite3BtreeSetAutoVacuum(pTemp, db->nextAutovac>=0 ? db->nextAutovac :
97116                                            sqlite3BtreeGetAutoVacuum(pMain));
97117 #endif
97118 
97119   /* Begin a transaction */
97120   rc = execSql(db, pzErrMsg, "BEGIN EXCLUSIVE;");
97121   if( rc!=SQLITE_OK ) goto end_of_vacuum;
97122 
97123   /* Query the schema of the main database. Create a mirror schema
97124   ** in the temporary database.
97125   */
97126   rc = execExecSql(db, pzErrMsg,
97127       "SELECT 'CREATE TABLE vacuum_db.' || substr(sql,14) "
97128       "  FROM sqlite_master WHERE type='table' AND name!='sqlite_sequence'"
97129       "   AND rootpage>0"
97130   );
97131   if( rc!=SQLITE_OK ) goto end_of_vacuum;
97132   rc = execExecSql(db, pzErrMsg,
97133       "SELECT 'CREATE INDEX vacuum_db.' || substr(sql,14)"
97134       "  FROM sqlite_master WHERE sql LIKE 'CREATE INDEX %' ");
97135   if( rc!=SQLITE_OK ) goto end_of_vacuum;
97136   rc = execExecSql(db, pzErrMsg,
97137       "SELECT 'CREATE UNIQUE INDEX vacuum_db.' || substr(sql,21) "
97138       "  FROM sqlite_master WHERE sql LIKE 'CREATE UNIQUE INDEX %'");
97139   if( rc!=SQLITE_OK ) goto end_of_vacuum;
97140 
97141   /* Loop through the tables in the main database. For each, do
97142   ** an "INSERT INTO vacuum_db.xxx SELECT * FROM main.xxx;" to copy
97143   ** the contents to the temporary database.
97144   */
97145   rc = execExecSql(db, pzErrMsg,
97146       "SELECT 'INSERT INTO vacuum_db.' || quote(name) "
97147       "|| ' SELECT * FROM main.' || quote(name) || ';'"
97148       "FROM main.sqlite_master "
97149       "WHERE type = 'table' AND name!='sqlite_sequence' "
97150       "  AND rootpage>0"
97151   );
97152   if( rc!=SQLITE_OK ) goto end_of_vacuum;
97153 
97154   /* Copy over the sequence table
97155   */
97156   rc = execExecSql(db, pzErrMsg,
97157       "SELECT 'DELETE FROM vacuum_db.' || quote(name) || ';' "
97158       "FROM vacuum_db.sqlite_master WHERE name='sqlite_sequence' "
97159   );
97160   if( rc!=SQLITE_OK ) goto end_of_vacuum;
97161   rc = execExecSql(db, pzErrMsg,
97162       "SELECT 'INSERT INTO vacuum_db.' || quote(name) "
97163       "|| ' SELECT * FROM main.' || quote(name) || ';' "
97164       "FROM vacuum_db.sqlite_master WHERE name=='sqlite_sequence';"
97165   );
97166   if( rc!=SQLITE_OK ) goto end_of_vacuum;
97167 
97168 
97169   /* Copy the triggers, views, and virtual tables from the main database
97170   ** over to the temporary database.  None of these objects has any
97171   ** associated storage, so all we have to do is copy their entries
97172   ** from the SQLITE_MASTER table.
97173   */
97174   rc = execSql(db, pzErrMsg,
97175       "INSERT INTO vacuum_db.sqlite_master "
97176       "  SELECT type, name, tbl_name, rootpage, sql"
97177       "    FROM main.sqlite_master"
97178       "   WHERE type='view' OR type='trigger'"
97179       "      OR (type='table' AND rootpage=0)"
97180   );
97181   if( rc ) goto end_of_vacuum;
97182 
97183   /* At this point, unless the main db was completely empty, there is now a
97184   ** transaction open on the vacuum database, but not on the main database.
97185   ** Open a btree level transaction on the main database. This allows a
97186   ** call to sqlite3BtreeCopyFile(). The main database btree level
97187   ** transaction is then committed, so the SQL level never knows it was
97188   ** opened for writing. This way, the SQL transaction used to create the
97189   ** temporary database never needs to be committed.
97190   */
97191   {
97192     u32 meta;
97193     int i;
97194 
97195     /* This array determines which meta meta values are preserved in the
97196     ** vacuum.  Even entries are the meta value number and odd entries
97197     ** are an increment to apply to the meta value after the vacuum.
97198     ** The increment is used to increase the schema cookie so that other
97199     ** connections to the same database will know to reread the schema.
97200     */
97201     static const unsigned char aCopy[] = {
97202        BTREE_SCHEMA_VERSION,     1,  /* Add one to the old schema cookie */
97203        BTREE_DEFAULT_CACHE_SIZE, 0,  /* Preserve the default page cache size */
97204        BTREE_TEXT_ENCODING,      0,  /* Preserve the text encoding */
97205        BTREE_USER_VERSION,       0,  /* Preserve the user version */
97206     };
97207 
97208     assert( 1==sqlite3BtreeIsInTrans(pTemp) );
97209     assert( 1==sqlite3BtreeIsInTrans(pMain) );
97210 
97211     /* Copy Btree meta values */
97212     for(i=0; i<ArraySize(aCopy); i+=2){
97213       /* GetMeta() and UpdateMeta() cannot fail in this context because
97214       ** we already have page 1 loaded into cache and marked dirty. */
97215       sqlite3BtreeGetMeta(pMain, aCopy[i], &meta);
97216       rc = sqlite3BtreeUpdateMeta(pTemp, aCopy[i], meta+aCopy[i+1]);
97217       if( NEVER(rc!=SQLITE_OK) ) goto end_of_vacuum;
97218     }
97219 
97220     rc = sqlite3BtreeCopyFile(pMain, pTemp);
97221     if( rc!=SQLITE_OK ) goto end_of_vacuum;
97222     rc = sqlite3BtreeCommit(pTemp);
97223     if( rc!=SQLITE_OK ) goto end_of_vacuum;
97224 #ifndef SQLITE_OMIT_AUTOVACUUM
97225     sqlite3BtreeSetAutoVacuum(pMain, sqlite3BtreeGetAutoVacuum(pTemp));
97226 #endif
97227   }
97228 
97229   assert( rc==SQLITE_OK );
97230   rc = sqlite3BtreeSetPageSize(pMain, sqlite3BtreeGetPageSize(pTemp), nRes,1);
97231 
97232 end_of_vacuum:
97233   /* Restore the original value of db->flags */
97234   db->flags = saved_flags;
97235   db->nChange = saved_nChange;
97236   db->nTotalChange = saved_nTotalChange;
97237   db->xTrace = saved_xTrace;
97238   sqlite3BtreeSetPageSize(pMain, -1, -1, 1);
97239 
97240   /* Currently there is an SQL level transaction open on the vacuum
97241   ** database. No locks are held on any other files (since the main file
97242   ** was committed at the btree level). So it safe to end the transaction
97243   ** by manually setting the autoCommit flag to true and detaching the
97244   ** vacuum database. The vacuum_db journal file is deleted when the pager
97245   ** is closed by the DETACH.
97246   */
97247   db->autoCommit = 1;
97248 
97249   if( pDb ){
97250     sqlite3BtreeClose(pDb->pBt);
97251     pDb->pBt = 0;
97252     pDb->pSchema = 0;
97253   }
97254 
97255   /* This both clears the schemas and reduces the size of the db->aDb[]
97256   ** array. */
97257   sqlite3ResetInternalSchema(db, -1);
97258 
97259   return rc;
97260 }
97261 
97262 #endif  /* SQLITE_OMIT_VACUUM && SQLITE_OMIT_ATTACH */
97263 
97264 /************** End of vacuum.c **********************************************/
97265 /************** Begin file vtab.c ********************************************/
97266 /*
97267 ** 2006 June 10
97268 **
97269 ** The author disclaims copyright to this source code.  In place of
97270 ** a legal notice, here is a blessing:
97271 **
97272 **    May you do good and not evil.
97273 **    May you find forgiveness for yourself and forgive others.
97274 **    May you share freely, never taking more than you give.
97275 **
97276 *************************************************************************
97277 ** This file contains code used to help implement virtual tables.
97278 */
97279 #ifndef SQLITE_OMIT_VIRTUALTABLE
97280 
97281 /*
97282 ** Before a virtual table xCreate() or xConnect() method is invoked, the
97283 ** sqlite3.pVtabCtx member variable is set to point to an instance of
97284 ** this struct allocated on the stack. It is used by the implementation of
97285 ** the sqlite3_declare_vtab() and sqlite3_vtab_config() APIs, both of which
97286 ** are invoked only from within xCreate and xConnect methods.
97287 */
97288 struct VtabCtx {
97289   Table *pTab;
97290   VTable *pVTable;
97291 };
97292 
97293 /*
97294 ** The actual function that does the work of creating a new module.
97295 ** This function implements the sqlite3_create_module() and
97296 ** sqlite3_create_module_v2() interfaces.
97297 */
97298 static int createModule(
97299   sqlite3 *db,                    /* Database in which module is registered */
97300   const char *zName,              /* Name assigned to this module */
97301   const sqlite3_module *pModule,  /* The definition of the module */
97302   void *pAux,                     /* Context pointer for xCreate/xConnect */
97303   void (*xDestroy)(void *)        /* Module destructor function */
97304 ){
97305   int rc, nName;
97306   Module *pMod;
97307 
97308   sqlite3_mutex_enter(db->mutex);
97309   nName = sqlite3Strlen30(zName);
97310   pMod = (Module *)sqlite3DbMallocRaw(db, sizeof(Module) + nName + 1);
97311   if( pMod ){
97312     Module *pDel;
97313     char *zCopy = (char *)(&pMod[1]);
97314     memcpy(zCopy, zName, nName+1);
97315     pMod->zName = zCopy;
97316     pMod->pModule = pModule;
97317     pMod->pAux = pAux;
97318     pMod->xDestroy = xDestroy;
97319     pDel = (Module *)sqlite3HashInsert(&db->aModule, zCopy, nName, (void*)pMod);
97320     if( pDel && pDel->xDestroy ){
97321       sqlite3ResetInternalSchema(db, -1);
97322       pDel->xDestroy(pDel->pAux);
97323     }
97324     sqlite3DbFree(db, pDel);
97325     if( pDel==pMod ){
97326       db->mallocFailed = 1;
97327     }
97328   }else if( xDestroy ){
97329     xDestroy(pAux);
97330   }
97331   rc = sqlite3ApiExit(db, SQLITE_OK);
97332   sqlite3_mutex_leave(db->mutex);
97333   return rc;
97334 }
97335 
97336 
97337 /*
97338 ** External API function used to create a new virtual-table module.
97339 */
97340 SQLITE_API int sqlite3_create_module(
97341   sqlite3 *db,                    /* Database in which module is registered */
97342   const char *zName,              /* Name assigned to this module */
97343   const sqlite3_module *pModule,  /* The definition of the module */
97344   void *pAux                      /* Context pointer for xCreate/xConnect */
97345 ){
97346   return createModule(db, zName, pModule, pAux, 0);
97347 }
97348 
97349 /*
97350 ** External API function used to create a new virtual-table module.
97351 */
97352 SQLITE_API int sqlite3_create_module_v2(
97353   sqlite3 *db,                    /* Database in which module is registered */
97354   const char *zName,              /* Name assigned to this module */
97355   const sqlite3_module *pModule,  /* The definition of the module */
97356   void *pAux,                     /* Context pointer for xCreate/xConnect */
97357   void (*xDestroy)(void *)        /* Module destructor function */
97358 ){
97359   return createModule(db, zName, pModule, pAux, xDestroy);
97360 }
97361 
97362 /*
97363 ** Lock the virtual table so that it cannot be disconnected.
97364 ** Locks nest.  Every lock should have a corresponding unlock.
97365 ** If an unlock is omitted, resources leaks will occur.
97366 **
97367 ** If a disconnect is attempted while a virtual table is locked,
97368 ** the disconnect is deferred until all locks have been removed.
97369 */
97370 SQLITE_PRIVATE void sqlite3VtabLock(VTable *pVTab){
97371   pVTab->nRef++;
97372 }
97373 
97374 
97375 /*
97376 ** pTab is a pointer to a Table structure representing a virtual-table.
97377 ** Return a pointer to the VTable object used by connection db to access
97378 ** this virtual-table, if one has been created, or NULL otherwise.
97379 */
97380 SQLITE_PRIVATE VTable *sqlite3GetVTable(sqlite3 *db, Table *pTab){
97381   VTable *pVtab;
97382   assert( IsVirtual(pTab) );
97383   for(pVtab=pTab->pVTable; pVtab && pVtab->db!=db; pVtab=pVtab->pNext);
97384   return pVtab;
97385 }
97386 
97387 /*
97388 ** Decrement the ref-count on a virtual table object. When the ref-count
97389 ** reaches zero, call the xDisconnect() method to delete the object.
97390 */
97391 SQLITE_PRIVATE void sqlite3VtabUnlock(VTable *pVTab){
97392   sqlite3 *db = pVTab->db;
97393 
97394   assert( db );
97395   assert( pVTab->nRef>0 );
97396   assert( sqlite3SafetyCheckOk(db) );
97397 
97398   pVTab->nRef--;
97399   if( pVTab->nRef==0 ){
97400     sqlite3_vtab *p = pVTab->pVtab;
97401     if( p ){
97402       p->pModule->xDisconnect(p);
97403     }
97404     sqlite3DbFree(db, pVTab);
97405   }
97406 }
97407 
97408 /*
97409 ** Table p is a virtual table. This function moves all elements in the
97410 ** p->pVTable list to the sqlite3.pDisconnect lists of their associated
97411 ** database connections to be disconnected at the next opportunity.
97412 ** Except, if argument db is not NULL, then the entry associated with
97413 ** connection db is left in the p->pVTable list.
97414 */
97415 static VTable *vtabDisconnectAll(sqlite3 *db, Table *p){
97416   VTable *pRet = 0;
97417   VTable *pVTable = p->pVTable;
97418   p->pVTable = 0;
97419 
97420   /* Assert that the mutex (if any) associated with the BtShared database
97421   ** that contains table p is held by the caller. See header comments
97422   ** above function sqlite3VtabUnlockList() for an explanation of why
97423   ** this makes it safe to access the sqlite3.pDisconnect list of any
97424   ** database connection that may have an entry in the p->pVTable list.
97425   */
97426   assert( db==0 || sqlite3SchemaMutexHeld(db, 0, p->pSchema) );
97427 
97428   while( pVTable ){
97429     sqlite3 *db2 = pVTable->db;
97430     VTable *pNext = pVTable->pNext;
97431     assert( db2 );
97432     if( db2==db ){
97433       pRet = pVTable;
97434       p->pVTable = pRet;
97435       pRet->pNext = 0;
97436     }else{
97437       pVTable->pNext = db2->pDisconnect;
97438       db2->pDisconnect = pVTable;
97439     }
97440     pVTable = pNext;
97441   }
97442 
97443   assert( !db || pRet );
97444   return pRet;
97445 }
97446 
97447 
97448 /*
97449 ** Disconnect all the virtual table objects in the sqlite3.pDisconnect list.
97450 **
97451 ** This function may only be called when the mutexes associated with all
97452 ** shared b-tree databases opened using connection db are held by the
97453 ** caller. This is done to protect the sqlite3.pDisconnect list. The
97454 ** sqlite3.pDisconnect list is accessed only as follows:
97455 **
97456 **   1) By this function. In this case, all BtShared mutexes and the mutex
97457 **      associated with the database handle itself must be held.
97458 **
97459 **   2) By function vtabDisconnectAll(), when it adds a VTable entry to
97460 **      the sqlite3.pDisconnect list. In this case either the BtShared mutex
97461 **      associated with the database the virtual table is stored in is held
97462 **      or, if the virtual table is stored in a non-sharable database, then
97463 **      the database handle mutex is held.
97464 **
97465 ** As a result, a sqlite3.pDisconnect cannot be accessed simultaneously
97466 ** by multiple threads. It is thread-safe.
97467 */
97468 SQLITE_PRIVATE void sqlite3VtabUnlockList(sqlite3 *db){
97469   VTable *p = db->pDisconnect;
97470   db->pDisconnect = 0;
97471 
97472   assert( sqlite3BtreeHoldsAllMutexes(db) );
97473   assert( sqlite3_mutex_held(db->mutex) );
97474 
97475   if( p ){
97476     sqlite3ExpirePreparedStatements(db);
97477     do {
97478       VTable *pNext = p->pNext;
97479       sqlite3VtabUnlock(p);
97480       p = pNext;
97481     }while( p );
97482   }
97483 }
97484 
97485 /*
97486 ** Clear any and all virtual-table information from the Table record.
97487 ** This routine is called, for example, just before deleting the Table
97488 ** record.
97489 **
97490 ** Since it is a virtual-table, the Table structure contains a pointer
97491 ** to the head of a linked list of VTable structures. Each VTable
97492 ** structure is associated with a single sqlite3* user of the schema.
97493 ** The reference count of the VTable structure associated with database
97494 ** connection db is decremented immediately (which may lead to the
97495 ** structure being xDisconnected and free). Any other VTable structures
97496 ** in the list are moved to the sqlite3.pDisconnect list of the associated
97497 ** database connection.
97498 */
97499 SQLITE_PRIVATE void sqlite3VtabClear(sqlite3 *db, Table *p){
97500   if( !db || db->pnBytesFreed==0 ) vtabDisconnectAll(0, p);
97501   if( p->azModuleArg ){
97502     int i;
97503     for(i=0; i<p->nModuleArg; i++){
97504       sqlite3DbFree(db, p->azModuleArg[i]);
97505     }
97506     sqlite3DbFree(db, p->azModuleArg);
97507   }
97508 }
97509 
97510 /*
97511 ** Add a new module argument to pTable->azModuleArg[].
97512 ** The string is not copied - the pointer is stored.  The
97513 ** string will be freed automatically when the table is
97514 ** deleted.
97515 */
97516 static void addModuleArgument(sqlite3 *db, Table *pTable, char *zArg){
97517   int i = pTable->nModuleArg++;
97518   int nBytes = sizeof(char *)*(1+pTable->nModuleArg);
97519   char **azModuleArg;
97520   azModuleArg = sqlite3DbRealloc(db, pTable->azModuleArg, nBytes);
97521   if( azModuleArg==0 ){
97522     int j;
97523     for(j=0; j<i; j++){
97524       sqlite3DbFree(db, pTable->azModuleArg[j]);
97525     }
97526     sqlite3DbFree(db, zArg);
97527     sqlite3DbFree(db, pTable->azModuleArg);
97528     pTable->nModuleArg = 0;
97529   }else{
97530     azModuleArg[i] = zArg;
97531     azModuleArg[i+1] = 0;
97532   }
97533   pTable->azModuleArg = azModuleArg;
97534 }
97535 
97536 /*
97537 ** The parser calls this routine when it first sees a CREATE VIRTUAL TABLE
97538 ** statement.  The module name has been parsed, but the optional list
97539 ** of parameters that follow the module name are still pending.
97540 */
97541 SQLITE_PRIVATE void sqlite3VtabBeginParse(
97542   Parse *pParse,        /* Parsing context */
97543   Token *pName1,        /* Name of new table, or database name */
97544   Token *pName2,        /* Name of new table or NULL */
97545   Token *pModuleName    /* Name of the module for the virtual table */
97546 ){
97547   int iDb;              /* The database the table is being created in */
97548   Table *pTable;        /* The new virtual table */
97549   sqlite3 *db;          /* Database connection */
97550 
97551   sqlite3StartTable(pParse, pName1, pName2, 0, 0, 1, 0);
97552   pTable = pParse->pNewTable;
97553   if( pTable==0 ) return;
97554   assert( 0==pTable->pIndex );
97555 
97556   db = pParse->db;
97557   iDb = sqlite3SchemaToIndex(db, pTable->pSchema);
97558   assert( iDb>=0 );
97559 
97560   pTable->tabFlags |= TF_Virtual;
97561   pTable->nModuleArg = 0;
97562   addModuleArgument(db, pTable, sqlite3NameFromToken(db, pModuleName));
97563   addModuleArgument(db, pTable, sqlite3DbStrDup(db, db->aDb[iDb].zName));
97564   addModuleArgument(db, pTable, sqlite3DbStrDup(db, pTable->zName));
97565   pParse->sNameToken.n = (int)(&pModuleName->z[pModuleName->n] - pName1->z);
97566 
97567 #ifndef SQLITE_OMIT_AUTHORIZATION
97568   /* Creating a virtual table invokes the authorization callback twice.
97569   ** The first invocation, to obtain permission to INSERT a row into the
97570   ** sqlite_master table, has already been made by sqlite3StartTable().
97571   ** The second call, to obtain permission to create the table, is made now.
97572   */
97573   if( pTable->azModuleArg ){
97574     sqlite3AuthCheck(pParse, SQLITE_CREATE_VTABLE, pTable->zName,
97575             pTable->azModuleArg[0], pParse->db->aDb[iDb].zName);
97576   }
97577 #endif
97578 }
97579 
97580 /*
97581 ** This routine takes the module argument that has been accumulating
97582 ** in pParse->zArg[] and appends it to the list of arguments on the
97583 ** virtual table currently under construction in pParse->pTable.
97584 */
97585 static void addArgumentToVtab(Parse *pParse){
97586   if( pParse->sArg.z && ALWAYS(pParse->pNewTable) ){
97587     const char *z = (const char*)pParse->sArg.z;
97588     int n = pParse->sArg.n;
97589     sqlite3 *db = pParse->db;
97590     addModuleArgument(db, pParse->pNewTable, sqlite3DbStrNDup(db, z, n));
97591   }
97592 }
97593 
97594 /*
97595 ** The parser calls this routine after the CREATE VIRTUAL TABLE statement
97596 ** has been completely parsed.
97597 */
97598 SQLITE_PRIVATE void sqlite3VtabFinishParse(Parse *pParse, Token *pEnd){
97599   Table *pTab = pParse->pNewTable;  /* The table being constructed */
97600   sqlite3 *db = pParse->db;         /* The database connection */
97601 
97602   if( pTab==0 ) return;
97603   addArgumentToVtab(pParse);
97604   pParse->sArg.z = 0;
97605   if( pTab->nModuleArg<1 ) return;
97606 
97607   /* If the CREATE VIRTUAL TABLE statement is being entered for the
97608   ** first time (in other words if the virtual table is actually being
97609   ** created now instead of just being read out of sqlite_master) then
97610   ** do additional initialization work and store the statement text
97611   ** in the sqlite_master table.
97612   */
97613   if( !db->init.busy ){
97614     char *zStmt;
97615     char *zWhere;
97616     int iDb;
97617     Vdbe *v;
97618 
97619     /* Compute the complete text of the CREATE VIRTUAL TABLE statement */
97620     if( pEnd ){
97621       pParse->sNameToken.n = (int)(pEnd->z - pParse->sNameToken.z) + pEnd->n;
97622     }
97623     zStmt = sqlite3MPrintf(db, "CREATE VIRTUAL TABLE %T", &pParse->sNameToken);
97624 
97625     /* A slot for the record has already been allocated in the
97626     ** SQLITE_MASTER table.  We just need to update that slot with all
97627     ** the information we've collected.
97628     **
97629     ** The VM register number pParse->regRowid holds the rowid of an
97630     ** entry in the sqlite_master table tht was created for this vtab
97631     ** by sqlite3StartTable().
97632     */
97633     iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
97634     sqlite3NestedParse(pParse,
97635       "UPDATE %Q.%s "
97636          "SET type='table', name=%Q, tbl_name=%Q, rootpage=0, sql=%Q "
97637        "WHERE rowid=#%d",
97638       db->aDb[iDb].zName, SCHEMA_TABLE(iDb),
97639       pTab->zName,
97640       pTab->zName,
97641       zStmt,
97642       pParse->regRowid
97643     );
97644     sqlite3DbFree(db, zStmt);
97645     v = sqlite3GetVdbe(pParse);
97646     sqlite3ChangeCookie(pParse, iDb);
97647 
97648     sqlite3VdbeAddOp2(v, OP_Expire, 0, 0);
97649     zWhere = sqlite3MPrintf(db, "name='%q' AND type='table'", pTab->zName);
97650     sqlite3VdbeAddParseSchemaOp(v, iDb, zWhere);
97651     sqlite3VdbeAddOp4(v, OP_VCreate, iDb, 0, 0,
97652                          pTab->zName, sqlite3Strlen30(pTab->zName) + 1);
97653   }
97654 
97655   /* If we are rereading the sqlite_master table create the in-memory
97656   ** record of the table. The xConnect() method is not called until
97657   ** the first time the virtual table is used in an SQL statement. This
97658   ** allows a schema that contains virtual tables to be loaded before
97659   ** the required virtual table implementations are registered.  */
97660   else {
97661     Table *pOld;
97662     Schema *pSchema = pTab->pSchema;
97663     const char *zName = pTab->zName;
97664     int nName = sqlite3Strlen30(zName);
97665     assert( sqlite3SchemaMutexHeld(db, 0, pSchema) );
97666     pOld = sqlite3HashInsert(&pSchema->tblHash, zName, nName, pTab);
97667     if( pOld ){
97668       db->mallocFailed = 1;
97669       assert( pTab==pOld );  /* Malloc must have failed inside HashInsert() */
97670       return;
97671     }
97672     pParse->pNewTable = 0;
97673   }
97674 }
97675 
97676 /*
97677 ** The parser calls this routine when it sees the first token
97678 ** of an argument to the module name in a CREATE VIRTUAL TABLE statement.
97679 */
97680 SQLITE_PRIVATE void sqlite3VtabArgInit(Parse *pParse){
97681   addArgumentToVtab(pParse);
97682   pParse->sArg.z = 0;
97683   pParse->sArg.n = 0;
97684 }
97685 
97686 /*
97687 ** The parser calls this routine for each token after the first token
97688 ** in an argument to the module name in a CREATE VIRTUAL TABLE statement.
97689 */
97690 SQLITE_PRIVATE void sqlite3VtabArgExtend(Parse *pParse, Token *p){
97691   Token *pArg = &pParse->sArg;
97692   if( pArg->z==0 ){
97693     pArg->z = p->z;
97694     pArg->n = p->n;
97695   }else{
97696     assert(pArg->z < p->z);
97697     pArg->n = (int)(&p->z[p->n] - pArg->z);
97698   }
97699 }
97700 
97701 /*
97702 ** Invoke a virtual table constructor (either xCreate or xConnect). The
97703 ** pointer to the function to invoke is passed as the fourth parameter
97704 ** to this procedure.
97705 */
97706 static int vtabCallConstructor(
97707   sqlite3 *db,
97708   Table *pTab,
97709   Module *pMod,
97710   int (*xConstruct)(sqlite3*,void*,int,const char*const*,sqlite3_vtab**,char**),
97711   char **pzErr
97712 ){
97713   VtabCtx sCtx;
97714   VTable *pVTable;
97715   int rc;
97716   const char *const*azArg = (const char *const*)pTab->azModuleArg;
97717   int nArg = pTab->nModuleArg;
97718   char *zErr = 0;
97719   char *zModuleName = sqlite3MPrintf(db, "%s", pTab->zName);
97720 
97721   if( !zModuleName ){
97722     return SQLITE_NOMEM;
97723   }
97724 
97725   pVTable = sqlite3DbMallocZero(db, sizeof(VTable));
97726   if( !pVTable ){
97727     sqlite3DbFree(db, zModuleName);
97728     return SQLITE_NOMEM;
97729   }
97730   pVTable->db = db;
97731   pVTable->pMod = pMod;
97732 
97733   /* Invoke the virtual table constructor */
97734   assert( &db->pVtabCtx );
97735   assert( xConstruct );
97736   sCtx.pTab = pTab;
97737   sCtx.pVTable = pVTable;
97738   db->pVtabCtx = &sCtx;
97739   rc = xConstruct(db, pMod->pAux, nArg, azArg, &pVTable->pVtab, &zErr);
97740   db->pVtabCtx = 0;
97741   if( rc==SQLITE_NOMEM ) db->mallocFailed = 1;
97742 
97743   if( SQLITE_OK!=rc ){
97744     if( zErr==0 ){
97745       *pzErr = sqlite3MPrintf(db, "vtable constructor failed: %s", zModuleName);
97746     }else {
97747       *pzErr = sqlite3MPrintf(db, "%s", zErr);
97748       sqlite3_free(zErr);
97749     }
97750     sqlite3DbFree(db, pVTable);
97751   }else if( ALWAYS(pVTable->pVtab) ){
97752     /* Justification of ALWAYS():  A correct vtab constructor must allocate
97753     ** the sqlite3_vtab object if successful.  */
97754     pVTable->pVtab->pModule = pMod->pModule;
97755     pVTable->nRef = 1;
97756     if( sCtx.pTab ){
97757       const char *zFormat = "vtable constructor did not declare schema: %s";
97758       *pzErr = sqlite3MPrintf(db, zFormat, pTab->zName);
97759       sqlite3VtabUnlock(pVTable);
97760       rc = SQLITE_ERROR;
97761     }else{
97762       int iCol;
97763       /* If everything went according to plan, link the new VTable structure
97764       ** into the linked list headed by pTab->pVTable. Then loop through the
97765       ** columns of the table to see if any of them contain the token "hidden".
97766       ** If so, set the Column.isHidden flag and remove the token from
97767       ** the type string.  */
97768       pVTable->pNext = pTab->pVTable;
97769       pTab->pVTable = pVTable;
97770 
97771       for(iCol=0; iCol<pTab->nCol; iCol++){
97772         char *zType = pTab->aCol[iCol].zType;
97773         int nType;
97774         int i = 0;
97775         if( !zType ) continue;
97776         nType = sqlite3Strlen30(zType);
97777         if( sqlite3StrNICmp("hidden", zType, 6)||(zType[6] && zType[6]!=' ') ){
97778           for(i=0; i<nType; i++){
97779             if( (0==sqlite3StrNICmp(" hidden", &zType[i], 7))
97780              && (zType[i+7]=='\0' || zType[i+7]==' ')
97781             ){
97782               i++;
97783               break;
97784             }
97785           }
97786         }
97787         if( i<nType ){
97788           int j;
97789           int nDel = 6 + (zType[i+6] ? 1 : 0);
97790           for(j=i; (j+nDel)<=nType; j++){
97791             zType[j] = zType[j+nDel];
97792           }
97793           if( zType[i]=='\0' && i>0 ){
97794             assert(zType[i-1]==' ');
97795             zType[i-1] = '\0';
97796           }
97797           pTab->aCol[iCol].isHidden = 1;
97798         }
97799       }
97800     }
97801   }
97802 
97803   sqlite3DbFree(db, zModuleName);
97804   return rc;
97805 }
97806 
97807 /*
97808 ** This function is invoked by the parser to call the xConnect() method
97809 ** of the virtual table pTab. If an error occurs, an error code is returned
97810 ** and an error left in pParse.
97811 **
97812 ** This call is a no-op if table pTab is not a virtual table.
97813 */
97814 SQLITE_PRIVATE int sqlite3VtabCallConnect(Parse *pParse, Table *pTab){
97815   sqlite3 *db = pParse->db;
97816   const char *zMod;
97817   Module *pMod;
97818   int rc;
97819 
97820   assert( pTab );
97821   if( (pTab->tabFlags & TF_Virtual)==0 || sqlite3GetVTable(db, pTab) ){
97822     return SQLITE_OK;
97823   }
97824 
97825   /* Locate the required virtual table module */
97826   zMod = pTab->azModuleArg[0];
97827   pMod = (Module*)sqlite3HashFind(&db->aModule, zMod, sqlite3Strlen30(zMod));
97828 
97829   if( !pMod ){
97830     const char *zModule = pTab->azModuleArg[0];
97831     sqlite3ErrorMsg(pParse, "no such module: %s", zModule);
97832     rc = SQLITE_ERROR;
97833   }else{
97834     char *zErr = 0;
97835     rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xConnect, &zErr);
97836     if( rc!=SQLITE_OK ){
97837       sqlite3ErrorMsg(pParse, "%s", zErr);
97838     }
97839     sqlite3DbFree(db, zErr);
97840   }
97841 
97842   return rc;
97843 }
97844 /*
97845 ** Grow the db->aVTrans[] array so that there is room for at least one
97846 ** more v-table. Return SQLITE_NOMEM if a malloc fails, or SQLITE_OK otherwise.
97847 */
97848 static int growVTrans(sqlite3 *db){
97849   const int ARRAY_INCR = 5;
97850 
97851   /* Grow the sqlite3.aVTrans array if required */
97852   if( (db->nVTrans%ARRAY_INCR)==0 ){
97853     VTable **aVTrans;
97854     int nBytes = sizeof(sqlite3_vtab *) * (db->nVTrans + ARRAY_INCR);
97855     aVTrans = sqlite3DbRealloc(db, (void *)db->aVTrans, nBytes);
97856     if( !aVTrans ){
97857       return SQLITE_NOMEM;
97858     }
97859     memset(&aVTrans[db->nVTrans], 0, sizeof(sqlite3_vtab *)*ARRAY_INCR);
97860     db->aVTrans = aVTrans;
97861   }
97862 
97863   return SQLITE_OK;
97864 }
97865 
97866 /*
97867 ** Add the virtual table pVTab to the array sqlite3.aVTrans[]. Space should
97868 ** have already been reserved using growVTrans().
97869 */
97870 static void addToVTrans(sqlite3 *db, VTable *pVTab){
97871   /* Add pVtab to the end of sqlite3.aVTrans */
97872   db->aVTrans[db->nVTrans++] = pVTab;
97873   sqlite3VtabLock(pVTab);
97874 }
97875 
97876 /*
97877 ** This function is invoked by the vdbe to call the xCreate method
97878 ** of the virtual table named zTab in database iDb.
97879 **
97880 ** If an error occurs, *pzErr is set to point an an English language
97881 ** description of the error and an SQLITE_XXX error code is returned.
97882 ** In this case the caller must call sqlite3DbFree(db, ) on *pzErr.
97883 */
97884 SQLITE_PRIVATE int sqlite3VtabCallCreate(sqlite3 *db, int iDb, const char *zTab, char **pzErr){
97885   int rc = SQLITE_OK;
97886   Table *pTab;
97887   Module *pMod;
97888   const char *zMod;
97889 
97890   pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName);
97891   assert( pTab && (pTab->tabFlags & TF_Virtual)!=0 && !pTab->pVTable );
97892 
97893   /* Locate the required virtual table module */
97894   zMod = pTab->azModuleArg[0];
97895   pMod = (Module*)sqlite3HashFind(&db->aModule, zMod, sqlite3Strlen30(zMod));
97896 
97897   /* If the module has been registered and includes a Create method,
97898   ** invoke it now. If the module has not been registered, return an
97899   ** error. Otherwise, do nothing.
97900   */
97901   if( !pMod ){
97902     *pzErr = sqlite3MPrintf(db, "no such module: %s", zMod);
97903     rc = SQLITE_ERROR;
97904   }else{
97905     rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xCreate, pzErr);
97906   }
97907 
97908   /* Justification of ALWAYS():  The xConstructor method is required to
97909   ** create a valid sqlite3_vtab if it returns SQLITE_OK. */
97910   if( rc==SQLITE_OK && ALWAYS(sqlite3GetVTable(db, pTab)) ){
97911     rc = growVTrans(db);
97912     if( rc==SQLITE_OK ){
97913       addToVTrans(db, sqlite3GetVTable(db, pTab));
97914     }
97915   }
97916 
97917   return rc;
97918 }
97919 
97920 /*
97921 ** This function is used to set the schema of a virtual table.  It is only
97922 ** valid to call this function from within the xCreate() or xConnect() of a
97923 ** virtual table module.
97924 */
97925 SQLITE_API int sqlite3_declare_vtab(sqlite3 *db, const char *zCreateTable){
97926   Parse *pParse;
97927 
97928   int rc = SQLITE_OK;
97929   Table *pTab;
97930   char *zErr = 0;
97931 
97932   sqlite3_mutex_enter(db->mutex);
97933   if( !db->pVtabCtx || !(pTab = db->pVtabCtx->pTab) ){
97934     sqlite3Error(db, SQLITE_MISUSE, 0);
97935     sqlite3_mutex_leave(db->mutex);
97936     return SQLITE_MISUSE_BKPT;
97937   }
97938   assert( (pTab->tabFlags & TF_Virtual)!=0 );
97939 
97940   pParse = sqlite3StackAllocZero(db, sizeof(*pParse));
97941   if( pParse==0 ){
97942     rc = SQLITE_NOMEM;
97943   }else{
97944     pParse->declareVtab = 1;
97945     pParse->db = db;
97946     pParse->nQueryLoop = 1;
97947 
97948     if( SQLITE_OK==sqlite3RunParser(pParse, zCreateTable, &zErr)
97949      && pParse->pNewTable
97950      && !db->mallocFailed
97951      && !pParse->pNewTable->pSelect
97952      && (pParse->pNewTable->tabFlags & TF_Virtual)==0
97953     ){
97954       if( !pTab->aCol ){
97955         pTab->aCol = pParse->pNewTable->aCol;
97956         pTab->nCol = pParse->pNewTable->nCol;
97957         pParse->pNewTable->nCol = 0;
97958         pParse->pNewTable->aCol = 0;
97959       }
97960       db->pVtabCtx->pTab = 0;
97961     }else{
97962       sqlite3Error(db, SQLITE_ERROR, (zErr ? "%s" : 0), zErr);
97963       sqlite3DbFree(db, zErr);
97964       rc = SQLITE_ERROR;
97965     }
97966     pParse->declareVtab = 0;
97967 
97968     if( pParse->pVdbe ){
97969       sqlite3VdbeFinalize(pParse->pVdbe);
97970     }
97971     sqlite3DeleteTable(db, pParse->pNewTable);
97972     sqlite3StackFree(db, pParse);
97973   }
97974 
97975   assert( (rc&0xff)==rc );
97976   rc = sqlite3ApiExit(db, rc);
97977   sqlite3_mutex_leave(db->mutex);
97978   return rc;
97979 }
97980 
97981 /*
97982 ** This function is invoked by the vdbe to call the xDestroy method
97983 ** of the virtual table named zTab in database iDb. This occurs
97984 ** when a DROP TABLE is mentioned.
97985 **
97986 ** This call is a no-op if zTab is not a virtual table.
97987 */
97988 SQLITE_PRIVATE int sqlite3VtabCallDestroy(sqlite3 *db, int iDb, const char *zTab){
97989   int rc = SQLITE_OK;
97990   Table *pTab;
97991 
97992   pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName);
97993   if( ALWAYS(pTab!=0 && pTab->pVTable!=0) ){
97994     VTable *p = vtabDisconnectAll(db, pTab);
97995 
97996     assert( rc==SQLITE_OK );
97997     rc = p->pMod->pModule->xDestroy(p->pVtab);
97998 
97999     /* Remove the sqlite3_vtab* from the aVTrans[] array, if applicable */
98000     if( rc==SQLITE_OK ){
98001       assert( pTab->pVTable==p && p->pNext==0 );
98002       p->pVtab = 0;
98003       pTab->pVTable = 0;
98004       sqlite3VtabUnlock(p);
98005     }
98006   }
98007 
98008   return rc;
98009 }
98010 
98011 /*
98012 ** This function invokes either the xRollback or xCommit method
98013 ** of each of the virtual tables in the sqlite3.aVTrans array. The method
98014 ** called is identified by the second argument, "offset", which is
98015 ** the offset of the method to call in the sqlite3_module structure.
98016 **
98017 ** The array is cleared after invoking the callbacks.
98018 */
98019 static void callFinaliser(sqlite3 *db, int offset){
98020   int i;
98021   if( db->aVTrans ){
98022     for(i=0; i<db->nVTrans; i++){
98023       VTable *pVTab = db->aVTrans[i];
98024       sqlite3_vtab *p = pVTab->pVtab;
98025       if( p ){
98026         int (*x)(sqlite3_vtab *);
98027         x = *(int (**)(sqlite3_vtab *))((char *)p->pModule + offset);
98028         if( x ) x(p);
98029       }
98030       pVTab->iSavepoint = 0;
98031       sqlite3VtabUnlock(pVTab);
98032     }
98033     sqlite3DbFree(db, db->aVTrans);
98034     db->nVTrans = 0;
98035     db->aVTrans = 0;
98036   }
98037 }
98038 
98039 /*
98040 ** Invoke the xSync method of all virtual tables in the sqlite3.aVTrans
98041 ** array. Return the error code for the first error that occurs, or
98042 ** SQLITE_OK if all xSync operations are successful.
98043 **
98044 ** Set *pzErrmsg to point to a buffer that should be released using
98045 ** sqlite3DbFree() containing an error message, if one is available.
98046 */
98047 SQLITE_PRIVATE int sqlite3VtabSync(sqlite3 *db, char **pzErrmsg){
98048   int i;
98049   int rc = SQLITE_OK;
98050   VTable **aVTrans = db->aVTrans;
98051 
98052   db->aVTrans = 0;
98053   for(i=0; rc==SQLITE_OK && i<db->nVTrans; i++){
98054     int (*x)(sqlite3_vtab *);
98055     sqlite3_vtab *pVtab = aVTrans[i]->pVtab;
98056     if( pVtab && (x = pVtab->pModule->xSync)!=0 ){
98057       rc = x(pVtab);
98058       sqlite3DbFree(db, *pzErrmsg);
98059       *pzErrmsg = sqlite3DbStrDup(db, pVtab->zErrMsg);
98060       sqlite3_free(pVtab->zErrMsg);
98061     }
98062   }
98063   db->aVTrans = aVTrans;
98064   return rc;
98065 }
98066 
98067 /*
98068 ** Invoke the xRollback method of all virtual tables in the
98069 ** sqlite3.aVTrans array. Then clear the array itself.
98070 */
98071 SQLITE_PRIVATE int sqlite3VtabRollback(sqlite3 *db){
98072   callFinaliser(db, offsetof(sqlite3_module,xRollback));
98073   return SQLITE_OK;
98074 }
98075 
98076 /*
98077 ** Invoke the xCommit method of all virtual tables in the
98078 ** sqlite3.aVTrans array. Then clear the array itself.
98079 */
98080 SQLITE_PRIVATE int sqlite3VtabCommit(sqlite3 *db){
98081   callFinaliser(db, offsetof(sqlite3_module,xCommit));
98082   return SQLITE_OK;
98083 }
98084 
98085 /*
98086 ** If the virtual table pVtab supports the transaction interface
98087 ** (xBegin/xRollback/xCommit and optionally xSync) and a transaction is
98088 ** not currently open, invoke the xBegin method now.
98089 **
98090 ** If the xBegin call is successful, place the sqlite3_vtab pointer
98091 ** in the sqlite3.aVTrans array.
98092 */
98093 SQLITE_PRIVATE int sqlite3VtabBegin(sqlite3 *db, VTable *pVTab){
98094   int rc = SQLITE_OK;
98095   const sqlite3_module *pModule;
98096 
98097   /* Special case: If db->aVTrans is NULL and db->nVTrans is greater
98098   ** than zero, then this function is being called from within a
98099   ** virtual module xSync() callback. It is illegal to write to
98100   ** virtual module tables in this case, so return SQLITE_LOCKED.
98101   */
98102   if( sqlite3VtabInSync(db) ){
98103     return SQLITE_LOCKED;
98104   }
98105   if( !pVTab ){
98106     return SQLITE_OK;
98107   }
98108   pModule = pVTab->pVtab->pModule;
98109 
98110   if( pModule->xBegin ){
98111     int i;
98112 
98113     /* If pVtab is already in the aVTrans array, return early */
98114     for(i=0; i<db->nVTrans; i++){
98115       if( db->aVTrans[i]==pVTab ){
98116         return SQLITE_OK;
98117       }
98118     }
98119 
98120     /* Invoke the xBegin method. If successful, add the vtab to the
98121     ** sqlite3.aVTrans[] array. */
98122     rc = growVTrans(db);
98123     if( rc==SQLITE_OK ){
98124       rc = pModule->xBegin(pVTab->pVtab);
98125       if( rc==SQLITE_OK ){
98126         addToVTrans(db, pVTab);
98127       }
98128     }
98129   }
98130   return rc;
98131 }
98132 
98133 /*
98134 ** Invoke either the xSavepoint, xRollbackTo or xRelease method of all
98135 ** virtual tables that currently have an open transaction. Pass iSavepoint
98136 ** as the second argument to the virtual table method invoked.
98137 **
98138 ** If op is SAVEPOINT_BEGIN, the xSavepoint method is invoked. If it is
98139 ** SAVEPOINT_ROLLBACK, the xRollbackTo method. Otherwise, if op is
98140 ** SAVEPOINT_RELEASE, then the xRelease method of each virtual table with
98141 ** an open transaction is invoked.
98142 **
98143 ** If any virtual table method returns an error code other than SQLITE_OK,
98144 ** processing is abandoned and the error returned to the caller of this
98145 ** function immediately. If all calls to virtual table methods are successful,
98146 ** SQLITE_OK is returned.
98147 */
98148 SQLITE_PRIVATE int sqlite3VtabSavepoint(sqlite3 *db, int op, int iSavepoint){
98149   int rc = SQLITE_OK;
98150 
98151   assert( op==SAVEPOINT_RELEASE||op==SAVEPOINT_ROLLBACK||op==SAVEPOINT_BEGIN );
98152   assert( iSavepoint>=0 );
98153   if( db->aVTrans ){
98154     int i;
98155     for(i=0; rc==SQLITE_OK && i<db->nVTrans; i++){
98156       VTable *pVTab = db->aVTrans[i];
98157       const sqlite3_module *pMod = pVTab->pMod->pModule;
98158       if( pMod->iVersion>=2 ){
98159         int (*xMethod)(sqlite3_vtab *, int);
98160         switch( op ){
98161           case SAVEPOINT_BEGIN:
98162             xMethod = pMod->xSavepoint;
98163             pVTab->iSavepoint = iSavepoint+1;
98164             break;
98165           case SAVEPOINT_ROLLBACK:
98166             xMethod = pMod->xRollbackTo;
98167             break;
98168           default:
98169             xMethod = pMod->xRelease;
98170             break;
98171         }
98172         if( xMethod && pVTab->iSavepoint>iSavepoint ){
98173           rc = xMethod(db->aVTrans[i]->pVtab, iSavepoint);
98174         }
98175       }
98176     }
98177   }
98178   return rc;
98179 }
98180 
98181 /*
98182 ** The first parameter (pDef) is a function implementation.  The
98183 ** second parameter (pExpr) is the first argument to this function.
98184 ** If pExpr is a column in a virtual table, then let the virtual
98185 ** table implementation have an opportunity to overload the function.
98186 **
98187 ** This routine is used to allow virtual table implementations to
98188 ** overload MATCH, LIKE, GLOB, and REGEXP operators.
98189 **
98190 ** Return either the pDef argument (indicating no change) or a
98191 ** new FuncDef structure that is marked as ephemeral using the
98192 ** SQLITE_FUNC_EPHEM flag.
98193 */
98194 SQLITE_PRIVATE FuncDef *sqlite3VtabOverloadFunction(
98195   sqlite3 *db,    /* Database connection for reporting malloc problems */
98196   FuncDef *pDef,  /* Function to possibly overload */
98197   int nArg,       /* Number of arguments to the function */
98198   Expr *pExpr     /* First argument to the function */
98199 ){
98200   Table *pTab;
98201   sqlite3_vtab *pVtab;
98202   sqlite3_module *pMod;
98203   void (*xFunc)(sqlite3_context*,int,sqlite3_value**) = 0;
98204   void *pArg = 0;
98205   FuncDef *pNew;
98206   int rc = 0;
98207   char *zLowerName;
98208   unsigned char *z;
98209 
98210 
98211   /* Check to see the left operand is a column in a virtual table */
98212   if( NEVER(pExpr==0) ) return pDef;
98213   if( pExpr->op!=TK_COLUMN ) return pDef;
98214   pTab = pExpr->pTab;
98215   if( NEVER(pTab==0) ) return pDef;
98216   if( (pTab->tabFlags & TF_Virtual)==0 ) return pDef;
98217   pVtab = sqlite3GetVTable(db, pTab)->pVtab;
98218   assert( pVtab!=0 );
98219   assert( pVtab->pModule!=0 );
98220   pMod = (sqlite3_module *)pVtab->pModule;
98221   if( pMod->xFindFunction==0 ) return pDef;
98222 
98223   /* Call the xFindFunction method on the virtual table implementation
98224   ** to see if the implementation wants to overload this function
98225   */
98226   zLowerName = sqlite3DbStrDup(db, pDef->zName);
98227   if( zLowerName ){
98228     for(z=(unsigned char*)zLowerName; *z; z++){
98229       *z = sqlite3UpperToLower[*z];
98230     }
98231     rc = pMod->xFindFunction(pVtab, nArg, zLowerName, &xFunc, &pArg);
98232     sqlite3DbFree(db, zLowerName);
98233   }
98234   if( rc==0 ){
98235     return pDef;
98236   }
98237 
98238   /* Create a new ephemeral function definition for the overloaded
98239   ** function */
98240   pNew = sqlite3DbMallocZero(db, sizeof(*pNew)
98241                              + sqlite3Strlen30(pDef->zName) + 1);
98242   if( pNew==0 ){
98243     return pDef;
98244   }
98245   *pNew = *pDef;
98246   pNew->zName = (char *)&pNew[1];
98247   memcpy(pNew->zName, pDef->zName, sqlite3Strlen30(pDef->zName)+1);
98248   pNew->xFunc = xFunc;
98249   pNew->pUserData = pArg;
98250   pNew->flags |= SQLITE_FUNC_EPHEM;
98251   return pNew;
98252 }
98253 
98254 /*
98255 ** Make sure virtual table pTab is contained in the pParse->apVirtualLock[]
98256 ** array so that an OP_VBegin will get generated for it.  Add pTab to the
98257 ** array if it is missing.  If pTab is already in the array, this routine
98258 ** is a no-op.
98259 */
98260 SQLITE_PRIVATE void sqlite3VtabMakeWritable(Parse *pParse, Table *pTab){
98261   Parse *pToplevel = sqlite3ParseToplevel(pParse);
98262   int i, n;
98263   Table **apVtabLock;
98264 
98265   assert( IsVirtual(pTab) );
98266   for(i=0; i<pToplevel->nVtabLock; i++){
98267     if( pTab==pToplevel->apVtabLock[i] ) return;
98268   }
98269   n = (pToplevel->nVtabLock+1)*sizeof(pToplevel->apVtabLock[0]);
98270   apVtabLock = sqlite3_realloc(pToplevel->apVtabLock, n);
98271   if( apVtabLock ){
98272     pToplevel->apVtabLock = apVtabLock;
98273     pToplevel->apVtabLock[pToplevel->nVtabLock++] = pTab;
98274   }else{
98275     pToplevel->db->mallocFailed = 1;
98276   }
98277 }
98278 
98279 /*
98280 ** Return the ON CONFLICT resolution mode in effect for the virtual
98281 ** table update operation currently in progress.
98282 **
98283 ** The results of this routine are undefined unless it is called from
98284 ** within an xUpdate method.
98285 */
98286 SQLITE_API int sqlite3_vtab_on_conflict(sqlite3 *db){
98287   static const unsigned char aMap[] = {
98288     SQLITE_ROLLBACK, SQLITE_ABORT, SQLITE_FAIL, SQLITE_IGNORE, SQLITE_REPLACE
98289   };
98290   assert( OE_Rollback==1 && OE_Abort==2 && OE_Fail==3 );
98291   assert( OE_Ignore==4 && OE_Replace==5 );
98292   assert( db->vtabOnConflict>=1 && db->vtabOnConflict<=5 );
98293   return (int)aMap[db->vtabOnConflict-1];
98294 }
98295 
98296 /*
98297 ** Call from within the xCreate() or xConnect() methods to provide
98298 ** the SQLite core with additional information about the behavior
98299 ** of the virtual table being implemented.
98300 */
98301 SQLITE_API int sqlite3_vtab_config(sqlite3 *db, int op, ...){
98302   va_list ap;
98303   int rc = SQLITE_OK;
98304 
98305   sqlite3_mutex_enter(db->mutex);
98306 
98307   va_start(ap, op);
98308   switch( op ){
98309     case SQLITE_VTAB_CONSTRAINT_SUPPORT: {
98310       VtabCtx *p = db->pVtabCtx;
98311       if( !p ){
98312         rc = SQLITE_MISUSE_BKPT;
98313       }else{
98314         assert( p->pTab==0 || (p->pTab->tabFlags & TF_Virtual)!=0 );
98315         p->pVTable->bConstraint = (u8)va_arg(ap, int);
98316       }
98317       break;
98318     }
98319     default:
98320       rc = SQLITE_MISUSE_BKPT;
98321       break;
98322   }
98323   va_end(ap);
98324 
98325   if( rc!=SQLITE_OK ) sqlite3Error(db, rc, 0);
98326   sqlite3_mutex_leave(db->mutex);
98327   return rc;
98328 }
98329 
98330 #endif /* SQLITE_OMIT_VIRTUALTABLE */
98331 
98332 /************** End of vtab.c ************************************************/
98333 /************** Begin file where.c *******************************************/
98334 /*
98335 ** 2001 September 15
98336 **
98337 ** The author disclaims copyright to this source code.  In place of
98338 ** a legal notice, here is a blessing:
98339 **
98340 **    May you do good and not evil.
98341 **    May you find forgiveness for yourself and forgive others.
98342 **    May you share freely, never taking more than you give.
98343 **
98344 *************************************************************************
98345 ** This module contains C code that generates VDBE code used to process
98346 ** the WHERE clause of SQL statements.  This module is responsible for
98347 ** generating the code that loops through a table looking for applicable
98348 ** rows.  Indices are selected and used to speed the search when doing
98349 ** so is applicable.  Because this module is responsible for selecting
98350 ** indices, you might also think of this module as the "query optimizer".
98351 */
98352 
98353 
98354 /*
98355 ** Trace output macros
98356 */
98357 #if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
98358 SQLITE_PRIVATE int sqlite3WhereTrace = 0;
98359 #endif
98360 #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
98361 # define WHERETRACE(X)  if(sqlite3WhereTrace) sqlite3DebugPrintf X
98362 #else
98363 # define WHERETRACE(X)
98364 #endif
98365 
98366 /* Forward reference
98367 */
98368 typedef struct WhereClause WhereClause;
98369 typedef struct WhereMaskSet WhereMaskSet;
98370 typedef struct WhereOrInfo WhereOrInfo;
98371 typedef struct WhereAndInfo WhereAndInfo;
98372 typedef struct WhereCost WhereCost;
98373 
98374 /*
98375 ** The query generator uses an array of instances of this structure to
98376 ** help it analyze the subexpressions of the WHERE clause.  Each WHERE
98377 ** clause subexpression is separated from the others by AND operators,
98378 ** usually, or sometimes subexpressions separated by OR.
98379 **
98380 ** All WhereTerms are collected into a single WhereClause structure.
98381 ** The following identity holds:
98382 **
98383 **        WhereTerm.pWC->a[WhereTerm.idx] == WhereTerm
98384 **
98385 ** When a term is of the form:
98386 **
98387 **              X <op> <expr>
98388 **
98389 ** where X is a column name and <op> is one of certain operators,
98390 ** then WhereTerm.leftCursor and WhereTerm.u.leftColumn record the
98391 ** cursor number and column number for X.  WhereTerm.eOperator records
98392 ** the <op> using a bitmask encoding defined by WO_xxx below.  The
98393 ** use of a bitmask encoding for the operator allows us to search
98394 ** quickly for terms that match any of several different operators.
98395 **
98396 ** A WhereTerm might also be two or more subterms connected by OR:
98397 **
98398 **         (t1.X <op> <expr>) OR (t1.Y <op> <expr>) OR ....
98399 **
98400 ** In this second case, wtFlag as the TERM_ORINFO set and eOperator==WO_OR
98401 ** and the WhereTerm.u.pOrInfo field points to auxiliary information that
98402 ** is collected about the
98403 **
98404 ** If a term in the WHERE clause does not match either of the two previous
98405 ** categories, then eOperator==0.  The WhereTerm.pExpr field is still set
98406 ** to the original subexpression content and wtFlags is set up appropriately
98407 ** but no other fields in the WhereTerm object are meaningful.
98408 **
98409 ** When eOperator!=0, prereqRight and prereqAll record sets of cursor numbers,
98410 ** but they do so indirectly.  A single WhereMaskSet structure translates
98411 ** cursor number into bits and the translated bit is stored in the prereq
98412 ** fields.  The translation is used in order to maximize the number of
98413 ** bits that will fit in a Bitmask.  The VDBE cursor numbers might be
98414 ** spread out over the non-negative integers.  For example, the cursor
98415 ** numbers might be 3, 8, 9, 10, 20, 23, 41, and 45.  The WhereMaskSet
98416 ** translates these sparse cursor numbers into consecutive integers
98417 ** beginning with 0 in order to make the best possible use of the available
98418 ** bits in the Bitmask.  So, in the example above, the cursor numbers
98419 ** would be mapped into integers 0 through 7.
98420 **
98421 ** The number of terms in a join is limited by the number of bits
98422 ** in prereqRight and prereqAll.  The default is 64 bits, hence SQLite
98423 ** is only able to process joins with 64 or fewer tables.
98424 */
98425 typedef struct WhereTerm WhereTerm;
98426 struct WhereTerm {
98427   Expr *pExpr;            /* Pointer to the subexpression that is this term */
98428   int iParent;            /* Disable pWC->a[iParent] when this term disabled */
98429   int leftCursor;         /* Cursor number of X in "X <op> <expr>" */
98430   union {
98431     int leftColumn;         /* Column number of X in "X <op> <expr>" */
98432     WhereOrInfo *pOrInfo;   /* Extra information if eOperator==WO_OR */
98433     WhereAndInfo *pAndInfo; /* Extra information if eOperator==WO_AND */
98434   } u;
98435   u16 eOperator;          /* A WO_xx value describing <op> */
98436   u8 wtFlags;             /* TERM_xxx bit flags.  See below */
98437   u8 nChild;              /* Number of children that must disable us */
98438   WhereClause *pWC;       /* The clause this term is part of */
98439   Bitmask prereqRight;    /* Bitmask of tables used by pExpr->pRight */
98440   Bitmask prereqAll;      /* Bitmask of tables referenced by pExpr */
98441 };
98442 
98443 /*
98444 ** Allowed values of WhereTerm.wtFlags
98445 */
98446 #define TERM_DYNAMIC    0x01   /* Need to call sqlite3ExprDelete(db, pExpr) */
98447 #define TERM_VIRTUAL    0x02   /* Added by the optimizer.  Do not code */
98448 #define TERM_CODED      0x04   /* This term is already coded */
98449 #define TERM_COPIED     0x08   /* Has a child */
98450 #define TERM_ORINFO     0x10   /* Need to free the WhereTerm.u.pOrInfo object */
98451 #define TERM_ANDINFO    0x20   /* Need to free the WhereTerm.u.pAndInfo obj */
98452 #define TERM_OR_OK      0x40   /* Used during OR-clause processing */
98453 #ifdef SQLITE_ENABLE_STAT2
98454 #  define TERM_VNULL    0x80   /* Manufactured x>NULL or x<=NULL term */
98455 #else
98456 #  define TERM_VNULL    0x00   /* Disabled if not using stat2 */
98457 #endif
98458 
98459 /*
98460 ** An instance of the following structure holds all information about a
98461 ** WHERE clause.  Mostly this is a container for one or more WhereTerms.
98462 */
98463 struct WhereClause {
98464   Parse *pParse;           /* The parser context */
98465   WhereMaskSet *pMaskSet;  /* Mapping of table cursor numbers to bitmasks */
98466   Bitmask vmask;           /* Bitmask identifying virtual table cursors */
98467   u8 op;                   /* Split operator.  TK_AND or TK_OR */
98468   int nTerm;               /* Number of terms */
98469   int nSlot;               /* Number of entries in a[] */
98470   WhereTerm *a;            /* Each a[] describes a term of the WHERE cluase */
98471 #if defined(SQLITE_SMALL_STACK)
98472   WhereTerm aStatic[1];    /* Initial static space for a[] */
98473 #else
98474   WhereTerm aStatic[8];    /* Initial static space for a[] */
98475 #endif
98476 };
98477 
98478 /*
98479 ** A WhereTerm with eOperator==WO_OR has its u.pOrInfo pointer set to
98480 ** a dynamically allocated instance of the following structure.
98481 */
98482 struct WhereOrInfo {
98483   WhereClause wc;          /* Decomposition into subterms */
98484   Bitmask indexable;       /* Bitmask of all indexable tables in the clause */
98485 };
98486 
98487 /*
98488 ** A WhereTerm with eOperator==WO_AND has its u.pAndInfo pointer set to
98489 ** a dynamically allocated instance of the following structure.
98490 */
98491 struct WhereAndInfo {
98492   WhereClause wc;          /* The subexpression broken out */
98493 };
98494 
98495 /*
98496 ** An instance of the following structure keeps track of a mapping
98497 ** between VDBE cursor numbers and bits of the bitmasks in WhereTerm.
98498 **
98499 ** The VDBE cursor numbers are small integers contained in
98500 ** SrcList_item.iCursor and Expr.iTable fields.  For any given WHERE
98501 ** clause, the cursor numbers might not begin with 0 and they might
98502 ** contain gaps in the numbering sequence.  But we want to make maximum
98503 ** use of the bits in our bitmasks.  This structure provides a mapping
98504 ** from the sparse cursor numbers into consecutive integers beginning
98505 ** with 0.
98506 **
98507 ** If WhereMaskSet.ix[A]==B it means that The A-th bit of a Bitmask
98508 ** corresponds VDBE cursor number B.  The A-th bit of a bitmask is 1<<A.
98509 **
98510 ** For example, if the WHERE clause expression used these VDBE
98511 ** cursors:  4, 5, 8, 29, 57, 73.  Then the  WhereMaskSet structure
98512 ** would map those cursor numbers into bits 0 through 5.
98513 **
98514 ** Note that the mapping is not necessarily ordered.  In the example
98515 ** above, the mapping might go like this:  4->3, 5->1, 8->2, 29->0,
98516 ** 57->5, 73->4.  Or one of 719 other combinations might be used. It
98517 ** does not really matter.  What is important is that sparse cursor
98518 ** numbers all get mapped into bit numbers that begin with 0 and contain
98519 ** no gaps.
98520 */
98521 struct WhereMaskSet {
98522   int n;                        /* Number of assigned cursor values */
98523   int ix[BMS];                  /* Cursor assigned to each bit */
98524 };
98525 
98526 /*
98527 ** A WhereCost object records a lookup strategy and the estimated
98528 ** cost of pursuing that strategy.
98529 */
98530 struct WhereCost {
98531   WherePlan plan;    /* The lookup strategy */
98532   double rCost;      /* Overall cost of pursuing this search strategy */
98533   Bitmask used;      /* Bitmask of cursors used by this plan */
98534 };
98535 
98536 /*
98537 ** Bitmasks for the operators that indices are able to exploit.  An
98538 ** OR-ed combination of these values can be used when searching for
98539 ** terms in the where clause.
98540 */
98541 #define WO_IN     0x001
98542 #define WO_EQ     0x002
98543 #define WO_LT     (WO_EQ<<(TK_LT-TK_EQ))
98544 #define WO_LE     (WO_EQ<<(TK_LE-TK_EQ))
98545 #define WO_GT     (WO_EQ<<(TK_GT-TK_EQ))
98546 #define WO_GE     (WO_EQ<<(TK_GE-TK_EQ))
98547 #define WO_MATCH  0x040
98548 #define WO_ISNULL 0x080
98549 #define WO_OR     0x100       /* Two or more OR-connected terms */
98550 #define WO_AND    0x200       /* Two or more AND-connected terms */
98551 #define WO_NOOP   0x800       /* This term does not restrict search space */
98552 
98553 #define WO_ALL    0xfff       /* Mask of all possible WO_* values */
98554 #define WO_SINGLE 0x0ff       /* Mask of all non-compound WO_* values */
98555 
98556 /*
98557 ** Value for wsFlags returned by bestIndex() and stored in
98558 ** WhereLevel.wsFlags.  These flags determine which search
98559 ** strategies are appropriate.
98560 **
98561 ** The least significant 12 bits is reserved as a mask for WO_ values above.
98562 ** The WhereLevel.wsFlags field is usually set to WO_IN|WO_EQ|WO_ISNULL.
98563 ** But if the table is the right table of a left join, WhereLevel.wsFlags
98564 ** is set to WO_IN|WO_EQ.  The WhereLevel.wsFlags field can then be used as
98565 ** the "op" parameter to findTerm when we are resolving equality constraints.
98566 ** ISNULL constraints will then not be used on the right table of a left
98567 ** join.  Tickets #2177 and #2189.
98568 */
98569 #define WHERE_ROWID_EQ     0x00001000  /* rowid=EXPR or rowid IN (...) */
98570 #define WHERE_ROWID_RANGE  0x00002000  /* rowid<EXPR and/or rowid>EXPR */
98571 #define WHERE_COLUMN_EQ    0x00010000  /* x=EXPR or x IN (...) or x IS NULL */
98572 #define WHERE_COLUMN_RANGE 0x00020000  /* x<EXPR and/or x>EXPR */
98573 #define WHERE_COLUMN_IN    0x00040000  /* x IN (...) */
98574 #define WHERE_COLUMN_NULL  0x00080000  /* x IS NULL */
98575 #define WHERE_INDEXED      0x000f0000  /* Anything that uses an index */
98576 #define WHERE_NOT_FULLSCAN 0x100f3000  /* Does not do a full table scan */
98577 #define WHERE_IN_ABLE      0x000f1000  /* Able to support an IN operator */
98578 #define WHERE_TOP_LIMIT    0x00100000  /* x<EXPR or x<=EXPR constraint */
98579 #define WHERE_BTM_LIMIT    0x00200000  /* x>EXPR or x>=EXPR constraint */
98580 #define WHERE_BOTH_LIMIT   0x00300000  /* Both x>EXPR and x<EXPR */
98581 #define WHERE_IDX_ONLY     0x00800000  /* Use index only - omit table */
98582 #define WHERE_ORDERBY      0x01000000  /* Output will appear in correct order */
98583 #define WHERE_REVERSE      0x02000000  /* Scan in reverse order */
98584 #define WHERE_UNIQUE       0x04000000  /* Selects no more than one row */
98585 #define WHERE_VIRTUALTABLE 0x08000000  /* Use virtual-table processing */
98586 #define WHERE_MULTI_OR     0x10000000  /* OR using multiple indices */
98587 #define WHERE_TEMP_INDEX   0x20000000  /* Uses an ephemeral index */
98588 
98589 /*
98590 ** Initialize a preallocated WhereClause structure.
98591 */
98592 static void whereClauseInit(
98593   WhereClause *pWC,        /* The WhereClause to be initialized */
98594   Parse *pParse,           /* The parsing context */
98595   WhereMaskSet *pMaskSet   /* Mapping from table cursor numbers to bitmasks */
98596 ){
98597   pWC->pParse = pParse;
98598   pWC->pMaskSet = pMaskSet;
98599   pWC->nTerm = 0;
98600   pWC->nSlot = ArraySize(pWC->aStatic);
98601   pWC->a = pWC->aStatic;
98602   pWC->vmask = 0;
98603 }
98604 
98605 /* Forward reference */
98606 static void whereClauseClear(WhereClause*);
98607 
98608 /*
98609 ** Deallocate all memory associated with a WhereOrInfo object.
98610 */
98611 static void whereOrInfoDelete(sqlite3 *db, WhereOrInfo *p){
98612   whereClauseClear(&p->wc);
98613   sqlite3DbFree(db, p);
98614 }
98615 
98616 /*
98617 ** Deallocate all memory associated with a WhereAndInfo object.
98618 */
98619 static void whereAndInfoDelete(sqlite3 *db, WhereAndInfo *p){
98620   whereClauseClear(&p->wc);
98621   sqlite3DbFree(db, p);
98622 }
98623 
98624 /*
98625 ** Deallocate a WhereClause structure.  The WhereClause structure
98626 ** itself is not freed.  This routine is the inverse of whereClauseInit().
98627 */
98628 static void whereClauseClear(WhereClause *pWC){
98629   int i;
98630   WhereTerm *a;
98631   sqlite3 *db = pWC->pParse->db;
98632   for(i=pWC->nTerm-1, a=pWC->a; i>=0; i--, a++){
98633     if( a->wtFlags & TERM_DYNAMIC ){
98634       sqlite3ExprDelete(db, a->pExpr);
98635     }
98636     if( a->wtFlags & TERM_ORINFO ){
98637       whereOrInfoDelete(db, a->u.pOrInfo);
98638     }else if( a->wtFlags & TERM_ANDINFO ){
98639       whereAndInfoDelete(db, a->u.pAndInfo);
98640     }
98641   }
98642   if( pWC->a!=pWC->aStatic ){
98643     sqlite3DbFree(db, pWC->a);
98644   }
98645 }
98646 
98647 /*
98648 ** Add a single new WhereTerm entry to the WhereClause object pWC.
98649 ** The new WhereTerm object is constructed from Expr p and with wtFlags.
98650 ** The index in pWC->a[] of the new WhereTerm is returned on success.
98651 ** 0 is returned if the new WhereTerm could not be added due to a memory
98652 ** allocation error.  The memory allocation failure will be recorded in
98653 ** the db->mallocFailed flag so that higher-level functions can detect it.
98654 **
98655 ** This routine will increase the size of the pWC->a[] array as necessary.
98656 **
98657 ** If the wtFlags argument includes TERM_DYNAMIC, then responsibility
98658 ** for freeing the expression p is assumed by the WhereClause object pWC.
98659 ** This is true even if this routine fails to allocate a new WhereTerm.
98660 **
98661 ** WARNING:  This routine might reallocate the space used to store
98662 ** WhereTerms.  All pointers to WhereTerms should be invalidated after
98663 ** calling this routine.  Such pointers may be reinitialized by referencing
98664 ** the pWC->a[] array.
98665 */
98666 static int whereClauseInsert(WhereClause *pWC, Expr *p, u8 wtFlags){
98667   WhereTerm *pTerm;
98668   int idx;
98669   testcase( wtFlags & TERM_VIRTUAL );  /* EV: R-00211-15100 */
98670   if( pWC->nTerm>=pWC->nSlot ){
98671     WhereTerm *pOld = pWC->a;
98672     sqlite3 *db = pWC->pParse->db;
98673     pWC->a = sqlite3DbMallocRaw(db, sizeof(pWC->a[0])*pWC->nSlot*2 );
98674     if( pWC->a==0 ){
98675       if( wtFlags & TERM_DYNAMIC ){
98676         sqlite3ExprDelete(db, p);
98677       }
98678       pWC->a = pOld;
98679       return 0;
98680     }
98681     memcpy(pWC->a, pOld, sizeof(pWC->a[0])*pWC->nTerm);
98682     if( pOld!=pWC->aStatic ){
98683       sqlite3DbFree(db, pOld);
98684     }
98685     pWC->nSlot = sqlite3DbMallocSize(db, pWC->a)/sizeof(pWC->a[0]);
98686   }
98687   pTerm = &pWC->a[idx = pWC->nTerm++];
98688   pTerm->pExpr = p;
98689   pTerm->wtFlags = wtFlags;
98690   pTerm->pWC = pWC;
98691   pTerm->iParent = -1;
98692   return idx;
98693 }
98694 
98695 /*
98696 ** This routine identifies subexpressions in the WHERE clause where
98697 ** each subexpression is separated by the AND operator or some other
98698 ** operator specified in the op parameter.  The WhereClause structure
98699 ** is filled with pointers to subexpressions.  For example:
98700 **
98701 **    WHERE  a=='hello' AND coalesce(b,11)<10 AND (c+12!=d OR c==22)
98702 **           \________/     \_______________/     \________________/
98703 **            slot[0]            slot[1]               slot[2]
98704 **
98705 ** The original WHERE clause in pExpr is unaltered.  All this routine
98706 ** does is make slot[] entries point to substructure within pExpr.
98707 **
98708 ** In the previous sentence and in the diagram, "slot[]" refers to
98709 ** the WhereClause.a[] array.  The slot[] array grows as needed to contain
98710 ** all terms of the WHERE clause.
98711 */
98712 static void whereSplit(WhereClause *pWC, Expr *pExpr, int op){
98713   pWC->op = (u8)op;
98714   if( pExpr==0 ) return;
98715   if( pExpr->op!=op ){
98716     whereClauseInsert(pWC, pExpr, 0);
98717   }else{
98718     whereSplit(pWC, pExpr->pLeft, op);
98719     whereSplit(pWC, pExpr->pRight, op);
98720   }
98721 }
98722 
98723 /*
98724 ** Initialize an expression mask set (a WhereMaskSet object)
98725 */
98726 #define initMaskSet(P)  memset(P, 0, sizeof(*P))
98727 
98728 /*
98729 ** Return the bitmask for the given cursor number.  Return 0 if
98730 ** iCursor is not in the set.
98731 */
98732 static Bitmask getMask(WhereMaskSet *pMaskSet, int iCursor){
98733   int i;
98734   assert( pMaskSet->n<=(int)sizeof(Bitmask)*8 );
98735   for(i=0; i<pMaskSet->n; i++){
98736     if( pMaskSet->ix[i]==iCursor ){
98737       return ((Bitmask)1)<<i;
98738     }
98739   }
98740   return 0;
98741 }
98742 
98743 /*
98744 ** Create a new mask for cursor iCursor.
98745 **
98746 ** There is one cursor per table in the FROM clause.  The number of
98747 ** tables in the FROM clause is limited by a test early in the
98748 ** sqlite3WhereBegin() routine.  So we know that the pMaskSet->ix[]
98749 ** array will never overflow.
98750 */
98751 static void createMask(WhereMaskSet *pMaskSet, int iCursor){
98752   assert( pMaskSet->n < ArraySize(pMaskSet->ix) );
98753   pMaskSet->ix[pMaskSet->n++] = iCursor;
98754 }
98755 
98756 /*
98757 ** This routine walks (recursively) an expression tree and generates
98758 ** a bitmask indicating which tables are used in that expression
98759 ** tree.
98760 **
98761 ** In order for this routine to work, the calling function must have
98762 ** previously invoked sqlite3ResolveExprNames() on the expression.  See
98763 ** the header comment on that routine for additional information.
98764 ** The sqlite3ResolveExprNames() routines looks for column names and
98765 ** sets their opcodes to TK_COLUMN and their Expr.iTable fields to
98766 ** the VDBE cursor number of the table.  This routine just has to
98767 ** translate the cursor numbers into bitmask values and OR all
98768 ** the bitmasks together.
98769 */
98770 static Bitmask exprListTableUsage(WhereMaskSet*, ExprList*);
98771 static Bitmask exprSelectTableUsage(WhereMaskSet*, Select*);
98772 static Bitmask exprTableUsage(WhereMaskSet *pMaskSet, Expr *p){
98773   Bitmask mask = 0;
98774   if( p==0 ) return 0;
98775   if( p->op==TK_COLUMN ){
98776     mask = getMask(pMaskSet, p->iTable);
98777     return mask;
98778   }
98779   mask = exprTableUsage(pMaskSet, p->pRight);
98780   mask |= exprTableUsage(pMaskSet, p->pLeft);
98781   if( ExprHasProperty(p, EP_xIsSelect) ){
98782     mask |= exprSelectTableUsage(pMaskSet, p->x.pSelect);
98783   }else{
98784     mask |= exprListTableUsage(pMaskSet, p->x.pList);
98785   }
98786   return mask;
98787 }
98788 static Bitmask exprListTableUsage(WhereMaskSet *pMaskSet, ExprList *pList){
98789   int i;
98790   Bitmask mask = 0;
98791   if( pList ){
98792     for(i=0; i<pList->nExpr; i++){
98793       mask |= exprTableUsage(pMaskSet, pList->a[i].pExpr);
98794     }
98795   }
98796   return mask;
98797 }
98798 static Bitmask exprSelectTableUsage(WhereMaskSet *pMaskSet, Select *pS){
98799   Bitmask mask = 0;
98800   while( pS ){
98801     mask |= exprListTableUsage(pMaskSet, pS->pEList);
98802     mask |= exprListTableUsage(pMaskSet, pS->pGroupBy);
98803     mask |= exprListTableUsage(pMaskSet, pS->pOrderBy);
98804     mask |= exprTableUsage(pMaskSet, pS->pWhere);
98805     mask |= exprTableUsage(pMaskSet, pS->pHaving);
98806     pS = pS->pPrior;
98807   }
98808   return mask;
98809 }
98810 
98811 /*
98812 ** Return TRUE if the given operator is one of the operators that is
98813 ** allowed for an indexable WHERE clause term.  The allowed operators are
98814 ** "=", "<", ">", "<=", ">=", and "IN".
98815 **
98816 ** IMPLEMENTATION-OF: R-59926-26393 To be usable by an index a term must be
98817 ** of one of the following forms: column = expression column > expression
98818 ** column >= expression column < expression column <= expression
98819 ** expression = column expression > column expression >= column
98820 ** expression < column expression <= column column IN
98821 ** (expression-list) column IN (subquery) column IS NULL
98822 */
98823 static int allowedOp(int op){
98824   assert( TK_GT>TK_EQ && TK_GT<TK_GE );
98825   assert( TK_LT>TK_EQ && TK_LT<TK_GE );
98826   assert( TK_LE>TK_EQ && TK_LE<TK_GE );
98827   assert( TK_GE==TK_EQ+4 );
98828   return op==TK_IN || (op>=TK_EQ && op<=TK_GE) || op==TK_ISNULL;
98829 }
98830 
98831 /*
98832 ** Swap two objects of type TYPE.
98833 */
98834 #define SWAP(TYPE,A,B) {TYPE t=A; A=B; B=t;}
98835 
98836 /*
98837 ** Commute a comparison operator.  Expressions of the form "X op Y"
98838 ** are converted into "Y op X".
98839 **
98840 ** If a collation sequence is associated with either the left or right
98841 ** side of the comparison, it remains associated with the same side after
98842 ** the commutation. So "Y collate NOCASE op X" becomes
98843 ** "X collate NOCASE op Y". This is because any collation sequence on
98844 ** the left hand side of a comparison overrides any collation sequence
98845 ** attached to the right. For the same reason the EP_ExpCollate flag
98846 ** is not commuted.
98847 */
98848 static void exprCommute(Parse *pParse, Expr *pExpr){
98849   u16 expRight = (pExpr->pRight->flags & EP_ExpCollate);
98850   u16 expLeft = (pExpr->pLeft->flags & EP_ExpCollate);
98851   assert( allowedOp(pExpr->op) && pExpr->op!=TK_IN );
98852   pExpr->pRight->pColl = sqlite3ExprCollSeq(pParse, pExpr->pRight);
98853   pExpr->pLeft->pColl = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
98854   SWAP(CollSeq*,pExpr->pRight->pColl,pExpr->pLeft->pColl);
98855   pExpr->pRight->flags = (pExpr->pRight->flags & ~EP_ExpCollate) | expLeft;
98856   pExpr->pLeft->flags = (pExpr->pLeft->flags & ~EP_ExpCollate) | expRight;
98857   SWAP(Expr*,pExpr->pRight,pExpr->pLeft);
98858   if( pExpr->op>=TK_GT ){
98859     assert( TK_LT==TK_GT+2 );
98860     assert( TK_GE==TK_LE+2 );
98861     assert( TK_GT>TK_EQ );
98862     assert( TK_GT<TK_LE );
98863     assert( pExpr->op>=TK_GT && pExpr->op<=TK_GE );
98864     pExpr->op = ((pExpr->op-TK_GT)^2)+TK_GT;
98865   }
98866 }
98867 
98868 /*
98869 ** Translate from TK_xx operator to WO_xx bitmask.
98870 */
98871 static u16 operatorMask(int op){
98872   u16 c;
98873   assert( allowedOp(op) );
98874   if( op==TK_IN ){
98875     c = WO_IN;
98876   }else if( op==TK_ISNULL ){
98877     c = WO_ISNULL;
98878   }else{
98879     assert( (WO_EQ<<(op-TK_EQ)) < 0x7fff );
98880     c = (u16)(WO_EQ<<(op-TK_EQ));
98881   }
98882   assert( op!=TK_ISNULL || c==WO_ISNULL );
98883   assert( op!=TK_IN || c==WO_IN );
98884   assert( op!=TK_EQ || c==WO_EQ );
98885   assert( op!=TK_LT || c==WO_LT );
98886   assert( op!=TK_LE || c==WO_LE );
98887   assert( op!=TK_GT || c==WO_GT );
98888   assert( op!=TK_GE || c==WO_GE );
98889   return c;
98890 }
98891 
98892 /*
98893 ** Search for a term in the WHERE clause that is of the form "X <op> <expr>"
98894 ** where X is a reference to the iColumn of table iCur and <op> is one of
98895 ** the WO_xx operator codes specified by the op parameter.
98896 ** Return a pointer to the term.  Return 0 if not found.
98897 */
98898 static WhereTerm *findTerm(
98899   WhereClause *pWC,     /* The WHERE clause to be searched */
98900   int iCur,             /* Cursor number of LHS */
98901   int iColumn,          /* Column number of LHS */
98902   Bitmask notReady,     /* RHS must not overlap with this mask */
98903   u32 op,               /* Mask of WO_xx values describing operator */
98904   Index *pIdx           /* Must be compatible with this index, if not NULL */
98905 ){
98906   WhereTerm *pTerm;
98907   int k;
98908   assert( iCur>=0 );
98909   op &= WO_ALL;
98910   for(pTerm=pWC->a, k=pWC->nTerm; k; k--, pTerm++){
98911     if( pTerm->leftCursor==iCur
98912        && (pTerm->prereqRight & notReady)==0
98913        && pTerm->u.leftColumn==iColumn
98914        && (pTerm->eOperator & op)!=0
98915     ){
98916       if( pIdx && pTerm->eOperator!=WO_ISNULL ){
98917         Expr *pX = pTerm->pExpr;
98918         CollSeq *pColl;
98919         char idxaff;
98920         int j;
98921         Parse *pParse = pWC->pParse;
98922 
98923         idxaff = pIdx->pTable->aCol[iColumn].affinity;
98924         if( !sqlite3IndexAffinityOk(pX, idxaff) ) continue;
98925 
98926         /* Figure out the collation sequence required from an index for
98927         ** it to be useful for optimising expression pX. Store this
98928         ** value in variable pColl.
98929         */
98930         assert(pX->pLeft);
98931         pColl = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight);
98932         assert(pColl || pParse->nErr);
98933 
98934         for(j=0; pIdx->aiColumn[j]!=iColumn; j++){
98935           if( NEVER(j>=pIdx->nColumn) ) return 0;
98936         }
98937         if( pColl && sqlite3StrICmp(pColl->zName, pIdx->azColl[j]) ) continue;
98938       }
98939       return pTerm;
98940     }
98941   }
98942   return 0;
98943 }
98944 
98945 /* Forward reference */
98946 static void exprAnalyze(SrcList*, WhereClause*, int);
98947 
98948 /*
98949 ** Call exprAnalyze on all terms in a WHERE clause.
98950 **
98951 **
98952 */
98953 static void exprAnalyzeAll(
98954   SrcList *pTabList,       /* the FROM clause */
98955   WhereClause *pWC         /* the WHERE clause to be analyzed */
98956 ){
98957   int i;
98958   for(i=pWC->nTerm-1; i>=0; i--){
98959     exprAnalyze(pTabList, pWC, i);
98960   }
98961 }
98962 
98963 #ifndef SQLITE_OMIT_LIKE_OPTIMIZATION
98964 /*
98965 ** Check to see if the given expression is a LIKE or GLOB operator that
98966 ** can be optimized using inequality constraints.  Return TRUE if it is
98967 ** so and false if not.
98968 **
98969 ** In order for the operator to be optimizible, the RHS must be a string
98970 ** literal that does not begin with a wildcard.
98971 */
98972 static int isLikeOrGlob(
98973   Parse *pParse,    /* Parsing and code generating context */
98974   Expr *pExpr,      /* Test this expression */
98975   Expr **ppPrefix,  /* Pointer to TK_STRING expression with pattern prefix */
98976   int *pisComplete, /* True if the only wildcard is % in the last character */
98977   int *pnoCase      /* True if uppercase is equivalent to lowercase */
98978 ){
98979   const char *z = 0;         /* String on RHS of LIKE operator */
98980   Expr *pRight, *pLeft;      /* Right and left size of LIKE operator */
98981   ExprList *pList;           /* List of operands to the LIKE operator */
98982   int c;                     /* One character in z[] */
98983   int cnt;                   /* Number of non-wildcard prefix characters */
98984   char wc[3];                /* Wildcard characters */
98985   sqlite3 *db = pParse->db;  /* Database connection */
98986   sqlite3_value *pVal = 0;
98987   int op;                    /* Opcode of pRight */
98988 
98989   if( !sqlite3IsLikeFunction(db, pExpr, pnoCase, wc) ){
98990     return 0;
98991   }
98992 #ifdef SQLITE_EBCDIC
98993   if( *pnoCase ) return 0;
98994 #endif
98995   pList = pExpr->x.pList;
98996   pLeft = pList->a[1].pExpr;
98997   if( pLeft->op!=TK_COLUMN || sqlite3ExprAffinity(pLeft)!=SQLITE_AFF_TEXT ){
98998     /* IMP: R-02065-49465 The left-hand side of the LIKE or GLOB operator must
98999     ** be the name of an indexed column with TEXT affinity. */
99000     return 0;
99001   }
99002   assert( pLeft->iColumn!=(-1) ); /* Because IPK never has AFF_TEXT */
99003 
99004   pRight = pList->a[0].pExpr;
99005   op = pRight->op;
99006   if( op==TK_REGISTER ){
99007     op = pRight->op2;
99008   }
99009   if( op==TK_VARIABLE ){
99010     Vdbe *pReprepare = pParse->pReprepare;
99011     int iCol = pRight->iColumn;
99012     pVal = sqlite3VdbeGetValue(pReprepare, iCol, SQLITE_AFF_NONE);
99013     if( pVal && sqlite3_value_type(pVal)==SQLITE_TEXT ){
99014       z = (char *)sqlite3_value_text(pVal);
99015     }
99016     sqlite3VdbeSetVarmask(pParse->pVdbe, iCol); /* IMP: R-23257-02778 */
99017     assert( pRight->op==TK_VARIABLE || pRight->op==TK_REGISTER );
99018   }else if( op==TK_STRING ){
99019     z = pRight->u.zToken;
99020   }
99021   if( z ){
99022     cnt = 0;
99023     while( (c=z[cnt])!=0 && c!=wc[0] && c!=wc[1] && c!=wc[2] ){
99024       cnt++;
99025     }
99026     if( cnt!=0 && 255!=(u8)z[cnt-1] ){
99027       Expr *pPrefix;
99028       *pisComplete = c==wc[0] && z[cnt+1]==0;
99029       pPrefix = sqlite3Expr(db, TK_STRING, z);
99030       if( pPrefix ) pPrefix->u.zToken[cnt] = 0;
99031       *ppPrefix = pPrefix;
99032       if( op==TK_VARIABLE ){
99033         Vdbe *v = pParse->pVdbe;
99034         sqlite3VdbeSetVarmask(v, pRight->iColumn); /* IMP: R-23257-02778 */
99035         if( *pisComplete && pRight->u.zToken[1] ){
99036           /* If the rhs of the LIKE expression is a variable, and the current
99037           ** value of the variable means there is no need to invoke the LIKE
99038           ** function, then no OP_Variable will be added to the program.
99039           ** This causes problems for the sqlite3_bind_parameter_name()
99040           ** API. To workaround them, add a dummy OP_Variable here.
99041           */
99042           int r1 = sqlite3GetTempReg(pParse);
99043           sqlite3ExprCodeTarget(pParse, pRight, r1);
99044           sqlite3VdbeChangeP3(v, sqlite3VdbeCurrentAddr(v)-1, 0);
99045           sqlite3ReleaseTempReg(pParse, r1);
99046         }
99047       }
99048     }else{
99049       z = 0;
99050     }
99051   }
99052 
99053   sqlite3ValueFree(pVal);
99054   return (z!=0);
99055 }
99056 #endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */
99057 
99058 
99059 #ifndef SQLITE_OMIT_VIRTUALTABLE
99060 /*
99061 ** Check to see if the given expression is of the form
99062 **
99063 **         column MATCH expr
99064 **
99065 ** If it is then return TRUE.  If not, return FALSE.
99066 */
99067 static int isMatchOfColumn(
99068   Expr *pExpr      /* Test this expression */
99069 ){
99070   ExprList *pList;
99071 
99072   if( pExpr->op!=TK_FUNCTION ){
99073     return 0;
99074   }
99075   if( sqlite3StrICmp(pExpr->u.zToken,"match")!=0 ){
99076     return 0;
99077   }
99078   pList = pExpr->x.pList;
99079   if( pList->nExpr!=2 ){
99080     return 0;
99081   }
99082   if( pList->a[1].pExpr->op != TK_COLUMN ){
99083     return 0;
99084   }
99085   return 1;
99086 }
99087 #endif /* SQLITE_OMIT_VIRTUALTABLE */
99088 
99089 /*
99090 ** If the pBase expression originated in the ON or USING clause of
99091 ** a join, then transfer the appropriate markings over to derived.
99092 */
99093 static void transferJoinMarkings(Expr *pDerived, Expr *pBase){
99094   pDerived->flags |= pBase->flags & EP_FromJoin;
99095   pDerived->iRightJoinTable = pBase->iRightJoinTable;
99096 }
99097 
99098 #if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY)
99099 /*
99100 ** Analyze a term that consists of two or more OR-connected
99101 ** subterms.  So in:
99102 **
99103 **     ... WHERE  (a=5) AND (b=7 OR c=9 OR d=13) AND (d=13)
99104 **                          ^^^^^^^^^^^^^^^^^^^^
99105 **
99106 ** This routine analyzes terms such as the middle term in the above example.
99107 ** A WhereOrTerm object is computed and attached to the term under
99108 ** analysis, regardless of the outcome of the analysis.  Hence:
99109 **
99110 **     WhereTerm.wtFlags   |=  TERM_ORINFO
99111 **     WhereTerm.u.pOrInfo  =  a dynamically allocated WhereOrTerm object
99112 **
99113 ** The term being analyzed must have two or more of OR-connected subterms.
99114 ** A single subterm might be a set of AND-connected sub-subterms.
99115 ** Examples of terms under analysis:
99116 **
99117 **     (A)     t1.x=t2.y OR t1.x=t2.z OR t1.y=15 OR t1.z=t3.a+5
99118 **     (B)     x=expr1 OR expr2=x OR x=expr3
99119 **     (C)     t1.x=t2.y OR (t1.x=t2.z AND t1.y=15)
99120 **     (D)     x=expr1 OR (y>11 AND y<22 AND z LIKE '*hello*')
99121 **     (E)     (p.a=1 AND q.b=2 AND r.c=3) OR (p.x=4 AND q.y=5 AND r.z=6)
99122 **
99123 ** CASE 1:
99124 **
99125 ** If all subterms are of the form T.C=expr for some single column of C
99126 ** a single table T (as shown in example B above) then create a new virtual
99127 ** term that is an equivalent IN expression.  In other words, if the term
99128 ** being analyzed is:
99129 **
99130 **      x = expr1  OR  expr2 = x  OR  x = expr3
99131 **
99132 ** then create a new virtual term like this:
99133 **
99134 **      x IN (expr1,expr2,expr3)
99135 **
99136 ** CASE 2:
99137 **
99138 ** If all subterms are indexable by a single table T, then set
99139 **
99140 **     WhereTerm.eOperator              =  WO_OR
99141 **     WhereTerm.u.pOrInfo->indexable  |=  the cursor number for table T
99142 **
99143 ** A subterm is "indexable" if it is of the form
99144 ** "T.C <op> <expr>" where C is any column of table T and
99145 ** <op> is one of "=", "<", "<=", ">", ">=", "IS NULL", or "IN".
99146 ** A subterm is also indexable if it is an AND of two or more
99147 ** subsubterms at least one of which is indexable.  Indexable AND
99148 ** subterms have their eOperator set to WO_AND and they have
99149 ** u.pAndInfo set to a dynamically allocated WhereAndTerm object.
99150 **
99151 ** From another point of view, "indexable" means that the subterm could
99152 ** potentially be used with an index if an appropriate index exists.
99153 ** This analysis does not consider whether or not the index exists; that
99154 ** is something the bestIndex() routine will determine.  This analysis
99155 ** only looks at whether subterms appropriate for indexing exist.
99156 **
99157 ** All examples A through E above all satisfy case 2.  But if a term
99158 ** also statisfies case 1 (such as B) we know that the optimizer will
99159 ** always prefer case 1, so in that case we pretend that case 2 is not
99160 ** satisfied.
99161 **
99162 ** It might be the case that multiple tables are indexable.  For example,
99163 ** (E) above is indexable on tables P, Q, and R.
99164 **
99165 ** Terms that satisfy case 2 are candidates for lookup by using
99166 ** separate indices to find rowids for each subterm and composing
99167 ** the union of all rowids using a RowSet object.  This is similar
99168 ** to "bitmap indices" in other database engines.
99169 **
99170 ** OTHERWISE:
99171 **
99172 ** If neither case 1 nor case 2 apply, then leave the eOperator set to
99173 ** zero.  This term is not useful for search.
99174 */
99175 static void exprAnalyzeOrTerm(
99176   SrcList *pSrc,            /* the FROM clause */
99177   WhereClause *pWC,         /* the complete WHERE clause */
99178   int idxTerm               /* Index of the OR-term to be analyzed */
99179 ){
99180   Parse *pParse = pWC->pParse;            /* Parser context */
99181   sqlite3 *db = pParse->db;               /* Database connection */
99182   WhereTerm *pTerm = &pWC->a[idxTerm];    /* The term to be analyzed */
99183   Expr *pExpr = pTerm->pExpr;             /* The expression of the term */
99184   WhereMaskSet *pMaskSet = pWC->pMaskSet; /* Table use masks */
99185   int i;                                  /* Loop counters */
99186   WhereClause *pOrWc;       /* Breakup of pTerm into subterms */
99187   WhereTerm *pOrTerm;       /* A Sub-term within the pOrWc */
99188   WhereOrInfo *pOrInfo;     /* Additional information associated with pTerm */
99189   Bitmask chngToIN;         /* Tables that might satisfy case 1 */
99190   Bitmask indexable;        /* Tables that are indexable, satisfying case 2 */
99191 
99192   /*
99193   ** Break the OR clause into its separate subterms.  The subterms are
99194   ** stored in a WhereClause structure containing within the WhereOrInfo
99195   ** object that is attached to the original OR clause term.
99196   */
99197   assert( (pTerm->wtFlags & (TERM_DYNAMIC|TERM_ORINFO|TERM_ANDINFO))==0 );
99198   assert( pExpr->op==TK_OR );
99199   pTerm->u.pOrInfo = pOrInfo = sqlite3DbMallocZero(db, sizeof(*pOrInfo));
99200   if( pOrInfo==0 ) return;
99201   pTerm->wtFlags |= TERM_ORINFO;
99202   pOrWc = &pOrInfo->wc;
99203   whereClauseInit(pOrWc, pWC->pParse, pMaskSet);
99204   whereSplit(pOrWc, pExpr, TK_OR);
99205   exprAnalyzeAll(pSrc, pOrWc);
99206   if( db->mallocFailed ) return;
99207   assert( pOrWc->nTerm>=2 );
99208 
99209   /*
99210   ** Compute the set of tables that might satisfy cases 1 or 2.
99211   */
99212   indexable = ~(Bitmask)0;
99213   chngToIN = ~(pWC->vmask);
99214   for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0 && indexable; i--, pOrTerm++){
99215     if( (pOrTerm->eOperator & WO_SINGLE)==0 ){
99216       WhereAndInfo *pAndInfo;
99217       assert( pOrTerm->eOperator==0 );
99218       assert( (pOrTerm->wtFlags & (TERM_ANDINFO|TERM_ORINFO))==0 );
99219       chngToIN = 0;
99220       pAndInfo = sqlite3DbMallocRaw(db, sizeof(*pAndInfo));
99221       if( pAndInfo ){
99222         WhereClause *pAndWC;
99223         WhereTerm *pAndTerm;
99224         int j;
99225         Bitmask b = 0;
99226         pOrTerm->u.pAndInfo = pAndInfo;
99227         pOrTerm->wtFlags |= TERM_ANDINFO;
99228         pOrTerm->eOperator = WO_AND;
99229         pAndWC = &pAndInfo->wc;
99230         whereClauseInit(pAndWC, pWC->pParse, pMaskSet);
99231         whereSplit(pAndWC, pOrTerm->pExpr, TK_AND);
99232         exprAnalyzeAll(pSrc, pAndWC);
99233         testcase( db->mallocFailed );
99234         if( !db->mallocFailed ){
99235           for(j=0, pAndTerm=pAndWC->a; j<pAndWC->nTerm; j++, pAndTerm++){
99236             assert( pAndTerm->pExpr );
99237             if( allowedOp(pAndTerm->pExpr->op) ){
99238               b |= getMask(pMaskSet, pAndTerm->leftCursor);
99239             }
99240           }
99241         }
99242         indexable &= b;
99243       }
99244     }else if( pOrTerm->wtFlags & TERM_COPIED ){
99245       /* Skip this term for now.  We revisit it when we process the
99246       ** corresponding TERM_VIRTUAL term */
99247     }else{
99248       Bitmask b;
99249       b = getMask(pMaskSet, pOrTerm->leftCursor);
99250       if( pOrTerm->wtFlags & TERM_VIRTUAL ){
99251         WhereTerm *pOther = &pOrWc->a[pOrTerm->iParent];
99252         b |= getMask(pMaskSet, pOther->leftCursor);
99253       }
99254       indexable &= b;
99255       if( pOrTerm->eOperator!=WO_EQ ){
99256         chngToIN = 0;
99257       }else{
99258         chngToIN &= b;
99259       }
99260     }
99261   }
99262 
99263   /*
99264   ** Record the set of tables that satisfy case 2.  The set might be
99265   ** empty.
99266   */
99267   pOrInfo->indexable = indexable;
99268   pTerm->eOperator = indexable==0 ? 0 : WO_OR;
99269 
99270   /*
99271   ** chngToIN holds a set of tables that *might* satisfy case 1.  But
99272   ** we have to do some additional checking to see if case 1 really
99273   ** is satisfied.
99274   **
99275   ** chngToIN will hold either 0, 1, or 2 bits.  The 0-bit case means
99276   ** that there is no possibility of transforming the OR clause into an
99277   ** IN operator because one or more terms in the OR clause contain
99278   ** something other than == on a column in the single table.  The 1-bit
99279   ** case means that every term of the OR clause is of the form
99280   ** "table.column=expr" for some single table.  The one bit that is set
99281   ** will correspond to the common table.  We still need to check to make
99282   ** sure the same column is used on all terms.  The 2-bit case is when
99283   ** the all terms are of the form "table1.column=table2.column".  It
99284   ** might be possible to form an IN operator with either table1.column
99285   ** or table2.column as the LHS if either is common to every term of
99286   ** the OR clause.
99287   **
99288   ** Note that terms of the form "table.column1=table.column2" (the
99289   ** same table on both sizes of the ==) cannot be optimized.
99290   */
99291   if( chngToIN ){
99292     int okToChngToIN = 0;     /* True if the conversion to IN is valid */
99293     int iColumn = -1;         /* Column index on lhs of IN operator */
99294     int iCursor = -1;         /* Table cursor common to all terms */
99295     int j = 0;                /* Loop counter */
99296 
99297     /* Search for a table and column that appears on one side or the
99298     ** other of the == operator in every subterm.  That table and column
99299     ** will be recorded in iCursor and iColumn.  There might not be any
99300     ** such table and column.  Set okToChngToIN if an appropriate table
99301     ** and column is found but leave okToChngToIN false if not found.
99302     */
99303     for(j=0; j<2 && !okToChngToIN; j++){
99304       pOrTerm = pOrWc->a;
99305       for(i=pOrWc->nTerm-1; i>=0; i--, pOrTerm++){
99306         assert( pOrTerm->eOperator==WO_EQ );
99307         pOrTerm->wtFlags &= ~TERM_OR_OK;
99308         if( pOrTerm->leftCursor==iCursor ){
99309           /* This is the 2-bit case and we are on the second iteration and
99310           ** current term is from the first iteration.  So skip this term. */
99311           assert( j==1 );
99312           continue;
99313         }
99314         if( (chngToIN & getMask(pMaskSet, pOrTerm->leftCursor))==0 ){
99315           /* This term must be of the form t1.a==t2.b where t2 is in the
99316           ** chngToIN set but t1 is not.  This term will be either preceeded
99317           ** or follwed by an inverted copy (t2.b==t1.a).  Skip this term
99318           ** and use its inversion. */
99319           testcase( pOrTerm->wtFlags & TERM_COPIED );
99320           testcase( pOrTerm->wtFlags & TERM_VIRTUAL );
99321           assert( pOrTerm->wtFlags & (TERM_COPIED|TERM_VIRTUAL) );
99322           continue;
99323         }
99324         iColumn = pOrTerm->u.leftColumn;
99325         iCursor = pOrTerm->leftCursor;
99326         break;
99327       }
99328       if( i<0 ){
99329         /* No candidate table+column was found.  This can only occur
99330         ** on the second iteration */
99331         assert( j==1 );
99332         assert( (chngToIN&(chngToIN-1))==0 );
99333         assert( chngToIN==getMask(pMaskSet, iCursor) );
99334         break;
99335       }
99336       testcase( j==1 );
99337 
99338       /* We have found a candidate table and column.  Check to see if that
99339       ** table and column is common to every term in the OR clause */
99340       okToChngToIN = 1;
99341       for(; i>=0 && okToChngToIN; i--, pOrTerm++){
99342         assert( pOrTerm->eOperator==WO_EQ );
99343         if( pOrTerm->leftCursor!=iCursor ){
99344           pOrTerm->wtFlags &= ~TERM_OR_OK;
99345         }else if( pOrTerm->u.leftColumn!=iColumn ){
99346           okToChngToIN = 0;
99347         }else{
99348           int affLeft, affRight;
99349           /* If the right-hand side is also a column, then the affinities
99350           ** of both right and left sides must be such that no type
99351           ** conversions are required on the right.  (Ticket #2249)
99352           */
99353           affRight = sqlite3ExprAffinity(pOrTerm->pExpr->pRight);
99354           affLeft = sqlite3ExprAffinity(pOrTerm->pExpr->pLeft);
99355           if( affRight!=0 && affRight!=affLeft ){
99356             okToChngToIN = 0;
99357           }else{
99358             pOrTerm->wtFlags |= TERM_OR_OK;
99359           }
99360         }
99361       }
99362     }
99363 
99364     /* At this point, okToChngToIN is true if original pTerm satisfies
99365     ** case 1.  In that case, construct a new virtual term that is
99366     ** pTerm converted into an IN operator.
99367     **
99368     ** EV: R-00211-15100
99369     */
99370     if( okToChngToIN ){
99371       Expr *pDup;            /* A transient duplicate expression */
99372       ExprList *pList = 0;   /* The RHS of the IN operator */
99373       Expr *pLeft = 0;       /* The LHS of the IN operator */
99374       Expr *pNew;            /* The complete IN operator */
99375 
99376       for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0; i--, pOrTerm++){
99377         if( (pOrTerm->wtFlags & TERM_OR_OK)==0 ) continue;
99378         assert( pOrTerm->eOperator==WO_EQ );
99379         assert( pOrTerm->leftCursor==iCursor );
99380         assert( pOrTerm->u.leftColumn==iColumn );
99381         pDup = sqlite3ExprDup(db, pOrTerm->pExpr->pRight, 0);
99382         pList = sqlite3ExprListAppend(pWC->pParse, pList, pDup);
99383         pLeft = pOrTerm->pExpr->pLeft;
99384       }
99385       assert( pLeft!=0 );
99386       pDup = sqlite3ExprDup(db, pLeft, 0);
99387       pNew = sqlite3PExpr(pParse, TK_IN, pDup, 0, 0);
99388       if( pNew ){
99389         int idxNew;
99390         transferJoinMarkings(pNew, pExpr);
99391         assert( !ExprHasProperty(pNew, EP_xIsSelect) );
99392         pNew->x.pList = pList;
99393         idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL|TERM_DYNAMIC);
99394         testcase( idxNew==0 );
99395         exprAnalyze(pSrc, pWC, idxNew);
99396         pTerm = &pWC->a[idxTerm];
99397         pWC->a[idxNew].iParent = idxTerm;
99398         pTerm->nChild = 1;
99399       }else{
99400         sqlite3ExprListDelete(db, pList);
99401       }
99402       pTerm->eOperator = WO_NOOP;  /* case 1 trumps case 2 */
99403     }
99404   }
99405 }
99406 #endif /* !SQLITE_OMIT_OR_OPTIMIZATION && !SQLITE_OMIT_SUBQUERY */
99407 
99408 
99409 /*
99410 ** The input to this routine is an WhereTerm structure with only the
99411 ** "pExpr" field filled in.  The job of this routine is to analyze the
99412 ** subexpression and populate all the other fields of the WhereTerm
99413 ** structure.
99414 **
99415 ** If the expression is of the form "<expr> <op> X" it gets commuted
99416 ** to the standard form of "X <op> <expr>".
99417 **
99418 ** If the expression is of the form "X <op> Y" where both X and Y are
99419 ** columns, then the original expression is unchanged and a new virtual
99420 ** term of the form "Y <op> X" is added to the WHERE clause and
99421 ** analyzed separately.  The original term is marked with TERM_COPIED
99422 ** and the new term is marked with TERM_DYNAMIC (because it's pExpr
99423 ** needs to be freed with the WhereClause) and TERM_VIRTUAL (because it
99424 ** is a commuted copy of a prior term.)  The original term has nChild=1
99425 ** and the copy has idxParent set to the index of the original term.
99426 */
99427 static void exprAnalyze(
99428   SrcList *pSrc,            /* the FROM clause */
99429   WhereClause *pWC,         /* the WHERE clause */
99430   int idxTerm               /* Index of the term to be analyzed */
99431 ){
99432   WhereTerm *pTerm;                /* The term to be analyzed */
99433   WhereMaskSet *pMaskSet;          /* Set of table index masks */
99434   Expr *pExpr;                     /* The expression to be analyzed */
99435   Bitmask prereqLeft;              /* Prerequesites of the pExpr->pLeft */
99436   Bitmask prereqAll;               /* Prerequesites of pExpr */
99437   Bitmask extraRight = 0;          /* Extra dependencies on LEFT JOIN */
99438   Expr *pStr1 = 0;                 /* RHS of LIKE/GLOB operator */
99439   int isComplete = 0;              /* RHS of LIKE/GLOB ends with wildcard */
99440   int noCase = 0;                  /* LIKE/GLOB distinguishes case */
99441   int op;                          /* Top-level operator.  pExpr->op */
99442   Parse *pParse = pWC->pParse;     /* Parsing context */
99443   sqlite3 *db = pParse->db;        /* Database connection */
99444 
99445   if( db->mallocFailed ){
99446     return;
99447   }
99448   pTerm = &pWC->a[idxTerm];
99449   pMaskSet = pWC->pMaskSet;
99450   pExpr = pTerm->pExpr;
99451   prereqLeft = exprTableUsage(pMaskSet, pExpr->pLeft);
99452   op = pExpr->op;
99453   if( op==TK_IN ){
99454     assert( pExpr->pRight==0 );
99455     if( ExprHasProperty(pExpr, EP_xIsSelect) ){
99456       pTerm->prereqRight = exprSelectTableUsage(pMaskSet, pExpr->x.pSelect);
99457     }else{
99458       pTerm->prereqRight = exprListTableUsage(pMaskSet, pExpr->x.pList);
99459     }
99460   }else if( op==TK_ISNULL ){
99461     pTerm->prereqRight = 0;
99462   }else{
99463     pTerm->prereqRight = exprTableUsage(pMaskSet, pExpr->pRight);
99464   }
99465   prereqAll = exprTableUsage(pMaskSet, pExpr);
99466   if( ExprHasProperty(pExpr, EP_FromJoin) ){
99467     Bitmask x = getMask(pMaskSet, pExpr->iRightJoinTable);
99468     prereqAll |= x;
99469     extraRight = x-1;  /* ON clause terms may not be used with an index
99470                        ** on left table of a LEFT JOIN.  Ticket #3015 */
99471   }
99472   pTerm->prereqAll = prereqAll;
99473   pTerm->leftCursor = -1;
99474   pTerm->iParent = -1;
99475   pTerm->eOperator = 0;
99476   if( allowedOp(op) && (pTerm->prereqRight & prereqLeft)==0 ){
99477     Expr *pLeft = pExpr->pLeft;
99478     Expr *pRight = pExpr->pRight;
99479     if( pLeft->op==TK_COLUMN ){
99480       pTerm->leftCursor = pLeft->iTable;
99481       pTerm->u.leftColumn = pLeft->iColumn;
99482       pTerm->eOperator = operatorMask(op);
99483     }
99484     if( pRight && pRight->op==TK_COLUMN ){
99485       WhereTerm *pNew;
99486       Expr *pDup;
99487       if( pTerm->leftCursor>=0 ){
99488         int idxNew;
99489         pDup = sqlite3ExprDup(db, pExpr, 0);
99490         if( db->mallocFailed ){
99491           sqlite3ExprDelete(db, pDup);
99492           return;
99493         }
99494         idxNew = whereClauseInsert(pWC, pDup, TERM_VIRTUAL|TERM_DYNAMIC);
99495         if( idxNew==0 ) return;
99496         pNew = &pWC->a[idxNew];
99497         pNew->iParent = idxTerm;
99498         pTerm = &pWC->a[idxTerm];
99499         pTerm->nChild = 1;
99500         pTerm->wtFlags |= TERM_COPIED;
99501       }else{
99502         pDup = pExpr;
99503         pNew = pTerm;
99504       }
99505       exprCommute(pParse, pDup);
99506       pLeft = pDup->pLeft;
99507       pNew->leftCursor = pLeft->iTable;
99508       pNew->u.leftColumn = pLeft->iColumn;
99509       testcase( (prereqLeft | extraRight) != prereqLeft );
99510       pNew->prereqRight = prereqLeft | extraRight;
99511       pNew->prereqAll = prereqAll;
99512       pNew->eOperator = operatorMask(pDup->op);
99513     }
99514   }
99515 
99516 #ifndef SQLITE_OMIT_BETWEEN_OPTIMIZATION
99517   /* If a term is the BETWEEN operator, create two new virtual terms
99518   ** that define the range that the BETWEEN implements.  For example:
99519   **
99520   **      a BETWEEN b AND c
99521   **
99522   ** is converted into:
99523   **
99524   **      (a BETWEEN b AND c) AND (a>=b) AND (a<=c)
99525   **
99526   ** The two new terms are added onto the end of the WhereClause object.
99527   ** The new terms are "dynamic" and are children of the original BETWEEN
99528   ** term.  That means that if the BETWEEN term is coded, the children are
99529   ** skipped.  Or, if the children are satisfied by an index, the original
99530   ** BETWEEN term is skipped.
99531   */
99532   else if( pExpr->op==TK_BETWEEN && pWC->op==TK_AND ){
99533     ExprList *pList = pExpr->x.pList;
99534     int i;
99535     static const u8 ops[] = {TK_GE, TK_LE};
99536     assert( pList!=0 );
99537     assert( pList->nExpr==2 );
99538     for(i=0; i<2; i++){
99539       Expr *pNewExpr;
99540       int idxNew;
99541       pNewExpr = sqlite3PExpr(pParse, ops[i],
99542                              sqlite3ExprDup(db, pExpr->pLeft, 0),
99543                              sqlite3ExprDup(db, pList->a[i].pExpr, 0), 0);
99544       idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC);
99545       testcase( idxNew==0 );
99546       exprAnalyze(pSrc, pWC, idxNew);
99547       pTerm = &pWC->a[idxTerm];
99548       pWC->a[idxNew].iParent = idxTerm;
99549     }
99550     pTerm->nChild = 2;
99551   }
99552 #endif /* SQLITE_OMIT_BETWEEN_OPTIMIZATION */
99553 
99554 #if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY)
99555   /* Analyze a term that is composed of two or more subterms connected by
99556   ** an OR operator.
99557   */
99558   else if( pExpr->op==TK_OR ){
99559     assert( pWC->op==TK_AND );
99560     exprAnalyzeOrTerm(pSrc, pWC, idxTerm);
99561     pTerm = &pWC->a[idxTerm];
99562   }
99563 #endif /* SQLITE_OMIT_OR_OPTIMIZATION */
99564 
99565 #ifndef SQLITE_OMIT_LIKE_OPTIMIZATION
99566   /* Add constraints to reduce the search space on a LIKE or GLOB
99567   ** operator.
99568   **
99569   ** A like pattern of the form "x LIKE 'abc%'" is changed into constraints
99570   **
99571   **          x>='abc' AND x<'abd' AND x LIKE 'abc%'
99572   **
99573   ** The last character of the prefix "abc" is incremented to form the
99574   ** termination condition "abd".
99575   */
99576   if( pWC->op==TK_AND
99577    && isLikeOrGlob(pParse, pExpr, &pStr1, &isComplete, &noCase)
99578   ){
99579     Expr *pLeft;       /* LHS of LIKE/GLOB operator */
99580     Expr *pStr2;       /* Copy of pStr1 - RHS of LIKE/GLOB operator */
99581     Expr *pNewExpr1;
99582     Expr *pNewExpr2;
99583     int idxNew1;
99584     int idxNew2;
99585     CollSeq *pColl;    /* Collating sequence to use */
99586 
99587     pLeft = pExpr->x.pList->a[1].pExpr;
99588     pStr2 = sqlite3ExprDup(db, pStr1, 0);
99589     if( !db->mallocFailed ){
99590       u8 c, *pC;       /* Last character before the first wildcard */
99591       pC = (u8*)&pStr2->u.zToken[sqlite3Strlen30(pStr2->u.zToken)-1];
99592       c = *pC;
99593       if( noCase ){
99594         /* The point is to increment the last character before the first
99595         ** wildcard.  But if we increment '@', that will push it into the
99596         ** alphabetic range where case conversions will mess up the
99597         ** inequality.  To avoid this, make sure to also run the full
99598         ** LIKE on all candidate expressions by clearing the isComplete flag
99599         */
99600         if( c=='A'-1 ) isComplete = 0;   /* EV: R-64339-08207 */
99601 
99602 
99603         c = sqlite3UpperToLower[c];
99604       }
99605       *pC = c + 1;
99606     }
99607     pColl = sqlite3FindCollSeq(db, SQLITE_UTF8, noCase ? "NOCASE" : "BINARY",0);
99608     pNewExpr1 = sqlite3PExpr(pParse, TK_GE,
99609                      sqlite3ExprSetColl(sqlite3ExprDup(db,pLeft,0), pColl),
99610                      pStr1, 0);
99611     idxNew1 = whereClauseInsert(pWC, pNewExpr1, TERM_VIRTUAL|TERM_DYNAMIC);
99612     testcase( idxNew1==0 );
99613     exprAnalyze(pSrc, pWC, idxNew1);
99614     pNewExpr2 = sqlite3PExpr(pParse, TK_LT,
99615                      sqlite3ExprSetColl(sqlite3ExprDup(db,pLeft,0), pColl),
99616                      pStr2, 0);
99617     idxNew2 = whereClauseInsert(pWC, pNewExpr2, TERM_VIRTUAL|TERM_DYNAMIC);
99618     testcase( idxNew2==0 );
99619     exprAnalyze(pSrc, pWC, idxNew2);
99620     pTerm = &pWC->a[idxTerm];
99621     if( isComplete ){
99622       pWC->a[idxNew1].iParent = idxTerm;
99623       pWC->a[idxNew2].iParent = idxTerm;
99624       pTerm->nChild = 2;
99625     }
99626   }
99627 #endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */
99628 
99629 #ifndef SQLITE_OMIT_VIRTUALTABLE
99630   /* Add a WO_MATCH auxiliary term to the constraint set if the
99631   ** current expression is of the form:  column MATCH expr.
99632   ** This information is used by the xBestIndex methods of
99633   ** virtual tables.  The native query optimizer does not attempt
99634   ** to do anything with MATCH functions.
99635   */
99636   if( isMatchOfColumn(pExpr) ){
99637     int idxNew;
99638     Expr *pRight, *pLeft;
99639     WhereTerm *pNewTerm;
99640     Bitmask prereqColumn, prereqExpr;
99641 
99642     pRight = pExpr->x.pList->a[0].pExpr;
99643     pLeft = pExpr->x.pList->a[1].pExpr;
99644     prereqExpr = exprTableUsage(pMaskSet, pRight);
99645     prereqColumn = exprTableUsage(pMaskSet, pLeft);
99646     if( (prereqExpr & prereqColumn)==0 ){
99647       Expr *pNewExpr;
99648       pNewExpr = sqlite3PExpr(pParse, TK_MATCH,
99649                               0, sqlite3ExprDup(db, pRight, 0), 0);
99650       idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC);
99651       testcase( idxNew==0 );
99652       pNewTerm = &pWC->a[idxNew];
99653       pNewTerm->prereqRight = prereqExpr;
99654       pNewTerm->leftCursor = pLeft->iTable;
99655       pNewTerm->u.leftColumn = pLeft->iColumn;
99656       pNewTerm->eOperator = WO_MATCH;
99657       pNewTerm->iParent = idxTerm;
99658       pTerm = &pWC->a[idxTerm];
99659       pTerm->nChild = 1;
99660       pTerm->wtFlags |= TERM_COPIED;
99661       pNewTerm->prereqAll = pTerm->prereqAll;
99662     }
99663   }
99664 #endif /* SQLITE_OMIT_VIRTUALTABLE */
99665 
99666 #ifdef SQLITE_ENABLE_STAT2
99667   /* When sqlite_stat2 histogram data is available an operator of the
99668   ** form "x IS NOT NULL" can sometimes be evaluated more efficiently
99669   ** as "x>NULL" if x is not an INTEGER PRIMARY KEY.  So construct a
99670   ** virtual term of that form.
99671   **
99672   ** Note that the virtual term must be tagged with TERM_VNULL.  This
99673   ** TERM_VNULL tag will suppress the not-null check at the beginning
99674   ** of the loop.  Without the TERM_VNULL flag, the not-null check at
99675   ** the start of the loop will prevent any results from being returned.
99676   */
99677   if( pExpr->op==TK_NOTNULL
99678    && pExpr->pLeft->op==TK_COLUMN
99679    && pExpr->pLeft->iColumn>=0
99680   ){
99681     Expr *pNewExpr;
99682     Expr *pLeft = pExpr->pLeft;
99683     int idxNew;
99684     WhereTerm *pNewTerm;
99685 
99686     pNewExpr = sqlite3PExpr(pParse, TK_GT,
99687                             sqlite3ExprDup(db, pLeft, 0),
99688                             sqlite3PExpr(pParse, TK_NULL, 0, 0, 0), 0);
99689 
99690     idxNew = whereClauseInsert(pWC, pNewExpr,
99691                               TERM_VIRTUAL|TERM_DYNAMIC|TERM_VNULL);
99692     if( idxNew ){
99693       pNewTerm = &pWC->a[idxNew];
99694       pNewTerm->prereqRight = 0;
99695       pNewTerm->leftCursor = pLeft->iTable;
99696       pNewTerm->u.leftColumn = pLeft->iColumn;
99697       pNewTerm->eOperator = WO_GT;
99698       pNewTerm->iParent = idxTerm;
99699       pTerm = &pWC->a[idxTerm];
99700       pTerm->nChild = 1;
99701       pTerm->wtFlags |= TERM_COPIED;
99702       pNewTerm->prereqAll = pTerm->prereqAll;
99703     }
99704   }
99705 #endif /* SQLITE_ENABLE_STAT2 */
99706 
99707   /* Prevent ON clause terms of a LEFT JOIN from being used to drive
99708   ** an index for tables to the left of the join.
99709   */
99710   pTerm->prereqRight |= extraRight;
99711 }
99712 
99713 /*
99714 ** Return TRUE if any of the expressions in pList->a[iFirst...] contain
99715 ** a reference to any table other than the iBase table.
99716 */
99717 static int referencesOtherTables(
99718   ExprList *pList,          /* Search expressions in ths list */
99719   WhereMaskSet *pMaskSet,   /* Mapping from tables to bitmaps */
99720   int iFirst,               /* Be searching with the iFirst-th expression */
99721   int iBase                 /* Ignore references to this table */
99722 ){
99723   Bitmask allowed = ~getMask(pMaskSet, iBase);
99724   while( iFirst<pList->nExpr ){
99725     if( (exprTableUsage(pMaskSet, pList->a[iFirst++].pExpr)&allowed)!=0 ){
99726       return 1;
99727     }
99728   }
99729   return 0;
99730 }
99731 
99732 
99733 /*
99734 ** This routine decides if pIdx can be used to satisfy the ORDER BY
99735 ** clause.  If it can, it returns 1.  If pIdx cannot satisfy the
99736 ** ORDER BY clause, this routine returns 0.
99737 **
99738 ** pOrderBy is an ORDER BY clause from a SELECT statement.  pTab is the
99739 ** left-most table in the FROM clause of that same SELECT statement and
99740 ** the table has a cursor number of "base".  pIdx is an index on pTab.
99741 **
99742 ** nEqCol is the number of columns of pIdx that are used as equality
99743 ** constraints.  Any of these columns may be missing from the ORDER BY
99744 ** clause and the match can still be a success.
99745 **
99746 ** All terms of the ORDER BY that match against the index must be either
99747 ** ASC or DESC.  (Terms of the ORDER BY clause past the end of a UNIQUE
99748 ** index do not need to satisfy this constraint.)  The *pbRev value is
99749 ** set to 1 if the ORDER BY clause is all DESC and it is set to 0 if
99750 ** the ORDER BY clause is all ASC.
99751 */
99752 static int isSortingIndex(
99753   Parse *pParse,          /* Parsing context */
99754   WhereMaskSet *pMaskSet, /* Mapping from table cursor numbers to bitmaps */
99755   Index *pIdx,            /* The index we are testing */
99756   int base,               /* Cursor number for the table to be sorted */
99757   ExprList *pOrderBy,     /* The ORDER BY clause */
99758   int nEqCol,             /* Number of index columns with == constraints */
99759   int wsFlags,            /* Index usages flags */
99760   int *pbRev              /* Set to 1 if ORDER BY is DESC */
99761 ){
99762   int i, j;                       /* Loop counters */
99763   int sortOrder = 0;              /* XOR of index and ORDER BY sort direction */
99764   int nTerm;                      /* Number of ORDER BY terms */
99765   struct ExprList_item *pTerm;    /* A term of the ORDER BY clause */
99766   sqlite3 *db = pParse->db;
99767 
99768   assert( pOrderBy!=0 );
99769   nTerm = pOrderBy->nExpr;
99770   assert( nTerm>0 );
99771 
99772   /* Argument pIdx must either point to a 'real' named index structure,
99773   ** or an index structure allocated on the stack by bestBtreeIndex() to
99774   ** represent the rowid index that is part of every table.  */
99775   assert( pIdx->zName || (pIdx->nColumn==1 && pIdx->aiColumn[0]==-1) );
99776 
99777   /* Match terms of the ORDER BY clause against columns of
99778   ** the index.
99779   **
99780   ** Note that indices have pIdx->nColumn regular columns plus
99781   ** one additional column containing the rowid.  The rowid column
99782   ** of the index is also allowed to match against the ORDER BY
99783   ** clause.
99784   */
99785   for(i=j=0, pTerm=pOrderBy->a; j<nTerm && i<=pIdx->nColumn; i++){
99786     Expr *pExpr;       /* The expression of the ORDER BY pTerm */
99787     CollSeq *pColl;    /* The collating sequence of pExpr */
99788     int termSortOrder; /* Sort order for this term */
99789     int iColumn;       /* The i-th column of the index.  -1 for rowid */
99790     int iSortOrder;    /* 1 for DESC, 0 for ASC on the i-th index term */
99791     const char *zColl; /* Name of the collating sequence for i-th index term */
99792 
99793     pExpr = pTerm->pExpr;
99794     if( pExpr->op!=TK_COLUMN || pExpr->iTable!=base ){
99795       /* Can not use an index sort on anything that is not a column in the
99796       ** left-most table of the FROM clause */
99797       break;
99798     }
99799     pColl = sqlite3ExprCollSeq(pParse, pExpr);
99800     if( !pColl ){
99801       pColl = db->pDfltColl;
99802     }
99803     if( pIdx->zName && i<pIdx->nColumn ){
99804       iColumn = pIdx->aiColumn[i];
99805       if( iColumn==pIdx->pTable->iPKey ){
99806         iColumn = -1;
99807       }
99808       iSortOrder = pIdx->aSortOrder[i];
99809       zColl = pIdx->azColl[i];
99810     }else{
99811       iColumn = -1;
99812       iSortOrder = 0;
99813       zColl = pColl->zName;
99814     }
99815     if( pExpr->iColumn!=iColumn || sqlite3StrICmp(pColl->zName, zColl) ){
99816       /* Term j of the ORDER BY clause does not match column i of the index */
99817       if( i<nEqCol ){
99818         /* If an index column that is constrained by == fails to match an
99819         ** ORDER BY term, that is OK.  Just ignore that column of the index
99820         */
99821         continue;
99822       }else if( i==pIdx->nColumn ){
99823         /* Index column i is the rowid.  All other terms match. */
99824         break;
99825       }else{
99826         /* If an index column fails to match and is not constrained by ==
99827         ** then the index cannot satisfy the ORDER BY constraint.
99828         */
99829         return 0;
99830       }
99831     }
99832     assert( pIdx->aSortOrder!=0 || iColumn==-1 );
99833     assert( pTerm->sortOrder==0 || pTerm->sortOrder==1 );
99834     assert( iSortOrder==0 || iSortOrder==1 );
99835     termSortOrder = iSortOrder ^ pTerm->sortOrder;
99836     if( i>nEqCol ){
99837       if( termSortOrder!=sortOrder ){
99838         /* Indices can only be used if all ORDER BY terms past the
99839         ** equality constraints are all either DESC or ASC. */
99840         return 0;
99841       }
99842     }else{
99843       sortOrder = termSortOrder;
99844     }
99845     j++;
99846     pTerm++;
99847     if( iColumn<0 && !referencesOtherTables(pOrderBy, pMaskSet, j, base) ){
99848       /* If the indexed column is the primary key and everything matches
99849       ** so far and none of the ORDER BY terms to the right reference other
99850       ** tables in the join, then we are assured that the index can be used
99851       ** to sort because the primary key is unique and so none of the other
99852       ** columns will make any difference
99853       */
99854       j = nTerm;
99855     }
99856   }
99857 
99858   *pbRev = sortOrder!=0;
99859   if( j>=nTerm ){
99860     /* All terms of the ORDER BY clause are covered by this index so
99861     ** this index can be used for sorting. */
99862     return 1;
99863   }
99864   if( pIdx->onError!=OE_None && i==pIdx->nColumn
99865       && (wsFlags & WHERE_COLUMN_NULL)==0
99866       && !referencesOtherTables(pOrderBy, pMaskSet, j, base) ){
99867     /* All terms of this index match some prefix of the ORDER BY clause
99868     ** and the index is UNIQUE and no terms on the tail of the ORDER BY
99869     ** clause reference other tables in a join.  If this is all true then
99870     ** the order by clause is superfluous.  Not that if the matching
99871     ** condition is IS NULL then the result is not necessarily unique
99872     ** even on a UNIQUE index, so disallow those cases. */
99873     return 1;
99874   }
99875   return 0;
99876 }
99877 
99878 /*
99879 ** Prepare a crude estimate of the logarithm of the input value.
99880 ** The results need not be exact.  This is only used for estimating
99881 ** the total cost of performing operations with O(logN) or O(NlogN)
99882 ** complexity.  Because N is just a guess, it is no great tragedy if
99883 ** logN is a little off.
99884 */
99885 static double estLog(double N){
99886   double logN = 1;
99887   double x = 10;
99888   while( N>x ){
99889     logN += 1;
99890     x *= 10;
99891   }
99892   return logN;
99893 }
99894 
99895 /*
99896 ** Two routines for printing the content of an sqlite3_index_info
99897 ** structure.  Used for testing and debugging only.  If neither
99898 ** SQLITE_TEST or SQLITE_DEBUG are defined, then these routines
99899 ** are no-ops.
99900 */
99901 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_DEBUG)
99902 static void TRACE_IDX_INPUTS(sqlite3_index_info *p){
99903   int i;
99904   if( !sqlite3WhereTrace ) return;
99905   for(i=0; i<p->nConstraint; i++){
99906     sqlite3DebugPrintf("  constraint[%d]: col=%d termid=%d op=%d usabled=%d\n",
99907        i,
99908        p->aConstraint[i].iColumn,
99909        p->aConstraint[i].iTermOffset,
99910        p->aConstraint[i].op,
99911        p->aConstraint[i].usable);
99912   }
99913   for(i=0; i<p->nOrderBy; i++){
99914     sqlite3DebugPrintf("  orderby[%d]: col=%d desc=%d\n",
99915        i,
99916        p->aOrderBy[i].iColumn,
99917        p->aOrderBy[i].desc);
99918   }
99919 }
99920 static void TRACE_IDX_OUTPUTS(sqlite3_index_info *p){
99921   int i;
99922   if( !sqlite3WhereTrace ) return;
99923   for(i=0; i<p->nConstraint; i++){
99924     sqlite3DebugPrintf("  usage[%d]: argvIdx=%d omit=%d\n",
99925        i,
99926        p->aConstraintUsage[i].argvIndex,
99927        p->aConstraintUsage[i].omit);
99928   }
99929   sqlite3DebugPrintf("  idxNum=%d\n", p->idxNum);
99930   sqlite3DebugPrintf("  idxStr=%s\n", p->idxStr);
99931   sqlite3DebugPrintf("  orderByConsumed=%d\n", p->orderByConsumed);
99932   sqlite3DebugPrintf("  estimatedCost=%g\n", p->estimatedCost);
99933 }
99934 #else
99935 #define TRACE_IDX_INPUTS(A)
99936 #define TRACE_IDX_OUTPUTS(A)
99937 #endif
99938 
99939 /*
99940 ** Required because bestIndex() is called by bestOrClauseIndex()
99941 */
99942 static void bestIndex(
99943     Parse*, WhereClause*, struct SrcList_item*,
99944     Bitmask, Bitmask, ExprList*, WhereCost*);
99945 
99946 /*
99947 ** This routine attempts to find an scanning strategy that can be used
99948 ** to optimize an 'OR' expression that is part of a WHERE clause.
99949 **
99950 ** The table associated with FROM clause term pSrc may be either a
99951 ** regular B-Tree table or a virtual table.
99952 */
99953 static void bestOrClauseIndex(
99954   Parse *pParse,              /* The parsing context */
99955   WhereClause *pWC,           /* The WHERE clause */
99956   struct SrcList_item *pSrc,  /* The FROM clause term to search */
99957   Bitmask notReady,           /* Mask of cursors not available for indexing */
99958   Bitmask notValid,           /* Cursors not available for any purpose */
99959   ExprList *pOrderBy,         /* The ORDER BY clause */
99960   WhereCost *pCost            /* Lowest cost query plan */
99961 ){
99962 #ifndef SQLITE_OMIT_OR_OPTIMIZATION
99963   const int iCur = pSrc->iCursor;   /* The cursor of the table to be accessed */
99964   const Bitmask maskSrc = getMask(pWC->pMaskSet, iCur);  /* Bitmask for pSrc */
99965   WhereTerm * const pWCEnd = &pWC->a[pWC->nTerm];        /* End of pWC->a[] */
99966   WhereTerm *pTerm;                 /* A single term of the WHERE clause */
99967 
99968   /* No OR-clause optimization allowed if the INDEXED BY or NOT INDEXED clauses
99969   ** are used */
99970   if( pSrc->notIndexed || pSrc->pIndex!=0 ){
99971     return;
99972   }
99973 
99974   /* Search the WHERE clause terms for a usable WO_OR term. */
99975   for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
99976     if( pTerm->eOperator==WO_OR
99977      && ((pTerm->prereqAll & ~maskSrc) & notReady)==0
99978      && (pTerm->u.pOrInfo->indexable & maskSrc)!=0
99979     ){
99980       WhereClause * const pOrWC = &pTerm->u.pOrInfo->wc;
99981       WhereTerm * const pOrWCEnd = &pOrWC->a[pOrWC->nTerm];
99982       WhereTerm *pOrTerm;
99983       int flags = WHERE_MULTI_OR;
99984       double rTotal = 0;
99985       double nRow = 0;
99986       Bitmask used = 0;
99987 
99988       for(pOrTerm=pOrWC->a; pOrTerm<pOrWCEnd; pOrTerm++){
99989         WhereCost sTermCost;
99990         WHERETRACE(("... Multi-index OR testing for term %d of %d....\n",
99991           (pOrTerm - pOrWC->a), (pTerm - pWC->a)
99992         ));
99993         if( pOrTerm->eOperator==WO_AND ){
99994           WhereClause *pAndWC = &pOrTerm->u.pAndInfo->wc;
99995           bestIndex(pParse, pAndWC, pSrc, notReady, notValid, 0, &sTermCost);
99996         }else if( pOrTerm->leftCursor==iCur ){
99997           WhereClause tempWC;
99998           tempWC.pParse = pWC->pParse;
99999           tempWC.pMaskSet = pWC->pMaskSet;
100000           tempWC.op = TK_AND;
100001           tempWC.a = pOrTerm;
100002           tempWC.nTerm = 1;
100003           bestIndex(pParse, &tempWC, pSrc, notReady, notValid, 0, &sTermCost);
100004         }else{
100005           continue;
100006         }
100007         rTotal += sTermCost.rCost;
100008         nRow += sTermCost.plan.nRow;
100009         used |= sTermCost.used;
100010         if( rTotal>=pCost->rCost ) break;
100011       }
100012 
100013       /* If there is an ORDER BY clause, increase the scan cost to account
100014       ** for the cost of the sort. */
100015       if( pOrderBy!=0 ){
100016         WHERETRACE(("... sorting increases OR cost %.9g to %.9g\n",
100017                     rTotal, rTotal+nRow*estLog(nRow)));
100018         rTotal += nRow*estLog(nRow);
100019       }
100020 
100021       /* If the cost of scanning using this OR term for optimization is
100022       ** less than the current cost stored in pCost, replace the contents
100023       ** of pCost. */
100024       WHERETRACE(("... multi-index OR cost=%.9g nrow=%.9g\n", rTotal, nRow));
100025       if( rTotal<pCost->rCost ){
100026         pCost->rCost = rTotal;
100027         pCost->used = used;
100028         pCost->plan.nRow = nRow;
100029         pCost->plan.wsFlags = flags;
100030         pCost->plan.u.pTerm = pTerm;
100031       }
100032     }
100033   }
100034 #endif /* SQLITE_OMIT_OR_OPTIMIZATION */
100035 }
100036 
100037 #ifndef SQLITE_OMIT_AUTOMATIC_INDEX
100038 /*
100039 ** Return TRUE if the WHERE clause term pTerm is of a form where it
100040 ** could be used with an index to access pSrc, assuming an appropriate
100041 ** index existed.
100042 */
100043 static int termCanDriveIndex(
100044   WhereTerm *pTerm,              /* WHERE clause term to check */
100045   struct SrcList_item *pSrc,     /* Table we are trying to access */
100046   Bitmask notReady               /* Tables in outer loops of the join */
100047 ){
100048   char aff;
100049   if( pTerm->leftCursor!=pSrc->iCursor ) return 0;
100050   if( pTerm->eOperator!=WO_EQ ) return 0;
100051   if( (pTerm->prereqRight & notReady)!=0 ) return 0;
100052   aff = pSrc->pTab->aCol[pTerm->u.leftColumn].affinity;
100053   if( !sqlite3IndexAffinityOk(pTerm->pExpr, aff) ) return 0;
100054   return 1;
100055 }
100056 #endif
100057 
100058 #ifndef SQLITE_OMIT_AUTOMATIC_INDEX
100059 /*
100060 ** If the query plan for pSrc specified in pCost is a full table scan
100061 ** and indexing is allows (if there is no NOT INDEXED clause) and it
100062 ** possible to construct a transient index that would perform better
100063 ** than a full table scan even when the cost of constructing the index
100064 ** is taken into account, then alter the query plan to use the
100065 ** transient index.
100066 */
100067 static void bestAutomaticIndex(
100068   Parse *pParse,              /* The parsing context */
100069   WhereClause *pWC,           /* The WHERE clause */
100070   struct SrcList_item *pSrc,  /* The FROM clause term to search */
100071   Bitmask notReady,           /* Mask of cursors that are not available */
100072   WhereCost *pCost            /* Lowest cost query plan */
100073 ){
100074   double nTableRow;           /* Rows in the input table */
100075   double logN;                /* log(nTableRow) */
100076   double costTempIdx;         /* per-query cost of the transient index */
100077   WhereTerm *pTerm;           /* A single term of the WHERE clause */
100078   WhereTerm *pWCEnd;          /* End of pWC->a[] */
100079   Table *pTable;              /* Table tht might be indexed */
100080 
100081   if( (pParse->db->flags & SQLITE_AutoIndex)==0 ){
100082     /* Automatic indices are disabled at run-time */
100083     return;
100084   }
100085   if( (pCost->plan.wsFlags & WHERE_NOT_FULLSCAN)!=0 ){
100086     /* We already have some kind of index in use for this query. */
100087     return;
100088   }
100089   if( pSrc->notIndexed ){
100090     /* The NOT INDEXED clause appears in the SQL. */
100091     return;
100092   }
100093 
100094   assert( pParse->nQueryLoop >= (double)1 );
100095   pTable = pSrc->pTab;
100096   nTableRow = pTable->nRowEst;
100097   logN = estLog(nTableRow);
100098   costTempIdx = 2*logN*(nTableRow/pParse->nQueryLoop + 1);
100099   if( costTempIdx>=pCost->rCost ){
100100     /* The cost of creating the transient table would be greater than
100101     ** doing the full table scan */
100102     return;
100103   }
100104 
100105   /* Search for any equality comparison term */
100106   pWCEnd = &pWC->a[pWC->nTerm];
100107   for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
100108     if( termCanDriveIndex(pTerm, pSrc, notReady) ){
100109       WHERETRACE(("auto-index reduces cost from %.1f to %.1f\n",
100110                     pCost->rCost, costTempIdx));
100111       pCost->rCost = costTempIdx;
100112       pCost->plan.nRow = logN + 1;
100113       pCost->plan.wsFlags = WHERE_TEMP_INDEX;
100114       pCost->used = pTerm->prereqRight;
100115       break;
100116     }
100117   }
100118 }
100119 #else
100120 # define bestAutomaticIndex(A,B,C,D,E)  /* no-op */
100121 #endif /* SQLITE_OMIT_AUTOMATIC_INDEX */
100122 
100123 
100124 #ifndef SQLITE_OMIT_AUTOMATIC_INDEX
100125 /*
100126 ** Generate code to construct the Index object for an automatic index
100127 ** and to set up the WhereLevel object pLevel so that the code generator
100128 ** makes use of the automatic index.
100129 */
100130 static void constructAutomaticIndex(
100131   Parse *pParse,              /* The parsing context */
100132   WhereClause *pWC,           /* The WHERE clause */
100133   struct SrcList_item *pSrc,  /* The FROM clause term to get the next index */
100134   Bitmask notReady,           /* Mask of cursors that are not available */
100135   WhereLevel *pLevel          /* Write new index here */
100136 ){
100137   int nColumn;                /* Number of columns in the constructed index */
100138   WhereTerm *pTerm;           /* A single term of the WHERE clause */
100139   WhereTerm *pWCEnd;          /* End of pWC->a[] */
100140   int nByte;                  /* Byte of memory needed for pIdx */
100141   Index *pIdx;                /* Object describing the transient index */
100142   Vdbe *v;                    /* Prepared statement under construction */
100143   int regIsInit;              /* Register set by initialization */
100144   int addrInit;               /* Address of the initialization bypass jump */
100145   Table *pTable;              /* The table being indexed */
100146   KeyInfo *pKeyinfo;          /* Key information for the index */
100147   int addrTop;                /* Top of the index fill loop */
100148   int regRecord;              /* Register holding an index record */
100149   int n;                      /* Column counter */
100150   int i;                      /* Loop counter */
100151   int mxBitCol;               /* Maximum column in pSrc->colUsed */
100152   CollSeq *pColl;             /* Collating sequence to on a column */
100153   Bitmask idxCols;            /* Bitmap of columns used for indexing */
100154   Bitmask extraCols;          /* Bitmap of additional columns */
100155 
100156   /* Generate code to skip over the creation and initialization of the
100157   ** transient index on 2nd and subsequent iterations of the loop. */
100158   v = pParse->pVdbe;
100159   assert( v!=0 );
100160   regIsInit = ++pParse->nMem;
100161   addrInit = sqlite3VdbeAddOp1(v, OP_If, regIsInit);
100162   sqlite3VdbeAddOp2(v, OP_Integer, 1, regIsInit);
100163 
100164   /* Count the number of columns that will be added to the index
100165   ** and used to match WHERE clause constraints */
100166   nColumn = 0;
100167   pTable = pSrc->pTab;
100168   pWCEnd = &pWC->a[pWC->nTerm];
100169   idxCols = 0;
100170   for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
100171     if( termCanDriveIndex(pTerm, pSrc, notReady) ){
100172       int iCol = pTerm->u.leftColumn;
100173       Bitmask cMask = iCol>=BMS ? ((Bitmask)1)<<(BMS-1) : ((Bitmask)1)<<iCol;
100174       testcase( iCol==BMS );
100175       testcase( iCol==BMS-1 );
100176       if( (idxCols & cMask)==0 ){
100177         nColumn++;
100178         idxCols |= cMask;
100179       }
100180     }
100181   }
100182   assert( nColumn>0 );
100183   pLevel->plan.nEq = nColumn;
100184 
100185   /* Count the number of additional columns needed to create a
100186   ** covering index.  A "covering index" is an index that contains all
100187   ** columns that are needed by the query.  With a covering index, the
100188   ** original table never needs to be accessed.  Automatic indices must
100189   ** be a covering index because the index will not be updated if the
100190   ** original table changes and the index and table cannot both be used
100191   ** if they go out of sync.
100192   */
100193   extraCols = pSrc->colUsed & (~idxCols | (((Bitmask)1)<<(BMS-1)));
100194   mxBitCol = (pTable->nCol >= BMS-1) ? BMS-1 : pTable->nCol;
100195   testcase( pTable->nCol==BMS-1 );
100196   testcase( pTable->nCol==BMS-2 );
100197   for(i=0; i<mxBitCol; i++){
100198     if( extraCols & (((Bitmask)1)<<i) ) nColumn++;
100199   }
100200   if( pSrc->colUsed & (((Bitmask)1)<<(BMS-1)) ){
100201     nColumn += pTable->nCol - BMS + 1;
100202   }
100203   pLevel->plan.wsFlags |= WHERE_COLUMN_EQ | WHERE_IDX_ONLY | WO_EQ;
100204 
100205   /* Construct the Index object to describe this index */
100206   nByte = sizeof(Index);
100207   nByte += nColumn*sizeof(int);     /* Index.aiColumn */
100208   nByte += nColumn*sizeof(char*);   /* Index.azColl */
100209   nByte += nColumn;                 /* Index.aSortOrder */
100210   pIdx = sqlite3DbMallocZero(pParse->db, nByte);
100211   if( pIdx==0 ) return;
100212   pLevel->plan.u.pIdx = pIdx;
100213   pIdx->azColl = (char**)&pIdx[1];
100214   pIdx->aiColumn = (int*)&pIdx->azColl[nColumn];
100215   pIdx->aSortOrder = (u8*)&pIdx->aiColumn[nColumn];
100216   pIdx->zName = "auto-index";
100217   pIdx->nColumn = nColumn;
100218   pIdx->pTable = pTable;
100219   n = 0;
100220   idxCols = 0;
100221   for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
100222     if( termCanDriveIndex(pTerm, pSrc, notReady) ){
100223       int iCol = pTerm->u.leftColumn;
100224       Bitmask cMask = iCol>=BMS ? ((Bitmask)1)<<(BMS-1) : ((Bitmask)1)<<iCol;
100225       if( (idxCols & cMask)==0 ){
100226         Expr *pX = pTerm->pExpr;
100227         idxCols |= cMask;
100228         pIdx->aiColumn[n] = pTerm->u.leftColumn;
100229         pColl = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight);
100230         pIdx->azColl[n] = ALWAYS(pColl) ? pColl->zName : "BINARY";
100231         n++;
100232       }
100233     }
100234   }
100235   assert( (u32)n==pLevel->plan.nEq );
100236 
100237   /* Add additional columns needed to make the automatic index into
100238   ** a covering index */
100239   for(i=0; i<mxBitCol; i++){
100240     if( extraCols & (((Bitmask)1)<<i) ){
100241       pIdx->aiColumn[n] = i;
100242       pIdx->azColl[n] = "BINARY";
100243       n++;
100244     }
100245   }
100246   if( pSrc->colUsed & (((Bitmask)1)<<(BMS-1)) ){
100247     for(i=BMS-1; i<pTable->nCol; i++){
100248       pIdx->aiColumn[n] = i;
100249       pIdx->azColl[n] = "BINARY";
100250       n++;
100251     }
100252   }
100253   assert( n==nColumn );
100254 
100255   /* Create the automatic index */
100256   pKeyinfo = sqlite3IndexKeyinfo(pParse, pIdx);
100257   assert( pLevel->iIdxCur>=0 );
100258   sqlite3VdbeAddOp4(v, OP_OpenAutoindex, pLevel->iIdxCur, nColumn+1, 0,
100259                     (char*)pKeyinfo, P4_KEYINFO_HANDOFF);
100260   VdbeComment((v, "for %s", pTable->zName));
100261 
100262   /* Fill the automatic index with content */
100263   addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, pLevel->iTabCur);
100264   regRecord = sqlite3GetTempReg(pParse);
100265   sqlite3GenerateIndexKey(pParse, pIdx, pLevel->iTabCur, regRecord, 1);
100266   sqlite3VdbeAddOp2(v, OP_IdxInsert, pLevel->iIdxCur, regRecord);
100267   sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
100268   sqlite3VdbeAddOp2(v, OP_Next, pLevel->iTabCur, addrTop+1);
100269   sqlite3VdbeChangeP5(v, SQLITE_STMTSTATUS_AUTOINDEX);
100270   sqlite3VdbeJumpHere(v, addrTop);
100271   sqlite3ReleaseTempReg(pParse, regRecord);
100272 
100273   /* Jump here when skipping the initialization */
100274   sqlite3VdbeJumpHere(v, addrInit);
100275 }
100276 #endif /* SQLITE_OMIT_AUTOMATIC_INDEX */
100277 
100278 #ifndef SQLITE_OMIT_VIRTUALTABLE
100279 /*
100280 ** Allocate and populate an sqlite3_index_info structure. It is the
100281 ** responsibility of the caller to eventually release the structure
100282 ** by passing the pointer returned by this function to sqlite3_free().
100283 */
100284 static sqlite3_index_info *allocateIndexInfo(
100285   Parse *pParse,
100286   WhereClause *pWC,
100287   struct SrcList_item *pSrc,
100288   ExprList *pOrderBy
100289 ){
100290   int i, j;
100291   int nTerm;
100292   struct sqlite3_index_constraint *pIdxCons;
100293   struct sqlite3_index_orderby *pIdxOrderBy;
100294   struct sqlite3_index_constraint_usage *pUsage;
100295   WhereTerm *pTerm;
100296   int nOrderBy;
100297   sqlite3_index_info *pIdxInfo;
100298 
100299   WHERETRACE(("Recomputing index info for %s...\n", pSrc->pTab->zName));
100300 
100301   /* Count the number of possible WHERE clause constraints referring
100302   ** to this virtual table */
100303   for(i=nTerm=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
100304     if( pTerm->leftCursor != pSrc->iCursor ) continue;
100305     assert( (pTerm->eOperator&(pTerm->eOperator-1))==0 );
100306     testcase( pTerm->eOperator==WO_IN );
100307     testcase( pTerm->eOperator==WO_ISNULL );
100308     if( pTerm->eOperator & (WO_IN|WO_ISNULL) ) continue;
100309     nTerm++;
100310   }
100311 
100312   /* If the ORDER BY clause contains only columns in the current
100313   ** virtual table then allocate space for the aOrderBy part of
100314   ** the sqlite3_index_info structure.
100315   */
100316   nOrderBy = 0;
100317   if( pOrderBy ){
100318     for(i=0; i<pOrderBy->nExpr; i++){
100319       Expr *pExpr = pOrderBy->a[i].pExpr;
100320       if( pExpr->op!=TK_COLUMN || pExpr->iTable!=pSrc->iCursor ) break;
100321     }
100322     if( i==pOrderBy->nExpr ){
100323       nOrderBy = pOrderBy->nExpr;
100324     }
100325   }
100326 
100327   /* Allocate the sqlite3_index_info structure
100328   */
100329   pIdxInfo = sqlite3DbMallocZero(pParse->db, sizeof(*pIdxInfo)
100330                            + (sizeof(*pIdxCons) + sizeof(*pUsage))*nTerm
100331                            + sizeof(*pIdxOrderBy)*nOrderBy );
100332   if( pIdxInfo==0 ){
100333     sqlite3ErrorMsg(pParse, "out of memory");
100334     /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
100335     return 0;
100336   }
100337 
100338   /* Initialize the structure.  The sqlite3_index_info structure contains
100339   ** many fields that are declared "const" to prevent xBestIndex from
100340   ** changing them.  We have to do some funky casting in order to
100341   ** initialize those fields.
100342   */
100343   pIdxCons = (struct sqlite3_index_constraint*)&pIdxInfo[1];
100344   pIdxOrderBy = (struct sqlite3_index_orderby*)&pIdxCons[nTerm];
100345   pUsage = (struct sqlite3_index_constraint_usage*)&pIdxOrderBy[nOrderBy];
100346   *(int*)&pIdxInfo->nConstraint = nTerm;
100347   *(int*)&pIdxInfo->nOrderBy = nOrderBy;
100348   *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint = pIdxCons;
100349   *(struct sqlite3_index_orderby**)&pIdxInfo->aOrderBy = pIdxOrderBy;
100350   *(struct sqlite3_index_constraint_usage**)&pIdxInfo->aConstraintUsage =
100351                                                                    pUsage;
100352 
100353   for(i=j=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
100354     if( pTerm->leftCursor != pSrc->iCursor ) continue;
100355     assert( (pTerm->eOperator&(pTerm->eOperator-1))==0 );
100356     testcase( pTerm->eOperator==WO_IN );
100357     testcase( pTerm->eOperator==WO_ISNULL );
100358     if( pTerm->eOperator & (WO_IN|WO_ISNULL) ) continue;
100359     pIdxCons[j].iColumn = pTerm->u.leftColumn;
100360     pIdxCons[j].iTermOffset = i;
100361     pIdxCons[j].op = (u8)pTerm->eOperator;
100362     /* The direct assignment in the previous line is possible only because
100363     ** the WO_ and SQLITE_INDEX_CONSTRAINT_ codes are identical.  The
100364     ** following asserts verify this fact. */
100365     assert( WO_EQ==SQLITE_INDEX_CONSTRAINT_EQ );
100366     assert( WO_LT==SQLITE_INDEX_CONSTRAINT_LT );
100367     assert( WO_LE==SQLITE_INDEX_CONSTRAINT_LE );
100368     assert( WO_GT==SQLITE_INDEX_CONSTRAINT_GT );
100369     assert( WO_GE==SQLITE_INDEX_CONSTRAINT_GE );
100370     assert( WO_MATCH==SQLITE_INDEX_CONSTRAINT_MATCH );
100371     assert( pTerm->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE|WO_MATCH) );
100372     j++;
100373   }
100374   for(i=0; i<nOrderBy; i++){
100375     Expr *pExpr = pOrderBy->a[i].pExpr;
100376     pIdxOrderBy[i].iColumn = pExpr->iColumn;
100377     pIdxOrderBy[i].desc = pOrderBy->a[i].sortOrder;
100378   }
100379 
100380   return pIdxInfo;
100381 }
100382 
100383 /*
100384 ** The table object reference passed as the second argument to this function
100385 ** must represent a virtual table. This function invokes the xBestIndex()
100386 ** method of the virtual table with the sqlite3_index_info pointer passed
100387 ** as the argument.
100388 **
100389 ** If an error occurs, pParse is populated with an error message and a
100390 ** non-zero value is returned. Otherwise, 0 is returned and the output
100391 ** part of the sqlite3_index_info structure is left populated.
100392 **
100393 ** Whether or not an error is returned, it is the responsibility of the
100394 ** caller to eventually free p->idxStr if p->needToFreeIdxStr indicates
100395 ** that this is required.
100396 */
100397 static int vtabBestIndex(Parse *pParse, Table *pTab, sqlite3_index_info *p){
100398   sqlite3_vtab *pVtab = sqlite3GetVTable(pParse->db, pTab)->pVtab;
100399   int i;
100400   int rc;
100401 
100402   WHERETRACE(("xBestIndex for %s\n", pTab->zName));
100403   TRACE_IDX_INPUTS(p);
100404   rc = pVtab->pModule->xBestIndex(pVtab, p);
100405   TRACE_IDX_OUTPUTS(p);
100406 
100407   if( rc!=SQLITE_OK ){
100408     if( rc==SQLITE_NOMEM ){
100409       pParse->db->mallocFailed = 1;
100410     }else if( !pVtab->zErrMsg ){
100411       sqlite3ErrorMsg(pParse, "%s", sqlite3ErrStr(rc));
100412     }else{
100413       sqlite3ErrorMsg(pParse, "%s", pVtab->zErrMsg);
100414     }
100415   }
100416   sqlite3_free(pVtab->zErrMsg);
100417   pVtab->zErrMsg = 0;
100418 
100419   for(i=0; i<p->nConstraint; i++){
100420     if( !p->aConstraint[i].usable && p->aConstraintUsage[i].argvIndex>0 ){
100421       sqlite3ErrorMsg(pParse,
100422           "table %s: xBestIndex returned an invalid plan", pTab->zName);
100423     }
100424   }
100425 
100426   return pParse->nErr;
100427 }
100428 
100429 
100430 /*
100431 ** Compute the best index for a virtual table.
100432 **
100433 ** The best index is computed by the xBestIndex method of the virtual
100434 ** table module.  This routine is really just a wrapper that sets up
100435 ** the sqlite3_index_info structure that is used to communicate with
100436 ** xBestIndex.
100437 **
100438 ** In a join, this routine might be called multiple times for the
100439 ** same virtual table.  The sqlite3_index_info structure is created
100440 ** and initialized on the first invocation and reused on all subsequent
100441 ** invocations.  The sqlite3_index_info structure is also used when
100442 ** code is generated to access the virtual table.  The whereInfoDelete()
100443 ** routine takes care of freeing the sqlite3_index_info structure after
100444 ** everybody has finished with it.
100445 */
100446 static void bestVirtualIndex(
100447   Parse *pParse,                  /* The parsing context */
100448   WhereClause *pWC,               /* The WHERE clause */
100449   struct SrcList_item *pSrc,      /* The FROM clause term to search */
100450   Bitmask notReady,               /* Mask of cursors not available for index */
100451   Bitmask notValid,               /* Cursors not valid for any purpose */
100452   ExprList *pOrderBy,             /* The order by clause */
100453   WhereCost *pCost,               /* Lowest cost query plan */
100454   sqlite3_index_info **ppIdxInfo  /* Index information passed to xBestIndex */
100455 ){
100456   Table *pTab = pSrc->pTab;
100457   sqlite3_index_info *pIdxInfo;
100458   struct sqlite3_index_constraint *pIdxCons;
100459   struct sqlite3_index_constraint_usage *pUsage;
100460   WhereTerm *pTerm;
100461   int i, j;
100462   int nOrderBy;
100463   double rCost;
100464 
100465   /* Make sure wsFlags is initialized to some sane value. Otherwise, if the
100466   ** malloc in allocateIndexInfo() fails and this function returns leaving
100467   ** wsFlags in an uninitialized state, the caller may behave unpredictably.
100468   */
100469   memset(pCost, 0, sizeof(*pCost));
100470   pCost->plan.wsFlags = WHERE_VIRTUALTABLE;
100471 
100472   /* If the sqlite3_index_info structure has not been previously
100473   ** allocated and initialized, then allocate and initialize it now.
100474   */
100475   pIdxInfo = *ppIdxInfo;
100476   if( pIdxInfo==0 ){
100477     *ppIdxInfo = pIdxInfo = allocateIndexInfo(pParse, pWC, pSrc, pOrderBy);
100478   }
100479   if( pIdxInfo==0 ){
100480     return;
100481   }
100482 
100483   /* At this point, the sqlite3_index_info structure that pIdxInfo points
100484   ** to will have been initialized, either during the current invocation or
100485   ** during some prior invocation.  Now we just have to customize the
100486   ** details of pIdxInfo for the current invocation and pass it to
100487   ** xBestIndex.
100488   */
100489 
100490   /* The module name must be defined. Also, by this point there must
100491   ** be a pointer to an sqlite3_vtab structure. Otherwise
100492   ** sqlite3ViewGetColumnNames() would have picked up the error.
100493   */
100494   assert( pTab->azModuleArg && pTab->azModuleArg[0] );
100495   assert( sqlite3GetVTable(pParse->db, pTab) );
100496 
100497   /* Set the aConstraint[].usable fields and initialize all
100498   ** output variables to zero.
100499   **
100500   ** aConstraint[].usable is true for constraints where the right-hand
100501   ** side contains only references to tables to the left of the current
100502   ** table.  In other words, if the constraint is of the form:
100503   **
100504   **           column = expr
100505   **
100506   ** and we are evaluating a join, then the constraint on column is
100507   ** only valid if all tables referenced in expr occur to the left
100508   ** of the table containing column.
100509   **
100510   ** The aConstraints[] array contains entries for all constraints
100511   ** on the current table.  That way we only have to compute it once
100512   ** even though we might try to pick the best index multiple times.
100513   ** For each attempt at picking an index, the order of tables in the
100514   ** join might be different so we have to recompute the usable flag
100515   ** each time.
100516   */
100517   pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
100518   pUsage = pIdxInfo->aConstraintUsage;
100519   for(i=0; i<pIdxInfo->nConstraint; i++, pIdxCons++){
100520     j = pIdxCons->iTermOffset;
100521     pTerm = &pWC->a[j];
100522     pIdxCons->usable = (pTerm->prereqRight&notReady) ? 0 : 1;
100523   }
100524   memset(pUsage, 0, sizeof(pUsage[0])*pIdxInfo->nConstraint);
100525   if( pIdxInfo->needToFreeIdxStr ){
100526     sqlite3_free(pIdxInfo->idxStr);
100527   }
100528   pIdxInfo->idxStr = 0;
100529   pIdxInfo->idxNum = 0;
100530   pIdxInfo->needToFreeIdxStr = 0;
100531   pIdxInfo->orderByConsumed = 0;
100532   /* ((double)2) In case of SQLITE_OMIT_FLOATING_POINT... */
100533   pIdxInfo->estimatedCost = SQLITE_BIG_DBL / ((double)2);
100534   nOrderBy = pIdxInfo->nOrderBy;
100535   if( !pOrderBy ){
100536     pIdxInfo->nOrderBy = 0;
100537   }
100538 
100539   if( vtabBestIndex(pParse, pTab, pIdxInfo) ){
100540     return;
100541   }
100542 
100543   pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
100544   for(i=0; i<pIdxInfo->nConstraint; i++){
100545     if( pUsage[i].argvIndex>0 ){
100546       pCost->used |= pWC->a[pIdxCons[i].iTermOffset].prereqRight;
100547     }
100548   }
100549 
100550   /* If there is an ORDER BY clause, and the selected virtual table index
100551   ** does not satisfy it, increase the cost of the scan accordingly. This
100552   ** matches the processing for non-virtual tables in bestBtreeIndex().
100553   */
100554   rCost = pIdxInfo->estimatedCost;
100555   if( pOrderBy && pIdxInfo->orderByConsumed==0 ){
100556     rCost += estLog(rCost)*rCost;
100557   }
100558 
100559   /* The cost is not allowed to be larger than SQLITE_BIG_DBL (the
100560   ** inital value of lowestCost in this loop. If it is, then the
100561   ** (cost<lowestCost) test below will never be true.
100562   **
100563   ** Use "(double)2" instead of "2.0" in case OMIT_FLOATING_POINT
100564   ** is defined.
100565   */
100566   if( (SQLITE_BIG_DBL/((double)2))<rCost ){
100567     pCost->rCost = (SQLITE_BIG_DBL/((double)2));
100568   }else{
100569     pCost->rCost = rCost;
100570   }
100571   pCost->plan.u.pVtabIdx = pIdxInfo;
100572   if( pIdxInfo->orderByConsumed ){
100573     pCost->plan.wsFlags |= WHERE_ORDERBY;
100574   }
100575   pCost->plan.nEq = 0;
100576   pIdxInfo->nOrderBy = nOrderBy;
100577 
100578   /* Try to find a more efficient access pattern by using multiple indexes
100579   ** to optimize an OR expression within the WHERE clause.
100580   */
100581   bestOrClauseIndex(pParse, pWC, pSrc, notReady, notValid, pOrderBy, pCost);
100582 }
100583 #endif /* SQLITE_OMIT_VIRTUALTABLE */
100584 
100585 /*
100586 ** Argument pIdx is a pointer to an index structure that has an array of
100587 ** SQLITE_INDEX_SAMPLES evenly spaced samples of the first indexed column
100588 ** stored in Index.aSample. These samples divide the domain of values stored
100589 ** the index into (SQLITE_INDEX_SAMPLES+1) regions.
100590 ** Region 0 contains all values less than the first sample value. Region
100591 ** 1 contains values between the first and second samples.  Region 2 contains
100592 ** values between samples 2 and 3.  And so on.  Region SQLITE_INDEX_SAMPLES
100593 ** contains values larger than the last sample.
100594 **
100595 ** If the index contains many duplicates of a single value, then it is
100596 ** possible that two or more adjacent samples can hold the same value.
100597 ** When that is the case, the smallest possible region code is returned
100598 ** when roundUp is false and the largest possible region code is returned
100599 ** when roundUp is true.
100600 **
100601 ** If successful, this function determines which of the regions value
100602 ** pVal lies in, sets *piRegion to the region index (a value between 0
100603 ** and SQLITE_INDEX_SAMPLES+1, inclusive) and returns SQLITE_OK.
100604 ** Or, if an OOM occurs while converting text values between encodings,
100605 ** SQLITE_NOMEM is returned and *piRegion is undefined.
100606 */
100607 #ifdef SQLITE_ENABLE_STAT2
100608 static int whereRangeRegion(
100609   Parse *pParse,              /* Database connection */
100610   Index *pIdx,                /* Index to consider domain of */
100611   sqlite3_value *pVal,        /* Value to consider */
100612   int roundUp,                /* Return largest valid region if true */
100613   int *piRegion               /* OUT: Region of domain in which value lies */
100614 ){
100615   assert( roundUp==0 || roundUp==1 );
100616   if( ALWAYS(pVal) ){
100617     IndexSample *aSample = pIdx->aSample;
100618     int i = 0;
100619     int eType = sqlite3_value_type(pVal);
100620 
100621     if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
100622       double r = sqlite3_value_double(pVal);
100623       for(i=0; i<SQLITE_INDEX_SAMPLES; i++){
100624         if( aSample[i].eType==SQLITE_NULL ) continue;
100625         if( aSample[i].eType>=SQLITE_TEXT ) break;
100626         if( roundUp ){
100627           if( aSample[i].u.r>r ) break;
100628         }else{
100629           if( aSample[i].u.r>=r ) break;
100630         }
100631       }
100632     }else if( eType==SQLITE_NULL ){
100633       i = 0;
100634       if( roundUp ){
100635         while( i<SQLITE_INDEX_SAMPLES && aSample[i].eType==SQLITE_NULL ) i++;
100636       }
100637     }else{
100638       sqlite3 *db = pParse->db;
100639       CollSeq *pColl;
100640       const u8 *z;
100641       int n;
100642 
100643       /* pVal comes from sqlite3ValueFromExpr() so the type cannot be NULL */
100644       assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB );
100645 
100646       if( eType==SQLITE_BLOB ){
100647         z = (const u8 *)sqlite3_value_blob(pVal);
100648         pColl = db->pDfltColl;
100649         assert( pColl->enc==SQLITE_UTF8 );
100650       }else{
100651         pColl = sqlite3GetCollSeq(db, SQLITE_UTF8, 0, *pIdx->azColl);
100652         if( pColl==0 ){
100653           sqlite3ErrorMsg(pParse, "no such collation sequence: %s",
100654                           *pIdx->azColl);
100655           return SQLITE_ERROR;
100656         }
100657         z = (const u8 *)sqlite3ValueText(pVal, pColl->enc);
100658         if( !z ){
100659           return SQLITE_NOMEM;
100660         }
100661         assert( z && pColl && pColl->xCmp );
100662       }
100663       n = sqlite3ValueBytes(pVal, pColl->enc);
100664 
100665       for(i=0; i<SQLITE_INDEX_SAMPLES; i++){
100666         int c;
100667         int eSampletype = aSample[i].eType;
100668         if( eSampletype==SQLITE_NULL || eSampletype<eType ) continue;
100669         if( (eSampletype!=eType) ) break;
100670 #ifndef SQLITE_OMIT_UTF16
100671         if( pColl->enc!=SQLITE_UTF8 ){
100672           int nSample;
100673           char *zSample = sqlite3Utf8to16(
100674               db, pColl->enc, aSample[i].u.z, aSample[i].nByte, &nSample
100675           );
100676           if( !zSample ){
100677             assert( db->mallocFailed );
100678             return SQLITE_NOMEM;
100679           }
100680           c = pColl->xCmp(pColl->pUser, nSample, zSample, n, z);
100681           sqlite3DbFree(db, zSample);
100682         }else
100683 #endif
100684         {
100685           c = pColl->xCmp(pColl->pUser, aSample[i].nByte, aSample[i].u.z, n, z);
100686         }
100687         if( c-roundUp>=0 ) break;
100688       }
100689     }
100690 
100691     assert( i>=0 && i<=SQLITE_INDEX_SAMPLES );
100692     *piRegion = i;
100693   }
100694   return SQLITE_OK;
100695 }
100696 #endif   /* #ifdef SQLITE_ENABLE_STAT2 */
100697 
100698 /*
100699 ** If expression pExpr represents a literal value, set *pp to point to
100700 ** an sqlite3_value structure containing the same value, with affinity
100701 ** aff applied to it, before returning. It is the responsibility of the
100702 ** caller to eventually release this structure by passing it to
100703 ** sqlite3ValueFree().
100704 **
100705 ** If the current parse is a recompile (sqlite3Reprepare()) and pExpr
100706 ** is an SQL variable that currently has a non-NULL value bound to it,
100707 ** create an sqlite3_value structure containing this value, again with
100708 ** affinity aff applied to it, instead.
100709 **
100710 ** If neither of the above apply, set *pp to NULL.
100711 **
100712 ** If an error occurs, return an error code. Otherwise, SQLITE_OK.
100713 */
100714 #ifdef SQLITE_ENABLE_STAT2
100715 static int valueFromExpr(
100716   Parse *pParse,
100717   Expr *pExpr,
100718   u8 aff,
100719   sqlite3_value **pp
100720 ){
100721   if( pExpr->op==TK_VARIABLE
100722    || (pExpr->op==TK_REGISTER && pExpr->op2==TK_VARIABLE)
100723   ){
100724     int iVar = pExpr->iColumn;
100725     sqlite3VdbeSetVarmask(pParse->pVdbe, iVar); /* IMP: R-23257-02778 */
100726     *pp = sqlite3VdbeGetValue(pParse->pReprepare, iVar, aff);
100727     return SQLITE_OK;
100728   }
100729   return sqlite3ValueFromExpr(pParse->db, pExpr, SQLITE_UTF8, aff, pp);
100730 }
100731 #endif
100732 
100733 /*
100734 ** This function is used to estimate the number of rows that will be visited
100735 ** by scanning an index for a range of values. The range may have an upper
100736 ** bound, a lower bound, or both. The WHERE clause terms that set the upper
100737 ** and lower bounds are represented by pLower and pUpper respectively. For
100738 ** example, assuming that index p is on t1(a):
100739 **
100740 **   ... FROM t1 WHERE a > ? AND a < ? ...
100741 **                    |_____|   |_____|
100742 **                       |         |
100743 **                     pLower    pUpper
100744 **
100745 ** If either of the upper or lower bound is not present, then NULL is passed in
100746 ** place of the corresponding WhereTerm.
100747 **
100748 ** The nEq parameter is passed the index of the index column subject to the
100749 ** range constraint. Or, equivalently, the number of equality constraints
100750 ** optimized by the proposed index scan. For example, assuming index p is
100751 ** on t1(a, b), and the SQL query is:
100752 **
100753 **   ... FROM t1 WHERE a = ? AND b > ? AND b < ? ...
100754 **
100755 ** then nEq should be passed the value 1 (as the range restricted column,
100756 ** b, is the second left-most column of the index). Or, if the query is:
100757 **
100758 **   ... FROM t1 WHERE a > ? AND a < ? ...
100759 **
100760 ** then nEq should be passed 0.
100761 **
100762 ** The returned value is an integer between 1 and 100, inclusive. A return
100763 ** value of 1 indicates that the proposed range scan is expected to visit
100764 ** approximately 1/100th (1%) of the rows selected by the nEq equality
100765 ** constraints (if any). A return value of 100 indicates that it is expected
100766 ** that the range scan will visit every row (100%) selected by the equality
100767 ** constraints.
100768 **
100769 ** In the absence of sqlite_stat2 ANALYZE data, each range inequality
100770 ** reduces the search space by 3/4ths.  Hence a single constraint (x>?)
100771 ** results in a return of 25 and a range constraint (x>? AND x<?) results
100772 ** in a return of 6.
100773 */
100774 static int whereRangeScanEst(
100775   Parse *pParse,       /* Parsing & code generating context */
100776   Index *p,            /* The index containing the range-compared column; "x" */
100777   int nEq,             /* index into p->aCol[] of the range-compared column */
100778   WhereTerm *pLower,   /* Lower bound on the range. ex: "x>123" Might be NULL */
100779   WhereTerm *pUpper,   /* Upper bound on the range. ex: "x<455" Might be NULL */
100780   int *piEst           /* OUT: Return value */
100781 ){
100782   int rc = SQLITE_OK;
100783 
100784 #ifdef SQLITE_ENABLE_STAT2
100785 
100786   if( nEq==0 && p->aSample ){
100787     sqlite3_value *pLowerVal = 0;
100788     sqlite3_value *pUpperVal = 0;
100789     int iEst;
100790     int iLower = 0;
100791     int iUpper = SQLITE_INDEX_SAMPLES;
100792     int roundUpUpper = 0;
100793     int roundUpLower = 0;
100794     u8 aff = p->pTable->aCol[p->aiColumn[0]].affinity;
100795 
100796     if( pLower ){
100797       Expr *pExpr = pLower->pExpr->pRight;
100798       rc = valueFromExpr(pParse, pExpr, aff, &pLowerVal);
100799       assert( pLower->eOperator==WO_GT || pLower->eOperator==WO_GE );
100800       roundUpLower = (pLower->eOperator==WO_GT) ?1:0;
100801     }
100802     if( rc==SQLITE_OK && pUpper ){
100803       Expr *pExpr = pUpper->pExpr->pRight;
100804       rc = valueFromExpr(pParse, pExpr, aff, &pUpperVal);
100805       assert( pUpper->eOperator==WO_LT || pUpper->eOperator==WO_LE );
100806       roundUpUpper = (pUpper->eOperator==WO_LE) ?1:0;
100807     }
100808 
100809     if( rc!=SQLITE_OK || (pLowerVal==0 && pUpperVal==0) ){
100810       sqlite3ValueFree(pLowerVal);
100811       sqlite3ValueFree(pUpperVal);
100812       goto range_est_fallback;
100813     }else if( pLowerVal==0 ){
100814       rc = whereRangeRegion(pParse, p, pUpperVal, roundUpUpper, &iUpper);
100815       if( pLower ) iLower = iUpper/2;
100816     }else if( pUpperVal==0 ){
100817       rc = whereRangeRegion(pParse, p, pLowerVal, roundUpLower, &iLower);
100818       if( pUpper ) iUpper = (iLower + SQLITE_INDEX_SAMPLES + 1)/2;
100819     }else{
100820       rc = whereRangeRegion(pParse, p, pUpperVal, roundUpUpper, &iUpper);
100821       if( rc==SQLITE_OK ){
100822         rc = whereRangeRegion(pParse, p, pLowerVal, roundUpLower, &iLower);
100823       }
100824     }
100825     WHERETRACE(("range scan regions: %d..%d\n", iLower, iUpper));
100826 
100827     iEst = iUpper - iLower;
100828     testcase( iEst==SQLITE_INDEX_SAMPLES );
100829     assert( iEst<=SQLITE_INDEX_SAMPLES );
100830     if( iEst<1 ){
100831       *piEst = 50/SQLITE_INDEX_SAMPLES;
100832     }else{
100833       *piEst = (iEst*100)/SQLITE_INDEX_SAMPLES;
100834     }
100835     sqlite3ValueFree(pLowerVal);
100836     sqlite3ValueFree(pUpperVal);
100837     return rc;
100838   }
100839 range_est_fallback:
100840 #else
100841   UNUSED_PARAMETER(pParse);
100842   UNUSED_PARAMETER(p);
100843   UNUSED_PARAMETER(nEq);
100844 #endif
100845   assert( pLower || pUpper );
100846   *piEst = 100;
100847   if( pLower && (pLower->wtFlags & TERM_VNULL)==0 ) *piEst /= 4;
100848   if( pUpper ) *piEst /= 4;
100849   return rc;
100850 }
100851 
100852 #ifdef SQLITE_ENABLE_STAT2
100853 /*
100854 ** Estimate the number of rows that will be returned based on
100855 ** an equality constraint x=VALUE and where that VALUE occurs in
100856 ** the histogram data.  This only works when x is the left-most
100857 ** column of an index and sqlite_stat2 histogram data is available
100858 ** for that index.  When pExpr==NULL that means the constraint is
100859 ** "x IS NULL" instead of "x=VALUE".
100860 **
100861 ** Write the estimated row count into *pnRow and return SQLITE_OK.
100862 ** If unable to make an estimate, leave *pnRow unchanged and return
100863 ** non-zero.
100864 **
100865 ** This routine can fail if it is unable to load a collating sequence
100866 ** required for string comparison, or if unable to allocate memory
100867 ** for a UTF conversion required for comparison.  The error is stored
100868 ** in the pParse structure.
100869 */
100870 static int whereEqualScanEst(
100871   Parse *pParse,       /* Parsing & code generating context */
100872   Index *p,            /* The index whose left-most column is pTerm */
100873   Expr *pExpr,         /* Expression for VALUE in the x=VALUE constraint */
100874   double *pnRow        /* Write the revised row estimate here */
100875 ){
100876   sqlite3_value *pRhs = 0;  /* VALUE on right-hand side of pTerm */
100877   int iLower, iUpper;       /* Range of histogram regions containing pRhs */
100878   u8 aff;                   /* Column affinity */
100879   int rc;                   /* Subfunction return code */
100880   double nRowEst;           /* New estimate of the number of rows */
100881 
100882   assert( p->aSample!=0 );
100883   aff = p->pTable->aCol[p->aiColumn[0]].affinity;
100884   if( pExpr ){
100885     rc = valueFromExpr(pParse, pExpr, aff, &pRhs);
100886     if( rc ) goto whereEqualScanEst_cancel;
100887   }else{
100888     pRhs = sqlite3ValueNew(pParse->db);
100889   }
100890   if( pRhs==0 ) return SQLITE_NOTFOUND;
100891   rc = whereRangeRegion(pParse, p, pRhs, 0, &iLower);
100892   if( rc ) goto whereEqualScanEst_cancel;
100893   rc = whereRangeRegion(pParse, p, pRhs, 1, &iUpper);
100894   if( rc ) goto whereEqualScanEst_cancel;
100895   WHERETRACE(("equality scan regions: %d..%d\n", iLower, iUpper));
100896   if( iLower>=iUpper ){
100897     nRowEst = p->aiRowEst[0]/(SQLITE_INDEX_SAMPLES*2);
100898     if( nRowEst<*pnRow ) *pnRow = nRowEst;
100899   }else{
100900     nRowEst = (iUpper-iLower)*p->aiRowEst[0]/SQLITE_INDEX_SAMPLES;
100901     *pnRow = nRowEst;
100902   }
100903 
100904 whereEqualScanEst_cancel:
100905   sqlite3ValueFree(pRhs);
100906   return rc;
100907 }
100908 #endif /* defined(SQLITE_ENABLE_STAT2) */
100909 
100910 #ifdef SQLITE_ENABLE_STAT2
100911 /*
100912 ** Estimate the number of rows that will be returned based on
100913 ** an IN constraint where the right-hand side of the IN operator
100914 ** is a list of values.  Example:
100915 **
100916 **        WHERE x IN (1,2,3,4)
100917 **
100918 ** Write the estimated row count into *pnRow and return SQLITE_OK.
100919 ** If unable to make an estimate, leave *pnRow unchanged and return
100920 ** non-zero.
100921 **
100922 ** This routine can fail if it is unable to load a collating sequence
100923 ** required for string comparison, or if unable to allocate memory
100924 ** for a UTF conversion required for comparison.  The error is stored
100925 ** in the pParse structure.
100926 */
100927 static int whereInScanEst(
100928   Parse *pParse,       /* Parsing & code generating context */
100929   Index *p,            /* The index whose left-most column is pTerm */
100930   ExprList *pList,     /* The value list on the RHS of "x IN (v1,v2,v3,...)" */
100931   double *pnRow        /* Write the revised row estimate here */
100932 ){
100933   sqlite3_value *pVal = 0;  /* One value from list */
100934   int iLower, iUpper;       /* Range of histogram regions containing pRhs */
100935   u8 aff;                   /* Column affinity */
100936   int rc = SQLITE_OK;       /* Subfunction return code */
100937   double nRowEst;           /* New estimate of the number of rows */
100938   int nSpan = 0;            /* Number of histogram regions spanned */
100939   int nSingle = 0;          /* Histogram regions hit by a single value */
100940   int nNotFound = 0;        /* Count of values that are not constants */
100941   int i;                               /* Loop counter */
100942   u8 aSpan[SQLITE_INDEX_SAMPLES+1];    /* Histogram regions that are spanned */
100943   u8 aSingle[SQLITE_INDEX_SAMPLES+1];  /* Histogram regions hit once */
100944 
100945   assert( p->aSample!=0 );
100946   aff = p->pTable->aCol[p->aiColumn[0]].affinity;
100947   memset(aSpan, 0, sizeof(aSpan));
100948   memset(aSingle, 0, sizeof(aSingle));
100949   for(i=0; i<pList->nExpr; i++){
100950     sqlite3ValueFree(pVal);
100951     rc = valueFromExpr(pParse, pList->a[i].pExpr, aff, &pVal);
100952     if( rc ) break;
100953     if( pVal==0 || sqlite3_value_type(pVal)==SQLITE_NULL ){
100954       nNotFound++;
100955       continue;
100956     }
100957     rc = whereRangeRegion(pParse, p, pVal, 0, &iLower);
100958     if( rc ) break;
100959     rc = whereRangeRegion(pParse, p, pVal, 1, &iUpper);
100960     if( rc ) break;
100961     if( iLower>=iUpper ){
100962       aSingle[iLower] = 1;
100963     }else{
100964       assert( iLower>=0 && iUpper<=SQLITE_INDEX_SAMPLES );
100965       while( iLower<iUpper ) aSpan[iLower++] = 1;
100966     }
100967   }
100968   if( rc==SQLITE_OK ){
100969     for(i=nSpan=0; i<=SQLITE_INDEX_SAMPLES; i++){
100970       if( aSpan[i] ){
100971         nSpan++;
100972       }else if( aSingle[i] ){
100973         nSingle++;
100974       }
100975     }
100976     nRowEst = (nSpan*2+nSingle)*p->aiRowEst[0]/(2*SQLITE_INDEX_SAMPLES)
100977                + nNotFound*p->aiRowEst[1];
100978     if( nRowEst > p->aiRowEst[0] ) nRowEst = p->aiRowEst[0];
100979     *pnRow = nRowEst;
100980     WHERETRACE(("IN row estimate: nSpan=%d, nSingle=%d, nNotFound=%d, est=%g\n",
100981                  nSpan, nSingle, nNotFound, nRowEst));
100982   }
100983   sqlite3ValueFree(pVal);
100984   return rc;
100985 }
100986 #endif /* defined(SQLITE_ENABLE_STAT2) */
100987 
100988 
100989 /*
100990 ** Find the best query plan for accessing a particular table.  Write the
100991 ** best query plan and its cost into the WhereCost object supplied as the
100992 ** last parameter.
100993 **
100994 ** The lowest cost plan wins.  The cost is an estimate of the amount of
100995 ** CPU and disk I/O needed to process the requested result.
100996 ** Factors that influence cost include:
100997 **
100998 **    *  The estimated number of rows that will be retrieved.  (The
100999 **       fewer the better.)
101000 **
101001 **    *  Whether or not sorting must occur.
101002 **
101003 **    *  Whether or not there must be separate lookups in the
101004 **       index and in the main table.
101005 **
101006 ** If there was an INDEXED BY clause (pSrc->pIndex) attached to the table in
101007 ** the SQL statement, then this function only considers plans using the
101008 ** named index. If no such plan is found, then the returned cost is
101009 ** SQLITE_BIG_DBL. If a plan is found that uses the named index,
101010 ** then the cost is calculated in the usual way.
101011 **
101012 ** If a NOT INDEXED clause (pSrc->notIndexed!=0) was attached to the table
101013 ** in the SELECT statement, then no indexes are considered. However, the
101014 ** selected plan may still take advantage of the built-in rowid primary key
101015 ** index.
101016 */
101017 static void bestBtreeIndex(
101018   Parse *pParse,              /* The parsing context */
101019   WhereClause *pWC,           /* The WHERE clause */
101020   struct SrcList_item *pSrc,  /* The FROM clause term to search */
101021   Bitmask notReady,           /* Mask of cursors not available for indexing */
101022   Bitmask notValid,           /* Cursors not available for any purpose */
101023   ExprList *pOrderBy,         /* The ORDER BY clause */
101024   WhereCost *pCost            /* Lowest cost query plan */
101025 ){
101026   int iCur = pSrc->iCursor;   /* The cursor of the table to be accessed */
101027   Index *pProbe;              /* An index we are evaluating */
101028   Index *pIdx;                /* Copy of pProbe, or zero for IPK index */
101029   int eqTermMask;             /* Current mask of valid equality operators */
101030   int idxEqTermMask;          /* Index mask of valid equality operators */
101031   Index sPk;                  /* A fake index object for the primary key */
101032   unsigned int aiRowEstPk[2]; /* The aiRowEst[] value for the sPk index */
101033   int aiColumnPk = -1;        /* The aColumn[] value for the sPk index */
101034   int wsFlagMask;             /* Allowed flags in pCost->plan.wsFlag */
101035 
101036   /* Initialize the cost to a worst-case value */
101037   memset(pCost, 0, sizeof(*pCost));
101038   pCost->rCost = SQLITE_BIG_DBL;
101039 
101040   /* If the pSrc table is the right table of a LEFT JOIN then we may not
101041   ** use an index to satisfy IS NULL constraints on that table.  This is
101042   ** because columns might end up being NULL if the table does not match -
101043   ** a circumstance which the index cannot help us discover.  Ticket #2177.
101044   */
101045   if( pSrc->jointype & JT_LEFT ){
101046     idxEqTermMask = WO_EQ|WO_IN;
101047   }else{
101048     idxEqTermMask = WO_EQ|WO_IN|WO_ISNULL;
101049   }
101050 
101051   if( pSrc->pIndex ){
101052     /* An INDEXED BY clause specifies a particular index to use */
101053     pIdx = pProbe = pSrc->pIndex;
101054     wsFlagMask = ~(WHERE_ROWID_EQ|WHERE_ROWID_RANGE);
101055     eqTermMask = idxEqTermMask;
101056   }else{
101057     /* There is no INDEXED BY clause.  Create a fake Index object in local
101058     ** variable sPk to represent the rowid primary key index.  Make this
101059     ** fake index the first in a chain of Index objects with all of the real
101060     ** indices to follow */
101061     Index *pFirst;                  /* First of real indices on the table */
101062     memset(&sPk, 0, sizeof(Index));
101063     sPk.nColumn = 1;
101064     sPk.aiColumn = &aiColumnPk;
101065     sPk.aiRowEst = aiRowEstPk;
101066     sPk.onError = OE_Replace;
101067     sPk.pTable = pSrc->pTab;
101068     aiRowEstPk[0] = pSrc->pTab->nRowEst;
101069     aiRowEstPk[1] = 1;
101070     pFirst = pSrc->pTab->pIndex;
101071     if( pSrc->notIndexed==0 ){
101072       /* The real indices of the table are only considered if the
101073       ** NOT INDEXED qualifier is omitted from the FROM clause */
101074       sPk.pNext = pFirst;
101075     }
101076     pProbe = &sPk;
101077     wsFlagMask = ~(
101078         WHERE_COLUMN_IN|WHERE_COLUMN_EQ|WHERE_COLUMN_NULL|WHERE_COLUMN_RANGE
101079     );
101080     eqTermMask = WO_EQ|WO_IN;
101081     pIdx = 0;
101082   }
101083 
101084   /* Loop over all indices looking for the best one to use
101085   */
101086   for(; pProbe; pIdx=pProbe=pProbe->pNext){
101087     const unsigned int * const aiRowEst = pProbe->aiRowEst;
101088     double cost;                /* Cost of using pProbe */
101089     double nRow;                /* Estimated number of rows in result set */
101090     double log10N;              /* base-10 logarithm of nRow (inexact) */
101091     int rev;                    /* True to scan in reverse order */
101092     int wsFlags = 0;
101093     Bitmask used = 0;
101094 
101095     /* The following variables are populated based on the properties of
101096     ** index being evaluated. They are then used to determine the expected
101097     ** cost and number of rows returned.
101098     **
101099     **  nEq:
101100     **    Number of equality terms that can be implemented using the index.
101101     **    In other words, the number of initial fields in the index that
101102     **    are used in == or IN or NOT NULL constraints of the WHERE clause.
101103     **
101104     **  nInMul:
101105     **    The "in-multiplier". This is an estimate of how many seek operations
101106     **    SQLite must perform on the index in question. For example, if the
101107     **    WHERE clause is:
101108     **
101109     **      WHERE a IN (1, 2, 3) AND b IN (4, 5, 6)
101110     **
101111     **    SQLite must perform 9 lookups on an index on (a, b), so nInMul is
101112     **    set to 9. Given the same schema and either of the following WHERE
101113     **    clauses:
101114     **
101115     **      WHERE a =  1
101116     **      WHERE a >= 2
101117     **
101118     **    nInMul is set to 1.
101119     **
101120     **    If there exists a WHERE term of the form "x IN (SELECT ...)", then
101121     **    the sub-select is assumed to return 25 rows for the purposes of
101122     **    determining nInMul.
101123     **
101124     **  bInEst:
101125     **    Set to true if there was at least one "x IN (SELECT ...)" term used
101126     **    in determining the value of nInMul.  Note that the RHS of the
101127     **    IN operator must be a SELECT, not a value list, for this variable
101128     **    to be true.
101129     **
101130     **  estBound:
101131     **    An estimate on the amount of the table that must be searched.  A
101132     **    value of 100 means the entire table is searched.  Range constraints
101133     **    might reduce this to a value less than 100 to indicate that only
101134     **    a fraction of the table needs searching.  In the absence of
101135     **    sqlite_stat2 ANALYZE data, a single inequality reduces the search
101136     **    space to 1/4rd its original size.  So an x>? constraint reduces
101137     **    estBound to 25.  Two constraints (x>? AND x<?) reduce estBound to 6.
101138     **
101139     **  bSort:
101140     **    Boolean. True if there is an ORDER BY clause that will require an
101141     **    external sort (i.e. scanning the index being evaluated will not
101142     **    correctly order records).
101143     **
101144     **  bLookup:
101145     **    Boolean. True if a table lookup is required for each index entry
101146     **    visited.  In other words, true if this is not a covering index.
101147     **    This is always false for the rowid primary key index of a table.
101148     **    For other indexes, it is true unless all the columns of the table
101149     **    used by the SELECT statement are present in the index (such an
101150     **    index is sometimes described as a covering index).
101151     **    For example, given the index on (a, b), the second of the following
101152     **    two queries requires table b-tree lookups in order to find the value
101153     **    of column c, but the first does not because columns a and b are
101154     **    both available in the index.
101155     **
101156     **             SELECT a, b    FROM tbl WHERE a = 1;
101157     **             SELECT a, b, c FROM tbl WHERE a = 1;
101158     */
101159     int nEq;                      /* Number of == or IN terms matching index */
101160     int bInEst = 0;               /* True if "x IN (SELECT...)" seen */
101161     int nInMul = 1;               /* Number of distinct equalities to lookup */
101162     int estBound = 100;           /* Estimated reduction in search space */
101163     int nBound = 0;               /* Number of range constraints seen */
101164     int bSort = 0;                /* True if external sort required */
101165     int bLookup = 0;              /* True if not a covering index */
101166     WhereTerm *pTerm;             /* A single term of the WHERE clause */
101167 #ifdef SQLITE_ENABLE_STAT2
101168     WhereTerm *pFirstTerm = 0;    /* First term matching the index */
101169 #endif
101170 
101171     /* Determine the values of nEq and nInMul */
101172     for(nEq=0; nEq<pProbe->nColumn; nEq++){
101173       int j = pProbe->aiColumn[nEq];
101174       pTerm = findTerm(pWC, iCur, j, notReady, eqTermMask, pIdx);
101175       if( pTerm==0 ) break;
101176       wsFlags |= (WHERE_COLUMN_EQ|WHERE_ROWID_EQ);
101177       if( pTerm->eOperator & WO_IN ){
101178         Expr *pExpr = pTerm->pExpr;
101179         wsFlags |= WHERE_COLUMN_IN;
101180         if( ExprHasProperty(pExpr, EP_xIsSelect) ){
101181           /* "x IN (SELECT ...)":  Assume the SELECT returns 25 rows */
101182           nInMul *= 25;
101183           bInEst = 1;
101184         }else if( ALWAYS(pExpr->x.pList && pExpr->x.pList->nExpr) ){
101185           /* "x IN (value, value, ...)" */
101186           nInMul *= pExpr->x.pList->nExpr;
101187         }
101188       }else if( pTerm->eOperator & WO_ISNULL ){
101189         wsFlags |= WHERE_COLUMN_NULL;
101190       }
101191 #ifdef SQLITE_ENABLE_STAT2
101192       if( nEq==0 && pProbe->aSample ) pFirstTerm = pTerm;
101193 #endif
101194       used |= pTerm->prereqRight;
101195     }
101196 
101197     /* Determine the value of estBound. */
101198     if( nEq<pProbe->nColumn && pProbe->bUnordered==0 ){
101199       int j = pProbe->aiColumn[nEq];
101200       if( findTerm(pWC, iCur, j, notReady, WO_LT|WO_LE|WO_GT|WO_GE, pIdx) ){
101201         WhereTerm *pTop = findTerm(pWC, iCur, j, notReady, WO_LT|WO_LE, pIdx);
101202         WhereTerm *pBtm = findTerm(pWC, iCur, j, notReady, WO_GT|WO_GE, pIdx);
101203         whereRangeScanEst(pParse, pProbe, nEq, pBtm, pTop, &estBound);
101204         if( pTop ){
101205           nBound = 1;
101206           wsFlags |= WHERE_TOP_LIMIT;
101207           used |= pTop->prereqRight;
101208         }
101209         if( pBtm ){
101210           nBound++;
101211           wsFlags |= WHERE_BTM_LIMIT;
101212           used |= pBtm->prereqRight;
101213         }
101214         wsFlags |= (WHERE_COLUMN_RANGE|WHERE_ROWID_RANGE);
101215       }
101216     }else if( pProbe->onError!=OE_None ){
101217       testcase( wsFlags & WHERE_COLUMN_IN );
101218       testcase( wsFlags & WHERE_COLUMN_NULL );
101219       if( (wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_NULL))==0 ){
101220         wsFlags |= WHERE_UNIQUE;
101221       }
101222     }
101223 
101224     /* If there is an ORDER BY clause and the index being considered will
101225     ** naturally scan rows in the required order, set the appropriate flags
101226     ** in wsFlags. Otherwise, if there is an ORDER BY clause but the index
101227     ** will scan rows in a different order, set the bSort variable.  */
101228     if( pOrderBy ){
101229       if( (wsFlags & WHERE_COLUMN_IN)==0
101230         && pProbe->bUnordered==0
101231         && isSortingIndex(pParse, pWC->pMaskSet, pProbe, iCur, pOrderBy,
101232                           nEq, wsFlags, &rev)
101233       ){
101234         wsFlags |= WHERE_ROWID_RANGE|WHERE_COLUMN_RANGE|WHERE_ORDERBY;
101235         wsFlags |= (rev ? WHERE_REVERSE : 0);
101236       }else{
101237         bSort = 1;
101238       }
101239     }
101240 
101241     /* If currently calculating the cost of using an index (not the IPK
101242     ** index), determine if all required column data may be obtained without
101243     ** using the main table (i.e. if the index is a covering
101244     ** index for this query). If it is, set the WHERE_IDX_ONLY flag in
101245     ** wsFlags. Otherwise, set the bLookup variable to true.  */
101246     if( pIdx && wsFlags ){
101247       Bitmask m = pSrc->colUsed;
101248       int j;
101249       for(j=0; j<pIdx->nColumn; j++){
101250         int x = pIdx->aiColumn[j];
101251         if( x<BMS-1 ){
101252           m &= ~(((Bitmask)1)<<x);
101253         }
101254       }
101255       if( m==0 ){
101256         wsFlags |= WHERE_IDX_ONLY;
101257       }else{
101258         bLookup = 1;
101259       }
101260     }
101261 
101262     /*
101263     ** Estimate the number of rows of output.  For an "x IN (SELECT...)"
101264     ** constraint, do not let the estimate exceed half the rows in the table.
101265     */
101266     nRow = (double)(aiRowEst[nEq] * nInMul);
101267     if( bInEst && nRow*2>aiRowEst[0] ){
101268       nRow = aiRowEst[0]/2;
101269       nInMul = (int)(nRow / aiRowEst[nEq]);
101270     }
101271 
101272 #ifdef SQLITE_ENABLE_STAT2
101273     /* If the constraint is of the form x=VALUE and histogram
101274     ** data is available for column x, then it might be possible
101275     ** to get a better estimate on the number of rows based on
101276     ** VALUE and how common that value is according to the histogram.
101277     */
101278     if( nRow>(double)1 && nEq==1 && pFirstTerm!=0 ){
101279       if( pFirstTerm->eOperator & (WO_EQ|WO_ISNULL) ){
101280         testcase( pFirstTerm->eOperator==WO_EQ );
101281         testcase( pFirstTerm->eOperator==WO_ISNULL );
101282         whereEqualScanEst(pParse, pProbe, pFirstTerm->pExpr->pRight, &nRow);
101283       }else if( pFirstTerm->eOperator==WO_IN && bInEst==0 ){
101284         whereInScanEst(pParse, pProbe, pFirstTerm->pExpr->x.pList, &nRow);
101285       }
101286     }
101287 #endif /* SQLITE_ENABLE_STAT2 */
101288 
101289     /* Adjust the number of output rows and downward to reflect rows
101290     ** that are excluded by range constraints.
101291     */
101292     nRow = (nRow * (double)estBound) / (double)100;
101293     if( nRow<1 ) nRow = 1;
101294 
101295     /* Experiments run on real SQLite databases show that the time needed
101296     ** to do a binary search to locate a row in a table or index is roughly
101297     ** log10(N) times the time to move from one row to the next row within
101298     ** a table or index.  The actual times can vary, with the size of
101299     ** records being an important factor.  Both moves and searches are
101300     ** slower with larger records, presumably because fewer records fit
101301     ** on one page and hence more pages have to be fetched.
101302     **
101303     ** The ANALYZE command and the sqlite_stat1 and sqlite_stat2 tables do
101304     ** not give us data on the relative sizes of table and index records.
101305     ** So this computation assumes table records are about twice as big
101306     ** as index records
101307     */
101308     if( (wsFlags & WHERE_NOT_FULLSCAN)==0 ){
101309       /* The cost of a full table scan is a number of move operations equal
101310       ** to the number of rows in the table.
101311       **
101312       ** We add an additional 4x penalty to full table scans.  This causes
101313       ** the cost function to err on the side of choosing an index over
101314       ** choosing a full scan.  This 4x full-scan penalty is an arguable
101315       ** decision and one which we expect to revisit in the future.  But
101316       ** it seems to be working well enough at the moment.
101317       */
101318       cost = aiRowEst[0]*4;
101319     }else{
101320       log10N = estLog(aiRowEst[0]);
101321       cost = nRow;
101322       if( pIdx ){
101323         if( bLookup ){
101324           /* For an index lookup followed by a table lookup:
101325           **    nInMul index searches to find the start of each index range
101326           **  + nRow steps through the index
101327           **  + nRow table searches to lookup the table entry using the rowid
101328           */
101329           cost += (nInMul + nRow)*log10N;
101330         }else{
101331           /* For a covering index:
101332           **     nInMul index searches to find the initial entry
101333           **   + nRow steps through the index
101334           */
101335           cost += nInMul*log10N;
101336         }
101337       }else{
101338         /* For a rowid primary key lookup:
101339         **    nInMult table searches to find the initial entry for each range
101340         **  + nRow steps through the table
101341         */
101342         cost += nInMul*log10N;
101343       }
101344     }
101345 
101346     /* Add in the estimated cost of sorting the result.  Actual experimental
101347     ** measurements of sorting performance in SQLite show that sorting time
101348     ** adds C*N*log10(N) to the cost, where N is the number of rows to be
101349     ** sorted and C is a factor between 1.95 and 4.3.  We will split the
101350     ** difference and select C of 3.0.
101351     */
101352     if( bSort ){
101353       cost += nRow*estLog(nRow)*3;
101354     }
101355 
101356     /**** Cost of using this index has now been computed ****/
101357 
101358     /* If there are additional constraints on this table that cannot
101359     ** be used with the current index, but which might lower the number
101360     ** of output rows, adjust the nRow value accordingly.  This only
101361     ** matters if the current index is the least costly, so do not bother
101362     ** with this step if we already know this index will not be chosen.
101363     ** Also, never reduce the output row count below 2 using this step.
101364     **
101365     ** It is critical that the notValid mask be used here instead of
101366     ** the notReady mask.  When computing an "optimal" index, the notReady
101367     ** mask will only have one bit set - the bit for the current table.
101368     ** The notValid mask, on the other hand, always has all bits set for
101369     ** tables that are not in outer loops.  If notReady is used here instead
101370     ** of notValid, then a optimal index that depends on inner joins loops
101371     ** might be selected even when there exists an optimal index that has
101372     ** no such dependency.
101373     */
101374     if( nRow>2 && cost<=pCost->rCost ){
101375       int k;                       /* Loop counter */
101376       int nSkipEq = nEq;           /* Number of == constraints to skip */
101377       int nSkipRange = nBound;     /* Number of < constraints to skip */
101378       Bitmask thisTab;             /* Bitmap for pSrc */
101379 
101380       thisTab = getMask(pWC->pMaskSet, iCur);
101381       for(pTerm=pWC->a, k=pWC->nTerm; nRow>2 && k; k--, pTerm++){
101382         if( pTerm->wtFlags & TERM_VIRTUAL ) continue;
101383         if( (pTerm->prereqAll & notValid)!=thisTab ) continue;
101384         if( pTerm->eOperator & (WO_EQ|WO_IN|WO_ISNULL) ){
101385           if( nSkipEq ){
101386             /* Ignore the first nEq equality matches since the index
101387             ** has already accounted for these */
101388             nSkipEq--;
101389           }else{
101390             /* Assume each additional equality match reduces the result
101391             ** set size by a factor of 10 */
101392             nRow /= 10;
101393           }
101394         }else if( pTerm->eOperator & (WO_LT|WO_LE|WO_GT|WO_GE) ){
101395           if( nSkipRange ){
101396             /* Ignore the first nSkipRange range constraints since the index
101397             ** has already accounted for these */
101398             nSkipRange--;
101399           }else{
101400             /* Assume each additional range constraint reduces the result
101401             ** set size by a factor of 3.  Indexed range constraints reduce
101402             ** the search space by a larger factor: 4.  We make indexed range
101403             ** more selective intentionally because of the subjective
101404             ** observation that indexed range constraints really are more
101405             ** selective in practice, on average. */
101406             nRow /= 3;
101407           }
101408         }else if( pTerm->eOperator!=WO_NOOP ){
101409           /* Any other expression lowers the output row count by half */
101410           nRow /= 2;
101411         }
101412       }
101413       if( nRow<2 ) nRow = 2;
101414     }
101415 
101416 
101417     WHERETRACE((
101418       "%s(%s): nEq=%d nInMul=%d estBound=%d bSort=%d bLookup=%d wsFlags=0x%x\n"
101419       "         notReady=0x%llx log10N=%.1f nRow=%.1f cost=%.1f used=0x%llx\n",
101420       pSrc->pTab->zName, (pIdx ? pIdx->zName : "ipk"),
101421       nEq, nInMul, estBound, bSort, bLookup, wsFlags,
101422       notReady, log10N, nRow, cost, used
101423     ));
101424 
101425     /* If this index is the best we have seen so far, then record this
101426     ** index and its cost in the pCost structure.
101427     */
101428     if( (!pIdx || wsFlags)
101429      && (cost<pCost->rCost || (cost<=pCost->rCost && nRow<pCost->plan.nRow))
101430     ){
101431       pCost->rCost = cost;
101432       pCost->used = used;
101433       pCost->plan.nRow = nRow;
101434       pCost->plan.wsFlags = (wsFlags&wsFlagMask);
101435       pCost->plan.nEq = nEq;
101436       pCost->plan.u.pIdx = pIdx;
101437     }
101438 
101439     /* If there was an INDEXED BY clause, then only that one index is
101440     ** considered. */
101441     if( pSrc->pIndex ) break;
101442 
101443     /* Reset masks for the next index in the loop */
101444     wsFlagMask = ~(WHERE_ROWID_EQ|WHERE_ROWID_RANGE);
101445     eqTermMask = idxEqTermMask;
101446   }
101447 
101448   /* If there is no ORDER BY clause and the SQLITE_ReverseOrder flag
101449   ** is set, then reverse the order that the index will be scanned
101450   ** in. This is used for application testing, to help find cases
101451   ** where application behaviour depends on the (undefined) order that
101452   ** SQLite outputs rows in in the absence of an ORDER BY clause.  */
101453   if( !pOrderBy && pParse->db->flags & SQLITE_ReverseOrder ){
101454     pCost->plan.wsFlags |= WHERE_REVERSE;
101455   }
101456 
101457   assert( pOrderBy || (pCost->plan.wsFlags&WHERE_ORDERBY)==0 );
101458   assert( pCost->plan.u.pIdx==0 || (pCost->plan.wsFlags&WHERE_ROWID_EQ)==0 );
101459   assert( pSrc->pIndex==0
101460        || pCost->plan.u.pIdx==0
101461        || pCost->plan.u.pIdx==pSrc->pIndex
101462   );
101463 
101464   WHERETRACE(("best index is: %s\n",
101465     ((pCost->plan.wsFlags & WHERE_NOT_FULLSCAN)==0 ? "none" :
101466          pCost->plan.u.pIdx ? pCost->plan.u.pIdx->zName : "ipk")
101467   ));
101468 
101469   bestOrClauseIndex(pParse, pWC, pSrc, notReady, notValid, pOrderBy, pCost);
101470   bestAutomaticIndex(pParse, pWC, pSrc, notReady, pCost);
101471   pCost->plan.wsFlags |= eqTermMask;
101472 }
101473 
101474 /*
101475 ** Find the query plan for accessing table pSrc->pTab. Write the
101476 ** best query plan and its cost into the WhereCost object supplied
101477 ** as the last parameter. This function may calculate the cost of
101478 ** both real and virtual table scans.
101479 */
101480 static void bestIndex(
101481   Parse *pParse,              /* The parsing context */
101482   WhereClause *pWC,           /* The WHERE clause */
101483   struct SrcList_item *pSrc,  /* The FROM clause term to search */
101484   Bitmask notReady,           /* Mask of cursors not available for indexing */
101485   Bitmask notValid,           /* Cursors not available for any purpose */
101486   ExprList *pOrderBy,         /* The ORDER BY clause */
101487   WhereCost *pCost            /* Lowest cost query plan */
101488 ){
101489 #ifndef SQLITE_OMIT_VIRTUALTABLE
101490   if( IsVirtual(pSrc->pTab) ){
101491     sqlite3_index_info *p = 0;
101492     bestVirtualIndex(pParse, pWC, pSrc, notReady, notValid, pOrderBy, pCost,&p);
101493     if( p->needToFreeIdxStr ){
101494       sqlite3_free(p->idxStr);
101495     }
101496     sqlite3DbFree(pParse->db, p);
101497   }else
101498 #endif
101499   {
101500     bestBtreeIndex(pParse, pWC, pSrc, notReady, notValid, pOrderBy, pCost);
101501   }
101502 }
101503 
101504 /*
101505 ** Disable a term in the WHERE clause.  Except, do not disable the term
101506 ** if it controls a LEFT OUTER JOIN and it did not originate in the ON
101507 ** or USING clause of that join.
101508 **
101509 ** Consider the term t2.z='ok' in the following queries:
101510 **
101511 **   (1)  SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x WHERE t2.z='ok'
101512 **   (2)  SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x AND t2.z='ok'
101513 **   (3)  SELECT * FROM t1, t2 WHERE t1.a=t2.x AND t2.z='ok'
101514 **
101515 ** The t2.z='ok' is disabled in the in (2) because it originates
101516 ** in the ON clause.  The term is disabled in (3) because it is not part
101517 ** of a LEFT OUTER JOIN.  In (1), the term is not disabled.
101518 **
101519 ** IMPLEMENTATION-OF: R-24597-58655 No tests are done for terms that are
101520 ** completely satisfied by indices.
101521 **
101522 ** Disabling a term causes that term to not be tested in the inner loop
101523 ** of the join.  Disabling is an optimization.  When terms are satisfied
101524 ** by indices, we disable them to prevent redundant tests in the inner
101525 ** loop.  We would get the correct results if nothing were ever disabled,
101526 ** but joins might run a little slower.  The trick is to disable as much
101527 ** as we can without disabling too much.  If we disabled in (1), we'd get
101528 ** the wrong answer.  See ticket #813.
101529 */
101530 static void disableTerm(WhereLevel *pLevel, WhereTerm *pTerm){
101531   if( pTerm
101532       && (pTerm->wtFlags & TERM_CODED)==0
101533       && (pLevel->iLeftJoin==0 || ExprHasProperty(pTerm->pExpr, EP_FromJoin))
101534   ){
101535     pTerm->wtFlags |= TERM_CODED;
101536     if( pTerm->iParent>=0 ){
101537       WhereTerm *pOther = &pTerm->pWC->a[pTerm->iParent];
101538       if( (--pOther->nChild)==0 ){
101539         disableTerm(pLevel, pOther);
101540       }
101541     }
101542   }
101543 }
101544 
101545 /*
101546 ** Code an OP_Affinity opcode to apply the column affinity string zAff
101547 ** to the n registers starting at base.
101548 **
101549 ** As an optimization, SQLITE_AFF_NONE entries (which are no-ops) at the
101550 ** beginning and end of zAff are ignored.  If all entries in zAff are
101551 ** SQLITE_AFF_NONE, then no code gets generated.
101552 **
101553 ** This routine makes its own copy of zAff so that the caller is free
101554 ** to modify zAff after this routine returns.
101555 */
101556 static void codeApplyAffinity(Parse *pParse, int base, int n, char *zAff){
101557   Vdbe *v = pParse->pVdbe;
101558   if( zAff==0 ){
101559     assert( pParse->db->mallocFailed );
101560     return;
101561   }
101562   assert( v!=0 );
101563 
101564   /* Adjust base and n to skip over SQLITE_AFF_NONE entries at the beginning
101565   ** and end of the affinity string.
101566   */
101567   while( n>0 && zAff[0]==SQLITE_AFF_NONE ){
101568     n--;
101569     base++;
101570     zAff++;
101571   }
101572   while( n>1 && zAff[n-1]==SQLITE_AFF_NONE ){
101573     n--;
101574   }
101575 
101576   /* Code the OP_Affinity opcode if there is anything left to do. */
101577   if( n>0 ){
101578     sqlite3VdbeAddOp2(v, OP_Affinity, base, n);
101579     sqlite3VdbeChangeP4(v, -1, zAff, n);
101580     sqlite3ExprCacheAffinityChange(pParse, base, n);
101581   }
101582 }
101583 
101584 
101585 /*
101586 ** Generate code for a single equality term of the WHERE clause.  An equality
101587 ** term can be either X=expr or X IN (...).   pTerm is the term to be
101588 ** coded.
101589 **
101590 ** The current value for the constraint is left in register iReg.
101591 **
101592 ** For a constraint of the form X=expr, the expression is evaluated and its
101593 ** result is left on the stack.  For constraints of the form X IN (...)
101594 ** this routine sets up a loop that will iterate over all values of X.
101595 */
101596 static int codeEqualityTerm(
101597   Parse *pParse,      /* The parsing context */
101598   WhereTerm *pTerm,   /* The term of the WHERE clause to be coded */
101599   WhereLevel *pLevel, /* When level of the FROM clause we are working on */
101600   int iTarget         /* Attempt to leave results in this register */
101601 ){
101602   Expr *pX = pTerm->pExpr;
101603   Vdbe *v = pParse->pVdbe;
101604   int iReg;                  /* Register holding results */
101605 
101606   assert( iTarget>0 );
101607   if( pX->op==TK_EQ ){
101608     iReg = sqlite3ExprCodeTarget(pParse, pX->pRight, iTarget);
101609   }else if( pX->op==TK_ISNULL ){
101610     iReg = iTarget;
101611     sqlite3VdbeAddOp2(v, OP_Null, 0, iReg);
101612 #ifndef SQLITE_OMIT_SUBQUERY
101613   }else{
101614     int eType;
101615     int iTab;
101616     struct InLoop *pIn;
101617 
101618     assert( pX->op==TK_IN );
101619     iReg = iTarget;
101620     eType = sqlite3FindInIndex(pParse, pX, 0);
101621     iTab = pX->iTable;
101622     sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0);
101623     assert( pLevel->plan.wsFlags & WHERE_IN_ABLE );
101624     if( pLevel->u.in.nIn==0 ){
101625       pLevel->addrNxt = sqlite3VdbeMakeLabel(v);
101626     }
101627     pLevel->u.in.nIn++;
101628     pLevel->u.in.aInLoop =
101629        sqlite3DbReallocOrFree(pParse->db, pLevel->u.in.aInLoop,
101630                               sizeof(pLevel->u.in.aInLoop[0])*pLevel->u.in.nIn);
101631     pIn = pLevel->u.in.aInLoop;
101632     if( pIn ){
101633       pIn += pLevel->u.in.nIn - 1;
101634       pIn->iCur = iTab;
101635       if( eType==IN_INDEX_ROWID ){
101636         pIn->addrInTop = sqlite3VdbeAddOp2(v, OP_Rowid, iTab, iReg);
101637       }else{
101638         pIn->addrInTop = sqlite3VdbeAddOp3(v, OP_Column, iTab, 0, iReg);
101639       }
101640       sqlite3VdbeAddOp1(v, OP_IsNull, iReg);
101641     }else{
101642       pLevel->u.in.nIn = 0;
101643     }
101644 #endif
101645   }
101646   disableTerm(pLevel, pTerm);
101647   return iReg;
101648 }
101649 
101650 /*
101651 ** Generate code that will evaluate all == and IN constraints for an
101652 ** index.
101653 **
101654 ** For example, consider table t1(a,b,c,d,e,f) with index i1(a,b,c).
101655 ** Suppose the WHERE clause is this:  a==5 AND b IN (1,2,3) AND c>5 AND c<10
101656 ** The index has as many as three equality constraints, but in this
101657 ** example, the third "c" value is an inequality.  So only two
101658 ** constraints are coded.  This routine will generate code to evaluate
101659 ** a==5 and b IN (1,2,3).  The current values for a and b will be stored
101660 ** in consecutive registers and the index of the first register is returned.
101661 **
101662 ** In the example above nEq==2.  But this subroutine works for any value
101663 ** of nEq including 0.  If nEq==0, this routine is nearly a no-op.
101664 ** The only thing it does is allocate the pLevel->iMem memory cell and
101665 ** compute the affinity string.
101666 **
101667 ** This routine always allocates at least one memory cell and returns
101668 ** the index of that memory cell. The code that
101669 ** calls this routine will use that memory cell to store the termination
101670 ** key value of the loop.  If one or more IN operators appear, then
101671 ** this routine allocates an additional nEq memory cells for internal
101672 ** use.
101673 **
101674 ** Before returning, *pzAff is set to point to a buffer containing a
101675 ** copy of the column affinity string of the index allocated using
101676 ** sqlite3DbMalloc(). Except, entries in the copy of the string associated
101677 ** with equality constraints that use NONE affinity are set to
101678 ** SQLITE_AFF_NONE. This is to deal with SQL such as the following:
101679 **
101680 **   CREATE TABLE t1(a TEXT PRIMARY KEY, b);
101681 **   SELECT ... FROM t1 AS t2, t1 WHERE t1.a = t2.b;
101682 **
101683 ** In the example above, the index on t1(a) has TEXT affinity. But since
101684 ** the right hand side of the equality constraint (t2.b) has NONE affinity,
101685 ** no conversion should be attempted before using a t2.b value as part of
101686 ** a key to search the index. Hence the first byte in the returned affinity
101687 ** string in this example would be set to SQLITE_AFF_NONE.
101688 */
101689 static int codeAllEqualityTerms(
101690   Parse *pParse,        /* Parsing context */
101691   WhereLevel *pLevel,   /* Which nested loop of the FROM we are coding */
101692   WhereClause *pWC,     /* The WHERE clause */
101693   Bitmask notReady,     /* Which parts of FROM have not yet been coded */
101694   int nExtraReg,        /* Number of extra registers to allocate */
101695   char **pzAff          /* OUT: Set to point to affinity string */
101696 ){
101697   int nEq = pLevel->plan.nEq;   /* The number of == or IN constraints to code */
101698   Vdbe *v = pParse->pVdbe;      /* The vm under construction */
101699   Index *pIdx;                  /* The index being used for this loop */
101700   int iCur = pLevel->iTabCur;   /* The cursor of the table */
101701   WhereTerm *pTerm;             /* A single constraint term */
101702   int j;                        /* Loop counter */
101703   int regBase;                  /* Base register */
101704   int nReg;                     /* Number of registers to allocate */
101705   char *zAff;                   /* Affinity string to return */
101706 
101707   /* This module is only called on query plans that use an index. */
101708   assert( pLevel->plan.wsFlags & WHERE_INDEXED );
101709   pIdx = pLevel->plan.u.pIdx;
101710 
101711   /* Figure out how many memory cells we will need then allocate them.
101712   */
101713   regBase = pParse->nMem + 1;
101714   nReg = pLevel->plan.nEq + nExtraReg;
101715   pParse->nMem += nReg;
101716 
101717   zAff = sqlite3DbStrDup(pParse->db, sqlite3IndexAffinityStr(v, pIdx));
101718   if( !zAff ){
101719     pParse->db->mallocFailed = 1;
101720   }
101721 
101722   /* Evaluate the equality constraints
101723   */
101724   assert( pIdx->nColumn>=nEq );
101725   for(j=0; j<nEq; j++){
101726     int r1;
101727     int k = pIdx->aiColumn[j];
101728     pTerm = findTerm(pWC, iCur, k, notReady, pLevel->plan.wsFlags, pIdx);
101729     if( NEVER(pTerm==0) ) break;
101730     /* The following true for indices with redundant columns.
101731     ** Ex: CREATE INDEX i1 ON t1(a,b,a); SELECT * FROM t1 WHERE a=0 AND b=0; */
101732     testcase( (pTerm->wtFlags & TERM_CODED)!=0 );
101733     testcase( pTerm->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */
101734     r1 = codeEqualityTerm(pParse, pTerm, pLevel, regBase+j);
101735     if( r1!=regBase+j ){
101736       if( nReg==1 ){
101737         sqlite3ReleaseTempReg(pParse, regBase);
101738         regBase = r1;
101739       }else{
101740         sqlite3VdbeAddOp2(v, OP_SCopy, r1, regBase+j);
101741       }
101742     }
101743     testcase( pTerm->eOperator & WO_ISNULL );
101744     testcase( pTerm->eOperator & WO_IN );
101745     if( (pTerm->eOperator & (WO_ISNULL|WO_IN))==0 ){
101746       Expr *pRight = pTerm->pExpr->pRight;
101747       sqlite3ExprCodeIsNullJump(v, pRight, regBase+j, pLevel->addrBrk);
101748       if( zAff ){
101749         if( sqlite3CompareAffinity(pRight, zAff[j])==SQLITE_AFF_NONE ){
101750           zAff[j] = SQLITE_AFF_NONE;
101751         }
101752         if( sqlite3ExprNeedsNoAffinityChange(pRight, zAff[j]) ){
101753           zAff[j] = SQLITE_AFF_NONE;
101754         }
101755       }
101756     }
101757   }
101758   *pzAff = zAff;
101759   return regBase;
101760 }
101761 
101762 #ifndef SQLITE_OMIT_EXPLAIN
101763 /*
101764 ** This routine is a helper for explainIndexRange() below
101765 **
101766 ** pStr holds the text of an expression that we are building up one term
101767 ** at a time.  This routine adds a new term to the end of the expression.
101768 ** Terms are separated by AND so add the "AND" text for second and subsequent
101769 ** terms only.
101770 */
101771 static void explainAppendTerm(
101772   StrAccum *pStr,             /* The text expression being built */
101773   int iTerm,                  /* Index of this term.  First is zero */
101774   const char *zColumn,        /* Name of the column */
101775   const char *zOp             /* Name of the operator */
101776 ){
101777   if( iTerm ) sqlite3StrAccumAppend(pStr, " AND ", 5);
101778   sqlite3StrAccumAppend(pStr, zColumn, -1);
101779   sqlite3StrAccumAppend(pStr, zOp, 1);
101780   sqlite3StrAccumAppend(pStr, "?", 1);
101781 }
101782 
101783 /*
101784 ** Argument pLevel describes a strategy for scanning table pTab. This
101785 ** function returns a pointer to a string buffer containing a description
101786 ** of the subset of table rows scanned by the strategy in the form of an
101787 ** SQL expression. Or, if all rows are scanned, NULL is returned.
101788 **
101789 ** For example, if the query:
101790 **
101791 **   SELECT * FROM t1 WHERE a=1 AND b>2;
101792 **
101793 ** is run and there is an index on (a, b), then this function returns a
101794 ** string similar to:
101795 **
101796 **   "a=? AND b>?"
101797 **
101798 ** The returned pointer points to memory obtained from sqlite3DbMalloc().
101799 ** It is the responsibility of the caller to free the buffer when it is
101800 ** no longer required.
101801 */
101802 static char *explainIndexRange(sqlite3 *db, WhereLevel *pLevel, Table *pTab){
101803   WherePlan *pPlan = &pLevel->plan;
101804   Index *pIndex = pPlan->u.pIdx;
101805   int nEq = pPlan->nEq;
101806   int i, j;
101807   Column *aCol = pTab->aCol;
101808   int *aiColumn = pIndex->aiColumn;
101809   StrAccum txt;
101810 
101811   if( nEq==0 && (pPlan->wsFlags & (WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))==0 ){
101812     return 0;
101813   }
101814   sqlite3StrAccumInit(&txt, 0, 0, SQLITE_MAX_LENGTH);
101815   txt.db = db;
101816   sqlite3StrAccumAppend(&txt, " (", 2);
101817   for(i=0; i<nEq; i++){
101818     explainAppendTerm(&txt, i, aCol[aiColumn[i]].zName, "=");
101819   }
101820 
101821   j = i;
101822   if( pPlan->wsFlags&WHERE_BTM_LIMIT ){
101823     explainAppendTerm(&txt, i++, aCol[aiColumn[j]].zName, ">");
101824   }
101825   if( pPlan->wsFlags&WHERE_TOP_LIMIT ){
101826     explainAppendTerm(&txt, i, aCol[aiColumn[j]].zName, "<");
101827   }
101828   sqlite3StrAccumAppend(&txt, ")", 1);
101829   return sqlite3StrAccumFinish(&txt);
101830 }
101831 
101832 /*
101833 ** This function is a no-op unless currently processing an EXPLAIN QUERY PLAN
101834 ** command. If the query being compiled is an EXPLAIN QUERY PLAN, a single
101835 ** record is added to the output to describe the table scan strategy in
101836 ** pLevel.
101837 */
101838 static void explainOneScan(
101839   Parse *pParse,                  /* Parse context */
101840   SrcList *pTabList,              /* Table list this loop refers to */
101841   WhereLevel *pLevel,             /* Scan to write OP_Explain opcode for */
101842   int iLevel,                     /* Value for "level" column of output */
101843   int iFrom,                      /* Value for "from" column of output */
101844   u16 wctrlFlags                  /* Flags passed to sqlite3WhereBegin() */
101845 ){
101846   if( pParse->explain==2 ){
101847     u32 flags = pLevel->plan.wsFlags;
101848     struct SrcList_item *pItem = &pTabList->a[pLevel->iFrom];
101849     Vdbe *v = pParse->pVdbe;      /* VM being constructed */
101850     sqlite3 *db = pParse->db;     /* Database handle */
101851     char *zMsg;                   /* Text to add to EQP output */
101852     sqlite3_int64 nRow;           /* Expected number of rows visited by scan */
101853     int iId = pParse->iSelectId;  /* Select id (left-most output column) */
101854     int isSearch;                 /* True for a SEARCH. False for SCAN. */
101855 
101856     if( (flags&WHERE_MULTI_OR) || (wctrlFlags&WHERE_ONETABLE_ONLY) ) return;
101857 
101858     isSearch = (pLevel->plan.nEq>0)
101859              || (flags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0
101860              || (wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX));
101861 
101862     zMsg = sqlite3MPrintf(db, "%s", isSearch?"SEARCH":"SCAN");
101863     if( pItem->pSelect ){
101864       zMsg = sqlite3MAppendf(db, zMsg, "%s SUBQUERY %d", zMsg,pItem->iSelectId);
101865     }else{
101866       zMsg = sqlite3MAppendf(db, zMsg, "%s TABLE %s", zMsg, pItem->zName);
101867     }
101868 
101869     if( pItem->zAlias ){
101870       zMsg = sqlite3MAppendf(db, zMsg, "%s AS %s", zMsg, pItem->zAlias);
101871     }
101872     if( (flags & WHERE_INDEXED)!=0 ){
101873       char *zWhere = explainIndexRange(db, pLevel, pItem->pTab);
101874       zMsg = sqlite3MAppendf(db, zMsg, "%s USING %s%sINDEX%s%s%s", zMsg,
101875           ((flags & WHERE_TEMP_INDEX)?"AUTOMATIC ":""),
101876           ((flags & WHERE_IDX_ONLY)?"COVERING ":""),
101877           ((flags & WHERE_TEMP_INDEX)?"":" "),
101878           ((flags & WHERE_TEMP_INDEX)?"": pLevel->plan.u.pIdx->zName),
101879           zWhere
101880       );
101881       sqlite3DbFree(db, zWhere);
101882     }else if( flags & (WHERE_ROWID_EQ|WHERE_ROWID_RANGE) ){
101883       zMsg = sqlite3MAppendf(db, zMsg, "%s USING INTEGER PRIMARY KEY", zMsg);
101884 
101885       if( flags&WHERE_ROWID_EQ ){
101886         zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid=?)", zMsg);
101887       }else if( (flags&WHERE_BOTH_LIMIT)==WHERE_BOTH_LIMIT ){
101888         zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid>? AND rowid<?)", zMsg);
101889       }else if( flags&WHERE_BTM_LIMIT ){
101890         zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid>?)", zMsg);
101891       }else if( flags&WHERE_TOP_LIMIT ){
101892         zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid<?)", zMsg);
101893       }
101894     }
101895 #ifndef SQLITE_OMIT_VIRTUALTABLE
101896     else if( (flags & WHERE_VIRTUALTABLE)!=0 ){
101897       sqlite3_index_info *pVtabIdx = pLevel->plan.u.pVtabIdx;
101898       zMsg = sqlite3MAppendf(db, zMsg, "%s VIRTUAL TABLE INDEX %d:%s", zMsg,
101899                   pVtabIdx->idxNum, pVtabIdx->idxStr);
101900     }
101901 #endif
101902     if( wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX) ){
101903       testcase( wctrlFlags & WHERE_ORDERBY_MIN );
101904       nRow = 1;
101905     }else{
101906       nRow = (sqlite3_int64)pLevel->plan.nRow;
101907     }
101908     zMsg = sqlite3MAppendf(db, zMsg, "%s (~%lld rows)", zMsg, nRow);
101909     sqlite3VdbeAddOp4(v, OP_Explain, iId, iLevel, iFrom, zMsg, P4_DYNAMIC);
101910   }
101911 }
101912 #else
101913 # define explainOneScan(u,v,w,x,y,z)
101914 #endif /* SQLITE_OMIT_EXPLAIN */
101915 
101916 
101917 /*
101918 ** Generate code for the start of the iLevel-th loop in the WHERE clause
101919 ** implementation described by pWInfo.
101920 */
101921 static Bitmask codeOneLoopStart(
101922   WhereInfo *pWInfo,   /* Complete information about the WHERE clause */
101923   int iLevel,          /* Which level of pWInfo->a[] should be coded */
101924   u16 wctrlFlags,      /* One of the WHERE_* flags defined in sqliteInt.h */
101925   Bitmask notReady     /* Which tables are currently available */
101926 ){
101927   int j, k;            /* Loop counters */
101928   int iCur;            /* The VDBE cursor for the table */
101929   int addrNxt;         /* Where to jump to continue with the next IN case */
101930   int omitTable;       /* True if we use the index only */
101931   int bRev;            /* True if we need to scan in reverse order */
101932   WhereLevel *pLevel;  /* The where level to be coded */
101933   WhereClause *pWC;    /* Decomposition of the entire WHERE clause */
101934   WhereTerm *pTerm;               /* A WHERE clause term */
101935   Parse *pParse;                  /* Parsing context */
101936   Vdbe *v;                        /* The prepared stmt under constructions */
101937   struct SrcList_item *pTabItem;  /* FROM clause term being coded */
101938   int addrBrk;                    /* Jump here to break out of the loop */
101939   int addrCont;                   /* Jump here to continue with next cycle */
101940   int iRowidReg = 0;        /* Rowid is stored in this register, if not zero */
101941   int iReleaseReg = 0;      /* Temp register to free before returning */
101942 
101943   pParse = pWInfo->pParse;
101944   v = pParse->pVdbe;
101945   pWC = pWInfo->pWC;
101946   pLevel = &pWInfo->a[iLevel];
101947   pTabItem = &pWInfo->pTabList->a[pLevel->iFrom];
101948   iCur = pTabItem->iCursor;
101949   bRev = (pLevel->plan.wsFlags & WHERE_REVERSE)!=0;
101950   omitTable = (pLevel->plan.wsFlags & WHERE_IDX_ONLY)!=0
101951            && (wctrlFlags & WHERE_FORCE_TABLE)==0;
101952 
101953   /* Create labels for the "break" and "continue" instructions
101954   ** for the current loop.  Jump to addrBrk to break out of a loop.
101955   ** Jump to cont to go immediately to the next iteration of the
101956   ** loop.
101957   **
101958   ** When there is an IN operator, we also have a "addrNxt" label that
101959   ** means to continue with the next IN value combination.  When
101960   ** there are no IN operators in the constraints, the "addrNxt" label
101961   ** is the same as "addrBrk".
101962   */
101963   addrBrk = pLevel->addrBrk = pLevel->addrNxt = sqlite3VdbeMakeLabel(v);
101964   addrCont = pLevel->addrCont = sqlite3VdbeMakeLabel(v);
101965 
101966   /* If this is the right table of a LEFT OUTER JOIN, allocate and
101967   ** initialize a memory cell that records if this table matches any
101968   ** row of the left table of the join.
101969   */
101970   if( pLevel->iFrom>0 && (pTabItem[0].jointype & JT_LEFT)!=0 ){
101971     pLevel->iLeftJoin = ++pParse->nMem;
101972     sqlite3VdbeAddOp2(v, OP_Integer, 0, pLevel->iLeftJoin);
101973     VdbeComment((v, "init LEFT JOIN no-match flag"));
101974   }
101975 
101976 #ifndef SQLITE_OMIT_VIRTUALTABLE
101977   if(  (pLevel->plan.wsFlags & WHERE_VIRTUALTABLE)!=0 ){
101978     /* Case 0:  The table is a virtual-table.  Use the VFilter and VNext
101979     **          to access the data.
101980     */
101981     int iReg;   /* P3 Value for OP_VFilter */
101982     sqlite3_index_info *pVtabIdx = pLevel->plan.u.pVtabIdx;
101983     int nConstraint = pVtabIdx->nConstraint;
101984     struct sqlite3_index_constraint_usage *aUsage =
101985                                                 pVtabIdx->aConstraintUsage;
101986     const struct sqlite3_index_constraint *aConstraint =
101987                                                 pVtabIdx->aConstraint;
101988 
101989     sqlite3ExprCachePush(pParse);
101990     iReg = sqlite3GetTempRange(pParse, nConstraint+2);
101991     for(j=1; j<=nConstraint; j++){
101992       for(k=0; k<nConstraint; k++){
101993         if( aUsage[k].argvIndex==j ){
101994           int iTerm = aConstraint[k].iTermOffset;
101995           sqlite3ExprCode(pParse, pWC->a[iTerm].pExpr->pRight, iReg+j+1);
101996           break;
101997         }
101998       }
101999       if( k==nConstraint ) break;
102000     }
102001     sqlite3VdbeAddOp2(v, OP_Integer, pVtabIdx->idxNum, iReg);
102002     sqlite3VdbeAddOp2(v, OP_Integer, j-1, iReg+1);
102003     sqlite3VdbeAddOp4(v, OP_VFilter, iCur, addrBrk, iReg, pVtabIdx->idxStr,
102004                       pVtabIdx->needToFreeIdxStr ? P4_MPRINTF : P4_STATIC);
102005     pVtabIdx->needToFreeIdxStr = 0;
102006     for(j=0; j<nConstraint; j++){
102007       if( aUsage[j].omit ){
102008         int iTerm = aConstraint[j].iTermOffset;
102009         disableTerm(pLevel, &pWC->a[iTerm]);
102010       }
102011     }
102012     pLevel->op = OP_VNext;
102013     pLevel->p1 = iCur;
102014     pLevel->p2 = sqlite3VdbeCurrentAddr(v);
102015     sqlite3ReleaseTempRange(pParse, iReg, nConstraint+2);
102016     sqlite3ExprCachePop(pParse, 1);
102017   }else
102018 #endif /* SQLITE_OMIT_VIRTUALTABLE */
102019 
102020   if( pLevel->plan.wsFlags & WHERE_ROWID_EQ ){
102021     /* Case 1:  We can directly reference a single row using an
102022     **          equality comparison against the ROWID field.  Or
102023     **          we reference multiple rows using a "rowid IN (...)"
102024     **          construct.
102025     */
102026     iReleaseReg = sqlite3GetTempReg(pParse);
102027     pTerm = findTerm(pWC, iCur, -1, notReady, WO_EQ|WO_IN, 0);
102028     assert( pTerm!=0 );
102029     assert( pTerm->pExpr!=0 );
102030     assert( pTerm->leftCursor==iCur );
102031     assert( omitTable==0 );
102032     testcase( pTerm->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */
102033     iRowidReg = codeEqualityTerm(pParse, pTerm, pLevel, iReleaseReg);
102034     addrNxt = pLevel->addrNxt;
102035     sqlite3VdbeAddOp2(v, OP_MustBeInt, iRowidReg, addrNxt);
102036     sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addrNxt, iRowidReg);
102037     sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
102038     VdbeComment((v, "pk"));
102039     pLevel->op = OP_Noop;
102040   }else if( pLevel->plan.wsFlags & WHERE_ROWID_RANGE ){
102041     /* Case 2:  We have an inequality comparison against the ROWID field.
102042     */
102043     int testOp = OP_Noop;
102044     int start;
102045     int memEndValue = 0;
102046     WhereTerm *pStart, *pEnd;
102047 
102048     assert( omitTable==0 );
102049     pStart = findTerm(pWC, iCur, -1, notReady, WO_GT|WO_GE, 0);
102050     pEnd = findTerm(pWC, iCur, -1, notReady, WO_LT|WO_LE, 0);
102051     if( bRev ){
102052       pTerm = pStart;
102053       pStart = pEnd;
102054       pEnd = pTerm;
102055     }
102056     if( pStart ){
102057       Expr *pX;             /* The expression that defines the start bound */
102058       int r1, rTemp;        /* Registers for holding the start boundary */
102059 
102060       /* The following constant maps TK_xx codes into corresponding
102061       ** seek opcodes.  It depends on a particular ordering of TK_xx
102062       */
102063       const u8 aMoveOp[] = {
102064            /* TK_GT */  OP_SeekGt,
102065            /* TK_LE */  OP_SeekLe,
102066            /* TK_LT */  OP_SeekLt,
102067            /* TK_GE */  OP_SeekGe
102068       };
102069       assert( TK_LE==TK_GT+1 );      /* Make sure the ordering.. */
102070       assert( TK_LT==TK_GT+2 );      /*  ... of the TK_xx values... */
102071       assert( TK_GE==TK_GT+3 );      /*  ... is correcct. */
102072 
102073       testcase( pStart->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */
102074       pX = pStart->pExpr;
102075       assert( pX!=0 );
102076       assert( pStart->leftCursor==iCur );
102077       r1 = sqlite3ExprCodeTemp(pParse, pX->pRight, &rTemp);
102078       sqlite3VdbeAddOp3(v, aMoveOp[pX->op-TK_GT], iCur, addrBrk, r1);
102079       VdbeComment((v, "pk"));
102080       sqlite3ExprCacheAffinityChange(pParse, r1, 1);
102081       sqlite3ReleaseTempReg(pParse, rTemp);
102082       disableTerm(pLevel, pStart);
102083     }else{
102084       sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iCur, addrBrk);
102085     }
102086     if( pEnd ){
102087       Expr *pX;
102088       pX = pEnd->pExpr;
102089       assert( pX!=0 );
102090       assert( pEnd->leftCursor==iCur );
102091       testcase( pEnd->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */
102092       memEndValue = ++pParse->nMem;
102093       sqlite3ExprCode(pParse, pX->pRight, memEndValue);
102094       if( pX->op==TK_LT || pX->op==TK_GT ){
102095         testOp = bRev ? OP_Le : OP_Ge;
102096       }else{
102097         testOp = bRev ? OP_Lt : OP_Gt;
102098       }
102099       disableTerm(pLevel, pEnd);
102100     }
102101     start = sqlite3VdbeCurrentAddr(v);
102102     pLevel->op = bRev ? OP_Prev : OP_Next;
102103     pLevel->p1 = iCur;
102104     pLevel->p2 = start;
102105     if( pStart==0 && pEnd==0 ){
102106       pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
102107     }else{
102108       assert( pLevel->p5==0 );
102109     }
102110     if( testOp!=OP_Noop ){
102111       iRowidReg = iReleaseReg = sqlite3GetTempReg(pParse);
102112       sqlite3VdbeAddOp2(v, OP_Rowid, iCur, iRowidReg);
102113       sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
102114       sqlite3VdbeAddOp3(v, testOp, memEndValue, addrBrk, iRowidReg);
102115       sqlite3VdbeChangeP5(v, SQLITE_AFF_NUMERIC | SQLITE_JUMPIFNULL);
102116     }
102117   }else if( pLevel->plan.wsFlags & (WHERE_COLUMN_RANGE|WHERE_COLUMN_EQ) ){
102118     /* Case 3: A scan using an index.
102119     **
102120     **         The WHERE clause may contain zero or more equality
102121     **         terms ("==" or "IN" operators) that refer to the N
102122     **         left-most columns of the index. It may also contain
102123     **         inequality constraints (>, <, >= or <=) on the indexed
102124     **         column that immediately follows the N equalities. Only
102125     **         the right-most column can be an inequality - the rest must
102126     **         use the "==" and "IN" operators. For example, if the
102127     **         index is on (x,y,z), then the following clauses are all
102128     **         optimized:
102129     **
102130     **            x=5
102131     **            x=5 AND y=10
102132     **            x=5 AND y<10
102133     **            x=5 AND y>5 AND y<10
102134     **            x=5 AND y=5 AND z<=10
102135     **
102136     **         The z<10 term of the following cannot be used, only
102137     **         the x=5 term:
102138     **
102139     **            x=5 AND z<10
102140     **
102141     **         N may be zero if there are inequality constraints.
102142     **         If there are no inequality constraints, then N is at
102143     **         least one.
102144     **
102145     **         This case is also used when there are no WHERE clause
102146     **         constraints but an index is selected anyway, in order
102147     **         to force the output order to conform to an ORDER BY.
102148     */
102149     static const u8 aStartOp[] = {
102150       0,
102151       0,
102152       OP_Rewind,           /* 2: (!start_constraints && startEq &&  !bRev) */
102153       OP_Last,             /* 3: (!start_constraints && startEq &&   bRev) */
102154       OP_SeekGt,           /* 4: (start_constraints  && !startEq && !bRev) */
102155       OP_SeekLt,           /* 5: (start_constraints  && !startEq &&  bRev) */
102156       OP_SeekGe,           /* 6: (start_constraints  &&  startEq && !bRev) */
102157       OP_SeekLe            /* 7: (start_constraints  &&  startEq &&  bRev) */
102158     };
102159     static const u8 aEndOp[] = {
102160       OP_Noop,             /* 0: (!end_constraints) */
102161       OP_IdxGE,            /* 1: (end_constraints && !bRev) */
102162       OP_IdxLT             /* 2: (end_constraints && bRev) */
102163     };
102164     int nEq = pLevel->plan.nEq;  /* Number of == or IN terms */
102165     int isMinQuery = 0;          /* If this is an optimized SELECT min(x).. */
102166     int regBase;                 /* Base register holding constraint values */
102167     int r1;                      /* Temp register */
102168     WhereTerm *pRangeStart = 0;  /* Inequality constraint at range start */
102169     WhereTerm *pRangeEnd = 0;    /* Inequality constraint at range end */
102170     int startEq;                 /* True if range start uses ==, >= or <= */
102171     int endEq;                   /* True if range end uses ==, >= or <= */
102172     int start_constraints;       /* Start of range is constrained */
102173     int nConstraint;             /* Number of constraint terms */
102174     Index *pIdx;                 /* The index we will be using */
102175     int iIdxCur;                 /* The VDBE cursor for the index */
102176     int nExtraReg = 0;           /* Number of extra registers needed */
102177     int op;                      /* Instruction opcode */
102178     char *zStartAff;             /* Affinity for start of range constraint */
102179     char *zEndAff;               /* Affinity for end of range constraint */
102180 
102181     pIdx = pLevel->plan.u.pIdx;
102182     iIdxCur = pLevel->iIdxCur;
102183     k = pIdx->aiColumn[nEq];     /* Column for inequality constraints */
102184 
102185     /* If this loop satisfies a sort order (pOrderBy) request that
102186     ** was passed to this function to implement a "SELECT min(x) ..."
102187     ** query, then the caller will only allow the loop to run for
102188     ** a single iteration. This means that the first row returned
102189     ** should not have a NULL value stored in 'x'. If column 'x' is
102190     ** the first one after the nEq equality constraints in the index,
102191     ** this requires some special handling.
102192     */
102193     if( (wctrlFlags&WHERE_ORDERBY_MIN)!=0
102194      && (pLevel->plan.wsFlags&WHERE_ORDERBY)
102195      && (pIdx->nColumn>nEq)
102196     ){
102197       /* assert( pOrderBy->nExpr==1 ); */
102198       /* assert( pOrderBy->a[0].pExpr->iColumn==pIdx->aiColumn[nEq] ); */
102199       isMinQuery = 1;
102200       nExtraReg = 1;
102201     }
102202 
102203     /* Find any inequality constraint terms for the start and end
102204     ** of the range.
102205     */
102206     if( pLevel->plan.wsFlags & WHERE_TOP_LIMIT ){
102207       pRangeEnd = findTerm(pWC, iCur, k, notReady, (WO_LT|WO_LE), pIdx);
102208       nExtraReg = 1;
102209     }
102210     if( pLevel->plan.wsFlags & WHERE_BTM_LIMIT ){
102211       pRangeStart = findTerm(pWC, iCur, k, notReady, (WO_GT|WO_GE), pIdx);
102212       nExtraReg = 1;
102213     }
102214 
102215     /* Generate code to evaluate all constraint terms using == or IN
102216     ** and store the values of those terms in an array of registers
102217     ** starting at regBase.
102218     */
102219     regBase = codeAllEqualityTerms(
102220         pParse, pLevel, pWC, notReady, nExtraReg, &zStartAff
102221     );
102222     zEndAff = sqlite3DbStrDup(pParse->db, zStartAff);
102223     addrNxt = pLevel->addrNxt;
102224 
102225     /* If we are doing a reverse order scan on an ascending index, or
102226     ** a forward order scan on a descending index, interchange the
102227     ** start and end terms (pRangeStart and pRangeEnd).
102228     */
102229     if( nEq<pIdx->nColumn && bRev==(pIdx->aSortOrder[nEq]==SQLITE_SO_ASC) ){
102230       SWAP(WhereTerm *, pRangeEnd, pRangeStart);
102231     }
102232 
102233     testcase( pRangeStart && pRangeStart->eOperator & WO_LE );
102234     testcase( pRangeStart && pRangeStart->eOperator & WO_GE );
102235     testcase( pRangeEnd && pRangeEnd->eOperator & WO_LE );
102236     testcase( pRangeEnd && pRangeEnd->eOperator & WO_GE );
102237     startEq = !pRangeStart || pRangeStart->eOperator & (WO_LE|WO_GE);
102238     endEq =   !pRangeEnd || pRangeEnd->eOperator & (WO_LE|WO_GE);
102239     start_constraints = pRangeStart || nEq>0;
102240 
102241     /* Seek the index cursor to the start of the range. */
102242     nConstraint = nEq;
102243     if( pRangeStart ){
102244       Expr *pRight = pRangeStart->pExpr->pRight;
102245       sqlite3ExprCode(pParse, pRight, regBase+nEq);
102246       if( (pRangeStart->wtFlags & TERM_VNULL)==0 ){
102247         sqlite3ExprCodeIsNullJump(v, pRight, regBase+nEq, addrNxt);
102248       }
102249       if( zStartAff ){
102250         if( sqlite3CompareAffinity(pRight, zStartAff[nEq])==SQLITE_AFF_NONE){
102251           /* Since the comparison is to be performed with no conversions
102252           ** applied to the operands, set the affinity to apply to pRight to
102253           ** SQLITE_AFF_NONE.  */
102254           zStartAff[nEq] = SQLITE_AFF_NONE;
102255         }
102256         if( sqlite3ExprNeedsNoAffinityChange(pRight, zStartAff[nEq]) ){
102257           zStartAff[nEq] = SQLITE_AFF_NONE;
102258         }
102259       }
102260       nConstraint++;
102261       testcase( pRangeStart->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */
102262     }else if( isMinQuery ){
102263       sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq);
102264       nConstraint++;
102265       startEq = 0;
102266       start_constraints = 1;
102267     }
102268     codeApplyAffinity(pParse, regBase, nConstraint, zStartAff);
102269     op = aStartOp[(start_constraints<<2) + (startEq<<1) + bRev];
102270     assert( op!=0 );
102271     testcase( op==OP_Rewind );
102272     testcase( op==OP_Last );
102273     testcase( op==OP_SeekGt );
102274     testcase( op==OP_SeekGe );
102275     testcase( op==OP_SeekLe );
102276     testcase( op==OP_SeekLt );
102277     sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint);
102278 
102279     /* Load the value for the inequality constraint at the end of the
102280     ** range (if any).
102281     */
102282     nConstraint = nEq;
102283     if( pRangeEnd ){
102284       Expr *pRight = pRangeEnd->pExpr->pRight;
102285       sqlite3ExprCacheRemove(pParse, regBase+nEq, 1);
102286       sqlite3ExprCode(pParse, pRight, regBase+nEq);
102287       if( (pRangeEnd->wtFlags & TERM_VNULL)==0 ){
102288         sqlite3ExprCodeIsNullJump(v, pRight, regBase+nEq, addrNxt);
102289       }
102290       if( zEndAff ){
102291         if( sqlite3CompareAffinity(pRight, zEndAff[nEq])==SQLITE_AFF_NONE){
102292           /* Since the comparison is to be performed with no conversions
102293           ** applied to the operands, set the affinity to apply to pRight to
102294           ** SQLITE_AFF_NONE.  */
102295           zEndAff[nEq] = SQLITE_AFF_NONE;
102296         }
102297         if( sqlite3ExprNeedsNoAffinityChange(pRight, zEndAff[nEq]) ){
102298           zEndAff[nEq] = SQLITE_AFF_NONE;
102299         }
102300       }
102301       codeApplyAffinity(pParse, regBase, nEq+1, zEndAff);
102302       nConstraint++;
102303       testcase( pRangeEnd->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */
102304     }
102305     sqlite3DbFree(pParse->db, zStartAff);
102306     sqlite3DbFree(pParse->db, zEndAff);
102307 
102308     /* Top of the loop body */
102309     pLevel->p2 = sqlite3VdbeCurrentAddr(v);
102310 
102311     /* Check if the index cursor is past the end of the range. */
102312     op = aEndOp[(pRangeEnd || nEq) * (1 + bRev)];
102313     testcase( op==OP_Noop );
102314     testcase( op==OP_IdxGE );
102315     testcase( op==OP_IdxLT );
102316     if( op!=OP_Noop ){
102317       sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint);
102318       sqlite3VdbeChangeP5(v, endEq!=bRev ?1:0);
102319     }
102320 
102321     /* If there are inequality constraints, check that the value
102322     ** of the table column that the inequality contrains is not NULL.
102323     ** If it is, jump to the next iteration of the loop.
102324     */
102325     r1 = sqlite3GetTempReg(pParse);
102326     testcase( pLevel->plan.wsFlags & WHERE_BTM_LIMIT );
102327     testcase( pLevel->plan.wsFlags & WHERE_TOP_LIMIT );
102328     if( (pLevel->plan.wsFlags & (WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0 ){
102329       sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, nEq, r1);
102330       sqlite3VdbeAddOp2(v, OP_IsNull, r1, addrCont);
102331     }
102332     sqlite3ReleaseTempReg(pParse, r1);
102333 
102334     /* Seek the table cursor, if required */
102335     disableTerm(pLevel, pRangeStart);
102336     disableTerm(pLevel, pRangeEnd);
102337     if( !omitTable ){
102338       iRowidReg = iReleaseReg = sqlite3GetTempReg(pParse);
102339       sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, iRowidReg);
102340       sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
102341       sqlite3VdbeAddOp2(v, OP_Seek, iCur, iRowidReg);  /* Deferred seek */
102342     }
102343 
102344     /* Record the instruction used to terminate the loop. Disable
102345     ** WHERE clause terms made redundant by the index range scan.
102346     */
102347     if( pLevel->plan.wsFlags & WHERE_UNIQUE ){
102348       pLevel->op = OP_Noop;
102349     }else if( bRev ){
102350       pLevel->op = OP_Prev;
102351     }else{
102352       pLevel->op = OP_Next;
102353     }
102354     pLevel->p1 = iIdxCur;
102355   }else
102356 
102357 #ifndef SQLITE_OMIT_OR_OPTIMIZATION
102358   if( pLevel->plan.wsFlags & WHERE_MULTI_OR ){
102359     /* Case 4:  Two or more separately indexed terms connected by OR
102360     **
102361     ** Example:
102362     **
102363     **   CREATE TABLE t1(a,b,c,d);
102364     **   CREATE INDEX i1 ON t1(a);
102365     **   CREATE INDEX i2 ON t1(b);
102366     **   CREATE INDEX i3 ON t1(c);
102367     **
102368     **   SELECT * FROM t1 WHERE a=5 OR b=7 OR (c=11 AND d=13)
102369     **
102370     ** In the example, there are three indexed terms connected by OR.
102371     ** The top of the loop looks like this:
102372     **
102373     **          Null       1                # Zero the rowset in reg 1
102374     **
102375     ** Then, for each indexed term, the following. The arguments to
102376     ** RowSetTest are such that the rowid of the current row is inserted
102377     ** into the RowSet. If it is already present, control skips the
102378     ** Gosub opcode and jumps straight to the code generated by WhereEnd().
102379     **
102380     **        sqlite3WhereBegin(<term>)
102381     **          RowSetTest                  # Insert rowid into rowset
102382     **          Gosub      2 A
102383     **        sqlite3WhereEnd()
102384     **
102385     ** Following the above, code to terminate the loop. Label A, the target
102386     ** of the Gosub above, jumps to the instruction right after the Goto.
102387     **
102388     **          Null       1                # Zero the rowset in reg 1
102389     **          Goto       B                # The loop is finished.
102390     **
102391     **       A: <loop body>                 # Return data, whatever.
102392     **
102393     **          Return     2                # Jump back to the Gosub
102394     **
102395     **       B: <after the loop>
102396     **
102397     */
102398     WhereClause *pOrWc;    /* The OR-clause broken out into subterms */
102399     SrcList *pOrTab;       /* Shortened table list or OR-clause generation */
102400 
102401     int regReturn = ++pParse->nMem;           /* Register used with OP_Gosub */
102402     int regRowset = 0;                        /* Register for RowSet object */
102403     int regRowid = 0;                         /* Register holding rowid */
102404     int iLoopBody = sqlite3VdbeMakeLabel(v);  /* Start of loop body */
102405     int iRetInit;                             /* Address of regReturn init */
102406     int untestedTerms = 0;             /* Some terms not completely tested */
102407     int ii;
102408 
102409     pTerm = pLevel->plan.u.pTerm;
102410     assert( pTerm!=0 );
102411     assert( pTerm->eOperator==WO_OR );
102412     assert( (pTerm->wtFlags & TERM_ORINFO)!=0 );
102413     pOrWc = &pTerm->u.pOrInfo->wc;
102414     pLevel->op = OP_Return;
102415     pLevel->p1 = regReturn;
102416 
102417     /* Set up a new SrcList ni pOrTab containing the table being scanned
102418     ** by this loop in the a[0] slot and all notReady tables in a[1..] slots.
102419     ** This becomes the SrcList in the recursive call to sqlite3WhereBegin().
102420     */
102421     if( pWInfo->nLevel>1 ){
102422       int nNotReady;                 /* The number of notReady tables */
102423       struct SrcList_item *origSrc;     /* Original list of tables */
102424       nNotReady = pWInfo->nLevel - iLevel - 1;
102425       pOrTab = sqlite3StackAllocRaw(pParse->db,
102426                             sizeof(*pOrTab)+ nNotReady*sizeof(pOrTab->a[0]));
102427       if( pOrTab==0 ) return notReady;
102428       pOrTab->nAlloc = (i16)(nNotReady + 1);
102429       pOrTab->nSrc = pOrTab->nAlloc;
102430       memcpy(pOrTab->a, pTabItem, sizeof(*pTabItem));
102431       origSrc = pWInfo->pTabList->a;
102432       for(k=1; k<=nNotReady; k++){
102433         memcpy(&pOrTab->a[k], &origSrc[pLevel[k].iFrom], sizeof(pOrTab->a[k]));
102434       }
102435     }else{
102436       pOrTab = pWInfo->pTabList;
102437     }
102438 
102439     /* Initialize the rowset register to contain NULL. An SQL NULL is
102440     ** equivalent to an empty rowset.
102441     **
102442     ** Also initialize regReturn to contain the address of the instruction
102443     ** immediately following the OP_Return at the bottom of the loop. This
102444     ** is required in a few obscure LEFT JOIN cases where control jumps
102445     ** over the top of the loop into the body of it. In this case the
102446     ** correct response for the end-of-loop code (the OP_Return) is to
102447     ** fall through to the next instruction, just as an OP_Next does if
102448     ** called on an uninitialized cursor.
102449     */
102450     if( (wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
102451       regRowset = ++pParse->nMem;
102452       regRowid = ++pParse->nMem;
102453       sqlite3VdbeAddOp2(v, OP_Null, 0, regRowset);
102454     }
102455     iRetInit = sqlite3VdbeAddOp2(v, OP_Integer, 0, regReturn);
102456 
102457     for(ii=0; ii<pOrWc->nTerm; ii++){
102458       WhereTerm *pOrTerm = &pOrWc->a[ii];
102459       if( pOrTerm->leftCursor==iCur || pOrTerm->eOperator==WO_AND ){
102460         WhereInfo *pSubWInfo;          /* Info for single OR-term scan */
102461         /* Loop through table entries that match term pOrTerm. */
102462         pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrTerm->pExpr, 0,
102463                         WHERE_OMIT_OPEN | WHERE_OMIT_CLOSE |
102464                         WHERE_FORCE_TABLE | WHERE_ONETABLE_ONLY);
102465         if( pSubWInfo ){
102466           explainOneScan(
102467               pParse, pOrTab, &pSubWInfo->a[0], iLevel, pLevel->iFrom, 0
102468           );
102469           if( (wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
102470             int iSet = ((ii==pOrWc->nTerm-1)?-1:ii);
102471             int r;
102472             r = sqlite3ExprCodeGetColumn(pParse, pTabItem->pTab, -1, iCur,
102473                                          regRowid);
102474             sqlite3VdbeAddOp4Int(v, OP_RowSetTest, regRowset,
102475                                  sqlite3VdbeCurrentAddr(v)+2, r, iSet);
102476           }
102477           sqlite3VdbeAddOp2(v, OP_Gosub, regReturn, iLoopBody);
102478 
102479           /* The pSubWInfo->untestedTerms flag means that this OR term
102480           ** contained one or more AND term from a notReady table.  The
102481           ** terms from the notReady table could not be tested and will
102482           ** need to be tested later.
102483           */
102484           if( pSubWInfo->untestedTerms ) untestedTerms = 1;
102485 
102486           /* Finish the loop through table entries that match term pOrTerm. */
102487           sqlite3WhereEnd(pSubWInfo);
102488         }
102489       }
102490     }
102491     sqlite3VdbeChangeP1(v, iRetInit, sqlite3VdbeCurrentAddr(v));
102492     sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrBrk);
102493     sqlite3VdbeResolveLabel(v, iLoopBody);
102494 
102495     if( pWInfo->nLevel>1 ) sqlite3StackFree(pParse->db, pOrTab);
102496     if( !untestedTerms ) disableTerm(pLevel, pTerm);
102497   }else
102498 #endif /* SQLITE_OMIT_OR_OPTIMIZATION */
102499 
102500   {
102501     /* Case 5:  There is no usable index.  We must do a complete
102502     **          scan of the entire table.
102503     */
102504     static const u8 aStep[] = { OP_Next, OP_Prev };
102505     static const u8 aStart[] = { OP_Rewind, OP_Last };
102506     assert( bRev==0 || bRev==1 );
102507     assert( omitTable==0 );
102508     pLevel->op = aStep[bRev];
102509     pLevel->p1 = iCur;
102510     pLevel->p2 = 1 + sqlite3VdbeAddOp2(v, aStart[bRev], iCur, addrBrk);
102511     pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
102512   }
102513   notReady &= ~getMask(pWC->pMaskSet, iCur);
102514 
102515   /* Insert code to test every subexpression that can be completely
102516   ** computed using the current set of tables.
102517   **
102518   ** IMPLEMENTATION-OF: R-49525-50935 Terms that cannot be satisfied through
102519   ** the use of indices become tests that are evaluated against each row of
102520   ** the relevant input tables.
102521   */
102522   for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){
102523     Expr *pE;
102524     testcase( pTerm->wtFlags & TERM_VIRTUAL ); /* IMP: R-30575-11662 */
102525     testcase( pTerm->wtFlags & TERM_CODED );
102526     if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
102527     if( (pTerm->prereqAll & notReady)!=0 ){
102528       testcase( pWInfo->untestedTerms==0
102529                && (pWInfo->wctrlFlags & WHERE_ONETABLE_ONLY)!=0 );
102530       pWInfo->untestedTerms = 1;
102531       continue;
102532     }
102533     pE = pTerm->pExpr;
102534     assert( pE!=0 );
102535     if( pLevel->iLeftJoin && !ExprHasProperty(pE, EP_FromJoin) ){
102536       continue;
102537     }
102538     sqlite3ExprIfFalse(pParse, pE, addrCont, SQLITE_JUMPIFNULL);
102539     pTerm->wtFlags |= TERM_CODED;
102540   }
102541 
102542   /* For a LEFT OUTER JOIN, generate code that will record the fact that
102543   ** at least one row of the right table has matched the left table.
102544   */
102545   if( pLevel->iLeftJoin ){
102546     pLevel->addrFirst = sqlite3VdbeCurrentAddr(v);
102547     sqlite3VdbeAddOp2(v, OP_Integer, 1, pLevel->iLeftJoin);
102548     VdbeComment((v, "record LEFT JOIN hit"));
102549     sqlite3ExprCacheClear(pParse);
102550     for(pTerm=pWC->a, j=0; j<pWC->nTerm; j++, pTerm++){
102551       testcase( pTerm->wtFlags & TERM_VIRTUAL );  /* IMP: R-30575-11662 */
102552       testcase( pTerm->wtFlags & TERM_CODED );
102553       if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
102554       if( (pTerm->prereqAll & notReady)!=0 ){
102555         assert( pWInfo->untestedTerms );
102556         continue;
102557       }
102558       assert( pTerm->pExpr );
102559       sqlite3ExprIfFalse(pParse, pTerm->pExpr, addrCont, SQLITE_JUMPIFNULL);
102560       pTerm->wtFlags |= TERM_CODED;
102561     }
102562   }
102563   sqlite3ReleaseTempReg(pParse, iReleaseReg);
102564 
102565   return notReady;
102566 }
102567 
102568 #if defined(SQLITE_TEST)
102569 /*
102570 ** The following variable holds a text description of query plan generated
102571 ** by the most recent call to sqlite3WhereBegin().  Each call to WhereBegin
102572 ** overwrites the previous.  This information is used for testing and
102573 ** analysis only.
102574 */
102575 SQLITE_API char sqlite3_query_plan[BMS*2*40];  /* Text of the join */
102576 static int nQPlan = 0;              /* Next free slow in _query_plan[] */
102577 
102578 #endif /* SQLITE_TEST */
102579 
102580 
102581 /*
102582 ** Free a WhereInfo structure
102583 */
102584 static void whereInfoFree(sqlite3 *db, WhereInfo *pWInfo){
102585   if( ALWAYS(pWInfo) ){
102586     int i;
102587     for(i=0; i<pWInfo->nLevel; i++){
102588       sqlite3_index_info *pInfo = pWInfo->a[i].pIdxInfo;
102589       if( pInfo ){
102590         /* assert( pInfo->needToFreeIdxStr==0 || db->mallocFailed ); */
102591         if( pInfo->needToFreeIdxStr ){
102592           sqlite3_free(pInfo->idxStr);
102593         }
102594         sqlite3DbFree(db, pInfo);
102595       }
102596       if( pWInfo->a[i].plan.wsFlags & WHERE_TEMP_INDEX ){
102597         Index *pIdx = pWInfo->a[i].plan.u.pIdx;
102598         if( pIdx ){
102599           sqlite3DbFree(db, pIdx->zColAff);
102600           sqlite3DbFree(db, pIdx);
102601         }
102602       }
102603     }
102604     whereClauseClear(pWInfo->pWC);
102605     sqlite3DbFree(db, pWInfo);
102606   }
102607 }
102608 
102609 
102610 /*
102611 ** Generate the beginning of the loop used for WHERE clause processing.
102612 ** The return value is a pointer to an opaque structure that contains
102613 ** information needed to terminate the loop.  Later, the calling routine
102614 ** should invoke sqlite3WhereEnd() with the return value of this function
102615 ** in order to complete the WHERE clause processing.
102616 **
102617 ** If an error occurs, this routine returns NULL.
102618 **
102619 ** The basic idea is to do a nested loop, one loop for each table in
102620 ** the FROM clause of a select.  (INSERT and UPDATE statements are the
102621 ** same as a SELECT with only a single table in the FROM clause.)  For
102622 ** example, if the SQL is this:
102623 **
102624 **       SELECT * FROM t1, t2, t3 WHERE ...;
102625 **
102626 ** Then the code generated is conceptually like the following:
102627 **
102628 **      foreach row1 in t1 do       \    Code generated
102629 **        foreach row2 in t2 do      |-- by sqlite3WhereBegin()
102630 **          foreach row3 in t3 do   /
102631 **            ...
102632 **          end                     \    Code generated
102633 **        end                        |-- by sqlite3WhereEnd()
102634 **      end                         /
102635 **
102636 ** Note that the loops might not be nested in the order in which they
102637 ** appear in the FROM clause if a different order is better able to make
102638 ** use of indices.  Note also that when the IN operator appears in
102639 ** the WHERE clause, it might result in additional nested loops for
102640 ** scanning through all values on the right-hand side of the IN.
102641 **
102642 ** There are Btree cursors associated with each table.  t1 uses cursor
102643 ** number pTabList->a[0].iCursor.  t2 uses the cursor pTabList->a[1].iCursor.
102644 ** And so forth.  This routine generates code to open those VDBE cursors
102645 ** and sqlite3WhereEnd() generates the code to close them.
102646 **
102647 ** The code that sqlite3WhereBegin() generates leaves the cursors named
102648 ** in pTabList pointing at their appropriate entries.  The [...] code
102649 ** can use OP_Column and OP_Rowid opcodes on these cursors to extract
102650 ** data from the various tables of the loop.
102651 **
102652 ** If the WHERE clause is empty, the foreach loops must each scan their
102653 ** entire tables.  Thus a three-way join is an O(N^3) operation.  But if
102654 ** the tables have indices and there are terms in the WHERE clause that
102655 ** refer to those indices, a complete table scan can be avoided and the
102656 ** code will run much faster.  Most of the work of this routine is checking
102657 ** to see if there are indices that can be used to speed up the loop.
102658 **
102659 ** Terms of the WHERE clause are also used to limit which rows actually
102660 ** make it to the "..." in the middle of the loop.  After each "foreach",
102661 ** terms of the WHERE clause that use only terms in that loop and outer
102662 ** loops are evaluated and if false a jump is made around all subsequent
102663 ** inner loops (or around the "..." if the test occurs within the inner-
102664 ** most loop)
102665 **
102666 ** OUTER JOINS
102667 **
102668 ** An outer join of tables t1 and t2 is conceptally coded as follows:
102669 **
102670 **    foreach row1 in t1 do
102671 **      flag = 0
102672 **      foreach row2 in t2 do
102673 **        start:
102674 **          ...
102675 **          flag = 1
102676 **      end
102677 **      if flag==0 then
102678 **        move the row2 cursor to a null row
102679 **        goto start
102680 **      fi
102681 **    end
102682 **
102683 ** ORDER BY CLAUSE PROCESSING
102684 **
102685 ** *ppOrderBy is a pointer to the ORDER BY clause of a SELECT statement,
102686 ** if there is one.  If there is no ORDER BY clause or if this routine
102687 ** is called from an UPDATE or DELETE statement, then ppOrderBy is NULL.
102688 **
102689 ** If an index can be used so that the natural output order of the table
102690 ** scan is correct for the ORDER BY clause, then that index is used and
102691 ** *ppOrderBy is set to NULL.  This is an optimization that prevents an
102692 ** unnecessary sort of the result set if an index appropriate for the
102693 ** ORDER BY clause already exists.
102694 **
102695 ** If the where clause loops cannot be arranged to provide the correct
102696 ** output order, then the *ppOrderBy is unchanged.
102697 */
102698 SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(
102699   Parse *pParse,        /* The parser context */
102700   SrcList *pTabList,    /* A list of all tables to be scanned */
102701   Expr *pWhere,         /* The WHERE clause */
102702   ExprList **ppOrderBy, /* An ORDER BY clause, or NULL */
102703   u16 wctrlFlags        /* One of the WHERE_* flags defined in sqliteInt.h */
102704 ){
102705   int i;                     /* Loop counter */
102706   int nByteWInfo;            /* Num. bytes allocated for WhereInfo struct */
102707   int nTabList;              /* Number of elements in pTabList */
102708   WhereInfo *pWInfo;         /* Will become the return value of this function */
102709   Vdbe *v = pParse->pVdbe;   /* The virtual database engine */
102710   Bitmask notReady;          /* Cursors that are not yet positioned */
102711   WhereMaskSet *pMaskSet;    /* The expression mask set */
102712   WhereClause *pWC;               /* Decomposition of the WHERE clause */
102713   struct SrcList_item *pTabItem;  /* A single entry from pTabList */
102714   WhereLevel *pLevel;             /* A single level in the pWInfo list */
102715   int iFrom;                      /* First unused FROM clause element */
102716   int andFlags;              /* AND-ed combination of all pWC->a[].wtFlags */
102717   sqlite3 *db;               /* Database connection */
102718 
102719   /* The number of tables in the FROM clause is limited by the number of
102720   ** bits in a Bitmask
102721   */
102722   testcase( pTabList->nSrc==BMS );
102723   if( pTabList->nSrc>BMS ){
102724     sqlite3ErrorMsg(pParse, "at most %d tables in a join", BMS);
102725     return 0;
102726   }
102727 
102728   /* This function normally generates a nested loop for all tables in
102729   ** pTabList.  But if the WHERE_ONETABLE_ONLY flag is set, then we should
102730   ** only generate code for the first table in pTabList and assume that
102731   ** any cursors associated with subsequent tables are uninitialized.
102732   */
102733   nTabList = (wctrlFlags & WHERE_ONETABLE_ONLY) ? 1 : pTabList->nSrc;
102734 
102735   /* Allocate and initialize the WhereInfo structure that will become the
102736   ** return value. A single allocation is used to store the WhereInfo
102737   ** struct, the contents of WhereInfo.a[], the WhereClause structure
102738   ** and the WhereMaskSet structure. Since WhereClause contains an 8-byte
102739   ** field (type Bitmask) it must be aligned on an 8-byte boundary on
102740   ** some architectures. Hence the ROUND8() below.
102741   */
102742   db = pParse->db;
102743   nByteWInfo = ROUND8(sizeof(WhereInfo)+(nTabList-1)*sizeof(WhereLevel));
102744   pWInfo = sqlite3DbMallocZero(db,
102745       nByteWInfo +
102746       sizeof(WhereClause) +
102747       sizeof(WhereMaskSet)
102748   );
102749   if( db->mallocFailed ){
102750     sqlite3DbFree(db, pWInfo);
102751     pWInfo = 0;
102752     goto whereBeginError;
102753   }
102754   pWInfo->nLevel = nTabList;
102755   pWInfo->pParse = pParse;
102756   pWInfo->pTabList = pTabList;
102757   pWInfo->iBreak = sqlite3VdbeMakeLabel(v);
102758   pWInfo->pWC = pWC = (WhereClause *)&((u8 *)pWInfo)[nByteWInfo];
102759   pWInfo->wctrlFlags = wctrlFlags;
102760   pWInfo->savedNQueryLoop = pParse->nQueryLoop;
102761   pMaskSet = (WhereMaskSet*)&pWC[1];
102762 
102763   /* Split the WHERE clause into separate subexpressions where each
102764   ** subexpression is separated by an AND operator.
102765   */
102766   initMaskSet(pMaskSet);
102767   whereClauseInit(pWC, pParse, pMaskSet);
102768   sqlite3ExprCodeConstants(pParse, pWhere);
102769   whereSplit(pWC, pWhere, TK_AND);   /* IMP: R-15842-53296 */
102770 
102771   /* Special case: a WHERE clause that is constant.  Evaluate the
102772   ** expression and either jump over all of the code or fall thru.
102773   */
102774   if( pWhere && (nTabList==0 || sqlite3ExprIsConstantNotJoin(pWhere)) ){
102775     sqlite3ExprIfFalse(pParse, pWhere, pWInfo->iBreak, SQLITE_JUMPIFNULL);
102776     pWhere = 0;
102777   }
102778 
102779   /* Assign a bit from the bitmask to every term in the FROM clause.
102780   **
102781   ** When assigning bitmask values to FROM clause cursors, it must be
102782   ** the case that if X is the bitmask for the N-th FROM clause term then
102783   ** the bitmask for all FROM clause terms to the left of the N-th term
102784   ** is (X-1).   An expression from the ON clause of a LEFT JOIN can use
102785   ** its Expr.iRightJoinTable value to find the bitmask of the right table
102786   ** of the join.  Subtracting one from the right table bitmask gives a
102787   ** bitmask for all tables to the left of the join.  Knowing the bitmask
102788   ** for all tables to the left of a left join is important.  Ticket #3015.
102789   **
102790   ** Configure the WhereClause.vmask variable so that bits that correspond
102791   ** to virtual table cursors are set. This is used to selectively disable
102792   ** the OR-to-IN transformation in exprAnalyzeOrTerm(). It is not helpful
102793   ** with virtual tables.
102794   **
102795   ** Note that bitmasks are created for all pTabList->nSrc tables in
102796   ** pTabList, not just the first nTabList tables.  nTabList is normally
102797   ** equal to pTabList->nSrc but might be shortened to 1 if the
102798   ** WHERE_ONETABLE_ONLY flag is set.
102799   */
102800   assert( pWC->vmask==0 && pMaskSet->n==0 );
102801   for(i=0; i<pTabList->nSrc; i++){
102802     createMask(pMaskSet, pTabList->a[i].iCursor);
102803 #ifndef SQLITE_OMIT_VIRTUALTABLE
102804     if( ALWAYS(pTabList->a[i].pTab) && IsVirtual(pTabList->a[i].pTab) ){
102805       pWC->vmask |= ((Bitmask)1 << i);
102806     }
102807 #endif
102808   }
102809 #ifndef NDEBUG
102810   {
102811     Bitmask toTheLeft = 0;
102812     for(i=0; i<pTabList->nSrc; i++){
102813       Bitmask m = getMask(pMaskSet, pTabList->a[i].iCursor);
102814       assert( (m-1)==toTheLeft );
102815       toTheLeft |= m;
102816     }
102817   }
102818 #endif
102819 
102820   /* Analyze all of the subexpressions.  Note that exprAnalyze() might
102821   ** add new virtual terms onto the end of the WHERE clause.  We do not
102822   ** want to analyze these virtual terms, so start analyzing at the end
102823   ** and work forward so that the added virtual terms are never processed.
102824   */
102825   exprAnalyzeAll(pTabList, pWC);
102826   if( db->mallocFailed ){
102827     goto whereBeginError;
102828   }
102829 
102830   /* Chose the best index to use for each table in the FROM clause.
102831   **
102832   ** This loop fills in the following fields:
102833   **
102834   **   pWInfo->a[].pIdx      The index to use for this level of the loop.
102835   **   pWInfo->a[].wsFlags   WHERE_xxx flags associated with pIdx
102836   **   pWInfo->a[].nEq       The number of == and IN constraints
102837   **   pWInfo->a[].iFrom     Which term of the FROM clause is being coded
102838   **   pWInfo->a[].iTabCur   The VDBE cursor for the database table
102839   **   pWInfo->a[].iIdxCur   The VDBE cursor for the index
102840   **   pWInfo->a[].pTerm     When wsFlags==WO_OR, the OR-clause term
102841   **
102842   ** This loop also figures out the nesting order of tables in the FROM
102843   ** clause.
102844   */
102845   notReady = ~(Bitmask)0;
102846   andFlags = ~0;
102847   WHERETRACE(("*** Optimizer Start ***\n"));
102848   for(i=iFrom=0, pLevel=pWInfo->a; i<nTabList; i++, pLevel++){
102849     WhereCost bestPlan;         /* Most efficient plan seen so far */
102850     Index *pIdx;                /* Index for FROM table at pTabItem */
102851     int j;                      /* For looping over FROM tables */
102852     int bestJ = -1;             /* The value of j */
102853     Bitmask m;                  /* Bitmask value for j or bestJ */
102854     int isOptimal;              /* Iterator for optimal/non-optimal search */
102855     int nUnconstrained;         /* Number tables without INDEXED BY */
102856     Bitmask notIndexed;         /* Mask of tables that cannot use an index */
102857 
102858     memset(&bestPlan, 0, sizeof(bestPlan));
102859     bestPlan.rCost = SQLITE_BIG_DBL;
102860     WHERETRACE(("*** Begin search for loop %d ***\n", i));
102861 
102862     /* Loop through the remaining entries in the FROM clause to find the
102863     ** next nested loop. The loop tests all FROM clause entries
102864     ** either once or twice.
102865     **
102866     ** The first test is always performed if there are two or more entries
102867     ** remaining and never performed if there is only one FROM clause entry
102868     ** to choose from.  The first test looks for an "optimal" scan.  In
102869     ** this context an optimal scan is one that uses the same strategy
102870     ** for the given FROM clause entry as would be selected if the entry
102871     ** were used as the innermost nested loop.  In other words, a table
102872     ** is chosen such that the cost of running that table cannot be reduced
102873     ** by waiting for other tables to run first.  This "optimal" test works
102874     ** by first assuming that the FROM clause is on the inner loop and finding
102875     ** its query plan, then checking to see if that query plan uses any
102876     ** other FROM clause terms that are notReady.  If no notReady terms are
102877     ** used then the "optimal" query plan works.
102878     **
102879     ** Note that the WhereCost.nRow parameter for an optimal scan might
102880     ** not be as small as it would be if the table really were the innermost
102881     ** join.  The nRow value can be reduced by WHERE clause constraints
102882     ** that do not use indices.  But this nRow reduction only happens if the
102883     ** table really is the innermost join.
102884     **
102885     ** The second loop iteration is only performed if no optimal scan
102886     ** strategies were found by the first iteration. This second iteration
102887     ** is used to search for the lowest cost scan overall.
102888     **
102889     ** Previous versions of SQLite performed only the second iteration -
102890     ** the next outermost loop was always that with the lowest overall
102891     ** cost. However, this meant that SQLite could select the wrong plan
102892     ** for scripts such as the following:
102893     **
102894     **   CREATE TABLE t1(a, b);
102895     **   CREATE TABLE t2(c, d);
102896     **   SELECT * FROM t2, t1 WHERE t2.rowid = t1.a;
102897     **
102898     ** The best strategy is to iterate through table t1 first. However it
102899     ** is not possible to determine this with a simple greedy algorithm.
102900     ** Since the cost of a linear scan through table t2 is the same
102901     ** as the cost of a linear scan through table t1, a simple greedy
102902     ** algorithm may choose to use t2 for the outer loop, which is a much
102903     ** costlier approach.
102904     */
102905     nUnconstrained = 0;
102906     notIndexed = 0;
102907     for(isOptimal=(iFrom<nTabList-1); isOptimal>=0 && bestJ<0; isOptimal--){
102908       Bitmask mask;             /* Mask of tables not yet ready */
102909       for(j=iFrom, pTabItem=&pTabList->a[j]; j<nTabList; j++, pTabItem++){
102910         int doNotReorder;    /* True if this table should not be reordered */
102911         WhereCost sCost;     /* Cost information from best[Virtual]Index() */
102912         ExprList *pOrderBy;  /* ORDER BY clause for index to optimize */
102913 
102914         doNotReorder =  (pTabItem->jointype & (JT_LEFT|JT_CROSS))!=0;
102915         if( j!=iFrom && doNotReorder ) break;
102916         m = getMask(pMaskSet, pTabItem->iCursor);
102917         if( (m & notReady)==0 ){
102918           if( j==iFrom ) iFrom++;
102919           continue;
102920         }
102921         mask = (isOptimal ? m : notReady);
102922         pOrderBy = ((i==0 && ppOrderBy )?*ppOrderBy:0);
102923         if( pTabItem->pIndex==0 ) nUnconstrained++;
102924 
102925         WHERETRACE(("=== trying table %d with isOptimal=%d ===\n",
102926                     j, isOptimal));
102927         assert( pTabItem->pTab );
102928 #ifndef SQLITE_OMIT_VIRTUALTABLE
102929         if( IsVirtual(pTabItem->pTab) ){
102930           sqlite3_index_info **pp = &pWInfo->a[j].pIdxInfo;
102931           bestVirtualIndex(pParse, pWC, pTabItem, mask, notReady, pOrderBy,
102932                            &sCost, pp);
102933         }else
102934 #endif
102935         {
102936           bestBtreeIndex(pParse, pWC, pTabItem, mask, notReady, pOrderBy,
102937                          &sCost);
102938         }
102939         assert( isOptimal || (sCost.used&notReady)==0 );
102940 
102941         /* If an INDEXED BY clause is present, then the plan must use that
102942         ** index if it uses any index at all */
102943         assert( pTabItem->pIndex==0
102944                   || (sCost.plan.wsFlags & WHERE_NOT_FULLSCAN)==0
102945                   || sCost.plan.u.pIdx==pTabItem->pIndex );
102946 
102947         if( isOptimal && (sCost.plan.wsFlags & WHERE_NOT_FULLSCAN)==0 ){
102948           notIndexed |= m;
102949         }
102950 
102951         /* Conditions under which this table becomes the best so far:
102952         **
102953         **   (1) The table must not depend on other tables that have not
102954         **       yet run.
102955         **
102956         **   (2) A full-table-scan plan cannot supercede indexed plan unless
102957         **       the full-table-scan is an "optimal" plan as defined above.
102958         **
102959         **   (3) All tables have an INDEXED BY clause or this table lacks an
102960         **       INDEXED BY clause or this table uses the specific
102961         **       index specified by its INDEXED BY clause.  This rule ensures
102962         **       that a best-so-far is always selected even if an impossible
102963         **       combination of INDEXED BY clauses are given.  The error
102964         **       will be detected and relayed back to the application later.
102965         **       The NEVER() comes about because rule (2) above prevents
102966         **       An indexable full-table-scan from reaching rule (3).
102967         **
102968         **   (4) The plan cost must be lower than prior plans or else the
102969         **       cost must be the same and the number of rows must be lower.
102970         */
102971         if( (sCost.used&notReady)==0                       /* (1) */
102972             && (bestJ<0 || (notIndexed&m)!=0               /* (2) */
102973                 || (bestPlan.plan.wsFlags & WHERE_NOT_FULLSCAN)==0
102974                 || (sCost.plan.wsFlags & WHERE_NOT_FULLSCAN)!=0)
102975             && (nUnconstrained==0 || pTabItem->pIndex==0   /* (3) */
102976                 || NEVER((sCost.plan.wsFlags & WHERE_NOT_FULLSCAN)!=0))
102977             && (bestJ<0 || sCost.rCost<bestPlan.rCost      /* (4) */
102978                 || (sCost.rCost<=bestPlan.rCost
102979                  && sCost.plan.nRow<bestPlan.plan.nRow))
102980         ){
102981           WHERETRACE(("=== table %d is best so far"
102982                       " with cost=%g and nRow=%g\n",
102983                       j, sCost.rCost, sCost.plan.nRow));
102984           bestPlan = sCost;
102985           bestJ = j;
102986         }
102987         if( doNotReorder ) break;
102988       }
102989     }
102990     assert( bestJ>=0 );
102991     assert( notReady & getMask(pMaskSet, pTabList->a[bestJ].iCursor) );
102992     WHERETRACE(("*** Optimizer selects table %d for loop %d"
102993                 " with cost=%g and nRow=%g\n",
102994                 bestJ, pLevel-pWInfo->a, bestPlan.rCost, bestPlan.plan.nRow));
102995     if( (bestPlan.plan.wsFlags & WHERE_ORDERBY)!=0 ){
102996       *ppOrderBy = 0;
102997     }
102998     andFlags &= bestPlan.plan.wsFlags;
102999     pLevel->plan = bestPlan.plan;
103000     testcase( bestPlan.plan.wsFlags & WHERE_INDEXED );
103001     testcase( bestPlan.plan.wsFlags & WHERE_TEMP_INDEX );
103002     if( bestPlan.plan.wsFlags & (WHERE_INDEXED|WHERE_TEMP_INDEX) ){
103003       pLevel->iIdxCur = pParse->nTab++;
103004     }else{
103005       pLevel->iIdxCur = -1;
103006     }
103007     notReady &= ~getMask(pMaskSet, pTabList->a[bestJ].iCursor);
103008     pLevel->iFrom = (u8)bestJ;
103009     if( bestPlan.plan.nRow>=(double)1 ){
103010       pParse->nQueryLoop *= bestPlan.plan.nRow;
103011     }
103012 
103013     /* Check that if the table scanned by this loop iteration had an
103014     ** INDEXED BY clause attached to it, that the named index is being
103015     ** used for the scan. If not, then query compilation has failed.
103016     ** Return an error.
103017     */
103018     pIdx = pTabList->a[bestJ].pIndex;
103019     if( pIdx ){
103020       if( (bestPlan.plan.wsFlags & WHERE_INDEXED)==0 ){
103021         sqlite3ErrorMsg(pParse, "cannot use index: %s", pIdx->zName);
103022         goto whereBeginError;
103023       }else{
103024         /* If an INDEXED BY clause is used, the bestIndex() function is
103025         ** guaranteed to find the index specified in the INDEXED BY clause
103026         ** if it find an index at all. */
103027         assert( bestPlan.plan.u.pIdx==pIdx );
103028       }
103029     }
103030   }
103031   WHERETRACE(("*** Optimizer Finished ***\n"));
103032   if( pParse->nErr || db->mallocFailed ){
103033     goto whereBeginError;
103034   }
103035 
103036   /* If the total query only selects a single row, then the ORDER BY
103037   ** clause is irrelevant.
103038   */
103039   if( (andFlags & WHERE_UNIQUE)!=0 && ppOrderBy ){
103040     *ppOrderBy = 0;
103041   }
103042 
103043   /* If the caller is an UPDATE or DELETE statement that is requesting
103044   ** to use a one-pass algorithm, determine if this is appropriate.
103045   ** The one-pass algorithm only works if the WHERE clause constraints
103046   ** the statement to update a single row.
103047   */
103048   assert( (wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || pWInfo->nLevel==1 );
103049   if( (wctrlFlags & WHERE_ONEPASS_DESIRED)!=0 && (andFlags & WHERE_UNIQUE)!=0 ){
103050     pWInfo->okOnePass = 1;
103051     pWInfo->a[0].plan.wsFlags &= ~WHERE_IDX_ONLY;
103052   }
103053 
103054   /* Open all tables in the pTabList and any indices selected for
103055   ** searching those tables.
103056   */
103057   sqlite3CodeVerifySchema(pParse, -1); /* Insert the cookie verifier Goto */
103058   notReady = ~(Bitmask)0;
103059   pWInfo->nRowOut = (double)1;
103060   for(i=0, pLevel=pWInfo->a; i<nTabList; i++, pLevel++){
103061     Table *pTab;     /* Table to open */
103062     int iDb;         /* Index of database containing table/index */
103063 
103064     pTabItem = &pTabList->a[pLevel->iFrom];
103065     pTab = pTabItem->pTab;
103066     pLevel->iTabCur = pTabItem->iCursor;
103067     pWInfo->nRowOut *= pLevel->plan.nRow;
103068     iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
103069     if( (pTab->tabFlags & TF_Ephemeral)!=0 || pTab->pSelect ){
103070       /* Do nothing */
103071     }else
103072 #ifndef SQLITE_OMIT_VIRTUALTABLE
103073     if( (pLevel->plan.wsFlags & WHERE_VIRTUALTABLE)!=0 ){
103074       const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
103075       int iCur = pTabItem->iCursor;
103076       sqlite3VdbeAddOp4(v, OP_VOpen, iCur, 0, 0, pVTab, P4_VTAB);
103077     }else
103078 #endif
103079     if( (pLevel->plan.wsFlags & WHERE_IDX_ONLY)==0
103080          && (wctrlFlags & WHERE_OMIT_OPEN)==0 ){
103081       int op = pWInfo->okOnePass ? OP_OpenWrite : OP_OpenRead;
103082       sqlite3OpenTable(pParse, pTabItem->iCursor, iDb, pTab, op);
103083       testcase( pTab->nCol==BMS-1 );
103084       testcase( pTab->nCol==BMS );
103085       if( !pWInfo->okOnePass && pTab->nCol<BMS ){
103086         Bitmask b = pTabItem->colUsed;
103087         int n = 0;
103088         for(; b; b=b>>1, n++){}
103089         sqlite3VdbeChangeP4(v, sqlite3VdbeCurrentAddr(v)-1,
103090                             SQLITE_INT_TO_PTR(n), P4_INT32);
103091         assert( n<=pTab->nCol );
103092       }
103093     }else{
103094       sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
103095     }
103096 #ifndef SQLITE_OMIT_AUTOMATIC_INDEX
103097     if( (pLevel->plan.wsFlags & WHERE_TEMP_INDEX)!=0 ){
103098       constructAutomaticIndex(pParse, pWC, pTabItem, notReady, pLevel);
103099     }else
103100 #endif
103101     if( (pLevel->plan.wsFlags & WHERE_INDEXED)!=0 ){
103102       Index *pIx = pLevel->plan.u.pIdx;
103103       KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIx);
103104       int iIdxCur = pLevel->iIdxCur;
103105       assert( pIx->pSchema==pTab->pSchema );
103106       assert( iIdxCur>=0 );
103107       sqlite3VdbeAddOp4(v, OP_OpenRead, iIdxCur, pIx->tnum, iDb,
103108                         (char*)pKey, P4_KEYINFO_HANDOFF);
103109       VdbeComment((v, "%s", pIx->zName));
103110     }
103111     sqlite3CodeVerifySchema(pParse, iDb);
103112     notReady &= ~getMask(pWC->pMaskSet, pTabItem->iCursor);
103113   }
103114   pWInfo->iTop = sqlite3VdbeCurrentAddr(v);
103115   if( db->mallocFailed ) goto whereBeginError;
103116 
103117   /* Generate the code to do the search.  Each iteration of the for
103118   ** loop below generates code for a single nested loop of the VM
103119   ** program.
103120   */
103121   notReady = ~(Bitmask)0;
103122   for(i=0; i<nTabList; i++){
103123     pLevel = &pWInfo->a[i];
103124     explainOneScan(pParse, pTabList, pLevel, i, pLevel->iFrom, wctrlFlags);
103125     notReady = codeOneLoopStart(pWInfo, i, wctrlFlags, notReady);
103126     pWInfo->iContinue = pLevel->addrCont;
103127   }
103128 
103129 #ifdef SQLITE_TEST  /* For testing and debugging use only */
103130   /* Record in the query plan information about the current table
103131   ** and the index used to access it (if any).  If the table itself
103132   ** is not used, its name is just '{}'.  If no index is used
103133   ** the index is listed as "{}".  If the primary key is used the
103134   ** index name is '*'.
103135   */
103136   for(i=0; i<nTabList; i++){
103137     char *z;
103138     int n;
103139     pLevel = &pWInfo->a[i];
103140     pTabItem = &pTabList->a[pLevel->iFrom];
103141     z = pTabItem->zAlias;
103142     if( z==0 ) z = pTabItem->pTab->zName;
103143     n = sqlite3Strlen30(z);
103144     if( n+nQPlan < sizeof(sqlite3_query_plan)-10 ){
103145       if( pLevel->plan.wsFlags & WHERE_IDX_ONLY ){
103146         memcpy(&sqlite3_query_plan[nQPlan], "{}", 2);
103147         nQPlan += 2;
103148       }else{
103149         memcpy(&sqlite3_query_plan[nQPlan], z, n);
103150         nQPlan += n;
103151       }
103152       sqlite3_query_plan[nQPlan++] = ' ';
103153     }
103154     testcase( pLevel->plan.wsFlags & WHERE_ROWID_EQ );
103155     testcase( pLevel->plan.wsFlags & WHERE_ROWID_RANGE );
103156     if( pLevel->plan.wsFlags & (WHERE_ROWID_EQ|WHERE_ROWID_RANGE) ){
103157       memcpy(&sqlite3_query_plan[nQPlan], "* ", 2);
103158       nQPlan += 2;
103159     }else if( (pLevel->plan.wsFlags & WHERE_INDEXED)!=0 ){
103160       n = sqlite3Strlen30(pLevel->plan.u.pIdx->zName);
103161       if( n+nQPlan < sizeof(sqlite3_query_plan)-2 ){
103162         memcpy(&sqlite3_query_plan[nQPlan], pLevel->plan.u.pIdx->zName, n);
103163         nQPlan += n;
103164         sqlite3_query_plan[nQPlan++] = ' ';
103165       }
103166     }else{
103167       memcpy(&sqlite3_query_plan[nQPlan], "{} ", 3);
103168       nQPlan += 3;
103169     }
103170   }
103171   while( nQPlan>0 && sqlite3_query_plan[nQPlan-1]==' ' ){
103172     sqlite3_query_plan[--nQPlan] = 0;
103173   }
103174   sqlite3_query_plan[nQPlan] = 0;
103175   nQPlan = 0;
103176 #endif /* SQLITE_TEST // Testing and debugging use only */
103177 
103178   /* Record the continuation address in the WhereInfo structure.  Then
103179   ** clean up and return.
103180   */
103181   return pWInfo;
103182 
103183   /* Jump here if malloc fails */
103184 whereBeginError:
103185   if( pWInfo ){
103186     pParse->nQueryLoop = pWInfo->savedNQueryLoop;
103187     whereInfoFree(db, pWInfo);
103188   }
103189   return 0;
103190 }
103191 
103192 /*
103193 ** Generate the end of the WHERE loop.  See comments on
103194 ** sqlite3WhereBegin() for additional information.
103195 */
103196 SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo *pWInfo){
103197   Parse *pParse = pWInfo->pParse;
103198   Vdbe *v = pParse->pVdbe;
103199   int i;
103200   WhereLevel *pLevel;
103201   SrcList *pTabList = pWInfo->pTabList;
103202   sqlite3 *db = pParse->db;
103203 
103204   /* Generate loop termination code.
103205   */
103206   sqlite3ExprCacheClear(pParse);
103207   for(i=pWInfo->nLevel-1; i>=0; i--){
103208     pLevel = &pWInfo->a[i];
103209     sqlite3VdbeResolveLabel(v, pLevel->addrCont);
103210     if( pLevel->op!=OP_Noop ){
103211       sqlite3VdbeAddOp2(v, pLevel->op, pLevel->p1, pLevel->p2);
103212       sqlite3VdbeChangeP5(v, pLevel->p5);
103213     }
103214     if( pLevel->plan.wsFlags & WHERE_IN_ABLE && pLevel->u.in.nIn>0 ){
103215       struct InLoop *pIn;
103216       int j;
103217       sqlite3VdbeResolveLabel(v, pLevel->addrNxt);
103218       for(j=pLevel->u.in.nIn, pIn=&pLevel->u.in.aInLoop[j-1]; j>0; j--, pIn--){
103219         sqlite3VdbeJumpHere(v, pIn->addrInTop+1);
103220         sqlite3VdbeAddOp2(v, OP_Next, pIn->iCur, pIn->addrInTop);
103221         sqlite3VdbeJumpHere(v, pIn->addrInTop-1);
103222       }
103223       sqlite3DbFree(db, pLevel->u.in.aInLoop);
103224     }
103225     sqlite3VdbeResolveLabel(v, pLevel->addrBrk);
103226     if( pLevel->iLeftJoin ){
103227       int addr;
103228       addr = sqlite3VdbeAddOp1(v, OP_IfPos, pLevel->iLeftJoin);
103229       assert( (pLevel->plan.wsFlags & WHERE_IDX_ONLY)==0
103230            || (pLevel->plan.wsFlags & WHERE_INDEXED)!=0 );
103231       if( (pLevel->plan.wsFlags & WHERE_IDX_ONLY)==0 ){
103232         sqlite3VdbeAddOp1(v, OP_NullRow, pTabList->a[i].iCursor);
103233       }
103234       if( pLevel->iIdxCur>=0 ){
103235         sqlite3VdbeAddOp1(v, OP_NullRow, pLevel->iIdxCur);
103236       }
103237       if( pLevel->op==OP_Return ){
103238         sqlite3VdbeAddOp2(v, OP_Gosub, pLevel->p1, pLevel->addrFirst);
103239       }else{
103240         sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrFirst);
103241       }
103242       sqlite3VdbeJumpHere(v, addr);
103243     }
103244   }
103245 
103246   /* The "break" point is here, just past the end of the outer loop.
103247   ** Set it.
103248   */
103249   sqlite3VdbeResolveLabel(v, pWInfo->iBreak);
103250 
103251   /* Close all of the cursors that were opened by sqlite3WhereBegin.
103252   */
103253   assert( pWInfo->nLevel==1 || pWInfo->nLevel==pTabList->nSrc );
103254   for(i=0, pLevel=pWInfo->a; i<pWInfo->nLevel; i++, pLevel++){
103255     struct SrcList_item *pTabItem = &pTabList->a[pLevel->iFrom];
103256     Table *pTab = pTabItem->pTab;
103257     assert( pTab!=0 );
103258     if( (pTab->tabFlags & TF_Ephemeral)==0
103259      && pTab->pSelect==0
103260      && (pWInfo->wctrlFlags & WHERE_OMIT_CLOSE)==0
103261     ){
103262       int ws = pLevel->plan.wsFlags;
103263       if( !pWInfo->okOnePass && (ws & WHERE_IDX_ONLY)==0 ){
103264         sqlite3VdbeAddOp1(v, OP_Close, pTabItem->iCursor);
103265       }
103266       if( (ws & WHERE_INDEXED)!=0 && (ws & WHERE_TEMP_INDEX)==0 ){
103267         sqlite3VdbeAddOp1(v, OP_Close, pLevel->iIdxCur);
103268       }
103269     }
103270 
103271     /* If this scan uses an index, make code substitutions to read data
103272     ** from the index in preference to the table. Sometimes, this means
103273     ** the table need never be read from. This is a performance boost,
103274     ** as the vdbe level waits until the table is read before actually
103275     ** seeking the table cursor to the record corresponding to the current
103276     ** position in the index.
103277     **
103278     ** Calls to the code generator in between sqlite3WhereBegin and
103279     ** sqlite3WhereEnd will have created code that references the table
103280     ** directly.  This loop scans all that code looking for opcodes
103281     ** that reference the table and converts them into opcodes that
103282     ** reference the index.
103283     */
103284     if( (pLevel->plan.wsFlags & WHERE_INDEXED)!=0 && !db->mallocFailed){
103285       int k, j, last;
103286       VdbeOp *pOp;
103287       Index *pIdx = pLevel->plan.u.pIdx;
103288 
103289       assert( pIdx!=0 );
103290       pOp = sqlite3VdbeGetOp(v, pWInfo->iTop);
103291       last = sqlite3VdbeCurrentAddr(v);
103292       for(k=pWInfo->iTop; k<last; k++, pOp++){
103293         if( pOp->p1!=pLevel->iTabCur ) continue;
103294         if( pOp->opcode==OP_Column ){
103295           for(j=0; j<pIdx->nColumn; j++){
103296             if( pOp->p2==pIdx->aiColumn[j] ){
103297               pOp->p2 = j;
103298               pOp->p1 = pLevel->iIdxCur;
103299               break;
103300             }
103301           }
103302           assert( (pLevel->plan.wsFlags & WHERE_IDX_ONLY)==0
103303                || j<pIdx->nColumn );
103304         }else if( pOp->opcode==OP_Rowid ){
103305           pOp->p1 = pLevel->iIdxCur;
103306           pOp->opcode = OP_IdxRowid;
103307         }
103308       }
103309     }
103310   }
103311 
103312   /* Final cleanup
103313   */
103314   pParse->nQueryLoop = pWInfo->savedNQueryLoop;
103315   whereInfoFree(db, pWInfo);
103316   return;
103317 }
103318 
103319 /************** End of where.c ***********************************************/
103320 /************** Begin file parse.c *******************************************/
103321 /* Driver template for the LEMON parser generator.
103322 ** The author disclaims copyright to this source code.
103323 **
103324 ** This version of "lempar.c" is modified, slightly, for use by SQLite.
103325 ** The only modifications are the addition of a couple of NEVER()
103326 ** macros to disable tests that are needed in the case of a general
103327 ** LALR(1) grammar but which are always false in the
103328 ** specific grammar used by SQLite.
103329 */
103330 /* First off, code is included that follows the "include" declaration
103331 ** in the input grammar file. */
103332 
103333 
103334 /*
103335 ** Disable all error recovery processing in the parser push-down
103336 ** automaton.
103337 */
103338 #define YYNOERRORRECOVERY 1
103339 
103340 /*
103341 ** Make yytestcase() the same as testcase()
103342 */
103343 #define yytestcase(X) testcase(X)
103344 
103345 /*
103346 ** An instance of this structure holds information about the
103347 ** LIMIT clause of a SELECT statement.
103348 */
103349 struct LimitVal {
103350   Expr *pLimit;    /* The LIMIT expression.  NULL if there is no limit */
103351   Expr *pOffset;   /* The OFFSET expression.  NULL if there is none */
103352 };
103353 
103354 /*
103355 ** An instance of this structure is used to store the LIKE,
103356 ** GLOB, NOT LIKE, and NOT GLOB operators.
103357 */
103358 struct LikeOp {
103359   Token eOperator;  /* "like" or "glob" or "regexp" */
103360   int not;         /* True if the NOT keyword is present */
103361 };
103362 
103363 /*
103364 ** An instance of the following structure describes the event of a
103365 ** TRIGGER.  "a" is the event type, one of TK_UPDATE, TK_INSERT,
103366 ** TK_DELETE, or TK_INSTEAD.  If the event is of the form
103367 **
103368 **      UPDATE ON (a,b,c)
103369 **
103370 ** Then the "b" IdList records the list "a,b,c".
103371 */
103372 struct TrigEvent { int a; IdList * b; };
103373 
103374 /*
103375 ** An instance of this structure holds the ATTACH key and the key type.
103376 */
103377 struct AttachKey { int type;  Token key; };
103378 
103379 
103380   /* This is a utility routine used to set the ExprSpan.zStart and
103381   ** ExprSpan.zEnd values of pOut so that the span covers the complete
103382   ** range of text beginning with pStart and going to the end of pEnd.
103383   */
103384   static void spanSet(ExprSpan *pOut, Token *pStart, Token *pEnd){
103385     pOut->zStart = pStart->z;
103386     pOut->zEnd = &pEnd->z[pEnd->n];
103387   }
103388 
103389   /* Construct a new Expr object from a single identifier.  Use the
103390   ** new Expr to populate pOut.  Set the span of pOut to be the identifier
103391   ** that created the expression.
103392   */
103393   static void spanExpr(ExprSpan *pOut, Parse *pParse, int op, Token *pValue){
103394     pOut->pExpr = sqlite3PExpr(pParse, op, 0, 0, pValue);
103395     pOut->zStart = pValue->z;
103396     pOut->zEnd = &pValue->z[pValue->n];
103397   }
103398 
103399   /* This routine constructs a binary expression node out of two ExprSpan
103400   ** objects and uses the result to populate a new ExprSpan object.
103401   */
103402   static void spanBinaryExpr(
103403     ExprSpan *pOut,     /* Write the result here */
103404     Parse *pParse,      /* The parsing context.  Errors accumulate here */
103405     int op,             /* The binary operation */
103406     ExprSpan *pLeft,    /* The left operand */
103407     ExprSpan *pRight    /* The right operand */
103408   ){
103409     pOut->pExpr = sqlite3PExpr(pParse, op, pLeft->pExpr, pRight->pExpr, 0);
103410     pOut->zStart = pLeft->zStart;
103411     pOut->zEnd = pRight->zEnd;
103412   }
103413 
103414   /* Construct an expression node for a unary postfix operator
103415   */
103416   static void spanUnaryPostfix(
103417     ExprSpan *pOut,        /* Write the new expression node here */
103418     Parse *pParse,         /* Parsing context to record errors */
103419     int op,                /* The operator */
103420     ExprSpan *pOperand,    /* The operand */
103421     Token *pPostOp         /* The operand token for setting the span */
103422   ){
103423     pOut->pExpr = sqlite3PExpr(pParse, op, pOperand->pExpr, 0, 0);
103424     pOut->zStart = pOperand->zStart;
103425     pOut->zEnd = &pPostOp->z[pPostOp->n];
103426   }
103427 
103428   /* A routine to convert a binary TK_IS or TK_ISNOT expression into a
103429   ** unary TK_ISNULL or TK_NOTNULL expression. */
103430   static void binaryToUnaryIfNull(Parse *pParse, Expr *pY, Expr *pA, int op){
103431     sqlite3 *db = pParse->db;
103432     if( db->mallocFailed==0 && pY->op==TK_NULL ){
103433       pA->op = (u8)op;
103434       sqlite3ExprDelete(db, pA->pRight);
103435       pA->pRight = 0;
103436     }
103437   }
103438 
103439   /* Construct an expression node for a unary prefix operator
103440   */
103441   static void spanUnaryPrefix(
103442     ExprSpan *pOut,        /* Write the new expression node here */
103443     Parse *pParse,         /* Parsing context to record errors */
103444     int op,                /* The operator */
103445     ExprSpan *pOperand,    /* The operand */
103446     Token *pPreOp         /* The operand token for setting the span */
103447   ){
103448     pOut->pExpr = sqlite3PExpr(pParse, op, pOperand->pExpr, 0, 0);
103449     pOut->zStart = pPreOp->z;
103450     pOut->zEnd = pOperand->zEnd;
103451   }
103452 /* Next is all token values, in a form suitable for use by makeheaders.
103453 ** This section will be null unless lemon is run with the -m switch.
103454 */
103455 /*
103456 ** These constants (all generated automatically by the parser generator)
103457 ** specify the various kinds of tokens (terminals) that the parser
103458 ** understands.
103459 **
103460 ** Each symbol here is a terminal symbol in the grammar.
103461 */
103462 /* Make sure the INTERFACE macro is defined.
103463 */
103464 #ifndef INTERFACE
103465 # define INTERFACE 1
103466 #endif
103467 /* The next thing included is series of defines which control
103468 ** various aspects of the generated parser.
103469 **    YYCODETYPE         is the data type used for storing terminal
103470 **                       and nonterminal numbers.  "unsigned char" is
103471 **                       used if there are fewer than 250 terminals
103472 **                       and nonterminals.  "int" is used otherwise.
103473 **    YYNOCODE           is a number of type YYCODETYPE which corresponds
103474 **                       to no legal terminal or nonterminal number.  This
103475 **                       number is used to fill in empty slots of the hash
103476 **                       table.
103477 **    YYFALLBACK         If defined, this indicates that one or more tokens
103478 **                       have fall-back values which should be used if the
103479 **                       original value of the token will not parse.
103480 **    YYACTIONTYPE       is the data type used for storing terminal
103481 **                       and nonterminal numbers.  "unsigned char" is
103482 **                       used if there are fewer than 250 rules and
103483 **                       states combined.  "int" is used otherwise.
103484 **    sqlite3ParserTOKENTYPE     is the data type used for minor tokens given
103485 **                       directly to the parser from the tokenizer.
103486 **    YYMINORTYPE        is the data type used for all minor tokens.
103487 **                       This is typically a union of many types, one of
103488 **                       which is sqlite3ParserTOKENTYPE.  The entry in the union
103489 **                       for base tokens is called "yy0".
103490 **    YYSTACKDEPTH       is the maximum depth of the parser's stack.  If
103491 **                       zero the stack is dynamically sized using realloc()
103492 **    sqlite3ParserARG_SDECL     A static variable declaration for the %extra_argument
103493 **    sqlite3ParserARG_PDECL     A parameter declaration for the %extra_argument
103494 **    sqlite3ParserARG_STORE     Code to store %extra_argument into yypParser
103495 **    sqlite3ParserARG_FETCH     Code to extract %extra_argument from yypParser
103496 **    YYNSTATE           the combined number of states.
103497 **    YYNRULE            the number of rules in the grammar
103498 **    YYERRORSYMBOL      is the code number of the error symbol.  If not
103499 **                       defined, then do no error processing.
103500 */
103501 #define YYCODETYPE unsigned char
103502 #define YYNOCODE 253
103503 #define YYACTIONTYPE unsigned short int
103504 #define YYWILDCARD 67
103505 #define sqlite3ParserTOKENTYPE Token
103506 typedef union {
103507   int yyinit;
103508   sqlite3ParserTOKENTYPE yy0;
103509   int yy4;
103510   struct TrigEvent yy90;
103511   ExprSpan yy118;
103512   TriggerStep* yy203;
103513   u8 yy210;
103514   struct {int value; int mask;} yy215;
103515   SrcList* yy259;
103516   struct LimitVal yy292;
103517   Expr* yy314;
103518   ExprList* yy322;
103519   struct LikeOp yy342;
103520   IdList* yy384;
103521   Select* yy387;
103522 } YYMINORTYPE;
103523 #ifndef YYSTACKDEPTH
103524 #define YYSTACKDEPTH 100
103525 #endif
103526 #define sqlite3ParserARG_SDECL Parse *pParse;
103527 #define sqlite3ParserARG_PDECL ,Parse *pParse
103528 #define sqlite3ParserARG_FETCH Parse *pParse = yypParser->pParse
103529 #define sqlite3ParserARG_STORE yypParser->pParse = pParse
103530 #define YYNSTATE 630
103531 #define YYNRULE 329
103532 #define YYFALLBACK 1
103533 #define YY_NO_ACTION      (YYNSTATE+YYNRULE+2)
103534 #define YY_ACCEPT_ACTION  (YYNSTATE+YYNRULE+1)
103535 #define YY_ERROR_ACTION   (YYNSTATE+YYNRULE)
103536 
103537 /* The yyzerominor constant is used to initialize instances of
103538 ** YYMINORTYPE objects to zero. */
103539 static const YYMINORTYPE yyzerominor = { 0 };
103540 
103541 /* Define the yytestcase() macro to be a no-op if is not already defined
103542 ** otherwise.
103543 **
103544 ** Applications can choose to define yytestcase() in the %include section
103545 ** to a macro that can assist in verifying code coverage.  For production
103546 ** code the yytestcase() macro should be turned off.  But it is useful
103547 ** for testing.
103548 */
103549 #ifndef yytestcase
103550 # define yytestcase(X)
103551 #endif
103552 
103553 
103554 /* Next are the tables used to determine what action to take based on the
103555 ** current state and lookahead token.  These tables are used to implement
103556 ** functions that take a state number and lookahead value and return an
103557 ** action integer.
103558 **
103559 ** Suppose the action integer is N.  Then the action is determined as
103560 ** follows
103561 **
103562 **   0 <= N < YYNSTATE                  Shift N.  That is, push the lookahead
103563 **                                      token onto the stack and goto state N.
103564 **
103565 **   YYNSTATE <= N < YYNSTATE+YYNRULE   Reduce by rule N-YYNSTATE.
103566 **
103567 **   N == YYNSTATE+YYNRULE              A syntax error has occurred.
103568 **
103569 **   N == YYNSTATE+YYNRULE+1            The parser accepts its input.
103570 **
103571 **   N == YYNSTATE+YYNRULE+2            No such action.  Denotes unused
103572 **                                      slots in the yy_action[] table.
103573 **
103574 ** The action table is constructed as a single large table named yy_action[].
103575 ** Given state S and lookahead X, the action is computed as
103576 **
103577 **      yy_action[ yy_shift_ofst[S] + X ]
103578 **
103579 ** If the index value yy_shift_ofst[S]+X is out of range or if the value
103580 ** yy_lookahead[yy_shift_ofst[S]+X] is not equal to X or if yy_shift_ofst[S]
103581 ** is equal to YY_SHIFT_USE_DFLT, it means that the action is not in the table
103582 ** and that yy_default[S] should be used instead.
103583 **
103584 ** The formula above is for computing the action when the lookahead is
103585 ** a terminal symbol.  If the lookahead is a non-terminal (as occurs after
103586 ** a reduce action) then the yy_reduce_ofst[] array is used in place of
103587 ** the yy_shift_ofst[] array and YY_REDUCE_USE_DFLT is used in place of
103588 ** YY_SHIFT_USE_DFLT.
103589 **
103590 ** The following are the tables generated in this section:
103591 **
103592 **  yy_action[]        A single table containing all actions.
103593 **  yy_lookahead[]     A table containing the lookahead for each entry in
103594 **                     yy_action.  Used to detect hash collisions.
103595 **  yy_shift_ofst[]    For each state, the offset into yy_action for
103596 **                     shifting terminals.
103597 **  yy_reduce_ofst[]   For each state, the offset into yy_action for
103598 **                     shifting non-terminals after a reduce.
103599 **  yy_default[]       Default action for each state.
103600 */
103601 #define YY_ACTTAB_COUNT (1557)
103602 static const YYACTIONTYPE yy_action[] = {
103603  /*     0 */   313,  960,  186,  419,    2,  172,  627,  597,   55,   55,
103604  /*    10 */    55,   55,   48,   53,   53,   53,   53,   52,   52,   51,
103605  /*    20 */    51,   51,   50,  238,  302,  283,  623,  622,  516,  515,
103606  /*    30 */   590,  584,   55,   55,   55,   55,  282,   53,   53,   53,
103607  /*    40 */    53,   52,   52,   51,   51,   51,   50,  238,    6,   56,
103608  /*    50 */    57,   47,  582,  581,  583,  583,   54,   54,   55,   55,
103609  /*    60 */    55,   55,  608,   53,   53,   53,   53,   52,   52,   51,
103610  /*    70 */    51,   51,   50,  238,  313,  597,  409,  330,  579,  579,
103611  /*    80 */    32,   53,   53,   53,   53,   52,   52,   51,   51,   51,
103612  /*    90 */    50,  238,  330,  217,  620,  619,  166,  411,  624,  382,
103613  /*   100 */   379,  378,    7,  491,  590,  584,  200,  199,  198,   58,
103614  /*   110 */   377,  300,  414,  621,  481,   66,  623,  622,  621,  580,
103615  /*   120 */   254,  601,   94,   56,   57,   47,  582,  581,  583,  583,
103616  /*   130 */    54,   54,   55,   55,   55,   55,  671,   53,   53,   53,
103617  /*   140 */    53,   52,   52,   51,   51,   51,   50,  238,  313,  532,
103618  /*   150 */   226,  506,  507,  133,  177,  139,  284,  385,  279,  384,
103619  /*   160 */   169,  197,  342,  398,  251,  226,  253,  275,  388,  167,
103620  /*   170 */   139,  284,  385,  279,  384,  169,  570,  236,  590,  584,
103621  /*   180 */   672,  240,  275,  157,  620,  619,  554,  437,   51,   51,
103622  /*   190 */    51,   50,  238,  343,  439,  553,  438,   56,   57,   47,
103623  /*   200 */   582,  581,  583,  583,   54,   54,   55,   55,   55,   55,
103624  /*   210 */   465,   53,   53,   53,   53,   52,   52,   51,   51,   51,
103625  /*   220 */    50,  238,  313,  390,   52,   52,   51,   51,   51,   50,
103626  /*   230 */   238,  391,  166,  491,  566,  382,  379,  378,  409,  440,
103627  /*   240 */   579,  579,  252,  440,  607,   66,  377,  513,  621,   49,
103628  /*   250 */    46,  147,  590,  584,  621,   16,  466,  189,  621,  441,
103629  /*   260 */   442,  673,  526,  441,  340,  577,  595,   64,  194,  482,
103630  /*   270 */   434,   56,   57,   47,  582,  581,  583,  583,   54,   54,
103631  /*   280 */    55,   55,   55,   55,   30,   53,   53,   53,   53,   52,
103632  /*   290 */    52,   51,   51,   51,   50,  238,  313,  593,  593,  593,
103633  /*   300 */   387,  578,  606,  493,  259,  351,  258,  411,    1,  623,
103634  /*   310 */   622,  496,  623,  622,   65,  240,  623,  622,  597,  443,
103635  /*   320 */   237,  239,  414,  341,  237,  602,  590,  584,   18,  603,
103636  /*   330 */   166,  601,   87,  382,  379,  378,   67,  623,  622,   38,
103637  /*   340 */   623,  622,  176,  270,  377,   56,   57,   47,  582,  581,
103638  /*   350 */   583,  583,   54,   54,   55,   55,   55,   55,  175,   53,
103639  /*   360 */    53,   53,   53,   52,   52,   51,   51,   51,   50,  238,
103640  /*   370 */   313,  396,  233,  411,  531,  565,  317,  620,  619,   44,
103641  /*   380 */   620,  619,  240,  206,  620,  619,  597,  266,  414,  268,
103642  /*   390 */   409,  597,  579,  579,  352,  184,  505,  601,   73,  533,
103643  /*   400 */   590,  584,  466,  548,  190,  620,  619,  576,  620,  619,
103644  /*   410 */   547,  383,  551,   35,  332,  575,  574,  600,  504,   56,
103645  /*   420 */    57,   47,  582,  581,  583,  583,   54,   54,   55,   55,
103646  /*   430 */    55,   55,  567,   53,   53,   53,   53,   52,   52,   51,
103647  /*   440 */    51,   51,   50,  238,  313,  411,  561,  561,  528,  364,
103648  /*   450 */   259,  351,  258,  183,  361,  549,  524,  374,  411,  597,
103649  /*   460 */   414,  240,  560,  560,  409,  604,  579,  579,  328,  601,
103650  /*   470 */    93,  623,  622,  414,  590,  584,  237,  564,  559,  559,
103651  /*   480 */   520,  402,  601,   87,  409,  210,  579,  579,  168,  421,
103652  /*   490 */   950,  519,  950,   56,   57,   47,  582,  581,  583,  583,
103653  /*   500 */    54,   54,   55,   55,   55,   55,  192,   53,   53,   53,
103654  /*   510 */    53,   52,   52,   51,   51,   51,   50,  238,  313,  600,
103655  /*   520 */   293,  563,  511,  234,  357,  146,  475,  475,  367,  411,
103656  /*   530 */   562,  411,  358,  542,  425,  171,  411,  215,  144,  620,
103657  /*   540 */   619,  544,  318,  353,  414,  203,  414,  275,  590,  584,
103658  /*   550 */   549,  414,  174,  601,   94,  601,   79,  558,  471,   61,
103659  /*   560 */   601,   79,  421,  949,  350,  949,   34,   56,   57,   47,
103660  /*   570 */   582,  581,  583,  583,   54,   54,   55,   55,   55,   55,
103661  /*   580 */   535,   53,   53,   53,   53,   52,   52,   51,   51,   51,
103662  /*   590 */    50,  238,  313,  307,  424,  394,  272,   49,   46,  147,
103663  /*   600 */   349,  322,    4,  411,  491,  312,  321,  425,  568,  492,
103664  /*   610 */   216,  264,  407,  575,  574,  429,   66,  549,  414,  621,
103665  /*   620 */   540,  602,  590,  584,   13,  603,  621,  601,   72,   12,
103666  /*   630 */   618,  617,  616,  202,  210,  621,  546,  469,  422,  319,
103667  /*   640 */   148,   56,   57,   47,  582,  581,  583,  583,   54,   54,
103668  /*   650 */    55,   55,   55,   55,  338,   53,   53,   53,   53,   52,
103669  /*   660 */    52,   51,   51,   51,   50,  238,  313,  600,  600,  411,
103670  /*   670 */    39,   21,   37,  170,  237,  875,  411,  572,  572,  201,
103671  /*   680 */   144,  473,  538,  331,  414,  474,  143,  146,  630,  628,
103672  /*   690 */   334,  414,  353,  601,   68,  168,  590,  584,  132,  365,
103673  /*   700 */   601,   96,  307,  423,  530,  336,   49,   46,  147,  568,
103674  /*   710 */   406,  216,  549,  360,  529,   56,   57,   47,  582,  581,
103675  /*   720 */   583,  583,   54,   54,   55,   55,   55,   55,  411,   53,
103676  /*   730 */    53,   53,   53,   52,   52,   51,   51,   51,   50,  238,
103677  /*   740 */   313,  411,  605,  414,  484,  510,  172,  422,  597,  318,
103678  /*   750 */   496,  485,  601,   99,  411,  142,  414,  411,  231,  411,
103679  /*   760 */   540,  411,  359,  629,    2,  601,   97,  426,  308,  414,
103680  /*   770 */   590,  584,  414,   20,  414,  621,  414,  621,  601,  106,
103681  /*   780 */   503,  601,  105,  601,  108,  601,  109,  204,   28,   56,
103682  /*   790 */    57,   47,  582,  581,  583,  583,   54,   54,   55,   55,
103683  /*   800 */    55,   55,  411,   53,   53,   53,   53,   52,   52,   51,
103684  /*   810 */    51,   51,   50,  238,  313,  411,  597,  414,  411,  276,
103685  /*   820 */   214,  600,  411,  366,  213,  381,  601,  134,  274,  500,
103686  /*   830 */   414,  167,  130,  414,  621,  411,  354,  414,  376,  601,
103687  /*   840 */   135,  129,  601,  100,  590,  584,  601,  104,  522,  521,
103688  /*   850 */   414,  621,  224,  273,  600,  167,  327,  282,  600,  601,
103689  /*   860 */   103,  468,  521,   56,   57,   47,  582,  581,  583,  583,
103690  /*   870 */    54,   54,   55,   55,   55,   55,  411,   53,   53,   53,
103691  /*   880 */    53,   52,   52,   51,   51,   51,   50,  238,  313,  411,
103692  /*   890 */    27,  414,  411,  375,  276,  167,  359,  544,   50,  238,
103693  /*   900 */   601,   95,  128,  223,  414,  411,  165,  414,  411,  621,
103694  /*   910 */   411,  621,  612,  601,  102,  372,  601,   76,  590,  584,
103695  /*   920 */   414,  570,  236,  414,  470,  414,  167,  621,  188,  601,
103696  /*   930 */    98,  225,  601,  138,  601,  137,  232,   56,   45,   47,
103697  /*   940 */   582,  581,  583,  583,   54,   54,   55,   55,   55,   55,
103698  /*   950 */   411,   53,   53,   53,   53,   52,   52,   51,   51,   51,
103699  /*   960 */    50,  238,  313,  276,  276,  414,  411,  276,  544,  459,
103700  /*   970 */   359,  171,  209,  479,  601,  136,  628,  334,  621,  621,
103701  /*   980 */   125,  414,  621,  368,  411,  621,  257,  540,  589,  588,
103702  /*   990 */   601,   75,  590,  584,  458,  446,   23,   23,  124,  414,
103703  /*  1000 */   326,  325,  621,  427,  324,  309,  600,  288,  601,   92,
103704  /*  1010 */   586,  585,   57,   47,  582,  581,  583,  583,   54,   54,
103705  /*  1020 */    55,   55,   55,   55,  411,   53,   53,   53,   53,   52,
103706  /*  1030 */    52,   51,   51,   51,   50,  238,  313,  587,  411,  414,
103707  /*  1040 */   411,  207,  611,  476,  171,  472,  160,  123,  601,   91,
103708  /*  1050 */   323,  261,   15,  414,  464,  414,  411,  621,  411,  354,
103709  /*  1060 */   222,  411,  601,   74,  601,   90,  590,  584,  159,  264,
103710  /*  1070 */   158,  414,  461,  414,  621,  600,  414,  121,  120,   25,
103711  /*  1080 */   601,   89,  601,  101,  621,  601,   88,   47,  582,  581,
103712  /*  1090 */   583,  583,   54,   54,   55,   55,   55,   55,  544,   53,
103713  /*  1100 */    53,   53,   53,   52,   52,   51,   51,   51,   50,  238,
103714  /*  1110 */    43,  405,  263,    3,  610,  264,  140,  415,  622,   24,
103715  /*  1120 */   410,   11,  456,  594,  118,  155,  219,  452,  408,  621,
103716  /*  1130 */   621,  621,  156,   43,  405,  621,    3,  286,  621,  113,
103717  /*  1140 */   415,  622,  111,  445,  411,  400,  557,  403,  545,   10,
103718  /*  1150 */   411,  408,  264,  110,  205,  436,  541,  566,  453,  414,
103719  /*  1160 */   621,  621,   63,  621,  435,  414,  411,  621,  601,   94,
103720  /*  1170 */   403,  621,  411,  337,  601,   86,  150,   40,   41,  534,
103721  /*  1180 */   566,  414,  242,  264,   42,  413,  412,  414,  600,  595,
103722  /*  1190 */   601,   85,  191,  333,  107,  451,  601,   84,  621,  539,
103723  /*  1200 */    40,   41,  420,  230,  411,  149,  316,   42,  413,  412,
103724  /*  1210 */   398,  127,  595,  315,  621,  399,  278,  625,  181,  414,
103725  /*  1220 */   593,  593,  593,  592,  591,   14,  450,  411,  601,   71,
103726  /*  1230 */   240,  621,   43,  405,  264,    3,  615,  180,  264,  415,
103727  /*  1240 */   622,  614,  414,  593,  593,  593,  592,  591,   14,  621,
103728  /*  1250 */   408,  601,   70,  621,  417,   33,  405,  613,    3,  411,
103729  /*  1260 */   264,  411,  415,  622,  418,  626,  178,  509,    8,  403,
103730  /*  1270 */   241,  416,  126,  408,  414,  621,  414,  449,  208,  566,
103731  /*  1280 */   240,  221,  621,  601,   83,  601,   82,  599,  297,  277,
103732  /*  1290 */   296,   30,  403,   31,  395,  264,  295,  397,  489,   40,
103733  /*  1300 */    41,  411,  566,  220,  621,  294,   42,  413,  412,  271,
103734  /*  1310 */   621,  595,  600,  621,   59,   60,  414,  269,  267,  623,
103735  /*  1320 */   622,   36,   40,   41,  621,  601,   81,  598,  235,   42,
103736  /*  1330 */   413,  412,  621,  621,  595,  265,  344,  411,  248,  556,
103737  /*  1340 */   173,  185,  593,  593,  593,  592,  591,   14,  218,   29,
103738  /*  1350 */   621,  543,  414,  305,  304,  303,  179,  301,  411,  566,
103739  /*  1360 */   454,  601,   80,  289,  335,  593,  593,  593,  592,  591,
103740  /*  1370 */    14,  411,  287,  414,  151,  392,  246,  260,  411,  196,
103741  /*  1380 */   195,  523,  601,   69,  411,  245,  414,  526,  537,  285,
103742  /*  1390 */   389,  595,  621,  414,  536,  601,   17,  362,  153,  414,
103743  /*  1400 */   466,  463,  601,   78,  154,  414,  462,  152,  601,   77,
103744  /*  1410 */   355,  255,  621,  455,  601,    9,  621,  386,  444,  517,
103745  /*  1420 */   247,  621,  593,  593,  593,  621,  621,  244,  621,  243,
103746  /*  1430 */   430,  518,  292,  621,  329,  621,  145,  393,  280,  513,
103747  /*  1440 */   291,  131,  621,  514,  621,  621,  311,  621,  259,  346,
103748  /*  1450 */   249,  621,  621,  229,  314,  621,  228,  512,  227,  240,
103749  /*  1460 */   494,  488,  310,  164,  487,  486,  373,  480,  163,  262,
103750  /*  1470 */   369,  371,  162,   26,  212,  478,  477,  161,  141,  363,
103751  /*  1480 */   467,  122,  339,  187,  119,  348,  347,  117,  116,  115,
103752  /*  1490 */   114,  112,  182,  457,  320,   22,  433,  432,  448,   19,
103753  /*  1500 */   609,  431,  428,   62,  193,  596,  573,  298,  555,  552,
103754  /*  1510 */   571,  404,  290,  380,  498,  510,  495,  306,  281,  499,
103755  /*  1520 */   250,    5,  497,  460,  345,  447,  569,  550,  238,  299,
103756  /*  1530 */   527,  525,  508,  961,  502,  501,  961,  401,  961,  211,
103757  /*  1540 */   490,  356,  256,  961,  483,  961,  961,  961,  961,  961,
103758  /*  1550 */   961,  961,  961,  961,  961,  961,  370,
103759 };
103760 static const YYCODETYPE yy_lookahead[] = {
103761  /*     0 */    19,  142,  143,  144,  145,   24,    1,   26,   77,   78,
103762  /*    10 */    79,   80,   81,   82,   83,   84,   85,   86,   87,   88,
103763  /*    20 */    89,   90,   91,   92,   15,   98,   26,   27,    7,    8,
103764  /*    30 */    49,   50,   77,   78,   79,   80,  109,   82,   83,   84,
103765  /*    40 */    85,   86,   87,   88,   89,   90,   91,   92,   22,   68,
103766  /*    50 */    69,   70,   71,   72,   73,   74,   75,   76,   77,   78,
103767  /*    60 */    79,   80,   23,   82,   83,   84,   85,   86,   87,   88,
103768  /*    70 */    89,   90,   91,   92,   19,   94,  112,   19,  114,  115,
103769  /*    80 */    25,   82,   83,   84,   85,   86,   87,   88,   89,   90,
103770  /*    90 */    91,   92,   19,   22,   94,   95,   96,  150,  150,   99,
103771  /*   100 */   100,  101,   76,  150,   49,   50,  105,  106,  107,   54,
103772  /*   110 */   110,  158,  165,  165,  161,  162,   26,   27,  165,  113,
103773  /*   120 */    16,  174,  175,   68,   69,   70,   71,   72,   73,   74,
103774  /*   130 */    75,   76,   77,   78,   79,   80,  118,   82,   83,   84,
103775  /*   140 */    85,   86,   87,   88,   89,   90,   91,   92,   19,   23,
103776  /*   150 */    92,   97,   98,   24,   96,   97,   98,   99,  100,  101,
103777  /*   160 */   102,   25,   97,  216,   60,   92,   62,  109,  221,   25,
103778  /*   170 */    97,   98,   99,  100,  101,  102,   86,   87,   49,   50,
103779  /*   180 */   118,  116,  109,   25,   94,   95,   32,   97,   88,   89,
103780  /*   190 */    90,   91,   92,  128,  104,   41,  106,   68,   69,   70,
103781  /*   200 */    71,   72,   73,   74,   75,   76,   77,   78,   79,   80,
103782  /*   210 */    11,   82,   83,   84,   85,   86,   87,   88,   89,   90,
103783  /*   220 */    91,   92,   19,   19,   86,   87,   88,   89,   90,   91,
103784  /*   230 */    92,   27,   96,  150,   66,   99,  100,  101,  112,  150,
103785  /*   240 */   114,  115,  138,  150,  161,  162,  110,  103,  165,  222,
103786  /*   250 */   223,  224,   49,   50,  165,   22,   57,   24,  165,  170,
103787  /*   260 */   171,  118,   94,  170,  171,   23,   98,   25,  185,  186,
103788  /*   270 */   243,   68,   69,   70,   71,   72,   73,   74,   75,   76,
103789  /*   280 */    77,   78,   79,   80,  126,   82,   83,   84,   85,   86,
103790  /*   290 */    87,   88,   89,   90,   91,   92,   19,  129,  130,  131,
103791  /*   300 */    88,   23,  172,  173,  105,  106,  107,  150,   22,   26,
103792  /*   310 */    27,  181,   26,   27,   22,  116,   26,   27,   26,  230,
103793  /*   320 */   231,  197,  165,  230,  231,  113,   49,   50,  204,  117,
103794  /*   330 */    96,  174,  175,   99,  100,  101,   22,   26,   27,  136,
103795  /*   340 */    26,   27,  118,   16,  110,   68,   69,   70,   71,   72,
103796  /*   350 */    73,   74,   75,   76,   77,   78,   79,   80,  118,   82,
103797  /*   360 */    83,   84,   85,   86,   87,   88,   89,   90,   91,   92,
103798  /*   370 */    19,  214,  215,  150,   23,   23,  155,   94,   95,   22,
103799  /*   380 */    94,   95,  116,  160,   94,   95,   94,   60,  165,   62,
103800  /*   390 */   112,   26,  114,  115,  128,   23,   36,  174,  175,   88,
103801  /*   400 */    49,   50,   57,  120,   22,   94,   95,   23,   94,   95,
103802  /*   410 */   120,   51,   25,  136,  169,  170,  171,  194,   58,   68,
103803  /*   420 */    69,   70,   71,   72,   73,   74,   75,   76,   77,   78,
103804  /*   430 */    79,   80,   23,   82,   83,   84,   85,   86,   87,   88,
103805  /*   440 */    89,   90,   91,   92,   19,  150,   12,   12,   23,  228,
103806  /*   450 */   105,  106,  107,   23,  233,   25,  165,   19,  150,   94,
103807  /*   460 */   165,  116,   28,   28,  112,  174,  114,  115,  108,  174,
103808  /*   470 */   175,   26,   27,  165,   49,   50,  231,   11,   44,   44,
103809  /*   480 */    46,   46,  174,  175,  112,  160,  114,  115,   50,   22,
103810  /*   490 */    23,   57,   25,   68,   69,   70,   71,   72,   73,   74,
103811  /*   500 */    75,   76,   77,   78,   79,   80,  119,   82,   83,   84,
103812  /*   510 */    85,   86,   87,   88,   89,   90,   91,   92,   19,  194,
103813  /*   520 */   225,   23,   23,  215,   19,   95,  105,  106,  107,  150,
103814  /*   530 */    23,  150,   27,   23,   67,   25,  150,  206,  207,   94,
103815  /*   540 */    95,  166,  104,  218,  165,   22,  165,  109,   49,   50,
103816  /*   550 */   120,  165,   25,  174,  175,  174,  175,   23,   21,  234,
103817  /*   560 */   174,  175,   22,   23,  239,   25,   25,   68,   69,   70,
103818  /*   570 */    71,   72,   73,   74,   75,   76,   77,   78,   79,   80,
103819  /*   580 */   205,   82,   83,   84,   85,   86,   87,   88,   89,   90,
103820  /*   590 */    91,   92,   19,   22,   23,  216,   23,  222,  223,  224,
103821  /*   600 */    63,  220,   35,  150,  150,  163,  220,   67,  166,  167,
103822  /*   610 */   168,  150,  169,  170,  171,  161,  162,   25,  165,  165,
103823  /*   620 */   150,  113,   49,   50,   25,  117,  165,  174,  175,   35,
103824  /*   630 */     7,    8,    9,  160,  160,  165,  120,  100,   67,  247,
103825  /*   640 */   248,   68,   69,   70,   71,   72,   73,   74,   75,   76,
103826  /*   650 */    77,   78,   79,   80,  193,   82,   83,   84,   85,   86,
103827  /*   660 */    87,   88,   89,   90,   91,   92,   19,  194,  194,  150,
103828  /*   670 */   135,   24,  137,   35,  231,  138,  150,  129,  130,  206,
103829  /*   680 */   207,   30,   27,  213,  165,   34,  118,   95,    0,    1,
103830  /*   690 */     2,  165,  218,  174,  175,   50,   49,   50,   22,   48,
103831  /*   700 */   174,  175,   22,   23,   23,  244,  222,  223,  224,  166,
103832  /*   710 */   167,  168,  120,  239,   23,   68,   69,   70,   71,   72,
103833  /*   720 */    73,   74,   75,   76,   77,   78,   79,   80,  150,   82,
103834  /*   730 */    83,   84,   85,   86,   87,   88,   89,   90,   91,   92,
103835  /*   740 */    19,  150,  173,  165,  181,  182,   24,   67,   26,  104,
103836  /*   750 */   181,  188,  174,  175,  150,   39,  165,  150,   52,  150,
103837  /*   760 */   150,  150,  150,  144,  145,  174,  175,  249,  250,  165,
103838  /*   770 */    49,   50,  165,   52,  165,  165,  165,  165,  174,  175,
103839  /*   780 */    29,  174,  175,  174,  175,  174,  175,  160,   22,   68,
103840  /*   790 */    69,   70,   71,   72,   73,   74,   75,   76,   77,   78,
103841  /*   800 */    79,   80,  150,   82,   83,   84,   85,   86,   87,   88,
103842  /*   810 */    89,   90,   91,   92,   19,  150,   94,  165,  150,  150,
103843  /*   820 */   160,  194,  150,  213,  160,   52,  174,  175,   23,   23,
103844  /*   830 */   165,   25,   22,  165,  165,  150,  150,  165,   52,  174,
103845  /*   840 */   175,   22,  174,  175,   49,   50,  174,  175,  190,  191,
103846  /*   850 */   165,  165,  240,   23,  194,   25,  187,  109,  194,  174,
103847  /*   860 */   175,  190,  191,   68,   69,   70,   71,   72,   73,   74,
103848  /*   870 */    75,   76,   77,   78,   79,   80,  150,   82,   83,   84,
103849  /*   880 */    85,   86,   87,   88,   89,   90,   91,   92,   19,  150,
103850  /*   890 */    22,  165,  150,   23,  150,   25,  150,  166,   91,   92,
103851  /*   900 */   174,  175,   22,  217,  165,  150,  102,  165,  150,  165,
103852  /*   910 */   150,  165,  150,  174,  175,   19,  174,  175,   49,   50,
103853  /*   920 */   165,   86,   87,  165,   23,  165,   25,  165,   24,  174,
103854  /*   930 */   175,  187,  174,  175,  174,  175,  205,   68,   69,   70,
103855  /*   940 */    71,   72,   73,   74,   75,   76,   77,   78,   79,   80,
103856  /*   950 */   150,   82,   83,   84,   85,   86,   87,   88,   89,   90,
103857  /*   960 */    91,   92,   19,  150,  150,  165,  150,  150,  166,   23,
103858  /*   970 */   150,   25,  160,   20,  174,  175,    1,    2,  165,  165,
103859  /*   980 */   104,  165,  165,   43,  150,  165,  240,  150,   49,   50,
103860  /*   990 */   174,  175,   49,   50,   23,   23,   25,   25,   53,  165,
103861  /*  1000 */   187,  187,  165,   23,  187,   25,  194,  205,  174,  175,
103862  /*  1010 */    71,   72,   69,   70,   71,   72,   73,   74,   75,   76,
103863  /*  1020 */    77,   78,   79,   80,  150,   82,   83,   84,   85,   86,
103864  /*  1030 */    87,   88,   89,   90,   91,   92,   19,   98,  150,  165,
103865  /*  1040 */   150,  160,  150,   59,   25,   53,  104,   22,  174,  175,
103866  /*  1050 */   213,  138,    5,  165,    1,  165,  150,  165,  150,  150,
103867  /*  1060 */   240,  150,  174,  175,  174,  175,   49,   50,  118,  150,
103868  /*  1070 */    35,  165,   27,  165,  165,  194,  165,  108,  127,   76,
103869  /*  1080 */   174,  175,  174,  175,  165,  174,  175,   70,   71,   72,
103870  /*  1090 */    73,   74,   75,   76,   77,   78,   79,   80,  166,   82,
103871  /*  1100 */    83,   84,   85,   86,   87,   88,   89,   90,   91,   92,
103872  /*  1110 */    19,   20,  193,   22,  150,  150,  150,   26,   27,   76,
103873  /*  1120 */   150,   22,    1,  150,  119,  121,  217,   20,   37,  165,
103874  /*  1130 */   165,  165,   16,   19,   20,  165,   22,  205,  165,  119,
103875  /*  1140 */    26,   27,  108,  128,  150,  150,  150,   56,  150,   22,
103876  /*  1150 */   150,   37,  150,  127,  160,   23,  150,   66,  193,  165,
103877  /*  1160 */   165,  165,   16,  165,   23,  165,  150,  165,  174,  175,
103878  /*  1170 */    56,  165,  150,   65,  174,  175,   15,   86,   87,   88,
103879  /*  1180 */    66,  165,  140,  150,   93,   94,   95,  165,  194,   98,
103880  /*  1190 */   174,  175,   22,    3,  164,  193,  174,  175,  165,  150,
103881  /*  1200 */    86,   87,    4,  180,  150,  248,  251,   93,   94,   95,
103882  /*  1210 */   216,  180,   98,  251,  165,  221,  150,  149,    6,  165,
103883  /*  1220 */   129,  130,  131,  132,  133,  134,  193,  150,  174,  175,
103884  /*  1230 */   116,  165,   19,   20,  150,   22,  149,  151,  150,   26,
103885  /*  1240 */    27,  149,  165,  129,  130,  131,  132,  133,  134,  165,
103886  /*  1250 */    37,  174,  175,  165,  149,   19,   20,   13,   22,  150,
103887  /*  1260 */   150,  150,   26,   27,  146,  147,  151,  150,   25,   56,
103888  /*  1270 */   152,  159,  154,   37,  165,  165,  165,  193,  160,   66,
103889  /*  1280 */   116,  193,  165,  174,  175,  174,  175,  194,  199,  150,
103890  /*  1290 */   200,  126,   56,  124,  123,  150,  201,  122,  150,   86,
103891  /*  1300 */    87,  150,   66,  193,  165,  202,   93,   94,   95,  150,
103892  /*  1310 */   165,   98,  194,  165,  125,   22,  165,  150,  150,   26,
103893  /*  1320 */    27,  135,   86,   87,  165,  174,  175,  203,  226,   93,
103894  /*  1330 */    94,   95,  165,  165,   98,  150,  218,  150,  193,  157,
103895  /*  1340 */   118,  157,  129,  130,  131,  132,  133,  134,    5,  104,
103896  /*  1350 */   165,  211,  165,   10,   11,   12,   13,   14,  150,   66,
103897  /*  1360 */    17,  174,  175,  210,  246,  129,  130,  131,  132,  133,
103898  /*  1370 */   134,  150,  210,  165,   31,  121,   33,  150,  150,   86,
103899  /*  1380 */    87,  176,  174,  175,  150,   42,  165,   94,  211,  210,
103900  /*  1390 */   150,   98,  165,  165,  211,  174,  175,  150,   55,  165,
103901  /*  1400 */    57,  150,  174,  175,   61,  165,  150,   64,  174,  175,
103902  /*  1410 */   150,  150,  165,  150,  174,  175,  165,  104,  150,  184,
103903  /*  1420 */   150,  165,  129,  130,  131,  165,  165,  150,  165,  150,
103904  /*  1430 */   150,  176,  150,  165,   47,  165,  150,  150,  176,  103,
103905  /*  1440 */   150,   22,  165,  178,  165,  165,  179,  165,  105,  106,
103906  /*  1450 */   107,  165,  165,  229,  111,  165,   92,  176,  229,  116,
103907  /*  1460 */   184,  176,  179,  156,  176,  176,   18,  157,  156,  237,
103908  /*  1470 */    45,  157,  156,  135,  157,  157,  238,  156,   68,  157,
103909  /*  1480 */   189,  189,  139,  219,   22,  157,   18,  192,  192,  192,
103910  /*  1490 */   192,  189,  219,  199,  157,  242,   40,  157,  199,  242,
103911  /*  1500 */   153,  157,   38,  245,  196,  166,  232,  198,  177,  177,
103912  /*  1510 */   232,  227,  209,  178,  166,  182,  166,  148,  177,  177,
103913  /*  1520 */   209,  196,  177,  199,  209,  199,  166,  208,   92,  195,
103914  /*  1530 */   174,  174,  183,  252,  183,  183,  252,  191,  252,  235,
103915  /*  1540 */   186,  241,  241,  252,  186,  252,  252,  252,  252,  252,
103916  /*  1550 */   252,  252,  252,  252,  252,  252,  236,
103917 };
103918 #define YY_SHIFT_USE_DFLT (-74)
103919 #define YY_SHIFT_COUNT (418)
103920 #define YY_SHIFT_MIN   (-73)
103921 #define YY_SHIFT_MAX   (1468)
103922 static const short yy_shift_ofst[] = {
103923  /*     0 */   975, 1114, 1343, 1114, 1213, 1213,   90,   90,    0,  -19,
103924  /*    10 */  1213, 1213, 1213, 1213, 1213,  345,  445,  721, 1091, 1213,
103925  /*    20 */  1213, 1213, 1213, 1213, 1213, 1213, 1213, 1213, 1213, 1213,
103926  /*    30 */  1213, 1213, 1213, 1213, 1213, 1213, 1213, 1213, 1213, 1213,
103927  /*    40 */  1213, 1213, 1213, 1213, 1213, 1213, 1213, 1236, 1213, 1213,
103928  /*    50 */  1213, 1213, 1213, 1213, 1213, 1213, 1213, 1213, 1213, 1213,
103929  /*    60 */  1213,  199,  445,  445,  835,  835,  365, 1164,   55,  647,
103930  /*    70 */   573,  499,  425,  351,  277,  203,  129,  795,  795,  795,
103931  /*    80 */   795,  795,  795,  795,  795,  795,  795,  795,  795,  795,
103932  /*    90 */   795,  795,  795,  795,  795,  869,  795,  943, 1017, 1017,
103933  /*   100 */   -69,  -45,  -45,  -45,  -45,  -45,   -1,   58,  138,  100,
103934  /*   110 */   445,  445,  445,  445,  445,  445,  445,  445,  445,  445,
103935  /*   120 */   445,  445,  445,  445,  445,  445,  537,  438,  445,  445,
103936  /*   130 */   445,  445,  445,  365,  807, 1436,  -74,  -74,  -74, 1293,
103937  /*   140 */    73,  434,  434,  311,  314,  290,  283,  286,  540,  467,
103938  /*   150 */   445,  445,  445,  445,  445,  445,  445,  445,  445,  445,
103939  /*   160 */   445,  445,  445,  445,  445,  445,  445,  445,  445,  445,
103940  /*   170 */   445,  445,  445,  445,  445,  445,  445,  445,  445,  445,
103941  /*   180 */   445,  445,   65,  722,  722,  722,  688,  266, 1164, 1164,
103942  /*   190 */  1164,  -74,  -74,  -74,  136,  168,  168,  234,  360,  360,
103943  /*   200 */   360,  430,  372,  435,  352,  278,  126,  -36,  -36,  -36,
103944  /*   210 */   -36,  421,  651,  -36,  -36,  592,  292,  212,  623,  158,
103945  /*   220 */   204,  204,  505,  158,  505,  144,  365,  154,  365,  154,
103946  /*   230 */   645,  154,  204,  154,  154,  535,  548,  548,  365,  387,
103947  /*   240 */   508,  233, 1464, 1222, 1222, 1456, 1456, 1222, 1462, 1410,
103948  /*   250 */  1165, 1468, 1468, 1468, 1468, 1222, 1165, 1462, 1410, 1410,
103949  /*   260 */  1222, 1448, 1338, 1425, 1222, 1222, 1448, 1222, 1448, 1222,
103950  /*   270 */  1448, 1419, 1313, 1313, 1313, 1387, 1364, 1364, 1419, 1313,
103951  /*   280 */  1336, 1313, 1387, 1313, 1313, 1254, 1245, 1254, 1245, 1254,
103952  /*   290 */  1245, 1222, 1222, 1186, 1189, 1175, 1169, 1171, 1165, 1164,
103953  /*   300 */  1243, 1244, 1244, 1212, 1212, 1212, 1212,  -74,  -74,  -74,
103954  /*   310 */   -74,  -74,  -74,  939,  104,  680,  571,  327,    1,  980,
103955  /*   320 */    26,  972,  971,  946,  901,  870,  830,  806,   54,   21,
103956  /*   330 */   -73,  510,  242, 1198, 1190, 1170, 1042, 1161, 1108, 1146,
103957  /*   340 */  1141, 1132, 1015, 1127, 1026, 1034, 1020, 1107, 1004, 1116,
103958  /*   350 */  1121, 1005, 1099,  951, 1043, 1003,  969, 1045, 1035,  950,
103959  /*   360 */  1053, 1047, 1025,  942,  913,  992, 1019,  945,  984,  940,
103960  /*   370 */   876,  904,  953,  896,  748,  804,  880,  786,  868,  819,
103961  /*   380 */   805,  810,  773,  751,  766,  706,  716,  691,  681,  568,
103962  /*   390 */   655,  638,  676,  516,  541,  594,  599,  567,  541,  534,
103963  /*   400 */   507,  527,  498,  523,  466,  382,  409,  384,  357,    6,
103964  /*   410 */   240,  224,  143,   62,   18,   71,   39,    9,    5,
103965 };
103966 #define YY_REDUCE_USE_DFLT (-142)
103967 #define YY_REDUCE_COUNT (312)
103968 #define YY_REDUCE_MIN   (-141)
103969 #define YY_REDUCE_MAX   (1369)
103970 static const short yy_reduce_ofst[] = {
103971  /*     0 */  -141,  994, 1118,  223,  157,  -53,   93,   89,   83,  375,
103972  /*    10 */   386,  381,  379,  308,  295,  325,  -47,   27, 1240, 1234,
103973  /*    20 */  1228, 1221, 1208, 1187, 1151, 1111, 1109, 1077, 1054, 1022,
103974  /*    30 */  1016, 1000,  911,  908,  906,  890,  888,  874,  834,  816,
103975  /*    40 */   800,  760,  758,  755,  742,  739,  726,  685,  672,  668,
103976  /*    50 */   665,  652,  611,  609,  607,  604,  591,  578,  526,  519,
103977  /*    60 */   453,  474,  454,  461,  443,  245,  442,  473,  484,  484,
103978  /*    70 */   484,  484,  484,  484,  484,  484,  484,  484,  484,  484,
103979  /*    80 */   484,  484,  484,  484,  484,  484,  484,  484,  484,  484,
103980  /*    90 */   484,  484,  484,  484,  484,  484,  484,  484,  484,  484,
103981  /*   100 */   484,  484,  484,  484,  484,  484,  484,  130,  484,  484,
103982  /*   110 */  1145,  909, 1110, 1088, 1084, 1033, 1002,  965,  820,  837,
103983  /*   120 */   746,  686,  612,  817,  610,  919,  221,  563,  814,  813,
103984  /*   130 */   744,  669,  470,  543,  484,  484,  484,  484,  484,  291,
103985  /*   140 */   569,  671,  658,  970, 1290, 1287, 1286, 1282,  518,  518,
103986  /*   150 */  1280, 1279, 1277, 1270, 1268, 1263, 1261, 1260, 1256, 1251,
103987  /*   160 */  1247, 1227, 1185, 1168, 1167, 1159, 1148, 1139, 1117, 1066,
103988  /*   170 */  1049, 1006,  998,  996,  995,  973,  970,  966,  964,  892,
103989  /*   180 */   762,  -52,  881,  932,  802,  731,  619,  812,  664,  660,
103990  /*   190 */   627,  392,  331,  124, 1358, 1357, 1356, 1354, 1352, 1351,
103991  /*   200 */  1349, 1319, 1334, 1346, 1334, 1334, 1334, 1334, 1334, 1334,
103992  /*   210 */  1334, 1320, 1304, 1334, 1334, 1319, 1360, 1325, 1369, 1326,
103993  /*   220 */  1315, 1311, 1301, 1324, 1300, 1335, 1350, 1345, 1348, 1342,
103994  /*   230 */  1333, 1341, 1303, 1332, 1331, 1284, 1278, 1274, 1339, 1309,
103995  /*   240 */  1308, 1347, 1258, 1344, 1340, 1257, 1253, 1337, 1273, 1302,
103996  /*   250 */  1299, 1298, 1297, 1296, 1295, 1328, 1294, 1264, 1292, 1291,
103997  /*   260 */  1322, 1321, 1238, 1232, 1318, 1317, 1316, 1314, 1312, 1310,
103998  /*   270 */  1307, 1283, 1289, 1288, 1285, 1276, 1229, 1224, 1267, 1281,
103999  /*   280 */  1265, 1262, 1235, 1255, 1205, 1183, 1179, 1177, 1162, 1140,
104000  /*   290 */  1153, 1184, 1182, 1102, 1124, 1103, 1095, 1090, 1089, 1093,
104001  /*   300 */  1112, 1115, 1086, 1105, 1092, 1087, 1068,  962,  955,  957,
104002  /*   310 */  1031, 1023, 1030,
104003 };
104004 static const YYACTIONTYPE yy_default[] = {
104005  /*     0 */   635,  870,  959,  959,  959,  870,  899,  899,  959,  759,
104006  /*    10 */   959,  959,  959,  959,  868,  959,  959,  933,  959,  959,
104007  /*    20 */   959,  959,  959,  959,  959,  959,  959,  959,  959,  959,
104008  /*    30 */   959,  959,  959,  959,  959,  959,  959,  959,  959,  959,
104009  /*    40 */   959,  959,  959,  959,  959,  959,  959,  959,  959,  959,
104010  /*    50 */   959,  959,  959,  959,  959,  959,  959,  959,  959,  959,
104011  /*    60 */   959,  959,  959,  959,  899,  899,  674,  763,  794,  959,
104012  /*    70 */   959,  959,  959,  959,  959,  959,  959,  932,  934,  809,
104013  /*    80 */   808,  802,  801,  912,  774,  799,  792,  785,  796,  871,
104014  /*    90 */   864,  865,  863,  867,  872,  959,  795,  831,  848,  830,
104015  /*   100 */   842,  847,  854,  846,  843,  833,  832,  666,  834,  835,
104016  /*   110 */   959,  959,  959,  959,  959,  959,  959,  959,  959,  959,
104017  /*   120 */   959,  959,  959,  959,  959,  959,  661,  728,  959,  959,
104018  /*   130 */   959,  959,  959,  959,  836,  837,  851,  850,  849,  959,
104019  /*   140 */   959,  959,  959,  959,  959,  959,  959,  959,  959,  959,
104020  /*   150 */   959,  939,  937,  959,  883,  959,  959,  959,  959,  959,
104021  /*   160 */   959,  959,  959,  959,  959,  959,  959,  959,  959,  959,
104022  /*   170 */   959,  959,  959,  959,  959,  959,  959,  959,  959,  959,
104023  /*   180 */   959,  641,  959,  759,  759,  759,  635,  959,  959,  959,
104024  /*   190 */   959,  951,  763,  753,  719,  959,  959,  959,  959,  959,
104025  /*   200 */   959,  959,  959,  959,  959,  959,  959,  804,  742,  922,
104026  /*   210 */   924,  959,  905,  740,  663,  761,  676,  751,  643,  798,
104027  /*   220 */   776,  776,  917,  798,  917,  700,  959,  788,  959,  788,
104028  /*   230 */   697,  788,  776,  788,  788,  866,  959,  959,  959,  760,
104029  /*   240 */   751,  959,  944,  767,  767,  936,  936,  767,  810,  732,
104030  /*   250 */   798,  739,  739,  739,  739,  767,  798,  810,  732,  732,
104031  /*   260 */   767,  658,  911,  909,  767,  767,  658,  767,  658,  767,
104032  /*   270 */   658,  876,  730,  730,  730,  715,  880,  880,  876,  730,
104033  /*   280 */   700,  730,  715,  730,  730,  780,  775,  780,  775,  780,
104034  /*   290 */   775,  767,  767,  959,  793,  781,  791,  789,  798,  959,
104035  /*   300 */   718,  651,  651,  640,  640,  640,  640,  956,  956,  951,
104036  /*   310 */   702,  702,  684,  959,  959,  959,  959,  959,  959,  959,
104037  /*   320 */   885,  959,  959,  959,  959,  959,  959,  959,  959,  959,
104038  /*   330 */   959,  959,  959,  959,  636,  946,  959,  959,  943,  959,
104039  /*   340 */   959,  959,  959,  959,  959,  959,  959,  959,  959,  959,
104040  /*   350 */   959,  959,  959,  959,  959,  959,  959,  959,  959,  915,
104041  /*   360 */   959,  959,  959,  959,  959,  959,  908,  907,  959,  959,
104042  /*   370 */   959,  959,  959,  959,  959,  959,  959,  959,  959,  959,
104043  /*   380 */   959,  959,  959,  959,  959,  959,  959,  959,  959,  959,
104044  /*   390 */   959,  959,  959,  959,  790,  959,  782,  959,  869,  959,
104045  /*   400 */   959,  959,  959,  959,  959,  959,  959,  959,  959,  745,
104046  /*   410 */   819,  959,  818,  822,  817,  668,  959,  649,  959,  632,
104047  /*   420 */   637,  955,  958,  957,  954,  953,  952,  947,  945,  942,
104048  /*   430 */   941,  940,  938,  935,  931,  889,  887,  894,  893,  892,
104049  /*   440 */   891,  890,  888,  886,  884,  805,  803,  800,  797,  930,
104050  /*   450 */   882,  741,  738,  737,  657,  948,  914,  923,  921,  811,
104051  /*   460 */   920,  919,  918,  916,  913,  900,  807,  806,  733,  874,
104052  /*   470 */   873,  660,  904,  903,  902,  906,  910,  901,  769,  659,
104053  /*   480 */   656,  665,  722,  721,  729,  727,  726,  725,  724,  723,
104054  /*   490 */   720,  667,  675,  686,  714,  699,  698,  879,  881,  878,
104055  /*   500 */   877,  707,  706,  712,  711,  710,  709,  708,  705,  704,
104056  /*   510 */   703,  696,  695,  701,  694,  717,  716,  713,  693,  736,
104057  /*   520 */   735,  734,  731,  692,  691,  690,  822,  689,  688,  828,
104058  /*   530 */   827,  815,  858,  756,  755,  754,  766,  765,  778,  777,
104059  /*   540 */   813,  812,  779,  764,  758,  757,  773,  772,  771,  770,
104060  /*   550 */   762,  752,  784,  787,  786,  783,  860,  768,  857,  929,
104061  /*   560 */   928,  927,  926,  925,  862,  861,  829,  826,  679,  680,
104062  /*   570 */   898,  896,  897,  895,  682,  681,  678,  677,  859,  747,
104063  /*   580 */   746,  855,  852,  844,  840,  856,  853,  845,  841,  839,
104064  /*   590 */   838,  824,  823,  821,  820,  816,  825,  670,  748,  744,
104065  /*   600 */   743,  814,  750,  749,  687,  685,  683,  664,  662,  655,
104066  /*   610 */   653,  652,  654,  650,  648,  647,  646,  645,  644,  673,
104067  /*   620 */   672,  671,  669,  668,  642,  639,  638,  634,  633,  631,
104068 };
104069 
104070 /* The next table maps tokens into fallback tokens.  If a construct
104071 ** like the following:
104072 **
104073 **      %fallback ID X Y Z.
104074 **
104075 ** appears in the grammar, then ID becomes a fallback token for X, Y,
104076 ** and Z.  Whenever one of the tokens X, Y, or Z is input to the parser
104077 ** but it does not parse, the type of the token is changed to ID and
104078 ** the parse is retried before an error is thrown.
104079 */
104080 #ifdef YYFALLBACK
104081 static const YYCODETYPE yyFallback[] = {
104082     0,  /*          $ => nothing */
104083     0,  /*       SEMI => nothing */
104084    26,  /*    EXPLAIN => ID */
104085    26,  /*      QUERY => ID */
104086    26,  /*       PLAN => ID */
104087    26,  /*      BEGIN => ID */
104088     0,  /* TRANSACTION => nothing */
104089    26,  /*   DEFERRED => ID */
104090    26,  /*  IMMEDIATE => ID */
104091    26,  /*  EXCLUSIVE => ID */
104092     0,  /*     COMMIT => nothing */
104093    26,  /*        END => ID */
104094    26,  /*   ROLLBACK => ID */
104095    26,  /*  SAVEPOINT => ID */
104096    26,  /*    RELEASE => ID */
104097     0,  /*         TO => nothing */
104098     0,  /*      TABLE => nothing */
104099     0,  /*     CREATE => nothing */
104100    26,  /*         IF => ID */
104101     0,  /*        NOT => nothing */
104102     0,  /*     EXISTS => nothing */
104103    26,  /*       TEMP => ID */
104104     0,  /*         LP => nothing */
104105     0,  /*         RP => nothing */
104106     0,  /*         AS => nothing */
104107     0,  /*      COMMA => nothing */
104108     0,  /*         ID => nothing */
104109     0,  /*    INDEXED => nothing */
104110    26,  /*      ABORT => ID */
104111    26,  /*     ACTION => ID */
104112    26,  /*      AFTER => ID */
104113    26,  /*    ANALYZE => ID */
104114    26,  /*        ASC => ID */
104115    26,  /*     ATTACH => ID */
104116    26,  /*     BEFORE => ID */
104117    26,  /*         BY => ID */
104118    26,  /*    CASCADE => ID */
104119    26,  /*       CAST => ID */
104120    26,  /*   COLUMNKW => ID */
104121    26,  /*   CONFLICT => ID */
104122    26,  /*   DATABASE => ID */
104123    26,  /*       DESC => ID */
104124    26,  /*     DETACH => ID */
104125    26,  /*       EACH => ID */
104126    26,  /*       FAIL => ID */
104127    26,  /*        FOR => ID */
104128    26,  /*     IGNORE => ID */
104129    26,  /*  INITIALLY => ID */
104130    26,  /*    INSTEAD => ID */
104131    26,  /*    LIKE_KW => ID */
104132    26,  /*      MATCH => ID */
104133    26,  /*         NO => ID */
104134    26,  /*        KEY => ID */
104135    26,  /*         OF => ID */
104136    26,  /*     OFFSET => ID */
104137    26,  /*     PRAGMA => ID */
104138    26,  /*      RAISE => ID */
104139    26,  /*    REPLACE => ID */
104140    26,  /*   RESTRICT => ID */
104141    26,  /*        ROW => ID */
104142    26,  /*    TRIGGER => ID */
104143    26,  /*     VACUUM => ID */
104144    26,  /*       VIEW => ID */
104145    26,  /*    VIRTUAL => ID */
104146    26,  /*    REINDEX => ID */
104147    26,  /*     RENAME => ID */
104148    26,  /*   CTIME_KW => ID */
104149 };
104150 #endif /* YYFALLBACK */
104151 
104152 /* The following structure represents a single element of the
104153 ** parser's stack.  Information stored includes:
104154 **
104155 **   +  The state number for the parser at this level of the stack.
104156 **
104157 **   +  The value of the token stored at this level of the stack.
104158 **      (In other words, the "major" token.)
104159 **
104160 **   +  The semantic value stored at this level of the stack.  This is
104161 **      the information used by the action routines in the grammar.
104162 **      It is sometimes called the "minor" token.
104163 */
104164 struct yyStackEntry {
104165   YYACTIONTYPE stateno;  /* The state-number */
104166   YYCODETYPE major;      /* The major token value.  This is the code
104167                          ** number for the token at this stack level */
104168   YYMINORTYPE minor;     /* The user-supplied minor token value.  This
104169                          ** is the value of the token  */
104170 };
104171 typedef struct yyStackEntry yyStackEntry;
104172 
104173 /* The state of the parser is completely contained in an instance of
104174 ** the following structure */
104175 struct yyParser {
104176   int yyidx;                    /* Index of top element in stack */
104177 #ifdef YYTRACKMAXSTACKDEPTH
104178   int yyidxMax;                 /* Maximum value of yyidx */
104179 #endif
104180   int yyerrcnt;                 /* Shifts left before out of the error */
104181   sqlite3ParserARG_SDECL                /* A place to hold %extra_argument */
104182 #if YYSTACKDEPTH<=0
104183   int yystksz;                  /* Current side of the stack */
104184   yyStackEntry *yystack;        /* The parser's stack */
104185 #else
104186   yyStackEntry yystack[YYSTACKDEPTH];  /* The parser's stack */
104187 #endif
104188 };
104189 typedef struct yyParser yyParser;
104190 
104191 #ifndef NDEBUG
104192 static FILE *yyTraceFILE = 0;
104193 static char *yyTracePrompt = 0;
104194 #endif /* NDEBUG */
104195 
104196 #ifndef NDEBUG
104197 /*
104198 ** Turn parser tracing on by giving a stream to which to write the trace
104199 ** and a prompt to preface each trace message.  Tracing is turned off
104200 ** by making either argument NULL
104201 **
104202 ** Inputs:
104203 ** <ul>
104204 ** <li> A FILE* to which trace output should be written.
104205 **      If NULL, then tracing is turned off.
104206 ** <li> A prefix string written at the beginning of every
104207 **      line of trace output.  If NULL, then tracing is
104208 **      turned off.
104209 ** </ul>
104210 **
104211 ** Outputs:
104212 ** None.
104213 */
104214 SQLITE_PRIVATE void sqlite3ParserTrace(FILE *TraceFILE, char *zTracePrompt){
104215   yyTraceFILE = TraceFILE;
104216   yyTracePrompt = zTracePrompt;
104217   if( yyTraceFILE==0 ) yyTracePrompt = 0;
104218   else if( yyTracePrompt==0 ) yyTraceFILE = 0;
104219 }
104220 #endif /* NDEBUG */
104221 
104222 #ifndef NDEBUG
104223 /* For tracing shifts, the names of all terminals and nonterminals
104224 ** are required.  The following table supplies these names */
104225 static const char *const yyTokenName[] = {
104226   "$",             "SEMI",          "EXPLAIN",       "QUERY",
104227   "PLAN",          "BEGIN",         "TRANSACTION",   "DEFERRED",
104228   "IMMEDIATE",     "EXCLUSIVE",     "COMMIT",        "END",
104229   "ROLLBACK",      "SAVEPOINT",     "RELEASE",       "TO",
104230   "TABLE",         "CREATE",        "IF",            "NOT",
104231   "EXISTS",        "TEMP",          "LP",            "RP",
104232   "AS",            "COMMA",         "ID",            "INDEXED",
104233   "ABORT",         "ACTION",        "AFTER",         "ANALYZE",
104234   "ASC",           "ATTACH",        "BEFORE",        "BY",
104235   "CASCADE",       "CAST",          "COLUMNKW",      "CONFLICT",
104236   "DATABASE",      "DESC",          "DETACH",        "EACH",
104237   "FAIL",          "FOR",           "IGNORE",        "INITIALLY",
104238   "INSTEAD",       "LIKE_KW",       "MATCH",         "NO",
104239   "KEY",           "OF",            "OFFSET",        "PRAGMA",
104240   "RAISE",         "REPLACE",       "RESTRICT",      "ROW",
104241   "TRIGGER",       "VACUUM",        "VIEW",          "VIRTUAL",
104242   "REINDEX",       "RENAME",        "CTIME_KW",      "ANY",
104243   "OR",            "AND",           "IS",            "BETWEEN",
104244   "IN",            "ISNULL",        "NOTNULL",       "NE",
104245   "EQ",            "GT",            "LE",            "LT",
104246   "GE",            "ESCAPE",        "BITAND",        "BITOR",
104247   "LSHIFT",        "RSHIFT",        "PLUS",          "MINUS",
104248   "STAR",          "SLASH",         "REM",           "CONCAT",
104249   "COLLATE",       "BITNOT",        "STRING",        "JOIN_KW",
104250   "CONSTRAINT",    "DEFAULT",       "NULL",          "PRIMARY",
104251   "UNIQUE",        "CHECK",         "REFERENCES",    "AUTOINCR",
104252   "ON",            "INSERT",        "DELETE",        "UPDATE",
104253   "SET",           "DEFERRABLE",    "FOREIGN",       "DROP",
104254   "UNION",         "ALL",           "EXCEPT",        "INTERSECT",
104255   "SELECT",        "DISTINCT",      "DOT",           "FROM",
104256   "JOIN",          "USING",         "ORDER",         "GROUP",
104257   "HAVING",        "LIMIT",         "WHERE",         "INTO",
104258   "VALUES",        "INTEGER",       "FLOAT",         "BLOB",
104259   "REGISTER",      "VARIABLE",      "CASE",          "WHEN",
104260   "THEN",          "ELSE",          "INDEX",         "ALTER",
104261   "ADD",           "error",         "input",         "cmdlist",
104262   "ecmd",          "explain",       "cmdx",          "cmd",
104263   "transtype",     "trans_opt",     "nm",            "savepoint_opt",
104264   "create_table",  "create_table_args",  "createkw",      "temp",
104265   "ifnotexists",   "dbnm",          "columnlist",    "conslist_opt",
104266   "select",        "column",        "columnid",      "type",
104267   "carglist",      "id",            "ids",           "typetoken",
104268   "typename",      "signed",        "plus_num",      "minus_num",
104269   "carg",          "ccons",         "term",          "expr",
104270   "onconf",        "sortorder",     "autoinc",       "idxlist_opt",
104271   "refargs",       "defer_subclause",  "refarg",        "refact",
104272   "init_deferred_pred_opt",  "conslist",      "tcons",         "idxlist",
104273   "defer_subclause_opt",  "orconf",        "resolvetype",   "raisetype",
104274   "ifexists",      "fullname",      "oneselect",     "multiselect_op",
104275   "distinct",      "selcollist",    "from",          "where_opt",
104276   "groupby_opt",   "having_opt",    "orderby_opt",   "limit_opt",
104277   "sclp",          "as",            "seltablist",    "stl_prefix",
104278   "joinop",        "indexed_opt",   "on_opt",        "using_opt",
104279   "joinop2",       "inscollist",    "sortlist",      "sortitem",
104280   "nexprlist",     "setlist",       "insert_cmd",    "inscollist_opt",
104281   "itemlist",      "exprlist",      "likeop",        "between_op",
104282   "in_op",         "case_operand",  "case_exprlist",  "case_else",
104283   "uniqueflag",    "collate",       "nmnum",         "plus_opt",
104284   "number",        "trigger_decl",  "trigger_cmd_list",  "trigger_time",
104285   "trigger_event",  "foreach_clause",  "when_clause",   "trigger_cmd",
104286   "trnm",          "tridxby",       "database_kw_opt",  "key_opt",
104287   "add_column_fullname",  "kwcolumn_opt",  "create_vtab",   "vtabarglist",
104288   "vtabarg",       "vtabargtoken",  "lp",            "anylist",
104289 };
104290 #endif /* NDEBUG */
104291 
104292 #ifndef NDEBUG
104293 /* For tracing reduce actions, the names of all rules are required.
104294 */
104295 static const char *const yyRuleName[] = {
104296  /*   0 */ "input ::= cmdlist",
104297  /*   1 */ "cmdlist ::= cmdlist ecmd",
104298  /*   2 */ "cmdlist ::= ecmd",
104299  /*   3 */ "ecmd ::= SEMI",
104300  /*   4 */ "ecmd ::= explain cmdx SEMI",
104301  /*   5 */ "explain ::=",
104302  /*   6 */ "explain ::= EXPLAIN",
104303  /*   7 */ "explain ::= EXPLAIN QUERY PLAN",
104304  /*   8 */ "cmdx ::= cmd",
104305  /*   9 */ "cmd ::= BEGIN transtype trans_opt",
104306  /*  10 */ "trans_opt ::=",
104307  /*  11 */ "trans_opt ::= TRANSACTION",
104308  /*  12 */ "trans_opt ::= TRANSACTION nm",
104309  /*  13 */ "transtype ::=",
104310  /*  14 */ "transtype ::= DEFERRED",
104311  /*  15 */ "transtype ::= IMMEDIATE",
104312  /*  16 */ "transtype ::= EXCLUSIVE",
104313  /*  17 */ "cmd ::= COMMIT trans_opt",
104314  /*  18 */ "cmd ::= END trans_opt",
104315  /*  19 */ "cmd ::= ROLLBACK trans_opt",
104316  /*  20 */ "savepoint_opt ::= SAVEPOINT",
104317  /*  21 */ "savepoint_opt ::=",
104318  /*  22 */ "cmd ::= SAVEPOINT nm",
104319  /*  23 */ "cmd ::= RELEASE savepoint_opt nm",
104320  /*  24 */ "cmd ::= ROLLBACK trans_opt TO savepoint_opt nm",
104321  /*  25 */ "cmd ::= create_table create_table_args",
104322  /*  26 */ "create_table ::= createkw temp TABLE ifnotexists nm dbnm",
104323  /*  27 */ "createkw ::= CREATE",
104324  /*  28 */ "ifnotexists ::=",
104325  /*  29 */ "ifnotexists ::= IF NOT EXISTS",
104326  /*  30 */ "temp ::= TEMP",
104327  /*  31 */ "temp ::=",
104328  /*  32 */ "create_table_args ::= LP columnlist conslist_opt RP",
104329  /*  33 */ "create_table_args ::= AS select",
104330  /*  34 */ "columnlist ::= columnlist COMMA column",
104331  /*  35 */ "columnlist ::= column",
104332  /*  36 */ "column ::= columnid type carglist",
104333  /*  37 */ "columnid ::= nm",
104334  /*  38 */ "id ::= ID",
104335  /*  39 */ "id ::= INDEXED",
104336  /*  40 */ "ids ::= ID|STRING",
104337  /*  41 */ "nm ::= id",
104338  /*  42 */ "nm ::= STRING",
104339  /*  43 */ "nm ::= JOIN_KW",
104340  /*  44 */ "type ::=",
104341  /*  45 */ "type ::= typetoken",
104342  /*  46 */ "typetoken ::= typename",
104343  /*  47 */ "typetoken ::= typename LP signed RP",
104344  /*  48 */ "typetoken ::= typename LP signed COMMA signed RP",
104345  /*  49 */ "typename ::= ids",
104346  /*  50 */ "typename ::= typename ids",
104347  /*  51 */ "signed ::= plus_num",
104348  /*  52 */ "signed ::= minus_num",
104349  /*  53 */ "carglist ::= carglist carg",
104350  /*  54 */ "carglist ::=",
104351  /*  55 */ "carg ::= CONSTRAINT nm ccons",
104352  /*  56 */ "carg ::= ccons",
104353  /*  57 */ "ccons ::= DEFAULT term",
104354  /*  58 */ "ccons ::= DEFAULT LP expr RP",
104355  /*  59 */ "ccons ::= DEFAULT PLUS term",
104356  /*  60 */ "ccons ::= DEFAULT MINUS term",
104357  /*  61 */ "ccons ::= DEFAULT id",
104358  /*  62 */ "ccons ::= NULL onconf",
104359  /*  63 */ "ccons ::= NOT NULL onconf",
104360  /*  64 */ "ccons ::= PRIMARY KEY sortorder onconf autoinc",
104361  /*  65 */ "ccons ::= UNIQUE onconf",
104362  /*  66 */ "ccons ::= CHECK LP expr RP",
104363  /*  67 */ "ccons ::= REFERENCES nm idxlist_opt refargs",
104364  /*  68 */ "ccons ::= defer_subclause",
104365  /*  69 */ "ccons ::= COLLATE ids",
104366  /*  70 */ "autoinc ::=",
104367  /*  71 */ "autoinc ::= AUTOINCR",
104368  /*  72 */ "refargs ::=",
104369  /*  73 */ "refargs ::= refargs refarg",
104370  /*  74 */ "refarg ::= MATCH nm",
104371  /*  75 */ "refarg ::= ON INSERT refact",
104372  /*  76 */ "refarg ::= ON DELETE refact",
104373  /*  77 */ "refarg ::= ON UPDATE refact",
104374  /*  78 */ "refact ::= SET NULL",
104375  /*  79 */ "refact ::= SET DEFAULT",
104376  /*  80 */ "refact ::= CASCADE",
104377  /*  81 */ "refact ::= RESTRICT",
104378  /*  82 */ "refact ::= NO ACTION",
104379  /*  83 */ "defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt",
104380  /*  84 */ "defer_subclause ::= DEFERRABLE init_deferred_pred_opt",
104381  /*  85 */ "init_deferred_pred_opt ::=",
104382  /*  86 */ "init_deferred_pred_opt ::= INITIALLY DEFERRED",
104383  /*  87 */ "init_deferred_pred_opt ::= INITIALLY IMMEDIATE",
104384  /*  88 */ "conslist_opt ::=",
104385  /*  89 */ "conslist_opt ::= COMMA conslist",
104386  /*  90 */ "conslist ::= conslist COMMA tcons",
104387  /*  91 */ "conslist ::= conslist tcons",
104388  /*  92 */ "conslist ::= tcons",
104389  /*  93 */ "tcons ::= CONSTRAINT nm",
104390  /*  94 */ "tcons ::= PRIMARY KEY LP idxlist autoinc RP onconf",
104391  /*  95 */ "tcons ::= UNIQUE LP idxlist RP onconf",
104392  /*  96 */ "tcons ::= CHECK LP expr RP onconf",
104393  /*  97 */ "tcons ::= FOREIGN KEY LP idxlist RP REFERENCES nm idxlist_opt refargs defer_subclause_opt",
104394  /*  98 */ "defer_subclause_opt ::=",
104395  /*  99 */ "defer_subclause_opt ::= defer_subclause",
104396  /* 100 */ "onconf ::=",
104397  /* 101 */ "onconf ::= ON CONFLICT resolvetype",
104398  /* 102 */ "orconf ::=",
104399  /* 103 */ "orconf ::= OR resolvetype",
104400  /* 104 */ "resolvetype ::= raisetype",
104401  /* 105 */ "resolvetype ::= IGNORE",
104402  /* 106 */ "resolvetype ::= REPLACE",
104403  /* 107 */ "cmd ::= DROP TABLE ifexists fullname",
104404  /* 108 */ "ifexists ::= IF EXISTS",
104405  /* 109 */ "ifexists ::=",
104406  /* 110 */ "cmd ::= createkw temp VIEW ifnotexists nm dbnm AS select",
104407  /* 111 */ "cmd ::= DROP VIEW ifexists fullname",
104408  /* 112 */ "cmd ::= select",
104409  /* 113 */ "select ::= oneselect",
104410  /* 114 */ "select ::= select multiselect_op oneselect",
104411  /* 115 */ "multiselect_op ::= UNION",
104412  /* 116 */ "multiselect_op ::= UNION ALL",
104413  /* 117 */ "multiselect_op ::= EXCEPT|INTERSECT",
104414  /* 118 */ "oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderby_opt limit_opt",
104415  /* 119 */ "distinct ::= DISTINCT",
104416  /* 120 */ "distinct ::= ALL",
104417  /* 121 */ "distinct ::=",
104418  /* 122 */ "sclp ::= selcollist COMMA",
104419  /* 123 */ "sclp ::=",
104420  /* 124 */ "selcollist ::= sclp expr as",
104421  /* 125 */ "selcollist ::= sclp STAR",
104422  /* 126 */ "selcollist ::= sclp nm DOT STAR",
104423  /* 127 */ "as ::= AS nm",
104424  /* 128 */ "as ::= ids",
104425  /* 129 */ "as ::=",
104426  /* 130 */ "from ::=",
104427  /* 131 */ "from ::= FROM seltablist",
104428  /* 132 */ "stl_prefix ::= seltablist joinop",
104429  /* 133 */ "stl_prefix ::=",
104430  /* 134 */ "seltablist ::= stl_prefix nm dbnm as indexed_opt on_opt using_opt",
104431  /* 135 */ "seltablist ::= stl_prefix LP select RP as on_opt using_opt",
104432  /* 136 */ "seltablist ::= stl_prefix LP seltablist RP as on_opt using_opt",
104433  /* 137 */ "dbnm ::=",
104434  /* 138 */ "dbnm ::= DOT nm",
104435  /* 139 */ "fullname ::= nm dbnm",
104436  /* 140 */ "joinop ::= COMMA|JOIN",
104437  /* 141 */ "joinop ::= JOIN_KW JOIN",
104438  /* 142 */ "joinop ::= JOIN_KW nm JOIN",
104439  /* 143 */ "joinop ::= JOIN_KW nm nm JOIN",
104440  /* 144 */ "on_opt ::= ON expr",
104441  /* 145 */ "on_opt ::=",
104442  /* 146 */ "indexed_opt ::=",
104443  /* 147 */ "indexed_opt ::= INDEXED BY nm",
104444  /* 148 */ "indexed_opt ::= NOT INDEXED",
104445  /* 149 */ "using_opt ::= USING LP inscollist RP",
104446  /* 150 */ "using_opt ::=",
104447  /* 151 */ "orderby_opt ::=",
104448  /* 152 */ "orderby_opt ::= ORDER BY sortlist",
104449  /* 153 */ "sortlist ::= sortlist COMMA sortitem sortorder",
104450  /* 154 */ "sortlist ::= sortitem sortorder",
104451  /* 155 */ "sortitem ::= expr",
104452  /* 156 */ "sortorder ::= ASC",
104453  /* 157 */ "sortorder ::= DESC",
104454  /* 158 */ "sortorder ::=",
104455  /* 159 */ "groupby_opt ::=",
104456  /* 160 */ "groupby_opt ::= GROUP BY nexprlist",
104457  /* 161 */ "having_opt ::=",
104458  /* 162 */ "having_opt ::= HAVING expr",
104459  /* 163 */ "limit_opt ::=",
104460  /* 164 */ "limit_opt ::= LIMIT expr",
104461  /* 165 */ "limit_opt ::= LIMIT expr OFFSET expr",
104462  /* 166 */ "limit_opt ::= LIMIT expr COMMA expr",
104463  /* 167 */ "cmd ::= DELETE FROM fullname indexed_opt where_opt",
104464  /* 168 */ "where_opt ::=",
104465  /* 169 */ "where_opt ::= WHERE expr",
104466  /* 170 */ "cmd ::= UPDATE orconf fullname indexed_opt SET setlist where_opt",
104467  /* 171 */ "setlist ::= setlist COMMA nm EQ expr",
104468  /* 172 */ "setlist ::= nm EQ expr",
104469  /* 173 */ "cmd ::= insert_cmd INTO fullname inscollist_opt VALUES LP itemlist RP",
104470  /* 174 */ "cmd ::= insert_cmd INTO fullname inscollist_opt select",
104471  /* 175 */ "cmd ::= insert_cmd INTO fullname inscollist_opt DEFAULT VALUES",
104472  /* 176 */ "insert_cmd ::= INSERT orconf",
104473  /* 177 */ "insert_cmd ::= REPLACE",
104474  /* 178 */ "itemlist ::= itemlist COMMA expr",
104475  /* 179 */ "itemlist ::= expr",
104476  /* 180 */ "inscollist_opt ::=",
104477  /* 181 */ "inscollist_opt ::= LP inscollist RP",
104478  /* 182 */ "inscollist ::= inscollist COMMA nm",
104479  /* 183 */ "inscollist ::= nm",
104480  /* 184 */ "expr ::= term",
104481  /* 185 */ "expr ::= LP expr RP",
104482  /* 186 */ "term ::= NULL",
104483  /* 187 */ "expr ::= id",
104484  /* 188 */ "expr ::= JOIN_KW",
104485  /* 189 */ "expr ::= nm DOT nm",
104486  /* 190 */ "expr ::= nm DOT nm DOT nm",
104487  /* 191 */ "term ::= INTEGER|FLOAT|BLOB",
104488  /* 192 */ "term ::= STRING",
104489  /* 193 */ "expr ::= REGISTER",
104490  /* 194 */ "expr ::= VARIABLE",
104491  /* 195 */ "expr ::= expr COLLATE ids",
104492  /* 196 */ "expr ::= CAST LP expr AS typetoken RP",
104493  /* 197 */ "expr ::= ID LP distinct exprlist RP",
104494  /* 198 */ "expr ::= ID LP STAR RP",
104495  /* 199 */ "term ::= CTIME_KW",
104496  /* 200 */ "expr ::= expr AND expr",
104497  /* 201 */ "expr ::= expr OR expr",
104498  /* 202 */ "expr ::= expr LT|GT|GE|LE expr",
104499  /* 203 */ "expr ::= expr EQ|NE expr",
104500  /* 204 */ "expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr",
104501  /* 205 */ "expr ::= expr PLUS|MINUS expr",
104502  /* 206 */ "expr ::= expr STAR|SLASH|REM expr",
104503  /* 207 */ "expr ::= expr CONCAT expr",
104504  /* 208 */ "likeop ::= LIKE_KW",
104505  /* 209 */ "likeop ::= NOT LIKE_KW",
104506  /* 210 */ "likeop ::= MATCH",
104507  /* 211 */ "likeop ::= NOT MATCH",
104508  /* 212 */ "expr ::= expr likeop expr",
104509  /* 213 */ "expr ::= expr likeop expr ESCAPE expr",
104510  /* 214 */ "expr ::= expr ISNULL|NOTNULL",
104511  /* 215 */ "expr ::= expr NOT NULL",
104512  /* 216 */ "expr ::= expr IS expr",
104513  /* 217 */ "expr ::= expr IS NOT expr",
104514  /* 218 */ "expr ::= NOT expr",
104515  /* 219 */ "expr ::= BITNOT expr",
104516  /* 220 */ "expr ::= MINUS expr",
104517  /* 221 */ "expr ::= PLUS expr",
104518  /* 222 */ "between_op ::= BETWEEN",
104519  /* 223 */ "between_op ::= NOT BETWEEN",
104520  /* 224 */ "expr ::= expr between_op expr AND expr",
104521  /* 225 */ "in_op ::= IN",
104522  /* 226 */ "in_op ::= NOT IN",
104523  /* 227 */ "expr ::= expr in_op LP exprlist RP",
104524  /* 228 */ "expr ::= LP select RP",
104525  /* 229 */ "expr ::= expr in_op LP select RP",
104526  /* 230 */ "expr ::= expr in_op nm dbnm",
104527  /* 231 */ "expr ::= EXISTS LP select RP",
104528  /* 232 */ "expr ::= CASE case_operand case_exprlist case_else END",
104529  /* 233 */ "case_exprlist ::= case_exprlist WHEN expr THEN expr",
104530  /* 234 */ "case_exprlist ::= WHEN expr THEN expr",
104531  /* 235 */ "case_else ::= ELSE expr",
104532  /* 236 */ "case_else ::=",
104533  /* 237 */ "case_operand ::= expr",
104534  /* 238 */ "case_operand ::=",
104535  /* 239 */ "exprlist ::= nexprlist",
104536  /* 240 */ "exprlist ::=",
104537  /* 241 */ "nexprlist ::= nexprlist COMMA expr",
104538  /* 242 */ "nexprlist ::= expr",
104539  /* 243 */ "cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP idxlist RP",
104540  /* 244 */ "uniqueflag ::= UNIQUE",
104541  /* 245 */ "uniqueflag ::=",
104542  /* 246 */ "idxlist_opt ::=",
104543  /* 247 */ "idxlist_opt ::= LP idxlist RP",
104544  /* 248 */ "idxlist ::= idxlist COMMA nm collate sortorder",
104545  /* 249 */ "idxlist ::= nm collate sortorder",
104546  /* 250 */ "collate ::=",
104547  /* 251 */ "collate ::= COLLATE ids",
104548  /* 252 */ "cmd ::= DROP INDEX ifexists fullname",
104549  /* 253 */ "cmd ::= VACUUM",
104550  /* 254 */ "cmd ::= VACUUM nm",
104551  /* 255 */ "cmd ::= PRAGMA nm dbnm",
104552  /* 256 */ "cmd ::= PRAGMA nm dbnm EQ nmnum",
104553  /* 257 */ "cmd ::= PRAGMA nm dbnm LP nmnum RP",
104554  /* 258 */ "cmd ::= PRAGMA nm dbnm EQ minus_num",
104555  /* 259 */ "cmd ::= PRAGMA nm dbnm LP minus_num RP",
104556  /* 260 */ "nmnum ::= plus_num",
104557  /* 261 */ "nmnum ::= nm",
104558  /* 262 */ "nmnum ::= ON",
104559  /* 263 */ "nmnum ::= DELETE",
104560  /* 264 */ "nmnum ::= DEFAULT",
104561  /* 265 */ "plus_num ::= plus_opt number",
104562  /* 266 */ "minus_num ::= MINUS number",
104563  /* 267 */ "number ::= INTEGER|FLOAT",
104564  /* 268 */ "plus_opt ::= PLUS",
104565  /* 269 */ "plus_opt ::=",
104566  /* 270 */ "cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END",
104567  /* 271 */ "trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON fullname foreach_clause when_clause",
104568  /* 272 */ "trigger_time ::= BEFORE",
104569  /* 273 */ "trigger_time ::= AFTER",
104570  /* 274 */ "trigger_time ::= INSTEAD OF",
104571  /* 275 */ "trigger_time ::=",
104572  /* 276 */ "trigger_event ::= DELETE|INSERT",
104573  /* 277 */ "trigger_event ::= UPDATE",
104574  /* 278 */ "trigger_event ::= UPDATE OF inscollist",
104575  /* 279 */ "foreach_clause ::=",
104576  /* 280 */ "foreach_clause ::= FOR EACH ROW",
104577  /* 281 */ "when_clause ::=",
104578  /* 282 */ "when_clause ::= WHEN expr",
104579  /* 283 */ "trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI",
104580  /* 284 */ "trigger_cmd_list ::= trigger_cmd SEMI",
104581  /* 285 */ "trnm ::= nm",
104582  /* 286 */ "trnm ::= nm DOT nm",
104583  /* 287 */ "tridxby ::=",
104584  /* 288 */ "tridxby ::= INDEXED BY nm",
104585  /* 289 */ "tridxby ::= NOT INDEXED",
104586  /* 290 */ "trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist where_opt",
104587  /* 291 */ "trigger_cmd ::= insert_cmd INTO trnm inscollist_opt VALUES LP itemlist RP",
104588  /* 292 */ "trigger_cmd ::= insert_cmd INTO trnm inscollist_opt select",
104589  /* 293 */ "trigger_cmd ::= DELETE FROM trnm tridxby where_opt",
104590  /* 294 */ "trigger_cmd ::= select",
104591  /* 295 */ "expr ::= RAISE LP IGNORE RP",
104592  /* 296 */ "expr ::= RAISE LP raisetype COMMA nm RP",
104593  /* 297 */ "raisetype ::= ROLLBACK",
104594  /* 298 */ "raisetype ::= ABORT",
104595  /* 299 */ "raisetype ::= FAIL",
104596  /* 300 */ "cmd ::= DROP TRIGGER ifexists fullname",
104597  /* 301 */ "cmd ::= ATTACH database_kw_opt expr AS expr key_opt",
104598  /* 302 */ "cmd ::= DETACH database_kw_opt expr",
104599  /* 303 */ "key_opt ::=",
104600  /* 304 */ "key_opt ::= KEY expr",
104601  /* 305 */ "database_kw_opt ::= DATABASE",
104602  /* 306 */ "database_kw_opt ::=",
104603  /* 307 */ "cmd ::= REINDEX",
104604  /* 308 */ "cmd ::= REINDEX nm dbnm",
104605  /* 309 */ "cmd ::= ANALYZE",
104606  /* 310 */ "cmd ::= ANALYZE nm dbnm",
104607  /* 311 */ "cmd ::= ALTER TABLE fullname RENAME TO nm",
104608  /* 312 */ "cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt column",
104609  /* 313 */ "add_column_fullname ::= fullname",
104610  /* 314 */ "kwcolumn_opt ::=",
104611  /* 315 */ "kwcolumn_opt ::= COLUMNKW",
104612  /* 316 */ "cmd ::= create_vtab",
104613  /* 317 */ "cmd ::= create_vtab LP vtabarglist RP",
104614  /* 318 */ "create_vtab ::= createkw VIRTUAL TABLE nm dbnm USING nm",
104615  /* 319 */ "vtabarglist ::= vtabarg",
104616  /* 320 */ "vtabarglist ::= vtabarglist COMMA vtabarg",
104617  /* 321 */ "vtabarg ::=",
104618  /* 322 */ "vtabarg ::= vtabarg vtabargtoken",
104619  /* 323 */ "vtabargtoken ::= ANY",
104620  /* 324 */ "vtabargtoken ::= lp anylist RP",
104621  /* 325 */ "lp ::= LP",
104622  /* 326 */ "anylist ::=",
104623  /* 327 */ "anylist ::= anylist LP anylist RP",
104624  /* 328 */ "anylist ::= anylist ANY",
104625 };
104626 #endif /* NDEBUG */
104627 
104628 
104629 #if YYSTACKDEPTH<=0
104630 /*
104631 ** Try to increase the size of the parser stack.
104632 */
104633 static void yyGrowStack(yyParser *p){
104634   int newSize;
104635   yyStackEntry *pNew;
104636 
104637   newSize = p->yystksz*2 + 100;
104638   pNew = realloc(p->yystack, newSize*sizeof(pNew[0]));
104639   if( pNew ){
104640     p->yystack = pNew;
104641     p->yystksz = newSize;
104642 #ifndef NDEBUG
104643     if( yyTraceFILE ){
104644       fprintf(yyTraceFILE,"%sStack grows to %d entries!\n",
104645               yyTracePrompt, p->yystksz);
104646     }
104647 #endif
104648   }
104649 }
104650 #endif
104651 
104652 /*
104653 ** This function allocates a new parser.
104654 ** The only argument is a pointer to a function which works like
104655 ** malloc.
104656 **
104657 ** Inputs:
104658 ** A pointer to the function used to allocate memory.
104659 **
104660 ** Outputs:
104661 ** A pointer to a parser.  This pointer is used in subsequent calls
104662 ** to sqlite3Parser and sqlite3ParserFree.
104663 */
104664 SQLITE_PRIVATE void *sqlite3ParserAlloc(void *(*mallocProc)(size_t)){
104665   yyParser *pParser;
104666   pParser = (yyParser*)(*mallocProc)( (size_t)sizeof(yyParser) );
104667   if( pParser ){
104668     pParser->yyidx = -1;
104669 #ifdef YYTRACKMAXSTACKDEPTH
104670     pParser->yyidxMax = 0;
104671 #endif
104672 #if YYSTACKDEPTH<=0
104673     pParser->yystack = NULL;
104674     pParser->yystksz = 0;
104675     yyGrowStack(pParser);
104676 #endif
104677   }
104678   return pParser;
104679 }
104680 
104681 /* The following function deletes the value associated with a
104682 ** symbol.  The symbol can be either a terminal or nonterminal.
104683 ** "yymajor" is the symbol code, and "yypminor" is a pointer to
104684 ** the value.
104685 */
104686 static void yy_destructor(
104687   yyParser *yypParser,    /* The parser */
104688   YYCODETYPE yymajor,     /* Type code for object to destroy */
104689   YYMINORTYPE *yypminor   /* The object to be destroyed */
104690 ){
104691   sqlite3ParserARG_FETCH;
104692   switch( yymajor ){
104693     /* Here is inserted the actions which take place when a
104694     ** terminal or non-terminal is destroyed.  This can happen
104695     ** when the symbol is popped from the stack during a
104696     ** reduce or during error processing or when a parser is
104697     ** being destroyed before it is finished parsing.
104698     **
104699     ** Note: during a reduce, the only symbols destroyed are those
104700     ** which appear on the RHS of the rule, but which are not used
104701     ** inside the C code.
104702     */
104703     case 160: /* select */
104704     case 194: /* oneselect */
104705 {
104706 sqlite3SelectDelete(pParse->db, (yypminor->yy387));
104707 }
104708       break;
104709     case 174: /* term */
104710     case 175: /* expr */
104711 {
104712 sqlite3ExprDelete(pParse->db, (yypminor->yy118).pExpr);
104713 }
104714       break;
104715     case 179: /* idxlist_opt */
104716     case 187: /* idxlist */
104717     case 197: /* selcollist */
104718     case 200: /* groupby_opt */
104719     case 202: /* orderby_opt */
104720     case 204: /* sclp */
104721     case 214: /* sortlist */
104722     case 216: /* nexprlist */
104723     case 217: /* setlist */
104724     case 220: /* itemlist */
104725     case 221: /* exprlist */
104726     case 226: /* case_exprlist */
104727 {
104728 sqlite3ExprListDelete(pParse->db, (yypminor->yy322));
104729 }
104730       break;
104731     case 193: /* fullname */
104732     case 198: /* from */
104733     case 206: /* seltablist */
104734     case 207: /* stl_prefix */
104735 {
104736 sqlite3SrcListDelete(pParse->db, (yypminor->yy259));
104737 }
104738       break;
104739     case 199: /* where_opt */
104740     case 201: /* having_opt */
104741     case 210: /* on_opt */
104742     case 215: /* sortitem */
104743     case 225: /* case_operand */
104744     case 227: /* case_else */
104745     case 238: /* when_clause */
104746     case 243: /* key_opt */
104747 {
104748 sqlite3ExprDelete(pParse->db, (yypminor->yy314));
104749 }
104750       break;
104751     case 211: /* using_opt */
104752     case 213: /* inscollist */
104753     case 219: /* inscollist_opt */
104754 {
104755 sqlite3IdListDelete(pParse->db, (yypminor->yy384));
104756 }
104757       break;
104758     case 234: /* trigger_cmd_list */
104759     case 239: /* trigger_cmd */
104760 {
104761 sqlite3DeleteTriggerStep(pParse->db, (yypminor->yy203));
104762 }
104763       break;
104764     case 236: /* trigger_event */
104765 {
104766 sqlite3IdListDelete(pParse->db, (yypminor->yy90).b);
104767 }
104768       break;
104769     default:  break;   /* If no destructor action specified: do nothing */
104770   }
104771 }
104772 
104773 /*
104774 ** Pop the parser's stack once.
104775 **
104776 ** If there is a destructor routine associated with the token which
104777 ** is popped from the stack, then call it.
104778 **
104779 ** Return the major token number for the symbol popped.
104780 */
104781 static int yy_pop_parser_stack(yyParser *pParser){
104782   YYCODETYPE yymajor;
104783   yyStackEntry *yytos = &pParser->yystack[pParser->yyidx];
104784 
104785   /* There is no mechanism by which the parser stack can be popped below
104786   ** empty in SQLite.  */
104787   if( NEVER(pParser->yyidx<0) ) return 0;
104788 #ifndef NDEBUG
104789   if( yyTraceFILE && pParser->yyidx>=0 ){
104790     fprintf(yyTraceFILE,"%sPopping %s\n",
104791       yyTracePrompt,
104792       yyTokenName[yytos->major]);
104793   }
104794 #endif
104795   yymajor = yytos->major;
104796   yy_destructor(pParser, yymajor, &yytos->minor);
104797   pParser->yyidx--;
104798   return yymajor;
104799 }
104800 
104801 /*
104802 ** Deallocate and destroy a parser.  Destructors are all called for
104803 ** all stack elements before shutting the parser down.
104804 **
104805 ** Inputs:
104806 ** <ul>
104807 ** <li>  A pointer to the parser.  This should be a pointer
104808 **       obtained from sqlite3ParserAlloc.
104809 ** <li>  A pointer to a function used to reclaim memory obtained
104810 **       from malloc.
104811 ** </ul>
104812 */
104813 SQLITE_PRIVATE void sqlite3ParserFree(
104814   void *p,                    /* The parser to be deleted */
104815   void (*freeProc)(void*)     /* Function used to reclaim memory */
104816 ){
104817   yyParser *pParser = (yyParser*)p;
104818   /* In SQLite, we never try to destroy a parser that was not successfully
104819   ** created in the first place. */
104820   if( NEVER(pParser==0) ) return;
104821   while( pParser->yyidx>=0 ) yy_pop_parser_stack(pParser);
104822 #if YYSTACKDEPTH<=0
104823   free(pParser->yystack);
104824 #endif
104825   (*freeProc)((void*)pParser);
104826 }
104827 
104828 /*
104829 ** Return the peak depth of the stack for a parser.
104830 */
104831 #ifdef YYTRACKMAXSTACKDEPTH
104832 SQLITE_PRIVATE int sqlite3ParserStackPeak(void *p){
104833   yyParser *pParser = (yyParser*)p;
104834   return pParser->yyidxMax;
104835 }
104836 #endif
104837 
104838 /*
104839 ** Find the appropriate action for a parser given the terminal
104840 ** look-ahead token iLookAhead.
104841 **
104842 ** If the look-ahead token is YYNOCODE, then check to see if the action is
104843 ** independent of the look-ahead.  If it is, return the action, otherwise
104844 ** return YY_NO_ACTION.
104845 */
104846 static int yy_find_shift_action(
104847   yyParser *pParser,        /* The parser */
104848   YYCODETYPE iLookAhead     /* The look-ahead token */
104849 ){
104850   int i;
104851   int stateno = pParser->yystack[pParser->yyidx].stateno;
104852 
104853   if( stateno>YY_SHIFT_COUNT
104854    || (i = yy_shift_ofst[stateno])==YY_SHIFT_USE_DFLT ){
104855     return yy_default[stateno];
104856   }
104857   assert( iLookAhead!=YYNOCODE );
104858   i += iLookAhead;
104859   if( i<0 || i>=YY_ACTTAB_COUNT || yy_lookahead[i]!=iLookAhead ){
104860     if( iLookAhead>0 ){
104861 #ifdef YYFALLBACK
104862       YYCODETYPE iFallback;            /* Fallback token */
104863       if( iLookAhead<sizeof(yyFallback)/sizeof(yyFallback[0])
104864              && (iFallback = yyFallback[iLookAhead])!=0 ){
104865 #ifndef NDEBUG
104866         if( yyTraceFILE ){
104867           fprintf(yyTraceFILE, "%sFALLBACK %s => %s\n",
104868              yyTracePrompt, yyTokenName[iLookAhead], yyTokenName[iFallback]);
104869         }
104870 #endif
104871         return yy_find_shift_action(pParser, iFallback);
104872       }
104873 #endif
104874 #ifdef YYWILDCARD
104875       {
104876         int j = i - iLookAhead + YYWILDCARD;
104877         if(
104878 #if YY_SHIFT_MIN+YYWILDCARD<0
104879           j>=0 &&
104880 #endif
104881 #if YY_SHIFT_MAX+YYWILDCARD>=YY_ACTTAB_COUNT
104882           j<YY_ACTTAB_COUNT &&
104883 #endif
104884           yy_lookahead[j]==YYWILDCARD
104885         ){
104886 #ifndef NDEBUG
104887           if( yyTraceFILE ){
104888             fprintf(yyTraceFILE, "%sWILDCARD %s => %s\n",
104889                yyTracePrompt, yyTokenName[iLookAhead], yyTokenName[YYWILDCARD]);
104890           }
104891 #endif /* NDEBUG */
104892           return yy_action[j];
104893         }
104894       }
104895 #endif /* YYWILDCARD */
104896     }
104897     return yy_default[stateno];
104898   }else{
104899     return yy_action[i];
104900   }
104901 }
104902 
104903 /*
104904 ** Find the appropriate action for a parser given the non-terminal
104905 ** look-ahead token iLookAhead.
104906 **
104907 ** If the look-ahead token is YYNOCODE, then check to see if the action is
104908 ** independent of the look-ahead.  If it is, return the action, otherwise
104909 ** return YY_NO_ACTION.
104910 */
104911 static int yy_find_reduce_action(
104912   int stateno,              /* Current state number */
104913   YYCODETYPE iLookAhead     /* The look-ahead token */
104914 ){
104915   int i;
104916 #ifdef YYERRORSYMBOL
104917   if( stateno>YY_REDUCE_COUNT ){
104918     return yy_default[stateno];
104919   }
104920 #else
104921   assert( stateno<=YY_REDUCE_COUNT );
104922 #endif
104923   i = yy_reduce_ofst[stateno];
104924   assert( i!=YY_REDUCE_USE_DFLT );
104925   assert( iLookAhead!=YYNOCODE );
104926   i += iLookAhead;
104927 #ifdef YYERRORSYMBOL
104928   if( i<0 || i>=YY_ACTTAB_COUNT || yy_lookahead[i]!=iLookAhead ){
104929     return yy_default[stateno];
104930   }
104931 #else
104932   assert( i>=0 && i<YY_ACTTAB_COUNT );
104933   assert( yy_lookahead[i]==iLookAhead );
104934 #endif
104935   return yy_action[i];
104936 }
104937 
104938 /*
104939 ** The following routine is called if the stack overflows.
104940 */
104941 static void yyStackOverflow(yyParser *yypParser, YYMINORTYPE *yypMinor){
104942    sqlite3ParserARG_FETCH;
104943    yypParser->yyidx--;
104944 #ifndef NDEBUG
104945    if( yyTraceFILE ){
104946      fprintf(yyTraceFILE,"%sStack Overflow!\n",yyTracePrompt);
104947    }
104948 #endif
104949    while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
104950    /* Here code is inserted which will execute if the parser
104951    ** stack every overflows */
104952 
104953   UNUSED_PARAMETER(yypMinor); /* Silence some compiler warnings */
104954   sqlite3ErrorMsg(pParse, "parser stack overflow");
104955   pParse->parseError = 1;
104956    sqlite3ParserARG_STORE; /* Suppress warning about unused %extra_argument var */
104957 }
104958 
104959 /*
104960 ** Perform a shift action.
104961 */
104962 static void yy_shift(
104963   yyParser *yypParser,          /* The parser to be shifted */
104964   int yyNewState,               /* The new state to shift in */
104965   int yyMajor,                  /* The major token to shift in */
104966   YYMINORTYPE *yypMinor         /* Pointer to the minor token to shift in */
104967 ){
104968   yyStackEntry *yytos;
104969   yypParser->yyidx++;
104970 #ifdef YYTRACKMAXSTACKDEPTH
104971   if( yypParser->yyidx>yypParser->yyidxMax ){
104972     yypParser->yyidxMax = yypParser->yyidx;
104973   }
104974 #endif
104975 #if YYSTACKDEPTH>0
104976   if( yypParser->yyidx>=YYSTACKDEPTH ){
104977     yyStackOverflow(yypParser, yypMinor);
104978     return;
104979   }
104980 #else
104981   if( yypParser->yyidx>=yypParser->yystksz ){
104982     yyGrowStack(yypParser);
104983     if( yypParser->yyidx>=yypParser->yystksz ){
104984       yyStackOverflow(yypParser, yypMinor);
104985       return;
104986     }
104987   }
104988 #endif
104989   yytos = &yypParser->yystack[yypParser->yyidx];
104990   yytos->stateno = (YYACTIONTYPE)yyNewState;
104991   yytos->major = (YYCODETYPE)yyMajor;
104992   yytos->minor = *yypMinor;
104993 #ifndef NDEBUG
104994   if( yyTraceFILE && yypParser->yyidx>0 ){
104995     int i;
104996     fprintf(yyTraceFILE,"%sShift %d\n",yyTracePrompt,yyNewState);
104997     fprintf(yyTraceFILE,"%sStack:",yyTracePrompt);
104998     for(i=1; i<=yypParser->yyidx; i++)
104999       fprintf(yyTraceFILE," %s",yyTokenName[yypParser->yystack[i].major]);
105000     fprintf(yyTraceFILE,"\n");
105001   }
105002 #endif
105003 }
105004 
105005 /* The following table contains information about every rule that
105006 ** is used during the reduce.
105007 */
105008 static const struct {
105009   YYCODETYPE lhs;         /* Symbol on the left-hand side of the rule */
105010   unsigned char nrhs;     /* Number of right-hand side symbols in the rule */
105011 } yyRuleInfo[] = {
105012   { 142, 1 },
105013   { 143, 2 },
105014   { 143, 1 },
105015   { 144, 1 },
105016   { 144, 3 },
105017   { 145, 0 },
105018   { 145, 1 },
105019   { 145, 3 },
105020   { 146, 1 },
105021   { 147, 3 },
105022   { 149, 0 },
105023   { 149, 1 },
105024   { 149, 2 },
105025   { 148, 0 },
105026   { 148, 1 },
105027   { 148, 1 },
105028   { 148, 1 },
105029   { 147, 2 },
105030   { 147, 2 },
105031   { 147, 2 },
105032   { 151, 1 },
105033   { 151, 0 },
105034   { 147, 2 },
105035   { 147, 3 },
105036   { 147, 5 },
105037   { 147, 2 },
105038   { 152, 6 },
105039   { 154, 1 },
105040   { 156, 0 },
105041   { 156, 3 },
105042   { 155, 1 },
105043   { 155, 0 },
105044   { 153, 4 },
105045   { 153, 2 },
105046   { 158, 3 },
105047   { 158, 1 },
105048   { 161, 3 },
105049   { 162, 1 },
105050   { 165, 1 },
105051   { 165, 1 },
105052   { 166, 1 },
105053   { 150, 1 },
105054   { 150, 1 },
105055   { 150, 1 },
105056   { 163, 0 },
105057   { 163, 1 },
105058   { 167, 1 },
105059   { 167, 4 },
105060   { 167, 6 },
105061   { 168, 1 },
105062   { 168, 2 },
105063   { 169, 1 },
105064   { 169, 1 },
105065   { 164, 2 },
105066   { 164, 0 },
105067   { 172, 3 },
105068   { 172, 1 },
105069   { 173, 2 },
105070   { 173, 4 },
105071   { 173, 3 },
105072   { 173, 3 },
105073   { 173, 2 },
105074   { 173, 2 },
105075   { 173, 3 },
105076   { 173, 5 },
105077   { 173, 2 },
105078   { 173, 4 },
105079   { 173, 4 },
105080   { 173, 1 },
105081   { 173, 2 },
105082   { 178, 0 },
105083   { 178, 1 },
105084   { 180, 0 },
105085   { 180, 2 },
105086   { 182, 2 },
105087   { 182, 3 },
105088   { 182, 3 },
105089   { 182, 3 },
105090   { 183, 2 },
105091   { 183, 2 },
105092   { 183, 1 },
105093   { 183, 1 },
105094   { 183, 2 },
105095   { 181, 3 },
105096   { 181, 2 },
105097   { 184, 0 },
105098   { 184, 2 },
105099   { 184, 2 },
105100   { 159, 0 },
105101   { 159, 2 },
105102   { 185, 3 },
105103   { 185, 2 },
105104   { 185, 1 },
105105   { 186, 2 },
105106   { 186, 7 },
105107   { 186, 5 },
105108   { 186, 5 },
105109   { 186, 10 },
105110   { 188, 0 },
105111   { 188, 1 },
105112   { 176, 0 },
105113   { 176, 3 },
105114   { 189, 0 },
105115   { 189, 2 },
105116   { 190, 1 },
105117   { 190, 1 },
105118   { 190, 1 },
105119   { 147, 4 },
105120   { 192, 2 },
105121   { 192, 0 },
105122   { 147, 8 },
105123   { 147, 4 },
105124   { 147, 1 },
105125   { 160, 1 },
105126   { 160, 3 },
105127   { 195, 1 },
105128   { 195, 2 },
105129   { 195, 1 },
105130   { 194, 9 },
105131   { 196, 1 },
105132   { 196, 1 },
105133   { 196, 0 },
105134   { 204, 2 },
105135   { 204, 0 },
105136   { 197, 3 },
105137   { 197, 2 },
105138   { 197, 4 },
105139   { 205, 2 },
105140   { 205, 1 },
105141   { 205, 0 },
105142   { 198, 0 },
105143   { 198, 2 },
105144   { 207, 2 },
105145   { 207, 0 },
105146   { 206, 7 },
105147   { 206, 7 },
105148   { 206, 7 },
105149   { 157, 0 },
105150   { 157, 2 },
105151   { 193, 2 },
105152   { 208, 1 },
105153   { 208, 2 },
105154   { 208, 3 },
105155   { 208, 4 },
105156   { 210, 2 },
105157   { 210, 0 },
105158   { 209, 0 },
105159   { 209, 3 },
105160   { 209, 2 },
105161   { 211, 4 },
105162   { 211, 0 },
105163   { 202, 0 },
105164   { 202, 3 },
105165   { 214, 4 },
105166   { 214, 2 },
105167   { 215, 1 },
105168   { 177, 1 },
105169   { 177, 1 },
105170   { 177, 0 },
105171   { 200, 0 },
105172   { 200, 3 },
105173   { 201, 0 },
105174   { 201, 2 },
105175   { 203, 0 },
105176   { 203, 2 },
105177   { 203, 4 },
105178   { 203, 4 },
105179   { 147, 5 },
105180   { 199, 0 },
105181   { 199, 2 },
105182   { 147, 7 },
105183   { 217, 5 },
105184   { 217, 3 },
105185   { 147, 8 },
105186   { 147, 5 },
105187   { 147, 6 },
105188   { 218, 2 },
105189   { 218, 1 },
105190   { 220, 3 },
105191   { 220, 1 },
105192   { 219, 0 },
105193   { 219, 3 },
105194   { 213, 3 },
105195   { 213, 1 },
105196   { 175, 1 },
105197   { 175, 3 },
105198   { 174, 1 },
105199   { 175, 1 },
105200   { 175, 1 },
105201   { 175, 3 },
105202   { 175, 5 },
105203   { 174, 1 },
105204   { 174, 1 },
105205   { 175, 1 },
105206   { 175, 1 },
105207   { 175, 3 },
105208   { 175, 6 },
105209   { 175, 5 },
105210   { 175, 4 },
105211   { 174, 1 },
105212   { 175, 3 },
105213   { 175, 3 },
105214   { 175, 3 },
105215   { 175, 3 },
105216   { 175, 3 },
105217   { 175, 3 },
105218   { 175, 3 },
105219   { 175, 3 },
105220   { 222, 1 },
105221   { 222, 2 },
105222   { 222, 1 },
105223   { 222, 2 },
105224   { 175, 3 },
105225   { 175, 5 },
105226   { 175, 2 },
105227   { 175, 3 },
105228   { 175, 3 },
105229   { 175, 4 },
105230   { 175, 2 },
105231   { 175, 2 },
105232   { 175, 2 },
105233   { 175, 2 },
105234   { 223, 1 },
105235   { 223, 2 },
105236   { 175, 5 },
105237   { 224, 1 },
105238   { 224, 2 },
105239   { 175, 5 },
105240   { 175, 3 },
105241   { 175, 5 },
105242   { 175, 4 },
105243   { 175, 4 },
105244   { 175, 5 },
105245   { 226, 5 },
105246   { 226, 4 },
105247   { 227, 2 },
105248   { 227, 0 },
105249   { 225, 1 },
105250   { 225, 0 },
105251   { 221, 1 },
105252   { 221, 0 },
105253   { 216, 3 },
105254   { 216, 1 },
105255   { 147, 11 },
105256   { 228, 1 },
105257   { 228, 0 },
105258   { 179, 0 },
105259   { 179, 3 },
105260   { 187, 5 },
105261   { 187, 3 },
105262   { 229, 0 },
105263   { 229, 2 },
105264   { 147, 4 },
105265   { 147, 1 },
105266   { 147, 2 },
105267   { 147, 3 },
105268   { 147, 5 },
105269   { 147, 6 },
105270   { 147, 5 },
105271   { 147, 6 },
105272   { 230, 1 },
105273   { 230, 1 },
105274   { 230, 1 },
105275   { 230, 1 },
105276   { 230, 1 },
105277   { 170, 2 },
105278   { 171, 2 },
105279   { 232, 1 },
105280   { 231, 1 },
105281   { 231, 0 },
105282   { 147, 5 },
105283   { 233, 11 },
105284   { 235, 1 },
105285   { 235, 1 },
105286   { 235, 2 },
105287   { 235, 0 },
105288   { 236, 1 },
105289   { 236, 1 },
105290   { 236, 3 },
105291   { 237, 0 },
105292   { 237, 3 },
105293   { 238, 0 },
105294   { 238, 2 },
105295   { 234, 3 },
105296   { 234, 2 },
105297   { 240, 1 },
105298   { 240, 3 },
105299   { 241, 0 },
105300   { 241, 3 },
105301   { 241, 2 },
105302   { 239, 7 },
105303   { 239, 8 },
105304   { 239, 5 },
105305   { 239, 5 },
105306   { 239, 1 },
105307   { 175, 4 },
105308   { 175, 6 },
105309   { 191, 1 },
105310   { 191, 1 },
105311   { 191, 1 },
105312   { 147, 4 },
105313   { 147, 6 },
105314   { 147, 3 },
105315   { 243, 0 },
105316   { 243, 2 },
105317   { 242, 1 },
105318   { 242, 0 },
105319   { 147, 1 },
105320   { 147, 3 },
105321   { 147, 1 },
105322   { 147, 3 },
105323   { 147, 6 },
105324   { 147, 6 },
105325   { 244, 1 },
105326   { 245, 0 },
105327   { 245, 1 },
105328   { 147, 1 },
105329   { 147, 4 },
105330   { 246, 7 },
105331   { 247, 1 },
105332   { 247, 3 },
105333   { 248, 0 },
105334   { 248, 2 },
105335   { 249, 1 },
105336   { 249, 3 },
105337   { 250, 1 },
105338   { 251, 0 },
105339   { 251, 4 },
105340   { 251, 2 },
105341 };
105342 
105343 static void yy_accept(yyParser*);  /* Forward Declaration */
105344 
105345 /*
105346 ** Perform a reduce action and the shift that must immediately
105347 ** follow the reduce.
105348 */
105349 static void yy_reduce(
105350   yyParser *yypParser,         /* The parser */
105351   int yyruleno                 /* Number of the rule by which to reduce */
105352 ){
105353   int yygoto;                     /* The next state */
105354   int yyact;                      /* The next action */
105355   YYMINORTYPE yygotominor;        /* The LHS of the rule reduced */
105356   yyStackEntry *yymsp;            /* The top of the parser's stack */
105357   int yysize;                     /* Amount to pop the stack */
105358   sqlite3ParserARG_FETCH;
105359   yymsp = &yypParser->yystack[yypParser->yyidx];
105360 #ifndef NDEBUG
105361   if( yyTraceFILE && yyruleno>=0
105362         && yyruleno<(int)(sizeof(yyRuleName)/sizeof(yyRuleName[0])) ){
105363     fprintf(yyTraceFILE, "%sReduce [%s].\n", yyTracePrompt,
105364       yyRuleName[yyruleno]);
105365   }
105366 #endif /* NDEBUG */
105367 
105368   /* Silence complaints from purify about yygotominor being uninitialized
105369   ** in some cases when it is copied into the stack after the following
105370   ** switch.  yygotominor is uninitialized when a rule reduces that does
105371   ** not set the value of its left-hand side nonterminal.  Leaving the
105372   ** value of the nonterminal uninitialized is utterly harmless as long
105373   ** as the value is never used.  So really the only thing this code
105374   ** accomplishes is to quieten purify.
105375   **
105376   ** 2007-01-16:  The wireshark project (www.wireshark.org) reports that
105377   ** without this code, their parser segfaults.  I'm not sure what there
105378   ** parser is doing to make this happen.  This is the second bug report
105379   ** from wireshark this week.  Clearly they are stressing Lemon in ways
105380   ** that it has not been previously stressed...  (SQLite ticket #2172)
105381   */
105382   /*memset(&yygotominor, 0, sizeof(yygotominor));*/
105383   yygotominor = yyzerominor;
105384 
105385 
105386   switch( yyruleno ){
105387   /* Beginning here are the reduction cases.  A typical example
105388   ** follows:
105389   **   case 0:
105390   **  #line <lineno> <grammarfile>
105391   **     { ... }           // User supplied code
105392   **  #line <lineno> <thisfile>
105393   **     break;
105394   */
105395       case 5: /* explain ::= */
105396 { sqlite3BeginParse(pParse, 0); }
105397         break;
105398       case 6: /* explain ::= EXPLAIN */
105399 { sqlite3BeginParse(pParse, 1); }
105400         break;
105401       case 7: /* explain ::= EXPLAIN QUERY PLAN */
105402 { sqlite3BeginParse(pParse, 2); }
105403         break;
105404       case 8: /* cmdx ::= cmd */
105405 { sqlite3FinishCoding(pParse); }
105406         break;
105407       case 9: /* cmd ::= BEGIN transtype trans_opt */
105408 {sqlite3BeginTransaction(pParse, yymsp[-1].minor.yy4);}
105409         break;
105410       case 13: /* transtype ::= */
105411 {yygotominor.yy4 = TK_DEFERRED;}
105412         break;
105413       case 14: /* transtype ::= DEFERRED */
105414       case 15: /* transtype ::= IMMEDIATE */ yytestcase(yyruleno==15);
105415       case 16: /* transtype ::= EXCLUSIVE */ yytestcase(yyruleno==16);
105416       case 115: /* multiselect_op ::= UNION */ yytestcase(yyruleno==115);
105417       case 117: /* multiselect_op ::= EXCEPT|INTERSECT */ yytestcase(yyruleno==117);
105418 {yygotominor.yy4 = yymsp[0].major;}
105419         break;
105420       case 17: /* cmd ::= COMMIT trans_opt */
105421       case 18: /* cmd ::= END trans_opt */ yytestcase(yyruleno==18);
105422 {sqlite3CommitTransaction(pParse);}
105423         break;
105424       case 19: /* cmd ::= ROLLBACK trans_opt */
105425 {sqlite3RollbackTransaction(pParse);}
105426         break;
105427       case 22: /* cmd ::= SAVEPOINT nm */
105428 {
105429   sqlite3Savepoint(pParse, SAVEPOINT_BEGIN, &yymsp[0].minor.yy0);
105430 }
105431         break;
105432       case 23: /* cmd ::= RELEASE savepoint_opt nm */
105433 {
105434   sqlite3Savepoint(pParse, SAVEPOINT_RELEASE, &yymsp[0].minor.yy0);
105435 }
105436         break;
105437       case 24: /* cmd ::= ROLLBACK trans_opt TO savepoint_opt nm */
105438 {
105439   sqlite3Savepoint(pParse, SAVEPOINT_ROLLBACK, &yymsp[0].minor.yy0);
105440 }
105441         break;
105442       case 26: /* create_table ::= createkw temp TABLE ifnotexists nm dbnm */
105443 {
105444    sqlite3StartTable(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0,yymsp[-4].minor.yy4,0,0,yymsp[-2].minor.yy4);
105445 }
105446         break;
105447       case 27: /* createkw ::= CREATE */
105448 {
105449   pParse->db->lookaside.bEnabled = 0;
105450   yygotominor.yy0 = yymsp[0].minor.yy0;
105451 }
105452         break;
105453       case 28: /* ifnotexists ::= */
105454       case 31: /* temp ::= */ yytestcase(yyruleno==31);
105455       case 70: /* autoinc ::= */ yytestcase(yyruleno==70);
105456       case 83: /* defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt */ yytestcase(yyruleno==83);
105457       case 85: /* init_deferred_pred_opt ::= */ yytestcase(yyruleno==85);
105458       case 87: /* init_deferred_pred_opt ::= INITIALLY IMMEDIATE */ yytestcase(yyruleno==87);
105459       case 98: /* defer_subclause_opt ::= */ yytestcase(yyruleno==98);
105460       case 109: /* ifexists ::= */ yytestcase(yyruleno==109);
105461       case 120: /* distinct ::= ALL */ yytestcase(yyruleno==120);
105462       case 121: /* distinct ::= */ yytestcase(yyruleno==121);
105463       case 222: /* between_op ::= BETWEEN */ yytestcase(yyruleno==222);
105464       case 225: /* in_op ::= IN */ yytestcase(yyruleno==225);
105465 {yygotominor.yy4 = 0;}
105466         break;
105467       case 29: /* ifnotexists ::= IF NOT EXISTS */
105468       case 30: /* temp ::= TEMP */ yytestcase(yyruleno==30);
105469       case 71: /* autoinc ::= AUTOINCR */ yytestcase(yyruleno==71);
105470       case 86: /* init_deferred_pred_opt ::= INITIALLY DEFERRED */ yytestcase(yyruleno==86);
105471       case 108: /* ifexists ::= IF EXISTS */ yytestcase(yyruleno==108);
105472       case 119: /* distinct ::= DISTINCT */ yytestcase(yyruleno==119);
105473       case 223: /* between_op ::= NOT BETWEEN */ yytestcase(yyruleno==223);
105474       case 226: /* in_op ::= NOT IN */ yytestcase(yyruleno==226);
105475 {yygotominor.yy4 = 1;}
105476         break;
105477       case 32: /* create_table_args ::= LP columnlist conslist_opt RP */
105478 {
105479   sqlite3EndTable(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0,0);
105480 }
105481         break;
105482       case 33: /* create_table_args ::= AS select */
105483 {
105484   sqlite3EndTable(pParse,0,0,yymsp[0].minor.yy387);
105485   sqlite3SelectDelete(pParse->db, yymsp[0].minor.yy387);
105486 }
105487         break;
105488       case 36: /* column ::= columnid type carglist */
105489 {
105490   yygotominor.yy0.z = yymsp[-2].minor.yy0.z;
105491   yygotominor.yy0.n = (int)(pParse->sLastToken.z-yymsp[-2].minor.yy0.z) + pParse->sLastToken.n;
105492 }
105493         break;
105494       case 37: /* columnid ::= nm */
105495 {
105496   sqlite3AddColumn(pParse,&yymsp[0].minor.yy0);
105497   yygotominor.yy0 = yymsp[0].minor.yy0;
105498 }
105499         break;
105500       case 38: /* id ::= ID */
105501       case 39: /* id ::= INDEXED */ yytestcase(yyruleno==39);
105502       case 40: /* ids ::= ID|STRING */ yytestcase(yyruleno==40);
105503       case 41: /* nm ::= id */ yytestcase(yyruleno==41);
105504       case 42: /* nm ::= STRING */ yytestcase(yyruleno==42);
105505       case 43: /* nm ::= JOIN_KW */ yytestcase(yyruleno==43);
105506       case 46: /* typetoken ::= typename */ yytestcase(yyruleno==46);
105507       case 49: /* typename ::= ids */ yytestcase(yyruleno==49);
105508       case 127: /* as ::= AS nm */ yytestcase(yyruleno==127);
105509       case 128: /* as ::= ids */ yytestcase(yyruleno==128);
105510       case 138: /* dbnm ::= DOT nm */ yytestcase(yyruleno==138);
105511       case 147: /* indexed_opt ::= INDEXED BY nm */ yytestcase(yyruleno==147);
105512       case 251: /* collate ::= COLLATE ids */ yytestcase(yyruleno==251);
105513       case 260: /* nmnum ::= plus_num */ yytestcase(yyruleno==260);
105514       case 261: /* nmnum ::= nm */ yytestcase(yyruleno==261);
105515       case 262: /* nmnum ::= ON */ yytestcase(yyruleno==262);
105516       case 263: /* nmnum ::= DELETE */ yytestcase(yyruleno==263);
105517       case 264: /* nmnum ::= DEFAULT */ yytestcase(yyruleno==264);
105518       case 265: /* plus_num ::= plus_opt number */ yytestcase(yyruleno==265);
105519       case 266: /* minus_num ::= MINUS number */ yytestcase(yyruleno==266);
105520       case 267: /* number ::= INTEGER|FLOAT */ yytestcase(yyruleno==267);
105521       case 285: /* trnm ::= nm */ yytestcase(yyruleno==285);
105522 {yygotominor.yy0 = yymsp[0].minor.yy0;}
105523         break;
105524       case 45: /* type ::= typetoken */
105525 {sqlite3AddColumnType(pParse,&yymsp[0].minor.yy0);}
105526         break;
105527       case 47: /* typetoken ::= typename LP signed RP */
105528 {
105529   yygotominor.yy0.z = yymsp[-3].minor.yy0.z;
105530   yygotominor.yy0.n = (int)(&yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n] - yymsp[-3].minor.yy0.z);
105531 }
105532         break;
105533       case 48: /* typetoken ::= typename LP signed COMMA signed RP */
105534 {
105535   yygotominor.yy0.z = yymsp[-5].minor.yy0.z;
105536   yygotominor.yy0.n = (int)(&yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n] - yymsp[-5].minor.yy0.z);
105537 }
105538         break;
105539       case 50: /* typename ::= typename ids */
105540 {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);}
105541         break;
105542       case 57: /* ccons ::= DEFAULT term */
105543       case 59: /* ccons ::= DEFAULT PLUS term */ yytestcase(yyruleno==59);
105544 {sqlite3AddDefaultValue(pParse,&yymsp[0].minor.yy118);}
105545         break;
105546       case 58: /* ccons ::= DEFAULT LP expr RP */
105547 {sqlite3AddDefaultValue(pParse,&yymsp[-1].minor.yy118);}
105548         break;
105549       case 60: /* ccons ::= DEFAULT MINUS term */
105550 {
105551   ExprSpan v;
105552   v.pExpr = sqlite3PExpr(pParse, TK_UMINUS, yymsp[0].minor.yy118.pExpr, 0, 0);
105553   v.zStart = yymsp[-1].minor.yy0.z;
105554   v.zEnd = yymsp[0].minor.yy118.zEnd;
105555   sqlite3AddDefaultValue(pParse,&v);
105556 }
105557         break;
105558       case 61: /* ccons ::= DEFAULT id */
105559 {
105560   ExprSpan v;
105561   spanExpr(&v, pParse, TK_STRING, &yymsp[0].minor.yy0);
105562   sqlite3AddDefaultValue(pParse,&v);
105563 }
105564         break;
105565       case 63: /* ccons ::= NOT NULL onconf */
105566 {sqlite3AddNotNull(pParse, yymsp[0].minor.yy4);}
105567         break;
105568       case 64: /* ccons ::= PRIMARY KEY sortorder onconf autoinc */
105569 {sqlite3AddPrimaryKey(pParse,0,yymsp[-1].minor.yy4,yymsp[0].minor.yy4,yymsp[-2].minor.yy4);}
105570         break;
105571       case 65: /* ccons ::= UNIQUE onconf */
105572 {sqlite3CreateIndex(pParse,0,0,0,0,yymsp[0].minor.yy4,0,0,0,0);}
105573         break;
105574       case 66: /* ccons ::= CHECK LP expr RP */
105575 {sqlite3AddCheckConstraint(pParse,yymsp[-1].minor.yy118.pExpr);}
105576         break;
105577       case 67: /* ccons ::= REFERENCES nm idxlist_opt refargs */
105578 {sqlite3CreateForeignKey(pParse,0,&yymsp[-2].minor.yy0,yymsp[-1].minor.yy322,yymsp[0].minor.yy4);}
105579         break;
105580       case 68: /* ccons ::= defer_subclause */
105581 {sqlite3DeferForeignKey(pParse,yymsp[0].minor.yy4);}
105582         break;
105583       case 69: /* ccons ::= COLLATE ids */
105584 {sqlite3AddCollateType(pParse, &yymsp[0].minor.yy0);}
105585         break;
105586       case 72: /* refargs ::= */
105587 { yygotominor.yy4 = OE_None*0x0101; /* EV: R-19803-45884 */}
105588         break;
105589       case 73: /* refargs ::= refargs refarg */
105590 { yygotominor.yy4 = (yymsp[-1].minor.yy4 & ~yymsp[0].minor.yy215.mask) | yymsp[0].minor.yy215.value; }
105591         break;
105592       case 74: /* refarg ::= MATCH nm */
105593       case 75: /* refarg ::= ON INSERT refact */ yytestcase(yyruleno==75);
105594 { yygotominor.yy215.value = 0;     yygotominor.yy215.mask = 0x000000; }
105595         break;
105596       case 76: /* refarg ::= ON DELETE refact */
105597 { yygotominor.yy215.value = yymsp[0].minor.yy4;     yygotominor.yy215.mask = 0x0000ff; }
105598         break;
105599       case 77: /* refarg ::= ON UPDATE refact */
105600 { yygotominor.yy215.value = yymsp[0].minor.yy4<<8;  yygotominor.yy215.mask = 0x00ff00; }
105601         break;
105602       case 78: /* refact ::= SET NULL */
105603 { yygotominor.yy4 = OE_SetNull;  /* EV: R-33326-45252 */}
105604         break;
105605       case 79: /* refact ::= SET DEFAULT */
105606 { yygotominor.yy4 = OE_SetDflt;  /* EV: R-33326-45252 */}
105607         break;
105608       case 80: /* refact ::= CASCADE */
105609 { yygotominor.yy4 = OE_Cascade;  /* EV: R-33326-45252 */}
105610         break;
105611       case 81: /* refact ::= RESTRICT */
105612 { yygotominor.yy4 = OE_Restrict; /* EV: R-33326-45252 */}
105613         break;
105614       case 82: /* refact ::= NO ACTION */
105615 { yygotominor.yy4 = OE_None;     /* EV: R-33326-45252 */}
105616         break;
105617       case 84: /* defer_subclause ::= DEFERRABLE init_deferred_pred_opt */
105618       case 99: /* defer_subclause_opt ::= defer_subclause */ yytestcase(yyruleno==99);
105619       case 101: /* onconf ::= ON CONFLICT resolvetype */ yytestcase(yyruleno==101);
105620       case 104: /* resolvetype ::= raisetype */ yytestcase(yyruleno==104);
105621 {yygotominor.yy4 = yymsp[0].minor.yy4;}
105622         break;
105623       case 88: /* conslist_opt ::= */
105624 {yygotominor.yy0.n = 0; yygotominor.yy0.z = 0;}
105625         break;
105626       case 89: /* conslist_opt ::= COMMA conslist */
105627 {yygotominor.yy0 = yymsp[-1].minor.yy0;}
105628         break;
105629       case 94: /* tcons ::= PRIMARY KEY LP idxlist autoinc RP onconf */
105630 {sqlite3AddPrimaryKey(pParse,yymsp[-3].minor.yy322,yymsp[0].minor.yy4,yymsp[-2].minor.yy4,0);}
105631         break;
105632       case 95: /* tcons ::= UNIQUE LP idxlist RP onconf */
105633 {sqlite3CreateIndex(pParse,0,0,0,yymsp[-2].minor.yy322,yymsp[0].minor.yy4,0,0,0,0);}
105634         break;
105635       case 96: /* tcons ::= CHECK LP expr RP onconf */
105636 {sqlite3AddCheckConstraint(pParse,yymsp[-2].minor.yy118.pExpr);}
105637         break;
105638       case 97: /* tcons ::= FOREIGN KEY LP idxlist RP REFERENCES nm idxlist_opt refargs defer_subclause_opt */
105639 {
105640     sqlite3CreateForeignKey(pParse, yymsp[-6].minor.yy322, &yymsp[-3].minor.yy0, yymsp[-2].minor.yy322, yymsp[-1].minor.yy4);
105641     sqlite3DeferForeignKey(pParse, yymsp[0].minor.yy4);
105642 }
105643         break;
105644       case 100: /* onconf ::= */
105645 {yygotominor.yy4 = OE_Default;}
105646         break;
105647       case 102: /* orconf ::= */
105648 {yygotominor.yy210 = OE_Default;}
105649         break;
105650       case 103: /* orconf ::= OR resolvetype */
105651 {yygotominor.yy210 = (u8)yymsp[0].minor.yy4;}
105652         break;
105653       case 105: /* resolvetype ::= IGNORE */
105654 {yygotominor.yy4 = OE_Ignore;}
105655         break;
105656       case 106: /* resolvetype ::= REPLACE */
105657 {yygotominor.yy4 = OE_Replace;}
105658         break;
105659       case 107: /* cmd ::= DROP TABLE ifexists fullname */
105660 {
105661   sqlite3DropTable(pParse, yymsp[0].minor.yy259, 0, yymsp[-1].minor.yy4);
105662 }
105663         break;
105664       case 110: /* cmd ::= createkw temp VIEW ifnotexists nm dbnm AS select */
105665 {
105666   sqlite3CreateView(pParse, &yymsp[-7].minor.yy0, &yymsp[-3].minor.yy0, &yymsp[-2].minor.yy0, yymsp[0].minor.yy387, yymsp[-6].minor.yy4, yymsp[-4].minor.yy4);
105667 }
105668         break;
105669       case 111: /* cmd ::= DROP VIEW ifexists fullname */
105670 {
105671   sqlite3DropTable(pParse, yymsp[0].minor.yy259, 1, yymsp[-1].minor.yy4);
105672 }
105673         break;
105674       case 112: /* cmd ::= select */
105675 {
105676   SelectDest dest = {SRT_Output, 0, 0, 0, 0};
105677   sqlite3Select(pParse, yymsp[0].minor.yy387, &dest);
105678   sqlite3SelectDelete(pParse->db, yymsp[0].minor.yy387);
105679 }
105680         break;
105681       case 113: /* select ::= oneselect */
105682 {yygotominor.yy387 = yymsp[0].minor.yy387;}
105683         break;
105684       case 114: /* select ::= select multiselect_op oneselect */
105685 {
105686   if( yymsp[0].minor.yy387 ){
105687     yymsp[0].minor.yy387->op = (u8)yymsp[-1].minor.yy4;
105688     yymsp[0].minor.yy387->pPrior = yymsp[-2].minor.yy387;
105689   }else{
105690     sqlite3SelectDelete(pParse->db, yymsp[-2].minor.yy387);
105691   }
105692   yygotominor.yy387 = yymsp[0].minor.yy387;
105693 }
105694         break;
105695       case 116: /* multiselect_op ::= UNION ALL */
105696 {yygotominor.yy4 = TK_ALL;}
105697         break;
105698       case 118: /* oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderby_opt limit_opt */
105699 {
105700   yygotominor.yy387 = sqlite3SelectNew(pParse,yymsp[-6].minor.yy322,yymsp[-5].minor.yy259,yymsp[-4].minor.yy314,yymsp[-3].minor.yy322,yymsp[-2].minor.yy314,yymsp[-1].minor.yy322,yymsp[-7].minor.yy4,yymsp[0].minor.yy292.pLimit,yymsp[0].minor.yy292.pOffset);
105701 }
105702         break;
105703       case 122: /* sclp ::= selcollist COMMA */
105704       case 247: /* idxlist_opt ::= LP idxlist RP */ yytestcase(yyruleno==247);
105705 {yygotominor.yy322 = yymsp[-1].minor.yy322;}
105706         break;
105707       case 123: /* sclp ::= */
105708       case 151: /* orderby_opt ::= */ yytestcase(yyruleno==151);
105709       case 159: /* groupby_opt ::= */ yytestcase(yyruleno==159);
105710       case 240: /* exprlist ::= */ yytestcase(yyruleno==240);
105711       case 246: /* idxlist_opt ::= */ yytestcase(yyruleno==246);
105712 {yygotominor.yy322 = 0;}
105713         break;
105714       case 124: /* selcollist ::= sclp expr as */
105715 {
105716    yygotominor.yy322 = sqlite3ExprListAppend(pParse, yymsp[-2].minor.yy322, yymsp[-1].minor.yy118.pExpr);
105717    if( yymsp[0].minor.yy0.n>0 ) sqlite3ExprListSetName(pParse, yygotominor.yy322, &yymsp[0].minor.yy0, 1);
105718    sqlite3ExprListSetSpan(pParse,yygotominor.yy322,&yymsp[-1].minor.yy118);
105719 }
105720         break;
105721       case 125: /* selcollist ::= sclp STAR */
105722 {
105723   Expr *p = sqlite3Expr(pParse->db, TK_ALL, 0);
105724   yygotominor.yy322 = sqlite3ExprListAppend(pParse, yymsp[-1].minor.yy322, p);
105725 }
105726         break;
105727       case 126: /* selcollist ::= sclp nm DOT STAR */
105728 {
105729   Expr *pRight = sqlite3PExpr(pParse, TK_ALL, 0, 0, &yymsp[0].minor.yy0);
105730   Expr *pLeft = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[-2].minor.yy0);
105731   Expr *pDot = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight, 0);
105732   yygotominor.yy322 = sqlite3ExprListAppend(pParse,yymsp[-3].minor.yy322, pDot);
105733 }
105734         break;
105735       case 129: /* as ::= */
105736 {yygotominor.yy0.n = 0;}
105737         break;
105738       case 130: /* from ::= */
105739 {yygotominor.yy259 = sqlite3DbMallocZero(pParse->db, sizeof(*yygotominor.yy259));}
105740         break;
105741       case 131: /* from ::= FROM seltablist */
105742 {
105743   yygotominor.yy259 = yymsp[0].minor.yy259;
105744   sqlite3SrcListShiftJoinType(yygotominor.yy259);
105745 }
105746         break;
105747       case 132: /* stl_prefix ::= seltablist joinop */
105748 {
105749    yygotominor.yy259 = yymsp[-1].minor.yy259;
105750    if( ALWAYS(yygotominor.yy259 && yygotominor.yy259->nSrc>0) ) yygotominor.yy259->a[yygotominor.yy259->nSrc-1].jointype = (u8)yymsp[0].minor.yy4;
105751 }
105752         break;
105753       case 133: /* stl_prefix ::= */
105754 {yygotominor.yy259 = 0;}
105755         break;
105756       case 134: /* seltablist ::= stl_prefix nm dbnm as indexed_opt on_opt using_opt */
105757 {
105758   yygotominor.yy259 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.yy259,&yymsp[-5].minor.yy0,&yymsp[-4].minor.yy0,&yymsp[-3].minor.yy0,0,yymsp[-1].minor.yy314,yymsp[0].minor.yy384);
105759   sqlite3SrcListIndexedBy(pParse, yygotominor.yy259, &yymsp[-2].minor.yy0);
105760 }
105761         break;
105762       case 135: /* seltablist ::= stl_prefix LP select RP as on_opt using_opt */
105763 {
105764     yygotominor.yy259 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.yy259,0,0,&yymsp[-2].minor.yy0,yymsp[-4].minor.yy387,yymsp[-1].minor.yy314,yymsp[0].minor.yy384);
105765   }
105766         break;
105767       case 136: /* seltablist ::= stl_prefix LP seltablist RP as on_opt using_opt */
105768 {
105769     if( yymsp[-6].minor.yy259==0 && yymsp[-2].minor.yy0.n==0 && yymsp[-1].minor.yy314==0 && yymsp[0].minor.yy384==0 ){
105770       yygotominor.yy259 = yymsp[-4].minor.yy259;
105771     }else{
105772       Select *pSubquery;
105773       sqlite3SrcListShiftJoinType(yymsp[-4].minor.yy259);
105774       pSubquery = sqlite3SelectNew(pParse,0,yymsp[-4].minor.yy259,0,0,0,0,0,0,0);
105775       yygotominor.yy259 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.yy259,0,0,&yymsp[-2].minor.yy0,pSubquery,yymsp[-1].minor.yy314,yymsp[0].minor.yy384);
105776     }
105777   }
105778         break;
105779       case 137: /* dbnm ::= */
105780       case 146: /* indexed_opt ::= */ yytestcase(yyruleno==146);
105781 {yygotominor.yy0.z=0; yygotominor.yy0.n=0;}
105782         break;
105783       case 139: /* fullname ::= nm dbnm */
105784 {yygotominor.yy259 = sqlite3SrcListAppend(pParse->db,0,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0);}
105785         break;
105786       case 140: /* joinop ::= COMMA|JOIN */
105787 { yygotominor.yy4 = JT_INNER; }
105788         break;
105789       case 141: /* joinop ::= JOIN_KW JOIN */
105790 { yygotominor.yy4 = sqlite3JoinType(pParse,&yymsp[-1].minor.yy0,0,0); }
105791         break;
105792       case 142: /* joinop ::= JOIN_KW nm JOIN */
105793 { yygotominor.yy4 = sqlite3JoinType(pParse,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0,0); }
105794         break;
105795       case 143: /* joinop ::= JOIN_KW nm nm JOIN */
105796 { yygotominor.yy4 = sqlite3JoinType(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0); }
105797         break;
105798       case 144: /* on_opt ::= ON expr */
105799       case 155: /* sortitem ::= expr */ yytestcase(yyruleno==155);
105800       case 162: /* having_opt ::= HAVING expr */ yytestcase(yyruleno==162);
105801       case 169: /* where_opt ::= WHERE expr */ yytestcase(yyruleno==169);
105802       case 235: /* case_else ::= ELSE expr */ yytestcase(yyruleno==235);
105803       case 237: /* case_operand ::= expr */ yytestcase(yyruleno==237);
105804 {yygotominor.yy314 = yymsp[0].minor.yy118.pExpr;}
105805         break;
105806       case 145: /* on_opt ::= */
105807       case 161: /* having_opt ::= */ yytestcase(yyruleno==161);
105808       case 168: /* where_opt ::= */ yytestcase(yyruleno==168);
105809       case 236: /* case_else ::= */ yytestcase(yyruleno==236);
105810       case 238: /* case_operand ::= */ yytestcase(yyruleno==238);
105811 {yygotominor.yy314 = 0;}
105812         break;
105813       case 148: /* indexed_opt ::= NOT INDEXED */
105814 {yygotominor.yy0.z=0; yygotominor.yy0.n=1;}
105815         break;
105816       case 149: /* using_opt ::= USING LP inscollist RP */
105817       case 181: /* inscollist_opt ::= LP inscollist RP */ yytestcase(yyruleno==181);
105818 {yygotominor.yy384 = yymsp[-1].minor.yy384;}
105819         break;
105820       case 150: /* using_opt ::= */
105821       case 180: /* inscollist_opt ::= */ yytestcase(yyruleno==180);
105822 {yygotominor.yy384 = 0;}
105823         break;
105824       case 152: /* orderby_opt ::= ORDER BY sortlist */
105825       case 160: /* groupby_opt ::= GROUP BY nexprlist */ yytestcase(yyruleno==160);
105826       case 239: /* exprlist ::= nexprlist */ yytestcase(yyruleno==239);
105827 {yygotominor.yy322 = yymsp[0].minor.yy322;}
105828         break;
105829       case 153: /* sortlist ::= sortlist COMMA sortitem sortorder */
105830 {
105831   yygotominor.yy322 = sqlite3ExprListAppend(pParse,yymsp[-3].minor.yy322,yymsp[-1].minor.yy314);
105832   if( yygotominor.yy322 ) yygotominor.yy322->a[yygotominor.yy322->nExpr-1].sortOrder = (u8)yymsp[0].minor.yy4;
105833 }
105834         break;
105835       case 154: /* sortlist ::= sortitem sortorder */
105836 {
105837   yygotominor.yy322 = sqlite3ExprListAppend(pParse,0,yymsp[-1].minor.yy314);
105838   if( yygotominor.yy322 && ALWAYS(yygotominor.yy322->a) ) yygotominor.yy322->a[0].sortOrder = (u8)yymsp[0].minor.yy4;
105839 }
105840         break;
105841       case 156: /* sortorder ::= ASC */
105842       case 158: /* sortorder ::= */ yytestcase(yyruleno==158);
105843 {yygotominor.yy4 = SQLITE_SO_ASC;}
105844         break;
105845       case 157: /* sortorder ::= DESC */
105846 {yygotominor.yy4 = SQLITE_SO_DESC;}
105847         break;
105848       case 163: /* limit_opt ::= */
105849 {yygotominor.yy292.pLimit = 0; yygotominor.yy292.pOffset = 0;}
105850         break;
105851       case 164: /* limit_opt ::= LIMIT expr */
105852 {yygotominor.yy292.pLimit = yymsp[0].minor.yy118.pExpr; yygotominor.yy292.pOffset = 0;}
105853         break;
105854       case 165: /* limit_opt ::= LIMIT expr OFFSET expr */
105855 {yygotominor.yy292.pLimit = yymsp[-2].minor.yy118.pExpr; yygotominor.yy292.pOffset = yymsp[0].minor.yy118.pExpr;}
105856         break;
105857       case 166: /* limit_opt ::= LIMIT expr COMMA expr */
105858 {yygotominor.yy292.pOffset = yymsp[-2].minor.yy118.pExpr; yygotominor.yy292.pLimit = yymsp[0].minor.yy118.pExpr;}
105859         break;
105860       case 167: /* cmd ::= DELETE FROM fullname indexed_opt where_opt */
105861 {
105862   sqlite3SrcListIndexedBy(pParse, yymsp[-2].minor.yy259, &yymsp[-1].minor.yy0);
105863   sqlite3DeleteFrom(pParse,yymsp[-2].minor.yy259,yymsp[0].minor.yy314);
105864 }
105865         break;
105866       case 170: /* cmd ::= UPDATE orconf fullname indexed_opt SET setlist where_opt */
105867 {
105868   sqlite3SrcListIndexedBy(pParse, yymsp[-4].minor.yy259, &yymsp[-3].minor.yy0);
105869   sqlite3ExprListCheckLength(pParse,yymsp[-1].minor.yy322,"set list");
105870   sqlite3Update(pParse,yymsp[-4].minor.yy259,yymsp[-1].minor.yy322,yymsp[0].minor.yy314,yymsp[-5].minor.yy210);
105871 }
105872         break;
105873       case 171: /* setlist ::= setlist COMMA nm EQ expr */
105874 {
105875   yygotominor.yy322 = sqlite3ExprListAppend(pParse, yymsp[-4].minor.yy322, yymsp[0].minor.yy118.pExpr);
105876   sqlite3ExprListSetName(pParse, yygotominor.yy322, &yymsp[-2].minor.yy0, 1);
105877 }
105878         break;
105879       case 172: /* setlist ::= nm EQ expr */
105880 {
105881   yygotominor.yy322 = sqlite3ExprListAppend(pParse, 0, yymsp[0].minor.yy118.pExpr);
105882   sqlite3ExprListSetName(pParse, yygotominor.yy322, &yymsp[-2].minor.yy0, 1);
105883 }
105884         break;
105885       case 173: /* cmd ::= insert_cmd INTO fullname inscollist_opt VALUES LP itemlist RP */
105886 {sqlite3Insert(pParse, yymsp[-5].minor.yy259, yymsp[-1].minor.yy322, 0, yymsp[-4].minor.yy384, yymsp[-7].minor.yy210);}
105887         break;
105888       case 174: /* cmd ::= insert_cmd INTO fullname inscollist_opt select */
105889 {sqlite3Insert(pParse, yymsp[-2].minor.yy259, 0, yymsp[0].minor.yy387, yymsp[-1].minor.yy384, yymsp[-4].minor.yy210);}
105890         break;
105891       case 175: /* cmd ::= insert_cmd INTO fullname inscollist_opt DEFAULT VALUES */
105892 {sqlite3Insert(pParse, yymsp[-3].minor.yy259, 0, 0, yymsp[-2].minor.yy384, yymsp[-5].minor.yy210);}
105893         break;
105894       case 176: /* insert_cmd ::= INSERT orconf */
105895 {yygotominor.yy210 = yymsp[0].minor.yy210;}
105896         break;
105897       case 177: /* insert_cmd ::= REPLACE */
105898 {yygotominor.yy210 = OE_Replace;}
105899         break;
105900       case 178: /* itemlist ::= itemlist COMMA expr */
105901       case 241: /* nexprlist ::= nexprlist COMMA expr */ yytestcase(yyruleno==241);
105902 {yygotominor.yy322 = sqlite3ExprListAppend(pParse,yymsp[-2].minor.yy322,yymsp[0].minor.yy118.pExpr);}
105903         break;
105904       case 179: /* itemlist ::= expr */
105905       case 242: /* nexprlist ::= expr */ yytestcase(yyruleno==242);
105906 {yygotominor.yy322 = sqlite3ExprListAppend(pParse,0,yymsp[0].minor.yy118.pExpr);}
105907         break;
105908       case 182: /* inscollist ::= inscollist COMMA nm */
105909 {yygotominor.yy384 = sqlite3IdListAppend(pParse->db,yymsp[-2].minor.yy384,&yymsp[0].minor.yy0);}
105910         break;
105911       case 183: /* inscollist ::= nm */
105912 {yygotominor.yy384 = sqlite3IdListAppend(pParse->db,0,&yymsp[0].minor.yy0);}
105913         break;
105914       case 184: /* expr ::= term */
105915 {yygotominor.yy118 = yymsp[0].minor.yy118;}
105916         break;
105917       case 185: /* expr ::= LP expr RP */
105918 {yygotominor.yy118.pExpr = yymsp[-1].minor.yy118.pExpr; spanSet(&yygotominor.yy118,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0);}
105919         break;
105920       case 186: /* term ::= NULL */
105921       case 191: /* term ::= INTEGER|FLOAT|BLOB */ yytestcase(yyruleno==191);
105922       case 192: /* term ::= STRING */ yytestcase(yyruleno==192);
105923 {spanExpr(&yygotominor.yy118, pParse, yymsp[0].major, &yymsp[0].minor.yy0);}
105924         break;
105925       case 187: /* expr ::= id */
105926       case 188: /* expr ::= JOIN_KW */ yytestcase(yyruleno==188);
105927 {spanExpr(&yygotominor.yy118, pParse, TK_ID, &yymsp[0].minor.yy0);}
105928         break;
105929       case 189: /* expr ::= nm DOT nm */
105930 {
105931   Expr *temp1 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[-2].minor.yy0);
105932   Expr *temp2 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[0].minor.yy0);
105933   yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_DOT, temp1, temp2, 0);
105934   spanSet(&yygotominor.yy118,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0);
105935 }
105936         break;
105937       case 190: /* expr ::= nm DOT nm DOT nm */
105938 {
105939   Expr *temp1 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[-4].minor.yy0);
105940   Expr *temp2 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[-2].minor.yy0);
105941   Expr *temp3 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[0].minor.yy0);
105942   Expr *temp4 = sqlite3PExpr(pParse, TK_DOT, temp2, temp3, 0);
105943   yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_DOT, temp1, temp4, 0);
105944   spanSet(&yygotominor.yy118,&yymsp[-4].minor.yy0,&yymsp[0].minor.yy0);
105945 }
105946         break;
105947       case 193: /* expr ::= REGISTER */
105948 {
105949   /* When doing a nested parse, one can include terms in an expression
105950   ** that look like this:   #1 #2 ...  These terms refer to registers
105951   ** in the virtual machine.  #N is the N-th register. */
105952   if( pParse->nested==0 ){
105953     sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &yymsp[0].minor.yy0);
105954     yygotominor.yy118.pExpr = 0;
105955   }else{
105956     yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_REGISTER, 0, 0, &yymsp[0].minor.yy0);
105957     if( yygotominor.yy118.pExpr ) sqlite3GetInt32(&yymsp[0].minor.yy0.z[1], &yygotominor.yy118.pExpr->iTable);
105958   }
105959   spanSet(&yygotominor.yy118, &yymsp[0].minor.yy0, &yymsp[0].minor.yy0);
105960 }
105961         break;
105962       case 194: /* expr ::= VARIABLE */
105963 {
105964   spanExpr(&yygotominor.yy118, pParse, TK_VARIABLE, &yymsp[0].minor.yy0);
105965   sqlite3ExprAssignVarNumber(pParse, yygotominor.yy118.pExpr);
105966   spanSet(&yygotominor.yy118, &yymsp[0].minor.yy0, &yymsp[0].minor.yy0);
105967 }
105968         break;
105969       case 195: /* expr ::= expr COLLATE ids */
105970 {
105971   yygotominor.yy118.pExpr = sqlite3ExprSetCollByToken(pParse, yymsp[-2].minor.yy118.pExpr, &yymsp[0].minor.yy0);
105972   yygotominor.yy118.zStart = yymsp[-2].minor.yy118.zStart;
105973   yygotominor.yy118.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
105974 }
105975         break;
105976       case 196: /* expr ::= CAST LP expr AS typetoken RP */
105977 {
105978   yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_CAST, yymsp[-3].minor.yy118.pExpr, 0, &yymsp[-1].minor.yy0);
105979   spanSet(&yygotominor.yy118,&yymsp[-5].minor.yy0,&yymsp[0].minor.yy0);
105980 }
105981         break;
105982       case 197: /* expr ::= ID LP distinct exprlist RP */
105983 {
105984   if( yymsp[-1].minor.yy322 && yymsp[-1].minor.yy322->nExpr>pParse->db->aLimit[SQLITE_LIMIT_FUNCTION_ARG] ){
105985     sqlite3ErrorMsg(pParse, "too many arguments on function %T", &yymsp[-4].minor.yy0);
105986   }
105987   yygotominor.yy118.pExpr = sqlite3ExprFunction(pParse, yymsp[-1].minor.yy322, &yymsp[-4].minor.yy0);
105988   spanSet(&yygotominor.yy118,&yymsp[-4].minor.yy0,&yymsp[0].minor.yy0);
105989   if( yymsp[-2].minor.yy4 && yygotominor.yy118.pExpr ){
105990     yygotominor.yy118.pExpr->flags |= EP_Distinct;
105991   }
105992 }
105993         break;
105994       case 198: /* expr ::= ID LP STAR RP */
105995 {
105996   yygotominor.yy118.pExpr = sqlite3ExprFunction(pParse, 0, &yymsp[-3].minor.yy0);
105997   spanSet(&yygotominor.yy118,&yymsp[-3].minor.yy0,&yymsp[0].minor.yy0);
105998 }
105999         break;
106000       case 199: /* term ::= CTIME_KW */
106001 {
106002   /* The CURRENT_TIME, CURRENT_DATE, and CURRENT_TIMESTAMP values are
106003   ** treated as functions that return constants */
106004   yygotominor.yy118.pExpr = sqlite3ExprFunction(pParse, 0,&yymsp[0].minor.yy0);
106005   if( yygotominor.yy118.pExpr ){
106006     yygotominor.yy118.pExpr->op = TK_CONST_FUNC;
106007   }
106008   spanSet(&yygotominor.yy118, &yymsp[0].minor.yy0, &yymsp[0].minor.yy0);
106009 }
106010         break;
106011       case 200: /* expr ::= expr AND expr */
106012       case 201: /* expr ::= expr OR expr */ yytestcase(yyruleno==201);
106013       case 202: /* expr ::= expr LT|GT|GE|LE expr */ yytestcase(yyruleno==202);
106014       case 203: /* expr ::= expr EQ|NE expr */ yytestcase(yyruleno==203);
106015       case 204: /* expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr */ yytestcase(yyruleno==204);
106016       case 205: /* expr ::= expr PLUS|MINUS expr */ yytestcase(yyruleno==205);
106017       case 206: /* expr ::= expr STAR|SLASH|REM expr */ yytestcase(yyruleno==206);
106018       case 207: /* expr ::= expr CONCAT expr */ yytestcase(yyruleno==207);
106019 {spanBinaryExpr(&yygotominor.yy118,pParse,yymsp[-1].major,&yymsp[-2].minor.yy118,&yymsp[0].minor.yy118);}
106020         break;
106021       case 208: /* likeop ::= LIKE_KW */
106022       case 210: /* likeop ::= MATCH */ yytestcase(yyruleno==210);
106023 {yygotominor.yy342.eOperator = yymsp[0].minor.yy0; yygotominor.yy342.not = 0;}
106024         break;
106025       case 209: /* likeop ::= NOT LIKE_KW */
106026       case 211: /* likeop ::= NOT MATCH */ yytestcase(yyruleno==211);
106027 {yygotominor.yy342.eOperator = yymsp[0].minor.yy0; yygotominor.yy342.not = 1;}
106028         break;
106029       case 212: /* expr ::= expr likeop expr */
106030 {
106031   ExprList *pList;
106032   pList = sqlite3ExprListAppend(pParse,0, yymsp[0].minor.yy118.pExpr);
106033   pList = sqlite3ExprListAppend(pParse,pList, yymsp[-2].minor.yy118.pExpr);
106034   yygotominor.yy118.pExpr = sqlite3ExprFunction(pParse, pList, &yymsp[-1].minor.yy342.eOperator);
106035   if( yymsp[-1].minor.yy342.not ) yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy118.pExpr, 0, 0);
106036   yygotominor.yy118.zStart = yymsp[-2].minor.yy118.zStart;
106037   yygotominor.yy118.zEnd = yymsp[0].minor.yy118.zEnd;
106038   if( yygotominor.yy118.pExpr ) yygotominor.yy118.pExpr->flags |= EP_InfixFunc;
106039 }
106040         break;
106041       case 213: /* expr ::= expr likeop expr ESCAPE expr */
106042 {
106043   ExprList *pList;
106044   pList = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy118.pExpr);
106045   pList = sqlite3ExprListAppend(pParse,pList, yymsp[-4].minor.yy118.pExpr);
106046   pList = sqlite3ExprListAppend(pParse,pList, yymsp[0].minor.yy118.pExpr);
106047   yygotominor.yy118.pExpr = sqlite3ExprFunction(pParse, pList, &yymsp[-3].minor.yy342.eOperator);
106048   if( yymsp[-3].minor.yy342.not ) yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy118.pExpr, 0, 0);
106049   yygotominor.yy118.zStart = yymsp[-4].minor.yy118.zStart;
106050   yygotominor.yy118.zEnd = yymsp[0].minor.yy118.zEnd;
106051   if( yygotominor.yy118.pExpr ) yygotominor.yy118.pExpr->flags |= EP_InfixFunc;
106052 }
106053         break;
106054       case 214: /* expr ::= expr ISNULL|NOTNULL */
106055 {spanUnaryPostfix(&yygotominor.yy118,pParse,yymsp[0].major,&yymsp[-1].minor.yy118,&yymsp[0].minor.yy0);}
106056         break;
106057       case 215: /* expr ::= expr NOT NULL */
106058 {spanUnaryPostfix(&yygotominor.yy118,pParse,TK_NOTNULL,&yymsp[-2].minor.yy118,&yymsp[0].minor.yy0);}
106059         break;
106060       case 216: /* expr ::= expr IS expr */
106061 {
106062   spanBinaryExpr(&yygotominor.yy118,pParse,TK_IS,&yymsp[-2].minor.yy118,&yymsp[0].minor.yy118);
106063   binaryToUnaryIfNull(pParse, yymsp[0].minor.yy118.pExpr, yygotominor.yy118.pExpr, TK_ISNULL);
106064 }
106065         break;
106066       case 217: /* expr ::= expr IS NOT expr */
106067 {
106068   spanBinaryExpr(&yygotominor.yy118,pParse,TK_ISNOT,&yymsp[-3].minor.yy118,&yymsp[0].minor.yy118);
106069   binaryToUnaryIfNull(pParse, yymsp[0].minor.yy118.pExpr, yygotominor.yy118.pExpr, TK_NOTNULL);
106070 }
106071         break;
106072       case 218: /* expr ::= NOT expr */
106073       case 219: /* expr ::= BITNOT expr */ yytestcase(yyruleno==219);
106074 {spanUnaryPrefix(&yygotominor.yy118,pParse,yymsp[-1].major,&yymsp[0].minor.yy118,&yymsp[-1].minor.yy0);}
106075         break;
106076       case 220: /* expr ::= MINUS expr */
106077 {spanUnaryPrefix(&yygotominor.yy118,pParse,TK_UMINUS,&yymsp[0].minor.yy118,&yymsp[-1].minor.yy0);}
106078         break;
106079       case 221: /* expr ::= PLUS expr */
106080 {spanUnaryPrefix(&yygotominor.yy118,pParse,TK_UPLUS,&yymsp[0].minor.yy118,&yymsp[-1].minor.yy0);}
106081         break;
106082       case 224: /* expr ::= expr between_op expr AND expr */
106083 {
106084   ExprList *pList = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy118.pExpr);
106085   pList = sqlite3ExprListAppend(pParse,pList, yymsp[0].minor.yy118.pExpr);
106086   yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_BETWEEN, yymsp[-4].minor.yy118.pExpr, 0, 0);
106087   if( yygotominor.yy118.pExpr ){
106088     yygotominor.yy118.pExpr->x.pList = pList;
106089   }else{
106090     sqlite3ExprListDelete(pParse->db, pList);
106091   }
106092   if( yymsp[-3].minor.yy4 ) yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy118.pExpr, 0, 0);
106093   yygotominor.yy118.zStart = yymsp[-4].minor.yy118.zStart;
106094   yygotominor.yy118.zEnd = yymsp[0].minor.yy118.zEnd;
106095 }
106096         break;
106097       case 227: /* expr ::= expr in_op LP exprlist RP */
106098 {
106099     if( yymsp[-1].minor.yy322==0 ){
106100       /* Expressions of the form
106101       **
106102       **      expr1 IN ()
106103       **      expr1 NOT IN ()
106104       **
106105       ** simplify to constants 0 (false) and 1 (true), respectively,
106106       ** regardless of the value of expr1.
106107       */
106108       yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_INTEGER, 0, 0, &sqlite3IntTokens[yymsp[-3].minor.yy4]);
106109       sqlite3ExprDelete(pParse->db, yymsp[-4].minor.yy118.pExpr);
106110     }else{
106111       yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_IN, yymsp[-4].minor.yy118.pExpr, 0, 0);
106112       if( yygotominor.yy118.pExpr ){
106113         yygotominor.yy118.pExpr->x.pList = yymsp[-1].minor.yy322;
106114         sqlite3ExprSetHeight(pParse, yygotominor.yy118.pExpr);
106115       }else{
106116         sqlite3ExprListDelete(pParse->db, yymsp[-1].minor.yy322);
106117       }
106118       if( yymsp[-3].minor.yy4 ) yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy118.pExpr, 0, 0);
106119     }
106120     yygotominor.yy118.zStart = yymsp[-4].minor.yy118.zStart;
106121     yygotominor.yy118.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
106122   }
106123         break;
106124       case 228: /* expr ::= LP select RP */
106125 {
106126     yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_SELECT, 0, 0, 0);
106127     if( yygotominor.yy118.pExpr ){
106128       yygotominor.yy118.pExpr->x.pSelect = yymsp[-1].minor.yy387;
106129       ExprSetProperty(yygotominor.yy118.pExpr, EP_xIsSelect);
106130       sqlite3ExprSetHeight(pParse, yygotominor.yy118.pExpr);
106131     }else{
106132       sqlite3SelectDelete(pParse->db, yymsp[-1].minor.yy387);
106133     }
106134     yygotominor.yy118.zStart = yymsp[-2].minor.yy0.z;
106135     yygotominor.yy118.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
106136   }
106137         break;
106138       case 229: /* expr ::= expr in_op LP select RP */
106139 {
106140     yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_IN, yymsp[-4].minor.yy118.pExpr, 0, 0);
106141     if( yygotominor.yy118.pExpr ){
106142       yygotominor.yy118.pExpr->x.pSelect = yymsp[-1].minor.yy387;
106143       ExprSetProperty(yygotominor.yy118.pExpr, EP_xIsSelect);
106144       sqlite3ExprSetHeight(pParse, yygotominor.yy118.pExpr);
106145     }else{
106146       sqlite3SelectDelete(pParse->db, yymsp[-1].minor.yy387);
106147     }
106148     if( yymsp[-3].minor.yy4 ) yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy118.pExpr, 0, 0);
106149     yygotominor.yy118.zStart = yymsp[-4].minor.yy118.zStart;
106150     yygotominor.yy118.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
106151   }
106152         break;
106153       case 230: /* expr ::= expr in_op nm dbnm */
106154 {
106155     SrcList *pSrc = sqlite3SrcListAppend(pParse->db, 0,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0);
106156     yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_IN, yymsp[-3].minor.yy118.pExpr, 0, 0);
106157     if( yygotominor.yy118.pExpr ){
106158       yygotominor.yy118.pExpr->x.pSelect = sqlite3SelectNew(pParse, 0,pSrc,0,0,0,0,0,0,0);
106159       ExprSetProperty(yygotominor.yy118.pExpr, EP_xIsSelect);
106160       sqlite3ExprSetHeight(pParse, yygotominor.yy118.pExpr);
106161     }else{
106162       sqlite3SrcListDelete(pParse->db, pSrc);
106163     }
106164     if( yymsp[-2].minor.yy4 ) yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy118.pExpr, 0, 0);
106165     yygotominor.yy118.zStart = yymsp[-3].minor.yy118.zStart;
106166     yygotominor.yy118.zEnd = yymsp[0].minor.yy0.z ? &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n] : &yymsp[-1].minor.yy0.z[yymsp[-1].minor.yy0.n];
106167   }
106168         break;
106169       case 231: /* expr ::= EXISTS LP select RP */
106170 {
106171     Expr *p = yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_EXISTS, 0, 0, 0);
106172     if( p ){
106173       p->x.pSelect = yymsp[-1].minor.yy387;
106174       ExprSetProperty(p, EP_xIsSelect);
106175       sqlite3ExprSetHeight(pParse, p);
106176     }else{
106177       sqlite3SelectDelete(pParse->db, yymsp[-1].minor.yy387);
106178     }
106179     yygotominor.yy118.zStart = yymsp[-3].minor.yy0.z;
106180     yygotominor.yy118.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
106181   }
106182         break;
106183       case 232: /* expr ::= CASE case_operand case_exprlist case_else END */
106184 {
106185   yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_CASE, yymsp[-3].minor.yy314, yymsp[-1].minor.yy314, 0);
106186   if( yygotominor.yy118.pExpr ){
106187     yygotominor.yy118.pExpr->x.pList = yymsp[-2].minor.yy322;
106188     sqlite3ExprSetHeight(pParse, yygotominor.yy118.pExpr);
106189   }else{
106190     sqlite3ExprListDelete(pParse->db, yymsp[-2].minor.yy322);
106191   }
106192   yygotominor.yy118.zStart = yymsp[-4].minor.yy0.z;
106193   yygotominor.yy118.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
106194 }
106195         break;
106196       case 233: /* case_exprlist ::= case_exprlist WHEN expr THEN expr */
106197 {
106198   yygotominor.yy322 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy322, yymsp[-2].minor.yy118.pExpr);
106199   yygotominor.yy322 = sqlite3ExprListAppend(pParse,yygotominor.yy322, yymsp[0].minor.yy118.pExpr);
106200 }
106201         break;
106202       case 234: /* case_exprlist ::= WHEN expr THEN expr */
106203 {
106204   yygotominor.yy322 = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy118.pExpr);
106205   yygotominor.yy322 = sqlite3ExprListAppend(pParse,yygotominor.yy322, yymsp[0].minor.yy118.pExpr);
106206 }
106207         break;
106208       case 243: /* cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP idxlist RP */
106209 {
106210   sqlite3CreateIndex(pParse, &yymsp[-6].minor.yy0, &yymsp[-5].minor.yy0,
106211                      sqlite3SrcListAppend(pParse->db,0,&yymsp[-3].minor.yy0,0), yymsp[-1].minor.yy322, yymsp[-9].minor.yy4,
106212                       &yymsp[-10].minor.yy0, &yymsp[0].minor.yy0, SQLITE_SO_ASC, yymsp[-7].minor.yy4);
106213 }
106214         break;
106215       case 244: /* uniqueflag ::= UNIQUE */
106216       case 298: /* raisetype ::= ABORT */ yytestcase(yyruleno==298);
106217 {yygotominor.yy4 = OE_Abort;}
106218         break;
106219       case 245: /* uniqueflag ::= */
106220 {yygotominor.yy4 = OE_None;}
106221         break;
106222       case 248: /* idxlist ::= idxlist COMMA nm collate sortorder */
106223 {
106224   Expr *p = 0;
106225   if( yymsp[-1].minor.yy0.n>0 ){
106226     p = sqlite3Expr(pParse->db, TK_COLUMN, 0);
106227     sqlite3ExprSetCollByToken(pParse, p, &yymsp[-1].minor.yy0);
106228   }
106229   yygotominor.yy322 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy322, p);
106230   sqlite3ExprListSetName(pParse,yygotominor.yy322,&yymsp[-2].minor.yy0,1);
106231   sqlite3ExprListCheckLength(pParse, yygotominor.yy322, "index");
106232   if( yygotominor.yy322 ) yygotominor.yy322->a[yygotominor.yy322->nExpr-1].sortOrder = (u8)yymsp[0].minor.yy4;
106233 }
106234         break;
106235       case 249: /* idxlist ::= nm collate sortorder */
106236 {
106237   Expr *p = 0;
106238   if( yymsp[-1].minor.yy0.n>0 ){
106239     p = sqlite3PExpr(pParse, TK_COLUMN, 0, 0, 0);
106240     sqlite3ExprSetCollByToken(pParse, p, &yymsp[-1].minor.yy0);
106241   }
106242   yygotominor.yy322 = sqlite3ExprListAppend(pParse,0, p);
106243   sqlite3ExprListSetName(pParse, yygotominor.yy322, &yymsp[-2].minor.yy0, 1);
106244   sqlite3ExprListCheckLength(pParse, yygotominor.yy322, "index");
106245   if( yygotominor.yy322 ) yygotominor.yy322->a[yygotominor.yy322->nExpr-1].sortOrder = (u8)yymsp[0].minor.yy4;
106246 }
106247         break;
106248       case 250: /* collate ::= */
106249 {yygotominor.yy0.z = 0; yygotominor.yy0.n = 0;}
106250         break;
106251       case 252: /* cmd ::= DROP INDEX ifexists fullname */
106252 {sqlite3DropIndex(pParse, yymsp[0].minor.yy259, yymsp[-1].minor.yy4);}
106253         break;
106254       case 253: /* cmd ::= VACUUM */
106255       case 254: /* cmd ::= VACUUM nm */ yytestcase(yyruleno==254);
106256 {sqlite3Vacuum(pParse);}
106257         break;
106258       case 255: /* cmd ::= PRAGMA nm dbnm */
106259 {sqlite3Pragma(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0,0,0);}
106260         break;
106261       case 256: /* cmd ::= PRAGMA nm dbnm EQ nmnum */
106262 {sqlite3Pragma(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0,0);}
106263         break;
106264       case 257: /* cmd ::= PRAGMA nm dbnm LP nmnum RP */
106265 {sqlite3Pragma(pParse,&yymsp[-4].minor.yy0,&yymsp[-3].minor.yy0,&yymsp[-1].minor.yy0,0);}
106266         break;
106267       case 258: /* cmd ::= PRAGMA nm dbnm EQ minus_num */
106268 {sqlite3Pragma(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0,1);}
106269         break;
106270       case 259: /* cmd ::= PRAGMA nm dbnm LP minus_num RP */
106271 {sqlite3Pragma(pParse,&yymsp[-4].minor.yy0,&yymsp[-3].minor.yy0,&yymsp[-1].minor.yy0,1);}
106272         break;
106273       case 270: /* cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END */
106274 {
106275   Token all;
106276   all.z = yymsp[-3].minor.yy0.z;
106277   all.n = (int)(yymsp[0].minor.yy0.z - yymsp[-3].minor.yy0.z) + yymsp[0].minor.yy0.n;
106278   sqlite3FinishTrigger(pParse, yymsp[-1].minor.yy203, &all);
106279 }
106280         break;
106281       case 271: /* trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON fullname foreach_clause when_clause */
106282 {
106283   sqlite3BeginTrigger(pParse, &yymsp[-7].minor.yy0, &yymsp[-6].minor.yy0, yymsp[-5].minor.yy4, yymsp[-4].minor.yy90.a, yymsp[-4].minor.yy90.b, yymsp[-2].minor.yy259, yymsp[0].minor.yy314, yymsp[-10].minor.yy4, yymsp[-8].minor.yy4);
106284   yygotominor.yy0 = (yymsp[-6].minor.yy0.n==0?yymsp[-7].minor.yy0:yymsp[-6].minor.yy0);
106285 }
106286         break;
106287       case 272: /* trigger_time ::= BEFORE */
106288       case 275: /* trigger_time ::= */ yytestcase(yyruleno==275);
106289 { yygotominor.yy4 = TK_BEFORE; }
106290         break;
106291       case 273: /* trigger_time ::= AFTER */
106292 { yygotominor.yy4 = TK_AFTER;  }
106293         break;
106294       case 274: /* trigger_time ::= INSTEAD OF */
106295 { yygotominor.yy4 = TK_INSTEAD;}
106296         break;
106297       case 276: /* trigger_event ::= DELETE|INSERT */
106298       case 277: /* trigger_event ::= UPDATE */ yytestcase(yyruleno==277);
106299 {yygotominor.yy90.a = yymsp[0].major; yygotominor.yy90.b = 0;}
106300         break;
106301       case 278: /* trigger_event ::= UPDATE OF inscollist */
106302 {yygotominor.yy90.a = TK_UPDATE; yygotominor.yy90.b = yymsp[0].minor.yy384;}
106303         break;
106304       case 281: /* when_clause ::= */
106305       case 303: /* key_opt ::= */ yytestcase(yyruleno==303);
106306 { yygotominor.yy314 = 0; }
106307         break;
106308       case 282: /* when_clause ::= WHEN expr */
106309       case 304: /* key_opt ::= KEY expr */ yytestcase(yyruleno==304);
106310 { yygotominor.yy314 = yymsp[0].minor.yy118.pExpr; }
106311         break;
106312       case 283: /* trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI */
106313 {
106314   assert( yymsp[-2].minor.yy203!=0 );
106315   yymsp[-2].minor.yy203->pLast->pNext = yymsp[-1].minor.yy203;
106316   yymsp[-2].minor.yy203->pLast = yymsp[-1].minor.yy203;
106317   yygotominor.yy203 = yymsp[-2].minor.yy203;
106318 }
106319         break;
106320       case 284: /* trigger_cmd_list ::= trigger_cmd SEMI */
106321 {
106322   assert( yymsp[-1].minor.yy203!=0 );
106323   yymsp[-1].minor.yy203->pLast = yymsp[-1].minor.yy203;
106324   yygotominor.yy203 = yymsp[-1].minor.yy203;
106325 }
106326         break;
106327       case 286: /* trnm ::= nm DOT nm */
106328 {
106329   yygotominor.yy0 = yymsp[0].minor.yy0;
106330   sqlite3ErrorMsg(pParse,
106331         "qualified table names are not allowed on INSERT, UPDATE, and DELETE "
106332         "statements within triggers");
106333 }
106334         break;
106335       case 288: /* tridxby ::= INDEXED BY nm */
106336 {
106337   sqlite3ErrorMsg(pParse,
106338         "the INDEXED BY clause is not allowed on UPDATE or DELETE statements "
106339         "within triggers");
106340 }
106341         break;
106342       case 289: /* tridxby ::= NOT INDEXED */
106343 {
106344   sqlite3ErrorMsg(pParse,
106345         "the NOT INDEXED clause is not allowed on UPDATE or DELETE statements "
106346         "within triggers");
106347 }
106348         break;
106349       case 290: /* trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist where_opt */
106350 { yygotominor.yy203 = sqlite3TriggerUpdateStep(pParse->db, &yymsp[-4].minor.yy0, yymsp[-1].minor.yy322, yymsp[0].minor.yy314, yymsp[-5].minor.yy210); }
106351         break;
106352       case 291: /* trigger_cmd ::= insert_cmd INTO trnm inscollist_opt VALUES LP itemlist RP */
106353 {yygotominor.yy203 = sqlite3TriggerInsertStep(pParse->db, &yymsp[-5].minor.yy0, yymsp[-4].minor.yy384, yymsp[-1].minor.yy322, 0, yymsp[-7].minor.yy210);}
106354         break;
106355       case 292: /* trigger_cmd ::= insert_cmd INTO trnm inscollist_opt select */
106356 {yygotominor.yy203 = sqlite3TriggerInsertStep(pParse->db, &yymsp[-2].minor.yy0, yymsp[-1].minor.yy384, 0, yymsp[0].minor.yy387, yymsp[-4].minor.yy210);}
106357         break;
106358       case 293: /* trigger_cmd ::= DELETE FROM trnm tridxby where_opt */
106359 {yygotominor.yy203 = sqlite3TriggerDeleteStep(pParse->db, &yymsp[-2].minor.yy0, yymsp[0].minor.yy314);}
106360         break;
106361       case 294: /* trigger_cmd ::= select */
106362 {yygotominor.yy203 = sqlite3TriggerSelectStep(pParse->db, yymsp[0].minor.yy387); }
106363         break;
106364       case 295: /* expr ::= RAISE LP IGNORE RP */
106365 {
106366   yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_RAISE, 0, 0, 0);
106367   if( yygotominor.yy118.pExpr ){
106368     yygotominor.yy118.pExpr->affinity = OE_Ignore;
106369   }
106370   yygotominor.yy118.zStart = yymsp[-3].minor.yy0.z;
106371   yygotominor.yy118.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
106372 }
106373         break;
106374       case 296: /* expr ::= RAISE LP raisetype COMMA nm RP */
106375 {
106376   yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_RAISE, 0, 0, &yymsp[-1].minor.yy0);
106377   if( yygotominor.yy118.pExpr ) {
106378     yygotominor.yy118.pExpr->affinity = (char)yymsp[-3].minor.yy4;
106379   }
106380   yygotominor.yy118.zStart = yymsp[-5].minor.yy0.z;
106381   yygotominor.yy118.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
106382 }
106383         break;
106384       case 297: /* raisetype ::= ROLLBACK */
106385 {yygotominor.yy4 = OE_Rollback;}
106386         break;
106387       case 299: /* raisetype ::= FAIL */
106388 {yygotominor.yy4 = OE_Fail;}
106389         break;
106390       case 300: /* cmd ::= DROP TRIGGER ifexists fullname */
106391 {
106392   sqlite3DropTrigger(pParse,yymsp[0].minor.yy259,yymsp[-1].minor.yy4);
106393 }
106394         break;
106395       case 301: /* cmd ::= ATTACH database_kw_opt expr AS expr key_opt */
106396 {
106397   sqlite3Attach(pParse, yymsp[-3].minor.yy118.pExpr, yymsp[-1].minor.yy118.pExpr, yymsp[0].minor.yy314);
106398 }
106399         break;
106400       case 302: /* cmd ::= DETACH database_kw_opt expr */
106401 {
106402   sqlite3Detach(pParse, yymsp[0].minor.yy118.pExpr);
106403 }
106404         break;
106405       case 307: /* cmd ::= REINDEX */
106406 {sqlite3Reindex(pParse, 0, 0);}
106407         break;
106408       case 308: /* cmd ::= REINDEX nm dbnm */
106409 {sqlite3Reindex(pParse, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy0);}
106410         break;
106411       case 309: /* cmd ::= ANALYZE */
106412 {sqlite3Analyze(pParse, 0, 0);}
106413         break;
106414       case 310: /* cmd ::= ANALYZE nm dbnm */
106415 {sqlite3Analyze(pParse, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy0);}
106416         break;
106417       case 311: /* cmd ::= ALTER TABLE fullname RENAME TO nm */
106418 {
106419   sqlite3AlterRenameTable(pParse,yymsp[-3].minor.yy259,&yymsp[0].minor.yy0);
106420 }
106421         break;
106422       case 312: /* cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt column */
106423 {
106424   sqlite3AlterFinishAddColumn(pParse, &yymsp[0].minor.yy0);
106425 }
106426         break;
106427       case 313: /* add_column_fullname ::= fullname */
106428 {
106429   pParse->db->lookaside.bEnabled = 0;
106430   sqlite3AlterBeginAddColumn(pParse, yymsp[0].minor.yy259);
106431 }
106432         break;
106433       case 316: /* cmd ::= create_vtab */
106434 {sqlite3VtabFinishParse(pParse,0);}
106435         break;
106436       case 317: /* cmd ::= create_vtab LP vtabarglist RP */
106437 {sqlite3VtabFinishParse(pParse,&yymsp[0].minor.yy0);}
106438         break;
106439       case 318: /* create_vtab ::= createkw VIRTUAL TABLE nm dbnm USING nm */
106440 {
106441     sqlite3VtabBeginParse(pParse, &yymsp[-3].minor.yy0, &yymsp[-2].minor.yy0, &yymsp[0].minor.yy0);
106442 }
106443         break;
106444       case 321: /* vtabarg ::= */
106445 {sqlite3VtabArgInit(pParse);}
106446         break;
106447       case 323: /* vtabargtoken ::= ANY */
106448       case 324: /* vtabargtoken ::= lp anylist RP */ yytestcase(yyruleno==324);
106449       case 325: /* lp ::= LP */ yytestcase(yyruleno==325);
106450 {sqlite3VtabArgExtend(pParse,&yymsp[0].minor.yy0);}
106451         break;
106452       default:
106453       /* (0) input ::= cmdlist */ yytestcase(yyruleno==0);
106454       /* (1) cmdlist ::= cmdlist ecmd */ yytestcase(yyruleno==1);
106455       /* (2) cmdlist ::= ecmd */ yytestcase(yyruleno==2);
106456       /* (3) ecmd ::= SEMI */ yytestcase(yyruleno==3);
106457       /* (4) ecmd ::= explain cmdx SEMI */ yytestcase(yyruleno==4);
106458       /* (10) trans_opt ::= */ yytestcase(yyruleno==10);
106459       /* (11) trans_opt ::= TRANSACTION */ yytestcase(yyruleno==11);
106460       /* (12) trans_opt ::= TRANSACTION nm */ yytestcase(yyruleno==12);
106461       /* (20) savepoint_opt ::= SAVEPOINT */ yytestcase(yyruleno==20);
106462       /* (21) savepoint_opt ::= */ yytestcase(yyruleno==21);
106463       /* (25) cmd ::= create_table create_table_args */ yytestcase(yyruleno==25);
106464       /* (34) columnlist ::= columnlist COMMA column */ yytestcase(yyruleno==34);
106465       /* (35) columnlist ::= column */ yytestcase(yyruleno==35);
106466       /* (44) type ::= */ yytestcase(yyruleno==44);
106467       /* (51) signed ::= plus_num */ yytestcase(yyruleno==51);
106468       /* (52) signed ::= minus_num */ yytestcase(yyruleno==52);
106469       /* (53) carglist ::= carglist carg */ yytestcase(yyruleno==53);
106470       /* (54) carglist ::= */ yytestcase(yyruleno==54);
106471       /* (55) carg ::= CONSTRAINT nm ccons */ yytestcase(yyruleno==55);
106472       /* (56) carg ::= ccons */ yytestcase(yyruleno==56);
106473       /* (62) ccons ::= NULL onconf */ yytestcase(yyruleno==62);
106474       /* (90) conslist ::= conslist COMMA tcons */ yytestcase(yyruleno==90);
106475       /* (91) conslist ::= conslist tcons */ yytestcase(yyruleno==91);
106476       /* (92) conslist ::= tcons */ yytestcase(yyruleno==92);
106477       /* (93) tcons ::= CONSTRAINT nm */ yytestcase(yyruleno==93);
106478       /* (268) plus_opt ::= PLUS */ yytestcase(yyruleno==268);
106479       /* (269) plus_opt ::= */ yytestcase(yyruleno==269);
106480       /* (279) foreach_clause ::= */ yytestcase(yyruleno==279);
106481       /* (280) foreach_clause ::= FOR EACH ROW */ yytestcase(yyruleno==280);
106482       /* (287) tridxby ::= */ yytestcase(yyruleno==287);
106483       /* (305) database_kw_opt ::= DATABASE */ yytestcase(yyruleno==305);
106484       /* (306) database_kw_opt ::= */ yytestcase(yyruleno==306);
106485       /* (314) kwcolumn_opt ::= */ yytestcase(yyruleno==314);
106486       /* (315) kwcolumn_opt ::= COLUMNKW */ yytestcase(yyruleno==315);
106487       /* (319) vtabarglist ::= vtabarg */ yytestcase(yyruleno==319);
106488       /* (320) vtabarglist ::= vtabarglist COMMA vtabarg */ yytestcase(yyruleno==320);
106489       /* (322) vtabarg ::= vtabarg vtabargtoken */ yytestcase(yyruleno==322);
106490       /* (326) anylist ::= */ yytestcase(yyruleno==326);
106491       /* (327) anylist ::= anylist LP anylist RP */ yytestcase(yyruleno==327);
106492       /* (328) anylist ::= anylist ANY */ yytestcase(yyruleno==328);
106493         break;
106494   };
106495   yygoto = yyRuleInfo[yyruleno].lhs;
106496   yysize = yyRuleInfo[yyruleno].nrhs;
106497   yypParser->yyidx -= yysize;
106498   yyact = yy_find_reduce_action(yymsp[-yysize].stateno,(YYCODETYPE)yygoto);
106499   if( yyact < YYNSTATE ){
106500 #ifdef NDEBUG
106501     /* If we are not debugging and the reduce action popped at least
106502     ** one element off the stack, then we can push the new element back
106503     ** onto the stack here, and skip the stack overflow test in yy_shift().
106504     ** That gives a significant speed improvement. */
106505     if( yysize ){
106506       yypParser->yyidx++;
106507       yymsp -= yysize-1;
106508       yymsp->stateno = (YYACTIONTYPE)yyact;
106509       yymsp->major = (YYCODETYPE)yygoto;
106510       yymsp->minor = yygotominor;
106511     }else
106512 #endif
106513     {
106514       yy_shift(yypParser,yyact,yygoto,&yygotominor);
106515     }
106516   }else{
106517     assert( yyact == YYNSTATE + YYNRULE + 1 );
106518     yy_accept(yypParser);
106519   }
106520 }
106521 
106522 /*
106523 ** The following code executes when the parse fails
106524 */
106525 #ifndef YYNOERRORRECOVERY
106526 static void yy_parse_failed(
106527   yyParser *yypParser           /* The parser */
106528 ){
106529   sqlite3ParserARG_FETCH;
106530 #ifndef NDEBUG
106531   if( yyTraceFILE ){
106532     fprintf(yyTraceFILE,"%sFail!\n",yyTracePrompt);
106533   }
106534 #endif
106535   while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
106536   /* Here code is inserted which will be executed whenever the
106537   ** parser fails */
106538   sqlite3ParserARG_STORE; /* Suppress warning about unused %extra_argument variable */
106539 }
106540 #endif /* YYNOERRORRECOVERY */
106541 
106542 /*
106543 ** The following code executes when a syntax error first occurs.
106544 */
106545 static void yy_syntax_error(
106546   yyParser *yypParser,           /* The parser */
106547   int yymajor,                   /* The major type of the error token */
106548   YYMINORTYPE yyminor            /* The minor type of the error token */
106549 ){
106550   sqlite3ParserARG_FETCH;
106551 #define TOKEN (yyminor.yy0)
106552 
106553   UNUSED_PARAMETER(yymajor);  /* Silence some compiler warnings */
106554   assert( TOKEN.z[0] );  /* The tokenizer always gives us a token */
106555   sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &TOKEN);
106556   pParse->parseError = 1;
106557   sqlite3ParserARG_STORE; /* Suppress warning about unused %extra_argument variable */
106558 }
106559 
106560 /*
106561 ** The following is executed when the parser accepts
106562 */
106563 static void yy_accept(
106564   yyParser *yypParser           /* The parser */
106565 ){
106566   sqlite3ParserARG_FETCH;
106567 #ifndef NDEBUG
106568   if( yyTraceFILE ){
106569     fprintf(yyTraceFILE,"%sAccept!\n",yyTracePrompt);
106570   }
106571 #endif
106572   while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
106573   /* Here code is inserted which will be executed whenever the
106574   ** parser accepts */
106575   sqlite3ParserARG_STORE; /* Suppress warning about unused %extra_argument variable */
106576 }
106577 
106578 /* The main parser program.
106579 ** The first argument is a pointer to a structure obtained from
106580 ** "sqlite3ParserAlloc" which describes the current state of the parser.
106581 ** The second argument is the major token number.  The third is
106582 ** the minor token.  The fourth optional argument is whatever the
106583 ** user wants (and specified in the grammar) and is available for
106584 ** use by the action routines.
106585 **
106586 ** Inputs:
106587 ** <ul>
106588 ** <li> A pointer to the parser (an opaque structure.)
106589 ** <li> The major token number.
106590 ** <li> The minor token number.
106591 ** <li> An option argument of a grammar-specified type.
106592 ** </ul>
106593 **
106594 ** Outputs:
106595 ** None.
106596 */
106597 SQLITE_PRIVATE void sqlite3Parser(
106598   void *yyp,                   /* The parser */
106599   int yymajor,                 /* The major token code number */
106600   sqlite3ParserTOKENTYPE yyminor       /* The value for the token */
106601   sqlite3ParserARG_PDECL               /* Optional %extra_argument parameter */
106602 ){
106603   YYMINORTYPE yyminorunion;
106604   int yyact;            /* The parser action. */
106605   int yyendofinput;     /* True if we are at the end of input */
106606 #ifdef YYERRORSYMBOL
106607   int yyerrorhit = 0;   /* True if yymajor has invoked an error */
106608 #endif
106609   yyParser *yypParser;  /* The parser */
106610 
106611   /* (re)initialize the parser, if necessary */
106612   yypParser = (yyParser*)yyp;
106613   if( yypParser->yyidx<0 ){
106614 #if YYSTACKDEPTH<=0
106615     if( yypParser->yystksz <=0 ){
106616       /*memset(&yyminorunion, 0, sizeof(yyminorunion));*/
106617       yyminorunion = yyzerominor;
106618       yyStackOverflow(yypParser, &yyminorunion);
106619       return;
106620     }
106621 #endif
106622     yypParser->yyidx = 0;
106623     yypParser->yyerrcnt = -1;
106624     yypParser->yystack[0].stateno = 0;
106625     yypParser->yystack[0].major = 0;
106626   }
106627   yyminorunion.yy0 = yyminor;
106628   yyendofinput = (yymajor==0);
106629   sqlite3ParserARG_STORE;
106630 
106631 #ifndef NDEBUG
106632   if( yyTraceFILE ){
106633     fprintf(yyTraceFILE,"%sInput %s\n",yyTracePrompt,yyTokenName[yymajor]);
106634   }
106635 #endif
106636 
106637   do{
106638     yyact = yy_find_shift_action(yypParser,(YYCODETYPE)yymajor);
106639     if( yyact<YYNSTATE ){
106640       assert( !yyendofinput );  /* Impossible to shift the $ token */
106641       yy_shift(yypParser,yyact,yymajor,&yyminorunion);
106642       yypParser->yyerrcnt--;
106643       yymajor = YYNOCODE;
106644     }else if( yyact < YYNSTATE + YYNRULE ){
106645       yy_reduce(yypParser,yyact-YYNSTATE);
106646     }else{
106647       assert( yyact == YY_ERROR_ACTION );
106648 #ifdef YYERRORSYMBOL
106649       int yymx;
106650 #endif
106651 #ifndef NDEBUG
106652       if( yyTraceFILE ){
106653         fprintf(yyTraceFILE,"%sSyntax Error!\n",yyTracePrompt);
106654       }
106655 #endif
106656 #ifdef YYERRORSYMBOL
106657       /* A syntax error has occurred.
106658       ** The response to an error depends upon whether or not the
106659       ** grammar defines an error token "ERROR".
106660       **
106661       ** This is what we do if the grammar does define ERROR:
106662       **
106663       **  * Call the %syntax_error function.
106664       **
106665       **  * Begin popping the stack until we enter a state where
106666       **    it is legal to shift the error symbol, then shift
106667       **    the error symbol.
106668       **
106669       **  * Set the error count to three.
106670       **
106671       **  * Begin accepting and shifting new tokens.  No new error
106672       **    processing will occur until three tokens have been
106673       **    shifted successfully.
106674       **
106675       */
106676       if( yypParser->yyerrcnt<0 ){
106677         yy_syntax_error(yypParser,yymajor,yyminorunion);
106678       }
106679       yymx = yypParser->yystack[yypParser->yyidx].major;
106680       if( yymx==YYERRORSYMBOL || yyerrorhit ){
106681 #ifndef NDEBUG
106682         if( yyTraceFILE ){
106683           fprintf(yyTraceFILE,"%sDiscard input token %s\n",
106684              yyTracePrompt,yyTokenName[yymajor]);
106685         }
106686 #endif
106687         yy_destructor(yypParser, (YYCODETYPE)yymajor,&yyminorunion);
106688         yymajor = YYNOCODE;
106689       }else{
106690          while(
106691           yypParser->yyidx >= 0 &&
106692           yymx != YYERRORSYMBOL &&
106693           (yyact = yy_find_reduce_action(
106694                         yypParser->yystack[yypParser->yyidx].stateno,
106695                         YYERRORSYMBOL)) >= YYNSTATE
106696         ){
106697           yy_pop_parser_stack(yypParser);
106698         }
106699         if( yypParser->yyidx < 0 || yymajor==0 ){
106700           yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
106701           yy_parse_failed(yypParser);
106702           yymajor = YYNOCODE;
106703         }else if( yymx!=YYERRORSYMBOL ){
106704           YYMINORTYPE u2;
106705           u2.YYERRSYMDT = 0;
106706           yy_shift(yypParser,yyact,YYERRORSYMBOL,&u2);
106707         }
106708       }
106709       yypParser->yyerrcnt = 3;
106710       yyerrorhit = 1;
106711 #elif defined(YYNOERRORRECOVERY)
106712       /* If the YYNOERRORRECOVERY macro is defined, then do not attempt to
106713       ** do any kind of error recovery.  Instead, simply invoke the syntax
106714       ** error routine and continue going as if nothing had happened.
106715       **
106716       ** Applications can set this macro (for example inside %include) if
106717       ** they intend to abandon the parse upon the first syntax error seen.
106718       */
106719       yy_syntax_error(yypParser,yymajor,yyminorunion);
106720       yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
106721       yymajor = YYNOCODE;
106722 
106723 #else  /* YYERRORSYMBOL is not defined */
106724       /* This is what we do if the grammar does not define ERROR:
106725       **
106726       **  * Report an error message, and throw away the input token.
106727       **
106728       **  * If the input token is $, then fail the parse.
106729       **
106730       ** As before, subsequent error messages are suppressed until
106731       ** three input tokens have been successfully shifted.
106732       */
106733       if( yypParser->yyerrcnt<=0 ){
106734         yy_syntax_error(yypParser,yymajor,yyminorunion);
106735       }
106736       yypParser->yyerrcnt = 3;
106737       yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
106738       if( yyendofinput ){
106739         yy_parse_failed(yypParser);
106740       }
106741       yymajor = YYNOCODE;
106742 #endif
106743     }
106744   }while( yymajor!=YYNOCODE && yypParser->yyidx>=0 );
106745   return;
106746 }
106747 
106748 /************** End of parse.c ***********************************************/
106749 /************** Begin file tokenize.c ****************************************/
106750 /*
106751 ** 2001 September 15
106752 **
106753 ** The author disclaims copyright to this source code.  In place of
106754 ** a legal notice, here is a blessing:
106755 **
106756 **    May you do good and not evil.
106757 **    May you find forgiveness for yourself and forgive others.
106758 **    May you share freely, never taking more than you give.
106759 **
106760 *************************************************************************
106761 ** An tokenizer for SQL
106762 **
106763 ** This file contains C code that splits an SQL input string up into
106764 ** individual tokens and sends those tokens one-by-one over to the
106765 ** parser for analysis.
106766 */
106767 
106768 /*
106769 ** The charMap() macro maps alphabetic characters into their
106770 ** lower-case ASCII equivalent.  On ASCII machines, this is just
106771 ** an upper-to-lower case map.  On EBCDIC machines we also need
106772 ** to adjust the encoding.  Only alphabetic characters and underscores
106773 ** need to be translated.
106774 */
106775 #ifdef SQLITE_ASCII
106776 # define charMap(X) sqlite3UpperToLower[(unsigned char)X]
106777 #endif
106778 #ifdef SQLITE_EBCDIC
106779 # define charMap(X) ebcdicToAscii[(unsigned char)X]
106780 const unsigned char ebcdicToAscii[] = {
106781 /* 0   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F */
106782    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 0x */
106783    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 1x */
106784    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 2x */
106785    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 3x */
106786    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 4x */
106787    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 5x */
106788    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 95,  0,  0,  /* 6x */
106789    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 7x */
106790    0, 97, 98, 99,100,101,102,103,104,105,  0,  0,  0,  0,  0,  0,  /* 8x */
106791    0,106,107,108,109,110,111,112,113,114,  0,  0,  0,  0,  0,  0,  /* 9x */
106792    0,  0,115,116,117,118,119,120,121,122,  0,  0,  0,  0,  0,  0,  /* Ax */
106793    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* Bx */
106794    0, 97, 98, 99,100,101,102,103,104,105,  0,  0,  0,  0,  0,  0,  /* Cx */
106795    0,106,107,108,109,110,111,112,113,114,  0,  0,  0,  0,  0,  0,  /* Dx */
106796    0,  0,115,116,117,118,119,120,121,122,  0,  0,  0,  0,  0,  0,  /* Ex */
106797    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* Fx */
106798 };
106799 #endif
106800 
106801 /*
106802 ** The sqlite3KeywordCode function looks up an identifier to determine if
106803 ** it is a keyword.  If it is a keyword, the token code of that keyword is
106804 ** returned.  If the input is not a keyword, TK_ID is returned.
106805 **
106806 ** The implementation of this routine was generated by a program,
106807 ** mkkeywordhash.h, located in the tool subdirectory of the distribution.
106808 ** The output of the mkkeywordhash.c program is written into a file
106809 ** named keywordhash.h and then included into this source file by
106810 ** the #include below.
106811 */
106812 /************** Include keywordhash.h in the middle of tokenize.c ************/
106813 /************** Begin file keywordhash.h *************************************/
106814 /***** This file contains automatically generated code ******
106815 **
106816 ** The code in this file has been automatically generated by
106817 **
106818 **   sqlite/tool/mkkeywordhash.c
106819 **
106820 ** The code in this file implements a function that determines whether
106821 ** or not a given identifier is really an SQL keyword.  The same thing
106822 ** might be implemented more directly using a hand-written hash table.
106823 ** But by using this automatically generated code, the size of the code
106824 ** is substantially reduced.  This is important for embedded applications
106825 ** on platforms with limited memory.
106826 */
106827 /* Hash score: 175 */
106828 static int keywordCode(const char *z, int n){
106829   /* zText[] encodes 811 bytes of keywords in 541 bytes */
106830   /*   REINDEXEDESCAPEACHECKEYBEFOREIGNOREGEXPLAINSTEADDATABASELECT       */
106831   /*   ABLEFTHENDEFERRABLELSEXCEPTRANSACTIONATURALTERAISEXCLUSIVE         */
106832   /*   XISTSAVEPOINTERSECTRIGGEREFERENCESCONSTRAINTOFFSETEMPORARY         */
106833   /*   UNIQUERYATTACHAVINGROUPDATEBEGINNERELEASEBETWEENOTNULLIKE          */
106834   /*   CASCADELETECASECOLLATECREATECURRENT_DATEDETACHIMMEDIATEJOIN        */
106835   /*   SERTMATCHPLANALYZEPRAGMABORTVALUESVIRTUALIMITWHENWHERENAME         */
106836   /*   AFTEREPLACEANDEFAULTAUTOINCREMENTCASTCOLUMNCOMMITCONFLICTCROSS     */
106837   /*   CURRENT_TIMESTAMPRIMARYDEFERREDISTINCTDROPFAILFROMFULLGLOBYIF      */
106838   /*   ISNULLORDERESTRICTOUTERIGHTROLLBACKROWUNIONUSINGVACUUMVIEW         */
106839   /*   INITIALLY                                                          */
106840   static const char zText[540] = {
106841     'R','E','I','N','D','E','X','E','D','E','S','C','A','P','E','A','C','H',
106842     'E','C','K','E','Y','B','E','F','O','R','E','I','G','N','O','R','E','G',
106843     'E','X','P','L','A','I','N','S','T','E','A','D','D','A','T','A','B','A',
106844     'S','E','L','E','C','T','A','B','L','E','F','T','H','E','N','D','E','F',
106845     'E','R','R','A','B','L','E','L','S','E','X','C','E','P','T','R','A','N',
106846     'S','A','C','T','I','O','N','A','T','U','R','A','L','T','E','R','A','I',
106847     'S','E','X','C','L','U','S','I','V','E','X','I','S','T','S','A','V','E',
106848     'P','O','I','N','T','E','R','S','E','C','T','R','I','G','G','E','R','E',
106849     'F','E','R','E','N','C','E','S','C','O','N','S','T','R','A','I','N','T',
106850     'O','F','F','S','E','T','E','M','P','O','R','A','R','Y','U','N','I','Q',
106851     'U','E','R','Y','A','T','T','A','C','H','A','V','I','N','G','R','O','U',
106852     'P','D','A','T','E','B','E','G','I','N','N','E','R','E','L','E','A','S',
106853     'E','B','E','T','W','E','E','N','O','T','N','U','L','L','I','K','E','C',
106854     'A','S','C','A','D','E','L','E','T','E','C','A','S','E','C','O','L','L',
106855     'A','T','E','C','R','E','A','T','E','C','U','R','R','E','N','T','_','D',
106856     'A','T','E','D','E','T','A','C','H','I','M','M','E','D','I','A','T','E',
106857     'J','O','I','N','S','E','R','T','M','A','T','C','H','P','L','A','N','A',
106858     'L','Y','Z','E','P','R','A','G','M','A','B','O','R','T','V','A','L','U',
106859     'E','S','V','I','R','T','U','A','L','I','M','I','T','W','H','E','N','W',
106860     'H','E','R','E','N','A','M','E','A','F','T','E','R','E','P','L','A','C',
106861     'E','A','N','D','E','F','A','U','L','T','A','U','T','O','I','N','C','R',
106862     'E','M','E','N','T','C','A','S','T','C','O','L','U','M','N','C','O','M',
106863     'M','I','T','C','O','N','F','L','I','C','T','C','R','O','S','S','C','U',
106864     'R','R','E','N','T','_','T','I','M','E','S','T','A','M','P','R','I','M',
106865     'A','R','Y','D','E','F','E','R','R','E','D','I','S','T','I','N','C','T',
106866     'D','R','O','P','F','A','I','L','F','R','O','M','F','U','L','L','G','L',
106867     'O','B','Y','I','F','I','S','N','U','L','L','O','R','D','E','R','E','S',
106868     'T','R','I','C','T','O','U','T','E','R','I','G','H','T','R','O','L','L',
106869     'B','A','C','K','R','O','W','U','N','I','O','N','U','S','I','N','G','V',
106870     'A','C','U','U','M','V','I','E','W','I','N','I','T','I','A','L','L','Y',
106871   };
106872   static const unsigned char aHash[127] = {
106873       72, 101, 114,  70,   0,  45,   0,   0,  78,   0,  73,   0,   0,
106874       42,  12,  74,  15,   0, 113,  81,  50, 108,   0,  19,   0,   0,
106875      118,   0, 116, 111,   0,  22,  89,   0,   9,   0,   0,  66,  67,
106876        0,  65,   6,   0,  48,  86,  98,   0, 115,  97,   0,   0,  44,
106877        0,  99,  24,   0,  17,   0, 119,  49,  23,   0,   5, 106,  25,
106878       92,   0,   0, 121, 102,  56, 120,  53,  28,  51,   0,  87,   0,
106879       96,  26,   0,  95,   0,   0,   0,  91,  88,  93,  84, 105,  14,
106880       39, 104,   0,  77,   0,  18,  85, 107,  32,   0, 117,  76, 109,
106881       58,  46,  80,   0,   0,  90,  40,   0, 112,   0,  36,   0,   0,
106882       29,   0,  82,  59,  60,   0,  20,  57,   0,  52,
106883   };
106884   static const unsigned char aNext[121] = {
106885        0,   0,   0,   0,   4,   0,   0,   0,   0,   0,   0,   0,   0,
106886        0,   2,   0,   0,   0,   0,   0,   0,  13,   0,   0,   0,   0,
106887        0,   7,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
106888        0,   0,   0,   0,  33,   0,  21,   0,   0,   0,  43,   3,  47,
106889        0,   0,   0,   0,  30,   0,  54,   0,  38,   0,   0,   0,   1,
106890       62,   0,   0,  63,   0,  41,   0,   0,   0,   0,   0,   0,   0,
106891       61,   0,   0,   0,   0,  31,  55,  16,  34,  10,   0,   0,   0,
106892        0,   0,   0,   0,  11,  68,  75,   0,   8,   0, 100,  94,   0,
106893      103,   0,  83,   0,  71,   0,   0, 110,  27,  37,  69,  79,   0,
106894       35,  64,   0,   0,
106895   };
106896   static const unsigned char aLen[121] = {
106897        7,   7,   5,   4,   6,   4,   5,   3,   6,   7,   3,   6,   6,
106898        7,   7,   3,   8,   2,   6,   5,   4,   4,   3,  10,   4,   6,
106899       11,   6,   2,   7,   5,   5,   9,   6,   9,   9,   7,  10,  10,
106900        4,   6,   2,   3,   9,   4,   2,   6,   5,   6,   6,   5,   6,
106901        5,   5,   7,   7,   7,   3,   2,   4,   4,   7,   3,   6,   4,
106902        7,   6,  12,   6,   9,   4,   6,   5,   4,   7,   6,   5,   6,
106903        7,   5,   4,   5,   6,   5,   7,   3,   7,  13,   2,   2,   4,
106904        6,   6,   8,   5,  17,  12,   7,   8,   8,   2,   4,   4,   4,
106905        4,   4,   2,   2,   6,   5,   8,   5,   5,   8,   3,   5,   5,
106906        6,   4,   9,   3,
106907   };
106908   static const unsigned short int aOffset[121] = {
106909        0,   2,   2,   8,   9,  14,  16,  20,  23,  25,  25,  29,  33,
106910       36,  41,  46,  48,  53,  54,  59,  62,  65,  67,  69,  78,  81,
106911       86,  91,  95,  96, 101, 105, 109, 117, 122, 128, 136, 142, 152,
106912      159, 162, 162, 165, 167, 167, 171, 176, 179, 184, 189, 194, 197,
106913      203, 206, 210, 217, 223, 223, 223, 226, 229, 233, 234, 238, 244,
106914      248, 255, 261, 273, 279, 288, 290, 296, 301, 303, 310, 315, 320,
106915      326, 332, 337, 341, 344, 350, 354, 361, 363, 370, 372, 374, 383,
106916      387, 393, 399, 407, 412, 412, 428, 435, 442, 443, 450, 454, 458,
106917      462, 466, 469, 471, 473, 479, 483, 491, 495, 500, 508, 511, 516,
106918      521, 527, 531, 536,
106919   };
106920   static const unsigned char aCode[121] = {
106921     TK_REINDEX,    TK_INDEXED,    TK_INDEX,      TK_DESC,       TK_ESCAPE,
106922     TK_EACH,       TK_CHECK,      TK_KEY,        TK_BEFORE,     TK_FOREIGN,
106923     TK_FOR,        TK_IGNORE,     TK_LIKE_KW,    TK_EXPLAIN,    TK_INSTEAD,
106924     TK_ADD,        TK_DATABASE,   TK_AS,         TK_SELECT,     TK_TABLE,
106925     TK_JOIN_KW,    TK_THEN,       TK_END,        TK_DEFERRABLE, TK_ELSE,
106926     TK_EXCEPT,     TK_TRANSACTION,TK_ACTION,     TK_ON,         TK_JOIN_KW,
106927     TK_ALTER,      TK_RAISE,      TK_EXCLUSIVE,  TK_EXISTS,     TK_SAVEPOINT,
106928     TK_INTERSECT,  TK_TRIGGER,    TK_REFERENCES, TK_CONSTRAINT, TK_INTO,
106929     TK_OFFSET,     TK_OF,         TK_SET,        TK_TEMP,       TK_TEMP,
106930     TK_OR,         TK_UNIQUE,     TK_QUERY,      TK_ATTACH,     TK_HAVING,
106931     TK_GROUP,      TK_UPDATE,     TK_BEGIN,      TK_JOIN_KW,    TK_RELEASE,
106932     TK_BETWEEN,    TK_NOTNULL,    TK_NOT,        TK_NO,         TK_NULL,
106933     TK_LIKE_KW,    TK_CASCADE,    TK_ASC,        TK_DELETE,     TK_CASE,
106934     TK_COLLATE,    TK_CREATE,     TK_CTIME_KW,   TK_DETACH,     TK_IMMEDIATE,
106935     TK_JOIN,       TK_INSERT,     TK_MATCH,      TK_PLAN,       TK_ANALYZE,
106936     TK_PRAGMA,     TK_ABORT,      TK_VALUES,     TK_VIRTUAL,    TK_LIMIT,
106937     TK_WHEN,       TK_WHERE,      TK_RENAME,     TK_AFTER,      TK_REPLACE,
106938     TK_AND,        TK_DEFAULT,    TK_AUTOINCR,   TK_TO,         TK_IN,
106939     TK_CAST,       TK_COLUMNKW,   TK_COMMIT,     TK_CONFLICT,   TK_JOIN_KW,
106940     TK_CTIME_KW,   TK_CTIME_KW,   TK_PRIMARY,    TK_DEFERRED,   TK_DISTINCT,
106941     TK_IS,         TK_DROP,       TK_FAIL,       TK_FROM,       TK_JOIN_KW,
106942     TK_LIKE_KW,    TK_BY,         TK_IF,         TK_ISNULL,     TK_ORDER,
106943     TK_RESTRICT,   TK_JOIN_KW,    TK_JOIN_KW,    TK_ROLLBACK,   TK_ROW,
106944     TK_UNION,      TK_USING,      TK_VACUUM,     TK_VIEW,       TK_INITIALLY,
106945     TK_ALL,
106946   };
106947   int h, i;
106948   if( n<2 ) return TK_ID;
106949   h = ((charMap(z[0])*4) ^
106950       (charMap(z[n-1])*3) ^
106951       n) % 127;
106952   for(i=((int)aHash[h])-1; i>=0; i=((int)aNext[i])-1){
106953     if( aLen[i]==n && sqlite3StrNICmp(&zText[aOffset[i]],z,n)==0 ){
106954       testcase( i==0 ); /* REINDEX */
106955       testcase( i==1 ); /* INDEXED */
106956       testcase( i==2 ); /* INDEX */
106957       testcase( i==3 ); /* DESC */
106958       testcase( i==4 ); /* ESCAPE */
106959       testcase( i==5 ); /* EACH */
106960       testcase( i==6 ); /* CHECK */
106961       testcase( i==7 ); /* KEY */
106962       testcase( i==8 ); /* BEFORE */
106963       testcase( i==9 ); /* FOREIGN */
106964       testcase( i==10 ); /* FOR */
106965       testcase( i==11 ); /* IGNORE */
106966       testcase( i==12 ); /* REGEXP */
106967       testcase( i==13 ); /* EXPLAIN */
106968       testcase( i==14 ); /* INSTEAD */
106969       testcase( i==15 ); /* ADD */
106970       testcase( i==16 ); /* DATABASE */
106971       testcase( i==17 ); /* AS */
106972       testcase( i==18 ); /* SELECT */
106973       testcase( i==19 ); /* TABLE */
106974       testcase( i==20 ); /* LEFT */
106975       testcase( i==21 ); /* THEN */
106976       testcase( i==22 ); /* END */
106977       testcase( i==23 ); /* DEFERRABLE */
106978       testcase( i==24 ); /* ELSE */
106979       testcase( i==25 ); /* EXCEPT */
106980       testcase( i==26 ); /* TRANSACTION */
106981       testcase( i==27 ); /* ACTION */
106982       testcase( i==28 ); /* ON */
106983       testcase( i==29 ); /* NATURAL */
106984       testcase( i==30 ); /* ALTER */
106985       testcase( i==31 ); /* RAISE */
106986       testcase( i==32 ); /* EXCLUSIVE */
106987       testcase( i==33 ); /* EXISTS */
106988       testcase( i==34 ); /* SAVEPOINT */
106989       testcase( i==35 ); /* INTERSECT */
106990       testcase( i==36 ); /* TRIGGER */
106991       testcase( i==37 ); /* REFERENCES */
106992       testcase( i==38 ); /* CONSTRAINT */
106993       testcase( i==39 ); /* INTO */
106994       testcase( i==40 ); /* OFFSET */
106995       testcase( i==41 ); /* OF */
106996       testcase( i==42 ); /* SET */
106997       testcase( i==43 ); /* TEMPORARY */
106998       testcase( i==44 ); /* TEMP */
106999       testcase( i==45 ); /* OR */
107000       testcase( i==46 ); /* UNIQUE */
107001       testcase( i==47 ); /* QUERY */
107002       testcase( i==48 ); /* ATTACH */
107003       testcase( i==49 ); /* HAVING */
107004       testcase( i==50 ); /* GROUP */
107005       testcase( i==51 ); /* UPDATE */
107006       testcase( i==52 ); /* BEGIN */
107007       testcase( i==53 ); /* INNER */
107008       testcase( i==54 ); /* RELEASE */
107009       testcase( i==55 ); /* BETWEEN */
107010       testcase( i==56 ); /* NOTNULL */
107011       testcase( i==57 ); /* NOT */
107012       testcase( i==58 ); /* NO */
107013       testcase( i==59 ); /* NULL */
107014       testcase( i==60 ); /* LIKE */
107015       testcase( i==61 ); /* CASCADE */
107016       testcase( i==62 ); /* ASC */
107017       testcase( i==63 ); /* DELETE */
107018       testcase( i==64 ); /* CASE */
107019       testcase( i==65 ); /* COLLATE */
107020       testcase( i==66 ); /* CREATE */
107021       testcase( i==67 ); /* CURRENT_DATE */
107022       testcase( i==68 ); /* DETACH */
107023       testcase( i==69 ); /* IMMEDIATE */
107024       testcase( i==70 ); /* JOIN */
107025       testcase( i==71 ); /* INSERT */
107026       testcase( i==72 ); /* MATCH */
107027       testcase( i==73 ); /* PLAN */
107028       testcase( i==74 ); /* ANALYZE */
107029       testcase( i==75 ); /* PRAGMA */
107030       testcase( i==76 ); /* ABORT */
107031       testcase( i==77 ); /* VALUES */
107032       testcase( i==78 ); /* VIRTUAL */
107033       testcase( i==79 ); /* LIMIT */
107034       testcase( i==80 ); /* WHEN */
107035       testcase( i==81 ); /* WHERE */
107036       testcase( i==82 ); /* RENAME */
107037       testcase( i==83 ); /* AFTER */
107038       testcase( i==84 ); /* REPLACE */
107039       testcase( i==85 ); /* AND */
107040       testcase( i==86 ); /* DEFAULT */
107041       testcase( i==87 ); /* AUTOINCREMENT */
107042       testcase( i==88 ); /* TO */
107043       testcase( i==89 ); /* IN */
107044       testcase( i==90 ); /* CAST */
107045       testcase( i==91 ); /* COLUMN */
107046       testcase( i==92 ); /* COMMIT */
107047       testcase( i==93 ); /* CONFLICT */
107048       testcase( i==94 ); /* CROSS */
107049       testcase( i==95 ); /* CURRENT_TIMESTAMP */
107050       testcase( i==96 ); /* CURRENT_TIME */
107051       testcase( i==97 ); /* PRIMARY */
107052       testcase( i==98 ); /* DEFERRED */
107053       testcase( i==99 ); /* DISTINCT */
107054       testcase( i==100 ); /* IS */
107055       testcase( i==101 ); /* DROP */
107056       testcase( i==102 ); /* FAIL */
107057       testcase( i==103 ); /* FROM */
107058       testcase( i==104 ); /* FULL */
107059       testcase( i==105 ); /* GLOB */
107060       testcase( i==106 ); /* BY */
107061       testcase( i==107 ); /* IF */
107062       testcase( i==108 ); /* ISNULL */
107063       testcase( i==109 ); /* ORDER */
107064       testcase( i==110 ); /* RESTRICT */
107065       testcase( i==111 ); /* OUTER */
107066       testcase( i==112 ); /* RIGHT */
107067       testcase( i==113 ); /* ROLLBACK */
107068       testcase( i==114 ); /* ROW */
107069       testcase( i==115 ); /* UNION */
107070       testcase( i==116 ); /* USING */
107071       testcase( i==117 ); /* VACUUM */
107072       testcase( i==118 ); /* VIEW */
107073       testcase( i==119 ); /* INITIALLY */
107074       testcase( i==120 ); /* ALL */
107075       return aCode[i];
107076     }
107077   }
107078   return TK_ID;
107079 }
107080 SQLITE_PRIVATE int sqlite3KeywordCode(const unsigned char *z, int n){
107081   return keywordCode((char*)z, n);
107082 }
107083 #define SQLITE_N_KEYWORD 121
107084 
107085 /************** End of keywordhash.h *****************************************/
107086 /************** Continuing where we left off in tokenize.c *******************/
107087 
107088 
107089 /*
107090 ** If X is a character that can be used in an identifier then
107091 ** IdChar(X) will be true.  Otherwise it is false.
107092 **
107093 ** For ASCII, any character with the high-order bit set is
107094 ** allowed in an identifier.  For 7-bit characters,
107095 ** sqlite3IsIdChar[X] must be 1.
107096 **
107097 ** For EBCDIC, the rules are more complex but have the same
107098 ** end result.
107099 **
107100 ** Ticket #1066.  the SQL standard does not allow '$' in the
107101 ** middle of identfiers.  But many SQL implementations do.
107102 ** SQLite will allow '$' in identifiers for compatibility.
107103 ** But the feature is undocumented.
107104 */
107105 #ifdef SQLITE_ASCII
107106 #define IdChar(C)  ((sqlite3CtypeMap[(unsigned char)C]&0x46)!=0)
107107 #endif
107108 #ifdef SQLITE_EBCDIC
107109 SQLITE_PRIVATE const char sqlite3IsEbcdicIdChar[] = {
107110 /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
107111     0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,  /* 4x */
107112     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0,  /* 5x */
107113     0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0,  /* 6x */
107114     0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0,  /* 7x */
107115     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0,  /* 8x */
107116     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0,  /* 9x */
107117     1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0,  /* Ax */
107118     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /* Bx */
107119     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,  /* Cx */
107120     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,  /* Dx */
107121     0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,  /* Ex */
107122     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0,  /* Fx */
107123 };
107124 #define IdChar(C)  (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
107125 #endif
107126 
107127 
107128 /*
107129 ** Return the length of the token that begins at z[0].
107130 ** Store the token type in *tokenType before returning.
107131 */
107132 SQLITE_PRIVATE int sqlite3GetToken(const unsigned char *z, int *tokenType){
107133   int i, c;
107134   switch( *z ){
107135     case ' ': case '\t': case '\n': case '\f': case '\r': {
107136       testcase( z[0]==' ' );
107137       testcase( z[0]=='\t' );
107138       testcase( z[0]=='\n' );
107139       testcase( z[0]=='\f' );
107140       testcase( z[0]=='\r' );
107141       for(i=1; sqlite3Isspace(z[i]); i++){}
107142       *tokenType = TK_SPACE;
107143       return i;
107144     }
107145     case '-': {
107146       if( z[1]=='-' ){
107147         /* IMP: R-15891-05542 -- syntax diagram for comments */
107148         for(i=2; (c=z[i])!=0 && c!='\n'; i++){}
107149         *tokenType = TK_SPACE;   /* IMP: R-22934-25134 */
107150         return i;
107151       }
107152       *tokenType = TK_MINUS;
107153       return 1;
107154     }
107155     case '(': {
107156       *tokenType = TK_LP;
107157       return 1;
107158     }
107159     case ')': {
107160       *tokenType = TK_RP;
107161       return 1;
107162     }
107163     case ';': {
107164       *tokenType = TK_SEMI;
107165       return 1;
107166     }
107167     case '+': {
107168       *tokenType = TK_PLUS;
107169       return 1;
107170     }
107171     case '*': {
107172       *tokenType = TK_STAR;
107173       return 1;
107174     }
107175     case '/': {
107176       if( z[1]!='*' || z[2]==0 ){
107177         *tokenType = TK_SLASH;
107178         return 1;
107179       }
107180       /* IMP: R-15891-05542 -- syntax diagram for comments */
107181       for(i=3, c=z[2]; (c!='*' || z[i]!='/') && (c=z[i])!=0; i++){}
107182       if( c ) i++;
107183       *tokenType = TK_SPACE;   /* IMP: R-22934-25134 */
107184       return i;
107185     }
107186     case '%': {
107187       *tokenType = TK_REM;
107188       return 1;
107189     }
107190     case '=': {
107191       *tokenType = TK_EQ;
107192       return 1 + (z[1]=='=');
107193     }
107194     case '<': {
107195       if( (c=z[1])=='=' ){
107196         *tokenType = TK_LE;
107197         return 2;
107198       }else if( c=='>' ){
107199         *tokenType = TK_NE;
107200         return 2;
107201       }else if( c=='<' ){
107202         *tokenType = TK_LSHIFT;
107203         return 2;
107204       }else{
107205         *tokenType = TK_LT;
107206         return 1;
107207       }
107208     }
107209     case '>': {
107210       if( (c=z[1])=='=' ){
107211         *tokenType = TK_GE;
107212         return 2;
107213       }else if( c=='>' ){
107214         *tokenType = TK_RSHIFT;
107215         return 2;
107216       }else{
107217         *tokenType = TK_GT;
107218         return 1;
107219       }
107220     }
107221     case '!': {
107222       if( z[1]!='=' ){
107223         *tokenType = TK_ILLEGAL;
107224         return 2;
107225       }else{
107226         *tokenType = TK_NE;
107227         return 2;
107228       }
107229     }
107230     case '|': {
107231       if( z[1]!='|' ){
107232         *tokenType = TK_BITOR;
107233         return 1;
107234       }else{
107235         *tokenType = TK_CONCAT;
107236         return 2;
107237       }
107238     }
107239     case ',': {
107240       *tokenType = TK_COMMA;
107241       return 1;
107242     }
107243     case '&': {
107244       *tokenType = TK_BITAND;
107245       return 1;
107246     }
107247     case '~': {
107248       *tokenType = TK_BITNOT;
107249       return 1;
107250     }
107251     case '`':
107252     case '\'':
107253     case '"': {
107254       int delim = z[0];
107255       testcase( delim=='`' );
107256       testcase( delim=='\'' );
107257       testcase( delim=='"' );
107258       for(i=1; (c=z[i])!=0; i++){
107259         if( c==delim ){
107260           if( z[i+1]==delim ){
107261             i++;
107262           }else{
107263             break;
107264           }
107265         }
107266       }
107267       if( c=='\'' ){
107268         *tokenType = TK_STRING;
107269         return i+1;
107270       }else if( c!=0 ){
107271         *tokenType = TK_ID;
107272         return i+1;
107273       }else{
107274         *tokenType = TK_ILLEGAL;
107275         return i;
107276       }
107277     }
107278     case '.': {
107279 #ifndef SQLITE_OMIT_FLOATING_POINT
107280       if( !sqlite3Isdigit(z[1]) )
107281 #endif
107282       {
107283         *tokenType = TK_DOT;
107284         return 1;
107285       }
107286       /* If the next character is a digit, this is a floating point
107287       ** number that begins with ".".  Fall thru into the next case */
107288     }
107289     case '0': case '1': case '2': case '3': case '4':
107290     case '5': case '6': case '7': case '8': case '9': {
107291       testcase( z[0]=='0' );  testcase( z[0]=='1' );  testcase( z[0]=='2' );
107292       testcase( z[0]=='3' );  testcase( z[0]=='4' );  testcase( z[0]=='5' );
107293       testcase( z[0]=='6' );  testcase( z[0]=='7' );  testcase( z[0]=='8' );
107294       testcase( z[0]=='9' );
107295       *tokenType = TK_INTEGER;
107296       for(i=0; sqlite3Isdigit(z[i]); i++){}
107297 #ifndef SQLITE_OMIT_FLOATING_POINT
107298       if( z[i]=='.' ){
107299         i++;
107300         while( sqlite3Isdigit(z[i]) ){ i++; }
107301         *tokenType = TK_FLOAT;
107302       }
107303       if( (z[i]=='e' || z[i]=='E') &&
107304            ( sqlite3Isdigit(z[i+1])
107305             || ((z[i+1]=='+' || z[i+1]=='-') && sqlite3Isdigit(z[i+2]))
107306            )
107307       ){
107308         i += 2;
107309         while( sqlite3Isdigit(z[i]) ){ i++; }
107310         *tokenType = TK_FLOAT;
107311       }
107312 #endif
107313       while( IdChar(z[i]) ){
107314         *tokenType = TK_ILLEGAL;
107315         i++;
107316       }
107317       return i;
107318     }
107319     case '[': {
107320       for(i=1, c=z[0]; c!=']' && (c=z[i])!=0; i++){}
107321       *tokenType = c==']' ? TK_ID : TK_ILLEGAL;
107322       return i;
107323     }
107324     case '?': {
107325       *tokenType = TK_VARIABLE;
107326       for(i=1; sqlite3Isdigit(z[i]); i++){}
107327       return i;
107328     }
107329     case '#': {
107330       for(i=1; sqlite3Isdigit(z[i]); i++){}
107331       if( i>1 ){
107332         /* Parameters of the form #NNN (where NNN is a number) are used
107333         ** internally by sqlite3NestedParse.  */
107334         *tokenType = TK_REGISTER;
107335         return i;
107336       }
107337       /* Fall through into the next case if the '#' is not followed by
107338       ** a digit. Try to match #AAAA where AAAA is a parameter name. */
107339     }
107340 #ifndef SQLITE_OMIT_TCL_VARIABLE
107341     case '$':
107342 #endif
107343     case '@':  /* For compatibility with MS SQL Server */
107344     case ':': {
107345       int n = 0;
107346       testcase( z[0]=='$' );  testcase( z[0]=='@' );  testcase( z[0]==':' );
107347       *tokenType = TK_VARIABLE;
107348       for(i=1; (c=z[i])!=0; i++){
107349         if( IdChar(c) ){
107350           n++;
107351 #ifndef SQLITE_OMIT_TCL_VARIABLE
107352         }else if( c=='(' && n>0 ){
107353           do{
107354             i++;
107355           }while( (c=z[i])!=0 && !sqlite3Isspace(c) && c!=')' );
107356           if( c==')' ){
107357             i++;
107358           }else{
107359             *tokenType = TK_ILLEGAL;
107360           }
107361           break;
107362         }else if( c==':' && z[i+1]==':' ){
107363           i++;
107364 #endif
107365         }else{
107366           break;
107367         }
107368       }
107369       if( n==0 ) *tokenType = TK_ILLEGAL;
107370       return i;
107371     }
107372 #ifndef SQLITE_OMIT_BLOB_LITERAL
107373     case 'x': case 'X': {
107374       testcase( z[0]=='x' ); testcase( z[0]=='X' );
107375       if( z[1]=='\'' ){
107376         *tokenType = TK_BLOB;
107377         for(i=2; sqlite3Isxdigit(z[i]); i++){}
107378         if( z[i]!='\'' || i%2 ){
107379           *tokenType = TK_ILLEGAL;
107380           while( z[i] && z[i]!='\'' ){ i++; }
107381         }
107382         if( z[i] ) i++;
107383         return i;
107384       }
107385       /* Otherwise fall through to the next case */
107386     }
107387 #endif
107388     default: {
107389       if( !IdChar(*z) ){
107390         break;
107391       }
107392       for(i=1; IdChar(z[i]); i++){}
107393       *tokenType = keywordCode((char*)z, i);
107394       return i;
107395     }
107396   }
107397   *tokenType = TK_ILLEGAL;
107398   return 1;
107399 }
107400 
107401 /*
107402 ** Run the parser on the given SQL string.  The parser structure is
107403 ** passed in.  An SQLITE_ status code is returned.  If an error occurs
107404 ** then an and attempt is made to write an error message into
107405 ** memory obtained from sqlite3_malloc() and to make *pzErrMsg point to that
107406 ** error message.
107407 */
107408 SQLITE_PRIVATE int sqlite3RunParser(Parse *pParse, const char *zSql, char **pzErrMsg){
107409   int nErr = 0;                   /* Number of errors encountered */
107410   int i;                          /* Loop counter */
107411   void *pEngine;                  /* The LEMON-generated LALR(1) parser */
107412   int tokenType;                  /* type of the next token */
107413   int lastTokenParsed = -1;       /* type of the previous token */
107414   u8 enableLookaside;             /* Saved value of db->lookaside.bEnabled */
107415   sqlite3 *db = pParse->db;       /* The database connection */
107416   int mxSqlLen;                   /* Max length of an SQL string */
107417 
107418 
107419   mxSqlLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
107420   if( db->activeVdbeCnt==0 ){
107421     db->u1.isInterrupted = 0;
107422   }
107423   pParse->rc = SQLITE_OK;
107424   pParse->zTail = zSql;
107425   i = 0;
107426   assert( pzErrMsg!=0 );
107427   pEngine = sqlite3ParserAlloc((void*(*)(size_t))sqlite3Malloc);
107428   if( pEngine==0 ){
107429     db->mallocFailed = 1;
107430     return SQLITE_NOMEM;
107431   }
107432   assert( pParse->pNewTable==0 );
107433   assert( pParse->pNewTrigger==0 );
107434   assert( pParse->nVar==0 );
107435   assert( pParse->nzVar==0 );
107436   assert( pParse->azVar==0 );
107437   enableLookaside = db->lookaside.bEnabled;
107438   if( db->lookaside.pStart ) db->lookaside.bEnabled = 1;
107439   while( !db->mallocFailed && zSql[i]!=0 ){
107440     assert( i>=0 );
107441     pParse->sLastToken.z = &zSql[i];
107442     pParse->sLastToken.n = sqlite3GetToken((unsigned char*)&zSql[i],&tokenType);
107443     i += pParse->sLastToken.n;
107444     if( i>mxSqlLen ){
107445       pParse->rc = SQLITE_TOOBIG;
107446       break;
107447     }
107448     switch( tokenType ){
107449       case TK_SPACE: {
107450         if( db->u1.isInterrupted ){
107451           sqlite3ErrorMsg(pParse, "interrupt");
107452           pParse->rc = SQLITE_INTERRUPT;
107453           goto abort_parse;
107454         }
107455         break;
107456       }
107457       case TK_ILLEGAL: {
107458         sqlite3DbFree(db, *pzErrMsg);
107459         *pzErrMsg = sqlite3MPrintf(db, "unrecognized token: \"%T\"",
107460                         &pParse->sLastToken);
107461         nErr++;
107462         goto abort_parse;
107463       }
107464       case TK_SEMI: {
107465         pParse->zTail = &zSql[i];
107466         /* Fall thru into the default case */
107467       }
107468       default: {
107469         sqlite3Parser(pEngine, tokenType, pParse->sLastToken, pParse);
107470         lastTokenParsed = tokenType;
107471         if( pParse->rc!=SQLITE_OK ){
107472           goto abort_parse;
107473         }
107474         break;
107475       }
107476     }
107477   }
107478 abort_parse:
107479   if( zSql[i]==0 && nErr==0 && pParse->rc==SQLITE_OK ){
107480     if( lastTokenParsed!=TK_SEMI ){
107481       sqlite3Parser(pEngine, TK_SEMI, pParse->sLastToken, pParse);
107482       pParse->zTail = &zSql[i];
107483     }
107484     sqlite3Parser(pEngine, 0, pParse->sLastToken, pParse);
107485   }
107486 #ifdef YYTRACKMAXSTACKDEPTH
107487   sqlite3StatusSet(SQLITE_STATUS_PARSER_STACK,
107488       sqlite3ParserStackPeak(pEngine)
107489   );
107490 #endif /* YYDEBUG */
107491   sqlite3ParserFree(pEngine, sqlite3_free);
107492   db->lookaside.bEnabled = enableLookaside;
107493   if( db->mallocFailed ){
107494     pParse->rc = SQLITE_NOMEM;
107495   }
107496   if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){
107497     sqlite3SetString(&pParse->zErrMsg, db, "%s", sqlite3ErrStr(pParse->rc));
107498   }
107499   assert( pzErrMsg!=0 );
107500   if( pParse->zErrMsg ){
107501     *pzErrMsg = pParse->zErrMsg;
107502     sqlite3_log(pParse->rc, "%s", *pzErrMsg);
107503     pParse->zErrMsg = 0;
107504     nErr++;
107505   }
107506   if( pParse->pVdbe && pParse->nErr>0 && pParse->nested==0 ){
107507     sqlite3VdbeDelete(pParse->pVdbe);
107508     pParse->pVdbe = 0;
107509   }
107510 #ifndef SQLITE_OMIT_SHARED_CACHE
107511   if( pParse->nested==0 ){
107512     sqlite3DbFree(db, pParse->aTableLock);
107513     pParse->aTableLock = 0;
107514     pParse->nTableLock = 0;
107515   }
107516 #endif
107517 #ifndef SQLITE_OMIT_VIRTUALTABLE
107518   sqlite3_free(pParse->apVtabLock);
107519 #endif
107520 
107521   if( !IN_DECLARE_VTAB ){
107522     /* If the pParse->declareVtab flag is set, do not delete any table
107523     ** structure built up in pParse->pNewTable. The calling code (see vtab.c)
107524     ** will take responsibility for freeing the Table structure.
107525     */
107526     sqlite3DeleteTable(db, pParse->pNewTable);
107527   }
107528 
107529   sqlite3DeleteTrigger(db, pParse->pNewTrigger);
107530   for(i=pParse->nzVar-1; i>=0; i--) sqlite3DbFree(db, pParse->azVar[i]);
107531   sqlite3DbFree(db, pParse->azVar);
107532   sqlite3DbFree(db, pParse->aAlias);
107533   while( pParse->pAinc ){
107534     AutoincInfo *p = pParse->pAinc;
107535     pParse->pAinc = p->pNext;
107536     sqlite3DbFree(db, p);
107537   }
107538   while( pParse->pZombieTab ){
107539     Table *p = pParse->pZombieTab;
107540     pParse->pZombieTab = p->pNextZombie;
107541     sqlite3DeleteTable(db, p);
107542   }
107543   if( nErr>0 && pParse->rc==SQLITE_OK ){
107544     pParse->rc = SQLITE_ERROR;
107545   }
107546   return nErr;
107547 }
107548 
107549 /************** End of tokenize.c ********************************************/
107550 /************** Begin file complete.c ****************************************/
107551 /*
107552 ** 2001 September 15
107553 **
107554 ** The author disclaims copyright to this source code.  In place of
107555 ** a legal notice, here is a blessing:
107556 **
107557 **    May you do good and not evil.
107558 **    May you find forgiveness for yourself and forgive others.
107559 **    May you share freely, never taking more than you give.
107560 **
107561 *************************************************************************
107562 ** An tokenizer for SQL
107563 **
107564 ** This file contains C code that implements the sqlite3_complete() API.
107565 ** This code used to be part of the tokenizer.c source file.  But by
107566 ** separating it out, the code will be automatically omitted from
107567 ** static links that do not use it.
107568 */
107569 #ifndef SQLITE_OMIT_COMPLETE
107570 
107571 /*
107572 ** This is defined in tokenize.c.  We just have to import the definition.
107573 */
107574 #ifndef SQLITE_AMALGAMATION
107575 #ifdef SQLITE_ASCII
107576 #define IdChar(C)  ((sqlite3CtypeMap[(unsigned char)C]&0x46)!=0)
107577 #endif
107578 #ifdef SQLITE_EBCDIC
107579 SQLITE_PRIVATE const char sqlite3IsEbcdicIdChar[];
107580 #define IdChar(C)  (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
107581 #endif
107582 #endif /* SQLITE_AMALGAMATION */
107583 
107584 
107585 /*
107586 ** Token types used by the sqlite3_complete() routine.  See the header
107587 ** comments on that procedure for additional information.
107588 */
107589 #define tkSEMI    0
107590 #define tkWS      1
107591 #define tkOTHER   2
107592 #ifndef SQLITE_OMIT_TRIGGER
107593 #define tkEXPLAIN 3
107594 #define tkCREATE  4
107595 #define tkTEMP    5
107596 #define tkTRIGGER 6
107597 #define tkEND     7
107598 #endif
107599 
107600 /*
107601 ** Return TRUE if the given SQL string ends in a semicolon.
107602 **
107603 ** Special handling is require for CREATE TRIGGER statements.
107604 ** Whenever the CREATE TRIGGER keywords are seen, the statement
107605 ** must end with ";END;".
107606 **
107607 ** This implementation uses a state machine with 8 states:
107608 **
107609 **   (0) INVALID   We have not yet seen a non-whitespace character.
107610 **
107611 **   (1) START     At the beginning or end of an SQL statement.  This routine
107612 **                 returns 1 if it ends in the START state and 0 if it ends
107613 **                 in any other state.
107614 **
107615 **   (2) NORMAL    We are in the middle of statement which ends with a single
107616 **                 semicolon.
107617 **
107618 **   (3) EXPLAIN   The keyword EXPLAIN has been seen at the beginning of
107619 **                 a statement.
107620 **
107621 **   (4) CREATE    The keyword CREATE has been seen at the beginning of a
107622 **                 statement, possibly preceeded by EXPLAIN and/or followed by
107623 **                 TEMP or TEMPORARY
107624 **
107625 **   (5) TRIGGER   We are in the middle of a trigger definition that must be
107626 **                 ended by a semicolon, the keyword END, and another semicolon.
107627 **
107628 **   (6) SEMI      We've seen the first semicolon in the ";END;" that occurs at
107629 **                 the end of a trigger definition.
107630 **
107631 **   (7) END       We've seen the ";END" of the ";END;" that occurs at the end
107632 **                 of a trigger difinition.
107633 **
107634 ** Transitions between states above are determined by tokens extracted
107635 ** from the input.  The following tokens are significant:
107636 **
107637 **   (0) tkSEMI      A semicolon.
107638 **   (1) tkWS        Whitespace.
107639 **   (2) tkOTHER     Any other SQL token.
107640 **   (3) tkEXPLAIN   The "explain" keyword.
107641 **   (4) tkCREATE    The "create" keyword.
107642 **   (5) tkTEMP      The "temp" or "temporary" keyword.
107643 **   (6) tkTRIGGER   The "trigger" keyword.
107644 **   (7) tkEND       The "end" keyword.
107645 **
107646 ** Whitespace never causes a state transition and is always ignored.
107647 ** This means that a SQL string of all whitespace is invalid.
107648 **
107649 ** If we compile with SQLITE_OMIT_TRIGGER, all of the computation needed
107650 ** to recognize the end of a trigger can be omitted.  All we have to do
107651 ** is look for a semicolon that is not part of an string or comment.
107652 */
107653 SQLITE_API int sqlite3_complete(const char *zSql){
107654   u8 state = 0;   /* Current state, using numbers defined in header comment */
107655   u8 token;       /* Value of the next token */
107656 
107657 #ifndef SQLITE_OMIT_TRIGGER
107658   /* A complex statement machine used to detect the end of a CREATE TRIGGER
107659   ** statement.  This is the normal case.
107660   */
107661   static const u8 trans[8][8] = {
107662                      /* Token:                                                */
107663      /* State:       **  SEMI  WS  OTHER  EXPLAIN  CREATE  TEMP  TRIGGER  END */
107664      /* 0 INVALID: */ {    1,  0,     2,       3,      4,    2,       2,   2, },
107665      /* 1   START: */ {    1,  1,     2,       3,      4,    2,       2,   2, },
107666      /* 2  NORMAL: */ {    1,  2,     2,       2,      2,    2,       2,   2, },
107667      /* 3 EXPLAIN: */ {    1,  3,     3,       2,      4,    2,       2,   2, },
107668      /* 4  CREATE: */ {    1,  4,     2,       2,      2,    4,       5,   2, },
107669      /* 5 TRIGGER: */ {    6,  5,     5,       5,      5,    5,       5,   5, },
107670      /* 6    SEMI: */ {    6,  6,     5,       5,      5,    5,       5,   7, },
107671      /* 7     END: */ {    1,  7,     5,       5,      5,    5,       5,   5, },
107672   };
107673 #else
107674   /* If triggers are not supported by this compile then the statement machine
107675   ** used to detect the end of a statement is much simplier
107676   */
107677   static const u8 trans[3][3] = {
107678                      /* Token:           */
107679      /* State:       **  SEMI  WS  OTHER */
107680      /* 0 INVALID: */ {    1,  0,     2, },
107681      /* 1   START: */ {    1,  1,     2, },
107682      /* 2  NORMAL: */ {    1,  2,     2, },
107683   };
107684 #endif /* SQLITE_OMIT_TRIGGER */
107685 
107686   while( *zSql ){
107687     switch( *zSql ){
107688       case ';': {  /* A semicolon */
107689         token = tkSEMI;
107690         break;
107691       }
107692       case ' ':
107693       case '\r':
107694       case '\t':
107695       case '\n':
107696       case '\f': {  /* White space is ignored */
107697         token = tkWS;
107698         break;
107699       }
107700       case '/': {   /* C-style comments */
107701         if( zSql[1]!='*' ){
107702           token = tkOTHER;
107703           break;
107704         }
107705         zSql += 2;
107706         while( zSql[0] && (zSql[0]!='*' || zSql[1]!='/') ){ zSql++; }
107707         if( zSql[0]==0 ) return 0;
107708         zSql++;
107709         token = tkWS;
107710         break;
107711       }
107712       case '-': {   /* SQL-style comments from "--" to end of line */
107713         if( zSql[1]!='-' ){
107714           token = tkOTHER;
107715           break;
107716         }
107717         while( *zSql && *zSql!='\n' ){ zSql++; }
107718         if( *zSql==0 ) return state==1;
107719         token = tkWS;
107720         break;
107721       }
107722       case '[': {   /* Microsoft-style identifiers in [...] */
107723         zSql++;
107724         while( *zSql && *zSql!=']' ){ zSql++; }
107725         if( *zSql==0 ) return 0;
107726         token = tkOTHER;
107727         break;
107728       }
107729       case '`':     /* Grave-accent quoted symbols used by MySQL */
107730       case '"':     /* single- and double-quoted strings */
107731       case '\'': {
107732         int c = *zSql;
107733         zSql++;
107734         while( *zSql && *zSql!=c ){ zSql++; }
107735         if( *zSql==0 ) return 0;
107736         token = tkOTHER;
107737         break;
107738       }
107739       default: {
107740 #ifdef SQLITE_EBCDIC
107741         unsigned char c;
107742 #endif
107743         if( IdChar((u8)*zSql) ){
107744           /* Keywords and unquoted identifiers */
107745           int nId;
107746           for(nId=1; IdChar(zSql[nId]); nId++){}
107747 #ifdef SQLITE_OMIT_TRIGGER
107748           token = tkOTHER;
107749 #else
107750           switch( *zSql ){
107751             case 'c': case 'C': {
107752               if( nId==6 && sqlite3StrNICmp(zSql, "create", 6)==0 ){
107753                 token = tkCREATE;
107754               }else{
107755                 token = tkOTHER;
107756               }
107757               break;
107758             }
107759             case 't': case 'T': {
107760               if( nId==7 && sqlite3StrNICmp(zSql, "trigger", 7)==0 ){
107761                 token = tkTRIGGER;
107762               }else if( nId==4 && sqlite3StrNICmp(zSql, "temp", 4)==0 ){
107763                 token = tkTEMP;
107764               }else if( nId==9 && sqlite3StrNICmp(zSql, "temporary", 9)==0 ){
107765                 token = tkTEMP;
107766               }else{
107767                 token = tkOTHER;
107768               }
107769               break;
107770             }
107771             case 'e':  case 'E': {
107772               if( nId==3 && sqlite3StrNICmp(zSql, "end", 3)==0 ){
107773                 token = tkEND;
107774               }else
107775 #ifndef SQLITE_OMIT_EXPLAIN
107776               if( nId==7 && sqlite3StrNICmp(zSql, "explain", 7)==0 ){
107777                 token = tkEXPLAIN;
107778               }else
107779 #endif
107780               {
107781                 token = tkOTHER;
107782               }
107783               break;
107784             }
107785             default: {
107786               token = tkOTHER;
107787               break;
107788             }
107789           }
107790 #endif /* SQLITE_OMIT_TRIGGER */
107791           zSql += nId-1;
107792         }else{
107793           /* Operators and special symbols */
107794           token = tkOTHER;
107795         }
107796         break;
107797       }
107798     }
107799     state = trans[state][token];
107800     zSql++;
107801   }
107802   return state==1;
107803 }
107804 
107805 #ifndef SQLITE_OMIT_UTF16
107806 /*
107807 ** This routine is the same as the sqlite3_complete() routine described
107808 ** above, except that the parameter is required to be UTF-16 encoded, not
107809 ** UTF-8.
107810 */
107811 SQLITE_API int sqlite3_complete16(const void *zSql){
107812   sqlite3_value *pVal;
107813   char const *zSql8;
107814   int rc = SQLITE_NOMEM;
107815 
107816 #ifndef SQLITE_OMIT_AUTOINIT
107817   rc = sqlite3_initialize();
107818   if( rc ) return rc;
107819 #endif
107820   pVal = sqlite3ValueNew(0);
107821   sqlite3ValueSetStr(pVal, -1, zSql, SQLITE_UTF16NATIVE, SQLITE_STATIC);
107822   zSql8 = sqlite3ValueText(pVal, SQLITE_UTF8);
107823   if( zSql8 ){
107824     rc = sqlite3_complete(zSql8);
107825   }else{
107826     rc = SQLITE_NOMEM;
107827   }
107828   sqlite3ValueFree(pVal);
107829   return sqlite3ApiExit(0, rc);
107830 }
107831 #endif /* SQLITE_OMIT_UTF16 */
107832 #endif /* SQLITE_OMIT_COMPLETE */
107833 
107834 /************** End of complete.c ********************************************/
107835 /************** Begin file main.c ********************************************/
107836 /*
107837 ** 2001 September 15
107838 **
107839 ** The author disclaims copyright to this source code.  In place of
107840 ** a legal notice, here is a blessing:
107841 **
107842 **    May you do good and not evil.
107843 **    May you find forgiveness for yourself and forgive others.
107844 **    May you share freely, never taking more than you give.
107845 **
107846 *************************************************************************
107847 ** Main file for the SQLite library.  The routines in this file
107848 ** implement the programmer interface to the library.  Routines in
107849 ** other files are for internal use by SQLite and should not be
107850 ** accessed by users of the library.
107851 */
107852 
107853 #ifdef SQLITE_ENABLE_FTS3
107854 /************** Include fts3.h in the middle of main.c ***********************/
107855 /************** Begin file fts3.h ********************************************/
107856 /*
107857 ** 2006 Oct 10
107858 **
107859 ** The author disclaims copyright to this source code.  In place of
107860 ** a legal notice, here is a blessing:
107861 **
107862 **    May you do good and not evil.
107863 **    May you find forgiveness for yourself and forgive others.
107864 **    May you share freely, never taking more than you give.
107865 **
107866 ******************************************************************************
107867 **
107868 ** This header file is used by programs that want to link against the
107869 ** FTS3 library.  All it does is declare the sqlite3Fts3Init() interface.
107870 */
107871 
107872 #if 0
107873 extern "C" {
107874 #endif  /* __cplusplus */
107875 
107876 SQLITE_PRIVATE int sqlite3Fts3Init(sqlite3 *db);
107877 
107878 #if 0
107879 }  /* extern "C" */
107880 #endif  /* __cplusplus */
107881 
107882 /************** End of fts3.h ************************************************/
107883 /************** Continuing where we left off in main.c ***********************/
107884 #endif
107885 #ifdef SQLITE_ENABLE_RTREE
107886 /************** Include rtree.h in the middle of main.c **********************/
107887 /************** Begin file rtree.h *******************************************/
107888 /*
107889 ** 2008 May 26
107890 **
107891 ** The author disclaims copyright to this source code.  In place of
107892 ** a legal notice, here is a blessing:
107893 **
107894 **    May you do good and not evil.
107895 **    May you find forgiveness for yourself and forgive others.
107896 **    May you share freely, never taking more than you give.
107897 **
107898 ******************************************************************************
107899 **
107900 ** This header file is used by programs that want to link against the
107901 ** RTREE library.  All it does is declare the sqlite3RtreeInit() interface.
107902 */
107903 
107904 #if 0
107905 extern "C" {
107906 #endif  /* __cplusplus */
107907 
107908 SQLITE_PRIVATE int sqlite3RtreeInit(sqlite3 *db);
107909 
107910 #if 0
107911 }  /* extern "C" */
107912 #endif  /* __cplusplus */
107913 
107914 /************** End of rtree.h ***********************************************/
107915 /************** Continuing where we left off in main.c ***********************/
107916 #endif
107917 #ifdef SQLITE_ENABLE_ICU
107918 /************** Include sqliteicu.h in the middle of main.c ******************/
107919 /************** Begin file sqliteicu.h ***************************************/
107920 /*
107921 ** 2008 May 26
107922 **
107923 ** The author disclaims copyright to this source code.  In place of
107924 ** a legal notice, here is a blessing:
107925 **
107926 **    May you do good and not evil.
107927 **    May you find forgiveness for yourself and forgive others.
107928 **    May you share freely, never taking more than you give.
107929 **
107930 ******************************************************************************
107931 **
107932 ** This header file is used by programs that want to link against the
107933 ** ICU extension.  All it does is declare the sqlite3IcuInit() interface.
107934 */
107935 
107936 #if 0
107937 extern "C" {
107938 #endif  /* __cplusplus */
107939 
107940 SQLITE_PRIVATE int sqlite3IcuInit(sqlite3 *db);
107941 
107942 #if 0
107943 }  /* extern "C" */
107944 #endif  /* __cplusplus */
107945 
107946 
107947 /************** End of sqliteicu.h *******************************************/
107948 /************** Continuing where we left off in main.c ***********************/
107949 #endif
107950 
107951 #ifndef SQLITE_AMALGAMATION
107952 /* IMPLEMENTATION-OF: R-46656-45156 The sqlite3_version[] string constant
107953 ** contains the text of SQLITE_VERSION macro.
107954 */
107955 SQLITE_API const char sqlite3_version[] = SQLITE_VERSION;
107956 #endif
107957 
107958 /* IMPLEMENTATION-OF: R-53536-42575 The sqlite3_libversion() function returns
107959 ** a pointer to the to the sqlite3_version[] string constant.
107960 */
107961 SQLITE_API const char *sqlite3_libversion(void){ return sqlite3_version; }
107962 
107963 /* IMPLEMENTATION-OF: R-63124-39300 The sqlite3_sourceid() function returns a
107964 ** pointer to a string constant whose value is the same as the
107965 ** SQLITE_SOURCE_ID C preprocessor macro.
107966 */
107967 SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; }
107968 
107969 /* IMPLEMENTATION-OF: R-35210-63508 The sqlite3_libversion_number() function
107970 ** returns an integer equal to SQLITE_VERSION_NUMBER.
107971 */
107972 SQLITE_API int sqlite3_libversion_number(void){ return SQLITE_VERSION_NUMBER; }
107973 
107974 /* IMPLEMENTATION-OF: R-54823-41343 The sqlite3_threadsafe() function returns
107975 ** zero if and only if SQLite was compiled mutexing code omitted due to
107976 ** the SQLITE_THREADSAFE compile-time option being set to 0.
107977 */
107978 SQLITE_API int sqlite3_threadsafe(void){ return SQLITE_THREADSAFE; }
107979 
107980 #if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
107981 /*
107982 ** If the following function pointer is not NULL and if
107983 ** SQLITE_ENABLE_IOTRACE is enabled, then messages describing
107984 ** I/O active are written using this function.  These messages
107985 ** are intended for debugging activity only.
107986 */
107987 SQLITE_PRIVATE void (*sqlite3IoTrace)(const char*, ...) = 0;
107988 #endif
107989 
107990 /*
107991 ** If the following global variable points to a string which is the
107992 ** name of a directory, then that directory will be used to store
107993 ** temporary files.
107994 **
107995 ** See also the "PRAGMA temp_store_directory" SQL command.
107996 */
107997 SQLITE_API char *sqlite3_temp_directory = 0;
107998 
107999 /*
108000 ** Initialize SQLite.
108001 **
108002 ** This routine must be called to initialize the memory allocation,
108003 ** VFS, and mutex subsystems prior to doing any serious work with
108004 ** SQLite.  But as long as you do not compile with SQLITE_OMIT_AUTOINIT
108005 ** this routine will be called automatically by key routines such as
108006 ** sqlite3_open().
108007 **
108008 ** This routine is a no-op except on its very first call for the process,
108009 ** or for the first call after a call to sqlite3_shutdown.
108010 **
108011 ** The first thread to call this routine runs the initialization to
108012 ** completion.  If subsequent threads call this routine before the first
108013 ** thread has finished the initialization process, then the subsequent
108014 ** threads must block until the first thread finishes with the initialization.
108015 **
108016 ** The first thread might call this routine recursively.  Recursive
108017 ** calls to this routine should not block, of course.  Otherwise the
108018 ** initialization process would never complete.
108019 **
108020 ** Let X be the first thread to enter this routine.  Let Y be some other
108021 ** thread.  Then while the initial invocation of this routine by X is
108022 ** incomplete, it is required that:
108023 **
108024 **    *  Calls to this routine from Y must block until the outer-most
108025 **       call by X completes.
108026 **
108027 **    *  Recursive calls to this routine from thread X return immediately
108028 **       without blocking.
108029 */
108030 SQLITE_API int sqlite3_initialize(void){
108031   sqlite3_mutex *pMaster;                      /* The main static mutex */
108032   int rc;                                      /* Result code */
108033 
108034 #ifdef SQLITE_OMIT_WSD
108035   rc = sqlite3_wsd_init(4096, 24);
108036   if( rc!=SQLITE_OK ){
108037     return rc;
108038   }
108039 #endif
108040 
108041   /* If SQLite is already completely initialized, then this call
108042   ** to sqlite3_initialize() should be a no-op.  But the initialization
108043   ** must be complete.  So isInit must not be set until the very end
108044   ** of this routine.
108045   */
108046   if( sqlite3GlobalConfig.isInit ) return SQLITE_OK;
108047 
108048   /* Make sure the mutex subsystem is initialized.  If unable to
108049   ** initialize the mutex subsystem, return early with the error.
108050   ** If the system is so sick that we are unable to allocate a mutex,
108051   ** there is not much SQLite is going to be able to do.
108052   **
108053   ** The mutex subsystem must take care of serializing its own
108054   ** initialization.
108055   */
108056   rc = sqlite3MutexInit();
108057   if( rc ) return rc;
108058 
108059   /* Initialize the malloc() system and the recursive pInitMutex mutex.
108060   ** This operation is protected by the STATIC_MASTER mutex.  Note that
108061   ** MutexAlloc() is called for a static mutex prior to initializing the
108062   ** malloc subsystem - this implies that the allocation of a static
108063   ** mutex must not require support from the malloc subsystem.
108064   */
108065   pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
108066   sqlite3_mutex_enter(pMaster);
108067   sqlite3GlobalConfig.isMutexInit = 1;
108068   if( !sqlite3GlobalConfig.isMallocInit ){
108069     rc = sqlite3MallocInit();
108070   }
108071   if( rc==SQLITE_OK ){
108072     sqlite3GlobalConfig.isMallocInit = 1;
108073     if( !sqlite3GlobalConfig.pInitMutex ){
108074       sqlite3GlobalConfig.pInitMutex =
108075            sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE);
108076       if( sqlite3GlobalConfig.bCoreMutex && !sqlite3GlobalConfig.pInitMutex ){
108077         rc = SQLITE_NOMEM;
108078       }
108079     }
108080   }
108081   if( rc==SQLITE_OK ){
108082     sqlite3GlobalConfig.nRefInitMutex++;
108083   }
108084   sqlite3_mutex_leave(pMaster);
108085 
108086   /* If rc is not SQLITE_OK at this point, then either the malloc
108087   ** subsystem could not be initialized or the system failed to allocate
108088   ** the pInitMutex mutex. Return an error in either case.  */
108089   if( rc!=SQLITE_OK ){
108090     return rc;
108091   }
108092 
108093   /* Do the rest of the initialization under the recursive mutex so
108094   ** that we will be able to handle recursive calls into
108095   ** sqlite3_initialize().  The recursive calls normally come through
108096   ** sqlite3_os_init() when it invokes sqlite3_vfs_register(), but other
108097   ** recursive calls might also be possible.
108098   **
108099   ** IMPLEMENTATION-OF: R-00140-37445 SQLite automatically serializes calls
108100   ** to the xInit method, so the xInit method need not be threadsafe.
108101   **
108102   ** The following mutex is what serializes access to the appdef pcache xInit
108103   ** methods.  The sqlite3_pcache_methods.xInit() all is embedded in the
108104   ** call to sqlite3PcacheInitialize().
108105   */
108106   sqlite3_mutex_enter(sqlite3GlobalConfig.pInitMutex);
108107   if( sqlite3GlobalConfig.isInit==0 && sqlite3GlobalConfig.inProgress==0 ){
108108     FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
108109     sqlite3GlobalConfig.inProgress = 1;
108110     memset(pHash, 0, sizeof(sqlite3GlobalFunctions));
108111     sqlite3RegisterGlobalFunctions();
108112     if( sqlite3GlobalConfig.isPCacheInit==0 ){
108113       rc = sqlite3PcacheInitialize();
108114     }
108115     if( rc==SQLITE_OK ){
108116       sqlite3GlobalConfig.isPCacheInit = 1;
108117       rc = sqlite3OsInit();
108118     }
108119     if( rc==SQLITE_OK ){
108120       sqlite3PCacheBufferSetup( sqlite3GlobalConfig.pPage,
108121           sqlite3GlobalConfig.szPage, sqlite3GlobalConfig.nPage);
108122       sqlite3GlobalConfig.isInit = 1;
108123     }
108124     sqlite3GlobalConfig.inProgress = 0;
108125   }
108126   sqlite3_mutex_leave(sqlite3GlobalConfig.pInitMutex);
108127 
108128   /* Go back under the static mutex and clean up the recursive
108129   ** mutex to prevent a resource leak.
108130   */
108131   sqlite3_mutex_enter(pMaster);
108132   sqlite3GlobalConfig.nRefInitMutex--;
108133   if( sqlite3GlobalConfig.nRefInitMutex<=0 ){
108134     assert( sqlite3GlobalConfig.nRefInitMutex==0 );
108135     sqlite3_mutex_free(sqlite3GlobalConfig.pInitMutex);
108136     sqlite3GlobalConfig.pInitMutex = 0;
108137   }
108138   sqlite3_mutex_leave(pMaster);
108139 
108140   /* The following is just a sanity check to make sure SQLite has
108141   ** been compiled correctly.  It is important to run this code, but
108142   ** we don't want to run it too often and soak up CPU cycles for no
108143   ** reason.  So we run it once during initialization.
108144   */
108145 #ifndef NDEBUG
108146 #ifndef SQLITE_OMIT_FLOATING_POINT
108147   /* This section of code's only "output" is via assert() statements. */
108148   if ( rc==SQLITE_OK ){
108149     u64 x = (((u64)1)<<63)-1;
108150     double y;
108151     assert(sizeof(x)==8);
108152     assert(sizeof(x)==sizeof(y));
108153     memcpy(&y, &x, 8);
108154     assert( sqlite3IsNaN(y) );
108155   }
108156 #endif
108157 #endif
108158 
108159   return rc;
108160 }
108161 
108162 /*
108163 ** Undo the effects of sqlite3_initialize().  Must not be called while
108164 ** there are outstanding database connections or memory allocations or
108165 ** while any part of SQLite is otherwise in use in any thread.  This
108166 ** routine is not threadsafe.  But it is safe to invoke this routine
108167 ** on when SQLite is already shut down.  If SQLite is already shut down
108168 ** when this routine is invoked, then this routine is a harmless no-op.
108169 */
108170 SQLITE_API int sqlite3_shutdown(void){
108171   if( sqlite3GlobalConfig.isInit ){
108172     sqlite3_os_end();
108173     sqlite3_reset_auto_extension();
108174     sqlite3GlobalConfig.isInit = 0;
108175   }
108176   if( sqlite3GlobalConfig.isPCacheInit ){
108177     sqlite3PcacheShutdown();
108178     sqlite3GlobalConfig.isPCacheInit = 0;
108179   }
108180   if( sqlite3GlobalConfig.isMallocInit ){
108181     sqlite3MallocEnd();
108182     sqlite3GlobalConfig.isMallocInit = 0;
108183   }
108184   if( sqlite3GlobalConfig.isMutexInit ){
108185     sqlite3MutexEnd();
108186     sqlite3GlobalConfig.isMutexInit = 0;
108187   }
108188 
108189   return SQLITE_OK;
108190 }
108191 
108192 /*
108193 ** This API allows applications to modify the global configuration of
108194 ** the SQLite library at run-time.
108195 **
108196 ** This routine should only be called when there are no outstanding
108197 ** database connections or memory allocations.  This routine is not
108198 ** threadsafe.  Failure to heed these warnings can lead to unpredictable
108199 ** behavior.
108200 */
108201 SQLITE_API int sqlite3_config(int op, ...){
108202   va_list ap;
108203   int rc = SQLITE_OK;
108204 
108205   /* sqlite3_config() shall return SQLITE_MISUSE if it is invoked while
108206   ** the SQLite library is in use. */
108207   if( sqlite3GlobalConfig.isInit ) return SQLITE_MISUSE_BKPT;
108208 
108209   va_start(ap, op);
108210   switch( op ){
108211 
108212     /* Mutex configuration options are only available in a threadsafe
108213     ** compile.
108214     */
108215 #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0
108216     case SQLITE_CONFIG_SINGLETHREAD: {
108217       /* Disable all mutexing */
108218       sqlite3GlobalConfig.bCoreMutex = 0;
108219       sqlite3GlobalConfig.bFullMutex = 0;
108220       break;
108221     }
108222     case SQLITE_CONFIG_MULTITHREAD: {
108223       /* Disable mutexing of database connections */
108224       /* Enable mutexing of core data structures */
108225       sqlite3GlobalConfig.bCoreMutex = 1;
108226       sqlite3GlobalConfig.bFullMutex = 0;
108227       break;
108228     }
108229     case SQLITE_CONFIG_SERIALIZED: {
108230       /* Enable all mutexing */
108231       sqlite3GlobalConfig.bCoreMutex = 1;
108232       sqlite3GlobalConfig.bFullMutex = 1;
108233       break;
108234     }
108235     case SQLITE_CONFIG_MUTEX: {
108236       /* Specify an alternative mutex implementation */
108237       sqlite3GlobalConfig.mutex = *va_arg(ap, sqlite3_mutex_methods*);
108238       break;
108239     }
108240     case SQLITE_CONFIG_GETMUTEX: {
108241       /* Retrieve the current mutex implementation */
108242       *va_arg(ap, sqlite3_mutex_methods*) = sqlite3GlobalConfig.mutex;
108243       break;
108244     }
108245 #endif
108246 
108247 
108248     case SQLITE_CONFIG_MALLOC: {
108249       /* Specify an alternative malloc implementation */
108250       sqlite3GlobalConfig.m = *va_arg(ap, sqlite3_mem_methods*);
108251       break;
108252     }
108253     case SQLITE_CONFIG_GETMALLOC: {
108254       /* Retrieve the current malloc() implementation */
108255       if( sqlite3GlobalConfig.m.xMalloc==0 ) sqlite3MemSetDefault();
108256       *va_arg(ap, sqlite3_mem_methods*) = sqlite3GlobalConfig.m;
108257       break;
108258     }
108259     case SQLITE_CONFIG_MEMSTATUS: {
108260       /* Enable or disable the malloc status collection */
108261       sqlite3GlobalConfig.bMemstat = va_arg(ap, int);
108262       break;
108263     }
108264     case SQLITE_CONFIG_SCRATCH: {
108265       /* Designate a buffer for scratch memory space */
108266       sqlite3GlobalConfig.pScratch = va_arg(ap, void*);
108267       sqlite3GlobalConfig.szScratch = va_arg(ap, int);
108268       sqlite3GlobalConfig.nScratch = va_arg(ap, int);
108269       break;
108270     }
108271     case SQLITE_CONFIG_PAGECACHE: {
108272       /* Designate a buffer for page cache memory space */
108273       sqlite3GlobalConfig.pPage = va_arg(ap, void*);
108274       sqlite3GlobalConfig.szPage = va_arg(ap, int);
108275       sqlite3GlobalConfig.nPage = va_arg(ap, int);
108276       break;
108277     }
108278 
108279     case SQLITE_CONFIG_PCACHE: {
108280       /* Specify an alternative page cache implementation */
108281       sqlite3GlobalConfig.pcache = *va_arg(ap, sqlite3_pcache_methods*);
108282       break;
108283     }
108284 
108285     case SQLITE_CONFIG_GETPCACHE: {
108286       if( sqlite3GlobalConfig.pcache.xInit==0 ){
108287         sqlite3PCacheSetDefault();
108288       }
108289       *va_arg(ap, sqlite3_pcache_methods*) = sqlite3GlobalConfig.pcache;
108290       break;
108291     }
108292 
108293 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
108294     case SQLITE_CONFIG_HEAP: {
108295       /* Designate a buffer for heap memory space */
108296       sqlite3GlobalConfig.pHeap = va_arg(ap, void*);
108297       sqlite3GlobalConfig.nHeap = va_arg(ap, int);
108298       sqlite3GlobalConfig.mnReq = va_arg(ap, int);
108299 
108300       if( sqlite3GlobalConfig.mnReq<1 ){
108301         sqlite3GlobalConfig.mnReq = 1;
108302       }else if( sqlite3GlobalConfig.mnReq>(1<<12) ){
108303         /* cap min request size at 2^12 */
108304         sqlite3GlobalConfig.mnReq = (1<<12);
108305       }
108306 
108307       if( sqlite3GlobalConfig.pHeap==0 ){
108308         /* If the heap pointer is NULL, then restore the malloc implementation
108309         ** back to NULL pointers too.  This will cause the malloc to go
108310         ** back to its default implementation when sqlite3_initialize() is
108311         ** run.
108312         */
108313         memset(&sqlite3GlobalConfig.m, 0, sizeof(sqlite3GlobalConfig.m));
108314       }else{
108315         /* The heap pointer is not NULL, then install one of the
108316         ** mem5.c/mem3.c methods. If neither ENABLE_MEMSYS3 nor
108317         ** ENABLE_MEMSYS5 is defined, return an error.
108318         */
108319 #ifdef SQLITE_ENABLE_MEMSYS3
108320         sqlite3GlobalConfig.m = *sqlite3MemGetMemsys3();
108321 #endif
108322 #ifdef SQLITE_ENABLE_MEMSYS5
108323         sqlite3GlobalConfig.m = *sqlite3MemGetMemsys5();
108324 #endif
108325       }
108326       break;
108327     }
108328 #endif
108329 
108330     case SQLITE_CONFIG_LOOKASIDE: {
108331       sqlite3GlobalConfig.szLookaside = va_arg(ap, int);
108332       sqlite3GlobalConfig.nLookaside = va_arg(ap, int);
108333       break;
108334     }
108335 
108336     /* Record a pointer to the logger funcction and its first argument.
108337     ** The default is NULL.  Logging is disabled if the function pointer is
108338     ** NULL.
108339     */
108340     case SQLITE_CONFIG_LOG: {
108341       /* MSVC is picky about pulling func ptrs from va lists.
108342       ** http://support.microsoft.com/kb/47961
108343       ** sqlite3GlobalConfig.xLog = va_arg(ap, void(*)(void*,int,const char*));
108344       */
108345       typedef void(*LOGFUNC_t)(void*,int,const char*);
108346       sqlite3GlobalConfig.xLog = va_arg(ap, LOGFUNC_t);
108347       sqlite3GlobalConfig.pLogArg = va_arg(ap, void*);
108348       break;
108349     }
108350 
108351     case SQLITE_CONFIG_URI: {
108352       sqlite3GlobalConfig.bOpenUri = va_arg(ap, int);
108353       break;
108354     }
108355 
108356     default: {
108357       rc = SQLITE_ERROR;
108358       break;
108359     }
108360   }
108361   va_end(ap);
108362   return rc;
108363 }
108364 
108365 /*
108366 ** Set up the lookaside buffers for a database connection.
108367 ** Return SQLITE_OK on success.
108368 ** If lookaside is already active, return SQLITE_BUSY.
108369 **
108370 ** The sz parameter is the number of bytes in each lookaside slot.
108371 ** The cnt parameter is the number of slots.  If pStart is NULL the
108372 ** space for the lookaside memory is obtained from sqlite3_malloc().
108373 ** If pStart is not NULL then it is sz*cnt bytes of memory to use for
108374 ** the lookaside memory.
108375 */
108376 static int setupLookaside(sqlite3 *db, void *pBuf, int sz, int cnt){
108377   void *pStart;
108378   if( db->lookaside.nOut ){
108379     return SQLITE_BUSY;
108380   }
108381   /* Free any existing lookaside buffer for this handle before
108382   ** allocating a new one so we don't have to have space for
108383   ** both at the same time.
108384   */
108385   if( db->lookaside.bMalloced ){
108386     sqlite3_free(db->lookaside.pStart);
108387   }
108388   /* The size of a lookaside slot needs to be larger than a pointer
108389   ** to be useful.
108390   */
108391   if( sz<=(int)sizeof(LookasideSlot*) ) sz = 0;
108392   if( cnt<0 ) cnt = 0;
108393   if( sz==0 || cnt==0 ){
108394     sz = 0;
108395     pStart = 0;
108396   }else if( pBuf==0 ){
108397     sz = ROUNDDOWN8(sz); /* IMP: R-33038-09382 */
108398     sqlite3BeginBenignMalloc();
108399     pStart = sqlite3Malloc( sz*cnt );  /* IMP: R-61949-35727 */
108400     sqlite3EndBenignMalloc();
108401   }else{
108402     sz = ROUNDDOWN8(sz); /* IMP: R-33038-09382 */
108403     pStart = pBuf;
108404   }
108405   db->lookaside.pStart = pStart;
108406   db->lookaside.pFree = 0;
108407   db->lookaside.sz = (u16)sz;
108408   if( pStart ){
108409     int i;
108410     LookasideSlot *p;
108411     assert( sz > (int)sizeof(LookasideSlot*) );
108412     p = (LookasideSlot*)pStart;
108413     for(i=cnt-1; i>=0; i--){
108414       p->pNext = db->lookaside.pFree;
108415       db->lookaside.pFree = p;
108416       p = (LookasideSlot*)&((u8*)p)[sz];
108417     }
108418     db->lookaside.pEnd = p;
108419     db->lookaside.bEnabled = 1;
108420     db->lookaside.bMalloced = pBuf==0 ?1:0;
108421   }else{
108422     db->lookaside.pEnd = 0;
108423     db->lookaside.bEnabled = 0;
108424     db->lookaside.bMalloced = 0;
108425   }
108426   return SQLITE_OK;
108427 }
108428 
108429 /*
108430 ** Return the mutex associated with a database connection.
108431 */
108432 SQLITE_API sqlite3_mutex *sqlite3_db_mutex(sqlite3 *db){
108433   return db->mutex;
108434 }
108435 
108436 /*
108437 ** Configuration settings for an individual database connection
108438 */
108439 SQLITE_API int sqlite3_db_config(sqlite3 *db, int op, ...){
108440   va_list ap;
108441   int rc;
108442   va_start(ap, op);
108443   switch( op ){
108444     case SQLITE_DBCONFIG_LOOKASIDE: {
108445       void *pBuf = va_arg(ap, void*); /* IMP: R-26835-10964 */
108446       int sz = va_arg(ap, int);       /* IMP: R-47871-25994 */
108447       int cnt = va_arg(ap, int);      /* IMP: R-04460-53386 */
108448       rc = setupLookaside(db, pBuf, sz, cnt);
108449       break;
108450     }
108451     default: {
108452       static const struct {
108453         int op;      /* The opcode */
108454         u32 mask;    /* Mask of the bit in sqlite3.flags to set/clear */
108455       } aFlagOp[] = {
108456         { SQLITE_DBCONFIG_ENABLE_FKEY,    SQLITE_ForeignKeys    },
108457         { SQLITE_DBCONFIG_ENABLE_TRIGGER, SQLITE_EnableTrigger  },
108458       };
108459       unsigned int i;
108460       rc = SQLITE_ERROR; /* IMP: R-42790-23372 */
108461       for(i=0; i<ArraySize(aFlagOp); i++){
108462         if( aFlagOp[i].op==op ){
108463           int onoff = va_arg(ap, int);
108464           int *pRes = va_arg(ap, int*);
108465           int oldFlags = db->flags;
108466           if( onoff>0 ){
108467             db->flags |= aFlagOp[i].mask;
108468           }else if( onoff==0 ){
108469             db->flags &= ~aFlagOp[i].mask;
108470           }
108471           if( oldFlags!=db->flags ){
108472             sqlite3ExpirePreparedStatements(db);
108473           }
108474           if( pRes ){
108475             *pRes = (db->flags & aFlagOp[i].mask)!=0;
108476           }
108477           rc = SQLITE_OK;
108478           break;
108479         }
108480       }
108481       break;
108482     }
108483   }
108484   va_end(ap);
108485   return rc;
108486 }
108487 
108488 
108489 /*
108490 ** Return true if the buffer z[0..n-1] contains all spaces.
108491 */
108492 static int allSpaces(const char *z, int n){
108493   while( n>0 && z[n-1]==' ' ){ n--; }
108494   return n==0;
108495 }
108496 
108497 /*
108498 ** This is the default collating function named "BINARY" which is always
108499 ** available.
108500 **
108501 ** If the padFlag argument is not NULL then space padding at the end
108502 ** of strings is ignored.  This implements the RTRIM collation.
108503 */
108504 static int binCollFunc(
108505   void *padFlag,
108506   int nKey1, const void *pKey1,
108507   int nKey2, const void *pKey2
108508 ){
108509   int rc, n;
108510   n = nKey1<nKey2 ? nKey1 : nKey2;
108511   rc = memcmp(pKey1, pKey2, n);
108512   if( rc==0 ){
108513     if( padFlag
108514      && allSpaces(((char*)pKey1)+n, nKey1-n)
108515      && allSpaces(((char*)pKey2)+n, nKey2-n)
108516     ){
108517       /* Leave rc unchanged at 0 */
108518     }else{
108519       rc = nKey1 - nKey2;
108520     }
108521   }
108522   return rc;
108523 }
108524 
108525 /*
108526 ** Another built-in collating sequence: NOCASE.
108527 **
108528 ** This collating sequence is intended to be used for "case independant
108529 ** comparison". SQLite's knowledge of upper and lower case equivalents
108530 ** extends only to the 26 characters used in the English language.
108531 **
108532 ** At the moment there is only a UTF-8 implementation.
108533 */
108534 static int nocaseCollatingFunc(
108535   void *NotUsed,
108536   int nKey1, const void *pKey1,
108537   int nKey2, const void *pKey2
108538 ){
108539   int r = sqlite3StrNICmp(
108540       (const char *)pKey1, (const char *)pKey2, (nKey1<nKey2)?nKey1:nKey2);
108541   UNUSED_PARAMETER(NotUsed);
108542   if( 0==r ){
108543     r = nKey1-nKey2;
108544   }
108545   return r;
108546 }
108547 
108548 /*
108549 ** Return the ROWID of the most recent insert
108550 */
108551 SQLITE_API sqlite_int64 sqlite3_last_insert_rowid(sqlite3 *db){
108552   return db->lastRowid;
108553 }
108554 
108555 /*
108556 ** Return the number of changes in the most recent call to sqlite3_exec().
108557 */
108558 SQLITE_API int sqlite3_changes(sqlite3 *db){
108559   return db->nChange;
108560 }
108561 
108562 /*
108563 ** Return the number of changes since the database handle was opened.
108564 */
108565 SQLITE_API int sqlite3_total_changes(sqlite3 *db){
108566   return db->nTotalChange;
108567 }
108568 
108569 /*
108570 ** Close all open savepoints. This function only manipulates fields of the
108571 ** database handle object, it does not close any savepoints that may be open
108572 ** at the b-tree/pager level.
108573 */
108574 SQLITE_PRIVATE void sqlite3CloseSavepoints(sqlite3 *db){
108575   while( db->pSavepoint ){
108576     Savepoint *pTmp = db->pSavepoint;
108577     db->pSavepoint = pTmp->pNext;
108578     sqlite3DbFree(db, pTmp);
108579   }
108580   db->nSavepoint = 0;
108581   db->nStatement = 0;
108582   db->isTransactionSavepoint = 0;
108583 }
108584 
108585 /*
108586 ** Invoke the destructor function associated with FuncDef p, if any. Except,
108587 ** if this is not the last copy of the function, do not invoke it. Multiple
108588 ** copies of a single function are created when create_function() is called
108589 ** with SQLITE_ANY as the encoding.
108590 */
108591 static void functionDestroy(sqlite3 *db, FuncDef *p){
108592   FuncDestructor *pDestructor = p->pDestructor;
108593   if( pDestructor ){
108594     pDestructor->nRef--;
108595     if( pDestructor->nRef==0 ){
108596       pDestructor->xDestroy(pDestructor->pUserData);
108597       sqlite3DbFree(db, pDestructor);
108598     }
108599   }
108600 }
108601 
108602 /*
108603 ** Close an existing SQLite database
108604 */
108605 SQLITE_API int sqlite3_close(sqlite3 *db){
108606   HashElem *i;                    /* Hash table iterator */
108607   int j;
108608 
108609   if( !db ){
108610     return SQLITE_OK;
108611   }
108612   if( !sqlite3SafetyCheckSickOrOk(db) ){
108613     return SQLITE_MISUSE_BKPT;
108614   }
108615   sqlite3_mutex_enter(db->mutex);
108616 
108617   /* Force xDestroy calls on all virtual tables */
108618   sqlite3ResetInternalSchema(db, -1);
108619 
108620   /* If a transaction is open, the ResetInternalSchema() call above
108621   ** will not have called the xDisconnect() method on any virtual
108622   ** tables in the db->aVTrans[] array. The following sqlite3VtabRollback()
108623   ** call will do so. We need to do this before the check for active
108624   ** SQL statements below, as the v-table implementation may be storing
108625   ** some prepared statements internally.
108626   */
108627   sqlite3VtabRollback(db);
108628 
108629   /* If there are any outstanding VMs, return SQLITE_BUSY. */
108630   if( db->pVdbe ){
108631     sqlite3Error(db, SQLITE_BUSY,
108632         "unable to close due to unfinalised statements");
108633     sqlite3_mutex_leave(db->mutex);
108634     return SQLITE_BUSY;
108635   }
108636   assert( sqlite3SafetyCheckSickOrOk(db) );
108637 
108638   for(j=0; j<db->nDb; j++){
108639     Btree *pBt = db->aDb[j].pBt;
108640     if( pBt && sqlite3BtreeIsInBackup(pBt) ){
108641       sqlite3Error(db, SQLITE_BUSY,
108642           "unable to close due to unfinished backup operation");
108643       sqlite3_mutex_leave(db->mutex);
108644       return SQLITE_BUSY;
108645     }
108646   }
108647 
108648   /* Free any outstanding Savepoint structures. */
108649   sqlite3CloseSavepoints(db);
108650 
108651   for(j=0; j<db->nDb; j++){
108652     struct Db *pDb = &db->aDb[j];
108653     if( pDb->pBt ){
108654       sqlite3BtreeClose(pDb->pBt);
108655       pDb->pBt = 0;
108656       if( j!=1 ){
108657         pDb->pSchema = 0;
108658       }
108659     }
108660   }
108661   sqlite3ResetInternalSchema(db, -1);
108662 
108663   /* Tell the code in notify.c that the connection no longer holds any
108664   ** locks and does not require any further unlock-notify callbacks.
108665   */
108666   sqlite3ConnectionClosed(db);
108667 
108668   assert( db->nDb<=2 );
108669   assert( db->aDb==db->aDbStatic );
108670   for(j=0; j<ArraySize(db->aFunc.a); j++){
108671     FuncDef *pNext, *pHash, *p;
108672     for(p=db->aFunc.a[j]; p; p=pHash){
108673       pHash = p->pHash;
108674       while( p ){
108675         functionDestroy(db, p);
108676         pNext = p->pNext;
108677         sqlite3DbFree(db, p);
108678         p = pNext;
108679       }
108680     }
108681   }
108682   for(i=sqliteHashFirst(&db->aCollSeq); i; i=sqliteHashNext(i)){
108683     CollSeq *pColl = (CollSeq *)sqliteHashData(i);
108684     /* Invoke any destructors registered for collation sequence user data. */
108685     for(j=0; j<3; j++){
108686       if( pColl[j].xDel ){
108687         pColl[j].xDel(pColl[j].pUser);
108688       }
108689     }
108690     sqlite3DbFree(db, pColl);
108691   }
108692   sqlite3HashClear(&db->aCollSeq);
108693 #ifndef SQLITE_OMIT_VIRTUALTABLE
108694   for(i=sqliteHashFirst(&db->aModule); i; i=sqliteHashNext(i)){
108695     Module *pMod = (Module *)sqliteHashData(i);
108696     if( pMod->xDestroy ){
108697       pMod->xDestroy(pMod->pAux);
108698     }
108699     sqlite3DbFree(db, pMod);
108700   }
108701   sqlite3HashClear(&db->aModule);
108702 #endif
108703 
108704   sqlite3Error(db, SQLITE_OK, 0); /* Deallocates any cached error strings. */
108705   if( db->pErr ){
108706     sqlite3ValueFree(db->pErr);
108707   }
108708   sqlite3CloseExtensions(db);
108709 
108710   db->magic = SQLITE_MAGIC_ERROR;
108711 
108712   /* The temp-database schema is allocated differently from the other schema
108713   ** objects (using sqliteMalloc() directly, instead of sqlite3BtreeSchema()).
108714   ** So it needs to be freed here. Todo: Why not roll the temp schema into
108715   ** the same sqliteMalloc() as the one that allocates the database
108716   ** structure?
108717   */
108718   sqlite3DbFree(db, db->aDb[1].pSchema);
108719   sqlite3_mutex_leave(db->mutex);
108720   db->magic = SQLITE_MAGIC_CLOSED;
108721   sqlite3_mutex_free(db->mutex);
108722   assert( db->lookaside.nOut==0 );  /* Fails on a lookaside memory leak */
108723   if( db->lookaside.bMalloced ){
108724     sqlite3_free(db->lookaside.pStart);
108725   }
108726   sqlite3_free(db);
108727   return SQLITE_OK;
108728 }
108729 
108730 /*
108731 ** Rollback all database files.
108732 */
108733 SQLITE_PRIVATE void sqlite3RollbackAll(sqlite3 *db){
108734   int i;
108735   int inTrans = 0;
108736   assert( sqlite3_mutex_held(db->mutex) );
108737   sqlite3BeginBenignMalloc();
108738   for(i=0; i<db->nDb; i++){
108739     if( db->aDb[i].pBt ){
108740       if( sqlite3BtreeIsInTrans(db->aDb[i].pBt) ){
108741         inTrans = 1;
108742       }
108743       sqlite3BtreeRollback(db->aDb[i].pBt);
108744       db->aDb[i].inTrans = 0;
108745     }
108746   }
108747   sqlite3VtabRollback(db);
108748   sqlite3EndBenignMalloc();
108749 
108750   if( db->flags&SQLITE_InternChanges ){
108751     sqlite3ExpirePreparedStatements(db);
108752     sqlite3ResetInternalSchema(db, -1);
108753   }
108754 
108755   /* Any deferred constraint violations have now been resolved. */
108756   db->nDeferredCons = 0;
108757 
108758   /* If one has been configured, invoke the rollback-hook callback */
108759   if( db->xRollbackCallback && (inTrans || !db->autoCommit) ){
108760     db->xRollbackCallback(db->pRollbackArg);
108761   }
108762 }
108763 
108764 /*
108765 ** Return a static string that describes the kind of error specified in the
108766 ** argument.
108767 */
108768 SQLITE_PRIVATE const char *sqlite3ErrStr(int rc){
108769   static const char* const aMsg[] = {
108770     /* SQLITE_OK          */ "not an error",
108771     /* SQLITE_ERROR       */ "SQL logic error or missing database",
108772     /* SQLITE_INTERNAL    */ 0,
108773     /* SQLITE_PERM        */ "access permission denied",
108774     /* SQLITE_ABORT       */ "callback requested query abort",
108775     /* SQLITE_BUSY        */ "database is locked",
108776     /* SQLITE_LOCKED      */ "database table is locked",
108777     /* SQLITE_NOMEM       */ "out of memory",
108778     /* SQLITE_READONLY    */ "attempt to write a readonly database",
108779     /* SQLITE_INTERRUPT   */ "interrupted",
108780     /* SQLITE_IOERR       */ "disk I/O error",
108781     /* SQLITE_CORRUPT     */ "database disk image is malformed",
108782     /* SQLITE_NOTFOUND    */ "unknown operation",
108783     /* SQLITE_FULL        */ "database or disk is full",
108784     /* SQLITE_CANTOPEN    */ "unable to open database file",
108785     /* SQLITE_PROTOCOL    */ "locking protocol",
108786     /* SQLITE_EMPTY       */ "table contains no data",
108787     /* SQLITE_SCHEMA      */ "database schema has changed",
108788     /* SQLITE_TOOBIG      */ "string or blob too big",
108789     /* SQLITE_CONSTRAINT  */ "constraint failed",
108790     /* SQLITE_MISMATCH    */ "datatype mismatch",
108791     /* SQLITE_MISUSE      */ "library routine called out of sequence",
108792     /* SQLITE_NOLFS       */ "large file support is disabled",
108793     /* SQLITE_AUTH        */ "authorization denied",
108794     /* SQLITE_FORMAT      */ "auxiliary database format error",
108795     /* SQLITE_RANGE       */ "bind or column index out of range",
108796     /* SQLITE_NOTADB      */ "file is encrypted or is not a database",
108797   };
108798   rc &= 0xff;
108799   if( ALWAYS(rc>=0) && rc<(int)(sizeof(aMsg)/sizeof(aMsg[0])) && aMsg[rc]!=0 ){
108800     return aMsg[rc];
108801   }else{
108802     return "unknown error";
108803   }
108804 }
108805 
108806 /*
108807 ** This routine implements a busy callback that sleeps and tries
108808 ** again until a timeout value is reached.  The timeout value is
108809 ** an integer number of milliseconds passed in as the first
108810 ** argument.
108811 */
108812 static int sqliteDefaultBusyCallback(
108813  void *ptr,               /* Database connection */
108814  int count                /* Number of times table has been busy */
108815 ){
108816 #if SQLITE_OS_WIN || (defined(HAVE_USLEEP) && HAVE_USLEEP)
108817   static const u8 delays[] =
108818      { 1, 2, 5, 10, 15, 20, 25, 25,  25,  50,  50, 100 };
108819   static const u8 totals[] =
108820      { 0, 1, 3,  8, 18, 33, 53, 78, 103, 128, 178, 228 };
108821 # define NDELAY ArraySize(delays)
108822   sqlite3 *db = (sqlite3 *)ptr;
108823   int timeout = db->busyTimeout;
108824   int delay, prior;
108825 
108826   assert( count>=0 );
108827   if( count < NDELAY ){
108828     delay = delays[count];
108829     prior = totals[count];
108830   }else{
108831     delay = delays[NDELAY-1];
108832     prior = totals[NDELAY-1] + delay*(count-(NDELAY-1));
108833   }
108834   if( prior + delay > timeout ){
108835     delay = timeout - prior;
108836     if( delay<=0 ) return 0;
108837   }
108838   sqlite3OsSleep(db->pVfs, delay*1000);
108839   return 1;
108840 #else
108841   sqlite3 *db = (sqlite3 *)ptr;
108842   int timeout = ((sqlite3 *)ptr)->busyTimeout;
108843   if( (count+1)*1000 > timeout ){
108844     return 0;
108845   }
108846   sqlite3OsSleep(db->pVfs, 1000000);
108847   return 1;
108848 #endif
108849 }
108850 
108851 /*
108852 ** Invoke the given busy handler.
108853 **
108854 ** This routine is called when an operation failed with a lock.
108855 ** If this routine returns non-zero, the lock is retried.  If it
108856 ** returns 0, the operation aborts with an SQLITE_BUSY error.
108857 */
108858 SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler *p){
108859   int rc;
108860   if( NEVER(p==0) || p->xFunc==0 || p->nBusy<0 ) return 0;
108861   rc = p->xFunc(p->pArg, p->nBusy);
108862   if( rc==0 ){
108863     p->nBusy = -1;
108864   }else{
108865     p->nBusy++;
108866   }
108867   return rc;
108868 }
108869 
108870 /*
108871 ** This routine sets the busy callback for an Sqlite database to the
108872 ** given callback function with the given argument.
108873 */
108874 SQLITE_API int sqlite3_busy_handler(
108875   sqlite3 *db,
108876   int (*xBusy)(void*,int),
108877   void *pArg
108878 ){
108879   sqlite3_mutex_enter(db->mutex);
108880   db->busyHandler.xFunc = xBusy;
108881   db->busyHandler.pArg = pArg;
108882   db->busyHandler.nBusy = 0;
108883   sqlite3_mutex_leave(db->mutex);
108884   return SQLITE_OK;
108885 }
108886 
108887 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
108888 /*
108889 ** This routine sets the progress callback for an Sqlite database to the
108890 ** given callback function with the given argument. The progress callback will
108891 ** be invoked every nOps opcodes.
108892 */
108893 SQLITE_API void sqlite3_progress_handler(
108894   sqlite3 *db,
108895   int nOps,
108896   int (*xProgress)(void*),
108897   void *pArg
108898 ){
108899   sqlite3_mutex_enter(db->mutex);
108900   if( nOps>0 ){
108901     db->xProgress = xProgress;
108902     db->nProgressOps = nOps;
108903     db->pProgressArg = pArg;
108904   }else{
108905     db->xProgress = 0;
108906     db->nProgressOps = 0;
108907     db->pProgressArg = 0;
108908   }
108909   sqlite3_mutex_leave(db->mutex);
108910 }
108911 #endif
108912 
108913 
108914 /*
108915 ** This routine installs a default busy handler that waits for the
108916 ** specified number of milliseconds before returning 0.
108917 */
108918 SQLITE_API int sqlite3_busy_timeout(sqlite3 *db, int ms){
108919   if( ms>0 ){
108920     db->busyTimeout = ms;
108921     sqlite3_busy_handler(db, sqliteDefaultBusyCallback, (void*)db);
108922   }else{
108923     sqlite3_busy_handler(db, 0, 0);
108924   }
108925   return SQLITE_OK;
108926 }
108927 
108928 /*
108929 ** Cause any pending operation to stop at its earliest opportunity.
108930 */
108931 SQLITE_API void sqlite3_interrupt(sqlite3 *db){
108932   db->u1.isInterrupted = 1;
108933 }
108934 
108935 
108936 /*
108937 ** This function is exactly the same as sqlite3_create_function(), except
108938 ** that it is designed to be called by internal code. The difference is
108939 ** that if a malloc() fails in sqlite3_create_function(), an error code
108940 ** is returned and the mallocFailed flag cleared.
108941 */
108942 SQLITE_PRIVATE int sqlite3CreateFunc(
108943   sqlite3 *db,
108944   const char *zFunctionName,
108945   int nArg,
108946   int enc,
108947   void *pUserData,
108948   void (*xFunc)(sqlite3_context*,int,sqlite3_value **),
108949   void (*xStep)(sqlite3_context*,int,sqlite3_value **),
108950   void (*xFinal)(sqlite3_context*),
108951   FuncDestructor *pDestructor
108952 ){
108953   FuncDef *p;
108954   int nName;
108955 
108956   assert( sqlite3_mutex_held(db->mutex) );
108957   if( zFunctionName==0 ||
108958       (xFunc && (xFinal || xStep)) ||
108959       (!xFunc && (xFinal && !xStep)) ||
108960       (!xFunc && (!xFinal && xStep)) ||
108961       (nArg<-1 || nArg>SQLITE_MAX_FUNCTION_ARG) ||
108962       (255<(nName = sqlite3Strlen30( zFunctionName))) ){
108963     return SQLITE_MISUSE_BKPT;
108964   }
108965 
108966 #ifndef SQLITE_OMIT_UTF16
108967   /* If SQLITE_UTF16 is specified as the encoding type, transform this
108968   ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
108969   ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
108970   **
108971   ** If SQLITE_ANY is specified, add three versions of the function
108972   ** to the hash table.
108973   */
108974   if( enc==SQLITE_UTF16 ){
108975     enc = SQLITE_UTF16NATIVE;
108976   }else if( enc==SQLITE_ANY ){
108977     int rc;
108978     rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF8,
108979          pUserData, xFunc, xStep, xFinal, pDestructor);
108980     if( rc==SQLITE_OK ){
108981       rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF16LE,
108982           pUserData, xFunc, xStep, xFinal, pDestructor);
108983     }
108984     if( rc!=SQLITE_OK ){
108985       return rc;
108986     }
108987     enc = SQLITE_UTF16BE;
108988   }
108989 #else
108990   enc = SQLITE_UTF8;
108991 #endif
108992 
108993   /* Check if an existing function is being overridden or deleted. If so,
108994   ** and there are active VMs, then return SQLITE_BUSY. If a function
108995   ** is being overridden/deleted but there are no active VMs, allow the
108996   ** operation to continue but invalidate all precompiled statements.
108997   */
108998   p = sqlite3FindFunction(db, zFunctionName, nName, nArg, (u8)enc, 0);
108999   if( p && p->iPrefEnc==enc && p->nArg==nArg ){
109000     if( db->activeVdbeCnt ){
109001       sqlite3Error(db, SQLITE_BUSY,
109002         "unable to delete/modify user-function due to active statements");
109003       assert( !db->mallocFailed );
109004       return SQLITE_BUSY;
109005     }else{
109006       sqlite3ExpirePreparedStatements(db);
109007     }
109008   }
109009 
109010   p = sqlite3FindFunction(db, zFunctionName, nName, nArg, (u8)enc, 1);
109011   assert(p || db->mallocFailed);
109012   if( !p ){
109013     return SQLITE_NOMEM;
109014   }
109015 
109016   /* If an older version of the function with a configured destructor is
109017   ** being replaced invoke the destructor function here. */
109018   functionDestroy(db, p);
109019 
109020   if( pDestructor ){
109021     pDestructor->nRef++;
109022   }
109023   p->pDestructor = pDestructor;
109024   p->flags = 0;
109025   p->xFunc = xFunc;
109026   p->xStep = xStep;
109027   p->xFinalize = xFinal;
109028   p->pUserData = pUserData;
109029   p->nArg = (u16)nArg;
109030   return SQLITE_OK;
109031 }
109032 
109033 /*
109034 ** Create new user functions.
109035 */
109036 SQLITE_API int sqlite3_create_function(
109037   sqlite3 *db,
109038   const char *zFunc,
109039   int nArg,
109040   int enc,
109041   void *p,
109042   void (*xFunc)(sqlite3_context*,int,sqlite3_value **),
109043   void (*xStep)(sqlite3_context*,int,sqlite3_value **),
109044   void (*xFinal)(sqlite3_context*)
109045 ){
109046   return sqlite3_create_function_v2(db, zFunc, nArg, enc, p, xFunc, xStep,
109047                                     xFinal, 0);
109048 }
109049 
109050 SQLITE_API int sqlite3_create_function_v2(
109051   sqlite3 *db,
109052   const char *zFunc,
109053   int nArg,
109054   int enc,
109055   void *p,
109056   void (*xFunc)(sqlite3_context*,int,sqlite3_value **),
109057   void (*xStep)(sqlite3_context*,int,sqlite3_value **),
109058   void (*xFinal)(sqlite3_context*),
109059   void (*xDestroy)(void *)
109060 ){
109061   int rc = SQLITE_ERROR;
109062   FuncDestructor *pArg = 0;
109063   sqlite3_mutex_enter(db->mutex);
109064   if( xDestroy ){
109065     pArg = (FuncDestructor *)sqlite3DbMallocZero(db, sizeof(FuncDestructor));
109066     if( !pArg ){
109067       xDestroy(p);
109068       goto out;
109069     }
109070     pArg->xDestroy = xDestroy;
109071     pArg->pUserData = p;
109072   }
109073   rc = sqlite3CreateFunc(db, zFunc, nArg, enc, p, xFunc, xStep, xFinal, pArg);
109074   if( pArg && pArg->nRef==0 ){
109075     assert( rc!=SQLITE_OK );
109076     xDestroy(p);
109077     sqlite3DbFree(db, pArg);
109078   }
109079 
109080  out:
109081   rc = sqlite3ApiExit(db, rc);
109082   sqlite3_mutex_leave(db->mutex);
109083   return rc;
109084 }
109085 
109086 #ifndef SQLITE_OMIT_UTF16
109087 SQLITE_API int sqlite3_create_function16(
109088   sqlite3 *db,
109089   const void *zFunctionName,
109090   int nArg,
109091   int eTextRep,
109092   void *p,
109093   void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
109094   void (*xStep)(sqlite3_context*,int,sqlite3_value**),
109095   void (*xFinal)(sqlite3_context*)
109096 ){
109097   int rc;
109098   char *zFunc8;
109099   sqlite3_mutex_enter(db->mutex);
109100   assert( !db->mallocFailed );
109101   zFunc8 = sqlite3Utf16to8(db, zFunctionName, -1, SQLITE_UTF16NATIVE);
109102   rc = sqlite3CreateFunc(db, zFunc8, nArg, eTextRep, p, xFunc, xStep, xFinal,0);
109103   sqlite3DbFree(db, zFunc8);
109104   rc = sqlite3ApiExit(db, rc);
109105   sqlite3_mutex_leave(db->mutex);
109106   return rc;
109107 }
109108 #endif
109109 
109110 
109111 /*
109112 ** Declare that a function has been overloaded by a virtual table.
109113 **
109114 ** If the function already exists as a regular global function, then
109115 ** this routine is a no-op.  If the function does not exist, then create
109116 ** a new one that always throws a run-time error.
109117 **
109118 ** When virtual tables intend to provide an overloaded function, they
109119 ** should call this routine to make sure the global function exists.
109120 ** A global function must exist in order for name resolution to work
109121 ** properly.
109122 */
109123 SQLITE_API int sqlite3_overload_function(
109124   sqlite3 *db,
109125   const char *zName,
109126   int nArg
109127 ){
109128   int nName = sqlite3Strlen30(zName);
109129   int rc;
109130   sqlite3_mutex_enter(db->mutex);
109131   if( sqlite3FindFunction(db, zName, nName, nArg, SQLITE_UTF8, 0)==0 ){
109132     sqlite3CreateFunc(db, zName, nArg, SQLITE_UTF8,
109133                       0, sqlite3InvalidFunction, 0, 0, 0);
109134   }
109135   rc = sqlite3ApiExit(db, SQLITE_OK);
109136   sqlite3_mutex_leave(db->mutex);
109137   return rc;
109138 }
109139 
109140 #ifndef SQLITE_OMIT_TRACE
109141 /*
109142 ** Register a trace function.  The pArg from the previously registered trace
109143 ** is returned.
109144 **
109145 ** A NULL trace function means that no tracing is executes.  A non-NULL
109146 ** trace is a pointer to a function that is invoked at the start of each
109147 ** SQL statement.
109148 */
109149 SQLITE_API void *sqlite3_trace(sqlite3 *db, void (*xTrace)(void*,const char*), void *pArg){
109150   void *pOld;
109151   sqlite3_mutex_enter(db->mutex);
109152   pOld = db->pTraceArg;
109153   db->xTrace = xTrace;
109154   db->pTraceArg = pArg;
109155   sqlite3_mutex_leave(db->mutex);
109156   return pOld;
109157 }
109158 /*
109159 ** Register a profile function.  The pArg from the previously registered
109160 ** profile function is returned.
109161 **
109162 ** A NULL profile function means that no profiling is executes.  A non-NULL
109163 ** profile is a pointer to a function that is invoked at the conclusion of
109164 ** each SQL statement that is run.
109165 */
109166 SQLITE_API void *sqlite3_profile(
109167   sqlite3 *db,
109168   void (*xProfile)(void*,const char*,sqlite_uint64),
109169   void *pArg
109170 ){
109171   void *pOld;
109172   sqlite3_mutex_enter(db->mutex);
109173   pOld = db->pProfileArg;
109174   db->xProfile = xProfile;
109175   db->pProfileArg = pArg;
109176   sqlite3_mutex_leave(db->mutex);
109177   return pOld;
109178 }
109179 #endif /* SQLITE_OMIT_TRACE */
109180 
109181 /*** EXPERIMENTAL ***
109182 **
109183 ** Register a function to be invoked when a transaction comments.
109184 ** If the invoked function returns non-zero, then the commit becomes a
109185 ** rollback.
109186 */
109187 SQLITE_API void *sqlite3_commit_hook(
109188   sqlite3 *db,              /* Attach the hook to this database */
109189   int (*xCallback)(void*),  /* Function to invoke on each commit */
109190   void *pArg                /* Argument to the function */
109191 ){
109192   void *pOld;
109193   sqlite3_mutex_enter(db->mutex);
109194   pOld = db->pCommitArg;
109195   db->xCommitCallback = xCallback;
109196   db->pCommitArg = pArg;
109197   sqlite3_mutex_leave(db->mutex);
109198   return pOld;
109199 }
109200 
109201 /*
109202 ** Register a callback to be invoked each time a row is updated,
109203 ** inserted or deleted using this database connection.
109204 */
109205 SQLITE_API void *sqlite3_update_hook(
109206   sqlite3 *db,              /* Attach the hook to this database */
109207   void (*xCallback)(void*,int,char const *,char const *,sqlite_int64),
109208   void *pArg                /* Argument to the function */
109209 ){
109210   void *pRet;
109211   sqlite3_mutex_enter(db->mutex);
109212   pRet = db->pUpdateArg;
109213   db->xUpdateCallback = xCallback;
109214   db->pUpdateArg = pArg;
109215   sqlite3_mutex_leave(db->mutex);
109216   return pRet;
109217 }
109218 
109219 /*
109220 ** Register a callback to be invoked each time a transaction is rolled
109221 ** back by this database connection.
109222 */
109223 SQLITE_API void *sqlite3_rollback_hook(
109224   sqlite3 *db,              /* Attach the hook to this database */
109225   void (*xCallback)(void*), /* Callback function */
109226   void *pArg                /* Argument to the function */
109227 ){
109228   void *pRet;
109229   sqlite3_mutex_enter(db->mutex);
109230   pRet = db->pRollbackArg;
109231   db->xRollbackCallback = xCallback;
109232   db->pRollbackArg = pArg;
109233   sqlite3_mutex_leave(db->mutex);
109234   return pRet;
109235 }
109236 
109237 #ifndef SQLITE_OMIT_WAL
109238 /*
109239 ** The sqlite3_wal_hook() callback registered by sqlite3_wal_autocheckpoint().
109240 ** Invoke sqlite3_wal_checkpoint if the number of frames in the log file
109241 ** is greater than sqlite3.pWalArg cast to an integer (the value configured by
109242 ** wal_autocheckpoint()).
109243 */
109244 SQLITE_PRIVATE int sqlite3WalDefaultHook(
109245   void *pClientData,     /* Argument */
109246   sqlite3 *db,           /* Connection */
109247   const char *zDb,       /* Database */
109248   int nFrame             /* Size of WAL */
109249 ){
109250   if( nFrame>=SQLITE_PTR_TO_INT(pClientData) ){
109251     sqlite3BeginBenignMalloc();
109252     sqlite3_wal_checkpoint(db, zDb);
109253     sqlite3EndBenignMalloc();
109254   }
109255   return SQLITE_OK;
109256 }
109257 #endif /* SQLITE_OMIT_WAL */
109258 
109259 /*
109260 ** Configure an sqlite3_wal_hook() callback to automatically checkpoint
109261 ** a database after committing a transaction if there are nFrame or
109262 ** more frames in the log file. Passing zero or a negative value as the
109263 ** nFrame parameter disables automatic checkpoints entirely.
109264 **
109265 ** The callback registered by this function replaces any existing callback
109266 ** registered using sqlite3_wal_hook(). Likewise, registering a callback
109267 ** using sqlite3_wal_hook() disables the automatic checkpoint mechanism
109268 ** configured by this function.
109269 */
109270 SQLITE_API int sqlite3_wal_autocheckpoint(sqlite3 *db, int nFrame){
109271 #ifdef SQLITE_OMIT_WAL
109272   UNUSED_PARAMETER(db);
109273   UNUSED_PARAMETER(nFrame);
109274 #else
109275   if( nFrame>0 ){
109276     sqlite3_wal_hook(db, sqlite3WalDefaultHook, SQLITE_INT_TO_PTR(nFrame));
109277   }else{
109278     sqlite3_wal_hook(db, 0, 0);
109279   }
109280 #endif
109281   return SQLITE_OK;
109282 }
109283 
109284 /*
109285 ** Register a callback to be invoked each time a transaction is written
109286 ** into the write-ahead-log by this database connection.
109287 */
109288 SQLITE_API void *sqlite3_wal_hook(
109289   sqlite3 *db,                    /* Attach the hook to this db handle */
109290   int(*xCallback)(void *, sqlite3*, const char*, int),
109291   void *pArg                      /* First argument passed to xCallback() */
109292 ){
109293 #ifndef SQLITE_OMIT_WAL
109294   void *pRet;
109295   sqlite3_mutex_enter(db->mutex);
109296   pRet = db->pWalArg;
109297   db->xWalCallback = xCallback;
109298   db->pWalArg = pArg;
109299   sqlite3_mutex_leave(db->mutex);
109300   return pRet;
109301 #else
109302   return 0;
109303 #endif
109304 }
109305 
109306 /*
109307 ** Checkpoint database zDb.
109308 */
109309 SQLITE_API int sqlite3_wal_checkpoint_v2(
109310   sqlite3 *db,                    /* Database handle */
109311   const char *zDb,                /* Name of attached database (or NULL) */
109312   int eMode,                      /* SQLITE_CHECKPOINT_* value */
109313   int *pnLog,                     /* OUT: Size of WAL log in frames */
109314   int *pnCkpt                     /* OUT: Total number of frames checkpointed */
109315 ){
109316 #ifdef SQLITE_OMIT_WAL
109317   return SQLITE_OK;
109318 #else
109319   int rc;                         /* Return code */
109320   int iDb = SQLITE_MAX_ATTACHED;  /* sqlite3.aDb[] index of db to checkpoint */
109321 
109322   /* Initialize the output variables to -1 in case an error occurs. */
109323   if( pnLog ) *pnLog = -1;
109324   if( pnCkpt ) *pnCkpt = -1;
109325 
109326   assert( SQLITE_CHECKPOINT_FULL>SQLITE_CHECKPOINT_PASSIVE );
109327   assert( SQLITE_CHECKPOINT_FULL<SQLITE_CHECKPOINT_RESTART );
109328   assert( SQLITE_CHECKPOINT_PASSIVE+2==SQLITE_CHECKPOINT_RESTART );
109329   if( eMode<SQLITE_CHECKPOINT_PASSIVE || eMode>SQLITE_CHECKPOINT_RESTART ){
109330     return SQLITE_MISUSE;
109331   }
109332 
109333   sqlite3_mutex_enter(db->mutex);
109334   if( zDb && zDb[0] ){
109335     iDb = sqlite3FindDbName(db, zDb);
109336   }
109337   if( iDb<0 ){
109338     rc = SQLITE_ERROR;
109339     sqlite3Error(db, SQLITE_ERROR, "unknown database: %s", zDb);
109340   }else{
109341     rc = sqlite3Checkpoint(db, iDb, eMode, pnLog, pnCkpt);
109342     sqlite3Error(db, rc, 0);
109343   }
109344   rc = sqlite3ApiExit(db, rc);
109345   sqlite3_mutex_leave(db->mutex);
109346   return rc;
109347 #endif
109348 }
109349 
109350 
109351 /*
109352 ** Checkpoint database zDb. If zDb is NULL, or if the buffer zDb points
109353 ** to contains a zero-length string, all attached databases are
109354 ** checkpointed.
109355 */
109356 SQLITE_API int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb){
109357   return sqlite3_wal_checkpoint_v2(db, zDb, SQLITE_CHECKPOINT_PASSIVE, 0, 0);
109358 }
109359 
109360 #ifndef SQLITE_OMIT_WAL
109361 /*
109362 ** Run a checkpoint on database iDb. This is a no-op if database iDb is
109363 ** not currently open in WAL mode.
109364 **
109365 ** If a transaction is open on the database being checkpointed, this
109366 ** function returns SQLITE_LOCKED and a checkpoint is not attempted. If
109367 ** an error occurs while running the checkpoint, an SQLite error code is
109368 ** returned (i.e. SQLITE_IOERR). Otherwise, SQLITE_OK.
109369 **
109370 ** The mutex on database handle db should be held by the caller. The mutex
109371 ** associated with the specific b-tree being checkpointed is taken by
109372 ** this function while the checkpoint is running.
109373 **
109374 ** If iDb is passed SQLITE_MAX_ATTACHED, then all attached databases are
109375 ** checkpointed. If an error is encountered it is returned immediately -
109376 ** no attempt is made to checkpoint any remaining databases.
109377 **
109378 ** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART.
109379 */
109380 SQLITE_PRIVATE int sqlite3Checkpoint(sqlite3 *db, int iDb, int eMode, int *pnLog, int *pnCkpt){
109381   int rc = SQLITE_OK;             /* Return code */
109382   int i;                          /* Used to iterate through attached dbs */
109383   int bBusy = 0;                  /* True if SQLITE_BUSY has been encountered */
109384 
109385   assert( sqlite3_mutex_held(db->mutex) );
109386   assert( !pnLog || *pnLog==-1 );
109387   assert( !pnCkpt || *pnCkpt==-1 );
109388 
109389   for(i=0; i<db->nDb && rc==SQLITE_OK; i++){
109390     if( i==iDb || iDb==SQLITE_MAX_ATTACHED ){
109391       rc = sqlite3BtreeCheckpoint(db->aDb[i].pBt, eMode, pnLog, pnCkpt);
109392       pnLog = 0;
109393       pnCkpt = 0;
109394       if( rc==SQLITE_BUSY ){
109395         bBusy = 1;
109396         rc = SQLITE_OK;
109397       }
109398     }
109399   }
109400 
109401   return (rc==SQLITE_OK && bBusy) ? SQLITE_BUSY : rc;
109402 }
109403 #endif /* SQLITE_OMIT_WAL */
109404 
109405 /*
109406 ** This function returns true if main-memory should be used instead of
109407 ** a temporary file for transient pager files and statement journals.
109408 ** The value returned depends on the value of db->temp_store (runtime
109409 ** parameter) and the compile time value of SQLITE_TEMP_STORE. The
109410 ** following table describes the relationship between these two values
109411 ** and this functions return value.
109412 **
109413 **   SQLITE_TEMP_STORE     db->temp_store     Location of temporary database
109414 **   -----------------     --------------     ------------------------------
109415 **   0                     any                file      (return 0)
109416 **   1                     1                  file      (return 0)
109417 **   1                     2                  memory    (return 1)
109418 **   1                     0                  file      (return 0)
109419 **   2                     1                  file      (return 0)
109420 **   2                     2                  memory    (return 1)
109421 **   2                     0                  memory    (return 1)
109422 **   3                     any                memory    (return 1)
109423 */
109424 SQLITE_PRIVATE int sqlite3TempInMemory(const sqlite3 *db){
109425 #if SQLITE_TEMP_STORE==1
109426   return ( db->temp_store==2 );
109427 #endif
109428 #if SQLITE_TEMP_STORE==2
109429   return ( db->temp_store!=1 );
109430 #endif
109431 #if SQLITE_TEMP_STORE==3
109432   return 1;
109433 #endif
109434 #if SQLITE_TEMP_STORE<1 || SQLITE_TEMP_STORE>3
109435   return 0;
109436 #endif
109437 }
109438 
109439 /*
109440 ** Return UTF-8 encoded English language explanation of the most recent
109441 ** error.
109442 */
109443 SQLITE_API const char *sqlite3_errmsg(sqlite3 *db){
109444   const char *z;
109445   if( !db ){
109446     return sqlite3ErrStr(SQLITE_NOMEM);
109447   }
109448   if( !sqlite3SafetyCheckSickOrOk(db) ){
109449     return sqlite3ErrStr(SQLITE_MISUSE_BKPT);
109450   }
109451   sqlite3_mutex_enter(db->mutex);
109452   if( db->mallocFailed ){
109453     z = sqlite3ErrStr(SQLITE_NOMEM);
109454   }else{
109455     z = (char*)sqlite3_value_text(db->pErr);
109456     assert( !db->mallocFailed );
109457     if( z==0 ){
109458       z = sqlite3ErrStr(db->errCode);
109459     }
109460   }
109461   sqlite3_mutex_leave(db->mutex);
109462   return z;
109463 }
109464 
109465 #ifndef SQLITE_OMIT_UTF16
109466 /*
109467 ** Return UTF-16 encoded English language explanation of the most recent
109468 ** error.
109469 */
109470 SQLITE_API const void *sqlite3_errmsg16(sqlite3 *db){
109471   static const u16 outOfMem[] = {
109472     'o', 'u', 't', ' ', 'o', 'f', ' ', 'm', 'e', 'm', 'o', 'r', 'y', 0
109473   };
109474   static const u16 misuse[] = {
109475     'l', 'i', 'b', 'r', 'a', 'r', 'y', ' ',
109476     'r', 'o', 'u', 't', 'i', 'n', 'e', ' ',
109477     'c', 'a', 'l', 'l', 'e', 'd', ' ',
109478     'o', 'u', 't', ' ',
109479     'o', 'f', ' ',
109480     's', 'e', 'q', 'u', 'e', 'n', 'c', 'e', 0
109481   };
109482 
109483   const void *z;
109484   if( !db ){
109485     return (void *)outOfMem;
109486   }
109487   if( !sqlite3SafetyCheckSickOrOk(db) ){
109488     return (void *)misuse;
109489   }
109490   sqlite3_mutex_enter(db->mutex);
109491   if( db->mallocFailed ){
109492     z = (void *)outOfMem;
109493   }else{
109494     z = sqlite3_value_text16(db->pErr);
109495     if( z==0 ){
109496       sqlite3ValueSetStr(db->pErr, -1, sqlite3ErrStr(db->errCode),
109497            SQLITE_UTF8, SQLITE_STATIC);
109498       z = sqlite3_value_text16(db->pErr);
109499     }
109500     /* A malloc() may have failed within the call to sqlite3_value_text16()
109501     ** above. If this is the case, then the db->mallocFailed flag needs to
109502     ** be cleared before returning. Do this directly, instead of via
109503     ** sqlite3ApiExit(), to avoid setting the database handle error message.
109504     */
109505     db->mallocFailed = 0;
109506   }
109507   sqlite3_mutex_leave(db->mutex);
109508   return z;
109509 }
109510 #endif /* SQLITE_OMIT_UTF16 */
109511 
109512 /*
109513 ** Return the most recent error code generated by an SQLite routine. If NULL is
109514 ** passed to this function, we assume a malloc() failed during sqlite3_open().
109515 */
109516 SQLITE_API int sqlite3_errcode(sqlite3 *db){
109517   if( db && !sqlite3SafetyCheckSickOrOk(db) ){
109518     return SQLITE_MISUSE_BKPT;
109519   }
109520   if( !db || db->mallocFailed ){
109521     return SQLITE_NOMEM;
109522   }
109523   return db->errCode & db->errMask;
109524 }
109525 SQLITE_API int sqlite3_extended_errcode(sqlite3 *db){
109526   if( db && !sqlite3SafetyCheckSickOrOk(db) ){
109527     return SQLITE_MISUSE_BKPT;
109528   }
109529   if( !db || db->mallocFailed ){
109530     return SQLITE_NOMEM;
109531   }
109532   return db->errCode;
109533 }
109534 
109535 /*
109536 ** Create a new collating function for database "db".  The name is zName
109537 ** and the encoding is enc.
109538 */
109539 static int createCollation(
109540   sqlite3* db,
109541   const char *zName,
109542   u8 enc,
109543   u8 collType,
109544   void* pCtx,
109545   int(*xCompare)(void*,int,const void*,int,const void*),
109546   void(*xDel)(void*)
109547 ){
109548   CollSeq *pColl;
109549   int enc2;
109550   int nName = sqlite3Strlen30(zName);
109551 
109552   assert( sqlite3_mutex_held(db->mutex) );
109553 
109554   /* If SQLITE_UTF16 is specified as the encoding type, transform this
109555   ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
109556   ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
109557   */
109558   enc2 = enc;
109559   testcase( enc2==SQLITE_UTF16 );
109560   testcase( enc2==SQLITE_UTF16_ALIGNED );
109561   if( enc2==SQLITE_UTF16 || enc2==SQLITE_UTF16_ALIGNED ){
109562     enc2 = SQLITE_UTF16NATIVE;
109563   }
109564   if( enc2<SQLITE_UTF8 || enc2>SQLITE_UTF16BE ){
109565     return SQLITE_MISUSE_BKPT;
109566   }
109567 
109568   /* Check if this call is removing or replacing an existing collation
109569   ** sequence. If so, and there are active VMs, return busy. If there
109570   ** are no active VMs, invalidate any pre-compiled statements.
109571   */
109572   pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, 0);
109573   if( pColl && pColl->xCmp ){
109574     if( db->activeVdbeCnt ){
109575       sqlite3Error(db, SQLITE_BUSY,
109576         "unable to delete/modify collation sequence due to active statements");
109577       return SQLITE_BUSY;
109578     }
109579     sqlite3ExpirePreparedStatements(db);
109580 
109581     /* If collation sequence pColl was created directly by a call to
109582     ** sqlite3_create_collation, and not generated by synthCollSeq(),
109583     ** then any copies made by synthCollSeq() need to be invalidated.
109584     ** Also, collation destructor - CollSeq.xDel() - function may need
109585     ** to be called.
109586     */
109587     if( (pColl->enc & ~SQLITE_UTF16_ALIGNED)==enc2 ){
109588       CollSeq *aColl = sqlite3HashFind(&db->aCollSeq, zName, nName);
109589       int j;
109590       for(j=0; j<3; j++){
109591         CollSeq *p = &aColl[j];
109592         if( p->enc==pColl->enc ){
109593           if( p->xDel ){
109594             p->xDel(p->pUser);
109595           }
109596           p->xCmp = 0;
109597         }
109598       }
109599     }
109600   }
109601 
109602   pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, 1);
109603   if( pColl==0 ) return SQLITE_NOMEM;
109604   pColl->xCmp = xCompare;
109605   pColl->pUser = pCtx;
109606   pColl->xDel = xDel;
109607   pColl->enc = (u8)(enc2 | (enc & SQLITE_UTF16_ALIGNED));
109608   pColl->type = collType;
109609   sqlite3Error(db, SQLITE_OK, 0);
109610   return SQLITE_OK;
109611 }
109612 
109613 
109614 /*
109615 ** This array defines hard upper bounds on limit values.  The
109616 ** initializer must be kept in sync with the SQLITE_LIMIT_*
109617 ** #defines in sqlite3.h.
109618 */
109619 static const int aHardLimit[] = {
109620   SQLITE_MAX_LENGTH,
109621   SQLITE_MAX_SQL_LENGTH,
109622   SQLITE_MAX_COLUMN,
109623   SQLITE_MAX_EXPR_DEPTH,
109624   SQLITE_MAX_COMPOUND_SELECT,
109625   SQLITE_MAX_VDBE_OP,
109626   SQLITE_MAX_FUNCTION_ARG,
109627   SQLITE_MAX_ATTACHED,
109628   SQLITE_MAX_LIKE_PATTERN_LENGTH,
109629   SQLITE_MAX_VARIABLE_NUMBER,
109630   SQLITE_MAX_TRIGGER_DEPTH,
109631 };
109632 
109633 /*
109634 ** Make sure the hard limits are set to reasonable values
109635 */
109636 #if SQLITE_MAX_LENGTH<100
109637 # error SQLITE_MAX_LENGTH must be at least 100
109638 #endif
109639 #if SQLITE_MAX_SQL_LENGTH<100
109640 # error SQLITE_MAX_SQL_LENGTH must be at least 100
109641 #endif
109642 #if SQLITE_MAX_SQL_LENGTH>SQLITE_MAX_LENGTH
109643 # error SQLITE_MAX_SQL_LENGTH must not be greater than SQLITE_MAX_LENGTH
109644 #endif
109645 #if SQLITE_MAX_COMPOUND_SELECT<2
109646 # error SQLITE_MAX_COMPOUND_SELECT must be at least 2
109647 #endif
109648 #if SQLITE_MAX_VDBE_OP<40
109649 # error SQLITE_MAX_VDBE_OP must be at least 40
109650 #endif
109651 #if SQLITE_MAX_FUNCTION_ARG<0 || SQLITE_MAX_FUNCTION_ARG>1000
109652 # error SQLITE_MAX_FUNCTION_ARG must be between 0 and 1000
109653 #endif
109654 #if SQLITE_MAX_ATTACHED<0 || SQLITE_MAX_ATTACHED>62
109655 # error SQLITE_MAX_ATTACHED must be between 0 and 62
109656 #endif
109657 #if SQLITE_MAX_LIKE_PATTERN_LENGTH<1
109658 # error SQLITE_MAX_LIKE_PATTERN_LENGTH must be at least 1
109659 #endif
109660 #if SQLITE_MAX_COLUMN>32767
109661 # error SQLITE_MAX_COLUMN must not exceed 32767
109662 #endif
109663 #if SQLITE_MAX_TRIGGER_DEPTH<1
109664 # error SQLITE_MAX_TRIGGER_DEPTH must be at least 1
109665 #endif
109666 
109667 
109668 /*
109669 ** Change the value of a limit.  Report the old value.
109670 ** If an invalid limit index is supplied, report -1.
109671 ** Make no changes but still report the old value if the
109672 ** new limit is negative.
109673 **
109674 ** A new lower limit does not shrink existing constructs.
109675 ** It merely prevents new constructs that exceed the limit
109676 ** from forming.
109677 */
109678 SQLITE_API int sqlite3_limit(sqlite3 *db, int limitId, int newLimit){
109679   int oldLimit;
109680 
109681 
109682   /* EVIDENCE-OF: R-30189-54097 For each limit category SQLITE_LIMIT_NAME
109683   ** there is a hard upper bound set at compile-time by a C preprocessor
109684   ** macro called SQLITE_MAX_NAME. (The "_LIMIT_" in the name is changed to
109685   ** "_MAX_".)
109686   */
109687   assert( aHardLimit[SQLITE_LIMIT_LENGTH]==SQLITE_MAX_LENGTH );
109688   assert( aHardLimit[SQLITE_LIMIT_SQL_LENGTH]==SQLITE_MAX_SQL_LENGTH );
109689   assert( aHardLimit[SQLITE_LIMIT_COLUMN]==SQLITE_MAX_COLUMN );
109690   assert( aHardLimit[SQLITE_LIMIT_EXPR_DEPTH]==SQLITE_MAX_EXPR_DEPTH );
109691   assert( aHardLimit[SQLITE_LIMIT_COMPOUND_SELECT]==SQLITE_MAX_COMPOUND_SELECT);
109692   assert( aHardLimit[SQLITE_LIMIT_VDBE_OP]==SQLITE_MAX_VDBE_OP );
109693   assert( aHardLimit[SQLITE_LIMIT_FUNCTION_ARG]==SQLITE_MAX_FUNCTION_ARG );
109694   assert( aHardLimit[SQLITE_LIMIT_ATTACHED]==SQLITE_MAX_ATTACHED );
109695   assert( aHardLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]==
109696                                                SQLITE_MAX_LIKE_PATTERN_LENGTH );
109697   assert( aHardLimit[SQLITE_LIMIT_VARIABLE_NUMBER]==SQLITE_MAX_VARIABLE_NUMBER);
109698   assert( aHardLimit[SQLITE_LIMIT_TRIGGER_DEPTH]==SQLITE_MAX_TRIGGER_DEPTH );
109699   assert( SQLITE_LIMIT_TRIGGER_DEPTH==(SQLITE_N_LIMIT-1) );
109700 
109701 
109702   if( limitId<0 || limitId>=SQLITE_N_LIMIT ){
109703     return -1;
109704   }
109705   oldLimit = db->aLimit[limitId];
109706   if( newLimit>=0 ){                   /* IMP: R-52476-28732 */
109707     if( newLimit>aHardLimit[limitId] ){
109708       newLimit = aHardLimit[limitId];  /* IMP: R-51463-25634 */
109709     }
109710     db->aLimit[limitId] = newLimit;
109711   }
109712   return oldLimit;                     /* IMP: R-53341-35419 */
109713 }
109714 
109715 /*
109716 ** This function is used to parse both URIs and non-URI filenames passed by the
109717 ** user to API functions sqlite3_open() or sqlite3_open_v2(), and for database
109718 ** URIs specified as part of ATTACH statements.
109719 **
109720 ** The first argument to this function is the name of the VFS to use (or
109721 ** a NULL to signify the default VFS) if the URI does not contain a "vfs=xxx"
109722 ** query parameter. The second argument contains the URI (or non-URI filename)
109723 ** itself. When this function is called the *pFlags variable should contain
109724 ** the default flags to open the database handle with. The value stored in
109725 ** *pFlags may be updated before returning if the URI filename contains
109726 ** "cache=xxx" or "mode=xxx" query parameters.
109727 **
109728 ** If successful, SQLITE_OK is returned. In this case *ppVfs is set to point to
109729 ** the VFS that should be used to open the database file. *pzFile is set to
109730 ** point to a buffer containing the name of the file to open. It is the
109731 ** responsibility of the caller to eventually call sqlite3_free() to release
109732 ** this buffer.
109733 **
109734 ** If an error occurs, then an SQLite error code is returned and *pzErrMsg
109735 ** may be set to point to a buffer containing an English language error
109736 ** message. It is the responsibility of the caller to eventually release
109737 ** this buffer by calling sqlite3_free().
109738 */
109739 SQLITE_PRIVATE int sqlite3ParseUri(
109740   const char *zDefaultVfs,        /* VFS to use if no "vfs=xxx" query option */
109741   const char *zUri,               /* Nul-terminated URI to parse */
109742   unsigned int *pFlags,           /* IN/OUT: SQLITE_OPEN_XXX flags */
109743   sqlite3_vfs **ppVfs,            /* OUT: VFS to use */
109744   char **pzFile,                  /* OUT: Filename component of URI */
109745   char **pzErrMsg                 /* OUT: Error message (if rc!=SQLITE_OK) */
109746 ){
109747   int rc = SQLITE_OK;
109748   unsigned int flags = *pFlags;
109749   const char *zVfs = zDefaultVfs;
109750   char *zFile;
109751   char c;
109752   int nUri = sqlite3Strlen30(zUri);
109753 
109754   assert( *pzErrMsg==0 );
109755 
109756   if( ((flags & SQLITE_OPEN_URI) || sqlite3GlobalConfig.bOpenUri)
109757    && nUri>=5 && memcmp(zUri, "file:", 5)==0
109758   ){
109759     char *zOpt;
109760     int eState;                   /* Parser state when parsing URI */
109761     int iIn;                      /* Input character index */
109762     int iOut = 0;                 /* Output character index */
109763     int nByte = nUri+2;           /* Bytes of space to allocate */
109764 
109765     /* Make sure the SQLITE_OPEN_URI flag is set to indicate to the VFS xOpen
109766     ** method that there may be extra parameters following the file-name.  */
109767     flags |= SQLITE_OPEN_URI;
109768 
109769     for(iIn=0; iIn<nUri; iIn++) nByte += (zUri[iIn]=='&');
109770     zFile = sqlite3_malloc(nByte);
109771     if( !zFile ) return SQLITE_NOMEM;
109772 
109773     /* Discard the scheme and authority segments of the URI. */
109774     if( zUri[5]=='/' && zUri[6]=='/' ){
109775       iIn = 7;
109776       while( zUri[iIn] && zUri[iIn]!='/' ) iIn++;
109777 
109778       if( iIn!=7 && (iIn!=16 || memcmp("localhost", &zUri[7], 9)) ){
109779         *pzErrMsg = sqlite3_mprintf("invalid uri authority: %.*s",
109780             iIn-7, &zUri[7]);
109781         rc = SQLITE_ERROR;
109782         goto parse_uri_out;
109783       }
109784     }else{
109785       iIn = 5;
109786     }
109787 
109788     /* Copy the filename and any query parameters into the zFile buffer.
109789     ** Decode %HH escape codes along the way.
109790     **
109791     ** Within this loop, variable eState may be set to 0, 1 or 2, depending
109792     ** on the parsing context. As follows:
109793     **
109794     **   0: Parsing file-name.
109795     **   1: Parsing name section of a name=value query parameter.
109796     **   2: Parsing value section of a name=value query parameter.
109797     */
109798     eState = 0;
109799     while( (c = zUri[iIn])!=0 && c!='#' ){
109800       iIn++;
109801       if( c=='%'
109802        && sqlite3Isxdigit(zUri[iIn])
109803        && sqlite3Isxdigit(zUri[iIn+1])
109804       ){
109805         int octet = (sqlite3HexToInt(zUri[iIn++]) << 4);
109806         octet += sqlite3HexToInt(zUri[iIn++]);
109807 
109808         assert( octet>=0 && octet<256 );
109809         if( octet==0 ){
109810           /* This branch is taken when "%00" appears within the URI. In this
109811           ** case we ignore all text in the remainder of the path, name or
109812           ** value currently being parsed. So ignore the current character
109813           ** and skip to the next "?", "=" or "&", as appropriate. */
109814           while( (c = zUri[iIn])!=0 && c!='#'
109815               && (eState!=0 || c!='?')
109816               && (eState!=1 || (c!='=' && c!='&'))
109817               && (eState!=2 || c!='&')
109818           ){
109819             iIn++;
109820           }
109821           continue;
109822         }
109823         c = octet;
109824       }else if( eState==1 && (c=='&' || c=='=') ){
109825         if( zFile[iOut-1]==0 ){
109826           /* An empty option name. Ignore this option altogether. */
109827           while( zUri[iIn] && zUri[iIn]!='#' && zUri[iIn-1]!='&' ) iIn++;
109828           continue;
109829         }
109830         if( c=='&' ){
109831           zFile[iOut++] = '\0';
109832         }else{
109833           eState = 2;
109834         }
109835         c = 0;
109836       }else if( (eState==0 && c=='?') || (eState==2 && c=='&') ){
109837         c = 0;
109838         eState = 1;
109839       }
109840       zFile[iOut++] = c;
109841     }
109842     if( eState==1 ) zFile[iOut++] = '\0';
109843     zFile[iOut++] = '\0';
109844     zFile[iOut++] = '\0';
109845 
109846     /* Check if there were any options specified that should be interpreted
109847     ** here. Options that are interpreted here include "vfs" and those that
109848     ** correspond to flags that may be passed to the sqlite3_open_v2()
109849     ** method. */
109850     zOpt = &zFile[sqlite3Strlen30(zFile)+1];
109851     while( zOpt[0] ){
109852       int nOpt = sqlite3Strlen30(zOpt);
109853       char *zVal = &zOpt[nOpt+1];
109854       int nVal = sqlite3Strlen30(zVal);
109855 
109856       if( nOpt==3 && memcmp("vfs", zOpt, 3)==0 ){
109857         zVfs = zVal;
109858       }else{
109859         struct OpenMode {
109860           const char *z;
109861           int mode;
109862         } *aMode = 0;
109863         char *zModeType = 0;
109864         int mask = 0;
109865         int limit = 0;
109866 
109867         if( nOpt==5 && memcmp("cache", zOpt, 5)==0 ){
109868           static struct OpenMode aCacheMode[] = {
109869             { "shared",  SQLITE_OPEN_SHAREDCACHE },
109870             { "private", SQLITE_OPEN_PRIVATECACHE },
109871             { 0, 0 }
109872           };
109873 
109874           mask = SQLITE_OPEN_SHAREDCACHE|SQLITE_OPEN_PRIVATECACHE;
109875           aMode = aCacheMode;
109876           limit = mask;
109877           zModeType = "cache";
109878         }
109879         if( nOpt==4 && memcmp("mode", zOpt, 4)==0 ){
109880           static struct OpenMode aOpenMode[] = {
109881             { "ro",  SQLITE_OPEN_READONLY },
109882             { "rw",  SQLITE_OPEN_READWRITE },
109883             { "rwc", SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE },
109884             { 0, 0 }
109885           };
109886 
109887           mask = SQLITE_OPEN_READONLY|SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
109888           aMode = aOpenMode;
109889           limit = mask & flags;
109890           zModeType = "access";
109891         }
109892 
109893         if( aMode ){
109894           int i;
109895           int mode = 0;
109896           for(i=0; aMode[i].z; i++){
109897             const char *z = aMode[i].z;
109898             if( nVal==sqlite3Strlen30(z) && 0==memcmp(zVal, z, nVal) ){
109899               mode = aMode[i].mode;
109900               break;
109901             }
109902           }
109903           if( mode==0 ){
109904             *pzErrMsg = sqlite3_mprintf("no such %s mode: %s", zModeType, zVal);
109905             rc = SQLITE_ERROR;
109906             goto parse_uri_out;
109907           }
109908           if( mode>limit ){
109909             *pzErrMsg = sqlite3_mprintf("%s mode not allowed: %s",
109910                                         zModeType, zVal);
109911             rc = SQLITE_PERM;
109912             goto parse_uri_out;
109913           }
109914           flags = (flags & ~mask) | mode;
109915         }
109916       }
109917 
109918       zOpt = &zVal[nVal+1];
109919     }
109920 
109921   }else{
109922     zFile = sqlite3_malloc(nUri+2);
109923     if( !zFile ) return SQLITE_NOMEM;
109924     memcpy(zFile, zUri, nUri);
109925     zFile[nUri] = '\0';
109926     zFile[nUri+1] = '\0';
109927   }
109928 
109929   *ppVfs = sqlite3_vfs_find(zVfs);
109930   if( *ppVfs==0 ){
109931     *pzErrMsg = sqlite3_mprintf("no such vfs: %s", zVfs);
109932     rc = SQLITE_ERROR;
109933   }
109934  parse_uri_out:
109935   if( rc!=SQLITE_OK ){
109936     sqlite3_free(zFile);
109937     zFile = 0;
109938   }
109939   *pFlags = flags;
109940   *pzFile = zFile;
109941   return rc;
109942 }
109943 
109944 
109945 /*
109946 ** This routine does the work of opening a database on behalf of
109947 ** sqlite3_open() and sqlite3_open16(). The database filename "zFilename"
109948 ** is UTF-8 encoded.
109949 */
109950 static int openDatabase(
109951   const char *zFilename, /* Database filename UTF-8 encoded */
109952   sqlite3 **ppDb,        /* OUT: Returned database handle */
109953   unsigned int flags,    /* Operational flags */
109954   const char *zVfs       /* Name of the VFS to use */
109955 ){
109956   sqlite3 *db;                    /* Store allocated handle here */
109957   int rc;                         /* Return code */
109958   int isThreadsafe;               /* True for threadsafe connections */
109959   char *zOpen = 0;                /* Filename argument to pass to BtreeOpen() */
109960   char *zErrMsg = 0;              /* Error message from sqlite3ParseUri() */
109961 
109962   *ppDb = 0;
109963 #ifndef SQLITE_OMIT_AUTOINIT
109964   rc = sqlite3_initialize();
109965   if( rc ) return rc;
109966 #endif
109967 
109968   /* Only allow sensible combinations of bits in the flags argument.
109969   ** Throw an error if any non-sense combination is used.  If we
109970   ** do not block illegal combinations here, it could trigger
109971   ** assert() statements in deeper layers.  Sensible combinations
109972   ** are:
109973   **
109974   **  1:  SQLITE_OPEN_READONLY
109975   **  2:  SQLITE_OPEN_READWRITE
109976   **  6:  SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE
109977   */
109978   assert( SQLITE_OPEN_READONLY  == 0x01 );
109979   assert( SQLITE_OPEN_READWRITE == 0x02 );
109980   assert( SQLITE_OPEN_CREATE    == 0x04 );
109981   testcase( (1<<(flags&7))==0x02 ); /* READONLY */
109982   testcase( (1<<(flags&7))==0x04 ); /* READWRITE */
109983   testcase( (1<<(flags&7))==0x40 ); /* READWRITE | CREATE */
109984   if( ((1<<(flags&7)) & 0x46)==0 ) return SQLITE_MISUSE_BKPT;
109985 
109986   if( sqlite3GlobalConfig.bCoreMutex==0 ){
109987     isThreadsafe = 0;
109988   }else if( flags & SQLITE_OPEN_NOMUTEX ){
109989     isThreadsafe = 0;
109990   }else if( flags & SQLITE_OPEN_FULLMUTEX ){
109991     isThreadsafe = 1;
109992   }else{
109993     isThreadsafe = sqlite3GlobalConfig.bFullMutex;
109994   }
109995   if( flags & SQLITE_OPEN_PRIVATECACHE ){
109996     flags &= ~SQLITE_OPEN_SHAREDCACHE;
109997   }else if( sqlite3GlobalConfig.sharedCacheEnabled ){
109998     flags |= SQLITE_OPEN_SHAREDCACHE;
109999   }
110000 
110001   /* Remove harmful bits from the flags parameter
110002   **
110003   ** The SQLITE_OPEN_NOMUTEX and SQLITE_OPEN_FULLMUTEX flags were
110004   ** dealt with in the previous code block.  Besides these, the only
110005   ** valid input flags for sqlite3_open_v2() are SQLITE_OPEN_READONLY,
110006   ** SQLITE_OPEN_READWRITE, SQLITE_OPEN_CREATE, SQLITE_OPEN_SHAREDCACHE,
110007   ** SQLITE_OPEN_PRIVATECACHE, and some reserved bits.  Silently mask
110008   ** off all other flags.
110009   */
110010   flags &=  ~( SQLITE_OPEN_DELETEONCLOSE |
110011                SQLITE_OPEN_EXCLUSIVE |
110012                SQLITE_OPEN_MAIN_DB |
110013                SQLITE_OPEN_TEMP_DB |
110014                SQLITE_OPEN_TRANSIENT_DB |
110015                SQLITE_OPEN_MAIN_JOURNAL |
110016                SQLITE_OPEN_TEMP_JOURNAL |
110017                SQLITE_OPEN_SUBJOURNAL |
110018                SQLITE_OPEN_MASTER_JOURNAL |
110019                SQLITE_OPEN_NOMUTEX |
110020                SQLITE_OPEN_FULLMUTEX |
110021                SQLITE_OPEN_WAL
110022              );
110023 
110024   /* Allocate the sqlite data structure */
110025   db = sqlite3MallocZero( sizeof(sqlite3) );
110026   if( db==0 ) goto opendb_out;
110027   if( isThreadsafe ){
110028     db->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE);
110029     if( db->mutex==0 ){
110030       sqlite3_free(db);
110031       db = 0;
110032       goto opendb_out;
110033     }
110034   }
110035   sqlite3_mutex_enter(db->mutex);
110036   db->errMask = 0xff;
110037   db->nDb = 2;
110038   db->magic = SQLITE_MAGIC_BUSY;
110039   db->aDb = db->aDbStatic;
110040 
110041   assert( sizeof(db->aLimit)==sizeof(aHardLimit) );
110042   memcpy(db->aLimit, aHardLimit, sizeof(db->aLimit));
110043   db->autoCommit = 1;
110044   db->nextAutovac = -1;
110045   db->nextPagesize = 0;
110046   db->flags |= SQLITE_ShortColNames | SQLITE_AutoIndex | SQLITE_EnableTrigger
110047 #if SQLITE_DEFAULT_FILE_FORMAT<4
110048                  | SQLITE_LegacyFileFmt
110049 #endif
110050 #ifdef SQLITE_ENABLE_LOAD_EXTENSION
110051                  | SQLITE_LoadExtension
110052 #endif
110053 #if SQLITE_DEFAULT_RECURSIVE_TRIGGERS
110054                  | SQLITE_RecTriggers
110055 #endif
110056 #if defined(SQLITE_DEFAULT_FOREIGN_KEYS) && SQLITE_DEFAULT_FOREIGN_KEYS
110057                  | SQLITE_ForeignKeys
110058 #endif
110059       ;
110060   sqlite3HashInit(&db->aCollSeq);
110061 #ifndef SQLITE_OMIT_VIRTUALTABLE
110062   sqlite3HashInit(&db->aModule);
110063 #endif
110064 
110065   /* Add the default collation sequence BINARY. BINARY works for both UTF-8
110066   ** and UTF-16, so add a version for each to avoid any unnecessary
110067   ** conversions. The only error that can occur here is a malloc() failure.
110068   */
110069   createCollation(db, "BINARY", SQLITE_UTF8, SQLITE_COLL_BINARY, 0,
110070                   binCollFunc, 0);
110071   createCollation(db, "BINARY", SQLITE_UTF16BE, SQLITE_COLL_BINARY, 0,
110072                   binCollFunc, 0);
110073   createCollation(db, "BINARY", SQLITE_UTF16LE, SQLITE_COLL_BINARY, 0,
110074                   binCollFunc, 0);
110075   createCollation(db, "RTRIM", SQLITE_UTF8, SQLITE_COLL_USER, (void*)1,
110076                   binCollFunc, 0);
110077   if( db->mallocFailed ){
110078     goto opendb_out;
110079   }
110080   db->pDfltColl = sqlite3FindCollSeq(db, SQLITE_UTF8, "BINARY", 0);
110081   assert( db->pDfltColl!=0 );
110082 
110083   /* Also add a UTF-8 case-insensitive collation sequence. */
110084   createCollation(db, "NOCASE", SQLITE_UTF8, SQLITE_COLL_NOCASE, 0,
110085                   nocaseCollatingFunc, 0);
110086 
110087   /* Parse the filename/URI argument. */
110088   db->openFlags = flags;
110089   rc = sqlite3ParseUri(zVfs, zFilename, &flags, &db->pVfs, &zOpen, &zErrMsg);
110090   if( rc!=SQLITE_OK ){
110091     if( rc==SQLITE_NOMEM ) db->mallocFailed = 1;
110092     sqlite3Error(db, rc, zErrMsg ? "%s" : 0, zErrMsg);
110093     sqlite3_free(zErrMsg);
110094     goto opendb_out;
110095   }
110096 
110097   /* Open the backend database driver */
110098   rc = sqlite3BtreeOpen(db->pVfs, zOpen, db, &db->aDb[0].pBt, 0,
110099                         flags | SQLITE_OPEN_MAIN_DB);
110100   if( rc!=SQLITE_OK ){
110101     if( rc==SQLITE_IOERR_NOMEM ){
110102       rc = SQLITE_NOMEM;
110103     }
110104     sqlite3Error(db, rc, 0);
110105     goto opendb_out;
110106   }
110107   db->aDb[0].pSchema = sqlite3SchemaGet(db, db->aDb[0].pBt);
110108   db->aDb[1].pSchema = sqlite3SchemaGet(db, 0);
110109 
110110 
110111   /* The default safety_level for the main database is 'full'; for the temp
110112   ** database it is 'NONE'. This matches the pager layer defaults.
110113   */
110114   db->aDb[0].zName = "main";
110115   db->aDb[0].safety_level = 3;
110116   db->aDb[1].zName = "temp";
110117   db->aDb[1].safety_level = 1;
110118 
110119   db->magic = SQLITE_MAGIC_OPEN;
110120   if( db->mallocFailed ){
110121     goto opendb_out;
110122   }
110123 
110124   /* Register all built-in functions, but do not attempt to read the
110125   ** database schema yet. This is delayed until the first time the database
110126   ** is accessed.
110127   */
110128   sqlite3Error(db, SQLITE_OK, 0);
110129   sqlite3RegisterBuiltinFunctions(db);
110130 
110131   /* Load automatic extensions - extensions that have been registered
110132   ** using the sqlite3_automatic_extension() API.
110133   */
110134   sqlite3AutoLoadExtensions(db);
110135   rc = sqlite3_errcode(db);
110136   if( rc!=SQLITE_OK ){
110137     goto opendb_out;
110138   }
110139 
110140 #ifdef SQLITE_ENABLE_FTS1
110141   if( !db->mallocFailed ){
110142     extern int sqlite3Fts1Init(sqlite3*);
110143     rc = sqlite3Fts1Init(db);
110144   }
110145 #endif
110146 
110147 #ifdef SQLITE_ENABLE_FTS2
110148   if( !db->mallocFailed && rc==SQLITE_OK ){
110149     extern int sqlite3Fts2Init(sqlite3*);
110150     rc = sqlite3Fts2Init(db);
110151   }
110152 #endif
110153 
110154 #ifdef SQLITE_ENABLE_FTS3
110155   if( !db->mallocFailed && rc==SQLITE_OK ){
110156     rc = sqlite3Fts3Init(db);
110157   }
110158 #endif
110159 
110160 #ifdef SQLITE_ENABLE_ICU
110161   if( !db->mallocFailed && rc==SQLITE_OK ){
110162     rc = sqlite3IcuInit(db);
110163   }
110164 #endif
110165 
110166 #ifdef SQLITE_ENABLE_RTREE
110167   if( !db->mallocFailed && rc==SQLITE_OK){
110168     rc = sqlite3RtreeInit(db);
110169   }
110170 #endif
110171 
110172   sqlite3Error(db, rc, 0);
110173 
110174   /* -DSQLITE_DEFAULT_LOCKING_MODE=1 makes EXCLUSIVE the default locking
110175   ** mode.  -DSQLITE_DEFAULT_LOCKING_MODE=0 make NORMAL the default locking
110176   ** mode.  Doing nothing at all also makes NORMAL the default.
110177   */
110178 #ifdef SQLITE_DEFAULT_LOCKING_MODE
110179   db->dfltLockMode = SQLITE_DEFAULT_LOCKING_MODE;
110180   sqlite3PagerLockingMode(sqlite3BtreePager(db->aDb[0].pBt),
110181                           SQLITE_DEFAULT_LOCKING_MODE);
110182 #endif
110183 
110184   /* Enable the lookaside-malloc subsystem */
110185   setupLookaside(db, 0, sqlite3GlobalConfig.szLookaside,
110186                         sqlite3GlobalConfig.nLookaside);
110187 
110188   sqlite3_wal_autocheckpoint(db, SQLITE_DEFAULT_WAL_AUTOCHECKPOINT);
110189 
110190 opendb_out:
110191   sqlite3_free(zOpen);
110192   if( db ){
110193     assert( db->mutex!=0 || isThreadsafe==0 || sqlite3GlobalConfig.bFullMutex==0 );
110194     sqlite3_mutex_leave(db->mutex);
110195   }
110196   rc = sqlite3_errcode(db);
110197   if( rc==SQLITE_NOMEM ){
110198     sqlite3_close(db);
110199     db = 0;
110200   }else if( rc!=SQLITE_OK ){
110201     db->magic = SQLITE_MAGIC_SICK;
110202   }
110203   *ppDb = db;
110204   return sqlite3ApiExit(0, rc);
110205 }
110206 
110207 /*
110208 ** Open a new database handle.
110209 */
110210 SQLITE_API int sqlite3_open(
110211   const char *zFilename,
110212   sqlite3 **ppDb
110213 ){
110214   return openDatabase(zFilename, ppDb,
110215                       SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0);
110216 }
110217 SQLITE_API int sqlite3_open_v2(
110218   const char *filename,   /* Database filename (UTF-8) */
110219   sqlite3 **ppDb,         /* OUT: SQLite db handle */
110220   int flags,              /* Flags */
110221   const char *zVfs        /* Name of VFS module to use */
110222 ){
110223   return openDatabase(filename, ppDb, (unsigned int)flags, zVfs);
110224 }
110225 
110226 #ifndef SQLITE_OMIT_UTF16
110227 /*
110228 ** Open a new database handle.
110229 */
110230 SQLITE_API int sqlite3_open16(
110231   const void *zFilename,
110232   sqlite3 **ppDb
110233 ){
110234   char const *zFilename8;   /* zFilename encoded in UTF-8 instead of UTF-16 */
110235   sqlite3_value *pVal;
110236   int rc;
110237 
110238   assert( zFilename );
110239   assert( ppDb );
110240   *ppDb = 0;
110241 #ifndef SQLITE_OMIT_AUTOINIT
110242   rc = sqlite3_initialize();
110243   if( rc ) return rc;
110244 #endif
110245   pVal = sqlite3ValueNew(0);
110246   sqlite3ValueSetStr(pVal, -1, zFilename, SQLITE_UTF16NATIVE, SQLITE_STATIC);
110247   zFilename8 = sqlite3ValueText(pVal, SQLITE_UTF8);
110248   if( zFilename8 ){
110249     rc = openDatabase(zFilename8, ppDb,
110250                       SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0);
110251     assert( *ppDb || rc==SQLITE_NOMEM );
110252     if( rc==SQLITE_OK && !DbHasProperty(*ppDb, 0, DB_SchemaLoaded) ){
110253       ENC(*ppDb) = SQLITE_UTF16NATIVE;
110254     }
110255   }else{
110256     rc = SQLITE_NOMEM;
110257   }
110258   sqlite3ValueFree(pVal);
110259 
110260   return sqlite3ApiExit(0, rc);
110261 }
110262 #endif /* SQLITE_OMIT_UTF16 */
110263 
110264 /*
110265 ** Register a new collation sequence with the database handle db.
110266 */
110267 SQLITE_API int sqlite3_create_collation(
110268   sqlite3* db,
110269   const char *zName,
110270   int enc,
110271   void* pCtx,
110272   int(*xCompare)(void*,int,const void*,int,const void*)
110273 ){
110274   int rc;
110275   sqlite3_mutex_enter(db->mutex);
110276   assert( !db->mallocFailed );
110277   rc = createCollation(db, zName, (u8)enc, SQLITE_COLL_USER, pCtx, xCompare, 0);
110278   rc = sqlite3ApiExit(db, rc);
110279   sqlite3_mutex_leave(db->mutex);
110280   return rc;
110281 }
110282 
110283 /*
110284 ** Register a new collation sequence with the database handle db.
110285 */
110286 SQLITE_API int sqlite3_create_collation_v2(
110287   sqlite3* db,
110288   const char *zName,
110289   int enc,
110290   void* pCtx,
110291   int(*xCompare)(void*,int,const void*,int,const void*),
110292   void(*xDel)(void*)
110293 ){
110294   int rc;
110295   sqlite3_mutex_enter(db->mutex);
110296   assert( !db->mallocFailed );
110297   rc = createCollation(db, zName, (u8)enc, SQLITE_COLL_USER, pCtx, xCompare, xDel);
110298   rc = sqlite3ApiExit(db, rc);
110299   sqlite3_mutex_leave(db->mutex);
110300   return rc;
110301 }
110302 
110303 #ifndef SQLITE_OMIT_UTF16
110304 /*
110305 ** Register a new collation sequence with the database handle db.
110306 */
110307 SQLITE_API int sqlite3_create_collation16(
110308   sqlite3* db,
110309   const void *zName,
110310   int enc,
110311   void* pCtx,
110312   int(*xCompare)(void*,int,const void*,int,const void*)
110313 ){
110314   int rc = SQLITE_OK;
110315   char *zName8;
110316   sqlite3_mutex_enter(db->mutex);
110317   assert( !db->mallocFailed );
110318   zName8 = sqlite3Utf16to8(db, zName, -1, SQLITE_UTF16NATIVE);
110319   if( zName8 ){
110320     rc = createCollation(db, zName8, (u8)enc, SQLITE_COLL_USER, pCtx, xCompare, 0);
110321     sqlite3DbFree(db, zName8);
110322   }
110323   rc = sqlite3ApiExit(db, rc);
110324   sqlite3_mutex_leave(db->mutex);
110325   return rc;
110326 }
110327 #endif /* SQLITE_OMIT_UTF16 */
110328 
110329 /*
110330 ** Register a collation sequence factory callback with the database handle
110331 ** db. Replace any previously installed collation sequence factory.
110332 */
110333 SQLITE_API int sqlite3_collation_needed(
110334   sqlite3 *db,
110335   void *pCollNeededArg,
110336   void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*)
110337 ){
110338   sqlite3_mutex_enter(db->mutex);
110339   db->xCollNeeded = xCollNeeded;
110340   db->xCollNeeded16 = 0;
110341   db->pCollNeededArg = pCollNeededArg;
110342   sqlite3_mutex_leave(db->mutex);
110343   return SQLITE_OK;
110344 }
110345 
110346 #ifndef SQLITE_OMIT_UTF16
110347 /*
110348 ** Register a collation sequence factory callback with the database handle
110349 ** db. Replace any previously installed collation sequence factory.
110350 */
110351 SQLITE_API int sqlite3_collation_needed16(
110352   sqlite3 *db,
110353   void *pCollNeededArg,
110354   void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*)
110355 ){
110356   sqlite3_mutex_enter(db->mutex);
110357   db->xCollNeeded = 0;
110358   db->xCollNeeded16 = xCollNeeded16;
110359   db->pCollNeededArg = pCollNeededArg;
110360   sqlite3_mutex_leave(db->mutex);
110361   return SQLITE_OK;
110362 }
110363 #endif /* SQLITE_OMIT_UTF16 */
110364 
110365 #ifndef SQLITE_OMIT_DEPRECATED
110366 /*
110367 ** This function is now an anachronism. It used to be used to recover from a
110368 ** malloc() failure, but SQLite now does this automatically.
110369 */
110370 SQLITE_API int sqlite3_global_recover(void){
110371   return SQLITE_OK;
110372 }
110373 #endif
110374 
110375 /*
110376 ** Test to see whether or not the database connection is in autocommit
110377 ** mode.  Return TRUE if it is and FALSE if not.  Autocommit mode is on
110378 ** by default.  Autocommit is disabled by a BEGIN statement and reenabled
110379 ** by the next COMMIT or ROLLBACK.
110380 **
110381 ******* THIS IS AN EXPERIMENTAL API AND IS SUBJECT TO CHANGE ******
110382 */
110383 SQLITE_API int sqlite3_get_autocommit(sqlite3 *db){
110384   return db->autoCommit;
110385 }
110386 
110387 /*
110388 ** The following routines are subtitutes for constants SQLITE_CORRUPT,
110389 ** SQLITE_MISUSE, SQLITE_CANTOPEN, SQLITE_IOERR and possibly other error
110390 ** constants.  They server two purposes:
110391 **
110392 **   1.  Serve as a convenient place to set a breakpoint in a debugger
110393 **       to detect when version error conditions occurs.
110394 **
110395 **   2.  Invoke sqlite3_log() to provide the source code location where
110396 **       a low-level error is first detected.
110397 */
110398 SQLITE_PRIVATE int sqlite3CorruptError(int lineno){
110399   testcase( sqlite3GlobalConfig.xLog!=0 );
110400   sqlite3_log(SQLITE_CORRUPT,
110401               "database corruption at line %d of [%.10s]",
110402               lineno, 20+sqlite3_sourceid());
110403   return SQLITE_CORRUPT;
110404 }
110405 SQLITE_PRIVATE int sqlite3MisuseError(int lineno){
110406   testcase( sqlite3GlobalConfig.xLog!=0 );
110407   sqlite3_log(SQLITE_MISUSE,
110408               "misuse at line %d of [%.10s]",
110409               lineno, 20+sqlite3_sourceid());
110410   return SQLITE_MISUSE;
110411 }
110412 SQLITE_PRIVATE int sqlite3CantopenError(int lineno){
110413   testcase( sqlite3GlobalConfig.xLog!=0 );
110414   sqlite3_log(SQLITE_CANTOPEN,
110415               "cannot open file at line %d of [%.10s]",
110416               lineno, 20+sqlite3_sourceid());
110417   return SQLITE_CANTOPEN;
110418 }
110419 
110420 
110421 #ifndef SQLITE_OMIT_DEPRECATED
110422 /*
110423 ** This is a convenience routine that makes sure that all thread-specific
110424 ** data for this thread has been deallocated.
110425 **
110426 ** SQLite no longer uses thread-specific data so this routine is now a
110427 ** no-op.  It is retained for historical compatibility.
110428 */
110429 SQLITE_API void sqlite3_thread_cleanup(void){
110430 }
110431 #endif
110432 
110433 /*
110434 ** Return meta information about a specific column of a database table.
110435 ** See comment in sqlite3.h (sqlite.h.in) for details.
110436 */
110437 #ifdef SQLITE_ENABLE_COLUMN_METADATA
110438 SQLITE_API int sqlite3_table_column_metadata(
110439   sqlite3 *db,                /* Connection handle */
110440   const char *zDbName,        /* Database name or NULL */
110441   const char *zTableName,     /* Table name */
110442   const char *zColumnName,    /* Column name */
110443   char const **pzDataType,    /* OUTPUT: Declared data type */
110444   char const **pzCollSeq,     /* OUTPUT: Collation sequence name */
110445   int *pNotNull,              /* OUTPUT: True if NOT NULL constraint exists */
110446   int *pPrimaryKey,           /* OUTPUT: True if column part of PK */
110447   int *pAutoinc               /* OUTPUT: True if column is auto-increment */
110448 ){
110449   int rc;
110450   char *zErrMsg = 0;
110451   Table *pTab = 0;
110452   Column *pCol = 0;
110453   int iCol;
110454 
110455   char const *zDataType = 0;
110456   char const *zCollSeq = 0;
110457   int notnull = 0;
110458   int primarykey = 0;
110459   int autoinc = 0;
110460 
110461   /* Ensure the database schema has been loaded */
110462   sqlite3_mutex_enter(db->mutex);
110463   sqlite3BtreeEnterAll(db);
110464   rc = sqlite3Init(db, &zErrMsg);
110465   if( SQLITE_OK!=rc ){
110466     goto error_out;
110467   }
110468 
110469   /* Locate the table in question */
110470   pTab = sqlite3FindTable(db, zTableName, zDbName);
110471   if( !pTab || pTab->pSelect ){
110472     pTab = 0;
110473     goto error_out;
110474   }
110475 
110476   /* Find the column for which info is requested */
110477   if( sqlite3IsRowid(zColumnName) ){
110478     iCol = pTab->iPKey;
110479     if( iCol>=0 ){
110480       pCol = &pTab->aCol[iCol];
110481     }
110482   }else{
110483     for(iCol=0; iCol<pTab->nCol; iCol++){
110484       pCol = &pTab->aCol[iCol];
110485       if( 0==sqlite3StrICmp(pCol->zName, zColumnName) ){
110486         break;
110487       }
110488     }
110489     if( iCol==pTab->nCol ){
110490       pTab = 0;
110491       goto error_out;
110492     }
110493   }
110494 
110495   /* The following block stores the meta information that will be returned
110496   ** to the caller in local variables zDataType, zCollSeq, notnull, primarykey
110497   ** and autoinc. At this point there are two possibilities:
110498   **
110499   **     1. The specified column name was rowid", "oid" or "_rowid_"
110500   **        and there is no explicitly declared IPK column.
110501   **
110502   **     2. The table is not a view and the column name identified an
110503   **        explicitly declared column. Copy meta information from *pCol.
110504   */
110505   if( pCol ){
110506     zDataType = pCol->zType;
110507     zCollSeq = pCol->zColl;
110508     notnull = pCol->notNull!=0;
110509     primarykey  = pCol->isPrimKey!=0;
110510     autoinc = pTab->iPKey==iCol && (pTab->tabFlags & TF_Autoincrement)!=0;
110511   }else{
110512     zDataType = "INTEGER";
110513     primarykey = 1;
110514   }
110515   if( !zCollSeq ){
110516     zCollSeq = "BINARY";
110517   }
110518 
110519 error_out:
110520   sqlite3BtreeLeaveAll(db);
110521 
110522   /* Whether the function call succeeded or failed, set the output parameters
110523   ** to whatever their local counterparts contain. If an error did occur,
110524   ** this has the effect of zeroing all output parameters.
110525   */
110526   if( pzDataType ) *pzDataType = zDataType;
110527   if( pzCollSeq ) *pzCollSeq = zCollSeq;
110528   if( pNotNull ) *pNotNull = notnull;
110529   if( pPrimaryKey ) *pPrimaryKey = primarykey;
110530   if( pAutoinc ) *pAutoinc = autoinc;
110531 
110532   if( SQLITE_OK==rc && !pTab ){
110533     sqlite3DbFree(db, zErrMsg);
110534     zErrMsg = sqlite3MPrintf(db, "no such table column: %s.%s", zTableName,
110535         zColumnName);
110536     rc = SQLITE_ERROR;
110537   }
110538   sqlite3Error(db, rc, (zErrMsg?"%s":0), zErrMsg);
110539   sqlite3DbFree(db, zErrMsg);
110540   rc = sqlite3ApiExit(db, rc);
110541   sqlite3_mutex_leave(db->mutex);
110542   return rc;
110543 }
110544 #endif
110545 
110546 /*
110547 ** Sleep for a little while.  Return the amount of time slept.
110548 */
110549 SQLITE_API int sqlite3_sleep(int ms){
110550   sqlite3_vfs *pVfs;
110551   int rc;
110552   pVfs = sqlite3_vfs_find(0);
110553   if( pVfs==0 ) return 0;
110554 
110555   /* This function works in milliseconds, but the underlying OsSleep()
110556   ** API uses microseconds. Hence the 1000's.
110557   */
110558   rc = (sqlite3OsSleep(pVfs, 1000*ms)/1000);
110559   return rc;
110560 }
110561 
110562 /*
110563 ** Enable or disable the extended result codes.
110564 */
110565 SQLITE_API int sqlite3_extended_result_codes(sqlite3 *db, int onoff){
110566   sqlite3_mutex_enter(db->mutex);
110567   db->errMask = onoff ? 0xffffffff : 0xff;
110568   sqlite3_mutex_leave(db->mutex);
110569   return SQLITE_OK;
110570 }
110571 
110572 /*
110573 ** Invoke the xFileControl method on a particular database.
110574 */
110575 SQLITE_API int sqlite3_file_control(sqlite3 *db, const char *zDbName, int op, void *pArg){
110576   int rc = SQLITE_ERROR;
110577   int iDb;
110578   sqlite3_mutex_enter(db->mutex);
110579   if( zDbName==0 ){
110580     iDb = 0;
110581   }else{
110582     for(iDb=0; iDb<db->nDb; iDb++){
110583       if( strcmp(db->aDb[iDb].zName, zDbName)==0 ) break;
110584     }
110585   }
110586   if( iDb<db->nDb ){
110587     Btree *pBtree = db->aDb[iDb].pBt;
110588     if( pBtree ){
110589       Pager *pPager;
110590       sqlite3_file *fd;
110591       sqlite3BtreeEnter(pBtree);
110592       pPager = sqlite3BtreePager(pBtree);
110593       assert( pPager!=0 );
110594       fd = sqlite3PagerFile(pPager);
110595       assert( fd!=0 );
110596       if( op==SQLITE_FCNTL_FILE_POINTER ){
110597         *(sqlite3_file**)pArg = fd;
110598         rc = SQLITE_OK;
110599       }else if( fd->pMethods ){
110600         rc = sqlite3OsFileControl(fd, op, pArg);
110601       }else{
110602         rc = SQLITE_NOTFOUND;
110603       }
110604       sqlite3BtreeLeave(pBtree);
110605     }
110606   }
110607   sqlite3_mutex_leave(db->mutex);
110608   return rc;
110609 }
110610 
110611 /*
110612 ** Interface to the testing logic.
110613 */
110614 SQLITE_API int sqlite3_test_control(int op, ...){
110615   int rc = 0;
110616 #ifndef SQLITE_OMIT_BUILTIN_TEST
110617   va_list ap;
110618   va_start(ap, op);
110619   switch( op ){
110620 
110621     /*
110622     ** Save the current state of the PRNG.
110623     */
110624     case SQLITE_TESTCTRL_PRNG_SAVE: {
110625       sqlite3PrngSaveState();
110626       break;
110627     }
110628 
110629     /*
110630     ** Restore the state of the PRNG to the last state saved using
110631     ** PRNG_SAVE.  If PRNG_SAVE has never before been called, then
110632     ** this verb acts like PRNG_RESET.
110633     */
110634     case SQLITE_TESTCTRL_PRNG_RESTORE: {
110635       sqlite3PrngRestoreState();
110636       break;
110637     }
110638 
110639     /*
110640     ** Reset the PRNG back to its uninitialized state.  The next call
110641     ** to sqlite3_randomness() will reseed the PRNG using a single call
110642     ** to the xRandomness method of the default VFS.
110643     */
110644     case SQLITE_TESTCTRL_PRNG_RESET: {
110645       sqlite3PrngResetState();
110646       break;
110647     }
110648 
110649     /*
110650     **  sqlite3_test_control(BITVEC_TEST, size, program)
110651     **
110652     ** Run a test against a Bitvec object of size.  The program argument
110653     ** is an array of integers that defines the test.  Return -1 on a
110654     ** memory allocation error, 0 on success, or non-zero for an error.
110655     ** See the sqlite3BitvecBuiltinTest() for additional information.
110656     */
110657     case SQLITE_TESTCTRL_BITVEC_TEST: {
110658       int sz = va_arg(ap, int);
110659       int *aProg = va_arg(ap, int*);
110660       rc = sqlite3BitvecBuiltinTest(sz, aProg);
110661       break;
110662     }
110663 
110664     /*
110665     **  sqlite3_test_control(BENIGN_MALLOC_HOOKS, xBegin, xEnd)
110666     **
110667     ** Register hooks to call to indicate which malloc() failures
110668     ** are benign.
110669     */
110670     case SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS: {
110671       typedef void (*void_function)(void);
110672       void_function xBenignBegin;
110673       void_function xBenignEnd;
110674       xBenignBegin = va_arg(ap, void_function);
110675       xBenignEnd = va_arg(ap, void_function);
110676       sqlite3BenignMallocHooks(xBenignBegin, xBenignEnd);
110677       break;
110678     }
110679 
110680     /*
110681     **  sqlite3_test_control(SQLITE_TESTCTRL_PENDING_BYTE, unsigned int X)
110682     **
110683     ** Set the PENDING byte to the value in the argument, if X>0.
110684     ** Make no changes if X==0.  Return the value of the pending byte
110685     ** as it existing before this routine was called.
110686     **
110687     ** IMPORTANT:  Changing the PENDING byte from 0x40000000 results in
110688     ** an incompatible database file format.  Changing the PENDING byte
110689     ** while any database connection is open results in undefined and
110690     ** dileterious behavior.
110691     */
110692     case SQLITE_TESTCTRL_PENDING_BYTE: {
110693       rc = PENDING_BYTE;
110694 #ifndef SQLITE_OMIT_WSD
110695       {
110696         unsigned int newVal = va_arg(ap, unsigned int);
110697         if( newVal ) sqlite3PendingByte = newVal;
110698       }
110699 #endif
110700       break;
110701     }
110702 
110703     /*
110704     **  sqlite3_test_control(SQLITE_TESTCTRL_ASSERT, int X)
110705     **
110706     ** This action provides a run-time test to see whether or not
110707     ** assert() was enabled at compile-time.  If X is true and assert()
110708     ** is enabled, then the return value is true.  If X is true and
110709     ** assert() is disabled, then the return value is zero.  If X is
110710     ** false and assert() is enabled, then the assertion fires and the
110711     ** process aborts.  If X is false and assert() is disabled, then the
110712     ** return value is zero.
110713     */
110714     case SQLITE_TESTCTRL_ASSERT: {
110715       volatile int x = 0;
110716       assert( (x = va_arg(ap,int))!=0 );
110717       rc = x;
110718       break;
110719     }
110720 
110721 
110722     /*
110723     **  sqlite3_test_control(SQLITE_TESTCTRL_ALWAYS, int X)
110724     **
110725     ** This action provides a run-time test to see how the ALWAYS and
110726     ** NEVER macros were defined at compile-time.
110727     **
110728     ** The return value is ALWAYS(X).
110729     **
110730     ** The recommended test is X==2.  If the return value is 2, that means
110731     ** ALWAYS() and NEVER() are both no-op pass-through macros, which is the
110732     ** default setting.  If the return value is 1, then ALWAYS() is either
110733     ** hard-coded to true or else it asserts if its argument is false.
110734     ** The first behavior (hard-coded to true) is the case if
110735     ** SQLITE_TESTCTRL_ASSERT shows that assert() is disabled and the second
110736     ** behavior (assert if the argument to ALWAYS() is false) is the case if
110737     ** SQLITE_TESTCTRL_ASSERT shows that assert() is enabled.
110738     **
110739     ** The run-time test procedure might look something like this:
110740     **
110741     **    if( sqlite3_test_control(SQLITE_TESTCTRL_ALWAYS, 2)==2 ){
110742     **      // ALWAYS() and NEVER() are no-op pass-through macros
110743     **    }else if( sqlite3_test_control(SQLITE_TESTCTRL_ASSERT, 1) ){
110744     **      // ALWAYS(x) asserts that x is true. NEVER(x) asserts x is false.
110745     **    }else{
110746     **      // ALWAYS(x) is a constant 1.  NEVER(x) is a constant 0.
110747     **    }
110748     */
110749     case SQLITE_TESTCTRL_ALWAYS: {
110750       int x = va_arg(ap,int);
110751       rc = ALWAYS(x);
110752       break;
110753     }
110754 
110755     /*   sqlite3_test_control(SQLITE_TESTCTRL_RESERVE, sqlite3 *db, int N)
110756     **
110757     ** Set the nReserve size to N for the main database on the database
110758     ** connection db.
110759     */
110760     case SQLITE_TESTCTRL_RESERVE: {
110761       sqlite3 *db = va_arg(ap, sqlite3*);
110762       int x = va_arg(ap,int);
110763       sqlite3_mutex_enter(db->mutex);
110764       sqlite3BtreeSetPageSize(db->aDb[0].pBt, 0, x, 0);
110765       sqlite3_mutex_leave(db->mutex);
110766       break;
110767     }
110768 
110769     /*  sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS, sqlite3 *db, int N)
110770     **
110771     ** Enable or disable various optimizations for testing purposes.  The
110772     ** argument N is a bitmask of optimizations to be disabled.  For normal
110773     ** operation N should be 0.  The idea is that a test program (like the
110774     ** SQL Logic Test or SLT test module) can run the same SQL multiple times
110775     ** with various optimizations disabled to verify that the same answer
110776     ** is obtained in every case.
110777     */
110778     case SQLITE_TESTCTRL_OPTIMIZATIONS: {
110779       sqlite3 *db = va_arg(ap, sqlite3*);
110780       int x = va_arg(ap,int);
110781       db->flags = (x & SQLITE_OptMask) | (db->flags & ~SQLITE_OptMask);
110782       break;
110783     }
110784 
110785 #ifdef SQLITE_N_KEYWORD
110786     /* sqlite3_test_control(SQLITE_TESTCTRL_ISKEYWORD, const char *zWord)
110787     **
110788     ** If zWord is a keyword recognized by the parser, then return the
110789     ** number of keywords.  Or if zWord is not a keyword, return 0.
110790     **
110791     ** This test feature is only available in the amalgamation since
110792     ** the SQLITE_N_KEYWORD macro is not defined in this file if SQLite
110793     ** is built using separate source files.
110794     */
110795     case SQLITE_TESTCTRL_ISKEYWORD: {
110796       const char *zWord = va_arg(ap, const char*);
110797       int n = sqlite3Strlen30(zWord);
110798       rc = (sqlite3KeywordCode((u8*)zWord, n)!=TK_ID) ? SQLITE_N_KEYWORD : 0;
110799       break;
110800     }
110801 #endif
110802 
110803     /* sqlite3_test_control(SQLITE_TESTCTRL_PGHDRSZ)
110804     **
110805     ** Return the size of a pcache header in bytes.
110806     */
110807     case SQLITE_TESTCTRL_PGHDRSZ: {
110808       rc = sizeof(PgHdr);
110809       break;
110810     }
110811 
110812     /* sqlite3_test_control(SQLITE_TESTCTRL_SCRATCHMALLOC, sz, &pNew, pFree);
110813     **
110814     ** Pass pFree into sqlite3ScratchFree().
110815     ** If sz>0 then allocate a scratch buffer into pNew.
110816     */
110817     case SQLITE_TESTCTRL_SCRATCHMALLOC: {
110818       void *pFree, **ppNew;
110819       int sz;
110820       sz = va_arg(ap, int);
110821       ppNew = va_arg(ap, void**);
110822       pFree = va_arg(ap, void*);
110823       if( sz ) *ppNew = sqlite3ScratchMalloc(sz);
110824       sqlite3ScratchFree(pFree);
110825       break;
110826     }
110827 
110828     /*   sqlite3_test_control(SQLITE_TESTCTRL_LOCALTIME_FAULT, int onoff);
110829     **
110830     ** If parameter onoff is non-zero, configure the wrappers so that all
110831     ** subsequent calls to localtime() and variants fail. If onoff is zero,
110832     ** undo this setting.
110833     */
110834     case SQLITE_TESTCTRL_LOCALTIME_FAULT: {
110835       sqlite3GlobalConfig.bLocaltimeFault = va_arg(ap, int);
110836       break;
110837     }
110838 
110839   }
110840   va_end(ap);
110841 #endif /* SQLITE_OMIT_BUILTIN_TEST */
110842   return rc;
110843 }
110844 
110845 /*
110846 ** This is a utility routine, useful to VFS implementations, that checks
110847 ** to see if a database file was a URI that contained a specific query
110848 ** parameter, and if so obtains the value of the query parameter.
110849 **
110850 ** The zFilename argument is the filename pointer passed into the xOpen()
110851 ** method of a VFS implementation.  The zParam argument is the name of the
110852 ** query parameter we seek.  This routine returns the value of the zParam
110853 ** parameter if it exists.  If the parameter does not exist, this routine
110854 ** returns a NULL pointer.
110855 */
110856 SQLITE_API const char *sqlite3_uri_parameter(const char *zFilename, const char *zParam){
110857   zFilename += sqlite3Strlen30(zFilename) + 1;
110858   while( zFilename[0] ){
110859     int x = strcmp(zFilename, zParam);
110860     zFilename += sqlite3Strlen30(zFilename) + 1;
110861     if( x==0 ) return zFilename;
110862     zFilename += sqlite3Strlen30(zFilename) + 1;
110863   }
110864   return 0;
110865 }
110866 
110867 /************** End of main.c ************************************************/
110868 /************** Begin file notify.c ******************************************/
110869 /*
110870 ** 2009 March 3
110871 **
110872 ** The author disclaims copyright to this source code.  In place of
110873 ** a legal notice, here is a blessing:
110874 **
110875 **    May you do good and not evil.
110876 **    May you find forgiveness for yourself and forgive others.
110877 **    May you share freely, never taking more than you give.
110878 **
110879 *************************************************************************
110880 **
110881 ** This file contains the implementation of the sqlite3_unlock_notify()
110882 ** API method and its associated functionality.
110883 */
110884 
110885 /* Omit this entire file if SQLITE_ENABLE_UNLOCK_NOTIFY is not defined. */
110886 #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
110887 
110888 /*
110889 ** Public interfaces:
110890 **
110891 **   sqlite3ConnectionBlocked()
110892 **   sqlite3ConnectionUnlocked()
110893 **   sqlite3ConnectionClosed()
110894 **   sqlite3_unlock_notify()
110895 */
110896 
110897 #define assertMutexHeld() \
110898   assert( sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)) )
110899 
110900 /*
110901 ** Head of a linked list of all sqlite3 objects created by this process
110902 ** for which either sqlite3.pBlockingConnection or sqlite3.pUnlockConnection
110903 ** is not NULL. This variable may only accessed while the STATIC_MASTER
110904 ** mutex is held.
110905 */
110906 static sqlite3 *SQLITE_WSD sqlite3BlockedList = 0;
110907 
110908 #ifndef NDEBUG
110909 /*
110910 ** This function is a complex assert() that verifies the following
110911 ** properties of the blocked connections list:
110912 **
110913 **   1) Each entry in the list has a non-NULL value for either
110914 **      pUnlockConnection or pBlockingConnection, or both.
110915 **
110916 **   2) All entries in the list that share a common value for
110917 **      xUnlockNotify are grouped together.
110918 **
110919 **   3) If the argument db is not NULL, then none of the entries in the
110920 **      blocked connections list have pUnlockConnection or pBlockingConnection
110921 **      set to db. This is used when closing connection db.
110922 */
110923 static void checkListProperties(sqlite3 *db){
110924   sqlite3 *p;
110925   for(p=sqlite3BlockedList; p; p=p->pNextBlocked){
110926     int seen = 0;
110927     sqlite3 *p2;
110928 
110929     /* Verify property (1) */
110930     assert( p->pUnlockConnection || p->pBlockingConnection );
110931 
110932     /* Verify property (2) */
110933     for(p2=sqlite3BlockedList; p2!=p; p2=p2->pNextBlocked){
110934       if( p2->xUnlockNotify==p->xUnlockNotify ) seen = 1;
110935       assert( p2->xUnlockNotify==p->xUnlockNotify || !seen );
110936       assert( db==0 || p->pUnlockConnection!=db );
110937       assert( db==0 || p->pBlockingConnection!=db );
110938     }
110939   }
110940 }
110941 #else
110942 # define checkListProperties(x)
110943 #endif
110944 
110945 /*
110946 ** Remove connection db from the blocked connections list. If connection
110947 ** db is not currently a part of the list, this function is a no-op.
110948 */
110949 static void removeFromBlockedList(sqlite3 *db){
110950   sqlite3 **pp;
110951   assertMutexHeld();
110952   for(pp=&sqlite3BlockedList; *pp; pp = &(*pp)->pNextBlocked){
110953     if( *pp==db ){
110954       *pp = (*pp)->pNextBlocked;
110955       break;
110956     }
110957   }
110958 }
110959 
110960 /*
110961 ** Add connection db to the blocked connections list. It is assumed
110962 ** that it is not already a part of the list.
110963 */
110964 static void addToBlockedList(sqlite3 *db){
110965   sqlite3 **pp;
110966   assertMutexHeld();
110967   for(
110968     pp=&sqlite3BlockedList;
110969     *pp && (*pp)->xUnlockNotify!=db->xUnlockNotify;
110970     pp=&(*pp)->pNextBlocked
110971   );
110972   db->pNextBlocked = *pp;
110973   *pp = db;
110974 }
110975 
110976 /*
110977 ** Obtain the STATIC_MASTER mutex.
110978 */
110979 static void enterMutex(void){
110980   sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
110981   checkListProperties(0);
110982 }
110983 
110984 /*
110985 ** Release the STATIC_MASTER mutex.
110986 */
110987 static void leaveMutex(void){
110988   assertMutexHeld();
110989   checkListProperties(0);
110990   sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
110991 }
110992 
110993 /*
110994 ** Register an unlock-notify callback.
110995 **
110996 ** This is called after connection "db" has attempted some operation
110997 ** but has received an SQLITE_LOCKED error because another connection
110998 ** (call it pOther) in the same process was busy using the same shared
110999 ** cache.  pOther is found by looking at db->pBlockingConnection.
111000 **
111001 ** If there is no blocking connection, the callback is invoked immediately,
111002 ** before this routine returns.
111003 **
111004 ** If pOther is already blocked on db, then report SQLITE_LOCKED, to indicate
111005 ** a deadlock.
111006 **
111007 ** Otherwise, make arrangements to invoke xNotify when pOther drops
111008 ** its locks.
111009 **
111010 ** Each call to this routine overrides any prior callbacks registered
111011 ** on the same "db".  If xNotify==0 then any prior callbacks are immediately
111012 ** cancelled.
111013 */
111014 SQLITE_API int sqlite3_unlock_notify(
111015   sqlite3 *db,
111016   void (*xNotify)(void **, int),
111017   void *pArg
111018 ){
111019   int rc = SQLITE_OK;
111020 
111021   sqlite3_mutex_enter(db->mutex);
111022   enterMutex();
111023 
111024   if( xNotify==0 ){
111025     removeFromBlockedList(db);
111026     db->pBlockingConnection = 0;
111027     db->pUnlockConnection = 0;
111028     db->xUnlockNotify = 0;
111029     db->pUnlockArg = 0;
111030   }else if( 0==db->pBlockingConnection ){
111031     /* The blocking transaction has been concluded. Or there never was a
111032     ** blocking transaction. In either case, invoke the notify callback
111033     ** immediately.
111034     */
111035     xNotify(&pArg, 1);
111036   }else{
111037     sqlite3 *p;
111038 
111039     for(p=db->pBlockingConnection; p && p!=db; p=p->pUnlockConnection){}
111040     if( p ){
111041       rc = SQLITE_LOCKED;              /* Deadlock detected. */
111042     }else{
111043       db->pUnlockConnection = db->pBlockingConnection;
111044       db->xUnlockNotify = xNotify;
111045       db->pUnlockArg = pArg;
111046       removeFromBlockedList(db);
111047       addToBlockedList(db);
111048     }
111049   }
111050 
111051   leaveMutex();
111052   assert( !db->mallocFailed );
111053   sqlite3Error(db, rc, (rc?"database is deadlocked":0));
111054   sqlite3_mutex_leave(db->mutex);
111055   return rc;
111056 }
111057 
111058 /*
111059 ** This function is called while stepping or preparing a statement
111060 ** associated with connection db. The operation will return SQLITE_LOCKED
111061 ** to the user because it requires a lock that will not be available
111062 ** until connection pBlocker concludes its current transaction.
111063 */
111064 SQLITE_PRIVATE void sqlite3ConnectionBlocked(sqlite3 *db, sqlite3 *pBlocker){
111065   enterMutex();
111066   if( db->pBlockingConnection==0 && db->pUnlockConnection==0 ){
111067     addToBlockedList(db);
111068   }
111069   db->pBlockingConnection = pBlocker;
111070   leaveMutex();
111071 }
111072 
111073 /*
111074 ** This function is called when
111075 ** the transaction opened by database db has just finished. Locks held
111076 ** by database connection db have been released.
111077 **
111078 ** This function loops through each entry in the blocked connections
111079 ** list and does the following:
111080 **
111081 **   1) If the sqlite3.pBlockingConnection member of a list entry is
111082 **      set to db, then set pBlockingConnection=0.
111083 **
111084 **   2) If the sqlite3.pUnlockConnection member of a list entry is
111085 **      set to db, then invoke the configured unlock-notify callback and
111086 **      set pUnlockConnection=0.
111087 **
111088 **   3) If the two steps above mean that pBlockingConnection==0 and
111089 **      pUnlockConnection==0, remove the entry from the blocked connections
111090 **      list.
111091 */
111092 SQLITE_PRIVATE void sqlite3ConnectionUnlocked(sqlite3 *db){
111093   void (*xUnlockNotify)(void **, int) = 0; /* Unlock-notify cb to invoke */
111094   int nArg = 0;                            /* Number of entries in aArg[] */
111095   sqlite3 **pp;                            /* Iterator variable */
111096   void **aArg;               /* Arguments to the unlock callback */
111097   void **aDyn = 0;           /* Dynamically allocated space for aArg[] */
111098   void *aStatic[16];         /* Starter space for aArg[].  No malloc required */
111099 
111100   aArg = aStatic;
111101   enterMutex();         /* Enter STATIC_MASTER mutex */
111102 
111103   /* This loop runs once for each entry in the blocked-connections list. */
111104   for(pp=&sqlite3BlockedList; *pp; /* no-op */ ){
111105     sqlite3 *p = *pp;
111106 
111107     /* Step 1. */
111108     if( p->pBlockingConnection==db ){
111109       p->pBlockingConnection = 0;
111110     }
111111 
111112     /* Step 2. */
111113     if( p->pUnlockConnection==db ){
111114       assert( p->xUnlockNotify );
111115       if( p->xUnlockNotify!=xUnlockNotify && nArg!=0 ){
111116         xUnlockNotify(aArg, nArg);
111117         nArg = 0;
111118       }
111119 
111120       sqlite3BeginBenignMalloc();
111121       assert( aArg==aDyn || (aDyn==0 && aArg==aStatic) );
111122       assert( nArg<=(int)ArraySize(aStatic) || aArg==aDyn );
111123       if( (!aDyn && nArg==(int)ArraySize(aStatic))
111124        || (aDyn && nArg==(int)(sqlite3MallocSize(aDyn)/sizeof(void*)))
111125       ){
111126         /* The aArg[] array needs to grow. */
111127         void **pNew = (void **)sqlite3Malloc(nArg*sizeof(void *)*2);
111128         if( pNew ){
111129           memcpy(pNew, aArg, nArg*sizeof(void *));
111130           sqlite3_free(aDyn);
111131           aDyn = aArg = pNew;
111132         }else{
111133           /* This occurs when the array of context pointers that need to
111134           ** be passed to the unlock-notify callback is larger than the
111135           ** aStatic[] array allocated on the stack and the attempt to
111136           ** allocate a larger array from the heap has failed.
111137           **
111138           ** This is a difficult situation to handle. Returning an error
111139           ** code to the caller is insufficient, as even if an error code
111140           ** is returned the transaction on connection db will still be
111141           ** closed and the unlock-notify callbacks on blocked connections
111142           ** will go unissued. This might cause the application to wait
111143           ** indefinitely for an unlock-notify callback that will never
111144           ** arrive.
111145           **
111146           ** Instead, invoke the unlock-notify callback with the context
111147           ** array already accumulated. We can then clear the array and
111148           ** begin accumulating any further context pointers without
111149           ** requiring any dynamic allocation. This is sub-optimal because
111150           ** it means that instead of one callback with a large array of
111151           ** context pointers the application will receive two or more
111152           ** callbacks with smaller arrays of context pointers, which will
111153           ** reduce the applications ability to prioritize multiple
111154           ** connections. But it is the best that can be done under the
111155           ** circumstances.
111156           */
111157           xUnlockNotify(aArg, nArg);
111158           nArg = 0;
111159         }
111160       }
111161       sqlite3EndBenignMalloc();
111162 
111163       aArg[nArg++] = p->pUnlockArg;
111164       xUnlockNotify = p->xUnlockNotify;
111165       p->pUnlockConnection = 0;
111166       p->xUnlockNotify = 0;
111167       p->pUnlockArg = 0;
111168     }
111169 
111170     /* Step 3. */
111171     if( p->pBlockingConnection==0 && p->pUnlockConnection==0 ){
111172       /* Remove connection p from the blocked connections list. */
111173       *pp = p->pNextBlocked;
111174       p->pNextBlocked = 0;
111175     }else{
111176       pp = &p->pNextBlocked;
111177     }
111178   }
111179 
111180   if( nArg!=0 ){
111181     xUnlockNotify(aArg, nArg);
111182   }
111183   sqlite3_free(aDyn);
111184   leaveMutex();         /* Leave STATIC_MASTER mutex */
111185 }
111186 
111187 /*
111188 ** This is called when the database connection passed as an argument is
111189 ** being closed. The connection is removed from the blocked list.
111190 */
111191 SQLITE_PRIVATE void sqlite3ConnectionClosed(sqlite3 *db){
111192   sqlite3ConnectionUnlocked(db);
111193   enterMutex();
111194   removeFromBlockedList(db);
111195   checkListProperties(db);
111196   leaveMutex();
111197 }
111198 #endif
111199 
111200 /************** End of notify.c **********************************************/
111201 /************** Begin file fts3.c ********************************************/
111202 /*
111203 ** 2006 Oct 10
111204 **
111205 ** The author disclaims copyright to this source code.  In place of
111206 ** a legal notice, here is a blessing:
111207 **
111208 **    May you do good and not evil.
111209 **    May you find forgiveness for yourself and forgive others.
111210 **    May you share freely, never taking more than you give.
111211 **
111212 ******************************************************************************
111213 **
111214 ** This is an SQLite module implementing full-text search.
111215 */
111216 
111217 /*
111218 ** The code in this file is only compiled if:
111219 **
111220 **     * The FTS3 module is being built as an extension
111221 **       (in which case SQLITE_CORE is not defined), or
111222 **
111223 **     * The FTS3 module is being built into the core of
111224 **       SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
111225 */
111226 
111227 /* The full-text index is stored in a series of b+tree (-like)
111228 ** structures called segments which map terms to doclists.  The
111229 ** structures are like b+trees in layout, but are constructed from the
111230 ** bottom up in optimal fashion and are not updatable.  Since trees
111231 ** are built from the bottom up, things will be described from the
111232 ** bottom up.
111233 **
111234 **
111235 **** Varints ****
111236 ** The basic unit of encoding is a variable-length integer called a
111237 ** varint.  We encode variable-length integers in little-endian order
111238 ** using seven bits * per byte as follows:
111239 **
111240 ** KEY:
111241 **         A = 0xxxxxxx    7 bits of data and one flag bit
111242 **         B = 1xxxxxxx    7 bits of data and one flag bit
111243 **
111244 **  7 bits - A
111245 ** 14 bits - BA
111246 ** 21 bits - BBA
111247 ** and so on.
111248 **
111249 ** This is similar in concept to how sqlite encodes "varints" but
111250 ** the encoding is not the same.  SQLite varints are big-endian
111251 ** are are limited to 9 bytes in length whereas FTS3 varints are
111252 ** little-endian and can be up to 10 bytes in length (in theory).
111253 **
111254 ** Example encodings:
111255 **
111256 **     1:    0x01
111257 **   127:    0x7f
111258 **   128:    0x81 0x00
111259 **
111260 **
111261 **** Document lists ****
111262 ** A doclist (document list) holds a docid-sorted list of hits for a
111263 ** given term.  Doclists hold docids and associated token positions.
111264 ** A docid is the unique integer identifier for a single document.
111265 ** A position is the index of a word within the document.  The first
111266 ** word of the document has a position of 0.
111267 **
111268 ** FTS3 used to optionally store character offsets using a compile-time
111269 ** option.  But that functionality is no longer supported.
111270 **
111271 ** A doclist is stored like this:
111272 **
111273 ** array {
111274 **   varint docid;
111275 **   array {                (position list for column 0)
111276 **     varint position;     (2 more than the delta from previous position)
111277 **   }
111278 **   array {
111279 **     varint POS_COLUMN;   (marks start of position list for new column)
111280 **     varint column;       (index of new column)
111281 **     array {
111282 **       varint position;   (2 more than the delta from previous position)
111283 **     }
111284 **   }
111285 **   varint POS_END;        (marks end of positions for this document.
111286 ** }
111287 **
111288 ** Here, array { X } means zero or more occurrences of X, adjacent in
111289 ** memory.  A "position" is an index of a token in the token stream
111290 ** generated by the tokenizer. Note that POS_END and POS_COLUMN occur
111291 ** in the same logical place as the position element, and act as sentinals
111292 ** ending a position list array.  POS_END is 0.  POS_COLUMN is 1.
111293 ** The positions numbers are not stored literally but rather as two more
111294 ** than the difference from the prior position, or the just the position plus
111295 ** 2 for the first position.  Example:
111296 **
111297 **   label:       A B C D E  F  G H   I  J K
111298 **   value:     123 5 9 1 1 14 35 0 234 72 0
111299 **
111300 ** The 123 value is the first docid.  For column zero in this document
111301 ** there are two matches at positions 3 and 10 (5-2 and 9-2+3).  The 1
111302 ** at D signals the start of a new column; the 1 at E indicates that the
111303 ** new column is column number 1.  There are two positions at 12 and 45
111304 ** (14-2 and 35-2+12).  The 0 at H indicate the end-of-document.  The
111305 ** 234 at I is the next docid.  It has one position 72 (72-2) and then
111306 ** terminates with the 0 at K.
111307 **
111308 ** A "position-list" is the list of positions for multiple columns for
111309 ** a single docid.  A "column-list" is the set of positions for a single
111310 ** column.  Hence, a position-list consists of one or more column-lists,
111311 ** a document record consists of a docid followed by a position-list and
111312 ** a doclist consists of one or more document records.
111313 **
111314 ** A bare doclist omits the position information, becoming an
111315 ** array of varint-encoded docids.
111316 **
111317 **** Segment leaf nodes ****
111318 ** Segment leaf nodes store terms and doclists, ordered by term.  Leaf
111319 ** nodes are written using LeafWriter, and read using LeafReader (to
111320 ** iterate through a single leaf node's data) and LeavesReader (to
111321 ** iterate through a segment's entire leaf layer).  Leaf nodes have
111322 ** the format:
111323 **
111324 ** varint iHeight;             (height from leaf level, always 0)
111325 ** varint nTerm;               (length of first term)
111326 ** char pTerm[nTerm];          (content of first term)
111327 ** varint nDoclist;            (length of term's associated doclist)
111328 ** char pDoclist[nDoclist];    (content of doclist)
111329 ** array {
111330 **                             (further terms are delta-encoded)
111331 **   varint nPrefix;           (length of prefix shared with previous term)
111332 **   varint nSuffix;           (length of unshared suffix)
111333 **   char pTermSuffix[nSuffix];(unshared suffix of next term)
111334 **   varint nDoclist;          (length of term's associated doclist)
111335 **   char pDoclist[nDoclist];  (content of doclist)
111336 ** }
111337 **
111338 ** Here, array { X } means zero or more occurrences of X, adjacent in
111339 ** memory.
111340 **
111341 ** Leaf nodes are broken into blocks which are stored contiguously in
111342 ** the %_segments table in sorted order.  This means that when the end
111343 ** of a node is reached, the next term is in the node with the next
111344 ** greater node id.
111345 **
111346 ** New data is spilled to a new leaf node when the current node
111347 ** exceeds LEAF_MAX bytes (default 2048).  New data which itself is
111348 ** larger than STANDALONE_MIN (default 1024) is placed in a standalone
111349 ** node (a leaf node with a single term and doclist).  The goal of
111350 ** these settings is to pack together groups of small doclists while
111351 ** making it efficient to directly access large doclists.  The
111352 ** assumption is that large doclists represent terms which are more
111353 ** likely to be query targets.
111354 **
111355 ** TODO(shess) It may be useful for blocking decisions to be more
111356 ** dynamic.  For instance, it may make more sense to have a 2.5k leaf
111357 ** node rather than splitting into 2k and .5k nodes.  My intuition is
111358 ** that this might extend through 2x or 4x the pagesize.
111359 **
111360 **
111361 **** Segment interior nodes ****
111362 ** Segment interior nodes store blockids for subtree nodes and terms
111363 ** to describe what data is stored by the each subtree.  Interior
111364 ** nodes are written using InteriorWriter, and read using
111365 ** InteriorReader.  InteriorWriters are created as needed when
111366 ** SegmentWriter creates new leaf nodes, or when an interior node
111367 ** itself grows too big and must be split.  The format of interior
111368 ** nodes:
111369 **
111370 ** varint iHeight;           (height from leaf level, always >0)
111371 ** varint iBlockid;          (block id of node's leftmost subtree)
111372 ** optional {
111373 **   varint nTerm;           (length of first term)
111374 **   char pTerm[nTerm];      (content of first term)
111375 **   array {
111376 **                                (further terms are delta-encoded)
111377 **     varint nPrefix;            (length of shared prefix with previous term)
111378 **     varint nSuffix;            (length of unshared suffix)
111379 **     char pTermSuffix[nSuffix]; (unshared suffix of next term)
111380 **   }
111381 ** }
111382 **
111383 ** Here, optional { X } means an optional element, while array { X }
111384 ** means zero or more occurrences of X, adjacent in memory.
111385 **
111386 ** An interior node encodes n terms separating n+1 subtrees.  The
111387 ** subtree blocks are contiguous, so only the first subtree's blockid
111388 ** is encoded.  The subtree at iBlockid will contain all terms less
111389 ** than the first term encoded (or all terms if no term is encoded).
111390 ** Otherwise, for terms greater than or equal to pTerm[i] but less
111391 ** than pTerm[i+1], the subtree for that term will be rooted at
111392 ** iBlockid+i.  Interior nodes only store enough term data to
111393 ** distinguish adjacent children (if the rightmost term of the left
111394 ** child is "something", and the leftmost term of the right child is
111395 ** "wicked", only "w" is stored).
111396 **
111397 ** New data is spilled to a new interior node at the same height when
111398 ** the current node exceeds INTERIOR_MAX bytes (default 2048).
111399 ** INTERIOR_MIN_TERMS (default 7) keeps large terms from monopolizing
111400 ** interior nodes and making the tree too skinny.  The interior nodes
111401 ** at a given height are naturally tracked by interior nodes at
111402 ** height+1, and so on.
111403 **
111404 **
111405 **** Segment directory ****
111406 ** The segment directory in table %_segdir stores meta-information for
111407 ** merging and deleting segments, and also the root node of the
111408 ** segment's tree.
111409 **
111410 ** The root node is the top node of the segment's tree after encoding
111411 ** the entire segment, restricted to ROOT_MAX bytes (default 1024).
111412 ** This could be either a leaf node or an interior node.  If the top
111413 ** node requires more than ROOT_MAX bytes, it is flushed to %_segments
111414 ** and a new root interior node is generated (which should always fit
111415 ** within ROOT_MAX because it only needs space for 2 varints, the
111416 ** height and the blockid of the previous root).
111417 **
111418 ** The meta-information in the segment directory is:
111419 **   level               - segment level (see below)
111420 **   idx                 - index within level
111421 **                       - (level,idx uniquely identify a segment)
111422 **   start_block         - first leaf node
111423 **   leaves_end_block    - last leaf node
111424 **   end_block           - last block (including interior nodes)
111425 **   root                - contents of root node
111426 **
111427 ** If the root node is a leaf node, then start_block,
111428 ** leaves_end_block, and end_block are all 0.
111429 **
111430 **
111431 **** Segment merging ****
111432 ** To amortize update costs, segments are grouped into levels and
111433 ** merged in batches.  Each increase in level represents exponentially
111434 ** more documents.
111435 **
111436 ** New documents (actually, document updates) are tokenized and
111437 ** written individually (using LeafWriter) to a level 0 segment, with
111438 ** incrementing idx.  When idx reaches MERGE_COUNT (default 16), all
111439 ** level 0 segments are merged into a single level 1 segment.  Level 1
111440 ** is populated like level 0, and eventually MERGE_COUNT level 1
111441 ** segments are merged to a single level 2 segment (representing
111442 ** MERGE_COUNT^2 updates), and so on.
111443 **
111444 ** A segment merge traverses all segments at a given level in
111445 ** parallel, performing a straightforward sorted merge.  Since segment
111446 ** leaf nodes are written in to the %_segments table in order, this
111447 ** merge traverses the underlying sqlite disk structures efficiently.
111448 ** After the merge, all segment blocks from the merged level are
111449 ** deleted.
111450 **
111451 ** MERGE_COUNT controls how often we merge segments.  16 seems to be
111452 ** somewhat of a sweet spot for insertion performance.  32 and 64 show
111453 ** very similar performance numbers to 16 on insertion, though they're
111454 ** a tiny bit slower (perhaps due to more overhead in merge-time
111455 ** sorting).  8 is about 20% slower than 16, 4 about 50% slower than
111456 ** 16, 2 about 66% slower than 16.
111457 **
111458 ** At query time, high MERGE_COUNT increases the number of segments
111459 ** which need to be scanned and merged.  For instance, with 100k docs
111460 ** inserted:
111461 **
111462 **    MERGE_COUNT   segments
111463 **       16           25
111464 **        8           12
111465 **        4           10
111466 **        2            6
111467 **
111468 ** This appears to have only a moderate impact on queries for very
111469 ** frequent terms (which are somewhat dominated by segment merge
111470 ** costs), and infrequent and non-existent terms still seem to be fast
111471 ** even with many segments.
111472 **
111473 ** TODO(shess) That said, it would be nice to have a better query-side
111474 ** argument for MERGE_COUNT of 16.  Also, it is possible/likely that
111475 ** optimizations to things like doclist merging will swing the sweet
111476 ** spot around.
111477 **
111478 **
111479 **
111480 **** Handling of deletions and updates ****
111481 ** Since we're using a segmented structure, with no docid-oriented
111482 ** index into the term index, we clearly cannot simply update the term
111483 ** index when a document is deleted or updated.  For deletions, we
111484 ** write an empty doclist (varint(docid) varint(POS_END)), for updates
111485 ** we simply write the new doclist.  Segment merges overwrite older
111486 ** data for a particular docid with newer data, so deletes or updates
111487 ** will eventually overtake the earlier data and knock it out.  The
111488 ** query logic likewise merges doclists so that newer data knocks out
111489 ** older data.
111490 **
111491 ** TODO(shess) Provide a VACUUM type operation to clear out all
111492 ** deletions and duplications.  This would basically be a forced merge
111493 ** into a single segment.
111494 */
111495 
111496 /************** Include fts3Int.h in the middle of fts3.c ********************/
111497 /************** Begin file fts3Int.h *****************************************/
111498 /*
111499 ** 2009 Nov 12
111500 **
111501 ** The author disclaims copyright to this source code.  In place of
111502 ** a legal notice, here is a blessing:
111503 **
111504 **    May you do good and not evil.
111505 **    May you find forgiveness for yourself and forgive others.
111506 **    May you share freely, never taking more than you give.
111507 **
111508 ******************************************************************************
111509 **
111510 */
111511 #ifndef _FTSINT_H
111512 #define _FTSINT_H
111513 
111514 #if !defined(NDEBUG) && !defined(SQLITE_DEBUG)
111515 # define NDEBUG 1
111516 #endif
111517 
111518 /*
111519 ** FTS4 is really an extension for FTS3.  It is enabled using the
111520 ** SQLITE_ENABLE_FTS3 macro.  But to avoid confusion we also all
111521 ** the SQLITE_ENABLE_FTS4 macro to serve as an alisse for SQLITE_ENABLE_FTS3.
111522 */
111523 #if defined(SQLITE_ENABLE_FTS4) && !defined(SQLITE_ENABLE_FTS3)
111524 # define SQLITE_ENABLE_FTS3
111525 #endif
111526 
111527 #ifdef SQLITE_ENABLE_FTS3
111528 /************** Include fts3_tokenizer.h in the middle of fts3Int.h **********/
111529 /************** Begin file fts3_tokenizer.h **********************************/
111530 /*
111531 ** 2006 July 10
111532 **
111533 ** The author disclaims copyright to this source code.
111534 **
111535 *************************************************************************
111536 ** Defines the interface to tokenizers used by fulltext-search.  There
111537 ** are three basic components:
111538 **
111539 ** sqlite3_tokenizer_module is a singleton defining the tokenizer
111540 ** interface functions.  This is essentially the class structure for
111541 ** tokenizers.
111542 **
111543 ** sqlite3_tokenizer is used to define a particular tokenizer, perhaps
111544 ** including customization information defined at creation time.
111545 **
111546 ** sqlite3_tokenizer_cursor is generated by a tokenizer to generate
111547 ** tokens from a particular input.
111548 */
111549 #ifndef _FTS3_TOKENIZER_H_
111550 #define _FTS3_TOKENIZER_H_
111551 
111552 /* TODO(shess) Only used for SQLITE_OK and SQLITE_DONE at this time.
111553 ** If tokenizers are to be allowed to call sqlite3_*() functions, then
111554 ** we will need a way to register the API consistently.
111555 */
111556 
111557 /*
111558 ** Structures used by the tokenizer interface. When a new tokenizer
111559 ** implementation is registered, the caller provides a pointer to
111560 ** an sqlite3_tokenizer_module containing pointers to the callback
111561 ** functions that make up an implementation.
111562 **
111563 ** When an fts3 table is created, it passes any arguments passed to
111564 ** the tokenizer clause of the CREATE VIRTUAL TABLE statement to the
111565 ** sqlite3_tokenizer_module.xCreate() function of the requested tokenizer
111566 ** implementation. The xCreate() function in turn returns an
111567 ** sqlite3_tokenizer structure representing the specific tokenizer to
111568 ** be used for the fts3 table (customized by the tokenizer clause arguments).
111569 **
111570 ** To tokenize an input buffer, the sqlite3_tokenizer_module.xOpen()
111571 ** method is called. It returns an sqlite3_tokenizer_cursor object
111572 ** that may be used to tokenize a specific input buffer based on
111573 ** the tokenization rules supplied by a specific sqlite3_tokenizer
111574 ** object.
111575 */
111576 typedef struct sqlite3_tokenizer_module sqlite3_tokenizer_module;
111577 typedef struct sqlite3_tokenizer sqlite3_tokenizer;
111578 typedef struct sqlite3_tokenizer_cursor sqlite3_tokenizer_cursor;
111579 
111580 struct sqlite3_tokenizer_module {
111581 
111582   /*
111583   ** Structure version. Should always be set to 0.
111584   */
111585   int iVersion;
111586 
111587   /*
111588   ** Create a new tokenizer. The values in the argv[] array are the
111589   ** arguments passed to the "tokenizer" clause of the CREATE VIRTUAL
111590   ** TABLE statement that created the fts3 table. For example, if
111591   ** the following SQL is executed:
111592   **
111593   **   CREATE .. USING fts3( ... , tokenizer <tokenizer-name> arg1 arg2)
111594   **
111595   ** then argc is set to 2, and the argv[] array contains pointers
111596   ** to the strings "arg1" and "arg2".
111597   **
111598   ** This method should return either SQLITE_OK (0), or an SQLite error
111599   ** code. If SQLITE_OK is returned, then *ppTokenizer should be set
111600   ** to point at the newly created tokenizer structure. The generic
111601   ** sqlite3_tokenizer.pModule variable should not be initialised by
111602   ** this callback. The caller will do so.
111603   */
111604   int (*xCreate)(
111605     int argc,                           /* Size of argv array */
111606     const char *const*argv,             /* Tokenizer argument strings */
111607     sqlite3_tokenizer **ppTokenizer     /* OUT: Created tokenizer */
111608   );
111609 
111610   /*
111611   ** Destroy an existing tokenizer. The fts3 module calls this method
111612   ** exactly once for each successful call to xCreate().
111613   */
111614   int (*xDestroy)(sqlite3_tokenizer *pTokenizer);
111615 
111616   /*
111617   ** Create a tokenizer cursor to tokenize an input buffer. The caller
111618   ** is responsible for ensuring that the input buffer remains valid
111619   ** until the cursor is closed (using the xClose() method).
111620   */
111621   int (*xOpen)(
111622     sqlite3_tokenizer *pTokenizer,       /* Tokenizer object */
111623     const char *pInput, int nBytes,      /* Input buffer */
111624     sqlite3_tokenizer_cursor **ppCursor  /* OUT: Created tokenizer cursor */
111625   );
111626 
111627   /*
111628   ** Destroy an existing tokenizer cursor. The fts3 module calls this
111629   ** method exactly once for each successful call to xOpen().
111630   */
111631   int (*xClose)(sqlite3_tokenizer_cursor *pCursor);
111632 
111633   /*
111634   ** Retrieve the next token from the tokenizer cursor pCursor. This
111635   ** method should either return SQLITE_OK and set the values of the
111636   ** "OUT" variables identified below, or SQLITE_DONE to indicate that
111637   ** the end of the buffer has been reached, or an SQLite error code.
111638   **
111639   ** *ppToken should be set to point at a buffer containing the
111640   ** normalized version of the token (i.e. after any case-folding and/or
111641   ** stemming has been performed). *pnBytes should be set to the length
111642   ** of this buffer in bytes. The input text that generated the token is
111643   ** identified by the byte offsets returned in *piStartOffset and
111644   ** *piEndOffset. *piStartOffset should be set to the index of the first
111645   ** byte of the token in the input buffer. *piEndOffset should be set
111646   ** to the index of the first byte just past the end of the token in
111647   ** the input buffer.
111648   **
111649   ** The buffer *ppToken is set to point at is managed by the tokenizer
111650   ** implementation. It is only required to be valid until the next call
111651   ** to xNext() or xClose().
111652   */
111653   /* TODO(shess) current implementation requires pInput to be
111654   ** nul-terminated.  This should either be fixed, or pInput/nBytes
111655   ** should be converted to zInput.
111656   */
111657   int (*xNext)(
111658     sqlite3_tokenizer_cursor *pCursor,   /* Tokenizer cursor */
111659     const char **ppToken, int *pnBytes,  /* OUT: Normalized text for token */
111660     int *piStartOffset,  /* OUT: Byte offset of token in input buffer */
111661     int *piEndOffset,    /* OUT: Byte offset of end of token in input buffer */
111662     int *piPosition      /* OUT: Number of tokens returned before this one */
111663   );
111664 };
111665 
111666 struct sqlite3_tokenizer {
111667   const sqlite3_tokenizer_module *pModule;  /* The module for this tokenizer */
111668   /* Tokenizer implementations will typically add additional fields */
111669 };
111670 
111671 struct sqlite3_tokenizer_cursor {
111672   sqlite3_tokenizer *pTokenizer;       /* Tokenizer for this cursor. */
111673   /* Tokenizer implementations will typically add additional fields */
111674 };
111675 
111676 int fts3_global_term_cnt(int iTerm, int iCol);
111677 int fts3_term_cnt(int iTerm, int iCol);
111678 
111679 
111680 #endif /* _FTS3_TOKENIZER_H_ */
111681 
111682 /************** End of fts3_tokenizer.h **************************************/
111683 /************** Continuing where we left off in fts3Int.h ********************/
111684 /************** Include fts3_hash.h in the middle of fts3Int.h ***************/
111685 /************** Begin file fts3_hash.h ***************************************/
111686 /*
111687 ** 2001 September 22
111688 **
111689 ** The author disclaims copyright to this source code.  In place of
111690 ** a legal notice, here is a blessing:
111691 **
111692 **    May you do good and not evil.
111693 **    May you find forgiveness for yourself and forgive others.
111694 **    May you share freely, never taking more than you give.
111695 **
111696 *************************************************************************
111697 ** This is the header file for the generic hash-table implemenation
111698 ** used in SQLite.  We've modified it slightly to serve as a standalone
111699 ** hash table implementation for the full-text indexing module.
111700 **
111701 */
111702 #ifndef _FTS3_HASH_H_
111703 #define _FTS3_HASH_H_
111704 
111705 /* Forward declarations of structures. */
111706 typedef struct Fts3Hash Fts3Hash;
111707 typedef struct Fts3HashElem Fts3HashElem;
111708 
111709 /* A complete hash table is an instance of the following structure.
111710 ** The internals of this structure are intended to be opaque -- client
111711 ** code should not attempt to access or modify the fields of this structure
111712 ** directly.  Change this structure only by using the routines below.
111713 ** However, many of the "procedures" and "functions" for modifying and
111714 ** accessing this structure are really macros, so we can't really make
111715 ** this structure opaque.
111716 */
111717 struct Fts3Hash {
111718   char keyClass;          /* HASH_INT, _POINTER, _STRING, _BINARY */
111719   char copyKey;           /* True if copy of key made on insert */
111720   int count;              /* Number of entries in this table */
111721   Fts3HashElem *first;    /* The first element of the array */
111722   int htsize;             /* Number of buckets in the hash table */
111723   struct _fts3ht {        /* the hash table */
111724     int count;               /* Number of entries with this hash */
111725     Fts3HashElem *chain;     /* Pointer to first entry with this hash */
111726   } *ht;
111727 };
111728 
111729 /* Each element in the hash table is an instance of the following
111730 ** structure.  All elements are stored on a single doubly-linked list.
111731 **
111732 ** Again, this structure is intended to be opaque, but it can't really
111733 ** be opaque because it is used by macros.
111734 */
111735 struct Fts3HashElem {
111736   Fts3HashElem *next, *prev; /* Next and previous elements in the table */
111737   void *data;                /* Data associated with this element */
111738   void *pKey; int nKey;      /* Key associated with this element */
111739 };
111740 
111741 /*
111742 ** There are 2 different modes of operation for a hash table:
111743 **
111744 **   FTS3_HASH_STRING        pKey points to a string that is nKey bytes long
111745 **                           (including the null-terminator, if any).  Case
111746 **                           is respected in comparisons.
111747 **
111748 **   FTS3_HASH_BINARY        pKey points to binary data nKey bytes long.
111749 **                           memcmp() is used to compare keys.
111750 **
111751 ** A copy of the key is made if the copyKey parameter to fts3HashInit is 1.
111752 */
111753 #define FTS3_HASH_STRING    1
111754 #define FTS3_HASH_BINARY    2
111755 
111756 /*
111757 ** Access routines.  To delete, insert a NULL pointer.
111758 */
111759 SQLITE_PRIVATE void sqlite3Fts3HashInit(Fts3Hash *pNew, char keyClass, char copyKey);
111760 SQLITE_PRIVATE void *sqlite3Fts3HashInsert(Fts3Hash*, const void *pKey, int nKey, void *pData);
111761 SQLITE_PRIVATE void *sqlite3Fts3HashFind(const Fts3Hash*, const void *pKey, int nKey);
111762 SQLITE_PRIVATE void sqlite3Fts3HashClear(Fts3Hash*);
111763 SQLITE_PRIVATE Fts3HashElem *sqlite3Fts3HashFindElem(const Fts3Hash *, const void *, int);
111764 
111765 /*
111766 ** Shorthand for the functions above
111767 */
111768 #define fts3HashInit     sqlite3Fts3HashInit
111769 #define fts3HashInsert   sqlite3Fts3HashInsert
111770 #define fts3HashFind     sqlite3Fts3HashFind
111771 #define fts3HashClear    sqlite3Fts3HashClear
111772 #define fts3HashFindElem sqlite3Fts3HashFindElem
111773 
111774 /*
111775 ** Macros for looping over all elements of a hash table.  The idiom is
111776 ** like this:
111777 **
111778 **   Fts3Hash h;
111779 **   Fts3HashElem *p;
111780 **   ...
111781 **   for(p=fts3HashFirst(&h); p; p=fts3HashNext(p)){
111782 **     SomeStructure *pData = fts3HashData(p);
111783 **     // do something with pData
111784 **   }
111785 */
111786 #define fts3HashFirst(H)  ((H)->first)
111787 #define fts3HashNext(E)   ((E)->next)
111788 #define fts3HashData(E)   ((E)->data)
111789 #define fts3HashKey(E)    ((E)->pKey)
111790 #define fts3HashKeysize(E) ((E)->nKey)
111791 
111792 /*
111793 ** Number of entries in a hash table
111794 */
111795 #define fts3HashCount(H)  ((H)->count)
111796 
111797 #endif /* _FTS3_HASH_H_ */
111798 
111799 /************** End of fts3_hash.h *******************************************/
111800 /************** Continuing where we left off in fts3Int.h ********************/
111801 
111802 /*
111803 ** This constant controls how often segments are merged. Once there are
111804 ** FTS3_MERGE_COUNT segments of level N, they are merged into a single
111805 ** segment of level N+1.
111806 */
111807 #define FTS3_MERGE_COUNT 16
111808 
111809 /*
111810 ** This is the maximum amount of data (in bytes) to store in the
111811 ** Fts3Table.pendingTerms hash table. Normally, the hash table is
111812 ** populated as documents are inserted/updated/deleted in a transaction
111813 ** and used to create a new segment when the transaction is committed.
111814 ** However if this limit is reached midway through a transaction, a new
111815 ** segment is created and the hash table cleared immediately.
111816 */
111817 #define FTS3_MAX_PENDING_DATA (1*1024*1024)
111818 
111819 /*
111820 ** Macro to return the number of elements in an array. SQLite has a
111821 ** similar macro called ArraySize(). Use a different name to avoid
111822 ** a collision when building an amalgamation with built-in FTS3.
111823 */
111824 #define SizeofArray(X) ((int)(sizeof(X)/sizeof(X[0])))
111825 
111826 
111827 #ifndef MIN
111828 # define MIN(x,y) ((x)<(y)?(x):(y))
111829 #endif
111830 
111831 /*
111832 ** Maximum length of a varint encoded integer. The varint format is different
111833 ** from that used by SQLite, so the maximum length is 10, not 9.
111834 */
111835 #define FTS3_VARINT_MAX 10
111836 
111837 /*
111838 ** FTS4 virtual tables may maintain multiple indexes - one index of all terms
111839 ** in the document set and zero or more prefix indexes. All indexes are stored
111840 ** as one or more b+-trees in the %_segments and %_segdir tables.
111841 **
111842 ** It is possible to determine which index a b+-tree belongs to based on the
111843 ** value stored in the "%_segdir.level" column. Given this value L, the index
111844 ** that the b+-tree belongs to is (L<<10). In other words, all b+-trees with
111845 ** level values between 0 and 1023 (inclusive) belong to index 0, all levels
111846 ** between 1024 and 2047 to index 1, and so on.
111847 **
111848 ** It is considered impossible for an index to use more than 1024 levels. In
111849 ** theory though this may happen, but only after at least
111850 ** (FTS3_MERGE_COUNT^1024) separate flushes of the pending-terms tables.
111851 */
111852 #define FTS3_SEGDIR_MAXLEVEL      1024
111853 #define FTS3_SEGDIR_MAXLEVEL_STR "1024"
111854 
111855 /*
111856 ** The testcase() macro is only used by the amalgamation.  If undefined,
111857 ** make it a no-op.
111858 */
111859 #ifndef testcase
111860 # define testcase(X)
111861 #endif
111862 
111863 /*
111864 ** Terminator values for position-lists and column-lists.
111865 */
111866 #define POS_COLUMN  (1)     /* Column-list terminator */
111867 #define POS_END     (0)     /* Position-list terminator */
111868 
111869 /*
111870 ** This section provides definitions to allow the
111871 ** FTS3 extension to be compiled outside of the
111872 ** amalgamation.
111873 */
111874 #ifndef SQLITE_AMALGAMATION
111875 /*
111876 ** Macros indicating that conditional expressions are always true or
111877 ** false.
111878 */
111879 #ifdef SQLITE_COVERAGE_TEST
111880 # define ALWAYS(x) (1)
111881 # define NEVER(X)  (0)
111882 #else
111883 # define ALWAYS(x) (x)
111884 # define NEVER(X)  (x)
111885 #endif
111886 
111887 /*
111888 ** Internal types used by SQLite.
111889 */
111890 typedef unsigned char u8;         /* 1-byte (or larger) unsigned integer */
111891 typedef short int i16;            /* 2-byte (or larger) signed integer */
111892 typedef unsigned int u32;         /* 4-byte unsigned integer */
111893 typedef sqlite3_uint64 u64;       /* 8-byte unsigned integer */
111894 
111895 /*
111896 ** Macro used to suppress compiler warnings for unused parameters.
111897 */
111898 #define UNUSED_PARAMETER(x) (void)(x)
111899 
111900 /*
111901 ** Activate assert() only if SQLITE_TEST is enabled.
111902 */
111903 #if !defined(NDEBUG) && !defined(SQLITE_DEBUG)
111904 # define NDEBUG 1
111905 #endif
111906 
111907 /*
111908 ** The TESTONLY macro is used to enclose variable declarations or
111909 ** other bits of code that are needed to support the arguments
111910 ** within testcase() and assert() macros.
111911 */
111912 #if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
111913 # define TESTONLY(X)  X
111914 #else
111915 # define TESTONLY(X)
111916 #endif
111917 
111918 #endif /* SQLITE_AMALGAMATION */
111919 
111920 typedef struct Fts3Table Fts3Table;
111921 typedef struct Fts3Cursor Fts3Cursor;
111922 typedef struct Fts3Expr Fts3Expr;
111923 typedef struct Fts3Phrase Fts3Phrase;
111924 typedef struct Fts3PhraseToken Fts3PhraseToken;
111925 
111926 typedef struct Fts3Doclist Fts3Doclist;
111927 typedef struct Fts3SegFilter Fts3SegFilter;
111928 typedef struct Fts3DeferredToken Fts3DeferredToken;
111929 typedef struct Fts3SegReader Fts3SegReader;
111930 typedef struct Fts3MultiSegReader Fts3MultiSegReader;
111931 
111932 /*
111933 ** A connection to a fulltext index is an instance of the following
111934 ** structure. The xCreate and xConnect methods create an instance
111935 ** of this structure and xDestroy and xDisconnect free that instance.
111936 ** All other methods receive a pointer to the structure as one of their
111937 ** arguments.
111938 */
111939 struct Fts3Table {
111940   sqlite3_vtab base;              /* Base class used by SQLite core */
111941   sqlite3 *db;                    /* The database connection */
111942   const char *zDb;                /* logical database name */
111943   const char *zName;              /* virtual table name */
111944   int nColumn;                    /* number of named columns in virtual table */
111945   char **azColumn;                /* column names.  malloced */
111946   sqlite3_tokenizer *pTokenizer;  /* tokenizer for inserts and queries */
111947 
111948   /* Precompiled statements used by the implementation. Each of these
111949   ** statements is run and reset within a single virtual table API call.
111950   */
111951   sqlite3_stmt *aStmt[27];
111952 
111953   char *zReadExprlist;
111954   char *zWriteExprlist;
111955 
111956   int nNodeSize;                  /* Soft limit for node size */
111957   u8 bHasStat;                    /* True if %_stat table exists */
111958   u8 bHasDocsize;                 /* True if %_docsize table exists */
111959   u8 bDescIdx;                    /* True if doclists are in reverse order */
111960   int nPgsz;                      /* Page size for host database */
111961   char *zSegmentsTbl;             /* Name of %_segments table */
111962   sqlite3_blob *pSegments;        /* Blob handle open on %_segments table */
111963 
111964   /* TODO: Fix the first paragraph of this comment.
111965   **
111966   ** The following hash table is used to buffer pending index updates during
111967   ** transactions. Variable nPendingData estimates the memory size of the
111968   ** pending data, including hash table overhead, but not malloc overhead.
111969   ** When nPendingData exceeds nMaxPendingData, the buffer is flushed
111970   ** automatically. Variable iPrevDocid is the docid of the most recently
111971   ** inserted record.
111972   **
111973   ** A single FTS4 table may have multiple full-text indexes. For each index
111974   ** there is an entry in the aIndex[] array. Index 0 is an index of all the
111975   ** terms that appear in the document set. Each subsequent index in aIndex[]
111976   ** is an index of prefixes of a specific length.
111977   */
111978   int nIndex;                     /* Size of aIndex[] */
111979   struct Fts3Index {
111980     int nPrefix;                  /* Prefix length (0 for main terms index) */
111981     Fts3Hash hPending;            /* Pending terms table for this index */
111982   } *aIndex;
111983   int nMaxPendingData;            /* Max pending data before flush to disk */
111984   int nPendingData;               /* Current bytes of pending data */
111985   sqlite_int64 iPrevDocid;        /* Docid of most recently inserted document */
111986 
111987 #if defined(SQLITE_DEBUG)
111988   /* State variables used for validating that the transaction control
111989   ** methods of the virtual table are called at appropriate times.  These
111990   ** values do not contribution to the FTS computation; they are used for
111991   ** verifying the SQLite core.
111992   */
111993   int inTransaction;     /* True after xBegin but before xCommit/xRollback */
111994   int mxSavepoint;       /* Largest valid xSavepoint integer */
111995 #endif
111996 };
111997 
111998 /*
111999 ** When the core wants to read from the virtual table, it creates a
112000 ** virtual table cursor (an instance of the following structure) using
112001 ** the xOpen method. Cursors are destroyed using the xClose method.
112002 */
112003 struct Fts3Cursor {
112004   sqlite3_vtab_cursor base;       /* Base class used by SQLite core */
112005   i16 eSearch;                    /* Search strategy (see below) */
112006   u8 isEof;                       /* True if at End Of Results */
112007   u8 isRequireSeek;               /* True if must seek pStmt to %_content row */
112008   sqlite3_stmt *pStmt;            /* Prepared statement in use by the cursor */
112009   Fts3Expr *pExpr;                /* Parsed MATCH query string */
112010   int nPhrase;                    /* Number of matchable phrases in query */
112011   Fts3DeferredToken *pDeferred;   /* Deferred search tokens, if any */
112012   sqlite3_int64 iPrevId;          /* Previous id read from aDoclist */
112013   char *pNextId;                  /* Pointer into the body of aDoclist */
112014   char *aDoclist;                 /* List of docids for full-text queries */
112015   int nDoclist;                   /* Size of buffer at aDoclist */
112016   u8 bDesc;                       /* True to sort in descending order */
112017   int eEvalmode;                  /* An FTS3_EVAL_XX constant */
112018   int nRowAvg;                    /* Average size of database rows, in pages */
112019   sqlite3_int64 nDoc;             /* Documents in table */
112020 
112021   int isMatchinfoNeeded;          /* True when aMatchinfo[] needs filling in */
112022   u32 *aMatchinfo;                /* Information about most recent match */
112023   int nMatchinfo;                 /* Number of elements in aMatchinfo[] */
112024   char *zMatchinfo;               /* Matchinfo specification */
112025 };
112026 
112027 #define FTS3_EVAL_FILTER    0
112028 #define FTS3_EVAL_NEXT      1
112029 #define FTS3_EVAL_MATCHINFO 2
112030 
112031 /*
112032 ** The Fts3Cursor.eSearch member is always set to one of the following.
112033 ** Actualy, Fts3Cursor.eSearch can be greater than or equal to
112034 ** FTS3_FULLTEXT_SEARCH.  If so, then Fts3Cursor.eSearch - 2 is the index
112035 ** of the column to be searched.  For example, in
112036 **
112037 **     CREATE VIRTUAL TABLE ex1 USING fts3(a,b,c,d);
112038 **     SELECT docid FROM ex1 WHERE b MATCH 'one two three';
112039 **
112040 ** Because the LHS of the MATCH operator is 2nd column "b",
112041 ** Fts3Cursor.eSearch will be set to FTS3_FULLTEXT_SEARCH+1.  (+0 for a,
112042 ** +1 for b, +2 for c, +3 for d.)  If the LHS of MATCH were "ex1"
112043 ** indicating that all columns should be searched,
112044 ** then eSearch would be set to FTS3_FULLTEXT_SEARCH+4.
112045 */
112046 #define FTS3_FULLSCAN_SEARCH 0    /* Linear scan of %_content table */
112047 #define FTS3_DOCID_SEARCH    1    /* Lookup by rowid on %_content table */
112048 #define FTS3_FULLTEXT_SEARCH 2    /* Full-text index search */
112049 
112050 
112051 struct Fts3Doclist {
112052   char *aAll;                    /* Array containing doclist (or NULL) */
112053   int nAll;                      /* Size of a[] in bytes */
112054   char *pNextDocid;              /* Pointer to next docid */
112055 
112056   sqlite3_int64 iDocid;          /* Current docid (if pList!=0) */
112057   int bFreeList;                 /* True if pList should be sqlite3_free()d */
112058   char *pList;                   /* Pointer to position list following iDocid */
112059   int nList;                     /* Length of position list */
112060 } doclist;
112061 
112062 /*
112063 ** A "phrase" is a sequence of one or more tokens that must match in
112064 ** sequence.  A single token is the base case and the most common case.
112065 ** For a sequence of tokens contained in double-quotes (i.e. "one two three")
112066 ** nToken will be the number of tokens in the string.
112067 */
112068 struct Fts3PhraseToken {
112069   char *z;                        /* Text of the token */
112070   int n;                          /* Number of bytes in buffer z */
112071   int isPrefix;                   /* True if token ends with a "*" character */
112072 
112073   /* Variables above this point are populated when the expression is
112074   ** parsed (by code in fts3_expr.c). Below this point the variables are
112075   ** used when evaluating the expression. */
112076   Fts3DeferredToken *pDeferred;   /* Deferred token object for this token */
112077   Fts3MultiSegReader *pSegcsr;    /* Segment-reader for this token */
112078 };
112079 
112080 struct Fts3Phrase {
112081   /* Cache of doclist for this phrase. */
112082   Fts3Doclist doclist;
112083   int bIncr;                 /* True if doclist is loaded incrementally */
112084   int iDoclistToken;
112085 
112086   /* Variables below this point are populated by fts3_expr.c when parsing
112087   ** a MATCH expression. Everything above is part of the evaluation phase.
112088   */
112089   int nToken;                /* Number of tokens in the phrase */
112090   int iColumn;               /* Index of column this phrase must match */
112091   Fts3PhraseToken aToken[1]; /* One entry for each token in the phrase */
112092 };
112093 
112094 /*
112095 ** A tree of these objects forms the RHS of a MATCH operator.
112096 **
112097 ** If Fts3Expr.eType is FTSQUERY_PHRASE and isLoaded is true, then aDoclist
112098 ** points to a malloced buffer, size nDoclist bytes, containing the results
112099 ** of this phrase query in FTS3 doclist format. As usual, the initial
112100 ** "Length" field found in doclists stored on disk is omitted from this
112101 ** buffer.
112102 **
112103 ** Variable aMI is used only for FTSQUERY_NEAR nodes to store the global
112104 ** matchinfo data. If it is not NULL, it points to an array of size nCol*3,
112105 ** where nCol is the number of columns in the queried FTS table. The array
112106 ** is populated as follows:
112107 **
112108 **   aMI[iCol*3 + 0] = Undefined
112109 **   aMI[iCol*3 + 1] = Number of occurrences
112110 **   aMI[iCol*3 + 2] = Number of rows containing at least one instance
112111 **
112112 ** The aMI array is allocated using sqlite3_malloc(). It should be freed
112113 ** when the expression node is.
112114 */
112115 struct Fts3Expr {
112116   int eType;                 /* One of the FTSQUERY_XXX values defined below */
112117   int nNear;                 /* Valid if eType==FTSQUERY_NEAR */
112118   Fts3Expr *pParent;         /* pParent->pLeft==this or pParent->pRight==this */
112119   Fts3Expr *pLeft;           /* Left operand */
112120   Fts3Expr *pRight;          /* Right operand */
112121   Fts3Phrase *pPhrase;       /* Valid if eType==FTSQUERY_PHRASE */
112122 
112123   /* The following are used by the fts3_eval.c module. */
112124   sqlite3_int64 iDocid;      /* Current docid */
112125   u8 bEof;                   /* True this expression is at EOF already */
112126   u8 bStart;                 /* True if iDocid is valid */
112127   u8 bDeferred;              /* True if this expression is entirely deferred */
112128 
112129   u32 *aMI;
112130 };
112131 
112132 /*
112133 ** Candidate values for Fts3Query.eType. Note that the order of the first
112134 ** four values is in order of precedence when parsing expressions. For
112135 ** example, the following:
112136 **
112137 **   "a OR b AND c NOT d NEAR e"
112138 **
112139 ** is equivalent to:
112140 **
112141 **   "a OR (b AND (c NOT (d NEAR e)))"
112142 */
112143 #define FTSQUERY_NEAR   1
112144 #define FTSQUERY_NOT    2
112145 #define FTSQUERY_AND    3
112146 #define FTSQUERY_OR     4
112147 #define FTSQUERY_PHRASE 5
112148 
112149 
112150 /* fts3_write.c */
112151 SQLITE_PRIVATE int sqlite3Fts3UpdateMethod(sqlite3_vtab*,int,sqlite3_value**,sqlite3_int64*);
112152 SQLITE_PRIVATE int sqlite3Fts3PendingTermsFlush(Fts3Table *);
112153 SQLITE_PRIVATE void sqlite3Fts3PendingTermsClear(Fts3Table *);
112154 SQLITE_PRIVATE int sqlite3Fts3Optimize(Fts3Table *);
112155 SQLITE_PRIVATE int sqlite3Fts3SegReaderNew(int, sqlite3_int64,
112156   sqlite3_int64, sqlite3_int64, const char *, int, Fts3SegReader**);
112157 SQLITE_PRIVATE int sqlite3Fts3SegReaderPending(
112158   Fts3Table*,int,const char*,int,int,Fts3SegReader**);
112159 SQLITE_PRIVATE void sqlite3Fts3SegReaderFree(Fts3SegReader *);
112160 SQLITE_PRIVATE int sqlite3Fts3AllSegdirs(Fts3Table*, int, int, sqlite3_stmt **);
112161 SQLITE_PRIVATE int sqlite3Fts3ReadLock(Fts3Table *);
112162 SQLITE_PRIVATE int sqlite3Fts3ReadBlock(Fts3Table*, sqlite3_int64, char **, int*, int*);
112163 
112164 SQLITE_PRIVATE int sqlite3Fts3SelectDoctotal(Fts3Table *, sqlite3_stmt **);
112165 SQLITE_PRIVATE int sqlite3Fts3SelectDocsize(Fts3Table *, sqlite3_int64, sqlite3_stmt **);
112166 
112167 SQLITE_PRIVATE void sqlite3Fts3FreeDeferredTokens(Fts3Cursor *);
112168 SQLITE_PRIVATE int sqlite3Fts3DeferToken(Fts3Cursor *, Fts3PhraseToken *, int);
112169 SQLITE_PRIVATE int sqlite3Fts3CacheDeferredDoclists(Fts3Cursor *);
112170 SQLITE_PRIVATE void sqlite3Fts3FreeDeferredDoclists(Fts3Cursor *);
112171 SQLITE_PRIVATE void sqlite3Fts3SegmentsClose(Fts3Table *);
112172 
112173 /* Special values interpreted by sqlite3SegReaderCursor() */
112174 #define FTS3_SEGCURSOR_PENDING        -1
112175 #define FTS3_SEGCURSOR_ALL            -2
112176 
112177 SQLITE_PRIVATE int sqlite3Fts3SegReaderStart(Fts3Table*, Fts3MultiSegReader*, Fts3SegFilter*);
112178 SQLITE_PRIVATE int sqlite3Fts3SegReaderStep(Fts3Table *, Fts3MultiSegReader *);
112179 SQLITE_PRIVATE void sqlite3Fts3SegReaderFinish(Fts3MultiSegReader *);
112180 
112181 SQLITE_PRIVATE int sqlite3Fts3SegReaderCursor(
112182     Fts3Table *, int, int, const char *, int, int, int, Fts3MultiSegReader *);
112183 
112184 /* Flags allowed as part of the 4th argument to SegmentReaderIterate() */
112185 #define FTS3_SEGMENT_REQUIRE_POS   0x00000001
112186 #define FTS3_SEGMENT_IGNORE_EMPTY  0x00000002
112187 #define FTS3_SEGMENT_COLUMN_FILTER 0x00000004
112188 #define FTS3_SEGMENT_PREFIX        0x00000008
112189 #define FTS3_SEGMENT_SCAN          0x00000010
112190 
112191 /* Type passed as 4th argument to SegmentReaderIterate() */
112192 struct Fts3SegFilter {
112193   const char *zTerm;
112194   int nTerm;
112195   int iCol;
112196   int flags;
112197 };
112198 
112199 struct Fts3MultiSegReader {
112200   /* Used internally by sqlite3Fts3SegReaderXXX() calls */
112201   Fts3SegReader **apSegment;      /* Array of Fts3SegReader objects */
112202   int nSegment;                   /* Size of apSegment array */
112203   int nAdvance;                   /* How many seg-readers to advance */
112204   Fts3SegFilter *pFilter;         /* Pointer to filter object */
112205   char *aBuffer;                  /* Buffer to merge doclists in */
112206   int nBuffer;                    /* Allocated size of aBuffer[] in bytes */
112207 
112208   int iColFilter;                 /* If >=0, filter for this column */
112209   int bRestart;
112210 
112211   /* Used by fts3.c only. */
112212   int nCost;                      /* Cost of running iterator */
112213   int bLookup;                    /* True if a lookup of a single entry. */
112214 
112215   /* Output values. Valid only after Fts3SegReaderStep() returns SQLITE_ROW. */
112216   char *zTerm;                    /* Pointer to term buffer */
112217   int nTerm;                      /* Size of zTerm in bytes */
112218   char *aDoclist;                 /* Pointer to doclist buffer */
112219   int nDoclist;                   /* Size of aDoclist[] in bytes */
112220 };
112221 
112222 /* fts3.c */
112223 SQLITE_PRIVATE int sqlite3Fts3PutVarint(char *, sqlite3_int64);
112224 SQLITE_PRIVATE int sqlite3Fts3GetVarint(const char *, sqlite_int64 *);
112225 SQLITE_PRIVATE int sqlite3Fts3GetVarint32(const char *, int *);
112226 SQLITE_PRIVATE int sqlite3Fts3VarintLen(sqlite3_uint64);
112227 SQLITE_PRIVATE void sqlite3Fts3Dequote(char *);
112228 SQLITE_PRIVATE void sqlite3Fts3DoclistPrev(int,char*,int,char**,sqlite3_int64*,int*,u8*);
112229 
112230 SQLITE_PRIVATE int sqlite3Fts3EvalPhraseStats(Fts3Cursor *, Fts3Expr *, u32 *);
112231 
112232 /* fts3_tokenizer.c */
112233 SQLITE_PRIVATE const char *sqlite3Fts3NextToken(const char *, int *);
112234 SQLITE_PRIVATE int sqlite3Fts3InitHashTable(sqlite3 *, Fts3Hash *, const char *);
112235 SQLITE_PRIVATE int sqlite3Fts3InitTokenizer(Fts3Hash *pHash, const char *,
112236     sqlite3_tokenizer **, char **
112237 );
112238 SQLITE_PRIVATE int sqlite3Fts3IsIdChar(char);
112239 
112240 /* fts3_snippet.c */
112241 SQLITE_PRIVATE void sqlite3Fts3Offsets(sqlite3_context*, Fts3Cursor*);
112242 SQLITE_PRIVATE void sqlite3Fts3Snippet(sqlite3_context *, Fts3Cursor *, const char *,
112243   const char *, const char *, int, int
112244 );
112245 SQLITE_PRIVATE void sqlite3Fts3Matchinfo(sqlite3_context *, Fts3Cursor *, const char *);
112246 
112247 /* fts3_expr.c */
112248 SQLITE_PRIVATE int sqlite3Fts3ExprParse(sqlite3_tokenizer *,
112249   char **, int, int, const char *, int, Fts3Expr **
112250 );
112251 SQLITE_PRIVATE void sqlite3Fts3ExprFree(Fts3Expr *);
112252 #ifdef SQLITE_TEST
112253 SQLITE_PRIVATE int sqlite3Fts3ExprInitTestInterface(sqlite3 *db);
112254 SQLITE_PRIVATE int sqlite3Fts3InitTerm(sqlite3 *db);
112255 #endif
112256 
112257 /* fts3_aux.c */
112258 SQLITE_PRIVATE int sqlite3Fts3InitAux(sqlite3 *db);
112259 
112260 SQLITE_PRIVATE int sqlite3Fts3TermSegReaderCursor(
112261   Fts3Cursor *pCsr,               /* Virtual table cursor handle */
112262   const char *zTerm,              /* Term to query for */
112263   int nTerm,                      /* Size of zTerm in bytes */
112264   int isPrefix,                   /* True for a prefix search */
112265   Fts3MultiSegReader **ppSegcsr   /* OUT: Allocated seg-reader cursor */
112266 );
112267 
112268 SQLITE_PRIVATE void sqlite3Fts3EvalPhraseCleanup(Fts3Phrase *);
112269 
112270 SQLITE_PRIVATE int sqlite3Fts3EvalStart(Fts3Cursor *, Fts3Expr *, int);
112271 SQLITE_PRIVATE int sqlite3Fts3EvalNext(Fts3Cursor *pCsr);
112272 
112273 SQLITE_PRIVATE int sqlite3Fts3MsrIncrStart(
112274     Fts3Table*, Fts3MultiSegReader*, int, const char*, int);
112275 SQLITE_PRIVATE int sqlite3Fts3MsrIncrNext(
112276     Fts3Table *, Fts3MultiSegReader *, sqlite3_int64 *, char **, int *);
112277 SQLITE_PRIVATE char *sqlite3Fts3EvalPhrasePoslist(Fts3Cursor *, Fts3Expr *, int iCol);
112278 SQLITE_PRIVATE int sqlite3Fts3MsrOvfl(Fts3Cursor *, Fts3MultiSegReader *, int *);
112279 SQLITE_PRIVATE int sqlite3Fts3MsrIncrRestart(Fts3MultiSegReader *pCsr);
112280 
112281 SQLITE_PRIVATE int sqlite3Fts3DeferredTokenList(Fts3DeferredToken *, char **, int *);
112282 
112283 #endif /* SQLITE_ENABLE_FTS3 */
112284 #endif /* _FTSINT_H */
112285 
112286 /************** End of fts3Int.h *********************************************/
112287 /************** Continuing where we left off in fts3.c ***********************/
112288 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
112289 
112290 #if defined(SQLITE_ENABLE_FTS3) && !defined(SQLITE_CORE)
112291 # define SQLITE_CORE 1
112292 #endif
112293 
112294 
112295 #ifndef SQLITE_CORE
112296   SQLITE_EXTENSION_INIT1
112297 #endif
112298 
112299 /*
112300 ** Write a 64-bit variable-length integer to memory starting at p[0].
112301 ** The length of data written will be between 1 and FTS3_VARINT_MAX bytes.
112302 ** The number of bytes written is returned.
112303 */
112304 SQLITE_PRIVATE int sqlite3Fts3PutVarint(char *p, sqlite_int64 v){
112305   unsigned char *q = (unsigned char *) p;
112306   sqlite_uint64 vu = v;
112307   do{
112308     *q++ = (unsigned char) ((vu & 0x7f) | 0x80);
112309     vu >>= 7;
112310   }while( vu!=0 );
112311   q[-1] &= 0x7f;  /* turn off high bit in final byte */
112312   assert( q - (unsigned char *)p <= FTS3_VARINT_MAX );
112313   return (int) (q - (unsigned char *)p);
112314 }
112315 
112316 /*
112317 ** Read a 64-bit variable-length integer from memory starting at p[0].
112318 ** Return the number of bytes read, or 0 on error.
112319 ** The value is stored in *v.
112320 */
112321 SQLITE_PRIVATE int sqlite3Fts3GetVarint(const char *p, sqlite_int64 *v){
112322   const unsigned char *q = (const unsigned char *) p;
112323   sqlite_uint64 x = 0, y = 1;
112324   while( (*q&0x80)==0x80 && q-(unsigned char *)p<FTS3_VARINT_MAX ){
112325     x += y * (*q++ & 0x7f);
112326     y <<= 7;
112327   }
112328   x += y * (*q++);
112329   *v = (sqlite_int64) x;
112330   return (int) (q - (unsigned char *)p);
112331 }
112332 
112333 /*
112334 ** Similar to sqlite3Fts3GetVarint(), except that the output is truncated to a
112335 ** 32-bit integer before it is returned.
112336 */
112337 SQLITE_PRIVATE int sqlite3Fts3GetVarint32(const char *p, int *pi){
112338  sqlite_int64 i;
112339  int ret = sqlite3Fts3GetVarint(p, &i);
112340  *pi = (int) i;
112341  return ret;
112342 }
112343 
112344 /*
112345 ** Return the number of bytes required to encode v as a varint
112346 */
112347 SQLITE_PRIVATE int sqlite3Fts3VarintLen(sqlite3_uint64 v){
112348   int i = 0;
112349   do{
112350     i++;
112351     v >>= 7;
112352   }while( v!=0 );
112353   return i;
112354 }
112355 
112356 /*
112357 ** Convert an SQL-style quoted string into a normal string by removing
112358 ** the quote characters.  The conversion is done in-place.  If the
112359 ** input does not begin with a quote character, then this routine
112360 ** is a no-op.
112361 **
112362 ** Examples:
112363 **
112364 **     "abc"   becomes   abc
112365 **     'xyz'   becomes   xyz
112366 **     [pqr]   becomes   pqr
112367 **     `mno`   becomes   mno
112368 **
112369 */
112370 SQLITE_PRIVATE void sqlite3Fts3Dequote(char *z){
112371   char quote;                     /* Quote character (if any ) */
112372 
112373   quote = z[0];
112374   if( quote=='[' || quote=='\'' || quote=='"' || quote=='`' ){
112375     int iIn = 1;                  /* Index of next byte to read from input */
112376     int iOut = 0;                 /* Index of next byte to write to output */
112377 
112378     /* If the first byte was a '[', then the close-quote character is a ']' */
112379     if( quote=='[' ) quote = ']';
112380 
112381     while( ALWAYS(z[iIn]) ){
112382       if( z[iIn]==quote ){
112383         if( z[iIn+1]!=quote ) break;
112384         z[iOut++] = quote;
112385         iIn += 2;
112386       }else{
112387         z[iOut++] = z[iIn++];
112388       }
112389     }
112390     z[iOut] = '\0';
112391   }
112392 }
112393 
112394 /*
112395 ** Read a single varint from the doclist at *pp and advance *pp to point
112396 ** to the first byte past the end of the varint.  Add the value of the varint
112397 ** to *pVal.
112398 */
112399 static void fts3GetDeltaVarint(char **pp, sqlite3_int64 *pVal){
112400   sqlite3_int64 iVal;
112401   *pp += sqlite3Fts3GetVarint(*pp, &iVal);
112402   *pVal += iVal;
112403 }
112404 
112405 /*
112406 ** When this function is called, *pp points to the first byte following a
112407 ** varint that is part of a doclist (or position-list, or any other list
112408 ** of varints). This function moves *pp to point to the start of that varint,
112409 ** and sets *pVal by the varint value.
112410 **
112411 ** Argument pStart points to the first byte of the doclist that the
112412 ** varint is part of.
112413 */
112414 static void fts3GetReverseVarint(
112415   char **pp,
112416   char *pStart,
112417   sqlite3_int64 *pVal
112418 ){
112419   sqlite3_int64 iVal;
112420   char *p = *pp;
112421 
112422   /* Pointer p now points at the first byte past the varint we are
112423   ** interested in. So, unless the doclist is corrupt, the 0x80 bit is
112424   ** clear on character p[-1]. */
112425   for(p = (*pp)-2; p>=pStart && *p&0x80; p--);
112426   p++;
112427   *pp = p;
112428 
112429   sqlite3Fts3GetVarint(p, &iVal);
112430   *pVal = iVal;
112431 }
112432 
112433 /*
112434 ** The xDisconnect() virtual table method.
112435 */
112436 static int fts3DisconnectMethod(sqlite3_vtab *pVtab){
112437   Fts3Table *p = (Fts3Table *)pVtab;
112438   int i;
112439 
112440   assert( p->nPendingData==0 );
112441   assert( p->pSegments==0 );
112442 
112443   /* Free any prepared statements held */
112444   for(i=0; i<SizeofArray(p->aStmt); i++){
112445     sqlite3_finalize(p->aStmt[i]);
112446   }
112447   sqlite3_free(p->zSegmentsTbl);
112448   sqlite3_free(p->zReadExprlist);
112449   sqlite3_free(p->zWriteExprlist);
112450 
112451   /* Invoke the tokenizer destructor to free the tokenizer. */
112452   p->pTokenizer->pModule->xDestroy(p->pTokenizer);
112453 
112454   sqlite3_free(p);
112455   return SQLITE_OK;
112456 }
112457 
112458 /*
112459 ** Construct one or more SQL statements from the format string given
112460 ** and then evaluate those statements. The success code is written
112461 ** into *pRc.
112462 **
112463 ** If *pRc is initially non-zero then this routine is a no-op.
112464 */
112465 static void fts3DbExec(
112466   int *pRc,              /* Success code */
112467   sqlite3 *db,           /* Database in which to run SQL */
112468   const char *zFormat,   /* Format string for SQL */
112469   ...                    /* Arguments to the format string */
112470 ){
112471   va_list ap;
112472   char *zSql;
112473   if( *pRc ) return;
112474   va_start(ap, zFormat);
112475   zSql = sqlite3_vmprintf(zFormat, ap);
112476   va_end(ap);
112477   if( zSql==0 ){
112478     *pRc = SQLITE_NOMEM;
112479   }else{
112480     *pRc = sqlite3_exec(db, zSql, 0, 0, 0);
112481     sqlite3_free(zSql);
112482   }
112483 }
112484 
112485 /*
112486 ** The xDestroy() virtual table method.
112487 */
112488 static int fts3DestroyMethod(sqlite3_vtab *pVtab){
112489   int rc = SQLITE_OK;              /* Return code */
112490   Fts3Table *p = (Fts3Table *)pVtab;
112491   sqlite3 *db = p->db;
112492 
112493   /* Drop the shadow tables */
112494   fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_content'", p->zDb, p->zName);
112495   fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_segments'", p->zDb,p->zName);
112496   fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_segdir'", p->zDb, p->zName);
112497   fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_docsize'", p->zDb, p->zName);
112498   fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_stat'", p->zDb, p->zName);
112499 
112500   /* If everything has worked, invoke fts3DisconnectMethod() to free the
112501   ** memory associated with the Fts3Table structure and return SQLITE_OK.
112502   ** Otherwise, return an SQLite error code.
112503   */
112504   return (rc==SQLITE_OK ? fts3DisconnectMethod(pVtab) : rc);
112505 }
112506 
112507 
112508 /*
112509 ** Invoke sqlite3_declare_vtab() to declare the schema for the FTS3 table
112510 ** passed as the first argument. This is done as part of the xConnect()
112511 ** and xCreate() methods.
112512 **
112513 ** If *pRc is non-zero when this function is called, it is a no-op.
112514 ** Otherwise, if an error occurs, an SQLite error code is stored in *pRc
112515 ** before returning.
112516 */
112517 static void fts3DeclareVtab(int *pRc, Fts3Table *p){
112518   if( *pRc==SQLITE_OK ){
112519     int i;                        /* Iterator variable */
112520     int rc;                       /* Return code */
112521     char *zSql;                   /* SQL statement passed to declare_vtab() */
112522     char *zCols;                  /* List of user defined columns */
112523 
112524     sqlite3_vtab_config(p->db, SQLITE_VTAB_CONSTRAINT_SUPPORT, 1);
112525 
112526     /* Create a list of user columns for the virtual table */
112527     zCols = sqlite3_mprintf("%Q, ", p->azColumn[0]);
112528     for(i=1; zCols && i<p->nColumn; i++){
112529       zCols = sqlite3_mprintf("%z%Q, ", zCols, p->azColumn[i]);
112530     }
112531 
112532     /* Create the whole "CREATE TABLE" statement to pass to SQLite */
112533     zSql = sqlite3_mprintf(
112534         "CREATE TABLE x(%s %Q HIDDEN, docid HIDDEN)", zCols, p->zName
112535     );
112536     if( !zCols || !zSql ){
112537       rc = SQLITE_NOMEM;
112538     }else{
112539       rc = sqlite3_declare_vtab(p->db, zSql);
112540     }
112541 
112542     sqlite3_free(zSql);
112543     sqlite3_free(zCols);
112544     *pRc = rc;
112545   }
112546 }
112547 
112548 /*
112549 ** Create the backing store tables (%_content, %_segments and %_segdir)
112550 ** required by the FTS3 table passed as the only argument. This is done
112551 ** as part of the vtab xCreate() method.
112552 **
112553 ** If the p->bHasDocsize boolean is true (indicating that this is an
112554 ** FTS4 table, not an FTS3 table) then also create the %_docsize and
112555 ** %_stat tables required by FTS4.
112556 */
112557 static int fts3CreateTables(Fts3Table *p){
112558   int rc = SQLITE_OK;             /* Return code */
112559   int i;                          /* Iterator variable */
112560   char *zContentCols;             /* Columns of %_content table */
112561   sqlite3 *db = p->db;            /* The database connection */
112562 
112563   /* Create a list of user columns for the content table */
112564   zContentCols = sqlite3_mprintf("docid INTEGER PRIMARY KEY");
112565   for(i=0; zContentCols && i<p->nColumn; i++){
112566     char *z = p->azColumn[i];
112567     zContentCols = sqlite3_mprintf("%z, 'c%d%q'", zContentCols, i, z);
112568   }
112569   if( zContentCols==0 ) rc = SQLITE_NOMEM;
112570 
112571   /* Create the content table */
112572   fts3DbExec(&rc, db,
112573      "CREATE TABLE %Q.'%q_content'(%s)",
112574      p->zDb, p->zName, zContentCols
112575   );
112576   sqlite3_free(zContentCols);
112577   /* Create other tables */
112578   fts3DbExec(&rc, db,
112579       "CREATE TABLE %Q.'%q_segments'(blockid INTEGER PRIMARY KEY, block BLOB);",
112580       p->zDb, p->zName
112581   );
112582   fts3DbExec(&rc, db,
112583       "CREATE TABLE %Q.'%q_segdir'("
112584         "level INTEGER,"
112585         "idx INTEGER,"
112586         "start_block INTEGER,"
112587         "leaves_end_block INTEGER,"
112588         "end_block INTEGER,"
112589         "root BLOB,"
112590         "PRIMARY KEY(level, idx)"
112591       ");",
112592       p->zDb, p->zName
112593   );
112594   if( p->bHasDocsize ){
112595     fts3DbExec(&rc, db,
112596         "CREATE TABLE %Q.'%q_docsize'(docid INTEGER PRIMARY KEY, size BLOB);",
112597         p->zDb, p->zName
112598     );
112599   }
112600   if( p->bHasStat ){
112601     fts3DbExec(&rc, db,
112602         "CREATE TABLE %Q.'%q_stat'(id INTEGER PRIMARY KEY, value BLOB);",
112603         p->zDb, p->zName
112604     );
112605   }
112606   return rc;
112607 }
112608 
112609 /*
112610 ** Store the current database page-size in bytes in p->nPgsz.
112611 **
112612 ** If *pRc is non-zero when this function is called, it is a no-op.
112613 ** Otherwise, if an error occurs, an SQLite error code is stored in *pRc
112614 ** before returning.
112615 */
112616 static void fts3DatabasePageSize(int *pRc, Fts3Table *p){
112617   if( *pRc==SQLITE_OK ){
112618     int rc;                       /* Return code */
112619     char *zSql;                   /* SQL text "PRAGMA %Q.page_size" */
112620     sqlite3_stmt *pStmt;          /* Compiled "PRAGMA %Q.page_size" statement */
112621 
112622     zSql = sqlite3_mprintf("PRAGMA %Q.page_size", p->zDb);
112623     if( !zSql ){
112624       rc = SQLITE_NOMEM;
112625     }else{
112626       rc = sqlite3_prepare(p->db, zSql, -1, &pStmt, 0);
112627       if( rc==SQLITE_OK ){
112628         sqlite3_step(pStmt);
112629         p->nPgsz = sqlite3_column_int(pStmt, 0);
112630         rc = sqlite3_finalize(pStmt);
112631       }else if( rc==SQLITE_AUTH ){
112632         p->nPgsz = 1024;
112633         rc = SQLITE_OK;
112634       }
112635     }
112636     assert( p->nPgsz>0 || rc!=SQLITE_OK );
112637     sqlite3_free(zSql);
112638     *pRc = rc;
112639   }
112640 }
112641 
112642 /*
112643 ** "Special" FTS4 arguments are column specifications of the following form:
112644 **
112645 **   <key> = <value>
112646 **
112647 ** There may not be whitespace surrounding the "=" character. The <value>
112648 ** term may be quoted, but the <key> may not.
112649 */
112650 static int fts3IsSpecialColumn(
112651   const char *z,
112652   int *pnKey,
112653   char **pzValue
112654 ){
112655   char *zValue;
112656   const char *zCsr = z;
112657 
112658   while( *zCsr!='=' ){
112659     if( *zCsr=='\0' ) return 0;
112660     zCsr++;
112661   }
112662 
112663   *pnKey = (int)(zCsr-z);
112664   zValue = sqlite3_mprintf("%s", &zCsr[1]);
112665   if( zValue ){
112666     sqlite3Fts3Dequote(zValue);
112667   }
112668   *pzValue = zValue;
112669   return 1;
112670 }
112671 
112672 /*
112673 ** Append the output of a printf() style formatting to an existing string.
112674 */
112675 static void fts3Appendf(
112676   int *pRc,                       /* IN/OUT: Error code */
112677   char **pz,                      /* IN/OUT: Pointer to string buffer */
112678   const char *zFormat,            /* Printf format string to append */
112679   ...                             /* Arguments for printf format string */
112680 ){
112681   if( *pRc==SQLITE_OK ){
112682     va_list ap;
112683     char *z;
112684     va_start(ap, zFormat);
112685     z = sqlite3_vmprintf(zFormat, ap);
112686     if( z && *pz ){
112687       char *z2 = sqlite3_mprintf("%s%s", *pz, z);
112688       sqlite3_free(z);
112689       z = z2;
112690     }
112691     if( z==0 ) *pRc = SQLITE_NOMEM;
112692     va_end(ap);
112693     sqlite3_free(*pz);
112694     *pz = z;
112695   }
112696 }
112697 
112698 /*
112699 ** Return a copy of input string zInput enclosed in double-quotes (") and
112700 ** with all double quote characters escaped. For example:
112701 **
112702 **     fts3QuoteId("un \"zip\"")   ->    "un \"\"zip\"\""
112703 **
112704 ** The pointer returned points to memory obtained from sqlite3_malloc(). It
112705 ** is the callers responsibility to call sqlite3_free() to release this
112706 ** memory.
112707 */
112708 static char *fts3QuoteId(char const *zInput){
112709   int nRet;
112710   char *zRet;
112711   nRet = 2 + strlen(zInput)*2 + 1;
112712   zRet = sqlite3_malloc(nRet);
112713   if( zRet ){
112714     int i;
112715     char *z = zRet;
112716     *(z++) = '"';
112717     for(i=0; zInput[i]; i++){
112718       if( zInput[i]=='"' ) *(z++) = '"';
112719       *(z++) = zInput[i];
112720     }
112721     *(z++) = '"';
112722     *(z++) = '\0';
112723   }
112724   return zRet;
112725 }
112726 
112727 /*
112728 ** Return a list of comma separated SQL expressions that could be used
112729 ** in a SELECT statement such as the following:
112730 **
112731 **     SELECT <list of expressions> FROM %_content AS x ...
112732 **
112733 ** to return the docid, followed by each column of text data in order
112734 ** from left to write. If parameter zFunc is not NULL, then instead of
112735 ** being returned directly each column of text data is passed to an SQL
112736 ** function named zFunc first. For example, if zFunc is "unzip" and the
112737 ** table has the three user-defined columns "a", "b", and "c", the following
112738 ** string is returned:
112739 **
112740 **     "docid, unzip(x.'a'), unzip(x.'b'), unzip(x.'c')"
112741 **
112742 ** The pointer returned points to a buffer allocated by sqlite3_malloc(). It
112743 ** is the responsibility of the caller to eventually free it.
112744 **
112745 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op (and
112746 ** a NULL pointer is returned). Otherwise, if an OOM error is encountered
112747 ** by this function, NULL is returned and *pRc is set to SQLITE_NOMEM. If
112748 ** no error occurs, *pRc is left unmodified.
112749 */
112750 static char *fts3ReadExprList(Fts3Table *p, const char *zFunc, int *pRc){
112751   char *zRet = 0;
112752   char *zFree = 0;
112753   char *zFunction;
112754   int i;
112755 
112756   if( !zFunc ){
112757     zFunction = "";
112758   }else{
112759     zFree = zFunction = fts3QuoteId(zFunc);
112760   }
112761   fts3Appendf(pRc, &zRet, "docid");
112762   for(i=0; i<p->nColumn; i++){
112763     fts3Appendf(pRc, &zRet, ",%s(x.'c%d%q')", zFunction, i, p->azColumn[i]);
112764   }
112765   sqlite3_free(zFree);
112766   return zRet;
112767 }
112768 
112769 /*
112770 ** Return a list of N comma separated question marks, where N is the number
112771 ** of columns in the %_content table (one for the docid plus one for each
112772 ** user-defined text column).
112773 **
112774 ** If argument zFunc is not NULL, then all but the first question mark
112775 ** is preceded by zFunc and an open bracket, and followed by a closed
112776 ** bracket. For example, if zFunc is "zip" and the FTS3 table has three
112777 ** user-defined text columns, the following string is returned:
112778 **
112779 **     "?, zip(?), zip(?), zip(?)"
112780 **
112781 ** The pointer returned points to a buffer allocated by sqlite3_malloc(). It
112782 ** is the responsibility of the caller to eventually free it.
112783 **
112784 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op (and
112785 ** a NULL pointer is returned). Otherwise, if an OOM error is encountered
112786 ** by this function, NULL is returned and *pRc is set to SQLITE_NOMEM. If
112787 ** no error occurs, *pRc is left unmodified.
112788 */
112789 static char *fts3WriteExprList(Fts3Table *p, const char *zFunc, int *pRc){
112790   char *zRet = 0;
112791   char *zFree = 0;
112792   char *zFunction;
112793   int i;
112794 
112795   if( !zFunc ){
112796     zFunction = "";
112797   }else{
112798     zFree = zFunction = fts3QuoteId(zFunc);
112799   }
112800   fts3Appendf(pRc, &zRet, "?");
112801   for(i=0; i<p->nColumn; i++){
112802     fts3Appendf(pRc, &zRet, ",%s(?)", zFunction);
112803   }
112804   sqlite3_free(zFree);
112805   return zRet;
112806 }
112807 
112808 static int fts3GobbleInt(const char **pp, int *pnOut){
112809   const char *p = *pp;
112810   int nInt = 0;
112811   for(p=*pp; p[0]>='0' && p[0]<='9'; p++){
112812     nInt = nInt * 10 + (p[0] - '0');
112813   }
112814   if( p==*pp ) return SQLITE_ERROR;
112815   *pnOut = nInt;
112816   *pp = p;
112817   return SQLITE_OK;
112818 }
112819 
112820 
112821 static int fts3PrefixParameter(
112822   const char *zParam,             /* ABC in prefix=ABC parameter to parse */
112823   int *pnIndex,                   /* OUT: size of *apIndex[] array */
112824   struct Fts3Index **apIndex,     /* OUT: Array of indexes for this table */
112825   struct Fts3Index **apFree       /* OUT: Free this with sqlite3_free() */
112826 ){
112827   struct Fts3Index *aIndex;
112828   int nIndex = 1;
112829 
112830   if( zParam && zParam[0] ){
112831     const char *p;
112832     nIndex++;
112833     for(p=zParam; *p; p++){
112834       if( *p==',' ) nIndex++;
112835     }
112836   }
112837 
112838   aIndex = sqlite3_malloc(sizeof(struct Fts3Index) * nIndex);
112839   *apIndex = *apFree = aIndex;
112840   *pnIndex = nIndex;
112841   if( !aIndex ){
112842     return SQLITE_NOMEM;
112843   }
112844 
112845   memset(aIndex, 0, sizeof(struct Fts3Index) * nIndex);
112846   if( zParam ){
112847     const char *p = zParam;
112848     int i;
112849     for(i=1; i<nIndex; i++){
112850       int nPrefix;
112851       if( fts3GobbleInt(&p, &nPrefix) ) return SQLITE_ERROR;
112852       aIndex[i].nPrefix = nPrefix;
112853       p++;
112854     }
112855   }
112856 
112857   return SQLITE_OK;
112858 }
112859 
112860 /*
112861 ** This function is the implementation of both the xConnect and xCreate
112862 ** methods of the FTS3 virtual table.
112863 **
112864 ** The argv[] array contains the following:
112865 **
112866 **   argv[0]   -> module name  ("fts3" or "fts4")
112867 **   argv[1]   -> database name
112868 **   argv[2]   -> table name
112869 **   argv[...] -> "column name" and other module argument fields.
112870 */
112871 static int fts3InitVtab(
112872   int isCreate,                   /* True for xCreate, false for xConnect */
112873   sqlite3 *db,                    /* The SQLite database connection */
112874   void *pAux,                     /* Hash table containing tokenizers */
112875   int argc,                       /* Number of elements in argv array */
112876   const char * const *argv,       /* xCreate/xConnect argument array */
112877   sqlite3_vtab **ppVTab,          /* Write the resulting vtab structure here */
112878   char **pzErr                    /* Write any error message here */
112879 ){
112880   Fts3Hash *pHash = (Fts3Hash *)pAux;
112881   Fts3Table *p = 0;               /* Pointer to allocated vtab */
112882   int rc = SQLITE_OK;             /* Return code */
112883   int i;                          /* Iterator variable */
112884   int nByte;                      /* Size of allocation used for *p */
112885   int iCol;                       /* Column index */
112886   int nString = 0;                /* Bytes required to hold all column names */
112887   int nCol = 0;                   /* Number of columns in the FTS table */
112888   char *zCsr;                     /* Space for holding column names */
112889   int nDb;                        /* Bytes required to hold database name */
112890   int nName;                      /* Bytes required to hold table name */
112891   int isFts4 = (argv[0][3]=='4'); /* True for FTS4, false for FTS3 */
112892   const char **aCol;              /* Array of column names */
112893   sqlite3_tokenizer *pTokenizer = 0;        /* Tokenizer for this table */
112894 
112895   int nIndex;                     /* Size of aIndex[] array */
112896   struct Fts3Index *aIndex;       /* Array of indexes for this table */
112897   struct Fts3Index *aFree = 0;    /* Free this before returning */
112898 
112899   /* The results of parsing supported FTS4 key=value options: */
112900   int bNoDocsize = 0;             /* True to omit %_docsize table */
112901   int bDescIdx = 0;               /* True to store descending indexes */
112902   char *zPrefix = 0;              /* Prefix parameter value (or NULL) */
112903   char *zCompress = 0;            /* compress=? parameter (or NULL) */
112904   char *zUncompress = 0;          /* uncompress=? parameter (or NULL) */
112905 
112906   assert( strlen(argv[0])==4 );
112907   assert( (sqlite3_strnicmp(argv[0], "fts4", 4)==0 && isFts4)
112908        || (sqlite3_strnicmp(argv[0], "fts3", 4)==0 && !isFts4)
112909   );
112910 
112911   nDb = (int)strlen(argv[1]) + 1;
112912   nName = (int)strlen(argv[2]) + 1;
112913 
112914   aCol = (const char **)sqlite3_malloc(sizeof(const char *) * (argc-2) );
112915   if( !aCol ) return SQLITE_NOMEM;
112916   memset((void *)aCol, 0, sizeof(const char *) * (argc-2));
112917 
112918   /* Loop through all of the arguments passed by the user to the FTS3/4
112919   ** module (i.e. all the column names and special arguments). This loop
112920   ** does the following:
112921   **
112922   **   + Figures out the number of columns the FTSX table will have, and
112923   **     the number of bytes of space that must be allocated to store copies
112924   **     of the column names.
112925   **
112926   **   + If there is a tokenizer specification included in the arguments,
112927   **     initializes the tokenizer pTokenizer.
112928   */
112929   for(i=3; rc==SQLITE_OK && i<argc; i++){
112930     char const *z = argv[i];
112931     int nKey;
112932     char *zVal;
112933 
112934     /* Check if this is a tokenizer specification */
112935     if( !pTokenizer
112936      && strlen(z)>8
112937      && 0==sqlite3_strnicmp(z, "tokenize", 8)
112938      && 0==sqlite3Fts3IsIdChar(z[8])
112939     ){
112940       rc = sqlite3Fts3InitTokenizer(pHash, &z[9], &pTokenizer, pzErr);
112941     }
112942 
112943     /* Check if it is an FTS4 special argument. */
112944     else if( isFts4 && fts3IsSpecialColumn(z, &nKey, &zVal) ){
112945       struct Fts4Option {
112946         const char *zOpt;
112947         int nOpt;
112948         char **pzVar;
112949       } aFts4Opt[] = {
112950         { "matchinfo",   9, 0 },            /* 0 -> MATCHINFO */
112951         { "prefix",      6, 0 },            /* 1 -> PREFIX */
112952         { "compress",    8, 0 },            /* 2 -> COMPRESS */
112953         { "uncompress", 10, 0 },            /* 3 -> UNCOMPRESS */
112954         { "order",       5, 0 }             /* 4 -> ORDER */
112955       };
112956 
112957       int iOpt;
112958       if( !zVal ){
112959         rc = SQLITE_NOMEM;
112960       }else{
112961         for(iOpt=0; iOpt<SizeofArray(aFts4Opt); iOpt++){
112962           struct Fts4Option *pOp = &aFts4Opt[iOpt];
112963           if( nKey==pOp->nOpt && !sqlite3_strnicmp(z, pOp->zOpt, pOp->nOpt) ){
112964             break;
112965           }
112966         }
112967         if( iOpt==SizeofArray(aFts4Opt) ){
112968           *pzErr = sqlite3_mprintf("unrecognized parameter: %s", z);
112969           rc = SQLITE_ERROR;
112970         }else{
112971           switch( iOpt ){
112972             case 0:               /* MATCHINFO */
112973               if( strlen(zVal)!=4 || sqlite3_strnicmp(zVal, "fts3", 4) ){
112974                 *pzErr = sqlite3_mprintf("unrecognized matchinfo: %s", zVal);
112975                 rc = SQLITE_ERROR;
112976               }
112977               bNoDocsize = 1;
112978               break;
112979 
112980             case 1:               /* PREFIX */
112981               sqlite3_free(zPrefix);
112982               zPrefix = zVal;
112983               zVal = 0;
112984               break;
112985 
112986             case 2:               /* COMPRESS */
112987               sqlite3_free(zCompress);
112988               zCompress = zVal;
112989               zVal = 0;
112990               break;
112991 
112992             case 3:               /* UNCOMPRESS */
112993               sqlite3_free(zUncompress);
112994               zUncompress = zVal;
112995               zVal = 0;
112996               break;
112997 
112998             case 4:               /* ORDER */
112999               if( (strlen(zVal)!=3 || sqlite3_strnicmp(zVal, "asc", 3))
113000                && (strlen(zVal)!=4 || sqlite3_strnicmp(zVal, "desc", 3))
113001               ){
113002                 *pzErr = sqlite3_mprintf("unrecognized order: %s", zVal);
113003                 rc = SQLITE_ERROR;
113004               }
113005               bDescIdx = (zVal[0]=='d' || zVal[0]=='D');
113006               break;
113007           }
113008         }
113009         sqlite3_free(zVal);
113010       }
113011     }
113012 
113013     /* Otherwise, the argument is a column name. */
113014     else {
113015       nString += (int)(strlen(z) + 1);
113016       aCol[nCol++] = z;
113017     }
113018   }
113019   if( rc!=SQLITE_OK ) goto fts3_init_out;
113020 
113021   if( nCol==0 ){
113022     assert( nString==0 );
113023     aCol[0] = "content";
113024     nString = 8;
113025     nCol = 1;
113026   }
113027 
113028   if( pTokenizer==0 ){
113029     rc = sqlite3Fts3InitTokenizer(pHash, "simple", &pTokenizer, pzErr);
113030     if( rc!=SQLITE_OK ) goto fts3_init_out;
113031   }
113032   assert( pTokenizer );
113033 
113034   rc = fts3PrefixParameter(zPrefix, &nIndex, &aIndex, &aFree);
113035   if( rc==SQLITE_ERROR ){
113036     assert( zPrefix );
113037     *pzErr = sqlite3_mprintf("error parsing prefix parameter: %s", zPrefix);
113038   }
113039   if( rc!=SQLITE_OK ) goto fts3_init_out;
113040 
113041   /* Allocate and populate the Fts3Table structure. */
113042   nByte = sizeof(Fts3Table) +                  /* Fts3Table */
113043           nCol * sizeof(char *) +              /* azColumn */
113044           nIndex * sizeof(struct Fts3Index) +  /* aIndex */
113045           nName +                              /* zName */
113046           nDb +                                /* zDb */
113047           nString;                             /* Space for azColumn strings */
113048   p = (Fts3Table*)sqlite3_malloc(nByte);
113049   if( p==0 ){
113050     rc = SQLITE_NOMEM;
113051     goto fts3_init_out;
113052   }
113053   memset(p, 0, nByte);
113054   p->db = db;
113055   p->nColumn = nCol;
113056   p->nPendingData = 0;
113057   p->azColumn = (char **)&p[1];
113058   p->pTokenizer = pTokenizer;
113059   p->nMaxPendingData = FTS3_MAX_PENDING_DATA;
113060   p->bHasDocsize = (isFts4 && bNoDocsize==0);
113061   p->bHasStat = isFts4;
113062   p->bDescIdx = bDescIdx;
113063   TESTONLY( p->inTransaction = -1 );
113064   TESTONLY( p->mxSavepoint = -1 );
113065 
113066   p->aIndex = (struct Fts3Index *)&p->azColumn[nCol];
113067   memcpy(p->aIndex, aIndex, sizeof(struct Fts3Index) * nIndex);
113068   p->nIndex = nIndex;
113069   for(i=0; i<nIndex; i++){
113070     fts3HashInit(&p->aIndex[i].hPending, FTS3_HASH_STRING, 1);
113071   }
113072 
113073   /* Fill in the zName and zDb fields of the vtab structure. */
113074   zCsr = (char *)&p->aIndex[nIndex];
113075   p->zName = zCsr;
113076   memcpy(zCsr, argv[2], nName);
113077   zCsr += nName;
113078   p->zDb = zCsr;
113079   memcpy(zCsr, argv[1], nDb);
113080   zCsr += nDb;
113081 
113082   /* Fill in the azColumn array */
113083   for(iCol=0; iCol<nCol; iCol++){
113084     char *z;
113085     int n = 0;
113086     z = (char *)sqlite3Fts3NextToken(aCol[iCol], &n);
113087     memcpy(zCsr, z, n);
113088     zCsr[n] = '\0';
113089     sqlite3Fts3Dequote(zCsr);
113090     p->azColumn[iCol] = zCsr;
113091     zCsr += n+1;
113092     assert( zCsr <= &((char *)p)[nByte] );
113093   }
113094 
113095   if( (zCompress==0)!=(zUncompress==0) ){
113096     char const *zMiss = (zCompress==0 ? "compress" : "uncompress");
113097     rc = SQLITE_ERROR;
113098     *pzErr = sqlite3_mprintf("missing %s parameter in fts4 constructor", zMiss);
113099   }
113100   p->zReadExprlist = fts3ReadExprList(p, zUncompress, &rc);
113101   p->zWriteExprlist = fts3WriteExprList(p, zCompress, &rc);
113102   if( rc!=SQLITE_OK ) goto fts3_init_out;
113103 
113104   /* If this is an xCreate call, create the underlying tables in the
113105   ** database. TODO: For xConnect(), it could verify that said tables exist.
113106   */
113107   if( isCreate ){
113108     rc = fts3CreateTables(p);
113109   }
113110 
113111   /* Figure out the page-size for the database. This is required in order to
113112   ** estimate the cost of loading large doclists from the database.  */
113113   fts3DatabasePageSize(&rc, p);
113114   p->nNodeSize = p->nPgsz-35;
113115 
113116   /* Declare the table schema to SQLite. */
113117   fts3DeclareVtab(&rc, p);
113118 
113119 fts3_init_out:
113120   sqlite3_free(zPrefix);
113121   sqlite3_free(aFree);
113122   sqlite3_free(zCompress);
113123   sqlite3_free(zUncompress);
113124   sqlite3_free((void *)aCol);
113125   if( rc!=SQLITE_OK ){
113126     if( p ){
113127       fts3DisconnectMethod((sqlite3_vtab *)p);
113128     }else if( pTokenizer ){
113129       pTokenizer->pModule->xDestroy(pTokenizer);
113130     }
113131   }else{
113132     assert( p->pSegments==0 );
113133     *ppVTab = &p->base;
113134   }
113135   return rc;
113136 }
113137 
113138 /*
113139 ** The xConnect() and xCreate() methods for the virtual table. All the
113140 ** work is done in function fts3InitVtab().
113141 */
113142 static int fts3ConnectMethod(
113143   sqlite3 *db,                    /* Database connection */
113144   void *pAux,                     /* Pointer to tokenizer hash table */
113145   int argc,                       /* Number of elements in argv array */
113146   const char * const *argv,       /* xCreate/xConnect argument array */
113147   sqlite3_vtab **ppVtab,          /* OUT: New sqlite3_vtab object */
113148   char **pzErr                    /* OUT: sqlite3_malloc'd error message */
113149 ){
113150   return fts3InitVtab(0, db, pAux, argc, argv, ppVtab, pzErr);
113151 }
113152 static int fts3CreateMethod(
113153   sqlite3 *db,                    /* Database connection */
113154   void *pAux,                     /* Pointer to tokenizer hash table */
113155   int argc,                       /* Number of elements in argv array */
113156   const char * const *argv,       /* xCreate/xConnect argument array */
113157   sqlite3_vtab **ppVtab,          /* OUT: New sqlite3_vtab object */
113158   char **pzErr                    /* OUT: sqlite3_malloc'd error message */
113159 ){
113160   return fts3InitVtab(1, db, pAux, argc, argv, ppVtab, pzErr);
113161 }
113162 
113163 /*
113164 ** Implementation of the xBestIndex method for FTS3 tables. There
113165 ** are three possible strategies, in order of preference:
113166 **
113167 **   1. Direct lookup by rowid or docid.
113168 **   2. Full-text search using a MATCH operator on a non-docid column.
113169 **   3. Linear scan of %_content table.
113170 */
113171 static int fts3BestIndexMethod(sqlite3_vtab *pVTab, sqlite3_index_info *pInfo){
113172   Fts3Table *p = (Fts3Table *)pVTab;
113173   int i;                          /* Iterator variable */
113174   int iCons = -1;                 /* Index of constraint to use */
113175 
113176   /* By default use a full table scan. This is an expensive option,
113177   ** so search through the constraints to see if a more efficient
113178   ** strategy is possible.
113179   */
113180   pInfo->idxNum = FTS3_FULLSCAN_SEARCH;
113181   pInfo->estimatedCost = 500000;
113182   for(i=0; i<pInfo->nConstraint; i++){
113183     struct sqlite3_index_constraint *pCons = &pInfo->aConstraint[i];
113184     if( pCons->usable==0 ) continue;
113185 
113186     /* A direct lookup on the rowid or docid column. Assign a cost of 1.0. */
113187     if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ
113188      && (pCons->iColumn<0 || pCons->iColumn==p->nColumn+1 )
113189     ){
113190       pInfo->idxNum = FTS3_DOCID_SEARCH;
113191       pInfo->estimatedCost = 1.0;
113192       iCons = i;
113193     }
113194 
113195     /* A MATCH constraint. Use a full-text search.
113196     **
113197     ** If there is more than one MATCH constraint available, use the first
113198     ** one encountered. If there is both a MATCH constraint and a direct
113199     ** rowid/docid lookup, prefer the MATCH strategy. This is done even
113200     ** though the rowid/docid lookup is faster than a MATCH query, selecting
113201     ** it would lead to an "unable to use function MATCH in the requested
113202     ** context" error.
113203     */
113204     if( pCons->op==SQLITE_INDEX_CONSTRAINT_MATCH
113205      && pCons->iColumn>=0 && pCons->iColumn<=p->nColumn
113206     ){
113207       pInfo->idxNum = FTS3_FULLTEXT_SEARCH + pCons->iColumn;
113208       pInfo->estimatedCost = 2.0;
113209       iCons = i;
113210       break;
113211     }
113212   }
113213 
113214   if( iCons>=0 ){
113215     pInfo->aConstraintUsage[iCons].argvIndex = 1;
113216     pInfo->aConstraintUsage[iCons].omit = 1;
113217   }
113218 
113219   /* Regardless of the strategy selected, FTS can deliver rows in rowid (or
113220   ** docid) order. Both ascending and descending are possible.
113221   */
113222   if( pInfo->nOrderBy==1 ){
113223     struct sqlite3_index_orderby *pOrder = &pInfo->aOrderBy[0];
113224     if( pOrder->iColumn<0 || pOrder->iColumn==p->nColumn+1 ){
113225       if( pOrder->desc ){
113226         pInfo->idxStr = "DESC";
113227       }else{
113228         pInfo->idxStr = "ASC";
113229       }
113230       pInfo->orderByConsumed = 1;
113231     }
113232   }
113233 
113234   assert( p->pSegments==0 );
113235   return SQLITE_OK;
113236 }
113237 
113238 /*
113239 ** Implementation of xOpen method.
113240 */
113241 static int fts3OpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){
113242   sqlite3_vtab_cursor *pCsr;               /* Allocated cursor */
113243 
113244   UNUSED_PARAMETER(pVTab);
113245 
113246   /* Allocate a buffer large enough for an Fts3Cursor structure. If the
113247   ** allocation succeeds, zero it and return SQLITE_OK. Otherwise,
113248   ** if the allocation fails, return SQLITE_NOMEM.
113249   */
113250   *ppCsr = pCsr = (sqlite3_vtab_cursor *)sqlite3_malloc(sizeof(Fts3Cursor));
113251   if( !pCsr ){
113252     return SQLITE_NOMEM;
113253   }
113254   memset(pCsr, 0, sizeof(Fts3Cursor));
113255   return SQLITE_OK;
113256 }
113257 
113258 /*
113259 ** Close the cursor.  For additional information see the documentation
113260 ** on the xClose method of the virtual table interface.
113261 */
113262 static int fts3CloseMethod(sqlite3_vtab_cursor *pCursor){
113263   Fts3Cursor *pCsr = (Fts3Cursor *)pCursor;
113264   assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
113265   sqlite3_finalize(pCsr->pStmt);
113266   sqlite3Fts3ExprFree(pCsr->pExpr);
113267   sqlite3Fts3FreeDeferredTokens(pCsr);
113268   sqlite3_free(pCsr->aDoclist);
113269   sqlite3_free(pCsr->aMatchinfo);
113270   assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
113271   sqlite3_free(pCsr);
113272   return SQLITE_OK;
113273 }
113274 
113275 /*
113276 ** Position the pCsr->pStmt statement so that it is on the row
113277 ** of the %_content table that contains the last match.  Return
113278 ** SQLITE_OK on success.
113279 */
113280 static int fts3CursorSeek(sqlite3_context *pContext, Fts3Cursor *pCsr){
113281   if( pCsr->isRequireSeek ){
113282     sqlite3_bind_int64(pCsr->pStmt, 1, pCsr->iPrevId);
113283     pCsr->isRequireSeek = 0;
113284     if( SQLITE_ROW==sqlite3_step(pCsr->pStmt) ){
113285       return SQLITE_OK;
113286     }else{
113287       int rc = sqlite3_reset(pCsr->pStmt);
113288       if( rc==SQLITE_OK ){
113289         /* If no row was found and no error has occured, then the %_content
113290         ** table is missing a row that is present in the full-text index.
113291         ** The data structures are corrupt.
113292         */
113293         rc = SQLITE_CORRUPT_VTAB;
113294       }
113295       pCsr->isEof = 1;
113296       if( pContext ){
113297         sqlite3_result_error_code(pContext, rc);
113298       }
113299       return rc;
113300     }
113301   }else{
113302     return SQLITE_OK;
113303   }
113304 }
113305 
113306 /*
113307 ** This function is used to process a single interior node when searching
113308 ** a b-tree for a term or term prefix. The node data is passed to this
113309 ** function via the zNode/nNode parameters. The term to search for is
113310 ** passed in zTerm/nTerm.
113311 **
113312 ** If piFirst is not NULL, then this function sets *piFirst to the blockid
113313 ** of the child node that heads the sub-tree that may contain the term.
113314 **
113315 ** If piLast is not NULL, then *piLast is set to the right-most child node
113316 ** that heads a sub-tree that may contain a term for which zTerm/nTerm is
113317 ** a prefix.
113318 **
113319 ** If an OOM error occurs, SQLITE_NOMEM is returned. Otherwise, SQLITE_OK.
113320 */
113321 static int fts3ScanInteriorNode(
113322   const char *zTerm,              /* Term to select leaves for */
113323   int nTerm,                      /* Size of term zTerm in bytes */
113324   const char *zNode,              /* Buffer containing segment interior node */
113325   int nNode,                      /* Size of buffer at zNode */
113326   sqlite3_int64 *piFirst,         /* OUT: Selected child node */
113327   sqlite3_int64 *piLast           /* OUT: Selected child node */
113328 ){
113329   int rc = SQLITE_OK;             /* Return code */
113330   const char *zCsr = zNode;       /* Cursor to iterate through node */
113331   const char *zEnd = &zCsr[nNode];/* End of interior node buffer */
113332   char *zBuffer = 0;              /* Buffer to load terms into */
113333   int nAlloc = 0;                 /* Size of allocated buffer */
113334   int isFirstTerm = 1;            /* True when processing first term on page */
113335   sqlite3_int64 iChild;           /* Block id of child node to descend to */
113336 
113337   /* Skip over the 'height' varint that occurs at the start of every
113338   ** interior node. Then load the blockid of the left-child of the b-tree
113339   ** node into variable iChild.
113340   **
113341   ** Even if the data structure on disk is corrupted, this (reading two
113342   ** varints from the buffer) does not risk an overread. If zNode is a
113343   ** root node, then the buffer comes from a SELECT statement. SQLite does
113344   ** not make this guarantee explicitly, but in practice there are always
113345   ** either more than 20 bytes of allocated space following the nNode bytes of
113346   ** contents, or two zero bytes. Or, if the node is read from the %_segments
113347   ** table, then there are always 20 bytes of zeroed padding following the
113348   ** nNode bytes of content (see sqlite3Fts3ReadBlock() for details).
113349   */
113350   zCsr += sqlite3Fts3GetVarint(zCsr, &iChild);
113351   zCsr += sqlite3Fts3GetVarint(zCsr, &iChild);
113352   if( zCsr>zEnd ){
113353     return SQLITE_CORRUPT_VTAB;
113354   }
113355 
113356   while( zCsr<zEnd && (piFirst || piLast) ){
113357     int cmp;                      /* memcmp() result */
113358     int nSuffix;                  /* Size of term suffix */
113359     int nPrefix = 0;              /* Size of term prefix */
113360     int nBuffer;                  /* Total term size */
113361 
113362     /* Load the next term on the node into zBuffer. Use realloc() to expand
113363     ** the size of zBuffer if required.  */
113364     if( !isFirstTerm ){
113365       zCsr += sqlite3Fts3GetVarint32(zCsr, &nPrefix);
113366     }
113367     isFirstTerm = 0;
113368     zCsr += sqlite3Fts3GetVarint32(zCsr, &nSuffix);
113369 
113370     if( nPrefix<0 || nSuffix<0 || &zCsr[nSuffix]>zEnd ){
113371       rc = SQLITE_CORRUPT_VTAB;
113372       goto finish_scan;
113373     }
113374     if( nPrefix+nSuffix>nAlloc ){
113375       char *zNew;
113376       nAlloc = (nPrefix+nSuffix) * 2;
113377       zNew = (char *)sqlite3_realloc(zBuffer, nAlloc);
113378       if( !zNew ){
113379         rc = SQLITE_NOMEM;
113380         goto finish_scan;
113381       }
113382       zBuffer = zNew;
113383     }
113384     memcpy(&zBuffer[nPrefix], zCsr, nSuffix);
113385     nBuffer = nPrefix + nSuffix;
113386     zCsr += nSuffix;
113387 
113388     /* Compare the term we are searching for with the term just loaded from
113389     ** the interior node. If the specified term is greater than or equal
113390     ** to the term from the interior node, then all terms on the sub-tree
113391     ** headed by node iChild are smaller than zTerm. No need to search
113392     ** iChild.
113393     **
113394     ** If the interior node term is larger than the specified term, then
113395     ** the tree headed by iChild may contain the specified term.
113396     */
113397     cmp = memcmp(zTerm, zBuffer, (nBuffer>nTerm ? nTerm : nBuffer));
113398     if( piFirst && (cmp<0 || (cmp==0 && nBuffer>nTerm)) ){
113399       *piFirst = iChild;
113400       piFirst = 0;
113401     }
113402 
113403     if( piLast && cmp<0 ){
113404       *piLast = iChild;
113405       piLast = 0;
113406     }
113407 
113408     iChild++;
113409   };
113410 
113411   if( piFirst ) *piFirst = iChild;
113412   if( piLast ) *piLast = iChild;
113413 
113414  finish_scan:
113415   sqlite3_free(zBuffer);
113416   return rc;
113417 }
113418 
113419 
113420 /*
113421 ** The buffer pointed to by argument zNode (size nNode bytes) contains an
113422 ** interior node of a b-tree segment. The zTerm buffer (size nTerm bytes)
113423 ** contains a term. This function searches the sub-tree headed by the zNode
113424 ** node for the range of leaf nodes that may contain the specified term
113425 ** or terms for which the specified term is a prefix.
113426 **
113427 ** If piLeaf is not NULL, then *piLeaf is set to the blockid of the
113428 ** left-most leaf node in the tree that may contain the specified term.
113429 ** If piLeaf2 is not NULL, then *piLeaf2 is set to the blockid of the
113430 ** right-most leaf node that may contain a term for which the specified
113431 ** term is a prefix.
113432 **
113433 ** It is possible that the range of returned leaf nodes does not contain
113434 ** the specified term or any terms for which it is a prefix. However, if the
113435 ** segment does contain any such terms, they are stored within the identified
113436 ** range. Because this function only inspects interior segment nodes (and
113437 ** never loads leaf nodes into memory), it is not possible to be sure.
113438 **
113439 ** If an error occurs, an error code other than SQLITE_OK is returned.
113440 */
113441 static int fts3SelectLeaf(
113442   Fts3Table *p,                   /* Virtual table handle */
113443   const char *zTerm,              /* Term to select leaves for */
113444   int nTerm,                      /* Size of term zTerm in bytes */
113445   const char *zNode,              /* Buffer containing segment interior node */
113446   int nNode,                      /* Size of buffer at zNode */
113447   sqlite3_int64 *piLeaf,          /* Selected leaf node */
113448   sqlite3_int64 *piLeaf2          /* Selected leaf node */
113449 ){
113450   int rc;                         /* Return code */
113451   int iHeight;                    /* Height of this node in tree */
113452 
113453   assert( piLeaf || piLeaf2 );
113454 
113455   sqlite3Fts3GetVarint32(zNode, &iHeight);
113456   rc = fts3ScanInteriorNode(zTerm, nTerm, zNode, nNode, piLeaf, piLeaf2);
113457   assert( !piLeaf2 || !piLeaf || rc!=SQLITE_OK || (*piLeaf<=*piLeaf2) );
113458 
113459   if( rc==SQLITE_OK && iHeight>1 ){
113460     char *zBlob = 0;              /* Blob read from %_segments table */
113461     int nBlob;                    /* Size of zBlob in bytes */
113462 
113463     if( piLeaf && piLeaf2 && (*piLeaf!=*piLeaf2) ){
113464       rc = sqlite3Fts3ReadBlock(p, *piLeaf, &zBlob, &nBlob, 0);
113465       if( rc==SQLITE_OK ){
113466         rc = fts3SelectLeaf(p, zTerm, nTerm, zBlob, nBlob, piLeaf, 0);
113467       }
113468       sqlite3_free(zBlob);
113469       piLeaf = 0;
113470       zBlob = 0;
113471     }
113472 
113473     if( rc==SQLITE_OK ){
113474       rc = sqlite3Fts3ReadBlock(p, piLeaf?*piLeaf:*piLeaf2, &zBlob, &nBlob, 0);
113475     }
113476     if( rc==SQLITE_OK ){
113477       rc = fts3SelectLeaf(p, zTerm, nTerm, zBlob, nBlob, piLeaf, piLeaf2);
113478     }
113479     sqlite3_free(zBlob);
113480   }
113481 
113482   return rc;
113483 }
113484 
113485 /*
113486 ** This function is used to create delta-encoded serialized lists of FTS3
113487 ** varints. Each call to this function appends a single varint to a list.
113488 */
113489 static void fts3PutDeltaVarint(
113490   char **pp,                      /* IN/OUT: Output pointer */
113491   sqlite3_int64 *piPrev,          /* IN/OUT: Previous value written to list */
113492   sqlite3_int64 iVal              /* Write this value to the list */
113493 ){
113494   assert( iVal-*piPrev > 0 || (*piPrev==0 && iVal==0) );
113495   *pp += sqlite3Fts3PutVarint(*pp, iVal-*piPrev);
113496   *piPrev = iVal;
113497 }
113498 
113499 /*
113500 ** When this function is called, *ppPoslist is assumed to point to the
113501 ** start of a position-list. After it returns, *ppPoslist points to the
113502 ** first byte after the position-list.
113503 **
113504 ** A position list is list of positions (delta encoded) and columns for
113505 ** a single document record of a doclist.  So, in other words, this
113506 ** routine advances *ppPoslist so that it points to the next docid in
113507 ** the doclist, or to the first byte past the end of the doclist.
113508 **
113509 ** If pp is not NULL, then the contents of the position list are copied
113510 ** to *pp. *pp is set to point to the first byte past the last byte copied
113511 ** before this function returns.
113512 */
113513 static void fts3PoslistCopy(char **pp, char **ppPoslist){
113514   char *pEnd = *ppPoslist;
113515   char c = 0;
113516 
113517   /* The end of a position list is marked by a zero encoded as an FTS3
113518   ** varint. A single POS_END (0) byte. Except, if the 0 byte is preceded by
113519   ** a byte with the 0x80 bit set, then it is not a varint 0, but the tail
113520   ** of some other, multi-byte, value.
113521   **
113522   ** The following while-loop moves pEnd to point to the first byte that is not
113523   ** immediately preceded by a byte with the 0x80 bit set. Then increments
113524   ** pEnd once more so that it points to the byte immediately following the
113525   ** last byte in the position-list.
113526   */
113527   while( *pEnd | c ){
113528     c = *pEnd++ & 0x80;
113529     testcase( c!=0 && (*pEnd)==0 );
113530   }
113531   pEnd++;  /* Advance past the POS_END terminator byte */
113532 
113533   if( pp ){
113534     int n = (int)(pEnd - *ppPoslist);
113535     char *p = *pp;
113536     memcpy(p, *ppPoslist, n);
113537     p += n;
113538     *pp = p;
113539   }
113540   *ppPoslist = pEnd;
113541 }
113542 
113543 /*
113544 ** When this function is called, *ppPoslist is assumed to point to the
113545 ** start of a column-list. After it returns, *ppPoslist points to the
113546 ** to the terminator (POS_COLUMN or POS_END) byte of the column-list.
113547 **
113548 ** A column-list is list of delta-encoded positions for a single column
113549 ** within a single document within a doclist.
113550 **
113551 ** The column-list is terminated either by a POS_COLUMN varint (1) or
113552 ** a POS_END varint (0).  This routine leaves *ppPoslist pointing to
113553 ** the POS_COLUMN or POS_END that terminates the column-list.
113554 **
113555 ** If pp is not NULL, then the contents of the column-list are copied
113556 ** to *pp. *pp is set to point to the first byte past the last byte copied
113557 ** before this function returns.  The POS_COLUMN or POS_END terminator
113558 ** is not copied into *pp.
113559 */
113560 static void fts3ColumnlistCopy(char **pp, char **ppPoslist){
113561   char *pEnd = *ppPoslist;
113562   char c = 0;
113563 
113564   /* A column-list is terminated by either a 0x01 or 0x00 byte that is
113565   ** not part of a multi-byte varint.
113566   */
113567   while( 0xFE & (*pEnd | c) ){
113568     c = *pEnd++ & 0x80;
113569     testcase( c!=0 && ((*pEnd)&0xfe)==0 );
113570   }
113571   if( pp ){
113572     int n = (int)(pEnd - *ppPoslist);
113573     char *p = *pp;
113574     memcpy(p, *ppPoslist, n);
113575     p += n;
113576     *pp = p;
113577   }
113578   *ppPoslist = pEnd;
113579 }
113580 
113581 /*
113582 ** Value used to signify the end of an position-list. This is safe because
113583 ** it is not possible to have a document with 2^31 terms.
113584 */
113585 #define POSITION_LIST_END 0x7fffffff
113586 
113587 /*
113588 ** This function is used to help parse position-lists. When this function is
113589 ** called, *pp may point to the start of the next varint in the position-list
113590 ** being parsed, or it may point to 1 byte past the end of the position-list
113591 ** (in which case **pp will be a terminator bytes POS_END (0) or
113592 ** (1)).
113593 **
113594 ** If *pp points past the end of the current position-list, set *pi to
113595 ** POSITION_LIST_END and return. Otherwise, read the next varint from *pp,
113596 ** increment the current value of *pi by the value read, and set *pp to
113597 ** point to the next value before returning.
113598 **
113599 ** Before calling this routine *pi must be initialized to the value of
113600 ** the previous position, or zero if we are reading the first position
113601 ** in the position-list.  Because positions are delta-encoded, the value
113602 ** of the previous position is needed in order to compute the value of
113603 ** the next position.
113604 */
113605 static void fts3ReadNextPos(
113606   char **pp,                    /* IN/OUT: Pointer into position-list buffer */
113607   sqlite3_int64 *pi             /* IN/OUT: Value read from position-list */
113608 ){
113609   if( (**pp)&0xFE ){
113610     fts3GetDeltaVarint(pp, pi);
113611     *pi -= 2;
113612   }else{
113613     *pi = POSITION_LIST_END;
113614   }
113615 }
113616 
113617 /*
113618 ** If parameter iCol is not 0, write an POS_COLUMN (1) byte followed by
113619 ** the value of iCol encoded as a varint to *pp.   This will start a new
113620 ** column list.
113621 **
113622 ** Set *pp to point to the byte just after the last byte written before
113623 ** returning (do not modify it if iCol==0). Return the total number of bytes
113624 ** written (0 if iCol==0).
113625 */
113626 static int fts3PutColNumber(char **pp, int iCol){
113627   int n = 0;                      /* Number of bytes written */
113628   if( iCol ){
113629     char *p = *pp;                /* Output pointer */
113630     n = 1 + sqlite3Fts3PutVarint(&p[1], iCol);
113631     *p = 0x01;
113632     *pp = &p[n];
113633   }
113634   return n;
113635 }
113636 
113637 /*
113638 ** Compute the union of two position lists.  The output written
113639 ** into *pp contains all positions of both *pp1 and *pp2 in sorted
113640 ** order and with any duplicates removed.  All pointers are
113641 ** updated appropriately.   The caller is responsible for insuring
113642 ** that there is enough space in *pp to hold the complete output.
113643 */
113644 static void fts3PoslistMerge(
113645   char **pp,                      /* Output buffer */
113646   char **pp1,                     /* Left input list */
113647   char **pp2                      /* Right input list */
113648 ){
113649   char *p = *pp;
113650   char *p1 = *pp1;
113651   char *p2 = *pp2;
113652 
113653   while( *p1 || *p2 ){
113654     int iCol1;         /* The current column index in pp1 */
113655     int iCol2;         /* The current column index in pp2 */
113656 
113657     if( *p1==POS_COLUMN ) sqlite3Fts3GetVarint32(&p1[1], &iCol1);
113658     else if( *p1==POS_END ) iCol1 = POSITION_LIST_END;
113659     else iCol1 = 0;
113660 
113661     if( *p2==POS_COLUMN ) sqlite3Fts3GetVarint32(&p2[1], &iCol2);
113662     else if( *p2==POS_END ) iCol2 = POSITION_LIST_END;
113663     else iCol2 = 0;
113664 
113665     if( iCol1==iCol2 ){
113666       sqlite3_int64 i1 = 0;       /* Last position from pp1 */
113667       sqlite3_int64 i2 = 0;       /* Last position from pp2 */
113668       sqlite3_int64 iPrev = 0;
113669       int n = fts3PutColNumber(&p, iCol1);
113670       p1 += n;
113671       p2 += n;
113672 
113673       /* At this point, both p1 and p2 point to the start of column-lists
113674       ** for the same column (the column with index iCol1 and iCol2).
113675       ** A column-list is a list of non-negative delta-encoded varints, each
113676       ** incremented by 2 before being stored. Each list is terminated by a
113677       ** POS_END (0) or POS_COLUMN (1). The following block merges the two lists
113678       ** and writes the results to buffer p. p is left pointing to the byte
113679       ** after the list written. No terminator (POS_END or POS_COLUMN) is
113680       ** written to the output.
113681       */
113682       fts3GetDeltaVarint(&p1, &i1);
113683       fts3GetDeltaVarint(&p2, &i2);
113684       do {
113685         fts3PutDeltaVarint(&p, &iPrev, (i1<i2) ? i1 : i2);
113686         iPrev -= 2;
113687         if( i1==i2 ){
113688           fts3ReadNextPos(&p1, &i1);
113689           fts3ReadNextPos(&p2, &i2);
113690         }else if( i1<i2 ){
113691           fts3ReadNextPos(&p1, &i1);
113692         }else{
113693           fts3ReadNextPos(&p2, &i2);
113694         }
113695       }while( i1!=POSITION_LIST_END || i2!=POSITION_LIST_END );
113696     }else if( iCol1<iCol2 ){
113697       p1 += fts3PutColNumber(&p, iCol1);
113698       fts3ColumnlistCopy(&p, &p1);
113699     }else{
113700       p2 += fts3PutColNumber(&p, iCol2);
113701       fts3ColumnlistCopy(&p, &p2);
113702     }
113703   }
113704 
113705   *p++ = POS_END;
113706   *pp = p;
113707   *pp1 = p1 + 1;
113708   *pp2 = p2 + 1;
113709 }
113710 
113711 /*
113712 ** nToken==1 searches for adjacent positions.
113713 **
113714 ** This function is used to merge two position lists into one. When it is
113715 ** called, *pp1 and *pp2 must both point to position lists. A position-list is
113716 ** the part of a doclist that follows each document id. For example, if a row
113717 ** contains:
113718 **
113719 **     'a b c'|'x y z'|'a b b a'
113720 **
113721 ** Then the position list for this row for token 'b' would consist of:
113722 **
113723 **     0x02 0x01 0x02 0x03 0x03 0x00
113724 **
113725 ** When this function returns, both *pp1 and *pp2 are left pointing to the
113726 ** byte following the 0x00 terminator of their respective position lists.
113727 **
113728 ** If isSaveLeft is 0, an entry is added to the output position list for
113729 ** each position in *pp2 for which there exists one or more positions in
113730 ** *pp1 so that (pos(*pp2)>pos(*pp1) && pos(*pp2)-pos(*pp1)<=nToken). i.e.
113731 ** when the *pp1 token appears before the *pp2 token, but not more than nToken
113732 ** slots before it.
113733 */
113734 static int fts3PoslistPhraseMerge(
113735   char **pp,                      /* IN/OUT: Preallocated output buffer */
113736   int nToken,                     /* Maximum difference in token positions */
113737   int isSaveLeft,                 /* Save the left position */
113738   int isExact,                    /* If *pp1 is exactly nTokens before *pp2 */
113739   char **pp1,                     /* IN/OUT: Left input list */
113740   char **pp2                      /* IN/OUT: Right input list */
113741 ){
113742   char *p = (pp ? *pp : 0);
113743   char *p1 = *pp1;
113744   char *p2 = *pp2;
113745   int iCol1 = 0;
113746   int iCol2 = 0;
113747 
113748   /* Never set both isSaveLeft and isExact for the same invocation. */
113749   assert( isSaveLeft==0 || isExact==0 );
113750 
113751   assert( *p1!=0 && *p2!=0 );
113752   if( *p1==POS_COLUMN ){
113753     p1++;
113754     p1 += sqlite3Fts3GetVarint32(p1, &iCol1);
113755   }
113756   if( *p2==POS_COLUMN ){
113757     p2++;
113758     p2 += sqlite3Fts3GetVarint32(p2, &iCol2);
113759   }
113760 
113761   while( 1 ){
113762     if( iCol1==iCol2 ){
113763       char *pSave = p;
113764       sqlite3_int64 iPrev = 0;
113765       sqlite3_int64 iPos1 = 0;
113766       sqlite3_int64 iPos2 = 0;
113767 
113768       if( pp && iCol1 ){
113769         *p++ = POS_COLUMN;
113770         p += sqlite3Fts3PutVarint(p, iCol1);
113771       }
113772 
113773       assert( *p1!=POS_END && *p1!=POS_COLUMN );
113774       assert( *p2!=POS_END && *p2!=POS_COLUMN );
113775       fts3GetDeltaVarint(&p1, &iPos1); iPos1 -= 2;
113776       fts3GetDeltaVarint(&p2, &iPos2); iPos2 -= 2;
113777 
113778       while( 1 ){
113779         if( iPos2==iPos1+nToken
113780          || (isExact==0 && iPos2>iPos1 && iPos2<=iPos1+nToken)
113781         ){
113782           sqlite3_int64 iSave;
113783           if( !pp ){
113784             fts3PoslistCopy(0, &p2);
113785             fts3PoslistCopy(0, &p1);
113786             *pp1 = p1;
113787             *pp2 = p2;
113788             return 1;
113789           }
113790           iSave = isSaveLeft ? iPos1 : iPos2;
113791           fts3PutDeltaVarint(&p, &iPrev, iSave+2); iPrev -= 2;
113792           pSave = 0;
113793         }
113794         if( (!isSaveLeft && iPos2<=(iPos1+nToken)) || iPos2<=iPos1 ){
113795           if( (*p2&0xFE)==0 ) break;
113796           fts3GetDeltaVarint(&p2, &iPos2); iPos2 -= 2;
113797         }else{
113798           if( (*p1&0xFE)==0 ) break;
113799           fts3GetDeltaVarint(&p1, &iPos1); iPos1 -= 2;
113800         }
113801       }
113802 
113803       if( pSave ){
113804         assert( pp && p );
113805         p = pSave;
113806       }
113807 
113808       fts3ColumnlistCopy(0, &p1);
113809       fts3ColumnlistCopy(0, &p2);
113810       assert( (*p1&0xFE)==0 && (*p2&0xFE)==0 );
113811       if( 0==*p1 || 0==*p2 ) break;
113812 
113813       p1++;
113814       p1 += sqlite3Fts3GetVarint32(p1, &iCol1);
113815       p2++;
113816       p2 += sqlite3Fts3GetVarint32(p2, &iCol2);
113817     }
113818 
113819     /* Advance pointer p1 or p2 (whichever corresponds to the smaller of
113820     ** iCol1 and iCol2) so that it points to either the 0x00 that marks the
113821     ** end of the position list, or the 0x01 that precedes the next
113822     ** column-number in the position list.
113823     */
113824     else if( iCol1<iCol2 ){
113825       fts3ColumnlistCopy(0, &p1);
113826       if( 0==*p1 ) break;
113827       p1++;
113828       p1 += sqlite3Fts3GetVarint32(p1, &iCol1);
113829     }else{
113830       fts3ColumnlistCopy(0, &p2);
113831       if( 0==*p2 ) break;
113832       p2++;
113833       p2 += sqlite3Fts3GetVarint32(p2, &iCol2);
113834     }
113835   }
113836 
113837   fts3PoslistCopy(0, &p2);
113838   fts3PoslistCopy(0, &p1);
113839   *pp1 = p1;
113840   *pp2 = p2;
113841   if( !pp || *pp==p ){
113842     return 0;
113843   }
113844   *p++ = 0x00;
113845   *pp = p;
113846   return 1;
113847 }
113848 
113849 /*
113850 ** Merge two position-lists as required by the NEAR operator. The argument
113851 ** position lists correspond to the left and right phrases of an expression
113852 ** like:
113853 **
113854 **     "phrase 1" NEAR "phrase number 2"
113855 **
113856 ** Position list *pp1 corresponds to the left-hand side of the NEAR
113857 ** expression and *pp2 to the right. As usual, the indexes in the position
113858 ** lists are the offsets of the last token in each phrase (tokens "1" and "2"
113859 ** in the example above).
113860 **
113861 ** The output position list - written to *pp - is a copy of *pp2 with those
113862 ** entries that are not sufficiently NEAR entries in *pp1 removed.
113863 */
113864 static int fts3PoslistNearMerge(
113865   char **pp,                      /* Output buffer */
113866   char *aTmp,                     /* Temporary buffer space */
113867   int nRight,                     /* Maximum difference in token positions */
113868   int nLeft,                      /* Maximum difference in token positions */
113869   char **pp1,                     /* IN/OUT: Left input list */
113870   char **pp2                      /* IN/OUT: Right input list */
113871 ){
113872   char *p1 = *pp1;
113873   char *p2 = *pp2;
113874 
113875   char *pTmp1 = aTmp;
113876   char *pTmp2;
113877   char *aTmp2;
113878   int res = 1;
113879 
113880   fts3PoslistPhraseMerge(&pTmp1, nRight, 0, 0, pp1, pp2);
113881   aTmp2 = pTmp2 = pTmp1;
113882   *pp1 = p1;
113883   *pp2 = p2;
113884   fts3PoslistPhraseMerge(&pTmp2, nLeft, 1, 0, pp2, pp1);
113885   if( pTmp1!=aTmp && pTmp2!=aTmp2 ){
113886     fts3PoslistMerge(pp, &aTmp, &aTmp2);
113887   }else if( pTmp1!=aTmp ){
113888     fts3PoslistCopy(pp, &aTmp);
113889   }else if( pTmp2!=aTmp2 ){
113890     fts3PoslistCopy(pp, &aTmp2);
113891   }else{
113892     res = 0;
113893   }
113894 
113895   return res;
113896 }
113897 
113898 /*
113899 ** A pointer to an instance of this structure is used as the context
113900 ** argument to sqlite3Fts3SegReaderIterate()
113901 */
113902 typedef struct TermSelect TermSelect;
113903 struct TermSelect {
113904   int isReqPos;
113905   char *aaOutput[16];             /* Malloc'd output buffer */
113906   int anOutput[16];               /* Size of output in bytes */
113907 };
113908 
113909 
113910 static void fts3GetDeltaVarint3(
113911   char **pp,
113912   char *pEnd,
113913   int bDescIdx,
113914   sqlite3_int64 *pVal
113915 ){
113916   if( *pp>=pEnd ){
113917     *pp = 0;
113918   }else{
113919     sqlite3_int64 iVal;
113920     *pp += sqlite3Fts3GetVarint(*pp, &iVal);
113921     if( bDescIdx ){
113922       *pVal -= iVal;
113923     }else{
113924       *pVal += iVal;
113925     }
113926   }
113927 }
113928 
113929 static void fts3PutDeltaVarint3(
113930   char **pp,                      /* IN/OUT: Output pointer */
113931   int bDescIdx,                   /* True for descending docids */
113932   sqlite3_int64 *piPrev,          /* IN/OUT: Previous value written to list */
113933   int *pbFirst,                   /* IN/OUT: True after first int written */
113934   sqlite3_int64 iVal              /* Write this value to the list */
113935 ){
113936   sqlite3_int64 iWrite;
113937   if( bDescIdx==0 || *pbFirst==0 ){
113938     iWrite = iVal - *piPrev;
113939   }else{
113940     iWrite = *piPrev - iVal;
113941   }
113942   assert( *pbFirst || *piPrev==0 );
113943   assert( *pbFirst==0 || iWrite>0 );
113944   *pp += sqlite3Fts3PutVarint(*pp, iWrite);
113945   *piPrev = iVal;
113946   *pbFirst = 1;
113947 }
113948 
113949 #define COMPARE_DOCID(i1, i2) ((bDescIdx?-1:1) * (i1-i2))
113950 
113951 static int fts3DoclistOrMerge(
113952   int bDescIdx,                   /* True if arguments are desc */
113953   char *a1, int n1,               /* First doclist */
113954   char *a2, int n2,               /* Second doclist */
113955   char **paOut, int *pnOut        /* OUT: Malloc'd doclist */
113956 ){
113957   sqlite3_int64 i1 = 0;
113958   sqlite3_int64 i2 = 0;
113959   sqlite3_int64 iPrev = 0;
113960   char *pEnd1 = &a1[n1];
113961   char *pEnd2 = &a2[n2];
113962   char *p1 = a1;
113963   char *p2 = a2;
113964   char *p;
113965   char *aOut;
113966   int bFirstOut = 0;
113967 
113968   *paOut = 0;
113969   *pnOut = 0;
113970   aOut = sqlite3_malloc(n1+n2);
113971   if( !aOut ) return SQLITE_NOMEM;
113972 
113973   p = aOut;
113974   fts3GetDeltaVarint3(&p1, pEnd1, 0, &i1);
113975   fts3GetDeltaVarint3(&p2, pEnd2, 0, &i2);
113976   while( p1 || p2 ){
113977     sqlite3_int64 iDiff = COMPARE_DOCID(i1, i2);
113978 
113979     if( p2 && p1 && iDiff==0 ){
113980       fts3PutDeltaVarint3(&p, bDescIdx, &iPrev, &bFirstOut, i1);
113981       fts3PoslistMerge(&p, &p1, &p2);
113982       fts3GetDeltaVarint3(&p1, pEnd1, bDescIdx, &i1);
113983       fts3GetDeltaVarint3(&p2, pEnd2, bDescIdx, &i2);
113984     }else if( !p2 || (p1 && iDiff<0) ){
113985       fts3PutDeltaVarint3(&p, bDescIdx, &iPrev, &bFirstOut, i1);
113986       fts3PoslistCopy(&p, &p1);
113987       fts3GetDeltaVarint3(&p1, pEnd1, bDescIdx, &i1);
113988     }else{
113989       fts3PutDeltaVarint3(&p, bDescIdx, &iPrev, &bFirstOut, i2);
113990       fts3PoslistCopy(&p, &p2);
113991       fts3GetDeltaVarint3(&p2, pEnd2, bDescIdx, &i2);
113992     }
113993   }
113994 
113995   *paOut = aOut;
113996   *pnOut = (p-aOut);
113997   return SQLITE_OK;
113998 }
113999 
114000 static void fts3DoclistPhraseMerge(
114001   int bDescIdx,                   /* True if arguments are desc */
114002   int nDist,                      /* Distance from left to right (1=adjacent) */
114003   char *aLeft, int nLeft,         /* Left doclist */
114004   char *aRight, int *pnRight      /* IN/OUT: Right/output doclist */
114005 ){
114006   sqlite3_int64 i1 = 0;
114007   sqlite3_int64 i2 = 0;
114008   sqlite3_int64 iPrev = 0;
114009   char *pEnd1 = &aLeft[nLeft];
114010   char *pEnd2 = &aRight[*pnRight];
114011   char *p1 = aLeft;
114012   char *p2 = aRight;
114013   char *p;
114014   int bFirstOut = 0;
114015   char *aOut = aRight;
114016 
114017   assert( nDist>0 );
114018 
114019   p = aOut;
114020   fts3GetDeltaVarint3(&p1, pEnd1, 0, &i1);
114021   fts3GetDeltaVarint3(&p2, pEnd2, 0, &i2);
114022 
114023   while( p1 && p2 ){
114024     sqlite3_int64 iDiff = COMPARE_DOCID(i1, i2);
114025     if( iDiff==0 ){
114026       char *pSave = p;
114027       sqlite3_int64 iPrevSave = iPrev;
114028       int bFirstOutSave = bFirstOut;
114029 
114030       fts3PutDeltaVarint3(&p, bDescIdx, &iPrev, &bFirstOut, i1);
114031       if( 0==fts3PoslistPhraseMerge(&p, nDist, 0, 1, &p1, &p2) ){
114032         p = pSave;
114033         iPrev = iPrevSave;
114034         bFirstOut = bFirstOutSave;
114035       }
114036       fts3GetDeltaVarint3(&p1, pEnd1, bDescIdx, &i1);
114037       fts3GetDeltaVarint3(&p2, pEnd2, bDescIdx, &i2);
114038     }else if( iDiff<0 ){
114039       fts3PoslistCopy(0, &p1);
114040       fts3GetDeltaVarint3(&p1, pEnd1, bDescIdx, &i1);
114041     }else{
114042       fts3PoslistCopy(0, &p2);
114043       fts3GetDeltaVarint3(&p2, pEnd2, bDescIdx, &i2);
114044     }
114045   }
114046 
114047   *pnRight = p - aOut;
114048 }
114049 
114050 
114051 /*
114052 ** Merge all doclists in the TermSelect.aaOutput[] array into a single
114053 ** doclist stored in TermSelect.aaOutput[0]. If successful, delete all
114054 ** other doclists (except the aaOutput[0] one) and return SQLITE_OK.
114055 **
114056 ** If an OOM error occurs, return SQLITE_NOMEM. In this case it is
114057 ** the responsibility of the caller to free any doclists left in the
114058 ** TermSelect.aaOutput[] array.
114059 */
114060 static int fts3TermSelectMerge(Fts3Table *p, TermSelect *pTS){
114061   char *aOut = 0;
114062   int nOut = 0;
114063   int i;
114064 
114065   /* Loop through the doclists in the aaOutput[] array. Merge them all
114066   ** into a single doclist.
114067   */
114068   for(i=0; i<SizeofArray(pTS->aaOutput); i++){
114069     if( pTS->aaOutput[i] ){
114070       if( !aOut ){
114071         aOut = pTS->aaOutput[i];
114072         nOut = pTS->anOutput[i];
114073         pTS->aaOutput[i] = 0;
114074       }else{
114075         int nNew;
114076         char *aNew;
114077 
114078         int rc = fts3DoclistOrMerge(p->bDescIdx,
114079             pTS->aaOutput[i], pTS->anOutput[i], aOut, nOut, &aNew, &nNew
114080         );
114081         if( rc!=SQLITE_OK ){
114082           sqlite3_free(aOut);
114083           return rc;
114084         }
114085 
114086         sqlite3_free(pTS->aaOutput[i]);
114087         sqlite3_free(aOut);
114088         pTS->aaOutput[i] = 0;
114089         aOut = aNew;
114090         nOut = nNew;
114091       }
114092     }
114093   }
114094 
114095   pTS->aaOutput[0] = aOut;
114096   pTS->anOutput[0] = nOut;
114097   return SQLITE_OK;
114098 }
114099 
114100 /*
114101 ** This function is used as the sqlite3Fts3SegReaderIterate() callback when
114102 ** querying the full-text index for a doclist associated with a term or
114103 ** term-prefix.
114104 */
114105 static int fts3TermSelectCb(
114106   Fts3Table *p,                   /* Virtual table object */
114107   void *pContext,                 /* Pointer to TermSelect structure */
114108   char *zTerm,
114109   int nTerm,
114110   char *aDoclist,
114111   int nDoclist
114112 ){
114113   TermSelect *pTS = (TermSelect *)pContext;
114114 
114115   UNUSED_PARAMETER(p);
114116   UNUSED_PARAMETER(zTerm);
114117   UNUSED_PARAMETER(nTerm);
114118 
114119   if( pTS->aaOutput[0]==0 ){
114120     /* If this is the first term selected, copy the doclist to the output
114121     ** buffer using memcpy(). */
114122     pTS->aaOutput[0] = sqlite3_malloc(nDoclist);
114123     pTS->anOutput[0] = nDoclist;
114124     if( pTS->aaOutput[0] ){
114125       memcpy(pTS->aaOutput[0], aDoclist, nDoclist);
114126     }else{
114127       return SQLITE_NOMEM;
114128     }
114129   }else{
114130     char *aMerge = aDoclist;
114131     int nMerge = nDoclist;
114132     int iOut;
114133 
114134     for(iOut=0; iOut<SizeofArray(pTS->aaOutput); iOut++){
114135       if( pTS->aaOutput[iOut]==0 ){
114136         assert( iOut>0 );
114137         pTS->aaOutput[iOut] = aMerge;
114138         pTS->anOutput[iOut] = nMerge;
114139         break;
114140       }else{
114141         char *aNew;
114142         int nNew;
114143 
114144         int rc = fts3DoclistOrMerge(p->bDescIdx, aMerge, nMerge,
114145             pTS->aaOutput[iOut], pTS->anOutput[iOut], &aNew, &nNew
114146         );
114147         if( rc!=SQLITE_OK ){
114148           if( aMerge!=aDoclist ) sqlite3_free(aMerge);
114149           return rc;
114150         }
114151 
114152         if( aMerge!=aDoclist ) sqlite3_free(aMerge);
114153         sqlite3_free(pTS->aaOutput[iOut]);
114154         pTS->aaOutput[iOut] = 0;
114155 
114156         aMerge = aNew;
114157         nMerge = nNew;
114158         if( (iOut+1)==SizeofArray(pTS->aaOutput) ){
114159           pTS->aaOutput[iOut] = aMerge;
114160           pTS->anOutput[iOut] = nMerge;
114161         }
114162       }
114163     }
114164   }
114165   return SQLITE_OK;
114166 }
114167 
114168 /*
114169 ** Append SegReader object pNew to the end of the pCsr->apSegment[] array.
114170 */
114171 static int fts3SegReaderCursorAppend(
114172   Fts3MultiSegReader *pCsr,
114173   Fts3SegReader *pNew
114174 ){
114175   if( (pCsr->nSegment%16)==0 ){
114176     Fts3SegReader **apNew;
114177     int nByte = (pCsr->nSegment + 16)*sizeof(Fts3SegReader*);
114178     apNew = (Fts3SegReader **)sqlite3_realloc(pCsr->apSegment, nByte);
114179     if( !apNew ){
114180       sqlite3Fts3SegReaderFree(pNew);
114181       return SQLITE_NOMEM;
114182     }
114183     pCsr->apSegment = apNew;
114184   }
114185   pCsr->apSegment[pCsr->nSegment++] = pNew;
114186   return SQLITE_OK;
114187 }
114188 
114189 static int fts3SegReaderCursor(
114190   Fts3Table *p,                   /* FTS3 table handle */
114191   int iIndex,                     /* Index to search (from 0 to p->nIndex-1) */
114192   int iLevel,                     /* Level of segments to scan */
114193   const char *zTerm,              /* Term to query for */
114194   int nTerm,                      /* Size of zTerm in bytes */
114195   int isPrefix,                   /* True for a prefix search */
114196   int isScan,                     /* True to scan from zTerm to EOF */
114197   Fts3MultiSegReader *pCsr       /* Cursor object to populate */
114198 ){
114199   int rc = SQLITE_OK;
114200   int rc2;
114201   sqlite3_stmt *pStmt = 0;
114202 
114203   /* If iLevel is less than 0 and this is not a scan, include a seg-reader
114204   ** for the pending-terms. If this is a scan, then this call must be being
114205   ** made by an fts4aux module, not an FTS table. In this case calling
114206   ** Fts3SegReaderPending might segfault, as the data structures used by
114207   ** fts4aux are not completely populated. So it's easiest to filter these
114208   ** calls out here.  */
114209   if( iLevel<0 && p->aIndex ){
114210     Fts3SegReader *pSeg = 0;
114211     rc = sqlite3Fts3SegReaderPending(p, iIndex, zTerm, nTerm, isPrefix, &pSeg);
114212     if( rc==SQLITE_OK && pSeg ){
114213       rc = fts3SegReaderCursorAppend(pCsr, pSeg);
114214     }
114215   }
114216 
114217   if( iLevel!=FTS3_SEGCURSOR_PENDING ){
114218     if( rc==SQLITE_OK ){
114219       rc = sqlite3Fts3AllSegdirs(p, iIndex, iLevel, &pStmt);
114220     }
114221 
114222     while( rc==SQLITE_OK && SQLITE_ROW==(rc = sqlite3_step(pStmt)) ){
114223       Fts3SegReader *pSeg = 0;
114224 
114225       /* Read the values returned by the SELECT into local variables. */
114226       sqlite3_int64 iStartBlock = sqlite3_column_int64(pStmt, 1);
114227       sqlite3_int64 iLeavesEndBlock = sqlite3_column_int64(pStmt, 2);
114228       sqlite3_int64 iEndBlock = sqlite3_column_int64(pStmt, 3);
114229       int nRoot = sqlite3_column_bytes(pStmt, 4);
114230       char const *zRoot = sqlite3_column_blob(pStmt, 4);
114231 
114232       /* If zTerm is not NULL, and this segment is not stored entirely on its
114233       ** root node, the range of leaves scanned can be reduced. Do this. */
114234       if( iStartBlock && zTerm ){
114235         sqlite3_int64 *pi = (isPrefix ? &iLeavesEndBlock : 0);
114236         rc = fts3SelectLeaf(p, zTerm, nTerm, zRoot, nRoot, &iStartBlock, pi);
114237         if( rc!=SQLITE_OK ) goto finished;
114238         if( isPrefix==0 && isScan==0 ) iLeavesEndBlock = iStartBlock;
114239       }
114240 
114241       rc = sqlite3Fts3SegReaderNew(pCsr->nSegment+1,
114242           iStartBlock, iLeavesEndBlock, iEndBlock, zRoot, nRoot, &pSeg
114243       );
114244       if( rc!=SQLITE_OK ) goto finished;
114245       rc = fts3SegReaderCursorAppend(pCsr, pSeg);
114246     }
114247   }
114248 
114249  finished:
114250   rc2 = sqlite3_reset(pStmt);
114251   if( rc==SQLITE_DONE ) rc = rc2;
114252 
114253   return rc;
114254 }
114255 
114256 /*
114257 ** Set up a cursor object for iterating through a full-text index or a
114258 ** single level therein.
114259 */
114260 SQLITE_PRIVATE int sqlite3Fts3SegReaderCursor(
114261   Fts3Table *p,                   /* FTS3 table handle */
114262   int iIndex,                     /* Index to search (from 0 to p->nIndex-1) */
114263   int iLevel,                     /* Level of segments to scan */
114264   const char *zTerm,              /* Term to query for */
114265   int nTerm,                      /* Size of zTerm in bytes */
114266   int isPrefix,                   /* True for a prefix search */
114267   int isScan,                     /* True to scan from zTerm to EOF */
114268   Fts3MultiSegReader *pCsr       /* Cursor object to populate */
114269 ){
114270   assert( iIndex>=0 && iIndex<p->nIndex );
114271   assert( iLevel==FTS3_SEGCURSOR_ALL
114272       ||  iLevel==FTS3_SEGCURSOR_PENDING
114273       ||  iLevel>=0
114274   );
114275   assert( iLevel<FTS3_SEGDIR_MAXLEVEL );
114276   assert( FTS3_SEGCURSOR_ALL<0 && FTS3_SEGCURSOR_PENDING<0 );
114277   assert( isPrefix==0 || isScan==0 );
114278 
114279   /* "isScan" is only set to true by the ft4aux module, an ordinary
114280   ** full-text tables. */
114281   assert( isScan==0 || p->aIndex==0 );
114282 
114283   memset(pCsr, 0, sizeof(Fts3MultiSegReader));
114284 
114285   return fts3SegReaderCursor(
114286       p, iIndex, iLevel, zTerm, nTerm, isPrefix, isScan, pCsr
114287   );
114288 }
114289 
114290 static int fts3SegReaderCursorAddZero(
114291   Fts3Table *p,
114292   const char *zTerm,
114293   int nTerm,
114294   Fts3MultiSegReader *pCsr
114295 ){
114296   return fts3SegReaderCursor(p, 0, FTS3_SEGCURSOR_ALL, zTerm, nTerm, 0, 0,pCsr);
114297 }
114298 
114299 
114300 SQLITE_PRIVATE int sqlite3Fts3TermSegReaderCursor(
114301   Fts3Cursor *pCsr,               /* Virtual table cursor handle */
114302   const char *zTerm,              /* Term to query for */
114303   int nTerm,                      /* Size of zTerm in bytes */
114304   int isPrefix,                   /* True for a prefix search */
114305   Fts3MultiSegReader **ppSegcsr   /* OUT: Allocated seg-reader cursor */
114306 ){
114307   Fts3MultiSegReader *pSegcsr;   /* Object to allocate and return */
114308   int rc = SQLITE_NOMEM;          /* Return code */
114309 
114310   pSegcsr = sqlite3_malloc(sizeof(Fts3MultiSegReader));
114311   if( pSegcsr ){
114312     int i;
114313     int bFound = 0;               /* True once an index has been found */
114314     Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
114315 
114316     if( isPrefix ){
114317       for(i=1; bFound==0 && i<p->nIndex; i++){
114318         if( p->aIndex[i].nPrefix==nTerm ){
114319           bFound = 1;
114320           rc = sqlite3Fts3SegReaderCursor(
114321               p, i, FTS3_SEGCURSOR_ALL, zTerm, nTerm, 0, 0, pSegcsr);
114322           pSegcsr->bLookup = 1;
114323         }
114324       }
114325 
114326       for(i=1; bFound==0 && i<p->nIndex; i++){
114327         if( p->aIndex[i].nPrefix==nTerm+1 ){
114328           bFound = 1;
114329           rc = sqlite3Fts3SegReaderCursor(
114330               p, i, FTS3_SEGCURSOR_ALL, zTerm, nTerm, 1, 0, pSegcsr
114331           );
114332           if( rc==SQLITE_OK ){
114333             rc = fts3SegReaderCursorAddZero(p, zTerm, nTerm, pSegcsr);
114334           }
114335         }
114336       }
114337     }
114338 
114339     if( bFound==0 ){
114340       rc = sqlite3Fts3SegReaderCursor(
114341           p, 0, FTS3_SEGCURSOR_ALL, zTerm, nTerm, isPrefix, 0, pSegcsr
114342       );
114343       pSegcsr->bLookup = !isPrefix;
114344     }
114345   }
114346 
114347   *ppSegcsr = pSegcsr;
114348   return rc;
114349 }
114350 
114351 static void fts3SegReaderCursorFree(Fts3MultiSegReader *pSegcsr){
114352   sqlite3Fts3SegReaderFinish(pSegcsr);
114353   sqlite3_free(pSegcsr);
114354 }
114355 
114356 /*
114357 ** This function retreives the doclist for the specified term (or term
114358 ** prefix) from the database.
114359 **
114360 ** The returned doclist may be in one of two formats, depending on the
114361 ** value of parameter isReqPos. If isReqPos is zero, then the doclist is
114362 ** a sorted list of delta-compressed docids (a bare doclist). If isReqPos
114363 ** is non-zero, then the returned list is in the same format as is stored
114364 ** in the database without the found length specifier at the start of on-disk
114365 ** doclists.
114366 */
114367 static int fts3TermSelect(
114368   Fts3Table *p,                   /* Virtual table handle */
114369   Fts3PhraseToken *pTok,          /* Token to query for */
114370   int iColumn,                    /* Column to query (or -ve for all columns) */
114371   int isReqPos,                   /* True to include position lists in output */
114372   int *pnOut,                     /* OUT: Size of buffer at *ppOut */
114373   char **ppOut                    /* OUT: Malloced result buffer */
114374 ){
114375   int rc;                         /* Return code */
114376   Fts3MultiSegReader *pSegcsr;   /* Seg-reader cursor for this term */
114377   TermSelect tsc;                 /* Context object for fts3TermSelectCb() */
114378   Fts3SegFilter filter;           /* Segment term filter configuration */
114379 
114380   pSegcsr = pTok->pSegcsr;
114381   memset(&tsc, 0, sizeof(TermSelect));
114382   tsc.isReqPos = isReqPos;
114383 
114384   filter.flags = FTS3_SEGMENT_IGNORE_EMPTY
114385         | (pTok->isPrefix ? FTS3_SEGMENT_PREFIX : 0)
114386         | (isReqPos ? FTS3_SEGMENT_REQUIRE_POS : 0)
114387         | (iColumn<p->nColumn ? FTS3_SEGMENT_COLUMN_FILTER : 0);
114388   filter.iCol = iColumn;
114389   filter.zTerm = pTok->z;
114390   filter.nTerm = pTok->n;
114391 
114392   rc = sqlite3Fts3SegReaderStart(p, pSegcsr, &filter);
114393   while( SQLITE_OK==rc
114394       && SQLITE_ROW==(rc = sqlite3Fts3SegReaderStep(p, pSegcsr))
114395   ){
114396     rc = fts3TermSelectCb(p, (void *)&tsc,
114397         pSegcsr->zTerm, pSegcsr->nTerm, pSegcsr->aDoclist, pSegcsr->nDoclist
114398     );
114399   }
114400 
114401   if( rc==SQLITE_OK ){
114402     rc = fts3TermSelectMerge(p, &tsc);
114403   }
114404   if( rc==SQLITE_OK ){
114405     *ppOut = tsc.aaOutput[0];
114406     *pnOut = tsc.anOutput[0];
114407   }else{
114408     int i;
114409     for(i=0; i<SizeofArray(tsc.aaOutput); i++){
114410       sqlite3_free(tsc.aaOutput[i]);
114411     }
114412   }
114413 
114414   fts3SegReaderCursorFree(pSegcsr);
114415   pTok->pSegcsr = 0;
114416   return rc;
114417 }
114418 
114419 /*
114420 ** This function counts the total number of docids in the doclist stored
114421 ** in buffer aList[], size nList bytes.
114422 **
114423 ** If the isPoslist argument is true, then it is assumed that the doclist
114424 ** contains a position-list following each docid. Otherwise, it is assumed
114425 ** that the doclist is simply a list of docids stored as delta encoded
114426 ** varints.
114427 */
114428 static int fts3DoclistCountDocids(int isPoslist, char *aList, int nList){
114429   int nDoc = 0;                   /* Return value */
114430   if( aList ){
114431     char *aEnd = &aList[nList];   /* Pointer to one byte after EOF */
114432     char *p = aList;              /* Cursor */
114433     if( !isPoslist ){
114434       /* The number of docids in the list is the same as the number of
114435       ** varints. In FTS3 a varint consists of a single byte with the 0x80
114436       ** bit cleared and zero or more bytes with the 0x80 bit set. So to
114437       ** count the varints in the buffer, just count the number of bytes
114438       ** with the 0x80 bit clear.  */
114439       while( p<aEnd ) nDoc += (((*p++)&0x80)==0);
114440     }else{
114441       while( p<aEnd ){
114442         nDoc++;
114443         while( (*p++)&0x80 );     /* Skip docid varint */
114444         fts3PoslistCopy(0, &p);   /* Skip over position list */
114445       }
114446     }
114447   }
114448 
114449   return nDoc;
114450 }
114451 
114452 /*
114453 ** Advance the cursor to the next row in the %_content table that
114454 ** matches the search criteria.  For a MATCH search, this will be
114455 ** the next row that matches. For a full-table scan, this will be
114456 ** simply the next row in the %_content table.  For a docid lookup,
114457 ** this routine simply sets the EOF flag.
114458 **
114459 ** Return SQLITE_OK if nothing goes wrong.  SQLITE_OK is returned
114460 ** even if we reach end-of-file.  The fts3EofMethod() will be called
114461 ** subsequently to determine whether or not an EOF was hit.
114462 */
114463 static int fts3NextMethod(sqlite3_vtab_cursor *pCursor){
114464   int rc;
114465   Fts3Cursor *pCsr = (Fts3Cursor *)pCursor;
114466   if( pCsr->eSearch==FTS3_DOCID_SEARCH || pCsr->eSearch==FTS3_FULLSCAN_SEARCH ){
114467     if( SQLITE_ROW!=sqlite3_step(pCsr->pStmt) ){
114468       pCsr->isEof = 1;
114469       rc = sqlite3_reset(pCsr->pStmt);
114470     }else{
114471       pCsr->iPrevId = sqlite3_column_int64(pCsr->pStmt, 0);
114472       rc = SQLITE_OK;
114473     }
114474   }else{
114475     rc = sqlite3Fts3EvalNext((Fts3Cursor *)pCursor);
114476   }
114477   assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
114478   return rc;
114479 }
114480 
114481 /*
114482 ** This is the xFilter interface for the virtual table.  See
114483 ** the virtual table xFilter method documentation for additional
114484 ** information.
114485 **
114486 ** If idxNum==FTS3_FULLSCAN_SEARCH then do a full table scan against
114487 ** the %_content table.
114488 **
114489 ** If idxNum==FTS3_DOCID_SEARCH then do a docid lookup for a single entry
114490 ** in the %_content table.
114491 **
114492 ** If idxNum>=FTS3_FULLTEXT_SEARCH then use the full text index.  The
114493 ** column on the left-hand side of the MATCH operator is column
114494 ** number idxNum-FTS3_FULLTEXT_SEARCH, 0 indexed.  argv[0] is the right-hand
114495 ** side of the MATCH operator.
114496 */
114497 static int fts3FilterMethod(
114498   sqlite3_vtab_cursor *pCursor,   /* The cursor used for this query */
114499   int idxNum,                     /* Strategy index */
114500   const char *idxStr,             /* Unused */
114501   int nVal,                       /* Number of elements in apVal */
114502   sqlite3_value **apVal           /* Arguments for the indexing scheme */
114503 ){
114504   int rc;
114505   char *zSql;                     /* SQL statement used to access %_content */
114506   Fts3Table *p = (Fts3Table *)pCursor->pVtab;
114507   Fts3Cursor *pCsr = (Fts3Cursor *)pCursor;
114508 
114509   UNUSED_PARAMETER(idxStr);
114510   UNUSED_PARAMETER(nVal);
114511 
114512   assert( idxNum>=0 && idxNum<=(FTS3_FULLTEXT_SEARCH+p->nColumn) );
114513   assert( nVal==0 || nVal==1 );
114514   assert( (nVal==0)==(idxNum==FTS3_FULLSCAN_SEARCH) );
114515   assert( p->pSegments==0 );
114516 
114517   /* In case the cursor has been used before, clear it now. */
114518   sqlite3_finalize(pCsr->pStmt);
114519   sqlite3_free(pCsr->aDoclist);
114520   sqlite3Fts3ExprFree(pCsr->pExpr);
114521   memset(&pCursor[1], 0, sizeof(Fts3Cursor)-sizeof(sqlite3_vtab_cursor));
114522 
114523   if( idxStr ){
114524     pCsr->bDesc = (idxStr[0]=='D');
114525   }else{
114526     pCsr->bDesc = p->bDescIdx;
114527   }
114528   pCsr->eSearch = (i16)idxNum;
114529 
114530   if( idxNum!=FTS3_DOCID_SEARCH && idxNum!=FTS3_FULLSCAN_SEARCH ){
114531     int iCol = idxNum-FTS3_FULLTEXT_SEARCH;
114532     const char *zQuery = (const char *)sqlite3_value_text(apVal[0]);
114533 
114534     if( zQuery==0 && sqlite3_value_type(apVal[0])!=SQLITE_NULL ){
114535       return SQLITE_NOMEM;
114536     }
114537 
114538     rc = sqlite3Fts3ExprParse(p->pTokenizer, p->azColumn, p->nColumn,
114539         iCol, zQuery, -1, &pCsr->pExpr
114540     );
114541     if( rc!=SQLITE_OK ){
114542       if( rc==SQLITE_ERROR ){
114543         static const char *zErr = "malformed MATCH expression: [%s]";
114544         p->base.zErrMsg = sqlite3_mprintf(zErr, zQuery);
114545       }
114546       return rc;
114547     }
114548 
114549     rc = sqlite3Fts3ReadLock(p);
114550     if( rc!=SQLITE_OK ) return rc;
114551 
114552     rc = sqlite3Fts3EvalStart(pCsr, pCsr->pExpr, 1);
114553 
114554     sqlite3Fts3SegmentsClose(p);
114555     if( rc!=SQLITE_OK ) return rc;
114556     pCsr->pNextId = pCsr->aDoclist;
114557     pCsr->iPrevId = 0;
114558   }
114559 
114560   /* Compile a SELECT statement for this cursor. For a full-table-scan, the
114561   ** statement loops through all rows of the %_content table. For a
114562   ** full-text query or docid lookup, the statement retrieves a single
114563   ** row by docid.
114564   */
114565   if( idxNum==FTS3_FULLSCAN_SEARCH ){
114566     const char *zSort = (pCsr->bDesc ? "DESC" : "ASC");
114567     const char *zTmpl = "SELECT %s FROM %Q.'%q_content' AS x ORDER BY docid %s";
114568     zSql = sqlite3_mprintf(zTmpl, p->zReadExprlist, p->zDb, p->zName, zSort);
114569   }else{
114570     const char *zTmpl = "SELECT %s FROM %Q.'%q_content' AS x WHERE docid = ?";
114571     zSql = sqlite3_mprintf(zTmpl, p->zReadExprlist, p->zDb, p->zName);
114572   }
114573   if( !zSql ) return SQLITE_NOMEM;
114574   rc = sqlite3_prepare_v2(p->db, zSql, -1, &pCsr->pStmt, 0);
114575   sqlite3_free(zSql);
114576   if( rc!=SQLITE_OK ) return rc;
114577 
114578   if( idxNum==FTS3_DOCID_SEARCH ){
114579     rc = sqlite3_bind_value(pCsr->pStmt, 1, apVal[0]);
114580     if( rc!=SQLITE_OK ) return rc;
114581   }
114582 
114583   return fts3NextMethod(pCursor);
114584 }
114585 
114586 /*
114587 ** This is the xEof method of the virtual table. SQLite calls this
114588 ** routine to find out if it has reached the end of a result set.
114589 */
114590 static int fts3EofMethod(sqlite3_vtab_cursor *pCursor){
114591   return ((Fts3Cursor *)pCursor)->isEof;
114592 }
114593 
114594 /*
114595 ** This is the xRowid method. The SQLite core calls this routine to
114596 ** retrieve the rowid for the current row of the result set. fts3
114597 ** exposes %_content.docid as the rowid for the virtual table. The
114598 ** rowid should be written to *pRowid.
114599 */
114600 static int fts3RowidMethod(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
114601   Fts3Cursor *pCsr = (Fts3Cursor *) pCursor;
114602   *pRowid = pCsr->iPrevId;
114603   return SQLITE_OK;
114604 }
114605 
114606 /*
114607 ** This is the xColumn method, called by SQLite to request a value from
114608 ** the row that the supplied cursor currently points to.
114609 */
114610 static int fts3ColumnMethod(
114611   sqlite3_vtab_cursor *pCursor,   /* Cursor to retrieve value from */
114612   sqlite3_context *pContext,      /* Context for sqlite3_result_xxx() calls */
114613   int iCol                        /* Index of column to read value from */
114614 ){
114615   int rc = SQLITE_OK;             /* Return Code */
114616   Fts3Cursor *pCsr = (Fts3Cursor *) pCursor;
114617   Fts3Table *p = (Fts3Table *)pCursor->pVtab;
114618 
114619   /* The column value supplied by SQLite must be in range. */
114620   assert( iCol>=0 && iCol<=p->nColumn+1 );
114621 
114622   if( iCol==p->nColumn+1 ){
114623     /* This call is a request for the "docid" column. Since "docid" is an
114624     ** alias for "rowid", use the xRowid() method to obtain the value.
114625     */
114626     sqlite3_result_int64(pContext, pCsr->iPrevId);
114627   }else if( iCol==p->nColumn ){
114628     /* The extra column whose name is the same as the table.
114629     ** Return a blob which is a pointer to the cursor.
114630     */
114631     sqlite3_result_blob(pContext, &pCsr, sizeof(pCsr), SQLITE_TRANSIENT);
114632   }else{
114633     rc = fts3CursorSeek(0, pCsr);
114634     if( rc==SQLITE_OK ){
114635       sqlite3_result_value(pContext, sqlite3_column_value(pCsr->pStmt, iCol+1));
114636     }
114637   }
114638 
114639   assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
114640   return rc;
114641 }
114642 
114643 /*
114644 ** This function is the implementation of the xUpdate callback used by
114645 ** FTS3 virtual tables. It is invoked by SQLite each time a row is to be
114646 ** inserted, updated or deleted.
114647 */
114648 static int fts3UpdateMethod(
114649   sqlite3_vtab *pVtab,            /* Virtual table handle */
114650   int nArg,                       /* Size of argument array */
114651   sqlite3_value **apVal,          /* Array of arguments */
114652   sqlite_int64 *pRowid            /* OUT: The affected (or effected) rowid */
114653 ){
114654   return sqlite3Fts3UpdateMethod(pVtab, nArg, apVal, pRowid);
114655 }
114656 
114657 /*
114658 ** Implementation of xSync() method. Flush the contents of the pending-terms
114659 ** hash-table to the database.
114660 */
114661 static int fts3SyncMethod(sqlite3_vtab *pVtab){
114662   int rc = sqlite3Fts3PendingTermsFlush((Fts3Table *)pVtab);
114663   sqlite3Fts3SegmentsClose((Fts3Table *)pVtab);
114664   return rc;
114665 }
114666 
114667 /*
114668 ** Implementation of xBegin() method. This is a no-op.
114669 */
114670 static int fts3BeginMethod(sqlite3_vtab *pVtab){
114671   TESTONLY( Fts3Table *p = (Fts3Table*)pVtab );
114672   UNUSED_PARAMETER(pVtab);
114673   assert( p->pSegments==0 );
114674   assert( p->nPendingData==0 );
114675   assert( p->inTransaction!=1 );
114676   TESTONLY( p->inTransaction = 1 );
114677   TESTONLY( p->mxSavepoint = -1; );
114678   return SQLITE_OK;
114679 }
114680 
114681 /*
114682 ** Implementation of xCommit() method. This is a no-op. The contents of
114683 ** the pending-terms hash-table have already been flushed into the database
114684 ** by fts3SyncMethod().
114685 */
114686 static int fts3CommitMethod(sqlite3_vtab *pVtab){
114687   TESTONLY( Fts3Table *p = (Fts3Table*)pVtab );
114688   UNUSED_PARAMETER(pVtab);
114689   assert( p->nPendingData==0 );
114690   assert( p->inTransaction!=0 );
114691   assert( p->pSegments==0 );
114692   TESTONLY( p->inTransaction = 0 );
114693   TESTONLY( p->mxSavepoint = -1; );
114694   return SQLITE_OK;
114695 }
114696 
114697 /*
114698 ** Implementation of xRollback(). Discard the contents of the pending-terms
114699 ** hash-table. Any changes made to the database are reverted by SQLite.
114700 */
114701 static int fts3RollbackMethod(sqlite3_vtab *pVtab){
114702   Fts3Table *p = (Fts3Table*)pVtab;
114703   sqlite3Fts3PendingTermsClear(p);
114704   assert( p->inTransaction!=0 );
114705   TESTONLY( p->inTransaction = 0 );
114706   TESTONLY( p->mxSavepoint = -1; );
114707   return SQLITE_OK;
114708 }
114709 
114710 /*
114711 ** When called, *ppPoslist must point to the byte immediately following the
114712 ** end of a position-list. i.e. ( (*ppPoslist)[-1]==POS_END ). This function
114713 ** moves *ppPoslist so that it instead points to the first byte of the
114714 ** same position list.
114715 */
114716 static void fts3ReversePoslist(char *pStart, char **ppPoslist){
114717   char *p = &(*ppPoslist)[-2];
114718   char c;
114719 
114720   while( p>pStart && (c=*p--)==0 );
114721   while( p>pStart && (*p & 0x80) | c ){
114722     c = *p--;
114723   }
114724   if( p>pStart ){ p = &p[2]; }
114725   while( *p++&0x80 );
114726   *ppPoslist = p;
114727 }
114728 
114729 /*
114730 ** Helper function used by the implementation of the overloaded snippet(),
114731 ** offsets() and optimize() SQL functions.
114732 **
114733 ** If the value passed as the third argument is a blob of size
114734 ** sizeof(Fts3Cursor*), then the blob contents are copied to the
114735 ** output variable *ppCsr and SQLITE_OK is returned. Otherwise, an error
114736 ** message is written to context pContext and SQLITE_ERROR returned. The
114737 ** string passed via zFunc is used as part of the error message.
114738 */
114739 static int fts3FunctionArg(
114740   sqlite3_context *pContext,      /* SQL function call context */
114741   const char *zFunc,              /* Function name */
114742   sqlite3_value *pVal,            /* argv[0] passed to function */
114743   Fts3Cursor **ppCsr              /* OUT: Store cursor handle here */
114744 ){
114745   Fts3Cursor *pRet;
114746   if( sqlite3_value_type(pVal)!=SQLITE_BLOB
114747    || sqlite3_value_bytes(pVal)!=sizeof(Fts3Cursor *)
114748   ){
114749     char *zErr = sqlite3_mprintf("illegal first argument to %s", zFunc);
114750     sqlite3_result_error(pContext, zErr, -1);
114751     sqlite3_free(zErr);
114752     return SQLITE_ERROR;
114753   }
114754   memcpy(&pRet, sqlite3_value_blob(pVal), sizeof(Fts3Cursor *));
114755   *ppCsr = pRet;
114756   return SQLITE_OK;
114757 }
114758 
114759 /*
114760 ** Implementation of the snippet() function for FTS3
114761 */
114762 static void fts3SnippetFunc(
114763   sqlite3_context *pContext,      /* SQLite function call context */
114764   int nVal,                       /* Size of apVal[] array */
114765   sqlite3_value **apVal           /* Array of arguments */
114766 ){
114767   Fts3Cursor *pCsr;               /* Cursor handle passed through apVal[0] */
114768   const char *zStart = "<b>";
114769   const char *zEnd = "</b>";
114770   const char *zEllipsis = "<b>...</b>";
114771   int iCol = -1;
114772   int nToken = 15;                /* Default number of tokens in snippet */
114773 
114774   /* There must be at least one argument passed to this function (otherwise
114775   ** the non-overloaded version would have been called instead of this one).
114776   */
114777   assert( nVal>=1 );
114778 
114779   if( nVal>6 ){
114780     sqlite3_result_error(pContext,
114781         "wrong number of arguments to function snippet()", -1);
114782     return;
114783   }
114784   if( fts3FunctionArg(pContext, "snippet", apVal[0], &pCsr) ) return;
114785 
114786   switch( nVal ){
114787     case 6: nToken = sqlite3_value_int(apVal[5]);
114788     case 5: iCol = sqlite3_value_int(apVal[4]);
114789     case 4: zEllipsis = (const char*)sqlite3_value_text(apVal[3]);
114790     case 3: zEnd = (const char*)sqlite3_value_text(apVal[2]);
114791     case 2: zStart = (const char*)sqlite3_value_text(apVal[1]);
114792   }
114793   if( !zEllipsis || !zEnd || !zStart ){
114794     sqlite3_result_error_nomem(pContext);
114795   }else if( SQLITE_OK==fts3CursorSeek(pContext, pCsr) ){
114796     sqlite3Fts3Snippet(pContext, pCsr, zStart, zEnd, zEllipsis, iCol, nToken);
114797   }
114798 }
114799 
114800 /*
114801 ** Implementation of the offsets() function for FTS3
114802 */
114803 static void fts3OffsetsFunc(
114804   sqlite3_context *pContext,      /* SQLite function call context */
114805   int nVal,                       /* Size of argument array */
114806   sqlite3_value **apVal           /* Array of arguments */
114807 ){
114808   Fts3Cursor *pCsr;               /* Cursor handle passed through apVal[0] */
114809 
114810   UNUSED_PARAMETER(nVal);
114811 
114812   assert( nVal==1 );
114813   if( fts3FunctionArg(pContext, "offsets", apVal[0], &pCsr) ) return;
114814   assert( pCsr );
114815   if( SQLITE_OK==fts3CursorSeek(pContext, pCsr) ){
114816     sqlite3Fts3Offsets(pContext, pCsr);
114817   }
114818 }
114819 
114820 /*
114821 ** Implementation of the special optimize() function for FTS3. This
114822 ** function merges all segments in the database to a single segment.
114823 ** Example usage is:
114824 **
114825 **   SELECT optimize(t) FROM t LIMIT 1;
114826 **
114827 ** where 't' is the name of an FTS3 table.
114828 */
114829 static void fts3OptimizeFunc(
114830   sqlite3_context *pContext,      /* SQLite function call context */
114831   int nVal,                       /* Size of argument array */
114832   sqlite3_value **apVal           /* Array of arguments */
114833 ){
114834   int rc;                         /* Return code */
114835   Fts3Table *p;                   /* Virtual table handle */
114836   Fts3Cursor *pCursor;            /* Cursor handle passed through apVal[0] */
114837 
114838   UNUSED_PARAMETER(nVal);
114839 
114840   assert( nVal==1 );
114841   if( fts3FunctionArg(pContext, "optimize", apVal[0], &pCursor) ) return;
114842   p = (Fts3Table *)pCursor->base.pVtab;
114843   assert( p );
114844 
114845   rc = sqlite3Fts3Optimize(p);
114846 
114847   switch( rc ){
114848     case SQLITE_OK:
114849       sqlite3_result_text(pContext, "Index optimized", -1, SQLITE_STATIC);
114850       break;
114851     case SQLITE_DONE:
114852       sqlite3_result_text(pContext, "Index already optimal", -1, SQLITE_STATIC);
114853       break;
114854     default:
114855       sqlite3_result_error_code(pContext, rc);
114856       break;
114857   }
114858 }
114859 
114860 /*
114861 ** Implementation of the matchinfo() function for FTS3
114862 */
114863 static void fts3MatchinfoFunc(
114864   sqlite3_context *pContext,      /* SQLite function call context */
114865   int nVal,                       /* Size of argument array */
114866   sqlite3_value **apVal           /* Array of arguments */
114867 ){
114868   Fts3Cursor *pCsr;               /* Cursor handle passed through apVal[0] */
114869   assert( nVal==1 || nVal==2 );
114870   if( SQLITE_OK==fts3FunctionArg(pContext, "matchinfo", apVal[0], &pCsr) ){
114871     const char *zArg = 0;
114872     if( nVal>1 ){
114873       zArg = (const char *)sqlite3_value_text(apVal[1]);
114874     }
114875     sqlite3Fts3Matchinfo(pContext, pCsr, zArg);
114876   }
114877 }
114878 
114879 /*
114880 ** This routine implements the xFindFunction method for the FTS3
114881 ** virtual table.
114882 */
114883 static int fts3FindFunctionMethod(
114884   sqlite3_vtab *pVtab,            /* Virtual table handle */
114885   int nArg,                       /* Number of SQL function arguments */
114886   const char *zName,              /* Name of SQL function */
114887   void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), /* OUT: Result */
114888   void **ppArg                    /* Unused */
114889 ){
114890   struct Overloaded {
114891     const char *zName;
114892     void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
114893   } aOverload[] = {
114894     { "snippet", fts3SnippetFunc },
114895     { "offsets", fts3OffsetsFunc },
114896     { "optimize", fts3OptimizeFunc },
114897     { "matchinfo", fts3MatchinfoFunc },
114898   };
114899   int i;                          /* Iterator variable */
114900 
114901   UNUSED_PARAMETER(pVtab);
114902   UNUSED_PARAMETER(nArg);
114903   UNUSED_PARAMETER(ppArg);
114904 
114905   for(i=0; i<SizeofArray(aOverload); i++){
114906     if( strcmp(zName, aOverload[i].zName)==0 ){
114907       *pxFunc = aOverload[i].xFunc;
114908       return 1;
114909     }
114910   }
114911 
114912   /* No function of the specified name was found. Return 0. */
114913   return 0;
114914 }
114915 
114916 /*
114917 ** Implementation of FTS3 xRename method. Rename an fts3 table.
114918 */
114919 static int fts3RenameMethod(
114920   sqlite3_vtab *pVtab,            /* Virtual table handle */
114921   const char *zName               /* New name of table */
114922 ){
114923   Fts3Table *p = (Fts3Table *)pVtab;
114924   sqlite3 *db = p->db;            /* Database connection */
114925   int rc;                         /* Return Code */
114926 
114927   rc = sqlite3Fts3PendingTermsFlush(p);
114928   if( rc!=SQLITE_OK ){
114929     return rc;
114930   }
114931 
114932   fts3DbExec(&rc, db,
114933     "ALTER TABLE %Q.'%q_content'  RENAME TO '%q_content';",
114934     p->zDb, p->zName, zName
114935   );
114936   if( p->bHasDocsize ){
114937     fts3DbExec(&rc, db,
114938       "ALTER TABLE %Q.'%q_docsize'  RENAME TO '%q_docsize';",
114939       p->zDb, p->zName, zName
114940     );
114941   }
114942   if( p->bHasStat ){
114943     fts3DbExec(&rc, db,
114944       "ALTER TABLE %Q.'%q_stat'  RENAME TO '%q_stat';",
114945       p->zDb, p->zName, zName
114946     );
114947   }
114948   fts3DbExec(&rc, db,
114949     "ALTER TABLE %Q.'%q_segments' RENAME TO '%q_segments';",
114950     p->zDb, p->zName, zName
114951   );
114952   fts3DbExec(&rc, db,
114953     "ALTER TABLE %Q.'%q_segdir'   RENAME TO '%q_segdir';",
114954     p->zDb, p->zName, zName
114955   );
114956   return rc;
114957 }
114958 
114959 static int fts3SavepointMethod(sqlite3_vtab *pVtab, int iSavepoint){
114960   UNUSED_PARAMETER(iSavepoint);
114961   assert( ((Fts3Table *)pVtab)->inTransaction );
114962   assert( ((Fts3Table *)pVtab)->mxSavepoint < iSavepoint );
114963   TESTONLY( ((Fts3Table *)pVtab)->mxSavepoint = iSavepoint );
114964   return fts3SyncMethod(pVtab);
114965 }
114966 static int fts3ReleaseMethod(sqlite3_vtab *pVtab, int iSavepoint){
114967   TESTONLY( Fts3Table *p = (Fts3Table*)pVtab );
114968   UNUSED_PARAMETER(iSavepoint);
114969   UNUSED_PARAMETER(pVtab);
114970   assert( p->inTransaction );
114971   assert( p->mxSavepoint >= iSavepoint );
114972   TESTONLY( p->mxSavepoint = iSavepoint-1 );
114973   return SQLITE_OK;
114974 }
114975 static int fts3RollbackToMethod(sqlite3_vtab *pVtab, int iSavepoint){
114976   Fts3Table *p = (Fts3Table*)pVtab;
114977   UNUSED_PARAMETER(iSavepoint);
114978   assert( p->inTransaction );
114979   assert( p->mxSavepoint >= iSavepoint );
114980   TESTONLY( p->mxSavepoint = iSavepoint );
114981   sqlite3Fts3PendingTermsClear(p);
114982   return SQLITE_OK;
114983 }
114984 
114985 static const sqlite3_module fts3Module = {
114986   /* iVersion      */ 2,
114987   /* xCreate       */ fts3CreateMethod,
114988   /* xConnect      */ fts3ConnectMethod,
114989   /* xBestIndex    */ fts3BestIndexMethod,
114990   /* xDisconnect   */ fts3DisconnectMethod,
114991   /* xDestroy      */ fts3DestroyMethod,
114992   /* xOpen         */ fts3OpenMethod,
114993   /* xClose        */ fts3CloseMethod,
114994   /* xFilter       */ fts3FilterMethod,
114995   /* xNext         */ fts3NextMethod,
114996   /* xEof          */ fts3EofMethod,
114997   /* xColumn       */ fts3ColumnMethod,
114998   /* xRowid        */ fts3RowidMethod,
114999   /* xUpdate       */ fts3UpdateMethod,
115000   /* xBegin        */ fts3BeginMethod,
115001   /* xSync         */ fts3SyncMethod,
115002   /* xCommit       */ fts3CommitMethod,
115003   /* xRollback     */ fts3RollbackMethod,
115004   /* xFindFunction */ fts3FindFunctionMethod,
115005   /* xRename */       fts3RenameMethod,
115006   /* xSavepoint    */ fts3SavepointMethod,
115007   /* xRelease      */ fts3ReleaseMethod,
115008   /* xRollbackTo   */ fts3RollbackToMethod,
115009 };
115010 
115011 /*
115012 ** This function is registered as the module destructor (called when an
115013 ** FTS3 enabled database connection is closed). It frees the memory
115014 ** allocated for the tokenizer hash table.
115015 */
115016 static void hashDestroy(void *p){
115017   Fts3Hash *pHash = (Fts3Hash *)p;
115018   sqlite3Fts3HashClear(pHash);
115019   sqlite3_free(pHash);
115020 }
115021 
115022 /*
115023 ** The fts3 built-in tokenizers - "simple", "porter" and "icu"- are
115024 ** implemented in files fts3_tokenizer1.c, fts3_porter.c and fts3_icu.c
115025 ** respectively. The following three forward declarations are for functions
115026 ** declared in these files used to retrieve the respective implementations.
115027 **
115028 ** Calling sqlite3Fts3SimpleTokenizerModule() sets the value pointed
115029 ** to by the argument to point to the "simple" tokenizer implementation.
115030 ** And so on.
115031 */
115032 SQLITE_PRIVATE void sqlite3Fts3SimpleTokenizerModule(sqlite3_tokenizer_module const**ppModule);
115033 SQLITE_PRIVATE void sqlite3Fts3PorterTokenizerModule(sqlite3_tokenizer_module const**ppModule);
115034 #ifdef SQLITE_ENABLE_ICU
115035 SQLITE_PRIVATE void sqlite3Fts3IcuTokenizerModule(sqlite3_tokenizer_module const**ppModule);
115036 #endif
115037 
115038 /*
115039 ** Initialise the fts3 extension. If this extension is built as part
115040 ** of the sqlite library, then this function is called directly by
115041 ** SQLite. If fts3 is built as a dynamically loadable extension, this
115042 ** function is called by the sqlite3_extension_init() entry point.
115043 */
115044 SQLITE_PRIVATE int sqlite3Fts3Init(sqlite3 *db){
115045   int rc = SQLITE_OK;
115046   Fts3Hash *pHash = 0;
115047   const sqlite3_tokenizer_module *pSimple = 0;
115048   const sqlite3_tokenizer_module *pPorter = 0;
115049 
115050 #ifdef SQLITE_ENABLE_ICU
115051   const sqlite3_tokenizer_module *pIcu = 0;
115052   sqlite3Fts3IcuTokenizerModule(&pIcu);
115053 #endif
115054 
115055 #ifdef SQLITE_TEST
115056   rc = sqlite3Fts3InitTerm(db);
115057   if( rc!=SQLITE_OK ) return rc;
115058 #endif
115059 
115060   rc = sqlite3Fts3InitAux(db);
115061   if( rc!=SQLITE_OK ) return rc;
115062 
115063   sqlite3Fts3SimpleTokenizerModule(&pSimple);
115064   sqlite3Fts3PorterTokenizerModule(&pPorter);
115065 
115066   /* Allocate and initialise the hash-table used to store tokenizers. */
115067   pHash = sqlite3_malloc(sizeof(Fts3Hash));
115068   if( !pHash ){
115069     rc = SQLITE_NOMEM;
115070   }else{
115071     sqlite3Fts3HashInit(pHash, FTS3_HASH_STRING, 1);
115072   }
115073 
115074   /* Load the built-in tokenizers into the hash table */
115075   if( rc==SQLITE_OK ){
115076     if( sqlite3Fts3HashInsert(pHash, "simple", 7, (void *)pSimple)
115077      || sqlite3Fts3HashInsert(pHash, "porter", 7, (void *)pPorter)
115078 #ifdef SQLITE_ENABLE_ICU
115079      || (pIcu && sqlite3Fts3HashInsert(pHash, "icu", 4, (void *)pIcu))
115080 #endif
115081     ){
115082       rc = SQLITE_NOMEM;
115083     }
115084   }
115085 
115086 #ifdef SQLITE_TEST
115087   if( rc==SQLITE_OK ){
115088     rc = sqlite3Fts3ExprInitTestInterface(db);
115089   }
115090 #endif
115091 
115092   /* Create the virtual table wrapper around the hash-table and overload
115093   ** the two scalar functions. If this is successful, register the
115094   ** module with sqlite.
115095   */
115096   if( SQLITE_OK==rc
115097    && SQLITE_OK==(rc = sqlite3Fts3InitHashTable(db, pHash, "fts3_tokenizer"))
115098    && SQLITE_OK==(rc = sqlite3_overload_function(db, "snippet", -1))
115099    && SQLITE_OK==(rc = sqlite3_overload_function(db, "offsets", 1))
115100    && SQLITE_OK==(rc = sqlite3_overload_function(db, "matchinfo", 1))
115101    && SQLITE_OK==(rc = sqlite3_overload_function(db, "matchinfo", 2))
115102    && SQLITE_OK==(rc = sqlite3_overload_function(db, "optimize", 1))
115103   ){
115104     rc = sqlite3_create_module_v2(
115105         db, "fts3", &fts3Module, (void *)pHash, hashDestroy
115106     );
115107     if( rc==SQLITE_OK ){
115108       rc = sqlite3_create_module_v2(
115109           db, "fts4", &fts3Module, (void *)pHash, 0
115110       );
115111     }
115112     return rc;
115113   }
115114 
115115   /* An error has occurred. Delete the hash table and return the error code. */
115116   assert( rc!=SQLITE_OK );
115117   if( pHash ){
115118     sqlite3Fts3HashClear(pHash);
115119     sqlite3_free(pHash);
115120   }
115121   return rc;
115122 }
115123 
115124 #if !SQLITE_CORE
115125 SQLITE_API int sqlite3_extension_init(
115126   sqlite3 *db,
115127   char **pzErrMsg,
115128   const sqlite3_api_routines *pApi
115129 ){
115130   SQLITE_EXTENSION_INIT2(pApi)
115131   return sqlite3Fts3Init(db);
115132 }
115133 #endif
115134 
115135 
115136 /*
115137 ** Allocate an Fts3MultiSegReader for each token in the expression headed
115138 ** by pExpr.
115139 **
115140 ** An Fts3SegReader object is a cursor that can seek or scan a range of
115141 ** entries within a single segment b-tree. An Fts3MultiSegReader uses multiple
115142 ** Fts3SegReader objects internally to provide an interface to seek or scan
115143 ** within the union of all segments of a b-tree. Hence the name.
115144 **
115145 ** If the allocated Fts3MultiSegReader just seeks to a single entry in a
115146 ** segment b-tree (if the term is not a prefix or it is a prefix for which
115147 ** there exists prefix b-tree of the right length) then it may be traversed
115148 ** and merged incrementally. Otherwise, it has to be merged into an in-memory
115149 ** doclist and then traversed.
115150 */
115151 static void fts3EvalAllocateReaders(
115152   Fts3Cursor *pCsr,
115153   Fts3Expr *pExpr,
115154   int *pnToken,                   /* OUT: Total number of tokens in phrase. */
115155   int *pnOr,                      /* OUT: Total number of OR nodes in expr. */
115156   int *pRc
115157 ){
115158   if( pExpr && SQLITE_OK==*pRc ){
115159     if( pExpr->eType==FTSQUERY_PHRASE ){
115160       int i;
115161       int nToken = pExpr->pPhrase->nToken;
115162       *pnToken += nToken;
115163       for(i=0; i<nToken; i++){
115164         Fts3PhraseToken *pToken = &pExpr->pPhrase->aToken[i];
115165         int rc = sqlite3Fts3TermSegReaderCursor(pCsr,
115166             pToken->z, pToken->n, pToken->isPrefix, &pToken->pSegcsr
115167         );
115168         if( rc!=SQLITE_OK ){
115169           *pRc = rc;
115170           return;
115171         }
115172       }
115173       assert( pExpr->pPhrase->iDoclistToken==0 );
115174       pExpr->pPhrase->iDoclistToken = -1;
115175     }else{
115176       *pnOr += (pExpr->eType==FTSQUERY_OR);
115177       fts3EvalAllocateReaders(pCsr, pExpr->pLeft, pnToken, pnOr, pRc);
115178       fts3EvalAllocateReaders(pCsr, pExpr->pRight, pnToken, pnOr, pRc);
115179     }
115180   }
115181 }
115182 
115183 static void fts3EvalPhraseMergeToken(
115184   Fts3Table *pTab,
115185   Fts3Phrase *p,
115186   int iToken,
115187   char *pList,
115188   int nList
115189 ){
115190   assert( iToken!=p->iDoclistToken );
115191 
115192   if( pList==0 ){
115193     sqlite3_free(p->doclist.aAll);
115194     p->doclist.aAll = 0;
115195     p->doclist.nAll = 0;
115196   }
115197 
115198   else if( p->iDoclistToken<0 ){
115199     p->doclist.aAll = pList;
115200     p->doclist.nAll = nList;
115201   }
115202 
115203   else if( p->doclist.aAll==0 ){
115204     sqlite3_free(pList);
115205   }
115206 
115207   else {
115208     char *pLeft;
115209     char *pRight;
115210     int nLeft;
115211     int nRight;
115212     int nDiff;
115213 
115214     if( p->iDoclistToken<iToken ){
115215       pLeft = p->doclist.aAll;
115216       nLeft = p->doclist.nAll;
115217       pRight = pList;
115218       nRight = nList;
115219       nDiff = iToken - p->iDoclistToken;
115220     }else{
115221       pRight = p->doclist.aAll;
115222       nRight = p->doclist.nAll;
115223       pLeft = pList;
115224       nLeft = nList;
115225       nDiff = p->iDoclistToken - iToken;
115226     }
115227 
115228     fts3DoclistPhraseMerge(pTab->bDescIdx, nDiff, pLeft, nLeft, pRight,&nRight);
115229     sqlite3_free(pLeft);
115230     p->doclist.aAll = pRight;
115231     p->doclist.nAll = nRight;
115232   }
115233 
115234   if( iToken>p->iDoclistToken ) p->iDoclistToken = iToken;
115235 }
115236 
115237 static int fts3EvalPhraseLoad(
115238   Fts3Cursor *pCsr,
115239   Fts3Phrase *p
115240 ){
115241   Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
115242   int iToken;
115243   int rc = SQLITE_OK;
115244 
115245   for(iToken=0; rc==SQLITE_OK && iToken<p->nToken; iToken++){
115246     Fts3PhraseToken *pToken = &p->aToken[iToken];
115247     assert( pToken->pDeferred==0 || pToken->pSegcsr==0 );
115248 
115249     if( pToken->pSegcsr ){
115250       int nThis = 0;
115251       char *pThis = 0;
115252       rc = fts3TermSelect(pTab, pToken, p->iColumn, 1, &nThis, &pThis);
115253       if( rc==SQLITE_OK ){
115254         fts3EvalPhraseMergeToken(pTab, p, iToken, pThis, nThis);
115255       }
115256     }
115257     assert( pToken->pSegcsr==0 );
115258   }
115259 
115260   return rc;
115261 }
115262 
115263 static int fts3EvalDeferredPhrase(Fts3Cursor *pCsr, Fts3Phrase *pPhrase){
115264   int iToken;
115265   int rc = SQLITE_OK;
115266 
115267   int nMaxUndeferred = pPhrase->iDoclistToken;
115268   char *aPoslist = 0;
115269   int nPoslist = 0;
115270   int iPrev = -1;
115271 
115272   assert( pPhrase->doclist.bFreeList==0 );
115273 
115274   for(iToken=0; rc==SQLITE_OK && iToken<pPhrase->nToken; iToken++){
115275     Fts3PhraseToken *pToken = &pPhrase->aToken[iToken];
115276     Fts3DeferredToken *pDeferred = pToken->pDeferred;
115277 
115278     if( pDeferred ){
115279       char *pList;
115280       int nList;
115281       rc = sqlite3Fts3DeferredTokenList(pDeferred, &pList, &nList);
115282       if( rc!=SQLITE_OK ) return rc;
115283 
115284       if( pList==0 ){
115285         sqlite3_free(aPoslist);
115286         pPhrase->doclist.pList = 0;
115287         pPhrase->doclist.nList = 0;
115288         return SQLITE_OK;
115289 
115290       }else if( aPoslist==0 ){
115291         aPoslist = pList;
115292         nPoslist = nList;
115293 
115294       }else{
115295         char *aOut = pList;
115296         char *p1 = aPoslist;
115297         char *p2 = aOut;
115298 
115299         assert( iPrev>=0 );
115300         fts3PoslistPhraseMerge(&aOut, iToken-iPrev, 0, 1, &p1, &p2);
115301         sqlite3_free(aPoslist);
115302         aPoslist = pList;
115303         nPoslist = aOut - aPoslist;
115304         if( nPoslist==0 ){
115305           sqlite3_free(aPoslist);
115306           pPhrase->doclist.pList = 0;
115307           pPhrase->doclist.nList = 0;
115308           return SQLITE_OK;
115309         }
115310       }
115311       iPrev = iToken;
115312     }
115313   }
115314 
115315   if( iPrev>=0 ){
115316     if( nMaxUndeferred<0 ){
115317       pPhrase->doclist.pList = aPoslist;
115318       pPhrase->doclist.nList = nPoslist;
115319       pPhrase->doclist.iDocid = pCsr->iPrevId;
115320       pPhrase->doclist.bFreeList = 1;
115321     }else{
115322       int nDistance;
115323       char *p1;
115324       char *p2;
115325       char *aOut;
115326 
115327       if( nMaxUndeferred>iPrev ){
115328         p1 = aPoslist;
115329         p2 = pPhrase->doclist.pList;
115330         nDistance = nMaxUndeferred - iPrev;
115331       }else{
115332         p1 = pPhrase->doclist.pList;
115333         p2 = aPoslist;
115334         nDistance = iPrev - nMaxUndeferred;
115335       }
115336 
115337       aOut = (char *)sqlite3_malloc(nPoslist+8);
115338       if( !aOut ){
115339         sqlite3_free(aPoslist);
115340         return SQLITE_NOMEM;
115341       }
115342 
115343       pPhrase->doclist.pList = aOut;
115344       if( fts3PoslistPhraseMerge(&aOut, nDistance, 0, 1, &p1, &p2) ){
115345         pPhrase->doclist.bFreeList = 1;
115346         pPhrase->doclist.nList = (aOut - pPhrase->doclist.pList);
115347       }else{
115348         sqlite3_free(aOut);
115349         pPhrase->doclist.pList = 0;
115350         pPhrase->doclist.nList = 0;
115351       }
115352       sqlite3_free(aPoslist);
115353     }
115354   }
115355 
115356   return SQLITE_OK;
115357 }
115358 
115359 /*
115360 ** This function is called for each Fts3Phrase in a full-text query
115361 ** expression to initialize the mechanism for returning rows. Once this
115362 ** function has been called successfully on an Fts3Phrase, it may be
115363 ** used with fts3EvalPhraseNext() to iterate through the matching docids.
115364 */
115365 static int fts3EvalPhraseStart(Fts3Cursor *pCsr, int bOptOk, Fts3Phrase *p){
115366   int rc;
115367   Fts3PhraseToken *pFirst = &p->aToken[0];
115368   Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
115369 
115370   if( pCsr->bDesc==pTab->bDescIdx
115371    && bOptOk==1
115372    && p->nToken==1
115373    && pFirst->pSegcsr
115374    && pFirst->pSegcsr->bLookup
115375   ){
115376     /* Use the incremental approach. */
115377     int iCol = (p->iColumn >= pTab->nColumn ? -1 : p->iColumn);
115378     rc = sqlite3Fts3MsrIncrStart(
115379         pTab, pFirst->pSegcsr, iCol, pFirst->z, pFirst->n);
115380     p->bIncr = 1;
115381 
115382   }else{
115383     /* Load the full doclist for the phrase into memory. */
115384     rc = fts3EvalPhraseLoad(pCsr, p);
115385     p->bIncr = 0;
115386   }
115387 
115388   assert( rc!=SQLITE_OK || p->nToken<1 || p->aToken[0].pSegcsr==0 || p->bIncr );
115389   return rc;
115390 }
115391 
115392 /*
115393 ** This function is used to iterate backwards (from the end to start)
115394 ** through doclists.
115395 */
115396 SQLITE_PRIVATE void sqlite3Fts3DoclistPrev(
115397   int bDescIdx,                   /* True if the doclist is desc */
115398   char *aDoclist,                 /* Pointer to entire doclist */
115399   int nDoclist,                   /* Length of aDoclist in bytes */
115400   char **ppIter,                  /* IN/OUT: Iterator pointer */
115401   sqlite3_int64 *piDocid,         /* IN/OUT: Docid pointer */
115402   int *pnList,                    /* IN/OUT: List length pointer */
115403   u8 *pbEof                       /* OUT: End-of-file flag */
115404 ){
115405   char *p = *ppIter;
115406 
115407   assert( nDoclist>0 );
115408   assert( *pbEof==0 );
115409   assert( p || *piDocid==0 );
115410   assert( !p || (p>aDoclist && p<&aDoclist[nDoclist]) );
115411 
115412   if( p==0 ){
115413     sqlite3_int64 iDocid = 0;
115414     char *pNext = 0;
115415     char *pDocid = aDoclist;
115416     char *pEnd = &aDoclist[nDoclist];
115417     int iMul = 1;
115418 
115419     while( pDocid<pEnd ){
115420       sqlite3_int64 iDelta;
115421       pDocid += sqlite3Fts3GetVarint(pDocid, &iDelta);
115422       iDocid += (iMul * iDelta);
115423       pNext = pDocid;
115424       fts3PoslistCopy(0, &pDocid);
115425       while( pDocid<pEnd && *pDocid==0 ) pDocid++;
115426       iMul = (bDescIdx ? -1 : 1);
115427     }
115428 
115429     *pnList = pEnd - pNext;
115430     *ppIter = pNext;
115431     *piDocid = iDocid;
115432   }else{
115433     int iMul = (bDescIdx ? -1 : 1);
115434     sqlite3_int64 iDelta;
115435     fts3GetReverseVarint(&p, aDoclist, &iDelta);
115436     *piDocid -= (iMul * iDelta);
115437 
115438     if( p==aDoclist ){
115439       *pbEof = 1;
115440     }else{
115441       char *pSave = p;
115442       fts3ReversePoslist(aDoclist, &p);
115443       *pnList = (pSave - p);
115444     }
115445     *ppIter = p;
115446   }
115447 }
115448 
115449 /*
115450 ** Attempt to move the phrase iterator to point to the next matching docid.
115451 ** If an error occurs, return an SQLite error code. Otherwise, return
115452 ** SQLITE_OK.
115453 **
115454 ** If there is no "next" entry and no error occurs, then *pbEof is set to
115455 ** 1 before returning. Otherwise, if no error occurs and the iterator is
115456 ** successfully advanced, *pbEof is set to 0.
115457 */
115458 static int fts3EvalPhraseNext(
115459   Fts3Cursor *pCsr,
115460   Fts3Phrase *p,
115461   u8 *pbEof
115462 ){
115463   int rc = SQLITE_OK;
115464   Fts3Doclist *pDL = &p->doclist;
115465   Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
115466 
115467   if( p->bIncr ){
115468     assert( p->nToken==1 );
115469     assert( pDL->pNextDocid==0 );
115470     rc = sqlite3Fts3MsrIncrNext(pTab, p->aToken[0].pSegcsr,
115471         &pDL->iDocid, &pDL->pList, &pDL->nList
115472     );
115473     if( rc==SQLITE_OK && !pDL->pList ){
115474       *pbEof = 1;
115475     }
115476   }else if( pCsr->bDesc!=pTab->bDescIdx && pDL->nAll ){
115477     sqlite3Fts3DoclistPrev(pTab->bDescIdx, pDL->aAll, pDL->nAll,
115478         &pDL->pNextDocid, &pDL->iDocid, &pDL->nList, pbEof
115479     );
115480     pDL->pList = pDL->pNextDocid;
115481   }else{
115482     char *pIter;                            /* Used to iterate through aAll */
115483     char *pEnd = &pDL->aAll[pDL->nAll];     /* 1 byte past end of aAll */
115484     if( pDL->pNextDocid ){
115485       pIter = pDL->pNextDocid;
115486     }else{
115487       pIter = pDL->aAll;
115488     }
115489 
115490     if( pIter>=pEnd ){
115491       /* We have already reached the end of this doclist. EOF. */
115492       *pbEof = 1;
115493     }else{
115494       sqlite3_int64 iDelta;
115495       pIter += sqlite3Fts3GetVarint(pIter, &iDelta);
115496       if( pTab->bDescIdx==0 || pDL->pNextDocid==0 ){
115497         pDL->iDocid += iDelta;
115498       }else{
115499         pDL->iDocid -= iDelta;
115500       }
115501       pDL->pList = pIter;
115502       fts3PoslistCopy(0, &pIter);
115503       pDL->nList = (pIter - pDL->pList);
115504 
115505       /* pIter now points just past the 0x00 that terminates the position-
115506       ** list for document pDL->iDocid. However, if this position-list was
115507       ** edited in place by fts3EvalNearTrim2(), then pIter may not actually
115508       ** point to the start of the next docid value. The following line deals
115509       ** with this case by advancing pIter past the zero-padding added by
115510       ** fts3EvalNearTrim2().  */
115511       while( pIter<pEnd && *pIter==0 ) pIter++;
115512 
115513       pDL->pNextDocid = pIter;
115514       assert( pIter>=&pDL->aAll[pDL->nAll] || *pIter );
115515       *pbEof = 0;
115516     }
115517   }
115518 
115519   return rc;
115520 }
115521 
115522 static void fts3EvalStartReaders(
115523   Fts3Cursor *pCsr,
115524   Fts3Expr *pExpr,
115525   int bOptOk,
115526   int *pRc
115527 ){
115528   if( pExpr && SQLITE_OK==*pRc ){
115529     if( pExpr->eType==FTSQUERY_PHRASE ){
115530       int i;
115531       int nToken = pExpr->pPhrase->nToken;
115532       for(i=0; i<nToken; i++){
115533         if( pExpr->pPhrase->aToken[i].pDeferred==0 ) break;
115534       }
115535       pExpr->bDeferred = (i==nToken);
115536       *pRc = fts3EvalPhraseStart(pCsr, bOptOk, pExpr->pPhrase);
115537     }else{
115538       fts3EvalStartReaders(pCsr, pExpr->pLeft, bOptOk, pRc);
115539       fts3EvalStartReaders(pCsr, pExpr->pRight, bOptOk, pRc);
115540       pExpr->bDeferred = (pExpr->pLeft->bDeferred && pExpr->pRight->bDeferred);
115541     }
115542   }
115543 }
115544 
115545 typedef struct Fts3TokenAndCost Fts3TokenAndCost;
115546 struct Fts3TokenAndCost {
115547   Fts3Phrase *pPhrase;            /* The phrase the token belongs to */
115548   int iToken;                     /* Position of token in phrase */
115549   Fts3PhraseToken *pToken;        /* The token itself */
115550   Fts3Expr *pRoot;
115551   int nOvfl;
115552   int iCol;                       /* The column the token must match */
115553 };
115554 
115555 static void fts3EvalTokenCosts(
115556   Fts3Cursor *pCsr,
115557   Fts3Expr *pRoot,
115558   Fts3Expr *pExpr,
115559   Fts3TokenAndCost **ppTC,
115560   Fts3Expr ***ppOr,
115561   int *pRc
115562 ){
115563   if( *pRc==SQLITE_OK && pExpr ){
115564     if( pExpr->eType==FTSQUERY_PHRASE ){
115565       Fts3Phrase *pPhrase = pExpr->pPhrase;
115566       int i;
115567       for(i=0; *pRc==SQLITE_OK && i<pPhrase->nToken; i++){
115568         Fts3TokenAndCost *pTC = (*ppTC)++;
115569         pTC->pPhrase = pPhrase;
115570         pTC->iToken = i;
115571         pTC->pRoot = pRoot;
115572         pTC->pToken = &pPhrase->aToken[i];
115573         pTC->iCol = pPhrase->iColumn;
115574         *pRc = sqlite3Fts3MsrOvfl(pCsr, pTC->pToken->pSegcsr, &pTC->nOvfl);
115575       }
115576     }else if( pExpr->eType!=FTSQUERY_NOT ){
115577       if( pExpr->eType==FTSQUERY_OR ){
115578         pRoot = pExpr->pLeft;
115579         **ppOr = pRoot;
115580         (*ppOr)++;
115581       }
115582       fts3EvalTokenCosts(pCsr, pRoot, pExpr->pLeft, ppTC, ppOr, pRc);
115583       if( pExpr->eType==FTSQUERY_OR ){
115584         pRoot = pExpr->pRight;
115585         **ppOr = pRoot;
115586         (*ppOr)++;
115587       }
115588       fts3EvalTokenCosts(pCsr, pRoot, pExpr->pRight, ppTC, ppOr, pRc);
115589     }
115590   }
115591 }
115592 
115593 static int fts3EvalAverageDocsize(Fts3Cursor *pCsr, int *pnPage){
115594   if( pCsr->nRowAvg==0 ){
115595     /* The average document size, which is required to calculate the cost
115596      ** of each doclist, has not yet been determined. Read the required
115597      ** data from the %_stat table to calculate it.
115598      **
115599      ** Entry 0 of the %_stat table is a blob containing (nCol+1) FTS3
115600      ** varints, where nCol is the number of columns in the FTS3 table.
115601      ** The first varint is the number of documents currently stored in
115602      ** the table. The following nCol varints contain the total amount of
115603      ** data stored in all rows of each column of the table, from left
115604      ** to right.
115605      */
115606     int rc;
115607     Fts3Table *p = (Fts3Table*)pCsr->base.pVtab;
115608     sqlite3_stmt *pStmt;
115609     sqlite3_int64 nDoc = 0;
115610     sqlite3_int64 nByte = 0;
115611     const char *pEnd;
115612     const char *a;
115613 
115614     rc = sqlite3Fts3SelectDoctotal(p, &pStmt);
115615     if( rc!=SQLITE_OK ) return rc;
115616     a = sqlite3_column_blob(pStmt, 0);
115617     assert( a );
115618 
115619     pEnd = &a[sqlite3_column_bytes(pStmt, 0)];
115620     a += sqlite3Fts3GetVarint(a, &nDoc);
115621     while( a<pEnd ){
115622       a += sqlite3Fts3GetVarint(a, &nByte);
115623     }
115624     if( nDoc==0 || nByte==0 ){
115625       sqlite3_reset(pStmt);
115626       return SQLITE_CORRUPT_VTAB;
115627     }
115628 
115629     pCsr->nDoc = nDoc;
115630     pCsr->nRowAvg = (int)(((nByte / nDoc) + p->nPgsz) / p->nPgsz);
115631     assert( pCsr->nRowAvg>0 );
115632     rc = sqlite3_reset(pStmt);
115633     if( rc!=SQLITE_OK ) return rc;
115634   }
115635 
115636   *pnPage = pCsr->nRowAvg;
115637   return SQLITE_OK;
115638 }
115639 
115640 static int fts3EvalSelectDeferred(
115641   Fts3Cursor *pCsr,
115642   Fts3Expr *pRoot,
115643   Fts3TokenAndCost *aTC,
115644   int nTC
115645 ){
115646   int nDocSize = 0;
115647   int nDocEst = 0;
115648   int rc = SQLITE_OK;
115649   Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
115650   int ii;
115651 
115652   int nOvfl = 0;
115653   int nTerm = 0;
115654 
115655   for(ii=0; ii<nTC; ii++){
115656     if( aTC[ii].pRoot==pRoot ){
115657       nOvfl += aTC[ii].nOvfl;
115658       nTerm++;
115659     }
115660   }
115661   if( nOvfl==0 || nTerm<2 ) return SQLITE_OK;
115662 
115663   rc = fts3EvalAverageDocsize(pCsr, &nDocSize);
115664 
115665   for(ii=0; ii<nTerm && rc==SQLITE_OK; ii++){
115666     int jj;
115667     Fts3TokenAndCost *pTC = 0;
115668 
115669     for(jj=0; jj<nTC; jj++){
115670       if( aTC[jj].pToken && aTC[jj].pRoot==pRoot
115671        && (!pTC || aTC[jj].nOvfl<pTC->nOvfl)
115672       ){
115673         pTC = &aTC[jj];
115674       }
115675     }
115676     assert( pTC );
115677 
115678     /* At this point pTC points to the cheapest remaining token. */
115679     if( ii==0 ){
115680       if( pTC->nOvfl ){
115681         nDocEst = (pTC->nOvfl * pTab->nPgsz + pTab->nPgsz) / 10;
115682       }else{
115683         Fts3PhraseToken *pToken = pTC->pToken;
115684         int nList = 0;
115685         char *pList = 0;
115686         rc = fts3TermSelect(pTab, pToken, pTC->iCol, 1, &nList, &pList);
115687         assert( rc==SQLITE_OK || pList==0 );
115688 
115689         if( rc==SQLITE_OK ){
115690           nDocEst = fts3DoclistCountDocids(1, pList, nList);
115691           fts3EvalPhraseMergeToken(pTab, pTC->pPhrase, pTC->iToken,pList,nList);
115692         }
115693       }
115694     }else{
115695       if( pTC->nOvfl>=(nDocEst*nDocSize) ){
115696         Fts3PhraseToken *pToken = pTC->pToken;
115697         rc = sqlite3Fts3DeferToken(pCsr, pToken, pTC->iCol);
115698         fts3SegReaderCursorFree(pToken->pSegcsr);
115699         pToken->pSegcsr = 0;
115700       }
115701       nDocEst = 1 + (nDocEst/4);
115702     }
115703     pTC->pToken = 0;
115704   }
115705 
115706   return rc;
115707 }
115708 
115709 SQLITE_PRIVATE int sqlite3Fts3EvalStart(Fts3Cursor *pCsr, Fts3Expr *pExpr, int bOptOk){
115710   Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
115711   int rc = SQLITE_OK;
115712   int nToken = 0;
115713   int nOr = 0;
115714 
115715   /* Allocate a MultiSegReader for each token in the expression. */
115716   fts3EvalAllocateReaders(pCsr, pExpr, &nToken, &nOr, &rc);
115717 
115718   /* Call fts3EvalPhraseStart() on all phrases in the expression. TODO:
115719   ** This call will eventually also be responsible for determining which
115720   ** tokens are 'deferred' until the document text is loaded into memory.
115721   **
115722   ** Each token in each phrase is dealt with using one of the following
115723   ** three strategies:
115724   **
115725   **   1. Entire doclist loaded into memory as part of the
115726   **      fts3EvalStartReaders() call.
115727   **
115728   **   2. Doclist loaded into memory incrementally, as part of each
115729   **      sqlite3Fts3EvalNext() call.
115730   **
115731   **   3. Token doclist is never loaded. Instead, documents are loaded into
115732   **      memory and scanned for the token as part of the sqlite3Fts3EvalNext()
115733   **      call. This is known as a "deferred" token.
115734   */
115735 
115736   /* If bOptOk is true, check if there are any tokens that should be deferred.
115737   */
115738   if( rc==SQLITE_OK && bOptOk && nToken>1 && pTab->bHasStat ){
115739     Fts3TokenAndCost *aTC;
115740     Fts3Expr **apOr;
115741     aTC = (Fts3TokenAndCost *)sqlite3_malloc(
115742         sizeof(Fts3TokenAndCost) * nToken
115743       + sizeof(Fts3Expr *) * nOr * 2
115744     );
115745     apOr = (Fts3Expr **)&aTC[nToken];
115746 
115747     if( !aTC ){
115748       rc = SQLITE_NOMEM;
115749     }else{
115750       int ii;
115751       Fts3TokenAndCost *pTC = aTC;
115752       Fts3Expr **ppOr = apOr;
115753 
115754       fts3EvalTokenCosts(pCsr, 0, pExpr, &pTC, &ppOr, &rc);
115755       nToken = pTC-aTC;
115756       nOr = ppOr-apOr;
115757 
115758       if( rc==SQLITE_OK ){
115759         rc = fts3EvalSelectDeferred(pCsr, 0, aTC, nToken);
115760         for(ii=0; rc==SQLITE_OK && ii<nOr; ii++){
115761           rc = fts3EvalSelectDeferred(pCsr, apOr[ii], aTC, nToken);
115762         }
115763       }
115764 
115765       sqlite3_free(aTC);
115766     }
115767   }
115768 
115769   fts3EvalStartReaders(pCsr, pExpr, bOptOk, &rc);
115770   return rc;
115771 }
115772 
115773 static void fts3EvalZeroPoslist(Fts3Phrase *pPhrase){
115774   if( pPhrase->doclist.bFreeList ){
115775     sqlite3_free(pPhrase->doclist.pList);
115776   }
115777   pPhrase->doclist.pList = 0;
115778   pPhrase->doclist.nList = 0;
115779   pPhrase->doclist.bFreeList = 0;
115780 }
115781 
115782 static int fts3EvalNearTrim2(
115783   int nNear,
115784   char *aTmp,                     /* Temporary space to use */
115785   char **paPoslist,               /* IN/OUT: Position list */
115786   int *pnToken,                   /* IN/OUT: Tokens in phrase of *paPoslist */
115787   Fts3Phrase *pPhrase             /* The phrase object to trim the doclist of */
115788 ){
115789   int nParam1 = nNear + pPhrase->nToken;
115790   int nParam2 = nNear + *pnToken;
115791   int nNew;
115792   char *p2;
115793   char *pOut;
115794   int res;
115795 
115796   assert( pPhrase->doclist.pList );
115797 
115798   p2 = pOut = pPhrase->doclist.pList;
115799   res = fts3PoslistNearMerge(
115800     &pOut, aTmp, nParam1, nParam2, paPoslist, &p2
115801   );
115802   if( res ){
115803     nNew = (pOut - pPhrase->doclist.pList) - 1;
115804     assert( pPhrase->doclist.pList[nNew]=='\0' );
115805     assert( nNew<=pPhrase->doclist.nList && nNew>0 );
115806     memset(&pPhrase->doclist.pList[nNew], 0, pPhrase->doclist.nList - nNew);
115807     pPhrase->doclist.nList = nNew;
115808     *paPoslist = pPhrase->doclist.pList;
115809     *pnToken = pPhrase->nToken;
115810   }
115811 
115812   return res;
115813 }
115814 
115815 static int fts3EvalNearTest(Fts3Expr *pExpr, int *pRc){
115816   int res = 1;
115817 
115818   /* The following block runs if pExpr is the root of a NEAR query.
115819   ** For example, the query:
115820   **
115821   **         "w" NEAR "x" NEAR "y" NEAR "z"
115822   **
115823   ** which is represented in tree form as:
115824   **
115825   **                               |
115826   **                          +--NEAR--+      <-- root of NEAR query
115827   **                          |        |
115828   **                     +--NEAR--+   "z"
115829   **                     |        |
115830   **                +--NEAR--+   "y"
115831   **                |        |
115832   **               "w"      "x"
115833   **
115834   ** The right-hand child of a NEAR node is always a phrase. The
115835   ** left-hand child may be either a phrase or a NEAR node. There are
115836   ** no exceptions to this.
115837   */
115838   if( *pRc==SQLITE_OK
115839    && pExpr->eType==FTSQUERY_NEAR
115840    && pExpr->bEof==0
115841    && (pExpr->pParent==0 || pExpr->pParent->eType!=FTSQUERY_NEAR)
115842   ){
115843     Fts3Expr *p;
115844     int nTmp = 0;                 /* Bytes of temp space */
115845     char *aTmp;                   /* Temp space for PoslistNearMerge() */
115846 
115847     /* Allocate temporary working space. */
115848     for(p=pExpr; p->pLeft; p=p->pLeft){
115849       nTmp += p->pRight->pPhrase->doclist.nList;
115850     }
115851     nTmp += p->pPhrase->doclist.nList;
115852     aTmp = sqlite3_malloc(nTmp*2);
115853     if( !aTmp ){
115854       *pRc = SQLITE_NOMEM;
115855       res = 0;
115856     }else{
115857       char *aPoslist = p->pPhrase->doclist.pList;
115858       int nToken = p->pPhrase->nToken;
115859 
115860       for(p=p->pParent;res && p && p->eType==FTSQUERY_NEAR; p=p->pParent){
115861         Fts3Phrase *pPhrase = p->pRight->pPhrase;
115862         int nNear = p->nNear;
115863         res = fts3EvalNearTrim2(nNear, aTmp, &aPoslist, &nToken, pPhrase);
115864       }
115865 
115866       aPoslist = pExpr->pRight->pPhrase->doclist.pList;
115867       nToken = pExpr->pRight->pPhrase->nToken;
115868       for(p=pExpr->pLeft; p && res; p=p->pLeft){
115869         int nNear = p->pParent->nNear;
115870         Fts3Phrase *pPhrase = (
115871             p->eType==FTSQUERY_NEAR ? p->pRight->pPhrase : p->pPhrase
115872         );
115873         res = fts3EvalNearTrim2(nNear, aTmp, &aPoslist, &nToken, pPhrase);
115874       }
115875     }
115876 
115877     sqlite3_free(aTmp);
115878   }
115879 
115880   return res;
115881 }
115882 
115883 /*
115884 ** This macro is used by the fts3EvalNext() function. The two arguments are
115885 ** 64-bit docid values. If the current query is "ORDER BY docid ASC", then
115886 ** the macro returns (i1 - i2). Or if it is "ORDER BY docid DESC", then
115887 ** it returns (i2 - i1). This allows the same code to be used for merging
115888 ** doclists in ascending or descending order.
115889 */
115890 #define DOCID_CMP(i1, i2) ((pCsr->bDesc?-1:1) * (i1-i2))
115891 
115892 static void fts3EvalNext(
115893   Fts3Cursor *pCsr,
115894   Fts3Expr *pExpr,
115895   int *pRc
115896 ){
115897   if( *pRc==SQLITE_OK ){
115898     assert( pExpr->bEof==0 );
115899     pExpr->bStart = 1;
115900 
115901     switch( pExpr->eType ){
115902       case FTSQUERY_NEAR:
115903       case FTSQUERY_AND: {
115904         Fts3Expr *pLeft = pExpr->pLeft;
115905         Fts3Expr *pRight = pExpr->pRight;
115906         assert( !pLeft->bDeferred || !pRight->bDeferred );
115907         if( pLeft->bDeferred ){
115908           fts3EvalNext(pCsr, pRight, pRc);
115909           pExpr->iDocid = pRight->iDocid;
115910           pExpr->bEof = pRight->bEof;
115911         }else if( pRight->bDeferred ){
115912           fts3EvalNext(pCsr, pLeft, pRc);
115913           pExpr->iDocid = pLeft->iDocid;
115914           pExpr->bEof = pLeft->bEof;
115915         }else{
115916           fts3EvalNext(pCsr, pLeft, pRc);
115917           fts3EvalNext(pCsr, pRight, pRc);
115918 
115919           while( !pLeft->bEof && !pRight->bEof && *pRc==SQLITE_OK ){
115920             sqlite3_int64 iDiff = DOCID_CMP(pLeft->iDocid, pRight->iDocid);
115921             if( iDiff==0 ) break;
115922             if( iDiff<0 ){
115923               fts3EvalNext(pCsr, pLeft, pRc);
115924             }else{
115925               fts3EvalNext(pCsr, pRight, pRc);
115926             }
115927           }
115928 
115929           pExpr->iDocid = pLeft->iDocid;
115930           pExpr->bEof = (pLeft->bEof || pRight->bEof);
115931         }
115932         break;
115933       }
115934 
115935       case FTSQUERY_OR: {
115936         Fts3Expr *pLeft = pExpr->pLeft;
115937         Fts3Expr *pRight = pExpr->pRight;
115938         sqlite3_int64 iCmp = DOCID_CMP(pLeft->iDocid, pRight->iDocid);
115939 
115940         assert( pLeft->bStart || pLeft->iDocid==pRight->iDocid );
115941         assert( pRight->bStart || pLeft->iDocid==pRight->iDocid );
115942 
115943         if( pRight->bEof || (pLeft->bEof==0 && iCmp<0) ){
115944           fts3EvalNext(pCsr, pLeft, pRc);
115945         }else if( pLeft->bEof || (pRight->bEof==0 && iCmp>0) ){
115946           fts3EvalNext(pCsr, pRight, pRc);
115947         }else{
115948           fts3EvalNext(pCsr, pLeft, pRc);
115949           fts3EvalNext(pCsr, pRight, pRc);
115950         }
115951 
115952         pExpr->bEof = (pLeft->bEof && pRight->bEof);
115953         iCmp = DOCID_CMP(pLeft->iDocid, pRight->iDocid);
115954         if( pRight->bEof || (pLeft->bEof==0 &&  iCmp<0) ){
115955           pExpr->iDocid = pLeft->iDocid;
115956         }else{
115957           pExpr->iDocid = pRight->iDocid;
115958         }
115959 
115960         break;
115961       }
115962 
115963       case FTSQUERY_NOT: {
115964         Fts3Expr *pLeft = pExpr->pLeft;
115965         Fts3Expr *pRight = pExpr->pRight;
115966 
115967         if( pRight->bStart==0 ){
115968           fts3EvalNext(pCsr, pRight, pRc);
115969           assert( *pRc!=SQLITE_OK || pRight->bStart );
115970         }
115971 
115972         fts3EvalNext(pCsr, pLeft, pRc);
115973         if( pLeft->bEof==0 ){
115974           while( !*pRc
115975               && !pRight->bEof
115976               && DOCID_CMP(pLeft->iDocid, pRight->iDocid)>0
115977           ){
115978             fts3EvalNext(pCsr, pRight, pRc);
115979           }
115980         }
115981         pExpr->iDocid = pLeft->iDocid;
115982         pExpr->bEof = pLeft->bEof;
115983         break;
115984       }
115985 
115986       default: {
115987         Fts3Phrase *pPhrase = pExpr->pPhrase;
115988         fts3EvalZeroPoslist(pPhrase);
115989         *pRc = fts3EvalPhraseNext(pCsr, pPhrase, &pExpr->bEof);
115990         pExpr->iDocid = pPhrase->doclist.iDocid;
115991         break;
115992       }
115993     }
115994   }
115995 }
115996 
115997 static int fts3EvalDeferredTest(Fts3Cursor *pCsr, Fts3Expr *pExpr, int *pRc){
115998   int bHit = 1;
115999   if( *pRc==SQLITE_OK ){
116000     switch( pExpr->eType ){
116001       case FTSQUERY_NEAR:
116002       case FTSQUERY_AND:
116003         bHit = (
116004             fts3EvalDeferredTest(pCsr, pExpr->pLeft, pRc)
116005          && fts3EvalDeferredTest(pCsr, pExpr->pRight, pRc)
116006          && fts3EvalNearTest(pExpr, pRc)
116007         );
116008 
116009         /* If the NEAR expression does not match any rows, zero the doclist for
116010         ** all phrases involved in the NEAR. This is because the snippet(),
116011         ** offsets() and matchinfo() functions are not supposed to recognize
116012         ** any instances of phrases that are part of unmatched NEAR queries.
116013         ** For example if this expression:
116014         **
116015         **    ... MATCH 'a OR (b NEAR c)'
116016         **
116017         ** is matched against a row containing:
116018         **
116019         **        'a b d e'
116020         **
116021         ** then any snippet() should ony highlight the "a" term, not the "b"
116022         ** (as "b" is part of a non-matching NEAR clause).
116023         */
116024         if( bHit==0
116025          && pExpr->eType==FTSQUERY_NEAR
116026          && (pExpr->pParent==0 || pExpr->pParent->eType!=FTSQUERY_NEAR)
116027         ){
116028           Fts3Expr *p;
116029           for(p=pExpr; p->pPhrase==0; p=p->pLeft){
116030             if( p->pRight->iDocid==pCsr->iPrevId ){
116031               fts3EvalZeroPoslist(p->pRight->pPhrase);
116032             }
116033           }
116034           if( p->iDocid==pCsr->iPrevId ){
116035             fts3EvalZeroPoslist(p->pPhrase);
116036           }
116037         }
116038 
116039         break;
116040 
116041       case FTSQUERY_OR: {
116042         int bHit1 = fts3EvalDeferredTest(pCsr, pExpr->pLeft, pRc);
116043         int bHit2 = fts3EvalDeferredTest(pCsr, pExpr->pRight, pRc);
116044         bHit = bHit1 || bHit2;
116045         break;
116046       }
116047 
116048       case FTSQUERY_NOT:
116049         bHit = (
116050             fts3EvalDeferredTest(pCsr, pExpr->pLeft, pRc)
116051          && !fts3EvalDeferredTest(pCsr, pExpr->pRight, pRc)
116052         );
116053         break;
116054 
116055       default: {
116056         if( pCsr->pDeferred
116057          && (pExpr->iDocid==pCsr->iPrevId || pExpr->bDeferred)
116058         ){
116059           Fts3Phrase *pPhrase = pExpr->pPhrase;
116060           assert( pExpr->bDeferred || pPhrase->doclist.bFreeList==0 );
116061           if( pExpr->bDeferred ){
116062             fts3EvalZeroPoslist(pPhrase);
116063           }
116064           *pRc = fts3EvalDeferredPhrase(pCsr, pPhrase);
116065           bHit = (pPhrase->doclist.pList!=0);
116066           pExpr->iDocid = pCsr->iPrevId;
116067         }else{
116068           bHit = (pExpr->bEof==0 && pExpr->iDocid==pCsr->iPrevId);
116069         }
116070         break;
116071       }
116072     }
116073   }
116074   return bHit;
116075 }
116076 
116077 /*
116078 ** Return 1 if both of the following are true:
116079 **
116080 **   1. *pRc is SQLITE_OK when this function returns, and
116081 **
116082 **   2. After scanning the current FTS table row for the deferred tokens,
116083 **      it is determined that the row does not match the query.
116084 **
116085 ** Or, if no error occurs and it seems the current row does match the FTS
116086 ** query, return 0.
116087 */
116088 static int fts3EvalLoadDeferred(Fts3Cursor *pCsr, int *pRc){
116089   int rc = *pRc;
116090   int bMiss = 0;
116091   if( rc==SQLITE_OK ){
116092     if( pCsr->pDeferred ){
116093       rc = fts3CursorSeek(0, pCsr);
116094       if( rc==SQLITE_OK ){
116095         rc = sqlite3Fts3CacheDeferredDoclists(pCsr);
116096       }
116097     }
116098     bMiss = (0==fts3EvalDeferredTest(pCsr, pCsr->pExpr, &rc));
116099     sqlite3Fts3FreeDeferredDoclists(pCsr);
116100     *pRc = rc;
116101   }
116102   return (rc==SQLITE_OK && bMiss);
116103 }
116104 
116105 /*
116106 ** Advance to the next document that matches the FTS expression in
116107 ** Fts3Cursor.pExpr.
116108 */
116109 SQLITE_PRIVATE int sqlite3Fts3EvalNext(Fts3Cursor *pCsr){
116110   int rc = SQLITE_OK;             /* Return Code */
116111   Fts3Expr *pExpr = pCsr->pExpr;
116112   assert( pCsr->isEof==0 );
116113   if( pExpr==0 ){
116114     pCsr->isEof = 1;
116115   }else{
116116     do {
116117       if( pCsr->isRequireSeek==0 ){
116118         sqlite3_reset(pCsr->pStmt);
116119       }
116120       assert( sqlite3_data_count(pCsr->pStmt)==0 );
116121       fts3EvalNext(pCsr, pExpr, &rc);
116122       pCsr->isEof = pExpr->bEof;
116123       pCsr->isRequireSeek = 1;
116124       pCsr->isMatchinfoNeeded = 1;
116125       pCsr->iPrevId = pExpr->iDocid;
116126     }while( pCsr->isEof==0 && fts3EvalLoadDeferred(pCsr, &rc) );
116127   }
116128   return rc;
116129 }
116130 
116131 /*
116132 ** Restart interation for expression pExpr so that the next call to
116133 ** sqlite3Fts3EvalNext() visits the first row. Do not allow incremental
116134 ** loading or merging of phrase doclists for this iteration.
116135 **
116136 ** If *pRc is other than SQLITE_OK when this function is called, it is
116137 ** a no-op. If an error occurs within this function, *pRc is set to an
116138 ** SQLite error code before returning.
116139 */
116140 static void fts3EvalRestart(
116141   Fts3Cursor *pCsr,
116142   Fts3Expr *pExpr,
116143   int *pRc
116144 ){
116145   if( pExpr && *pRc==SQLITE_OK ){
116146     Fts3Phrase *pPhrase = pExpr->pPhrase;
116147 
116148     if( pPhrase ){
116149       fts3EvalZeroPoslist(pPhrase);
116150       if( pPhrase->bIncr ){
116151         assert( pPhrase->nToken==1 );
116152         assert( pPhrase->aToken[0].pSegcsr );
116153         sqlite3Fts3MsrIncrRestart(pPhrase->aToken[0].pSegcsr);
116154         *pRc = fts3EvalPhraseStart(pCsr, 0, pPhrase);
116155       }
116156 
116157       pPhrase->doclist.pNextDocid = 0;
116158       pPhrase->doclist.iDocid = 0;
116159     }
116160 
116161     pExpr->iDocid = 0;
116162     pExpr->bEof = 0;
116163     pExpr->bStart = 0;
116164 
116165     fts3EvalRestart(pCsr, pExpr->pLeft, pRc);
116166     fts3EvalRestart(pCsr, pExpr->pRight, pRc);
116167   }
116168 }
116169 
116170 /*
116171 ** After allocating the Fts3Expr.aMI[] array for each phrase in the
116172 ** expression rooted at pExpr, the cursor iterates through all rows matched
116173 ** by pExpr, calling this function for each row. This function increments
116174 ** the values in Fts3Expr.aMI[] according to the position-list currently
116175 ** found in Fts3Expr.pPhrase->doclist.pList for each of the phrase
116176 ** expression nodes.
116177 */
116178 static void fts3EvalUpdateCounts(Fts3Expr *pExpr){
116179   if( pExpr ){
116180     Fts3Phrase *pPhrase = pExpr->pPhrase;
116181     if( pPhrase && pPhrase->doclist.pList ){
116182       int iCol = 0;
116183       char *p = pPhrase->doclist.pList;
116184 
116185       assert( *p );
116186       while( 1 ){
116187         u8 c = 0;
116188         int iCnt = 0;
116189         while( 0xFE & (*p | c) ){
116190           if( (c&0x80)==0 ) iCnt++;
116191           c = *p++ & 0x80;
116192         }
116193 
116194         /* aMI[iCol*3 + 1] = Number of occurrences
116195         ** aMI[iCol*3 + 2] = Number of rows containing at least one instance
116196         */
116197         pExpr->aMI[iCol*3 + 1] += iCnt;
116198         pExpr->aMI[iCol*3 + 2] += (iCnt>0);
116199         if( *p==0x00 ) break;
116200         p++;
116201         p += sqlite3Fts3GetVarint32(p, &iCol);
116202       }
116203     }
116204 
116205     fts3EvalUpdateCounts(pExpr->pLeft);
116206     fts3EvalUpdateCounts(pExpr->pRight);
116207   }
116208 }
116209 
116210 /*
116211 ** Expression pExpr must be of type FTSQUERY_PHRASE.
116212 **
116213 ** If it is not already allocated and populated, this function allocates and
116214 ** populates the Fts3Expr.aMI[] array for expression pExpr. If pExpr is part
116215 ** of a NEAR expression, then it also allocates and populates the same array
116216 ** for all other phrases that are part of the NEAR expression.
116217 **
116218 ** SQLITE_OK is returned if the aMI[] array is successfully allocated and
116219 ** populated. Otherwise, if an error occurs, an SQLite error code is returned.
116220 */
116221 static int fts3EvalGatherStats(
116222   Fts3Cursor *pCsr,               /* Cursor object */
116223   Fts3Expr *pExpr                 /* FTSQUERY_PHRASE expression */
116224 ){
116225   int rc = SQLITE_OK;             /* Return code */
116226 
116227   assert( pExpr->eType==FTSQUERY_PHRASE );
116228   if( pExpr->aMI==0 ){
116229     Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
116230     Fts3Expr *pRoot;                /* Root of NEAR expression */
116231     Fts3Expr *p;                    /* Iterator used for several purposes */
116232 
116233     sqlite3_int64 iPrevId = pCsr->iPrevId;
116234     sqlite3_int64 iDocid;
116235     u8 bEof;
116236 
116237     /* Find the root of the NEAR expression */
116238     pRoot = pExpr;
116239     while( pRoot->pParent && pRoot->pParent->eType==FTSQUERY_NEAR ){
116240       pRoot = pRoot->pParent;
116241     }
116242     iDocid = pRoot->iDocid;
116243     bEof = pRoot->bEof;
116244     assert( pRoot->bStart );
116245 
116246     /* Allocate space for the aMSI[] array of each FTSQUERY_PHRASE node */
116247     for(p=pRoot; p; p=p->pLeft){
116248       Fts3Expr *pE = (p->eType==FTSQUERY_PHRASE?p:p->pRight);
116249       assert( pE->aMI==0 );
116250       pE->aMI = (u32 *)sqlite3_malloc(pTab->nColumn * 3 * sizeof(u32));
116251       if( !pE->aMI ) return SQLITE_NOMEM;
116252       memset(pE->aMI, 0, pTab->nColumn * 3 * sizeof(u32));
116253     }
116254 
116255     fts3EvalRestart(pCsr, pRoot, &rc);
116256 
116257     while( pCsr->isEof==0 && rc==SQLITE_OK ){
116258 
116259       do {
116260         /* Ensure the %_content statement is reset. */
116261         if( pCsr->isRequireSeek==0 ) sqlite3_reset(pCsr->pStmt);
116262         assert( sqlite3_data_count(pCsr->pStmt)==0 );
116263 
116264         /* Advance to the next document */
116265         fts3EvalNext(pCsr, pRoot, &rc);
116266         pCsr->isEof = pRoot->bEof;
116267         pCsr->isRequireSeek = 1;
116268         pCsr->isMatchinfoNeeded = 1;
116269         pCsr->iPrevId = pRoot->iDocid;
116270       }while( pCsr->isEof==0
116271            && pRoot->eType==FTSQUERY_NEAR
116272            && fts3EvalLoadDeferred(pCsr, &rc)
116273       );
116274 
116275       if( rc==SQLITE_OK && pCsr->isEof==0 ){
116276         fts3EvalUpdateCounts(pRoot);
116277       }
116278     }
116279 
116280     pCsr->isEof = 0;
116281     pCsr->iPrevId = iPrevId;
116282 
116283     if( bEof ){
116284       pRoot->bEof = bEof;
116285     }else{
116286       /* Caution: pRoot may iterate through docids in ascending or descending
116287       ** order. For this reason, even though it seems more defensive, the
116288       ** do loop can not be written:
116289       **
116290       **   do {...} while( pRoot->iDocid<iDocid && rc==SQLITE_OK );
116291       */
116292       fts3EvalRestart(pCsr, pRoot, &rc);
116293       do {
116294         fts3EvalNext(pCsr, pRoot, &rc);
116295         assert( pRoot->bEof==0 );
116296       }while( pRoot->iDocid!=iDocid && rc==SQLITE_OK );
116297       fts3EvalLoadDeferred(pCsr, &rc);
116298     }
116299   }
116300   return rc;
116301 }
116302 
116303 /*
116304 ** This function is used by the matchinfo() module to query a phrase
116305 ** expression node for the following information:
116306 **
116307 **   1. The total number of occurrences of the phrase in each column of
116308 **      the FTS table (considering all rows), and
116309 **
116310 **   2. For each column, the number of rows in the table for which the
116311 **      column contains at least one instance of the phrase.
116312 **
116313 ** If no error occurs, SQLITE_OK is returned and the values for each column
116314 ** written into the array aiOut as follows:
116315 **
116316 **   aiOut[iCol*3 + 1] = Number of occurrences
116317 **   aiOut[iCol*3 + 2] = Number of rows containing at least one instance
116318 **
116319 ** Caveats:
116320 **
116321 **   * If a phrase consists entirely of deferred tokens, then all output
116322 **     values are set to the number of documents in the table. In other
116323 **     words we assume that very common tokens occur exactly once in each
116324 **     column of each row of the table.
116325 **
116326 **   * If a phrase contains some deferred tokens (and some non-deferred
116327 **     tokens), count the potential occurrence identified by considering
116328 **     the non-deferred tokens instead of actual phrase occurrences.
116329 **
116330 **   * If the phrase is part of a NEAR expression, then only phrase instances
116331 **     that meet the NEAR constraint are included in the counts.
116332 */
116333 SQLITE_PRIVATE int sqlite3Fts3EvalPhraseStats(
116334   Fts3Cursor *pCsr,               /* FTS cursor handle */
116335   Fts3Expr *pExpr,                /* Phrase expression */
116336   u32 *aiOut                      /* Array to write results into (see above) */
116337 ){
116338   Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
116339   int rc = SQLITE_OK;
116340   int iCol;
116341 
116342   if( pExpr->bDeferred && pExpr->pParent->eType!=FTSQUERY_NEAR ){
116343     assert( pCsr->nDoc>0 );
116344     for(iCol=0; iCol<pTab->nColumn; iCol++){
116345       aiOut[iCol*3 + 1] = (u32)pCsr->nDoc;
116346       aiOut[iCol*3 + 2] = (u32)pCsr->nDoc;
116347     }
116348   }else{
116349     rc = fts3EvalGatherStats(pCsr, pExpr);
116350     if( rc==SQLITE_OK ){
116351       assert( pExpr->aMI );
116352       for(iCol=0; iCol<pTab->nColumn; iCol++){
116353         aiOut[iCol*3 + 1] = pExpr->aMI[iCol*3 + 1];
116354         aiOut[iCol*3 + 2] = pExpr->aMI[iCol*3 + 2];
116355       }
116356     }
116357   }
116358 
116359   return rc;
116360 }
116361 
116362 /*
116363 ** The expression pExpr passed as the second argument to this function
116364 ** must be of type FTSQUERY_PHRASE.
116365 **
116366 ** The returned value is either NULL or a pointer to a buffer containing
116367 ** a position-list indicating the occurrences of the phrase in column iCol
116368 ** of the current row.
116369 **
116370 ** More specifically, the returned buffer contains 1 varint for each
116371 ** occurence of the phrase in the column, stored using the normal (delta+2)
116372 ** compression and is terminated by either an 0x01 or 0x00 byte. For example,
116373 ** if the requested column contains "a b X c d X X" and the position-list
116374 ** for 'X' is requested, the buffer returned may contain:
116375 **
116376 **     0x04 0x05 0x03 0x01   or   0x04 0x05 0x03 0x00
116377 **
116378 ** This function works regardless of whether or not the phrase is deferred,
116379 ** incremental, or neither.
116380 */
116381 SQLITE_PRIVATE char *sqlite3Fts3EvalPhrasePoslist(
116382   Fts3Cursor *pCsr,               /* FTS3 cursor object */
116383   Fts3Expr *pExpr,                /* Phrase to return doclist for */
116384   int iCol                        /* Column to return position list for */
116385 ){
116386   Fts3Phrase *pPhrase = pExpr->pPhrase;
116387   Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
116388   char *pIter = pPhrase->doclist.pList;
116389   int iThis;
116390 
116391   assert( iCol>=0 && iCol<pTab->nColumn );
116392   if( !pIter
116393    || pExpr->bEof
116394    || pExpr->iDocid!=pCsr->iPrevId
116395    || (pPhrase->iColumn<pTab->nColumn && pPhrase->iColumn!=iCol)
116396   ){
116397     return 0;
116398   }
116399 
116400   assert( pPhrase->doclist.nList>0 );
116401   if( *pIter==0x01 ){
116402     pIter++;
116403     pIter += sqlite3Fts3GetVarint32(pIter, &iThis);
116404   }else{
116405     iThis = 0;
116406   }
116407   while( iThis<iCol ){
116408     fts3ColumnlistCopy(0, &pIter);
116409     if( *pIter==0x00 ) return 0;
116410     pIter++;
116411     pIter += sqlite3Fts3GetVarint32(pIter, &iThis);
116412   }
116413 
116414   return ((iCol==iThis)?pIter:0);
116415 }
116416 
116417 /*
116418 ** Free all components of the Fts3Phrase structure that were allocated by
116419 ** the eval module. Specifically, this means to free:
116420 **
116421 **   * the contents of pPhrase->doclist, and
116422 **   * any Fts3MultiSegReader objects held by phrase tokens.
116423 */
116424 SQLITE_PRIVATE void sqlite3Fts3EvalPhraseCleanup(Fts3Phrase *pPhrase){
116425   if( pPhrase ){
116426     int i;
116427     sqlite3_free(pPhrase->doclist.aAll);
116428     fts3EvalZeroPoslist(pPhrase);
116429     memset(&pPhrase->doclist, 0, sizeof(Fts3Doclist));
116430     for(i=0; i<pPhrase->nToken; i++){
116431       fts3SegReaderCursorFree(pPhrase->aToken[i].pSegcsr);
116432       pPhrase->aToken[i].pSegcsr = 0;
116433     }
116434   }
116435 }
116436 
116437 #endif
116438 
116439 /************** End of fts3.c ************************************************/
116440 /************** Begin file fts3_aux.c ****************************************/
116441 /*
116442 ** 2011 Jan 27
116443 **
116444 ** The author disclaims copyright to this source code.  In place of
116445 ** a legal notice, here is a blessing:
116446 **
116447 **    May you do good and not evil.
116448 **    May you find forgiveness for yourself and forgive others.
116449 **    May you share freely, never taking more than you give.
116450 **
116451 ******************************************************************************
116452 **
116453 */
116454 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
116455 
116456 
116457 typedef struct Fts3auxTable Fts3auxTable;
116458 typedef struct Fts3auxCursor Fts3auxCursor;
116459 
116460 struct Fts3auxTable {
116461   sqlite3_vtab base;              /* Base class used by SQLite core */
116462   Fts3Table *pFts3Tab;
116463 };
116464 
116465 struct Fts3auxCursor {
116466   sqlite3_vtab_cursor base;       /* Base class used by SQLite core */
116467   Fts3MultiSegReader csr;        /* Must be right after "base" */
116468   Fts3SegFilter filter;
116469   char *zStop;
116470   int nStop;                      /* Byte-length of string zStop */
116471   int isEof;                      /* True if cursor is at EOF */
116472   sqlite3_int64 iRowid;           /* Current rowid */
116473 
116474   int iCol;                       /* Current value of 'col' column */
116475   int nStat;                      /* Size of aStat[] array */
116476   struct Fts3auxColstats {
116477     sqlite3_int64 nDoc;           /* 'documents' values for current csr row */
116478     sqlite3_int64 nOcc;           /* 'occurrences' values for current csr row */
116479   } *aStat;
116480 };
116481 
116482 /*
116483 ** Schema of the terms table.
116484 */
116485 #define FTS3_TERMS_SCHEMA "CREATE TABLE x(term, col, documents, occurrences)"
116486 
116487 /*
116488 ** This function does all the work for both the xConnect and xCreate methods.
116489 ** These tables have no persistent representation of their own, so xConnect
116490 ** and xCreate are identical operations.
116491 */
116492 static int fts3auxConnectMethod(
116493   sqlite3 *db,                    /* Database connection */
116494   void *pUnused,                  /* Unused */
116495   int argc,                       /* Number of elements in argv array */
116496   const char * const *argv,       /* xCreate/xConnect argument array */
116497   sqlite3_vtab **ppVtab,          /* OUT: New sqlite3_vtab object */
116498   char **pzErr                    /* OUT: sqlite3_malloc'd error message */
116499 ){
116500   char const *zDb;                /* Name of database (e.g. "main") */
116501   char const *zFts3;              /* Name of fts3 table */
116502   int nDb;                        /* Result of strlen(zDb) */
116503   int nFts3;                      /* Result of strlen(zFts3) */
116504   int nByte;                      /* Bytes of space to allocate here */
116505   int rc;                         /* value returned by declare_vtab() */
116506   Fts3auxTable *p;                /* Virtual table object to return */
116507 
116508   UNUSED_PARAMETER(pUnused);
116509 
116510   /* The user should specify a single argument - the name of an fts3 table. */
116511   if( argc!=4 ){
116512     *pzErr = sqlite3_mprintf(
116513         "wrong number of arguments to fts4aux constructor"
116514     );
116515     return SQLITE_ERROR;
116516   }
116517 
116518   zDb = argv[1];
116519   nDb = strlen(zDb);
116520   zFts3 = argv[3];
116521   nFts3 = strlen(zFts3);
116522 
116523   rc = sqlite3_declare_vtab(db, FTS3_TERMS_SCHEMA);
116524   if( rc!=SQLITE_OK ) return rc;
116525 
116526   nByte = sizeof(Fts3auxTable) + sizeof(Fts3Table) + nDb + nFts3 + 2;
116527   p = (Fts3auxTable *)sqlite3_malloc(nByte);
116528   if( !p ) return SQLITE_NOMEM;
116529   memset(p, 0, nByte);
116530 
116531   p->pFts3Tab = (Fts3Table *)&p[1];
116532   p->pFts3Tab->zDb = (char *)&p->pFts3Tab[1];
116533   p->pFts3Tab->zName = &p->pFts3Tab->zDb[nDb+1];
116534   p->pFts3Tab->db = db;
116535   p->pFts3Tab->nIndex = 1;
116536 
116537   memcpy((char *)p->pFts3Tab->zDb, zDb, nDb);
116538   memcpy((char *)p->pFts3Tab->zName, zFts3, nFts3);
116539   sqlite3Fts3Dequote((char *)p->pFts3Tab->zName);
116540 
116541   *ppVtab = (sqlite3_vtab *)p;
116542   return SQLITE_OK;
116543 }
116544 
116545 /*
116546 ** This function does the work for both the xDisconnect and xDestroy methods.
116547 ** These tables have no persistent representation of their own, so xDisconnect
116548 ** and xDestroy are identical operations.
116549 */
116550 static int fts3auxDisconnectMethod(sqlite3_vtab *pVtab){
116551   Fts3auxTable *p = (Fts3auxTable *)pVtab;
116552   Fts3Table *pFts3 = p->pFts3Tab;
116553   int i;
116554 
116555   /* Free any prepared statements held */
116556   for(i=0; i<SizeofArray(pFts3->aStmt); i++){
116557     sqlite3_finalize(pFts3->aStmt[i]);
116558   }
116559   sqlite3_free(pFts3->zSegmentsTbl);
116560   sqlite3_free(p);
116561   return SQLITE_OK;
116562 }
116563 
116564 #define FTS4AUX_EQ_CONSTRAINT 1
116565 #define FTS4AUX_GE_CONSTRAINT 2
116566 #define FTS4AUX_LE_CONSTRAINT 4
116567 
116568 /*
116569 ** xBestIndex - Analyze a WHERE and ORDER BY clause.
116570 */
116571 static int fts3auxBestIndexMethod(
116572   sqlite3_vtab *pVTab,
116573   sqlite3_index_info *pInfo
116574 ){
116575   int i;
116576   int iEq = -1;
116577   int iGe = -1;
116578   int iLe = -1;
116579 
116580   UNUSED_PARAMETER(pVTab);
116581 
116582   /* This vtab delivers always results in "ORDER BY term ASC" order. */
116583   if( pInfo->nOrderBy==1
116584    && pInfo->aOrderBy[0].iColumn==0
116585    && pInfo->aOrderBy[0].desc==0
116586   ){
116587     pInfo->orderByConsumed = 1;
116588   }
116589 
116590   /* Search for equality and range constraints on the "term" column. */
116591   for(i=0; i<pInfo->nConstraint; i++){
116592     if( pInfo->aConstraint[i].usable && pInfo->aConstraint[i].iColumn==0 ){
116593       int op = pInfo->aConstraint[i].op;
116594       if( op==SQLITE_INDEX_CONSTRAINT_EQ ) iEq = i;
116595       if( op==SQLITE_INDEX_CONSTRAINT_LT ) iLe = i;
116596       if( op==SQLITE_INDEX_CONSTRAINT_LE ) iLe = i;
116597       if( op==SQLITE_INDEX_CONSTRAINT_GT ) iGe = i;
116598       if( op==SQLITE_INDEX_CONSTRAINT_GE ) iGe = i;
116599     }
116600   }
116601 
116602   if( iEq>=0 ){
116603     pInfo->idxNum = FTS4AUX_EQ_CONSTRAINT;
116604     pInfo->aConstraintUsage[iEq].argvIndex = 1;
116605     pInfo->estimatedCost = 5;
116606   }else{
116607     pInfo->idxNum = 0;
116608     pInfo->estimatedCost = 20000;
116609     if( iGe>=0 ){
116610       pInfo->idxNum += FTS4AUX_GE_CONSTRAINT;
116611       pInfo->aConstraintUsage[iGe].argvIndex = 1;
116612       pInfo->estimatedCost /= 2;
116613     }
116614     if( iLe>=0 ){
116615       pInfo->idxNum += FTS4AUX_LE_CONSTRAINT;
116616       pInfo->aConstraintUsage[iLe].argvIndex = 1 + (iGe>=0);
116617       pInfo->estimatedCost /= 2;
116618     }
116619   }
116620 
116621   return SQLITE_OK;
116622 }
116623 
116624 /*
116625 ** xOpen - Open a cursor.
116626 */
116627 static int fts3auxOpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){
116628   Fts3auxCursor *pCsr;            /* Pointer to cursor object to return */
116629 
116630   UNUSED_PARAMETER(pVTab);
116631 
116632   pCsr = (Fts3auxCursor *)sqlite3_malloc(sizeof(Fts3auxCursor));
116633   if( !pCsr ) return SQLITE_NOMEM;
116634   memset(pCsr, 0, sizeof(Fts3auxCursor));
116635 
116636   *ppCsr = (sqlite3_vtab_cursor *)pCsr;
116637   return SQLITE_OK;
116638 }
116639 
116640 /*
116641 ** xClose - Close a cursor.
116642 */
116643 static int fts3auxCloseMethod(sqlite3_vtab_cursor *pCursor){
116644   Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab;
116645   Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
116646 
116647   sqlite3Fts3SegmentsClose(pFts3);
116648   sqlite3Fts3SegReaderFinish(&pCsr->csr);
116649   sqlite3_free((void *)pCsr->filter.zTerm);
116650   sqlite3_free(pCsr->zStop);
116651   sqlite3_free(pCsr->aStat);
116652   sqlite3_free(pCsr);
116653   return SQLITE_OK;
116654 }
116655 
116656 static int fts3auxGrowStatArray(Fts3auxCursor *pCsr, int nSize){
116657   if( nSize>pCsr->nStat ){
116658     struct Fts3auxColstats *aNew;
116659     aNew = (struct Fts3auxColstats *)sqlite3_realloc(pCsr->aStat,
116660         sizeof(struct Fts3auxColstats) * nSize
116661     );
116662     if( aNew==0 ) return SQLITE_NOMEM;
116663     memset(&aNew[pCsr->nStat], 0,
116664         sizeof(struct Fts3auxColstats) * (nSize - pCsr->nStat)
116665     );
116666     pCsr->aStat = aNew;
116667     pCsr->nStat = nSize;
116668   }
116669   return SQLITE_OK;
116670 }
116671 
116672 /*
116673 ** xNext - Advance the cursor to the next row, if any.
116674 */
116675 static int fts3auxNextMethod(sqlite3_vtab_cursor *pCursor){
116676   Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
116677   Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab;
116678   int rc;
116679 
116680   /* Increment our pretend rowid value. */
116681   pCsr->iRowid++;
116682 
116683   for(pCsr->iCol++; pCsr->iCol<pCsr->nStat; pCsr->iCol++){
116684     if( pCsr->aStat[pCsr->iCol].nDoc>0 ) return SQLITE_OK;
116685   }
116686 
116687   rc = sqlite3Fts3SegReaderStep(pFts3, &pCsr->csr);
116688   if( rc==SQLITE_ROW ){
116689     int i = 0;
116690     int nDoclist = pCsr->csr.nDoclist;
116691     char *aDoclist = pCsr->csr.aDoclist;
116692     int iCol;
116693 
116694     int eState = 0;
116695 
116696     if( pCsr->zStop ){
116697       int n = (pCsr->nStop<pCsr->csr.nTerm) ? pCsr->nStop : pCsr->csr.nTerm;
116698       int mc = memcmp(pCsr->zStop, pCsr->csr.zTerm, n);
116699       if( mc<0 || (mc==0 && pCsr->csr.nTerm>pCsr->nStop) ){
116700         pCsr->isEof = 1;
116701         return SQLITE_OK;
116702       }
116703     }
116704 
116705     if( fts3auxGrowStatArray(pCsr, 2) ) return SQLITE_NOMEM;
116706     memset(pCsr->aStat, 0, sizeof(struct Fts3auxColstats) * pCsr->nStat);
116707     iCol = 0;
116708 
116709     while( i<nDoclist ){
116710       sqlite3_int64 v = 0;
116711 
116712       i += sqlite3Fts3GetVarint(&aDoclist[i], &v);
116713       switch( eState ){
116714         /* State 0. In this state the integer just read was a docid. */
116715         case 0:
116716           pCsr->aStat[0].nDoc++;
116717           eState = 1;
116718           iCol = 0;
116719           break;
116720 
116721         /* State 1. In this state we are expecting either a 1, indicating
116722         ** that the following integer will be a column number, or the
116723         ** start of a position list for column 0.
116724         **
116725         ** The only difference between state 1 and state 2 is that if the
116726         ** integer encountered in state 1 is not 0 or 1, then we need to
116727         ** increment the column 0 "nDoc" count for this term.
116728         */
116729         case 1:
116730           assert( iCol==0 );
116731           if( v>1 ){
116732             pCsr->aStat[1].nDoc++;
116733           }
116734           eState = 2;
116735           /* fall through */
116736 
116737         case 2:
116738           if( v==0 ){       /* 0x00. Next integer will be a docid. */
116739             eState = 0;
116740           }else if( v==1 ){ /* 0x01. Next integer will be a column number. */
116741             eState = 3;
116742           }else{            /* 2 or greater. A position. */
116743             pCsr->aStat[iCol+1].nOcc++;
116744             pCsr->aStat[0].nOcc++;
116745           }
116746           break;
116747 
116748         /* State 3. The integer just read is a column number. */
116749         default: assert( eState==3 );
116750           iCol = (int)v;
116751           if( fts3auxGrowStatArray(pCsr, iCol+2) ) return SQLITE_NOMEM;
116752           pCsr->aStat[iCol+1].nDoc++;
116753           eState = 2;
116754           break;
116755       }
116756     }
116757 
116758     pCsr->iCol = 0;
116759     rc = SQLITE_OK;
116760   }else{
116761     pCsr->isEof = 1;
116762   }
116763   return rc;
116764 }
116765 
116766 /*
116767 ** xFilter - Initialize a cursor to point at the start of its data.
116768 */
116769 static int fts3auxFilterMethod(
116770   sqlite3_vtab_cursor *pCursor,   /* The cursor used for this query */
116771   int idxNum,                     /* Strategy index */
116772   const char *idxStr,             /* Unused */
116773   int nVal,                       /* Number of elements in apVal */
116774   sqlite3_value **apVal           /* Arguments for the indexing scheme */
116775 ){
116776   Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
116777   Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab;
116778   int rc;
116779   int isScan;
116780 
116781   UNUSED_PARAMETER(nVal);
116782   UNUSED_PARAMETER(idxStr);
116783 
116784   assert( idxStr==0 );
116785   assert( idxNum==FTS4AUX_EQ_CONSTRAINT || idxNum==0
116786        || idxNum==FTS4AUX_LE_CONSTRAINT || idxNum==FTS4AUX_GE_CONSTRAINT
116787        || idxNum==(FTS4AUX_LE_CONSTRAINT|FTS4AUX_GE_CONSTRAINT)
116788   );
116789   isScan = (idxNum!=FTS4AUX_EQ_CONSTRAINT);
116790 
116791   /* In case this cursor is being reused, close and zero it. */
116792   testcase(pCsr->filter.zTerm);
116793   sqlite3Fts3SegReaderFinish(&pCsr->csr);
116794   sqlite3_free((void *)pCsr->filter.zTerm);
116795   sqlite3_free(pCsr->aStat);
116796   memset(&pCsr->csr, 0, ((u8*)&pCsr[1]) - (u8*)&pCsr->csr);
116797 
116798   pCsr->filter.flags = FTS3_SEGMENT_REQUIRE_POS|FTS3_SEGMENT_IGNORE_EMPTY;
116799   if( isScan ) pCsr->filter.flags |= FTS3_SEGMENT_SCAN;
116800 
116801   if( idxNum&(FTS4AUX_EQ_CONSTRAINT|FTS4AUX_GE_CONSTRAINT) ){
116802     const unsigned char *zStr = sqlite3_value_text(apVal[0]);
116803     if( zStr ){
116804       pCsr->filter.zTerm = sqlite3_mprintf("%s", zStr);
116805       pCsr->filter.nTerm = sqlite3_value_bytes(apVal[0]);
116806       if( pCsr->filter.zTerm==0 ) return SQLITE_NOMEM;
116807     }
116808   }
116809   if( idxNum&FTS4AUX_LE_CONSTRAINT ){
116810     int iIdx = (idxNum&FTS4AUX_GE_CONSTRAINT) ? 1 : 0;
116811     pCsr->zStop = sqlite3_mprintf("%s", sqlite3_value_text(apVal[iIdx]));
116812     pCsr->nStop = sqlite3_value_bytes(apVal[iIdx]);
116813     if( pCsr->zStop==0 ) return SQLITE_NOMEM;
116814   }
116815 
116816   rc = sqlite3Fts3SegReaderCursor(pFts3, 0, FTS3_SEGCURSOR_ALL,
116817       pCsr->filter.zTerm, pCsr->filter.nTerm, 0, isScan, &pCsr->csr
116818   );
116819   if( rc==SQLITE_OK ){
116820     rc = sqlite3Fts3SegReaderStart(pFts3, &pCsr->csr, &pCsr->filter);
116821   }
116822 
116823   if( rc==SQLITE_OK ) rc = fts3auxNextMethod(pCursor);
116824   return rc;
116825 }
116826 
116827 /*
116828 ** xEof - Return true if the cursor is at EOF, or false otherwise.
116829 */
116830 static int fts3auxEofMethod(sqlite3_vtab_cursor *pCursor){
116831   Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
116832   return pCsr->isEof;
116833 }
116834 
116835 /*
116836 ** xColumn - Return a column value.
116837 */
116838 static int fts3auxColumnMethod(
116839   sqlite3_vtab_cursor *pCursor,   /* Cursor to retrieve value from */
116840   sqlite3_context *pContext,      /* Context for sqlite3_result_xxx() calls */
116841   int iCol                        /* Index of column to read value from */
116842 ){
116843   Fts3auxCursor *p = (Fts3auxCursor *)pCursor;
116844 
116845   assert( p->isEof==0 );
116846   if( iCol==0 ){        /* Column "term" */
116847     sqlite3_result_text(pContext, p->csr.zTerm, p->csr.nTerm, SQLITE_TRANSIENT);
116848   }else if( iCol==1 ){  /* Column "col" */
116849     if( p->iCol ){
116850       sqlite3_result_int(pContext, p->iCol-1);
116851     }else{
116852       sqlite3_result_text(pContext, "*", -1, SQLITE_STATIC);
116853     }
116854   }else if( iCol==2 ){  /* Column "documents" */
116855     sqlite3_result_int64(pContext, p->aStat[p->iCol].nDoc);
116856   }else{                /* Column "occurrences" */
116857     sqlite3_result_int64(pContext, p->aStat[p->iCol].nOcc);
116858   }
116859 
116860   return SQLITE_OK;
116861 }
116862 
116863 /*
116864 ** xRowid - Return the current rowid for the cursor.
116865 */
116866 static int fts3auxRowidMethod(
116867   sqlite3_vtab_cursor *pCursor,   /* Cursor to retrieve value from */
116868   sqlite_int64 *pRowid            /* OUT: Rowid value */
116869 ){
116870   Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
116871   *pRowid = pCsr->iRowid;
116872   return SQLITE_OK;
116873 }
116874 
116875 /*
116876 ** Register the fts3aux module with database connection db. Return SQLITE_OK
116877 ** if successful or an error code if sqlite3_create_module() fails.
116878 */
116879 SQLITE_PRIVATE int sqlite3Fts3InitAux(sqlite3 *db){
116880   static const sqlite3_module fts3aux_module = {
116881      0,                           /* iVersion      */
116882      fts3auxConnectMethod,        /* xCreate       */
116883      fts3auxConnectMethod,        /* xConnect      */
116884      fts3auxBestIndexMethod,      /* xBestIndex    */
116885      fts3auxDisconnectMethod,     /* xDisconnect   */
116886      fts3auxDisconnectMethod,     /* xDestroy      */
116887      fts3auxOpenMethod,           /* xOpen         */
116888      fts3auxCloseMethod,          /* xClose        */
116889      fts3auxFilterMethod,         /* xFilter       */
116890      fts3auxNextMethod,           /* xNext         */
116891      fts3auxEofMethod,            /* xEof          */
116892      fts3auxColumnMethod,         /* xColumn       */
116893      fts3auxRowidMethod,          /* xRowid        */
116894      0,                           /* xUpdate       */
116895      0,                           /* xBegin        */
116896      0,                           /* xSync         */
116897      0,                           /* xCommit       */
116898      0,                           /* xRollback     */
116899      0,                           /* xFindFunction */
116900      0,                           /* xRename       */
116901      0,                           /* xSavepoint    */
116902      0,                           /* xRelease      */
116903      0                            /* xRollbackTo   */
116904   };
116905   int rc;                         /* Return code */
116906 
116907   rc = sqlite3_create_module(db, "fts4aux", &fts3aux_module, 0);
116908   return rc;
116909 }
116910 
116911 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
116912 
116913 /************** End of fts3_aux.c ********************************************/
116914 /************** Begin file fts3_expr.c ***************************************/
116915 /*
116916 ** 2008 Nov 28
116917 **
116918 ** The author disclaims copyright to this source code.  In place of
116919 ** a legal notice, here is a blessing:
116920 **
116921 **    May you do good and not evil.
116922 **    May you find forgiveness for yourself and forgive others.
116923 **    May you share freely, never taking more than you give.
116924 **
116925 ******************************************************************************
116926 **
116927 ** This module contains code that implements a parser for fts3 query strings
116928 ** (the right-hand argument to the MATCH operator). Because the supported
116929 ** syntax is relatively simple, the whole tokenizer/parser system is
116930 ** hand-coded.
116931 */
116932 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
116933 
116934 /*
116935 ** By default, this module parses the legacy syntax that has been
116936 ** traditionally used by fts3. Or, if SQLITE_ENABLE_FTS3_PARENTHESIS
116937 ** is defined, then it uses the new syntax. The differences between
116938 ** the new and the old syntaxes are:
116939 **
116940 **  a) The new syntax supports parenthesis. The old does not.
116941 **
116942 **  b) The new syntax supports the AND and NOT operators. The old does not.
116943 **
116944 **  c) The old syntax supports the "-" token qualifier. This is not
116945 **     supported by the new syntax (it is replaced by the NOT operator).
116946 **
116947 **  d) When using the old syntax, the OR operator has a greater precedence
116948 **     than an implicit AND. When using the new, both implicity and explicit
116949 **     AND operators have a higher precedence than OR.
116950 **
116951 ** If compiled with SQLITE_TEST defined, then this module exports the
116952 ** symbol "int sqlite3_fts3_enable_parentheses". Setting this variable
116953 ** to zero causes the module to use the old syntax. If it is set to
116954 ** non-zero the new syntax is activated. This is so both syntaxes can
116955 ** be tested using a single build of testfixture.
116956 **
116957 ** The following describes the syntax supported by the fts3 MATCH
116958 ** operator in a similar format to that used by the lemon parser
116959 ** generator. This module does not use actually lemon, it uses a
116960 ** custom parser.
116961 **
116962 **   query ::= andexpr (OR andexpr)*.
116963 **
116964 **   andexpr ::= notexpr (AND? notexpr)*.
116965 **
116966 **   notexpr ::= nearexpr (NOT nearexpr|-TOKEN)*.
116967 **   notexpr ::= LP query RP.
116968 **
116969 **   nearexpr ::= phrase (NEAR distance_opt nearexpr)*.
116970 **
116971 **   distance_opt ::= .
116972 **   distance_opt ::= / INTEGER.
116973 **
116974 **   phrase ::= TOKEN.
116975 **   phrase ::= COLUMN:TOKEN.
116976 **   phrase ::= "TOKEN TOKEN TOKEN...".
116977 */
116978 
116979 #ifdef SQLITE_TEST
116980 SQLITE_API int sqlite3_fts3_enable_parentheses = 0;
116981 #else
116982 # ifdef SQLITE_ENABLE_FTS3_PARENTHESIS
116983 #  define sqlite3_fts3_enable_parentheses 1
116984 # else
116985 #  define sqlite3_fts3_enable_parentheses 0
116986 # endif
116987 #endif
116988 
116989 /*
116990 ** Default span for NEAR operators.
116991 */
116992 #define SQLITE_FTS3_DEFAULT_NEAR_PARAM 10
116993 
116994 
116995 /*
116996 ** isNot:
116997 **   This variable is used by function getNextNode(). When getNextNode() is
116998 **   called, it sets ParseContext.isNot to true if the 'next node' is a
116999 **   FTSQUERY_PHRASE with a unary "-" attached to it. i.e. "mysql" in the
117000 **   FTS3 query "sqlite -mysql". Otherwise, ParseContext.isNot is set to
117001 **   zero.
117002 */
117003 typedef struct ParseContext ParseContext;
117004 struct ParseContext {
117005   sqlite3_tokenizer *pTokenizer;      /* Tokenizer module */
117006   const char **azCol;                 /* Array of column names for fts3 table */
117007   int nCol;                           /* Number of entries in azCol[] */
117008   int iDefaultCol;                    /* Default column to query */
117009   int isNot;                          /* True if getNextNode() sees a unary - */
117010   sqlite3_context *pCtx;              /* Write error message here */
117011   int nNest;                          /* Number of nested brackets */
117012 };
117013 
117014 /*
117015 ** This function is equivalent to the standard isspace() function.
117016 **
117017 ** The standard isspace() can be awkward to use safely, because although it
117018 ** is defined to accept an argument of type int, its behaviour when passed
117019 ** an integer that falls outside of the range of the unsigned char type
117020 ** is undefined (and sometimes, "undefined" means segfault). This wrapper
117021 ** is defined to accept an argument of type char, and always returns 0 for
117022 ** any values that fall outside of the range of the unsigned char type (i.e.
117023 ** negative values).
117024 */
117025 static int fts3isspace(char c){
117026   return c==' ' || c=='\t' || c=='\n' || c=='\r' || c=='\v' || c=='\f';
117027 }
117028 
117029 /*
117030 ** Allocate nByte bytes of memory using sqlite3_malloc(). If successful,
117031 ** zero the memory before returning a pointer to it. If unsuccessful,
117032 ** return NULL.
117033 */
117034 static void *fts3MallocZero(int nByte){
117035   void *pRet = sqlite3_malloc(nByte);
117036   if( pRet ) memset(pRet, 0, nByte);
117037   return pRet;
117038 }
117039 
117040 
117041 /*
117042 ** Extract the next token from buffer z (length n) using the tokenizer
117043 ** and other information (column names etc.) in pParse. Create an Fts3Expr
117044 ** structure of type FTSQUERY_PHRASE containing a phrase consisting of this
117045 ** single token and set *ppExpr to point to it. If the end of the buffer is
117046 ** reached before a token is found, set *ppExpr to zero. It is the
117047 ** responsibility of the caller to eventually deallocate the allocated
117048 ** Fts3Expr structure (if any) by passing it to sqlite3_free().
117049 **
117050 ** Return SQLITE_OK if successful, or SQLITE_NOMEM if a memory allocation
117051 ** fails.
117052 */
117053 static int getNextToken(
117054   ParseContext *pParse,                   /* fts3 query parse context */
117055   int iCol,                               /* Value for Fts3Phrase.iColumn */
117056   const char *z, int n,                   /* Input string */
117057   Fts3Expr **ppExpr,                      /* OUT: expression */
117058   int *pnConsumed                         /* OUT: Number of bytes consumed */
117059 ){
117060   sqlite3_tokenizer *pTokenizer = pParse->pTokenizer;
117061   sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
117062   int rc;
117063   sqlite3_tokenizer_cursor *pCursor;
117064   Fts3Expr *pRet = 0;
117065   int nConsumed = 0;
117066 
117067   rc = pModule->xOpen(pTokenizer, z, n, &pCursor);
117068   if( rc==SQLITE_OK ){
117069     const char *zToken;
117070     int nToken, iStart, iEnd, iPosition;
117071     int nByte;                               /* total space to allocate */
117072 
117073     pCursor->pTokenizer = pTokenizer;
117074     rc = pModule->xNext(pCursor, &zToken, &nToken, &iStart, &iEnd, &iPosition);
117075 
117076     if( rc==SQLITE_OK ){
117077       nByte = sizeof(Fts3Expr) + sizeof(Fts3Phrase) + nToken;
117078       pRet = (Fts3Expr *)fts3MallocZero(nByte);
117079       if( !pRet ){
117080         rc = SQLITE_NOMEM;
117081       }else{
117082         pRet->eType = FTSQUERY_PHRASE;
117083         pRet->pPhrase = (Fts3Phrase *)&pRet[1];
117084         pRet->pPhrase->nToken = 1;
117085         pRet->pPhrase->iColumn = iCol;
117086         pRet->pPhrase->aToken[0].n = nToken;
117087         pRet->pPhrase->aToken[0].z = (char *)&pRet->pPhrase[1];
117088         memcpy(pRet->pPhrase->aToken[0].z, zToken, nToken);
117089 
117090         if( iEnd<n && z[iEnd]=='*' ){
117091           pRet->pPhrase->aToken[0].isPrefix = 1;
117092           iEnd++;
117093         }
117094         if( !sqlite3_fts3_enable_parentheses && iStart>0 && z[iStart-1]=='-' ){
117095           pParse->isNot = 1;
117096         }
117097       }
117098       nConsumed = iEnd;
117099     }
117100 
117101     pModule->xClose(pCursor);
117102   }
117103 
117104   *pnConsumed = nConsumed;
117105   *ppExpr = pRet;
117106   return rc;
117107 }
117108 
117109 
117110 /*
117111 ** Enlarge a memory allocation.  If an out-of-memory allocation occurs,
117112 ** then free the old allocation.
117113 */
117114 static void *fts3ReallocOrFree(void *pOrig, int nNew){
117115   void *pRet = sqlite3_realloc(pOrig, nNew);
117116   if( !pRet ){
117117     sqlite3_free(pOrig);
117118   }
117119   return pRet;
117120 }
117121 
117122 /*
117123 ** Buffer zInput, length nInput, contains the contents of a quoted string
117124 ** that appeared as part of an fts3 query expression. Neither quote character
117125 ** is included in the buffer. This function attempts to tokenize the entire
117126 ** input buffer and create an Fts3Expr structure of type FTSQUERY_PHRASE
117127 ** containing the results.
117128 **
117129 ** If successful, SQLITE_OK is returned and *ppExpr set to point at the
117130 ** allocated Fts3Expr structure. Otherwise, either SQLITE_NOMEM (out of memory
117131 ** error) or SQLITE_ERROR (tokenization error) is returned and *ppExpr set
117132 ** to 0.
117133 */
117134 static int getNextString(
117135   ParseContext *pParse,                   /* fts3 query parse context */
117136   const char *zInput, int nInput,         /* Input string */
117137   Fts3Expr **ppExpr                       /* OUT: expression */
117138 ){
117139   sqlite3_tokenizer *pTokenizer = pParse->pTokenizer;
117140   sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
117141   int rc;
117142   Fts3Expr *p = 0;
117143   sqlite3_tokenizer_cursor *pCursor = 0;
117144   char *zTemp = 0;
117145   int nTemp = 0;
117146 
117147   const int nSpace = sizeof(Fts3Expr) + sizeof(Fts3Phrase);
117148   int nToken = 0;
117149 
117150   /* The final Fts3Expr data structure, including the Fts3Phrase,
117151   ** Fts3PhraseToken structures token buffers are all stored as a single
117152   ** allocation so that the expression can be freed with a single call to
117153   ** sqlite3_free(). Setting this up requires a two pass approach.
117154   **
117155   ** The first pass, in the block below, uses a tokenizer cursor to iterate
117156   ** through the tokens in the expression. This pass uses fts3ReallocOrFree()
117157   ** to assemble data in two dynamic buffers:
117158   **
117159   **   Buffer p: Points to the Fts3Expr structure, followed by the Fts3Phrase
117160   **             structure, followed by the array of Fts3PhraseToken
117161   **             structures. This pass only populates the Fts3PhraseToken array.
117162   **
117163   **   Buffer zTemp: Contains copies of all tokens.
117164   **
117165   ** The second pass, in the block that begins "if( rc==SQLITE_DONE )" below,
117166   ** appends buffer zTemp to buffer p, and fills in the Fts3Expr and Fts3Phrase
117167   ** structures.
117168   */
117169   rc = pModule->xOpen(pTokenizer, zInput, nInput, &pCursor);
117170   if( rc==SQLITE_OK ){
117171     int ii;
117172     pCursor->pTokenizer = pTokenizer;
117173     for(ii=0; rc==SQLITE_OK; ii++){
117174       const char *zByte;
117175       int nByte, iBegin, iEnd, iPos;
117176       rc = pModule->xNext(pCursor, &zByte, &nByte, &iBegin, &iEnd, &iPos);
117177       if( rc==SQLITE_OK ){
117178         Fts3PhraseToken *pToken;
117179 
117180         p = fts3ReallocOrFree(p, nSpace + ii*sizeof(Fts3PhraseToken));
117181         if( !p ) goto no_mem;
117182 
117183         zTemp = fts3ReallocOrFree(zTemp, nTemp + nByte);
117184         if( !zTemp ) goto no_mem;
117185 
117186         assert( nToken==ii );
117187         pToken = &((Fts3Phrase *)(&p[1]))->aToken[ii];
117188         memset(pToken, 0, sizeof(Fts3PhraseToken));
117189 
117190         memcpy(&zTemp[nTemp], zByte, nByte);
117191         nTemp += nByte;
117192 
117193         pToken->n = nByte;
117194         pToken->isPrefix = (iEnd<nInput && zInput[iEnd]=='*');
117195         nToken = ii+1;
117196       }
117197     }
117198 
117199     pModule->xClose(pCursor);
117200     pCursor = 0;
117201   }
117202 
117203   if( rc==SQLITE_DONE ){
117204     int jj;
117205     char *zBuf = 0;
117206 
117207     p = fts3ReallocOrFree(p, nSpace + nToken*sizeof(Fts3PhraseToken) + nTemp);
117208     if( !p ) goto no_mem;
117209     memset(p, 0, (char *)&(((Fts3Phrase *)&p[1])->aToken[0])-(char *)p);
117210     p->eType = FTSQUERY_PHRASE;
117211     p->pPhrase = (Fts3Phrase *)&p[1];
117212     p->pPhrase->iColumn = pParse->iDefaultCol;
117213     p->pPhrase->nToken = nToken;
117214 
117215     zBuf = (char *)&p->pPhrase->aToken[nToken];
117216     memcpy(zBuf, zTemp, nTemp);
117217     sqlite3_free(zTemp);
117218 
117219     for(jj=0; jj<p->pPhrase->nToken; jj++){
117220       p->pPhrase->aToken[jj].z = zBuf;
117221       zBuf += p->pPhrase->aToken[jj].n;
117222     }
117223     rc = SQLITE_OK;
117224   }
117225 
117226   *ppExpr = p;
117227   return rc;
117228 no_mem:
117229 
117230   if( pCursor ){
117231     pModule->xClose(pCursor);
117232   }
117233   sqlite3_free(zTemp);
117234   sqlite3_free(p);
117235   *ppExpr = 0;
117236   return SQLITE_NOMEM;
117237 }
117238 
117239 /*
117240 ** Function getNextNode(), which is called by fts3ExprParse(), may itself
117241 ** call fts3ExprParse(). So this forward declaration is required.
117242 */
117243 static int fts3ExprParse(ParseContext *, const char *, int, Fts3Expr **, int *);
117244 
117245 /*
117246 ** The output variable *ppExpr is populated with an allocated Fts3Expr
117247 ** structure, or set to 0 if the end of the input buffer is reached.
117248 **
117249 ** Returns an SQLite error code. SQLITE_OK if everything works, SQLITE_NOMEM
117250 ** if a malloc failure occurs, or SQLITE_ERROR if a parse error is encountered.
117251 ** If SQLITE_ERROR is returned, pContext is populated with an error message.
117252 */
117253 static int getNextNode(
117254   ParseContext *pParse,                   /* fts3 query parse context */
117255   const char *z, int n,                   /* Input string */
117256   Fts3Expr **ppExpr,                      /* OUT: expression */
117257   int *pnConsumed                         /* OUT: Number of bytes consumed */
117258 ){
117259   static const struct Fts3Keyword {
117260     char *z;                              /* Keyword text */
117261     unsigned char n;                      /* Length of the keyword */
117262     unsigned char parenOnly;              /* Only valid in paren mode */
117263     unsigned char eType;                  /* Keyword code */
117264   } aKeyword[] = {
117265     { "OR" ,  2, 0, FTSQUERY_OR   },
117266     { "AND",  3, 1, FTSQUERY_AND  },
117267     { "NOT",  3, 1, FTSQUERY_NOT  },
117268     { "NEAR", 4, 0, FTSQUERY_NEAR }
117269   };
117270   int ii;
117271   int iCol;
117272   int iColLen;
117273   int rc;
117274   Fts3Expr *pRet = 0;
117275 
117276   const char *zInput = z;
117277   int nInput = n;
117278 
117279   pParse->isNot = 0;
117280 
117281   /* Skip over any whitespace before checking for a keyword, an open or
117282   ** close bracket, or a quoted string.
117283   */
117284   while( nInput>0 && fts3isspace(*zInput) ){
117285     nInput--;
117286     zInput++;
117287   }
117288   if( nInput==0 ){
117289     return SQLITE_DONE;
117290   }
117291 
117292   /* See if we are dealing with a keyword. */
117293   for(ii=0; ii<(int)(sizeof(aKeyword)/sizeof(struct Fts3Keyword)); ii++){
117294     const struct Fts3Keyword *pKey = &aKeyword[ii];
117295 
117296     if( (pKey->parenOnly & ~sqlite3_fts3_enable_parentheses)!=0 ){
117297       continue;
117298     }
117299 
117300     if( nInput>=pKey->n && 0==memcmp(zInput, pKey->z, pKey->n) ){
117301       int nNear = SQLITE_FTS3_DEFAULT_NEAR_PARAM;
117302       int nKey = pKey->n;
117303       char cNext;
117304 
117305       /* If this is a "NEAR" keyword, check for an explicit nearness. */
117306       if( pKey->eType==FTSQUERY_NEAR ){
117307         assert( nKey==4 );
117308         if( zInput[4]=='/' && zInput[5]>='0' && zInput[5]<='9' ){
117309           nNear = 0;
117310           for(nKey=5; zInput[nKey]>='0' && zInput[nKey]<='9'; nKey++){
117311             nNear = nNear * 10 + (zInput[nKey] - '0');
117312           }
117313         }
117314       }
117315 
117316       /* At this point this is probably a keyword. But for that to be true,
117317       ** the next byte must contain either whitespace, an open or close
117318       ** parenthesis, a quote character, or EOF.
117319       */
117320       cNext = zInput[nKey];
117321       if( fts3isspace(cNext)
117322        || cNext=='"' || cNext=='(' || cNext==')' || cNext==0
117323       ){
117324         pRet = (Fts3Expr *)fts3MallocZero(sizeof(Fts3Expr));
117325         if( !pRet ){
117326           return SQLITE_NOMEM;
117327         }
117328         pRet->eType = pKey->eType;
117329         pRet->nNear = nNear;
117330         *ppExpr = pRet;
117331         *pnConsumed = (int)((zInput - z) + nKey);
117332         return SQLITE_OK;
117333       }
117334 
117335       /* Turns out that wasn't a keyword after all. This happens if the
117336       ** user has supplied a token such as "ORacle". Continue.
117337       */
117338     }
117339   }
117340 
117341   /* Check for an open bracket. */
117342   if( sqlite3_fts3_enable_parentheses ){
117343     if( *zInput=='(' ){
117344       int nConsumed;
117345       pParse->nNest++;
117346       rc = fts3ExprParse(pParse, &zInput[1], nInput-1, ppExpr, &nConsumed);
117347       if( rc==SQLITE_OK && !*ppExpr ){
117348         rc = SQLITE_DONE;
117349       }
117350       *pnConsumed = (int)((zInput - z) + 1 + nConsumed);
117351       return rc;
117352     }
117353 
117354     /* Check for a close bracket. */
117355     if( *zInput==')' ){
117356       pParse->nNest--;
117357       *pnConsumed = (int)((zInput - z) + 1);
117358       return SQLITE_DONE;
117359     }
117360   }
117361 
117362   /* See if we are dealing with a quoted phrase. If this is the case, then
117363   ** search for the closing quote and pass the whole string to getNextString()
117364   ** for processing. This is easy to do, as fts3 has no syntax for escaping
117365   ** a quote character embedded in a string.
117366   */
117367   if( *zInput=='"' ){
117368     for(ii=1; ii<nInput && zInput[ii]!='"'; ii++);
117369     *pnConsumed = (int)((zInput - z) + ii + 1);
117370     if( ii==nInput ){
117371       return SQLITE_ERROR;
117372     }
117373     return getNextString(pParse, &zInput[1], ii-1, ppExpr);
117374   }
117375 
117376 
117377   /* If control flows to this point, this must be a regular token, or
117378   ** the end of the input. Read a regular token using the sqlite3_tokenizer
117379   ** interface. Before doing so, figure out if there is an explicit
117380   ** column specifier for the token.
117381   **
117382   ** TODO: Strangely, it is not possible to associate a column specifier
117383   ** with a quoted phrase, only with a single token. Not sure if this was
117384   ** an implementation artifact or an intentional decision when fts3 was
117385   ** first implemented. Whichever it was, this module duplicates the
117386   ** limitation.
117387   */
117388   iCol = pParse->iDefaultCol;
117389   iColLen = 0;
117390   for(ii=0; ii<pParse->nCol; ii++){
117391     const char *zStr = pParse->azCol[ii];
117392     int nStr = (int)strlen(zStr);
117393     if( nInput>nStr && zInput[nStr]==':'
117394      && sqlite3_strnicmp(zStr, zInput, nStr)==0
117395     ){
117396       iCol = ii;
117397       iColLen = (int)((zInput - z) + nStr + 1);
117398       break;
117399     }
117400   }
117401   rc = getNextToken(pParse, iCol, &z[iColLen], n-iColLen, ppExpr, pnConsumed);
117402   *pnConsumed += iColLen;
117403   return rc;
117404 }
117405 
117406 /*
117407 ** The argument is an Fts3Expr structure for a binary operator (any type
117408 ** except an FTSQUERY_PHRASE). Return an integer value representing the
117409 ** precedence of the operator. Lower values have a higher precedence (i.e.
117410 ** group more tightly). For example, in the C language, the == operator
117411 ** groups more tightly than ||, and would therefore have a higher precedence.
117412 **
117413 ** When using the new fts3 query syntax (when SQLITE_ENABLE_FTS3_PARENTHESIS
117414 ** is defined), the order of the operators in precedence from highest to
117415 ** lowest is:
117416 **
117417 **   NEAR
117418 **   NOT
117419 **   AND (including implicit ANDs)
117420 **   OR
117421 **
117422 ** Note that when using the old query syntax, the OR operator has a higher
117423 ** precedence than the AND operator.
117424 */
117425 static int opPrecedence(Fts3Expr *p){
117426   assert( p->eType!=FTSQUERY_PHRASE );
117427   if( sqlite3_fts3_enable_parentheses ){
117428     return p->eType;
117429   }else if( p->eType==FTSQUERY_NEAR ){
117430     return 1;
117431   }else if( p->eType==FTSQUERY_OR ){
117432     return 2;
117433   }
117434   assert( p->eType==FTSQUERY_AND );
117435   return 3;
117436 }
117437 
117438 /*
117439 ** Argument ppHead contains a pointer to the current head of a query
117440 ** expression tree being parsed. pPrev is the expression node most recently
117441 ** inserted into the tree. This function adds pNew, which is always a binary
117442 ** operator node, into the expression tree based on the relative precedence
117443 ** of pNew and the existing nodes of the tree. This may result in the head
117444 ** of the tree changing, in which case *ppHead is set to the new root node.
117445 */
117446 static void insertBinaryOperator(
117447   Fts3Expr **ppHead,       /* Pointer to the root node of a tree */
117448   Fts3Expr *pPrev,         /* Node most recently inserted into the tree */
117449   Fts3Expr *pNew           /* New binary node to insert into expression tree */
117450 ){
117451   Fts3Expr *pSplit = pPrev;
117452   while( pSplit->pParent && opPrecedence(pSplit->pParent)<=opPrecedence(pNew) ){
117453     pSplit = pSplit->pParent;
117454   }
117455 
117456   if( pSplit->pParent ){
117457     assert( pSplit->pParent->pRight==pSplit );
117458     pSplit->pParent->pRight = pNew;
117459     pNew->pParent = pSplit->pParent;
117460   }else{
117461     *ppHead = pNew;
117462   }
117463   pNew->pLeft = pSplit;
117464   pSplit->pParent = pNew;
117465 }
117466 
117467 /*
117468 ** Parse the fts3 query expression found in buffer z, length n. This function
117469 ** returns either when the end of the buffer is reached or an unmatched
117470 ** closing bracket - ')' - is encountered.
117471 **
117472 ** If successful, SQLITE_OK is returned, *ppExpr is set to point to the
117473 ** parsed form of the expression and *pnConsumed is set to the number of
117474 ** bytes read from buffer z. Otherwise, *ppExpr is set to 0 and SQLITE_NOMEM
117475 ** (out of memory error) or SQLITE_ERROR (parse error) is returned.
117476 */
117477 static int fts3ExprParse(
117478   ParseContext *pParse,                   /* fts3 query parse context */
117479   const char *z, int n,                   /* Text of MATCH query */
117480   Fts3Expr **ppExpr,                      /* OUT: Parsed query structure */
117481   int *pnConsumed                         /* OUT: Number of bytes consumed */
117482 ){
117483   Fts3Expr *pRet = 0;
117484   Fts3Expr *pPrev = 0;
117485   Fts3Expr *pNotBranch = 0;               /* Only used in legacy parse mode */
117486   int nIn = n;
117487   const char *zIn = z;
117488   int rc = SQLITE_OK;
117489   int isRequirePhrase = 1;
117490 
117491   while( rc==SQLITE_OK ){
117492     Fts3Expr *p = 0;
117493     int nByte = 0;
117494     rc = getNextNode(pParse, zIn, nIn, &p, &nByte);
117495     if( rc==SQLITE_OK ){
117496       int isPhrase;
117497 
117498       if( !sqlite3_fts3_enable_parentheses
117499        && p->eType==FTSQUERY_PHRASE && pParse->isNot
117500       ){
117501         /* Create an implicit NOT operator. */
117502         Fts3Expr *pNot = fts3MallocZero(sizeof(Fts3Expr));
117503         if( !pNot ){
117504           sqlite3Fts3ExprFree(p);
117505           rc = SQLITE_NOMEM;
117506           goto exprparse_out;
117507         }
117508         pNot->eType = FTSQUERY_NOT;
117509         pNot->pRight = p;
117510         if( pNotBranch ){
117511           pNot->pLeft = pNotBranch;
117512         }
117513         pNotBranch = pNot;
117514         p = pPrev;
117515       }else{
117516         int eType = p->eType;
117517         isPhrase = (eType==FTSQUERY_PHRASE || p->pLeft);
117518 
117519         /* The isRequirePhrase variable is set to true if a phrase or
117520         ** an expression contained in parenthesis is required. If a
117521         ** binary operator (AND, OR, NOT or NEAR) is encounted when
117522         ** isRequirePhrase is set, this is a syntax error.
117523         */
117524         if( !isPhrase && isRequirePhrase ){
117525           sqlite3Fts3ExprFree(p);
117526           rc = SQLITE_ERROR;
117527           goto exprparse_out;
117528         }
117529 
117530         if( isPhrase && !isRequirePhrase ){
117531           /* Insert an implicit AND operator. */
117532           Fts3Expr *pAnd;
117533           assert( pRet && pPrev );
117534           pAnd = fts3MallocZero(sizeof(Fts3Expr));
117535           if( !pAnd ){
117536             sqlite3Fts3ExprFree(p);
117537             rc = SQLITE_NOMEM;
117538             goto exprparse_out;
117539           }
117540           pAnd->eType = FTSQUERY_AND;
117541           insertBinaryOperator(&pRet, pPrev, pAnd);
117542           pPrev = pAnd;
117543         }
117544 
117545         /* This test catches attempts to make either operand of a NEAR
117546         ** operator something other than a phrase. For example, either of
117547         ** the following:
117548         **
117549         **    (bracketed expression) NEAR phrase
117550         **    phrase NEAR (bracketed expression)
117551         **
117552         ** Return an error in either case.
117553         */
117554         if( pPrev && (
117555             (eType==FTSQUERY_NEAR && !isPhrase && pPrev->eType!=FTSQUERY_PHRASE)
117556          || (eType!=FTSQUERY_PHRASE && isPhrase && pPrev->eType==FTSQUERY_NEAR)
117557         )){
117558           sqlite3Fts3ExprFree(p);
117559           rc = SQLITE_ERROR;
117560           goto exprparse_out;
117561         }
117562 
117563         if( isPhrase ){
117564           if( pRet ){
117565             assert( pPrev && pPrev->pLeft && pPrev->pRight==0 );
117566             pPrev->pRight = p;
117567             p->pParent = pPrev;
117568           }else{
117569             pRet = p;
117570           }
117571         }else{
117572           insertBinaryOperator(&pRet, pPrev, p);
117573         }
117574         isRequirePhrase = !isPhrase;
117575       }
117576       assert( nByte>0 );
117577     }
117578     assert( rc!=SQLITE_OK || (nByte>0 && nByte<=nIn) );
117579     nIn -= nByte;
117580     zIn += nByte;
117581     pPrev = p;
117582   }
117583 
117584   if( rc==SQLITE_DONE && pRet && isRequirePhrase ){
117585     rc = SQLITE_ERROR;
117586   }
117587 
117588   if( rc==SQLITE_DONE ){
117589     rc = SQLITE_OK;
117590     if( !sqlite3_fts3_enable_parentheses && pNotBranch ){
117591       if( !pRet ){
117592         rc = SQLITE_ERROR;
117593       }else{
117594         Fts3Expr *pIter = pNotBranch;
117595         while( pIter->pLeft ){
117596           pIter = pIter->pLeft;
117597         }
117598         pIter->pLeft = pRet;
117599         pRet = pNotBranch;
117600       }
117601     }
117602   }
117603   *pnConsumed = n - nIn;
117604 
117605 exprparse_out:
117606   if( rc!=SQLITE_OK ){
117607     sqlite3Fts3ExprFree(pRet);
117608     sqlite3Fts3ExprFree(pNotBranch);
117609     pRet = 0;
117610   }
117611   *ppExpr = pRet;
117612   return rc;
117613 }
117614 
117615 /*
117616 ** Parameters z and n contain a pointer to and length of a buffer containing
117617 ** an fts3 query expression, respectively. This function attempts to parse the
117618 ** query expression and create a tree of Fts3Expr structures representing the
117619 ** parsed expression. If successful, *ppExpr is set to point to the head
117620 ** of the parsed expression tree and SQLITE_OK is returned. If an error
117621 ** occurs, either SQLITE_NOMEM (out-of-memory error) or SQLITE_ERROR (parse
117622 ** error) is returned and *ppExpr is set to 0.
117623 **
117624 ** If parameter n is a negative number, then z is assumed to point to a
117625 ** nul-terminated string and the length is determined using strlen().
117626 **
117627 ** The first parameter, pTokenizer, is passed the fts3 tokenizer module to
117628 ** use to normalize query tokens while parsing the expression. The azCol[]
117629 ** array, which is assumed to contain nCol entries, should contain the names
117630 ** of each column in the target fts3 table, in order from left to right.
117631 ** Column names must be nul-terminated strings.
117632 **
117633 ** The iDefaultCol parameter should be passed the index of the table column
117634 ** that appears on the left-hand-side of the MATCH operator (the default
117635 ** column to match against for tokens for which a column name is not explicitly
117636 ** specified as part of the query string), or -1 if tokens may by default
117637 ** match any table column.
117638 */
117639 SQLITE_PRIVATE int sqlite3Fts3ExprParse(
117640   sqlite3_tokenizer *pTokenizer,      /* Tokenizer module */
117641   char **azCol,                       /* Array of column names for fts3 table */
117642   int nCol,                           /* Number of entries in azCol[] */
117643   int iDefaultCol,                    /* Default column to query */
117644   const char *z, int n,               /* Text of MATCH query */
117645   Fts3Expr **ppExpr                   /* OUT: Parsed query structure */
117646 ){
117647   int nParsed;
117648   int rc;
117649   ParseContext sParse;
117650   sParse.pTokenizer = pTokenizer;
117651   sParse.azCol = (const char **)azCol;
117652   sParse.nCol = nCol;
117653   sParse.iDefaultCol = iDefaultCol;
117654   sParse.nNest = 0;
117655   if( z==0 ){
117656     *ppExpr = 0;
117657     return SQLITE_OK;
117658   }
117659   if( n<0 ){
117660     n = (int)strlen(z);
117661   }
117662   rc = fts3ExprParse(&sParse, z, n, ppExpr, &nParsed);
117663 
117664   /* Check for mismatched parenthesis */
117665   if( rc==SQLITE_OK && sParse.nNest ){
117666     rc = SQLITE_ERROR;
117667     sqlite3Fts3ExprFree(*ppExpr);
117668     *ppExpr = 0;
117669   }
117670 
117671   return rc;
117672 }
117673 
117674 /*
117675 ** Free a parsed fts3 query expression allocated by sqlite3Fts3ExprParse().
117676 */
117677 SQLITE_PRIVATE void sqlite3Fts3ExprFree(Fts3Expr *p){
117678   if( p ){
117679     assert( p->eType==FTSQUERY_PHRASE || p->pPhrase==0 );
117680     sqlite3Fts3ExprFree(p->pLeft);
117681     sqlite3Fts3ExprFree(p->pRight);
117682     sqlite3Fts3EvalPhraseCleanup(p->pPhrase);
117683     sqlite3_free(p->aMI);
117684     sqlite3_free(p);
117685   }
117686 }
117687 
117688 /****************************************************************************
117689 *****************************************************************************
117690 ** Everything after this point is just test code.
117691 */
117692 
117693 #ifdef SQLITE_TEST
117694 
117695 
117696 /*
117697 ** Function to query the hash-table of tokenizers (see README.tokenizers).
117698 */
117699 static int queryTestTokenizer(
117700   sqlite3 *db,
117701   const char *zName,
117702   const sqlite3_tokenizer_module **pp
117703 ){
117704   int rc;
117705   sqlite3_stmt *pStmt;
117706   const char zSql[] = "SELECT fts3_tokenizer(?)";
117707 
117708   *pp = 0;
117709   rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
117710   if( rc!=SQLITE_OK ){
117711     return rc;
117712   }
117713 
117714   sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC);
117715   if( SQLITE_ROW==sqlite3_step(pStmt) ){
117716     if( sqlite3_column_type(pStmt, 0)==SQLITE_BLOB ){
117717       memcpy((void *)pp, sqlite3_column_blob(pStmt, 0), sizeof(*pp));
117718     }
117719   }
117720 
117721   return sqlite3_finalize(pStmt);
117722 }
117723 
117724 /*
117725 ** Return a pointer to a buffer containing a text representation of the
117726 ** expression passed as the first argument. The buffer is obtained from
117727 ** sqlite3_malloc(). It is the responsibility of the caller to use
117728 ** sqlite3_free() to release the memory. If an OOM condition is encountered,
117729 ** NULL is returned.
117730 **
117731 ** If the second argument is not NULL, then its contents are prepended to
117732 ** the returned expression text and then freed using sqlite3_free().
117733 */
117734 static char *exprToString(Fts3Expr *pExpr, char *zBuf){
117735   switch( pExpr->eType ){
117736     case FTSQUERY_PHRASE: {
117737       Fts3Phrase *pPhrase = pExpr->pPhrase;
117738       int i;
117739       zBuf = sqlite3_mprintf(
117740           "%zPHRASE %d 0", zBuf, pPhrase->iColumn);
117741       for(i=0; zBuf && i<pPhrase->nToken; i++){
117742         zBuf = sqlite3_mprintf("%z %.*s%s", zBuf,
117743             pPhrase->aToken[i].n, pPhrase->aToken[i].z,
117744             (pPhrase->aToken[i].isPrefix?"+":"")
117745         );
117746       }
117747       return zBuf;
117748     }
117749 
117750     case FTSQUERY_NEAR:
117751       zBuf = sqlite3_mprintf("%zNEAR/%d ", zBuf, pExpr->nNear);
117752       break;
117753     case FTSQUERY_NOT:
117754       zBuf = sqlite3_mprintf("%zNOT ", zBuf);
117755       break;
117756     case FTSQUERY_AND:
117757       zBuf = sqlite3_mprintf("%zAND ", zBuf);
117758       break;
117759     case FTSQUERY_OR:
117760       zBuf = sqlite3_mprintf("%zOR ", zBuf);
117761       break;
117762   }
117763 
117764   if( zBuf ) zBuf = sqlite3_mprintf("%z{", zBuf);
117765   if( zBuf ) zBuf = exprToString(pExpr->pLeft, zBuf);
117766   if( zBuf ) zBuf = sqlite3_mprintf("%z} {", zBuf);
117767 
117768   if( zBuf ) zBuf = exprToString(pExpr->pRight, zBuf);
117769   if( zBuf ) zBuf = sqlite3_mprintf("%z}", zBuf);
117770 
117771   return zBuf;
117772 }
117773 
117774 /*
117775 ** This is the implementation of a scalar SQL function used to test the
117776 ** expression parser. It should be called as follows:
117777 **
117778 **   fts3_exprtest(<tokenizer>, <expr>, <column 1>, ...);
117779 **
117780 ** The first argument, <tokenizer>, is the name of the fts3 tokenizer used
117781 ** to parse the query expression (see README.tokenizers). The second argument
117782 ** is the query expression to parse. Each subsequent argument is the name
117783 ** of a column of the fts3 table that the query expression may refer to.
117784 ** For example:
117785 **
117786 **   SELECT fts3_exprtest('simple', 'Bill col2:Bloggs', 'col1', 'col2');
117787 */
117788 static void fts3ExprTest(
117789   sqlite3_context *context,
117790   int argc,
117791   sqlite3_value **argv
117792 ){
117793   sqlite3_tokenizer_module const *pModule = 0;
117794   sqlite3_tokenizer *pTokenizer = 0;
117795   int rc;
117796   char **azCol = 0;
117797   const char *zExpr;
117798   int nExpr;
117799   int nCol;
117800   int ii;
117801   Fts3Expr *pExpr;
117802   char *zBuf = 0;
117803   sqlite3 *db = sqlite3_context_db_handle(context);
117804 
117805   if( argc<3 ){
117806     sqlite3_result_error(context,
117807         "Usage: fts3_exprtest(tokenizer, expr, col1, ...", -1
117808     );
117809     return;
117810   }
117811 
117812   rc = queryTestTokenizer(db,
117813                           (const char *)sqlite3_value_text(argv[0]), &pModule);
117814   if( rc==SQLITE_NOMEM ){
117815     sqlite3_result_error_nomem(context);
117816     goto exprtest_out;
117817   }else if( !pModule ){
117818     sqlite3_result_error(context, "No such tokenizer module", -1);
117819     goto exprtest_out;
117820   }
117821 
117822   rc = pModule->xCreate(0, 0, &pTokenizer);
117823   assert( rc==SQLITE_NOMEM || rc==SQLITE_OK );
117824   if( rc==SQLITE_NOMEM ){
117825     sqlite3_result_error_nomem(context);
117826     goto exprtest_out;
117827   }
117828   pTokenizer->pModule = pModule;
117829 
117830   zExpr = (const char *)sqlite3_value_text(argv[1]);
117831   nExpr = sqlite3_value_bytes(argv[1]);
117832   nCol = argc-2;
117833   azCol = (char **)sqlite3_malloc(nCol*sizeof(char *));
117834   if( !azCol ){
117835     sqlite3_result_error_nomem(context);
117836     goto exprtest_out;
117837   }
117838   for(ii=0; ii<nCol; ii++){
117839     azCol[ii] = (char *)sqlite3_value_text(argv[ii+2]);
117840   }
117841 
117842   rc = sqlite3Fts3ExprParse(
117843       pTokenizer, azCol, nCol, nCol, zExpr, nExpr, &pExpr
117844   );
117845   if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM ){
117846     sqlite3_result_error(context, "Error parsing expression", -1);
117847   }else if( rc==SQLITE_NOMEM || !(zBuf = exprToString(pExpr, 0)) ){
117848     sqlite3_result_error_nomem(context);
117849   }else{
117850     sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
117851     sqlite3_free(zBuf);
117852   }
117853 
117854   sqlite3Fts3ExprFree(pExpr);
117855 
117856 exprtest_out:
117857   if( pModule && pTokenizer ){
117858     rc = pModule->xDestroy(pTokenizer);
117859   }
117860   sqlite3_free(azCol);
117861 }
117862 
117863 /*
117864 ** Register the query expression parser test function fts3_exprtest()
117865 ** with database connection db.
117866 */
117867 SQLITE_PRIVATE int sqlite3Fts3ExprInitTestInterface(sqlite3* db){
117868   return sqlite3_create_function(
117869       db, "fts3_exprtest", -1, SQLITE_UTF8, 0, fts3ExprTest, 0, 0
117870   );
117871 }
117872 
117873 #endif
117874 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
117875 
117876 /************** End of fts3_expr.c *******************************************/
117877 /************** Begin file fts3_hash.c ***************************************/
117878 /*
117879 ** 2001 September 22
117880 **
117881 ** The author disclaims copyright to this source code.  In place of
117882 ** a legal notice, here is a blessing:
117883 **
117884 **    May you do good and not evil.
117885 **    May you find forgiveness for yourself and forgive others.
117886 **    May you share freely, never taking more than you give.
117887 **
117888 *************************************************************************
117889 ** This is the implementation of generic hash-tables used in SQLite.
117890 ** We've modified it slightly to serve as a standalone hash table
117891 ** implementation for the full-text indexing module.
117892 */
117893 
117894 /*
117895 ** The code in this file is only compiled if:
117896 **
117897 **     * The FTS3 module is being built as an extension
117898 **       (in which case SQLITE_CORE is not defined), or
117899 **
117900 **     * The FTS3 module is being built into the core of
117901 **       SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
117902 */
117903 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
117904 
117905 
117906 
117907 /*
117908 ** Malloc and Free functions
117909 */
117910 static void *fts3HashMalloc(int n){
117911   void *p = sqlite3_malloc(n);
117912   if( p ){
117913     memset(p, 0, n);
117914   }
117915   return p;
117916 }
117917 static void fts3HashFree(void *p){
117918   sqlite3_free(p);
117919 }
117920 
117921 /* Turn bulk memory into a hash table object by initializing the
117922 ** fields of the Hash structure.
117923 **
117924 ** "pNew" is a pointer to the hash table that is to be initialized.
117925 ** keyClass is one of the constants
117926 ** FTS3_HASH_BINARY or FTS3_HASH_STRING.  The value of keyClass
117927 ** determines what kind of key the hash table will use.  "copyKey" is
117928 ** true if the hash table should make its own private copy of keys and
117929 ** false if it should just use the supplied pointer.
117930 */
117931 SQLITE_PRIVATE void sqlite3Fts3HashInit(Fts3Hash *pNew, char keyClass, char copyKey){
117932   assert( pNew!=0 );
117933   assert( keyClass>=FTS3_HASH_STRING && keyClass<=FTS3_HASH_BINARY );
117934   pNew->keyClass = keyClass;
117935   pNew->copyKey = copyKey;
117936   pNew->first = 0;
117937   pNew->count = 0;
117938   pNew->htsize = 0;
117939   pNew->ht = 0;
117940 }
117941 
117942 /* Remove all entries from a hash table.  Reclaim all memory.
117943 ** Call this routine to delete a hash table or to reset a hash table
117944 ** to the empty state.
117945 */
117946 SQLITE_PRIVATE void sqlite3Fts3HashClear(Fts3Hash *pH){
117947   Fts3HashElem *elem;         /* For looping over all elements of the table */
117948 
117949   assert( pH!=0 );
117950   elem = pH->first;
117951   pH->first = 0;
117952   fts3HashFree(pH->ht);
117953   pH->ht = 0;
117954   pH->htsize = 0;
117955   while( elem ){
117956     Fts3HashElem *next_elem = elem->next;
117957     if( pH->copyKey && elem->pKey ){
117958       fts3HashFree(elem->pKey);
117959     }
117960     fts3HashFree(elem);
117961     elem = next_elem;
117962   }
117963   pH->count = 0;
117964 }
117965 
117966 /*
117967 ** Hash and comparison functions when the mode is FTS3_HASH_STRING
117968 */
117969 static int fts3StrHash(const void *pKey, int nKey){
117970   const char *z = (const char *)pKey;
117971   int h = 0;
117972   if( nKey<=0 ) nKey = (int) strlen(z);
117973   while( nKey > 0  ){
117974     h = (h<<3) ^ h ^ *z++;
117975     nKey--;
117976   }
117977   return h & 0x7fffffff;
117978 }
117979 static int fts3StrCompare(const void *pKey1, int n1, const void *pKey2, int n2){
117980   if( n1!=n2 ) return 1;
117981   return strncmp((const char*)pKey1,(const char*)pKey2,n1);
117982 }
117983 
117984 /*
117985 ** Hash and comparison functions when the mode is FTS3_HASH_BINARY
117986 */
117987 static int fts3BinHash(const void *pKey, int nKey){
117988   int h = 0;
117989   const char *z = (const char *)pKey;
117990   while( nKey-- > 0 ){
117991     h = (h<<3) ^ h ^ *(z++);
117992   }
117993   return h & 0x7fffffff;
117994 }
117995 static int fts3BinCompare(const void *pKey1, int n1, const void *pKey2, int n2){
117996   if( n1!=n2 ) return 1;
117997   return memcmp(pKey1,pKey2,n1);
117998 }
117999 
118000 /*
118001 ** Return a pointer to the appropriate hash function given the key class.
118002 **
118003 ** The C syntax in this function definition may be unfamilar to some
118004 ** programmers, so we provide the following additional explanation:
118005 **
118006 ** The name of the function is "ftsHashFunction".  The function takes a
118007 ** single parameter "keyClass".  The return value of ftsHashFunction()
118008 ** is a pointer to another function.  Specifically, the return value
118009 ** of ftsHashFunction() is a pointer to a function that takes two parameters
118010 ** with types "const void*" and "int" and returns an "int".
118011 */
118012 static int (*ftsHashFunction(int keyClass))(const void*,int){
118013   if( keyClass==FTS3_HASH_STRING ){
118014     return &fts3StrHash;
118015   }else{
118016     assert( keyClass==FTS3_HASH_BINARY );
118017     return &fts3BinHash;
118018   }
118019 }
118020 
118021 /*
118022 ** Return a pointer to the appropriate hash function given the key class.
118023 **
118024 ** For help in interpreted the obscure C code in the function definition,
118025 ** see the header comment on the previous function.
118026 */
118027 static int (*ftsCompareFunction(int keyClass))(const void*,int,const void*,int){
118028   if( keyClass==FTS3_HASH_STRING ){
118029     return &fts3StrCompare;
118030   }else{
118031     assert( keyClass==FTS3_HASH_BINARY );
118032     return &fts3BinCompare;
118033   }
118034 }
118035 
118036 /* Link an element into the hash table
118037 */
118038 static void fts3HashInsertElement(
118039   Fts3Hash *pH,            /* The complete hash table */
118040   struct _fts3ht *pEntry,  /* The entry into which pNew is inserted */
118041   Fts3HashElem *pNew       /* The element to be inserted */
118042 ){
118043   Fts3HashElem *pHead;     /* First element already in pEntry */
118044   pHead = pEntry->chain;
118045   if( pHead ){
118046     pNew->next = pHead;
118047     pNew->prev = pHead->prev;
118048     if( pHead->prev ){ pHead->prev->next = pNew; }
118049     else             { pH->first = pNew; }
118050     pHead->prev = pNew;
118051   }else{
118052     pNew->next = pH->first;
118053     if( pH->first ){ pH->first->prev = pNew; }
118054     pNew->prev = 0;
118055     pH->first = pNew;
118056   }
118057   pEntry->count++;
118058   pEntry->chain = pNew;
118059 }
118060 
118061 
118062 /* Resize the hash table so that it cantains "new_size" buckets.
118063 ** "new_size" must be a power of 2.  The hash table might fail
118064 ** to resize if sqliteMalloc() fails.
118065 **
118066 ** Return non-zero if a memory allocation error occurs.
118067 */
118068 static int fts3Rehash(Fts3Hash *pH, int new_size){
118069   struct _fts3ht *new_ht;          /* The new hash table */
118070   Fts3HashElem *elem, *next_elem;  /* For looping over existing elements */
118071   int (*xHash)(const void*,int);   /* The hash function */
118072 
118073   assert( (new_size & (new_size-1))==0 );
118074   new_ht = (struct _fts3ht *)fts3HashMalloc( new_size*sizeof(struct _fts3ht) );
118075   if( new_ht==0 ) return 1;
118076   fts3HashFree(pH->ht);
118077   pH->ht = new_ht;
118078   pH->htsize = new_size;
118079   xHash = ftsHashFunction(pH->keyClass);
118080   for(elem=pH->first, pH->first=0; elem; elem = next_elem){
118081     int h = (*xHash)(elem->pKey, elem->nKey) & (new_size-1);
118082     next_elem = elem->next;
118083     fts3HashInsertElement(pH, &new_ht[h], elem);
118084   }
118085   return 0;
118086 }
118087 
118088 /* This function (for internal use only) locates an element in an
118089 ** hash table that matches the given key.  The hash for this key has
118090 ** already been computed and is passed as the 4th parameter.
118091 */
118092 static Fts3HashElem *fts3FindElementByHash(
118093   const Fts3Hash *pH, /* The pH to be searched */
118094   const void *pKey,   /* The key we are searching for */
118095   int nKey,
118096   int h               /* The hash for this key. */
118097 ){
118098   Fts3HashElem *elem;            /* Used to loop thru the element list */
118099   int count;                     /* Number of elements left to test */
118100   int (*xCompare)(const void*,int,const void*,int);  /* comparison function */
118101 
118102   if( pH->ht ){
118103     struct _fts3ht *pEntry = &pH->ht[h];
118104     elem = pEntry->chain;
118105     count = pEntry->count;
118106     xCompare = ftsCompareFunction(pH->keyClass);
118107     while( count-- && elem ){
118108       if( (*xCompare)(elem->pKey,elem->nKey,pKey,nKey)==0 ){
118109         return elem;
118110       }
118111       elem = elem->next;
118112     }
118113   }
118114   return 0;
118115 }
118116 
118117 /* Remove a single entry from the hash table given a pointer to that
118118 ** element and a hash on the element's key.
118119 */
118120 static void fts3RemoveElementByHash(
118121   Fts3Hash *pH,         /* The pH containing "elem" */
118122   Fts3HashElem* elem,   /* The element to be removed from the pH */
118123   int h                 /* Hash value for the element */
118124 ){
118125   struct _fts3ht *pEntry;
118126   if( elem->prev ){
118127     elem->prev->next = elem->next;
118128   }else{
118129     pH->first = elem->next;
118130   }
118131   if( elem->next ){
118132     elem->next->prev = elem->prev;
118133   }
118134   pEntry = &pH->ht[h];
118135   if( pEntry->chain==elem ){
118136     pEntry->chain = elem->next;
118137   }
118138   pEntry->count--;
118139   if( pEntry->count<=0 ){
118140     pEntry->chain = 0;
118141   }
118142   if( pH->copyKey && elem->pKey ){
118143     fts3HashFree(elem->pKey);
118144   }
118145   fts3HashFree( elem );
118146   pH->count--;
118147   if( pH->count<=0 ){
118148     assert( pH->first==0 );
118149     assert( pH->count==0 );
118150     fts3HashClear(pH);
118151   }
118152 }
118153 
118154 SQLITE_PRIVATE Fts3HashElem *sqlite3Fts3HashFindElem(
118155   const Fts3Hash *pH,
118156   const void *pKey,
118157   int nKey
118158 ){
118159   int h;                          /* A hash on key */
118160   int (*xHash)(const void*,int);  /* The hash function */
118161 
118162   if( pH==0 || pH->ht==0 ) return 0;
118163   xHash = ftsHashFunction(pH->keyClass);
118164   assert( xHash!=0 );
118165   h = (*xHash)(pKey,nKey);
118166   assert( (pH->htsize & (pH->htsize-1))==0 );
118167   return fts3FindElementByHash(pH,pKey,nKey, h & (pH->htsize-1));
118168 }
118169 
118170 /*
118171 ** Attempt to locate an element of the hash table pH with a key
118172 ** that matches pKey,nKey.  Return the data for this element if it is
118173 ** found, or NULL if there is no match.
118174 */
118175 SQLITE_PRIVATE void *sqlite3Fts3HashFind(const Fts3Hash *pH, const void *pKey, int nKey){
118176   Fts3HashElem *pElem;            /* The element that matches key (if any) */
118177 
118178   pElem = sqlite3Fts3HashFindElem(pH, pKey, nKey);
118179   return pElem ? pElem->data : 0;
118180 }
118181 
118182 /* Insert an element into the hash table pH.  The key is pKey,nKey
118183 ** and the data is "data".
118184 **
118185 ** If no element exists with a matching key, then a new
118186 ** element is created.  A copy of the key is made if the copyKey
118187 ** flag is set.  NULL is returned.
118188 **
118189 ** If another element already exists with the same key, then the
118190 ** new data replaces the old data and the old data is returned.
118191 ** The key is not copied in this instance.  If a malloc fails, then
118192 ** the new data is returned and the hash table is unchanged.
118193 **
118194 ** If the "data" parameter to this function is NULL, then the
118195 ** element corresponding to "key" is removed from the hash table.
118196 */
118197 SQLITE_PRIVATE void *sqlite3Fts3HashInsert(
118198   Fts3Hash *pH,        /* The hash table to insert into */
118199   const void *pKey,    /* The key */
118200   int nKey,            /* Number of bytes in the key */
118201   void *data           /* The data */
118202 ){
118203   int hraw;                 /* Raw hash value of the key */
118204   int h;                    /* the hash of the key modulo hash table size */
118205   Fts3HashElem *elem;       /* Used to loop thru the element list */
118206   Fts3HashElem *new_elem;   /* New element added to the pH */
118207   int (*xHash)(const void*,int);  /* The hash function */
118208 
118209   assert( pH!=0 );
118210   xHash = ftsHashFunction(pH->keyClass);
118211   assert( xHash!=0 );
118212   hraw = (*xHash)(pKey, nKey);
118213   assert( (pH->htsize & (pH->htsize-1))==0 );
118214   h = hraw & (pH->htsize-1);
118215   elem = fts3FindElementByHash(pH,pKey,nKey,h);
118216   if( elem ){
118217     void *old_data = elem->data;
118218     if( data==0 ){
118219       fts3RemoveElementByHash(pH,elem,h);
118220     }else{
118221       elem->data = data;
118222     }
118223     return old_data;
118224   }
118225   if( data==0 ) return 0;
118226   if( (pH->htsize==0 && fts3Rehash(pH,8))
118227    || (pH->count>=pH->htsize && fts3Rehash(pH, pH->htsize*2))
118228   ){
118229     pH->count = 0;
118230     return data;
118231   }
118232   assert( pH->htsize>0 );
118233   new_elem = (Fts3HashElem*)fts3HashMalloc( sizeof(Fts3HashElem) );
118234   if( new_elem==0 ) return data;
118235   if( pH->copyKey && pKey!=0 ){
118236     new_elem->pKey = fts3HashMalloc( nKey );
118237     if( new_elem->pKey==0 ){
118238       fts3HashFree(new_elem);
118239       return data;
118240     }
118241     memcpy((void*)new_elem->pKey, pKey, nKey);
118242   }else{
118243     new_elem->pKey = (void*)pKey;
118244   }
118245   new_elem->nKey = nKey;
118246   pH->count++;
118247   assert( pH->htsize>0 );
118248   assert( (pH->htsize & (pH->htsize-1))==0 );
118249   h = hraw & (pH->htsize-1);
118250   fts3HashInsertElement(pH, &pH->ht[h], new_elem);
118251   new_elem->data = data;
118252   return 0;
118253 }
118254 
118255 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
118256 
118257 /************** End of fts3_hash.c *******************************************/
118258 /************** Begin file fts3_porter.c *************************************/
118259 /*
118260 ** 2006 September 30
118261 **
118262 ** The author disclaims copyright to this source code.  In place of
118263 ** a legal notice, here is a blessing:
118264 **
118265 **    May you do good and not evil.
118266 **    May you find forgiveness for yourself and forgive others.
118267 **    May you share freely, never taking more than you give.
118268 **
118269 *************************************************************************
118270 ** Implementation of the full-text-search tokenizer that implements
118271 ** a Porter stemmer.
118272 */
118273 
118274 /*
118275 ** The code in this file is only compiled if:
118276 **
118277 **     * The FTS3 module is being built as an extension
118278 **       (in which case SQLITE_CORE is not defined), or
118279 **
118280 **     * The FTS3 module is being built into the core of
118281 **       SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
118282 */
118283 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
118284 
118285 
118286 
118287 /*
118288 ** Class derived from sqlite3_tokenizer
118289 */
118290 typedef struct porter_tokenizer {
118291   sqlite3_tokenizer base;      /* Base class */
118292 } porter_tokenizer;
118293 
118294 /*
118295 ** Class derived from sqlit3_tokenizer_cursor
118296 */
118297 typedef struct porter_tokenizer_cursor {
118298   sqlite3_tokenizer_cursor base;
118299   const char *zInput;          /* input we are tokenizing */
118300   int nInput;                  /* size of the input */
118301   int iOffset;                 /* current position in zInput */
118302   int iToken;                  /* index of next token to be returned */
118303   char *zToken;                /* storage for current token */
118304   int nAllocated;              /* space allocated to zToken buffer */
118305 } porter_tokenizer_cursor;
118306 
118307 
118308 /*
118309 ** Create a new tokenizer instance.
118310 */
118311 static int porterCreate(
118312   int argc, const char * const *argv,
118313   sqlite3_tokenizer **ppTokenizer
118314 ){
118315   porter_tokenizer *t;
118316 
118317   UNUSED_PARAMETER(argc);
118318   UNUSED_PARAMETER(argv);
118319 
118320   t = (porter_tokenizer *) sqlite3_malloc(sizeof(*t));
118321   if( t==NULL ) return SQLITE_NOMEM;
118322   memset(t, 0, sizeof(*t));
118323   *ppTokenizer = &t->base;
118324   return SQLITE_OK;
118325 }
118326 
118327 /*
118328 ** Destroy a tokenizer
118329 */
118330 static int porterDestroy(sqlite3_tokenizer *pTokenizer){
118331   sqlite3_free(pTokenizer);
118332   return SQLITE_OK;
118333 }
118334 
118335 /*
118336 ** Prepare to begin tokenizing a particular string.  The input
118337 ** string to be tokenized is zInput[0..nInput-1].  A cursor
118338 ** used to incrementally tokenize this string is returned in
118339 ** *ppCursor.
118340 */
118341 static int porterOpen(
118342   sqlite3_tokenizer *pTokenizer,         /* The tokenizer */
118343   const char *zInput, int nInput,        /* String to be tokenized */
118344   sqlite3_tokenizer_cursor **ppCursor    /* OUT: Tokenization cursor */
118345 ){
118346   porter_tokenizer_cursor *c;
118347 
118348   UNUSED_PARAMETER(pTokenizer);
118349 
118350   c = (porter_tokenizer_cursor *) sqlite3_malloc(sizeof(*c));
118351   if( c==NULL ) return SQLITE_NOMEM;
118352 
118353   c->zInput = zInput;
118354   if( zInput==0 ){
118355     c->nInput = 0;
118356   }else if( nInput<0 ){
118357     c->nInput = (int)strlen(zInput);
118358   }else{
118359     c->nInput = nInput;
118360   }
118361   c->iOffset = 0;                 /* start tokenizing at the beginning */
118362   c->iToken = 0;
118363   c->zToken = NULL;               /* no space allocated, yet. */
118364   c->nAllocated = 0;
118365 
118366   *ppCursor = &c->base;
118367   return SQLITE_OK;
118368 }
118369 
118370 /*
118371 ** Close a tokenization cursor previously opened by a call to
118372 ** porterOpen() above.
118373 */
118374 static int porterClose(sqlite3_tokenizer_cursor *pCursor){
118375   porter_tokenizer_cursor *c = (porter_tokenizer_cursor *) pCursor;
118376   sqlite3_free(c->zToken);
118377   sqlite3_free(c);
118378   return SQLITE_OK;
118379 }
118380 /*
118381 ** Vowel or consonant
118382 */
118383 static const char cType[] = {
118384    0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0,
118385    1, 1, 1, 2, 1
118386 };
118387 
118388 /*
118389 ** isConsonant() and isVowel() determine if their first character in
118390 ** the string they point to is a consonant or a vowel, according
118391 ** to Porter ruls.
118392 **
118393 ** A consonate is any letter other than 'a', 'e', 'i', 'o', or 'u'.
118394 ** 'Y' is a consonant unless it follows another consonant,
118395 ** in which case it is a vowel.
118396 **
118397 ** In these routine, the letters are in reverse order.  So the 'y' rule
118398 ** is that 'y' is a consonant unless it is followed by another
118399 ** consonent.
118400 */
118401 static int isVowel(const char*);
118402 static int isConsonant(const char *z){
118403   int j;
118404   char x = *z;
118405   if( x==0 ) return 0;
118406   assert( x>='a' && x<='z' );
118407   j = cType[x-'a'];
118408   if( j<2 ) return j;
118409   return z[1]==0 || isVowel(z + 1);
118410 }
118411 static int isVowel(const char *z){
118412   int j;
118413   char x = *z;
118414   if( x==0 ) return 0;
118415   assert( x>='a' && x<='z' );
118416   j = cType[x-'a'];
118417   if( j<2 ) return 1-j;
118418   return isConsonant(z + 1);
118419 }
118420 
118421 /*
118422 ** Let any sequence of one or more vowels be represented by V and let
118423 ** C be sequence of one or more consonants.  Then every word can be
118424 ** represented as:
118425 **
118426 **           [C] (VC){m} [V]
118427 **
118428 ** In prose:  A word is an optional consonant followed by zero or
118429 ** vowel-consonant pairs followed by an optional vowel.  "m" is the
118430 ** number of vowel consonant pairs.  This routine computes the value
118431 ** of m for the first i bytes of a word.
118432 **
118433 ** Return true if the m-value for z is 1 or more.  In other words,
118434 ** return true if z contains at least one vowel that is followed
118435 ** by a consonant.
118436 **
118437 ** In this routine z[] is in reverse order.  So we are really looking
118438 ** for an instance of of a consonant followed by a vowel.
118439 */
118440 static int m_gt_0(const char *z){
118441   while( isVowel(z) ){ z++; }
118442   if( *z==0 ) return 0;
118443   while( isConsonant(z) ){ z++; }
118444   return *z!=0;
118445 }
118446 
118447 /* Like mgt0 above except we are looking for a value of m which is
118448 ** exactly 1
118449 */
118450 static int m_eq_1(const char *z){
118451   while( isVowel(z) ){ z++; }
118452   if( *z==0 ) return 0;
118453   while( isConsonant(z) ){ z++; }
118454   if( *z==0 ) return 0;
118455   while( isVowel(z) ){ z++; }
118456   if( *z==0 ) return 1;
118457   while( isConsonant(z) ){ z++; }
118458   return *z==0;
118459 }
118460 
118461 /* Like mgt0 above except we are looking for a value of m>1 instead
118462 ** or m>0
118463 */
118464 static int m_gt_1(const char *z){
118465   while( isVowel(z) ){ z++; }
118466   if( *z==0 ) return 0;
118467   while( isConsonant(z) ){ z++; }
118468   if( *z==0 ) return 0;
118469   while( isVowel(z) ){ z++; }
118470   if( *z==0 ) return 0;
118471   while( isConsonant(z) ){ z++; }
118472   return *z!=0;
118473 }
118474 
118475 /*
118476 ** Return TRUE if there is a vowel anywhere within z[0..n-1]
118477 */
118478 static int hasVowel(const char *z){
118479   while( isConsonant(z) ){ z++; }
118480   return *z!=0;
118481 }
118482 
118483 /*
118484 ** Return TRUE if the word ends in a double consonant.
118485 **
118486 ** The text is reversed here. So we are really looking at
118487 ** the first two characters of z[].
118488 */
118489 static int doubleConsonant(const char *z){
118490   return isConsonant(z) && z[0]==z[1];
118491 }
118492 
118493 /*
118494 ** Return TRUE if the word ends with three letters which
118495 ** are consonant-vowel-consonent and where the final consonant
118496 ** is not 'w', 'x', or 'y'.
118497 **
118498 ** The word is reversed here.  So we are really checking the
118499 ** first three letters and the first one cannot be in [wxy].
118500 */
118501 static int star_oh(const char *z){
118502   return
118503     isConsonant(z) &&
118504     z[0]!='w' && z[0]!='x' && z[0]!='y' &&
118505     isVowel(z+1) &&
118506     isConsonant(z+2);
118507 }
118508 
118509 /*
118510 ** If the word ends with zFrom and xCond() is true for the stem
118511 ** of the word that preceeds the zFrom ending, then change the
118512 ** ending to zTo.
118513 **
118514 ** The input word *pz and zFrom are both in reverse order.  zTo
118515 ** is in normal order.
118516 **
118517 ** Return TRUE if zFrom matches.  Return FALSE if zFrom does not
118518 ** match.  Not that TRUE is returned even if xCond() fails and
118519 ** no substitution occurs.
118520 */
118521 static int stem(
118522   char **pz,             /* The word being stemmed (Reversed) */
118523   const char *zFrom,     /* If the ending matches this... (Reversed) */
118524   const char *zTo,       /* ... change the ending to this (not reversed) */
118525   int (*xCond)(const char*)   /* Condition that must be true */
118526 ){
118527   char *z = *pz;
118528   while( *zFrom && *zFrom==*z ){ z++; zFrom++; }
118529   if( *zFrom!=0 ) return 0;
118530   if( xCond && !xCond(z) ) return 1;
118531   while( *zTo ){
118532     *(--z) = *(zTo++);
118533   }
118534   *pz = z;
118535   return 1;
118536 }
118537 
118538 /*
118539 ** This is the fallback stemmer used when the porter stemmer is
118540 ** inappropriate.  The input word is copied into the output with
118541 ** US-ASCII case folding.  If the input word is too long (more
118542 ** than 20 bytes if it contains no digits or more than 6 bytes if
118543 ** it contains digits) then word is truncated to 20 or 6 bytes
118544 ** by taking 10 or 3 bytes from the beginning and end.
118545 */
118546 static void copy_stemmer(const char *zIn, int nIn, char *zOut, int *pnOut){
118547   int i, mx, j;
118548   int hasDigit = 0;
118549   for(i=0; i<nIn; i++){
118550     char c = zIn[i];
118551     if( c>='A' && c<='Z' ){
118552       zOut[i] = c - 'A' + 'a';
118553     }else{
118554       if( c>='0' && c<='9' ) hasDigit = 1;
118555       zOut[i] = c;
118556     }
118557   }
118558   mx = hasDigit ? 3 : 10;
118559   if( nIn>mx*2 ){
118560     for(j=mx, i=nIn-mx; i<nIn; i++, j++){
118561       zOut[j] = zOut[i];
118562     }
118563     i = j;
118564   }
118565   zOut[i] = 0;
118566   *pnOut = i;
118567 }
118568 
118569 
118570 /*
118571 ** Stem the input word zIn[0..nIn-1].  Store the output in zOut.
118572 ** zOut is at least big enough to hold nIn bytes.  Write the actual
118573 ** size of the output word (exclusive of the '\0' terminator) into *pnOut.
118574 **
118575 ** Any upper-case characters in the US-ASCII character set ([A-Z])
118576 ** are converted to lower case.  Upper-case UTF characters are
118577 ** unchanged.
118578 **
118579 ** Words that are longer than about 20 bytes are stemmed by retaining
118580 ** a few bytes from the beginning and the end of the word.  If the
118581 ** word contains digits, 3 bytes are taken from the beginning and
118582 ** 3 bytes from the end.  For long words without digits, 10 bytes
118583 ** are taken from each end.  US-ASCII case folding still applies.
118584 **
118585 ** If the input word contains not digits but does characters not
118586 ** in [a-zA-Z] then no stemming is attempted and this routine just
118587 ** copies the input into the input into the output with US-ASCII
118588 ** case folding.
118589 **
118590 ** Stemming never increases the length of the word.  So there is
118591 ** no chance of overflowing the zOut buffer.
118592 */
118593 static void porter_stemmer(const char *zIn, int nIn, char *zOut, int *pnOut){
118594   int i, j;
118595   char zReverse[28];
118596   char *z, *z2;
118597   if( nIn<3 || nIn>=(int)sizeof(zReverse)-7 ){
118598     /* The word is too big or too small for the porter stemmer.
118599     ** Fallback to the copy stemmer */
118600     copy_stemmer(zIn, nIn, zOut, pnOut);
118601     return;
118602   }
118603   for(i=0, j=sizeof(zReverse)-6; i<nIn; i++, j--){
118604     char c = zIn[i];
118605     if( c>='A' && c<='Z' ){
118606       zReverse[j] = c + 'a' - 'A';
118607     }else if( c>='a' && c<='z' ){
118608       zReverse[j] = c;
118609     }else{
118610       /* The use of a character not in [a-zA-Z] means that we fallback
118611       ** to the copy stemmer */
118612       copy_stemmer(zIn, nIn, zOut, pnOut);
118613       return;
118614     }
118615   }
118616   memset(&zReverse[sizeof(zReverse)-5], 0, 5);
118617   z = &zReverse[j+1];
118618 
118619 
118620   /* Step 1a */
118621   if( z[0]=='s' ){
118622     if(
118623      !stem(&z, "sess", "ss", 0) &&
118624      !stem(&z, "sei", "i", 0)  &&
118625      !stem(&z, "ss", "ss", 0)
118626     ){
118627       z++;
118628     }
118629   }
118630 
118631   /* Step 1b */
118632   z2 = z;
118633   if( stem(&z, "dee", "ee", m_gt_0) ){
118634     /* Do nothing.  The work was all in the test */
118635   }else if(
118636      (stem(&z, "gni", "", hasVowel) || stem(&z, "de", "", hasVowel))
118637       && z!=z2
118638   ){
118639      if( stem(&z, "ta", "ate", 0) ||
118640          stem(&z, "lb", "ble", 0) ||
118641          stem(&z, "zi", "ize", 0) ){
118642        /* Do nothing.  The work was all in the test */
118643      }else if( doubleConsonant(z) && (*z!='l' && *z!='s' && *z!='z') ){
118644        z++;
118645      }else if( m_eq_1(z) && star_oh(z) ){
118646        *(--z) = 'e';
118647      }
118648   }
118649 
118650   /* Step 1c */
118651   if( z[0]=='y' && hasVowel(z+1) ){
118652     z[0] = 'i';
118653   }
118654 
118655   /* Step 2 */
118656   switch( z[1] ){
118657    case 'a':
118658      stem(&z, "lanoita", "ate", m_gt_0) ||
118659      stem(&z, "lanoit", "tion", m_gt_0);
118660      break;
118661    case 'c':
118662      stem(&z, "icne", "ence", m_gt_0) ||
118663      stem(&z, "icna", "ance", m_gt_0);
118664      break;
118665    case 'e':
118666      stem(&z, "rezi", "ize", m_gt_0);
118667      break;
118668    case 'g':
118669      stem(&z, "igol", "log", m_gt_0);
118670      break;
118671    case 'l':
118672      stem(&z, "ilb", "ble", m_gt_0) ||
118673      stem(&z, "illa", "al", m_gt_0) ||
118674      stem(&z, "iltne", "ent", m_gt_0) ||
118675      stem(&z, "ile", "e", m_gt_0) ||
118676      stem(&z, "ilsuo", "ous", m_gt_0);
118677      break;
118678    case 'o':
118679      stem(&z, "noitazi", "ize", m_gt_0) ||
118680      stem(&z, "noita", "ate", m_gt_0) ||
118681      stem(&z, "rota", "ate", m_gt_0);
118682      break;
118683    case 's':
118684      stem(&z, "msila", "al", m_gt_0) ||
118685      stem(&z, "ssenevi", "ive", m_gt_0) ||
118686      stem(&z, "ssenluf", "ful", m_gt_0) ||
118687      stem(&z, "ssensuo", "ous", m_gt_0);
118688      break;
118689    case 't':
118690      stem(&z, "itila", "al", m_gt_0) ||
118691      stem(&z, "itivi", "ive", m_gt_0) ||
118692      stem(&z, "itilib", "ble", m_gt_0);
118693      break;
118694   }
118695 
118696   /* Step 3 */
118697   switch( z[0] ){
118698    case 'e':
118699      stem(&z, "etaci", "ic", m_gt_0) ||
118700      stem(&z, "evita", "", m_gt_0)   ||
118701      stem(&z, "ezila", "al", m_gt_0);
118702      break;
118703    case 'i':
118704      stem(&z, "itici", "ic", m_gt_0);
118705      break;
118706    case 'l':
118707      stem(&z, "laci", "ic", m_gt_0) ||
118708      stem(&z, "luf", "", m_gt_0);
118709      break;
118710    case 's':
118711      stem(&z, "ssen", "", m_gt_0);
118712      break;
118713   }
118714 
118715   /* Step 4 */
118716   switch( z[1] ){
118717    case 'a':
118718      if( z[0]=='l' && m_gt_1(z+2) ){
118719        z += 2;
118720      }
118721      break;
118722    case 'c':
118723      if( z[0]=='e' && z[2]=='n' && (z[3]=='a' || z[3]=='e')  && m_gt_1(z+4)  ){
118724        z += 4;
118725      }
118726      break;
118727    case 'e':
118728      if( z[0]=='r' && m_gt_1(z+2) ){
118729        z += 2;
118730      }
118731      break;
118732    case 'i':
118733      if( z[0]=='c' && m_gt_1(z+2) ){
118734        z += 2;
118735      }
118736      break;
118737    case 'l':
118738      if( z[0]=='e' && z[2]=='b' && (z[3]=='a' || z[3]=='i') && m_gt_1(z+4) ){
118739        z += 4;
118740      }
118741      break;
118742    case 'n':
118743      if( z[0]=='t' ){
118744        if( z[2]=='a' ){
118745          if( m_gt_1(z+3) ){
118746            z += 3;
118747          }
118748        }else if( z[2]=='e' ){
118749          stem(&z, "tneme", "", m_gt_1) ||
118750          stem(&z, "tnem", "", m_gt_1) ||
118751          stem(&z, "tne", "", m_gt_1);
118752        }
118753      }
118754      break;
118755    case 'o':
118756      if( z[0]=='u' ){
118757        if( m_gt_1(z+2) ){
118758          z += 2;
118759        }
118760      }else if( z[3]=='s' || z[3]=='t' ){
118761        stem(&z, "noi", "", m_gt_1);
118762      }
118763      break;
118764    case 's':
118765      if( z[0]=='m' && z[2]=='i' && m_gt_1(z+3) ){
118766        z += 3;
118767      }
118768      break;
118769    case 't':
118770      stem(&z, "eta", "", m_gt_1) ||
118771      stem(&z, "iti", "", m_gt_1);
118772      break;
118773    case 'u':
118774      if( z[0]=='s' && z[2]=='o' && m_gt_1(z+3) ){
118775        z += 3;
118776      }
118777      break;
118778    case 'v':
118779    case 'z':
118780      if( z[0]=='e' && z[2]=='i' && m_gt_1(z+3) ){
118781        z += 3;
118782      }
118783      break;
118784   }
118785 
118786   /* Step 5a */
118787   if( z[0]=='e' ){
118788     if( m_gt_1(z+1) ){
118789       z++;
118790     }else if( m_eq_1(z+1) && !star_oh(z+1) ){
118791       z++;
118792     }
118793   }
118794 
118795   /* Step 5b */
118796   if( m_gt_1(z) && z[0]=='l' && z[1]=='l' ){
118797     z++;
118798   }
118799 
118800   /* z[] is now the stemmed word in reverse order.  Flip it back
118801   ** around into forward order and return.
118802   */
118803   *pnOut = i = (int)strlen(z);
118804   zOut[i] = 0;
118805   while( *z ){
118806     zOut[--i] = *(z++);
118807   }
118808 }
118809 
118810 /*
118811 ** Characters that can be part of a token.  We assume any character
118812 ** whose value is greater than 0x80 (any UTF character) can be
118813 ** part of a token.  In other words, delimiters all must have
118814 ** values of 0x7f or lower.
118815 */
118816 static const char porterIdChar[] = {
118817 /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
118818     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,  /* 3x */
118819     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* 4x */
118820     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1,  /* 5x */
118821     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* 6x */
118822     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,  /* 7x */
118823 };
118824 #define isDelim(C) (((ch=C)&0x80)==0 && (ch<0x30 || !porterIdChar[ch-0x30]))
118825 
118826 /*
118827 ** Extract the next token from a tokenization cursor.  The cursor must
118828 ** have been opened by a prior call to porterOpen().
118829 */
118830 static int porterNext(
118831   sqlite3_tokenizer_cursor *pCursor,  /* Cursor returned by porterOpen */
118832   const char **pzToken,               /* OUT: *pzToken is the token text */
118833   int *pnBytes,                       /* OUT: Number of bytes in token */
118834   int *piStartOffset,                 /* OUT: Starting offset of token */
118835   int *piEndOffset,                   /* OUT: Ending offset of token */
118836   int *piPosition                     /* OUT: Position integer of token */
118837 ){
118838   porter_tokenizer_cursor *c = (porter_tokenizer_cursor *) pCursor;
118839   const char *z = c->zInput;
118840 
118841   while( c->iOffset<c->nInput ){
118842     int iStartOffset, ch;
118843 
118844     /* Scan past delimiter characters */
118845     while( c->iOffset<c->nInput && isDelim(z[c->iOffset]) ){
118846       c->iOffset++;
118847     }
118848 
118849     /* Count non-delimiter characters. */
118850     iStartOffset = c->iOffset;
118851     while( c->iOffset<c->nInput && !isDelim(z[c->iOffset]) ){
118852       c->iOffset++;
118853     }
118854 
118855     if( c->iOffset>iStartOffset ){
118856       int n = c->iOffset-iStartOffset;
118857       if( n>c->nAllocated ){
118858         char *pNew;
118859         c->nAllocated = n+20;
118860         pNew = sqlite3_realloc(c->zToken, c->nAllocated);
118861         if( !pNew ) return SQLITE_NOMEM;
118862         c->zToken = pNew;
118863       }
118864       porter_stemmer(&z[iStartOffset], n, c->zToken, pnBytes);
118865       *pzToken = c->zToken;
118866       *piStartOffset = iStartOffset;
118867       *piEndOffset = c->iOffset;
118868       *piPosition = c->iToken++;
118869       return SQLITE_OK;
118870     }
118871   }
118872   return SQLITE_DONE;
118873 }
118874 
118875 /*
118876 ** The set of routines that implement the porter-stemmer tokenizer
118877 */
118878 static const sqlite3_tokenizer_module porterTokenizerModule = {
118879   0,
118880   porterCreate,
118881   porterDestroy,
118882   porterOpen,
118883   porterClose,
118884   porterNext,
118885 };
118886 
118887 /*
118888 ** Allocate a new porter tokenizer.  Return a pointer to the new
118889 ** tokenizer in *ppModule
118890 */
118891 SQLITE_PRIVATE void sqlite3Fts3PorterTokenizerModule(
118892   sqlite3_tokenizer_module const**ppModule
118893 ){
118894   *ppModule = &porterTokenizerModule;
118895 }
118896 
118897 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
118898 
118899 /************** End of fts3_porter.c *****************************************/
118900 /************** Begin file fts3_tokenizer.c **********************************/
118901 /*
118902 ** 2007 June 22
118903 **
118904 ** The author disclaims copyright to this source code.  In place of
118905 ** a legal notice, here is a blessing:
118906 **
118907 **    May you do good and not evil.
118908 **    May you find forgiveness for yourself and forgive others.
118909 **    May you share freely, never taking more than you give.
118910 **
118911 ******************************************************************************
118912 **
118913 ** This is part of an SQLite module implementing full-text search.
118914 ** This particular file implements the generic tokenizer interface.
118915 */
118916 
118917 /*
118918 ** The code in this file is only compiled if:
118919 **
118920 **     * The FTS3 module is being built as an extension
118921 **       (in which case SQLITE_CORE is not defined), or
118922 **
118923 **     * The FTS3 module is being built into the core of
118924 **       SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
118925 */
118926 #ifndef SQLITE_CORE
118927   SQLITE_EXTENSION_INIT1
118928 #endif
118929 
118930 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
118931 
118932 
118933 /*
118934 ** Implementation of the SQL scalar function for accessing the underlying
118935 ** hash table. This function may be called as follows:
118936 **
118937 **   SELECT <function-name>(<key-name>);
118938 **   SELECT <function-name>(<key-name>, <pointer>);
118939 **
118940 ** where <function-name> is the name passed as the second argument
118941 ** to the sqlite3Fts3InitHashTable() function (e.g. 'fts3_tokenizer').
118942 **
118943 ** If the <pointer> argument is specified, it must be a blob value
118944 ** containing a pointer to be stored as the hash data corresponding
118945 ** to the string <key-name>. If <pointer> is not specified, then
118946 ** the string <key-name> must already exist in the has table. Otherwise,
118947 ** an error is returned.
118948 **
118949 ** Whether or not the <pointer> argument is specified, the value returned
118950 ** is a blob containing the pointer stored as the hash data corresponding
118951 ** to string <key-name> (after the hash-table is updated, if applicable).
118952 */
118953 static void scalarFunc(
118954   sqlite3_context *context,
118955   int argc,
118956   sqlite3_value **argv
118957 ){
118958   Fts3Hash *pHash;
118959   void *pPtr = 0;
118960   const unsigned char *zName;
118961   int nName;
118962 
118963   assert( argc==1 || argc==2 );
118964 
118965   pHash = (Fts3Hash *)sqlite3_user_data(context);
118966 
118967   zName = sqlite3_value_text(argv[0]);
118968   nName = sqlite3_value_bytes(argv[0])+1;
118969 
118970   if( argc==2 ){
118971     void *pOld;
118972     int n = sqlite3_value_bytes(argv[1]);
118973     if( n!=sizeof(pPtr) ){
118974       sqlite3_result_error(context, "argument type mismatch", -1);
118975       return;
118976     }
118977     pPtr = *(void **)sqlite3_value_blob(argv[1]);
118978     pOld = sqlite3Fts3HashInsert(pHash, (void *)zName, nName, pPtr);
118979     if( pOld==pPtr ){
118980       sqlite3_result_error(context, "out of memory", -1);
118981       return;
118982     }
118983   }else{
118984     pPtr = sqlite3Fts3HashFind(pHash, zName, nName);
118985     if( !pPtr ){
118986       char *zErr = sqlite3_mprintf("unknown tokenizer: %s", zName);
118987       sqlite3_result_error(context, zErr, -1);
118988       sqlite3_free(zErr);
118989       return;
118990     }
118991   }
118992 
118993   sqlite3_result_blob(context, (void *)&pPtr, sizeof(pPtr), SQLITE_TRANSIENT);
118994 }
118995 
118996 SQLITE_PRIVATE int sqlite3Fts3IsIdChar(char c){
118997   static const char isFtsIdChar[] = {
118998       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /* 0x */
118999       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /* 1x */
119000       0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /* 2x */
119001       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,  /* 3x */
119002       0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* 4x */
119003       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1,  /* 5x */
119004       0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* 6x */
119005       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,  /* 7x */
119006   };
119007   return (c&0x80 || isFtsIdChar[(int)(c)]);
119008 }
119009 
119010 SQLITE_PRIVATE const char *sqlite3Fts3NextToken(const char *zStr, int *pn){
119011   const char *z1;
119012   const char *z2 = 0;
119013 
119014   /* Find the start of the next token. */
119015   z1 = zStr;
119016   while( z2==0 ){
119017     char c = *z1;
119018     switch( c ){
119019       case '\0': return 0;        /* No more tokens here */
119020       case '\'':
119021       case '"':
119022       case '`': {
119023         z2 = z1;
119024         while( *++z2 && (*z2!=c || *++z2==c) );
119025         break;
119026       }
119027       case '[':
119028         z2 = &z1[1];
119029         while( *z2 && z2[0]!=']' ) z2++;
119030         if( *z2 ) z2++;
119031         break;
119032 
119033       default:
119034         if( sqlite3Fts3IsIdChar(*z1) ){
119035           z2 = &z1[1];
119036           while( sqlite3Fts3IsIdChar(*z2) ) z2++;
119037         }else{
119038           z1++;
119039         }
119040     }
119041   }
119042 
119043   *pn = (int)(z2-z1);
119044   return z1;
119045 }
119046 
119047 SQLITE_PRIVATE int sqlite3Fts3InitTokenizer(
119048   Fts3Hash *pHash,                /* Tokenizer hash table */
119049   const char *zArg,               /* Tokenizer name */
119050   sqlite3_tokenizer **ppTok,      /* OUT: Tokenizer (if applicable) */
119051   char **pzErr                    /* OUT: Set to malloced error message */
119052 ){
119053   int rc;
119054   char *z = (char *)zArg;
119055   int n = 0;
119056   char *zCopy;
119057   char *zEnd;                     /* Pointer to nul-term of zCopy */
119058   sqlite3_tokenizer_module *m;
119059 
119060   zCopy = sqlite3_mprintf("%s", zArg);
119061   if( !zCopy ) return SQLITE_NOMEM;
119062   zEnd = &zCopy[strlen(zCopy)];
119063 
119064   z = (char *)sqlite3Fts3NextToken(zCopy, &n);
119065   z[n] = '\0';
119066   sqlite3Fts3Dequote(z);
119067 
119068   m = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash,z,(int)strlen(z)+1);
119069   if( !m ){
119070     *pzErr = sqlite3_mprintf("unknown tokenizer: %s", z);
119071     rc = SQLITE_ERROR;
119072   }else{
119073     char const **aArg = 0;
119074     int iArg = 0;
119075     z = &z[n+1];
119076     while( z<zEnd && (NULL!=(z = (char *)sqlite3Fts3NextToken(z, &n))) ){
119077       int nNew = sizeof(char *)*(iArg+1);
119078       char const **aNew = (const char **)sqlite3_realloc((void *)aArg, nNew);
119079       if( !aNew ){
119080         sqlite3_free(zCopy);
119081         sqlite3_free((void *)aArg);
119082         return SQLITE_NOMEM;
119083       }
119084       aArg = aNew;
119085       aArg[iArg++] = z;
119086       z[n] = '\0';
119087       sqlite3Fts3Dequote(z);
119088       z = &z[n+1];
119089     }
119090     rc = m->xCreate(iArg, aArg, ppTok);
119091     assert( rc!=SQLITE_OK || *ppTok );
119092     if( rc!=SQLITE_OK ){
119093       *pzErr = sqlite3_mprintf("unknown tokenizer");
119094     }else{
119095       (*ppTok)->pModule = m;
119096     }
119097     sqlite3_free((void *)aArg);
119098   }
119099 
119100   sqlite3_free(zCopy);
119101   return rc;
119102 }
119103 
119104 
119105 #ifdef SQLITE_TEST
119106 
119107 
119108 /*
119109 ** Implementation of a special SQL scalar function for testing tokenizers
119110 ** designed to be used in concert with the Tcl testing framework. This
119111 ** function must be called with two arguments:
119112 **
119113 **   SELECT <function-name>(<key-name>, <input-string>);
119114 **   SELECT <function-name>(<key-name>, <pointer>);
119115 **
119116 ** where <function-name> is the name passed as the second argument
119117 ** to the sqlite3Fts3InitHashTable() function (e.g. 'fts3_tokenizer')
119118 ** concatenated with the string '_test' (e.g. 'fts3_tokenizer_test').
119119 **
119120 ** The return value is a string that may be interpreted as a Tcl
119121 ** list. For each token in the <input-string>, three elements are
119122 ** added to the returned list. The first is the token position, the
119123 ** second is the token text (folded, stemmed, etc.) and the third is the
119124 ** substring of <input-string> associated with the token. For example,
119125 ** using the built-in "simple" tokenizer:
119126 **
119127 **   SELECT fts_tokenizer_test('simple', 'I don't see how');
119128 **
119129 ** will return the string:
119130 **
119131 **   "{0 i I 1 dont don't 2 see see 3 how how}"
119132 **
119133 */
119134 static void testFunc(
119135   sqlite3_context *context,
119136   int argc,
119137   sqlite3_value **argv
119138 ){
119139   Fts3Hash *pHash;
119140   sqlite3_tokenizer_module *p;
119141   sqlite3_tokenizer *pTokenizer = 0;
119142   sqlite3_tokenizer_cursor *pCsr = 0;
119143 
119144   const char *zErr = 0;
119145 
119146   const char *zName;
119147   int nName;
119148   const char *zInput;
119149   int nInput;
119150 
119151   const char *zArg = 0;
119152 
119153   const char *zToken;
119154   int nToken;
119155   int iStart;
119156   int iEnd;
119157   int iPos;
119158 
119159   Tcl_Obj *pRet;
119160 
119161   assert( argc==2 || argc==3 );
119162 
119163   nName = sqlite3_value_bytes(argv[0]);
119164   zName = (const char *)sqlite3_value_text(argv[0]);
119165   nInput = sqlite3_value_bytes(argv[argc-1]);
119166   zInput = (const char *)sqlite3_value_text(argv[argc-1]);
119167 
119168   if( argc==3 ){
119169     zArg = (const char *)sqlite3_value_text(argv[1]);
119170   }
119171 
119172   pHash = (Fts3Hash *)sqlite3_user_data(context);
119173   p = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash, zName, nName+1);
119174 
119175   if( !p ){
119176     char *zErr = sqlite3_mprintf("unknown tokenizer: %s", zName);
119177     sqlite3_result_error(context, zErr, -1);
119178     sqlite3_free(zErr);
119179     return;
119180   }
119181 
119182   pRet = Tcl_NewObj();
119183   Tcl_IncrRefCount(pRet);
119184 
119185   if( SQLITE_OK!=p->xCreate(zArg ? 1 : 0, &zArg, &pTokenizer) ){
119186     zErr = "error in xCreate()";
119187     goto finish;
119188   }
119189   pTokenizer->pModule = p;
119190   if( SQLITE_OK!=p->xOpen(pTokenizer, zInput, nInput, &pCsr) ){
119191     zErr = "error in xOpen()";
119192     goto finish;
119193   }
119194   pCsr->pTokenizer = pTokenizer;
119195 
119196   while( SQLITE_OK==p->xNext(pCsr, &zToken, &nToken, &iStart, &iEnd, &iPos) ){
119197     Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(iPos));
119198     Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zToken, nToken));
119199     zToken = &zInput[iStart];
119200     nToken = iEnd-iStart;
119201     Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zToken, nToken));
119202   }
119203 
119204   if( SQLITE_OK!=p->xClose(pCsr) ){
119205     zErr = "error in xClose()";
119206     goto finish;
119207   }
119208   if( SQLITE_OK!=p->xDestroy(pTokenizer) ){
119209     zErr = "error in xDestroy()";
119210     goto finish;
119211   }
119212 
119213 finish:
119214   if( zErr ){
119215     sqlite3_result_error(context, zErr, -1);
119216   }else{
119217     sqlite3_result_text(context, Tcl_GetString(pRet), -1, SQLITE_TRANSIENT);
119218   }
119219   Tcl_DecrRefCount(pRet);
119220 }
119221 
119222 static
119223 int registerTokenizer(
119224   sqlite3 *db,
119225   char *zName,
119226   const sqlite3_tokenizer_module *p
119227 ){
119228   int rc;
119229   sqlite3_stmt *pStmt;
119230   const char zSql[] = "SELECT fts3_tokenizer(?, ?)";
119231 
119232   rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
119233   if( rc!=SQLITE_OK ){
119234     return rc;
119235   }
119236 
119237   sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC);
119238   sqlite3_bind_blob(pStmt, 2, &p, sizeof(p), SQLITE_STATIC);
119239   sqlite3_step(pStmt);
119240 
119241   return sqlite3_finalize(pStmt);
119242 }
119243 
119244 static
119245 int queryTokenizer(
119246   sqlite3 *db,
119247   char *zName,
119248   const sqlite3_tokenizer_module **pp
119249 ){
119250   int rc;
119251   sqlite3_stmt *pStmt;
119252   const char zSql[] = "SELECT fts3_tokenizer(?)";
119253 
119254   *pp = 0;
119255   rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
119256   if( rc!=SQLITE_OK ){
119257     return rc;
119258   }
119259 
119260   sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC);
119261   if( SQLITE_ROW==sqlite3_step(pStmt) ){
119262     if( sqlite3_column_type(pStmt, 0)==SQLITE_BLOB ){
119263       memcpy((void *)pp, sqlite3_column_blob(pStmt, 0), sizeof(*pp));
119264     }
119265   }
119266 
119267   return sqlite3_finalize(pStmt);
119268 }
119269 
119270 SQLITE_PRIVATE void sqlite3Fts3SimpleTokenizerModule(sqlite3_tokenizer_module const**ppModule);
119271 
119272 /*
119273 ** Implementation of the scalar function fts3_tokenizer_internal_test().
119274 ** This function is used for testing only, it is not included in the
119275 ** build unless SQLITE_TEST is defined.
119276 **
119277 ** The purpose of this is to test that the fts3_tokenizer() function
119278 ** can be used as designed by the C-code in the queryTokenizer and
119279 ** registerTokenizer() functions above. These two functions are repeated
119280 ** in the README.tokenizer file as an example, so it is important to
119281 ** test them.
119282 **
119283 ** To run the tests, evaluate the fts3_tokenizer_internal_test() scalar
119284 ** function with no arguments. An assert() will fail if a problem is
119285 ** detected. i.e.:
119286 **
119287 **     SELECT fts3_tokenizer_internal_test();
119288 **
119289 */
119290 static void intTestFunc(
119291   sqlite3_context *context,
119292   int argc,
119293   sqlite3_value **argv
119294 ){
119295   int rc;
119296   const sqlite3_tokenizer_module *p1;
119297   const sqlite3_tokenizer_module *p2;
119298   sqlite3 *db = (sqlite3 *)sqlite3_user_data(context);
119299 
119300   UNUSED_PARAMETER(argc);
119301   UNUSED_PARAMETER(argv);
119302 
119303   /* Test the query function */
119304   sqlite3Fts3SimpleTokenizerModule(&p1);
119305   rc = queryTokenizer(db, "simple", &p2);
119306   assert( rc==SQLITE_OK );
119307   assert( p1==p2 );
119308   rc = queryTokenizer(db, "nosuchtokenizer", &p2);
119309   assert( rc==SQLITE_ERROR );
119310   assert( p2==0 );
119311   assert( 0==strcmp(sqlite3_errmsg(db), "unknown tokenizer: nosuchtokenizer") );
119312 
119313   /* Test the storage function */
119314   rc = registerTokenizer(db, "nosuchtokenizer", p1);
119315   assert( rc==SQLITE_OK );
119316   rc = queryTokenizer(db, "nosuchtokenizer", &p2);
119317   assert( rc==SQLITE_OK );
119318   assert( p2==p1 );
119319 
119320   sqlite3_result_text(context, "ok", -1, SQLITE_STATIC);
119321 }
119322 
119323 #endif
119324 
119325 /*
119326 ** Set up SQL objects in database db used to access the contents of
119327 ** the hash table pointed to by argument pHash. The hash table must
119328 ** been initialised to use string keys, and to take a private copy
119329 ** of the key when a value is inserted. i.e. by a call similar to:
119330 **
119331 **    sqlite3Fts3HashInit(pHash, FTS3_HASH_STRING, 1);
119332 **
119333 ** This function adds a scalar function (see header comment above
119334 ** scalarFunc() in this file for details) and, if ENABLE_TABLE is
119335 ** defined at compilation time, a temporary virtual table (see header
119336 ** comment above struct HashTableVtab) to the database schema. Both
119337 ** provide read/write access to the contents of *pHash.
119338 **
119339 ** The third argument to this function, zName, is used as the name
119340 ** of both the scalar and, if created, the virtual table.
119341 */
119342 SQLITE_PRIVATE int sqlite3Fts3InitHashTable(
119343   sqlite3 *db,
119344   Fts3Hash *pHash,
119345   const char *zName
119346 ){
119347   int rc = SQLITE_OK;
119348   void *p = (void *)pHash;
119349   const int any = SQLITE_ANY;
119350 
119351 #ifdef SQLITE_TEST
119352   char *zTest = 0;
119353   char *zTest2 = 0;
119354   void *pdb = (void *)db;
119355   zTest = sqlite3_mprintf("%s_test", zName);
119356   zTest2 = sqlite3_mprintf("%s_internal_test", zName);
119357   if( !zTest || !zTest2 ){
119358     rc = SQLITE_NOMEM;
119359   }
119360 #endif
119361 
119362   if( SQLITE_OK==rc ){
119363     rc = sqlite3_create_function(db, zName, 1, any, p, scalarFunc, 0, 0);
119364   }
119365   if( SQLITE_OK==rc ){
119366     rc = sqlite3_create_function(db, zName, 2, any, p, scalarFunc, 0, 0);
119367   }
119368 #ifdef SQLITE_TEST
119369   if( SQLITE_OK==rc ){
119370     rc = sqlite3_create_function(db, zTest, 2, any, p, testFunc, 0, 0);
119371   }
119372   if( SQLITE_OK==rc ){
119373     rc = sqlite3_create_function(db, zTest, 3, any, p, testFunc, 0, 0);
119374   }
119375   if( SQLITE_OK==rc ){
119376     rc = sqlite3_create_function(db, zTest2, 0, any, pdb, intTestFunc, 0, 0);
119377   }
119378 #endif
119379 
119380 #ifdef SQLITE_TEST
119381   sqlite3_free(zTest);
119382   sqlite3_free(zTest2);
119383 #endif
119384 
119385   return rc;
119386 }
119387 
119388 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
119389 
119390 /************** End of fts3_tokenizer.c **************************************/
119391 /************** Begin file fts3_tokenizer1.c *********************************/
119392 /*
119393 ** 2006 Oct 10
119394 **
119395 ** The author disclaims copyright to this source code.  In place of
119396 ** a legal notice, here is a blessing:
119397 **
119398 **    May you do good and not evil.
119399 **    May you find forgiveness for yourself and forgive others.
119400 **    May you share freely, never taking more than you give.
119401 **
119402 ******************************************************************************
119403 **
119404 ** Implementation of the "simple" full-text-search tokenizer.
119405 */
119406 
119407 /*
119408 ** The code in this file is only compiled if:
119409 **
119410 **     * The FTS3 module is being built as an extension
119411 **       (in which case SQLITE_CORE is not defined), or
119412 **
119413 **     * The FTS3 module is being built into the core of
119414 **       SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
119415 */
119416 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
119417 
119418 
119419 
119420 typedef struct simple_tokenizer {
119421   sqlite3_tokenizer base;
119422   char delim[128];             /* flag ASCII delimiters */
119423 } simple_tokenizer;
119424 
119425 typedef struct simple_tokenizer_cursor {
119426   sqlite3_tokenizer_cursor base;
119427   const char *pInput;          /* input we are tokenizing */
119428   int nBytes;                  /* size of the input */
119429   int iOffset;                 /* current position in pInput */
119430   int iToken;                  /* index of next token to be returned */
119431   char *pToken;                /* storage for current token */
119432   int nTokenAllocated;         /* space allocated to zToken buffer */
119433 } simple_tokenizer_cursor;
119434 
119435 
119436 static int simpleDelim(simple_tokenizer *t, unsigned char c){
119437   return c<0x80 && t->delim[c];
119438 }
119439 static int fts3_isalnum(int x){
119440   return (x>='0' && x<='9') || (x>='A' && x<='Z') || (x>='a' && x<='z');
119441 }
119442 
119443 /*
119444 ** Create a new tokenizer instance.
119445 */
119446 static int simpleCreate(
119447   int argc, const char * const *argv,
119448   sqlite3_tokenizer **ppTokenizer
119449 ){
119450   simple_tokenizer *t;
119451 
119452   t = (simple_tokenizer *) sqlite3_malloc(sizeof(*t));
119453   if( t==NULL ) return SQLITE_NOMEM;
119454   memset(t, 0, sizeof(*t));
119455 
119456   /* TODO(shess) Delimiters need to remain the same from run to run,
119457   ** else we need to reindex.  One solution would be a meta-table to
119458   ** track such information in the database, then we'd only want this
119459   ** information on the initial create.
119460   */
119461   if( argc>1 ){
119462     int i, n = (int)strlen(argv[1]);
119463     for(i=0; i<n; i++){
119464       unsigned char ch = argv[1][i];
119465       /* We explicitly don't support UTF-8 delimiters for now. */
119466       if( ch>=0x80 ){
119467         sqlite3_free(t);
119468         return SQLITE_ERROR;
119469       }
119470       t->delim[ch] = 1;
119471     }
119472   } else {
119473     /* Mark non-alphanumeric ASCII characters as delimiters */
119474     int i;
119475     for(i=1; i<0x80; i++){
119476       t->delim[i] = !fts3_isalnum(i) ? -1 : 0;
119477     }
119478   }
119479 
119480   *ppTokenizer = &t->base;
119481   return SQLITE_OK;
119482 }
119483 
119484 /*
119485 ** Destroy a tokenizer
119486 */
119487 static int simpleDestroy(sqlite3_tokenizer *pTokenizer){
119488   sqlite3_free(pTokenizer);
119489   return SQLITE_OK;
119490 }
119491 
119492 /*
119493 ** Prepare to begin tokenizing a particular string.  The input
119494 ** string to be tokenized is pInput[0..nBytes-1].  A cursor
119495 ** used to incrementally tokenize this string is returned in
119496 ** *ppCursor.
119497 */
119498 static int simpleOpen(
119499   sqlite3_tokenizer *pTokenizer,         /* The tokenizer */
119500   const char *pInput, int nBytes,        /* String to be tokenized */
119501   sqlite3_tokenizer_cursor **ppCursor    /* OUT: Tokenization cursor */
119502 ){
119503   simple_tokenizer_cursor *c;
119504 
119505   UNUSED_PARAMETER(pTokenizer);
119506 
119507   c = (simple_tokenizer_cursor *) sqlite3_malloc(sizeof(*c));
119508   if( c==NULL ) return SQLITE_NOMEM;
119509 
119510   c->pInput = pInput;
119511   if( pInput==0 ){
119512     c->nBytes = 0;
119513   }else if( nBytes<0 ){
119514     c->nBytes = (int)strlen(pInput);
119515   }else{
119516     c->nBytes = nBytes;
119517   }
119518   c->iOffset = 0;                 /* start tokenizing at the beginning */
119519   c->iToken = 0;
119520   c->pToken = NULL;               /* no space allocated, yet. */
119521   c->nTokenAllocated = 0;
119522 
119523   *ppCursor = &c->base;
119524   return SQLITE_OK;
119525 }
119526 
119527 /*
119528 ** Close a tokenization cursor previously opened by a call to
119529 ** simpleOpen() above.
119530 */
119531 static int simpleClose(sqlite3_tokenizer_cursor *pCursor){
119532   simple_tokenizer_cursor *c = (simple_tokenizer_cursor *) pCursor;
119533   sqlite3_free(c->pToken);
119534   sqlite3_free(c);
119535   return SQLITE_OK;
119536 }
119537 
119538 /*
119539 ** Extract the next token from a tokenization cursor.  The cursor must
119540 ** have been opened by a prior call to simpleOpen().
119541 */
119542 static int simpleNext(
119543   sqlite3_tokenizer_cursor *pCursor,  /* Cursor returned by simpleOpen */
119544   const char **ppToken,               /* OUT: *ppToken is the token text */
119545   int *pnBytes,                       /* OUT: Number of bytes in token */
119546   int *piStartOffset,                 /* OUT: Starting offset of token */
119547   int *piEndOffset,                   /* OUT: Ending offset of token */
119548   int *piPosition                     /* OUT: Position integer of token */
119549 ){
119550   simple_tokenizer_cursor *c = (simple_tokenizer_cursor *) pCursor;
119551   simple_tokenizer *t = (simple_tokenizer *) pCursor->pTokenizer;
119552   unsigned char *p = (unsigned char *)c->pInput;
119553 
119554   while( c->iOffset<c->nBytes ){
119555     int iStartOffset;
119556 
119557     /* Scan past delimiter characters */
119558     while( c->iOffset<c->nBytes && simpleDelim(t, p[c->iOffset]) ){
119559       c->iOffset++;
119560     }
119561 
119562     /* Count non-delimiter characters. */
119563     iStartOffset = c->iOffset;
119564     while( c->iOffset<c->nBytes && !simpleDelim(t, p[c->iOffset]) ){
119565       c->iOffset++;
119566     }
119567 
119568     if( c->iOffset>iStartOffset ){
119569       int i, n = c->iOffset-iStartOffset;
119570       if( n>c->nTokenAllocated ){
119571         char *pNew;
119572         c->nTokenAllocated = n+20;
119573         pNew = sqlite3_realloc(c->pToken, c->nTokenAllocated);
119574         if( !pNew ) return SQLITE_NOMEM;
119575         c->pToken = pNew;
119576       }
119577       for(i=0; i<n; i++){
119578         /* TODO(shess) This needs expansion to handle UTF-8
119579         ** case-insensitivity.
119580         */
119581         unsigned char ch = p[iStartOffset+i];
119582         c->pToken[i] = (char)((ch>='A' && ch<='Z') ? ch-'A'+'a' : ch);
119583       }
119584       *ppToken = c->pToken;
119585       *pnBytes = n;
119586       *piStartOffset = iStartOffset;
119587       *piEndOffset = c->iOffset;
119588       *piPosition = c->iToken++;
119589 
119590       return SQLITE_OK;
119591     }
119592   }
119593   return SQLITE_DONE;
119594 }
119595 
119596 /*
119597 ** The set of routines that implement the simple tokenizer
119598 */
119599 static const sqlite3_tokenizer_module simpleTokenizerModule = {
119600   0,
119601   simpleCreate,
119602   simpleDestroy,
119603   simpleOpen,
119604   simpleClose,
119605   simpleNext,
119606 };
119607 
119608 /*
119609 ** Allocate a new simple tokenizer.  Return a pointer to the new
119610 ** tokenizer in *ppModule
119611 */
119612 SQLITE_PRIVATE void sqlite3Fts3SimpleTokenizerModule(
119613   sqlite3_tokenizer_module const**ppModule
119614 ){
119615   *ppModule = &simpleTokenizerModule;
119616 }
119617 
119618 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
119619 
119620 /************** End of fts3_tokenizer1.c *************************************/
119621 /************** Begin file fts3_write.c **************************************/
119622 /*
119623 ** 2009 Oct 23
119624 **
119625 ** The author disclaims copyright to this source code.  In place of
119626 ** a legal notice, here is a blessing:
119627 **
119628 **    May you do good and not evil.
119629 **    May you find forgiveness for yourself and forgive others.
119630 **    May you share freely, never taking more than you give.
119631 **
119632 ******************************************************************************
119633 **
119634 ** This file is part of the SQLite FTS3 extension module. Specifically,
119635 ** this file contains code to insert, update and delete rows from FTS3
119636 ** tables. It also contains code to merge FTS3 b-tree segments. Some
119637 ** of the sub-routines used to merge segments are also used by the query
119638 ** code in fts3.c.
119639 */
119640 
119641 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
119642 
119643 
119644 /*
119645 ** When full-text index nodes are loaded from disk, the buffer that they
119646 ** are loaded into has the following number of bytes of padding at the end
119647 ** of it. i.e. if a full-text index node is 900 bytes in size, then a buffer
119648 ** of 920 bytes is allocated for it.
119649 **
119650 ** This means that if we have a pointer into a buffer containing node data,
119651 ** it is always safe to read up to two varints from it without risking an
119652 ** overread, even if the node data is corrupted.
119653 */
119654 #define FTS3_NODE_PADDING (FTS3_VARINT_MAX*2)
119655 
119656 /*
119657 ** Under certain circumstances, b-tree nodes (doclists) can be loaded into
119658 ** memory incrementally instead of all at once. This can be a big performance
119659 ** win (reduced IO and CPU) if SQLite stops calling the virtual table xNext()
119660 ** method before retrieving all query results (as may happen, for example,
119661 ** if a query has a LIMIT clause).
119662 **
119663 ** Incremental loading is used for b-tree nodes FTS3_NODE_CHUNK_THRESHOLD
119664 ** bytes and larger. Nodes are loaded in chunks of FTS3_NODE_CHUNKSIZE bytes.
119665 ** The code is written so that the hard lower-limit for each of these values
119666 ** is 1. Clearly such small values would be inefficient, but can be useful
119667 ** for testing purposes.
119668 **
119669 ** If this module is built with SQLITE_TEST defined, these constants may
119670 ** be overridden at runtime for testing purposes. File fts3_test.c contains
119671 ** a Tcl interface to read and write the values.
119672 */
119673 #ifdef SQLITE_TEST
119674 int test_fts3_node_chunksize = (4*1024);
119675 int test_fts3_node_chunk_threshold = (4*1024)*4;
119676 # define FTS3_NODE_CHUNKSIZE       test_fts3_node_chunksize
119677 # define FTS3_NODE_CHUNK_THRESHOLD test_fts3_node_chunk_threshold
119678 #else
119679 # define FTS3_NODE_CHUNKSIZE (4*1024)
119680 # define FTS3_NODE_CHUNK_THRESHOLD (FTS3_NODE_CHUNKSIZE*4)
119681 #endif
119682 
119683 typedef struct PendingList PendingList;
119684 typedef struct SegmentNode SegmentNode;
119685 typedef struct SegmentWriter SegmentWriter;
119686 
119687 /*
119688 ** An instance of the following data structure is used to build doclists
119689 ** incrementally. See function fts3PendingListAppend() for details.
119690 */
119691 struct PendingList {
119692   int nData;
119693   char *aData;
119694   int nSpace;
119695   sqlite3_int64 iLastDocid;
119696   sqlite3_int64 iLastCol;
119697   sqlite3_int64 iLastPos;
119698 };
119699 
119700 
119701 /*
119702 ** Each cursor has a (possibly empty) linked list of the following objects.
119703 */
119704 struct Fts3DeferredToken {
119705   Fts3PhraseToken *pToken;        /* Pointer to corresponding expr token */
119706   int iCol;                       /* Column token must occur in */
119707   Fts3DeferredToken *pNext;       /* Next in list of deferred tokens */
119708   PendingList *pList;             /* Doclist is assembled here */
119709 };
119710 
119711 /*
119712 ** An instance of this structure is used to iterate through the terms on
119713 ** a contiguous set of segment b-tree leaf nodes. Although the details of
119714 ** this structure are only manipulated by code in this file, opaque handles
119715 ** of type Fts3SegReader* are also used by code in fts3.c to iterate through
119716 ** terms when querying the full-text index. See functions:
119717 **
119718 **   sqlite3Fts3SegReaderNew()
119719 **   sqlite3Fts3SegReaderFree()
119720 **   sqlite3Fts3SegReaderIterate()
119721 **
119722 ** Methods used to manipulate Fts3SegReader structures:
119723 **
119724 **   fts3SegReaderNext()
119725 **   fts3SegReaderFirstDocid()
119726 **   fts3SegReaderNextDocid()
119727 */
119728 struct Fts3SegReader {
119729   int iIdx;                       /* Index within level, or 0x7FFFFFFF for PT */
119730 
119731   sqlite3_int64 iStartBlock;      /* Rowid of first leaf block to traverse */
119732   sqlite3_int64 iLeafEndBlock;    /* Rowid of final leaf block to traverse */
119733   sqlite3_int64 iEndBlock;        /* Rowid of final block in segment (or 0) */
119734   sqlite3_int64 iCurrentBlock;    /* Current leaf block (or 0) */
119735 
119736   char *aNode;                    /* Pointer to node data (or NULL) */
119737   int nNode;                      /* Size of buffer at aNode (or 0) */
119738   int nPopulate;                  /* If >0, bytes of buffer aNode[] loaded */
119739   sqlite3_blob *pBlob;            /* If not NULL, blob handle to read node */
119740 
119741   Fts3HashElem **ppNextElem;
119742 
119743   /* Variables set by fts3SegReaderNext(). These may be read directly
119744   ** by the caller. They are valid from the time SegmentReaderNew() returns
119745   ** until SegmentReaderNext() returns something other than SQLITE_OK
119746   ** (i.e. SQLITE_DONE).
119747   */
119748   int nTerm;                      /* Number of bytes in current term */
119749   char *zTerm;                    /* Pointer to current term */
119750   int nTermAlloc;                 /* Allocated size of zTerm buffer */
119751   char *aDoclist;                 /* Pointer to doclist of current entry */
119752   int nDoclist;                   /* Size of doclist in current entry */
119753 
119754   /* The following variables are used by fts3SegReaderNextDocid() to iterate
119755   ** through the current doclist (aDoclist/nDoclist).
119756   */
119757   char *pOffsetList;
119758   int nOffsetList;                /* For descending pending seg-readers only */
119759   sqlite3_int64 iDocid;
119760 };
119761 
119762 #define fts3SegReaderIsPending(p) ((p)->ppNextElem!=0)
119763 #define fts3SegReaderIsRootOnly(p) ((p)->aNode==(char *)&(p)[1])
119764 
119765 /*
119766 ** An instance of this structure is used to create a segment b-tree in the
119767 ** database. The internal details of this type are only accessed by the
119768 ** following functions:
119769 **
119770 **   fts3SegWriterAdd()
119771 **   fts3SegWriterFlush()
119772 **   fts3SegWriterFree()
119773 */
119774 struct SegmentWriter {
119775   SegmentNode *pTree;             /* Pointer to interior tree structure */
119776   sqlite3_int64 iFirst;           /* First slot in %_segments written */
119777   sqlite3_int64 iFree;            /* Next free slot in %_segments */
119778   char *zTerm;                    /* Pointer to previous term buffer */
119779   int nTerm;                      /* Number of bytes in zTerm */
119780   int nMalloc;                    /* Size of malloc'd buffer at zMalloc */
119781   char *zMalloc;                  /* Malloc'd space (possibly) used for zTerm */
119782   int nSize;                      /* Size of allocation at aData */
119783   int nData;                      /* Bytes of data in aData */
119784   char *aData;                    /* Pointer to block from malloc() */
119785 };
119786 
119787 /*
119788 ** Type SegmentNode is used by the following three functions to create
119789 ** the interior part of the segment b+-tree structures (everything except
119790 ** the leaf nodes). These functions and type are only ever used by code
119791 ** within the fts3SegWriterXXX() family of functions described above.
119792 **
119793 **   fts3NodeAddTerm()
119794 **   fts3NodeWrite()
119795 **   fts3NodeFree()
119796 **
119797 ** When a b+tree is written to the database (either as a result of a merge
119798 ** or the pending-terms table being flushed), leaves are written into the
119799 ** database file as soon as they are completely populated. The interior of
119800 ** the tree is assembled in memory and written out only once all leaves have
119801 ** been populated and stored. This is Ok, as the b+-tree fanout is usually
119802 ** very large, meaning that the interior of the tree consumes relatively
119803 ** little memory.
119804 */
119805 struct SegmentNode {
119806   SegmentNode *pParent;           /* Parent node (or NULL for root node) */
119807   SegmentNode *pRight;            /* Pointer to right-sibling */
119808   SegmentNode *pLeftmost;         /* Pointer to left-most node of this depth */
119809   int nEntry;                     /* Number of terms written to node so far */
119810   char *zTerm;                    /* Pointer to previous term buffer */
119811   int nTerm;                      /* Number of bytes in zTerm */
119812   int nMalloc;                    /* Size of malloc'd buffer at zMalloc */
119813   char *zMalloc;                  /* Malloc'd space (possibly) used for zTerm */
119814   int nData;                      /* Bytes of valid data so far */
119815   char *aData;                    /* Node data */
119816 };
119817 
119818 /*
119819 ** Valid values for the second argument to fts3SqlStmt().
119820 */
119821 #define SQL_DELETE_CONTENT             0
119822 #define SQL_IS_EMPTY                   1
119823 #define SQL_DELETE_ALL_CONTENT         2
119824 #define SQL_DELETE_ALL_SEGMENTS        3
119825 #define SQL_DELETE_ALL_SEGDIR          4
119826 #define SQL_DELETE_ALL_DOCSIZE         5
119827 #define SQL_DELETE_ALL_STAT            6
119828 #define SQL_SELECT_CONTENT_BY_ROWID    7
119829 #define SQL_NEXT_SEGMENT_INDEX         8
119830 #define SQL_INSERT_SEGMENTS            9
119831 #define SQL_NEXT_SEGMENTS_ID          10
119832 #define SQL_INSERT_SEGDIR             11
119833 #define SQL_SELECT_LEVEL              12
119834 #define SQL_SELECT_LEVEL_RANGE        13
119835 #define SQL_SELECT_LEVEL_COUNT        14
119836 #define SQL_SELECT_SEGDIR_MAX_LEVEL   15
119837 #define SQL_DELETE_SEGDIR_LEVEL       16
119838 #define SQL_DELETE_SEGMENTS_RANGE     17
119839 #define SQL_CONTENT_INSERT            18
119840 #define SQL_DELETE_DOCSIZE            19
119841 #define SQL_REPLACE_DOCSIZE           20
119842 #define SQL_SELECT_DOCSIZE            21
119843 #define SQL_SELECT_DOCTOTAL           22
119844 #define SQL_REPLACE_DOCTOTAL          23
119845 
119846 #define SQL_SELECT_ALL_PREFIX_LEVEL   24
119847 #define SQL_DELETE_ALL_TERMS_SEGDIR   25
119848 
119849 #define SQL_DELETE_SEGDIR_RANGE       26
119850 
119851 /*
119852 ** This function is used to obtain an SQLite prepared statement handle
119853 ** for the statement identified by the second argument. If successful,
119854 ** *pp is set to the requested statement handle and SQLITE_OK returned.
119855 ** Otherwise, an SQLite error code is returned and *pp is set to 0.
119856 **
119857 ** If argument apVal is not NULL, then it must point to an array with
119858 ** at least as many entries as the requested statement has bound
119859 ** parameters. The values are bound to the statements parameters before
119860 ** returning.
119861 */
119862 static int fts3SqlStmt(
119863   Fts3Table *p,                   /* Virtual table handle */
119864   int eStmt,                      /* One of the SQL_XXX constants above */
119865   sqlite3_stmt **pp,              /* OUT: Statement handle */
119866   sqlite3_value **apVal           /* Values to bind to statement */
119867 ){
119868   const char *azSql[] = {
119869 /* 0  */  "DELETE FROM %Q.'%q_content' WHERE rowid = ?",
119870 /* 1  */  "SELECT NOT EXISTS(SELECT docid FROM %Q.'%q_content' WHERE rowid!=?)",
119871 /* 2  */  "DELETE FROM %Q.'%q_content'",
119872 /* 3  */  "DELETE FROM %Q.'%q_segments'",
119873 /* 4  */  "DELETE FROM %Q.'%q_segdir'",
119874 /* 5  */  "DELETE FROM %Q.'%q_docsize'",
119875 /* 6  */  "DELETE FROM %Q.'%q_stat'",
119876 /* 7  */  "SELECT %s FROM %Q.'%q_content' AS x WHERE rowid=?",
119877 /* 8  */  "SELECT (SELECT max(idx) FROM %Q.'%q_segdir' WHERE level = ?) + 1",
119878 /* 9  */  "INSERT INTO %Q.'%q_segments'(blockid, block) VALUES(?, ?)",
119879 /* 10 */  "SELECT coalesce((SELECT max(blockid) FROM %Q.'%q_segments') + 1, 1)",
119880 /* 11 */  "INSERT INTO %Q.'%q_segdir' VALUES(?,?,?,?,?,?)",
119881 
119882           /* Return segments in order from oldest to newest.*/
119883 /* 12 */  "SELECT idx, start_block, leaves_end_block, end_block, root "
119884             "FROM %Q.'%q_segdir' WHERE level = ? ORDER BY idx ASC",
119885 /* 13 */  "SELECT idx, start_block, leaves_end_block, end_block, root "
119886             "FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?"
119887             "ORDER BY level DESC, idx ASC",
119888 
119889 /* 14 */  "SELECT count(*) FROM %Q.'%q_segdir' WHERE level = ?",
119890 /* 15 */  "SELECT max(level) FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?",
119891 
119892 /* 16 */  "DELETE FROM %Q.'%q_segdir' WHERE level = ?",
119893 /* 17 */  "DELETE FROM %Q.'%q_segments' WHERE blockid BETWEEN ? AND ?",
119894 /* 18 */  "INSERT INTO %Q.'%q_content' VALUES(%s)",
119895 /* 19 */  "DELETE FROM %Q.'%q_docsize' WHERE docid = ?",
119896 /* 20 */  "REPLACE INTO %Q.'%q_docsize' VALUES(?,?)",
119897 /* 21 */  "SELECT size FROM %Q.'%q_docsize' WHERE docid=?",
119898 /* 22 */  "SELECT value FROM %Q.'%q_stat' WHERE id=0",
119899 /* 23 */  "REPLACE INTO %Q.'%q_stat' VALUES(0,?)",
119900 /* 24 */  "",
119901 /* 25 */  "",
119902 
119903 /* 26 */ "DELETE FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?",
119904 
119905   };
119906   int rc = SQLITE_OK;
119907   sqlite3_stmt *pStmt;
119908 
119909   assert( SizeofArray(azSql)==SizeofArray(p->aStmt) );
119910   assert( eStmt<SizeofArray(azSql) && eStmt>=0 );
119911 
119912   pStmt = p->aStmt[eStmt];
119913   if( !pStmt ){
119914     char *zSql;
119915     if( eStmt==SQL_CONTENT_INSERT ){
119916       zSql = sqlite3_mprintf(azSql[eStmt], p->zDb, p->zName, p->zWriteExprlist);
119917     }else if( eStmt==SQL_SELECT_CONTENT_BY_ROWID ){
119918       zSql = sqlite3_mprintf(azSql[eStmt], p->zReadExprlist, p->zDb, p->zName);
119919     }else{
119920       zSql = sqlite3_mprintf(azSql[eStmt], p->zDb, p->zName);
119921     }
119922     if( !zSql ){
119923       rc = SQLITE_NOMEM;
119924     }else{
119925       rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, NULL);
119926       sqlite3_free(zSql);
119927       assert( rc==SQLITE_OK || pStmt==0 );
119928       p->aStmt[eStmt] = pStmt;
119929     }
119930   }
119931   if( apVal ){
119932     int i;
119933     int nParam = sqlite3_bind_parameter_count(pStmt);
119934     for(i=0; rc==SQLITE_OK && i<nParam; i++){
119935       rc = sqlite3_bind_value(pStmt, i+1, apVal[i]);
119936     }
119937   }
119938   *pp = pStmt;
119939   return rc;
119940 }
119941 
119942 static int fts3SelectDocsize(
119943   Fts3Table *pTab,                /* FTS3 table handle */
119944   int eStmt,                      /* Either SQL_SELECT_DOCSIZE or DOCTOTAL */
119945   sqlite3_int64 iDocid,           /* Docid to bind for SQL_SELECT_DOCSIZE */
119946   sqlite3_stmt **ppStmt           /* OUT: Statement handle */
119947 ){
119948   sqlite3_stmt *pStmt = 0;        /* Statement requested from fts3SqlStmt() */
119949   int rc;                         /* Return code */
119950 
119951   assert( eStmt==SQL_SELECT_DOCSIZE || eStmt==SQL_SELECT_DOCTOTAL );
119952 
119953   rc = fts3SqlStmt(pTab, eStmt, &pStmt, 0);
119954   if( rc==SQLITE_OK ){
119955     if( eStmt==SQL_SELECT_DOCSIZE ){
119956       sqlite3_bind_int64(pStmt, 1, iDocid);
119957     }
119958     rc = sqlite3_step(pStmt);
119959     if( rc!=SQLITE_ROW || sqlite3_column_type(pStmt, 0)!=SQLITE_BLOB ){
119960       rc = sqlite3_reset(pStmt);
119961       if( rc==SQLITE_OK ) rc = SQLITE_CORRUPT_VTAB;
119962       pStmt = 0;
119963     }else{
119964       rc = SQLITE_OK;
119965     }
119966   }
119967 
119968   *ppStmt = pStmt;
119969   return rc;
119970 }
119971 
119972 SQLITE_PRIVATE int sqlite3Fts3SelectDoctotal(
119973   Fts3Table *pTab,                /* Fts3 table handle */
119974   sqlite3_stmt **ppStmt           /* OUT: Statement handle */
119975 ){
119976   return fts3SelectDocsize(pTab, SQL_SELECT_DOCTOTAL, 0, ppStmt);
119977 }
119978 
119979 SQLITE_PRIVATE int sqlite3Fts3SelectDocsize(
119980   Fts3Table *pTab,                /* Fts3 table handle */
119981   sqlite3_int64 iDocid,           /* Docid to read size data for */
119982   sqlite3_stmt **ppStmt           /* OUT: Statement handle */
119983 ){
119984   return fts3SelectDocsize(pTab, SQL_SELECT_DOCSIZE, iDocid, ppStmt);
119985 }
119986 
119987 /*
119988 ** Similar to fts3SqlStmt(). Except, after binding the parameters in
119989 ** array apVal[] to the SQL statement identified by eStmt, the statement
119990 ** is executed.
119991 **
119992 ** Returns SQLITE_OK if the statement is successfully executed, or an
119993 ** SQLite error code otherwise.
119994 */
119995 static void fts3SqlExec(
119996   int *pRC,                /* Result code */
119997   Fts3Table *p,            /* The FTS3 table */
119998   int eStmt,               /* Index of statement to evaluate */
119999   sqlite3_value **apVal    /* Parameters to bind */
120000 ){
120001   sqlite3_stmt *pStmt;
120002   int rc;
120003   if( *pRC ) return;
120004   rc = fts3SqlStmt(p, eStmt, &pStmt, apVal);
120005   if( rc==SQLITE_OK ){
120006     sqlite3_step(pStmt);
120007     rc = sqlite3_reset(pStmt);
120008   }
120009   *pRC = rc;
120010 }
120011 
120012 
120013 /*
120014 ** This function ensures that the caller has obtained a shared-cache
120015 ** table-lock on the %_content table. This is required before reading
120016 ** data from the fts3 table. If this lock is not acquired first, then
120017 ** the caller may end up holding read-locks on the %_segments and %_segdir
120018 ** tables, but no read-lock on the %_content table. If this happens
120019 ** a second connection will be able to write to the fts3 table, but
120020 ** attempting to commit those writes might return SQLITE_LOCKED or
120021 ** SQLITE_LOCKED_SHAREDCACHE (because the commit attempts to obtain
120022 ** write-locks on the %_segments and %_segdir ** tables).
120023 **
120024 ** We try to avoid this because if FTS3 returns any error when committing
120025 ** a transaction, the whole transaction will be rolled back. And this is
120026 ** not what users expect when they get SQLITE_LOCKED_SHAREDCACHE. It can
120027 ** still happen if the user reads data directly from the %_segments or
120028 ** %_segdir tables instead of going through FTS3 though.
120029 */
120030 SQLITE_PRIVATE int sqlite3Fts3ReadLock(Fts3Table *p){
120031   int rc;                         /* Return code */
120032   sqlite3_stmt *pStmt;            /* Statement used to obtain lock */
120033 
120034   rc = fts3SqlStmt(p, SQL_SELECT_CONTENT_BY_ROWID, &pStmt, 0);
120035   if( rc==SQLITE_OK ){
120036     sqlite3_bind_null(pStmt, 1);
120037     sqlite3_step(pStmt);
120038     rc = sqlite3_reset(pStmt);
120039   }
120040   return rc;
120041 }
120042 
120043 /*
120044 ** Set *ppStmt to a statement handle that may be used to iterate through
120045 ** all rows in the %_segdir table, from oldest to newest. If successful,
120046 ** return SQLITE_OK. If an error occurs while preparing the statement,
120047 ** return an SQLite error code.
120048 **
120049 ** There is only ever one instance of this SQL statement compiled for
120050 ** each FTS3 table.
120051 **
120052 ** The statement returns the following columns from the %_segdir table:
120053 **
120054 **   0: idx
120055 **   1: start_block
120056 **   2: leaves_end_block
120057 **   3: end_block
120058 **   4: root
120059 */
120060 SQLITE_PRIVATE int sqlite3Fts3AllSegdirs(
120061   Fts3Table *p,                   /* FTS3 table */
120062   int iIndex,                     /* Index for p->aIndex[] */
120063   int iLevel,                     /* Level to select */
120064   sqlite3_stmt **ppStmt           /* OUT: Compiled statement */
120065 ){
120066   int rc;
120067   sqlite3_stmt *pStmt = 0;
120068 
120069   assert( iLevel==FTS3_SEGCURSOR_ALL || iLevel>=0 );
120070   assert( iLevel<FTS3_SEGDIR_MAXLEVEL );
120071   assert( iIndex>=0 && iIndex<p->nIndex );
120072 
120073   if( iLevel<0 ){
120074     /* "SELECT * FROM %_segdir WHERE level BETWEEN ? AND ? ORDER BY ..." */
120075     rc = fts3SqlStmt(p, SQL_SELECT_LEVEL_RANGE, &pStmt, 0);
120076     if( rc==SQLITE_OK ){
120077       sqlite3_bind_int(pStmt, 1, iIndex*FTS3_SEGDIR_MAXLEVEL);
120078       sqlite3_bind_int(pStmt, 2, (iIndex+1)*FTS3_SEGDIR_MAXLEVEL-1);
120079     }
120080   }else{
120081     /* "SELECT * FROM %_segdir WHERE level = ? ORDER BY ..." */
120082     rc = fts3SqlStmt(p, SQL_SELECT_LEVEL, &pStmt, 0);
120083     if( rc==SQLITE_OK ){
120084       sqlite3_bind_int(pStmt, 1, iLevel+iIndex*FTS3_SEGDIR_MAXLEVEL);
120085     }
120086   }
120087   *ppStmt = pStmt;
120088   return rc;
120089 }
120090 
120091 
120092 /*
120093 ** Append a single varint to a PendingList buffer. SQLITE_OK is returned
120094 ** if successful, or an SQLite error code otherwise.
120095 **
120096 ** This function also serves to allocate the PendingList structure itself.
120097 ** For example, to create a new PendingList structure containing two
120098 ** varints:
120099 **
120100 **   PendingList *p = 0;
120101 **   fts3PendingListAppendVarint(&p, 1);
120102 **   fts3PendingListAppendVarint(&p, 2);
120103 */
120104 static int fts3PendingListAppendVarint(
120105   PendingList **pp,               /* IN/OUT: Pointer to PendingList struct */
120106   sqlite3_int64 i                 /* Value to append to data */
120107 ){
120108   PendingList *p = *pp;
120109 
120110   /* Allocate or grow the PendingList as required. */
120111   if( !p ){
120112     p = sqlite3_malloc(sizeof(*p) + 100);
120113     if( !p ){
120114       return SQLITE_NOMEM;
120115     }
120116     p->nSpace = 100;
120117     p->aData = (char *)&p[1];
120118     p->nData = 0;
120119   }
120120   else if( p->nData+FTS3_VARINT_MAX+1>p->nSpace ){
120121     int nNew = p->nSpace * 2;
120122     p = sqlite3_realloc(p, sizeof(*p) + nNew);
120123     if( !p ){
120124       sqlite3_free(*pp);
120125       *pp = 0;
120126       return SQLITE_NOMEM;
120127     }
120128     p->nSpace = nNew;
120129     p->aData = (char *)&p[1];
120130   }
120131 
120132   /* Append the new serialized varint to the end of the list. */
120133   p->nData += sqlite3Fts3PutVarint(&p->aData[p->nData], i);
120134   p->aData[p->nData] = '\0';
120135   *pp = p;
120136   return SQLITE_OK;
120137 }
120138 
120139 /*
120140 ** Add a docid/column/position entry to a PendingList structure. Non-zero
120141 ** is returned if the structure is sqlite3_realloced as part of adding
120142 ** the entry. Otherwise, zero.
120143 **
120144 ** If an OOM error occurs, *pRc is set to SQLITE_NOMEM before returning.
120145 ** Zero is always returned in this case. Otherwise, if no OOM error occurs,
120146 ** it is set to SQLITE_OK.
120147 */
120148 static int fts3PendingListAppend(
120149   PendingList **pp,               /* IN/OUT: PendingList structure */
120150   sqlite3_int64 iDocid,           /* Docid for entry to add */
120151   sqlite3_int64 iCol,             /* Column for entry to add */
120152   sqlite3_int64 iPos,             /* Position of term for entry to add */
120153   int *pRc                        /* OUT: Return code */
120154 ){
120155   PendingList *p = *pp;
120156   int rc = SQLITE_OK;
120157 
120158   assert( !p || p->iLastDocid<=iDocid );
120159 
120160   if( !p || p->iLastDocid!=iDocid ){
120161     sqlite3_int64 iDelta = iDocid - (p ? p->iLastDocid : 0);
120162     if( p ){
120163       assert( p->nData<p->nSpace );
120164       assert( p->aData[p->nData]==0 );
120165       p->nData++;
120166     }
120167     if( SQLITE_OK!=(rc = fts3PendingListAppendVarint(&p, iDelta)) ){
120168       goto pendinglistappend_out;
120169     }
120170     p->iLastCol = -1;
120171     p->iLastPos = 0;
120172     p->iLastDocid = iDocid;
120173   }
120174   if( iCol>0 && p->iLastCol!=iCol ){
120175     if( SQLITE_OK!=(rc = fts3PendingListAppendVarint(&p, 1))
120176      || SQLITE_OK!=(rc = fts3PendingListAppendVarint(&p, iCol))
120177     ){
120178       goto pendinglistappend_out;
120179     }
120180     p->iLastCol = iCol;
120181     p->iLastPos = 0;
120182   }
120183   if( iCol>=0 ){
120184     assert( iPos>p->iLastPos || (iPos==0 && p->iLastPos==0) );
120185     rc = fts3PendingListAppendVarint(&p, 2+iPos-p->iLastPos);
120186     if( rc==SQLITE_OK ){
120187       p->iLastPos = iPos;
120188     }
120189   }
120190 
120191  pendinglistappend_out:
120192   *pRc = rc;
120193   if( p!=*pp ){
120194     *pp = p;
120195     return 1;
120196   }
120197   return 0;
120198 }
120199 
120200 /*
120201 ** Free a PendingList object allocated by fts3PendingListAppend().
120202 */
120203 static void fts3PendingListDelete(PendingList *pList){
120204   sqlite3_free(pList);
120205 }
120206 
120207 /*
120208 ** Add an entry to one of the pending-terms hash tables.
120209 */
120210 static int fts3PendingTermsAddOne(
120211   Fts3Table *p,
120212   int iCol,
120213   int iPos,
120214   Fts3Hash *pHash,                /* Pending terms hash table to add entry to */
120215   const char *zToken,
120216   int nToken
120217 ){
120218   PendingList *pList;
120219   int rc = SQLITE_OK;
120220 
120221   pList = (PendingList *)fts3HashFind(pHash, zToken, nToken);
120222   if( pList ){
120223     p->nPendingData -= (pList->nData + nToken + sizeof(Fts3HashElem));
120224   }
120225   if( fts3PendingListAppend(&pList, p->iPrevDocid, iCol, iPos, &rc) ){
120226     if( pList==fts3HashInsert(pHash, zToken, nToken, pList) ){
120227       /* Malloc failed while inserting the new entry. This can only
120228       ** happen if there was no previous entry for this token.
120229       */
120230       assert( 0==fts3HashFind(pHash, zToken, nToken) );
120231       sqlite3_free(pList);
120232       rc = SQLITE_NOMEM;
120233     }
120234   }
120235   if( rc==SQLITE_OK ){
120236     p->nPendingData += (pList->nData + nToken + sizeof(Fts3HashElem));
120237   }
120238   return rc;
120239 }
120240 
120241 /*
120242 ** Tokenize the nul-terminated string zText and add all tokens to the
120243 ** pending-terms hash-table. The docid used is that currently stored in
120244 ** p->iPrevDocid, and the column is specified by argument iCol.
120245 **
120246 ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error code.
120247 */
120248 static int fts3PendingTermsAdd(
120249   Fts3Table *p,                   /* Table into which text will be inserted */
120250   const char *zText,              /* Text of document to be inserted */
120251   int iCol,                       /* Column into which text is being inserted */
120252   u32 *pnWord                     /* OUT: Number of tokens inserted */
120253 ){
120254   int rc;
120255   int iStart;
120256   int iEnd;
120257   int iPos;
120258   int nWord = 0;
120259 
120260   char const *zToken;
120261   int nToken;
120262 
120263   sqlite3_tokenizer *pTokenizer = p->pTokenizer;
120264   sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
120265   sqlite3_tokenizer_cursor *pCsr;
120266   int (*xNext)(sqlite3_tokenizer_cursor *pCursor,
120267       const char**,int*,int*,int*,int*);
120268 
120269   assert( pTokenizer && pModule );
120270 
120271   /* If the user has inserted a NULL value, this function may be called with
120272   ** zText==0. In this case, add zero token entries to the hash table and
120273   ** return early. */
120274   if( zText==0 ){
120275     *pnWord = 0;
120276     return SQLITE_OK;
120277   }
120278 
120279   rc = pModule->xOpen(pTokenizer, zText, -1, &pCsr);
120280   if( rc!=SQLITE_OK ){
120281     return rc;
120282   }
120283   pCsr->pTokenizer = pTokenizer;
120284 
120285   xNext = pModule->xNext;
120286   while( SQLITE_OK==rc
120287       && SQLITE_OK==(rc = xNext(pCsr, &zToken, &nToken, &iStart, &iEnd, &iPos))
120288   ){
120289     int i;
120290     if( iPos>=nWord ) nWord = iPos+1;
120291 
120292     /* Positions cannot be negative; we use -1 as a terminator internally.
120293     ** Tokens must have a non-zero length.
120294     */
120295     if( iPos<0 || !zToken || nToken<=0 ){
120296       rc = SQLITE_ERROR;
120297       break;
120298     }
120299 
120300     /* Add the term to the terms index */
120301     rc = fts3PendingTermsAddOne(
120302         p, iCol, iPos, &p->aIndex[0].hPending, zToken, nToken
120303     );
120304 
120305     /* Add the term to each of the prefix indexes that it is not too
120306     ** short for. */
120307     for(i=1; rc==SQLITE_OK && i<p->nIndex; i++){
120308       struct Fts3Index *pIndex = &p->aIndex[i];
120309       if( nToken<pIndex->nPrefix ) continue;
120310       rc = fts3PendingTermsAddOne(
120311           p, iCol, iPos, &pIndex->hPending, zToken, pIndex->nPrefix
120312       );
120313     }
120314   }
120315 
120316   pModule->xClose(pCsr);
120317   *pnWord = nWord;
120318   return (rc==SQLITE_DONE ? SQLITE_OK : rc);
120319 }
120320 
120321 /*
120322 ** Calling this function indicates that subsequent calls to
120323 ** fts3PendingTermsAdd() are to add term/position-list pairs for the
120324 ** contents of the document with docid iDocid.
120325 */
120326 static int fts3PendingTermsDocid(Fts3Table *p, sqlite_int64 iDocid){
120327   /* TODO(shess) Explore whether partially flushing the buffer on
120328   ** forced-flush would provide better performance.  I suspect that if
120329   ** we ordered the doclists by size and flushed the largest until the
120330   ** buffer was half empty, that would let the less frequent terms
120331   ** generate longer doclists.
120332   */
120333   if( iDocid<=p->iPrevDocid || p->nPendingData>p->nMaxPendingData ){
120334     int rc = sqlite3Fts3PendingTermsFlush(p);
120335     if( rc!=SQLITE_OK ) return rc;
120336   }
120337   p->iPrevDocid = iDocid;
120338   return SQLITE_OK;
120339 }
120340 
120341 /*
120342 ** Discard the contents of the pending-terms hash tables.
120343 */
120344 SQLITE_PRIVATE void sqlite3Fts3PendingTermsClear(Fts3Table *p){
120345   int i;
120346   for(i=0; i<p->nIndex; i++){
120347     Fts3HashElem *pElem;
120348     Fts3Hash *pHash = &p->aIndex[i].hPending;
120349     for(pElem=fts3HashFirst(pHash); pElem; pElem=fts3HashNext(pElem)){
120350       PendingList *pList = (PendingList *)fts3HashData(pElem);
120351       fts3PendingListDelete(pList);
120352     }
120353     fts3HashClear(pHash);
120354   }
120355   p->nPendingData = 0;
120356 }
120357 
120358 /*
120359 ** This function is called by the xUpdate() method as part of an INSERT
120360 ** operation. It adds entries for each term in the new record to the
120361 ** pendingTerms hash table.
120362 **
120363 ** Argument apVal is the same as the similarly named argument passed to
120364 ** fts3InsertData(). Parameter iDocid is the docid of the new row.
120365 */
120366 static int fts3InsertTerms(Fts3Table *p, sqlite3_value **apVal, u32 *aSz){
120367   int i;                          /* Iterator variable */
120368   for(i=2; i<p->nColumn+2; i++){
120369     const char *zText = (const char *)sqlite3_value_text(apVal[i]);
120370     int rc = fts3PendingTermsAdd(p, zText, i-2, &aSz[i-2]);
120371     if( rc!=SQLITE_OK ){
120372       return rc;
120373     }
120374     aSz[p->nColumn] += sqlite3_value_bytes(apVal[i]);
120375   }
120376   return SQLITE_OK;
120377 }
120378 
120379 /*
120380 ** This function is called by the xUpdate() method for an INSERT operation.
120381 ** The apVal parameter is passed a copy of the apVal argument passed by
120382 ** SQLite to the xUpdate() method. i.e:
120383 **
120384 **   apVal[0]                Not used for INSERT.
120385 **   apVal[1]                rowid
120386 **   apVal[2]                Left-most user-defined column
120387 **   ...
120388 **   apVal[p->nColumn+1]     Right-most user-defined column
120389 **   apVal[p->nColumn+2]     Hidden column with same name as table
120390 **   apVal[p->nColumn+3]     Hidden "docid" column (alias for rowid)
120391 */
120392 static int fts3InsertData(
120393   Fts3Table *p,                   /* Full-text table */
120394   sqlite3_value **apVal,          /* Array of values to insert */
120395   sqlite3_int64 *piDocid          /* OUT: Docid for row just inserted */
120396 ){
120397   int rc;                         /* Return code */
120398   sqlite3_stmt *pContentInsert;   /* INSERT INTO %_content VALUES(...) */
120399 
120400   /* Locate the statement handle used to insert data into the %_content
120401   ** table. The SQL for this statement is:
120402   **
120403   **   INSERT INTO %_content VALUES(?, ?, ?, ...)
120404   **
120405   ** The statement features N '?' variables, where N is the number of user
120406   ** defined columns in the FTS3 table, plus one for the docid field.
120407   */
120408   rc = fts3SqlStmt(p, SQL_CONTENT_INSERT, &pContentInsert, &apVal[1]);
120409   if( rc!=SQLITE_OK ){
120410     return rc;
120411   }
120412 
120413   /* There is a quirk here. The users INSERT statement may have specified
120414   ** a value for the "rowid" field, for the "docid" field, or for both.
120415   ** Which is a problem, since "rowid" and "docid" are aliases for the
120416   ** same value. For example:
120417   **
120418   **   INSERT INTO fts3tbl(rowid, docid) VALUES(1, 2);
120419   **
120420   ** In FTS3, this is an error. It is an error to specify non-NULL values
120421   ** for both docid and some other rowid alias.
120422   */
120423   if( SQLITE_NULL!=sqlite3_value_type(apVal[3+p->nColumn]) ){
120424     if( SQLITE_NULL==sqlite3_value_type(apVal[0])
120425      && SQLITE_NULL!=sqlite3_value_type(apVal[1])
120426     ){
120427       /* A rowid/docid conflict. */
120428       return SQLITE_ERROR;
120429     }
120430     rc = sqlite3_bind_value(pContentInsert, 1, apVal[3+p->nColumn]);
120431     if( rc!=SQLITE_OK ) return rc;
120432   }
120433 
120434   /* Execute the statement to insert the record. Set *piDocid to the
120435   ** new docid value.
120436   */
120437   sqlite3_step(pContentInsert);
120438   rc = sqlite3_reset(pContentInsert);
120439 
120440   *piDocid = sqlite3_last_insert_rowid(p->db);
120441   return rc;
120442 }
120443 
120444 
120445 
120446 /*
120447 ** Remove all data from the FTS3 table. Clear the hash table containing
120448 ** pending terms.
120449 */
120450 static int fts3DeleteAll(Fts3Table *p){
120451   int rc = SQLITE_OK;             /* Return code */
120452 
120453   /* Discard the contents of the pending-terms hash table. */
120454   sqlite3Fts3PendingTermsClear(p);
120455 
120456   /* Delete everything from the %_content, %_segments and %_segdir tables. */
120457   fts3SqlExec(&rc, p, SQL_DELETE_ALL_CONTENT, 0);
120458   fts3SqlExec(&rc, p, SQL_DELETE_ALL_SEGMENTS, 0);
120459   fts3SqlExec(&rc, p, SQL_DELETE_ALL_SEGDIR, 0);
120460   if( p->bHasDocsize ){
120461     fts3SqlExec(&rc, p, SQL_DELETE_ALL_DOCSIZE, 0);
120462   }
120463   if( p->bHasStat ){
120464     fts3SqlExec(&rc, p, SQL_DELETE_ALL_STAT, 0);
120465   }
120466   return rc;
120467 }
120468 
120469 /*
120470 ** The first element in the apVal[] array is assumed to contain the docid
120471 ** (an integer) of a row about to be deleted. Remove all terms from the
120472 ** full-text index.
120473 */
120474 static void fts3DeleteTerms(
120475   int *pRC,               /* Result code */
120476   Fts3Table *p,           /* The FTS table to delete from */
120477   sqlite3_value *pRowid,  /* The docid to be deleted */
120478   u32 *aSz                /* Sizes of deleted document written here */
120479 ){
120480   int rc;
120481   sqlite3_stmt *pSelect;
120482 
120483   if( *pRC ) return;
120484   rc = fts3SqlStmt(p, SQL_SELECT_CONTENT_BY_ROWID, &pSelect, &pRowid);
120485   if( rc==SQLITE_OK ){
120486     if( SQLITE_ROW==sqlite3_step(pSelect) ){
120487       int i;
120488       for(i=1; i<=p->nColumn; i++){
120489         const char *zText = (const char *)sqlite3_column_text(pSelect, i);
120490         rc = fts3PendingTermsAdd(p, zText, -1, &aSz[i-1]);
120491         if( rc!=SQLITE_OK ){
120492           sqlite3_reset(pSelect);
120493           *pRC = rc;
120494           return;
120495         }
120496         aSz[p->nColumn] += sqlite3_column_bytes(pSelect, i);
120497       }
120498     }
120499     rc = sqlite3_reset(pSelect);
120500   }else{
120501     sqlite3_reset(pSelect);
120502   }
120503   *pRC = rc;
120504 }
120505 
120506 /*
120507 ** Forward declaration to account for the circular dependency between
120508 ** functions fts3SegmentMerge() and fts3AllocateSegdirIdx().
120509 */
120510 static int fts3SegmentMerge(Fts3Table *, int, int);
120511 
120512 /*
120513 ** This function allocates a new level iLevel index in the segdir table.
120514 ** Usually, indexes are allocated within a level sequentially starting
120515 ** with 0, so the allocated index is one greater than the value returned
120516 ** by:
120517 **
120518 **   SELECT max(idx) FROM %_segdir WHERE level = :iLevel
120519 **
120520 ** However, if there are already FTS3_MERGE_COUNT indexes at the requested
120521 ** level, they are merged into a single level (iLevel+1) segment and the
120522 ** allocated index is 0.
120523 **
120524 ** If successful, *piIdx is set to the allocated index slot and SQLITE_OK
120525 ** returned. Otherwise, an SQLite error code is returned.
120526 */
120527 static int fts3AllocateSegdirIdx(
120528   Fts3Table *p,
120529   int iIndex,                     /* Index for p->aIndex */
120530   int iLevel,
120531   int *piIdx
120532 ){
120533   int rc;                         /* Return Code */
120534   sqlite3_stmt *pNextIdx;         /* Query for next idx at level iLevel */
120535   int iNext = 0;                  /* Result of query pNextIdx */
120536 
120537   /* Set variable iNext to the next available segdir index at level iLevel. */
120538   rc = fts3SqlStmt(p, SQL_NEXT_SEGMENT_INDEX, &pNextIdx, 0);
120539   if( rc==SQLITE_OK ){
120540     sqlite3_bind_int(pNextIdx, 1, iIndex*FTS3_SEGDIR_MAXLEVEL + iLevel);
120541     if( SQLITE_ROW==sqlite3_step(pNextIdx) ){
120542       iNext = sqlite3_column_int(pNextIdx, 0);
120543     }
120544     rc = sqlite3_reset(pNextIdx);
120545   }
120546 
120547   if( rc==SQLITE_OK ){
120548     /* If iNext is FTS3_MERGE_COUNT, indicating that level iLevel is already
120549     ** full, merge all segments in level iLevel into a single iLevel+1
120550     ** segment and allocate (newly freed) index 0 at level iLevel. Otherwise,
120551     ** if iNext is less than FTS3_MERGE_COUNT, allocate index iNext.
120552     */
120553     if( iNext>=FTS3_MERGE_COUNT ){
120554       rc = fts3SegmentMerge(p, iIndex, iLevel);
120555       *piIdx = 0;
120556     }else{
120557       *piIdx = iNext;
120558     }
120559   }
120560 
120561   return rc;
120562 }
120563 
120564 /*
120565 ** The %_segments table is declared as follows:
120566 **
120567 **   CREATE TABLE %_segments(blockid INTEGER PRIMARY KEY, block BLOB)
120568 **
120569 ** This function reads data from a single row of the %_segments table. The
120570 ** specific row is identified by the iBlockid parameter. If paBlob is not
120571 ** NULL, then a buffer is allocated using sqlite3_malloc() and populated
120572 ** with the contents of the blob stored in the "block" column of the
120573 ** identified table row is. Whether or not paBlob is NULL, *pnBlob is set
120574 ** to the size of the blob in bytes before returning.
120575 **
120576 ** If an error occurs, or the table does not contain the specified row,
120577 ** an SQLite error code is returned. Otherwise, SQLITE_OK is returned. If
120578 ** paBlob is non-NULL, then it is the responsibility of the caller to
120579 ** eventually free the returned buffer.
120580 **
120581 ** This function may leave an open sqlite3_blob* handle in the
120582 ** Fts3Table.pSegments variable. This handle is reused by subsequent calls
120583 ** to this function. The handle may be closed by calling the
120584 ** sqlite3Fts3SegmentsClose() function. Reusing a blob handle is a handy
120585 ** performance improvement, but the blob handle should always be closed
120586 ** before control is returned to the user (to prevent a lock being held
120587 ** on the database file for longer than necessary). Thus, any virtual table
120588 ** method (xFilter etc.) that may directly or indirectly call this function
120589 ** must call sqlite3Fts3SegmentsClose() before returning.
120590 */
120591 SQLITE_PRIVATE int sqlite3Fts3ReadBlock(
120592   Fts3Table *p,                   /* FTS3 table handle */
120593   sqlite3_int64 iBlockid,         /* Access the row with blockid=$iBlockid */
120594   char **paBlob,                  /* OUT: Blob data in malloc'd buffer */
120595   int *pnBlob,                    /* OUT: Size of blob data */
120596   int *pnLoad                     /* OUT: Bytes actually loaded */
120597 ){
120598   int rc;                         /* Return code */
120599 
120600   /* pnBlob must be non-NULL. paBlob may be NULL or non-NULL. */
120601   assert( pnBlob);
120602 
120603   if( p->pSegments ){
120604     rc = sqlite3_blob_reopen(p->pSegments, iBlockid);
120605   }else{
120606     if( 0==p->zSegmentsTbl ){
120607       p->zSegmentsTbl = sqlite3_mprintf("%s_segments", p->zName);
120608       if( 0==p->zSegmentsTbl ) return SQLITE_NOMEM;
120609     }
120610     rc = sqlite3_blob_open(
120611        p->db, p->zDb, p->zSegmentsTbl, "block", iBlockid, 0, &p->pSegments
120612     );
120613   }
120614 
120615   if( rc==SQLITE_OK ){
120616     int nByte = sqlite3_blob_bytes(p->pSegments);
120617     *pnBlob = nByte;
120618     if( paBlob ){
120619       char *aByte = sqlite3_malloc(nByte + FTS3_NODE_PADDING);
120620       if( !aByte ){
120621         rc = SQLITE_NOMEM;
120622       }else{
120623         if( pnLoad && nByte>(FTS3_NODE_CHUNK_THRESHOLD) ){
120624           nByte = FTS3_NODE_CHUNKSIZE;
120625           *pnLoad = nByte;
120626         }
120627         rc = sqlite3_blob_read(p->pSegments, aByte, nByte, 0);
120628         memset(&aByte[nByte], 0, FTS3_NODE_PADDING);
120629         if( rc!=SQLITE_OK ){
120630           sqlite3_free(aByte);
120631           aByte = 0;
120632         }
120633       }
120634       *paBlob = aByte;
120635     }
120636   }
120637 
120638   return rc;
120639 }
120640 
120641 /*
120642 ** Close the blob handle at p->pSegments, if it is open. See comments above
120643 ** the sqlite3Fts3ReadBlock() function for details.
120644 */
120645 SQLITE_PRIVATE void sqlite3Fts3SegmentsClose(Fts3Table *p){
120646   sqlite3_blob_close(p->pSegments);
120647   p->pSegments = 0;
120648 }
120649 
120650 static int fts3SegReaderIncrRead(Fts3SegReader *pReader){
120651   int nRead;                      /* Number of bytes to read */
120652   int rc;                         /* Return code */
120653 
120654   nRead = MIN(pReader->nNode - pReader->nPopulate, FTS3_NODE_CHUNKSIZE);
120655   rc = sqlite3_blob_read(
120656       pReader->pBlob,
120657       &pReader->aNode[pReader->nPopulate],
120658       nRead,
120659       pReader->nPopulate
120660   );
120661 
120662   if( rc==SQLITE_OK ){
120663     pReader->nPopulate += nRead;
120664     memset(&pReader->aNode[pReader->nPopulate], 0, FTS3_NODE_PADDING);
120665     if( pReader->nPopulate==pReader->nNode ){
120666       sqlite3_blob_close(pReader->pBlob);
120667       pReader->pBlob = 0;
120668       pReader->nPopulate = 0;
120669     }
120670   }
120671   return rc;
120672 }
120673 
120674 static int fts3SegReaderRequire(Fts3SegReader *pReader, char *pFrom, int nByte){
120675   int rc = SQLITE_OK;
120676   assert( !pReader->pBlob
120677        || (pFrom>=pReader->aNode && pFrom<&pReader->aNode[pReader->nNode])
120678   );
120679   while( pReader->pBlob && rc==SQLITE_OK
120680      &&  (pFrom - pReader->aNode + nByte)>pReader->nPopulate
120681   ){
120682     rc = fts3SegReaderIncrRead(pReader);
120683   }
120684   return rc;
120685 }
120686 
120687 /*
120688 ** Move the iterator passed as the first argument to the next term in the
120689 ** segment. If successful, SQLITE_OK is returned. If there is no next term,
120690 ** SQLITE_DONE. Otherwise, an SQLite error code.
120691 */
120692 static int fts3SegReaderNext(
120693   Fts3Table *p,
120694   Fts3SegReader *pReader,
120695   int bIncr
120696 ){
120697   int rc;                         /* Return code of various sub-routines */
120698   char *pNext;                    /* Cursor variable */
120699   int nPrefix;                    /* Number of bytes in term prefix */
120700   int nSuffix;                    /* Number of bytes in term suffix */
120701 
120702   if( !pReader->aDoclist ){
120703     pNext = pReader->aNode;
120704   }else{
120705     pNext = &pReader->aDoclist[pReader->nDoclist];
120706   }
120707 
120708   if( !pNext || pNext>=&pReader->aNode[pReader->nNode] ){
120709 
120710     if( fts3SegReaderIsPending(pReader) ){
120711       Fts3HashElem *pElem = *(pReader->ppNextElem);
120712       if( pElem==0 ){
120713         pReader->aNode = 0;
120714       }else{
120715         PendingList *pList = (PendingList *)fts3HashData(pElem);
120716         pReader->zTerm = (char *)fts3HashKey(pElem);
120717         pReader->nTerm = fts3HashKeysize(pElem);
120718         pReader->nNode = pReader->nDoclist = pList->nData + 1;
120719         pReader->aNode = pReader->aDoclist = pList->aData;
120720         pReader->ppNextElem++;
120721         assert( pReader->aNode );
120722       }
120723       return SQLITE_OK;
120724     }
120725 
120726     if( !fts3SegReaderIsRootOnly(pReader) ){
120727       sqlite3_free(pReader->aNode);
120728       sqlite3_blob_close(pReader->pBlob);
120729       pReader->pBlob = 0;
120730     }
120731     pReader->aNode = 0;
120732 
120733     /* If iCurrentBlock>=iLeafEndBlock, this is an EOF condition. All leaf
120734     ** blocks have already been traversed.  */
120735     assert( pReader->iCurrentBlock<=pReader->iLeafEndBlock );
120736     if( pReader->iCurrentBlock>=pReader->iLeafEndBlock ){
120737       return SQLITE_OK;
120738     }
120739 
120740     rc = sqlite3Fts3ReadBlock(
120741         p, ++pReader->iCurrentBlock, &pReader->aNode, &pReader->nNode,
120742         (bIncr ? &pReader->nPopulate : 0)
120743     );
120744     if( rc!=SQLITE_OK ) return rc;
120745     assert( pReader->pBlob==0 );
120746     if( bIncr && pReader->nPopulate<pReader->nNode ){
120747       pReader->pBlob = p->pSegments;
120748       p->pSegments = 0;
120749     }
120750     pNext = pReader->aNode;
120751   }
120752 
120753   assert( !fts3SegReaderIsPending(pReader) );
120754 
120755   rc = fts3SegReaderRequire(pReader, pNext, FTS3_VARINT_MAX*2);
120756   if( rc!=SQLITE_OK ) return rc;
120757 
120758   /* Because of the FTS3_NODE_PADDING bytes of padding, the following is
120759   ** safe (no risk of overread) even if the node data is corrupted. */
120760   pNext += sqlite3Fts3GetVarint32(pNext, &nPrefix);
120761   pNext += sqlite3Fts3GetVarint32(pNext, &nSuffix);
120762   if( nPrefix<0 || nSuffix<=0
120763    || &pNext[nSuffix]>&pReader->aNode[pReader->nNode]
120764   ){
120765     return SQLITE_CORRUPT_VTAB;
120766   }
120767 
120768   if( nPrefix+nSuffix>pReader->nTermAlloc ){
120769     int nNew = (nPrefix+nSuffix)*2;
120770     char *zNew = sqlite3_realloc(pReader->zTerm, nNew);
120771     if( !zNew ){
120772       return SQLITE_NOMEM;
120773     }
120774     pReader->zTerm = zNew;
120775     pReader->nTermAlloc = nNew;
120776   }
120777 
120778   rc = fts3SegReaderRequire(pReader, pNext, nSuffix+FTS3_VARINT_MAX);
120779   if( rc!=SQLITE_OK ) return rc;
120780 
120781   memcpy(&pReader->zTerm[nPrefix], pNext, nSuffix);
120782   pReader->nTerm = nPrefix+nSuffix;
120783   pNext += nSuffix;
120784   pNext += sqlite3Fts3GetVarint32(pNext, &pReader->nDoclist);
120785   pReader->aDoclist = pNext;
120786   pReader->pOffsetList = 0;
120787 
120788   /* Check that the doclist does not appear to extend past the end of the
120789   ** b-tree node. And that the final byte of the doclist is 0x00. If either
120790   ** of these statements is untrue, then the data structure is corrupt.
120791   */
120792   if( &pReader->aDoclist[pReader->nDoclist]>&pReader->aNode[pReader->nNode]
120793    || (pReader->nPopulate==0 && pReader->aDoclist[pReader->nDoclist-1])
120794   ){
120795     return SQLITE_CORRUPT_VTAB;
120796   }
120797   return SQLITE_OK;
120798 }
120799 
120800 /*
120801 ** Set the SegReader to point to the first docid in the doclist associated
120802 ** with the current term.
120803 */
120804 static int fts3SegReaderFirstDocid(Fts3Table *pTab, Fts3SegReader *pReader){
120805   int rc = SQLITE_OK;
120806   assert( pReader->aDoclist );
120807   assert( !pReader->pOffsetList );
120808   if( pTab->bDescIdx && fts3SegReaderIsPending(pReader) ){
120809     u8 bEof = 0;
120810     pReader->iDocid = 0;
120811     pReader->nOffsetList = 0;
120812     sqlite3Fts3DoclistPrev(0,
120813         pReader->aDoclist, pReader->nDoclist, &pReader->pOffsetList,
120814         &pReader->iDocid, &pReader->nOffsetList, &bEof
120815     );
120816   }else{
120817     rc = fts3SegReaderRequire(pReader, pReader->aDoclist, FTS3_VARINT_MAX);
120818     if( rc==SQLITE_OK ){
120819       int n = sqlite3Fts3GetVarint(pReader->aDoclist, &pReader->iDocid);
120820       pReader->pOffsetList = &pReader->aDoclist[n];
120821     }
120822   }
120823   return rc;
120824 }
120825 
120826 /*
120827 ** Advance the SegReader to point to the next docid in the doclist
120828 ** associated with the current term.
120829 **
120830 ** If arguments ppOffsetList and pnOffsetList are not NULL, then
120831 ** *ppOffsetList is set to point to the first column-offset list
120832 ** in the doclist entry (i.e. immediately past the docid varint).
120833 ** *pnOffsetList is set to the length of the set of column-offset
120834 ** lists, not including the nul-terminator byte. For example:
120835 */
120836 static int fts3SegReaderNextDocid(
120837   Fts3Table *pTab,
120838   Fts3SegReader *pReader,         /* Reader to advance to next docid */
120839   char **ppOffsetList,            /* OUT: Pointer to current position-list */
120840   int *pnOffsetList               /* OUT: Length of *ppOffsetList in bytes */
120841 ){
120842   int rc = SQLITE_OK;
120843   char *p = pReader->pOffsetList;
120844   char c = 0;
120845 
120846   assert( p );
120847 
120848   if( pTab->bDescIdx && fts3SegReaderIsPending(pReader) ){
120849     /* A pending-terms seg-reader for an FTS4 table that uses order=desc.
120850     ** Pending-terms doclists are always built up in ascending order, so
120851     ** we have to iterate through them backwards here. */
120852     u8 bEof = 0;
120853     if( ppOffsetList ){
120854       *ppOffsetList = pReader->pOffsetList;
120855       *pnOffsetList = pReader->nOffsetList - 1;
120856     }
120857     sqlite3Fts3DoclistPrev(0,
120858         pReader->aDoclist, pReader->nDoclist, &p, &pReader->iDocid,
120859         &pReader->nOffsetList, &bEof
120860     );
120861     if( bEof ){
120862       pReader->pOffsetList = 0;
120863     }else{
120864       pReader->pOffsetList = p;
120865     }
120866   }else{
120867     char *pEnd = &pReader->aDoclist[pReader->nDoclist];
120868 
120869     /* Pointer p currently points at the first byte of an offset list. The
120870     ** following block advances it to point one byte past the end of
120871     ** the same offset list. */
120872     while( 1 ){
120873 
120874       /* The following line of code (and the "p++" below the while() loop) is
120875       ** normally all that is required to move pointer p to the desired
120876       ** position. The exception is if this node is being loaded from disk
120877       ** incrementally and pointer "p" now points to the first byte passed
120878       ** the populated part of pReader->aNode[].
120879       */
120880       while( *p | c ) c = *p++ & 0x80;
120881       assert( *p==0 );
120882 
120883       if( pReader->pBlob==0 || p<&pReader->aNode[pReader->nPopulate] ) break;
120884       rc = fts3SegReaderIncrRead(pReader);
120885       if( rc!=SQLITE_OK ) return rc;
120886     }
120887     p++;
120888 
120889     /* If required, populate the output variables with a pointer to and the
120890     ** size of the previous offset-list.
120891     */
120892     if( ppOffsetList ){
120893       *ppOffsetList = pReader->pOffsetList;
120894       *pnOffsetList = (int)(p - pReader->pOffsetList - 1);
120895     }
120896 
120897     while( p<pEnd && *p==0 ) p++;
120898 
120899     /* If there are no more entries in the doclist, set pOffsetList to
120900     ** NULL. Otherwise, set Fts3SegReader.iDocid to the next docid and
120901     ** Fts3SegReader.pOffsetList to point to the next offset list before
120902     ** returning.
120903     */
120904     if( p>=pEnd ){
120905       pReader->pOffsetList = 0;
120906     }else{
120907       rc = fts3SegReaderRequire(pReader, p, FTS3_VARINT_MAX);
120908       if( rc==SQLITE_OK ){
120909         sqlite3_int64 iDelta;
120910         pReader->pOffsetList = p + sqlite3Fts3GetVarint(p, &iDelta);
120911         if( pTab->bDescIdx ){
120912           pReader->iDocid -= iDelta;
120913         }else{
120914           pReader->iDocid += iDelta;
120915         }
120916       }
120917     }
120918   }
120919 
120920   return SQLITE_OK;
120921 }
120922 
120923 
120924 SQLITE_PRIVATE int sqlite3Fts3MsrOvfl(
120925   Fts3Cursor *pCsr,
120926   Fts3MultiSegReader *pMsr,
120927   int *pnOvfl
120928 ){
120929   Fts3Table *p = (Fts3Table*)pCsr->base.pVtab;
120930   int nOvfl = 0;
120931   int ii;
120932   int rc = SQLITE_OK;
120933   int pgsz = p->nPgsz;
120934 
120935   assert( p->bHasStat );
120936   assert( pgsz>0 );
120937 
120938   for(ii=0; rc==SQLITE_OK && ii<pMsr->nSegment; ii++){
120939     Fts3SegReader *pReader = pMsr->apSegment[ii];
120940     if( !fts3SegReaderIsPending(pReader)
120941      && !fts3SegReaderIsRootOnly(pReader)
120942     ){
120943       sqlite3_int64 jj;
120944       for(jj=pReader->iStartBlock; jj<=pReader->iLeafEndBlock; jj++){
120945         int nBlob;
120946         rc = sqlite3Fts3ReadBlock(p, jj, 0, &nBlob, 0);
120947         if( rc!=SQLITE_OK ) break;
120948         if( (nBlob+35)>pgsz ){
120949           nOvfl += (nBlob + 34)/pgsz;
120950         }
120951       }
120952     }
120953   }
120954   *pnOvfl = nOvfl;
120955   return rc;
120956 }
120957 
120958 /*
120959 ** Free all allocations associated with the iterator passed as the
120960 ** second argument.
120961 */
120962 SQLITE_PRIVATE void sqlite3Fts3SegReaderFree(Fts3SegReader *pReader){
120963   if( pReader && !fts3SegReaderIsPending(pReader) ){
120964     sqlite3_free(pReader->zTerm);
120965     if( !fts3SegReaderIsRootOnly(pReader) ){
120966       sqlite3_free(pReader->aNode);
120967       sqlite3_blob_close(pReader->pBlob);
120968     }
120969   }
120970   sqlite3_free(pReader);
120971 }
120972 
120973 /*
120974 ** Allocate a new SegReader object.
120975 */
120976 SQLITE_PRIVATE int sqlite3Fts3SegReaderNew(
120977   int iAge,                       /* Segment "age". */
120978   sqlite3_int64 iStartLeaf,       /* First leaf to traverse */
120979   sqlite3_int64 iEndLeaf,         /* Final leaf to traverse */
120980   sqlite3_int64 iEndBlock,        /* Final block of segment */
120981   const char *zRoot,              /* Buffer containing root node */
120982   int nRoot,                      /* Size of buffer containing root node */
120983   Fts3SegReader **ppReader        /* OUT: Allocated Fts3SegReader */
120984 ){
120985   int rc = SQLITE_OK;             /* Return code */
120986   Fts3SegReader *pReader;         /* Newly allocated SegReader object */
120987   int nExtra = 0;                 /* Bytes to allocate segment root node */
120988 
120989   assert( iStartLeaf<=iEndLeaf );
120990   if( iStartLeaf==0 ){
120991     nExtra = nRoot + FTS3_NODE_PADDING;
120992   }
120993 
120994   pReader = (Fts3SegReader *)sqlite3_malloc(sizeof(Fts3SegReader) + nExtra);
120995   if( !pReader ){
120996     return SQLITE_NOMEM;
120997   }
120998   memset(pReader, 0, sizeof(Fts3SegReader));
120999   pReader->iIdx = iAge;
121000   pReader->iStartBlock = iStartLeaf;
121001   pReader->iLeafEndBlock = iEndLeaf;
121002   pReader->iEndBlock = iEndBlock;
121003 
121004   if( nExtra ){
121005     /* The entire segment is stored in the root node. */
121006     pReader->aNode = (char *)&pReader[1];
121007     pReader->nNode = nRoot;
121008     memcpy(pReader->aNode, zRoot, nRoot);
121009     memset(&pReader->aNode[nRoot], 0, FTS3_NODE_PADDING);
121010   }else{
121011     pReader->iCurrentBlock = iStartLeaf-1;
121012   }
121013 
121014   if( rc==SQLITE_OK ){
121015     *ppReader = pReader;
121016   }else{
121017     sqlite3Fts3SegReaderFree(pReader);
121018   }
121019   return rc;
121020 }
121021 
121022 /*
121023 ** This is a comparison function used as a qsort() callback when sorting
121024 ** an array of pending terms by term. This occurs as part of flushing
121025 ** the contents of the pending-terms hash table to the database.
121026 */
121027 static int fts3CompareElemByTerm(const void *lhs, const void *rhs){
121028   char *z1 = fts3HashKey(*(Fts3HashElem **)lhs);
121029   char *z2 = fts3HashKey(*(Fts3HashElem **)rhs);
121030   int n1 = fts3HashKeysize(*(Fts3HashElem **)lhs);
121031   int n2 = fts3HashKeysize(*(Fts3HashElem **)rhs);
121032 
121033   int n = (n1<n2 ? n1 : n2);
121034   int c = memcmp(z1, z2, n);
121035   if( c==0 ){
121036     c = n1 - n2;
121037   }
121038   return c;
121039 }
121040 
121041 /*
121042 ** This function is used to allocate an Fts3SegReader that iterates through
121043 ** a subset of the terms stored in the Fts3Table.pendingTerms array.
121044 **
121045 ** If the isPrefixIter parameter is zero, then the returned SegReader iterates
121046 ** through each term in the pending-terms table. Or, if isPrefixIter is
121047 ** non-zero, it iterates through each term and its prefixes. For example, if
121048 ** the pending terms hash table contains the terms "sqlite", "mysql" and
121049 ** "firebird", then the iterator visits the following 'terms' (in the order
121050 ** shown):
121051 **
121052 **   f fi fir fire fireb firebi firebir firebird
121053 **   m my mys mysq mysql
121054 **   s sq sql sqli sqlit sqlite
121055 **
121056 ** Whereas if isPrefixIter is zero, the terms visited are:
121057 **
121058 **   firebird mysql sqlite
121059 */
121060 SQLITE_PRIVATE int sqlite3Fts3SegReaderPending(
121061   Fts3Table *p,                   /* Virtual table handle */
121062   int iIndex,                     /* Index for p->aIndex */
121063   const char *zTerm,              /* Term to search for */
121064   int nTerm,                      /* Size of buffer zTerm */
121065   int bPrefix,                    /* True for a prefix iterator */
121066   Fts3SegReader **ppReader        /* OUT: SegReader for pending-terms */
121067 ){
121068   Fts3SegReader *pReader = 0;     /* Fts3SegReader object to return */
121069   Fts3HashElem **aElem = 0;       /* Array of term hash entries to scan */
121070   int nElem = 0;                  /* Size of array at aElem */
121071   int rc = SQLITE_OK;             /* Return Code */
121072   Fts3Hash *pHash;
121073 
121074   pHash = &p->aIndex[iIndex].hPending;
121075   if( bPrefix ){
121076     int nAlloc = 0;               /* Size of allocated array at aElem */
121077     Fts3HashElem *pE = 0;         /* Iterator variable */
121078 
121079     for(pE=fts3HashFirst(pHash); pE; pE=fts3HashNext(pE)){
121080       char *zKey = (char *)fts3HashKey(pE);
121081       int nKey = fts3HashKeysize(pE);
121082       if( nTerm==0 || (nKey>=nTerm && 0==memcmp(zKey, zTerm, nTerm)) ){
121083         if( nElem==nAlloc ){
121084           Fts3HashElem **aElem2;
121085           nAlloc += 16;
121086           aElem2 = (Fts3HashElem **)sqlite3_realloc(
121087               aElem, nAlloc*sizeof(Fts3HashElem *)
121088           );
121089           if( !aElem2 ){
121090             rc = SQLITE_NOMEM;
121091             nElem = 0;
121092             break;
121093           }
121094           aElem = aElem2;
121095         }
121096 
121097         aElem[nElem++] = pE;
121098       }
121099     }
121100 
121101     /* If more than one term matches the prefix, sort the Fts3HashElem
121102     ** objects in term order using qsort(). This uses the same comparison
121103     ** callback as is used when flushing terms to disk.
121104     */
121105     if( nElem>1 ){
121106       qsort(aElem, nElem, sizeof(Fts3HashElem *), fts3CompareElemByTerm);
121107     }
121108 
121109   }else{
121110     /* The query is a simple term lookup that matches at most one term in
121111     ** the index. All that is required is a straight hash-lookup. */
121112     Fts3HashElem *pE = fts3HashFindElem(pHash, zTerm, nTerm);
121113     if( pE ){
121114       aElem = &pE;
121115       nElem = 1;
121116     }
121117   }
121118 
121119   if( nElem>0 ){
121120     int nByte = sizeof(Fts3SegReader) + (nElem+1)*sizeof(Fts3HashElem *);
121121     pReader = (Fts3SegReader *)sqlite3_malloc(nByte);
121122     if( !pReader ){
121123       rc = SQLITE_NOMEM;
121124     }else{
121125       memset(pReader, 0, nByte);
121126       pReader->iIdx = 0x7FFFFFFF;
121127       pReader->ppNextElem = (Fts3HashElem **)&pReader[1];
121128       memcpy(pReader->ppNextElem, aElem, nElem*sizeof(Fts3HashElem *));
121129     }
121130   }
121131 
121132   if( bPrefix ){
121133     sqlite3_free(aElem);
121134   }
121135   *ppReader = pReader;
121136   return rc;
121137 }
121138 
121139 /*
121140 ** Compare the entries pointed to by two Fts3SegReader structures.
121141 ** Comparison is as follows:
121142 **
121143 **   1) EOF is greater than not EOF.
121144 **
121145 **   2) The current terms (if any) are compared using memcmp(). If one
121146 **      term is a prefix of another, the longer term is considered the
121147 **      larger.
121148 **
121149 **   3) By segment age. An older segment is considered larger.
121150 */
121151 static int fts3SegReaderCmp(Fts3SegReader *pLhs, Fts3SegReader *pRhs){
121152   int rc;
121153   if( pLhs->aNode && pRhs->aNode ){
121154     int rc2 = pLhs->nTerm - pRhs->nTerm;
121155     if( rc2<0 ){
121156       rc = memcmp(pLhs->zTerm, pRhs->zTerm, pLhs->nTerm);
121157     }else{
121158       rc = memcmp(pLhs->zTerm, pRhs->zTerm, pRhs->nTerm);
121159     }
121160     if( rc==0 ){
121161       rc = rc2;
121162     }
121163   }else{
121164     rc = (pLhs->aNode==0) - (pRhs->aNode==0);
121165   }
121166   if( rc==0 ){
121167     rc = pRhs->iIdx - pLhs->iIdx;
121168   }
121169   assert( rc!=0 );
121170   return rc;
121171 }
121172 
121173 /*
121174 ** A different comparison function for SegReader structures. In this
121175 ** version, it is assumed that each SegReader points to an entry in
121176 ** a doclist for identical terms. Comparison is made as follows:
121177 **
121178 **   1) EOF (end of doclist in this case) is greater than not EOF.
121179 **
121180 **   2) By current docid.
121181 **
121182 **   3) By segment age. An older segment is considered larger.
121183 */
121184 static int fts3SegReaderDoclistCmp(Fts3SegReader *pLhs, Fts3SegReader *pRhs){
121185   int rc = (pLhs->pOffsetList==0)-(pRhs->pOffsetList==0);
121186   if( rc==0 ){
121187     if( pLhs->iDocid==pRhs->iDocid ){
121188       rc = pRhs->iIdx - pLhs->iIdx;
121189     }else{
121190       rc = (pLhs->iDocid > pRhs->iDocid) ? 1 : -1;
121191     }
121192   }
121193   assert( pLhs->aNode && pRhs->aNode );
121194   return rc;
121195 }
121196 static int fts3SegReaderDoclistCmpRev(Fts3SegReader *pLhs, Fts3SegReader *pRhs){
121197   int rc = (pLhs->pOffsetList==0)-(pRhs->pOffsetList==0);
121198   if( rc==0 ){
121199     if( pLhs->iDocid==pRhs->iDocid ){
121200       rc = pRhs->iIdx - pLhs->iIdx;
121201     }else{
121202       rc = (pLhs->iDocid < pRhs->iDocid) ? 1 : -1;
121203     }
121204   }
121205   assert( pLhs->aNode && pRhs->aNode );
121206   return rc;
121207 }
121208 
121209 /*
121210 ** Compare the term that the Fts3SegReader object passed as the first argument
121211 ** points to with the term specified by arguments zTerm and nTerm.
121212 **
121213 ** If the pSeg iterator is already at EOF, return 0. Otherwise, return
121214 ** -ve if the pSeg term is less than zTerm/nTerm, 0 if the two terms are
121215 ** equal, or +ve if the pSeg term is greater than zTerm/nTerm.
121216 */
121217 static int fts3SegReaderTermCmp(
121218   Fts3SegReader *pSeg,            /* Segment reader object */
121219   const char *zTerm,              /* Term to compare to */
121220   int nTerm                       /* Size of term zTerm in bytes */
121221 ){
121222   int res = 0;
121223   if( pSeg->aNode ){
121224     if( pSeg->nTerm>nTerm ){
121225       res = memcmp(pSeg->zTerm, zTerm, nTerm);
121226     }else{
121227       res = memcmp(pSeg->zTerm, zTerm, pSeg->nTerm);
121228     }
121229     if( res==0 ){
121230       res = pSeg->nTerm-nTerm;
121231     }
121232   }
121233   return res;
121234 }
121235 
121236 /*
121237 ** Argument apSegment is an array of nSegment elements. It is known that
121238 ** the final (nSegment-nSuspect) members are already in sorted order
121239 ** (according to the comparison function provided). This function shuffles
121240 ** the array around until all entries are in sorted order.
121241 */
121242 static void fts3SegReaderSort(
121243   Fts3SegReader **apSegment,                     /* Array to sort entries of */
121244   int nSegment,                                  /* Size of apSegment array */
121245   int nSuspect,                                  /* Unsorted entry count */
121246   int (*xCmp)(Fts3SegReader *, Fts3SegReader *)  /* Comparison function */
121247 ){
121248   int i;                          /* Iterator variable */
121249 
121250   assert( nSuspect<=nSegment );
121251 
121252   if( nSuspect==nSegment ) nSuspect--;
121253   for(i=nSuspect-1; i>=0; i--){
121254     int j;
121255     for(j=i; j<(nSegment-1); j++){
121256       Fts3SegReader *pTmp;
121257       if( xCmp(apSegment[j], apSegment[j+1])<0 ) break;
121258       pTmp = apSegment[j+1];
121259       apSegment[j+1] = apSegment[j];
121260       apSegment[j] = pTmp;
121261     }
121262   }
121263 
121264 #ifndef NDEBUG
121265   /* Check that the list really is sorted now. */
121266   for(i=0; i<(nSuspect-1); i++){
121267     assert( xCmp(apSegment[i], apSegment[i+1])<0 );
121268   }
121269 #endif
121270 }
121271 
121272 /*
121273 ** Insert a record into the %_segments table.
121274 */
121275 static int fts3WriteSegment(
121276   Fts3Table *p,                   /* Virtual table handle */
121277   sqlite3_int64 iBlock,           /* Block id for new block */
121278   char *z,                        /* Pointer to buffer containing block data */
121279   int n                           /* Size of buffer z in bytes */
121280 ){
121281   sqlite3_stmt *pStmt;
121282   int rc = fts3SqlStmt(p, SQL_INSERT_SEGMENTS, &pStmt, 0);
121283   if( rc==SQLITE_OK ){
121284     sqlite3_bind_int64(pStmt, 1, iBlock);
121285     sqlite3_bind_blob(pStmt, 2, z, n, SQLITE_STATIC);
121286     sqlite3_step(pStmt);
121287     rc = sqlite3_reset(pStmt);
121288   }
121289   return rc;
121290 }
121291 
121292 /*
121293 ** Insert a record into the %_segdir table.
121294 */
121295 static int fts3WriteSegdir(
121296   Fts3Table *p,                   /* Virtual table handle */
121297   int iLevel,                     /* Value for "level" field */
121298   int iIdx,                       /* Value for "idx" field */
121299   sqlite3_int64 iStartBlock,      /* Value for "start_block" field */
121300   sqlite3_int64 iLeafEndBlock,    /* Value for "leaves_end_block" field */
121301   sqlite3_int64 iEndBlock,        /* Value for "end_block" field */
121302   char *zRoot,                    /* Blob value for "root" field */
121303   int nRoot                       /* Number of bytes in buffer zRoot */
121304 ){
121305   sqlite3_stmt *pStmt;
121306   int rc = fts3SqlStmt(p, SQL_INSERT_SEGDIR, &pStmt, 0);
121307   if( rc==SQLITE_OK ){
121308     sqlite3_bind_int(pStmt, 1, iLevel);
121309     sqlite3_bind_int(pStmt, 2, iIdx);
121310     sqlite3_bind_int64(pStmt, 3, iStartBlock);
121311     sqlite3_bind_int64(pStmt, 4, iLeafEndBlock);
121312     sqlite3_bind_int64(pStmt, 5, iEndBlock);
121313     sqlite3_bind_blob(pStmt, 6, zRoot, nRoot, SQLITE_STATIC);
121314     sqlite3_step(pStmt);
121315     rc = sqlite3_reset(pStmt);
121316   }
121317   return rc;
121318 }
121319 
121320 /*
121321 ** Return the size of the common prefix (if any) shared by zPrev and
121322 ** zNext, in bytes. For example,
121323 **
121324 **   fts3PrefixCompress("abc", 3, "abcdef", 6)   // returns 3
121325 **   fts3PrefixCompress("abX", 3, "abcdef", 6)   // returns 2
121326 **   fts3PrefixCompress("abX", 3, "Xbcdef", 6)   // returns 0
121327 */
121328 static int fts3PrefixCompress(
121329   const char *zPrev,              /* Buffer containing previous term */
121330   int nPrev,                      /* Size of buffer zPrev in bytes */
121331   const char *zNext,              /* Buffer containing next term */
121332   int nNext                       /* Size of buffer zNext in bytes */
121333 ){
121334   int n;
121335   UNUSED_PARAMETER(nNext);
121336   for(n=0; n<nPrev && zPrev[n]==zNext[n]; n++);
121337   return n;
121338 }
121339 
121340 /*
121341 ** Add term zTerm to the SegmentNode. It is guaranteed that zTerm is larger
121342 ** (according to memcmp) than the previous term.
121343 */
121344 static int fts3NodeAddTerm(
121345   Fts3Table *p,                   /* Virtual table handle */
121346   SegmentNode **ppTree,           /* IN/OUT: SegmentNode handle */
121347   int isCopyTerm,                 /* True if zTerm/nTerm is transient */
121348   const char *zTerm,              /* Pointer to buffer containing term */
121349   int nTerm                       /* Size of term in bytes */
121350 ){
121351   SegmentNode *pTree = *ppTree;
121352   int rc;
121353   SegmentNode *pNew;
121354 
121355   /* First try to append the term to the current node. Return early if
121356   ** this is possible.
121357   */
121358   if( pTree ){
121359     int nData = pTree->nData;     /* Current size of node in bytes */
121360     int nReq = nData;             /* Required space after adding zTerm */
121361     int nPrefix;                  /* Number of bytes of prefix compression */
121362     int nSuffix;                  /* Suffix length */
121363 
121364     nPrefix = fts3PrefixCompress(pTree->zTerm, pTree->nTerm, zTerm, nTerm);
121365     nSuffix = nTerm-nPrefix;
121366 
121367     nReq += sqlite3Fts3VarintLen(nPrefix)+sqlite3Fts3VarintLen(nSuffix)+nSuffix;
121368     if( nReq<=p->nNodeSize || !pTree->zTerm ){
121369 
121370       if( nReq>p->nNodeSize ){
121371         /* An unusual case: this is the first term to be added to the node
121372         ** and the static node buffer (p->nNodeSize bytes) is not large
121373         ** enough. Use a separately malloced buffer instead This wastes
121374         ** p->nNodeSize bytes, but since this scenario only comes about when
121375         ** the database contain two terms that share a prefix of almost 2KB,
121376         ** this is not expected to be a serious problem.
121377         */
121378         assert( pTree->aData==(char *)&pTree[1] );
121379         pTree->aData = (char *)sqlite3_malloc(nReq);
121380         if( !pTree->aData ){
121381           return SQLITE_NOMEM;
121382         }
121383       }
121384 
121385       if( pTree->zTerm ){
121386         /* There is no prefix-length field for first term in a node */
121387         nData += sqlite3Fts3PutVarint(&pTree->aData[nData], nPrefix);
121388       }
121389 
121390       nData += sqlite3Fts3PutVarint(&pTree->aData[nData], nSuffix);
121391       memcpy(&pTree->aData[nData], &zTerm[nPrefix], nSuffix);
121392       pTree->nData = nData + nSuffix;
121393       pTree->nEntry++;
121394 
121395       if( isCopyTerm ){
121396         if( pTree->nMalloc<nTerm ){
121397           char *zNew = sqlite3_realloc(pTree->zMalloc, nTerm*2);
121398           if( !zNew ){
121399             return SQLITE_NOMEM;
121400           }
121401           pTree->nMalloc = nTerm*2;
121402           pTree->zMalloc = zNew;
121403         }
121404         pTree->zTerm = pTree->zMalloc;
121405         memcpy(pTree->zTerm, zTerm, nTerm);
121406         pTree->nTerm = nTerm;
121407       }else{
121408         pTree->zTerm = (char *)zTerm;
121409         pTree->nTerm = nTerm;
121410       }
121411       return SQLITE_OK;
121412     }
121413   }
121414 
121415   /* If control flows to here, it was not possible to append zTerm to the
121416   ** current node. Create a new node (a right-sibling of the current node).
121417   ** If this is the first node in the tree, the term is added to it.
121418   **
121419   ** Otherwise, the term is not added to the new node, it is left empty for
121420   ** now. Instead, the term is inserted into the parent of pTree. If pTree
121421   ** has no parent, one is created here.
121422   */
121423   pNew = (SegmentNode *)sqlite3_malloc(sizeof(SegmentNode) + p->nNodeSize);
121424   if( !pNew ){
121425     return SQLITE_NOMEM;
121426   }
121427   memset(pNew, 0, sizeof(SegmentNode));
121428   pNew->nData = 1 + FTS3_VARINT_MAX;
121429   pNew->aData = (char *)&pNew[1];
121430 
121431   if( pTree ){
121432     SegmentNode *pParent = pTree->pParent;
121433     rc = fts3NodeAddTerm(p, &pParent, isCopyTerm, zTerm, nTerm);
121434     if( pTree->pParent==0 ){
121435       pTree->pParent = pParent;
121436     }
121437     pTree->pRight = pNew;
121438     pNew->pLeftmost = pTree->pLeftmost;
121439     pNew->pParent = pParent;
121440     pNew->zMalloc = pTree->zMalloc;
121441     pNew->nMalloc = pTree->nMalloc;
121442     pTree->zMalloc = 0;
121443   }else{
121444     pNew->pLeftmost = pNew;
121445     rc = fts3NodeAddTerm(p, &pNew, isCopyTerm, zTerm, nTerm);
121446   }
121447 
121448   *ppTree = pNew;
121449   return rc;
121450 }
121451 
121452 /*
121453 ** Helper function for fts3NodeWrite().
121454 */
121455 static int fts3TreeFinishNode(
121456   SegmentNode *pTree,
121457   int iHeight,
121458   sqlite3_int64 iLeftChild
121459 ){
121460   int nStart;
121461   assert( iHeight>=1 && iHeight<128 );
121462   nStart = FTS3_VARINT_MAX - sqlite3Fts3VarintLen(iLeftChild);
121463   pTree->aData[nStart] = (char)iHeight;
121464   sqlite3Fts3PutVarint(&pTree->aData[nStart+1], iLeftChild);
121465   return nStart;
121466 }
121467 
121468 /*
121469 ** Write the buffer for the segment node pTree and all of its peers to the
121470 ** database. Then call this function recursively to write the parent of
121471 ** pTree and its peers to the database.
121472 **
121473 ** Except, if pTree is a root node, do not write it to the database. Instead,
121474 ** set output variables *paRoot and *pnRoot to contain the root node.
121475 **
121476 ** If successful, SQLITE_OK is returned and output variable *piLast is
121477 ** set to the largest blockid written to the database (or zero if no
121478 ** blocks were written to the db). Otherwise, an SQLite error code is
121479 ** returned.
121480 */
121481 static int fts3NodeWrite(
121482   Fts3Table *p,                   /* Virtual table handle */
121483   SegmentNode *pTree,             /* SegmentNode handle */
121484   int iHeight,                    /* Height of this node in tree */
121485   sqlite3_int64 iLeaf,            /* Block id of first leaf node */
121486   sqlite3_int64 iFree,            /* Block id of next free slot in %_segments */
121487   sqlite3_int64 *piLast,          /* OUT: Block id of last entry written */
121488   char **paRoot,                  /* OUT: Data for root node */
121489   int *pnRoot                     /* OUT: Size of root node in bytes */
121490 ){
121491   int rc = SQLITE_OK;
121492 
121493   if( !pTree->pParent ){
121494     /* Root node of the tree. */
121495     int nStart = fts3TreeFinishNode(pTree, iHeight, iLeaf);
121496     *piLast = iFree-1;
121497     *pnRoot = pTree->nData - nStart;
121498     *paRoot = &pTree->aData[nStart];
121499   }else{
121500     SegmentNode *pIter;
121501     sqlite3_int64 iNextFree = iFree;
121502     sqlite3_int64 iNextLeaf = iLeaf;
121503     for(pIter=pTree->pLeftmost; pIter && rc==SQLITE_OK; pIter=pIter->pRight){
121504       int nStart = fts3TreeFinishNode(pIter, iHeight, iNextLeaf);
121505       int nWrite = pIter->nData - nStart;
121506 
121507       rc = fts3WriteSegment(p, iNextFree, &pIter->aData[nStart], nWrite);
121508       iNextFree++;
121509       iNextLeaf += (pIter->nEntry+1);
121510     }
121511     if( rc==SQLITE_OK ){
121512       assert( iNextLeaf==iFree );
121513       rc = fts3NodeWrite(
121514           p, pTree->pParent, iHeight+1, iFree, iNextFree, piLast, paRoot, pnRoot
121515       );
121516     }
121517   }
121518 
121519   return rc;
121520 }
121521 
121522 /*
121523 ** Free all memory allocations associated with the tree pTree.
121524 */
121525 static void fts3NodeFree(SegmentNode *pTree){
121526   if( pTree ){
121527     SegmentNode *p = pTree->pLeftmost;
121528     fts3NodeFree(p->pParent);
121529     while( p ){
121530       SegmentNode *pRight = p->pRight;
121531       if( p->aData!=(char *)&p[1] ){
121532         sqlite3_free(p->aData);
121533       }
121534       assert( pRight==0 || p->zMalloc==0 );
121535       sqlite3_free(p->zMalloc);
121536       sqlite3_free(p);
121537       p = pRight;
121538     }
121539   }
121540 }
121541 
121542 /*
121543 ** Add a term to the segment being constructed by the SegmentWriter object
121544 ** *ppWriter. When adding the first term to a segment, *ppWriter should
121545 ** be passed NULL. This function will allocate a new SegmentWriter object
121546 ** and return it via the input/output variable *ppWriter in this case.
121547 **
121548 ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error code.
121549 */
121550 static int fts3SegWriterAdd(
121551   Fts3Table *p,                   /* Virtual table handle */
121552   SegmentWriter **ppWriter,       /* IN/OUT: SegmentWriter handle */
121553   int isCopyTerm,                 /* True if buffer zTerm must be copied */
121554   const char *zTerm,              /* Pointer to buffer containing term */
121555   int nTerm,                      /* Size of term in bytes */
121556   const char *aDoclist,           /* Pointer to buffer containing doclist */
121557   int nDoclist                    /* Size of doclist in bytes */
121558 ){
121559   int nPrefix;                    /* Size of term prefix in bytes */
121560   int nSuffix;                    /* Size of term suffix in bytes */
121561   int nReq;                       /* Number of bytes required on leaf page */
121562   int nData;
121563   SegmentWriter *pWriter = *ppWriter;
121564 
121565   if( !pWriter ){
121566     int rc;
121567     sqlite3_stmt *pStmt;
121568 
121569     /* Allocate the SegmentWriter structure */
121570     pWriter = (SegmentWriter *)sqlite3_malloc(sizeof(SegmentWriter));
121571     if( !pWriter ) return SQLITE_NOMEM;
121572     memset(pWriter, 0, sizeof(SegmentWriter));
121573     *ppWriter = pWriter;
121574 
121575     /* Allocate a buffer in which to accumulate data */
121576     pWriter->aData = (char *)sqlite3_malloc(p->nNodeSize);
121577     if( !pWriter->aData ) return SQLITE_NOMEM;
121578     pWriter->nSize = p->nNodeSize;
121579 
121580     /* Find the next free blockid in the %_segments table */
121581     rc = fts3SqlStmt(p, SQL_NEXT_SEGMENTS_ID, &pStmt, 0);
121582     if( rc!=SQLITE_OK ) return rc;
121583     if( SQLITE_ROW==sqlite3_step(pStmt) ){
121584       pWriter->iFree = sqlite3_column_int64(pStmt, 0);
121585       pWriter->iFirst = pWriter->iFree;
121586     }
121587     rc = sqlite3_reset(pStmt);
121588     if( rc!=SQLITE_OK ) return rc;
121589   }
121590   nData = pWriter->nData;
121591 
121592   nPrefix = fts3PrefixCompress(pWriter->zTerm, pWriter->nTerm, zTerm, nTerm);
121593   nSuffix = nTerm-nPrefix;
121594 
121595   /* Figure out how many bytes are required by this new entry */
121596   nReq = sqlite3Fts3VarintLen(nPrefix) +    /* varint containing prefix size */
121597     sqlite3Fts3VarintLen(nSuffix) +         /* varint containing suffix size */
121598     nSuffix +                               /* Term suffix */
121599     sqlite3Fts3VarintLen(nDoclist) +        /* Size of doclist */
121600     nDoclist;                               /* Doclist data */
121601 
121602   if( nData>0 && nData+nReq>p->nNodeSize ){
121603     int rc;
121604 
121605     /* The current leaf node is full. Write it out to the database. */
121606     rc = fts3WriteSegment(p, pWriter->iFree++, pWriter->aData, nData);
121607     if( rc!=SQLITE_OK ) return rc;
121608 
121609     /* Add the current term to the interior node tree. The term added to
121610     ** the interior tree must:
121611     **
121612     **   a) be greater than the largest term on the leaf node just written
121613     **      to the database (still available in pWriter->zTerm), and
121614     **
121615     **   b) be less than or equal to the term about to be added to the new
121616     **      leaf node (zTerm/nTerm).
121617     **
121618     ** In other words, it must be the prefix of zTerm 1 byte longer than
121619     ** the common prefix (if any) of zTerm and pWriter->zTerm.
121620     */
121621     assert( nPrefix<nTerm );
121622     rc = fts3NodeAddTerm(p, &pWriter->pTree, isCopyTerm, zTerm, nPrefix+1);
121623     if( rc!=SQLITE_OK ) return rc;
121624 
121625     nData = 0;
121626     pWriter->nTerm = 0;
121627 
121628     nPrefix = 0;
121629     nSuffix = nTerm;
121630     nReq = 1 +                              /* varint containing prefix size */
121631       sqlite3Fts3VarintLen(nTerm) +         /* varint containing suffix size */
121632       nTerm +                               /* Term suffix */
121633       sqlite3Fts3VarintLen(nDoclist) +      /* Size of doclist */
121634       nDoclist;                             /* Doclist data */
121635   }
121636 
121637   /* If the buffer currently allocated is too small for this entry, realloc
121638   ** the buffer to make it large enough.
121639   */
121640   if( nReq>pWriter->nSize ){
121641     char *aNew = sqlite3_realloc(pWriter->aData, nReq);
121642     if( !aNew ) return SQLITE_NOMEM;
121643     pWriter->aData = aNew;
121644     pWriter->nSize = nReq;
121645   }
121646   assert( nData+nReq<=pWriter->nSize );
121647 
121648   /* Append the prefix-compressed term and doclist to the buffer. */
121649   nData += sqlite3Fts3PutVarint(&pWriter->aData[nData], nPrefix);
121650   nData += sqlite3Fts3PutVarint(&pWriter->aData[nData], nSuffix);
121651   memcpy(&pWriter->aData[nData], &zTerm[nPrefix], nSuffix);
121652   nData += nSuffix;
121653   nData += sqlite3Fts3PutVarint(&pWriter->aData[nData], nDoclist);
121654   memcpy(&pWriter->aData[nData], aDoclist, nDoclist);
121655   pWriter->nData = nData + nDoclist;
121656 
121657   /* Save the current term so that it can be used to prefix-compress the next.
121658   ** If the isCopyTerm parameter is true, then the buffer pointed to by
121659   ** zTerm is transient, so take a copy of the term data. Otherwise, just
121660   ** store a copy of the pointer.
121661   */
121662   if( isCopyTerm ){
121663     if( nTerm>pWriter->nMalloc ){
121664       char *zNew = sqlite3_realloc(pWriter->zMalloc, nTerm*2);
121665       if( !zNew ){
121666         return SQLITE_NOMEM;
121667       }
121668       pWriter->nMalloc = nTerm*2;
121669       pWriter->zMalloc = zNew;
121670       pWriter->zTerm = zNew;
121671     }
121672     assert( pWriter->zTerm==pWriter->zMalloc );
121673     memcpy(pWriter->zTerm, zTerm, nTerm);
121674   }else{
121675     pWriter->zTerm = (char *)zTerm;
121676   }
121677   pWriter->nTerm = nTerm;
121678 
121679   return SQLITE_OK;
121680 }
121681 
121682 /*
121683 ** Flush all data associated with the SegmentWriter object pWriter to the
121684 ** database. This function must be called after all terms have been added
121685 ** to the segment using fts3SegWriterAdd(). If successful, SQLITE_OK is
121686 ** returned. Otherwise, an SQLite error code.
121687 */
121688 static int fts3SegWriterFlush(
121689   Fts3Table *p,                   /* Virtual table handle */
121690   SegmentWriter *pWriter,         /* SegmentWriter to flush to the db */
121691   int iLevel,                     /* Value for 'level' column of %_segdir */
121692   int iIdx                        /* Value for 'idx' column of %_segdir */
121693 ){
121694   int rc;                         /* Return code */
121695   if( pWriter->pTree ){
121696     sqlite3_int64 iLast = 0;      /* Largest block id written to database */
121697     sqlite3_int64 iLastLeaf;      /* Largest leaf block id written to db */
121698     char *zRoot = NULL;           /* Pointer to buffer containing root node */
121699     int nRoot = 0;                /* Size of buffer zRoot */
121700 
121701     iLastLeaf = pWriter->iFree;
121702     rc = fts3WriteSegment(p, pWriter->iFree++, pWriter->aData, pWriter->nData);
121703     if( rc==SQLITE_OK ){
121704       rc = fts3NodeWrite(p, pWriter->pTree, 1,
121705           pWriter->iFirst, pWriter->iFree, &iLast, &zRoot, &nRoot);
121706     }
121707     if( rc==SQLITE_OK ){
121708       rc = fts3WriteSegdir(
121709           p, iLevel, iIdx, pWriter->iFirst, iLastLeaf, iLast, zRoot, nRoot);
121710     }
121711   }else{
121712     /* The entire tree fits on the root node. Write it to the segdir table. */
121713     rc = fts3WriteSegdir(
121714         p, iLevel, iIdx, 0, 0, 0, pWriter->aData, pWriter->nData);
121715   }
121716   return rc;
121717 }
121718 
121719 /*
121720 ** Release all memory held by the SegmentWriter object passed as the
121721 ** first argument.
121722 */
121723 static void fts3SegWriterFree(SegmentWriter *pWriter){
121724   if( pWriter ){
121725     sqlite3_free(pWriter->aData);
121726     sqlite3_free(pWriter->zMalloc);
121727     fts3NodeFree(pWriter->pTree);
121728     sqlite3_free(pWriter);
121729   }
121730 }
121731 
121732 /*
121733 ** The first value in the apVal[] array is assumed to contain an integer.
121734 ** This function tests if there exist any documents with docid values that
121735 ** are different from that integer. i.e. if deleting the document with docid
121736 ** pRowid would mean the FTS3 table were empty.
121737 **
121738 ** If successful, *pisEmpty is set to true if the table is empty except for
121739 ** document pRowid, or false otherwise, and SQLITE_OK is returned. If an
121740 ** error occurs, an SQLite error code is returned.
121741 */
121742 static int fts3IsEmpty(Fts3Table *p, sqlite3_value *pRowid, int *pisEmpty){
121743   sqlite3_stmt *pStmt;
121744   int rc;
121745   rc = fts3SqlStmt(p, SQL_IS_EMPTY, &pStmt, &pRowid);
121746   if( rc==SQLITE_OK ){
121747     if( SQLITE_ROW==sqlite3_step(pStmt) ){
121748       *pisEmpty = sqlite3_column_int(pStmt, 0);
121749     }
121750     rc = sqlite3_reset(pStmt);
121751   }
121752   return rc;
121753 }
121754 
121755 /*
121756 ** Set *pnMax to the largest segment level in the database for the index
121757 ** iIndex.
121758 **
121759 ** Segment levels are stored in the 'level' column of the %_segdir table.
121760 **
121761 ** Return SQLITE_OK if successful, or an SQLite error code if not.
121762 */
121763 static int fts3SegmentMaxLevel(Fts3Table *p, int iIndex, int *pnMax){
121764   sqlite3_stmt *pStmt;
121765   int rc;
121766   assert( iIndex>=0 && iIndex<p->nIndex );
121767 
121768   /* Set pStmt to the compiled version of:
121769   **
121770   **   SELECT max(level) FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?
121771   **
121772   ** (1024 is actually the value of macro FTS3_SEGDIR_PREFIXLEVEL_STR).
121773   */
121774   rc = fts3SqlStmt(p, SQL_SELECT_SEGDIR_MAX_LEVEL, &pStmt, 0);
121775   if( rc!=SQLITE_OK ) return rc;
121776   sqlite3_bind_int(pStmt, 1, iIndex*FTS3_SEGDIR_MAXLEVEL);
121777   sqlite3_bind_int(pStmt, 2, (iIndex+1)*FTS3_SEGDIR_MAXLEVEL - 1);
121778   if( SQLITE_ROW==sqlite3_step(pStmt) ){
121779     *pnMax = sqlite3_column_int(pStmt, 0);
121780   }
121781   return sqlite3_reset(pStmt);
121782 }
121783 
121784 /*
121785 ** This function is used after merging multiple segments into a single large
121786 ** segment to delete the old, now redundant, segment b-trees. Specifically,
121787 ** it:
121788 **
121789 **   1) Deletes all %_segments entries for the segments associated with
121790 **      each of the SegReader objects in the array passed as the third
121791 **      argument, and
121792 **
121793 **   2) deletes all %_segdir entries with level iLevel, or all %_segdir
121794 **      entries regardless of level if (iLevel<0).
121795 **
121796 ** SQLITE_OK is returned if successful, otherwise an SQLite error code.
121797 */
121798 static int fts3DeleteSegdir(
121799   Fts3Table *p,                   /* Virtual table handle */
121800   int iIndex,                     /* Index for p->aIndex */
121801   int iLevel,                     /* Level of %_segdir entries to delete */
121802   Fts3SegReader **apSegment,      /* Array of SegReader objects */
121803   int nReader                     /* Size of array apSegment */
121804 ){
121805   int rc;                         /* Return Code */
121806   int i;                          /* Iterator variable */
121807   sqlite3_stmt *pDelete;          /* SQL statement to delete rows */
121808 
121809   rc = fts3SqlStmt(p, SQL_DELETE_SEGMENTS_RANGE, &pDelete, 0);
121810   for(i=0; rc==SQLITE_OK && i<nReader; i++){
121811     Fts3SegReader *pSegment = apSegment[i];
121812     if( pSegment->iStartBlock ){
121813       sqlite3_bind_int64(pDelete, 1, pSegment->iStartBlock);
121814       sqlite3_bind_int64(pDelete, 2, pSegment->iEndBlock);
121815       sqlite3_step(pDelete);
121816       rc = sqlite3_reset(pDelete);
121817     }
121818   }
121819   if( rc!=SQLITE_OK ){
121820     return rc;
121821   }
121822 
121823   assert( iLevel>=0 || iLevel==FTS3_SEGCURSOR_ALL );
121824   if( iLevel==FTS3_SEGCURSOR_ALL ){
121825     rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_RANGE, &pDelete, 0);
121826     if( rc==SQLITE_OK ){
121827       sqlite3_bind_int(pDelete, 1, iIndex*FTS3_SEGDIR_MAXLEVEL);
121828       sqlite3_bind_int(pDelete, 2, (iIndex+1) * FTS3_SEGDIR_MAXLEVEL - 1);
121829     }
121830   }else{
121831     rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_LEVEL, &pDelete, 0);
121832     if( rc==SQLITE_OK ){
121833       sqlite3_bind_int(pDelete, 1, iIndex*FTS3_SEGDIR_MAXLEVEL + iLevel);
121834     }
121835   }
121836 
121837   if( rc==SQLITE_OK ){
121838     sqlite3_step(pDelete);
121839     rc = sqlite3_reset(pDelete);
121840   }
121841 
121842   return rc;
121843 }
121844 
121845 /*
121846 ** When this function is called, buffer *ppList (size *pnList bytes) contains
121847 ** a position list that may (or may not) feature multiple columns. This
121848 ** function adjusts the pointer *ppList and the length *pnList so that they
121849 ** identify the subset of the position list that corresponds to column iCol.
121850 **
121851 ** If there are no entries in the input position list for column iCol, then
121852 ** *pnList is set to zero before returning.
121853 */
121854 static void fts3ColumnFilter(
121855   int iCol,                       /* Column to filter on */
121856   char **ppList,                  /* IN/OUT: Pointer to position list */
121857   int *pnList                     /* IN/OUT: Size of buffer *ppList in bytes */
121858 ){
121859   char *pList = *ppList;
121860   int nList = *pnList;
121861   char *pEnd = &pList[nList];
121862   int iCurrent = 0;
121863   char *p = pList;
121864 
121865   assert( iCol>=0 );
121866   while( 1 ){
121867     char c = 0;
121868     while( p<pEnd && (c | *p)&0xFE ) c = *p++ & 0x80;
121869 
121870     if( iCol==iCurrent ){
121871       nList = (int)(p - pList);
121872       break;
121873     }
121874 
121875     nList -= (int)(p - pList);
121876     pList = p;
121877     if( nList==0 ){
121878       break;
121879     }
121880     p = &pList[1];
121881     p += sqlite3Fts3GetVarint32(p, &iCurrent);
121882   }
121883 
121884   *ppList = pList;
121885   *pnList = nList;
121886 }
121887 
121888 /*
121889 ** Cache data in the Fts3MultiSegReader.aBuffer[] buffer (overwriting any
121890 ** existing data). Grow the buffer if required.
121891 **
121892 ** If successful, return SQLITE_OK. Otherwise, if an OOM error is encountered
121893 ** trying to resize the buffer, return SQLITE_NOMEM.
121894 */
121895 static int fts3MsrBufferData(
121896   Fts3MultiSegReader *pMsr,       /* Multi-segment-reader handle */
121897   char *pList,
121898   int nList
121899 ){
121900   if( nList>pMsr->nBuffer ){
121901     char *pNew;
121902     pMsr->nBuffer = nList*2;
121903     pNew = (char *)sqlite3_realloc(pMsr->aBuffer, pMsr->nBuffer);
121904     if( !pNew ) return SQLITE_NOMEM;
121905     pMsr->aBuffer = pNew;
121906   }
121907 
121908   memcpy(pMsr->aBuffer, pList, nList);
121909   return SQLITE_OK;
121910 }
121911 
121912 SQLITE_PRIVATE int sqlite3Fts3MsrIncrNext(
121913   Fts3Table *p,                   /* Virtual table handle */
121914   Fts3MultiSegReader *pMsr,       /* Multi-segment-reader handle */
121915   sqlite3_int64 *piDocid,         /* OUT: Docid value */
121916   char **paPoslist,               /* OUT: Pointer to position list */
121917   int *pnPoslist                  /* OUT: Size of position list in bytes */
121918 ){
121919   int nMerge = pMsr->nAdvance;
121920   Fts3SegReader **apSegment = pMsr->apSegment;
121921   int (*xCmp)(Fts3SegReader *, Fts3SegReader *) = (
121922     p->bDescIdx ? fts3SegReaderDoclistCmpRev : fts3SegReaderDoclistCmp
121923   );
121924 
121925   if( nMerge==0 ){
121926     *paPoslist = 0;
121927     return SQLITE_OK;
121928   }
121929 
121930   while( 1 ){
121931     Fts3SegReader *pSeg;
121932     pSeg = pMsr->apSegment[0];
121933 
121934     if( pSeg->pOffsetList==0 ){
121935       *paPoslist = 0;
121936       break;
121937     }else{
121938       int rc;
121939       char *pList;
121940       int nList;
121941       int j;
121942       sqlite3_int64 iDocid = apSegment[0]->iDocid;
121943 
121944       rc = fts3SegReaderNextDocid(p, apSegment[0], &pList, &nList);
121945       j = 1;
121946       while( rc==SQLITE_OK
121947         && j<nMerge
121948         && apSegment[j]->pOffsetList
121949         && apSegment[j]->iDocid==iDocid
121950       ){
121951         rc = fts3SegReaderNextDocid(p, apSegment[j], 0, 0);
121952         j++;
121953       }
121954       if( rc!=SQLITE_OK ) return rc;
121955       fts3SegReaderSort(pMsr->apSegment, nMerge, j, xCmp);
121956 
121957       if( pMsr->iColFilter>=0 ){
121958         fts3ColumnFilter(pMsr->iColFilter, &pList, &nList);
121959       }
121960 
121961       if( nList>0 ){
121962         if( fts3SegReaderIsPending(apSegment[0]) ){
121963           rc = fts3MsrBufferData(pMsr, pList, nList+1);
121964           if( rc!=SQLITE_OK ) return rc;
121965           *paPoslist = pMsr->aBuffer;
121966           assert( (pMsr->aBuffer[nList] & 0xFE)==0x00 );
121967         }else{
121968           *paPoslist = pList;
121969         }
121970         *piDocid = iDocid;
121971         *pnPoslist = nList;
121972         break;
121973       }
121974     }
121975   }
121976 
121977   return SQLITE_OK;
121978 }
121979 
121980 static int fts3SegReaderStart(
121981   Fts3Table *p,                   /* Virtual table handle */
121982   Fts3MultiSegReader *pCsr,       /* Cursor object */
121983   const char *zTerm,              /* Term searched for (or NULL) */
121984   int nTerm                       /* Length of zTerm in bytes */
121985 ){
121986   int i;
121987   int nSeg = pCsr->nSegment;
121988 
121989   /* If the Fts3SegFilter defines a specific term (or term prefix) to search
121990   ** for, then advance each segment iterator until it points to a term of
121991   ** equal or greater value than the specified term. This prevents many
121992   ** unnecessary merge/sort operations for the case where single segment
121993   ** b-tree leaf nodes contain more than one term.
121994   */
121995   for(i=0; pCsr->bRestart==0 && i<pCsr->nSegment; i++){
121996     Fts3SegReader *pSeg = pCsr->apSegment[i];
121997     do {
121998       int rc = fts3SegReaderNext(p, pSeg, 0);
121999       if( rc!=SQLITE_OK ) return rc;
122000     }while( zTerm && fts3SegReaderTermCmp(pSeg, zTerm, nTerm)<0 );
122001   }
122002   fts3SegReaderSort(pCsr->apSegment, nSeg, nSeg, fts3SegReaderCmp);
122003 
122004   return SQLITE_OK;
122005 }
122006 
122007 SQLITE_PRIVATE int sqlite3Fts3SegReaderStart(
122008   Fts3Table *p,                   /* Virtual table handle */
122009   Fts3MultiSegReader *pCsr,       /* Cursor object */
122010   Fts3SegFilter *pFilter          /* Restrictions on range of iteration */
122011 ){
122012   pCsr->pFilter = pFilter;
122013   return fts3SegReaderStart(p, pCsr, pFilter->zTerm, pFilter->nTerm);
122014 }
122015 
122016 SQLITE_PRIVATE int sqlite3Fts3MsrIncrStart(
122017   Fts3Table *p,                   /* Virtual table handle */
122018   Fts3MultiSegReader *pCsr,       /* Cursor object */
122019   int iCol,                       /* Column to match on. */
122020   const char *zTerm,              /* Term to iterate through a doclist for */
122021   int nTerm                       /* Number of bytes in zTerm */
122022 ){
122023   int i;
122024   int rc;
122025   int nSegment = pCsr->nSegment;
122026   int (*xCmp)(Fts3SegReader *, Fts3SegReader *) = (
122027     p->bDescIdx ? fts3SegReaderDoclistCmpRev : fts3SegReaderDoclistCmp
122028   );
122029 
122030   assert( pCsr->pFilter==0 );
122031   assert( zTerm && nTerm>0 );
122032 
122033   /* Advance each segment iterator until it points to the term zTerm/nTerm. */
122034   rc = fts3SegReaderStart(p, pCsr, zTerm, nTerm);
122035   if( rc!=SQLITE_OK ) return rc;
122036 
122037   /* Determine how many of the segments actually point to zTerm/nTerm. */
122038   for(i=0; i<nSegment; i++){
122039     Fts3SegReader *pSeg = pCsr->apSegment[i];
122040     if( !pSeg->aNode || fts3SegReaderTermCmp(pSeg, zTerm, nTerm) ){
122041       break;
122042     }
122043   }
122044   pCsr->nAdvance = i;
122045 
122046   /* Advance each of the segments to point to the first docid. */
122047   for(i=0; i<pCsr->nAdvance; i++){
122048     rc = fts3SegReaderFirstDocid(p, pCsr->apSegment[i]);
122049     if( rc!=SQLITE_OK ) return rc;
122050   }
122051   fts3SegReaderSort(pCsr->apSegment, i, i, xCmp);
122052 
122053   assert( iCol<0 || iCol<p->nColumn );
122054   pCsr->iColFilter = iCol;
122055 
122056   return SQLITE_OK;
122057 }
122058 
122059 /*
122060 ** This function is called on a MultiSegReader that has been started using
122061 ** sqlite3Fts3MsrIncrStart(). One or more calls to MsrIncrNext() may also
122062 ** have been made. Calling this function puts the MultiSegReader in such
122063 ** a state that if the next two calls are:
122064 **
122065 **   sqlite3Fts3SegReaderStart()
122066 **   sqlite3Fts3SegReaderStep()
122067 **
122068 ** then the entire doclist for the term is available in
122069 ** MultiSegReader.aDoclist/nDoclist.
122070 */
122071 SQLITE_PRIVATE int sqlite3Fts3MsrIncrRestart(Fts3MultiSegReader *pCsr){
122072   int i;                          /* Used to iterate through segment-readers */
122073 
122074   assert( pCsr->zTerm==0 );
122075   assert( pCsr->nTerm==0 );
122076   assert( pCsr->aDoclist==0 );
122077   assert( pCsr->nDoclist==0 );
122078 
122079   pCsr->nAdvance = 0;
122080   pCsr->bRestart = 1;
122081   for(i=0; i<pCsr->nSegment; i++){
122082     pCsr->apSegment[i]->pOffsetList = 0;
122083     pCsr->apSegment[i]->nOffsetList = 0;
122084     pCsr->apSegment[i]->iDocid = 0;
122085   }
122086 
122087   return SQLITE_OK;
122088 }
122089 
122090 
122091 SQLITE_PRIVATE int sqlite3Fts3SegReaderStep(
122092   Fts3Table *p,                   /* Virtual table handle */
122093   Fts3MultiSegReader *pCsr        /* Cursor object */
122094 ){
122095   int rc = SQLITE_OK;
122096 
122097   int isIgnoreEmpty =  (pCsr->pFilter->flags & FTS3_SEGMENT_IGNORE_EMPTY);
122098   int isRequirePos =   (pCsr->pFilter->flags & FTS3_SEGMENT_REQUIRE_POS);
122099   int isColFilter =    (pCsr->pFilter->flags & FTS3_SEGMENT_COLUMN_FILTER);
122100   int isPrefix =       (pCsr->pFilter->flags & FTS3_SEGMENT_PREFIX);
122101   int isScan =         (pCsr->pFilter->flags & FTS3_SEGMENT_SCAN);
122102 
122103   Fts3SegReader **apSegment = pCsr->apSegment;
122104   int nSegment = pCsr->nSegment;
122105   Fts3SegFilter *pFilter = pCsr->pFilter;
122106   int (*xCmp)(Fts3SegReader *, Fts3SegReader *) = (
122107     p->bDescIdx ? fts3SegReaderDoclistCmpRev : fts3SegReaderDoclistCmp
122108   );
122109 
122110   if( pCsr->nSegment==0 ) return SQLITE_OK;
122111 
122112   do {
122113     int nMerge;
122114     int i;
122115 
122116     /* Advance the first pCsr->nAdvance entries in the apSegment[] array
122117     ** forward. Then sort the list in order of current term again.
122118     */
122119     for(i=0; i<pCsr->nAdvance; i++){
122120       rc = fts3SegReaderNext(p, apSegment[i], 0);
122121       if( rc!=SQLITE_OK ) return rc;
122122     }
122123     fts3SegReaderSort(apSegment, nSegment, pCsr->nAdvance, fts3SegReaderCmp);
122124     pCsr->nAdvance = 0;
122125 
122126     /* If all the seg-readers are at EOF, we're finished. return SQLITE_OK. */
122127     assert( rc==SQLITE_OK );
122128     if( apSegment[0]->aNode==0 ) break;
122129 
122130     pCsr->nTerm = apSegment[0]->nTerm;
122131     pCsr->zTerm = apSegment[0]->zTerm;
122132 
122133     /* If this is a prefix-search, and if the term that apSegment[0] points
122134     ** to does not share a suffix with pFilter->zTerm/nTerm, then all
122135     ** required callbacks have been made. In this case exit early.
122136     **
122137     ** Similarly, if this is a search for an exact match, and the first term
122138     ** of segment apSegment[0] is not a match, exit early.
122139     */
122140     if( pFilter->zTerm && !isScan ){
122141       if( pCsr->nTerm<pFilter->nTerm
122142        || (!isPrefix && pCsr->nTerm>pFilter->nTerm)
122143        || memcmp(pCsr->zTerm, pFilter->zTerm, pFilter->nTerm)
122144       ){
122145         break;
122146       }
122147     }
122148 
122149     nMerge = 1;
122150     while( nMerge<nSegment
122151         && apSegment[nMerge]->aNode
122152         && apSegment[nMerge]->nTerm==pCsr->nTerm
122153         && 0==memcmp(pCsr->zTerm, apSegment[nMerge]->zTerm, pCsr->nTerm)
122154     ){
122155       nMerge++;
122156     }
122157 
122158     assert( isIgnoreEmpty || (isRequirePos && !isColFilter) );
122159     if( nMerge==1
122160      && !isIgnoreEmpty
122161      && (p->bDescIdx==0 || fts3SegReaderIsPending(apSegment[0])==0)
122162     ){
122163       pCsr->nDoclist = apSegment[0]->nDoclist;
122164       if( fts3SegReaderIsPending(apSegment[0]) ){
122165         rc = fts3MsrBufferData(pCsr, apSegment[0]->aDoclist, pCsr->nDoclist);
122166         pCsr->aDoclist = pCsr->aBuffer;
122167       }else{
122168         pCsr->aDoclist = apSegment[0]->aDoclist;
122169       }
122170       if( rc==SQLITE_OK ) rc = SQLITE_ROW;
122171     }else{
122172       int nDoclist = 0;           /* Size of doclist */
122173       sqlite3_int64 iPrev = 0;    /* Previous docid stored in doclist */
122174 
122175       /* The current term of the first nMerge entries in the array
122176       ** of Fts3SegReader objects is the same. The doclists must be merged
122177       ** and a single term returned with the merged doclist.
122178       */
122179       for(i=0; i<nMerge; i++){
122180         fts3SegReaderFirstDocid(p, apSegment[i]);
122181       }
122182       fts3SegReaderSort(apSegment, nMerge, nMerge, xCmp);
122183       while( apSegment[0]->pOffsetList ){
122184         int j;                    /* Number of segments that share a docid */
122185         char *pList;
122186         int nList;
122187         int nByte;
122188         sqlite3_int64 iDocid = apSegment[0]->iDocid;
122189         fts3SegReaderNextDocid(p, apSegment[0], &pList, &nList);
122190         j = 1;
122191         while( j<nMerge
122192             && apSegment[j]->pOffsetList
122193             && apSegment[j]->iDocid==iDocid
122194         ){
122195           fts3SegReaderNextDocid(p, apSegment[j], 0, 0);
122196           j++;
122197         }
122198 
122199         if( isColFilter ){
122200           fts3ColumnFilter(pFilter->iCol, &pList, &nList);
122201         }
122202 
122203         if( !isIgnoreEmpty || nList>0 ){
122204 
122205           /* Calculate the 'docid' delta value to write into the merged
122206           ** doclist. */
122207           sqlite3_int64 iDelta;
122208           if( p->bDescIdx && nDoclist>0 ){
122209             iDelta = iPrev - iDocid;
122210           }else{
122211             iDelta = iDocid - iPrev;
122212           }
122213           assert( iDelta>0 || (nDoclist==0 && iDelta==iDocid) );
122214           assert( nDoclist>0 || iDelta==iDocid );
122215 
122216           nByte = sqlite3Fts3VarintLen(iDelta) + (isRequirePos?nList+1:0);
122217           if( nDoclist+nByte>pCsr->nBuffer ){
122218             char *aNew;
122219             pCsr->nBuffer = (nDoclist+nByte)*2;
122220             aNew = sqlite3_realloc(pCsr->aBuffer, pCsr->nBuffer);
122221             if( !aNew ){
122222               return SQLITE_NOMEM;
122223             }
122224             pCsr->aBuffer = aNew;
122225           }
122226           nDoclist += sqlite3Fts3PutVarint(&pCsr->aBuffer[nDoclist], iDelta);
122227           iPrev = iDocid;
122228           if( isRequirePos ){
122229             memcpy(&pCsr->aBuffer[nDoclist], pList, nList);
122230             nDoclist += nList;
122231             pCsr->aBuffer[nDoclist++] = '\0';
122232           }
122233         }
122234 
122235         fts3SegReaderSort(apSegment, nMerge, j, xCmp);
122236       }
122237       if( nDoclist>0 ){
122238         pCsr->aDoclist = pCsr->aBuffer;
122239         pCsr->nDoclist = nDoclist;
122240         rc = SQLITE_ROW;
122241       }
122242     }
122243     pCsr->nAdvance = nMerge;
122244   }while( rc==SQLITE_OK );
122245 
122246   return rc;
122247 }
122248 
122249 
122250 SQLITE_PRIVATE void sqlite3Fts3SegReaderFinish(
122251   Fts3MultiSegReader *pCsr       /* Cursor object */
122252 ){
122253   if( pCsr ){
122254     int i;
122255     for(i=0; i<pCsr->nSegment; i++){
122256       sqlite3Fts3SegReaderFree(pCsr->apSegment[i]);
122257     }
122258     sqlite3_free(pCsr->apSegment);
122259     sqlite3_free(pCsr->aBuffer);
122260 
122261     pCsr->nSegment = 0;
122262     pCsr->apSegment = 0;
122263     pCsr->aBuffer = 0;
122264   }
122265 }
122266 
122267 /*
122268 ** Merge all level iLevel segments in the database into a single
122269 ** iLevel+1 segment. Or, if iLevel<0, merge all segments into a
122270 ** single segment with a level equal to the numerically largest level
122271 ** currently present in the database.
122272 **
122273 ** If this function is called with iLevel<0, but there is only one
122274 ** segment in the database, SQLITE_DONE is returned immediately.
122275 ** Otherwise, if successful, SQLITE_OK is returned. If an error occurs,
122276 ** an SQLite error code is returned.
122277 */
122278 static int fts3SegmentMerge(Fts3Table *p, int iIndex, int iLevel){
122279   int rc;                         /* Return code */
122280   int iIdx = 0;                   /* Index of new segment */
122281   int iNewLevel = 0;              /* Level/index to create new segment at */
122282   SegmentWriter *pWriter = 0;     /* Used to write the new, merged, segment */
122283   Fts3SegFilter filter;           /* Segment term filter condition */
122284   Fts3MultiSegReader csr;        /* Cursor to iterate through level(s) */
122285   int bIgnoreEmpty = 0;           /* True to ignore empty segments */
122286 
122287   assert( iLevel==FTS3_SEGCURSOR_ALL
122288        || iLevel==FTS3_SEGCURSOR_PENDING
122289        || iLevel>=0
122290   );
122291   assert( iLevel<FTS3_SEGDIR_MAXLEVEL );
122292   assert( iIndex>=0 && iIndex<p->nIndex );
122293 
122294   rc = sqlite3Fts3SegReaderCursor(p, iIndex, iLevel, 0, 0, 1, 0, &csr);
122295   if( rc!=SQLITE_OK || csr.nSegment==0 ) goto finished;
122296 
122297   if( iLevel==FTS3_SEGCURSOR_ALL ){
122298     /* This call is to merge all segments in the database to a single
122299     ** segment. The level of the new segment is equal to the the numerically
122300     ** greatest segment level currently present in the database for this
122301     ** index. The idx of the new segment is always 0.  */
122302     if( csr.nSegment==1 ){
122303       rc = SQLITE_DONE;
122304       goto finished;
122305     }
122306     rc = fts3SegmentMaxLevel(p, iIndex, &iNewLevel);
122307     bIgnoreEmpty = 1;
122308 
122309   }else if( iLevel==FTS3_SEGCURSOR_PENDING ){
122310     iNewLevel = iIndex * FTS3_SEGDIR_MAXLEVEL;
122311     rc = fts3AllocateSegdirIdx(p, iIndex, 0, &iIdx);
122312   }else{
122313     /* This call is to merge all segments at level iLevel. find the next
122314     ** available segment index at level iLevel+1. The call to
122315     ** fts3AllocateSegdirIdx() will merge the segments at level iLevel+1 to
122316     ** a single iLevel+2 segment if necessary.  */
122317     rc = fts3AllocateSegdirIdx(p, iIndex, iLevel+1, &iIdx);
122318     iNewLevel = iIndex * FTS3_SEGDIR_MAXLEVEL + iLevel+1;
122319   }
122320   if( rc!=SQLITE_OK ) goto finished;
122321   assert( csr.nSegment>0 );
122322   assert( iNewLevel>=(iIndex*FTS3_SEGDIR_MAXLEVEL) );
122323   assert( iNewLevel<((iIndex+1)*FTS3_SEGDIR_MAXLEVEL) );
122324 
122325   memset(&filter, 0, sizeof(Fts3SegFilter));
122326   filter.flags = FTS3_SEGMENT_REQUIRE_POS;
122327   filter.flags |= (bIgnoreEmpty ? FTS3_SEGMENT_IGNORE_EMPTY : 0);
122328 
122329   rc = sqlite3Fts3SegReaderStart(p, &csr, &filter);
122330   while( SQLITE_OK==rc ){
122331     rc = sqlite3Fts3SegReaderStep(p, &csr);
122332     if( rc!=SQLITE_ROW ) break;
122333     rc = fts3SegWriterAdd(p, &pWriter, 1,
122334         csr.zTerm, csr.nTerm, csr.aDoclist, csr.nDoclist);
122335   }
122336   if( rc!=SQLITE_OK ) goto finished;
122337   assert( pWriter );
122338 
122339   if( iLevel!=FTS3_SEGCURSOR_PENDING ){
122340     rc = fts3DeleteSegdir(p, iIndex, iLevel, csr.apSegment, csr.nSegment);
122341     if( rc!=SQLITE_OK ) goto finished;
122342   }
122343   rc = fts3SegWriterFlush(p, pWriter, iNewLevel, iIdx);
122344 
122345  finished:
122346   fts3SegWriterFree(pWriter);
122347   sqlite3Fts3SegReaderFinish(&csr);
122348   return rc;
122349 }
122350 
122351 
122352 /*
122353 ** Flush the contents of pendingTerms to level 0 segments.
122354 */
122355 SQLITE_PRIVATE int sqlite3Fts3PendingTermsFlush(Fts3Table *p){
122356   int rc = SQLITE_OK;
122357   int i;
122358   for(i=0; rc==SQLITE_OK && i<p->nIndex; i++){
122359     rc = fts3SegmentMerge(p, i, FTS3_SEGCURSOR_PENDING);
122360     if( rc==SQLITE_DONE ) rc = SQLITE_OK;
122361   }
122362   sqlite3Fts3PendingTermsClear(p);
122363   return rc;
122364 }
122365 
122366 /*
122367 ** Encode N integers as varints into a blob.
122368 */
122369 static void fts3EncodeIntArray(
122370   int N,             /* The number of integers to encode */
122371   u32 *a,            /* The integer values */
122372   char *zBuf,        /* Write the BLOB here */
122373   int *pNBuf         /* Write number of bytes if zBuf[] used here */
122374 ){
122375   int i, j;
122376   for(i=j=0; i<N; i++){
122377     j += sqlite3Fts3PutVarint(&zBuf[j], (sqlite3_int64)a[i]);
122378   }
122379   *pNBuf = j;
122380 }
122381 
122382 /*
122383 ** Decode a blob of varints into N integers
122384 */
122385 static void fts3DecodeIntArray(
122386   int N,             /* The number of integers to decode */
122387   u32 *a,            /* Write the integer values */
122388   const char *zBuf,  /* The BLOB containing the varints */
122389   int nBuf           /* size of the BLOB */
122390 ){
122391   int i, j;
122392   UNUSED_PARAMETER(nBuf);
122393   for(i=j=0; i<N; i++){
122394     sqlite3_int64 x;
122395     j += sqlite3Fts3GetVarint(&zBuf[j], &x);
122396     assert(j<=nBuf);
122397     a[i] = (u32)(x & 0xffffffff);
122398   }
122399 }
122400 
122401 /*
122402 ** Insert the sizes (in tokens) for each column of the document
122403 ** with docid equal to p->iPrevDocid.  The sizes are encoded as
122404 ** a blob of varints.
122405 */
122406 static void fts3InsertDocsize(
122407   int *pRC,         /* Result code */
122408   Fts3Table *p,     /* Table into which to insert */
122409   u32 *aSz          /* Sizes of each column */
122410 ){
122411   char *pBlob;             /* The BLOB encoding of the document size */
122412   int nBlob;               /* Number of bytes in the BLOB */
122413   sqlite3_stmt *pStmt;     /* Statement used to insert the encoding */
122414   int rc;                  /* Result code from subfunctions */
122415 
122416   if( *pRC ) return;
122417   pBlob = sqlite3_malloc( 10*p->nColumn );
122418   if( pBlob==0 ){
122419     *pRC = SQLITE_NOMEM;
122420     return;
122421   }
122422   fts3EncodeIntArray(p->nColumn, aSz, pBlob, &nBlob);
122423   rc = fts3SqlStmt(p, SQL_REPLACE_DOCSIZE, &pStmt, 0);
122424   if( rc ){
122425     sqlite3_free(pBlob);
122426     *pRC = rc;
122427     return;
122428   }
122429   sqlite3_bind_int64(pStmt, 1, p->iPrevDocid);
122430   sqlite3_bind_blob(pStmt, 2, pBlob, nBlob, sqlite3_free);
122431   sqlite3_step(pStmt);
122432   *pRC = sqlite3_reset(pStmt);
122433 }
122434 
122435 /*
122436 ** Record 0 of the %_stat table contains a blob consisting of N varints,
122437 ** where N is the number of user defined columns in the fts3 table plus
122438 ** two. If nCol is the number of user defined columns, then values of the
122439 ** varints are set as follows:
122440 **
122441 **   Varint 0:       Total number of rows in the table.
122442 **
122443 **   Varint 1..nCol: For each column, the total number of tokens stored in
122444 **                   the column for all rows of the table.
122445 **
122446 **   Varint 1+nCol:  The total size, in bytes, of all text values in all
122447 **                   columns of all rows of the table.
122448 **
122449 */
122450 static void fts3UpdateDocTotals(
122451   int *pRC,                       /* The result code */
122452   Fts3Table *p,                   /* Table being updated */
122453   u32 *aSzIns,                    /* Size increases */
122454   u32 *aSzDel,                    /* Size decreases */
122455   int nChng                       /* Change in the number of documents */
122456 ){
122457   char *pBlob;             /* Storage for BLOB written into %_stat */
122458   int nBlob;               /* Size of BLOB written into %_stat */
122459   u32 *a;                  /* Array of integers that becomes the BLOB */
122460   sqlite3_stmt *pStmt;     /* Statement for reading and writing */
122461   int i;                   /* Loop counter */
122462   int rc;                  /* Result code from subfunctions */
122463 
122464   const int nStat = p->nColumn+2;
122465 
122466   if( *pRC ) return;
122467   a = sqlite3_malloc( (sizeof(u32)+10)*nStat );
122468   if( a==0 ){
122469     *pRC = SQLITE_NOMEM;
122470     return;
122471   }
122472   pBlob = (char*)&a[nStat];
122473   rc = fts3SqlStmt(p, SQL_SELECT_DOCTOTAL, &pStmt, 0);
122474   if( rc ){
122475     sqlite3_free(a);
122476     *pRC = rc;
122477     return;
122478   }
122479   if( sqlite3_step(pStmt)==SQLITE_ROW ){
122480     fts3DecodeIntArray(nStat, a,
122481          sqlite3_column_blob(pStmt, 0),
122482          sqlite3_column_bytes(pStmt, 0));
122483   }else{
122484     memset(a, 0, sizeof(u32)*(nStat) );
122485   }
122486   sqlite3_reset(pStmt);
122487   if( nChng<0 && a[0]<(u32)(-nChng) ){
122488     a[0] = 0;
122489   }else{
122490     a[0] += nChng;
122491   }
122492   for(i=0; i<p->nColumn+1; i++){
122493     u32 x = a[i+1];
122494     if( x+aSzIns[i] < aSzDel[i] ){
122495       x = 0;
122496     }else{
122497       x = x + aSzIns[i] - aSzDel[i];
122498     }
122499     a[i+1] = x;
122500   }
122501   fts3EncodeIntArray(nStat, a, pBlob, &nBlob);
122502   rc = fts3SqlStmt(p, SQL_REPLACE_DOCTOTAL, &pStmt, 0);
122503   if( rc ){
122504     sqlite3_free(a);
122505     *pRC = rc;
122506     return;
122507   }
122508   sqlite3_bind_blob(pStmt, 1, pBlob, nBlob, SQLITE_STATIC);
122509   sqlite3_step(pStmt);
122510   *pRC = sqlite3_reset(pStmt);
122511   sqlite3_free(a);
122512 }
122513 
122514 static int fts3DoOptimize(Fts3Table *p, int bReturnDone){
122515   int i;
122516   int bSeenDone = 0;
122517   int rc = SQLITE_OK;
122518   for(i=0; rc==SQLITE_OK && i<p->nIndex; i++){
122519     rc = fts3SegmentMerge(p, i, FTS3_SEGCURSOR_ALL);
122520     if( rc==SQLITE_DONE ){
122521       bSeenDone = 1;
122522       rc = SQLITE_OK;
122523     }
122524   }
122525   sqlite3Fts3SegmentsClose(p);
122526   sqlite3Fts3PendingTermsClear(p);
122527 
122528   return (rc==SQLITE_OK && bReturnDone && bSeenDone) ? SQLITE_DONE : rc;
122529 }
122530 
122531 /*
122532 ** Handle a 'special' INSERT of the form:
122533 **
122534 **   "INSERT INTO tbl(tbl) VALUES(<expr>)"
122535 **
122536 ** Argument pVal contains the result of <expr>. Currently the only
122537 ** meaningful value to insert is the text 'optimize'.
122538 */
122539 static int fts3SpecialInsert(Fts3Table *p, sqlite3_value *pVal){
122540   int rc;                         /* Return Code */
122541   const char *zVal = (const char *)sqlite3_value_text(pVal);
122542   int nVal = sqlite3_value_bytes(pVal);
122543 
122544   if( !zVal ){
122545     return SQLITE_NOMEM;
122546   }else if( nVal==8 && 0==sqlite3_strnicmp(zVal, "optimize", 8) ){
122547     rc = fts3DoOptimize(p, 0);
122548 #ifdef SQLITE_TEST
122549   }else if( nVal>9 && 0==sqlite3_strnicmp(zVal, "nodesize=", 9) ){
122550     p->nNodeSize = atoi(&zVal[9]);
122551     rc = SQLITE_OK;
122552   }else if( nVal>11 && 0==sqlite3_strnicmp(zVal, "maxpending=", 9) ){
122553     p->nMaxPendingData = atoi(&zVal[11]);
122554     rc = SQLITE_OK;
122555 #endif
122556   }else{
122557     rc = SQLITE_ERROR;
122558   }
122559 
122560   return rc;
122561 }
122562 
122563 /*
122564 ** Delete all cached deferred doclists. Deferred doclists are cached
122565 ** (allocated) by the sqlite3Fts3CacheDeferredDoclists() function.
122566 */
122567 SQLITE_PRIVATE void sqlite3Fts3FreeDeferredDoclists(Fts3Cursor *pCsr){
122568   Fts3DeferredToken *pDef;
122569   for(pDef=pCsr->pDeferred; pDef; pDef=pDef->pNext){
122570     fts3PendingListDelete(pDef->pList);
122571     pDef->pList = 0;
122572   }
122573 }
122574 
122575 /*
122576 ** Free all entries in the pCsr->pDeffered list. Entries are added to
122577 ** this list using sqlite3Fts3DeferToken().
122578 */
122579 SQLITE_PRIVATE void sqlite3Fts3FreeDeferredTokens(Fts3Cursor *pCsr){
122580   Fts3DeferredToken *pDef;
122581   Fts3DeferredToken *pNext;
122582   for(pDef=pCsr->pDeferred; pDef; pDef=pNext){
122583     pNext = pDef->pNext;
122584     fts3PendingListDelete(pDef->pList);
122585     sqlite3_free(pDef);
122586   }
122587   pCsr->pDeferred = 0;
122588 }
122589 
122590 /*
122591 ** Generate deferred-doclists for all tokens in the pCsr->pDeferred list
122592 ** based on the row that pCsr currently points to.
122593 **
122594 ** A deferred-doclist is like any other doclist with position information
122595 ** included, except that it only contains entries for a single row of the
122596 ** table, not for all rows.
122597 */
122598 SQLITE_PRIVATE int sqlite3Fts3CacheDeferredDoclists(Fts3Cursor *pCsr){
122599   int rc = SQLITE_OK;             /* Return code */
122600   if( pCsr->pDeferred ){
122601     int i;                        /* Used to iterate through table columns */
122602     sqlite3_int64 iDocid;         /* Docid of the row pCsr points to */
122603     Fts3DeferredToken *pDef;      /* Used to iterate through deferred tokens */
122604 
122605     Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
122606     sqlite3_tokenizer *pT = p->pTokenizer;
122607     sqlite3_tokenizer_module const *pModule = pT->pModule;
122608 
122609     assert( pCsr->isRequireSeek==0 );
122610     iDocid = sqlite3_column_int64(pCsr->pStmt, 0);
122611 
122612     for(i=0; i<p->nColumn && rc==SQLITE_OK; i++){
122613       const char *zText = (const char *)sqlite3_column_text(pCsr->pStmt, i+1);
122614       sqlite3_tokenizer_cursor *pTC = 0;
122615 
122616       rc = pModule->xOpen(pT, zText, -1, &pTC);
122617       while( rc==SQLITE_OK ){
122618         char const *zToken;       /* Buffer containing token */
122619         int nToken;               /* Number of bytes in token */
122620         int iDum1, iDum2;         /* Dummy variables */
122621         int iPos;                 /* Position of token in zText */
122622 
122623         pTC->pTokenizer = pT;
122624         rc = pModule->xNext(pTC, &zToken, &nToken, &iDum1, &iDum2, &iPos);
122625         for(pDef=pCsr->pDeferred; pDef && rc==SQLITE_OK; pDef=pDef->pNext){
122626           Fts3PhraseToken *pPT = pDef->pToken;
122627           if( (pDef->iCol>=p->nColumn || pDef->iCol==i)
122628            && (pPT->n==nToken || (pPT->isPrefix && pPT->n<nToken))
122629            && (0==memcmp(zToken, pPT->z, pPT->n))
122630           ){
122631             fts3PendingListAppend(&pDef->pList, iDocid, i, iPos, &rc);
122632           }
122633         }
122634       }
122635       if( pTC ) pModule->xClose(pTC);
122636       if( rc==SQLITE_DONE ) rc = SQLITE_OK;
122637     }
122638 
122639     for(pDef=pCsr->pDeferred; pDef && rc==SQLITE_OK; pDef=pDef->pNext){
122640       if( pDef->pList ){
122641         rc = fts3PendingListAppendVarint(&pDef->pList, 0);
122642       }
122643     }
122644   }
122645 
122646   return rc;
122647 }
122648 
122649 SQLITE_PRIVATE int sqlite3Fts3DeferredTokenList(
122650   Fts3DeferredToken *p,
122651   char **ppData,
122652   int *pnData
122653 ){
122654   char *pRet;
122655   int nSkip;
122656   sqlite3_int64 dummy;
122657 
122658   *ppData = 0;
122659   *pnData = 0;
122660 
122661   if( p->pList==0 ){
122662     return SQLITE_OK;
122663   }
122664 
122665   pRet = (char *)sqlite3_malloc(p->pList->nData);
122666   if( !pRet ) return SQLITE_NOMEM;
122667 
122668   nSkip = sqlite3Fts3GetVarint(p->pList->aData, &dummy);
122669   *pnData = p->pList->nData - nSkip;
122670   *ppData = pRet;
122671 
122672   memcpy(pRet, &p->pList->aData[nSkip], *pnData);
122673   return SQLITE_OK;
122674 }
122675 
122676 /*
122677 ** Add an entry for token pToken to the pCsr->pDeferred list.
122678 */
122679 SQLITE_PRIVATE int sqlite3Fts3DeferToken(
122680   Fts3Cursor *pCsr,               /* Fts3 table cursor */
122681   Fts3PhraseToken *pToken,        /* Token to defer */
122682   int iCol                        /* Column that token must appear in (or -1) */
122683 ){
122684   Fts3DeferredToken *pDeferred;
122685   pDeferred = sqlite3_malloc(sizeof(*pDeferred));
122686   if( !pDeferred ){
122687     return SQLITE_NOMEM;
122688   }
122689   memset(pDeferred, 0, sizeof(*pDeferred));
122690   pDeferred->pToken = pToken;
122691   pDeferred->pNext = pCsr->pDeferred;
122692   pDeferred->iCol = iCol;
122693   pCsr->pDeferred = pDeferred;
122694 
122695   assert( pToken->pDeferred==0 );
122696   pToken->pDeferred = pDeferred;
122697 
122698   return SQLITE_OK;
122699 }
122700 
122701 /*
122702 ** SQLite value pRowid contains the rowid of a row that may or may not be
122703 ** present in the FTS3 table. If it is, delete it and adjust the contents
122704 ** of subsiduary data structures accordingly.
122705 */
122706 static int fts3DeleteByRowid(
122707   Fts3Table *p,
122708   sqlite3_value *pRowid,
122709   int *pnDoc,
122710   u32 *aSzDel
122711 ){
122712   int isEmpty = 0;
122713   int rc = fts3IsEmpty(p, pRowid, &isEmpty);
122714   if( rc==SQLITE_OK ){
122715     if( isEmpty ){
122716       /* Deleting this row means the whole table is empty. In this case
122717       ** delete the contents of all three tables and throw away any
122718       ** data in the pendingTerms hash table.  */
122719       rc = fts3DeleteAll(p);
122720       *pnDoc = *pnDoc - 1;
122721     }else{
122722       sqlite3_int64 iRemove = sqlite3_value_int64(pRowid);
122723       rc = fts3PendingTermsDocid(p, iRemove);
122724       fts3DeleteTerms(&rc, p, pRowid, aSzDel);
122725       fts3SqlExec(&rc, p, SQL_DELETE_CONTENT, &pRowid);
122726       if( sqlite3_changes(p->db) ) *pnDoc = *pnDoc - 1;
122727       if( p->bHasDocsize ){
122728         fts3SqlExec(&rc, p, SQL_DELETE_DOCSIZE, &pRowid);
122729       }
122730     }
122731   }
122732 
122733   return rc;
122734 }
122735 
122736 /*
122737 ** This function does the work for the xUpdate method of FTS3 virtual
122738 ** tables.
122739 */
122740 SQLITE_PRIVATE int sqlite3Fts3UpdateMethod(
122741   sqlite3_vtab *pVtab,            /* FTS3 vtab object */
122742   int nArg,                       /* Size of argument array */
122743   sqlite3_value **apVal,          /* Array of arguments */
122744   sqlite_int64 *pRowid            /* OUT: The affected (or effected) rowid */
122745 ){
122746   Fts3Table *p = (Fts3Table *)pVtab;
122747   int rc = SQLITE_OK;             /* Return Code */
122748   int isRemove = 0;               /* True for an UPDATE or DELETE */
122749   sqlite3_int64 iRemove = 0;      /* Rowid removed by UPDATE or DELETE */
122750   u32 *aSzIns = 0;                /* Sizes of inserted documents */
122751   u32 *aSzDel;                    /* Sizes of deleted documents */
122752   int nChng = 0;                  /* Net change in number of documents */
122753   int bInsertDone = 0;
122754 
122755   assert( p->pSegments==0 );
122756 
122757   /* Check for a "special" INSERT operation. One of the form:
122758   **
122759   **   INSERT INTO xyz(xyz) VALUES('command');
122760   */
122761   if( nArg>1
122762    && sqlite3_value_type(apVal[0])==SQLITE_NULL
122763    && sqlite3_value_type(apVal[p->nColumn+2])!=SQLITE_NULL
122764   ){
122765     rc = fts3SpecialInsert(p, apVal[p->nColumn+2]);
122766     goto update_out;
122767   }
122768 
122769   /* Allocate space to hold the change in document sizes */
122770   aSzIns = sqlite3_malloc( sizeof(aSzIns[0])*(p->nColumn+1)*2 );
122771   if( aSzIns==0 ){
122772     rc = SQLITE_NOMEM;
122773     goto update_out;
122774   }
122775   aSzDel = &aSzIns[p->nColumn+1];
122776   memset(aSzIns, 0, sizeof(aSzIns[0])*(p->nColumn+1)*2);
122777 
122778   /* If this is an INSERT operation, or an UPDATE that modifies the rowid
122779   ** value, then this operation requires constraint handling.
122780   **
122781   ** If the on-conflict mode is REPLACE, this means that the existing row
122782   ** should be deleted from the database before inserting the new row. Or,
122783   ** if the on-conflict mode is other than REPLACE, then this method must
122784   ** detect the conflict and return SQLITE_CONSTRAINT before beginning to
122785   ** modify the database file.
122786   */
122787   if( nArg>1 ){
122788     /* Find the value object that holds the new rowid value. */
122789     sqlite3_value *pNewRowid = apVal[3+p->nColumn];
122790     if( sqlite3_value_type(pNewRowid)==SQLITE_NULL ){
122791       pNewRowid = apVal[1];
122792     }
122793 
122794     if( sqlite3_value_type(pNewRowid)!=SQLITE_NULL && (
122795         sqlite3_value_type(apVal[0])==SQLITE_NULL
122796      || sqlite3_value_int64(apVal[0])!=sqlite3_value_int64(pNewRowid)
122797     )){
122798       /* The new rowid is not NULL (in this case the rowid will be
122799       ** automatically assigned and there is no chance of a conflict), and
122800       ** the statement is either an INSERT or an UPDATE that modifies the
122801       ** rowid column. So if the conflict mode is REPLACE, then delete any
122802       ** existing row with rowid=pNewRowid.
122803       **
122804       ** Or, if the conflict mode is not REPLACE, insert the new record into
122805       ** the %_content table. If we hit the duplicate rowid constraint (or any
122806       ** other error) while doing so, return immediately.
122807       **
122808       ** This branch may also run if pNewRowid contains a value that cannot
122809       ** be losslessly converted to an integer. In this case, the eventual
122810       ** call to fts3InsertData() (either just below or further on in this
122811       ** function) will return SQLITE_MISMATCH. If fts3DeleteByRowid is
122812       ** invoked, it will delete zero rows (since no row will have
122813       ** docid=$pNewRowid if $pNewRowid is not an integer value).
122814       */
122815       if( sqlite3_vtab_on_conflict(p->db)==SQLITE_REPLACE ){
122816         rc = fts3DeleteByRowid(p, pNewRowid, &nChng, aSzDel);
122817       }else{
122818         rc = fts3InsertData(p, apVal, pRowid);
122819         bInsertDone = 1;
122820       }
122821     }
122822   }
122823   if( rc!=SQLITE_OK ){
122824     goto update_out;
122825   }
122826 
122827   /* If this is a DELETE or UPDATE operation, remove the old record. */
122828   if( sqlite3_value_type(apVal[0])!=SQLITE_NULL ){
122829     assert( sqlite3_value_type(apVal[0])==SQLITE_INTEGER );
122830     rc = fts3DeleteByRowid(p, apVal[0], &nChng, aSzDel);
122831     isRemove = 1;
122832     iRemove = sqlite3_value_int64(apVal[0]);
122833   }
122834 
122835   /* If this is an INSERT or UPDATE operation, insert the new record. */
122836   if( nArg>1 && rc==SQLITE_OK ){
122837     if( bInsertDone==0 ){
122838       rc = fts3InsertData(p, apVal, pRowid);
122839       if( rc==SQLITE_CONSTRAINT ) rc = SQLITE_CORRUPT_VTAB;
122840     }
122841     if( rc==SQLITE_OK && (!isRemove || *pRowid!=iRemove) ){
122842       rc = fts3PendingTermsDocid(p, *pRowid);
122843     }
122844     if( rc==SQLITE_OK ){
122845       rc = fts3InsertTerms(p, apVal, aSzIns);
122846     }
122847     if( p->bHasDocsize ){
122848       fts3InsertDocsize(&rc, p, aSzIns);
122849     }
122850     nChng++;
122851   }
122852 
122853   if( p->bHasStat ){
122854     fts3UpdateDocTotals(&rc, p, aSzIns, aSzDel, nChng);
122855   }
122856 
122857  update_out:
122858   sqlite3_free(aSzIns);
122859   sqlite3Fts3SegmentsClose(p);
122860   return rc;
122861 }
122862 
122863 /*
122864 ** Flush any data in the pending-terms hash table to disk. If successful,
122865 ** merge all segments in the database (including the new segment, if
122866 ** there was any data to flush) into a single segment.
122867 */
122868 SQLITE_PRIVATE int sqlite3Fts3Optimize(Fts3Table *p){
122869   int rc;
122870   rc = sqlite3_exec(p->db, "SAVEPOINT fts3", 0, 0, 0);
122871   if( rc==SQLITE_OK ){
122872     rc = fts3DoOptimize(p, 1);
122873     if( rc==SQLITE_OK || rc==SQLITE_DONE ){
122874       int rc2 = sqlite3_exec(p->db, "RELEASE fts3", 0, 0, 0);
122875       if( rc2!=SQLITE_OK ) rc = rc2;
122876     }else{
122877       sqlite3_exec(p->db, "ROLLBACK TO fts3", 0, 0, 0);
122878       sqlite3_exec(p->db, "RELEASE fts3", 0, 0, 0);
122879     }
122880   }
122881   sqlite3Fts3SegmentsClose(p);
122882   return rc;
122883 }
122884 
122885 #endif
122886 
122887 /************** End of fts3_write.c ******************************************/
122888 /************** Begin file fts3_snippet.c ************************************/
122889 /*
122890 ** 2009 Oct 23
122891 **
122892 ** The author disclaims copyright to this source code.  In place of
122893 ** a legal notice, here is a blessing:
122894 **
122895 **    May you do good and not evil.
122896 **    May you find forgiveness for yourself and forgive others.
122897 **    May you share freely, never taking more than you give.
122898 **
122899 ******************************************************************************
122900 */
122901 
122902 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
122903 
122904 
122905 /*
122906 ** Characters that may appear in the second argument to matchinfo().
122907 */
122908 #define FTS3_MATCHINFO_NPHRASE   'p'        /* 1 value */
122909 #define FTS3_MATCHINFO_NCOL      'c'        /* 1 value */
122910 #define FTS3_MATCHINFO_NDOC      'n'        /* 1 value */
122911 #define FTS3_MATCHINFO_AVGLENGTH 'a'        /* nCol values */
122912 #define FTS3_MATCHINFO_LENGTH    'l'        /* nCol values */
122913 #define FTS3_MATCHINFO_LCS       's'        /* nCol values */
122914 #define FTS3_MATCHINFO_HITS      'x'        /* 3*nCol*nPhrase values */
122915 
122916 /*
122917 ** The default value for the second argument to matchinfo().
122918 */
122919 #define FTS3_MATCHINFO_DEFAULT   "pcx"
122920 
122921 
122922 /*
122923 ** Used as an fts3ExprIterate() context when loading phrase doclists to
122924 ** Fts3Expr.aDoclist[]/nDoclist.
122925 */
122926 typedef struct LoadDoclistCtx LoadDoclistCtx;
122927 struct LoadDoclistCtx {
122928   Fts3Cursor *pCsr;               /* FTS3 Cursor */
122929   int nPhrase;                    /* Number of phrases seen so far */
122930   int nToken;                     /* Number of tokens seen so far */
122931 };
122932 
122933 /*
122934 ** The following types are used as part of the implementation of the
122935 ** fts3BestSnippet() routine.
122936 */
122937 typedef struct SnippetIter SnippetIter;
122938 typedef struct SnippetPhrase SnippetPhrase;
122939 typedef struct SnippetFragment SnippetFragment;
122940 
122941 struct SnippetIter {
122942   Fts3Cursor *pCsr;               /* Cursor snippet is being generated from */
122943   int iCol;                       /* Extract snippet from this column */
122944   int nSnippet;                   /* Requested snippet length (in tokens) */
122945   int nPhrase;                    /* Number of phrases in query */
122946   SnippetPhrase *aPhrase;         /* Array of size nPhrase */
122947   int iCurrent;                   /* First token of current snippet */
122948 };
122949 
122950 struct SnippetPhrase {
122951   int nToken;                     /* Number of tokens in phrase */
122952   char *pList;                    /* Pointer to start of phrase position list */
122953   int iHead;                      /* Next value in position list */
122954   char *pHead;                    /* Position list data following iHead */
122955   int iTail;                      /* Next value in trailing position list */
122956   char *pTail;                    /* Position list data following iTail */
122957 };
122958 
122959 struct SnippetFragment {
122960   int iCol;                       /* Column snippet is extracted from */
122961   int iPos;                       /* Index of first token in snippet */
122962   u64 covered;                    /* Mask of query phrases covered */
122963   u64 hlmask;                     /* Mask of snippet terms to highlight */
122964 };
122965 
122966 /*
122967 ** This type is used as an fts3ExprIterate() context object while
122968 ** accumulating the data returned by the matchinfo() function.
122969 */
122970 typedef struct MatchInfo MatchInfo;
122971 struct MatchInfo {
122972   Fts3Cursor *pCursor;            /* FTS3 Cursor */
122973   int nCol;                       /* Number of columns in table */
122974   int nPhrase;                    /* Number of matchable phrases in query */
122975   sqlite3_int64 nDoc;             /* Number of docs in database */
122976   u32 *aMatchinfo;                /* Pre-allocated buffer */
122977 };
122978 
122979 
122980 
122981 /*
122982 ** The snippet() and offsets() functions both return text values. An instance
122983 ** of the following structure is used to accumulate those values while the
122984 ** functions are running. See fts3StringAppend() for details.
122985 */
122986 typedef struct StrBuffer StrBuffer;
122987 struct StrBuffer {
122988   char *z;                        /* Pointer to buffer containing string */
122989   int n;                          /* Length of z in bytes (excl. nul-term) */
122990   int nAlloc;                     /* Allocated size of buffer z in bytes */
122991 };
122992 
122993 
122994 /*
122995 ** This function is used to help iterate through a position-list. A position
122996 ** list is a list of unique integers, sorted from smallest to largest. Each
122997 ** element of the list is represented by an FTS3 varint that takes the value
122998 ** of the difference between the current element and the previous one plus
122999 ** two. For example, to store the position-list:
123000 **
123001 **     4 9 113
123002 **
123003 ** the three varints:
123004 **
123005 **     6 7 106
123006 **
123007 ** are encoded.
123008 **
123009 ** When this function is called, *pp points to the start of an element of
123010 ** the list. *piPos contains the value of the previous entry in the list.
123011 ** After it returns, *piPos contains the value of the next element of the
123012 ** list and *pp is advanced to the following varint.
123013 */
123014 static void fts3GetDeltaPosition(char **pp, int *piPos){
123015   int iVal;
123016   *pp += sqlite3Fts3GetVarint32(*pp, &iVal);
123017   *piPos += (iVal-2);
123018 }
123019 
123020 /*
123021 ** Helper function for fts3ExprIterate() (see below).
123022 */
123023 static int fts3ExprIterate2(
123024   Fts3Expr *pExpr,                /* Expression to iterate phrases of */
123025   int *piPhrase,                  /* Pointer to phrase counter */
123026   int (*x)(Fts3Expr*,int,void*),  /* Callback function to invoke for phrases */
123027   void *pCtx                      /* Second argument to pass to callback */
123028 ){
123029   int rc;                         /* Return code */
123030   int eType = pExpr->eType;       /* Type of expression node pExpr */
123031 
123032   if( eType!=FTSQUERY_PHRASE ){
123033     assert( pExpr->pLeft && pExpr->pRight );
123034     rc = fts3ExprIterate2(pExpr->pLeft, piPhrase, x, pCtx);
123035     if( rc==SQLITE_OK && eType!=FTSQUERY_NOT ){
123036       rc = fts3ExprIterate2(pExpr->pRight, piPhrase, x, pCtx);
123037     }
123038   }else{
123039     rc = x(pExpr, *piPhrase, pCtx);
123040     (*piPhrase)++;
123041   }
123042   return rc;
123043 }
123044 
123045 /*
123046 ** Iterate through all phrase nodes in an FTS3 query, except those that
123047 ** are part of a sub-tree that is the right-hand-side of a NOT operator.
123048 ** For each phrase node found, the supplied callback function is invoked.
123049 **
123050 ** If the callback function returns anything other than SQLITE_OK,
123051 ** the iteration is abandoned and the error code returned immediately.
123052 ** Otherwise, SQLITE_OK is returned after a callback has been made for
123053 ** all eligible phrase nodes.
123054 */
123055 static int fts3ExprIterate(
123056   Fts3Expr *pExpr,                /* Expression to iterate phrases of */
123057   int (*x)(Fts3Expr*,int,void*),  /* Callback function to invoke for phrases */
123058   void *pCtx                      /* Second argument to pass to callback */
123059 ){
123060   int iPhrase = 0;                /* Variable used as the phrase counter */
123061   return fts3ExprIterate2(pExpr, &iPhrase, x, pCtx);
123062 }
123063 
123064 /*
123065 ** This is an fts3ExprIterate() callback used while loading the doclists
123066 ** for each phrase into Fts3Expr.aDoclist[]/nDoclist. See also
123067 ** fts3ExprLoadDoclists().
123068 */
123069 static int fts3ExprLoadDoclistsCb(Fts3Expr *pExpr, int iPhrase, void *ctx){
123070   int rc = SQLITE_OK;
123071   Fts3Phrase *pPhrase = pExpr->pPhrase;
123072   LoadDoclistCtx *p = (LoadDoclistCtx *)ctx;
123073 
123074   UNUSED_PARAMETER(iPhrase);
123075 
123076   p->nPhrase++;
123077   p->nToken += pPhrase->nToken;
123078 
123079   return rc;
123080 }
123081 
123082 /*
123083 ** Load the doclists for each phrase in the query associated with FTS3 cursor
123084 ** pCsr.
123085 **
123086 ** If pnPhrase is not NULL, then *pnPhrase is set to the number of matchable
123087 ** phrases in the expression (all phrases except those directly or
123088 ** indirectly descended from the right-hand-side of a NOT operator). If
123089 ** pnToken is not NULL, then it is set to the number of tokens in all
123090 ** matchable phrases of the expression.
123091 */
123092 static int fts3ExprLoadDoclists(
123093   Fts3Cursor *pCsr,               /* Fts3 cursor for current query */
123094   int *pnPhrase,                  /* OUT: Number of phrases in query */
123095   int *pnToken                    /* OUT: Number of tokens in query */
123096 ){
123097   int rc;                         /* Return Code */
123098   LoadDoclistCtx sCtx = {0,0,0};  /* Context for fts3ExprIterate() */
123099   sCtx.pCsr = pCsr;
123100   rc = fts3ExprIterate(pCsr->pExpr, fts3ExprLoadDoclistsCb, (void *)&sCtx);
123101   if( pnPhrase ) *pnPhrase = sCtx.nPhrase;
123102   if( pnToken ) *pnToken = sCtx.nToken;
123103   return rc;
123104 }
123105 
123106 static int fts3ExprPhraseCountCb(Fts3Expr *pExpr, int iPhrase, void *ctx){
123107   (*(int *)ctx)++;
123108   UNUSED_PARAMETER(pExpr);
123109   UNUSED_PARAMETER(iPhrase);
123110   return SQLITE_OK;
123111 }
123112 static int fts3ExprPhraseCount(Fts3Expr *pExpr){
123113   int nPhrase = 0;
123114   (void)fts3ExprIterate(pExpr, fts3ExprPhraseCountCb, (void *)&nPhrase);
123115   return nPhrase;
123116 }
123117 
123118 /*
123119 ** Advance the position list iterator specified by the first two
123120 ** arguments so that it points to the first element with a value greater
123121 ** than or equal to parameter iNext.
123122 */
123123 static void fts3SnippetAdvance(char **ppIter, int *piIter, int iNext){
123124   char *pIter = *ppIter;
123125   if( pIter ){
123126     int iIter = *piIter;
123127 
123128     while( iIter<iNext ){
123129       if( 0==(*pIter & 0xFE) ){
123130         iIter = -1;
123131         pIter = 0;
123132         break;
123133       }
123134       fts3GetDeltaPosition(&pIter, &iIter);
123135     }
123136 
123137     *piIter = iIter;
123138     *ppIter = pIter;
123139   }
123140 }
123141 
123142 /*
123143 ** Advance the snippet iterator to the next candidate snippet.
123144 */
123145 static int fts3SnippetNextCandidate(SnippetIter *pIter){
123146   int i;                          /* Loop counter */
123147 
123148   if( pIter->iCurrent<0 ){
123149     /* The SnippetIter object has just been initialized. The first snippet
123150     ** candidate always starts at offset 0 (even if this candidate has a
123151     ** score of 0.0).
123152     */
123153     pIter->iCurrent = 0;
123154 
123155     /* Advance the 'head' iterator of each phrase to the first offset that
123156     ** is greater than or equal to (iNext+nSnippet).
123157     */
123158     for(i=0; i<pIter->nPhrase; i++){
123159       SnippetPhrase *pPhrase = &pIter->aPhrase[i];
123160       fts3SnippetAdvance(&pPhrase->pHead, &pPhrase->iHead, pIter->nSnippet);
123161     }
123162   }else{
123163     int iStart;
123164     int iEnd = 0x7FFFFFFF;
123165 
123166     for(i=0; i<pIter->nPhrase; i++){
123167       SnippetPhrase *pPhrase = &pIter->aPhrase[i];
123168       if( pPhrase->pHead && pPhrase->iHead<iEnd ){
123169         iEnd = pPhrase->iHead;
123170       }
123171     }
123172     if( iEnd==0x7FFFFFFF ){
123173       return 1;
123174     }
123175 
123176     pIter->iCurrent = iStart = iEnd - pIter->nSnippet + 1;
123177     for(i=0; i<pIter->nPhrase; i++){
123178       SnippetPhrase *pPhrase = &pIter->aPhrase[i];
123179       fts3SnippetAdvance(&pPhrase->pHead, &pPhrase->iHead, iEnd+1);
123180       fts3SnippetAdvance(&pPhrase->pTail, &pPhrase->iTail, iStart);
123181     }
123182   }
123183 
123184   return 0;
123185 }
123186 
123187 /*
123188 ** Retrieve information about the current candidate snippet of snippet
123189 ** iterator pIter.
123190 */
123191 static void fts3SnippetDetails(
123192   SnippetIter *pIter,             /* Snippet iterator */
123193   u64 mCovered,                   /* Bitmask of phrases already covered */
123194   int *piToken,                   /* OUT: First token of proposed snippet */
123195   int *piScore,                   /* OUT: "Score" for this snippet */
123196   u64 *pmCover,                   /* OUT: Bitmask of phrases covered */
123197   u64 *pmHighlight                /* OUT: Bitmask of terms to highlight */
123198 ){
123199   int iStart = pIter->iCurrent;   /* First token of snippet */
123200   int iScore = 0;                 /* Score of this snippet */
123201   int i;                          /* Loop counter */
123202   u64 mCover = 0;                 /* Mask of phrases covered by this snippet */
123203   u64 mHighlight = 0;             /* Mask of tokens to highlight in snippet */
123204 
123205   for(i=0; i<pIter->nPhrase; i++){
123206     SnippetPhrase *pPhrase = &pIter->aPhrase[i];
123207     if( pPhrase->pTail ){
123208       char *pCsr = pPhrase->pTail;
123209       int iCsr = pPhrase->iTail;
123210 
123211       while( iCsr<(iStart+pIter->nSnippet) ){
123212         int j;
123213         u64 mPhrase = (u64)1 << i;
123214         u64 mPos = (u64)1 << (iCsr - iStart);
123215         assert( iCsr>=iStart );
123216         if( (mCover|mCovered)&mPhrase ){
123217           iScore++;
123218         }else{
123219           iScore += 1000;
123220         }
123221         mCover |= mPhrase;
123222 
123223         for(j=0; j<pPhrase->nToken; j++){
123224           mHighlight |= (mPos>>j);
123225         }
123226 
123227         if( 0==(*pCsr & 0x0FE) ) break;
123228         fts3GetDeltaPosition(&pCsr, &iCsr);
123229       }
123230     }
123231   }
123232 
123233   /* Set the output variables before returning. */
123234   *piToken = iStart;
123235   *piScore = iScore;
123236   *pmCover = mCover;
123237   *pmHighlight = mHighlight;
123238 }
123239 
123240 /*
123241 ** This function is an fts3ExprIterate() callback used by fts3BestSnippet().
123242 ** Each invocation populates an element of the SnippetIter.aPhrase[] array.
123243 */
123244 static int fts3SnippetFindPositions(Fts3Expr *pExpr, int iPhrase, void *ctx){
123245   SnippetIter *p = (SnippetIter *)ctx;
123246   SnippetPhrase *pPhrase = &p->aPhrase[iPhrase];
123247   char *pCsr;
123248 
123249   pPhrase->nToken = pExpr->pPhrase->nToken;
123250 
123251   pCsr = sqlite3Fts3EvalPhrasePoslist(p->pCsr, pExpr, p->iCol);
123252   if( pCsr ){
123253     int iFirst = 0;
123254     pPhrase->pList = pCsr;
123255     fts3GetDeltaPosition(&pCsr, &iFirst);
123256     pPhrase->pHead = pCsr;
123257     pPhrase->pTail = pCsr;
123258     pPhrase->iHead = iFirst;
123259     pPhrase->iTail = iFirst;
123260   }else{
123261     assert( pPhrase->pList==0 && pPhrase->pHead==0 && pPhrase->pTail==0 );
123262   }
123263 
123264   return SQLITE_OK;
123265 }
123266 
123267 /*
123268 ** Select the fragment of text consisting of nFragment contiguous tokens
123269 ** from column iCol that represent the "best" snippet. The best snippet
123270 ** is the snippet with the highest score, where scores are calculated
123271 ** by adding:
123272 **
123273 **   (a) +1 point for each occurence of a matchable phrase in the snippet.
123274 **
123275 **   (b) +1000 points for the first occurence of each matchable phrase in
123276 **       the snippet for which the corresponding mCovered bit is not set.
123277 **
123278 ** The selected snippet parameters are stored in structure *pFragment before
123279 ** returning. The score of the selected snippet is stored in *piScore
123280 ** before returning.
123281 */
123282 static int fts3BestSnippet(
123283   int nSnippet,                   /* Desired snippet length */
123284   Fts3Cursor *pCsr,               /* Cursor to create snippet for */
123285   int iCol,                       /* Index of column to create snippet from */
123286   u64 mCovered,                   /* Mask of phrases already covered */
123287   u64 *pmSeen,                    /* IN/OUT: Mask of phrases seen */
123288   SnippetFragment *pFragment,     /* OUT: Best snippet found */
123289   int *piScore                    /* OUT: Score of snippet pFragment */
123290 ){
123291   int rc;                         /* Return Code */
123292   int nList;                      /* Number of phrases in expression */
123293   SnippetIter sIter;              /* Iterates through snippet candidates */
123294   int nByte;                      /* Number of bytes of space to allocate */
123295   int iBestScore = -1;            /* Best snippet score found so far */
123296   int i;                          /* Loop counter */
123297 
123298   memset(&sIter, 0, sizeof(sIter));
123299 
123300   /* Iterate through the phrases in the expression to count them. The same
123301   ** callback makes sure the doclists are loaded for each phrase.
123302   */
123303   rc = fts3ExprLoadDoclists(pCsr, &nList, 0);
123304   if( rc!=SQLITE_OK ){
123305     return rc;
123306   }
123307 
123308   /* Now that it is known how many phrases there are, allocate and zero
123309   ** the required space using malloc().
123310   */
123311   nByte = sizeof(SnippetPhrase) * nList;
123312   sIter.aPhrase = (SnippetPhrase *)sqlite3_malloc(nByte);
123313   if( !sIter.aPhrase ){
123314     return SQLITE_NOMEM;
123315   }
123316   memset(sIter.aPhrase, 0, nByte);
123317 
123318   /* Initialize the contents of the SnippetIter object. Then iterate through
123319   ** the set of phrases in the expression to populate the aPhrase[] array.
123320   */
123321   sIter.pCsr = pCsr;
123322   sIter.iCol = iCol;
123323   sIter.nSnippet = nSnippet;
123324   sIter.nPhrase = nList;
123325   sIter.iCurrent = -1;
123326   (void)fts3ExprIterate(pCsr->pExpr, fts3SnippetFindPositions, (void *)&sIter);
123327 
123328   /* Set the *pmSeen output variable. */
123329   for(i=0; i<nList; i++){
123330     if( sIter.aPhrase[i].pHead ){
123331       *pmSeen |= (u64)1 << i;
123332     }
123333   }
123334 
123335   /* Loop through all candidate snippets. Store the best snippet in
123336   ** *pFragment. Store its associated 'score' in iBestScore.
123337   */
123338   pFragment->iCol = iCol;
123339   while( !fts3SnippetNextCandidate(&sIter) ){
123340     int iPos;
123341     int iScore;
123342     u64 mCover;
123343     u64 mHighlight;
123344     fts3SnippetDetails(&sIter, mCovered, &iPos, &iScore, &mCover, &mHighlight);
123345     assert( iScore>=0 );
123346     if( iScore>iBestScore ){
123347       pFragment->iPos = iPos;
123348       pFragment->hlmask = mHighlight;
123349       pFragment->covered = mCover;
123350       iBestScore = iScore;
123351     }
123352   }
123353 
123354   sqlite3_free(sIter.aPhrase);
123355   *piScore = iBestScore;
123356   return SQLITE_OK;
123357 }
123358 
123359 
123360 /*
123361 ** Append a string to the string-buffer passed as the first argument.
123362 **
123363 ** If nAppend is negative, then the length of the string zAppend is
123364 ** determined using strlen().
123365 */
123366 static int fts3StringAppend(
123367   StrBuffer *pStr,                /* Buffer to append to */
123368   const char *zAppend,            /* Pointer to data to append to buffer */
123369   int nAppend                     /* Size of zAppend in bytes (or -1) */
123370 ){
123371   if( nAppend<0 ){
123372     nAppend = (int)strlen(zAppend);
123373   }
123374 
123375   /* If there is insufficient space allocated at StrBuffer.z, use realloc()
123376   ** to grow the buffer until so that it is big enough to accomadate the
123377   ** appended data.
123378   */
123379   if( pStr->n+nAppend+1>=pStr->nAlloc ){
123380     int nAlloc = pStr->nAlloc+nAppend+100;
123381     char *zNew = sqlite3_realloc(pStr->z, nAlloc);
123382     if( !zNew ){
123383       return SQLITE_NOMEM;
123384     }
123385     pStr->z = zNew;
123386     pStr->nAlloc = nAlloc;
123387   }
123388 
123389   /* Append the data to the string buffer. */
123390   memcpy(&pStr->z[pStr->n], zAppend, nAppend);
123391   pStr->n += nAppend;
123392   pStr->z[pStr->n] = '\0';
123393 
123394   return SQLITE_OK;
123395 }
123396 
123397 /*
123398 ** The fts3BestSnippet() function often selects snippets that end with a
123399 ** query term. That is, the final term of the snippet is always a term
123400 ** that requires highlighting. For example, if 'X' is a highlighted term
123401 ** and '.' is a non-highlighted term, BestSnippet() may select:
123402 **
123403 **     ........X.....X
123404 **
123405 ** This function "shifts" the beginning of the snippet forward in the
123406 ** document so that there are approximately the same number of
123407 ** non-highlighted terms to the right of the final highlighted term as there
123408 ** are to the left of the first highlighted term. For example, to this:
123409 **
123410 **     ....X.....X....
123411 **
123412 ** This is done as part of extracting the snippet text, not when selecting
123413 ** the snippet. Snippet selection is done based on doclists only, so there
123414 ** is no way for fts3BestSnippet() to know whether or not the document
123415 ** actually contains terms that follow the final highlighted term.
123416 */
123417 static int fts3SnippetShift(
123418   Fts3Table *pTab,                /* FTS3 table snippet comes from */
123419   int nSnippet,                   /* Number of tokens desired for snippet */
123420   const char *zDoc,               /* Document text to extract snippet from */
123421   int nDoc,                       /* Size of buffer zDoc in bytes */
123422   int *piPos,                     /* IN/OUT: First token of snippet */
123423   u64 *pHlmask                    /* IN/OUT: Mask of tokens to highlight */
123424 ){
123425   u64 hlmask = *pHlmask;          /* Local copy of initial highlight-mask */
123426 
123427   if( hlmask ){
123428     int nLeft;                    /* Tokens to the left of first highlight */
123429     int nRight;                   /* Tokens to the right of last highlight */
123430     int nDesired;                 /* Ideal number of tokens to shift forward */
123431 
123432     for(nLeft=0; !(hlmask & ((u64)1 << nLeft)); nLeft++);
123433     for(nRight=0; !(hlmask & ((u64)1 << (nSnippet-1-nRight))); nRight++);
123434     nDesired = (nLeft-nRight)/2;
123435 
123436     /* Ideally, the start of the snippet should be pushed forward in the
123437     ** document nDesired tokens. This block checks if there are actually
123438     ** nDesired tokens to the right of the snippet. If so, *piPos and
123439     ** *pHlMask are updated to shift the snippet nDesired tokens to the
123440     ** right. Otherwise, the snippet is shifted by the number of tokens
123441     ** available.
123442     */
123443     if( nDesired>0 ){
123444       int nShift;                 /* Number of tokens to shift snippet by */
123445       int iCurrent = 0;           /* Token counter */
123446       int rc;                     /* Return Code */
123447       sqlite3_tokenizer_module *pMod;
123448       sqlite3_tokenizer_cursor *pC;
123449       pMod = (sqlite3_tokenizer_module *)pTab->pTokenizer->pModule;
123450 
123451       /* Open a cursor on zDoc/nDoc. Check if there are (nSnippet+nDesired)
123452       ** or more tokens in zDoc/nDoc.
123453       */
123454       rc = pMod->xOpen(pTab->pTokenizer, zDoc, nDoc, &pC);
123455       if( rc!=SQLITE_OK ){
123456         return rc;
123457       }
123458       pC->pTokenizer = pTab->pTokenizer;
123459       while( rc==SQLITE_OK && iCurrent<(nSnippet+nDesired) ){
123460         const char *ZDUMMY; int DUMMY1, DUMMY2, DUMMY3;
123461         rc = pMod->xNext(pC, &ZDUMMY, &DUMMY1, &DUMMY2, &DUMMY3, &iCurrent);
123462       }
123463       pMod->xClose(pC);
123464       if( rc!=SQLITE_OK && rc!=SQLITE_DONE ){ return rc; }
123465 
123466       nShift = (rc==SQLITE_DONE)+iCurrent-nSnippet;
123467       assert( nShift<=nDesired );
123468       if( nShift>0 ){
123469         *piPos += nShift;
123470         *pHlmask = hlmask >> nShift;
123471       }
123472     }
123473   }
123474   return SQLITE_OK;
123475 }
123476 
123477 /*
123478 ** Extract the snippet text for fragment pFragment from cursor pCsr and
123479 ** append it to string buffer pOut.
123480 */
123481 static int fts3SnippetText(
123482   Fts3Cursor *pCsr,               /* FTS3 Cursor */
123483   SnippetFragment *pFragment,     /* Snippet to extract */
123484   int iFragment,                  /* Fragment number */
123485   int isLast,                     /* True for final fragment in snippet */
123486   int nSnippet,                   /* Number of tokens in extracted snippet */
123487   const char *zOpen,              /* String inserted before highlighted term */
123488   const char *zClose,             /* String inserted after highlighted term */
123489   const char *zEllipsis,          /* String inserted between snippets */
123490   StrBuffer *pOut                 /* Write output here */
123491 ){
123492   Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
123493   int rc;                         /* Return code */
123494   const char *zDoc;               /* Document text to extract snippet from */
123495   int nDoc;                       /* Size of zDoc in bytes */
123496   int iCurrent = 0;               /* Current token number of document */
123497   int iEnd = 0;                   /* Byte offset of end of current token */
123498   int isShiftDone = 0;            /* True after snippet is shifted */
123499   int iPos = pFragment->iPos;     /* First token of snippet */
123500   u64 hlmask = pFragment->hlmask; /* Highlight-mask for snippet */
123501   int iCol = pFragment->iCol+1;   /* Query column to extract text from */
123502   sqlite3_tokenizer_module *pMod; /* Tokenizer module methods object */
123503   sqlite3_tokenizer_cursor *pC;   /* Tokenizer cursor open on zDoc/nDoc */
123504   const char *ZDUMMY;             /* Dummy argument used with tokenizer */
123505   int DUMMY1;                     /* Dummy argument used with tokenizer */
123506 
123507   zDoc = (const char *)sqlite3_column_text(pCsr->pStmt, iCol);
123508   if( zDoc==0 ){
123509     if( sqlite3_column_type(pCsr->pStmt, iCol)!=SQLITE_NULL ){
123510       return SQLITE_NOMEM;
123511     }
123512     return SQLITE_OK;
123513   }
123514   nDoc = sqlite3_column_bytes(pCsr->pStmt, iCol);
123515 
123516   /* Open a token cursor on the document. */
123517   pMod = (sqlite3_tokenizer_module *)pTab->pTokenizer->pModule;
123518   rc = pMod->xOpen(pTab->pTokenizer, zDoc, nDoc, &pC);
123519   if( rc!=SQLITE_OK ){
123520     return rc;
123521   }
123522   pC->pTokenizer = pTab->pTokenizer;
123523 
123524   while( rc==SQLITE_OK ){
123525     int iBegin;                   /* Offset in zDoc of start of token */
123526     int iFin;                     /* Offset in zDoc of end of token */
123527     int isHighlight;              /* True for highlighted terms */
123528 
123529     rc = pMod->xNext(pC, &ZDUMMY, &DUMMY1, &iBegin, &iFin, &iCurrent);
123530     if( rc!=SQLITE_OK ){
123531       if( rc==SQLITE_DONE ){
123532         /* Special case - the last token of the snippet is also the last token
123533         ** of the column. Append any punctuation that occurred between the end
123534         ** of the previous token and the end of the document to the output.
123535         ** Then break out of the loop. */
123536         rc = fts3StringAppend(pOut, &zDoc[iEnd], -1);
123537       }
123538       break;
123539     }
123540     if( iCurrent<iPos ){ continue; }
123541 
123542     if( !isShiftDone ){
123543       int n = nDoc - iBegin;
123544       rc = fts3SnippetShift(pTab, nSnippet, &zDoc[iBegin], n, &iPos, &hlmask);
123545       isShiftDone = 1;
123546 
123547       /* Now that the shift has been done, check if the initial "..." are
123548       ** required. They are required if (a) this is not the first fragment,
123549       ** or (b) this fragment does not begin at position 0 of its column.
123550       */
123551       if( rc==SQLITE_OK && (iPos>0 || iFragment>0) ){
123552         rc = fts3StringAppend(pOut, zEllipsis, -1);
123553       }
123554       if( rc!=SQLITE_OK || iCurrent<iPos ) continue;
123555     }
123556 
123557     if( iCurrent>=(iPos+nSnippet) ){
123558       if( isLast ){
123559         rc = fts3StringAppend(pOut, zEllipsis, -1);
123560       }
123561       break;
123562     }
123563 
123564     /* Set isHighlight to true if this term should be highlighted. */
123565     isHighlight = (hlmask & ((u64)1 << (iCurrent-iPos)))!=0;
123566 
123567     if( iCurrent>iPos ) rc = fts3StringAppend(pOut, &zDoc[iEnd], iBegin-iEnd);
123568     if( rc==SQLITE_OK && isHighlight ) rc = fts3StringAppend(pOut, zOpen, -1);
123569     if( rc==SQLITE_OK ) rc = fts3StringAppend(pOut, &zDoc[iBegin], iFin-iBegin);
123570     if( rc==SQLITE_OK && isHighlight ) rc = fts3StringAppend(pOut, zClose, -1);
123571 
123572     iEnd = iFin;
123573   }
123574 
123575   pMod->xClose(pC);
123576   return rc;
123577 }
123578 
123579 
123580 /*
123581 ** This function is used to count the entries in a column-list (a
123582 ** delta-encoded list of term offsets within a single column of a single
123583 ** row). When this function is called, *ppCollist should point to the
123584 ** beginning of the first varint in the column-list (the varint that
123585 ** contains the position of the first matching term in the column data).
123586 ** Before returning, *ppCollist is set to point to the first byte after
123587 ** the last varint in the column-list (either the 0x00 signifying the end
123588 ** of the position-list, or the 0x01 that precedes the column number of
123589 ** the next column in the position-list).
123590 **
123591 ** The number of elements in the column-list is returned.
123592 */
123593 static int fts3ColumnlistCount(char **ppCollist){
123594   char *pEnd = *ppCollist;
123595   char c = 0;
123596   int nEntry = 0;
123597 
123598   /* A column-list is terminated by either a 0x01 or 0x00. */
123599   while( 0xFE & (*pEnd | c) ){
123600     c = *pEnd++ & 0x80;
123601     if( !c ) nEntry++;
123602   }
123603 
123604   *ppCollist = pEnd;
123605   return nEntry;
123606 }
123607 
123608 /*
123609 ** fts3ExprIterate() callback used to collect the "global" matchinfo stats
123610 ** for a single query.
123611 **
123612 ** fts3ExprIterate() callback to load the 'global' elements of a
123613 ** FTS3_MATCHINFO_HITS matchinfo array. The global stats are those elements
123614 ** of the matchinfo array that are constant for all rows returned by the
123615 ** current query.
123616 **
123617 ** Argument pCtx is actually a pointer to a struct of type MatchInfo. This
123618 ** function populates Matchinfo.aMatchinfo[] as follows:
123619 **
123620 **   for(iCol=0; iCol<nCol; iCol++){
123621 **     aMatchinfo[3*iPhrase*nCol + 3*iCol + 1] = X;
123622 **     aMatchinfo[3*iPhrase*nCol + 3*iCol + 2] = Y;
123623 **   }
123624 **
123625 ** where X is the number of matches for phrase iPhrase is column iCol of all
123626 ** rows of the table. Y is the number of rows for which column iCol contains
123627 ** at least one instance of phrase iPhrase.
123628 **
123629 ** If the phrase pExpr consists entirely of deferred tokens, then all X and
123630 ** Y values are set to nDoc, where nDoc is the number of documents in the
123631 ** file system. This is done because the full-text index doclist is required
123632 ** to calculate these values properly, and the full-text index doclist is
123633 ** not available for deferred tokens.
123634 */
123635 static int fts3ExprGlobalHitsCb(
123636   Fts3Expr *pExpr,                /* Phrase expression node */
123637   int iPhrase,                    /* Phrase number (numbered from zero) */
123638   void *pCtx                      /* Pointer to MatchInfo structure */
123639 ){
123640   MatchInfo *p = (MatchInfo *)pCtx;
123641   return sqlite3Fts3EvalPhraseStats(
123642       p->pCursor, pExpr, &p->aMatchinfo[3*iPhrase*p->nCol]
123643   );
123644 }
123645 
123646 /*
123647 ** fts3ExprIterate() callback used to collect the "local" part of the
123648 ** FTS3_MATCHINFO_HITS array. The local stats are those elements of the
123649 ** array that are different for each row returned by the query.
123650 */
123651 static int fts3ExprLocalHitsCb(
123652   Fts3Expr *pExpr,                /* Phrase expression node */
123653   int iPhrase,                    /* Phrase number */
123654   void *pCtx                      /* Pointer to MatchInfo structure */
123655 ){
123656   MatchInfo *p = (MatchInfo *)pCtx;
123657   int iStart = iPhrase * p->nCol * 3;
123658   int i;
123659 
123660   for(i=0; i<p->nCol; i++){
123661     char *pCsr;
123662     pCsr = sqlite3Fts3EvalPhrasePoslist(p->pCursor, pExpr, i);
123663     if( pCsr ){
123664       p->aMatchinfo[iStart+i*3] = fts3ColumnlistCount(&pCsr);
123665     }else{
123666       p->aMatchinfo[iStart+i*3] = 0;
123667     }
123668   }
123669 
123670   return SQLITE_OK;
123671 }
123672 
123673 static int fts3MatchinfoCheck(
123674   Fts3Table *pTab,
123675   char cArg,
123676   char **pzErr
123677 ){
123678   if( (cArg==FTS3_MATCHINFO_NPHRASE)
123679    || (cArg==FTS3_MATCHINFO_NCOL)
123680    || (cArg==FTS3_MATCHINFO_NDOC && pTab->bHasStat)
123681    || (cArg==FTS3_MATCHINFO_AVGLENGTH && pTab->bHasStat)
123682    || (cArg==FTS3_MATCHINFO_LENGTH && pTab->bHasDocsize)
123683    || (cArg==FTS3_MATCHINFO_LCS)
123684    || (cArg==FTS3_MATCHINFO_HITS)
123685   ){
123686     return SQLITE_OK;
123687   }
123688   *pzErr = sqlite3_mprintf("unrecognized matchinfo request: %c", cArg);
123689   return SQLITE_ERROR;
123690 }
123691 
123692 static int fts3MatchinfoSize(MatchInfo *pInfo, char cArg){
123693   int nVal;                       /* Number of integers output by cArg */
123694 
123695   switch( cArg ){
123696     case FTS3_MATCHINFO_NDOC:
123697     case FTS3_MATCHINFO_NPHRASE:
123698     case FTS3_MATCHINFO_NCOL:
123699       nVal = 1;
123700       break;
123701 
123702     case FTS3_MATCHINFO_AVGLENGTH:
123703     case FTS3_MATCHINFO_LENGTH:
123704     case FTS3_MATCHINFO_LCS:
123705       nVal = pInfo->nCol;
123706       break;
123707 
123708     default:
123709       assert( cArg==FTS3_MATCHINFO_HITS );
123710       nVal = pInfo->nCol * pInfo->nPhrase * 3;
123711       break;
123712   }
123713 
123714   return nVal;
123715 }
123716 
123717 static int fts3MatchinfoSelectDoctotal(
123718   Fts3Table *pTab,
123719   sqlite3_stmt **ppStmt,
123720   sqlite3_int64 *pnDoc,
123721   const char **paLen
123722 ){
123723   sqlite3_stmt *pStmt;
123724   const char *a;
123725   sqlite3_int64 nDoc;
123726 
123727   if( !*ppStmt ){
123728     int rc = sqlite3Fts3SelectDoctotal(pTab, ppStmt);
123729     if( rc!=SQLITE_OK ) return rc;
123730   }
123731   pStmt = *ppStmt;
123732   assert( sqlite3_data_count(pStmt)==1 );
123733 
123734   a = sqlite3_column_blob(pStmt, 0);
123735   a += sqlite3Fts3GetVarint(a, &nDoc);
123736   if( nDoc==0 ) return SQLITE_CORRUPT_VTAB;
123737   *pnDoc = (u32)nDoc;
123738 
123739   if( paLen ) *paLen = a;
123740   return SQLITE_OK;
123741 }
123742 
123743 /*
123744 ** An instance of the following structure is used to store state while
123745 ** iterating through a multi-column position-list corresponding to the
123746 ** hits for a single phrase on a single row in order to calculate the
123747 ** values for a matchinfo() FTS3_MATCHINFO_LCS request.
123748 */
123749 typedef struct LcsIterator LcsIterator;
123750 struct LcsIterator {
123751   Fts3Expr *pExpr;                /* Pointer to phrase expression */
123752   int iPosOffset;                 /* Tokens count up to end of this phrase */
123753   char *pRead;                    /* Cursor used to iterate through aDoclist */
123754   int iPos;                       /* Current position */
123755 };
123756 
123757 /*
123758 ** If LcsIterator.iCol is set to the following value, the iterator has
123759 ** finished iterating through all offsets for all columns.
123760 */
123761 #define LCS_ITERATOR_FINISHED 0x7FFFFFFF;
123762 
123763 static int fts3MatchinfoLcsCb(
123764   Fts3Expr *pExpr,                /* Phrase expression node */
123765   int iPhrase,                    /* Phrase number (numbered from zero) */
123766   void *pCtx                      /* Pointer to MatchInfo structure */
123767 ){
123768   LcsIterator *aIter = (LcsIterator *)pCtx;
123769   aIter[iPhrase].pExpr = pExpr;
123770   return SQLITE_OK;
123771 }
123772 
123773 /*
123774 ** Advance the iterator passed as an argument to the next position. Return
123775 ** 1 if the iterator is at EOF or if it now points to the start of the
123776 ** position list for the next column.
123777 */
123778 static int fts3LcsIteratorAdvance(LcsIterator *pIter){
123779   char *pRead = pIter->pRead;
123780   sqlite3_int64 iRead;
123781   int rc = 0;
123782 
123783   pRead += sqlite3Fts3GetVarint(pRead, &iRead);
123784   if( iRead==0 || iRead==1 ){
123785     pRead = 0;
123786     rc = 1;
123787   }else{
123788     pIter->iPos += (int)(iRead-2);
123789   }
123790 
123791   pIter->pRead = pRead;
123792   return rc;
123793 }
123794 
123795 /*
123796 ** This function implements the FTS3_MATCHINFO_LCS matchinfo() flag.
123797 **
123798 ** If the call is successful, the longest-common-substring lengths for each
123799 ** column are written into the first nCol elements of the pInfo->aMatchinfo[]
123800 ** array before returning. SQLITE_OK is returned in this case.
123801 **
123802 ** Otherwise, if an error occurs, an SQLite error code is returned and the
123803 ** data written to the first nCol elements of pInfo->aMatchinfo[] is
123804 ** undefined.
123805 */
123806 static int fts3MatchinfoLcs(Fts3Cursor *pCsr, MatchInfo *pInfo){
123807   LcsIterator *aIter;
123808   int i;
123809   int iCol;
123810   int nToken = 0;
123811 
123812   /* Allocate and populate the array of LcsIterator objects. The array
123813   ** contains one element for each matchable phrase in the query.
123814   **/
123815   aIter = sqlite3_malloc(sizeof(LcsIterator) * pCsr->nPhrase);
123816   if( !aIter ) return SQLITE_NOMEM;
123817   memset(aIter, 0, sizeof(LcsIterator) * pCsr->nPhrase);
123818   (void)fts3ExprIterate(pCsr->pExpr, fts3MatchinfoLcsCb, (void*)aIter);
123819 
123820   for(i=0; i<pInfo->nPhrase; i++){
123821     LcsIterator *pIter = &aIter[i];
123822     nToken -= pIter->pExpr->pPhrase->nToken;
123823     pIter->iPosOffset = nToken;
123824   }
123825 
123826   for(iCol=0; iCol<pInfo->nCol; iCol++){
123827     int nLcs = 0;                 /* LCS value for this column */
123828     int nLive = 0;                /* Number of iterators in aIter not at EOF */
123829 
123830     for(i=0; i<pInfo->nPhrase; i++){
123831       LcsIterator *pIt = &aIter[i];
123832       pIt->pRead = sqlite3Fts3EvalPhrasePoslist(pCsr, pIt->pExpr, iCol);
123833       if( pIt->pRead ){
123834         pIt->iPos = pIt->iPosOffset;
123835         fts3LcsIteratorAdvance(&aIter[i]);
123836         nLive++;
123837       }
123838     }
123839 
123840     while( nLive>0 ){
123841       LcsIterator *pAdv = 0;      /* The iterator to advance by one position */
123842       int nThisLcs = 0;           /* LCS for the current iterator positions */
123843 
123844       for(i=0; i<pInfo->nPhrase; i++){
123845         LcsIterator *pIter = &aIter[i];
123846         if( pIter->pRead==0 ){
123847           /* This iterator is already at EOF for this column. */
123848           nThisLcs = 0;
123849         }else{
123850           if( pAdv==0 || pIter->iPos<pAdv->iPos ){
123851             pAdv = pIter;
123852           }
123853           if( nThisLcs==0 || pIter->iPos==pIter[-1].iPos ){
123854             nThisLcs++;
123855           }else{
123856             nThisLcs = 1;
123857           }
123858           if( nThisLcs>nLcs ) nLcs = nThisLcs;
123859         }
123860       }
123861       if( fts3LcsIteratorAdvance(pAdv) ) nLive--;
123862     }
123863 
123864     pInfo->aMatchinfo[iCol] = nLcs;
123865   }
123866 
123867   sqlite3_free(aIter);
123868   return SQLITE_OK;
123869 }
123870 
123871 /*
123872 ** Populate the buffer pInfo->aMatchinfo[] with an array of integers to
123873 ** be returned by the matchinfo() function. Argument zArg contains the
123874 ** format string passed as the second argument to matchinfo (or the
123875 ** default value "pcx" if no second argument was specified). The format
123876 ** string has already been validated and the pInfo->aMatchinfo[] array
123877 ** is guaranteed to be large enough for the output.
123878 **
123879 ** If bGlobal is true, then populate all fields of the matchinfo() output.
123880 ** If it is false, then assume that those fields that do not change between
123881 ** rows (i.e. FTS3_MATCHINFO_NPHRASE, NCOL, NDOC, AVGLENGTH and part of HITS)
123882 ** have already been populated.
123883 **
123884 ** Return SQLITE_OK if successful, or an SQLite error code if an error
123885 ** occurs. If a value other than SQLITE_OK is returned, the state the
123886 ** pInfo->aMatchinfo[] buffer is left in is undefined.
123887 */
123888 static int fts3MatchinfoValues(
123889   Fts3Cursor *pCsr,               /* FTS3 cursor object */
123890   int bGlobal,                    /* True to grab the global stats */
123891   MatchInfo *pInfo,               /* Matchinfo context object */
123892   const char *zArg                /* Matchinfo format string */
123893 ){
123894   int rc = SQLITE_OK;
123895   int i;
123896   Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
123897   sqlite3_stmt *pSelect = 0;
123898 
123899   for(i=0; rc==SQLITE_OK && zArg[i]; i++){
123900 
123901     switch( zArg[i] ){
123902       case FTS3_MATCHINFO_NPHRASE:
123903         if( bGlobal ) pInfo->aMatchinfo[0] = pInfo->nPhrase;
123904         break;
123905 
123906       case FTS3_MATCHINFO_NCOL:
123907         if( bGlobal ) pInfo->aMatchinfo[0] = pInfo->nCol;
123908         break;
123909 
123910       case FTS3_MATCHINFO_NDOC:
123911         if( bGlobal ){
123912           sqlite3_int64 nDoc = 0;
123913           rc = fts3MatchinfoSelectDoctotal(pTab, &pSelect, &nDoc, 0);
123914           pInfo->aMatchinfo[0] = (u32)nDoc;
123915         }
123916         break;
123917 
123918       case FTS3_MATCHINFO_AVGLENGTH:
123919         if( bGlobal ){
123920           sqlite3_int64 nDoc;     /* Number of rows in table */
123921           const char *a;          /* Aggregate column length array */
123922 
123923           rc = fts3MatchinfoSelectDoctotal(pTab, &pSelect, &nDoc, &a);
123924           if( rc==SQLITE_OK ){
123925             int iCol;
123926             for(iCol=0; iCol<pInfo->nCol; iCol++){
123927               u32 iVal;
123928               sqlite3_int64 nToken;
123929               a += sqlite3Fts3GetVarint(a, &nToken);
123930               iVal = (u32)(((u32)(nToken&0xffffffff)+nDoc/2)/nDoc);
123931               pInfo->aMatchinfo[iCol] = iVal;
123932             }
123933           }
123934         }
123935         break;
123936 
123937       case FTS3_MATCHINFO_LENGTH: {
123938         sqlite3_stmt *pSelectDocsize = 0;
123939         rc = sqlite3Fts3SelectDocsize(pTab, pCsr->iPrevId, &pSelectDocsize);
123940         if( rc==SQLITE_OK ){
123941           int iCol;
123942           const char *a = sqlite3_column_blob(pSelectDocsize, 0);
123943           for(iCol=0; iCol<pInfo->nCol; iCol++){
123944             sqlite3_int64 nToken;
123945             a += sqlite3Fts3GetVarint(a, &nToken);
123946             pInfo->aMatchinfo[iCol] = (u32)nToken;
123947           }
123948         }
123949         sqlite3_reset(pSelectDocsize);
123950         break;
123951       }
123952 
123953       case FTS3_MATCHINFO_LCS:
123954         rc = fts3ExprLoadDoclists(pCsr, 0, 0);
123955         if( rc==SQLITE_OK ){
123956           rc = fts3MatchinfoLcs(pCsr, pInfo);
123957         }
123958         break;
123959 
123960       default: {
123961         Fts3Expr *pExpr;
123962         assert( zArg[i]==FTS3_MATCHINFO_HITS );
123963         pExpr = pCsr->pExpr;
123964         rc = fts3ExprLoadDoclists(pCsr, 0, 0);
123965         if( rc!=SQLITE_OK ) break;
123966         if( bGlobal ){
123967           if( pCsr->pDeferred ){
123968             rc = fts3MatchinfoSelectDoctotal(pTab, &pSelect, &pInfo->nDoc, 0);
123969             if( rc!=SQLITE_OK ) break;
123970           }
123971           rc = fts3ExprIterate(pExpr, fts3ExprGlobalHitsCb,(void*)pInfo);
123972           if( rc!=SQLITE_OK ) break;
123973         }
123974         (void)fts3ExprIterate(pExpr, fts3ExprLocalHitsCb,(void*)pInfo);
123975         break;
123976       }
123977     }
123978 
123979     pInfo->aMatchinfo += fts3MatchinfoSize(pInfo, zArg[i]);
123980   }
123981 
123982   sqlite3_reset(pSelect);
123983   return rc;
123984 }
123985 
123986 
123987 /*
123988 ** Populate pCsr->aMatchinfo[] with data for the current row. The
123989 ** 'matchinfo' data is an array of 32-bit unsigned integers (C type u32).
123990 */
123991 static int fts3GetMatchinfo(
123992   Fts3Cursor *pCsr,               /* FTS3 Cursor object */
123993   const char *zArg                /* Second argument to matchinfo() function */
123994 ){
123995   MatchInfo sInfo;
123996   Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
123997   int rc = SQLITE_OK;
123998   int bGlobal = 0;                /* Collect 'global' stats as well as local */
123999 
124000   memset(&sInfo, 0, sizeof(MatchInfo));
124001   sInfo.pCursor = pCsr;
124002   sInfo.nCol = pTab->nColumn;
124003 
124004   /* If there is cached matchinfo() data, but the format string for the
124005   ** cache does not match the format string for this request, discard
124006   ** the cached data. */
124007   if( pCsr->zMatchinfo && strcmp(pCsr->zMatchinfo, zArg) ){
124008     assert( pCsr->aMatchinfo );
124009     sqlite3_free(pCsr->aMatchinfo);
124010     pCsr->zMatchinfo = 0;
124011     pCsr->aMatchinfo = 0;
124012   }
124013 
124014   /* If Fts3Cursor.aMatchinfo[] is NULL, then this is the first time the
124015   ** matchinfo function has been called for this query. In this case
124016   ** allocate the array used to accumulate the matchinfo data and
124017   ** initialize those elements that are constant for every row.
124018   */
124019   if( pCsr->aMatchinfo==0 ){
124020     int nMatchinfo = 0;           /* Number of u32 elements in match-info */
124021     int nArg;                     /* Bytes in zArg */
124022     int i;                        /* Used to iterate through zArg */
124023 
124024     /* Determine the number of phrases in the query */
124025     pCsr->nPhrase = fts3ExprPhraseCount(pCsr->pExpr);
124026     sInfo.nPhrase = pCsr->nPhrase;
124027 
124028     /* Determine the number of integers in the buffer returned by this call. */
124029     for(i=0; zArg[i]; i++){
124030       nMatchinfo += fts3MatchinfoSize(&sInfo, zArg[i]);
124031     }
124032 
124033     /* Allocate space for Fts3Cursor.aMatchinfo[] and Fts3Cursor.zMatchinfo. */
124034     nArg = (int)strlen(zArg);
124035     pCsr->aMatchinfo = (u32 *)sqlite3_malloc(sizeof(u32)*nMatchinfo + nArg + 1);
124036     if( !pCsr->aMatchinfo ) return SQLITE_NOMEM;
124037 
124038     pCsr->zMatchinfo = (char *)&pCsr->aMatchinfo[nMatchinfo];
124039     pCsr->nMatchinfo = nMatchinfo;
124040     memcpy(pCsr->zMatchinfo, zArg, nArg+1);
124041     memset(pCsr->aMatchinfo, 0, sizeof(u32)*nMatchinfo);
124042     pCsr->isMatchinfoNeeded = 1;
124043     bGlobal = 1;
124044   }
124045 
124046   sInfo.aMatchinfo = pCsr->aMatchinfo;
124047   sInfo.nPhrase = pCsr->nPhrase;
124048   if( pCsr->isMatchinfoNeeded ){
124049     rc = fts3MatchinfoValues(pCsr, bGlobal, &sInfo, zArg);
124050     pCsr->isMatchinfoNeeded = 0;
124051   }
124052 
124053   return rc;
124054 }
124055 
124056 /*
124057 ** Implementation of snippet() function.
124058 */
124059 SQLITE_PRIVATE void sqlite3Fts3Snippet(
124060   sqlite3_context *pCtx,          /* SQLite function call context */
124061   Fts3Cursor *pCsr,               /* Cursor object */
124062   const char *zStart,             /* Snippet start text - "<b>" */
124063   const char *zEnd,               /* Snippet end text - "</b>" */
124064   const char *zEllipsis,          /* Snippet ellipsis text - "<b>...</b>" */
124065   int iCol,                       /* Extract snippet from this column */
124066   int nToken                      /* Approximate number of tokens in snippet */
124067 ){
124068   Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
124069   int rc = SQLITE_OK;
124070   int i;
124071   StrBuffer res = {0, 0, 0};
124072 
124073   /* The returned text includes up to four fragments of text extracted from
124074   ** the data in the current row. The first iteration of the for(...) loop
124075   ** below attempts to locate a single fragment of text nToken tokens in
124076   ** size that contains at least one instance of all phrases in the query
124077   ** expression that appear in the current row. If such a fragment of text
124078   ** cannot be found, the second iteration of the loop attempts to locate
124079   ** a pair of fragments, and so on.
124080   */
124081   int nSnippet = 0;               /* Number of fragments in this snippet */
124082   SnippetFragment aSnippet[4];    /* Maximum of 4 fragments per snippet */
124083   int nFToken = -1;               /* Number of tokens in each fragment */
124084 
124085   if( !pCsr->pExpr ){
124086     sqlite3_result_text(pCtx, "", 0, SQLITE_STATIC);
124087     return;
124088   }
124089 
124090   for(nSnippet=1; 1; nSnippet++){
124091 
124092     int iSnip;                    /* Loop counter 0..nSnippet-1 */
124093     u64 mCovered = 0;             /* Bitmask of phrases covered by snippet */
124094     u64 mSeen = 0;                /* Bitmask of phrases seen by BestSnippet() */
124095 
124096     if( nToken>=0 ){
124097       nFToken = (nToken+nSnippet-1) / nSnippet;
124098     }else{
124099       nFToken = -1 * nToken;
124100     }
124101 
124102     for(iSnip=0; iSnip<nSnippet; iSnip++){
124103       int iBestScore = -1;        /* Best score of columns checked so far */
124104       int iRead;                  /* Used to iterate through columns */
124105       SnippetFragment *pFragment = &aSnippet[iSnip];
124106 
124107       memset(pFragment, 0, sizeof(*pFragment));
124108 
124109       /* Loop through all columns of the table being considered for snippets.
124110       ** If the iCol argument to this function was negative, this means all
124111       ** columns of the FTS3 table. Otherwise, only column iCol is considered.
124112       */
124113       for(iRead=0; iRead<pTab->nColumn; iRead++){
124114         SnippetFragment sF = {0, 0, 0, 0};
124115         int iS;
124116         if( iCol>=0 && iRead!=iCol ) continue;
124117 
124118         /* Find the best snippet of nFToken tokens in column iRead. */
124119         rc = fts3BestSnippet(nFToken, pCsr, iRead, mCovered, &mSeen, &sF, &iS);
124120         if( rc!=SQLITE_OK ){
124121           goto snippet_out;
124122         }
124123         if( iS>iBestScore ){
124124           *pFragment = sF;
124125           iBestScore = iS;
124126         }
124127       }
124128 
124129       mCovered |= pFragment->covered;
124130     }
124131 
124132     /* If all query phrases seen by fts3BestSnippet() are present in at least
124133     ** one of the nSnippet snippet fragments, break out of the loop.
124134     */
124135     assert( (mCovered&mSeen)==mCovered );
124136     if( mSeen==mCovered || nSnippet==SizeofArray(aSnippet) ) break;
124137   }
124138 
124139   assert( nFToken>0 );
124140 
124141   for(i=0; i<nSnippet && rc==SQLITE_OK; i++){
124142     rc = fts3SnippetText(pCsr, &aSnippet[i],
124143         i, (i==nSnippet-1), nFToken, zStart, zEnd, zEllipsis, &res
124144     );
124145   }
124146 
124147  snippet_out:
124148   sqlite3Fts3SegmentsClose(pTab);
124149   if( rc!=SQLITE_OK ){
124150     sqlite3_result_error_code(pCtx, rc);
124151     sqlite3_free(res.z);
124152   }else{
124153     sqlite3_result_text(pCtx, res.z, -1, sqlite3_free);
124154   }
124155 }
124156 
124157 
124158 typedef struct TermOffset TermOffset;
124159 typedef struct TermOffsetCtx TermOffsetCtx;
124160 
124161 struct TermOffset {
124162   char *pList;                    /* Position-list */
124163   int iPos;                       /* Position just read from pList */
124164   int iOff;                       /* Offset of this term from read positions */
124165 };
124166 
124167 struct TermOffsetCtx {
124168   Fts3Cursor *pCsr;
124169   int iCol;                       /* Column of table to populate aTerm for */
124170   int iTerm;
124171   sqlite3_int64 iDocid;
124172   TermOffset *aTerm;
124173 };
124174 
124175 /*
124176 ** This function is an fts3ExprIterate() callback used by sqlite3Fts3Offsets().
124177 */
124178 static int fts3ExprTermOffsetInit(Fts3Expr *pExpr, int iPhrase, void *ctx){
124179   TermOffsetCtx *p = (TermOffsetCtx *)ctx;
124180   int nTerm;                      /* Number of tokens in phrase */
124181   int iTerm;                      /* For looping through nTerm phrase terms */
124182   char *pList;                    /* Pointer to position list for phrase */
124183   int iPos = 0;                   /* First position in position-list */
124184 
124185   UNUSED_PARAMETER(iPhrase);
124186   pList = sqlite3Fts3EvalPhrasePoslist(p->pCsr, pExpr, p->iCol);
124187   nTerm = pExpr->pPhrase->nToken;
124188   if( pList ){
124189     fts3GetDeltaPosition(&pList, &iPos);
124190     assert( iPos>=0 );
124191   }
124192 
124193   for(iTerm=0; iTerm<nTerm; iTerm++){
124194     TermOffset *pT = &p->aTerm[p->iTerm++];
124195     pT->iOff = nTerm-iTerm-1;
124196     pT->pList = pList;
124197     pT->iPos = iPos;
124198   }
124199 
124200   return SQLITE_OK;
124201 }
124202 
124203 /*
124204 ** Implementation of offsets() function.
124205 */
124206 SQLITE_PRIVATE void sqlite3Fts3Offsets(
124207   sqlite3_context *pCtx,          /* SQLite function call context */
124208   Fts3Cursor *pCsr                /* Cursor object */
124209 ){
124210   Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
124211   sqlite3_tokenizer_module const *pMod = pTab->pTokenizer->pModule;
124212   const char *ZDUMMY;             /* Dummy argument used with xNext() */
124213   int NDUMMY;                     /* Dummy argument used with xNext() */
124214   int rc;                         /* Return Code */
124215   int nToken;                     /* Number of tokens in query */
124216   int iCol;                       /* Column currently being processed */
124217   StrBuffer res = {0, 0, 0};      /* Result string */
124218   TermOffsetCtx sCtx;             /* Context for fts3ExprTermOffsetInit() */
124219 
124220   if( !pCsr->pExpr ){
124221     sqlite3_result_text(pCtx, "", 0, SQLITE_STATIC);
124222     return;
124223   }
124224 
124225   memset(&sCtx, 0, sizeof(sCtx));
124226   assert( pCsr->isRequireSeek==0 );
124227 
124228   /* Count the number of terms in the query */
124229   rc = fts3ExprLoadDoclists(pCsr, 0, &nToken);
124230   if( rc!=SQLITE_OK ) goto offsets_out;
124231 
124232   /* Allocate the array of TermOffset iterators. */
124233   sCtx.aTerm = (TermOffset *)sqlite3_malloc(sizeof(TermOffset)*nToken);
124234   if( 0==sCtx.aTerm ){
124235     rc = SQLITE_NOMEM;
124236     goto offsets_out;
124237   }
124238   sCtx.iDocid = pCsr->iPrevId;
124239   sCtx.pCsr = pCsr;
124240 
124241   /* Loop through the table columns, appending offset information to
124242   ** string-buffer res for each column.
124243   */
124244   for(iCol=0; iCol<pTab->nColumn; iCol++){
124245     sqlite3_tokenizer_cursor *pC; /* Tokenizer cursor */
124246     int iStart;
124247     int iEnd;
124248     int iCurrent;
124249     const char *zDoc;
124250     int nDoc;
124251 
124252     /* Initialize the contents of sCtx.aTerm[] for column iCol. There is
124253     ** no way that this operation can fail, so the return code from
124254     ** fts3ExprIterate() can be discarded.
124255     */
124256     sCtx.iCol = iCol;
124257     sCtx.iTerm = 0;
124258     (void)fts3ExprIterate(pCsr->pExpr, fts3ExprTermOffsetInit, (void *)&sCtx);
124259 
124260     /* Retreive the text stored in column iCol. If an SQL NULL is stored
124261     ** in column iCol, jump immediately to the next iteration of the loop.
124262     ** If an OOM occurs while retrieving the data (this can happen if SQLite
124263     ** needs to transform the data from utf-16 to utf-8), return SQLITE_NOMEM
124264     ** to the caller.
124265     */
124266     zDoc = (const char *)sqlite3_column_text(pCsr->pStmt, iCol+1);
124267     nDoc = sqlite3_column_bytes(pCsr->pStmt, iCol+1);
124268     if( zDoc==0 ){
124269       if( sqlite3_column_type(pCsr->pStmt, iCol+1)==SQLITE_NULL ){
124270         continue;
124271       }
124272       rc = SQLITE_NOMEM;
124273       goto offsets_out;
124274     }
124275 
124276     /* Initialize a tokenizer iterator to iterate through column iCol. */
124277     rc = pMod->xOpen(pTab->pTokenizer, zDoc, nDoc, &pC);
124278     if( rc!=SQLITE_OK ) goto offsets_out;
124279     pC->pTokenizer = pTab->pTokenizer;
124280 
124281     rc = pMod->xNext(pC, &ZDUMMY, &NDUMMY, &iStart, &iEnd, &iCurrent);
124282     while( rc==SQLITE_OK ){
124283       int i;                      /* Used to loop through terms */
124284       int iMinPos = 0x7FFFFFFF;   /* Position of next token */
124285       TermOffset *pTerm = 0;      /* TermOffset associated with next token */
124286 
124287       for(i=0; i<nToken; i++){
124288         TermOffset *pT = &sCtx.aTerm[i];
124289         if( pT->pList && (pT->iPos-pT->iOff)<iMinPos ){
124290           iMinPos = pT->iPos-pT->iOff;
124291           pTerm = pT;
124292         }
124293       }
124294 
124295       if( !pTerm ){
124296         /* All offsets for this column have been gathered. */
124297         break;
124298       }else{
124299         assert( iCurrent<=iMinPos );
124300         if( 0==(0xFE&*pTerm->pList) ){
124301           pTerm->pList = 0;
124302         }else{
124303           fts3GetDeltaPosition(&pTerm->pList, &pTerm->iPos);
124304         }
124305         while( rc==SQLITE_OK && iCurrent<iMinPos ){
124306           rc = pMod->xNext(pC, &ZDUMMY, &NDUMMY, &iStart, &iEnd, &iCurrent);
124307         }
124308         if( rc==SQLITE_OK ){
124309           char aBuffer[64];
124310           sqlite3_snprintf(sizeof(aBuffer), aBuffer,
124311               "%d %d %d %d ", iCol, pTerm-sCtx.aTerm, iStart, iEnd-iStart
124312           );
124313           rc = fts3StringAppend(&res, aBuffer, -1);
124314         }else if( rc==SQLITE_DONE ){
124315           rc = SQLITE_CORRUPT_VTAB;
124316         }
124317       }
124318     }
124319     if( rc==SQLITE_DONE ){
124320       rc = SQLITE_OK;
124321     }
124322 
124323     pMod->xClose(pC);
124324     if( rc!=SQLITE_OK ) goto offsets_out;
124325   }
124326 
124327  offsets_out:
124328   sqlite3_free(sCtx.aTerm);
124329   assert( rc!=SQLITE_DONE );
124330   sqlite3Fts3SegmentsClose(pTab);
124331   if( rc!=SQLITE_OK ){
124332     sqlite3_result_error_code(pCtx,  rc);
124333     sqlite3_free(res.z);
124334   }else{
124335     sqlite3_result_text(pCtx, res.z, res.n-1, sqlite3_free);
124336   }
124337   return;
124338 }
124339 
124340 /*
124341 ** Implementation of matchinfo() function.
124342 */
124343 SQLITE_PRIVATE void sqlite3Fts3Matchinfo(
124344   sqlite3_context *pContext,      /* Function call context */
124345   Fts3Cursor *pCsr,               /* FTS3 table cursor */
124346   const char *zArg                /* Second arg to matchinfo() function */
124347 ){
124348   Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
124349   int rc;
124350   int i;
124351   const char *zFormat;
124352 
124353   if( zArg ){
124354     for(i=0; zArg[i]; i++){
124355       char *zErr = 0;
124356       if( fts3MatchinfoCheck(pTab, zArg[i], &zErr) ){
124357         sqlite3_result_error(pContext, zErr, -1);
124358         sqlite3_free(zErr);
124359         return;
124360       }
124361     }
124362     zFormat = zArg;
124363   }else{
124364     zFormat = FTS3_MATCHINFO_DEFAULT;
124365   }
124366 
124367   if( !pCsr->pExpr ){
124368     sqlite3_result_blob(pContext, "", 0, SQLITE_STATIC);
124369     return;
124370   }
124371 
124372   /* Retrieve matchinfo() data. */
124373   rc = fts3GetMatchinfo(pCsr, zFormat);
124374   sqlite3Fts3SegmentsClose(pTab);
124375 
124376   if( rc!=SQLITE_OK ){
124377     sqlite3_result_error_code(pContext, rc);
124378   }else{
124379     int n = pCsr->nMatchinfo * sizeof(u32);
124380     sqlite3_result_blob(pContext, pCsr->aMatchinfo, n, SQLITE_TRANSIENT);
124381   }
124382 }
124383 
124384 #endif
124385 
124386 /************** End of fts3_snippet.c ****************************************/
124387 /************** Begin file rtree.c *******************************************/
124388 /*
124389 ** 2001 September 15
124390 **
124391 ** The author disclaims copyright to this source code.  In place of
124392 ** a legal notice, here is a blessing:
124393 **
124394 **    May you do good and not evil.
124395 **    May you find forgiveness for yourself and forgive others.
124396 **    May you share freely, never taking more than you give.
124397 **
124398 *************************************************************************
124399 ** This file contains code for implementations of the r-tree and r*-tree
124400 ** algorithms packaged as an SQLite virtual table module.
124401 */
124402 
124403 /*
124404 ** Database Format of R-Tree Tables
124405 ** --------------------------------
124406 **
124407 ** The data structure for a single virtual r-tree table is stored in three
124408 ** native SQLite tables declared as follows. In each case, the '%' character
124409 ** in the table name is replaced with the user-supplied name of the r-tree
124410 ** table.
124411 **
124412 **   CREATE TABLE %_node(nodeno INTEGER PRIMARY KEY, data BLOB)
124413 **   CREATE TABLE %_parent(nodeno INTEGER PRIMARY KEY, parentnode INTEGER)
124414 **   CREATE TABLE %_rowid(rowid INTEGER PRIMARY KEY, nodeno INTEGER)
124415 **
124416 ** The data for each node of the r-tree structure is stored in the %_node
124417 ** table. For each node that is not the root node of the r-tree, there is
124418 ** an entry in the %_parent table associating the node with its parent.
124419 ** And for each row of data in the table, there is an entry in the %_rowid
124420 ** table that maps from the entries rowid to the id of the node that it
124421 ** is stored on.
124422 **
124423 ** The root node of an r-tree always exists, even if the r-tree table is
124424 ** empty. The nodeno of the root node is always 1. All other nodes in the
124425 ** table must be the same size as the root node. The content of each node
124426 ** is formatted as follows:
124427 **
124428 **   1. If the node is the root node (node 1), then the first 2 bytes
124429 **      of the node contain the tree depth as a big-endian integer.
124430 **      For non-root nodes, the first 2 bytes are left unused.
124431 **
124432 **   2. The next 2 bytes contain the number of entries currently
124433 **      stored in the node.
124434 **
124435 **   3. The remainder of the node contains the node entries. Each entry
124436 **      consists of a single 8-byte integer followed by an even number
124437 **      of 4-byte coordinates. For leaf nodes the integer is the rowid
124438 **      of a record. For internal nodes it is the node number of a
124439 **      child page.
124440 */
124441 
124442 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_RTREE)
124443 
124444 /*
124445 ** This file contains an implementation of a couple of different variants
124446 ** of the r-tree algorithm. See the README file for further details. The
124447 ** same data-structure is used for all, but the algorithms for insert and
124448 ** delete operations vary. The variants used are selected at compile time
124449 ** by defining the following symbols:
124450 */
124451 
124452 /* Either, both or none of the following may be set to activate
124453 ** r*tree variant algorithms.
124454 */
124455 #define VARIANT_RSTARTREE_CHOOSESUBTREE 0
124456 #define VARIANT_RSTARTREE_REINSERT      1
124457 
124458 /*
124459 ** Exactly one of the following must be set to 1.
124460 */
124461 #define VARIANT_GUTTMAN_QUADRATIC_SPLIT 0
124462 #define VARIANT_GUTTMAN_LINEAR_SPLIT    0
124463 #define VARIANT_RSTARTREE_SPLIT         1
124464 
124465 #define VARIANT_GUTTMAN_SPLIT \
124466         (VARIANT_GUTTMAN_LINEAR_SPLIT||VARIANT_GUTTMAN_QUADRATIC_SPLIT)
124467 
124468 #if VARIANT_GUTTMAN_QUADRATIC_SPLIT
124469   #define PickNext QuadraticPickNext
124470   #define PickSeeds QuadraticPickSeeds
124471   #define AssignCells splitNodeGuttman
124472 #endif
124473 #if VARIANT_GUTTMAN_LINEAR_SPLIT
124474   #define PickNext LinearPickNext
124475   #define PickSeeds LinearPickSeeds
124476   #define AssignCells splitNodeGuttman
124477 #endif
124478 #if VARIANT_RSTARTREE_SPLIT
124479   #define AssignCells splitNodeStartree
124480 #endif
124481 
124482 #if !defined(NDEBUG) && !defined(SQLITE_DEBUG)
124483 # define NDEBUG 1
124484 #endif
124485 
124486 #ifndef SQLITE_CORE
124487   SQLITE_EXTENSION_INIT1
124488 #else
124489 #endif
124490 
124491 
124492 #ifndef SQLITE_AMALGAMATION
124493 #include "sqlite3rtree.h"
124494 typedef sqlite3_int64 i64;
124495 typedef unsigned char u8;
124496 typedef unsigned int u32;
124497 #endif
124498 
124499 /*  The following macro is used to suppress compiler warnings.
124500 */
124501 #ifndef UNUSED_PARAMETER
124502 # define UNUSED_PARAMETER(x) (void)(x)
124503 #endif
124504 
124505 typedef struct Rtree Rtree;
124506 typedef struct RtreeCursor RtreeCursor;
124507 typedef struct RtreeNode RtreeNode;
124508 typedef struct RtreeCell RtreeCell;
124509 typedef struct RtreeConstraint RtreeConstraint;
124510 typedef struct RtreeMatchArg RtreeMatchArg;
124511 typedef struct RtreeGeomCallback RtreeGeomCallback;
124512 typedef union RtreeCoord RtreeCoord;
124513 
124514 /* The rtree may have between 1 and RTREE_MAX_DIMENSIONS dimensions. */
124515 #define RTREE_MAX_DIMENSIONS 5
124516 
124517 /* Size of hash table Rtree.aHash. This hash table is not expected to
124518 ** ever contain very many entries, so a fixed number of buckets is
124519 ** used.
124520 */
124521 #define HASHSIZE 128
124522 
124523 /*
124524 ** An rtree virtual-table object.
124525 */
124526 struct Rtree {
124527   sqlite3_vtab base;
124528   sqlite3 *db;                /* Host database connection */
124529   int iNodeSize;              /* Size in bytes of each node in the node table */
124530   int nDim;                   /* Number of dimensions */
124531   int nBytesPerCell;          /* Bytes consumed per cell */
124532   int iDepth;                 /* Current depth of the r-tree structure */
124533   char *zDb;                  /* Name of database containing r-tree table */
124534   char *zName;                /* Name of r-tree table */
124535   RtreeNode *aHash[HASHSIZE]; /* Hash table of in-memory nodes. */
124536   int nBusy;                  /* Current number of users of this structure */
124537 
124538   /* List of nodes removed during a CondenseTree operation. List is
124539   ** linked together via the pointer normally used for hash chains -
124540   ** RtreeNode.pNext. RtreeNode.iNode stores the depth of the sub-tree
124541   ** headed by the node (leaf nodes have RtreeNode.iNode==0).
124542   */
124543   RtreeNode *pDeleted;
124544   int iReinsertHeight;        /* Height of sub-trees Reinsert() has run on */
124545 
124546   /* Statements to read/write/delete a record from xxx_node */
124547   sqlite3_stmt *pReadNode;
124548   sqlite3_stmt *pWriteNode;
124549   sqlite3_stmt *pDeleteNode;
124550 
124551   /* Statements to read/write/delete a record from xxx_rowid */
124552   sqlite3_stmt *pReadRowid;
124553   sqlite3_stmt *pWriteRowid;
124554   sqlite3_stmt *pDeleteRowid;
124555 
124556   /* Statements to read/write/delete a record from xxx_parent */
124557   sqlite3_stmt *pReadParent;
124558   sqlite3_stmt *pWriteParent;
124559   sqlite3_stmt *pDeleteParent;
124560 
124561   int eCoordType;
124562 };
124563 
124564 /* Possible values for eCoordType: */
124565 #define RTREE_COORD_REAL32 0
124566 #define RTREE_COORD_INT32  1
124567 
124568 /*
124569 ** The minimum number of cells allowed for a node is a third of the
124570 ** maximum. In Gutman's notation:
124571 **
124572 **     m = M/3
124573 **
124574 ** If an R*-tree "Reinsert" operation is required, the same number of
124575 ** cells are removed from the overfull node and reinserted into the tree.
124576 */
124577 #define RTREE_MINCELLS(p) ((((p)->iNodeSize-4)/(p)->nBytesPerCell)/3)
124578 #define RTREE_REINSERT(p) RTREE_MINCELLS(p)
124579 #define RTREE_MAXCELLS 51
124580 
124581 /*
124582 ** The smallest possible node-size is (512-64)==448 bytes. And the largest
124583 ** supported cell size is 48 bytes (8 byte rowid + ten 4 byte coordinates).
124584 ** Therefore all non-root nodes must contain at least 3 entries. Since
124585 ** 2^40 is greater than 2^64, an r-tree structure always has a depth of
124586 ** 40 or less.
124587 */
124588 #define RTREE_MAX_DEPTH 40
124589 
124590 /*
124591 ** An rtree cursor object.
124592 */
124593 struct RtreeCursor {
124594   sqlite3_vtab_cursor base;
124595   RtreeNode *pNode;                 /* Node cursor is currently pointing at */
124596   int iCell;                        /* Index of current cell in pNode */
124597   int iStrategy;                    /* Copy of idxNum search parameter */
124598   int nConstraint;                  /* Number of entries in aConstraint */
124599   RtreeConstraint *aConstraint;     /* Search constraints. */
124600 };
124601 
124602 union RtreeCoord {
124603   float f;
124604   int i;
124605 };
124606 
124607 /*
124608 ** The argument is an RtreeCoord. Return the value stored within the RtreeCoord
124609 ** formatted as a double. This macro assumes that local variable pRtree points
124610 ** to the Rtree structure associated with the RtreeCoord.
124611 */
124612 #define DCOORD(coord) (                           \
124613   (pRtree->eCoordType==RTREE_COORD_REAL32) ?      \
124614     ((double)coord.f) :                           \
124615     ((double)coord.i)                             \
124616 )
124617 
124618 /*
124619 ** A search constraint.
124620 */
124621 struct RtreeConstraint {
124622   int iCoord;                     /* Index of constrained coordinate */
124623   int op;                         /* Constraining operation */
124624   double rValue;                  /* Constraint value. */
124625   int (*xGeom)(sqlite3_rtree_geometry *, int, double *, int *);
124626   sqlite3_rtree_geometry *pGeom;  /* Constraint callback argument for a MATCH */
124627 };
124628 
124629 /* Possible values for RtreeConstraint.op */
124630 #define RTREE_EQ    0x41
124631 #define RTREE_LE    0x42
124632 #define RTREE_LT    0x43
124633 #define RTREE_GE    0x44
124634 #define RTREE_GT    0x45
124635 #define RTREE_MATCH 0x46
124636 
124637 /*
124638 ** An rtree structure node.
124639 */
124640 struct RtreeNode {
124641   RtreeNode *pParent;               /* Parent node */
124642   i64 iNode;
124643   int nRef;
124644   int isDirty;
124645   u8 *zData;
124646   RtreeNode *pNext;                 /* Next node in this hash chain */
124647 };
124648 #define NCELL(pNode) readInt16(&(pNode)->zData[2])
124649 
124650 /*
124651 ** Structure to store a deserialized rtree record.
124652 */
124653 struct RtreeCell {
124654   i64 iRowid;
124655   RtreeCoord aCoord[RTREE_MAX_DIMENSIONS*2];
124656 };
124657 
124658 
124659 /*
124660 ** Value for the first field of every RtreeMatchArg object. The MATCH
124661 ** operator tests that the first field of a blob operand matches this
124662 ** value to avoid operating on invalid blobs (which could cause a segfault).
124663 */
124664 #define RTREE_GEOMETRY_MAGIC 0x891245AB
124665 
124666 /*
124667 ** An instance of this structure must be supplied as a blob argument to
124668 ** the right-hand-side of an SQL MATCH operator used to constrain an
124669 ** r-tree query.
124670 */
124671 struct RtreeMatchArg {
124672   u32 magic;                      /* Always RTREE_GEOMETRY_MAGIC */
124673   int (*xGeom)(sqlite3_rtree_geometry *, int, double *, int *);
124674   void *pContext;
124675   int nParam;
124676   double aParam[1];
124677 };
124678 
124679 /*
124680 ** When a geometry callback is created (see sqlite3_rtree_geometry_callback),
124681 ** a single instance of the following structure is allocated. It is used
124682 ** as the context for the user-function created by by s_r_g_c(). The object
124683 ** is eventually deleted by the destructor mechanism provided by
124684 ** sqlite3_create_function_v2() (which is called by s_r_g_c() to create
124685 ** the geometry callback function).
124686 */
124687 struct RtreeGeomCallback {
124688   int (*xGeom)(sqlite3_rtree_geometry *, int, double *, int *);
124689   void *pContext;
124690 };
124691 
124692 #ifndef MAX
124693 # define MAX(x,y) ((x) < (y) ? (y) : (x))
124694 #endif
124695 #ifndef MIN
124696 # define MIN(x,y) ((x) > (y) ? (y) : (x))
124697 #endif
124698 
124699 /*
124700 ** Functions to deserialize a 16 bit integer, 32 bit real number and
124701 ** 64 bit integer. The deserialized value is returned.
124702 */
124703 static int readInt16(u8 *p){
124704   return (p[0]<<8) + p[1];
124705 }
124706 static void readCoord(u8 *p, RtreeCoord *pCoord){
124707   u32 i = (
124708     (((u32)p[0]) << 24) +
124709     (((u32)p[1]) << 16) +
124710     (((u32)p[2]) <<  8) +
124711     (((u32)p[3]) <<  0)
124712   );
124713   *(u32 *)pCoord = i;
124714 }
124715 static i64 readInt64(u8 *p){
124716   return (
124717     (((i64)p[0]) << 56) +
124718     (((i64)p[1]) << 48) +
124719     (((i64)p[2]) << 40) +
124720     (((i64)p[3]) << 32) +
124721     (((i64)p[4]) << 24) +
124722     (((i64)p[5]) << 16) +
124723     (((i64)p[6]) <<  8) +
124724     (((i64)p[7]) <<  0)
124725   );
124726 }
124727 
124728 /*
124729 ** Functions to serialize a 16 bit integer, 32 bit real number and
124730 ** 64 bit integer. The value returned is the number of bytes written
124731 ** to the argument buffer (always 2, 4 and 8 respectively).
124732 */
124733 static int writeInt16(u8 *p, int i){
124734   p[0] = (i>> 8)&0xFF;
124735   p[1] = (i>> 0)&0xFF;
124736   return 2;
124737 }
124738 static int writeCoord(u8 *p, RtreeCoord *pCoord){
124739   u32 i;
124740   assert( sizeof(RtreeCoord)==4 );
124741   assert( sizeof(u32)==4 );
124742   i = *(u32 *)pCoord;
124743   p[0] = (i>>24)&0xFF;
124744   p[1] = (i>>16)&0xFF;
124745   p[2] = (i>> 8)&0xFF;
124746   p[3] = (i>> 0)&0xFF;
124747   return 4;
124748 }
124749 static int writeInt64(u8 *p, i64 i){
124750   p[0] = (i>>56)&0xFF;
124751   p[1] = (i>>48)&0xFF;
124752   p[2] = (i>>40)&0xFF;
124753   p[3] = (i>>32)&0xFF;
124754   p[4] = (i>>24)&0xFF;
124755   p[5] = (i>>16)&0xFF;
124756   p[6] = (i>> 8)&0xFF;
124757   p[7] = (i>> 0)&0xFF;
124758   return 8;
124759 }
124760 
124761 /*
124762 ** Increment the reference count of node p.
124763 */
124764 static void nodeReference(RtreeNode *p){
124765   if( p ){
124766     p->nRef++;
124767   }
124768 }
124769 
124770 /*
124771 ** Clear the content of node p (set all bytes to 0x00).
124772 */
124773 static void nodeZero(Rtree *pRtree, RtreeNode *p){
124774   memset(&p->zData[2], 0, pRtree->iNodeSize-2);
124775   p->isDirty = 1;
124776 }
124777 
124778 /*
124779 ** Given a node number iNode, return the corresponding key to use
124780 ** in the Rtree.aHash table.
124781 */
124782 static int nodeHash(i64 iNode){
124783   return (
124784     (iNode>>56) ^ (iNode>>48) ^ (iNode>>40) ^ (iNode>>32) ^
124785     (iNode>>24) ^ (iNode>>16) ^ (iNode>> 8) ^ (iNode>> 0)
124786   ) % HASHSIZE;
124787 }
124788 
124789 /*
124790 ** Search the node hash table for node iNode. If found, return a pointer
124791 ** to it. Otherwise, return 0.
124792 */
124793 static RtreeNode *nodeHashLookup(Rtree *pRtree, i64 iNode){
124794   RtreeNode *p;
124795   for(p=pRtree->aHash[nodeHash(iNode)]; p && p->iNode!=iNode; p=p->pNext);
124796   return p;
124797 }
124798 
124799 /*
124800 ** Add node pNode to the node hash table.
124801 */
124802 static void nodeHashInsert(Rtree *pRtree, RtreeNode *pNode){
124803   int iHash;
124804   assert( pNode->pNext==0 );
124805   iHash = nodeHash(pNode->iNode);
124806   pNode->pNext = pRtree->aHash[iHash];
124807   pRtree->aHash[iHash] = pNode;
124808 }
124809 
124810 /*
124811 ** Remove node pNode from the node hash table.
124812 */
124813 static void nodeHashDelete(Rtree *pRtree, RtreeNode *pNode){
124814   RtreeNode **pp;
124815   if( pNode->iNode!=0 ){
124816     pp = &pRtree->aHash[nodeHash(pNode->iNode)];
124817     for( ; (*pp)!=pNode; pp = &(*pp)->pNext){ assert(*pp); }
124818     *pp = pNode->pNext;
124819     pNode->pNext = 0;
124820   }
124821 }
124822 
124823 /*
124824 ** Allocate and return new r-tree node. Initially, (RtreeNode.iNode==0),
124825 ** indicating that node has not yet been assigned a node number. It is
124826 ** assigned a node number when nodeWrite() is called to write the
124827 ** node contents out to the database.
124828 */
124829 static RtreeNode *nodeNew(Rtree *pRtree, RtreeNode *pParent){
124830   RtreeNode *pNode;
124831   pNode = (RtreeNode *)sqlite3_malloc(sizeof(RtreeNode) + pRtree->iNodeSize);
124832   if( pNode ){
124833     memset(pNode, 0, sizeof(RtreeNode) + pRtree->iNodeSize);
124834     pNode->zData = (u8 *)&pNode[1];
124835     pNode->nRef = 1;
124836     pNode->pParent = pParent;
124837     pNode->isDirty = 1;
124838     nodeReference(pParent);
124839   }
124840   return pNode;
124841 }
124842 
124843 /*
124844 ** Obtain a reference to an r-tree node.
124845 */
124846 static int
124847 nodeAcquire(
124848   Rtree *pRtree,             /* R-tree structure */
124849   i64 iNode,                 /* Node number to load */
124850   RtreeNode *pParent,        /* Either the parent node or NULL */
124851   RtreeNode **ppNode         /* OUT: Acquired node */
124852 ){
124853   int rc;
124854   int rc2 = SQLITE_OK;
124855   RtreeNode *pNode;
124856 
124857   /* Check if the requested node is already in the hash table. If so,
124858   ** increase its reference count and return it.
124859   */
124860   if( (pNode = nodeHashLookup(pRtree, iNode)) ){
124861     assert( !pParent || !pNode->pParent || pNode->pParent==pParent );
124862     if( pParent && !pNode->pParent ){
124863       nodeReference(pParent);
124864       pNode->pParent = pParent;
124865     }
124866     pNode->nRef++;
124867     *ppNode = pNode;
124868     return SQLITE_OK;
124869   }
124870 
124871   sqlite3_bind_int64(pRtree->pReadNode, 1, iNode);
124872   rc = sqlite3_step(pRtree->pReadNode);
124873   if( rc==SQLITE_ROW ){
124874     const u8 *zBlob = sqlite3_column_blob(pRtree->pReadNode, 0);
124875     if( pRtree->iNodeSize==sqlite3_column_bytes(pRtree->pReadNode, 0) ){
124876       pNode = (RtreeNode *)sqlite3_malloc(sizeof(RtreeNode)+pRtree->iNodeSize);
124877       if( !pNode ){
124878         rc2 = SQLITE_NOMEM;
124879       }else{
124880         pNode->pParent = pParent;
124881         pNode->zData = (u8 *)&pNode[1];
124882         pNode->nRef = 1;
124883         pNode->iNode = iNode;
124884         pNode->isDirty = 0;
124885         pNode->pNext = 0;
124886         memcpy(pNode->zData, zBlob, pRtree->iNodeSize);
124887         nodeReference(pParent);
124888       }
124889     }
124890   }
124891   rc = sqlite3_reset(pRtree->pReadNode);
124892   if( rc==SQLITE_OK ) rc = rc2;
124893 
124894   /* If the root node was just loaded, set pRtree->iDepth to the height
124895   ** of the r-tree structure. A height of zero means all data is stored on
124896   ** the root node. A height of one means the children of the root node
124897   ** are the leaves, and so on. If the depth as specified on the root node
124898   ** is greater than RTREE_MAX_DEPTH, the r-tree structure must be corrupt.
124899   */
124900   if( pNode && iNode==1 ){
124901     pRtree->iDepth = readInt16(pNode->zData);
124902     if( pRtree->iDepth>RTREE_MAX_DEPTH ){
124903       rc = SQLITE_CORRUPT_VTAB;
124904     }
124905   }
124906 
124907   /* If no error has occurred so far, check if the "number of entries"
124908   ** field on the node is too large. If so, set the return code to
124909   ** SQLITE_CORRUPT_VTAB.
124910   */
124911   if( pNode && rc==SQLITE_OK ){
124912     if( NCELL(pNode)>((pRtree->iNodeSize-4)/pRtree->nBytesPerCell) ){
124913       rc = SQLITE_CORRUPT_VTAB;
124914     }
124915   }
124916 
124917   if( rc==SQLITE_OK ){
124918     if( pNode!=0 ){
124919       nodeHashInsert(pRtree, pNode);
124920     }else{
124921       rc = SQLITE_CORRUPT_VTAB;
124922     }
124923     *ppNode = pNode;
124924   }else{
124925     sqlite3_free(pNode);
124926     *ppNode = 0;
124927   }
124928 
124929   return rc;
124930 }
124931 
124932 /*
124933 ** Overwrite cell iCell of node pNode with the contents of pCell.
124934 */
124935 static void nodeOverwriteCell(
124936   Rtree *pRtree,
124937   RtreeNode *pNode,
124938   RtreeCell *pCell,
124939   int iCell
124940 ){
124941   int ii;
124942   u8 *p = &pNode->zData[4 + pRtree->nBytesPerCell*iCell];
124943   p += writeInt64(p, pCell->iRowid);
124944   for(ii=0; ii<(pRtree->nDim*2); ii++){
124945     p += writeCoord(p, &pCell->aCoord[ii]);
124946   }
124947   pNode->isDirty = 1;
124948 }
124949 
124950 /*
124951 ** Remove cell the cell with index iCell from node pNode.
124952 */
124953 static void nodeDeleteCell(Rtree *pRtree, RtreeNode *pNode, int iCell){
124954   u8 *pDst = &pNode->zData[4 + pRtree->nBytesPerCell*iCell];
124955   u8 *pSrc = &pDst[pRtree->nBytesPerCell];
124956   int nByte = (NCELL(pNode) - iCell - 1) * pRtree->nBytesPerCell;
124957   memmove(pDst, pSrc, nByte);
124958   writeInt16(&pNode->zData[2], NCELL(pNode)-1);
124959   pNode->isDirty = 1;
124960 }
124961 
124962 /*
124963 ** Insert the contents of cell pCell into node pNode. If the insert
124964 ** is successful, return SQLITE_OK.
124965 **
124966 ** If there is not enough free space in pNode, return SQLITE_FULL.
124967 */
124968 static int
124969 nodeInsertCell(
124970   Rtree *pRtree,
124971   RtreeNode *pNode,
124972   RtreeCell *pCell
124973 ){
124974   int nCell;                    /* Current number of cells in pNode */
124975   int nMaxCell;                 /* Maximum number of cells for pNode */
124976 
124977   nMaxCell = (pRtree->iNodeSize-4)/pRtree->nBytesPerCell;
124978   nCell = NCELL(pNode);
124979 
124980   assert( nCell<=nMaxCell );
124981   if( nCell<nMaxCell ){
124982     nodeOverwriteCell(pRtree, pNode, pCell, nCell);
124983     writeInt16(&pNode->zData[2], nCell+1);
124984     pNode->isDirty = 1;
124985   }
124986 
124987   return (nCell==nMaxCell);
124988 }
124989 
124990 /*
124991 ** If the node is dirty, write it out to the database.
124992 */
124993 static int
124994 nodeWrite(Rtree *pRtree, RtreeNode *pNode){
124995   int rc = SQLITE_OK;
124996   if( pNode->isDirty ){
124997     sqlite3_stmt *p = pRtree->pWriteNode;
124998     if( pNode->iNode ){
124999       sqlite3_bind_int64(p, 1, pNode->iNode);
125000     }else{
125001       sqlite3_bind_null(p, 1);
125002     }
125003     sqlite3_bind_blob(p, 2, pNode->zData, pRtree->iNodeSize, SQLITE_STATIC);
125004     sqlite3_step(p);
125005     pNode->isDirty = 0;
125006     rc = sqlite3_reset(p);
125007     if( pNode->iNode==0 && rc==SQLITE_OK ){
125008       pNode->iNode = sqlite3_last_insert_rowid(pRtree->db);
125009       nodeHashInsert(pRtree, pNode);
125010     }
125011   }
125012   return rc;
125013 }
125014 
125015 /*
125016 ** Release a reference to a node. If the node is dirty and the reference
125017 ** count drops to zero, the node data is written to the database.
125018 */
125019 static int
125020 nodeRelease(Rtree *pRtree, RtreeNode *pNode){
125021   int rc = SQLITE_OK;
125022   if( pNode ){
125023     assert( pNode->nRef>0 );
125024     pNode->nRef--;
125025     if( pNode->nRef==0 ){
125026       if( pNode->iNode==1 ){
125027         pRtree->iDepth = -1;
125028       }
125029       if( pNode->pParent ){
125030         rc = nodeRelease(pRtree, pNode->pParent);
125031       }
125032       if( rc==SQLITE_OK ){
125033         rc = nodeWrite(pRtree, pNode);
125034       }
125035       nodeHashDelete(pRtree, pNode);
125036       sqlite3_free(pNode);
125037     }
125038   }
125039   return rc;
125040 }
125041 
125042 /*
125043 ** Return the 64-bit integer value associated with cell iCell of
125044 ** node pNode. If pNode is a leaf node, this is a rowid. If it is
125045 ** an internal node, then the 64-bit integer is a child page number.
125046 */
125047 static i64 nodeGetRowid(
125048   Rtree *pRtree,
125049   RtreeNode *pNode,
125050   int iCell
125051 ){
125052   assert( iCell<NCELL(pNode) );
125053   return readInt64(&pNode->zData[4 + pRtree->nBytesPerCell*iCell]);
125054 }
125055 
125056 /*
125057 ** Return coordinate iCoord from cell iCell in node pNode.
125058 */
125059 static void nodeGetCoord(
125060   Rtree *pRtree,
125061   RtreeNode *pNode,
125062   int iCell,
125063   int iCoord,
125064   RtreeCoord *pCoord           /* Space to write result to */
125065 ){
125066   readCoord(&pNode->zData[12 + pRtree->nBytesPerCell*iCell + 4*iCoord], pCoord);
125067 }
125068 
125069 /*
125070 ** Deserialize cell iCell of node pNode. Populate the structure pointed
125071 ** to by pCell with the results.
125072 */
125073 static void nodeGetCell(
125074   Rtree *pRtree,
125075   RtreeNode *pNode,
125076   int iCell,
125077   RtreeCell *pCell
125078 ){
125079   int ii;
125080   pCell->iRowid = nodeGetRowid(pRtree, pNode, iCell);
125081   for(ii=0; ii<pRtree->nDim*2; ii++){
125082     nodeGetCoord(pRtree, pNode, iCell, ii, &pCell->aCoord[ii]);
125083   }
125084 }
125085 
125086 
125087 /* Forward declaration for the function that does the work of
125088 ** the virtual table module xCreate() and xConnect() methods.
125089 */
125090 static int rtreeInit(
125091   sqlite3 *, void *, int, const char *const*, sqlite3_vtab **, char **, int
125092 );
125093 
125094 /*
125095 ** Rtree virtual table module xCreate method.
125096 */
125097 static int rtreeCreate(
125098   sqlite3 *db,
125099   void *pAux,
125100   int argc, const char *const*argv,
125101   sqlite3_vtab **ppVtab,
125102   char **pzErr
125103 ){
125104   return rtreeInit(db, pAux, argc, argv, ppVtab, pzErr, 1);
125105 }
125106 
125107 /*
125108 ** Rtree virtual table module xConnect method.
125109 */
125110 static int rtreeConnect(
125111   sqlite3 *db,
125112   void *pAux,
125113   int argc, const char *const*argv,
125114   sqlite3_vtab **ppVtab,
125115   char **pzErr
125116 ){
125117   return rtreeInit(db, pAux, argc, argv, ppVtab, pzErr, 0);
125118 }
125119 
125120 /*
125121 ** Increment the r-tree reference count.
125122 */
125123 static void rtreeReference(Rtree *pRtree){
125124   pRtree->nBusy++;
125125 }
125126 
125127 /*
125128 ** Decrement the r-tree reference count. When the reference count reaches
125129 ** zero the structure is deleted.
125130 */
125131 static void rtreeRelease(Rtree *pRtree){
125132   pRtree->nBusy--;
125133   if( pRtree->nBusy==0 ){
125134     sqlite3_finalize(pRtree->pReadNode);
125135     sqlite3_finalize(pRtree->pWriteNode);
125136     sqlite3_finalize(pRtree->pDeleteNode);
125137     sqlite3_finalize(pRtree->pReadRowid);
125138     sqlite3_finalize(pRtree->pWriteRowid);
125139     sqlite3_finalize(pRtree->pDeleteRowid);
125140     sqlite3_finalize(pRtree->pReadParent);
125141     sqlite3_finalize(pRtree->pWriteParent);
125142     sqlite3_finalize(pRtree->pDeleteParent);
125143     sqlite3_free(pRtree);
125144   }
125145 }
125146 
125147 /*
125148 ** Rtree virtual table module xDisconnect method.
125149 */
125150 static int rtreeDisconnect(sqlite3_vtab *pVtab){
125151   rtreeRelease((Rtree *)pVtab);
125152   return SQLITE_OK;
125153 }
125154 
125155 /*
125156 ** Rtree virtual table module xDestroy method.
125157 */
125158 static int rtreeDestroy(sqlite3_vtab *pVtab){
125159   Rtree *pRtree = (Rtree *)pVtab;
125160   int rc;
125161   char *zCreate = sqlite3_mprintf(
125162     "DROP TABLE '%q'.'%q_node';"
125163     "DROP TABLE '%q'.'%q_rowid';"
125164     "DROP TABLE '%q'.'%q_parent';",
125165     pRtree->zDb, pRtree->zName,
125166     pRtree->zDb, pRtree->zName,
125167     pRtree->zDb, pRtree->zName
125168   );
125169   if( !zCreate ){
125170     rc = SQLITE_NOMEM;
125171   }else{
125172     rc = sqlite3_exec(pRtree->db, zCreate, 0, 0, 0);
125173     sqlite3_free(zCreate);
125174   }
125175   if( rc==SQLITE_OK ){
125176     rtreeRelease(pRtree);
125177   }
125178 
125179   return rc;
125180 }
125181 
125182 /*
125183 ** Rtree virtual table module xOpen method.
125184 */
125185 static int rtreeOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
125186   int rc = SQLITE_NOMEM;
125187   RtreeCursor *pCsr;
125188 
125189   pCsr = (RtreeCursor *)sqlite3_malloc(sizeof(RtreeCursor));
125190   if( pCsr ){
125191     memset(pCsr, 0, sizeof(RtreeCursor));
125192     pCsr->base.pVtab = pVTab;
125193     rc = SQLITE_OK;
125194   }
125195   *ppCursor = (sqlite3_vtab_cursor *)pCsr;
125196 
125197   return rc;
125198 }
125199 
125200 
125201 /*
125202 ** Free the RtreeCursor.aConstraint[] array and its contents.
125203 */
125204 static void freeCursorConstraints(RtreeCursor *pCsr){
125205   if( pCsr->aConstraint ){
125206     int i;                        /* Used to iterate through constraint array */
125207     for(i=0; i<pCsr->nConstraint; i++){
125208       sqlite3_rtree_geometry *pGeom = pCsr->aConstraint[i].pGeom;
125209       if( pGeom ){
125210         if( pGeom->xDelUser ) pGeom->xDelUser(pGeom->pUser);
125211         sqlite3_free(pGeom);
125212       }
125213     }
125214     sqlite3_free(pCsr->aConstraint);
125215     pCsr->aConstraint = 0;
125216   }
125217 }
125218 
125219 /*
125220 ** Rtree virtual table module xClose method.
125221 */
125222 static int rtreeClose(sqlite3_vtab_cursor *cur){
125223   Rtree *pRtree = (Rtree *)(cur->pVtab);
125224   int rc;
125225   RtreeCursor *pCsr = (RtreeCursor *)cur;
125226   freeCursorConstraints(pCsr);
125227   rc = nodeRelease(pRtree, pCsr->pNode);
125228   sqlite3_free(pCsr);
125229   return rc;
125230 }
125231 
125232 /*
125233 ** Rtree virtual table module xEof method.
125234 **
125235 ** Return non-zero if the cursor does not currently point to a valid
125236 ** record (i.e if the scan has finished), or zero otherwise.
125237 */
125238 static int rtreeEof(sqlite3_vtab_cursor *cur){
125239   RtreeCursor *pCsr = (RtreeCursor *)cur;
125240   return (pCsr->pNode==0);
125241 }
125242 
125243 /*
125244 ** The r-tree constraint passed as the second argument to this function is
125245 ** guaranteed to be a MATCH constraint.
125246 */
125247 static int testRtreeGeom(
125248   Rtree *pRtree,                  /* R-Tree object */
125249   RtreeConstraint *pConstraint,   /* MATCH constraint to test */
125250   RtreeCell *pCell,               /* Cell to test */
125251   int *pbRes                      /* OUT: Test result */
125252 ){
125253   int i;
125254   double aCoord[RTREE_MAX_DIMENSIONS*2];
125255   int nCoord = pRtree->nDim*2;
125256 
125257   assert( pConstraint->op==RTREE_MATCH );
125258   assert( pConstraint->pGeom );
125259 
125260   for(i=0; i<nCoord; i++){
125261     aCoord[i] = DCOORD(pCell->aCoord[i]);
125262   }
125263   return pConstraint->xGeom(pConstraint->pGeom, nCoord, aCoord, pbRes);
125264 }
125265 
125266 /*
125267 ** Cursor pCursor currently points to a cell in a non-leaf page.
125268 ** Set *pbEof to true if the sub-tree headed by the cell is filtered
125269 ** (excluded) by the constraints in the pCursor->aConstraint[]
125270 ** array, or false otherwise.
125271 **
125272 ** Return SQLITE_OK if successful or an SQLite error code if an error
125273 ** occurs within a geometry callback.
125274 */
125275 static int testRtreeCell(Rtree *pRtree, RtreeCursor *pCursor, int *pbEof){
125276   RtreeCell cell;
125277   int ii;
125278   int bRes = 0;
125279   int rc = SQLITE_OK;
125280 
125281   nodeGetCell(pRtree, pCursor->pNode, pCursor->iCell, &cell);
125282   for(ii=0; bRes==0 && ii<pCursor->nConstraint; ii++){
125283     RtreeConstraint *p = &pCursor->aConstraint[ii];
125284     double cell_min = DCOORD(cell.aCoord[(p->iCoord>>1)*2]);
125285     double cell_max = DCOORD(cell.aCoord[(p->iCoord>>1)*2+1]);
125286 
125287     assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE
125288         || p->op==RTREE_GT || p->op==RTREE_EQ || p->op==RTREE_MATCH
125289     );
125290 
125291     switch( p->op ){
125292       case RTREE_LE: case RTREE_LT:
125293         bRes = p->rValue<cell_min;
125294         break;
125295 
125296       case RTREE_GE: case RTREE_GT:
125297         bRes = p->rValue>cell_max;
125298         break;
125299 
125300       case RTREE_EQ:
125301         bRes = (p->rValue>cell_max || p->rValue<cell_min);
125302         break;
125303 
125304       default: {
125305         assert( p->op==RTREE_MATCH );
125306         rc = testRtreeGeom(pRtree, p, &cell, &bRes);
125307         bRes = !bRes;
125308         break;
125309       }
125310     }
125311   }
125312 
125313   *pbEof = bRes;
125314   return rc;
125315 }
125316 
125317 /*
125318 ** Test if the cell that cursor pCursor currently points to
125319 ** would be filtered (excluded) by the constraints in the
125320 ** pCursor->aConstraint[] array. If so, set *pbEof to true before
125321 ** returning. If the cell is not filtered (excluded) by the constraints,
125322 ** set pbEof to zero.
125323 **
125324 ** Return SQLITE_OK if successful or an SQLite error code if an error
125325 ** occurs within a geometry callback.
125326 **
125327 ** This function assumes that the cell is part of a leaf node.
125328 */
125329 static int testRtreeEntry(Rtree *pRtree, RtreeCursor *pCursor, int *pbEof){
125330   RtreeCell cell;
125331   int ii;
125332   *pbEof = 0;
125333 
125334   nodeGetCell(pRtree, pCursor->pNode, pCursor->iCell, &cell);
125335   for(ii=0; ii<pCursor->nConstraint; ii++){
125336     RtreeConstraint *p = &pCursor->aConstraint[ii];
125337     double coord = DCOORD(cell.aCoord[p->iCoord]);
125338     int res;
125339     assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE
125340         || p->op==RTREE_GT || p->op==RTREE_EQ || p->op==RTREE_MATCH
125341     );
125342     switch( p->op ){
125343       case RTREE_LE: res = (coord<=p->rValue); break;
125344       case RTREE_LT: res = (coord<p->rValue);  break;
125345       case RTREE_GE: res = (coord>=p->rValue); break;
125346       case RTREE_GT: res = (coord>p->rValue);  break;
125347       case RTREE_EQ: res = (coord==p->rValue); break;
125348       default: {
125349         int rc;
125350         assert( p->op==RTREE_MATCH );
125351         rc = testRtreeGeom(pRtree, p, &cell, &res);
125352         if( rc!=SQLITE_OK ){
125353           return rc;
125354         }
125355         break;
125356       }
125357     }
125358 
125359     if( !res ){
125360       *pbEof = 1;
125361       return SQLITE_OK;
125362     }
125363   }
125364 
125365   return SQLITE_OK;
125366 }
125367 
125368 /*
125369 ** Cursor pCursor currently points at a node that heads a sub-tree of
125370 ** height iHeight (if iHeight==0, then the node is a leaf). Descend
125371 ** to point to the left-most cell of the sub-tree that matches the
125372 ** configured constraints.
125373 */
125374 static int descendToCell(
125375   Rtree *pRtree,
125376   RtreeCursor *pCursor,
125377   int iHeight,
125378   int *pEof                 /* OUT: Set to true if cannot descend */
125379 ){
125380   int isEof;
125381   int rc;
125382   int ii;
125383   RtreeNode *pChild;
125384   sqlite3_int64 iRowid;
125385 
125386   RtreeNode *pSavedNode = pCursor->pNode;
125387   int iSavedCell = pCursor->iCell;
125388 
125389   assert( iHeight>=0 );
125390 
125391   if( iHeight==0 ){
125392     rc = testRtreeEntry(pRtree, pCursor, &isEof);
125393   }else{
125394     rc = testRtreeCell(pRtree, pCursor, &isEof);
125395   }
125396   if( rc!=SQLITE_OK || isEof || iHeight==0 ){
125397     goto descend_to_cell_out;
125398   }
125399 
125400   iRowid = nodeGetRowid(pRtree, pCursor->pNode, pCursor->iCell);
125401   rc = nodeAcquire(pRtree, iRowid, pCursor->pNode, &pChild);
125402   if( rc!=SQLITE_OK ){
125403     goto descend_to_cell_out;
125404   }
125405 
125406   nodeRelease(pRtree, pCursor->pNode);
125407   pCursor->pNode = pChild;
125408   isEof = 1;
125409   for(ii=0; isEof && ii<NCELL(pChild); ii++){
125410     pCursor->iCell = ii;
125411     rc = descendToCell(pRtree, pCursor, iHeight-1, &isEof);
125412     if( rc!=SQLITE_OK ){
125413       goto descend_to_cell_out;
125414     }
125415   }
125416 
125417   if( isEof ){
125418     assert( pCursor->pNode==pChild );
125419     nodeReference(pSavedNode);
125420     nodeRelease(pRtree, pChild);
125421     pCursor->pNode = pSavedNode;
125422     pCursor->iCell = iSavedCell;
125423   }
125424 
125425 descend_to_cell_out:
125426   *pEof = isEof;
125427   return rc;
125428 }
125429 
125430 /*
125431 ** One of the cells in node pNode is guaranteed to have a 64-bit
125432 ** integer value equal to iRowid. Return the index of this cell.
125433 */
125434 static int nodeRowidIndex(
125435   Rtree *pRtree,
125436   RtreeNode *pNode,
125437   i64 iRowid,
125438   int *piIndex
125439 ){
125440   int ii;
125441   int nCell = NCELL(pNode);
125442   for(ii=0; ii<nCell; ii++){
125443     if( nodeGetRowid(pRtree, pNode, ii)==iRowid ){
125444       *piIndex = ii;
125445       return SQLITE_OK;
125446     }
125447   }
125448   return SQLITE_CORRUPT_VTAB;
125449 }
125450 
125451 /*
125452 ** Return the index of the cell containing a pointer to node pNode
125453 ** in its parent. If pNode is the root node, return -1.
125454 */
125455 static int nodeParentIndex(Rtree *pRtree, RtreeNode *pNode, int *piIndex){
125456   RtreeNode *pParent = pNode->pParent;
125457   if( pParent ){
125458     return nodeRowidIndex(pRtree, pParent, pNode->iNode, piIndex);
125459   }
125460   *piIndex = -1;
125461   return SQLITE_OK;
125462 }
125463 
125464 /*
125465 ** Rtree virtual table module xNext method.
125466 */
125467 static int rtreeNext(sqlite3_vtab_cursor *pVtabCursor){
125468   Rtree *pRtree = (Rtree *)(pVtabCursor->pVtab);
125469   RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
125470   int rc = SQLITE_OK;
125471 
125472   /* RtreeCursor.pNode must not be NULL. If is is NULL, then this cursor is
125473   ** already at EOF. It is against the rules to call the xNext() method of
125474   ** a cursor that has already reached EOF.
125475   */
125476   assert( pCsr->pNode );
125477 
125478   if( pCsr->iStrategy==1 ){
125479     /* This "scan" is a direct lookup by rowid. There is no next entry. */
125480     nodeRelease(pRtree, pCsr->pNode);
125481     pCsr->pNode = 0;
125482   }else{
125483     /* Move to the next entry that matches the configured constraints. */
125484     int iHeight = 0;
125485     while( pCsr->pNode ){
125486       RtreeNode *pNode = pCsr->pNode;
125487       int nCell = NCELL(pNode);
125488       for(pCsr->iCell++; pCsr->iCell<nCell; pCsr->iCell++){
125489         int isEof;
125490         rc = descendToCell(pRtree, pCsr, iHeight, &isEof);
125491         if( rc!=SQLITE_OK || !isEof ){
125492           return rc;
125493         }
125494       }
125495       pCsr->pNode = pNode->pParent;
125496       rc = nodeParentIndex(pRtree, pNode, &pCsr->iCell);
125497       if( rc!=SQLITE_OK ){
125498         return rc;
125499       }
125500       nodeReference(pCsr->pNode);
125501       nodeRelease(pRtree, pNode);
125502       iHeight++;
125503     }
125504   }
125505 
125506   return rc;
125507 }
125508 
125509 /*
125510 ** Rtree virtual table module xRowid method.
125511 */
125512 static int rtreeRowid(sqlite3_vtab_cursor *pVtabCursor, sqlite_int64 *pRowid){
125513   Rtree *pRtree = (Rtree *)pVtabCursor->pVtab;
125514   RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
125515 
125516   assert(pCsr->pNode);
125517   *pRowid = nodeGetRowid(pRtree, pCsr->pNode, pCsr->iCell);
125518 
125519   return SQLITE_OK;
125520 }
125521 
125522 /*
125523 ** Rtree virtual table module xColumn method.
125524 */
125525 static int rtreeColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
125526   Rtree *pRtree = (Rtree *)cur->pVtab;
125527   RtreeCursor *pCsr = (RtreeCursor *)cur;
125528 
125529   if( i==0 ){
125530     i64 iRowid = nodeGetRowid(pRtree, pCsr->pNode, pCsr->iCell);
125531     sqlite3_result_int64(ctx, iRowid);
125532   }else{
125533     RtreeCoord c;
125534     nodeGetCoord(pRtree, pCsr->pNode, pCsr->iCell, i-1, &c);
125535     if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
125536       sqlite3_result_double(ctx, c.f);
125537     }else{
125538       assert( pRtree->eCoordType==RTREE_COORD_INT32 );
125539       sqlite3_result_int(ctx, c.i);
125540     }
125541   }
125542 
125543   return SQLITE_OK;
125544 }
125545 
125546 /*
125547 ** Use nodeAcquire() to obtain the leaf node containing the record with
125548 ** rowid iRowid. If successful, set *ppLeaf to point to the node and
125549 ** return SQLITE_OK. If there is no such record in the table, set
125550 ** *ppLeaf to 0 and return SQLITE_OK. If an error occurs, set *ppLeaf
125551 ** to zero and return an SQLite error code.
125552 */
125553 static int findLeafNode(Rtree *pRtree, i64 iRowid, RtreeNode **ppLeaf){
125554   int rc;
125555   *ppLeaf = 0;
125556   sqlite3_bind_int64(pRtree->pReadRowid, 1, iRowid);
125557   if( sqlite3_step(pRtree->pReadRowid)==SQLITE_ROW ){
125558     i64 iNode = sqlite3_column_int64(pRtree->pReadRowid, 0);
125559     rc = nodeAcquire(pRtree, iNode, 0, ppLeaf);
125560     sqlite3_reset(pRtree->pReadRowid);
125561   }else{
125562     rc = sqlite3_reset(pRtree->pReadRowid);
125563   }
125564   return rc;
125565 }
125566 
125567 /*
125568 ** This function is called to configure the RtreeConstraint object passed
125569 ** as the second argument for a MATCH constraint. The value passed as the
125570 ** first argument to this function is the right-hand operand to the MATCH
125571 ** operator.
125572 */
125573 static int deserializeGeometry(sqlite3_value *pValue, RtreeConstraint *pCons){
125574   RtreeMatchArg *p;
125575   sqlite3_rtree_geometry *pGeom;
125576   int nBlob;
125577 
125578   /* Check that value is actually a blob. */
125579   if( !sqlite3_value_type(pValue)==SQLITE_BLOB ) return SQLITE_ERROR;
125580 
125581   /* Check that the blob is roughly the right size. */
125582   nBlob = sqlite3_value_bytes(pValue);
125583   if( nBlob<(int)sizeof(RtreeMatchArg)
125584    || ((nBlob-sizeof(RtreeMatchArg))%sizeof(double))!=0
125585   ){
125586     return SQLITE_ERROR;
125587   }
125588 
125589   pGeom = (sqlite3_rtree_geometry *)sqlite3_malloc(
125590       sizeof(sqlite3_rtree_geometry) + nBlob
125591   );
125592   if( !pGeom ) return SQLITE_NOMEM;
125593   memset(pGeom, 0, sizeof(sqlite3_rtree_geometry));
125594   p = (RtreeMatchArg *)&pGeom[1];
125595 
125596   memcpy(p, sqlite3_value_blob(pValue), nBlob);
125597   if( p->magic!=RTREE_GEOMETRY_MAGIC
125598    || nBlob!=(int)(sizeof(RtreeMatchArg) + (p->nParam-1)*sizeof(double))
125599   ){
125600     sqlite3_free(pGeom);
125601     return SQLITE_ERROR;
125602   }
125603 
125604   pGeom->pContext = p->pContext;
125605   pGeom->nParam = p->nParam;
125606   pGeom->aParam = p->aParam;
125607 
125608   pCons->xGeom = p->xGeom;
125609   pCons->pGeom = pGeom;
125610   return SQLITE_OK;
125611 }
125612 
125613 /*
125614 ** Rtree virtual table module xFilter method.
125615 */
125616 static int rtreeFilter(
125617   sqlite3_vtab_cursor *pVtabCursor,
125618   int idxNum, const char *idxStr,
125619   int argc, sqlite3_value **argv
125620 ){
125621   Rtree *pRtree = (Rtree *)pVtabCursor->pVtab;
125622   RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
125623 
125624   RtreeNode *pRoot = 0;
125625   int ii;
125626   int rc = SQLITE_OK;
125627 
125628   rtreeReference(pRtree);
125629 
125630   freeCursorConstraints(pCsr);
125631   pCsr->iStrategy = idxNum;
125632 
125633   if( idxNum==1 ){
125634     /* Special case - lookup by rowid. */
125635     RtreeNode *pLeaf;        /* Leaf on which the required cell resides */
125636     i64 iRowid = sqlite3_value_int64(argv[0]);
125637     rc = findLeafNode(pRtree, iRowid, &pLeaf);
125638     pCsr->pNode = pLeaf;
125639     if( pLeaf ){
125640       assert( rc==SQLITE_OK );
125641       rc = nodeRowidIndex(pRtree, pLeaf, iRowid, &pCsr->iCell);
125642     }
125643   }else{
125644     /* Normal case - r-tree scan. Set up the RtreeCursor.aConstraint array
125645     ** with the configured constraints.
125646     */
125647     if( argc>0 ){
125648       pCsr->aConstraint = sqlite3_malloc(sizeof(RtreeConstraint)*argc);
125649       pCsr->nConstraint = argc;
125650       if( !pCsr->aConstraint ){
125651         rc = SQLITE_NOMEM;
125652       }else{
125653         memset(pCsr->aConstraint, 0, sizeof(RtreeConstraint)*argc);
125654         assert( (idxStr==0 && argc==0) || (int)strlen(idxStr)==argc*2 );
125655         for(ii=0; ii<argc; ii++){
125656           RtreeConstraint *p = &pCsr->aConstraint[ii];
125657           p->op = idxStr[ii*2];
125658           p->iCoord = idxStr[ii*2+1]-'a';
125659           if( p->op==RTREE_MATCH ){
125660             /* A MATCH operator. The right-hand-side must be a blob that
125661             ** can be cast into an RtreeMatchArg object. One created using
125662             ** an sqlite3_rtree_geometry_callback() SQL user function.
125663             */
125664             rc = deserializeGeometry(argv[ii], p);
125665             if( rc!=SQLITE_OK ){
125666               break;
125667             }
125668           }else{
125669             p->rValue = sqlite3_value_double(argv[ii]);
125670           }
125671         }
125672       }
125673     }
125674 
125675     if( rc==SQLITE_OK ){
125676       pCsr->pNode = 0;
125677       rc = nodeAcquire(pRtree, 1, 0, &pRoot);
125678     }
125679     if( rc==SQLITE_OK ){
125680       int isEof = 1;
125681       int nCell = NCELL(pRoot);
125682       pCsr->pNode = pRoot;
125683       for(pCsr->iCell=0; rc==SQLITE_OK && pCsr->iCell<nCell; pCsr->iCell++){
125684         assert( pCsr->pNode==pRoot );
125685         rc = descendToCell(pRtree, pCsr, pRtree->iDepth, &isEof);
125686         if( !isEof ){
125687           break;
125688         }
125689       }
125690       if( rc==SQLITE_OK && isEof ){
125691         assert( pCsr->pNode==pRoot );
125692         nodeRelease(pRtree, pRoot);
125693         pCsr->pNode = 0;
125694       }
125695       assert( rc!=SQLITE_OK || !pCsr->pNode || pCsr->iCell<NCELL(pCsr->pNode) );
125696     }
125697   }
125698 
125699   rtreeRelease(pRtree);
125700   return rc;
125701 }
125702 
125703 /*
125704 ** Rtree virtual table module xBestIndex method. There are three
125705 ** table scan strategies to choose from (in order from most to
125706 ** least desirable):
125707 **
125708 **   idxNum     idxStr        Strategy
125709 **   ------------------------------------------------
125710 **     1        Unused        Direct lookup by rowid.
125711 **     2        See below     R-tree query or full-table scan.
125712 **   ------------------------------------------------
125713 **
125714 ** If strategy 1 is used, then idxStr is not meaningful. If strategy
125715 ** 2 is used, idxStr is formatted to contain 2 bytes for each
125716 ** constraint used. The first two bytes of idxStr correspond to
125717 ** the constraint in sqlite3_index_info.aConstraintUsage[] with
125718 ** (argvIndex==1) etc.
125719 **
125720 ** The first of each pair of bytes in idxStr identifies the constraint
125721 ** operator as follows:
125722 **
125723 **   Operator    Byte Value
125724 **   ----------------------
125725 **      =        0x41 ('A')
125726 **     <=        0x42 ('B')
125727 **      <        0x43 ('C')
125728 **     >=        0x44 ('D')
125729 **      >        0x45 ('E')
125730 **   MATCH       0x46 ('F')
125731 **   ----------------------
125732 **
125733 ** The second of each pair of bytes identifies the coordinate column
125734 ** to which the constraint applies. The leftmost coordinate column
125735 ** is 'a', the second from the left 'b' etc.
125736 */
125737 static int rtreeBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
125738   int rc = SQLITE_OK;
125739   int ii;
125740 
125741   int iIdx = 0;
125742   char zIdxStr[RTREE_MAX_DIMENSIONS*8+1];
125743   memset(zIdxStr, 0, sizeof(zIdxStr));
125744   UNUSED_PARAMETER(tab);
125745 
125746   assert( pIdxInfo->idxStr==0 );
125747   for(ii=0; ii<pIdxInfo->nConstraint && iIdx<(int)(sizeof(zIdxStr)-1); ii++){
125748     struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[ii];
125749 
125750     if( p->usable && p->iColumn==0 && p->op==SQLITE_INDEX_CONSTRAINT_EQ ){
125751       /* We have an equality constraint on the rowid. Use strategy 1. */
125752       int jj;
125753       for(jj=0; jj<ii; jj++){
125754         pIdxInfo->aConstraintUsage[jj].argvIndex = 0;
125755         pIdxInfo->aConstraintUsage[jj].omit = 0;
125756       }
125757       pIdxInfo->idxNum = 1;
125758       pIdxInfo->aConstraintUsage[ii].argvIndex = 1;
125759       pIdxInfo->aConstraintUsage[jj].omit = 1;
125760 
125761       /* This strategy involves a two rowid lookups on an B-Tree structures
125762       ** and then a linear search of an R-Tree node. This should be
125763       ** considered almost as quick as a direct rowid lookup (for which
125764       ** sqlite uses an internal cost of 0.0).
125765       */
125766       pIdxInfo->estimatedCost = 10.0;
125767       return SQLITE_OK;
125768     }
125769 
125770     if( p->usable && (p->iColumn>0 || p->op==SQLITE_INDEX_CONSTRAINT_MATCH) ){
125771       u8 op;
125772       switch( p->op ){
125773         case SQLITE_INDEX_CONSTRAINT_EQ: op = RTREE_EQ; break;
125774         case SQLITE_INDEX_CONSTRAINT_GT: op = RTREE_GT; break;
125775         case SQLITE_INDEX_CONSTRAINT_LE: op = RTREE_LE; break;
125776         case SQLITE_INDEX_CONSTRAINT_LT: op = RTREE_LT; break;
125777         case SQLITE_INDEX_CONSTRAINT_GE: op = RTREE_GE; break;
125778         default:
125779           assert( p->op==SQLITE_INDEX_CONSTRAINT_MATCH );
125780           op = RTREE_MATCH;
125781           break;
125782       }
125783       zIdxStr[iIdx++] = op;
125784       zIdxStr[iIdx++] = p->iColumn - 1 + 'a';
125785       pIdxInfo->aConstraintUsage[ii].argvIndex = (iIdx/2);
125786       pIdxInfo->aConstraintUsage[ii].omit = 1;
125787     }
125788   }
125789 
125790   pIdxInfo->idxNum = 2;
125791   pIdxInfo->needToFreeIdxStr = 1;
125792   if( iIdx>0 && 0==(pIdxInfo->idxStr = sqlite3_mprintf("%s", zIdxStr)) ){
125793     return SQLITE_NOMEM;
125794   }
125795   assert( iIdx>=0 );
125796   pIdxInfo->estimatedCost = (2000000.0 / (double)(iIdx + 1));
125797   return rc;
125798 }
125799 
125800 /*
125801 ** Return the N-dimensional volumn of the cell stored in *p.
125802 */
125803 static float cellArea(Rtree *pRtree, RtreeCell *p){
125804   float area = 1.0;
125805   int ii;
125806   for(ii=0; ii<(pRtree->nDim*2); ii+=2){
125807     area = (float)(area * (DCOORD(p->aCoord[ii+1]) - DCOORD(p->aCoord[ii])));
125808   }
125809   return area;
125810 }
125811 
125812 /*
125813 ** Return the margin length of cell p. The margin length is the sum
125814 ** of the objects size in each dimension.
125815 */
125816 static float cellMargin(Rtree *pRtree, RtreeCell *p){
125817   float margin = 0.0;
125818   int ii;
125819   for(ii=0; ii<(pRtree->nDim*2); ii+=2){
125820     margin += (float)(DCOORD(p->aCoord[ii+1]) - DCOORD(p->aCoord[ii]));
125821   }
125822   return margin;
125823 }
125824 
125825 /*
125826 ** Store the union of cells p1 and p2 in p1.
125827 */
125828 static void cellUnion(Rtree *pRtree, RtreeCell *p1, RtreeCell *p2){
125829   int ii;
125830   if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
125831     for(ii=0; ii<(pRtree->nDim*2); ii+=2){
125832       p1->aCoord[ii].f = MIN(p1->aCoord[ii].f, p2->aCoord[ii].f);
125833       p1->aCoord[ii+1].f = MAX(p1->aCoord[ii+1].f, p2->aCoord[ii+1].f);
125834     }
125835   }else{
125836     for(ii=0; ii<(pRtree->nDim*2); ii+=2){
125837       p1->aCoord[ii].i = MIN(p1->aCoord[ii].i, p2->aCoord[ii].i);
125838       p1->aCoord[ii+1].i = MAX(p1->aCoord[ii+1].i, p2->aCoord[ii+1].i);
125839     }
125840   }
125841 }
125842 
125843 /*
125844 ** Return true if the area covered by p2 is a subset of the area covered
125845 ** by p1. False otherwise.
125846 */
125847 static int cellContains(Rtree *pRtree, RtreeCell *p1, RtreeCell *p2){
125848   int ii;
125849   int isInt = (pRtree->eCoordType==RTREE_COORD_INT32);
125850   for(ii=0; ii<(pRtree->nDim*2); ii+=2){
125851     RtreeCoord *a1 = &p1->aCoord[ii];
125852     RtreeCoord *a2 = &p2->aCoord[ii];
125853     if( (!isInt && (a2[0].f<a1[0].f || a2[1].f>a1[1].f))
125854      || ( isInt && (a2[0].i<a1[0].i || a2[1].i>a1[1].i))
125855     ){
125856       return 0;
125857     }
125858   }
125859   return 1;
125860 }
125861 
125862 /*
125863 ** Return the amount cell p would grow by if it were unioned with pCell.
125864 */
125865 static float cellGrowth(Rtree *pRtree, RtreeCell *p, RtreeCell *pCell){
125866   float area;
125867   RtreeCell cell;
125868   memcpy(&cell, p, sizeof(RtreeCell));
125869   area = cellArea(pRtree, &cell);
125870   cellUnion(pRtree, &cell, pCell);
125871   return (cellArea(pRtree, &cell)-area);
125872 }
125873 
125874 #if VARIANT_RSTARTREE_CHOOSESUBTREE || VARIANT_RSTARTREE_SPLIT
125875 static float cellOverlap(
125876   Rtree *pRtree,
125877   RtreeCell *p,
125878   RtreeCell *aCell,
125879   int nCell,
125880   int iExclude
125881 ){
125882   int ii;
125883   float overlap = 0.0;
125884   for(ii=0; ii<nCell; ii++){
125885 #if VARIANT_RSTARTREE_CHOOSESUBTREE
125886     if( ii!=iExclude )
125887 #else
125888     assert( iExclude==-1 );
125889     UNUSED_PARAMETER(iExclude);
125890 #endif
125891     {
125892       int jj;
125893       float o = 1.0;
125894       for(jj=0; jj<(pRtree->nDim*2); jj+=2){
125895         double x1;
125896         double x2;
125897 
125898         x1 = MAX(DCOORD(p->aCoord[jj]), DCOORD(aCell[ii].aCoord[jj]));
125899         x2 = MIN(DCOORD(p->aCoord[jj+1]), DCOORD(aCell[ii].aCoord[jj+1]));
125900 
125901         if( x2<x1 ){
125902           o = 0.0;
125903           break;
125904         }else{
125905           o = o * (float)(x2-x1);
125906         }
125907       }
125908       overlap += o;
125909     }
125910   }
125911   return overlap;
125912 }
125913 #endif
125914 
125915 #if VARIANT_RSTARTREE_CHOOSESUBTREE
125916 static float cellOverlapEnlargement(
125917   Rtree *pRtree,
125918   RtreeCell *p,
125919   RtreeCell *pInsert,
125920   RtreeCell *aCell,
125921   int nCell,
125922   int iExclude
125923 ){
125924   double before;
125925   double after;
125926   before = cellOverlap(pRtree, p, aCell, nCell, iExclude);
125927   cellUnion(pRtree, p, pInsert);
125928   after = cellOverlap(pRtree, p, aCell, nCell, iExclude);
125929   return (float)(after-before);
125930 }
125931 #endif
125932 
125933 
125934 /*
125935 ** This function implements the ChooseLeaf algorithm from Gutman[84].
125936 ** ChooseSubTree in r*tree terminology.
125937 */
125938 static int ChooseLeaf(
125939   Rtree *pRtree,               /* Rtree table */
125940   RtreeCell *pCell,            /* Cell to insert into rtree */
125941   int iHeight,                 /* Height of sub-tree rooted at pCell */
125942   RtreeNode **ppLeaf           /* OUT: Selected leaf page */
125943 ){
125944   int rc;
125945   int ii;
125946   RtreeNode *pNode;
125947   rc = nodeAcquire(pRtree, 1, 0, &pNode);
125948 
125949   for(ii=0; rc==SQLITE_OK && ii<(pRtree->iDepth-iHeight); ii++){
125950     int iCell;
125951     sqlite3_int64 iBest = 0;
125952 
125953     float fMinGrowth = 0.0;
125954     float fMinArea = 0.0;
125955     float fMinOverlap = 0.0;
125956 
125957     int nCell = NCELL(pNode);
125958     RtreeCell cell;
125959     RtreeNode *pChild;
125960 
125961     RtreeCell *aCell = 0;
125962 
125963 #if VARIANT_RSTARTREE_CHOOSESUBTREE
125964     if( ii==(pRtree->iDepth-1) ){
125965       int jj;
125966       aCell = sqlite3_malloc(sizeof(RtreeCell)*nCell);
125967       if( !aCell ){
125968         rc = SQLITE_NOMEM;
125969         nodeRelease(pRtree, pNode);
125970         pNode = 0;
125971         continue;
125972       }
125973       for(jj=0; jj<nCell; jj++){
125974         nodeGetCell(pRtree, pNode, jj, &aCell[jj]);
125975       }
125976     }
125977 #endif
125978 
125979     /* Select the child node which will be enlarged the least if pCell
125980     ** is inserted into it. Resolve ties by choosing the entry with
125981     ** the smallest area.
125982     */
125983     for(iCell=0; iCell<nCell; iCell++){
125984       int bBest = 0;
125985       float growth;
125986       float area;
125987       float overlap = 0.0;
125988       nodeGetCell(pRtree, pNode, iCell, &cell);
125989       growth = cellGrowth(pRtree, &cell, pCell);
125990       area = cellArea(pRtree, &cell);
125991 
125992 #if VARIANT_RSTARTREE_CHOOSESUBTREE
125993       if( ii==(pRtree->iDepth-1) ){
125994         overlap = cellOverlapEnlargement(pRtree,&cell,pCell,aCell,nCell,iCell);
125995       }
125996       if( (iCell==0)
125997        || (overlap<fMinOverlap)
125998        || (overlap==fMinOverlap && growth<fMinGrowth)
125999        || (overlap==fMinOverlap && growth==fMinGrowth && area<fMinArea)
126000       ){
126001         bBest = 1;
126002       }
126003 #else
126004       if( iCell==0||growth<fMinGrowth||(growth==fMinGrowth && area<fMinArea) ){
126005         bBest = 1;
126006       }
126007 #endif
126008       if( bBest ){
126009         fMinOverlap = overlap;
126010         fMinGrowth = growth;
126011         fMinArea = area;
126012         iBest = cell.iRowid;
126013       }
126014     }
126015 
126016     sqlite3_free(aCell);
126017     rc = nodeAcquire(pRtree, iBest, pNode, &pChild);
126018     nodeRelease(pRtree, pNode);
126019     pNode = pChild;
126020   }
126021 
126022   *ppLeaf = pNode;
126023   return rc;
126024 }
126025 
126026 /*
126027 ** A cell with the same content as pCell has just been inserted into
126028 ** the node pNode. This function updates the bounding box cells in
126029 ** all ancestor elements.
126030 */
126031 static int AdjustTree(
126032   Rtree *pRtree,                    /* Rtree table */
126033   RtreeNode *pNode,                 /* Adjust ancestry of this node. */
126034   RtreeCell *pCell                  /* This cell was just inserted */
126035 ){
126036   RtreeNode *p = pNode;
126037   while( p->pParent ){
126038     RtreeNode *pParent = p->pParent;
126039     RtreeCell cell;
126040     int iCell;
126041 
126042     if( nodeParentIndex(pRtree, p, &iCell) ){
126043       return SQLITE_CORRUPT_VTAB;
126044     }
126045 
126046     nodeGetCell(pRtree, pParent, iCell, &cell);
126047     if( !cellContains(pRtree, &cell, pCell) ){
126048       cellUnion(pRtree, &cell, pCell);
126049       nodeOverwriteCell(pRtree, pParent, &cell, iCell);
126050     }
126051 
126052     p = pParent;
126053   }
126054   return SQLITE_OK;
126055 }
126056 
126057 /*
126058 ** Write mapping (iRowid->iNode) to the <rtree>_rowid table.
126059 */
126060 static int rowidWrite(Rtree *pRtree, sqlite3_int64 iRowid, sqlite3_int64 iNode){
126061   sqlite3_bind_int64(pRtree->pWriteRowid, 1, iRowid);
126062   sqlite3_bind_int64(pRtree->pWriteRowid, 2, iNode);
126063   sqlite3_step(pRtree->pWriteRowid);
126064   return sqlite3_reset(pRtree->pWriteRowid);
126065 }
126066 
126067 /*
126068 ** Write mapping (iNode->iPar) to the <rtree>_parent table.
126069 */
126070 static int parentWrite(Rtree *pRtree, sqlite3_int64 iNode, sqlite3_int64 iPar){
126071   sqlite3_bind_int64(pRtree->pWriteParent, 1, iNode);
126072   sqlite3_bind_int64(pRtree->pWriteParent, 2, iPar);
126073   sqlite3_step(pRtree->pWriteParent);
126074   return sqlite3_reset(pRtree->pWriteParent);
126075 }
126076 
126077 static int rtreeInsertCell(Rtree *, RtreeNode *, RtreeCell *, int);
126078 
126079 #if VARIANT_GUTTMAN_LINEAR_SPLIT
126080 /*
126081 ** Implementation of the linear variant of the PickNext() function from
126082 ** Guttman[84].
126083 */
126084 static RtreeCell *LinearPickNext(
126085   Rtree *pRtree,
126086   RtreeCell *aCell,
126087   int nCell,
126088   RtreeCell *pLeftBox,
126089   RtreeCell *pRightBox,
126090   int *aiUsed
126091 ){
126092   int ii;
126093   for(ii=0; aiUsed[ii]; ii++);
126094   aiUsed[ii] = 1;
126095   return &aCell[ii];
126096 }
126097 
126098 /*
126099 ** Implementation of the linear variant of the PickSeeds() function from
126100 ** Guttman[84].
126101 */
126102 static void LinearPickSeeds(
126103   Rtree *pRtree,
126104   RtreeCell *aCell,
126105   int nCell,
126106   int *piLeftSeed,
126107   int *piRightSeed
126108 ){
126109   int i;
126110   int iLeftSeed = 0;
126111   int iRightSeed = 1;
126112   float maxNormalInnerWidth = 0.0;
126113 
126114   /* Pick two "seed" cells from the array of cells. The algorithm used
126115   ** here is the LinearPickSeeds algorithm from Gutman[1984]. The
126116   ** indices of the two seed cells in the array are stored in local
126117   ** variables iLeftSeek and iRightSeed.
126118   */
126119   for(i=0; i<pRtree->nDim; i++){
126120     float x1 = DCOORD(aCell[0].aCoord[i*2]);
126121     float x2 = DCOORD(aCell[0].aCoord[i*2+1]);
126122     float x3 = x1;
126123     float x4 = x2;
126124     int jj;
126125 
126126     int iCellLeft = 0;
126127     int iCellRight = 0;
126128 
126129     for(jj=1; jj<nCell; jj++){
126130       float left = DCOORD(aCell[jj].aCoord[i*2]);
126131       float right = DCOORD(aCell[jj].aCoord[i*2+1]);
126132 
126133       if( left<x1 ) x1 = left;
126134       if( right>x4 ) x4 = right;
126135       if( left>x3 ){
126136         x3 = left;
126137         iCellRight = jj;
126138       }
126139       if( right<x2 ){
126140         x2 = right;
126141         iCellLeft = jj;
126142       }
126143     }
126144 
126145     if( x4!=x1 ){
126146       float normalwidth = (x3 - x2) / (x4 - x1);
126147       if( normalwidth>maxNormalInnerWidth ){
126148         iLeftSeed = iCellLeft;
126149         iRightSeed = iCellRight;
126150       }
126151     }
126152   }
126153 
126154   *piLeftSeed = iLeftSeed;
126155   *piRightSeed = iRightSeed;
126156 }
126157 #endif /* VARIANT_GUTTMAN_LINEAR_SPLIT */
126158 
126159 #if VARIANT_GUTTMAN_QUADRATIC_SPLIT
126160 /*
126161 ** Implementation of the quadratic variant of the PickNext() function from
126162 ** Guttman[84].
126163 */
126164 static RtreeCell *QuadraticPickNext(
126165   Rtree *pRtree,
126166   RtreeCell *aCell,
126167   int nCell,
126168   RtreeCell *pLeftBox,
126169   RtreeCell *pRightBox,
126170   int *aiUsed
126171 ){
126172   #define FABS(a) ((a)<0.0?-1.0*(a):(a))
126173 
126174   int iSelect = -1;
126175   float fDiff;
126176   int ii;
126177   for(ii=0; ii<nCell; ii++){
126178     if( aiUsed[ii]==0 ){
126179       float left = cellGrowth(pRtree, pLeftBox, &aCell[ii]);
126180       float right = cellGrowth(pRtree, pLeftBox, &aCell[ii]);
126181       float diff = FABS(right-left);
126182       if( iSelect<0 || diff>fDiff ){
126183         fDiff = diff;
126184         iSelect = ii;
126185       }
126186     }
126187   }
126188   aiUsed[iSelect] = 1;
126189   return &aCell[iSelect];
126190 }
126191 
126192 /*
126193 ** Implementation of the quadratic variant of the PickSeeds() function from
126194 ** Guttman[84].
126195 */
126196 static void QuadraticPickSeeds(
126197   Rtree *pRtree,
126198   RtreeCell *aCell,
126199   int nCell,
126200   int *piLeftSeed,
126201   int *piRightSeed
126202 ){
126203   int ii;
126204   int jj;
126205 
126206   int iLeftSeed = 0;
126207   int iRightSeed = 1;
126208   float fWaste = 0.0;
126209 
126210   for(ii=0; ii<nCell; ii++){
126211     for(jj=ii+1; jj<nCell; jj++){
126212       float right = cellArea(pRtree, &aCell[jj]);
126213       float growth = cellGrowth(pRtree, &aCell[ii], &aCell[jj]);
126214       float waste = growth - right;
126215 
126216       if( waste>fWaste ){
126217         iLeftSeed = ii;
126218         iRightSeed = jj;
126219         fWaste = waste;
126220       }
126221     }
126222   }
126223 
126224   *piLeftSeed = iLeftSeed;
126225   *piRightSeed = iRightSeed;
126226 }
126227 #endif /* VARIANT_GUTTMAN_QUADRATIC_SPLIT */
126228 
126229 /*
126230 ** Arguments aIdx, aDistance and aSpare all point to arrays of size
126231 ** nIdx. The aIdx array contains the set of integers from 0 to
126232 ** (nIdx-1) in no particular order. This function sorts the values
126233 ** in aIdx according to the indexed values in aDistance. For
126234 ** example, assuming the inputs:
126235 **
126236 **   aIdx      = { 0,   1,   2,   3 }
126237 **   aDistance = { 5.0, 2.0, 7.0, 6.0 }
126238 **
126239 ** this function sets the aIdx array to contain:
126240 **
126241 **   aIdx      = { 0,   1,   2,   3 }
126242 **
126243 ** The aSpare array is used as temporary working space by the
126244 ** sorting algorithm.
126245 */
126246 static void SortByDistance(
126247   int *aIdx,
126248   int nIdx,
126249   float *aDistance,
126250   int *aSpare
126251 ){
126252   if( nIdx>1 ){
126253     int iLeft = 0;
126254     int iRight = 0;
126255 
126256     int nLeft = nIdx/2;
126257     int nRight = nIdx-nLeft;
126258     int *aLeft = aIdx;
126259     int *aRight = &aIdx[nLeft];
126260 
126261     SortByDistance(aLeft, nLeft, aDistance, aSpare);
126262     SortByDistance(aRight, nRight, aDistance, aSpare);
126263 
126264     memcpy(aSpare, aLeft, sizeof(int)*nLeft);
126265     aLeft = aSpare;
126266 
126267     while( iLeft<nLeft || iRight<nRight ){
126268       if( iLeft==nLeft ){
126269         aIdx[iLeft+iRight] = aRight[iRight];
126270         iRight++;
126271       }else if( iRight==nRight ){
126272         aIdx[iLeft+iRight] = aLeft[iLeft];
126273         iLeft++;
126274       }else{
126275         float fLeft = aDistance[aLeft[iLeft]];
126276         float fRight = aDistance[aRight[iRight]];
126277         if( fLeft<fRight ){
126278           aIdx[iLeft+iRight] = aLeft[iLeft];
126279           iLeft++;
126280         }else{
126281           aIdx[iLeft+iRight] = aRight[iRight];
126282           iRight++;
126283         }
126284       }
126285     }
126286 
126287 #if 0
126288     /* Check that the sort worked */
126289     {
126290       int jj;
126291       for(jj=1; jj<nIdx; jj++){
126292         float left = aDistance[aIdx[jj-1]];
126293         float right = aDistance[aIdx[jj]];
126294         assert( left<=right );
126295       }
126296     }
126297 #endif
126298   }
126299 }
126300 
126301 /*
126302 ** Arguments aIdx, aCell and aSpare all point to arrays of size
126303 ** nIdx. The aIdx array contains the set of integers from 0 to
126304 ** (nIdx-1) in no particular order. This function sorts the values
126305 ** in aIdx according to dimension iDim of the cells in aCell. The
126306 ** minimum value of dimension iDim is considered first, the
126307 ** maximum used to break ties.
126308 **
126309 ** The aSpare array is used as temporary working space by the
126310 ** sorting algorithm.
126311 */
126312 static void SortByDimension(
126313   Rtree *pRtree,
126314   int *aIdx,
126315   int nIdx,
126316   int iDim,
126317   RtreeCell *aCell,
126318   int *aSpare
126319 ){
126320   if( nIdx>1 ){
126321 
126322     int iLeft = 0;
126323     int iRight = 0;
126324 
126325     int nLeft = nIdx/2;
126326     int nRight = nIdx-nLeft;
126327     int *aLeft = aIdx;
126328     int *aRight = &aIdx[nLeft];
126329 
126330     SortByDimension(pRtree, aLeft, nLeft, iDim, aCell, aSpare);
126331     SortByDimension(pRtree, aRight, nRight, iDim, aCell, aSpare);
126332 
126333     memcpy(aSpare, aLeft, sizeof(int)*nLeft);
126334     aLeft = aSpare;
126335     while( iLeft<nLeft || iRight<nRight ){
126336       double xleft1 = DCOORD(aCell[aLeft[iLeft]].aCoord[iDim*2]);
126337       double xleft2 = DCOORD(aCell[aLeft[iLeft]].aCoord[iDim*2+1]);
126338       double xright1 = DCOORD(aCell[aRight[iRight]].aCoord[iDim*2]);
126339       double xright2 = DCOORD(aCell[aRight[iRight]].aCoord[iDim*2+1]);
126340       if( (iLeft!=nLeft) && ((iRight==nRight)
126341        || (xleft1<xright1)
126342        || (xleft1==xright1 && xleft2<xright2)
126343       )){
126344         aIdx[iLeft+iRight] = aLeft[iLeft];
126345         iLeft++;
126346       }else{
126347         aIdx[iLeft+iRight] = aRight[iRight];
126348         iRight++;
126349       }
126350     }
126351 
126352 #if 0
126353     /* Check that the sort worked */
126354     {
126355       int jj;
126356       for(jj=1; jj<nIdx; jj++){
126357         float xleft1 = aCell[aIdx[jj-1]].aCoord[iDim*2];
126358         float xleft2 = aCell[aIdx[jj-1]].aCoord[iDim*2+1];
126359         float xright1 = aCell[aIdx[jj]].aCoord[iDim*2];
126360         float xright2 = aCell[aIdx[jj]].aCoord[iDim*2+1];
126361         assert( xleft1<=xright1 && (xleft1<xright1 || xleft2<=xright2) );
126362       }
126363     }
126364 #endif
126365   }
126366 }
126367 
126368 #if VARIANT_RSTARTREE_SPLIT
126369 /*
126370 ** Implementation of the R*-tree variant of SplitNode from Beckman[1990].
126371 */
126372 static int splitNodeStartree(
126373   Rtree *pRtree,
126374   RtreeCell *aCell,
126375   int nCell,
126376   RtreeNode *pLeft,
126377   RtreeNode *pRight,
126378   RtreeCell *pBboxLeft,
126379   RtreeCell *pBboxRight
126380 ){
126381   int **aaSorted;
126382   int *aSpare;
126383   int ii;
126384 
126385   int iBestDim = 0;
126386   int iBestSplit = 0;
126387   float fBestMargin = 0.0;
126388 
126389   int nByte = (pRtree->nDim+1)*(sizeof(int*)+nCell*sizeof(int));
126390 
126391   aaSorted = (int **)sqlite3_malloc(nByte);
126392   if( !aaSorted ){
126393     return SQLITE_NOMEM;
126394   }
126395 
126396   aSpare = &((int *)&aaSorted[pRtree->nDim])[pRtree->nDim*nCell];
126397   memset(aaSorted, 0, nByte);
126398   for(ii=0; ii<pRtree->nDim; ii++){
126399     int jj;
126400     aaSorted[ii] = &((int *)&aaSorted[pRtree->nDim])[ii*nCell];
126401     for(jj=0; jj<nCell; jj++){
126402       aaSorted[ii][jj] = jj;
126403     }
126404     SortByDimension(pRtree, aaSorted[ii], nCell, ii, aCell, aSpare);
126405   }
126406 
126407   for(ii=0; ii<pRtree->nDim; ii++){
126408     float margin = 0.0;
126409     float fBestOverlap = 0.0;
126410     float fBestArea = 0.0;
126411     int iBestLeft = 0;
126412     int nLeft;
126413 
126414     for(
126415       nLeft=RTREE_MINCELLS(pRtree);
126416       nLeft<=(nCell-RTREE_MINCELLS(pRtree));
126417       nLeft++
126418     ){
126419       RtreeCell left;
126420       RtreeCell right;
126421       int kk;
126422       float overlap;
126423       float area;
126424 
126425       memcpy(&left, &aCell[aaSorted[ii][0]], sizeof(RtreeCell));
126426       memcpy(&right, &aCell[aaSorted[ii][nCell-1]], sizeof(RtreeCell));
126427       for(kk=1; kk<(nCell-1); kk++){
126428         if( kk<nLeft ){
126429           cellUnion(pRtree, &left, &aCell[aaSorted[ii][kk]]);
126430         }else{
126431           cellUnion(pRtree, &right, &aCell[aaSorted[ii][kk]]);
126432         }
126433       }
126434       margin += cellMargin(pRtree, &left);
126435       margin += cellMargin(pRtree, &right);
126436       overlap = cellOverlap(pRtree, &left, &right, 1, -1);
126437       area = cellArea(pRtree, &left) + cellArea(pRtree, &right);
126438       if( (nLeft==RTREE_MINCELLS(pRtree))
126439        || (overlap<fBestOverlap)
126440        || (overlap==fBestOverlap && area<fBestArea)
126441       ){
126442         iBestLeft = nLeft;
126443         fBestOverlap = overlap;
126444         fBestArea = area;
126445       }
126446     }
126447 
126448     if( ii==0 || margin<fBestMargin ){
126449       iBestDim = ii;
126450       fBestMargin = margin;
126451       iBestSplit = iBestLeft;
126452     }
126453   }
126454 
126455   memcpy(pBboxLeft, &aCell[aaSorted[iBestDim][0]], sizeof(RtreeCell));
126456   memcpy(pBboxRight, &aCell[aaSorted[iBestDim][iBestSplit]], sizeof(RtreeCell));
126457   for(ii=0; ii<nCell; ii++){
126458     RtreeNode *pTarget = (ii<iBestSplit)?pLeft:pRight;
126459     RtreeCell *pBbox = (ii<iBestSplit)?pBboxLeft:pBboxRight;
126460     RtreeCell *pCell = &aCell[aaSorted[iBestDim][ii]];
126461     nodeInsertCell(pRtree, pTarget, pCell);
126462     cellUnion(pRtree, pBbox, pCell);
126463   }
126464 
126465   sqlite3_free(aaSorted);
126466   return SQLITE_OK;
126467 }
126468 #endif
126469 
126470 #if VARIANT_GUTTMAN_SPLIT
126471 /*
126472 ** Implementation of the regular R-tree SplitNode from Guttman[1984].
126473 */
126474 static int splitNodeGuttman(
126475   Rtree *pRtree,
126476   RtreeCell *aCell,
126477   int nCell,
126478   RtreeNode *pLeft,
126479   RtreeNode *pRight,
126480   RtreeCell *pBboxLeft,
126481   RtreeCell *pBboxRight
126482 ){
126483   int iLeftSeed = 0;
126484   int iRightSeed = 1;
126485   int *aiUsed;
126486   int i;
126487 
126488   aiUsed = sqlite3_malloc(sizeof(int)*nCell);
126489   if( !aiUsed ){
126490     return SQLITE_NOMEM;
126491   }
126492   memset(aiUsed, 0, sizeof(int)*nCell);
126493 
126494   PickSeeds(pRtree, aCell, nCell, &iLeftSeed, &iRightSeed);
126495 
126496   memcpy(pBboxLeft, &aCell[iLeftSeed], sizeof(RtreeCell));
126497   memcpy(pBboxRight, &aCell[iRightSeed], sizeof(RtreeCell));
126498   nodeInsertCell(pRtree, pLeft, &aCell[iLeftSeed]);
126499   nodeInsertCell(pRtree, pRight, &aCell[iRightSeed]);
126500   aiUsed[iLeftSeed] = 1;
126501   aiUsed[iRightSeed] = 1;
126502 
126503   for(i=nCell-2; i>0; i--){
126504     RtreeCell *pNext;
126505     pNext = PickNext(pRtree, aCell, nCell, pBboxLeft, pBboxRight, aiUsed);
126506     float diff =
126507       cellGrowth(pRtree, pBboxLeft, pNext) -
126508       cellGrowth(pRtree, pBboxRight, pNext)
126509     ;
126510     if( (RTREE_MINCELLS(pRtree)-NCELL(pRight)==i)
126511      || (diff>0.0 && (RTREE_MINCELLS(pRtree)-NCELL(pLeft)!=i))
126512     ){
126513       nodeInsertCell(pRtree, pRight, pNext);
126514       cellUnion(pRtree, pBboxRight, pNext);
126515     }else{
126516       nodeInsertCell(pRtree, pLeft, pNext);
126517       cellUnion(pRtree, pBboxLeft, pNext);
126518     }
126519   }
126520 
126521   sqlite3_free(aiUsed);
126522   return SQLITE_OK;
126523 }
126524 #endif
126525 
126526 static int updateMapping(
126527   Rtree *pRtree,
126528   i64 iRowid,
126529   RtreeNode *pNode,
126530   int iHeight
126531 ){
126532   int (*xSetMapping)(Rtree *, sqlite3_int64, sqlite3_int64);
126533   xSetMapping = ((iHeight==0)?rowidWrite:parentWrite);
126534   if( iHeight>0 ){
126535     RtreeNode *pChild = nodeHashLookup(pRtree, iRowid);
126536     if( pChild ){
126537       nodeRelease(pRtree, pChild->pParent);
126538       nodeReference(pNode);
126539       pChild->pParent = pNode;
126540     }
126541   }
126542   return xSetMapping(pRtree, iRowid, pNode->iNode);
126543 }
126544 
126545 static int SplitNode(
126546   Rtree *pRtree,
126547   RtreeNode *pNode,
126548   RtreeCell *pCell,
126549   int iHeight
126550 ){
126551   int i;
126552   int newCellIsRight = 0;
126553 
126554   int rc = SQLITE_OK;
126555   int nCell = NCELL(pNode);
126556   RtreeCell *aCell;
126557   int *aiUsed;
126558 
126559   RtreeNode *pLeft = 0;
126560   RtreeNode *pRight = 0;
126561 
126562   RtreeCell leftbbox;
126563   RtreeCell rightbbox;
126564 
126565   /* Allocate an array and populate it with a copy of pCell and
126566   ** all cells from node pLeft. Then zero the original node.
126567   */
126568   aCell = sqlite3_malloc((sizeof(RtreeCell)+sizeof(int))*(nCell+1));
126569   if( !aCell ){
126570     rc = SQLITE_NOMEM;
126571     goto splitnode_out;
126572   }
126573   aiUsed = (int *)&aCell[nCell+1];
126574   memset(aiUsed, 0, sizeof(int)*(nCell+1));
126575   for(i=0; i<nCell; i++){
126576     nodeGetCell(pRtree, pNode, i, &aCell[i]);
126577   }
126578   nodeZero(pRtree, pNode);
126579   memcpy(&aCell[nCell], pCell, sizeof(RtreeCell));
126580   nCell++;
126581 
126582   if( pNode->iNode==1 ){
126583     pRight = nodeNew(pRtree, pNode);
126584     pLeft = nodeNew(pRtree, pNode);
126585     pRtree->iDepth++;
126586     pNode->isDirty = 1;
126587     writeInt16(pNode->zData, pRtree->iDepth);
126588   }else{
126589     pLeft = pNode;
126590     pRight = nodeNew(pRtree, pLeft->pParent);
126591     nodeReference(pLeft);
126592   }
126593 
126594   if( !pLeft || !pRight ){
126595     rc = SQLITE_NOMEM;
126596     goto splitnode_out;
126597   }
126598 
126599   memset(pLeft->zData, 0, pRtree->iNodeSize);
126600   memset(pRight->zData, 0, pRtree->iNodeSize);
126601 
126602   rc = AssignCells(pRtree, aCell, nCell, pLeft, pRight, &leftbbox, &rightbbox);
126603   if( rc!=SQLITE_OK ){
126604     goto splitnode_out;
126605   }
126606 
126607   /* Ensure both child nodes have node numbers assigned to them by calling
126608   ** nodeWrite(). Node pRight always needs a node number, as it was created
126609   ** by nodeNew() above. But node pLeft sometimes already has a node number.
126610   ** In this case avoid the all to nodeWrite().
126611   */
126612   if( SQLITE_OK!=(rc = nodeWrite(pRtree, pRight))
126613    || (0==pLeft->iNode && SQLITE_OK!=(rc = nodeWrite(pRtree, pLeft)))
126614   ){
126615     goto splitnode_out;
126616   }
126617 
126618   rightbbox.iRowid = pRight->iNode;
126619   leftbbox.iRowid = pLeft->iNode;
126620 
126621   if( pNode->iNode==1 ){
126622     rc = rtreeInsertCell(pRtree, pLeft->pParent, &leftbbox, iHeight+1);
126623     if( rc!=SQLITE_OK ){
126624       goto splitnode_out;
126625     }
126626   }else{
126627     RtreeNode *pParent = pLeft->pParent;
126628     int iCell;
126629     rc = nodeParentIndex(pRtree, pLeft, &iCell);
126630     if( rc==SQLITE_OK ){
126631       nodeOverwriteCell(pRtree, pParent, &leftbbox, iCell);
126632       rc = AdjustTree(pRtree, pParent, &leftbbox);
126633     }
126634     if( rc!=SQLITE_OK ){
126635       goto splitnode_out;
126636     }
126637   }
126638   if( (rc = rtreeInsertCell(pRtree, pRight->pParent, &rightbbox, iHeight+1)) ){
126639     goto splitnode_out;
126640   }
126641 
126642   for(i=0; i<NCELL(pRight); i++){
126643     i64 iRowid = nodeGetRowid(pRtree, pRight, i);
126644     rc = updateMapping(pRtree, iRowid, pRight, iHeight);
126645     if( iRowid==pCell->iRowid ){
126646       newCellIsRight = 1;
126647     }
126648     if( rc!=SQLITE_OK ){
126649       goto splitnode_out;
126650     }
126651   }
126652   if( pNode->iNode==1 ){
126653     for(i=0; i<NCELL(pLeft); i++){
126654       i64 iRowid = nodeGetRowid(pRtree, pLeft, i);
126655       rc = updateMapping(pRtree, iRowid, pLeft, iHeight);
126656       if( rc!=SQLITE_OK ){
126657         goto splitnode_out;
126658       }
126659     }
126660   }else if( newCellIsRight==0 ){
126661     rc = updateMapping(pRtree, pCell->iRowid, pLeft, iHeight);
126662   }
126663 
126664   if( rc==SQLITE_OK ){
126665     rc = nodeRelease(pRtree, pRight);
126666     pRight = 0;
126667   }
126668   if( rc==SQLITE_OK ){
126669     rc = nodeRelease(pRtree, pLeft);
126670     pLeft = 0;
126671   }
126672 
126673 splitnode_out:
126674   nodeRelease(pRtree, pRight);
126675   nodeRelease(pRtree, pLeft);
126676   sqlite3_free(aCell);
126677   return rc;
126678 }
126679 
126680 /*
126681 ** If node pLeaf is not the root of the r-tree and its pParent pointer is
126682 ** still NULL, load all ancestor nodes of pLeaf into memory and populate
126683 ** the pLeaf->pParent chain all the way up to the root node.
126684 **
126685 ** This operation is required when a row is deleted (or updated - an update
126686 ** is implemented as a delete followed by an insert). SQLite provides the
126687 ** rowid of the row to delete, which can be used to find the leaf on which
126688 ** the entry resides (argument pLeaf). Once the leaf is located, this
126689 ** function is called to determine its ancestry.
126690 */
126691 static int fixLeafParent(Rtree *pRtree, RtreeNode *pLeaf){
126692   int rc = SQLITE_OK;
126693   RtreeNode *pChild = pLeaf;
126694   while( rc==SQLITE_OK && pChild->iNode!=1 && pChild->pParent==0 ){
126695     int rc2 = SQLITE_OK;          /* sqlite3_reset() return code */
126696     sqlite3_bind_int64(pRtree->pReadParent, 1, pChild->iNode);
126697     rc = sqlite3_step(pRtree->pReadParent);
126698     if( rc==SQLITE_ROW ){
126699       RtreeNode *pTest;           /* Used to test for reference loops */
126700       i64 iNode;                  /* Node number of parent node */
126701 
126702       /* Before setting pChild->pParent, test that we are not creating a
126703       ** loop of references (as we would if, say, pChild==pParent). We don't
126704       ** want to do this as it leads to a memory leak when trying to delete
126705       ** the referenced counted node structures.
126706       */
126707       iNode = sqlite3_column_int64(pRtree->pReadParent, 0);
126708       for(pTest=pLeaf; pTest && pTest->iNode!=iNode; pTest=pTest->pParent);
126709       if( !pTest ){
126710         rc2 = nodeAcquire(pRtree, iNode, 0, &pChild->pParent);
126711       }
126712     }
126713     rc = sqlite3_reset(pRtree->pReadParent);
126714     if( rc==SQLITE_OK ) rc = rc2;
126715     if( rc==SQLITE_OK && !pChild->pParent ) rc = SQLITE_CORRUPT_VTAB;
126716     pChild = pChild->pParent;
126717   }
126718   return rc;
126719 }
126720 
126721 static int deleteCell(Rtree *, RtreeNode *, int, int);
126722 
126723 static int removeNode(Rtree *pRtree, RtreeNode *pNode, int iHeight){
126724   int rc;
126725   int rc2;
126726   RtreeNode *pParent = 0;
126727   int iCell;
126728 
126729   assert( pNode->nRef==1 );
126730 
126731   /* Remove the entry in the parent cell. */
126732   rc = nodeParentIndex(pRtree, pNode, &iCell);
126733   if( rc==SQLITE_OK ){
126734     pParent = pNode->pParent;
126735     pNode->pParent = 0;
126736     rc = deleteCell(pRtree, pParent, iCell, iHeight+1);
126737   }
126738   rc2 = nodeRelease(pRtree, pParent);
126739   if( rc==SQLITE_OK ){
126740     rc = rc2;
126741   }
126742   if( rc!=SQLITE_OK ){
126743     return rc;
126744   }
126745 
126746   /* Remove the xxx_node entry. */
126747   sqlite3_bind_int64(pRtree->pDeleteNode, 1, pNode->iNode);
126748   sqlite3_step(pRtree->pDeleteNode);
126749   if( SQLITE_OK!=(rc = sqlite3_reset(pRtree->pDeleteNode)) ){
126750     return rc;
126751   }
126752 
126753   /* Remove the xxx_parent entry. */
126754   sqlite3_bind_int64(pRtree->pDeleteParent, 1, pNode->iNode);
126755   sqlite3_step(pRtree->pDeleteParent);
126756   if( SQLITE_OK!=(rc = sqlite3_reset(pRtree->pDeleteParent)) ){
126757     return rc;
126758   }
126759 
126760   /* Remove the node from the in-memory hash table and link it into
126761   ** the Rtree.pDeleted list. Its contents will be re-inserted later on.
126762   */
126763   nodeHashDelete(pRtree, pNode);
126764   pNode->iNode = iHeight;
126765   pNode->pNext = pRtree->pDeleted;
126766   pNode->nRef++;
126767   pRtree->pDeleted = pNode;
126768 
126769   return SQLITE_OK;
126770 }
126771 
126772 static int fixBoundingBox(Rtree *pRtree, RtreeNode *pNode){
126773   RtreeNode *pParent = pNode->pParent;
126774   int rc = SQLITE_OK;
126775   if( pParent ){
126776     int ii;
126777     int nCell = NCELL(pNode);
126778     RtreeCell box;                            /* Bounding box for pNode */
126779     nodeGetCell(pRtree, pNode, 0, &box);
126780     for(ii=1; ii<nCell; ii++){
126781       RtreeCell cell;
126782       nodeGetCell(pRtree, pNode, ii, &cell);
126783       cellUnion(pRtree, &box, &cell);
126784     }
126785     box.iRowid = pNode->iNode;
126786     rc = nodeParentIndex(pRtree, pNode, &ii);
126787     if( rc==SQLITE_OK ){
126788       nodeOverwriteCell(pRtree, pParent, &box, ii);
126789       rc = fixBoundingBox(pRtree, pParent);
126790     }
126791   }
126792   return rc;
126793 }
126794 
126795 /*
126796 ** Delete the cell at index iCell of node pNode. After removing the
126797 ** cell, adjust the r-tree data structure if required.
126798 */
126799 static int deleteCell(Rtree *pRtree, RtreeNode *pNode, int iCell, int iHeight){
126800   RtreeNode *pParent;
126801   int rc;
126802 
126803   if( SQLITE_OK!=(rc = fixLeafParent(pRtree, pNode)) ){
126804     return rc;
126805   }
126806 
126807   /* Remove the cell from the node. This call just moves bytes around
126808   ** the in-memory node image, so it cannot fail.
126809   */
126810   nodeDeleteCell(pRtree, pNode, iCell);
126811 
126812   /* If the node is not the tree root and now has less than the minimum
126813   ** number of cells, remove it from the tree. Otherwise, update the
126814   ** cell in the parent node so that it tightly contains the updated
126815   ** node.
126816   */
126817   pParent = pNode->pParent;
126818   assert( pParent || pNode->iNode==1 );
126819   if( pParent ){
126820     if( NCELL(pNode)<RTREE_MINCELLS(pRtree) ){
126821       rc = removeNode(pRtree, pNode, iHeight);
126822     }else{
126823       rc = fixBoundingBox(pRtree, pNode);
126824     }
126825   }
126826 
126827   return rc;
126828 }
126829 
126830 static int Reinsert(
126831   Rtree *pRtree,
126832   RtreeNode *pNode,
126833   RtreeCell *pCell,
126834   int iHeight
126835 ){
126836   int *aOrder;
126837   int *aSpare;
126838   RtreeCell *aCell;
126839   float *aDistance;
126840   int nCell;
126841   float aCenterCoord[RTREE_MAX_DIMENSIONS];
126842   int iDim;
126843   int ii;
126844   int rc = SQLITE_OK;
126845 
126846   memset(aCenterCoord, 0, sizeof(float)*RTREE_MAX_DIMENSIONS);
126847 
126848   nCell = NCELL(pNode)+1;
126849 
126850   /* Allocate the buffers used by this operation. The allocation is
126851   ** relinquished before this function returns.
126852   */
126853   aCell = (RtreeCell *)sqlite3_malloc(nCell * (
126854     sizeof(RtreeCell) +         /* aCell array */
126855     sizeof(int)       +         /* aOrder array */
126856     sizeof(int)       +         /* aSpare array */
126857     sizeof(float)               /* aDistance array */
126858   ));
126859   if( !aCell ){
126860     return SQLITE_NOMEM;
126861   }
126862   aOrder    = (int *)&aCell[nCell];
126863   aSpare    = (int *)&aOrder[nCell];
126864   aDistance = (float *)&aSpare[nCell];
126865 
126866   for(ii=0; ii<nCell; ii++){
126867     if( ii==(nCell-1) ){
126868       memcpy(&aCell[ii], pCell, sizeof(RtreeCell));
126869     }else{
126870       nodeGetCell(pRtree, pNode, ii, &aCell[ii]);
126871     }
126872     aOrder[ii] = ii;
126873     for(iDim=0; iDim<pRtree->nDim; iDim++){
126874       aCenterCoord[iDim] += (float)DCOORD(aCell[ii].aCoord[iDim*2]);
126875       aCenterCoord[iDim] += (float)DCOORD(aCell[ii].aCoord[iDim*2+1]);
126876     }
126877   }
126878   for(iDim=0; iDim<pRtree->nDim; iDim++){
126879     aCenterCoord[iDim] = (float)(aCenterCoord[iDim]/((float)nCell*2.0));
126880   }
126881 
126882   for(ii=0; ii<nCell; ii++){
126883     aDistance[ii] = 0.0;
126884     for(iDim=0; iDim<pRtree->nDim; iDim++){
126885       float coord = (float)(DCOORD(aCell[ii].aCoord[iDim*2+1]) -
126886           DCOORD(aCell[ii].aCoord[iDim*2]));
126887       aDistance[ii] += (coord-aCenterCoord[iDim])*(coord-aCenterCoord[iDim]);
126888     }
126889   }
126890 
126891   SortByDistance(aOrder, nCell, aDistance, aSpare);
126892   nodeZero(pRtree, pNode);
126893 
126894   for(ii=0; rc==SQLITE_OK && ii<(nCell-(RTREE_MINCELLS(pRtree)+1)); ii++){
126895     RtreeCell *p = &aCell[aOrder[ii]];
126896     nodeInsertCell(pRtree, pNode, p);
126897     if( p->iRowid==pCell->iRowid ){
126898       if( iHeight==0 ){
126899         rc = rowidWrite(pRtree, p->iRowid, pNode->iNode);
126900       }else{
126901         rc = parentWrite(pRtree, p->iRowid, pNode->iNode);
126902       }
126903     }
126904   }
126905   if( rc==SQLITE_OK ){
126906     rc = fixBoundingBox(pRtree, pNode);
126907   }
126908   for(; rc==SQLITE_OK && ii<nCell; ii++){
126909     /* Find a node to store this cell in. pNode->iNode currently contains
126910     ** the height of the sub-tree headed by the cell.
126911     */
126912     RtreeNode *pInsert;
126913     RtreeCell *p = &aCell[aOrder[ii]];
126914     rc = ChooseLeaf(pRtree, p, iHeight, &pInsert);
126915     if( rc==SQLITE_OK ){
126916       int rc2;
126917       rc = rtreeInsertCell(pRtree, pInsert, p, iHeight);
126918       rc2 = nodeRelease(pRtree, pInsert);
126919       if( rc==SQLITE_OK ){
126920         rc = rc2;
126921       }
126922     }
126923   }
126924 
126925   sqlite3_free(aCell);
126926   return rc;
126927 }
126928 
126929 /*
126930 ** Insert cell pCell into node pNode. Node pNode is the head of a
126931 ** subtree iHeight high (leaf nodes have iHeight==0).
126932 */
126933 static int rtreeInsertCell(
126934   Rtree *pRtree,
126935   RtreeNode *pNode,
126936   RtreeCell *pCell,
126937   int iHeight
126938 ){
126939   int rc = SQLITE_OK;
126940   if( iHeight>0 ){
126941     RtreeNode *pChild = nodeHashLookup(pRtree, pCell->iRowid);
126942     if( pChild ){
126943       nodeRelease(pRtree, pChild->pParent);
126944       nodeReference(pNode);
126945       pChild->pParent = pNode;
126946     }
126947   }
126948   if( nodeInsertCell(pRtree, pNode, pCell) ){
126949 #if VARIANT_RSTARTREE_REINSERT
126950     if( iHeight<=pRtree->iReinsertHeight || pNode->iNode==1){
126951       rc = SplitNode(pRtree, pNode, pCell, iHeight);
126952     }else{
126953       pRtree->iReinsertHeight = iHeight;
126954       rc = Reinsert(pRtree, pNode, pCell, iHeight);
126955     }
126956 #else
126957     rc = SplitNode(pRtree, pNode, pCell, iHeight);
126958 #endif
126959   }else{
126960     rc = AdjustTree(pRtree, pNode, pCell);
126961     if( rc==SQLITE_OK ){
126962       if( iHeight==0 ){
126963         rc = rowidWrite(pRtree, pCell->iRowid, pNode->iNode);
126964       }else{
126965         rc = parentWrite(pRtree, pCell->iRowid, pNode->iNode);
126966       }
126967     }
126968   }
126969   return rc;
126970 }
126971 
126972 static int reinsertNodeContent(Rtree *pRtree, RtreeNode *pNode){
126973   int ii;
126974   int rc = SQLITE_OK;
126975   int nCell = NCELL(pNode);
126976 
126977   for(ii=0; rc==SQLITE_OK && ii<nCell; ii++){
126978     RtreeNode *pInsert;
126979     RtreeCell cell;
126980     nodeGetCell(pRtree, pNode, ii, &cell);
126981 
126982     /* Find a node to store this cell in. pNode->iNode currently contains
126983     ** the height of the sub-tree headed by the cell.
126984     */
126985     rc = ChooseLeaf(pRtree, &cell, (int)pNode->iNode, &pInsert);
126986     if( rc==SQLITE_OK ){
126987       int rc2;
126988       rc = rtreeInsertCell(pRtree, pInsert, &cell, (int)pNode->iNode);
126989       rc2 = nodeRelease(pRtree, pInsert);
126990       if( rc==SQLITE_OK ){
126991         rc = rc2;
126992       }
126993     }
126994   }
126995   return rc;
126996 }
126997 
126998 /*
126999 ** Select a currently unused rowid for a new r-tree record.
127000 */
127001 static int newRowid(Rtree *pRtree, i64 *piRowid){
127002   int rc;
127003   sqlite3_bind_null(pRtree->pWriteRowid, 1);
127004   sqlite3_bind_null(pRtree->pWriteRowid, 2);
127005   sqlite3_step(pRtree->pWriteRowid);
127006   rc = sqlite3_reset(pRtree->pWriteRowid);
127007   *piRowid = sqlite3_last_insert_rowid(pRtree->db);
127008   return rc;
127009 }
127010 
127011 /*
127012 ** Remove the entry with rowid=iDelete from the r-tree structure.
127013 */
127014 static int rtreeDeleteRowid(Rtree *pRtree, sqlite3_int64 iDelete){
127015   int rc;                         /* Return code */
127016   RtreeNode *pLeaf;               /* Leaf node containing record iDelete */
127017   int iCell;                      /* Index of iDelete cell in pLeaf */
127018   RtreeNode *pRoot;               /* Root node of rtree structure */
127019 
127020 
127021   /* Obtain a reference to the root node to initialise Rtree.iDepth */
127022   rc = nodeAcquire(pRtree, 1, 0, &pRoot);
127023 
127024   /* Obtain a reference to the leaf node that contains the entry
127025   ** about to be deleted.
127026   */
127027   if( rc==SQLITE_OK ){
127028     rc = findLeafNode(pRtree, iDelete, &pLeaf);
127029   }
127030 
127031   /* Delete the cell in question from the leaf node. */
127032   if( rc==SQLITE_OK ){
127033     int rc2;
127034     rc = nodeRowidIndex(pRtree, pLeaf, iDelete, &iCell);
127035     if( rc==SQLITE_OK ){
127036       rc = deleteCell(pRtree, pLeaf, iCell, 0);
127037     }
127038     rc2 = nodeRelease(pRtree, pLeaf);
127039     if( rc==SQLITE_OK ){
127040       rc = rc2;
127041     }
127042   }
127043 
127044   /* Delete the corresponding entry in the <rtree>_rowid table. */
127045   if( rc==SQLITE_OK ){
127046     sqlite3_bind_int64(pRtree->pDeleteRowid, 1, iDelete);
127047     sqlite3_step(pRtree->pDeleteRowid);
127048     rc = sqlite3_reset(pRtree->pDeleteRowid);
127049   }
127050 
127051   /* Check if the root node now has exactly one child. If so, remove
127052   ** it, schedule the contents of the child for reinsertion and
127053   ** reduce the tree height by one.
127054   **
127055   ** This is equivalent to copying the contents of the child into
127056   ** the root node (the operation that Gutman's paper says to perform
127057   ** in this scenario).
127058   */
127059   if( rc==SQLITE_OK && pRtree->iDepth>0 && NCELL(pRoot)==1 ){
127060     int rc2;
127061     RtreeNode *pChild;
127062     i64 iChild = nodeGetRowid(pRtree, pRoot, 0);
127063     rc = nodeAcquire(pRtree, iChild, pRoot, &pChild);
127064     if( rc==SQLITE_OK ){
127065       rc = removeNode(pRtree, pChild, pRtree->iDepth-1);
127066     }
127067     rc2 = nodeRelease(pRtree, pChild);
127068     if( rc==SQLITE_OK ) rc = rc2;
127069     if( rc==SQLITE_OK ){
127070       pRtree->iDepth--;
127071       writeInt16(pRoot->zData, pRtree->iDepth);
127072       pRoot->isDirty = 1;
127073     }
127074   }
127075 
127076   /* Re-insert the contents of any underfull nodes removed from the tree. */
127077   for(pLeaf=pRtree->pDeleted; pLeaf; pLeaf=pRtree->pDeleted){
127078     if( rc==SQLITE_OK ){
127079       rc = reinsertNodeContent(pRtree, pLeaf);
127080     }
127081     pRtree->pDeleted = pLeaf->pNext;
127082     sqlite3_free(pLeaf);
127083   }
127084 
127085   /* Release the reference to the root node. */
127086   if( rc==SQLITE_OK ){
127087     rc = nodeRelease(pRtree, pRoot);
127088   }else{
127089     nodeRelease(pRtree, pRoot);
127090   }
127091 
127092   return rc;
127093 }
127094 
127095 /*
127096 ** The xUpdate method for rtree module virtual tables.
127097 */
127098 static int rtreeUpdate(
127099   sqlite3_vtab *pVtab,
127100   int nData,
127101   sqlite3_value **azData,
127102   sqlite_int64 *pRowid
127103 ){
127104   Rtree *pRtree = (Rtree *)pVtab;
127105   int rc = SQLITE_OK;
127106   RtreeCell cell;                 /* New cell to insert if nData>1 */
127107   int bHaveRowid = 0;             /* Set to 1 after new rowid is determined */
127108 
127109   rtreeReference(pRtree);
127110   assert(nData>=1);
127111 
127112   /* Constraint handling. A write operation on an r-tree table may return
127113   ** SQLITE_CONSTRAINT for two reasons:
127114   **
127115   **   1. A duplicate rowid value, or
127116   **   2. The supplied data violates the "x2>=x1" constraint.
127117   **
127118   ** In the first case, if the conflict-handling mode is REPLACE, then
127119   ** the conflicting row can be removed before proceeding. In the second
127120   ** case, SQLITE_CONSTRAINT must be returned regardless of the
127121   ** conflict-handling mode specified by the user.
127122   */
127123   if( nData>1 ){
127124     int ii;
127125 
127126     /* Populate the cell.aCoord[] array. The first coordinate is azData[3]. */
127127     assert( nData==(pRtree->nDim*2 + 3) );
127128     if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
127129       for(ii=0; ii<(pRtree->nDim*2); ii+=2){
127130         cell.aCoord[ii].f = (float)sqlite3_value_double(azData[ii+3]);
127131         cell.aCoord[ii+1].f = (float)sqlite3_value_double(azData[ii+4]);
127132         if( cell.aCoord[ii].f>cell.aCoord[ii+1].f ){
127133           rc = SQLITE_CONSTRAINT;
127134           goto constraint;
127135         }
127136       }
127137     }else{
127138       for(ii=0; ii<(pRtree->nDim*2); ii+=2){
127139         cell.aCoord[ii].i = sqlite3_value_int(azData[ii+3]);
127140         cell.aCoord[ii+1].i = sqlite3_value_int(azData[ii+4]);
127141         if( cell.aCoord[ii].i>cell.aCoord[ii+1].i ){
127142           rc = SQLITE_CONSTRAINT;
127143           goto constraint;
127144         }
127145       }
127146     }
127147 
127148     /* If a rowid value was supplied, check if it is already present in
127149     ** the table. If so, the constraint has failed. */
127150     if( sqlite3_value_type(azData[2])!=SQLITE_NULL ){
127151       cell.iRowid = sqlite3_value_int64(azData[2]);
127152       if( sqlite3_value_type(azData[0])==SQLITE_NULL
127153        || sqlite3_value_int64(azData[0])!=cell.iRowid
127154       ){
127155         int steprc;
127156         sqlite3_bind_int64(pRtree->pReadRowid, 1, cell.iRowid);
127157         steprc = sqlite3_step(pRtree->pReadRowid);
127158         rc = sqlite3_reset(pRtree->pReadRowid);
127159         if( SQLITE_ROW==steprc ){
127160           if( sqlite3_vtab_on_conflict(pRtree->db)==SQLITE_REPLACE ){
127161             rc = rtreeDeleteRowid(pRtree, cell.iRowid);
127162           }else{
127163             rc = SQLITE_CONSTRAINT;
127164             goto constraint;
127165           }
127166         }
127167       }
127168       bHaveRowid = 1;
127169     }
127170   }
127171 
127172   /* If azData[0] is not an SQL NULL value, it is the rowid of a
127173   ** record to delete from the r-tree table. The following block does
127174   ** just that.
127175   */
127176   if( sqlite3_value_type(azData[0])!=SQLITE_NULL ){
127177     rc = rtreeDeleteRowid(pRtree, sqlite3_value_int64(azData[0]));
127178   }
127179 
127180   /* If the azData[] array contains more than one element, elements
127181   ** (azData[2]..azData[argc-1]) contain a new record to insert into
127182   ** the r-tree structure.
127183   */
127184   if( rc==SQLITE_OK && nData>1 ){
127185     /* Insert the new record into the r-tree */
127186     RtreeNode *pLeaf;
127187 
127188     /* Figure out the rowid of the new row. */
127189     if( bHaveRowid==0 ){
127190       rc = newRowid(pRtree, &cell.iRowid);
127191     }
127192     *pRowid = cell.iRowid;
127193 
127194     if( rc==SQLITE_OK ){
127195       rc = ChooseLeaf(pRtree, &cell, 0, &pLeaf);
127196     }
127197     if( rc==SQLITE_OK ){
127198       int rc2;
127199       pRtree->iReinsertHeight = -1;
127200       rc = rtreeInsertCell(pRtree, pLeaf, &cell, 0);
127201       rc2 = nodeRelease(pRtree, pLeaf);
127202       if( rc==SQLITE_OK ){
127203         rc = rc2;
127204       }
127205     }
127206   }
127207 
127208 constraint:
127209   rtreeRelease(pRtree);
127210   return rc;
127211 }
127212 
127213 /*
127214 ** The xRename method for rtree module virtual tables.
127215 */
127216 static int rtreeRename(sqlite3_vtab *pVtab, const char *zNewName){
127217   Rtree *pRtree = (Rtree *)pVtab;
127218   int rc = SQLITE_NOMEM;
127219   char *zSql = sqlite3_mprintf(
127220     "ALTER TABLE %Q.'%q_node'   RENAME TO \"%w_node\";"
127221     "ALTER TABLE %Q.'%q_parent' RENAME TO \"%w_parent\";"
127222     "ALTER TABLE %Q.'%q_rowid'  RENAME TO \"%w_rowid\";"
127223     , pRtree->zDb, pRtree->zName, zNewName
127224     , pRtree->zDb, pRtree->zName, zNewName
127225     , pRtree->zDb, pRtree->zName, zNewName
127226   );
127227   if( zSql ){
127228     rc = sqlite3_exec(pRtree->db, zSql, 0, 0, 0);
127229     sqlite3_free(zSql);
127230   }
127231   return rc;
127232 }
127233 
127234 static sqlite3_module rtreeModule = {
127235   0,                          /* iVersion */
127236   rtreeCreate,                /* xCreate - create a table */
127237   rtreeConnect,               /* xConnect - connect to an existing table */
127238   rtreeBestIndex,             /* xBestIndex - Determine search strategy */
127239   rtreeDisconnect,            /* xDisconnect - Disconnect from a table */
127240   rtreeDestroy,               /* xDestroy - Drop a table */
127241   rtreeOpen,                  /* xOpen - open a cursor */
127242   rtreeClose,                 /* xClose - close a cursor */
127243   rtreeFilter,                /* xFilter - configure scan constraints */
127244   rtreeNext,                  /* xNext - advance a cursor */
127245   rtreeEof,                   /* xEof */
127246   rtreeColumn,                /* xColumn - read data */
127247   rtreeRowid,                 /* xRowid - read data */
127248   rtreeUpdate,                /* xUpdate - write data */
127249   0,                          /* xBegin - begin transaction */
127250   0,                          /* xSync - sync transaction */
127251   0,                          /* xCommit - commit transaction */
127252   0,                          /* xRollback - rollback transaction */
127253   0,                          /* xFindFunction - function overloading */
127254   rtreeRename,                /* xRename - rename the table */
127255   0,                          /* xSavepoint */
127256   0,                          /* xRelease */
127257   0                           /* xRollbackTo */
127258 };
127259 
127260 static int rtreeSqlInit(
127261   Rtree *pRtree,
127262   sqlite3 *db,
127263   const char *zDb,
127264   const char *zPrefix,
127265   int isCreate
127266 ){
127267   int rc = SQLITE_OK;
127268 
127269   #define N_STATEMENT 9
127270   static const char *azSql[N_STATEMENT] = {
127271     /* Read and write the xxx_node table */
127272     "SELECT data FROM '%q'.'%q_node' WHERE nodeno = :1",
127273     "INSERT OR REPLACE INTO '%q'.'%q_node' VALUES(:1, :2)",
127274     "DELETE FROM '%q'.'%q_node' WHERE nodeno = :1",
127275 
127276     /* Read and write the xxx_rowid table */
127277     "SELECT nodeno FROM '%q'.'%q_rowid' WHERE rowid = :1",
127278     "INSERT OR REPLACE INTO '%q'.'%q_rowid' VALUES(:1, :2)",
127279     "DELETE FROM '%q'.'%q_rowid' WHERE rowid = :1",
127280 
127281     /* Read and write the xxx_parent table */
127282     "SELECT parentnode FROM '%q'.'%q_parent' WHERE nodeno = :1",
127283     "INSERT OR REPLACE INTO '%q'.'%q_parent' VALUES(:1, :2)",
127284     "DELETE FROM '%q'.'%q_parent' WHERE nodeno = :1"
127285   };
127286   sqlite3_stmt **appStmt[N_STATEMENT];
127287   int i;
127288 
127289   pRtree->db = db;
127290 
127291   if( isCreate ){
127292     char *zCreate = sqlite3_mprintf(
127293 "CREATE TABLE \"%w\".\"%w_node\"(nodeno INTEGER PRIMARY KEY, data BLOB);"
127294 "CREATE TABLE \"%w\".\"%w_rowid\"(rowid INTEGER PRIMARY KEY, nodeno INTEGER);"
127295 "CREATE TABLE \"%w\".\"%w_parent\"(nodeno INTEGER PRIMARY KEY, parentnode INTEGER);"
127296 "INSERT INTO '%q'.'%q_node' VALUES(1, zeroblob(%d))",
127297       zDb, zPrefix, zDb, zPrefix, zDb, zPrefix, zDb, zPrefix, pRtree->iNodeSize
127298     );
127299     if( !zCreate ){
127300       return SQLITE_NOMEM;
127301     }
127302     rc = sqlite3_exec(db, zCreate, 0, 0, 0);
127303     sqlite3_free(zCreate);
127304     if( rc!=SQLITE_OK ){
127305       return rc;
127306     }
127307   }
127308 
127309   appStmt[0] = &pRtree->pReadNode;
127310   appStmt[1] = &pRtree->pWriteNode;
127311   appStmt[2] = &pRtree->pDeleteNode;
127312   appStmt[3] = &pRtree->pReadRowid;
127313   appStmt[4] = &pRtree->pWriteRowid;
127314   appStmt[5] = &pRtree->pDeleteRowid;
127315   appStmt[6] = &pRtree->pReadParent;
127316   appStmt[7] = &pRtree->pWriteParent;
127317   appStmt[8] = &pRtree->pDeleteParent;
127318 
127319   for(i=0; i<N_STATEMENT && rc==SQLITE_OK; i++){
127320     char *zSql = sqlite3_mprintf(azSql[i], zDb, zPrefix);
127321     if( zSql ){
127322       rc = sqlite3_prepare_v2(db, zSql, -1, appStmt[i], 0);
127323     }else{
127324       rc = SQLITE_NOMEM;
127325     }
127326     sqlite3_free(zSql);
127327   }
127328 
127329   return rc;
127330 }
127331 
127332 /*
127333 ** The second argument to this function contains the text of an SQL statement
127334 ** that returns a single integer value. The statement is compiled and executed
127335 ** using database connection db. If successful, the integer value returned
127336 ** is written to *piVal and SQLITE_OK returned. Otherwise, an SQLite error
127337 ** code is returned and the value of *piVal after returning is not defined.
127338 */
127339 static int getIntFromStmt(sqlite3 *db, const char *zSql, int *piVal){
127340   int rc = SQLITE_NOMEM;
127341   if( zSql ){
127342     sqlite3_stmt *pStmt = 0;
127343     rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
127344     if( rc==SQLITE_OK ){
127345       if( SQLITE_ROW==sqlite3_step(pStmt) ){
127346         *piVal = sqlite3_column_int(pStmt, 0);
127347       }
127348       rc = sqlite3_finalize(pStmt);
127349     }
127350   }
127351   return rc;
127352 }
127353 
127354 /*
127355 ** This function is called from within the xConnect() or xCreate() method to
127356 ** determine the node-size used by the rtree table being created or connected
127357 ** to. If successful, pRtree->iNodeSize is populated and SQLITE_OK returned.
127358 ** Otherwise, an SQLite error code is returned.
127359 **
127360 ** If this function is being called as part of an xConnect(), then the rtree
127361 ** table already exists. In this case the node-size is determined by inspecting
127362 ** the root node of the tree.
127363 **
127364 ** Otherwise, for an xCreate(), use 64 bytes less than the database page-size.
127365 ** This ensures that each node is stored on a single database page. If the
127366 ** database page-size is so large that more than RTREE_MAXCELLS entries
127367 ** would fit in a single node, use a smaller node-size.
127368 */
127369 static int getNodeSize(
127370   sqlite3 *db,                    /* Database handle */
127371   Rtree *pRtree,                  /* Rtree handle */
127372   int isCreate                    /* True for xCreate, false for xConnect */
127373 ){
127374   int rc;
127375   char *zSql;
127376   if( isCreate ){
127377     int iPageSize = 0;
127378     zSql = sqlite3_mprintf("PRAGMA %Q.page_size", pRtree->zDb);
127379     rc = getIntFromStmt(db, zSql, &iPageSize);
127380     if( rc==SQLITE_OK ){
127381       pRtree->iNodeSize = iPageSize-64;
127382       if( (4+pRtree->nBytesPerCell*RTREE_MAXCELLS)<pRtree->iNodeSize ){
127383         pRtree->iNodeSize = 4+pRtree->nBytesPerCell*RTREE_MAXCELLS;
127384       }
127385     }
127386   }else{
127387     zSql = sqlite3_mprintf(
127388         "SELECT length(data) FROM '%q'.'%q_node' WHERE nodeno = 1",
127389         pRtree->zDb, pRtree->zName
127390     );
127391     rc = getIntFromStmt(db, zSql, &pRtree->iNodeSize);
127392   }
127393 
127394   sqlite3_free(zSql);
127395   return rc;
127396 }
127397 
127398 /*
127399 ** This function is the implementation of both the xConnect and xCreate
127400 ** methods of the r-tree virtual table.
127401 **
127402 **   argv[0]   -> module name
127403 **   argv[1]   -> database name
127404 **   argv[2]   -> table name
127405 **   argv[...] -> column names...
127406 */
127407 static int rtreeInit(
127408   sqlite3 *db,                        /* Database connection */
127409   void *pAux,                         /* One of the RTREE_COORD_* constants */
127410   int argc, const char *const*argv,   /* Parameters to CREATE TABLE statement */
127411   sqlite3_vtab **ppVtab,              /* OUT: New virtual table */
127412   char **pzErr,                       /* OUT: Error message, if any */
127413   int isCreate                        /* True for xCreate, false for xConnect */
127414 ){
127415   int rc = SQLITE_OK;
127416   Rtree *pRtree;
127417   int nDb;              /* Length of string argv[1] */
127418   int nName;            /* Length of string argv[2] */
127419   int eCoordType = (pAux ? RTREE_COORD_INT32 : RTREE_COORD_REAL32);
127420 
127421   const char *aErrMsg[] = {
127422     0,                                                    /* 0 */
127423     "Wrong number of columns for an rtree table",         /* 1 */
127424     "Too few columns for an rtree table",                 /* 2 */
127425     "Too many columns for an rtree table"                 /* 3 */
127426   };
127427 
127428   int iErr = (argc<6) ? 2 : argc>(RTREE_MAX_DIMENSIONS*2+4) ? 3 : argc%2;
127429   if( aErrMsg[iErr] ){
127430     *pzErr = sqlite3_mprintf("%s", aErrMsg[iErr]);
127431     return SQLITE_ERROR;
127432   }
127433 
127434   sqlite3_vtab_config(db, SQLITE_VTAB_CONSTRAINT_SUPPORT, 1);
127435 
127436   /* Allocate the sqlite3_vtab structure */
127437   nDb = strlen(argv[1]);
127438   nName = strlen(argv[2]);
127439   pRtree = (Rtree *)sqlite3_malloc(sizeof(Rtree)+nDb+nName+2);
127440   if( !pRtree ){
127441     return SQLITE_NOMEM;
127442   }
127443   memset(pRtree, 0, sizeof(Rtree)+nDb+nName+2);
127444   pRtree->nBusy = 1;
127445   pRtree->base.pModule = &rtreeModule;
127446   pRtree->zDb = (char *)&pRtree[1];
127447   pRtree->zName = &pRtree->zDb[nDb+1];
127448   pRtree->nDim = (argc-4)/2;
127449   pRtree->nBytesPerCell = 8 + pRtree->nDim*4*2;
127450   pRtree->eCoordType = eCoordType;
127451   memcpy(pRtree->zDb, argv[1], nDb);
127452   memcpy(pRtree->zName, argv[2], nName);
127453 
127454   /* Figure out the node size to use. */
127455   rc = getNodeSize(db, pRtree, isCreate);
127456 
127457   /* Create/Connect to the underlying relational database schema. If
127458   ** that is successful, call sqlite3_declare_vtab() to configure
127459   ** the r-tree table schema.
127460   */
127461   if( rc==SQLITE_OK ){
127462     if( (rc = rtreeSqlInit(pRtree, db, argv[1], argv[2], isCreate)) ){
127463       *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
127464     }else{
127465       char *zSql = sqlite3_mprintf("CREATE TABLE x(%s", argv[3]);
127466       char *zTmp;
127467       int ii;
127468       for(ii=4; zSql && ii<argc; ii++){
127469         zTmp = zSql;
127470         zSql = sqlite3_mprintf("%s, %s", zTmp, argv[ii]);
127471         sqlite3_free(zTmp);
127472       }
127473       if( zSql ){
127474         zTmp = zSql;
127475         zSql = sqlite3_mprintf("%s);", zTmp);
127476         sqlite3_free(zTmp);
127477       }
127478       if( !zSql ){
127479         rc = SQLITE_NOMEM;
127480       }else if( SQLITE_OK!=(rc = sqlite3_declare_vtab(db, zSql)) ){
127481         *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
127482       }
127483       sqlite3_free(zSql);
127484     }
127485   }
127486 
127487   if( rc==SQLITE_OK ){
127488     *ppVtab = (sqlite3_vtab *)pRtree;
127489   }else{
127490     rtreeRelease(pRtree);
127491   }
127492   return rc;
127493 }
127494 
127495 
127496 /*
127497 ** Implementation of a scalar function that decodes r-tree nodes to
127498 ** human readable strings. This can be used for debugging and analysis.
127499 **
127500 ** The scalar function takes two arguments, a blob of data containing
127501 ** an r-tree node, and the number of dimensions the r-tree indexes.
127502 ** For a two-dimensional r-tree structure called "rt", to deserialize
127503 ** all nodes, a statement like:
127504 **
127505 **   SELECT rtreenode(2, data) FROM rt_node;
127506 **
127507 ** The human readable string takes the form of a Tcl list with one
127508 ** entry for each cell in the r-tree node. Each entry is itself a
127509 ** list, containing the 8-byte rowid/pageno followed by the
127510 ** <num-dimension>*2 coordinates.
127511 */
127512 static void rtreenode(sqlite3_context *ctx, int nArg, sqlite3_value **apArg){
127513   char *zText = 0;
127514   RtreeNode node;
127515   Rtree tree;
127516   int ii;
127517 
127518   UNUSED_PARAMETER(nArg);
127519   memset(&node, 0, sizeof(RtreeNode));
127520   memset(&tree, 0, sizeof(Rtree));
127521   tree.nDim = sqlite3_value_int(apArg[0]);
127522   tree.nBytesPerCell = 8 + 8 * tree.nDim;
127523   node.zData = (u8 *)sqlite3_value_blob(apArg[1]);
127524 
127525   for(ii=0; ii<NCELL(&node); ii++){
127526     char zCell[512];
127527     int nCell = 0;
127528     RtreeCell cell;
127529     int jj;
127530 
127531     nodeGetCell(&tree, &node, ii, &cell);
127532     sqlite3_snprintf(512-nCell,&zCell[nCell],"%lld", cell.iRowid);
127533     nCell = strlen(zCell);
127534     for(jj=0; jj<tree.nDim*2; jj++){
127535       sqlite3_snprintf(512-nCell,&zCell[nCell]," %f",(double)cell.aCoord[jj].f);
127536       nCell = strlen(zCell);
127537     }
127538 
127539     if( zText ){
127540       char *zTextNew = sqlite3_mprintf("%s {%s}", zText, zCell);
127541       sqlite3_free(zText);
127542       zText = zTextNew;
127543     }else{
127544       zText = sqlite3_mprintf("{%s}", zCell);
127545     }
127546   }
127547 
127548   sqlite3_result_text(ctx, zText, -1, sqlite3_free);
127549 }
127550 
127551 static void rtreedepth(sqlite3_context *ctx, int nArg, sqlite3_value **apArg){
127552   UNUSED_PARAMETER(nArg);
127553   if( sqlite3_value_type(apArg[0])!=SQLITE_BLOB
127554    || sqlite3_value_bytes(apArg[0])<2
127555   ){
127556     sqlite3_result_error(ctx, "Invalid argument to rtreedepth()", -1);
127557   }else{
127558     u8 *zBlob = (u8 *)sqlite3_value_blob(apArg[0]);
127559     sqlite3_result_int(ctx, readInt16(zBlob));
127560   }
127561 }
127562 
127563 /*
127564 ** Register the r-tree module with database handle db. This creates the
127565 ** virtual table module "rtree" and the debugging/analysis scalar
127566 ** function "rtreenode".
127567 */
127568 SQLITE_PRIVATE int sqlite3RtreeInit(sqlite3 *db){
127569   const int utf8 = SQLITE_UTF8;
127570   int rc;
127571 
127572   rc = sqlite3_create_function(db, "rtreenode", 2, utf8, 0, rtreenode, 0, 0);
127573   if( rc==SQLITE_OK ){
127574     rc = sqlite3_create_function(db, "rtreedepth", 1, utf8, 0,rtreedepth, 0, 0);
127575   }
127576   if( rc==SQLITE_OK ){
127577     void *c = (void *)RTREE_COORD_REAL32;
127578     rc = sqlite3_create_module_v2(db, "rtree", &rtreeModule, c, 0);
127579   }
127580   if( rc==SQLITE_OK ){
127581     void *c = (void *)RTREE_COORD_INT32;
127582     rc = sqlite3_create_module_v2(db, "rtree_i32", &rtreeModule, c, 0);
127583   }
127584 
127585   return rc;
127586 }
127587 
127588 /*
127589 ** A version of sqlite3_free() that can be used as a callback. This is used
127590 ** in two places - as the destructor for the blob value returned by the
127591 ** invocation of a geometry function, and as the destructor for the geometry
127592 ** functions themselves.
127593 */
127594 static void doSqlite3Free(void *p){
127595   sqlite3_free(p);
127596 }
127597 
127598 /*
127599 ** Each call to sqlite3_rtree_geometry_callback() creates an ordinary SQLite
127600 ** scalar user function. This C function is the callback used for all such
127601 ** registered SQL functions.
127602 **
127603 ** The scalar user functions return a blob that is interpreted by r-tree
127604 ** table MATCH operators.
127605 */
127606 static void geomCallback(sqlite3_context *ctx, int nArg, sqlite3_value **aArg){
127607   RtreeGeomCallback *pGeomCtx = (RtreeGeomCallback *)sqlite3_user_data(ctx);
127608   RtreeMatchArg *pBlob;
127609   int nBlob;
127610 
127611   nBlob = sizeof(RtreeMatchArg) + (nArg-1)*sizeof(double);
127612   pBlob = (RtreeMatchArg *)sqlite3_malloc(nBlob);
127613   if( !pBlob ){
127614     sqlite3_result_error_nomem(ctx);
127615   }else{
127616     int i;
127617     pBlob->magic = RTREE_GEOMETRY_MAGIC;
127618     pBlob->xGeom = pGeomCtx->xGeom;
127619     pBlob->pContext = pGeomCtx->pContext;
127620     pBlob->nParam = nArg;
127621     for(i=0; i<nArg; i++){
127622       pBlob->aParam[i] = sqlite3_value_double(aArg[i]);
127623     }
127624     sqlite3_result_blob(ctx, pBlob, nBlob, doSqlite3Free);
127625   }
127626 }
127627 
127628 /*
127629 ** Register a new geometry function for use with the r-tree MATCH operator.
127630 */
127631 SQLITE_API int sqlite3_rtree_geometry_callback(
127632   sqlite3 *db,
127633   const char *zGeom,
127634   int (*xGeom)(sqlite3_rtree_geometry *, int, double *, int *),
127635   void *pContext
127636 ){
127637   RtreeGeomCallback *pGeomCtx;      /* Context object for new user-function */
127638 
127639   /* Allocate and populate the context object. */
127640   pGeomCtx = (RtreeGeomCallback *)sqlite3_malloc(sizeof(RtreeGeomCallback));
127641   if( !pGeomCtx ) return SQLITE_NOMEM;
127642   pGeomCtx->xGeom = xGeom;
127643   pGeomCtx->pContext = pContext;
127644 
127645   /* Create the new user-function. Register a destructor function to delete
127646   ** the context object when it is no longer required.  */
127647   return sqlite3_create_function_v2(db, zGeom, -1, SQLITE_ANY,
127648       (void *)pGeomCtx, geomCallback, 0, 0, doSqlite3Free
127649   );
127650 }
127651 
127652 #if !SQLITE_CORE
127653 SQLITE_API int sqlite3_extension_init(
127654   sqlite3 *db,
127655   char **pzErrMsg,
127656   const sqlite3_api_routines *pApi
127657 ){
127658   SQLITE_EXTENSION_INIT2(pApi)
127659   return sqlite3RtreeInit(db);
127660 }
127661 #endif
127662 
127663 #endif
127664 
127665 /************** End of rtree.c ***********************************************/
127666 /************** Begin file icu.c *********************************************/
127667 /*
127668 ** 2007 May 6
127669 **
127670 ** The author disclaims copyright to this source code.  In place of
127671 ** a legal notice, here is a blessing:
127672 **
127673 **    May you do good and not evil.
127674 **    May you find forgiveness for yourself and forgive others.
127675 **    May you share freely, never taking more than you give.
127676 **
127677 *************************************************************************
127678 ** $Id$
127679 **
127680 ** This file implements an integration between the ICU library
127681 ** ("International Components for Unicode", an open-source library
127682 ** for handling unicode data) and SQLite. The integration uses
127683 ** ICU to provide the following to SQLite:
127684 **
127685 **   * An implementation of the SQL regexp() function (and hence REGEXP
127686 **     operator) using the ICU uregex_XX() APIs.
127687 **
127688 **   * Implementations of the SQL scalar upper() and lower() functions
127689 **     for case mapping.
127690 **
127691 **   * Integration of ICU and SQLite collation seqences.
127692 **
127693 **   * An implementation of the LIKE operator that uses ICU to
127694 **     provide case-independent matching.
127695 */
127696 
127697 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_ICU)
127698 
127699 /* Include ICU headers */
127700 #include <unicode/utypes.h>
127701 #include <unicode/uregex.h>
127702 #include <unicode/ustring.h>
127703 #include <unicode/ucol.h>
127704 
127705 
127706 #ifndef SQLITE_CORE
127707   SQLITE_EXTENSION_INIT1
127708 #else
127709 #endif
127710 
127711 /*
127712 ** Maximum length (in bytes) of the pattern in a LIKE or GLOB
127713 ** operator.
127714 */
127715 #ifndef SQLITE_MAX_LIKE_PATTERN_LENGTH
127716 # define SQLITE_MAX_LIKE_PATTERN_LENGTH 50000
127717 #endif
127718 
127719 /*
127720 ** Version of sqlite3_free() that is always a function, never a macro.
127721 */
127722 static void xFree(void *p){
127723   sqlite3_free(p);
127724 }
127725 
127726 /*
127727 ** Compare two UTF-8 strings for equality where the first string is
127728 ** a "LIKE" expression. Return true (1) if they are the same and
127729 ** false (0) if they are different.
127730 */
127731 static int icuLikeCompare(
127732   const uint8_t *zPattern,   /* LIKE pattern */
127733   const uint8_t *zString,    /* The UTF-8 string to compare against */
127734   const UChar32 uEsc         /* The escape character */
127735 ){
127736   static const int MATCH_ONE = (UChar32)'_';
127737   static const int MATCH_ALL = (UChar32)'%';
127738 
127739   int iPattern = 0;       /* Current byte index in zPattern */
127740   int iString = 0;        /* Current byte index in zString */
127741 
127742   int prevEscape = 0;     /* True if the previous character was uEsc */
127743 
127744   while( zPattern[iPattern]!=0 ){
127745 
127746     /* Read (and consume) the next character from the input pattern. */
127747     UChar32 uPattern;
127748     U8_NEXT_UNSAFE(zPattern, iPattern, uPattern);
127749     assert(uPattern!=0);
127750 
127751     /* There are now 4 possibilities:
127752     **
127753     **     1. uPattern is an unescaped match-all character "%",
127754     **     2. uPattern is an unescaped match-one character "_",
127755     **     3. uPattern is an unescaped escape character, or
127756     **     4. uPattern is to be handled as an ordinary character
127757     */
127758     if( !prevEscape && uPattern==MATCH_ALL ){
127759       /* Case 1. */
127760       uint8_t c;
127761 
127762       /* Skip any MATCH_ALL or MATCH_ONE characters that follow a
127763       ** MATCH_ALL. For each MATCH_ONE, skip one character in the
127764       ** test string.
127765       */
127766       while( (c=zPattern[iPattern]) == MATCH_ALL || c == MATCH_ONE ){
127767         if( c==MATCH_ONE ){
127768           if( zString[iString]==0 ) return 0;
127769           U8_FWD_1_UNSAFE(zString, iString);
127770         }
127771         iPattern++;
127772       }
127773 
127774       if( zPattern[iPattern]==0 ) return 1;
127775 
127776       while( zString[iString] ){
127777         if( icuLikeCompare(&zPattern[iPattern], &zString[iString], uEsc) ){
127778           return 1;
127779         }
127780         U8_FWD_1_UNSAFE(zString, iString);
127781       }
127782       return 0;
127783 
127784     }else if( !prevEscape && uPattern==MATCH_ONE ){
127785       /* Case 2. */
127786       if( zString[iString]==0 ) return 0;
127787       U8_FWD_1_UNSAFE(zString, iString);
127788 
127789     }else if( !prevEscape && uPattern==uEsc){
127790       /* Case 3. */
127791       prevEscape = 1;
127792 
127793     }else{
127794       /* Case 4. */
127795       UChar32 uString;
127796       U8_NEXT_UNSAFE(zString, iString, uString);
127797       uString = u_foldCase(uString, U_FOLD_CASE_DEFAULT);
127798       uPattern = u_foldCase(uPattern, U_FOLD_CASE_DEFAULT);
127799       if( uString!=uPattern ){
127800         return 0;
127801       }
127802       prevEscape = 0;
127803     }
127804   }
127805 
127806   return zString[iString]==0;
127807 }
127808 
127809 /*
127810 ** Implementation of the like() SQL function.  This function implements
127811 ** the build-in LIKE operator.  The first argument to the function is the
127812 ** pattern and the second argument is the string.  So, the SQL statements:
127813 **
127814 **       A LIKE B
127815 **
127816 ** is implemented as like(B, A). If there is an escape character E,
127817 **
127818 **       A LIKE B ESCAPE E
127819 **
127820 ** is mapped to like(B, A, E).
127821 */
127822 static void icuLikeFunc(
127823   sqlite3_context *context,
127824   int argc,
127825   sqlite3_value **argv
127826 ){
127827   const unsigned char *zA = sqlite3_value_text(argv[0]);
127828   const unsigned char *zB = sqlite3_value_text(argv[1]);
127829   UChar32 uEsc = 0;
127830 
127831   /* Limit the length of the LIKE or GLOB pattern to avoid problems
127832   ** of deep recursion and N*N behavior in patternCompare().
127833   */
127834   if( sqlite3_value_bytes(argv[0])>SQLITE_MAX_LIKE_PATTERN_LENGTH ){
127835     sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1);
127836     return;
127837   }
127838 
127839 
127840   if( argc==3 ){
127841     /* The escape character string must consist of a single UTF-8 character.
127842     ** Otherwise, return an error.
127843     */
127844     int nE= sqlite3_value_bytes(argv[2]);
127845     const unsigned char *zE = sqlite3_value_text(argv[2]);
127846     int i = 0;
127847     if( zE==0 ) return;
127848     U8_NEXT(zE, i, nE, uEsc);
127849     if( i!=nE){
127850       sqlite3_result_error(context,
127851           "ESCAPE expression must be a single character", -1);
127852       return;
127853     }
127854   }
127855 
127856   if( zA && zB ){
127857     sqlite3_result_int(context, icuLikeCompare(zA, zB, uEsc));
127858   }
127859 }
127860 
127861 /*
127862 ** This function is called when an ICU function called from within
127863 ** the implementation of an SQL scalar function returns an error.
127864 **
127865 ** The scalar function context passed as the first argument is
127866 ** loaded with an error message based on the following two args.
127867 */
127868 static void icuFunctionError(
127869   sqlite3_context *pCtx,       /* SQLite scalar function context */
127870   const char *zName,           /* Name of ICU function that failed */
127871   UErrorCode e                 /* Error code returned by ICU function */
127872 ){
127873   char zBuf[128];
127874   sqlite3_snprintf(128, zBuf, "ICU error: %s(): %s", zName, u_errorName(e));
127875   zBuf[127] = '\0';
127876   sqlite3_result_error(pCtx, zBuf, -1);
127877 }
127878 
127879 /*
127880 ** Function to delete compiled regexp objects. Registered as
127881 ** a destructor function with sqlite3_set_auxdata().
127882 */
127883 static void icuRegexpDelete(void *p){
127884   URegularExpression *pExpr = (URegularExpression *)p;
127885   uregex_close(pExpr);
127886 }
127887 
127888 /*
127889 ** Implementation of SQLite REGEXP operator. This scalar function takes
127890 ** two arguments. The first is a regular expression pattern to compile
127891 ** the second is a string to match against that pattern. If either
127892 ** argument is an SQL NULL, then NULL Is returned. Otherwise, the result
127893 ** is 1 if the string matches the pattern, or 0 otherwise.
127894 **
127895 ** SQLite maps the regexp() function to the regexp() operator such
127896 ** that the following two are equivalent:
127897 **
127898 **     zString REGEXP zPattern
127899 **     regexp(zPattern, zString)
127900 **
127901 ** Uses the following ICU regexp APIs:
127902 **
127903 **     uregex_open()
127904 **     uregex_matches()
127905 **     uregex_close()
127906 */
127907 static void icuRegexpFunc(sqlite3_context *p, int nArg, sqlite3_value **apArg){
127908   UErrorCode status = U_ZERO_ERROR;
127909   URegularExpression *pExpr;
127910   UBool res;
127911   const UChar *zString = sqlite3_value_text16(apArg[1]);
127912 
127913   (void)nArg;  /* Unused parameter */
127914 
127915   /* If the left hand side of the regexp operator is NULL,
127916   ** then the result is also NULL.
127917   */
127918   if( !zString ){
127919     return;
127920   }
127921 
127922   pExpr = sqlite3_get_auxdata(p, 0);
127923   if( !pExpr ){
127924     const UChar *zPattern = sqlite3_value_text16(apArg[0]);
127925     if( !zPattern ){
127926       return;
127927     }
127928     pExpr = uregex_open(zPattern, -1, 0, 0, &status);
127929 
127930     if( U_SUCCESS(status) ){
127931       sqlite3_set_auxdata(p, 0, pExpr, icuRegexpDelete);
127932     }else{
127933       assert(!pExpr);
127934       icuFunctionError(p, "uregex_open", status);
127935       return;
127936     }
127937   }
127938 
127939   /* Configure the text that the regular expression operates on. */
127940   uregex_setText(pExpr, zString, -1, &status);
127941   if( !U_SUCCESS(status) ){
127942     icuFunctionError(p, "uregex_setText", status);
127943     return;
127944   }
127945 
127946   /* Attempt the match */
127947   res = uregex_matches(pExpr, 0, &status);
127948   if( !U_SUCCESS(status) ){
127949     icuFunctionError(p, "uregex_matches", status);
127950     return;
127951   }
127952 
127953   /* Set the text that the regular expression operates on to a NULL
127954   ** pointer. This is not really necessary, but it is tidier than
127955   ** leaving the regular expression object configured with an invalid
127956   ** pointer after this function returns.
127957   */
127958   uregex_setText(pExpr, 0, 0, &status);
127959 
127960   /* Return 1 or 0. */
127961   sqlite3_result_int(p, res ? 1 : 0);
127962 }
127963 
127964 /*
127965 ** Implementations of scalar functions for case mapping - upper() and
127966 ** lower(). Function upper() converts its input to upper-case (ABC).
127967 ** Function lower() converts to lower-case (abc).
127968 **
127969 ** ICU provides two types of case mapping, "general" case mapping and
127970 ** "language specific". Refer to ICU documentation for the differences
127971 ** between the two.
127972 **
127973 ** To utilise "general" case mapping, the upper() or lower() scalar
127974 ** functions are invoked with one argument:
127975 **
127976 **     upper('ABC') -> 'abc'
127977 **     lower('abc') -> 'ABC'
127978 **
127979 ** To access ICU "language specific" case mapping, upper() or lower()
127980 ** should be invoked with two arguments. The second argument is the name
127981 ** of the locale to use. Passing an empty string ("") or SQL NULL value
127982 ** as the second argument is the same as invoking the 1 argument version
127983 ** of upper() or lower().
127984 **
127985 **     lower('I', 'en_us') -> 'i'
127986 **     lower('I', 'tr_tr') -> 'ı' (small dotless i)
127987 **
127988 ** http://www.icu-project.org/userguide/posix.html#case_mappings
127989 */
127990 static void icuCaseFunc16(sqlite3_context *p, int nArg, sqlite3_value **apArg){
127991   const UChar *zInput;
127992   UChar *zOutput;
127993   int nInput;
127994   int nOutput;
127995 
127996   UErrorCode status = U_ZERO_ERROR;
127997   const char *zLocale = 0;
127998 
127999   assert(nArg==1 || nArg==2);
128000   if( nArg==2 ){
128001     zLocale = (const char *)sqlite3_value_text(apArg[1]);
128002   }
128003 
128004   zInput = sqlite3_value_text16(apArg[0]);
128005   if( !zInput ){
128006     return;
128007   }
128008   nInput = sqlite3_value_bytes16(apArg[0]);
128009 
128010   nOutput = nInput * 2 + 2;
128011   zOutput = sqlite3_malloc(nOutput);
128012   if( !zOutput ){
128013     return;
128014   }
128015 
128016   if( sqlite3_user_data(p) ){
128017     u_strToUpper(zOutput, nOutput/2, zInput, nInput/2, zLocale, &status);
128018   }else{
128019     u_strToLower(zOutput, nOutput/2, zInput, nInput/2, zLocale, &status);
128020   }
128021 
128022   if( !U_SUCCESS(status) ){
128023     icuFunctionError(p, "u_strToLower()/u_strToUpper", status);
128024     return;
128025   }
128026 
128027   sqlite3_result_text16(p, zOutput, -1, xFree);
128028 }
128029 
128030 /*
128031 ** Collation sequence destructor function. The pCtx argument points to
128032 ** a UCollator structure previously allocated using ucol_open().
128033 */
128034 static void icuCollationDel(void *pCtx){
128035   UCollator *p = (UCollator *)pCtx;
128036   ucol_close(p);
128037 }
128038 
128039 /*
128040 ** Collation sequence comparison function. The pCtx argument points to
128041 ** a UCollator structure previously allocated using ucol_open().
128042 */
128043 static int icuCollationColl(
128044   void *pCtx,
128045   int nLeft,
128046   const void *zLeft,
128047   int nRight,
128048   const void *zRight
128049 ){
128050   UCollationResult res;
128051   UCollator *p = (UCollator *)pCtx;
128052   res = ucol_strcoll(p, (UChar *)zLeft, nLeft/2, (UChar *)zRight, nRight/2);
128053   switch( res ){
128054     case UCOL_LESS:    return -1;
128055     case UCOL_GREATER: return +1;
128056     case UCOL_EQUAL:   return 0;
128057   }
128058   assert(!"Unexpected return value from ucol_strcoll()");
128059   return 0;
128060 }
128061 
128062 /*
128063 ** Implementation of the scalar function icu_load_collation().
128064 **
128065 ** This scalar function is used to add ICU collation based collation
128066 ** types to an SQLite database connection. It is intended to be called
128067 ** as follows:
128068 **
128069 **     SELECT icu_load_collation(<locale>, <collation-name>);
128070 **
128071 ** Where <locale> is a string containing an ICU locale identifier (i.e.
128072 ** "en_AU", "tr_TR" etc.) and <collation-name> is the name of the
128073 ** collation sequence to create.
128074 */
128075 static void icuLoadCollation(
128076   sqlite3_context *p,
128077   int nArg,
128078   sqlite3_value **apArg
128079 ){
128080   sqlite3 *db = (sqlite3 *)sqlite3_user_data(p);
128081   UErrorCode status = U_ZERO_ERROR;
128082   const char *zLocale;      /* Locale identifier - (eg. "jp_JP") */
128083   const char *zName;        /* SQL Collation sequence name (eg. "japanese") */
128084   UCollator *pUCollator;    /* ICU library collation object */
128085   int rc;                   /* Return code from sqlite3_create_collation_x() */
128086 
128087   assert(nArg==2);
128088   zLocale = (const char *)sqlite3_value_text(apArg[0]);
128089   zName = (const char *)sqlite3_value_text(apArg[1]);
128090 
128091   if( !zLocale || !zName ){
128092     return;
128093   }
128094 
128095   pUCollator = ucol_open(zLocale, &status);
128096   if( !U_SUCCESS(status) ){
128097     icuFunctionError(p, "ucol_open", status);
128098     return;
128099   }
128100   assert(p);
128101 
128102   rc = sqlite3_create_collation_v2(db, zName, SQLITE_UTF16, (void *)pUCollator,
128103       icuCollationColl, icuCollationDel
128104   );
128105   if( rc!=SQLITE_OK ){
128106     ucol_close(pUCollator);
128107     sqlite3_result_error(p, "Error registering collation function", -1);
128108   }
128109 }
128110 
128111 /*
128112 ** Register the ICU extension functions with database db.
128113 */
128114 SQLITE_PRIVATE int sqlite3IcuInit(sqlite3 *db){
128115   struct IcuScalar {
128116     const char *zName;                        /* Function name */
128117     int nArg;                                 /* Number of arguments */
128118     int enc;                                  /* Optimal text encoding */
128119     void *pContext;                           /* sqlite3_user_data() context */
128120     void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
128121   } scalars[] = {
128122     {"regexp", 2, SQLITE_ANY,          0, icuRegexpFunc},
128123 
128124     {"lower",  1, SQLITE_UTF16,        0, icuCaseFunc16},
128125     {"lower",  2, SQLITE_UTF16,        0, icuCaseFunc16},
128126     {"upper",  1, SQLITE_UTF16, (void*)1, icuCaseFunc16},
128127     {"upper",  2, SQLITE_UTF16, (void*)1, icuCaseFunc16},
128128 
128129     {"lower",  1, SQLITE_UTF8,         0, icuCaseFunc16},
128130     {"lower",  2, SQLITE_UTF8,         0, icuCaseFunc16},
128131     {"upper",  1, SQLITE_UTF8,  (void*)1, icuCaseFunc16},
128132     {"upper",  2, SQLITE_UTF8,  (void*)1, icuCaseFunc16},
128133 
128134     {"like",   2, SQLITE_UTF8,         0, icuLikeFunc},
128135     {"like",   3, SQLITE_UTF8,         0, icuLikeFunc},
128136 
128137     {"icu_load_collation",  2, SQLITE_UTF8, (void*)db, icuLoadCollation},
128138   };
128139 
128140   int rc = SQLITE_OK;
128141   int i;
128142 
128143   for(i=0; rc==SQLITE_OK && i<(int)(sizeof(scalars)/sizeof(scalars[0])); i++){
128144     struct IcuScalar *p = &scalars[i];
128145     rc = sqlite3_create_function(
128146         db, p->zName, p->nArg, p->enc, p->pContext, p->xFunc, 0, 0
128147     );
128148   }
128149 
128150   return rc;
128151 }
128152 
128153 #if !SQLITE_CORE
128154 SQLITE_API int sqlite3_extension_init(
128155   sqlite3 *db,
128156   char **pzErrMsg,
128157   const sqlite3_api_routines *pApi
128158 ){
128159   SQLITE_EXTENSION_INIT2(pApi)
128160   return sqlite3IcuInit(db);
128161 }
128162 #endif
128163 
128164 #endif
128165 
128166 /************** End of icu.c *************************************************/
128167 /************** Begin file fts3_icu.c ****************************************/
128168 /*
128169 ** 2007 June 22
128170 **
128171 ** The author disclaims copyright to this source code.  In place of
128172 ** a legal notice, here is a blessing:
128173 **
128174 **    May you do good and not evil.
128175 **    May you find forgiveness for yourself and forgive others.
128176 **    May you share freely, never taking more than you give.
128177 **
128178 *************************************************************************
128179 ** This file implements a tokenizer for fts3 based on the ICU library.
128180 */
128181 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
128182 #ifdef SQLITE_ENABLE_ICU
128183 
128184 
128185 #include <unicode/ubrk.h>
128186 #include <unicode/utf16.h>
128187 
128188 typedef struct IcuTokenizer IcuTokenizer;
128189 typedef struct IcuCursor IcuCursor;
128190 
128191 struct IcuTokenizer {
128192   sqlite3_tokenizer base;
128193   char *zLocale;
128194 };
128195 
128196 struct IcuCursor {
128197   sqlite3_tokenizer_cursor base;
128198 
128199   UBreakIterator *pIter;      /* ICU break-iterator object */
128200   int nChar;                  /* Number of UChar elements in pInput */
128201   UChar *aChar;               /* Copy of input using utf-16 encoding */
128202   int *aOffset;               /* Offsets of each character in utf-8 input */
128203 
128204   int nBuffer;
128205   char *zBuffer;
128206 
128207   int iToken;
128208 };
128209 
128210 /*
128211 ** Create a new tokenizer instance.
128212 */
128213 static int icuCreate(
128214   int argc,                            /* Number of entries in argv[] */
128215   const char * const *argv,            /* Tokenizer creation arguments */
128216   sqlite3_tokenizer **ppTokenizer      /* OUT: Created tokenizer */
128217 ){
128218   IcuTokenizer *p;
128219   int n = 0;
128220 
128221   if( argc>0 ){
128222     n = strlen(argv[0])+1;
128223   }
128224   p = (IcuTokenizer *)sqlite3_malloc(sizeof(IcuTokenizer)+n);
128225   if( !p ){
128226     return SQLITE_NOMEM;
128227   }
128228   memset(p, 0, sizeof(IcuTokenizer));
128229 
128230   if( n ){
128231     p->zLocale = (char *)&p[1];
128232     memcpy(p->zLocale, argv[0], n);
128233   }
128234 
128235   *ppTokenizer = (sqlite3_tokenizer *)p;
128236 
128237   return SQLITE_OK;
128238 }
128239 
128240 /*
128241 ** Destroy a tokenizer
128242 */
128243 static int icuDestroy(sqlite3_tokenizer *pTokenizer){
128244   IcuTokenizer *p = (IcuTokenizer *)pTokenizer;
128245   sqlite3_free(p);
128246   return SQLITE_OK;
128247 }
128248 
128249 /*
128250 ** Prepare to begin tokenizing a particular string.  The input
128251 ** string to be tokenized is pInput[0..nBytes-1].  A cursor
128252 ** used to incrementally tokenize this string is returned in
128253 ** *ppCursor.
128254 */
128255 static int icuOpen(
128256   sqlite3_tokenizer *pTokenizer,         /* The tokenizer */
128257   const char *zInput,                    /* Input string */
128258   int nInput,                            /* Length of zInput in bytes */
128259   sqlite3_tokenizer_cursor **ppCursor    /* OUT: Tokenization cursor */
128260 ){
128261   IcuTokenizer *p = (IcuTokenizer *)pTokenizer;
128262   IcuCursor *pCsr;
128263 
128264   const int32_t opt = U_FOLD_CASE_DEFAULT;
128265   UErrorCode status = U_ZERO_ERROR;
128266   int nChar;
128267 
128268   UChar32 c;
128269   int iInput = 0;
128270   int iOut = 0;
128271 
128272   *ppCursor = 0;
128273 
128274   if( nInput<0 ){
128275     nInput = strlen(zInput);
128276   }
128277   nChar = nInput+1;
128278   pCsr = (IcuCursor *)sqlite3_malloc(
128279       sizeof(IcuCursor) +                /* IcuCursor */
128280       nChar * sizeof(UChar) +            /* IcuCursor.aChar[] */
128281       (nChar+1) * sizeof(int)            /* IcuCursor.aOffset[] */
128282   );
128283   if( !pCsr ){
128284     return SQLITE_NOMEM;
128285   }
128286   memset(pCsr, 0, sizeof(IcuCursor));
128287   pCsr->aChar = (UChar *)&pCsr[1];
128288   pCsr->aOffset = (int *)&pCsr->aChar[nChar];
128289 
128290   pCsr->aOffset[iOut] = iInput;
128291   U8_NEXT(zInput, iInput, nInput, c);
128292   while( c>0 ){
128293     int isError = 0;
128294     c = u_foldCase(c, opt);
128295     U16_APPEND(pCsr->aChar, iOut, nChar, c, isError);
128296     if( isError ){
128297       sqlite3_free(pCsr);
128298       return SQLITE_ERROR;
128299     }
128300     pCsr->aOffset[iOut] = iInput;
128301 
128302     if( iInput<nInput ){
128303       U8_NEXT(zInput, iInput, nInput, c);
128304     }else{
128305       c = 0;
128306     }
128307   }
128308 
128309   pCsr->pIter = ubrk_open(UBRK_WORD, p->zLocale, pCsr->aChar, iOut, &status);
128310   if( !U_SUCCESS(status) ){
128311     sqlite3_free(pCsr);
128312     return SQLITE_ERROR;
128313   }
128314   pCsr->nChar = iOut;
128315 
128316   ubrk_first(pCsr->pIter);
128317   *ppCursor = (sqlite3_tokenizer_cursor *)pCsr;
128318   return SQLITE_OK;
128319 }
128320 
128321 /*
128322 ** Close a tokenization cursor previously opened by a call to icuOpen().
128323 */
128324 static int icuClose(sqlite3_tokenizer_cursor *pCursor){
128325   IcuCursor *pCsr = (IcuCursor *)pCursor;
128326   ubrk_close(pCsr->pIter);
128327   sqlite3_free(pCsr->zBuffer);
128328   sqlite3_free(pCsr);
128329   return SQLITE_OK;
128330 }
128331 
128332 /*
128333 ** Extract the next token from a tokenization cursor.
128334 */
128335 static int icuNext(
128336   sqlite3_tokenizer_cursor *pCursor,  /* Cursor returned by simpleOpen */
128337   const char **ppToken,               /* OUT: *ppToken is the token text */
128338   int *pnBytes,                       /* OUT: Number of bytes in token */
128339   int *piStartOffset,                 /* OUT: Starting offset of token */
128340   int *piEndOffset,                   /* OUT: Ending offset of token */
128341   int *piPosition                     /* OUT: Position integer of token */
128342 ){
128343   IcuCursor *pCsr = (IcuCursor *)pCursor;
128344 
128345   int iStart = 0;
128346   int iEnd = 0;
128347   int nByte = 0;
128348 
128349   while( iStart==iEnd ){
128350     UChar32 c;
128351 
128352     iStart = ubrk_current(pCsr->pIter);
128353     iEnd = ubrk_next(pCsr->pIter);
128354     if( iEnd==UBRK_DONE ){
128355       return SQLITE_DONE;
128356     }
128357 
128358     while( iStart<iEnd ){
128359       int iWhite = iStart;
128360       U8_NEXT(pCsr->aChar, iWhite, pCsr->nChar, c);
128361       if( u_isspace(c) ){
128362         iStart = iWhite;
128363       }else{
128364         break;
128365       }
128366     }
128367     assert(iStart<=iEnd);
128368   }
128369 
128370   do {
128371     UErrorCode status = U_ZERO_ERROR;
128372     if( nByte ){
128373       char *zNew = sqlite3_realloc(pCsr->zBuffer, nByte);
128374       if( !zNew ){
128375         return SQLITE_NOMEM;
128376       }
128377       pCsr->zBuffer = zNew;
128378       pCsr->nBuffer = nByte;
128379     }
128380 
128381     u_strToUTF8(
128382         pCsr->zBuffer, pCsr->nBuffer, &nByte,    /* Output vars */
128383         &pCsr->aChar[iStart], iEnd-iStart,       /* Input vars */
128384         &status                                  /* Output success/failure */
128385     );
128386   } while( nByte>pCsr->nBuffer );
128387 
128388   *ppToken = pCsr->zBuffer;
128389   *pnBytes = nByte;
128390   *piStartOffset = pCsr->aOffset[iStart];
128391   *piEndOffset = pCsr->aOffset[iEnd];
128392   *piPosition = pCsr->iToken++;
128393 
128394   return SQLITE_OK;
128395 }
128396 
128397 /*
128398 ** The set of routines that implement the simple tokenizer
128399 */
128400 static const sqlite3_tokenizer_module icuTokenizerModule = {
128401   0,                           /* iVersion */
128402   icuCreate,                   /* xCreate  */
128403   icuDestroy,                  /* xCreate  */
128404   icuOpen,                     /* xOpen    */
128405   icuClose,                    /* xClose   */
128406   icuNext,                     /* xNext    */
128407 };
128408 
128409 /*
128410 ** Set *ppModule to point at the implementation of the ICU tokenizer.
128411 */
128412 SQLITE_PRIVATE void sqlite3Fts3IcuTokenizerModule(
128413   sqlite3_tokenizer_module const**ppModule
128414 ){
128415   *ppModule = &icuTokenizerModule;
128416 }
128417 
128418 #endif /* defined(SQLITE_ENABLE_ICU) */
128419 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
128420 
128421 /************** End of fts3_icu.c ********************************************/
128422 
128423 #if defined(_MSC_VER) && _MSC_VER < 1300
128424 #pragma optimize("", on)
128425 #endif
128426